src/BytecodeCompiler.cpp
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 23:26:49 2012 -0700 (2012-08-04)
changeset 152 153d0cafc1fd
parent 151 bb419abf0df4
child 153 fc91c4a3dfe2
permissions -rw-r--r--
Support assigning structs.
     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());
   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     Local<Type> type(zone_, node->expression()->type());
   205 
   206     // The first step is three sub-steps:
   207     //
   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.
   212     //
   213     if (node->lvalue()->isNameProxy()) {
   214         NameProxy *proxy = node->lvalue()->toNameProxy();
   215         Variable *var = proxy->binding()->toVariable();
   216 
   217         // Bind.
   218         if (var->type()->isReference() ||
   219             var->type()->isArray() ||
   220             var->type()->isStruct())
   221         {
   222             __ get(var);
   223         }
   224 
   225         // Get the value if needed.
   226         if (node->token() != TOK_ASSIGN) {
   227             // += etc is not allowed on arrays.
   228             assert(!var->type()->isArray());
   229 
   230             if (var->type()->isReference()) {
   231                 __ dup();
   232                 __ getref(var->type()->contained());
   233             } else {
   234                 __ get(var);
   235             }
   236         }
   237     } else if (node->lvalue()->isFieldExpression()) {
   238         FieldExpression *expr = node->lvalue()->toFieldExpression();
   239         __ note_position(expr->pos());
   240 
   241         expr->left()->accept(this);
   242 
   243         if (node->token() != TOK_ASSIGN)
   244             __ dup();
   245 
   246         if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray()))
   247             __ getfield(expr->fieldIndex(), expr->type());
   248     } else {
   249         IndexExpression *index = node->lvalue()->toIndexExpression();
   250         __ note_position(index->pos());
   251 
   252         // Note: Eventually, watch out for a hole here. An expression like:
   253         //
   254         //   (a[4] = (5, a = []))
   255         //
   256         // Could shrink array, so we need to either bind a[i] to a Reference,
   257         // or keep the bounds check near the set. This is just a semantic
   258         // decision.
   259         index->left()->accept(this);
   260         index->right()->accept(this);
   261 
   262         if (node->token() != TOK_ASSIGN)
   263             __ dup2();
   264 
   265         if (node->token() != TOK_ASSIGN || (type->isStruct() || type->isArray())) {
   266             __ bounds();
   267             __ getarray(index->type());
   268         }
   269     }
   270 
   271     if (node->token() == TOK_ASSIGN) {
   272         node->expression()->accept(this);
   273     } else {
   274         visitForALU(node->type(), node->expression());
   275 
   276         TokenKind alu = TokenKind(int(node->token()) - (TOK_ASSIGN_ADD - TOK_PLUS));
   277         __ note_position(node->pos());
   278         __ binary(TokenToOpcode(alu));
   279     }
   280 
   281     // Emit the store.
   282     if (type->isArray()) {
   283         // Unlike references, which can only be computed from a NamePRoxy,
   284         // arrays can be computed from multiple lvalue types.
   285         __ note_position(node->pos());
   286         __ copyarray();
   287     } else if (type->isStruct()) {
   288         // Structs have value semantics, so emit a full copy. We don't break
   289         // this down into u-ops, to avoid unnecessary bytecode explosion.
   290         __ note_position(node->pos());
   291         __ copystruct();
   292     } else if (node->lvalue()->isNameProxy()) {
   293         NameProxy *proxy = node->lvalue()->toNameProxy();
   294         Variable *var = proxy->binding()->toVariable();
   295 
   296         if (var->type()->isReference())
   297             __ setref(var->type()->contained());
   298         else
   299             __ set(var);
   300     } else if (node->lvalue()->isFieldExpression()) {
   301         FieldExpression *expr = node->lvalue()->toFieldExpression();
   302         __ setfield(expr->fieldIndex(), expr->type());
   303     } else {
   304         IndexExpression *index = node->lvalue()->toIndexExpression();
   305         // :TODO: get rid of the BOUNDS instruction, embed it in g/setarray*?
   306         // We can't emit it here because the stack is slightly out of position.
   307         // __ bounds();
   308         __ setarray(index->type());
   309     }
   310 }
   311 
   312 void
   313 BytecodeCompiler::visit(IncDecExpression *node)
   314 {
   315     // In the postfix case, the logic looks like:
   316     //      (1) Bind       -> REF
   317     //      (2) Dup        -> REF REF
   318     //      (3) Fetch      -> REF VAL
   319     //      (4) Swap       -> VAL REF
   320     //      (5) Pick       -> VAL REF VAL
   321     //      (6) Add        -> VAL REF VAL+1
   322     //      (7) Store      -> VAL VAL+1
   323     //      (8) Drop       -> VAL
   324     //
   325     // In the prefix case, the logic looks like:
   326     //      (1) Bind       -> REF
   327     //      (2) Dup        -> REF REF
   328     //      (3) Fetch      -> REF VAL
   329     //      --
   330     //      --
   331     //      (6) Add        -> REF VAL+1
   332     //      (7) Store      -> VAL+1
   333     //      --
   334     //
   335     // This is squirrely enough such that we don't bother sharing too much
   336     // with the above.
   337     // 
   338     // Note: We simulate a "Pick" operation with a dup2+pop.
   339 
   340     // This is steps (1) to (5).
   341     Expression *expr = node->expression();
   342     if (expr->isNameProxy()) {
   343         NameProxy *proxy = expr->toNameProxy();
   344         Variable *var = proxy->binding()->toVariable();
   345 
   346         // Bind.
   347         if (var->type()->isReference() || var->type()->isArray())
   348             __ get(var);
   349 
   350         // Dup and fetch.
   351         if (var->type()->isReference()) {
   352             __ dup();
   353             __ getref(node->type());
   354             if (node->postfix()) {
   355                 __ swap();
   356                 __ pick2();
   357             }
   358         } else {
   359             assert(!var->type()->isArray());
   360             __ get(var);
   361             if (node->postfix())
   362                 __ dup();
   363         }
   364     } else if (expr->isFieldExpression()) {
   365         FieldExpression *field = expr->toFieldExpression();
   366 
   367         field->left()->accept(this);
   368         __ dup();
   369         __ getfield(field->fieldIndex(), field->type());
   370 
   371         if (node->postfix()) {
   372             // The stack looks like this now:
   373             // OBJECT VALUE
   374             __ swap();          // VALUE OBJECT
   375             __ pick2();         // VALUE OBJECT VALUE
   376         }
   377     } else {
   378         IndexExpression *index = expr->toIndexExpression();
   379         __ note_position(index->pos());
   380 
   381         index->left()->accept(this);
   382         index->right()->accept(this);
   383 
   384         assert(!node->type()->isArray());
   385         __ bounds();
   386         __ dup2();
   387         __ getarray(node->type());
   388 
   389         if (node->postfix()) {
   390             // The stack looks like this now:
   391             // ARRAY INDEX VALUE
   392             //
   393             // So we must use a PICK operation to rotate the stack into position.
   394             __ roll3();         // INDEX VALUE ARRAY
   395             __ roll3();         // VALUE ARRAY INDEX
   396             __ pick3();         // VALUE ARRAY INDEX VALUE
   397         }
   398     }
   399 
   400     // Perform step (6).
   401     if (node->type()->isFloat()) {
   402         __ float_(1.0);
   403     } else {
   404         assert(node->type()->isInt32());
   405         __ int_(1);
   406     }
   407 
   408     __ note_position(node->pos());
   409     if (node->token() == TOK_INCREMENT)
   410         __ binary(OP_ADD);
   411     else
   412         __ binary(OP_SUB);
   413 
   414     // Perform step (7).
   415     if (expr->isNameProxy()) {
   416         NameProxy *proxy = expr->toNameProxy();
   417         Variable *var = proxy->binding()->toVariable();
   418 
   419         if (var->type()->isReference())
   420             __ setref(node->type());
   421         else
   422             __ set(var);
   423     } else if (expr->isFieldExpression()) {
   424         FieldExpression *field = expr->toFieldExpression();
   425         __ setfield(field->fieldIndex(), field->type());
   426     } else {
   427         assert(expr->isIndexExpression());
   428         __ setarray(node->type());
   429     }
   430 
   431     // Perform step (8).
   432     if (node->postfix())
   433         __ pop();
   434 }
   435 
   436 void
   437 BytecodeCompiler::visitForALU(Type *needed, Expression *node)
   438 {
   439     node->accept(this);
   440     if (needed->isFloat() && !node->type()->isFloat())
   441         __ cvt_i2f();
   442     else
   443         assert(needed->primitive() == node->type()->primitive());
   444 }
   445 
   446 static inline Type *
   447 GetCoercionType(TokenKind tok, Type *left, Type *right)
   448 {
   449     if (left->isFloat())
   450         return left;
   451     return right;
   452 }
   453 
   454 void
   455 BytecodeCompiler::visit(UnaryExpression *node)
   456 {
   457     Local<Type> out(zone_, ((Expression *)node)->type());
   458 
   459     switch (node->token()) {
   460       case TOK_MINUS:
   461       {
   462         Local<Type> type(zone_, node->expression()->type());
   463         node->expression()->accept(this);
   464         
   465         if (out->isFloat())
   466             __ neg_f();
   467         else
   468             __ neg();
   469         break;
   470       }
   471 
   472       case TOK_NOT:
   473       {
   474         Local<Type> type(zone_, node->expression()->type());
   475         node->expression()->accept(this);
   476         
   477         if (out->isFloat())
   478             __ not_f();
   479         else
   480             __ not_();
   481         break;
   482       }
   483 
   484       case TOK_LABEL:
   485       {
   486         Local<Type> from(zone_, node->expression()->type());
   487 
   488         node->expression()->accept(this);
   489         if (!from->isFloat() && out->isFloat())
   490             __ bits_f2i();
   491         else if (from->isFloat() && !out->isFloat())
   492             __ bits_i2f();
   493         break;
   494       }
   495 
   496       default:
   497         assert(node->token() == TOK_TILDE);
   498         node->expression()->accept(this);
   499         __ bitnot();
   500         break;
   501     }
   502 }
   503 
   504 void
   505 BytecodeCompiler::visit(BinaryExpression *node)
   506 {
   507     if (node->token() == TOK_AND || node->token() == TOK_OR) {
   508         Label success, failure, done;
   509         visitForTest(node, &success, &failure, &success);
   510         __ bind(&success);
   511         __ int_(1);
   512         __ drop(1);         // Otherwise, the stack depth will be +1 too much.
   513         __ jump(&done);
   514         __ bind(&failure);
   515         __ int_(0);
   516         __ bind(&done);
   517         return;
   518     }
   519 
   520     Local<Type> leftType(zone_, node->left()->type());
   521     Local<Type> rightType(zone_, node->right()->type());
   522     Local<Type> coerce(zone_, GetCoercionType(node->token(), leftType, rightType));
   523 
   524     visitForALU(coerce, node->left());
   525     visitForALU(coerce, node->right());
   526 
   527     Opcode op = TokenToOpcode(node->token());
   528     if (node->type()->isFloat())
   529         op = (Opcode)(int(op) + (OP_ADD_F - OP_ADD));
   530 
   531     __ note_position(node->pos());
   532     __ binary(op);
   533 }
   534 
   535 void
   536 BytecodeCompiler::visit(IndexExpression *node)
   537 {
   538     node->left()->accept(this);
   539     node->right()->accept(this);
   540     __ note_position(node->pos());
   541     __ bounds();
   542 
   543     if (node->type()->isArray())
   544         __ getarray_o();
   545     else
   546         __ getarray(node->type());
   547 }
   548 
   549 void
   550 BytecodeCompiler::visit(FieldExpression *node)
   551 {
   552     node->left()->accept(this);
   553     __ getfield(node->fieldIndex(), node->type());
   554 }
   555 
   556 void
   557 BytecodeCompiler::visit(ReturnStatement *node)
   558 {
   559     if (node->expression()) {
   560         node->expression()->accept(this);
   561         __ return_();
   562     } else {
   563         __ returnvoid();
   564     }
   565 }
   566 
   567 void
   568 BytecodeCompiler::visitForTest(Expression *expression,
   569                                Label *trueBranch,
   570                                Label *falseBranch,
   571                                Label *fallthrough)
   572 {
   573     TokenKind token = ToCompare(expression);
   574     if (token == TOK_EOF || token == TOK_NOT) {
   575         if (token == TOK_EOF)
   576             expression->accept(this);
   577         else
   578             expression->toUnaryExpression()->expression()->accept(this);
   579         
   580         bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
   581 
   582         Opcode op = jumpOnTrue ? OP_JT : OP_JF;
   583         if (expression->type()->isFloat())
   584             op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
   585 
   586         if (fallthrough == falseBranch)
   587             __ j(op, trueBranch);
   588         else
   589             __ j(op, falseBranch); 
   590         return;
   591     }
   592 
   593     BinaryExpression *binary = expression->toBinaryExpression();
   594 
   595     if (token == TOK_AND || token == TOK_OR) {
   596         Label next;
   597         if (token == TOK_AND)
   598             visitForTest(binary->left(), &next, falseBranch, &next);
   599         else
   600             visitForTest(binary->left(), trueBranch, &next, &next);
   601         __ bind(&next);
   602         visitForTest(binary->right(), trueBranch, falseBranch, fallthrough);
   603         return;
   604     }
   605 
   606     Opcode op = CompareTokenToJump(token);
   607     if (fallthrough == trueBranch)
   608         op = Invert(op);
   609 
   610     Local<Type> leftType(zone_, binary->left()->type());
   611     Local<Type> rightType(zone_, binary->right()->type());
   612     Local<Type> coerce(zone_, GetCoercionType(binary->token(), leftType, rightType));
   613 
   614     visitForALU(coerce, binary->left());
   615     visitForALU(coerce, binary->right());
   616     if (coerce->isFloat())
   617         op = (Opcode)(int(op) + (OP_JL_F - OP_JL));
   618 
   619     if (fallthrough == trueBranch)
   620         __ j(op, falseBranch);
   621     else
   622         __ j(op, trueBranch);
   623 }
   624 
   625 void
   626 BytecodeCompiler::visit(ForStatement *node)
   627 {
   628     AutoEnterScope forScope(this, node->scope());
   629 
   630     recordLifoSlot(node->scope());
   631 
   632     Label join, update;
   633     LoopScope loop(scope_, &join, &update, &loop_);
   634 
   635     if (node->initialization())
   636         node->initialization()->accept(this);
   637 
   638     Label header, test;
   639     if (node->condition())
   640         __ jump(&test);
   641 
   642     __ bind(&header);
   643     node->body()->accept(this);
   644 
   645     __ bind(&update);
   646     if (node->update())
   647         node->update()->accept(this);
   648 
   649     if (node->condition()) {
   650         __ bind(&test);
   651         visitForTest(node->condition(), &header, &join, &join);
   652     } else {
   653         __ jump(&header);
   654     }
   655 
   656     __ bind(&join);
   657 
   658     if (node->scope())
   659         unwindTo(node->scope()->enclosing());
   660     assert(__ stackDepth() == 0);
   661 }
   662 
   663 void
   664 BytecodeCompiler::visit(WhileStatement *node)
   665 {
   666     Label header, join;
   667 
   668     __ bind(&header);
   669 
   670     LoopScope loop(scope_, &join, &header, &loop_);
   671 
   672     if (node->token() == TOK_WHILE) {
   673         Label body;
   674 
   675         visitForTest(node->condition(), &body, &join, &body);
   676         __ bind(&body);
   677         node->body()->accept(this);
   678         __ jump(&header);
   679     } else {
   680         node->body()->accept(this);
   681         visitForTest(node->condition(), &header, &join, &join);
   682     }
   683 
   684     __ bind(&join);
   685 }
   686 
   687 void
   688 BytecodeCompiler::visit(BreakStatement *node)
   689 {
   690     unwindTo(loop_->scope());
   691     __ jump(loop_->break_());
   692 }
   693 
   694 void
   695 BytecodeCompiler::visit(ContinueStatement *node)
   696 {
   697     unwindTo(loop_->scope());
   698     __ jump(loop_->continue_());
   699 }
   700 
   701 void
   702 BytecodeCompiler::visit(BlockStatement *node)
   703 {
   704     AutoEnterScope blockScope(this, node->scope());
   705 
   706     recordLifoSlot(node->scope());
   707 
   708     for (size_t i = 0; i < node->statements()->length(); i++) {
   709         Statement *statement = node->statements()->at(i);
   710         statement->accept(this);
   711         assert(__ stackDepth() == 0);
   712     }
   713 
   714     // We don't bother emitting an "unwind" instruction for function scopes,
   715     // since this is taken care of by the calling convention.
   716     if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
   717         unwindTo(node->scope()->enclosing());
   718 }
   719 
   720 void
   721 BytecodeCompiler::visit(IntegerLiteral *node)
   722 {
   723     __ int_(node->value());
   724 }
   725 
   726 void
   727 BytecodeCompiler::visit(BooleanLiteral *node)
   728 {
   729     if (node->token() == TOK_TRUE)
   730         __ true_();
   731     else
   732         __ false_();
   733 }
   734 
   735 void
   736 BytecodeCompiler::visit(FloatLiteral *node)
   737 {
   738     __ float_((float)node->value());
   739 }
   740 
   741 static inline ArrayMap *
   742 BuildArrayMap(Zone *zone, Handle<Type> type)
   743 {
   744     Local<ArrayMap> contained(zone);
   745     if (type->levels() > 1) {
   746         Local<Type> containedType(zone, type->contained());
   747         contained = BuildArrayMap(zone, containedType);
   748         if (!contained)
   749             return NULL;
   750     }
   751 
   752     return ArrayMap::New(zone, type, contained);
   753 }
   754 
   755 void
   756 BytecodeCompiler::visit(StringLiteral *node)
   757 {
   758     Local<Type> type(zone_, node->type());
   759     Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
   760     if (!map)
   761         return;
   762 
   763     Local<Array> array(zone_, Array::New(zone_, map, node->string()->length(), Heap::Tenure_Old));
   764     if (!array)
   765         return;
   766 
   767     assert(array->nelements() >= node->string()->length());
   768     memcpy(array->chars(), node->string()->raw(), node->string()->length() * sizeof(char));
   769 
   770     __ note_position(node->pos());
   771     __ object(array);
   772 }
   773 
   774 void
   775 BytecodeCompiler::getEnvSlot(Variable *var)
   776 {
   777     assert(var->scope()->kind() == Scope::GLOBAL);
   778     __ getglobal(var);
   779 }
   780 
   781 void
   782 BytecodeCompiler::setEnvSlot(Variable *var)
   783 {
   784     assert(var->scope()->kind() == Scope::GLOBAL);
   785     __ setglobal(var);
   786 }
   787 
   788 void
   789 BytecodeCompiler::visit(VariableDeclaration *node)
   790 {
   791     Variable *var = node->variable();
   792     
   793     Local<Type> type(zone_, var->type());
   794     if (type->isArray()) {
   795         Local<ArrayMap> map(zone_, BuildArrayMap(zone_, type));
   796         __ note_position(node->pos());
   797         if (var->scope()->kind() == Scope::GLOBAL)
   798             __ newarray(map, Heap::Tenure_Old);
   799         else if (var->needsHeapAllocation())
   800             __ newarray(map, Heap::Tenure_Default);
   801         else
   802             __ newarray(map, Heap::Tenure_Stack);
   803 
   804         if (node->initialization()) {
   805             node->initialization()->accept(this);
   806             __ note_position(node->pos());
   807             __ copyarray();
   808         }
   809     } else if (type->isStruct()) {
   810         Local<Map> map(zone_, type->newMap());
   811         if (var->scope()->kind() == Scope::GLOBAL)
   812             __ newstruct(map, Heap::Tenure_Old);
   813         else if (var->needsHeapAllocation())
   814             __ newstruct(map, Heap::Tenure_Default);
   815         else
   816             __ newstruct(map, Heap::Tenure_Stack);
   817     } else {
   818         if (node->initialization()) {
   819             node->initialization()->accept(this);
   820         } else {
   821             if (type->isInt32OrEnum() || type->isBool()) {
   822                 __ int_(0);
   823             } else {
   824                 assert(type->isFloat());
   825                 __ float_(0);
   826             }
   827         }
   828     }
   829 
   830     if (var->location() == Variable::STACK) {
   831         __ setlocal(var);
   832         locals_->set(zone_, var->slot(), var->type());
   833     } else {
   834         assert(var->location() == Variable::ENVIRONMENT);
   835         setEnvSlot(var);
   836     }
   837 
   838     __ pop();
   839 }
   840 
   841 void
   842 BytecodeCompiler::visit(CallExpression *node)
   843 {
   844     Local<Type> funType(zone_, node->callee()->type());
   845 
   846     // Sniff out natives, which have a specific opcode and are guaranteed to
   847     // be called via name proxy.
   848     NamedFunction *native = NULL;
   849     if (funType->isNative()) {
   850         native = node->callee()->toNameProxy()->binding()->toNamedFunction();
   851     }
   852 
   853     for (size_t i = node->arguments()->length() - 1; i < node->arguments()->length(); i--) {
   854         Expression *arg = node->arguments()->at(i);
   855 
   856         // We need to compute a reference if the parameter was in the form:
   857         //  f(&a)
   858         // Note this form does not allow: f({})
   859         // 
   860         // We also need to compute references for all variadic native
   861         // arguments, but arrays and references are already in the
   862         // appropriate form.
   863         Local<Type> given(zone_, arg->type());
   864         bool mustPushReference = false;
   865         if (i >= funType->parameters()->length()) {
   866             assert(funType->isNative() && funType->isNativeVariadic());
   867             mustPushReference = !given->isArray() && !given->isReference();
   868         } else if (funType->parameterAt(i)->isReference()) {
   869             mustPushReference = !given->isReference();
   870         }
   871 
   872         // If a native is passed to a native, it must only be one level deep.
   873         assert(!funType->isNative() || !given->isArray() || given->levels() == 1);
   874 
   875         if (mustPushReference) {
   876             if (arg->isNameProxy()) {
   877                 // :TODO: test with functions and globals
   878                 // Compute a reference to the name.
   879                 Variable *var = arg->toNameProxy()->binding()->toVariable();
   880                 __ note_position(node->pos());
   881                 __ newref(var);
   882             } else if (arg->isIndexExpression()) {
   883                 IndexExpression *expr = arg->toIndexExpression();
   884                 expr->left()->accept(this);
   885                 expr->right()->accept(this);
   886                 __ note_position(expr->pos());
   887                 __ bounds();
   888                 __ arrayref_lifo();
   889             } else {
   890                 // We don't know how to, or can't, build a direct reference to
   891                 // this type. Make an indirect one, by taking the top of the
   892                 // stack and boxing it up.
   893                 arg->accept(this);
   894                 __ note_position(node->pos());
   895                 __ slotref_lifo();
   896             }
   897         } else {
   898             arg->accept(this);
   899         }
   900     }
   901 
   902     __ note_position(node->pos());
   903     if (native) {
   904         __ callnative(native->slot(), node->arguments()->length());
   905         __ drop(node->arguments()->length());
   906     } else {
   907         node->callee()->accept(this);
   908         __ call(funType);
   909         __ drop(node->arguments()->length() + 1);
   910     }
   911 }
   912 
   913 void
   914 BytecodeCompiler::visit(TernaryExpression *node)
   915 {
   916     Label success, failure, join;
   917 
   918     visitForTest(node->condition(), &success, &failure, &success);
   919     __ bind(&success);
   920     node->left()->accept(this);
   921     __ drop(1);
   922     __ jump(&join);
   923     __ bind(&failure);
   924     node->right()->accept(this);
   925     __ bind(&join);
   926 }
   927 
   928 void
   929 BytecodeCompiler::visit(IfStatement *node)
   930 {
   931     Label join;
   932     if (!node->ifFalse()) {
   933         Label success;
   934 
   935         visitForTest(node->condition(), &success, &join, &success);
   936         __ bind(&success);
   937         node->ifTrue()->accept(this);
   938     } else {
   939         Label success, next;
   940 
   941         visitForTest(node->condition(), &success, &next, &success);
   942         __ bind(&success);
   943         node->ifTrue()->accept(this);
   944         __ jump(&join);
   945         __ bind(&next);
   946         node->ifFalse()->accept(this);
   947     }
   948 
   949     __ bind(&join);
   950 }
   951 
   952 void
   953 BytecodeCompiler::visit(FunctionStatement *node)
   954 {
   955     if (!node->fun()->used())
   956         return;
   957 
   958     if (node->fun()->isNative()) {
   959         assert(!moduleData_->natives->at(node->fun()->slot()));
   960 
   961         Local<Type> type(zone_, node->fun()->type());
   962         Local<String> name(zone_, node->fun()->name());
   963         Local<Native> native(zone_, Native::New(zone_, type, name));
   964         if (!native)
   965             return;
   966 
   967         moduleData_->natives->set(zone_, node->fun()->slot(), native);
   968         return;
   969     }
   970 
   971     // If the function has no body, then it's just a declaration.
   972     if (!node->body()) {
   973         assert(node->fun()->isForward() || node->fun()->isPublic());
   974         return;
   975     }
   976 
   977     BytecodeCompiler child(zone_, tree_, moduleData_);
   978     Local<Function> fun(zone_, child.compileFunction(moduleData_, node));
   979     if (!fun)
   980         return;
   981     
   982     assert(!moduleData_->module->getFunction(node->fun()->slot()));
   983     moduleData_->module->setFunction(zone_, node->fun()->slot(), fun);
   984 }
   985 
   986 void
   987 BytecodeCompiler::visit(EnumStatement *node)
   988 {
   989     // :TODO: eventually, we'll export these.
   990 }
   991 
   992 void
   993 BytecodeCompiler::visit(StructureStatement *node)
   994 {
   995 }
   996 
   997 Code *
   998 BytecodeCompiler::compileCode(Scope *scope, unsigned lineno)
   999 {
  1000     // STOP doubles as RETURNVOID.
  1001     __ stop();
  1002 
  1003     if (__ oom() || zone_->hasPendingException())
  1004         return NULL;
  1005 
  1006     Local<ByteArray> bytes(zone_, ByteArray::New(zone_, __ length() * sizeof(bytecode), Heap::Tenure_Old));
  1007     if (!bytes)
  1008         return NULL;
  1009     memcpy(bytes->raw(), __ buffer(), __ length());
  1010 
  1011     for (size_t i = 0; i < scope->maxContainedLocals(); i++)
  1012         assert(locals_->at(i));
  1013 
  1014     Local<FixedArray> objects(zone_, FixedArray::New(zone_, __ objects()->length(), Heap::Tenure_Old));
  1015     if (!objects)
  1016         return NULL;
  1017 
  1018     for (size_t i = 0; i < __ objects()->length(); i++)
  1019         objects->set(zone_, i, *(__ objects()->at(i)));
  1020 
  1021     Local<ByteArray> safepoints(zone_);
  1022 
  1023     // If any stack slots hold gc pointers at potential failure points in the
  1024     // bytecode, record them now. We do this by building a list of tuples;
  1025     // each tuple contains the pc offset, and a bitset containing which stack
  1026     // slots must be scanned by the gc.
  1027     if (__ oopRecords()->length()) {
  1028         size_t bytesPerSafepoint = Code::BytesPerSafepoint(__ maxStackDepth());
  1029         size_t gcmapSize = bytesPerSafepoint * __ oopRecords()->length();
  1030         safepoints = ByteArray::New(zone_, gcmapSize, Heap::Tenure_Old);
  1031         if (!safepoints)
  1032             return NULL;
  1033 
  1034         Address cursor = safepoints->raw();
  1035         for (size_t i = 0; i < __ oopRecords()->length(); i++) {
  1036             OopRecord *record = __ oopRecords()->at(i);
  1037             assert(record->depth <= __ maxStackDepth());
  1038 
  1039             *reinterpret_cast<unsigned *>(cursor) = record->offset;
  1040 
  1041             unsigned char *bits = reinterpret_cast<unsigned char *>(cursor + sizeof(unsigned));
  1042             for (unsigned i = 0; i < record->depth; i++)
  1043                 bits[i / 8] |= (1 << (i % 8));
  1044 
  1045             cursor += bytesPerSafepoint;
  1046         }
  1047 
  1048         assert(cursor == safepoints->raw() + gcmapSize);
  1049     }
  1050 
  1051     Local<ByteArray> sourcemap(zone_);
  1052     if (__ sourcemapLength()) {
  1053         sourcemap = ByteArray::New(zone_, __ sourcemapLength(), Heap::Tenure_Old);
  1054         if (!sourcemap)
  1055             return NULL;
  1056 
  1057         memcpy(sourcemap->raw(), __ sourcemap(), __ sourcemapLength());
  1058     }
  1059 
  1060     Local<Code> code(zone_);
  1061 
  1062     code = Code::New(zone_,
  1063                      bytes,
  1064                      locals_,
  1065                      objects,
  1066                      __ maxStackDepth(),
  1067                      moduleData_->module,
  1068                      safepoints,
  1069                      sourcemap,
  1070                      lineno);
  1071     return code;
  1072 }
  1073 
  1074 Function *
  1075 BytecodeCompiler::compileFunction(ModuleData *module, FunctionStatement *node)
  1076 {
  1077     // Note: No need to record a life slot here, since it's part of the Frame.
  1078     AutoEnterScope functionScope(this, node->scope());
  1079 
  1080     // Set the initial line, because line mappings are deltas.
  1081     __ setFirstLine(node->pos().line);
  1082     locals_ = FixedArray::New(zone_, node->scope()->maxContainedLocals(), Heap::Tenure_Old);
  1083 
  1084     node->body()->accept(this);
  1085 
  1086     unsigned nargs = node->scope()->numArguments();
  1087     Local<FixedArray> args(zone_, FixedArray::New(zone_, nargs, Heap::Tenure_Old));
  1088     if (!args)
  1089         return NULL;
  1090 
  1091     for (size_t i = 0; i < node->scope()->numArguments(); i++)
  1092         args->set(zone_, i, node->scope()->getArgument(i)->type());
  1093 
  1094 
  1095     Local<Code> code(zone_, compileCode(node->scope(), node->pos().line));
  1096     if (!code)
  1097         return NULL;
  1098 
  1099     Local<Type> type(zone_, node->fun()->type());
  1100     Local<String> name(zone_, node->fun()->name());
  1101     Local<Function> fun(zone_, Function::New(zone_, code, type, name, node->pos()));
  1102     if (!fun)
  1103         return NULL;
  1104 
  1105     // If this function had a forward, export it as a named public.
  1106     if (node->fun()->isPublic()) {
  1107         assert(node->fun()->isPublic());
  1108         ScopedRoot<Function> root(fun);
  1109         if (!root)
  1110             return NULL;
  1111         if (!moduleData_->publics.append(root))
  1112             return NULL;
  1113     }
  1114 
  1115     Disassemble(code, stdout);
  1116     
  1117     return fun;
  1118 }
  1119 
  1120 Module *
  1121 BytecodeCompiler::compile()
  1122 {
  1123     AutoEnterScope scope(this, tree_->scope());
  1124 
  1125     ModuleData moduleData;
  1126 
  1127     moduleData_ = &moduleData;
  1128     moduleData_->module = Module::New(zone_, tree_->scope()->numFunctions());
  1129 
  1130     if (!moduleData_)
  1131         return NULL;
  1132     
  1133     moduleData_->natives = FixedArray::New(zone_, tree_->scope()->numNatives(), Heap::Tenure_Old);
  1134     if (!moduleData_->natives)
  1135         return NULL;
  1136 
  1137     for (size_t i = 0; i < tree_->statements()->length(); i++) {
  1138         Statement *statement = tree_->statements()->at(i);
  1139         statement->accept(this);
  1140     }
  1141 
  1142     if (zone_->hasPendingException())
  1143         return NULL;
  1144 
  1145 #ifndef NDEBUG
  1146     for (size_t i = 0; i < tree_->scope()->numFunctions(); i++)
  1147         assert(moduleData_->module->getFunction(i));
  1148     for (size_t i = 0; i < tree_->scope()->numNatives(); i++)
  1149         assert(moduleData_->natives->at(i));
  1150 #endif
  1151 
  1152     if (moduleData_->publics.length()) {
  1153         Local<Function> fun(zone_);
  1154         Local<FixedArray> list(zone_, FixedArray::New(zone_, moduleData_->publics.length(),
  1155                                Heap::Tenure_Old));
  1156         if (!list)
  1157             return NULL;
  1158 
  1159         for (size_t i = 0; i < moduleData_->publics.length(); i++)
  1160             list->set(zone_, i, moduleData_->publics[i]);
  1161 
  1162         moduleData_->module->setPublics(list);
  1163     }
  1164 
  1165     moduleData_->module->setNatives(moduleData_->natives);
  1166 
  1167     if (scope_->numEnvironmentVars()) {
  1168         Local<EnvironmentMap> globalMap(zone_, scope_->createEnvironmentMap(zone_));
  1169         if (!globalMap)
  1170             return NULL;
  1171 
  1172         Local<Environment> environment(zone_, Environment::New(zone_, globalMap));
  1173         if (!environment)
  1174             return NULL;
  1175 
  1176         moduleData_->module->setEnvironment(environment);
  1177     }
  1178 
  1179     Local<Code> code(zone_, compileCode(tree_->scope(), 1));
  1180     if (!code)
  1181         return NULL;
  1182 
  1183     Disassemble(code, stdout);
  1184 
  1185     moduleData_->module->setCode(code);
  1186 
  1187     return moduleData_->module;
  1188 }