Remove OP_BOUNDS in favor of an inline check.
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 index->left()->accept(this);
253 index->right()->accept(this);
255 if (node->token() != TOK_ASSIGN)
258 if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray()))
259 __ getelem(index->type());
262 if (node->token() == TOK_ASSIGN) {
263 node->expression()->accept(this);
265 visitForALU(node->type(), node->expression());
267 TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
268 __ note_position(node->pos());
269 __ binary(TokenToOpcode(alu));
273 if (type->isArray()) {
274 // Unlike references, which can only be computed from a NamePRoxy,
275 // arrays can be computed from multiple lvalue types.
276 __ note_position(node->pos());
278 } else if (type->isStruct()) {
279 // Structs have value semantics, so emit a full copy. We don't break
280 // this down into u-ops, to avoid unnecessary bytecode explosion.
281 __ note_position(node->pos());
283 } else if (node->lvalue()->isNameProxy()) {
284 NameProxy *proxy = node->lvalue()->toNameProxy();
285 Variable *var = proxy->binding()->toVariable();
287 if (var->type()->isReference())
288 __ setref(var->type()->contained());
291 } else if (node->lvalue()->isFieldExpression()) {
292 FieldExpression *expr = node->lvalue()->toFieldExpression();
293 __ setfield(expr->fieldIndex(), expr->type());
295 IndexExpression *index = node->lvalue()->toIndexExpression();
296 __ setelem(index->type());
301 BytecodeCompiler::visit(IncDecExpression *node)
303 // In the postfix case, the logic looks like:
305 // (2) Dup -> REF REF
306 // (3) Fetch -> REF VAL
307 // (4) Swap -> VAL REF
308 // (5) Pick -> VAL REF VAL
309 // (6) Add -> VAL REF VAL+1
310 // (7) Store -> VAL VAL+1
313 // In the prefix case, the logic looks like:
315 // (2) Dup -> REF REF
316 // (3) Fetch -> REF VAL
319 // (6) Add -> REF VAL+1
320 // (7) Store -> VAL+1
323 // This is squirrely enough such that we don't bother sharing too much
326 // Note: We simulate a "Pick" operation with a dup2+pop.
328 // This is steps (1) to (5).
329 Expression *expr = node->expression();
330 if (expr->isNameProxy()) {
331 NameProxy *proxy = expr->toNameProxy();
332 Variable *var = proxy->binding()->toVariable();
335 if (var->type()->isReference() || var->type()->isArray())
339 if (var->type()->isReference()) {
341 __ getref(node->type());
342 if (node->postfix()) {
347 assert(!var->type()->isArray());
352 } else if (expr->isFieldExpression()) {
353 FieldExpression *field = expr->toFieldExpression();
355 field->left()->accept(this);
357 __ getfield(field->fieldIndex(), field->type());
359 if (node->postfix()) {
360 // The stack looks like this now:
362 __ swap(); // VALUE OBJECT
363 __ pick2(); // VALUE OBJECT VALUE
366 IndexExpression *index = expr->toIndexExpression();
367 __ note_position(index->pos());
369 index->left()->accept(this);
370 index->right()->accept(this);
372 assert(!node->type()->isArray());
374 __ getelem(node->type());
376 if (node->postfix()) {
377 // The stack looks like this now:
380 // So we must use a PICK operation to rotate the stack into position.
381 __ roll3(); // INDEX VALUE ARRAY
382 __ roll3(); // VALUE ARRAY INDEX
383 __ pick3(); // VALUE ARRAY INDEX VALUE
388 if (node->type()->isFloat()) {
391 assert(node->type()->isInt32());
395 __ note_position(node->pos());
396 if (node->token() == TOK_INCREMENT)
402 if (expr->isNameProxy()) {
403 NameProxy *proxy = expr->toNameProxy();
404 Variable *var = proxy->binding()->toVariable();
406 if (var->type()->isReference())
407 __ setref(node->type());
410 } else if (expr->isFieldExpression()) {
411 FieldExpression *field = expr->toFieldExpression();
412 __ setfield(field->fieldIndex(), field->type());
414 assert(expr->isIndexExpression());
415 __ setelem(node->type());
424 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
427 if (needed->isFloat() && !node->type()->isFloat())
430 assert(needed->primitive() == node->type()->primitive());
434 GetCoercionType(TokenKind tok, Type *left, Type *right)
442 BytecodeCompiler::visit(UnaryExpression *node)
444 Local<Type> out(zone_, ((Expression *)node)->type());
446 switch (node->token()) {
449 Local<Type> type(zone_, node->expression()->type());
450 node->expression()->accept(this);
461 Local<Type> type(zone_, node->expression()->type());
462 node->expression()->accept(this);
473 Local<Type> from(zone_, node->expression()->type());
475 node->expression()->accept(this);
476 if (!from->isFloat() && out->isFloat())
478 else if (from->isFloat() && !out->isFloat())
484 assert(node->token() == TOK_TILDE);
485 node->expression()->accept(this);
492 BytecodeCompiler::visit(BinaryExpression *node)
494 if (node->token() == TOK_AND || node->token() == TOK_OR) {
495 Label success, failure, done;
496 visitForTest(node, &success, &failure, &success);
499 __ drop(1); // Otherwise, the stack depth will be +1 too much.
507 Local<Type> leftType(zone_, node->left()->type());
508 Local<Type> rightType(zone_, node->right()->type());
509 Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
511 visitForALU(coerce, node->left());
512 visitForALU(coerce, node->right());
514 Opcode op = TokenToOpcode(node->token());
515 if (node->type()->isFloat())
516 op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
518 __ note_position(node->pos());
523 BytecodeCompiler::visit(IndexExpression *node)
525 node->left()->accept(this);
526 node->right()->accept(this);
527 __ note_position(node->pos());
528 __ getelem(node->type());
532 BytecodeCompiler::visit(FieldExpression *node)
534 node->left()->accept(this);
535 __ getfield(node->fieldIndex(), node->type());
539 BytecodeCompiler::visit(ReturnStatement *node)
541 if (node->expression()) {
542 node->expression()->accept(this);
550 BytecodeCompiler::visitForTest(Expression *expression,
555 TokenKind token = ToCompare(expression);
556 if (token == TOK_EOF || token == TOK_NOT) {
557 if (token == TOK_EOF)
558 expression->accept(this);
560 expression->toUnaryExpression()->expression()->accept(this);
562 bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
564 Opcode op = jumpOnTrue ? OP_JT : OP_JF;
565 if (expression->type()->isFloat())
566 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
568 if (fallthrough == falseBranch)
569 __ j(op, trueBranch);
571 __ j(op, falseBranch);
575 BinaryExpression *binary = expression->toBinaryExpression();
577 if (token == TOK_AND || token == TOK_OR) {
579 if (token == TOK_AND)
580 visitForTest(binary->left(), &next, falseBranch, &next);
582 visitForTest(binary->left(), trueBranch, &next, &next);
584 visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
588 Opcode op = CompareTokenToJump(token);
589 if (fallthrough == trueBranch)
592 Local<Type> leftType(zone_, binary->left()->type());
593 Local<Type> rightType(zone_, binary->right()->type());
594 Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
596 visitForALU(coerce, binary->left());
597 visitForALU(coerce, binary->right());
598 if (coerce->isFloat())
599 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
601 if (fallthrough == trueBranch)
602 __ j(op, falseBranch);
604 __ j(op, trueBranch);
608 BytecodeCompiler::visit(ForStatement *node)
610 AutoEnterScope forScope(this, node->scope());
612 recordLifoSlot(node->scope());
615 LoopScope loop(scope_, &join, &update, &loop_);
617 if (node->initialization())
618 node->initialization()->accept(this);
621 if (node->condition())
625 node->body()->accept(this);
629 node->update()->accept(this);
631 if (node->condition()) {
633 visitForTest(node->condition(), &header, &join, &join);
641 unwindTo(node->scope()->enclosing());
642 assert(__ stackDepth() == 0);
646 BytecodeCompiler::visit(WhileStatement *node)
652 LoopScope loop(scope_, &join, &header, &loop_);
654 if (node->token() == TOK_WHILE) {
657 visitForTest(node->condition(), &body, &join, &body);
659 node->body()->accept(this);
662 node->body()->accept(this);
663 visitForTest(node->condition(), &header, &join, &join);
670 BytecodeCompiler::visit(BreakStatement *node)
672 unwindTo(loop_->scope());
673 __ jump(loop_->break_());
677 BytecodeCompiler::visit(ContinueStatement *node)
679 unwindTo(loop_->scope());
680 __ jump(loop_->continue_());
684 BytecodeCompiler::visit(BlockStatement *node)
686 AutoEnterScope blockScope(this, node->scope());
688 recordLifoSlot(node->scope());
690 for (size_t i = 0; i < node->statements()->length(); i++) {
691 Statement *statement = node->statements()->at(i);
692 statement->accept(this);
693 assert(__ stackDepth() == 0);
696 // We don't bother emitting an "unwind" instruction for function scopes,
697 // since this is taken care of by the calling convention.
698 if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
699 unwindTo(node->scope()->enclosing());
703 BytecodeCompiler::visit(IntegerLiteral *node)
705 __ int_(node->value());
709 BytecodeCompiler::visit(BooleanLiteral *node)
711 if (node->token() == TOK_TRUE)
718 BytecodeCompiler::visit(FloatLiteral *node)
720 __ float_((float)node->value());
723 static inline ArrayMap *
724 BuildArrayMap(Zone *zone, Handle<Type> type)
727 return ArrayMap::cast(type->newMap());
729 Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
733 // Ensure the inner type has an array map if needed.
734 if (type->contained()->isArray()) {
735 Local<Type> contained(zone, type->contained());
736 if (!BuildArrayMap(zone, contained))
740 type->setNewMap(map);
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->length() >= 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());
878 // We don't know how to, or can't, build a direct reference to
879 // this type. Make an indirect one, by taking the top of the
880 // stack and boxing it up.
882 __ note_position(node->pos());
890 __ note_position(node->pos());
892 __ callnative(native->slot(), node->arguments()->length());
893 __ drop(node->arguments()->length());
895 node->callee()->accept(this);
897 __ drop(node->arguments()->length() + 1);
902 BytecodeCompiler::visit(TernaryExpression *node)
904 Label success, failure, join;
906 visitForTest(node->condition(), &success, &failure, &success);
908 node->left()->accept(this);
912 node->right()->accept(this);
917 BytecodeCompiler::visit(IfStatement *node)
920 if (!node->ifFalse()) {
923 visitForTest(node->condition(), &success, &join, &success);
925 node->ifTrue()->accept(this);
929 visitForTest(node->condition(), &success, &next, &success);
931 node->ifTrue()->accept(this);
934 node->ifFalse()->accept(this);
941 BytecodeCompiler::visit(FunctionStatement *node)
943 if (!node->fun()->used())
946 if (node->fun()->isNative()) {
947 assert(!moduleData_->natives->at(node->fun()->slot()));
949 Local<Type> type(zone_, node->fun()->type());
950 Local<String> name(zone_, node->fun()->name());
951 Local<Native> native(zone_, Native::New(zone_, type, name));
955 moduleData_->natives->set(zone_, node->fun()->slot(), native);
959 // If the function has no body, then it's just a declaration.
961 assert(node->fun()->isForward() || node->fun()->isPublic());
965 BytecodeCompiler child(zone_, tree_, moduleData_);
966 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
970 assert(!moduleData_->module->getFunction(node->fun()->slot()));
971 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
975 BytecodeCompiler::visit(EnumStatement *node)
977 // :TODO: eventually, we'll export these.
981 BytecodeCompiler::visit(StructureStatement *node)
986 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
988 // STOP doubles as RETURNVOID.
991 if (__ oom() || zone_->hasPendingException())
994 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
997 memcpy(bytes->raw(), __ buffer(), __ length());
999 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
1000 assert(locals_->at(i));
1002 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
1006 for (size_t i = 0; i < __ objects()->length(); i++)
1007 objects->set(zone_, i, *(__ objects()->at(i)));
1009 Local<ByteArray> safepoints(zone_);
1011 // If any stack slots hold gc pointers at potential failure points in the
1012 // bytecode, record them now. We do this by building a list of tuples;
1013 // each tuple contains the pc offset, and a bitset containing which stack
1014 // slots must be scanned by the gc.
1015 if (__ oopRecords()->length()) {
1016 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
1017 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
1018 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
1022 Address cursor = safepoints->raw();
1023 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
1024 OopRecord *record = __ oopRecords()->at(i);
1025 assert(record->depth <= __ maxStackDepth());
1027 *reinterpret_cast<unsigned *>(cursor) = record->offset;
1029 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
1030 for (unsigned i = 0; i < record->depth; i++)
1031 bits[i / 8] |= (1 << (i % 8));
1033 cursor += bytesPerSafepoint;
1036 assert(cursor == safepoints->raw() + gcmapSize);
1039 Local<ByteArray> sourcemap(zone_);
1040 if (__ sourcemapLength()) {
1041 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
1045 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
1048 Local<Code> code(zone_);
1050 code = Code::New(zone_,
1055 moduleData_->module,
1063 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1065 // Note: No need to record a life slot here, since it's part of the Frame.
1066 AutoEnterScope functionScope(this, node->scope());
1068 // Set the initial line, because line mappings are deltas.
1069 __ setFirstLine(node->pos().line);
1070 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1072 node->body()->accept(this);
1074 unsigned nargs = node->scope()->numArguments();
1075 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1079 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1080 args->set(zone_, i, node->scope()->getArgument(i)->type());
1083 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1087 Local<Type> type(zone_, node->fun()->type());
1088 Local<String> name(zone_, node->fun()->name());
1089 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1093 // If this function had a forward, export it as a named public.
1094 if (node->fun()->isPublic()) {
1095 assert(node->fun()->isPublic());
1096 ScopedRoot<Function> root(fun);
1099 if (!moduleData_->publics.append(root))
1104 Disassemble(code, stdout);
1111 BytecodeCompiler::compile()
1113 AutoEnterScope scope(this, tree_->scope());
1115 ModuleData moduleData;
1117 moduleData_ = &moduleData;
1118 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1123 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1124 if (!moduleData_->natives)
1127 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1128 Statement *statement = tree_->statements()->at(i);
1129 statement->accept(this);
1132 if (zone_->hasPendingException())
1136 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1137 assert(moduleData_->module->getFunction(i));
1138 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1139 assert(moduleData_->natives->at(i));
1142 if (moduleData_->publics.length()) {
1143 Local<Function> fun(zone_);
1144 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1149 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1150 list->set(zone_, i, moduleData_->publics[i]);
1152 moduleData_->module->setPublics(list);
1155 moduleData_->module->setNatives(moduleData_->natives);
1157 if (scope_->numEnvironmentVars()) {
1158 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1162 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1166 moduleData_->module->setEnvironment(environment);
1169 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1173 Disassemble(code, stdout);
1175 moduleData_->module->setCode(code);
1177 return moduleData_->module;