Parser support for public variables.
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());
725 BytecodeCompiler::visit(StringLiteral *node)
727 Local<Type> type(zone_, node->type());
728 Local<ArrayMap> map(zone_, ArrayMap::Attach(zone_, type));
732 Local<Array> array(zone_,
733 Array::NewString(zone_,
735 node->string()->raw(),
736 node->string()->length(),
741 __ note_position(node->pos());
746 BytecodeCompiler::getEnvSlot(Variable *var)
748 assert(var->scope()->kind() == Scope::GLOBAL);
753 BytecodeCompiler::setEnvSlot(Variable *var)
755 assert(var->scope()->kind() == Scope::GLOBAL);
760 BytecodeCompiler::visit(VariableDeclaration *node)
762 Variable *var = node->variable();
764 Local<Type> type(zone_, var->type());
765 if (type->isArray()) {
766 Local<ArrayMap> map(zone_, ArrayMap::Attach(zone_, type));
767 __ note_position(node->pos());
768 if (var->scope()->kind() == Scope::GLOBAL)
769 __ newarray(map, Heap::Tenure_Old);
770 else if (var->needsHeapAllocation())
771 __ newarray(map, Heap::Tenure_Default);
773 __ newarray(map, Heap::Tenure_Stack);
775 if (node->initialization()) {
776 node->initialization()->accept(this);
777 __ note_position(node->pos());
780 } else if (type->isStruct()) {
781 Local<Map> map(zone_, type->newMap());
782 if (var->scope()->kind() == Scope::GLOBAL)
783 __ newstruct(map, Heap::Tenure_Old);
784 else if (var->needsHeapAllocation())
785 __ newstruct(map, Heap::Tenure_Default);
787 __ newstruct(map, Heap::Tenure_Stack);
789 if (node->initialization()) {
790 node->initialization()->accept(this);
792 if (type->isInt32OrEnum() || type->isBool()) {
795 assert(type->isFloat());
801 if (var->location() == Variable::STACK) {
803 locals_->set(zone_, var->slot(), var->type());
805 assert(var->location() == Variable::ENVIRONMENT);
813 BytecodeCompiler::visit(CallExpression *node)
815 Local<Type> funType(zone_, node->callee()->type());
817 // Sniff out natives, which have a specific opcode and are guaranteed to
818 // be called via name proxy.
819 NamedFunction *native = NULL;
820 if (funType->isNative()) {
821 native = node->callee()->toNameProxy()->binding()->toNamedFunction();
824 Local<Type> given(zone_);
825 for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
826 Expression *arg = node->arguments()->at(i);
830 // We need to compute a reference if the parameter was in the form:
832 // Note this form does not allow: f({})
834 // We also need to compute references for all variadic native
835 // arguments, but arrays and references are already in the
837 bool mustPushReference = false;
838 if (i >= funType->parameters()->length()) {
839 assert(funType->isNative() && funType->isNativeVariadic());
840 mustPushReference = !given->isArray() && !given->isReference();
841 } else if (funType->parameterAt(i)->isReference()) {
842 mustPushReference = !given->isReference();
845 // If a native is passed to a native, it must only be one level deep.
846 assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
848 if (mustPushReference) {
849 if (arg->isNameProxy()) {
850 // :TODO: test with functions and globals
851 // Compute a reference to the name.
852 Variable *var = arg->toNameProxy()->binding()->toVariable();
853 __ note_position(node->pos());
855 } else if (arg->isIndexExpression()) {
856 IndexExpression *expr = arg->toIndexExpression();
857 expr->left()->accept(this);
858 expr->right()->accept(this);
859 __ note_position(expr->pos());
861 } else if (arg->isFieldExpression()) {
862 FieldExpression *expr = arg->toFieldExpression();
863 expr->left()->accept(this);
864 __ note_position(expr->pos());
865 __ fieldref_lifo(expr->fieldIndex());
867 // We don't know how to, or can't, build a direct reference to
868 // this type. Make an indirect one, by taking the top of the
869 // stack and boxing it up.
871 __ note_position(node->pos());
879 unsigned argc = node->arguments()->length();
881 // If there are missing arguments, then we're not done and have to push
882 // default parameters.
883 Local<Box> box(zone_);
884 Local<Array> array(zone_);
885 Local<ArrayMap> map(zone_);
886 for (size_t i = node->arguments()->length(); i < funType->parameters()->length(); i++) {
887 box = Box::cast(funType->defaults()->at(i));
889 if (box->type()->isPod()) {
890 if (box->type()->pod() == PrimitiveType_Int32)
891 __ int_(box->toInt32());
892 else if (box->type()->pod() == PrimitiveType_Float)
893 __ float_(box->toFloat());
897 if (funType->parameterAt(i)->isReference()) {
898 __ note_position(node->pos());
902 assert(box->type()->isArray());
904 array = Array::cast(box->toObject());
905 if (funType->parameterAt(i)->isConst()) {
908 // We need to copy the array since it's mutable. Note that
909 // although COPYARRAY returns its left-hand side, this may
910 // change in the future, we do the same thing and dupe the
911 // newly allocated array.
912 map = ArrayMap::cast(array->map());
913 __ newarray(map, Heap::Tenure_Stack);
924 __ note_position(node->pos());
926 __ callnative(native->slot(), argc);
929 node->callee()->accept(this);
936 BytecodeCompiler::visit(TernaryExpression *node)
938 Label success, failure, join;
940 visitForTest(node->condition(), &success, &failure, &success);
942 node->left()->accept(this);
946 node->right()->accept(this);
951 BytecodeCompiler::visit(IfStatement *node)
954 if (!node->ifFalse()) {
957 visitForTest(node->condition(), &success, &join, &success);
959 node->ifTrue()->accept(this);
963 visitForTest(node->condition(), &success, &next, &success);
965 node->ifTrue()->accept(this);
968 node->ifFalse()->accept(this);
975 BytecodeCompiler::visit(FunctionStatement *node)
977 if (!node->fun()->used())
980 if (node->fun()->isNative()) {
981 assert(!moduleData_->natives->at(node->fun()->slot()));
983 Local<Type> type(zone_, node->fun()->type());
984 Local<String> name(zone_, node->fun()->name());
985 Local<Native> native(zone_, Native::New(zone_, type, name));
989 moduleData_->natives->set(zone_, node->fun()->slot(), native);
993 // If the function has no body, then it's just a declaration.
995 assert(node->fun()->isForward() || node->fun()->isPublic());
999 BytecodeCompiler child(zone_, tree_, moduleData_);
1000 Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
1004 assert(!moduleData_->module->getFunction(node->fun()->slot()));
1005 moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
1009 BytecodeCompiler::visit(EnumStatement *node)
1011 // :TODO: eventually, we'll export these.
1015 BytecodeCompiler::visit(StructureStatement *node)
1020 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
1022 // STOP doubles as RETURNVOID.
1025 if (__ oom() || zone_->hasPendingException())
1028 Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
1031 memcpy(bytes->raw(), __ buffer(), __ length());
1033 for (size_t i = 0; i < scope->maxContainedLocals(); i++)
1034 assert(locals_->at(i));
1036 Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
1040 for (size_t i = 0; i < __ objects()->length(); i++)
1041 objects->set(zone_, i, *(__ objects()->at(i)));
1043 Local<ByteArray> safepoints(zone_);
1045 // If any stack slots hold gc pointers at potential failure points in the
1046 // bytecode, record them now. We do this by building a list of tuples;
1047 // each tuple contains the pc offset, and a bitset containing which stack
1048 // slots must be scanned by the gc.
1049 if (__ oopRecords()->length()) {
1050 size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
1051 size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
1052 safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
1056 Address cursor = safepoints->raw();
1057 for (size_t i = 0; i < __ oopRecords()->length(); i++) {
1058 OopRecord *record = __ oopRecords()->at(i);
1059 assert(record->depth <= __ maxStackDepth());
1061 *reinterpret_cast<unsigned *>(cursor) = record->offset;
1063 unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
1064 for (unsigned i = 0; i < record->depth; i++)
1065 bits[i / 8] |= (1 << (i % 8));
1067 cursor += bytesPerSafepoint;
1070 assert(cursor == safepoints->raw() + gcmapSize);
1073 Local<ByteArray> sourcemap(zone_);
1074 if (__ sourcemapLength()) {
1075 sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
1079 memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
1082 Local<Code> code(zone_);
1084 code = Code::New(zone_,
1089 moduleData_->module,
1097 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
1099 // Note: No need to record a life slot here, since it's part of the Frame.
1100 AutoEnterScope functionScope(this, node->scope());
1102 // Set the initial line, because line mappings are deltas.
1103 __ setFirstLine(node->pos().line);
1104 locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
1106 node->body()->accept(this);
1108 unsigned nargs = node->scope()->numArguments();
1109 Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
1113 for (size_t i = 0; i < node->scope()->numArguments(); i++)
1114 args->set(zone_, i, node->scope()->getArgument(i)->type());
1117 Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
1121 Local<Type> type(zone_, node->fun()->type());
1122 Local<String> name(zone_, node->fun()->name());
1123 Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
1127 // If this function had a forward, export it as a named public.
1128 if (node->fun()->isPublic()) {
1129 assert(node->fun()->isPublic());
1130 ScopedRoot<Function> root(fun);
1133 if (!moduleData_->publics.append(root))
1138 Disassemble(code, stdout);
1145 BytecodeCompiler::compile()
1147 AutoEnterScope scope(this, tree_->scope());
1149 ModuleData moduleData;
1151 moduleData_ = &moduleData;
1152 moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
1157 moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
1158 if (!moduleData_->natives)
1161 for (size_t i = 0; i < tree_->statements()->length(); i++) {
1162 Statement *statement = tree_->statements()->at(i);
1163 statement->accept(this);
1166 if (zone_->hasPendingException())
1170 for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
1171 assert(moduleData_->module->getFunction(i));
1172 for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
1173 assert(moduleData_->natives->at(i));
1176 if (moduleData_->publics.length()) {
1177 Local<Function> fun(zone_);
1178 Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
1183 for (size_t i = 0; i < moduleData_->publics.length(); i++)
1184 list->set(zone_, i, moduleData_->publics[i]);
1186 moduleData_->module->setPublics(list);
1189 moduleData_->module->setNatives(moduleData_->natives);
1191 if (scope_->numEnvironmentVars()) {
1192 Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
1196 Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
1200 moduleData_->module->setEnvironment(environment);
1203 Local<Code> code(zone_, compileCode(tree_->scope(), 1));
1207 Disassemble(code, stdout);
1209 moduleData_->module->setCode(code);
1211 return moduleData_->module;