Added runtime support for default arguments.
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"
33 typedef BytecodeEmitter::Label Label;
37 BytecodeCompiler::BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module)
40 emitter_(zone->pool()),
48 static inline TokenKind
49 ToCompare(Expression *expr)
51 if (expr->isBinaryExpression()) {
52 BinaryExpression *b = expr->toBinaryExpression();
53 if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
55 } else if (expr->isUnaryExpression()) {
56 UnaryExpression *u = expr->toUnaryExpression();
57 if (u->token() == TOK_NOT)
64 CompareTokenToJump(TokenKind kind)
78 assert(kind == TOK_LE);
84 TokenToOpcode(TokenKind kind)
120 assert(kind == TOK_BITXOR);
126 BytecodeCompiler::recordLifoSlot(Scope *scope)
131 if (Variable *var = scope->lifoSlot()) {
132 locals_->set(zone_, var->slot(), var->type());
133 __ savelifo(var->slot());
138 BytecodeCompiler::unwindTo(Scope *scope)
140 // Note: we only need to unwind to the outermost scope's LIFO slot.
141 Variable *var = NULL;
142 for (Scope *current = scope_; current != scope; current = current->enclosing()) {
143 if (Variable *slot = current->lifoSlot())
148 __ unwindlifo(var->slot());
152 BytecodeCompiler::visit(NameProxy *name)
154 switch (name->binding()->kind()) {
155 case BoundName::VARIABLE:
157 Variable *var = name->binding()->toVariable();
159 if (var->location() == Variable::STACK) {
161 assert(!name->type()->isReference());
162 } else if (var->location() == Variable::ENVIRONMENT) {
165 assert(var->location() == Variable::PARAMETER);
168 // If this argument is actually a reference, dereference it now.
169 if (var->type()->isReference())
170 __ getref(var->type()->contained());
175 case BoundName::FUNCTION:
177 NamedFunction *fun = name->binding()->toNamedFunction();
179 __ getmodule_fn(fun->slot());
185 NamedConstant *constant = name->binding()->toNamedConstant();
186 __ int_(constant->value().toInt());
193 BytecodeCompiler::visit(ExpressionStatement *node)
195 // Expressions leave their value on the top of the stack, so we make sure
196 // to balance the stack if they're used as a statement.
197 node->expression()->accept(this);
198 if (!node->expression()->type()->isVoid())
203 BytecodeCompiler::visit(Assignment *node)
205 Local<Type> type(zone_, node->expression()->type());
207 // The first step is three sub-steps:
209 // (1) Bind the left-hand side.
210 // -- If a cumulative operation (+=, etc):
211 // (2) Save a copy of the binding on the stack.
212 // (3) Dereference the binding to get a value.
214 if (node->lvalue()->isNameProxy()) {
215 NameProxy *proxy = node->lvalue()->toNameProxy();
216 Variable *var = proxy->binding()->toVariable();
219 if (var->type()->isReference() ||
220 var->type()->isArray() ||
221 var->type()->isStruct())
226 // Get the value if needed.
227 if (node->token() != TOK_ASSIGN) {
228 // += etc is not allowed on arrays.
229 assert(!var->type()->isArray());
231 if (var->type()->isReference()) {
233 __ getref(var->type()->contained());
238 } else if (node->lvalue()->isFieldExpression()) {
239 FieldExpression *expr = node->lvalue()->toFieldExpression();
240 __ note_position(expr->pos());
242 expr->left()->accept(this);
244 if (node->token() != TOK_ASSIGN)
247 if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray()))
248 __ getfield(expr->fieldIndex(), expr->type());
250 IndexExpression *index = node->lvalue()->toIndexExpression();
251 __ note_position(index->pos());
253 index->left()->accept(this);
254 index->right()->accept(this);
256 if (node->token() != TOK_ASSIGN)
259 if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray()))
260 __ getelem(index->type());
263 if (node->token() == TOK_ASSIGN) {
264 node->expression()->accept(this);
266 visitForALU(node->type(), node->expression());
268 TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
269 __ note_position(node->pos());
270 __ binary(TokenToOpcode(alu));
274 if (type->isArray()) {
275 // Unlike references, which can only be computed from a NamePRoxy,
276 // arrays can be computed from multiple lvalue types.
277 __ note_position(node->pos());
279 } else if (type->isStruct()) {
280 // Structs have value semantics, so emit a full copy. We don't break
281 // this down into u-ops, to avoid unnecessary bytecode explosion.
282 __ note_position(node->pos());
284 } else if (node->lvalue()->isNameProxy()) {
285 NameProxy *proxy = node->lvalue()->toNameProxy();
286 Variable *var = proxy->binding()->toVariable();
288 if (var->type()->isReference())
289 __ setref(var->type()->contained());
292 } else if (node->lvalue()->isFieldExpression()) {
293 FieldExpression *expr = node->lvalue()->toFieldExpression();
294 __ setfield(expr->fieldIndex(), expr->type());
296 IndexExpression *index = node->lvalue()->toIndexExpression();
297 __ setelem(index->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());
375 __ getelem(node->type());
377 if (node->postfix()) {
378 // The stack looks like this now:
381 // So we must use a PICK operation to rotate the stack into position.
382 __ roll3(); // INDEX VALUE ARRAY
383 __ roll3(); // VALUE ARRAY INDEX
384 __ pick3(); // VALUE ARRAY INDEX VALUE
389 if (node->type()->isFloat()) {
392 assert(node->type()->isInt32());
396 __ note_position(node->pos());
397 if (node->token() == TOK_INCREMENT)
403 if (expr->isNameProxy()) {
404 NameProxy *proxy = expr->toNameProxy();
405 Variable *var = proxy->binding()->toVariable();
407 if (var->type()->isReference())
408 __ setref(node->type());
411 } else if (expr->isFieldExpression()) {
412 FieldExpression *field = expr->toFieldExpression();
413 __ setfield(field->fieldIndex(), field->type());
415 assert(expr->isIndexExpression());
416 __ setelem(node->type());
425 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
428 if (needed->isFloat() && !node->type()->isFloat())
431 assert(needed->primitive() == node->type()->primitive());
435 GetCoercionType(TokenKind tok, Type *left, Type *right)
443 BytecodeCompiler::visit(UnaryExpression *node)
445 Local<Type> out(zone_, ((Expression *)node)->type());
447 switch (node->token()) {
450 Local<Type> type(zone_, node->expression()->type());
451 node->expression()->accept(this);
462 Local<Type> type(zone_, node->expression()->type());
463 node->expression()->accept(this);
474 Local<Type> from(zone_, node->expression()->type());
476 node->expression()->accept(this);
477 if (!from->isFloat() && out->isFloat())
479 else if (from->isFloat() && !out->isFloat())
485 assert(node->token() == TOK_TILDE);
486 node->expression()->accept(this);
493 BytecodeCompiler::visit(BinaryExpression *node)
495 if (node->token() == TOK_AND || node->token() == TOK_OR) {
496 Label success, failure, done;
497 visitForTest(node, &success, &failure, &success);
500 __ drop(1); // Otherwise, the stack depth will be +1 too much.
508 Local<Type> leftType(zone_, node->left()->type());
509 Local<Type> rightType(zone_, node->right()->type());
510 Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
512 visitForALU(coerce, node->left());
513 visitForALU(coerce, node->right());
515 Opcode op = TokenToOpcode(node->token());
516 if (node->type()->isFloat())
517 op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
519 __ note_position(node->pos());
524 BytecodeCompiler::visit(IndexExpression *node)
526 node->left()->accept(this);
527 node->right()->accept(this);
528 __ note_position(node->pos());
529 __ getelem(node->type());
533 BytecodeCompiler::visit(FieldExpression *node)
535 node->left()->accept(this);
536 __ getfield(node->fieldIndex(), node->type());
540 BytecodeCompiler::visit(ReturnStatement *node)
542 if (node->expression()) {
543 node->expression()->accept(this);
551 BytecodeCompiler::visitForTest(Expression *expression,
556 TokenKind token = ToCompare(expression);
557 if (token == TOK_EOF || token == TOK_NOT) {
558 if (token == TOK_EOF)
559 expression->accept(this);
561 expression->toUnaryExpression()->expression()->accept(this);
563 bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
565 Opcode op = jumpOnTrue ? OP_JT : OP_JF;
566 if (expression->type()->isFloat())
567 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
569 if (fallthrough == falseBranch)
570 __ j(op, trueBranch);
572 __ j(op, falseBranch);
576 BinaryExpression *binary = expression->toBinaryExpression();
578 if (token == TOK_AND || token == TOK_OR) {
580 if (token == TOK_AND)
581 visitForTest(binary->left(), &next, falseBranch, &next);
583 visitForTest(binary->left(), trueBranch, &next, &next);
585 visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
589 Opcode op = CompareTokenToJump(token);
590 if (fallthrough == trueBranch)
593 Local<Type> leftType(zone_, binary->left()->type());
594 Local<Type> rightType(zone_, binary->right()->type());
595 Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
597 visitForALU(coerce, binary->left());
598 visitForALU(coerce, binary->right());
599 if (coerce->isFloat())
600 op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
602 if (fallthrough == trueBranch)
603 __ j(op, falseBranch);
605 __ j(op, trueBranch);
609 BytecodeCompiler::visit(ForStatement *node)
611 AutoEnterScope forScope(this, node->scope());
613 recordLifoSlot(node->scope());
616 LoopScope loop(scope_, &join, &update, &loop_);
618 if (node->initialization())
619 node->initialization()->accept(this);
622 if (node->condition())
626 node->body()->accept(this);
630 node->update()->accept(this);
632 if (node->condition()) {
634 visitForTest(node->condition(), &header, &join, &join);
642 unwindTo(node->scope()->enclosing());
643 assert(__ stackDepth() == 0);
647 BytecodeCompiler::visit(WhileStatement *node)
653 LoopScope loop(scope_, &join, &header, &loop_);
655 if (node->token() == TOK_WHILE) {
658 visitForTest(node->condition(), &body, &join, &body);
660 node->body()->accept(this);
663 node->body()->accept(this);
664 visitForTest(node->condition(), &header, &join, &join);
671 BytecodeCompiler::visit(BreakStatement *node)
673 unwindTo(loop_->scope());
674 __ jump(loop_->break_());
678 BytecodeCompiler::visit(ContinueStatement *node)
680 unwindTo(loop_->scope());
681 __ jump(loop_->continue_());
685 BytecodeCompiler::visit(BlockStatement *node)
687 AutoEnterScope blockScope(this, node->scope());
689 recordLifoSlot(node->scope());
691 for (size_t i = 0; i < node->statements()->length(); i++) {
692 Statement *statement = node->statements()->at(i);
693 statement->accept(this);
694 assert(__ stackDepth() == 0);
697 // We don't bother emitting an "unwind" instruction for function scopes,
698 // since this is taken care of by the calling convention.
699 if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
700 unwindTo(node->scope()->enclosing());
704 BytecodeCompiler::visit(IntegerLiteral *node)
706 __ int_(node->value());
710 BytecodeCompiler::visit(BooleanLiteral *node)
712 if (node->token() == TOK_TRUE)
719 BytecodeCompiler::visit(FloatLiteral *node)
721 __ float_((float)node->value());
724 static inline ArrayMap *
725 BuildArrayMap(Zone *zone, Handle<Type> type)
728 return ArrayMap::cast(type->newMap());
730 Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
734 // Ensure the inner type has an array map if needed.
735 if (type->contained()->isArray()) {
736 Local<Type> contained(zone, type->contained());
737 if (!BuildArrayMap(zone, contained))
741 type->setNewMap(map);
746 BytecodeCompiler::visit(StringLiteral *node)
748 Local<Type> type(zone_, node->type());
749 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
753 Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
757 assert(array->length() >= node->string()->length());
758 memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
760 __ note_position(node->pos());
765 BytecodeCompiler::getEnvSlot(Variable *var)
767 assert(var->scope()->kind() == Scope::GLOBAL);
772 BytecodeCompiler::setEnvSlot(Variable *var)
774 assert(var->scope()->kind() == Scope::GLOBAL);
779 BytecodeCompiler::visit(VariableDeclaration *node)
781 Variable *var = node->variable();
783 Local<Type> type(zone_, var->type());
784 if (type->isArray()) {
785 Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
786 __ note_position(node->pos());
787 if (var->scope()->kind() == Scope::GLOBAL)
788 __ newarray(map, Heap::Tenure_Old);
789 else if (var->needsHeapAllocation())
790 __ newarray(map, Heap::Tenure_Default);
792 __ newarray(map, Heap::Tenure_Stack);
794 if (node->initialization()) {
795 node->initialization()->accept(this);
796 __ note_position(node->pos());
799 } else if (type->isStruct()) {
800 Local<Map> map(zone_, type->newMap());
801 if (var->scope()->kind() == Scope::GLOBAL)
802 __ newstruct(map, Heap::Tenure_Old);
803 else if (var->needsHeapAllocation())
804 __ newstruct(map, Heap::Tenure_Default);
806 __ newstruct(map, Heap::Tenure_Stack);
808 if (node->initialization()) {
809 node->initialization()->accept(this);
811 if (type->isInt32OrEnum() || type->isBool()) {
814 assert(type->isFloat());
820 if (var->location() == Variable::STACK) {
822 locals_->set(zone_, var->slot(), var->type());
824 assert(var->location() == Variable::ENVIRONMENT);
832 BytecodeCompiler::visit(CallExpression *node)
834 Local<Type> funType(zone_, node->callee()->type());
836 // Sniff out natives, which have a specific opcode and are guaranteed to
837 // be called via name proxy.
838 NamedFunction *native = NULL;
839 if (funType->isNative()) {
840 native = node->callee()->toNameProxy()->binding()->toNamedFunction();
843 Local<Type> given(zone_);
844 for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
845 Expression *arg = node->arguments()->at(i);
849 // We need to compute a reference if the parameter was in the form:
851 // Note this form does not allow: f({})
853 // We also need to compute references for all variadic native
854 // arguments, but arrays and references are already in the
856 bool mustPushReference = false;
857 if (i >= funType->parameters()->length()) {
858 assert(funType->isNative() && funType->isNativeVariadic());
859 mustPushReference = !given->isArray() && !given->isReference();
860 } else if (funType->parameterAt(i)->isReference()) {
861 mustPushReference = !given->isReference();
864 // If a native is passed to a native, it must only be one level deep.
865 assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
867 if (mustPushReference) {
868 if (arg->isNameProxy()) {
869 // :TODO: test with functions and globals
870 // Compute a reference to the name.
871 Variable *var = arg->toNameProxy()->binding()->toVariable();
872 __ note_position(node->pos());
874 } else if (arg->isIndexExpression()) {
875 IndexExpression *expr = arg->toIndexExpression();
876 expr->left()->accept(this);
877 expr->right()->accept(this);
878 __ note_position(expr->pos());
880 } else if (arg->isFieldExpression()) {
881 FieldExpression *expr = arg->toFieldExpression();
882 expr->left()->accept(this);
883 __ note_position(expr->pos());
884 __ fieldref_lifo(expr->fieldIndex());
886 // We don't know how to, or can't, build a direct reference to
887 // this type. Make an indirect one, by taking the top of the
888 // stack and boxing it up.
890 __ note_position(node->pos());
898 unsigned argc = node->arguments()->length();
900 // If there are missing arguments, then we're not done and have to push
901 // default parameters.
902 Local<Box> box(zone_);
903 for (size_t i = node->arguments()->length(); i < funType->parameters()->length(); i++) {
904 box = Box::cast(funType->defaults()->at(i));
906 if (box->type()->isPod()) {
907 if (box->type()->pod() == PrimitiveType_Int32)
908 __ int_(box->toInt32());
909 else if (box->type()->pod() == PrimitiveType_Float)
910 __ float_(box->toFloat());
917 if (funType->parameterAt(i)->isReference()) {
918 __ note_position(node->pos());
925 __ note_position(node->pos());
927 __ callnative(native->slot(), argc);
930 node->callee()->accept(this);
937 BytecodeCompiler::visit(TernaryExpression *node)
939 Label success, failure, join;
941 visitForTest(node->condition(), &success, &failure, &success);
943 node->left()->accept(this);
947 node->right()->accept(this);
952 BytecodeCompiler::visit(IfStatement *node)
955 if (!node->ifFalse()) {
958 visitForTest(node->condition(), &success, &join, &success);
960 node->ifTrue()->accept(this);
964 visitForTest(node->condition(), &success, &next, &success);
966 node->ifTrue()->accept(this);
969 node->ifFalse()->accept(this);
976 BytecodeCompiler::visit(FunctionStatement *node)
978 if (!node->fun()->used())
981 if (node->fun()->isNative()) {
982 assert(!moduleData_->natives->at(node->fun()->slot()));
984 Local<Type> type(zone_, node->fun()->type());
985 Local<String> name(zone_, node->fun()->name());
986 Local<Native> native(zone_, Native::New(zone_, type, name));
990 moduleData_->natives->set(zone_, node->fun()->slot(), native);
994 // If the function has no body, then it's just a declaration.
996 assert(node->fun()->isForward() || node->fun()->isPublic());
1000 BytecodeCompiler child(zone_, tree_, moduleData_);
1001 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
1005 assert(!moduleData_->module->getFunction(node->fun()->slot()));
1006 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
1010 BytecodeCompiler::visit(EnumStatement *node)
1012 // :TODO: eventually, we'll export these.
1016 BytecodeCompiler::visit(StructureStatement *node)
1021 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
1023 // STOP doubles as RETURNVOID.
1026 if (__ oom() || zone_->hasPendingException())
1029 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
1032 memcpy(bytes->raw(), __ buffer(), __ length());
1034 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
1035 assert(locals_->at(i));
1037 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
1041 for (size_t i = 0; i < __ objects()->length(); i++)
1042 objects->set(zone_, i, *(__ objects()->at(i)));
1044 Local<ByteArray> safepoints(zone_);
1046 // If any stack slots hold gc pointers at potential failure points in the
1047 // bytecode, record them now. We do this by building a list of tuples;
1048 // each tuple contains the pc offset, and a bitset containing which stack
1049 // slots must be scanned by the gc.
1050 if (__ oopRecords()->length()) {
1051 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
1052 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
1053 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
1057 Address cursor = safepoints->raw();
1058 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
1059 OopRecord *record = __ oopRecords()->at(i);
1060 assert(record->depth <= __ maxStackDepth());
1062 *reinterpret_cast<unsigned *>(cursor) = record->offset;
1064 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
1065 for (unsigned i = 0; i < record->depth; i++)
1066 bits[i / 8] |= (1 << (i % 8));
1068 cursor += bytesPerSafepoint;
1071 assert(cursor == safepoints->raw() + gcmapSize);
1074 Local<ByteArray> sourcemap(zone_);
1075 if (__ sourcemapLength()) {
1076 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
1080 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
1083 Local<Code> code(zone_);
1085 code = Code::New(zone_,
1090 moduleData_->module,
1098 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1100 // Note: No need to record a life slot here, since it's part of the Frame.
1101 AutoEnterScope functionScope(this, node->scope());
1103 // Set the initial line, because line mappings are deltas.
1104 __ setFirstLine(node->pos().line);
1105 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1107 node->body()->accept(this);
1109 unsigned nargs = node->scope()->numArguments();
1110 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1114 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1115 args->set(zone_, i, node->scope()->getArgument(i)->type());
1118 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1122 Local<Type> type(zone_, node->fun()->type());
1123 Local<String> name(zone_, node->fun()->name());
1124 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1128 // If this function had a forward, export it as a named public.
1129 if (node->fun()->isPublic()) {
1130 assert(node->fun()->isPublic());
1131 ScopedRoot<Function> root(fun);
1134 if (!moduleData_->publics.append(root))
1139 Disassemble(code, stdout);
1146 BytecodeCompiler::compile()
1148 AutoEnterScope scope(this, tree_->scope());
1150 ModuleData moduleData;
1152 moduleData_ = &moduleData;
1153 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1158 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1159 if (!moduleData_->natives)
1162 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1163 Statement *statement = tree_->statements()->at(i);
1164 statement->accept(this);
1167 if (zone_->hasPendingException())
1171 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1172 assert(moduleData_->module->getFunction(i));
1173 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1174 assert(moduleData_->natives->at(i));
1177 if (moduleData_->publics.length()) {
1178 Local<Function> fun(zone_);
1179 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1184 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1185 list->set(zone_, i, moduleData_->publics[i]);
1187 moduleData_->module->setPublics(list);
1190 moduleData_->module->setNatives(moduleData_->natives);
1192 if (scope_->numEnvironmentVars()) {
1193 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1197 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1201 moduleData_->module->setEnvironment(environment);
1204 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1208 Disassemble(code, stdout);
1210 moduleData_->module->setCode(code);
1212 return moduleData_->module;