Re-add enum support.
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 "../Heap-inl.h"
23 #include "../Handles-inl.h"
27 class HIRTranslator : public HIRVisitor
30 HIRTranslator(Zone *zone, BytecodeEmitter &emitter)
36 void translate(HIR *hir) {
40 void visitFloat(HFloat *hir);
41 void visitInteger(HInteger *hir);
42 void visitCall(HCall *call);
43 void visitStore(HStore *store);
44 void visitBinary(HBinary *binary);
45 void visitLocal(HLocal *local);
46 void visitGlobal(HGlobal *global);
47 void visitConvert(HConvert *convert);
48 void visitIndex(HIndex *index);
49 void visitPostIncDec(HPostIncDec *hir);
50 void visitToRef(HToRef *hir);
54 BytecodeEmitter &emitter_;
60 HIRTranslator::visitFloat(HFloat *hir)
62 __ float_(hir->value());
66 HIRTranslator::visitInteger(HInteger *hir)
68 __ int_(hir->value());
72 HIRTranslator::visitCall(HCall *call)
74 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
76 assert(call->args()->length() == fun->parameters()->length());
78 __ note_position(call->node()->pos());
80 unsigned argc = call->args()->length();
81 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
82 HIR *hir = call->args()->at(i);
86 call->callee()->accept(this);
91 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
96 HGlobal::hold(BytecodeEmitter &emitter_)
101 HGlobal::swapAndPick(BytecodeEmitter &emitter_)
107 HGlobal::store(BytecodeEmitter &emitter_)
113 HGlobal::load(BytecodeEmitter &emitter_)
119 HGlobal::ref(BytecodeEmitter &emitter_)
125 MaybeDereference(Type *type)
127 if (type->isReference())
128 return ReferenceType::cast(type)->contained();
132 HLocal::HLocal(AstNode *node, VariableSymbol *sym)
133 : HLValue(node, MaybeDereference(sym->type())),
139 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
141 if (sym_->type()->isReference()) {
142 if (sym_->storage() == VariableSymbol::Local)
144 else if (sym_->storage() == VariableSymbol::Arg)
152 HLocal::hold(BytecodeEmitter &emitter_)
154 if (sym_->type()->isReference())
159 HLocal::swapAndPick(BytecodeEmitter &emitter_)
161 if (sym_->type()->isReference()) {
170 HLocal::store(BytecodeEmitter &emitter_)
172 if (sym_->type()->isReference()) {
173 __ setref(ReferenceType::cast(sym_->type())->contained());
175 if (sym_->storage() == VariableSymbol::Local)
177 else if (sym_->storage() == VariableSymbol::Arg)
185 HLocal::load(BytecodeEmitter &emitter_)
187 if (sym_->type()->isReference())
188 __ getref(ReferenceType::cast(sym_->type())->contained());
189 else if (sym_->storage() == VariableSymbol::Local)
191 else if (sym_->storage() == VariableSymbol::Arg)
198 HLocal::ref(BytecodeEmitter &emitter_)
200 if (sym_->storage() == VariableSymbol::Heap) {
201 // Closing over a reference should never pass SemA.
202 assert(!sym_->type()->isReference());
204 // We don't support direct references to heap-allocated locals. This
205 // should probably be banned in SemA? But for now, just assert. :TODO:
210 if (sym_->type()->isReference()) {
211 if (sym_->storage() == VariableSymbol::Local)
213 else if (sym_->storage() == VariableSymbol::Arg)
225 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
227 __ note_position(node()->pos());
228 left_->accept(visitor);
229 right_->accept(visitor);
233 HIndex::hold(BytecodeEmitter &emitter_)
239 HIndex::swapAndPick(BytecodeEmitter &emitter_)
242 __ roll3(); // INDEX VALUE BASE
243 __ roll3(); // VALUE BASE INDEX
244 __ pick3(); // VALUE BASE INDEX VALUE
248 HIndex::store(BytecodeEmitter &emitter_)
254 HIndex::load(BytecodeEmitter &emitter_)
260 HIndex::ref(BytecodeEmitter &emitter_)
266 HIRTranslator::visitLocal(HLocal *local)
268 local->bind(this, emitter_);
269 local->load(emitter_);
273 HIRTranslator::visitIndex(HIndex *index)
275 index->bind(this, emitter_);
276 index->load(emitter_);
280 HIRTranslator::visitGlobal(HGlobal *global)
282 global->bind(this, emitter_);
283 global->load(emitter_);
287 TokenToOpcode(TokenKind kind)
311 case TOK_ASSIGN_USHR:
317 case TOK_ASSIGN_BITAND:
320 case TOK_ASSIGN_BITOR:
335 assert(kind == TOK_BITXOR);
341 HIRTranslator::visitStore(HStore *store)
343 store->lval()->bind(this, emitter_);
344 if (store->kind() == TOK_ASSIGN) {
345 store->rval()->accept(this);
347 // Advanced case: dup the ref, load, op, store.
348 store->lval()->hold(emitter_);
349 store->lval()->load(emitter_);
350 store->rval()->accept(this);
351 __ binary(TokenToOpcode(store->kind()));
353 store->lval()->store(emitter_);
357 HIRTranslator::visitPostIncDec(HPostIncDec *hir)
359 hir->lval()->bind(this, emitter_);
360 hir->lval()->hold(emitter_);
361 hir->lval()->load(emitter_);
362 hir->lval()->swapAndPick(emitter_);
363 hir->rval()->accept(this);
364 __ binary(TokenToOpcode(hir->kind()));
365 hir->lval()->store(emitter_);
370 HIRTranslator::visitBinary(HBinary *binary)
372 binary->left()->accept(this);
373 binary->right()->accept(this);
375 // We define three sets of math operators:
377 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
379 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
381 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
382 // equal integer types and return that integer type.
384 Opcode op = TokenToOpcode(binary->token());
387 __ note_position(binary->node()->pos());
389 if (op >= OP_ADD && op <= OP_NE) {
390 if (binary->type()->isFloat())
391 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
393 assert(binary->type()->isInt32());
400 HIRTranslator::visitConvert(HConvert *convert)
402 convert->in()->accept(this);
403 __ unary(convert->opcode());
407 HIRTranslator::visitToRef(HToRef *hir)
411 // See if we can bind a direct reference.
412 if (in->isLValue() && in->toLValue()->ref(emitter_))
415 // No, we can't. So instead we create an on-stack reference.
421 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
423 HIRTranslator translator(zone, emitter);
424 translator.translate(hir);