Split ArrayType from Type.
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/.
20 #include "BytecodeEmitter.h"
21 #include "../Heap-inl.h"
22 #include "../Handles-inl.h"
26 class HIRTranslator : public HIRVisitor
29 HIRTranslator(Zone *zone, BytecodeEmitter &emitter)
35 void translate(HIR *hir) {
39 void visitFloat(HFloat *hir);
40 void visitInteger(HInteger *hir);
41 void visitCall(HCall *call);
42 void visitStore(HStore *store);
43 void visitBinary(HBinary *binary);
44 void visitLocal(HLocal *local);
45 void visitGlobal(HGlobal *global);
46 void visitConvert(HConvert *convert);
50 BytecodeEmitter &emitter_;
56 HIRTranslator::visitFloat(HFloat *hir)
58 __ float_(hir->value());
62 HIRTranslator::visitInteger(HInteger *hir)
64 __ int_(hir->value());
68 HIRTranslator::visitCall(HCall *call)
70 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
72 assert(call->args()->length() == fun->parameters()->length());
74 unsigned argc = call->args()->length();
76 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
77 HIR *hir = call->args()->at(i);
81 call->callee()->accept(this);
86 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
91 HGlobal::store(BytecodeEmitter &emitter_)
97 HGlobal::load(BytecodeEmitter &emitter_)
103 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
105 if (sym_->type()->isReference())
110 HLocal::store(BytecodeEmitter &emitter_)
112 if (sym_->type()->isReference())
113 __ setref(sym_->type()->contained_ref());
119 HLocal::load(BytecodeEmitter &emitter_)
121 if (sym_->type()->isReference())
122 __ getref(sym_->type());
128 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
130 left_->accept(visitor);
131 right_->accept(visitor);
135 HIndex::store(BytecodeEmitter &emitter_)
141 HIndex::load(BytecodeEmitter &emitter_)
147 HIRTranslator::visitLocal(HLocal *local)
149 local->bind(this, emitter_);
150 local->load(emitter_);
154 HIRTranslator::visitGlobal(HGlobal *global)
156 __ getglobal(global->sym());
160 HIRTranslator::visitStore(HStore *store)
162 store->lval()->bind(this, emitter_);
163 store->rval()->accept(this);
164 store->lval()->store(emitter_);
168 TokenToOpcode(TokenKind kind)
204 assert(kind == TOK_BITXOR);
210 HIRTranslator::visitBinary(HBinary *binary)
212 binary->left()->accept(this);
213 binary->right()->accept(this);
215 // We define three sets of math operators:
217 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
219 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
221 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
222 // equal integer types and return that integer type.
224 Opcode op = TokenToOpcode(binary->token());
226 if (op >= OP_ADD && op <= OP_NE) {
227 if (binary->type()->isFloat())
228 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
230 assert(binary->type()->isInt32());
237 HIRTranslator::visitConvert(HConvert *convert)
239 convert->in()->accept(this);
240 __ unary(convert->opcode());
244 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
246 HIRTranslator translator(zone, emitter);
247 translator.translate(hir);