src/compiler/SemanticAnalysis.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 15:24:30 2012 -0800 (2012-11-18)
changeset 215 b8f650a441a5
parent 212 443d24cfdf2d
child 217 c22e983b3b6e
permissions -rw-r--r--
Get load-store assign ops 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 Type *
   590 SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
   591 {
   592     Handle<Type> l = left->type();
   593     Handle<Type> r = right->type();
   594 
   595     if (token >= TOK_PLUS && token <= TOK_SLASH) {
   596         // We allow: [float|int32] + [float|int32]
   597         if (l->isFloat() && r->isInt32())
   598             return l;
   599         if (l->isInt32() && r->isFloat())
   600             return r;
   601         if (l->isInt32() && r->isInt32())
   602             return l;
   603     }
   604 
   605     assert(l == r);
   606 
   607     return left->type();
   608 }
   609 
   610 HIR *
   611 SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
   612 {
   613     Handle<Type> from = hir->type();
   614 
   615     if (from->isInt32() && to->isFloat())
   616         return new (pool_) HConvert(to, OP_CVT_I2F, hir);
   617     if (from->isInt32() && to->isBool())
   618         return new (pool_) HConvert(to, OP_CVT_I2B, hir);
   619     
   620     assert(from == to);
   621     
   622     return hir;
   623 }
   624 
   625 void
   626 SemanticAnalysis::set(VariableSymbol *sym)
   627 {
   628     if (sym->scope()->kind() == Scope::GLOBAL) {
   629         __ setglobal(sym);
   630     } else {
   631         assert(sym->storage() == VariableSymbol::Local);
   632         __ setlocal(sym);
   633     }
   634 }
   635 
   636 HIR *
   637 SemanticAnalysis::binary(TokenKind token, HIR *left, HIR *right)
   638 {
   639     Local<Type> coercion(zone_, coercionType(token, left, right));
   640     if (!coercion)
   641         return NULL;
   642     if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
   643         return NULL;
   644     if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
   645         return NULL;
   646     
   647     switch (token) {
   648      case TOK_PLUS:
   649       case TOK_MINUS:
   650       case TOK_STAR:
   651       case TOK_SLASH:
   652       case TOK_PERCENT:
   653       case TOK_AMPERSAND:
   654       case TOK_BITOR:
   655       case TOK_BITXOR:
   656       case TOK_SHR:
   657       case TOK_USHR:
   658       case TOK_SHL:
   659         return new (pool_) HBinary(left->type(), token, left, right);
   660 
   661       case TOK_EQUALS:
   662       case TOK_NOTEQUALS:
   663       case TOK_LT:
   664       case TOK_LE:
   665       case TOK_GT:
   666       case TOK_GE:
   667       case TOK_AND:
   668       case TOK_OR:
   669       {
   670         Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   671         return new (pool_) HBinary(result, token, left, right);
   672       }
   673 
   674       default:
   675         assert(false);
   676         return NULL;
   677     }
   678 }
   679 
   680 static inline TokenKind
   681 ToCompare(Expression *expr)
   682 {
   683     if (expr->isBinaryExpression()) {
   684         BinaryExpression *b = expr->toBinaryExpression();
   685         if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
   686             return b->token();
   687     } else if (expr->isUnaryExpression()) {
   688         UnaryExpression *u = expr->toUnaryExpression();
   689         if (u->token() == TOK_NOT)
   690             return TOK_NOT;
   691     }
   692     return TOK_EOF;
   693 }
   694 
   695 void
   696 SemanticAnalysis::visitForTest(Expression *expr,
   697                                Label *trueBranch,
   698                                Label *falseBranch,
   699                                Label *fallthrough)
   700 {
   701     Local<Type> boolType(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   702 
   703     TokenKind token = ToCompare(expr);
   704     if (token == TOK_EOF || token == TOK_NOT) {
   705         HIR *hir;
   706         if (token == TOK_EOF)
   707             hir = eval(expr);
   708         else
   709             hir = eval(expr->toUnaryExpression()->expression());
   710         if (!hir)
   711             return;
   712         if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   713             return;
   714 
   715         discharge(hir);
   716 
   717         bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
   718         Opcode op = jumpOnTrue ? OP_JT : OP_JF;
   719         
   720         if (fallthrough == falseBranch)
   721             __ j(op, trueBranch);
   722         else
   723             __ j(op, falseBranch);
   724         return;
   725     }
   726 
   727     BinaryExpression *bin = expr->toBinaryExpression();
   728     if (token == TOK_AND || token == TOK_OR) {
   729         assert(false);
   730         //visitForLogicTest(expr, trueBranch, falseBranch, fallthrough);
   731         return;
   732     }
   733 
   734     HIR *left = eval(bin->left());
   735     HIR *right = eval(bin->right());
   736     if (!left || !right)
   737         return;
   738 
   739     HIR *hir = binary(bin->token(), left, right);
   740     if (!hir)
   741         return;
   742     if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   743         return;
   744 
   745     discharge(hir);
   746     if (fallthrough == trueBranch)
   747         __ j(OP_JF, falseBranch);
   748     else
   749         __ j(OP_JT, trueBranch);
   750 }
   751 
   752 HLValue *
   753 SemanticAnalysis::lvalue(Expression *expr)
   754 {
   755     HIR *hir = eval(expr);
   756     if (!hir)
   757         return NULL;
   758     
   759     if (!hir->isLValue()) {
   760         cc_.reportError(expr->pos(), Message_ExpectedLValue);
   761         return NULL;
   762     }
   763 
   764     return static_cast<HLValue *>(hir);
   765 }
   766 
   767 HIR *
   768 SemanticAnalysis::eval(Expression *expr)
   769 {
   770     assert(!hir_);
   771     expr->accept(this);
   772 
   773     HIR *hir = hir_;
   774     hir_ = NULL;
   775 
   776     return hir;
   777 }
   778 
   779 void
   780 SemanticAnalysis::discharge(HIR *hir)
   781 {
   782     EmitHIR(zone_, emitter_, hir);
   783 }