Implement |const| keyword.
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 visitFloat(HFloat *hir);
42 void visitInteger(HInteger *hir);
43 void visitArray(HArray *hir);
44 void visitCall(HCall *call);
45 void visitStore(HStore *store);
46 void visitBinary(HBinary *binary);
47 void visitLocal(HLocal *local);
48 void visitGlobal(HGlobal *global);
49 void visitConvert(HConvert *convert);
50 void visitIndex(HIndex *index);
51 void visitPostIncDec(HPostIncDec *hir);
52 void visitToRef(HToRef *hir);
56 BytecodeEmitter &emitter_;
62 HIRTranslator::visitFloat(HFloat *hir)
64 __ float_(hir->value());
68 HIRTranslator::visitInteger(HInteger *hir)
70 __ int_(hir->value());
74 HIRTranslator::visitArray(HArray *hir)
76 __ object(hir->array());
83 HIRTranslator::visitCall(HCall *call)
85 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
87 assert(call->args()->length() == fun->parameters()->length());
89 __ note_position(call->node()->pos());
91 unsigned argc = call->args()->length();
92 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
93 HIR *hir = call->args()->at(i);
97 call->callee()->accept(this);
102 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
107 HGlobal::hold(BytecodeEmitter &emitter_)
112 HGlobal::swapAndPick(BytecodeEmitter &emitter_)
118 HGlobal::store(BytecodeEmitter &emitter_)
124 HGlobal::load(BytecodeEmitter &emitter_)
130 HGlobal::ref(BytecodeEmitter &emitter_)
136 MaybeDereference(Type *type)
138 if (type->isReference())
139 return ReferenceType::cast(type)->contained();
143 HLocal::HLocal(AstNode *node, VariableSymbol *sym)
144 : HLValue(node, MaybeDereference(sym->type())),
150 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
152 if (sym_->type()->isReference()) {
153 if (sym_->storage() == VariableSymbol::Local)
155 else if (sym_->storage() == VariableSymbol::Arg)
163 HLocal::hold(BytecodeEmitter &emitter_)
165 if (sym_->type()->isReference())
170 HLocal::swapAndPick(BytecodeEmitter &emitter_)
172 if (sym_->type()->isReference()) {
181 HLocal::store(BytecodeEmitter &emitter_)
183 if (sym_->type()->isReference()) {
184 __ setref(ReferenceType::cast(sym_->type())->contained());
186 if (sym_->storage() == VariableSymbol::Local)
188 else if (sym_->storage() == VariableSymbol::Arg)
196 HLocal::load(BytecodeEmitter &emitter_)
198 if (sym_->type()->isReference())
199 __ getref(ReferenceType::cast(sym_->type())->contained());
200 else if (sym_->storage() == VariableSymbol::Local)
202 else if (sym_->storage() == VariableSymbol::Arg)
209 HLocal::ref(BytecodeEmitter &emitter_)
211 if (sym_->storage() == VariableSymbol::Heap) {
212 // Closing over a reference should never pass SemA.
213 assert(!sym_->type()->isReference());
215 // We don't support direct references to heap-allocated locals. This
216 // should probably be banned in SemA? But for now, just assert. :TODO:
221 if (sym_->type()->isReference()) {
222 if (sym_->storage() == VariableSymbol::Local)
224 else if (sym_->storage() == VariableSymbol::Arg)
236 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
238 __ note_position(node()->pos());
239 left_->accept(visitor);
240 right_->accept(visitor);
244 HIndex::hold(BytecodeEmitter &emitter_)
250 HIndex::swapAndPick(BytecodeEmitter &emitter_)
253 __ roll3(); // INDEX VALUE BASE
254 __ roll3(); // VALUE BASE INDEX
255 __ pick3(); // VALUE BASE INDEX VALUE
259 HIndex::store(BytecodeEmitter &emitter_)
265 HIndex::load(BytecodeEmitter &emitter_)
271 HIndex::ref(BytecodeEmitter &emitter_)
277 HIRTranslator::visitLocal(HLocal *local)
279 local->bind(this, emitter_);
280 local->load(emitter_);
284 HIRTranslator::visitIndex(HIndex *index)
286 index->bind(this, emitter_);
287 index->load(emitter_);
291 HIRTranslator::visitGlobal(HGlobal *global)
293 global->bind(this, emitter_);
294 global->load(emitter_);
298 TokenToOpcode(TokenKind kind)
322 case TOK_ASSIGN_USHR:
328 case TOK_ASSIGN_BITAND:
331 case TOK_ASSIGN_BITOR:
346 assert(kind == TOK_BITXOR);
352 HIRTranslator::visitStore(HStore *store)
354 store->lval()->bind(this, emitter_);
356 if (store->kind() == TOK_ASSIGN) {
357 // Fixed-length arrays and structs have copy semantics.
358 Local<Type> type(zone_, store->lval()->type());
359 if (type->isArray() && ArrayType::cast(type)->isFixedLength()) {
360 store->lval()->load(emitter_);
361 store->rval()->accept(this);
363 } else if (type->isStruct()) {
364 store->lval()->load(emitter_);
365 store->rval()->accept(this);
368 store->rval()->accept(this);
369 store->lval()->store(emitter_);
372 // Advanced case: dup the ref, load, op, store.
373 store->lval()->hold(emitter_);
374 store->lval()->load(emitter_);
375 store->rval()->accept(this);
376 __ binary(TokenToOpcode(store->kind()));
377 store->lval()->store(emitter_);
382 HIRTranslator::visitPostIncDec(HPostIncDec *hir)
384 hir->lval()->bind(this, emitter_);
385 hir->lval()->hold(emitter_);
386 hir->lval()->load(emitter_);
387 hir->lval()->swapAndPick(emitter_);
388 hir->rval()->accept(this);
389 __ binary(TokenToOpcode(hir->kind()));
390 hir->lval()->store(emitter_);
395 HIRTranslator::visitBinary(HBinary *binary)
397 binary->left()->accept(this);
398 binary->right()->accept(this);
400 // We define three sets of math operators:
402 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
404 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
406 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
407 // equal integer types and return that integer type.
409 Opcode op = TokenToOpcode(binary->token());
412 __ note_position(binary->node()->pos());
414 if (op >= OP_ADD && op <= OP_NE) {
415 if (binary->type()->isFloat())
416 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
418 assert(binary->type()->isInt32());
425 HIRTranslator::visitConvert(HConvert *convert)
427 convert->in()->accept(this);
428 __ unary(convert->opcode());
432 HIRTranslator::visitToRef(HToRef *hir)
436 // See if we can bind a direct reference.
437 if (in->isLValue() && in->toLValue()->ref(emitter_))
440 // No, we can't. So instead we create an on-stack reference.
446 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
448 HIRTranslator translator(zone, emitter);
449 translator.translate(hir);