Treat IndexExpression's type field similar to a FieldExpression.
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());
231 } else if (node->lvalue()->isFieldExpression()) {
232 FieldExpression *expr = node->lvalue()->toFieldExpression();
233 __ note_position(expr->pos());
235 expr->left()->accept(this);
237 if (node->token() != TOK_ASSIGN) {
239 __ getfield(expr->fieldIndex(), expr->type());
242 IndexExpression *index = node->lvalue()->toIndexExpression();
243 __ note_position(index->pos());
245 // Note: Eventually, watch out for a whole here. An expression like:
247 // (a[4] = (5, a = []))
249 // Could shrink array, so we need to either bind a[i] to a Reference,
250 // or keep the bounds check near the set. This is just a semantic
252 index->left()->accept(this);
253 index->right()->accept(this);
255 if (node->token() != TOK_ASSIGN) {
258 if (node->type()->isArray())
261 __ getarray(node->type());
265 if (node->token() == TOK_ASSIGN) {
266 node->expression()->accept(this);
268 visitForALU(node->type(), node->expression());
270 TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
271 __ note_position(node->pos());
272 __ binary(TokenToOpcode(alu));
276 if (node->expression()->type()->isArray()) {
277 // Unlike references, which can only be computed from a NamePRoxy,
278 // arrays can be computed from multiple lvalue types.
279 __ note_position(node->pos());
281 } else if (node->lvalue()->isNameProxy()) {
282 NameProxy *proxy = node->lvalue()->toNameProxy();
283 Variable *var = proxy->binding()->toVariable();
285 if (var->type()->isReference())
286 __ setref(var->type()->contained());
289 } else if (node->lvalue()->isFieldExpression()) {
290 FieldExpression *expr = node->lvalue()->toFieldExpression();
291 __ setfield(expr->fieldIndex(), expr->type());
293 IndexExpression *index = node->lvalue()->toIndexExpression();
294 // :TODO: get rid of the BOUNDS instruction, embed it in g/setarray*?
295 // We can't emit it here because the stack is slightly out of position.
297 __ setarray(index->left()->type());
302 BytecodeCompiler::visit(IncDecExpression *node)
304 // In the postfix case, the logic looks like:
306 // (2) Dup -> REF REF
307 // (3) Fetch -> REF VAL
308 // (4) Swap -> VAL REF
309 // (5) Pick -> VAL REF VAL
310 // (6) Add -> VAL REF VAL+1
311 // (7) Store -> VAL VAL+1
314 // In the prefix case, the logic looks like:
316 // (2) Dup -> REF REF
317 // (3) Fetch -> REF VAL
320 // (6) Add -> REF VAL+1
321 // (7) Store -> VAL+1
324 // This is squirrely enough such that we don't bother sharing too much
327 // Note: We simulate a "Pick" operation with a dup2+pop.
329 // This is steps (1) to (5).
330 Expression *expr = node->expression();
331 if (expr->isNameProxy()) {
332 NameProxy *proxy = expr->toNameProxy();
333 Variable *var = proxy->binding()->toVariable();
336 if (var->type()->isReference() || var->type()->isArray())
340 if (var->type()->isReference()) {
342 __ getref(node->type());
343 if (node->postfix()) {
348 assert(!var->type()->isArray());
353 } else if (expr->isFieldExpression()) {
354 FieldExpression *field = expr->toFieldExpression();
356 field->left()->accept(this);
358 __ getfield(field->fieldIndex(), field->type());
360 if (node->postfix()) {
361 // The stack looks like this now:
363 __ swap(); // VALUE OBJECT
364 __ pick2(); // VALUE OBJECT VALUE
367 IndexExpression *index = expr->toIndexExpression();
368 __ note_position(index->pos());
370 index->left()->accept(this);
371 index->right()->accept(this);
373 assert(!node->type()->isArray());
376 __ getarray(node->type());
378 if (node->postfix()) {
379 // The stack looks like this now:
382 // So we must use a PICK operation to rotate the stack into position.
383 __ roll3(); // INDEX VALUE ARRAY
384 __ roll3(); // VALUE ARRAY INDEX
385 __ pick3(); // VALUE ARRAY INDEX VALUE
390 if (node->type()->isFloat()) {
393 assert(node->type()->isInt32());
397 __ note_position(node->pos());
398 if (node->token() == TOK_INCREMENT)
404 if (expr->isNameProxy()) {
405 NameProxy *proxy = expr->toNameProxy();
406 Variable *var = proxy->binding()->toVariable();
408 if (var->type()->isReference())
409 __ setref(node->type());
412 } else if (expr->isFieldExpression()) {
413 FieldExpression *field = expr->toFieldExpression();
414 __ setfield(field->fieldIndex(), field->type());
416 assert(expr->isIndexExpression());
417 __ setarray(node->type());
426 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
429 if (needed->isFloat() && !node->type()->isFloat())
432 assert(needed->primitive() == node->type()->primitive());
436 GetCoercionType(TokenKind tok, Type *left, Type *right)
444 BytecodeCompiler::visit(UnaryExpression *node)
446 Local<Type> out(zone_, ((Expression *)node)->type());
448 switch (node->token()) {
451 Local<Type> type(zone_, node->expression()->type());
452 node->expression()->accept(this);
463 Local<Type> type(zone_, node->expression()->type());
464 node->expression()->accept(this);
475 Local<Type> from(zone_, node->expression()->type());
477 node->expression()->accept(this);
478 if (!from->isFloat() && out->isFloat())
480 else if (from->isFloat() && !out->isFloat())
486 assert(node->token() == TOK_TILDE);
487 node->expression()->accept(this);
494 BytecodeCompiler::visit(BinaryExpression *node)
496 if (node->token() == TOK_AND || node->token() == TOK_OR) {
497 Label success, failure, done;
498 visitForTest(node, &success, &failure, &success);
501 __ drop(1); // Otherwise, the stack depth will be +1 too much.
509 Local<Type> leftType(zone_, node->left()->type());
510 Local<Type> rightType(zone_, node->right()->type());
511 Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
513 visitForALU(coerce, node->left());
514 visitForALU(coerce, node->right());
516 Opcode op = TokenToOpcode(node->token());
517 if (node->type()->isFloat())
518 op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
520 __ note_position(node->pos());
525 BytecodeCompiler::visit(IndexExpression *node)
527 node->left()->accept(this);
528 node->right()->accept(this);
529 __ note_position(node->pos());
532 if (node->type()->isArray())
535 __ getarray(node->type());
539 BytecodeCompiler::visit(FieldExpression *node)
541 node->left()->accept(this);
542 __ getfield(node->fieldIndex(), node->type());
546 BytecodeCompiler::visit(ReturnStatement *node)
548 if (node->expression()) {
549 node->expression()->accept(this);
557 BytecodeCompiler::visitForTest(Expression *expression,
562 TokenKind token = ToCompare(expression);
563 if (token == TOK_EOF || token == TOK_NOT) {
564 if (token == TOK_EOF)
565 expression->accept(this);
567 expression->toUnaryExpression()->expression()->accept(this);
569 bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
571 Opcode op = jumpOnTrue ? OP_JT : OP_JF;
572 if (expression->type()->isFloat())
573 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
575 if (fallthrough == falseBranch)
576 __ j(op, trueBranch);
578 __ j(op, falseBranch);
582 BinaryExpression *binary = expression->toBinaryExpression();
584 if (token == TOK_AND || token == TOK_OR) {
586 if (token == TOK_AND)
587 visitForTest(binary->left(), &next, falseBranch, &next);
589 visitForTest(binary->left(), trueBranch, &next, &next);
591 visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
595 Opcode op = CompareTokenToJump(token);
596 if (fallthrough == trueBranch)
599 Local<Type> leftType(zone_, binary->left()->type());
600 Local<Type> rightType(zone_, binary->right()->type());
601 Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
603 visitForALU(coerce, binary->left());
604 visitForALU(coerce, binary->right());
605 if (coerce->isFloat())
606 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
608 if (fallthrough == trueBranch)
609 __ j(op, falseBranch);
611 __ j(op, trueBranch);
615 BytecodeCompiler::visit(ForStatement *node)
617 AutoEnterScope forScope(this, node->scope());
619 recordLifoSlot(node->scope());
622 LoopScope loop(scope_, &join, &update, &loop_);
624 if (node->initialization())
625 node->initialization()->accept(this);
628 if (node->condition())
632 node->body()->accept(this);
636 node->update()->accept(this);
638 if (node->condition()) {
640 visitForTest(node->condition(), &header, &join, &join);
648 unwindTo(node->scope()->enclosing());
649 assert(__ stackDepth() == 0);
653 BytecodeCompiler::visit(WhileStatement *node)
659 LoopScope loop(scope_, &join, &header, &loop_);
661 if (node->token() == TOK_WHILE) {
664 visitForTest(node->condition(), &body, &join, &body);
666 node->body()->accept(this);
669 node->body()->accept(this);
670 visitForTest(node->condition(), &header, &join, &join);
677 BytecodeCompiler::visit(BreakStatement *node)
679 unwindTo(loop_->scope());
680 __ jump(loop_->break_());
684 BytecodeCompiler::visit(ContinueStatement *node)
686 unwindTo(loop_->scope());
687 __ jump(loop_->continue_());
691 BytecodeCompiler::visit(BlockStatement *node)
693 AutoEnterScope blockScope(this, node->scope());
695 recordLifoSlot(node->scope());
697 for (size_t i = 0; i < node->statements()->length(); i++) {
698 Statement *statement = node->statements()->at(i);
699 statement->accept(this);
700 assert(__ stackDepth() == 0);
703 // We don't bother emitting an "unwind" instruction for function scopes,
704 // since this is taken care of by the calling convention.
705 if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
706 unwindTo(node->scope()->enclosing());
710 BytecodeCompiler::visit(IntegerLiteral *node)
712 __ int_(node->value());
716 BytecodeCompiler::visit(BooleanLiteral *node)
718 if (node->token() == TOK_TRUE)
725 BytecodeCompiler::visit(FloatLiteral *node)
727 __ float_((float)node->value());
730 static inline ArrayMap *
731 BuildArrayMap(Zone *zone, Handle<Type> type)
733 Local<ArrayMap> contained(zone);
734 if (type->levels() > 1) {
735 Local<Type> containedType(zone, type->contained());
736 contained = BuildArrayMap(zone, containedType);
741 return ArrayMap::New(zone, type, contained);
745 BytecodeCompiler::visit(StringLiteral *node)
747 Local<Type> type(zone_, node->type());
748 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
752 Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
756 assert(array->nelements() >= node->string()->length());
757 memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
759 __ note_position(node->pos());
764 BytecodeCompiler::getEnvSlot(Variable *var)
766 assert(var->scope()->kind() == Scope::GLOBAL);
771 BytecodeCompiler::setEnvSlot(Variable *var)
773 assert(var->scope()->kind() == Scope::GLOBAL);
778 BytecodeCompiler::visit(VariableDeclaration *node)
780 Variable *var = node->variable();
782 Local<Type> type(zone_, var->type());
783 if (type->isArray()) {
784 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
785 __ note_position(node->pos());
786 if (var->scope()->kind() == Scope::GLOBAL)
787 __ newarray(map, Heap::Tenure_Old);
788 else if (var->needsHeapAllocation())
789 __ newarray(map, Heap::Tenure_Default);
791 __ newarray(map, Heap::Tenure_Stack);
793 if (node->initialization()) {
794 node->initialization()->accept(this);
795 __ note_position(node->pos());
798 } else if (type->isStruct()) {
799 Local<Map> map(zone_, type->newMap());
800 if (var->scope()->kind() == Scope::GLOBAL)
801 __ newstruct(map, Heap::Tenure_Old);
802 else if (var->needsHeapAllocation())
803 __ newstruct(map, Heap::Tenure_Default);
805 __ newstruct(map, Heap::Tenure_Stack);
807 if (node->initialization()) {
808 node->initialization()->accept(this);
810 if (type->isInt32OrEnum() || type->isBool()) {
813 assert(type->isFloat());
819 if (var->location() == Variable::STACK) {
821 locals_->set(zone_, var->slot(), var->type());
823 assert(var->location() == Variable::ENVIRONMENT);
831 BytecodeCompiler::visit(CallExpression *node)
833 Local<Type> funType(zone_, node->callee()->type());
835 // Sniff out natives, which have a specific opcode and are guaranteed to
836 // be called via name proxy.
837 NamedFunction *native = NULL;
838 if (funType->isNative()) {
839 native = node->callee()->toNameProxy()->binding()->toNamedFunction();
842 for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
843 Expression *arg = node->arguments()->at(i);
845 // We need to compute a reference if the parameter was in the form:
847 // Note this form does not allow: f({})
849 // We also need to compute references for all variadic native
850 // arguments, but arrays and references are already in the
852 Local<Type> given(zone_, arg->type());
853 bool mustPushReference = false;
854 if (i >= funType->parameters()->length()) {
855 assert(funType->isNative() && funType->isNativeVariadic());
856 mustPushReference = !given->isArray() && !given->isReference();
857 } else if (funType->parameterAt(i)->isReference()) {
858 mustPushReference = !given->isReference();
861 // If a native is passed to a native, it must only be one level deep.
862 assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
864 if (mustPushReference) {
865 if (arg->isNameProxy()) {
866 // :TODO: test with functions and globals
867 // Compute a reference to the name.
868 Variable *var = arg->toNameProxy()->binding()->toVariable();
869 __ note_position(node->pos());
871 } else if (arg->isIndexExpression()) {
872 IndexExpression *expr = arg->toIndexExpression();
873 expr->left()->accept(this);
874 expr->right()->accept(this);
875 __ note_position(expr->pos());
879 // We don't know how to, or can't, build a direct reference to
880 // this type. Make an indirect one, by taking the top of the
881 // stack and boxing it up.
883 __ note_position(node->pos());
891 __ note_position(node->pos());
893 __ callnative(native->slot(), node->arguments()->length());
894 __ drop(node->arguments()->length());
896 node->callee()->accept(this);
898 __ drop(node->arguments()->length() + 1);
903 BytecodeCompiler::visit(TernaryExpression *node)
905 Label success, failure, join;
907 visitForTest(node->condition(), &success, &failure, &success);
909 node->left()->accept(this);
913 node->right()->accept(this);
918 BytecodeCompiler::visit(IfStatement *node)
921 if (!node->ifFalse()) {
924 visitForTest(node->condition(), &success, &join, &success);
926 node->ifTrue()->accept(this);
930 visitForTest(node->condition(), &success, &next, &success);
932 node->ifTrue()->accept(this);
935 node->ifFalse()->accept(this);
942 BytecodeCompiler::visit(FunctionStatement *node)
944 if (!node->fun()->used())
947 if (node->fun()->isNative()) {
948 assert(!moduleData_->natives->at(node->fun()->slot()));
950 Local<Type> type(zone_, node->fun()->type());
951 Local<String> name(zone_, node->fun()->name());
952 Local<Native> native(zone_, Native::New(zone_, type, name));
956 moduleData_->natives->set(zone_, node->fun()->slot(), native);
960 // If the function has no body, then it's just a declaration.
962 assert(node->fun()->isForward() || node->fun()->isPublic());
966 BytecodeCompiler child(zone_, tree_, moduleData_);
967 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
971 assert(!moduleData_->module->getFunction(node->fun()->slot()));
972 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
976 BytecodeCompiler::visit(EnumStatement *node)
978 // :TODO: eventually, we'll export these.
982 BytecodeCompiler::visit(StructureStatement *node)
987 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
989 // STOP doubles as RETURNVOID.
992 if (__ oom() || zone_->hasPendingException())
995 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
998 memcpy(bytes->raw(), __ buffer(), __ length());
1000 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
1001 assert(locals_->at(i));
1003 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
1007 for (size_t i = 0; i < __ objects()->length(); i++)
1008 objects->set(zone_, i, *(__ objects()->at(i)));
1010 Local<ByteArray> safepoints(zone_);
1012 // If any stack slots hold gc pointers at potential failure points in the
1013 // bytecode, record them now. We do this by building a list of tuples;
1014 // each tuple contains the pc offset, and a bitset containing which stack
1015 // slots must be scanned by the gc.
1016 if (__ oopRecords()->length()) {
1017 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
1018 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
1019 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
1023 Address cursor = safepoints->raw();
1024 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
1025 OopRecord *record = __ oopRecords()->at(i);
1026 assert(record->depth <= __ maxStackDepth());
1028 *reinterpret_cast<unsigned *>(cursor) = record->offset;
1030 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
1031 for (unsigned i = 0; i < record->depth; i++)
1032 bits[i / 8] |= (1 << (i % 8));
1034 cursor += bytesPerSafepoint;
1037 assert(cursor == safepoints->raw() + gcmapSize);
1040 Local<ByteArray> sourcemap(zone_);
1041 if (__ sourcemapLength()) {
1042 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
1046 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
1049 Local<Code> code(zone_);
1051 code = Code::New(zone_,
1056 moduleData_->module,
1064 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1066 // Note: No need to record a life slot here, since it's part of the Frame.
1067 AutoEnterScope functionScope(this, node->scope());
1069 // Set the initial line, because line mappings are deltas.
1070 __ setFirstLine(node->pos().line);
1071 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1073 node->body()->accept(this);
1075 unsigned nargs = node->scope()->numArguments();
1076 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1080 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1081 args->set(zone_, i, node->scope()->getArgument(i)->type());
1084 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1088 Local<Type> type(zone_, node->fun()->type());
1089 Local<String> name(zone_, node->fun()->name());
1090 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1094 // If this function had a forward, export it as a named public.
1095 if (node->fun()->isPublic()) {
1096 assert(node->fun()->isPublic());
1097 ScopedRoot<Function> root(fun);
1100 if (!moduleData_->publics.append(root))
1104 Disassemble(code, stdout);
1110 BytecodeCompiler::compile()
1112 AutoEnterScope scope(this, tree_->scope());
1114 ModuleData moduleData;
1116 moduleData_ = &moduleData;
1117 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1122 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1123 if (!moduleData_->natives)
1126 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1127 Statement *statement = tree_->statements()->at(i);
1128 statement->accept(this);
1131 if (zone_->hasPendingException())
1135 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1136 assert(moduleData_->module->getFunction(i));
1137 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1138 assert(moduleData_->natives->at(i));
1141 if (moduleData_->publics.length()) {
1142 Local<Function> fun(zone_);
1143 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1148 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1149 list->set(zone_, i, moduleData_->publics[i]);
1151 moduleData_->module->setPublics(list);
1154 moduleData_->module->setNatives(moduleData_->natives);
1156 if (scope_->numEnvironmentVars()) {
1157 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1161 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1165 moduleData_->module->setEnvironment(environment);
1168 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1172 Disassemble(code, stdout);
1174 moduleData_->module->setCode(code);
1176 return moduleData_->module;