Introduced type semantics, completely replacing tag semantics.
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 "BytecodeCompiler.h"
27 #include "Environments.h"
32 typedef BytecodeEmitter::Label Label;
36 BytecodeCompiler::BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module)
39 emitter_(zone->pool()),
47 static inline TokenKind
48 ToCompare(Expression *expr)
50 if (expr->isBinaryExpression()) {
51 BinaryExpression *b = expr->toBinaryExpression();
52 if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
54 } else if (expr->isUnaryExpression()) {
55 UnaryExpression *u = expr->toUnaryExpression();
56 if (u->token() == TOK_NOT)
63 CompareTokenToJump(TokenKind kind)
77 assert(kind == TOK_LE);
83 TokenToOpcode(TokenKind kind)
119 assert(kind == TOK_BITXOR);
125 BytecodeCompiler::recordLifoSlot(Scope *scope)
130 if (Variable *var = scope->lifoSlot()) {
131 locals_->set(zone_, var->slot(), var->type());
132 __ savelifo(var->slot());
137 BytecodeCompiler::unwindTo(Scope *scope)
139 // Note: we only need to unwind to the outermost scope's LIFO slot.
140 Variable *var = NULL;
141 for (Scope *current = scope_; current != scope; current = current->enclosing()) {
142 if (Variable *slot = current->lifoSlot())
147 __ unwindlifo(var->slot());
151 BytecodeCompiler::visit(NameProxy *name)
153 switch (name->binding()->kind()) {
154 case BoundName::VARIABLE:
156 Variable *var = name->binding()->toVariable();
158 if (var->location() == Variable::STACK) {
160 assert(!name->type()->isReference());
161 } else if (var->location() == Variable::ENVIRONMENT) {
164 assert(var->location() == Variable::PARAMETER);
167 // If this argument is actually a reference, dereference it now.
168 if (var->type()->isReference())
169 __ getref(var->type()->contained());
174 case BoundName::FUNCTION:
176 NamedFunction *fun = name->binding()->toNamedFunction();
178 __ getmodule_fn(fun->slot());
184 NamedConstant *constant = name->binding()->toNamedConstant();
185 __ int_(constant->value().toInt());
192 BytecodeCompiler::visit(ExpressionStatement *node)
194 // Expressions leave their value on the top of the stack, so we make sure
195 // to balance the stack if they're used as a statement.
196 node->expression()->accept(this);
197 if (!node->expression()->type()->isVoid())
202 BytecodeCompiler::visit(Assignment *node)
204 // The first step is three sub-steps:
206 // (1) Bind the left-hand side.
207 // -- If a cumulative operation (+=, etc):
208 // (2) Save a copy of the binding on the stack.
209 // (3) Dereference the binding to get a value.
211 if (node->lvalue()->isNameProxy()) {
212 NameProxy *proxy = node->lvalue()->toNameProxy();
213 Variable *var = proxy->binding()->toVariable();
216 if (var->type()->isReference() || var->type()->isArray())
219 // Get the value if needed.
220 if (node->token() != TOK_ASSIGN) {
221 // += etc is not allowed on arrays.
222 assert(!var->type()->isArray());
224 if (var->type()->isReference()) {
226 __ getref(var->type()->contained());
232 IndexExpression *index = node->lvalue()->toIndexExpression();
233 __ note_position(index->pos());
235 // Note: Eventually, watch out for a whole here. An expression like:
237 // (a[4] = (5, a = []))
239 // Could shrink array, so we need to either bind a[i] to a Reference,
240 // or keep the bounds check near the set. This is just a semantic
242 index->left()->accept(this);
243 index->right()->accept(this);
245 if (node->token() != TOK_ASSIGN) {
248 if (node->type()->isArray())
251 __ getarray(node->type());
255 if (node->token() == TOK_ASSIGN) {
256 node->expression()->accept(this);
258 visitForALU(node->type(), node->expression());
260 TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
261 __ note_position(node->pos());
262 __ binary(TokenToOpcode(alu));
266 if (node->expression()->type()->isArray()) {
267 // Unlike references, which can only be computed from a NamePRoxy,
268 // arrays can be computed from multiple lvalue types.
269 __ note_position(node->pos());
271 } else if (node->lvalue()->isNameProxy()) {
272 NameProxy *proxy = node->lvalue()->toNameProxy();
273 Variable *var = proxy->binding()->toVariable();
275 if (var->type()->isReference())
276 __ setref(var->type()->contained());
280 IndexExpression *index = node->lvalue()->toIndexExpression();
281 // :TODO: get rid of the BOUNDS instruction, embed it in g/setarray*?
282 // We can't emit it here because the stack is slightly out of position.
284 __ setarray(index->left()->type()->contained());
289 BytecodeCompiler::visit(IncDecExpression *node)
291 // In the postfix case, the logic looks like:
293 // (2) Dup -> REF REF
294 // (3) Fetch -> REF VAL
295 // (4) Swap -> VAL REF
296 // (5) Pick -> VAL REF VAL
297 // (6) Add -> VAL REF VAL+1
298 // (7) Store -> VAL VAL+1
301 // In the prefix case, the logic looks like:
303 // (2) Dup -> REF REF
304 // (3) Fetch -> REF VAL
307 // (6) Add -> REF VAL+1
308 // (7) Store -> VAL+1
311 // This is squirrely enough such that we don't bother sharing too much
314 // Note: We simulate a "Pick" operation with a dup2+pop.
316 // This is steps (1) to (5).
317 Expression *expr = node->expression();
318 if (expr->isNameProxy()) {
319 NameProxy *proxy = expr->toNameProxy();
320 Variable *var = proxy->binding()->toVariable();
323 if (var->type()->isReference() || var->type()->isArray())
327 if (var->type()->isReference()) {
329 __ getref(node->type());
330 if (node->postfix()) {
335 assert(!var->type()->isArray());
341 IndexExpression *index = expr->toIndexExpression();
342 __ note_position(index->pos());
344 index->left()->accept(this);
345 index->right()->accept(this);
347 assert(!node->type()->isArray());
350 __ getarray(node->type());
352 if (node->postfix()) {
353 // The stack looks like this now:
356 // So we must use a PICK operation to rotate the stack into position.
357 __ roll3(); // INDEX VALUE ARRAY
358 __ roll3(); // VALUE ARRAY INDEX
359 __ pick3(); // VALUE ARRAY INDEX VALUE
364 if (node->type()->isFloat()) {
367 assert(node->type()->isInt32());
371 __ note_position(node->pos());
372 if (node->token() == TOK_INCREMENT)
378 if (expr->isNameProxy()) {
379 NameProxy *proxy = expr->toNameProxy();
380 Variable *var = proxy->binding()->toVariable();
382 if (var->type()->isReference())
383 __ setref(node->type());
387 __ setarray(node->type());
396 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
399 if (needed->isFloat() && !node->type()->isFloat())
402 assert(needed->primitive() == node->type()->primitive());
406 GetCoercionType(TokenKind tok, Type *left, Type *right)
414 BytecodeCompiler::visit(UnaryExpression *node)
416 Local<Type> out(zone_, ((Expression *)node)->type());
418 switch (node->token()) {
421 Local<Type> type(zone_, node->expression()->type());
422 node->expression()->accept(this);
433 Local<Type> type(zone_, node->expression()->type());
434 node->expression()->accept(this);
445 Local<Type> from(zone_, node->expression()->type());
447 node->expression()->accept(this);
448 if (!from->isFloat() && out->isFloat())
450 else if (from->isFloat() && !out->isFloat())
456 assert(node->token() == TOK_TILDE);
457 node->expression()->accept(this);
464 BytecodeCompiler::visit(BinaryExpression *node)
466 if (node->token() == TOK_AND || node->token() == TOK_OR) {
467 Label success, failure, done;
468 visitForTest(node, &success, &failure, &success);
471 __ drop(1); // Otherwise, the stack depth will be +1 too much.
479 Local<Type> leftType(zone_, node->left()->type());
480 Local<Type> rightType(zone_, node->right()->type());
481 Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
483 visitForALU(coerce, node->left());
484 visitForALU(coerce, node->right());
486 Opcode op = TokenToOpcode(node->token());
487 if (node->type()->isFloat())
488 op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
490 __ note_position(node->pos());
495 BytecodeCompiler::visit(IndexExpression *node)
497 node->left()->accept(this);
498 node->right()->accept(this);
499 __ note_position(node->pos());
502 if (node->type()->isArray())
505 __ getarray(node->type());
509 BytecodeCompiler::visit(ReturnStatement *node)
511 if (node->expression()) {
512 node->expression()->accept(this);
520 BytecodeCompiler::visitForTest(Expression *expression,
525 TokenKind token = ToCompare(expression);
526 if (token == TOK_EOF || token == TOK_NOT) {
527 if (token == TOK_EOF)
528 expression->accept(this);
530 expression->toUnaryExpression()->expression()->accept(this);
532 bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
534 Opcode op = jumpOnTrue ? OP_JT : OP_JF;
535 if (expression->type()->isFloat())
536 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
538 if (fallthrough == falseBranch)
539 __ j(op, trueBranch);
541 __ j(op, falseBranch);
545 BinaryExpression *binary = expression->toBinaryExpression();
547 if (token == TOK_AND || token == TOK_OR) {
549 if (token == TOK_AND)
550 visitForTest(binary->left(), &next, falseBranch, &next);
552 visitForTest(binary->left(), trueBranch, &next, &next);
554 visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
558 Opcode op = CompareTokenToJump(token);
559 if (fallthrough == trueBranch)
562 Local<Type> leftType(zone_, binary->left()->type());
563 Local<Type> rightType(zone_, binary->right()->type());
564 Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
566 visitForALU(coerce, binary->left());
567 visitForALU(coerce, binary->right());
568 if (coerce->isFloat())
569 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
571 if (fallthrough == trueBranch)
572 __ j(op, falseBranch);
574 __ j(op, trueBranch);
578 BytecodeCompiler::visit(ForStatement *node)
580 AutoEnterScope forScope(this, node->scope());
582 recordLifoSlot(node->scope());
585 LoopScope loop(scope_, &join, &update, &loop_);
587 if (node->initialization())
588 node->initialization()->accept(this);
591 if (node->condition())
595 node->body()->accept(this);
599 node->update()->accept(this);
601 if (node->condition()) {
603 visitForTest(node->condition(), &header, &join, &join);
611 unwindTo(node->scope()->enclosing());
612 assert(__ stackDepth() == 0);
616 BytecodeCompiler::visit(WhileStatement *node)
622 LoopScope loop(scope_, &join, &header, &loop_);
624 if (node->token() == TOK_WHILE) {
627 visitForTest(node->condition(), &body, &join, &body);
629 node->body()->accept(this);
632 node->body()->accept(this);
633 visitForTest(node->condition(), &header, &join, &join);
640 BytecodeCompiler::visit(BreakStatement *node)
642 unwindTo(loop_->scope());
643 __ jump(loop_->break_());
647 BytecodeCompiler::visit(ContinueStatement *node)
649 unwindTo(loop_->scope());
650 __ jump(loop_->continue_());
654 BytecodeCompiler::visit(BlockStatement *node)
656 AutoEnterScope blockScope(this, node->scope());
658 recordLifoSlot(node->scope());
660 for (size_t i = 0; i < node->statements()->length(); i++) {
661 Statement *statement = node->statements()->at(i);
662 statement->accept(this);
663 assert(__ stackDepth() == 0);
666 // We don't bother emitting an "unwind" instruction for function scopes,
667 // since this is taken care of by the calling convention.
668 if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
669 unwindTo(node->scope()->enclosing());
673 BytecodeCompiler::visit(IntegerLiteral *node)
675 __ int_(node->value());
679 BytecodeCompiler::visit(BooleanLiteral *node)
681 if (node->token() == TOK_TRUE)
688 BytecodeCompiler::visit(FloatLiteral *node)
690 __ float_((float)node->value());
693 static inline ArrayMap *
694 BuildArrayMap(Zone *zone, Handle<Type> type)
696 Local<ArrayMap> contained(zone);
697 if (type->levels() > 1) {
698 Local<Type> containedType(zone, type->contained());
699 contained = BuildArrayMap(zone, containedType);
704 return ArrayMap::New(zone, type, contained);
708 BytecodeCompiler::visit(StringLiteral *node)
710 Local<Type> type(zone_, node->type());
711 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
715 Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
719 assert(array->nelements() >= node->string()->length());
720 memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
722 __ note_position(node->pos());
727 BytecodeCompiler::getEnvSlot(Variable *var)
729 assert(var->scope()->kind() == Scope::GLOBAL);
734 BytecodeCompiler::setEnvSlot(Variable *var)
736 assert(var->scope()->kind() == Scope::GLOBAL);
741 BytecodeCompiler::visit(VariableDeclaration *node)
743 Variable *var = node->variable();
745 Local<Type> type(zone_, var->type());
746 if (type->isArray()) {
747 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
748 __ note_position(node->pos());
749 if (var->scope()->kind() == Scope::GLOBAL)
750 __ newarray(map, Heap::Tenure_Old);
751 else if (var->needsHeapAllocation())
752 __ newarray(map, Heap::Tenure_Default);
754 __ newarray(map, Heap::Tenure_Stack);
756 if (node->initialization()) {
757 node->initialization()->accept(this);
758 __ note_position(node->pos());
762 if (node->initialization()) {
763 node->initialization()->accept(this);
765 if (!node->postDimensions())
770 if (var->location() == Variable::STACK) {
772 locals_->set(zone_, var->slot(), var->type());
774 assert(var->location() == Variable::ENVIRONMENT);
782 BytecodeCompiler::visit(CallExpression *node)
784 Local<Type> funType(zone_, node->callee()->type());
786 // Sniff out natives, which have a specific opcode and are guaranteed to
787 // be called via name proxy.
788 NamedFunction *native = NULL;
789 if (funType->isNative()) {
790 native = node->callee()->toNameProxy()->binding()->toNamedFunction();
793 for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
794 Expression *arg = node->arguments()->at(i);
796 // We need to compute a reference if the parameter was in the form:
798 // Note this form does not allow: f({})
800 // We also need to compute references for all variadic native
801 // arguments, but arrays and references are already in the
803 Local<Type> given(zone_, arg->type());
804 bool mustPushReference = false;
805 if (i >= funType->parameters()->length()) {
806 assert(funType->isNative() && funType->isNativeVariadic());
807 mustPushReference = !given->isArray() && !given->isReference();
808 } else if (funType->parameterAt(i)->isReference()) {
809 mustPushReference = !given->isReference();
812 // If a native is passed to a native, it must only be one level deep.
813 assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
815 if (mustPushReference) {
816 if (arg->isNameProxy()) {
817 // :TODO: test with functions and globals
818 // Compute a reference to the name.
819 Variable *var = arg->toNameProxy()->binding()->toVariable();
820 __ note_position(node->pos());
822 } else if (arg->isIndexExpression()) {
823 IndexExpression *expr = arg->toIndexExpression();
824 expr->left()->accept(this);
825 expr->right()->accept(this);
826 __ note_position(expr->pos());
830 // We don't know how to, or can't, build a direct reference to
831 // this type. Make an indirect one, by taking the top of the
832 // stack and boxing it up.
834 __ note_position(node->pos());
842 __ note_position(node->pos());
844 __ callnative(native->slot(), node->arguments()->length());
845 __ drop(node->arguments()->length());
847 node->callee()->accept(this);
849 __ drop(node->arguments()->length() + 1);
854 BytecodeCompiler::visit(TernaryExpression *node)
856 Label success, failure, join;
858 visitForTest(node->condition(), &success, &failure, &success);
860 node->left()->accept(this);
864 node->right()->accept(this);
869 BytecodeCompiler::visit(IfStatement *node)
872 if (!node->ifFalse()) {
875 visitForTest(node->condition(), &success, &join, &success);
877 node->ifTrue()->accept(this);
881 visitForTest(node->condition(), &success, &next, &success);
883 node->ifTrue()->accept(this);
886 node->ifFalse()->accept(this);
893 BytecodeCompiler::visit(FunctionStatement *node)
895 if (!node->fun()->used())
898 if (node->fun()->isNative()) {
899 assert(!moduleData_->natives->at(node->fun()->slot()));
901 Local<Type> type(zone_, node->fun()->type());
902 Local<String> name(zone_, node->fun()->name());
903 Local<Native> native(zone_, Native::New(zone_, type, name));
907 moduleData_->natives->set(zone_, node->fun()->slot(), native);
911 // If the function has no body, then it's just a declaration.
913 assert(node->fun()->isForward() || node->fun()->isPublic());
917 BytecodeCompiler child(zone_, tree_, moduleData_);
918 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
922 assert(!moduleData_->module->getFunction(node->fun()->slot()));
923 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
927 BytecodeCompiler::visit(EnumStatement *node)
929 // :TODO: eventually, we'll export these.
933 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
935 // STOP doubles as RETURNVOID.
938 if (__ oom() || zone_->hasPendingException())
941 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
944 memcpy(bytes->raw(), __ buffer(), __ length());
946 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
947 assert(locals_->at(i));
949 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
953 for (size_t i = 0; i < __ objects()->length(); i++)
954 objects->set(zone_, i, *(__ objects()->at(i)));
956 Local<ByteArray> safepoints(zone_);
958 // If any stack slots hold gc pointers at potential failure points in the
959 // bytecode, record them now. We do this by building a list of tuples;
960 // each tuple contains the pc offset, and a bitset containing which stack
961 // slots must be scanned by the gc.
962 if (__ oopRecords()->length()) {
963 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
964 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
965 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
969 Address cursor = safepoints->raw();
970 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
971 OopRecord *record = __ oopRecords()->at(i);
972 assert(record->depth <= __ maxStackDepth());
974 *reinterpret_cast<unsigned *>(cursor) = record->offset;
976 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
977 for (unsigned i = 0; i < record->depth; i++)
978 bits[i / 8] |= (1 << (i % 8));
980 cursor += bytesPerSafepoint;
983 assert(cursor == safepoints->raw() + gcmapSize);
986 Local<ByteArray> sourcemap(zone_);
987 if (__ sourcemapLength()) {
988 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
992 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
995 Local<Code> code(zone_);
997 code = Code::New(zone_,
1002 moduleData_->module,
1010 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1012 // Note: No need to record a life slot here, since it's part of the Frame.
1013 AutoEnterScope functionScope(this, node->scope());
1015 // Set the initial line, because line mappings are deltas.
1016 __ setFirstLine(node->pos().line);
1017 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1019 node->body()->accept(this);
1021 unsigned nargs = node->scope()->numArguments();
1022 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1026 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1027 args->set(zone_, i, node->scope()->getArgument(i)->type());
1030 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1034 Local<Type> type(zone_, node->fun()->type());
1035 Local<String> name(zone_, node->fun()->name());
1036 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1040 // If this function had a forward, export it as a named public.
1041 if (node->fun()->isPublic()) {
1042 assert(node->fun()->isPublic());
1043 ScopedRoot<Function> root(fun);
1046 if (!moduleData_->publics.append(root))
1050 Disassemble(code, stdout);
1056 BytecodeCompiler::compile()
1058 AutoEnterScope scope(this, tree_->scope());
1060 ModuleData moduleData;
1062 moduleData_ = &moduleData;
1063 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1068 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1069 if (!moduleData_->natives)
1072 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1073 Statement *statement = tree_->statements()->at(i);
1074 statement->accept(this);
1077 if (zone_->hasPendingException())
1081 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1082 assert(moduleData_->module->getFunction(i));
1083 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1084 assert(moduleData_->natives->at(i));
1087 if (moduleData_->publics.length()) {
1088 Local<Function> fun(zone_);
1089 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1094 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1095 list->set(zone_, i, moduleData_->publics[i]);
1097 moduleData_->module->setPublics(list);
1100 moduleData_->module->setNatives(moduleData_->natives);
1102 if (scope_->numEnvironmentVars()) {
1103 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1107 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1111 moduleData_->module->setEnvironment(environment);
1114 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1118 Disassemble(code, stdout);
1120 moduleData_->module->setCode(code);
1122 return moduleData_->module;