src/compiler/SemanticAnalysis.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 15:50:41 2012 -0800 (2012-11-18)
changeset 217 c22e983b3b6e
parent 215 b8f650a441a5
child 218 ccb089827f87
permissions -rw-r--r--
Get preinc working again.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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  * SourcePawn 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  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <assert.h>
    20 #include "../Zone.h"
    21 #include "../Strings.h"
    22 #include "../NativeTable.h"
    23 #include "../TypeManager.h"
    24 #include "../Modules.h"
    25 #include "../Code.h"
    26 #include "SemanticAnalysis.h"
    27 #include "../Heap-inl.h"
    28 
    29 using namespace ke;
    30 
    31 #define __ emitter_.
    32 
    33 typedef BytecodeEmitter::Label Label;
    34 
    35 class AutoEnterScope
    36 {
    37   public:
    38     AutoEnterScope(Scope **prevp, Scope *scope)
    39       : prevp_(prevp),
    40         prev_(*prevp),
    41         scope_(scope)
    42     {
    43         if (!scope_)
    44             return;
    45         prev_ = *prevp_;
    46         *prevp_ = scope;
    47         assert(prev_ == scope->enclosing());
    48     }
    49     ~AutoEnterScope()
    50     {
    51         if (scope_)
    52             *prevp_ = prev_;
    53     }
    54   private:
    55     Scope **prevp_;
    56     Scope *prev_;
    57     Scope *scope_;
    58 };
    59 
    60 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit)
    61   : zone_(zone),
    62     pool_(zone->pool()),
    63     cc_(cc),
    64     unit_(unit),
    65     hir_(NULL),
    66     emitter_(pool_),
    67     module_(NULL),
    68     loop_(NULL)
    69 {
    70 }
    71 
    72 SemanticAnalysis::SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun)
    73   : zone_(parent->zone_),
    74     pool_(parent->pool_),
    75     cc_(parent->cc_),
    76     unit_(parent->unit_),
    77     hir_(NULL),
    78     scope_(parent->scope_),
    79     emitter_(pool_),
    80     module_(parent->module_),
    81     fun_(fun),
    82     loop_(NULL)
    83 {
    84 
    85 }
    86 
    87 void
    88 SemanticAnalysis::analyze()
    89 {
    90     scope_ = unit_->globalScope();
    91 
    92     module_ = Module::New(zone_);
    93 
    94     for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
    95         Statement *stmt = unit_->tree()->statements()->at(i);
    96         stmt->accept(this);
    97     }
    98 
    99     Local<Struct> globals(zone_, unit_->globalScope()->build(zone_));
   100     if (!globals)
   101         return;
   102     module_->setGlobals(globals);
   103 
   104     Local<Code> code(zone_, __ finish());
   105     if (!code)
   106         return;
   107     code->setModule(module_);
   108     module_->setCode(code);
   109 
   110 #ifndef NDEBUG
   111 //    Disassemble(code, stdout);
   112 #endif
   113 
   114     unit_->module_ = module_;
   115 }
   116 
   117 void
   118 SemanticAnalysis::visitFunctionStatement(FunctionStatement *stmt)
   119 {
   120     FunctionSymbol *sym = stmt->sym();
   121     Local<FunctionType> type(zone_, sym->type());
   122 
   123     assert(scope_->kind() == Scope::GLOBAL);
   124 
   125     if (stmt->token() == TOK_NATIVE) {
   126         // Define the native now.
   127         size_t index;
   128         if (!zone_->nativeTable()->add(stmt->name(), &index))
   129             return;
   130 
   131         Local<Native> native(zone_, Native::New(zone_, type, index));
   132         if (!native)
   133             return;
   134 
   135         __ object(native);
   136         __ setglobal(sym);
   137         __ pop();
   138     }
   139 
   140     // If the function is a forward or native, we don't have to do anything.
   141     if (stmt->token() == TOK_NATIVE || stmt->token() == TOK_FORWARD)
   142         return;
   143 
   144     SemanticAnalysis child(this, type);
   145     Local<Function> funobj(zone_, child.compile(stmt));
   146     if (!funobj)
   147         return;
   148 
   149     __ object(funobj);
   150     __ setglobal(sym);
   151     if (stmt->token() == TOK_PUBLIC)
   152         __ public_();
   153     else
   154         __ pop();
   155 }
   156 
   157 Function *
   158 SemanticAnalysis::compile(FunctionStatement *stmt)
   159 {
   160     __ setFirstLine(stmt->pos().line);
   161 
   162     AutoEnterScope argScope(&scope_, stmt->argScope());
   163     AutoEnterScope varScope(&scope_, stmt->varScope());
   164 
   165     stmt->body()->accept(this);
   166 
   167     Local<Code> code(zone_, __ finish());
   168     if (!code)
   169         return NULL;
   170     code->setModule(module_);
   171 
   172     Local<Function> fun(zone_, Function::New(zone_, code, fun_, stmt->name(), stmt->pos()));
   173     if (!fun)
   174         return NULL;
   175 
   176 #ifndef NDEBUG
   177 //    Disassemble(code, stdout);
   178 #endif
   179 
   180     return fun;
   181 }
   182 
   183 void 
   184 SemanticAnalysis::visitBlockStatement(BlockStatement *node)
   185 {
   186     AutoEnterScope scope(&scope_, node->scope());
   187 
   188     for (size_t i = 0; i < node->statements()->length(); i++) {
   189         Statement *stmt = node->statements()->at(i);
   190         stmt->accept(this);
   191         assert(__ stackDepth() == 0);
   192     }
   193 }
   194 
   195 void
   196 SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
   197 {
   198     // While building HIR, we enter new pool and root scopes, since HIR
   199     // creation is purely temporary.
   200     PoolScope poolScope(zone_->pool());
   201     RootScope rootScope(zone_);
   202 
   203     HIR *hir = eval(node->expression());
   204     if (!hir)
   205         return;
   206 
   207     discharge(hir);
   208     __ pop();
   209 }
   210 
   211 void
   212 SemanticAnalysis::visitIfStatement(IfStatement *node)
   213 {
   214     Label join;
   215 
   216     if (!node->ifFalse()) {
   217         Label success;
   218 
   219         visitForTest(node->condition(), &success, &join, &success);
   220         __ bind(&success);
   221         node->ifTrue()->accept(this);
   222     } else {
   223         Label success, next;
   224 
   225         visitForTest(node->condition(), &success, &next, &success);
   226         __ bind(&success);
   227         node->ifTrue()->accept(this);
   228         __ jump(&join);
   229         __ bind(&next);
   230         node->ifFalse()->accept(this);
   231     }
   232 
   233     __ bind(&join);
   234 }
   235 
   236 void
   237 SemanticAnalysis::visitForStatement(ForStatement *node)
   238 {
   239     AutoEnterScope scope(&scope_, node->scope());
   240     PoolScope poolScope(zone_->pool());
   241     RootScope rootScope(zone_);
   242 
   243     Label join, update;
   244     LoopScope loop(scope_, &join, &update, &loop_);
   245 
   246     if (node->initialization())
   247         node->initialization()->accept(this);
   248 
   249     Label header, test;
   250     if (node->condition())
   251         __ jump(&test);
   252 
   253     __ bind(&header);
   254     node->body()->accept(this);
   255 
   256     __ bind(&update);
   257     if (node->update())
   258         node->update()->accept(this);
   259 
   260     if (node->condition()) {
   261         __ bind(&test);
   262         visitForTest(node->condition(), &header, &join, &join);
   263     } else {
   264         __ jump(&header);
   265     }
   266 
   267     __ bind(&join);
   268 
   269     assert(__ stackDepth() == 0);
   270 }
   271 
   272 void
   273 SemanticAnalysis::visitWhileStatement(WhileStatement *node)
   274 {
   275     PoolScope poolScope(zone_->pool());
   276     RootScope rootScope(zone_);
   277 
   278     Label header, join;
   279 
   280     __ bind(&header);
   281 
   282     LoopScope loop(scope_, &join, &header, &loop_);
   283     if (node->token() == TOK_WHILE) {
   284         Label body;
   285         visitForTest(node->condition(), &body, &join, &body);
   286         node->body()->accept(this);
   287         __ jump(&header);
   288     } else {
   289         node->body()->accept(this);
   290         visitForTest(node->condition(), &header, &join, &join);
   291     }
   292 
   293     __ bind(&join);
   294 
   295     assert(__ stackDepth() == 0);
   296 }
   297 
   298 static inline Heap::Tenure
   299 InferTenure(Symbol *sym)
   300 {
   301     if (sym->scope()->kind() == Scope::GLOBAL)
   302         return Heap::Tenure_Old;
   303     // :TODO: need to use LIFO!
   304     return Heap::Tenure_Default;
   305 }
   306 
   307 void
   308 SemanticAnalysis::visitVariableDeclaration(VariableDeclaration *node)
   309 {
   310     PoolScope poolScope(zone_->pool());
   311     RootScope rootScope(zone_);
   312 
   313     if (node->initialization()) {
   314         HIR *hir = NULL;
   315 
   316         if ((hir = eval(node->initialization())) == NULL)
   317             return;
   318         if ((hir = coerce(hir, node->sym()->type(), Coerce_Assign)) == NULL)
   319             return;
   320 
   321         discharge(hir);
   322     } else {
   323         if (node->sym()->type()->isInt32() ||
   324             node->sym()->type()->isEnum() ||
   325             node->sym()->type()->isChar() ||
   326             node->sym()->type()->isBool())
   327         {
   328             __ int_(0);
   329         } else if (node->sym()->type()->isFloat()) {
   330             __ float_(0);
   331         } else if (node->sym()->type()->isArray()) {
   332             Local<ArrayType> type(zone_, ArrayType::cast(node->sym()->type()));
   333             Local<ArrayMap> map(zone_, ArrayMap::Attach(zone_, type));
   334             if (!map)
   335                 return;
   336 
   337             if (type->isFixedLength()) {
   338                 __ newarray(OP_NEWFIXED, map, InferTenure(node->sym()));
   339             } else if (node->dims()) {
   340                 assert(false);
   341                 __ newarray(OP_NEWSIZED, map, InferTenure(node->sym()));
   342             } else {
   343                 __ newarray(OP_NEWEMPTY, map, InferTenure(node->sym()));
   344             }
   345         }
   346     }
   347 
   348     if (!__ allocate(node->sym()))
   349         return;
   350 
   351     set(node->sym());
   352     __ pop();
   353 }
   354 
   355 void
   356 SemanticAnalysis::visitReturnStatement(ReturnStatement *node)
   357 {
   358     if (!node->expression()) {
   359         if (!fun_->returnType()->isVoid()) {
   360             cc_.reportError(node->pos(), Message_UsedVoidReturn);
   361             return;
   362         }
   363 
   364         __ returnvoid();
   365         return;
   366     }
   367 
   368     PoolScope poolScope(zone_->pool());
   369     RootScope rootScope(zone_);
   370 
   371     HIR *hir = eval(node->expression());
   372     if (!hir)
   373         return;
   374 
   375     Local<Type> returnType(zone_, fun_->returnType());
   376     if ((hir = coerce(hir, returnType, Coerce_Return)) == NULL)
   377         return;
   378 
   379     discharge(hir);
   380     __ return_();
   381 }
   382 
   383 void
   384 SemanticAnalysis::visitBinaryExpression(BinaryExpression *node)
   385 {
   386     HIR *left = eval(node->left());
   387     HIR *right = eval(node->right());
   388     if (!left || !right)
   389         return;
   390 
   391     hir_ = binary(node->token(), left, right);
   392 }
   393 
   394 bool
   395 SemanticAnalysis::checkArgumentCount(Handle<FunctionType> type, unsigned actual)
   396 {
   397     if (type->parameters()->length() == actual)
   398         return true;
   399 
   400     if (actual > type->parameters()->length()) {
   401         if (!type->isNative() || !type->isNativeVariadic())
   402             return false;
   403         return true;
   404     }
   405 
   406     if (!type->defaults())
   407         return false;
   408 
   409     // Missing arguments. See if there's a default argument for each missing
   410     // actual.
   411     for (unsigned i = actual; i < type->parameters()->length(); i++) {
   412         if (!type->defaults()->at(i))
   413             return false;
   414     }
   415 
   416     return true;
   417 }
   418 
   419 void
   420 SemanticAnalysis::visitNameProxy(NameProxy *proxy)
   421 {
   422     Symbol *sym = proxy->sym();
   423     VariableSymbol *var = sym->asVariable();
   424 
   425     Scope *in = sym->scope();
   426     switch (in->kind()) {
   427       case Scope::GLOBAL:
   428         hir_ = new (pool_) HGlobal(sym);
   429         return;
   430 
   431       default:
   432         assert(in->kind() == Scope::BLOCK);
   433         assert(var->storage() == VariableSymbol::Local);
   434         hir_ = new (pool_) HLocal(var);
   435         return;
   436     }
   437 }
   438 
   439 void
   440 SemanticAnalysis::visitIntegerLiteral(IntegerLiteral *lit)
   441 {
   442     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
   443     hir_ = new (pool_) HInteger(type, lit->value());
   444 }
   445 
   446 void
   447 SemanticAnalysis::visitFloatLiteral(FloatLiteral *node)
   448 {
   449     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
   450     hir_ = new (pool_) HFloat(type, (float)node->value());
   451 }
   452 
   453 void
   454 SemanticAnalysis::visitCallExpression(CallExpression *node)
   455 {
   456     HIR *callee = eval(node->callee());
   457     if (!callee)
   458         return;
   459 
   460     if (!callee->type()->isFunction()) {
   461         cc_.reportError(node->pos(), Message_CalleeNotFunction);
   462         return;
   463     }
   464 
   465     Local<FunctionType> fun(zone_, FunctionType::cast(callee->type()));
   466     
   467     if (!checkArgumentCount(fun, node->arguments()->length())) {
   468         cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
   469         return;
   470     }
   471 
   472     if (fun->isForward()) {
   473         cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
   474         return;
   475     }
   476 
   477     HIRList *args = new (pool_) HIRList;
   478     for (unsigned i = 0; i < node->arguments()->length(); i++) {
   479         Expression *expression = node->arguments()->at(i);
   480         HIR *arg = eval(expression);
   481         if (!arg)
   482             return;
   483 
   484         Handle<Type> given = arg->type();
   485         
   486         bool mustComputeReference;
   487         if (i >= fun->parameters()->length()) {
   488             // Special handling for natives. Arguments past the formal must
   489             // always have references computed. However, arrays are already
   490             // references for natives.
   491             assert(fun->isNative() && fun->isNativeVariadic());
   492             mustComputeReference = !given->isReference() &&
   493                                    !given->isArray();
   494         } else {
   495             Local<Type> actual(zone_, fun->parameterAt(i));
   496 
   497             if (expression->isIndexExpression()) {
   498                 // Temporarily disabled.
   499                 assert(!actual->isArray());
   500                 if (actual->isReference()) {
   501                     cc_.reportError(expression->pos(), Message_CannotComputeIndexRef);
   502                     return;
   503                 }
   504             }
   505 
   506             // When we start overloading, coercion will have to happen later.
   507             arg = coerce(arg, actual, Coerce_Arg);
   508             if (!arg)
   509                 return;
   510 
   511             mustComputeReference = actual->isReference() && !given->isReference();
   512         }
   513 
   514         // :TODO: handle mustComputeReference
   515         assert(!mustComputeReference);
   516 
   517         if (!args->append(arg))
   518             return;
   519     }
   520 
   521     hir_ = new (pool_) HCall(fun->returnType(), callee, args);
   522 }
   523 
   524 void
   525 SemanticAnalysis::visitAssignment(Assignment *node)
   526 {
   527     HLValue *lval = lvalue(node->lvalue());
   528     if (!lval)
   529         return;
   530 
   531     HIR *hir = eval(node->expression());
   532     if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
   533         return;
   534 
   535     hir_ = new (pool_) HStore(node->token(), lval, hir);
   536 }
   537 
   538 void
   539 SemanticAnalysis::visitBreakStatement(BreakStatement *node)
   540 {
   541     if (!loop_) {
   542         cc_.reportError(node->pos(), Message_BreakOutsideLoop);
   543         return;
   544     }
   545 
   546     // Note, if we re-introduce LIFO allocs, we need to unwind here.
   547     __ jump(loop_->break_());
   548 }
   549 
   550 void
   551 SemanticAnalysis::visitContinueStatement(ContinueStatement *node)
   552 {
   553     if (!loop_) {
   554         cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
   555         return;
   556     }
   557 
   558     // Note, if we re-introduce LIFO allocs, we need to unwind here.
   559     __ jump(loop_->continue_());
   560 }
   561 
   562 void
   563 SemanticAnalysis::visitIndexExpression(IndexExpression *node)
   564 {
   565     HIR *left = eval(node->left());
   566     if (!left)
   567         return;
   568 
   569     HIR *right = eval(node->right());
   570     if (!right)
   571         return;
   572 
   573     // Hrm... yes, for now, let's coerce here.
   574     Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
   575     if ((right = coerce(right, i32, Coerce_Arg)) == NULL)
   576         return;
   577     
   578     // Do not coerce here, there's no reason to yet.
   579     if (!left->type()->isArray()) {
   580         cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
   581         return;
   582     }
   583 
   584     hir_ = new (pool_) HIndex(ArrayType::cast(left->type())->contained(),
   585                               left,
   586                               right);
   587 }
   588 
   589 void
   590 SemanticAnalysis::visitIncDecExpression(IncDecExpression *node)
   591 {
   592     HLValue *lval = lvalue(node->expression());
   593     if (!lval)
   594         return;
   595 
   596     if (!lval->type()->isInt32() && !lval->type()->isFloat()) {
   597         assert(false);
   598         return;
   599     }
   600 
   601     HIR *rval;
   602     if (lval->type()->isInt32())
   603         rval = new (pool_) HInteger(lval->type(), 1);
   604     else
   605         rval = new (pool_) HFloat(lval->type(), 1.0);
   606 
   607     if (node->postfix()) {
   608         assert(false);
   609     } else {
   610         hir_ = new (pool_) HStore(node->token(), lval, rval);
   611     }
   612 }
   613 
   614 Type *
   615 SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
   616 {
   617     Handle<Type> l = left->type();
   618     Handle<Type> r = right->type();
   619 
   620     if (token >= TOK_PLUS && token <= TOK_SLASH) {
   621         // We allow: [float|int32] + [float|int32]
   622         if (l->isFloat() && r->isInt32())
   623             return l;
   624         if (l->isInt32() && r->isFloat())
   625             return r;
   626         if (l->isInt32() && r->isInt32())
   627             return l;
   628     }
   629 
   630     assert(l == r);
   631 
   632     return left->type();
   633 }
   634 
   635 HIR *
   636 SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
   637 {
   638     Handle<Type> from = hir->type();
   639 
   640     if (from->isInt32() && to->isFloat())
   641         return new (pool_) HConvert(to, OP_CVT_I2F, hir);
   642     if (from->isInt32() && to->isBool())
   643         return new (pool_) HConvert(to, OP_CVT_I2B, hir);
   644     
   645     assert(from == to);
   646     
   647     return hir;
   648 }
   649 
   650 void
   651 SemanticAnalysis::set(VariableSymbol *sym)
   652 {
   653     if (sym->scope()->kind() == Scope::GLOBAL) {
   654         __ setglobal(sym);
   655     } else {
   656         assert(sym->storage() == VariableSymbol::Local);
   657         __ setlocal(sym);
   658     }
   659 }
   660 
   661 HIR *
   662 SemanticAnalysis::binary(TokenKind token, HIR *left, HIR *right)
   663 {
   664     Local<Type> coercion(zone_, coercionType(token, left, right));
   665     if (!coercion)
   666         return NULL;
   667     if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
   668         return NULL;
   669     if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
   670         return NULL;
   671     
   672     switch (token) {
   673      case TOK_PLUS:
   674       case TOK_MINUS:
   675       case TOK_STAR:
   676       case TOK_SLASH:
   677       case TOK_PERCENT:
   678       case TOK_AMPERSAND:
   679       case TOK_BITOR:
   680       case TOK_BITXOR:
   681       case TOK_SHR:
   682       case TOK_USHR:
   683       case TOK_SHL:
   684         return new (pool_) HBinary(left->type(), token, left, right);
   685 
   686       case TOK_EQUALS:
   687       case TOK_NOTEQUALS:
   688       case TOK_LT:
   689       case TOK_LE:
   690       case TOK_GT:
   691       case TOK_GE:
   692       case TOK_AND:
   693       case TOK_OR:
   694       {
   695         Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   696         return new (pool_) HBinary(result, token, left, right);
   697       }
   698 
   699       default:
   700         assert(false);
   701         return NULL;
   702     }
   703 }
   704 
   705 static inline TokenKind
   706 ToCompare(Expression *expr)
   707 {
   708     if (expr->isBinaryExpression()) {
   709         BinaryExpression *b = expr->toBinaryExpression();
   710         if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
   711             return b->token();
   712     } else if (expr->isUnaryExpression()) {
   713         UnaryExpression *u = expr->toUnaryExpression();
   714         if (u->token() == TOK_NOT)
   715             return TOK_NOT;
   716     }
   717     return TOK_EOF;
   718 }
   719 
   720 void
   721 SemanticAnalysis::visitForTest(Expression *expr,
   722                                Label *trueBranch,
   723                                Label *falseBranch,
   724                                Label *fallthrough)
   725 {
   726     Local<Type> boolType(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   727 
   728     TokenKind token = ToCompare(expr);
   729     if (token == TOK_EOF || token == TOK_NOT) {
   730         HIR *hir;
   731         if (token == TOK_EOF)
   732             hir = eval(expr);
   733         else
   734             hir = eval(expr->toUnaryExpression()->expression());
   735         if (!hir)
   736             return;
   737         if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   738             return;
   739 
   740         discharge(hir);
   741 
   742         bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
   743         Opcode op = jumpOnTrue ? OP_JT : OP_JF;
   744         
   745         if (fallthrough == falseBranch)
   746             __ j(op, trueBranch);
   747         else
   748             __ j(op, falseBranch);
   749         return;
   750     }
   751 
   752     BinaryExpression *bin = expr->toBinaryExpression();
   753     if (token == TOK_AND || token == TOK_OR) {
   754         assert(false);
   755         //visitForLogicTest(expr, trueBranch, falseBranch, fallthrough);
   756         return;
   757     }
   758 
   759     HIR *left = eval(bin->left());
   760     HIR *right = eval(bin->right());
   761     if (!left || !right)
   762         return;
   763 
   764     HIR *hir = binary(bin->token(), left, right);
   765     if (!hir)
   766         return;
   767     if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   768         return;
   769 
   770     discharge(hir);
   771     if (fallthrough == trueBranch)
   772         __ j(OP_JF, falseBranch);
   773     else
   774         __ j(OP_JT, trueBranch);
   775 }
   776 
   777 HLValue *
   778 SemanticAnalysis::lvalue(Expression *expr)
   779 {
   780     HIR *hir = eval(expr);
   781     if (!hir)
   782         return NULL;
   783     
   784     if (!hir->isLValue()) {
   785         cc_.reportError(expr->pos(), Message_ExpectedLValue);
   786         return NULL;
   787     }
   788 
   789     return static_cast<HLValue *>(hir);
   790 }
   791 
   792 HIR *
   793 SemanticAnalysis::eval(Expression *expr)
   794 {
   795     assert(!hir_);
   796     expr->accept(this);
   797 
   798     HIR *hir = hir_;
   799     hir_ = NULL;
   800 
   801     return hir;
   802 }
   803 
   804 void
   805 SemanticAnalysis::discharge(HIR *hir)
   806 {
   807     EmitHIR(zone_, emitter_, hir);
   808 }