Add support for unary - and bitcast operator.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
21 #include "BytecodeEmitter.h"
22 #include "../Strings.h"
23 #include "../Heap-inl.h"
24 #include "../Handles-inl.h"
28 class HIRTranslator : public HIRVisitor
31 HIRTranslator(Zone *zone, BytecodeEmitter &emitter)
37 void translate(HIR *hir) {
41 void visitBoolean(HBoolean *hir);
42 void visitFloat(HFloat *hir);
43 void visitInteger(HInteger *hir);
44 void visitArray(HArray *hir);
45 void visitCall(HCall *call);
46 void visitStore(HStore *store);
47 void visitUnary(HUnary *unary);
48 void visitBinary(HBinary *binary);
49 void visitLocal(HLocal *local);
50 void visitGlobal(HGlobal *global);
51 void visitConvert(HConvert *convert);
52 void visitIndex(HIndex *index);
53 void visitPostIncDec(HPostIncDec *hir);
54 void visitToRef(HToRef *hir);
58 BytecodeEmitter &emitter_;
64 HIRTranslator::visitBoolean(HBoolean *hir)
73 HIRTranslator::visitFloat(HFloat *hir)
75 __ float_(hir->value());
79 HIRTranslator::visitInteger(HInteger *hir)
81 __ int_(hir->value());
85 HIRTranslator::visitArray(HArray *hir)
87 __ object(hir->array());
94 HIRTranslator::visitCall(HCall *call)
96 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
98 assert(call->args()->length() == fun->parameters()->length());
100 __ note_position(call->node()->pos());
102 unsigned argc = call->args()->length();
103 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
104 HIR *hir = call->args()->at(i);
108 call->callee()->accept(this);
113 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
118 HGlobal::hold(BytecodeEmitter &emitter_)
123 HGlobal::swapAndPick(BytecodeEmitter &emitter_)
129 HGlobal::store(BytecodeEmitter &emitter_)
135 HGlobal::load(BytecodeEmitter &emitter_)
141 HGlobal::ref(BytecodeEmitter &emitter_)
147 MaybeDereference(Type *type)
149 if (type->isReference())
150 return ReferenceType::cast(type)->contained();
154 HLocal::HLocal(AstNode *node, VariableSymbol *sym)
155 : HLValue(node, MaybeDereference(sym->type())),
161 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
163 if (sym_->type()->isReference()) {
164 if (sym_->storage() == VariableSymbol::Local)
166 else if (sym_->storage() == VariableSymbol::Arg)
174 HLocal::hold(BytecodeEmitter &emitter_)
176 if (sym_->type()->isReference())
181 HLocal::swapAndPick(BytecodeEmitter &emitter_)
183 if (sym_->type()->isReference()) {
192 HLocal::store(BytecodeEmitter &emitter_)
194 if (sym_->type()->isReference()) {
195 __ setref(ReferenceType::cast(sym_->type())->contained());
197 if (sym_->storage() == VariableSymbol::Local)
199 else if (sym_->storage() == VariableSymbol::Arg)
207 HLocal::load(BytecodeEmitter &emitter_)
209 if (sym_->type()->isReference())
210 __ getref(ReferenceType::cast(sym_->type())->contained());
211 else if (sym_->storage() == VariableSymbol::Local)
213 else if (sym_->storage() == VariableSymbol::Arg)
220 HLocal::ref(BytecodeEmitter &emitter_)
222 if (sym_->storage() == VariableSymbol::Heap) {
223 // Closing over a reference should never pass SemA.
224 assert(!sym_->type()->isReference());
226 // We don't support direct references to heap-allocated locals. This
227 // should probably be banned in SemA? But for now, just assert. :TODO:
232 if (sym_->type()->isReference()) {
233 if (sym_->storage() == VariableSymbol::Local)
235 else if (sym_->storage() == VariableSymbol::Arg)
247 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
249 __ note_position(node()->pos());
250 left_->accept(visitor);
251 right_->accept(visitor);
255 HIndex::hold(BytecodeEmitter &emitter_)
261 HIndex::swapAndPick(BytecodeEmitter &emitter_)
264 __ roll3(); // INDEX VALUE BASE
265 __ roll3(); // VALUE BASE INDEX
266 __ pick3(); // VALUE BASE INDEX VALUE
270 HIndex::store(BytecodeEmitter &emitter_)
276 HIndex::load(BytecodeEmitter &emitter_)
282 HIndex::ref(BytecodeEmitter &emitter_)
288 HIRTranslator::visitLocal(HLocal *local)
290 local->bind(this, emitter_);
291 local->load(emitter_);
295 HIRTranslator::visitIndex(HIndex *index)
297 index->bind(this, emitter_);
298 index->load(emitter_);
302 HIRTranslator::visitGlobal(HGlobal *global)
304 global->bind(this, emitter_);
305 global->load(emitter_);
309 TokenToOpcode(TokenKind kind)
333 case TOK_ASSIGN_USHR:
339 case TOK_ASSIGN_BITAND:
342 case TOK_ASSIGN_BITOR:
357 assert(kind == TOK_BITXOR);
363 HIRTranslator::visitStore(HStore *store)
365 store->lval()->bind(this, emitter_);
367 if (store->kind() == TOK_ASSIGN) {
368 // Fixed-length arrays and structs have copy semantics.
369 Local<Type> type(zone_, store->lval()->type());
370 if (type->isArray() && ArrayType::cast(type)->isFixedLength()) {
371 store->lval()->load(emitter_);
372 store->rval()->accept(this);
374 } else if (type->isStruct()) {
375 store->lval()->load(emitter_);
376 store->rval()->accept(this);
379 store->rval()->accept(this);
380 store->lval()->store(emitter_);
383 // Advanced case: dup the ref, load, op, store.
384 store->lval()->hold(emitter_);
385 store->lval()->load(emitter_);
386 store->rval()->accept(this);
387 __ binary(TokenToOpcode(store->kind()));
388 store->lval()->store(emitter_);
393 HIRTranslator::visitPostIncDec(HPostIncDec *hir)
395 hir->lval()->bind(this, emitter_);
396 hir->lval()->hold(emitter_);
397 hir->lval()->load(emitter_);
398 hir->lval()->swapAndPick(emitter_);
399 hir->rval()->accept(this);
400 __ binary(TokenToOpcode(hir->kind()));
401 hir->lval()->store(emitter_);
406 HIRTranslator::visitBinary(HBinary *binary)
408 binary->left()->accept(this);
409 binary->right()->accept(this);
411 // We define three sets of math operators:
413 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
415 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
417 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
418 // equal integer types and return that integer type.
420 Opcode op = TokenToOpcode(binary->token());
423 __ note_position(binary->node()->pos());
425 if (op >= OP_ADD && op <= OP_NE) {
426 if (binary->type()->isFloat())
427 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
429 assert(binary->type()->isInt32());
436 HIRTranslator::visitUnary(HUnary *unary)
438 unary->in()->accept(this);
440 switch (unary->token()) {
442 if (unary->type()->isFloat())
449 assert(unary->token() == TOK_LABEL);
450 __ bitcast(unary->type());
456 HIRTranslator::visitConvert(HConvert *convert)
458 convert->in()->accept(this);
459 __ unary(convert->opcode());
463 HIRTranslator::visitToRef(HToRef *hir)
467 // See if we can bind a direct reference.
468 if (in->isLValue() && in->toLValue()->ref(emitter_))
471 // No, we can't. So instead we create an on-stack reference.
477 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
479 HIRTranslator translator(zone, emitter);
480 translator.translate(hir);