Support admin.inc again.
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 visitBinary(HBinary *binary);
48 void visitLocal(HLocal *local);
49 void visitGlobal(HGlobal *global);
50 void visitConvert(HConvert *convert);
51 void visitIndex(HIndex *index);
52 void visitPostIncDec(HPostIncDec *hir);
53 void visitToRef(HToRef *hir);
57 BytecodeEmitter &emitter_;
63 HIRTranslator::visitBoolean(HBoolean *hir)
72 HIRTranslator::visitFloat(HFloat *hir)
74 __ float_(hir->value());
78 HIRTranslator::visitInteger(HInteger *hir)
80 __ int_(hir->value());
84 HIRTranslator::visitArray(HArray *hir)
86 __ object(hir->array());
93 HIRTranslator::visitCall(HCall *call)
95 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
97 assert(call->args()->length() == fun->parameters()->length());
99 __ note_position(call->node()->pos());
101 unsigned argc = call->args()->length();
102 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
103 HIR *hir = call->args()->at(i);
107 call->callee()->accept(this);
112 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
117 HGlobal::hold(BytecodeEmitter &emitter_)
122 HGlobal::swapAndPick(BytecodeEmitter &emitter_)
128 HGlobal::store(BytecodeEmitter &emitter_)
134 HGlobal::load(BytecodeEmitter &emitter_)
140 HGlobal::ref(BytecodeEmitter &emitter_)
146 MaybeDereference(Type *type)
148 if (type->isReference())
149 return ReferenceType::cast(type)->contained();
153 HLocal::HLocal(AstNode *node, VariableSymbol *sym)
154 : HLValue(node, MaybeDereference(sym->type())),
160 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
162 if (sym_->type()->isReference()) {
163 if (sym_->storage() == VariableSymbol::Local)
165 else if (sym_->storage() == VariableSymbol::Arg)
173 HLocal::hold(BytecodeEmitter &emitter_)
175 if (sym_->type()->isReference())
180 HLocal::swapAndPick(BytecodeEmitter &emitter_)
182 if (sym_->type()->isReference()) {
191 HLocal::store(BytecodeEmitter &emitter_)
193 if (sym_->type()->isReference()) {
194 __ setref(ReferenceType::cast(sym_->type())->contained());
196 if (sym_->storage() == VariableSymbol::Local)
198 else if (sym_->storage() == VariableSymbol::Arg)
206 HLocal::load(BytecodeEmitter &emitter_)
208 if (sym_->type()->isReference())
209 __ getref(ReferenceType::cast(sym_->type())->contained());
210 else if (sym_->storage() == VariableSymbol::Local)
212 else if (sym_->storage() == VariableSymbol::Arg)
219 HLocal::ref(BytecodeEmitter &emitter_)
221 if (sym_->storage() == VariableSymbol::Heap) {
222 // Closing over a reference should never pass SemA.
223 assert(!sym_->type()->isReference());
225 // We don't support direct references to heap-allocated locals. This
226 // should probably be banned in SemA? But for now, just assert. :TODO:
231 if (sym_->type()->isReference()) {
232 if (sym_->storage() == VariableSymbol::Local)
234 else if (sym_->storage() == VariableSymbol::Arg)
246 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
248 __ note_position(node()->pos());
249 left_->accept(visitor);
250 right_->accept(visitor);
254 HIndex::hold(BytecodeEmitter &emitter_)
260 HIndex::swapAndPick(BytecodeEmitter &emitter_)
263 __ roll3(); // INDEX VALUE BASE
264 __ roll3(); // VALUE BASE INDEX
265 __ pick3(); // VALUE BASE INDEX VALUE
269 HIndex::store(BytecodeEmitter &emitter_)
275 HIndex::load(BytecodeEmitter &emitter_)
281 HIndex::ref(BytecodeEmitter &emitter_)
287 HIRTranslator::visitLocal(HLocal *local)
289 local->bind(this, emitter_);
290 local->load(emitter_);
294 HIRTranslator::visitIndex(HIndex *index)
296 index->bind(this, emitter_);
297 index->load(emitter_);
301 HIRTranslator::visitGlobal(HGlobal *global)
303 global->bind(this, emitter_);
304 global->load(emitter_);
308 TokenToOpcode(TokenKind kind)
332 case TOK_ASSIGN_USHR:
338 case TOK_ASSIGN_BITAND:
341 case TOK_ASSIGN_BITOR:
356 assert(kind == TOK_BITXOR);
362 HIRTranslator::visitStore(HStore *store)
364 store->lval()->bind(this, emitter_);
366 if (store->kind() == TOK_ASSIGN) {
367 // Fixed-length arrays and structs have copy semantics.
368 Local<Type> type(zone_, store->lval()->type());
369 if (type->isArray() && ArrayType::cast(type)->isFixedLength()) {
370 store->lval()->load(emitter_);
371 store->rval()->accept(this);
373 } else if (type->isStruct()) {
374 store->lval()->load(emitter_);
375 store->rval()->accept(this);
378 store->rval()->accept(this);
379 store->lval()->store(emitter_);
382 // Advanced case: dup the ref, load, op, store.
383 store->lval()->hold(emitter_);
384 store->lval()->load(emitter_);
385 store->rval()->accept(this);
386 __ binary(TokenToOpcode(store->kind()));
387 store->lval()->store(emitter_);
392 HIRTranslator::visitPostIncDec(HPostIncDec *hir)
394 hir->lval()->bind(this, emitter_);
395 hir->lval()->hold(emitter_);
396 hir->lval()->load(emitter_);
397 hir->lval()->swapAndPick(emitter_);
398 hir->rval()->accept(this);
399 __ binary(TokenToOpcode(hir->kind()));
400 hir->lval()->store(emitter_);
405 HIRTranslator::visitBinary(HBinary *binary)
407 binary->left()->accept(this);
408 binary->right()->accept(this);
410 // We define three sets of math operators:
412 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
414 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
416 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
417 // equal integer types and return that integer type.
419 Opcode op = TokenToOpcode(binary->token());
422 __ note_position(binary->node()->pos());
424 if (op >= OP_ADD && op <= OP_NE) {
425 if (binary->type()->isFloat())
426 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
428 assert(binary->type()->isInt32());
435 HIRTranslator::visitConvert(HConvert *convert)
437 convert->in()->accept(this);
438 __ unary(convert->opcode());
442 HIRTranslator::visitToRef(HToRef *hir)
446 // See if we can bind a direct reference.
447 if (in->isLValue() && in->toLValue()->ref(emitter_))
450 // No, we can't. So instead we create an on-stack reference.
456 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
458 HIRTranslator translator(zone, emitter);
459 translator.translate(hir);