src/BytecodeCompiler.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jul 28 16:42:38 2012 -0700 (2012-07-28)
changeset 108 9dcf58a17d32
parent 92 e3974e7360e9
child 137 be919b22085b
permissions -rw-r--r--
Stop using ccache on OS X.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     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)
    10  * any later version.
    11  * 
    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.
    15  *
    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/.
    18  */
    19 #include <string.h>
    20 #include "BytecodeCompiler.h"
    21 #include "Scopes.h"
    22 #include "Modules.h"
    23 #include "Types.h"
    24 #include "Array.h"
    25 #include "Strings.h"
    26 #include "Zone.h"
    27 #include "Environments.h"
    28 #include "Heap-inl.h"
    29 
    30 using namespace ke;
    31 
    32 typedef BytecodeEmitter::Label Label;
    33 
    34 #define __ emitter_.
    35 
    36 BytecodeCompiler::BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module)
    37   : zone_(zone),
    38     tree_(tree),
    39     emitter_(zone->pool()),
    40     locals_(NULL),
    41     scope_(NULL),
    42     moduleData_(module),
    43     loop_(NULL)
    44 {
    45 }
    46 
    47 static inline TokenKind
    48 ToCompare(Expression *expr)
    49 {
    50     if (expr->isBinaryExpression()) {
    51         BinaryExpression *b = expr->toBinaryExpression();
    52         if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
    53             return b->token();
    54     } else if (expr->isUnaryExpression()) {
    55         UnaryExpression *u = expr->toUnaryExpression();
    56         if (u->token() == TOK_NOT)
    57             return TOK_NOT;
    58     }
    59     return TOK_EOF;
    60 }
    61 
    62 static inline Opcode
    63 CompareTokenToJump(TokenKind kind)
    64 {
    65     switch (kind) {
    66       case TOK_EQUALS:
    67         return OP_JEQ;
    68       case TOK_NOTEQUALS:
    69         return OP_JNE;
    70       case TOK_GT:
    71         return OP_JG;
    72       case TOK_GE:
    73         return OP_JGE;
    74       case TOK_LT:
    75         return OP_JL;
    76       default:
    77         assert(kind == TOK_LE);
    78         return OP_JLE;
    79     }
    80 }
    81 
    82 static inline Opcode
    83 TokenToOpcode(TokenKind kind)
    84 {
    85     switch (kind) {
    86       case TOK_PLUS:
    87         return OP_ADD;
    88       case TOK_MINUS:
    89         return OP_SUB;
    90       case TOK_STAR:
    91         return OP_MUL;
    92       case TOK_SLASH:
    93         return OP_DIV;
    94       case TOK_PERCENT:
    95         return OP_MOD;
    96       case TOK_SHR:
    97         return OP_SHR;
    98       case TOK_USHR:
    99         return OP_USHR;
   100       case TOK_SHL:
   101         return OP_SHL;
   102       case TOK_BITAND:
   103         return OP_BITAND;
   104       case TOK_BITOR:
   105         return OP_BITOR;
   106       case TOK_GT:
   107           return OP_GT;
   108       case TOK_GE:
   109           return OP_GE;
   110       case TOK_LT:
   111           return OP_LT;
   112       case TOK_LE:
   113           return OP_LE;
   114       case TOK_EQUALS:
   115           return OP_EQ;
   116       case TOK_NOTEQUALS:
   117           return OP_NE;
   118       default:
   119         assert(kind == TOK_BITXOR);
   120         return OP_BITXOR;
   121     }
   122 }
   123 
   124 void
   125 BytecodeCompiler::recordLifoSlot(Scope *scope)
   126 {
   127     if (!scope)
   128         return;
   129 
   130     if (Variable *var = scope->lifoSlot()) {
   131         locals_->set(zone_, var->slot(), var->type());
   132         __ savelifo(var->slot());
   133     }
   134 }
   135 
   136 void
   137 BytecodeCompiler::unwindTo(Scope *scope)
   138 {
   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())
   143             var = slot;
   144     }
   145 
   146     if (var)
   147         __ unwindlifo(var->slot());
   148 }
   149 
   150 void
   151 BytecodeCompiler::visit(NameProxy *name)
   152 {
   153     switch (name->binding()->kind()) {
   154       case BoundName::VARIABLE:
   155       {
   156         Variable *var = name->binding()->toVariable();
   157 
   158         if (var->location() == Variable::STACK) {
   159             __ getlocal(var);
   160             assert(!name->type()->isReference());
   161         } else if (var->location() == Variable::ENVIRONMENT) {
   162             getEnvSlot(var);
   163         } else {
   164             assert(var->location() == Variable::PARAMETER);
   165             __ getarg(var);
   166 
   167             // If this argument is actually a reference, dereference it now.
   168             if (var->type()->isReference())
   169                 __ getref(var->type()->contained()->primitive());
   170         }
   171         break;
   172       }
   173 
   174       case BoundName::FUNCTION:
   175       {
   176         NamedFunction *fun = name->binding()->toNamedFunction();
   177         __ module();
   178         __ getmodule_fn(fun->slot());
   179         break;
   180       }
   181 
   182       default:
   183       {
   184         NamedConstant *constant = name->binding()->toNamedConstant();
   185         __ int_(constant->value().toInt());
   186         break;
   187       }
   188     }
   189 }
   190 
   191 void
   192 BytecodeCompiler::visit(ExpressionStatement *node)
   193 {
   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())
   198         __ pop();
   199 }
   200 
   201 void
   202 BytecodeCompiler::visit(Assignment *node)
   203 {
   204     // The first step is three sub-steps:
   205     //
   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.
   210     //
   211     if (node->lvalue()->isNameProxy()) {
   212         NameProxy *proxy = node->lvalue()->toNameProxy();
   213         Variable *var = proxy->binding()->toVariable();
   214 
   215         // Bind.
   216         if (var->type()->isReference() || var->type()->isArray())
   217             __ get(var);
   218 
   219         // Get the value if needed.
   220         if (node->token() != TOK_ASSIGN) {
   221             // += etc is not allowed on arrays.
   222             assert(!var->type()->isArray());
   223 
   224             if (var->type()->isReference()) {
   225                 __ dup();
   226                 __ getref(var->type()->contained()->primitive());
   227             } else {
   228                 __ get(var);
   229             }
   230         }
   231     } else {
   232         IndexExpression *index = node->lvalue()->toIndexExpression();
   233         __ note_position(index->pos());
   234 
   235         // Note: Eventually, watch out for a whole here. An expression like:
   236         //
   237         //   (a[4] = (5, a = []))
   238         //
   239         // Could shrink array, so we need to either bind a[i] to a Reference,
   240         // or keep the bounds check near the set. This is just a semantic
   241         // decision.
   242         index->left()->accept(this);
   243         index->right()->accept(this);
   244 
   245         if (node->token() != TOK_ASSIGN) {
   246             __ dup2();
   247             __ bounds();
   248             if (node->type()->isArray())
   249                 __ getarray_o();
   250             else
   251                 __ getarray(node->type()->primitive());
   252         }
   253     }
   254 
   255     if (node->token() == TOK_ASSIGN) {
   256         node->expression()->accept(this);
   257     } else {
   258         visitForALU(node->type(), node->expression());
   259 
   260         TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
   261         __ note_position(node->pos());
   262         __ binary(TokenToOpcode(alu));
   263     }
   264 
   265     // Emit the store.
   266     if (node->expression()->type()->isArray()) {
   267         // Unlike references, which can only be computed from a NamePRoxy,
   268         // arrays can be computed from multiple lvalue types.
   269         __ note_position(node->pos());
   270         __ arraycopy();
   271     } else if (node->lvalue()->isNameProxy()) {
   272         NameProxy *proxy = node->lvalue()->toNameProxy();
   273         Variable *var = proxy->binding()->toVariable();
   274 
   275         if (var->type()->isReference())
   276             __ setref(var->type()->contained()->primitive());
   277         else
   278             __ set(var);
   279     } else {
   280         IndexExpression *index = node->lvalue()->toIndexExpression();
   281         // :TODO: get rid of the BOUNDS instruction, embed it in g/setarray*?
   282         // We can't emit it here because the stack is slightly out of position.
   283         // __ bounds();
   284         __ setarray(index->left()->type()->contained()->primitive());
   285     }
   286 }
   287 
   288 void
   289 BytecodeCompiler::visit(IncDecExpression *node)
   290 {
   291     // In the postfix case, the logic looks like:
   292     //      (1) Bind       -> REF
   293     //      (2) Dup        -> REF REF
   294     //      (3) Fetch      -> REF VAL
   295     //      (4) Swap       -> VAL REF
   296     //      (5) Pick       -> VAL REF VAL
   297     //      (6) Add        -> VAL REF VAL+1
   298     //      (7) Store      -> VAL VAL+1
   299     //      (8) Drop       -> VAL
   300     //
   301     // In the prefix case, the logic looks like:
   302     //      (1) Bind       -> REF
   303     //      (2) Dup        -> REF REF
   304     //      (3) Fetch      -> REF VAL
   305     //      --
   306     //      --
   307     //      (6) Add        -> REF VAL+1
   308     //      (7) Store      -> VAL+1
   309     //      --
   310     //
   311     // This is squirrely enough such that we don't bother sharing too much
   312     // with the above.
   313     // 
   314     // Note: We simulate a "Pick" operation with a dup2+pop.
   315 
   316     // This is steps (1) to (5).
   317     Expression *expr = node->expression();
   318     if (expr->isNameProxy()) {
   319         NameProxy *proxy = expr->toNameProxy();
   320         Variable *var = proxy->binding()->toVariable();
   321 
   322         // Bind.
   323         if (var->type()->isReference() || var->type()->isArray())
   324             __ get(var);
   325 
   326         // Dup and fetch.
   327         if (var->type()->isReference()) {
   328             __ dup();
   329             __ getref(node->type()->primitive());
   330             if (node->postfix()) {
   331                 __ swap();
   332                 __ pick2();
   333             }
   334         } else {
   335             assert(!var->type()->isArray());
   336             __ get(var);
   337             if (node->postfix())
   338                 __ dup();
   339         }
   340     } else {
   341         IndexExpression *index = expr->toIndexExpression();
   342         __ note_position(index->pos());
   343 
   344         index->left()->accept(this);
   345         index->right()->accept(this);
   346 
   347         assert(!node->type()->isArray());
   348         __ bounds();
   349         __ dup2();
   350         __ getarray(node->type()->primitive());
   351 
   352         if (node->postfix()) {
   353             // The stack looks like this now:
   354             // ARRAY INDEX VALUE
   355             //
   356             // So we must use a PICK operation to rotate the stack into position.
   357             __ roll3();         // INDEX VALUE ARRAY
   358             __ roll3();         // VALUE ARRAY INDEX
   359             __ pick3();         // VALUE ARRAY INDEX VALUE
   360         }
   361     }
   362 
   363     // Perform step (6).
   364     if (node->type()->isFloat()) {
   365         __ float_(1.0);
   366     } else {
   367         assert(node->type()->isInt32());
   368         __ int_(1);
   369     }
   370 
   371     __ note_position(node->pos());
   372     if (node->token() == TOK_INCREMENT)
   373         __ binary(OP_ADD);
   374     else
   375         __ binary(OP_SUB);
   376 
   377     // Perform step (7).
   378     if (expr->isNameProxy()) {
   379         NameProxy *proxy = expr->toNameProxy();
   380         Variable *var = proxy->binding()->toVariable();
   381 
   382         if (var->type()->isReference())
   383             __ setref(node->type()->primitive());
   384         else
   385             __ set(var);
   386     } else {
   387         __ setarray(node->type()->primitive());
   388     }
   389 
   390     // Perform step (8).
   391     if (node->postfix())
   392         __ pop();
   393 }
   394 
   395 void
   396 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
   397 {
   398     node->accept(this);
   399     if (needed->isFloat() && !node->type()->isFloat())
   400         __ cvt_i2f();
   401     else
   402         assert(needed->primitive() == node->type()->primitive());
   403 }
   404 
   405 static inline Type *
   406 GetCoercionType(TokenKind tok, Type *left, Type *right)
   407 {
   408     if (left->isFloat())
   409         return left;
   410     return right;
   411 }
   412 
   413 void
   414 BytecodeCompiler::visit(UnaryExpression *node)
   415 {
   416     switch (node->token()) {
   417       case TOK_MINUS:
   418       {
   419         Local<Type> type(zone_, node->expression()->type());
   420         node->expression()->accept(this);
   421         
   422         if (node->type()->isFloat())
   423             __ neg_f();
   424         else
   425             __ neg();
   426         break;
   427       }
   428 
   429       case TOK_NOT:
   430       {
   431         Local<Type> type(zone_, node->expression()->type());
   432         node->expression()->accept(this);
   433         
   434         if (node->type()->isFloat())
   435             __ not_f();
   436         else
   437             __ not_();
   438         break;
   439       }
   440 
   441       default:
   442         assert(node->token() == TOK_TILDE);
   443         node->expression()->accept(this);
   444         __ bitnot();
   445         break;
   446     }
   447 }
   448 
   449 void
   450 BytecodeCompiler::visit(BinaryExpression *node)
   451 {
   452     if (node->token() == TOK_AND || node->token() == TOK_OR) {
   453         Label success, failure, done;
   454         visitForTest(node, &success, &failure, &success);
   455         __ bind(&success);
   456         __ int_(1);
   457         __ drop(1);         // Otherwise, the stack depth will be +1 too much.
   458         __ jump(&done);
   459         __ bind(&failure);
   460         __ int_(0);
   461         __ bind(&done);
   462         return;
   463     }
   464 
   465     Local<Type> leftType(zone_, node->left()->type());
   466     Local<Type> rightType(zone_, node->right()->type());
   467     Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
   468 
   469     visitForALU(coerce, node->left());
   470     visitForALU(coerce, node->right());
   471 
   472     Opcode op = TokenToOpcode(node->token());
   473     if (node->type()->isFloat())
   474         op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
   475 
   476     __ note_position(node->pos());
   477     __ binary(op);
   478 }
   479 
   480 void
   481 BytecodeCompiler::visit(IndexExpression *node)
   482 {
   483     node->left()->accept(this);
   484     node->right()->accept(this);
   485     __ note_position(node->pos());
   486     __ bounds();
   487 
   488     if (node->type()->isArray())
   489         __ getarray_o();
   490     else
   491         __ getarray(node->type()->primitive());
   492 }
   493 
   494 void
   495 BytecodeCompiler::visit(ReturnStatement *node)
   496 {
   497     if (node->expression()) {
   498         node->expression()->accept(this);
   499         __ return_();
   500     } else {
   501         __ returnvoid();
   502     }
   503 }
   504 
   505 void
   506 BytecodeCompiler::visitForTest(Expression *expression,
   507                                Label *trueBranch,
   508                                Label *falseBranch,
   509                                Label *fallthrough)
   510 {
   511     TokenKind token = ToCompare(expression);
   512     if (token == TOK_EOF || token == TOK_NOT) {
   513         if (token == TOK_EOF)
   514             expression->accept(this);
   515         else
   516             expression->toUnaryExpression()->expression()->accept(this);
   517         
   518         bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
   519 
   520         Opcode op = jumpOnTrue ? OP_JT : OP_JF;
   521         if (expression->type()->isFloat())
   522             op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
   523 
   524         if (fallthrough == falseBranch)
   525             __ j(op, trueBranch);
   526         else
   527             __ j(op, falseBranch); 
   528         return;
   529     }
   530 
   531     BinaryExpression *binary = expression->toBinaryExpression();
   532 
   533     if (token == TOK_AND || token == TOK_OR) {
   534         Label next;
   535         if (token == TOK_AND)
   536             visitForTest(binary->left(), &next, falseBranch, &next);
   537         else
   538             visitForTest(binary->left(), trueBranch, &next, &next);
   539         __ bind(&next);
   540         visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
   541         return;
   542     }
   543 
   544     Opcode op = CompareTokenToJump(token);
   545     if (fallthrough == trueBranch)
   546         op = Invert(op);
   547 
   548     Local<Type> leftType(zone_, binary->left()->type());
   549     Local<Type> rightType(zone_, binary->right()->type());
   550     Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
   551 
   552     visitForALU(coerce, binary->left());
   553     visitForALU(coerce, binary->right());
   554     if (coerce->isFloat())
   555         op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
   556 
   557     if (fallthrough == trueBranch)
   558         __ j(op, falseBranch);
   559     else
   560         __ j(op, trueBranch);
   561 }
   562 
   563 void
   564 BytecodeCompiler::visit(ForStatement *node)
   565 {
   566     AutoEnterScope forScope(this, node->scope());
   567 
   568     recordLifoSlot(node->scope());
   569 
   570     Label join, update;
   571     LoopScope loop(scope_, &join, &update, &loop_);
   572 
   573     if (node->initialization())
   574         node->initialization()->accept(this);
   575 
   576     Label header, test;
   577     if (node->condition())
   578         __ jump(&test);
   579 
   580     __ bind(&header);
   581     node->body()->accept(this);
   582 
   583     __ bind(&update);
   584     if (node->update())
   585         node->update()->accept(this);
   586 
   587     if (node->condition()) {
   588         __ bind(&test);
   589         visitForTest(node->condition(), &header, &join, &join);
   590     } else {
   591         __ jump(&header);
   592     }
   593 
   594     __ bind(&join);
   595 
   596     if (node->scope())
   597         unwindTo(node->scope()->enclosing());
   598     assert(__ stackDepth() == 0);
   599 }
   600 
   601 void
   602 BytecodeCompiler::visit(WhileStatement *node)
   603 {
   604     Label header, join;
   605 
   606     __ bind(&header);
   607 
   608     LoopScope loop(scope_, &join, &header, &loop_);
   609 
   610     if (node->token() == TOK_WHILE) {
   611         Label body;
   612 
   613         visitForTest(node->condition(), &body, &join, &body);
   614         __ bind(&body);
   615         node->body()->accept(this);
   616         __ jump(&header);
   617     } else {
   618         node->body()->accept(this);
   619         visitForTest(node->condition(), &header, &join, &join);
   620     }
   621 
   622     __ bind(&join);
   623 }
   624 
   625 void
   626 BytecodeCompiler::visit(BreakStatement *node)
   627 {
   628     unwindTo(loop_->scope());
   629     __ jump(loop_->break_());
   630 }
   631 
   632 void
   633 BytecodeCompiler::visit(ContinueStatement *node)
   634 {
   635     unwindTo(loop_->scope());
   636     __ jump(loop_->continue_());
   637 }
   638 
   639 void
   640 BytecodeCompiler::visit(BlockStatement *node)
   641 {
   642     AutoEnterScope blockScope(this, node->scope());
   643 
   644     recordLifoSlot(node->scope());
   645 
   646     for (size_t i = 0; i < node->statements()->length(); i++) {
   647         Statement *statement = node->statements()->at(i);
   648         statement->accept(this);
   649         assert(__ stackDepth() == 0);
   650     }
   651 
   652     // We don't bother emitting an "unwind" instruction for function scopes,
   653     // since this is taken care of by the calling convention.
   654     if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
   655         unwindTo(node->scope()->enclosing());
   656 }
   657 
   658 void
   659 BytecodeCompiler::visit(IntegerLiteral *node)
   660 {
   661     __ int_(node->value());
   662 }
   663 
   664 void
   665 BytecodeCompiler::visit(FloatLiteral *node)
   666 {
   667     __ float_((float)node->value());
   668 }
   669 
   670 static inline ArrayMap *
   671 BuildArrayMap(Zone *zone, Handle<Type> type)
   672 {
   673     Local<ArrayMap> contained(zone);
   674     if (type->levels() > 1) {
   675         Local<Type> containedType(zone, type->contained());
   676         contained = BuildArrayMap(zone, containedType);
   677         if (!contained)
   678             return NULL;
   679     }
   680 
   681     return ArrayMap::New(zone, type, contained);
   682 }
   683 
   684 void
   685 BytecodeCompiler::visit(StringLiteral *node)
   686 {
   687     Local<Type> type(zone_, node->type());
   688     Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
   689     if (!map)
   690         return;
   691 
   692     Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
   693     if (!array)
   694         return;
   695 
   696     assert(array->nelements() >= node->string()->length());
   697     memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
   698 
   699     __ note_position(node->pos());
   700     __ object(array);
   701 }
   702 
   703 void
   704 BytecodeCompiler::getEnvSlot(Variable *var)
   705 {
   706     assert(var->scope()->kind() == Scope::GLOBAL);
   707     __ getglobal(var);
   708 }
   709 
   710 void
   711 BytecodeCompiler::setEnvSlot(Variable *var)
   712 {
   713     assert(var->scope()->kind() == Scope::GLOBAL);
   714     __ setglobal(var);
   715 }
   716 
   717 void
   718 BytecodeCompiler::visit(VariableDeclaration *node)
   719 {
   720     Variable *var = node->variable();
   721     
   722     Local<Type> type(zone_, var->type());
   723     if (type->isArray()) {
   724         Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
   725         __ note_position(node->pos());
   726         if (var->scope()->kind() == Scope::GLOBAL)
   727             __ newarray(map, Heap::Tenure_Old);
   728         else if (var->needsHeapAllocation())
   729             __ newarray(map, Heap::Tenure_Default);
   730         else
   731             __ newarray(map, Heap::Tenure_Stack);
   732 
   733         if (node->initialization()) {
   734             node->initialization()->accept(this);
   735             __ note_position(node->pos());
   736             __ arraycopy();
   737         }
   738     } else {
   739         if (node->initialization()) {
   740             node->initialization()->accept(this);
   741         } else {
   742             if (!node->postDimensions())
   743                 __ int_(0);
   744         }
   745     }
   746 
   747     if (var->location() == Variable::STACK) {
   748         __ setlocal(var);
   749         locals_->set(zone_, var->slot(), var->type());
   750     } else {
   751         assert(var->location() == Variable::ENVIRONMENT);
   752         setEnvSlot(var);
   753     }
   754 
   755     __ pop();
   756 }
   757 
   758 void
   759 BytecodeCompiler::visit(CallExpression *node)
   760 {
   761     Local<Type> funType(zone_, node->callee()->type());
   762 
   763     // Sniff out natives, which have a specific opcode and are guaranteed to
   764     // be called via name proxy.
   765     NamedFunction *native = NULL;
   766     if (funType->isNative()) {
   767         native = node->callee()->toNameProxy()->binding()->toNamedFunction();
   768     }
   769 
   770     for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
   771         Expression *arg = node->arguments()->at(i);
   772 
   773         // We need to compute a reference if the parameter was in the form:
   774         //  f(&a)
   775         // Note this form does not allow: f({})
   776         // 
   777         // We also need to compute references for all variadic native
   778         // arguments, but arrays and references are already in the
   779         // appropriate form.
   780         Local<Type> given(zone_, arg->type());
   781         bool mustPushReference = false;
   782         if (i >= funType->parameters()->length()) {
   783             assert(funType->isNative() && funType->isNativeVariadic());
   784             mustPushReference = !given->isArray() && !given->isReference();
   785         } else if (funType->parameterAt(i)->isReference()) {
   786             mustPushReference = !given->isReference();
   787         }
   788 
   789         // If a native is passed to a native, it must only be one level deep.
   790         assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
   791 
   792         if (mustPushReference) {
   793             if (arg->isNameProxy()) {
   794                 // :TODO: test with functions and globals
   795                 // Compute a reference to the name.
   796                 Variable *var = arg->toNameProxy()->binding()->toVariable();
   797                 __ note_position(node->pos());
   798                 __ newref(var);
   799             } else if (arg->isIndexExpression()) {
   800                 IndexExpression *expr = arg->toIndexExpression();
   801                 expr->left()->accept(this);
   802                 expr->right()->accept(this);
   803                 __ note_position(expr->pos());
   804                 __ bounds();
   805                 __ arrayref_lifo();
   806             } else {
   807                 // We don't know how to, or can't, build a direct reference to
   808                 // this type. Make an indirect one, by taking the top of the
   809                 // stack and boxing it up.
   810                 arg->accept(this);
   811                 __ note_position(node->pos());
   812                 __ slotref_lifo();
   813             }
   814         } else {
   815             arg->accept(this);
   816         }
   817     }
   818 
   819     __ note_position(node->pos());
   820     if (native) {
   821         __ callnative(native->slot(), node->arguments()->length());
   822         __ drop(node->arguments()->length());
   823     } else {
   824         node->callee()->accept(this);
   825         __ call(funType);
   826         __ drop(node->arguments()->length() + 1);
   827     }
   828 }
   829 
   830 void
   831 BytecodeCompiler::visit(TernaryExpression *node)
   832 {
   833     Label success, failure, join;
   834 
   835     visitForTest(node->condition(), &success, &failure, &success);
   836     __ bind(&success);
   837     node->left()->accept(this);
   838     __ drop(1);
   839     __ jump(&join);
   840     __ bind(&failure);
   841     node->right()->accept(this);
   842     __ bind(&join);
   843 }
   844 
   845 void
   846 BytecodeCompiler::visit(IfStatement *node)
   847 {
   848     Label join;
   849     if (!node->ifFalse()) {
   850         Label success;
   851 
   852         visitForTest(node->condition(), &success, &join, &success);
   853         __ bind(&success);
   854         node->ifTrue()->accept(this);
   855     } else {
   856         Label success, next;
   857 
   858         visitForTest(node->condition(), &success, &next, &success);
   859         __ bind(&success);
   860         node->ifTrue()->accept(this);
   861         __ jump(&join);
   862         __ bind(&next);
   863         node->ifFalse()->accept(this);
   864     }
   865 
   866     __ bind(&join);
   867 }
   868 
   869 void
   870 BytecodeCompiler::visit(FunctionStatement *node)
   871 {
   872     if (node->fun()->isNative()) {
   873         assert(!moduleData_->natives->at(node->fun()->slot()));
   874 
   875         Local<Type> type(zone_, node->fun()->type());
   876         Local<String> name(zone_, node->fun()->name());
   877         Local<Native> native(zone_, Native::New(zone_, type, name));
   878         if (!native)
   879             return;
   880 
   881         moduleData_->natives->set(zone_, node->fun()->slot(), native);
   882         return;
   883     }
   884 
   885     // If the function has no body, then it's just a declaration.
   886     if (!node->body()) {
   887         assert(node->fun()->isForward() || node->fun()->isPublic());
   888         return;
   889     }
   890 
   891     BytecodeCompiler child(zone_, tree_, moduleData_);
   892     Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
   893     if (!fun)
   894         return;
   895     
   896     assert(!moduleData_->module->getFunction(node->fun()->slot()));
   897     moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
   898 }
   899 
   900 void
   901 BytecodeCompiler::visit(EnumStatement *node)
   902 {
   903     // :TODO: eventually, we'll export these.
   904 }
   905 
   906 Code *
   907 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
   908 {
   909     // STOP doubles as RETURNVOID.
   910     __ stop();
   911 
   912     if (__ oom() || zone_->hasPendingException())
   913         return NULL;
   914 
   915     Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
   916     if (!bytes)
   917         return NULL;
   918     memcpy(bytes->raw(), __ buffer(), __ length());
   919 
   920     for (size_t i = 0; i < scope->maxContainedLocals(); i++)
   921         assert(locals_->at(i));
   922 
   923     Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
   924     if (!objects)
   925         return NULL;
   926 
   927     for (size_t i = 0; i < __ objects()->length(); i++)
   928         objects->set(zone_, i, *(__ objects()->at(i)));
   929 
   930     Local<ByteArray> safepoints(zone_);
   931 
   932     // If any stack slots hold gc pointers at potential failure points in the
   933     // bytecode, record them now. We do this by building a list of tuples;
   934     // each tuple contains the pc offset, and a bitset containing which stack
   935     // slots must be scanned by the gc.
   936     if (__ oopRecords()->length()) {
   937         size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
   938         size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
   939         safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
   940         if (!safepoints)
   941             return NULL;
   942 
   943         Address cursor = safepoints->raw();
   944         for (size_t i = 0; i < __ oopRecords()->length(); i++) {
   945             OopRecord *record = __ oopRecords()->at(i);
   946             assert(record->depth <= __ maxStackDepth());
   947 
   948             *reinterpret_cast<unsigned *>(cursor) = record->offset;
   949 
   950             unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
   951             for (unsigned i = 0; i < record->depth; i++)
   952                 bits[i / 8] |= (1 << (i % 8));
   953 
   954             cursor += bytesPerSafepoint;
   955         }
   956 
   957         assert(cursor == safepoints->raw() + gcmapSize);
   958     }
   959 
   960     Local<ByteArray> sourcemap(zone_);
   961     if (__ sourcemapLength()) {
   962         sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
   963         if (!sourcemap)
   964             return NULL;
   965 
   966         memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
   967     }
   968 
   969     Local<Code> code(zone_);
   970 
   971     code = Code::New(zone_,
   972                      bytes,
   973                      locals_,
   974                      objects,
   975                      __ maxStackDepth(),
   976                      moduleData_->module,
   977                      safepoints,
   978                      sourcemap,
   979                      lineno);
   980     return code;
   981 }
   982 
   983 Function *
   984 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
   985 {
   986     // Note: No need to record a life slot here, since it's part of the Frame.
   987     AutoEnterScope functionScope(this, node->scope());
   988 
   989     // Set the initial line, because line mappings are deltas.
   990     __ setFirstLine(node->pos().line);
   991     locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
   992 
   993     node->body()->accept(this);
   994 
   995     unsigned nargs = node->scope()->numArguments();
   996     Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
   997     if (!args)
   998         return NULL;
   999 
  1000     for (size_t i = 0; i < node->scope()->numArguments(); i++)
  1001         args->set(zone_, i, node->scope()->getArgument(i)->type());
  1002 
  1003 
  1004     Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
  1005     if (!code)
  1006         return NULL;
  1007 
  1008     Local<Type> type(zone_, node->fun()->type());
  1009     Local<String> name(zone_, node->fun()->name());
  1010     Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
  1011     if (!fun)
  1012         return NULL;
  1013 
  1014     // If this function had a forward, export it as a named public.
  1015     if (node->fun()->isPublic()) {
  1016         assert(node->fun()->isPublic());
  1017         ScopedRoot<Function> root(fun);
  1018         if (!root)
  1019             return NULL;
  1020         if (!moduleData_->publics.append(root))
  1021             return NULL;
  1022     }
  1023 
  1024     Disassemble(code, stdout);
  1025     
  1026     return fun;
  1027 }
  1028 
  1029 Module *
  1030 BytecodeCompiler::compile()
  1031 {
  1032     AutoEnterScope scope(this, tree_->scope());
  1033 
  1034     ModuleData moduleData;
  1035 
  1036     moduleData_ = &moduleData;
  1037     moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
  1038 
  1039     if (!moduleData_)
  1040         return NULL;
  1041     
  1042     moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
  1043     if (!moduleData_->natives)
  1044         return NULL;
  1045 
  1046     for (size_t i = 0; i < tree_->statements()->length(); i++) {
  1047         Statement *statement = tree_->statements()->at(i);
  1048         statement->accept(this);
  1049     }
  1050 
  1051     if (zone_->hasPendingException())
  1052         return NULL;
  1053 
  1054 #ifndef NDEBUG
  1055     for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
  1056         assert(moduleData_->module->getFunction(i));
  1057     for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
  1058         assert(moduleData_->natives->at(i));
  1059 #endif
  1060 
  1061     if (moduleData_->publics.length()) {
  1062         Local<Function> fun(zone_);
  1063         Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
  1064                                Heap::Tenure_Old));
  1065         if (!list)
  1066             return NULL;
  1067 
  1068         for (size_t i = 0; i < moduleData_->publics.length(); i++)
  1069             list->set(zone_, i, moduleData_->publics[i]);
  1070 
  1071         moduleData_->module->setPublics(list);
  1072     }
  1073 
  1074     moduleData_->module->setNatives(moduleData_->natives);
  1075 
  1076     if (scope_->numEnvironmentVars()) {
  1077         Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
  1078         if (!globalMap)
  1079             return NULL;
  1080 
  1081         Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
  1082         if (!environment)
  1083             return NULL;
  1084 
  1085         moduleData_->module->setEnvironment(environment);
  1086     }
  1087 
  1088     Local<Code> code(zone_, compileCode(tree_->scope(), 1));
  1089     if (!code)
  1090         return NULL;
  1091 
  1092     Disassemble(code, stdout);
  1093 
  1094     moduleData_->module->setCode(code);
  1095 
  1096     return moduleData_->module;
  1097 }