Support assigning structs.
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 Local<Type> type(zone_, node->expression()->type());
206 // The first step is three sub-steps:
208 // (1) Bind the left-hand side.
209 // -- If a cumulative operation (+=, etc):
210 // (2) Save a copy of the binding on the stack.
211 // (3) Dereference the binding to get a value.
213 if (node->lvalue()->isNameProxy()) {
214 NameProxy *proxy = node->lvalue()->toNameProxy();
215 Variable *var = proxy->binding()->toVariable();
218 if (var->type()->isReference() ||
219 var->type()->isArray() ||
220 var->type()->isStruct())
225 // Get the value if needed.
226 if (node->token() != TOK_ASSIGN) {
227 // += etc is not allowed on arrays.
228 assert(!var->type()->isArray());
230 if (var->type()->isReference()) {
232 __ getref(var->type()->contained());
237 } else if (node->lvalue()->isFieldExpression()) {
238 FieldExpression *expr = node->lvalue()->toFieldExpression();
239 __ note_position(expr->pos());
241 expr->left()->accept(this);
243 if (node->token() != TOK_ASSIGN)
246 if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray()))
247 __ getfield(expr->fieldIndex(), expr->type());
249 IndexExpression *index = node->lvalue()->toIndexExpression();
250 __ note_position(index->pos());
252 // Note: Eventually, watch out for a hole here. An expression like:
254 // (a[4] = (5, a = []))
256 // Could shrink array, so we need to either bind a[i] to a Reference,
257 // or keep the bounds check near the set. This is just a semantic
259 index->left()->accept(this);
260 index->right()->accept(this);
262 if (node->token() != TOK_ASSIGN)
265 if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray())) {
267 __ getarray(index->type());
271 if (node->token() == TOK_ASSIGN) {
272 node->expression()->accept(this);
274 visitForALU(node->type(), node->expression());
276 TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
277 __ note_position(node->pos());
278 __ binary(TokenToOpcode(alu));
282 if (type->isArray()) {
283 // Unlike references, which can only be computed from a NamePRoxy,
284 // arrays can be computed from multiple lvalue types.
285 __ note_position(node->pos());
287 } else if (type->isStruct()) {
288 // Structs have value semantics, so emit a full copy. We don't break
289 // this down into u-ops, to avoid unnecessary bytecode explosion.
290 __ note_position(node->pos());
292 } else if (node->lvalue()->isNameProxy()) {
293 NameProxy *proxy = node->lvalue()->toNameProxy();
294 Variable *var = proxy->binding()->toVariable();
296 if (var->type()->isReference())
297 __ setref(var->type()->contained());
300 } else if (node->lvalue()->isFieldExpression()) {
301 FieldExpression *expr = node->lvalue()->toFieldExpression();
302 __ setfield(expr->fieldIndex(), expr->type());
304 IndexExpression *index = node->lvalue()->toIndexExpression();
305 // :TODO: get rid of the BOUNDS instruction, embed it in g/setarray*?
306 // We can't emit it here because the stack is slightly out of position.
308 __ setarray(index->type());
313 BytecodeCompiler::visit(IncDecExpression *node)
315 // In the postfix case, the logic looks like:
317 // (2) Dup -> REF REF
318 // (3) Fetch -> REF VAL
319 // (4) Swap -> VAL REF
320 // (5) Pick -> VAL REF VAL
321 // (6) Add -> VAL REF VAL+1
322 // (7) Store -> VAL VAL+1
325 // In the prefix case, the logic looks like:
327 // (2) Dup -> REF REF
328 // (3) Fetch -> REF VAL
331 // (6) Add -> REF VAL+1
332 // (7) Store -> VAL+1
335 // This is squirrely enough such that we don't bother sharing too much
338 // Note: We simulate a "Pick" operation with a dup2+pop.
340 // This is steps (1) to (5).
341 Expression *expr = node->expression();
342 if (expr->isNameProxy()) {
343 NameProxy *proxy = expr->toNameProxy();
344 Variable *var = proxy->binding()->toVariable();
347 if (var->type()->isReference() || var->type()->isArray())
351 if (var->type()->isReference()) {
353 __ getref(node->type());
354 if (node->postfix()) {
359 assert(!var->type()->isArray());
364 } else if (expr->isFieldExpression()) {
365 FieldExpression *field = expr->toFieldExpression();
367 field->left()->accept(this);
369 __ getfield(field->fieldIndex(), field->type());
371 if (node->postfix()) {
372 // The stack looks like this now:
374 __ swap(); // VALUE OBJECT
375 __ pick2(); // VALUE OBJECT VALUE
378 IndexExpression *index = expr->toIndexExpression();
379 __ note_position(index->pos());
381 index->left()->accept(this);
382 index->right()->accept(this);
384 assert(!node->type()->isArray());
387 __ getarray(node->type());
389 if (node->postfix()) {
390 // The stack looks like this now:
393 // So we must use a PICK operation to rotate the stack into position.
394 __ roll3(); // INDEX VALUE ARRAY
395 __ roll3(); // VALUE ARRAY INDEX
396 __ pick3(); // VALUE ARRAY INDEX VALUE
401 if (node->type()->isFloat()) {
404 assert(node->type()->isInt32());
408 __ note_position(node->pos());
409 if (node->token() == TOK_INCREMENT)
415 if (expr->isNameProxy()) {
416 NameProxy *proxy = expr->toNameProxy();
417 Variable *var = proxy->binding()->toVariable();
419 if (var->type()->isReference())
420 __ setref(node->type());
423 } else if (expr->isFieldExpression()) {
424 FieldExpression *field = expr->toFieldExpression();
425 __ setfield(field->fieldIndex(), field->type());
427 assert(expr->isIndexExpression());
428 __ setarray(node->type());
437 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
440 if (needed->isFloat() && !node->type()->isFloat())
443 assert(needed->primitive() == node->type()->primitive());
447 GetCoercionType(TokenKind tok, Type *left, Type *right)
455 BytecodeCompiler::visit(UnaryExpression *node)
457 Local<Type> out(zone_, ((Expression *)node)->type());
459 switch (node->token()) {
462 Local<Type> type(zone_, node->expression()->type());
463 node->expression()->accept(this);
474 Local<Type> type(zone_, node->expression()->type());
475 node->expression()->accept(this);
486 Local<Type> from(zone_, node->expression()->type());
488 node->expression()->accept(this);
489 if (!from->isFloat() && out->isFloat())
491 else if (from->isFloat() && !out->isFloat())
497 assert(node->token() == TOK_TILDE);
498 node->expression()->accept(this);
505 BytecodeCompiler::visit(BinaryExpression *node)
507 if (node->token() == TOK_AND || node->token() == TOK_OR) {
508 Label success, failure, done;
509 visitForTest(node, &success, &failure, &success);
512 __ drop(1); // Otherwise, the stack depth will be +1 too much.
520 Local<Type> leftType(zone_, node->left()->type());
521 Local<Type> rightType(zone_, node->right()->type());
522 Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
524 visitForALU(coerce, node->left());
525 visitForALU(coerce, node->right());
527 Opcode op = TokenToOpcode(node->token());
528 if (node->type()->isFloat())
529 op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
531 __ note_position(node->pos());
536 BytecodeCompiler::visit(IndexExpression *node)
538 node->left()->accept(this);
539 node->right()->accept(this);
540 __ note_position(node->pos());
543 if (node->type()->isArray())
546 __ getarray(node->type());
550 BytecodeCompiler::visit(FieldExpression *node)
552 node->left()->accept(this);
553 __ getfield(node->fieldIndex(), node->type());
557 BytecodeCompiler::visit(ReturnStatement *node)
559 if (node->expression()) {
560 node->expression()->accept(this);
568 BytecodeCompiler::visitForTest(Expression *expression,
573 TokenKind token = ToCompare(expression);
574 if (token == TOK_EOF || token == TOK_NOT) {
575 if (token == TOK_EOF)
576 expression->accept(this);
578 expression->toUnaryExpression()->expression()->accept(this);
580 bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
582 Opcode op = jumpOnTrue ? OP_JT : OP_JF;
583 if (expression->type()->isFloat())
584 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
586 if (fallthrough == falseBranch)
587 __ j(op, trueBranch);
589 __ j(op, falseBranch);
593 BinaryExpression *binary = expression->toBinaryExpression();
595 if (token == TOK_AND || token == TOK_OR) {
597 if (token == TOK_AND)
598 visitForTest(binary->left(), &next, falseBranch, &next);
600 visitForTest(binary->left(), trueBranch, &next, &next);
602 visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
606 Opcode op = CompareTokenToJump(token);
607 if (fallthrough == trueBranch)
610 Local<Type> leftType(zone_, binary->left()->type());
611 Local<Type> rightType(zone_, binary->right()->type());
612 Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
614 visitForALU(coerce, binary->left());
615 visitForALU(coerce, binary->right());
616 if (coerce->isFloat())
617 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
619 if (fallthrough == trueBranch)
620 __ j(op, falseBranch);
622 __ j(op, trueBranch);
626 BytecodeCompiler::visit(ForStatement *node)
628 AutoEnterScope forScope(this, node->scope());
630 recordLifoSlot(node->scope());
633 LoopScope loop(scope_, &join, &update, &loop_);
635 if (node->initialization())
636 node->initialization()->accept(this);
639 if (node->condition())
643 node->body()->accept(this);
647 node->update()->accept(this);
649 if (node->condition()) {
651 visitForTest(node->condition(), &header, &join, &join);
659 unwindTo(node->scope()->enclosing());
660 assert(__ stackDepth() == 0);
664 BytecodeCompiler::visit(WhileStatement *node)
670 LoopScope loop(scope_, &join, &header, &loop_);
672 if (node->token() == TOK_WHILE) {
675 visitForTest(node->condition(), &body, &join, &body);
677 node->body()->accept(this);
680 node->body()->accept(this);
681 visitForTest(node->condition(), &header, &join, &join);
688 BytecodeCompiler::visit(BreakStatement *node)
690 unwindTo(loop_->scope());
691 __ jump(loop_->break_());
695 BytecodeCompiler::visit(ContinueStatement *node)
697 unwindTo(loop_->scope());
698 __ jump(loop_->continue_());
702 BytecodeCompiler::visit(BlockStatement *node)
704 AutoEnterScope blockScope(this, node->scope());
706 recordLifoSlot(node->scope());
708 for (size_t i = 0; i < node->statements()->length(); i++) {
709 Statement *statement = node->statements()->at(i);
710 statement->accept(this);
711 assert(__ stackDepth() == 0);
714 // We don't bother emitting an "unwind" instruction for function scopes,
715 // since this is taken care of by the calling convention.
716 if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
717 unwindTo(node->scope()->enclosing());
721 BytecodeCompiler::visit(IntegerLiteral *node)
723 __ int_(node->value());
727 BytecodeCompiler::visit(BooleanLiteral *node)
729 if (node->token() == TOK_TRUE)
736 BytecodeCompiler::visit(FloatLiteral *node)
738 __ float_((float)node->value());
741 static inline ArrayMap *
742 BuildArrayMap(Zone *zone, Handle<Type> type)
744 Local<ArrayMap> contained(zone);
745 if (type->levels() > 1) {
746 Local<Type> containedType(zone, type->contained());
747 contained = BuildArrayMap(zone, containedType);
752 return ArrayMap::New(zone, type, contained);
756 BytecodeCompiler::visit(StringLiteral *node)
758 Local<Type> type(zone_, node->type());
759 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
763 Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
767 assert(array->nelements() >= node->string()->length());
768 memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
770 __ note_position(node->pos());
775 BytecodeCompiler::getEnvSlot(Variable *var)
777 assert(var->scope()->kind() == Scope::GLOBAL);
782 BytecodeCompiler::setEnvSlot(Variable *var)
784 assert(var->scope()->kind() == Scope::GLOBAL);
789 BytecodeCompiler::visit(VariableDeclaration *node)
791 Variable *var = node->variable();
793 Local<Type> type(zone_, var->type());
794 if (type->isArray()) {
795 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
796 __ note_position(node->pos());
797 if (var->scope()->kind() == Scope::GLOBAL)
798 __ newarray(map, Heap::Tenure_Old);
799 else if (var->needsHeapAllocation())
800 __ newarray(map, Heap::Tenure_Default);
802 __ newarray(map, Heap::Tenure_Stack);
804 if (node->initialization()) {
805 node->initialization()->accept(this);
806 __ note_position(node->pos());
809 } else if (type->isStruct()) {
810 Local<Map> map(zone_, type->newMap());
811 if (var->scope()->kind() == Scope::GLOBAL)
812 __ newstruct(map, Heap::Tenure_Old);
813 else if (var->needsHeapAllocation())
814 __ newstruct(map, Heap::Tenure_Default);
816 __ newstruct(map, Heap::Tenure_Stack);
818 if (node->initialization()) {
819 node->initialization()->accept(this);
821 if (type->isInt32OrEnum() || type->isBool()) {
824 assert(type->isFloat());
830 if (var->location() == Variable::STACK) {
832 locals_->set(zone_, var->slot(), var->type());
834 assert(var->location() == Variable::ENVIRONMENT);
842 BytecodeCompiler::visit(CallExpression *node)
844 Local<Type> funType(zone_, node->callee()->type());
846 // Sniff out natives, which have a specific opcode and are guaranteed to
847 // be called via name proxy.
848 NamedFunction *native = NULL;
849 if (funType->isNative()) {
850 native = node->callee()->toNameProxy()->binding()->toNamedFunction();
853 for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
854 Expression *arg = node->arguments()->at(i);
856 // We need to compute a reference if the parameter was in the form:
858 // Note this form does not allow: f({})
860 // We also need to compute references for all variadic native
861 // arguments, but arrays and references are already in the
863 Local<Type> given(zone_, arg->type());
864 bool mustPushReference = false;
865 if (i >= funType->parameters()->length()) {
866 assert(funType->isNative() && funType->isNativeVariadic());
867 mustPushReference = !given->isArray() && !given->isReference();
868 } else if (funType->parameterAt(i)->isReference()) {
869 mustPushReference = !given->isReference();
872 // If a native is passed to a native, it must only be one level deep.
873 assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
875 if (mustPushReference) {
876 if (arg->isNameProxy()) {
877 // :TODO: test with functions and globals
878 // Compute a reference to the name.
879 Variable *var = arg->toNameProxy()->binding()->toVariable();
880 __ note_position(node->pos());
882 } else if (arg->isIndexExpression()) {
883 IndexExpression *expr = arg->toIndexExpression();
884 expr->left()->accept(this);
885 expr->right()->accept(this);
886 __ note_position(expr->pos());
890 // We don't know how to, or can't, build a direct reference to
891 // this type. Make an indirect one, by taking the top of the
892 // stack and boxing it up.
894 __ note_position(node->pos());
902 __ note_position(node->pos());
904 __ callnative(native->slot(), node->arguments()->length());
905 __ drop(node->arguments()->length());
907 node->callee()->accept(this);
909 __ drop(node->arguments()->length() + 1);
914 BytecodeCompiler::visit(TernaryExpression *node)
916 Label success, failure, join;
918 visitForTest(node->condition(), &success, &failure, &success);
920 node->left()->accept(this);
924 node->right()->accept(this);
929 BytecodeCompiler::visit(IfStatement *node)
932 if (!node->ifFalse()) {
935 visitForTest(node->condition(), &success, &join, &success);
937 node->ifTrue()->accept(this);
941 visitForTest(node->condition(), &success, &next, &success);
943 node->ifTrue()->accept(this);
946 node->ifFalse()->accept(this);
953 BytecodeCompiler::visit(FunctionStatement *node)
955 if (!node->fun()->used())
958 if (node->fun()->isNative()) {
959 assert(!moduleData_->natives->at(node->fun()->slot()));
961 Local<Type> type(zone_, node->fun()->type());
962 Local<String> name(zone_, node->fun()->name());
963 Local<Native> native(zone_, Native::New(zone_, type, name));
967 moduleData_->natives->set(zone_, node->fun()->slot(), native);
971 // If the function has no body, then it's just a declaration.
973 assert(node->fun()->isForward() || node->fun()->isPublic());
977 BytecodeCompiler child(zone_, tree_, moduleData_);
978 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
982 assert(!moduleData_->module->getFunction(node->fun()->slot()));
983 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
987 BytecodeCompiler::visit(EnumStatement *node)
989 // :TODO: eventually, we'll export these.
993 BytecodeCompiler::visit(StructureStatement *node)
998 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
1000 // STOP doubles as RETURNVOID.
1003 if (__ oom() || zone_->hasPendingException())
1006 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
1009 memcpy(bytes->raw(), __ buffer(), __ length());
1011 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
1012 assert(locals_->at(i));
1014 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
1018 for (size_t i = 0; i < __ objects()->length(); i++)
1019 objects->set(zone_, i, *(__ objects()->at(i)));
1021 Local<ByteArray> safepoints(zone_);
1023 // If any stack slots hold gc pointers at potential failure points in the
1024 // bytecode, record them now. We do this by building a list of tuples;
1025 // each tuple contains the pc offset, and a bitset containing which stack
1026 // slots must be scanned by the gc.
1027 if (__ oopRecords()->length()) {
1028 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
1029 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
1030 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
1034 Address cursor = safepoints->raw();
1035 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
1036 OopRecord *record = __ oopRecords()->at(i);
1037 assert(record->depth <= __ maxStackDepth());
1039 *reinterpret_cast<unsigned *>(cursor) = record->offset;
1041 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
1042 for (unsigned i = 0; i < record->depth; i++)
1043 bits[i / 8] |= (1 << (i % 8));
1045 cursor += bytesPerSafepoint;
1048 assert(cursor == safepoints->raw() + gcmapSize);
1051 Local<ByteArray> sourcemap(zone_);
1052 if (__ sourcemapLength()) {
1053 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
1057 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
1060 Local<Code> code(zone_);
1062 code = Code::New(zone_,
1067 moduleData_->module,
1075 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1077 // Note: No need to record a life slot here, since it's part of the Frame.
1078 AutoEnterScope functionScope(this, node->scope());
1080 // Set the initial line, because line mappings are deltas.
1081 __ setFirstLine(node->pos().line);
1082 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1084 node->body()->accept(this);
1086 unsigned nargs = node->scope()->numArguments();
1087 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1091 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1092 args->set(zone_, i, node->scope()->getArgument(i)->type());
1095 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1099 Local<Type> type(zone_, node->fun()->type());
1100 Local<String> name(zone_, node->fun()->name());
1101 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1105 // If this function had a forward, export it as a named public.
1106 if (node->fun()->isPublic()) {
1107 assert(node->fun()->isPublic());
1108 ScopedRoot<Function> root(fun);
1111 if (!moduleData_->publics.append(root))
1115 Disassemble(code, stdout);
1121 BytecodeCompiler::compile()
1123 AutoEnterScope scope(this, tree_->scope());
1125 ModuleData moduleData;
1127 moduleData_ = &moduleData;
1128 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1133 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1134 if (!moduleData_->natives)
1137 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1138 Statement *statement = tree_->statements()->at(i);
1139 statement->accept(this);
1142 if (zone_->hasPendingException())
1146 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1147 assert(moduleData_->module->getFunction(i));
1148 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1149 assert(moduleData_->natives->at(i));
1152 if (moduleData_->publics.length()) {
1153 Local<Function> fun(zone_);
1154 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1159 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1160 list->set(zone_, i, moduleData_->publics[i]);
1162 moduleData_->module->setPublics(list);
1165 moduleData_->module->setNatives(moduleData_->natives);
1167 if (scope_->numEnvironmentVars()) {
1168 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1172 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1176 moduleData_->module->setEnvironment(environment);
1179 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1183 Disassemble(code, stdout);
1185 moduleData_->module->setCode(code);
1187 return moduleData_->module;