Fix linux/mac 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 "../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);
55 void visitNewDependentArray(HNewDependentArray *hir);
59 BytecodeEmitter &emitter_;
65 HIRTranslator::visitBoolean(HBoolean *hir)
74 HIRTranslator::visitFloat(HFloat *hir)
76 __ float_(hir->value());
80 HIRTranslator::visitInteger(HInteger *hir)
82 __ int_(hir->value());
86 HIRTranslator::visitArray(HArray *hir)
88 __ object(hir->array());
95 HIRTranslator::visitCall(HCall *call)
97 Local<FunctionType> fun(zone_, FunctionType::cast(call->callee()->type()));
99 assert(call->args()->length() == fun->parameters()->length());
101 __ note_position(call->node()->pos());
103 unsigned argc = call->args()->length();
104 for (size_t i = call->args()->length() - 1; i < call->args()->length(); i--) {
105 HIR *hir = call->args()->at(i);
109 call->callee()->accept(this);
114 HGlobal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
119 HGlobal::hold(BytecodeEmitter &emitter_)
124 HGlobal::swapAndPick(BytecodeEmitter &emitter_)
130 HGlobal::store(BytecodeEmitter &emitter_)
136 HGlobal::load(BytecodeEmitter &emitter_)
142 HGlobal::ref(BytecodeEmitter &emitter_)
148 MaybeDereference(Type *type)
150 if (type->isReference())
151 return ReferenceType::cast(type)->contained();
155 HLocal::HLocal(AstNode *node, VariableSymbol *sym)
156 : HLValue(node, MaybeDereference(sym->type())),
162 HLocal::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
164 if (sym_->type()->isReference()) {
165 if (sym_->storage() == VariableSymbol::Local)
167 else if (sym_->storage() == VariableSymbol::Arg)
175 HLocal::hold(BytecodeEmitter &emitter_)
177 if (sym_->type()->isReference())
182 HLocal::swapAndPick(BytecodeEmitter &emitter_)
184 if (sym_->type()->isReference()) {
193 HLocal::store(BytecodeEmitter &emitter_)
195 if (sym_->type()->isReference()) {
196 __ setref(ReferenceType::cast(sym_->type())->contained());
198 if (sym_->storage() == VariableSymbol::Local)
200 else if (sym_->storage() == VariableSymbol::Arg)
208 HLocal::load(BytecodeEmitter &emitter_)
210 if (sym_->type()->isReference())
211 __ getref(ReferenceType::cast(sym_->type())->contained());
212 else if (sym_->storage() == VariableSymbol::Local)
214 else if (sym_->storage() == VariableSymbol::Arg)
221 HLocal::ref(BytecodeEmitter &emitter_)
223 if (sym_->storage() == VariableSymbol::Heap) {
224 // Closing over a reference should never pass SemA.
225 assert(!sym_->type()->isReference());
227 // We don't support direct references to heap-allocated locals. This
228 // should probably be banned in SemA? But for now, just assert. :TODO:
233 if (sym_->type()->isReference()) {
234 if (sym_->storage() == VariableSymbol::Local)
236 else if (sym_->storage() == VariableSymbol::Arg)
248 HIndex::bind(HIRVisitor *visitor, BytecodeEmitter &emitter_)
250 __ note_position(node()->pos());
251 left_->accept(visitor);
252 right_->accept(visitor);
256 HIndex::hold(BytecodeEmitter &emitter_)
262 HIndex::swapAndPick(BytecodeEmitter &emitter_)
265 __ roll3(); // INDEX VALUE BASE
266 __ roll3(); // VALUE BASE INDEX
267 __ pick3(); // VALUE BASE INDEX VALUE
271 HIndex::store(BytecodeEmitter &emitter_)
277 HIndex::load(BytecodeEmitter &emitter_)
283 HIndex::ref(BytecodeEmitter &emitter_)
289 HIRTranslator::visitLocal(HLocal *local)
291 local->bind(this, emitter_);
292 local->load(emitter_);
296 HIRTranslator::visitIndex(HIndex *index)
298 index->bind(this, emitter_);
299 index->load(emitter_);
303 HIRTranslator::visitGlobal(HGlobal *global)
305 global->bind(this, emitter_);
306 global->load(emitter_);
310 TokenToOpcode(TokenKind kind)
334 case TOK_ASSIGN_USHR:
340 case TOK_ASSIGN_BITAND:
343 case TOK_ASSIGN_BITOR:
358 assert(kind == TOK_BITXOR);
364 HIRTranslator::visitStore(HStore *store)
366 store->lval()->bind(this, emitter_);
368 if (store->kind() == TOK_ASSIGN) {
369 // Fixed-length arrays and structs have copy semantics.
370 Local<Type> type(zone_, store->lval()->type());
371 if (type->isArray() && ArrayType::cast(type)->isFixedLength()) {
372 store->lval()->load(emitter_);
373 store->rval()->accept(this);
375 } else if (type->isStruct()) {
376 store->lval()->load(emitter_);
377 store->rval()->accept(this);
380 store->rval()->accept(this);
381 store->lval()->store(emitter_);
384 // Advanced case: dup the ref, load, op, store.
385 store->lval()->hold(emitter_);
386 store->lval()->load(emitter_);
387 store->rval()->accept(this);
388 __ binary(TokenToOpcode(store->kind()));
389 store->lval()->store(emitter_);
394 HIRTranslator::visitPostIncDec(HPostIncDec *hir)
396 hir->lval()->bind(this, emitter_);
397 hir->lval()->hold(emitter_);
398 hir->lval()->load(emitter_);
399 hir->lval()->swapAndPick(emitter_);
400 hir->rval()->accept(this);
401 __ binary(TokenToOpcode(hir->kind()));
402 hir->lval()->store(emitter_);
407 HIRTranslator::visitBinary(HBinary *binary)
409 binary->left()->accept(this);
410 binary->right()->accept(this);
412 // We define three sets of math operators:
414 // (1) ADD, SUB, MUL, DIV, MOD, which take two equal types and produce
416 // (2) LT, LE, GE, GT, EQ, NE, which take two equal types and produce
418 // (3) SHL, SR, USHR, BITAND, BITOR, BITXOR, BITNOT, which take two
419 // equal integer types and return that integer type.
421 Opcode op = TokenToOpcode(binary->token());
424 __ note_position(binary->node()->pos());
426 if (op >= OP_ADD && op <= OP_NE) {
427 if (binary->type()->isFloat())
428 op = Opcode(int(op) + int(OP_ADD_F - OP_ADD));
430 assert(binary->type()->isInt32());
437 HIRTranslator::visitUnary(HUnary *unary)
439 unary->in()->accept(this);
441 switch (unary->token()) {
447 if (unary->type()->isFloat())
454 assert(unary->token() == TOK_LABEL);
455 __ bitcast(unary->type());
461 HIRTranslator::visitConvert(HConvert *convert)
463 convert->in()->accept(this);
464 __ unary(convert->opcode());
468 HIRTranslator::visitToRef(HToRef *hir)
472 // See if we can bind a direct reference.
473 if (in->isLValue() && in->toLValue()->ref(emitter_))
476 // No, we can't. So instead we create an on-stack reference.
482 HIRTranslator::visitNewDependentArray(HNewDependentArray *hir)
484 hir->base()->accept(this);
485 hir->index()->accept(this);
487 Local<ArrayType> type(zone_, ArrayType::cast(hir->type()));
489 // Attach an actual map to the dependent type, in case it's a new
491 if (!ArrayMap::Attach(zone_, type))
494 // The dependent map is used for the opcode, but is not attached.
495 Local<ArrayMap> map(zone_, ArrayMap::NewDependent(zone_, type));
503 ke::EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir)
505 HIRTranslator translator(zone, emitter);
506 translator.translate(hir);