src/compiler/SemanticAnalysis.cpp
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 262 c4b1341297e5
permissions -rw-r--r--
Normalise line endings.
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#include <assert.h>
asherkin@263
    20
#include "../Zone.h"
asherkin@263
    21
#include "../Strings.h"
asherkin@263
    22
#include "../NativeTable.h"
asherkin@263
    23
#include "../TypeManager.h"
asherkin@263
    24
#include "../Modules.h"
asherkin@263
    25
#include "../Code.h"
asherkin@263
    26
#include "SemanticAnalysis.h"
asherkin@263
    27
#include "../Heap-inl.h"
asherkin@263
    28
asherkin@263
    29
using namespace ke;
asherkin@263
    30
asherkin@263
    31
#define __ emitter_.
asherkin@263
    32
asherkin@263
    33
typedef BytecodeEmitter::Label Label;
asherkin@263
    34
asherkin@263
    35
class AutoEnterScope
asherkin@263
    36
{
asherkin@263
    37
  public:
asherkin@263
    38
    AutoEnterScope(Scope **prevp, Scope *scope)
asherkin@263
    39
      : prevp_(prevp),
asherkin@263
    40
        prev_(*prevp),
asherkin@263
    41
        scope_(scope)
asherkin@263
    42
    {
asherkin@263
    43
        if (!scope_)
asherkin@263
    44
            return;
asherkin@263
    45
        prev_ = *prevp_;
asherkin@263
    46
        *prevp_ = scope;
asherkin@263
    47
        assert(prev_ == scope->enclosing());
asherkin@263
    48
    }
asherkin@263
    49
    ~AutoEnterScope()
asherkin@263
    50
    {
asherkin@263
    51
        if (scope_)
asherkin@263
    52
            *prevp_ = prev_;
asherkin@263
    53
    }
asherkin@263
    54
  private:
asherkin@263
    55
    Scope **prevp_;
asherkin@263
    56
    Scope *prev_;
asherkin@263
    57
    Scope *scope_;
asherkin@263
    58
};
asherkin@263
    59
asherkin@263
    60
SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit)
asherkin@263
    61
  : zone_(zone),
asherkin@263
    62
    pool_(zone->pool()),
asherkin@263
    63
    cc_(cc),
asherkin@263
    64
    unit_(unit),
asherkin@263
    65
    hir_(NULL),
asherkin@263
    66
    loop_(NULL)
asherkin@263
    67
{
asherkin@263
    68
}
asherkin@263
    69
asherkin@263
    70
SemanticAnalysis::SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun)
asherkin@263
    71
  : zone_(parent->zone_),
asherkin@263
    72
    pool_(parent->pool_),
asherkin@263
    73
    cc_(parent->cc_),
asherkin@263
    74
    unit_(parent->unit_),
asherkin@263
    75
    hir_(NULL),
asherkin@263
    76
    scope_(parent->scope_),
asherkin@263
    77
    fun_(fun),
asherkin@263
    78
    loop_(NULL),
asherkin@263
    79
    control_(NULL)
asherkin@263
    80
{
asherkin@263
    81
asherkin@263
    82
}
asherkin@263
    83
asherkin@263
    84
void
asherkin@263
    85
SemanticAnalysis::analyze()
asherkin@263
    86
{
asherkin@263
    87
    scope_ = unit_->globalScope();
asherkin@263
    88
asherkin@263
    89
    for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
asherkin@263
    90
        Statement *stmt = unit_->tree()->statements()->at(i);
asherkin@263
    91
        stmt->accept(this);
asherkin@263
    92
    }
asherkin@263
    93
asherkin@263
    94
    Local<Code> code(zone_, __ finish());
asherkin@263
    95
    if (!code)
asherkin@263
    96
        return;
asherkin@263
    97
    code->setModule(unit_->module());
asherkin@263
    98
    unit_->module()->setCode(code);
asherkin@263
    99
asherkin@263
   100
#ifndef NDEBUG
asherkin@263
   101
//    Disassemble(code, stdout);
asherkin@263
   102
#endif
asherkin@263
   103
}
asherkin@263
   104
asherkin@263
   105
void
asherkin@263
   106
SemanticAnalysis::visitFunctionStatement(FunctionStatement *stmt)
asherkin@263
   107
{
asherkin@263
   108
    FunctionSymbol *sym = stmt->sym();
asherkin@263
   109
    Local<FunctionType> type(zone_, sym->type());
asherkin@263
   110
asherkin@263
   111
    assert(scope_->kind() == Scope::GLOBAL);
asherkin@263
   112
asherkin@263
   113
    if (stmt->token() == TOK_NATIVE) {
asherkin@263
   114
        // Define the native now.
asherkin@263
   115
        size_t index;
asherkin@263
   116
        if (!zone_->nativeTable()->add(stmt->name(), &index))
asherkin@263
   117
            return;
asherkin@263
   118
asherkin@263
   119
        Local<Native> native(zone_, Native::New(zone_, type, index));
asherkin@263
   120
        if (!native)
asherkin@263
   121
            return;
asherkin@263
   122
asherkin@263
   123
        __ object(native);
asherkin@263
   124
        __ setglobal(sym);
asherkin@263
   125
        __ pop();
asherkin@263
   126
    }
asherkin@263
   127
asherkin@263
   128
    // If the function is a forward or native, we don't have to do anything.
asherkin@263
   129
    if (stmt->token() == TOK_NATIVE || stmt->token() == TOK_FORWARD)
asherkin@263
   130
        return;
asherkin@263
   131
asherkin@263
   132
    SemanticAnalysis child(this, type);
asherkin@263
   133
    Local<Function> funobj(zone_, child.compile(stmt));
asherkin@263
   134
    if (!funobj)
asherkin@263
   135
        return;
asherkin@263
   136
asherkin@263
   137
    __ object(funobj);
asherkin@263
   138
    __ setglobal(sym);
asherkin@263
   139
    if (stmt->token() == TOK_PUBLIC)
asherkin@263
   140
        __ public_();
asherkin@263
   141
    else
asherkin@263
   142
        __ pop();
asherkin@263
   143
}
asherkin@263
   144
asherkin@263
   145
Function *
asherkin@263
   146
SemanticAnalysis::compile(FunctionStatement *stmt)
asherkin@263
   147
{
asherkin@263
   148
    __ setFirstLine(stmt->pos().line);
asherkin@263
   149
asherkin@263
   150
    AutoEnterScope argScope(&scope_, stmt->argScope());
asherkin@263
   151
asherkin@263
   152
    for (size_t i = 0; i < stmt->signature().parameters()->length(); i++) {
asherkin@263
   153
        Parameter *param = stmt->signature().parameters()->at(i);
asherkin@263
   154
        param->sym()->allocate(VariableSymbol::Arg, i);
asherkin@263
   155
    }
asherkin@263
   156
asherkin@263
   157
    AutoEnterScope varScope(&scope_, stmt->varScope());
asherkin@263
   158
    ControlStatus status = visitForControl(stmt->body());
asherkin@263
   159
asherkin@263
   160
    if (!fun_->returnType()->isVoid()) {
asherkin@263
   161
        // If the function is not void, report if not all paths return a value.
asherkin@263
   162
        // All other incorrect return cases have been caught already.
asherkin@263
   163
        if (status.returns() == Returns_Sometimes) {
asherkin@263
   164
            cc_.reportError(stmt->pos(), Message_NotAllPathsReturnValue);
asherkin@263
   165
            return NULL;
asherkin@263
   166
        }
asherkin@263
   167
asherkin@263
   168
        if (status.returns() == Returns_Never) {
asherkin@263
   169
            cc_.reportError(stmt->pos(), Message_MissingReturnValue);
asherkin@263
   170
            return NULL;
asherkin@263
   171
        }
asherkin@263
   172
    }
asherkin@263
   173
asherkin@263
   174
    Local<Code> code(zone_, __ finish());
asherkin@263
   175
    if (!code)
asherkin@263
   176
        return NULL;
asherkin@263
   177
    code->setModule(unit_->module());
asherkin@263
   178
asherkin@263
   179
    Local<Function> fun(zone_, Function::New(zone_, code, fun_, stmt->name(), stmt->pos()));
asherkin@263
   180
    if (!fun)
asherkin@263
   181
        return NULL;
asherkin@263
   182
asherkin@263
   183
#ifndef NDEBUG
asherkin@263
   184
//    Disassemble(code, stdout);
asherkin@263
   185
#endif
asherkin@263
   186
asherkin@263
   187
    return fun;
asherkin@263
   188
}
asherkin@263
   189
asherkin@263
   190
void 
asherkin@263
   191
SemanticAnalysis::visitBlockStatement(BlockStatement *node)
asherkin@263
   192
{
asherkin@263
   193
    AutoEnterScope scope(&scope_, node->scope());
asherkin@263
   194
asherkin@263
   195
    for (size_t i = 0; i < node->statements()->length(); i++) {
asherkin@263
   196
        Statement *stmt = node->statements()->at(i);
asherkin@263
   197
        __ note_position(stmt->pos());
asherkin@263
   198
        stmt->accept(this);
asherkin@263
   199
        assert(__ stackDepth() == 0);
asherkin@263
   200
    }
asherkin@263
   201
}
asherkin@263
   202
asherkin@263
   203
void
asherkin@263
   204
SemanticAnalysis::visitEnumStatement(EnumStatement *node)
asherkin@263
   205
{
asherkin@263
   206
}
asherkin@263
   207
asherkin@263
   208
void
asherkin@263
   209
SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
asherkin@263
   210
{
asherkin@263
   211
    // While building HIR, we enter new pool and root scopes, since HIR
asherkin@263
   212
    // creation is purely temporary.
asherkin@263
   213
    PoolScope poolScope(zone_->pool());
asherkin@263
   214
    RootScope rootScope(zone_);
asherkin@263
   215
asherkin@263
   216
    HIR *hir = eval(node->expression());
asherkin@263
   217
    if (!hir)
asherkin@263
   218
        return;
asherkin@263
   219
asherkin@263
   220
    discharge(hir);
asherkin@263
   221
    if (!hir->type()->isVoid())
asherkin@263
   222
        __ pop();
asherkin@263
   223
}
asherkin@263
   224
asherkin@263
   225
void
asherkin@263
   226
SemanticAnalysis::ControlScope::merge(const ControlStatus &left, const ControlStatus &right)
asherkin@263
   227
{
asherkin@263
   228
    // If both sides terminate in a return, we consider this a return as well.
asherkin@263
   229
    if (left.returns() == Returns_Always && right.returns() == Returns_Always) {
asherkin@263
   230
        terminate(Returns_Always);
asherkin@263
   231
        return;
asherkin@263
   232
    }
asherkin@263
   233
asherkin@263
   234
    ReturnStatus status = (left.returns() == Returns_Never && right.returns() == Returns_Never)
asherkin@263
   235
                          ? Returns_Never
asherkin@263
   236
                          : Returns_Sometimes;
asherkin@263
   237
asherkin@263
   238
    if (left.terminates() && right.terminates())
asherkin@263
   239
        terminate(status);
asherkin@263
   240
    else
asherkin@263
   241
        returns(status);
asherkin@263
   242
}
asherkin@263
   243
asherkin@263
   244
SemanticAnalysis::ControlStatus
asherkin@263
   245
SemanticAnalysis::visitForControl(Statement *statement)
asherkin@263
   246
{
asherkin@263
   247
    ControlScope scope(&control_);
asherkin@263
   248
    statement->accept(this);
asherkin@263
   249
    return scope.status();
asherkin@263
   250
}
asherkin@263
   251
asherkin@263
   252
void
asherkin@263
   253
SemanticAnalysis::visitIfStatement(IfStatement *node)
asherkin@263
   254
{
asherkin@263
   255
    Label join;
asherkin@263
   256
asherkin@263
   257
    ControlStatus trueBranch;
asherkin@263
   258
    ControlStatus falseBranch(false, Returns_Never);
asherkin@263
   259
asherkin@263
   260
    if (!node->ifFalse()) {
asherkin@263
   261
        Label success;
asherkin@263
   262
asherkin@263
   263
        visitForTest(node->condition(), &success, &join, &success);
asherkin@263
   264
        __ bind(&success);
asherkin@263
   265
        trueBranch = visitForControl(node->ifTrue());
asherkin@263
   266
    } else {
asherkin@263
   267
        Label success, next;
asherkin@263
   268
asherkin@263
   269
        visitForTest(node->condition(), &success, &next, &success);
asherkin@263
   270
        __ bind(&success);
asherkin@263
   271
        trueBranch = visitForControl(node->ifTrue());
asherkin@263
   272
        __ jump(&join);
asherkin@263
   273
        __ bind(&next);
asherkin@263
   274
        falseBranch = visitForControl(node->ifFalse());
asherkin@263
   275
    }
asherkin@263
   276
asherkin@263
   277
    __ bind(&join);
asherkin@263
   278
asherkin@263
   279
    control_->merge(trueBranch, falseBranch);
asherkin@263
   280
}
asherkin@263
   281
asherkin@263
   282
void
asherkin@263
   283
SemanticAnalysis::requireLifoStack()
asherkin@263
   284
{
asherkin@263
   285
    if (!loop_)
asherkin@263
   286
        return;
asherkin@263
   287
asherkin@263
   288
    loop_->setUsesLifoStack();
asherkin@263
   289
}
asherkin@263
   290
asherkin@263
   291
SemanticAnalysis::ControlStatus
asherkin@263
   292
SemanticAnalysis::visitLoopBody(Statement *stmt)
asherkin@263
   293
{
asherkin@263
   294
    // We always mark off a loop body, so on each edge, we can balance the
asherkin@263
   295
    // LIFO stack. Right now, we don't have the ability to budget the LIFO
asherkin@263
   296
    // stack ahead of time. If we turn out to never need the LIFO stack, we
asherkin@263
   297
    // mark it as unnecessary. (The interpreter can't really make use of this,
asherkin@263
   298
    // but the JIT can).
asherkin@263
   299
    //
asherkin@263
   300
    // Note that balancing the LIFO stack isn't even necessary, so this
asherkin@263
   301
    // feature may be removed. All this mechanism does is mitigate the
asherkin@263
   302
    // possibility that a long-running loop will overflow the LIFO region
asherkin@263
   303
    // and therefore start requiring nursery allocations.
asherkin@263
   304
    int pos = __ enterloop();
asherkin@263
   305
    ControlStatus status = visitForControl(stmt);
asherkin@263
   306
    __ leaveloop();
asherkin@263
   307
asherkin@263
   308
    if (loop_->usesLifoStack())
asherkin@263
   309
        __ setLoopUsesLifoStack(pos);
asherkin@263
   310
asherkin@263
   311
    return status;
asherkin@263
   312
}
asherkin@263
   313
asherkin@263
   314
void
asherkin@263
   315
SemanticAnalysis::visitForStatement(ForStatement *node)
asherkin@263
   316
{
asherkin@263
   317
    AutoEnterScope scope(&scope_, node->scope());
asherkin@263
   318
    PoolScope poolScope(zone_->pool());
asherkin@263
   319
    RootScope rootScope(zone_);
asherkin@263
   320
asherkin@263
   321
    Label join, update;
asherkin@263
   322
    LoopScope loop(scope_, &join, &update, &loop_);
asherkin@263
   323
asherkin@263
   324
    if (node->initialization())
asherkin@263
   325
        node->initialization()->accept(this);
asherkin@263
   326
asherkin@263
   327
    Label header, test;
asherkin@263
   328
    if (node->condition())
asherkin@263
   329
        __ jump(&test);
asherkin@263
   330
asherkin@263
   331
    __ bind(&header);
asherkin@263
   332
    ControlStatus status = visitLoopBody(node->body());
asherkin@263
   333
asherkin@263
   334
    __ bind(&update);
asherkin@263
   335
    if (node->update())
asherkin@263
   336
        node->update()->accept(this);
asherkin@263
   337
asherkin@263
   338
    if (node->condition()) {
asherkin@263
   339
        __ bind(&test);
asherkin@263
   340
        visitForTest(node->condition(), &header, &join, &join);
asherkin@263
   341
    } else {
asherkin@263
   342
        __ jump(&header);
asherkin@263
   343
    }
asherkin@263
   344
asherkin@263
   345
    __ bind(&join);
asherkin@263
   346
    
asherkin@263
   347
    // If there was a condition, we treat this like a while loop. Otherwise,
asherkin@263
   348
    // the join point is only reachable if the loop contains a break. The
asherkin@263
   349
    // termination status doesn't tell us anything yet.
asherkin@263
   350
    if (node->condition() || loop.breaks())
asherkin@263
   351
        control_->merge(status, ControlStatus(false, Returns_Never));
asherkin@263
   352
    else
asherkin@263
   353
        control_->terminate(status.returns());
asherkin@263
   354
asherkin@263
   355
    assert(__ stackDepth() == 0);
asherkin@263
   356
}
asherkin@263
   357
asherkin@263
   358
void
asherkin@263
   359
SemanticAnalysis::visitWhileStatement(WhileStatement *node)
asherkin@263
   360
{
asherkin@263
   361
    PoolScope poolScope(zone_->pool());
asherkin@263
   362
    RootScope rootScope(zone_);
asherkin@263
   363
asherkin@263
   364
    Label header, join;
asherkin@263
   365
asherkin@263
   366
    __ bind(&header);
asherkin@263
   367
asherkin@263
   368
    ControlStatus status;
asherkin@263
   369
asherkin@263
   370
    LoopScope loop(scope_, &join, &header, &loop_);
asherkin@263
   371
    if (node->token() == TOK_WHILE) {
asherkin@263
   372
        Label body;
asherkin@263
   373
        visitForTest(node->condition(), &body, &join, &body);
asherkin@263
   374
        status = visitLoopBody(node->body());
asherkin@263
   375
        __ jump(&header);
asherkin@263
   376
    } else {
asherkin@263
   377
        status = visitLoopBody(node->body());
asherkin@263
   378
        visitForTest(node->condition(), &header, &join, &join);
asherkin@263
   379
    }
asherkin@263
   380
asherkin@263
   381
    __ bind(&join);
asherkin@263
   382
asherkin@263
   383
    // Detect loops that will always have a fork in the contro-flow graph.
asherkin@263
   384
    // This will be a |while| loop, or a |do-while| loop whose body does
asherkin@263
   385
    // not terminate. For now, we treat these as an |if| regardless of the
asherkin@263
   386
    // direction the branch can take. That means the control-flow is always
asherkin@263
   387
    // conditional.
asherkin@263
   388
    if (node->token() == TOK_WHILE || status.terminates() || loop.breaks()) {
asherkin@263
   389
        control_->merge(status, ControlStatus(false, Returns_Never));
asherkin@263
   390
    } else {
asherkin@263
   391
        // At this point, we've got a do-while loop with a |return| or
asherkin@263
   392
        // |continue|, meaning that the rest of the block is unreachable.
asherkin@263
   393
        control_->terminate(status.returns());
asherkin@263
   394
    }
asherkin@263
   395
asherkin@263
   396
    assert(__ stackDepth() == 0);
asherkin@263
   397
}
asherkin@263
   398
asherkin@263
   399
static inline Heap::Tenure
asherkin@263
   400
InferTenure(Symbol *sym)
asherkin@263
   401
{
asherkin@263
   402
    if (sym->scope()->kind() == Scope::GLOBAL)
asherkin@263
   403
        return Heap::Tenure_Old;
asherkin@263
   404
asherkin@263
   405
    // SP2's array semantics allow any array kind to escape, so we must use
asherkin@263
   406
    // the normal heap. (Hopefully escape analysis can ease this pain later).
asherkin@263
   407
    if (sym->type()->isArray())
asherkin@263
   408
        return Heap::Tenure_Default;
asherkin@263
   409
asherkin@263
   410
    return Heap::Tenure_Default;
asherkin@263
   411
}
asherkin@263
   412
asherkin@263
   413
void
asherkin@263
   414
SemanticAnalysis::visitVariableDeclaration(VariableDeclaration *node)
asherkin@263
   415
{
asherkin@263
   416
    PoolScope poolScope(zone_->pool());
asherkin@263
   417
    RootScope rootScope(zone_);
asherkin@263
   418
asherkin@263
   419
    Local<Type> type(zone_, node->sym()->type());
asherkin@263
   420
asherkin@263
   421
    if (node->sym()->type()->isVoid()) {
asherkin@263
   422
        cc_.reportError(node->type()->pos(), Message_CannotDeclareVoid);
asherkin@263
   423
        return;
asherkin@263
   424
    }
asherkin@263
   425
asherkin@263
   426
    if (node->initialization()) {
asherkin@263
   427
        HIR *hir = NULL;
asherkin@263
   428
asherkin@263
   429
        if ((hir = eval(node->initialization())) == NULL)
asherkin@263
   430
            return;
asherkin@263
   431
        if ((hir = coerce(hir, type, Coerce_Decl)) == NULL)
asherkin@263
   432
            return;
asherkin@263
   433
asherkin@263
   434
        discharge(hir);
asherkin@263
   435
    } else {
asherkin@263
   436
        if (type->isInt32() || type->isEnum() ||
asherkin@263
   437
            type->isChar() || type->isBool())
asherkin@263
   438
        {
asherkin@263
   439
            __ int_(0);
asherkin@263
   440
        } else if (type->isFloat()) {
asherkin@263
   441
            __ float_(0);
asherkin@263
   442
        } else if (type->isArray()) {
asherkin@263
   443
            Local<ArrayType> atype(zone_, ArrayType::cast(type));
asherkin@263
   444
            Local<ArrayMap> map(zone_, atype->newMap());
asherkin@263
   445
            if (!map)
asherkin@263
   446
                return;
asherkin@263
   447
asherkin@263
   448
            if (atype->isFixedLength()) {
asherkin@263
   449
                __ newarray(OP_NEWFIXED, map, InferTenure(node->sym()));
asherkin@263
   450
            } else if (node->dims()->at(0)) {
asherkin@263
   451
                // This may not be true if we allow typedefing, so :TODO:
asherkin@263
   452
                // figure out proper semantics.
asherkin@263
   453
                assert(node->dims()->length() == atype->levels());
asherkin@263
   454
asherkin@263
   455
                Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
asherkin@263
   456
                for (size_t i = 0; i < node->dims()->length(); i++) {
asherkin@263
   457
                    Expression *expr = node->dims()->at(i);
asherkin@263
   458
                    if (expr) {
asherkin@263
   459
                        HIR *hir = eval(expr);
asherkin@263
   460
                        if (!hir || ((hir = coerce(hir, i32, Coerce_Arg)) == NULL))
asherkin@263
   461
                            return;
asherkin@263
   462
                        discharge(hir);
asherkin@263
   463
                    } else {
asherkin@263
   464
                        __ int_(0);
asherkin@263
   465
                    }
asherkin@263
   466
                }
asherkin@263
   467
                __ newarray(OP_NEWSIZED, map, InferTenure(node->sym()));
asherkin@263
   468
            } else {
asherkin@263
   469
                __ newarray(OP_NEWEMPTY, map, InferTenure(node->sym()));
asherkin@263
   470
            }
asherkin@263
   471
        } else {
asherkin@263
   472
            assert(false);
asherkin@263
   473
        }
asherkin@263
   474
    }
asherkin@263
   475
asherkin@263
   476
    if (node->sym()->scope()->kind() != Scope::GLOBAL) {
asherkin@263
   477
        if (!__ allocate(node->sym()))
asherkin@263
   478
            return;
asherkin@263
   479
    }
asherkin@263
   480
asherkin@263
   481
    set(node->sym());
asherkin@263
   482
    __ pop();
asherkin@263
   483
}
asherkin@263
   484
asherkin@263
   485
void
asherkin@263
   486
SemanticAnalysis::visitReturnStatement(ReturnStatement *node)
asherkin@263
   487
{
asherkin@263
   488
    control_->returns(Returns_Always);
asherkin@263
   489
asherkin@263
   490
    if (!node->expression()) {
asherkin@263
   491
        if (!fun_->returnType()->isVoid()) {
asherkin@263
   492
            cc_.reportError(node->pos(), Message_MissingReturnValue);
asherkin@263
   493
            return;
asherkin@263
   494
        }
asherkin@263
   495
asherkin@263
   496
        __ returnvoid();
asherkin@263
   497
        return;
asherkin@263
   498
    }
asherkin@263
   499
asherkin@263
   500
    if (fun_->returnType()->isVoid()) {
asherkin@263
   501
        cc_.reportError(node->pos(), Message_UsedVoidReturn);
asherkin@263
   502
        return;
asherkin@263
   503
    }
asherkin@263
   504
asherkin@263
   505
    PoolScope poolScope(zone_->pool());
asherkin@263
   506
    RootScope rootScope(zone_);
asherkin@263
   507
asherkin@263
   508
    HIR *hir = eval(node->expression());
asherkin@263
   509
    if (!hir)
asherkin@263
   510
        return;
asherkin@263
   511
asherkin@263
   512
    Local<Type> returnType(zone_, fun_->returnType());
asherkin@263
   513
    if ((hir = coerce(hir, returnType, Coerce_Return)) == NULL)
asherkin@263
   514
        return;
asherkin@263
   515
asherkin@263
   516
    discharge(hir);
asherkin@263
   517
asherkin@263
   518
    if (hir->type()->isVoid())
asherkin@263
   519
        __ returnvoid();
asherkin@263
   520
    else
asherkin@263
   521
        __ return_();
asherkin@263
   522
}
asherkin@263
   523
asherkin@263
   524
void
asherkin@263
   525
SemanticAnalysis::visitBinaryExpression(BinaryExpression *node)
asherkin@263
   526
{
asherkin@263
   527
    HIR *left = eval(node->left());
asherkin@263
   528
    HIR *right = eval(node->right());
asherkin@263
   529
    if (!left || !right)
asherkin@263
   530
        return;
asherkin@263
   531
asherkin@263
   532
    hir_ = binary(node, node->token(), left, right);
asherkin@263
   533
}
asherkin@263
   534
asherkin@263
   535
bool
asherkin@263
   536
SemanticAnalysis::checkArgumentCount(Handle<FunctionType> type, unsigned actual)
asherkin@263
   537
{
asherkin@263
   538
    if (type->parameters()->length() == actual)
asherkin@263
   539
        return true;
asherkin@263
   540
asherkin@263
   541
    if (actual > type->parameters()->length()) {
asherkin@263
   542
        if (!type->isNative() || !type->isNativeVariadic())
asherkin@263
   543
            return false;
asherkin@263
   544
        return true;
asherkin@263
   545
    }
asherkin@263
   546
asherkin@263
   547
    if (!type->defaults())
asherkin@263
   548
        return false;
asherkin@263
   549
asherkin@263
   550
    // Missing arguments. See if there's a default argument for each missing
asherkin@263
   551
    // actual.
asherkin@263
   552
    for (unsigned i = actual; i < type->parameters()->length(); i++) {
asherkin@263
   553
        if (!type->defaults()->at(i))
asherkin@263
   554
            return false;
asherkin@263
   555
    }
asherkin@263
   556
asherkin@263
   557
    return true;
asherkin@263
   558
}
asherkin@263
   559
asherkin@263
   560
static unsigned
asherkin@263
   561
BindImplicitName(Module *from, ImplicitSymbol *sym)
asherkin@263
   562
{
asherkin@263
   563
    if (sym->bound())
asherkin@263
   564
        return sym->fieldIndex();
asherkin@263
   565
asherkin@263
   566
    Module *module = Module::cast(from->getImportAt(sym->index()));
asherkin@263
   567
    StructMap *map = module->globals()->map();
asherkin@263
   568
asherkin@263
   569
    unsigned fieldIndex;
asherkin@263
   570
    Descriptor *desc = map->type()->lookupField(sym->name(), &fieldIndex);
asherkin@263
   571
    assert(desc && desc->type() == sym->type());
asherkin@263
   572
asherkin@263
   573
    sym->bind(fieldIndex);
asherkin@263
   574
    return fieldIndex;
asherkin@263
   575
}
asherkin@263
   576
asherkin@263
   577
void
asherkin@263
   578
SemanticAnalysis::visitNameProxy(NameProxy *proxy)
asherkin@263
   579
{
asherkin@263
   580
    Symbol *sym = proxy->sym();
asherkin@263
   581
asherkin@263
   582
    switch (sym->kind()) {
asherkin@263
   583
      case Symbol::kImport:
asherkin@263
   584
      {
asherkin@263
   585
        ImportSymbol *import = sym->toImport();
asherkin@263
   586
        hir_ = new (pool_) HImport(proxy, sym->type(), import->index());
asherkin@263
   587
        break;
asherkin@263
   588
      }
asherkin@263
   589
asherkin@263
   590
      case Symbol::kImplicit:
asherkin@263
   591
      {
asherkin@263
   592
        ImplicitSymbol *import = sym->toImplicit();
asherkin@263
   593
        unsigned fieldIndex = BindImplicitName(unit_->module(), import);
asherkin@263
   594
asherkin@263
   595
        hir_ = new (pool_) HImport(proxy, zone_->types()->getImportable(), import->index());
asherkin@263
   596
        hir_ = new (pool_) HField(proxy, hir_, sym->type(), fieldIndex);
asherkin@263
   597
        break;
asherkin@263
   598
      }
asherkin@263
   599
asherkin@263
   600
      case Symbol::kConstant:
asherkin@263
   601
      {
asherkin@263
   602
        ConstantSymbol *val = sym->toConstant();
asherkin@263
   603
        hir_ = new (pool_) HInteger(proxy, sym->type(), val->value().toInt());
asherkin@263
   604
        break;
asherkin@263
   605
      }
asherkin@263
   606
      
asherkin@263
   607
      // This is kind of screwy. Symbol::kFunction is like a constant we can't
asherkin@263
   608
      // evaluate yet. Maybe we should extract the GLOBAL case and say that
asherkin@263
   609
      // kFunction is only used for global functions (and use const vars for
asherkin@263
   610
      // local functions).
asherkin@263
   611
      case Symbol::kFunction:
asherkin@263
   612
      case Symbol::kVariable:
asherkin@263
   613
      {
asherkin@263
   614
        Scope *in = sym->scope();
asherkin@263
   615
        switch (in->kind()) {
asherkin@263
   616
          case Scope::GLOBAL:
asherkin@263
   617
            hir_ = new (pool_) HGlobal(proxy, sym);
asherkin@263
   618
            break;
asherkin@263
   619
asherkin@263
   620
          case Scope::FUNCTION:
asherkin@263
   621
          {
asherkin@263
   622
            VariableSymbol *var = sym->toVariable();
asherkin@263
   623
            assert(var->storage() == VariableSymbol::Arg);
asherkin@263
   624
            hir_ = new (pool_) HLocal(proxy, var);
asherkin@263
   625
            break;
asherkin@263
   626
          }
asherkin@263
   627
asherkin@263
   628
          default:
asherkin@263
   629
          {
asherkin@263
   630
            VariableSymbol *var = sym->toVariable();
asherkin@263
   631
            assert(in->kind() == Scope::BLOCK);
asherkin@263
   632
            assert(var->storage() == VariableSymbol::Local);
asherkin@263
   633
            hir_ = new (pool_) HLocal(proxy, var);
asherkin@263
   634
            break;
asherkin@263
   635
          }
asherkin@263
   636
        }
asherkin@263
   637
        break;
asherkin@263
   638
      }
asherkin@263
   639
asherkin@263
   640
      default:
asherkin@263
   641
        // :TODO:
asherkin@263
   642
        assert(false);
asherkin@263
   643
    }
asherkin@263
   644
}
asherkin@263
   645
asherkin@263
   646
void
asherkin@263
   647
SemanticAnalysis::visitBooleanLiteral(BooleanLiteral *node)
asherkin@263
   648
{
asherkin@263
   649
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
asherkin@263
   650
    hir_ = new (pool_) HBoolean(node, type, node->token() == TOK_TRUE);
asherkin@263
   651
}
asherkin@263
   652
asherkin@263
   653
void
asherkin@263
   654
SemanticAnalysis::visitIntegerLiteral(IntegerLiteral *lit)
asherkin@263
   655
{
asherkin@263
   656
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
asherkin@263
   657
    hir_ = new (pool_) HInteger(lit, type, lit->value());
asherkin@263
   658
}
asherkin@263
   659
asherkin@263
   660
void
asherkin@263
   661
SemanticAnalysis::visitFloatLiteral(FloatLiteral *node)
asherkin@263
   662
{
asherkin@263
   663
    Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
asherkin@263
   664
    hir_ = new (pool_) HFloat(node, type, (float)node->value());
asherkin@263
   665
}
asherkin@263
   666
asherkin@263
   667
void
asherkin@263
   668
SemanticAnalysis::visitStringLiteral(StringLiteral *node)
asherkin@263
   669
{
asherkin@263
   670
    Type *type = zone_->types()->getString();
asherkin@263
   671
    hir_ = new (pool_) HString(node, type, node->string());
asherkin@263
   672
}
asherkin@263
   673
asherkin@263
   674
void
asherkin@263
   675
SemanticAnalysis::visitCallExpression(CallExpression *node)
asherkin@263
   676
{
asherkin@263
   677
    HIR *callee = eval(node->callee());
asherkin@263
   678
    if (!callee)
asherkin@263
   679
        return;
asherkin@263
   680
asherkin@263
   681
    if (!callee->type()->isFunction()) {
asherkin@263
   682
        cc_.reportError(node->pos(), Message_CalleeNotFunction);
asherkin@263
   683
        return;
asherkin@263
   684
    }
asherkin@263
   685
asherkin@263
   686
    Local<FunctionType> fun(zone_, FunctionType::cast(callee->type()));
asherkin@263
   687
    
asherkin@263
   688
    if (!checkArgumentCount(fun, node->arguments()->length())) {
asherkin@263
   689
        cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
asherkin@263
   690
        return;
asherkin@263
   691
    }
asherkin@263
   692
asherkin@263
   693
    if (fun->isForward()) {
asherkin@263
   694
        cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
asherkin@263
   695
        return;
asherkin@263
   696
    }
asherkin@263
   697
asherkin@263
   698
    HIRList *args = new (pool_) HIRList;
asherkin@263
   699
    for (unsigned i = 0; i < node->arguments()->length(); i++) {
asherkin@263
   700
        Expression *expression = node->arguments()->at(i);
asherkin@263
   701
        HIR *arg = eval(expression);
asherkin@263
   702
        if (!arg)
asherkin@263
   703
            return;
asherkin@263
   704
asherkin@263
   705
        Handle<Type> given = arg->type();
asherkin@263
   706
        
asherkin@263
   707
        bool mustComputeReference;
asherkin@263
   708
        if (i >= fun->parameters()->length()) {
asherkin@263
   709
            // Special handling for natives. Arguments past the formal must
asherkin@263
   710
            // always have references computed. However, arrays are already
asherkin@263
   711
            // references for natives.
asherkin@263
   712
            assert(fun->isNative() && fun->isNativeVariadic());
asherkin@263
   713
            mustComputeReference = !given->isReference() &&
asherkin@263
   714
                                   !given->isArray();
asherkin@263
   715
        } else {
asherkin@263
   716
            Local<Type> actual(zone_, fun->parameterAt(i));
asherkin@263
   717
asherkin@263
   718
            if (expression->isIndexExpression() && arg->isIndex()) {
asherkin@263
   719
                // If the expression is the form x[y], and this produced a
asherkin@263
   720
                // normal array indexing operation, then we apply some 
asherkin@263
   721
                // special rules.
asherkin@263
   722
asherkin@263
   723
                if (actual->isReference()) {
asherkin@263
   724
                    // Disabled, since this doesn't have easily definable or
asherkin@263
   725
                    // sensible semantics with SP2 arrays, but we don't want
asherkin@263
   726
                    // to silently break SP1 semantics.
asherkin@263
   727
                    cc_.reportError(expression->pos(), Message_CannotComputeIndexRef);
asherkin@263
   728
                    return;
asherkin@263
   729
                }
asherkin@263
   730
asherkin@263
   731
                // As a special rule to account for SP1 semantics, we allow x[y]
asherkin@263
   732
                // to automatically compute a sliced, read-only array view if
asherkin@263
   733
                // the desired type is an array of one more dimension than x[y]
asherkin@263
   734
                // would return.
asherkin@263
   735
                //
asherkin@263
   736
                // As an additional restriction, this is only allowed for
asherkin@263
   737
                // one-level char arrays, to minimize potential issues until
asherkin@263
   738
                // we determine whether more compatibility is needed.
asherkin@263
   739
                //
asherkin@263
   740
                // Internally, for sanity, the dependent array operation does
asherkin@263
   741
                // not necessarily create a dependent array. For extensible
asherkin@263
   742
                // arrays, we instead create a duplicate arrary of just the
asherkin@263
   743
                // sliced portion, rather than deal with the complexity of the
asherkin@263
   744
                // source array being resized.
asherkin@263
   745
                //
asherkin@263
   746
                // Note that we don't really know whether a dependent array
asherkin@263
   747
                // will be dependent at compile-time, so the dependent type's
asherkin@263
   748
                // just inherits from the source.
asherkin@263
   749
                if (actual->isArray()) {
asherkin@263
   750
                    HIndex *index = arg->toIndex();
asherkin@263
   751
                    Local<ArrayType> basetype(zone_, ArrayType::cast(index->base()->type()));
asherkin@263
   752
                    Local<ArrayType> atype(zone_, ArrayType::cast(actual));
asherkin@263
   753
asherkin@263
   754
                    if (atype->isCharArray() && basetype->isCharArray()) {
asherkin@263
   755
                        // There is no possible way this will type check, so
asherkin@263
   756
                        // it's safe to just try something completely
asherkin@263
   757
                        // different.
asherkin@263
   758
                        Local<Type> slicetype(zone_);
asherkin@263
   759
                        if (basetype->isFixedLength()) {
asherkin@263
   760
                            // If the base type is fixed length, we need to
asherkin@263
   761
                            // compute a new dynamic array type based on the
asherkin@263
   762
                            // fixed array's contents and qualifiers.
asherkin@263
   763
                            Local<Type> contained(zone_, basetype->contained());
asherkin@263
   764
                            slicetype = zone_->types()->newArray(contained, ArrayType::DYNAMIC_ARRAY_SIZE);
asherkin@263
   765
                            if (!slicetype)
asherkin@263
   766
                                return;
asherkin@263
   767
                            slicetype = zone_->types()->qualify(slicetype, basetype->quals());
asherkin@263
   768
                            if (!slicetype)
asherkin@263
   769
                                return;
asherkin@263
   770
                        } else {
asherkin@263
   771
                            slicetype = basetype;
asherkin@263
   772
                        }
asherkin@263
   773
                        arg = new (pool_) HNewDependentArray(expression, slicetype, index->base(), index->index());
asherkin@263
   774
                        given = arg->type();
asherkin@263
   775
                    }
asherkin@263
   776
                }
asherkin@263
   777
            }
asherkin@263
   778
asherkin@263
   779
            if (actual->isReference()) {
asherkin@263
   780
                // If the actual is a reference, the coercion rules are much
asherkin@263
   781
                // stricter; the contained types must be identical.
asherkin@263
   782
                // :TODO: make this not an assert
asherkin@263
   783
                assert(ReferenceType::cast(actual)->contained() == arg->type());
asherkin@263
   784
            } else {
asherkin@263
   785
                // When we start overloading, coercion will have to happen later.
asherkin@263
   786
                arg = coerce(arg, actual, Coerce_Arg);
asherkin@263
   787
                if (!arg)
asherkin@263
   788
                    return;
asherkin@263
   789
            }
asherkin@263
   790
asherkin@263
   791
            mustComputeReference = actual->isReference() && !given->isReference();
asherkin@263
   792
        }
asherkin@263
   793
asherkin@263
   794
        if (mustComputeReference) {
asherkin@263
   795
            // Refs always use the LIFO stack if they can.
asherkin@263
   796
            arg = new (pool_) HToRef(arg->node(), arg->type(), arg);
asherkin@263
   797
            requireLifoStack();
asherkin@263
   798
        }
asherkin@263
   799
asherkin@263
   800
        if (!args->append(arg))
asherkin@263
   801
            return;
asherkin@263
   802
    }
asherkin@263
   803
asherkin@263
   804
    hir_ = new (pool_) HCall(node, fun->returnType(), callee, args);
asherkin@263
   805
}
asherkin@263
   806
asherkin@263
   807
void
asherkin@263
   808
SemanticAnalysis::visitAssignment(Assignment *node)
asherkin@263
   809
{
asherkin@263
   810
    HLValue *lval = lvalue(node->lvalue());
asherkin@263
   811
    if (!lval)
asherkin@263
   812
        return;
asherkin@263
   813
asherkin@263
   814
    if (lval->type()->quals() & TypeQual_Const) {
asherkin@263
   815
        cc_.reportError(node->pos(), Message_CannotModifyConst);
asherkin@263
   816
        return;
asherkin@263
   817
    }
asherkin@263
   818
asherkin@263
   819
    HIR *hir = eval(node->expression());
asherkin@263
   820
    if (!hir)
asherkin@263
   821
        return;
asherkin@263
   822
    if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
asherkin@263
   823
        return;
asherkin@263
   824
asherkin@263
   825
    hir_ = new (pool_) HStore(node, node->token(), lval, hir);
asherkin@263
   826
}
asherkin@263
   827
asherkin@263
   828
void
asherkin@263
   829
SemanticAnalysis::visitBreakStatement(BreakStatement *node)
asherkin@263
   830
{
asherkin@263
   831
    if (!loop_) {
asherkin@263
   832
        cc_.reportError(node->pos(), Message_BreakOutsideLoop);
asherkin@263
   833
        return;
asherkin@263
   834
    }
asherkin@263
   835
asherkin@263
   836
    loop_->setBreaks();
asherkin@263
   837
    __ j(OP_BREAK, loop_->break_());
asherkin@263
   838
}
asherkin@263
   839
asherkin@263
   840
void
asherkin@263
   841
SemanticAnalysis::visitContinueStatement(ContinueStatement *node)
asherkin@263
   842
{
asherkin@263
   843
    if (!loop_) {
asherkin@263
   844
        cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
asherkin@263
   845
        return;
asherkin@263
   846
    }
asherkin@263
   847
asherkin@263
   848
    __ j(OP_CONTINUE, loop_->continue_());
asherkin@263
   849
}
asherkin@263
   850
asherkin@263
   851
void
asherkin@263
   852
SemanticAnalysis::visitIndexExpression(IndexExpression *node)
asherkin@263
   853
{
asherkin@263
   854
    HIR *left = eval(node->left());
asherkin@263
   855
    if (!left)
asherkin@263
   856
        return;
asherkin@263
   857
asherkin@263
   858
    HIR *right = eval(node->right());
asherkin@263
   859
    if (!right)
asherkin@263
   860
        return;
asherkin@263
   861
asherkin@263
   862
    // Hrm... yes, for now, let's coerce here.
asherkin@263
   863
    Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
asherkin@263
   864
    if ((right = coerce(right, i32, Coerce_Arg)) == NULL)
asherkin@263
   865
        return;
asherkin@263
   866
    
asherkin@263
   867
    // Do not coerce here, there's no reason to yet.
asherkin@263
   868
    if (!left->type()->isArray()) {
asherkin@263
   869
        cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
asherkin@263
   870
        return;
asherkin@263
   871
    }
asherkin@263
   872
asherkin@263
   873
    hir_ = new (pool_) HIndex(node,
asherkin@263
   874
                              ArrayType::cast(left->type())->contained(),
asherkin@263
   875
                              left,
asherkin@263
   876
                              right);
asherkin@263
   877
}
asherkin@263
   878
asherkin@263
   879
void
asherkin@263
   880
SemanticAnalysis::visitIncDecExpression(IncDecExpression *node)
asherkin@263
   881
{
asherkin@263
   882
    HLValue *lval = lvalue(node->expression());
asherkin@263
   883
    if (!lval)
asherkin@263
   884
        return;
asherkin@263
   885
asherkin@263
   886
    if (!lval->type()->isInt32() && !lval->type()->isFloat()) {
asherkin@263
   887
        assert(false);
asherkin@263
   888
        return;
asherkin@263
   889
    }
asherkin@263
   890
asherkin@263
   891
    HIR *rval;
asherkin@263
   892
    if (lval->type()->isInt32())
asherkin@263
   893
        rval = new (pool_) HInteger(node, lval->type(), 1);
asherkin@263
   894
    else
asherkin@263
   895
        rval = new (pool_) HFloat(node, lval->type(), 1.0);
asherkin@263
   896
asherkin@263
   897
    if (node->postfix())
asherkin@263
   898
        hir_ = new (pool_) HPostIncDec(node, node->token(), lval, rval);
asherkin@263
   899
    else
asherkin@263
   900
        hir_ = new (pool_) HStore(node, node->token(), lval, rval);
asherkin@263
   901
}
asherkin@263
   902
asherkin@263
   903
Type *
asherkin@263
   904
SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
asherkin@263
   905
{
asherkin@263
   906
    Local<Type> l(zone_, left->type());
asherkin@263
   907
    Local<Type> r(zone_, right->type());
asherkin@263
   908
asherkin@263
   909
    if (l->isPod())
asherkin@263
   910
        l = l->unqualified();
asherkin@263
   911
    if (r->isPod())
asherkin@263
   912
        r = r->unqualified();
asherkin@263
   913
asherkin@263
   914
    if ((token >= TOK_PLUS && token <= TOK_SLASH) ||
asherkin@263
   915
        (token >= TOK_EQUALS && token <= TOK_GE))
asherkin@263
   916
    {
asherkin@263
   917
        // We allow: [float|int32] + [float|int32]
asherkin@263
   918
        if (l->isFloat() && r->isInt32())
asherkin@263
   919
            return l;
asherkin@263
   920
        if (l->isInt32() && r->isFloat())
asherkin@263
   921
            return r;
asherkin@263
   922
        if (l->isInt32() && r->isInt32())
asherkin@263
   923
            return l;
asherkin@263
   924
asherkin@263
   925
        if (l->isChar() && r->isInt32())
asherkin@263
   926
             return r;
asherkin@263
   927
        if (l->isInt32() && r->isChar())
asherkin@263
   928
            return l;
asherkin@263
   929
    }
asherkin@263
   930
asherkin@263
   931
    // :TODO: remove
asherkin@263
   932
    assert(*l == *r);
asherkin@263
   933
asherkin@263
   934
    return left->type();
asherkin@263
   935
}
asherkin@263
   936
asherkin@263
   937
HIR *
asherkin@263
   938
SemanticAnalysis::typeCheckArray(const SourcePosition &pos,
asherkin@263
   939
                                 HIR *from,
asherkin@263
   940
                                 Handle<ArrayType> argActual,
asherkin@263
   941
                                 CoercionKind kind)
asherkin@263
   942
{
asherkin@263
   943
    Local<Type> argGiven(zone_, from->type());
asherkin@263
   944
asherkin@263
   945
    // Special case: if we receive a string literal, and this is a char array,
asherkin@263
   946
    // we allow a coercion.
asherkin@263
   947
    if (from->isString() &&
asherkin@263
   948
        argGiven->isString() &&
asherkin@263
   949
        argActual->isArray() &&
asherkin@263
   950
        ArrayType::cast(argActual)->isCharArray())
asherkin@263
   951
    {
asherkin@263
   952
        HString *hir = from->toString();
asherkin@263
   953
        Local<ArrayType> actual(zone_, ArrayType::cast(argActual));
asherkin@263
   954
asherkin@263
   955
        if (actual->isFixedLength()) {
asherkin@263
   956
            if (hir->string()->length() + 1 > actual->fixedLength()) {
asherkin@263
   957
                cc_.reportError(pos, Message_ArrayAssignmentTooBig);
asherkin@263
   958
                return NULL;
asherkin@263
   959
            }
asherkin@263
   960
        }
asherkin@263
   961
asherkin@263
   962
        // Transform the string literal into an array literal. We give it the
asherkin@263
   963
        // type of the left-hand side. The constness of the destination type
asherkin@263
   964
        // will determine whether HArray duplicates it or not.
asherkin@263
   965
        Local<ArrayMap> map(zone_, actual->newMap());
asherkin@263
   966
        if (!map)
asherkin@263
   967
            return NULL;
asherkin@263
   968
asherkin@263
   969
        Local<Array> array(zone_);
asherkin@263
   970
        array = Array::NewString(zone_,
asherkin@263
   971
                                 map,
asherkin@263
   972
                                 hir->string()->chars(),
asherkin@263
   973
                                 hir->string()->length(),
asherkin@263
   974
                                 Heap::Tenure_Old);
asherkin@263
   975
        if (!array)
asherkin@263
   976
            return NULL;
asherkin@263
   977
asherkin@263
   978
        // Since we're creating a canonical copy of the char array, we have to
asherkin@263
   979
        // be careful that it doesn't get mutated. There are two cases where
asherkin@263
   980
        // we know it can't be mutated, and therefore don't need to specify
asherkin@263
   981
        // that it be cloned after pushing it onto the stack.
asherkin@263
   982
        //
asherkin@263
   983
        //  (1) The array type is const.
asherkin@263
   984
        //  (2) The array type is fixed-length, and the coercion is for
asherkin@263
   985
        //      assignment, meaning it will be immediately copied.
asherkin@263
   986
        //
asherkin@263
   987
        // We treat declaration separately from assignment, since for
asherkin@263
   988
        // declaration we can just immediately use the cloned version.
asherkin@263
   989
        bool shouldDuplicate = true;
asherkin@263
   990
        if ((actual->quals() & TypeQual_Const) ||
asherkin@263
   991
            (actual->isFixedLength() && kind == Coerce_Assign))
asherkin@263
   992
        {
asherkin@263
   993
            shouldDuplicate = false;
asherkin@263
   994
        }
asherkin@263
   995
asherkin@263
   996
        return new (pool_) HArray(hir->node(), actual, array, shouldDuplicate);
asherkin@263
   997
    }
asherkin@263
   998
asherkin@263
   999
    if (!argGiven->isArray()) {
asherkin@263
  1000
        cc_.reportError(pos, Message_ExpectedArray);
asherkin@263
  1001
        return NULL;
asherkin@263
  1002
    }
asherkin@263
  1003
asherkin@263
  1004
    Local<ArrayType> actual(zone_, ArrayType::cast(argActual));
asherkin@263
  1005
    Local<ArrayType> given(zone_, ArrayType::cast(argGiven));
asherkin@263
  1006
asherkin@263
  1007
    if (actual->levels() != given->levels()) {
asherkin@263
  1008
        cc_.reportError(pos, Message_ArrayDimensionsMismatch);
asherkin@263
  1009
        return NULL;
asherkin@263
  1010
    }
asherkin@263
  1011
asherkin@263
  1012
    unsigned level = 1;
asherkin@263
  1013
    while (true) {
asherkin@263
  1014
        if (actual->isFixedLength()) {
asherkin@263
  1015
            if (!given->isFixedLength()) {
asherkin@263
  1016
                cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
asherkin@263
  1017
                return NULL;
asherkin@263
  1018
            }
asherkin@263
  1019
asherkin@263
  1020
            if (actual->isCharArray()) {
asherkin@263
  1021
                // String assignment can have a truncated |given|.
asherkin@263
  1022
                if (given->fixedLength() > actual->fixedLength()) {
asherkin@263
  1023
                    cc_.reportError(pos, Message_ArrayAssignmentTooBig);
asherkin@263
  1024
                    return NULL;
asherkin@263
  1025
                }
asherkin@263
  1026
            } else {
asherkin@263
  1027
                if (given->fixedLength() != actual->fixedLength()) {
asherkin@263
  1028
                    cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
asherkin@263
  1029
                    return NULL;
asherkin@263
  1030
                }
asherkin@263
  1031
            }
asherkin@263
  1032
        } else if (given->isFixedLength()) {
asherkin@263
  1033
            // We do not allow assignments in the form:
asherkin@263
  1034
            //  [][] = [][Y]
asherkin@263
  1035
            //
asherkin@263
  1036
            // Otherwise, the following code could break the size guarantees
asherkin@263
  1037
            // of the right-hand side:
asherkin@263
  1038
            //
asherkin@263
  1039
            //  new x[][5];
asherkin@263
  1040
            //  new y[][];
asherkin@263
  1041
            //  new z[3];
asherkin@263
  1042
            //
asherkin@263
  1043
            //
asherkin@263
  1044
            //  y = x;
asherkin@263
  1045
            //  y[0] = z;
asherkin@263
  1046
            //
asherkin@263
  1047
            // Now |x[0]| contains a 3-length array.
asherkin@263
  1048
            if (level > 1) {
asherkin@263
  1049
                cc_.reportError(pos, Message_ArrayDimensionSizeMismatch, level);
asherkin@263
  1050
                return NULL;
asherkin@263
  1051
            }
asherkin@263
  1052
        }
asherkin@263
  1053
asherkin@263
  1054
        if (!given->contained()->isArray() || !actual->contained()->isArray())
asherkin@263
  1055
            break;
asherkin@263
  1056
asherkin@263
  1057
        given = ArrayType::cast(given->contained());
asherkin@263
  1058
        actual = ArrayType::cast(actual->contained());
asherkin@263
  1059
    }
asherkin@263
  1060
asherkin@263
  1061
    bool givenConst = !!(given->contained()->quals() & TypeQual_Const);
asherkin@263
  1062
    bool actualConst = !!(actual->contained()->quals() & TypeQual_Const);
asherkin@263
  1063
asherkin@263
  1064
    // These arrays must contain the same innermost type.
asherkin@263
  1065
    if (given->contained()->unqualified() != actual->contained()->unqualified() ||
asherkin@263
  1066
        (givenConst && !actualConst))
asherkin@263
  1067
    {
asherkin@263
  1068
        char aname[255];
asherkin@263
  1069
        char gname[255];
asherkin@263
  1070
        cc_.reportError(pos, Message_TypeMismatch,
asherkin@263
  1071
                        GetTypeName(argActual, aname, sizeof(aname)),
asherkin@263
  1072
                        GetTypeName(argGiven, gname, sizeof(gname)));
asherkin@263
  1073
        return NULL;
asherkin@263
  1074
    }
asherkin@263
  1075
asherkin@263
  1076
    return from;
asherkin@263
  1077
}
asherkin@263
  1078
asherkin@263
  1079
HIR *
asherkin@263
  1080
SemanticAnalysis::typeFailure(const SourcePosition &pos, Handle<Type> from, Handle<Type> to)
asherkin@263
  1081
{
asherkin@263
  1082
    char fname[255];
asherkin@263
  1083
    char tname[255];
asherkin@263
  1084
    cc_.reportError(pos, Message_InvalidCoercion,
asherkin@263
  1085
                    GetTypeName(from, fname, sizeof(fname)),
asherkin@263
  1086
                    GetTypeName(to, tname, sizeof(tname)));
asherkin@263
  1087
    return NULL;
asherkin@263
  1088
}
asherkin@263
  1089
asherkin@263
  1090
HIR *
asherkin@263
  1091
SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
asherkin@263
  1092
{
asherkin@263
  1093
    Local<Type> from(zone_, hir->type());
asherkin@263
  1094
asherkin@263
  1095
    // If the source is *ever* an importable, we just fail.
asherkin@263
  1096
    if (from->isImportable())
asherkin@263
  1097
        return typeFailure(hir->node()->pos(), from, to);
asherkin@263
  1098
asherkin@263
  1099
    if (from->isInt32() && to->isFloat())
asherkin@263
  1100
        return new (pool_) HConvert(hir->node(), to, OP_CVT_I2F, hir);
asherkin@263
  1101
    if (from->isInt32() && to->isBool())
asherkin@263
  1102
        return new (pool_) HConvert(hir->node(), to, OP_CVT_I2B, hir);
asherkin@263
  1103
asherkin@263
  1104
    if (to->isEnum() && from->unqualified() != to->unqualified())
asherkin@263
  1105
        return typeFailure(hir->node()->pos(), from, to);
asherkin@263
  1106
    
asherkin@263
  1107
    if (to->isArray()) {
asherkin@263
  1108
        Local<ArrayType> atype(zone_, ArrayType::cast(to));
asherkin@263
  1109
        return typeCheckArray(hir->node()->pos(), hir, atype, kind);
asherkin@263
  1110
    }
asherkin@263
  1111
    
asherkin@263
  1112
    return hir;
asherkin@263
  1113
}
asherkin@263
  1114
asherkin@263
  1115
void
asherkin@263
  1116
SemanticAnalysis::set(VariableSymbol *sym)
asherkin@263
  1117
{
asherkin@263
  1118
    if (sym->scope()->kind() == Scope::GLOBAL) {
asherkin@263
  1119
        __ setglobal(sym);
asherkin@263
  1120
    } else {
asherkin@263
  1121
        assert(sym->storage() == VariableSymbol::Local);
asherkin@263
  1122
        __ setlocal(sym);
asherkin@263
  1123
    }
asherkin@263
  1124
}
asherkin@263
  1125
asherkin@263
  1126
HIR *
asherkin@263
  1127
SemanticAnalysis::binary(AstNode *node, TokenKind token, HIR *left, HIR *right)
asherkin@263
  1128
{
asherkin@263
  1129
    Local<Type> coercion(zone_, coercionType(token, left, right));
asherkin@263
  1130
    if (!coercion)
asherkin@263
  1131
        return NULL;
asherkin@263
  1132
    if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
asherkin@263
  1133
        return NULL;
asherkin@263
  1134
    if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
asherkin@263
  1135
        return NULL;
asherkin@263
  1136
    
asherkin@263
  1137
    switch (token) {
asherkin@263
  1138
     case TOK_PLUS:
asherkin@263
  1139
      case TOK_MINUS:
asherkin@263
  1140
      case TOK_STAR:
asherkin@263
  1141
      case TOK_SLASH:
asherkin@263
  1142
      case TOK_PERCENT:
asherkin@263
  1143
      case TOK_AMPERSAND:
asherkin@263
  1144
      case TOK_BITOR:
asherkin@263
  1145
      case TOK_BITXOR:
asherkin@263
  1146
      case TOK_SHR:
asherkin@263
  1147
      case TOK_USHR:
asherkin@263
  1148
      case TOK_SHL:
asherkin@263
  1149
        return new (pool_) HBinary(node, left->type(), token, left, right);
asherkin@263
  1150
asherkin@263
  1151
      case TOK_EQUALS:
asherkin@263
  1152
      case TOK_NOTEQUALS:
asherkin@263
  1153
      case TOK_LT:
asherkin@263
  1154
      case TOK_LE:
asherkin@263
  1155
      case TOK_GT:
asherkin@263
  1156
      case TOK_GE:
asherkin@263
  1157
      case TOK_AND:
asherkin@263
  1158
      case TOK_OR:
asherkin@263
  1159
      {
asherkin@263
  1160
        Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
asherkin@263
  1161
        return new (pool_) HBinary(node, result, token, left, right);
asherkin@263
  1162
      }
asherkin@263
  1163
asherkin@263
  1164
      default:
asherkin@263
  1165
        assert(false);
asherkin@263
  1166
        return NULL;
asherkin@263
  1167
    }
asherkin@263
  1168
}
asherkin@263
  1169
asherkin@263
  1170
static inline bool
asherkin@263
  1171
IsValidUncheckedType(Type *type)
asherkin@263
  1172
{
asherkin@263
  1173
    return type->isEnum() ||
asherkin@263
  1174
           type->isFloat() ||
asherkin@263
  1175
           type->isInt32() ||
asherkin@263
  1176
           type->isChar() ||
asherkin@263
  1177
           type->isUnchecked() ||
asherkin@263
  1178
           type->isBool();
asherkin@263
  1179
}
asherkin@263
  1180
asherkin@263
  1181
void
asherkin@263
  1182
SemanticAnalysis::visitUnaryExpression(UnaryExpression *node)
asherkin@263
  1183
{
asherkin@263
  1184
    HIR *hir = eval(node->expression());
asherkin@263
  1185
    if (!hir)
asherkin@263
  1186
        return;
asherkin@263
  1187
asherkin@263
  1188
    switch (node->token()) {
asherkin@263
  1189
      case TOK_TILDE:
asherkin@263
  1190
      {
asherkin@263
  1191
        if (!hir->type()->isInt32() && !hir->type()->isEnum()) {
asherkin@263
  1192
            char name[255];
asherkin@263
  1193
            cc_.reportError(node->pos(), Message_InvalidUnaryType,
asherkin@263
  1194
                            "~",
asherkin@263
  1195
                            GetTypeName(hir->type(), name, sizeof(name)));
asherkin@263
  1196
        }
asherkin@263
  1197
        Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
asherkin@263
  1198
        hir_ = new (pool_) HUnary(node, hir->type(), TOK_TILDE, hir);
asherkin@263
  1199
        return;
asherkin@263
  1200
      }
asherkin@263
  1201
asherkin@263
  1202
      case TOK_MINUS:
asherkin@263
  1203
      {
asherkin@263
  1204
        if (hir->type()->isInt32() || hir->type()->isEnum()) {
asherkin@263
  1205
            Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
asherkin@263
  1206
            hir_ = new (pool_) HUnary(node, type, TOK_MINUS, hir);
asherkin@263
  1207
            return;
asherkin@263
  1208
        }
asherkin@263
  1209
        if (hir->type()->isFloat()) {
asherkin@263
  1210
            hir_ = new (pool_) HUnary(node, hir->type(), TOK_MINUS, hir);
asherkin@263
  1211
            return;
asherkin@263
  1212
        }
asherkin@263
  1213
asherkin@263
  1214
        char name[255];
asherkin@263
  1215
        cc_.reportError(node->pos(), Message_InvalidUnaryType,
asherkin@263
  1216
                        "-",
asherkin@263
  1217
                        GetTypeName(hir->type(), name, sizeof(name)));
asherkin@263
  1218
        break;
asherkin@263
  1219
      }
asherkin@263
  1220
asherkin@263
  1221
      default:
asherkin@263
  1222
      {
asherkin@263
  1223
        assert(node->token() == TOK_LABEL);
asherkin@263
  1224
asherkin@263
  1225
        TypeSymbol *sym = node->tag()->sym()->asType();
asherkin@263
  1226
        if (!sym) {
asherkin@263
  1227
            cc_.reportError(node->pos(), Message_IdentifierIsNotAType,
asherkin@263
  1228
                            sym->name()->chars());
asherkin@263
  1229
            return;
asherkin@263
  1230
        }
asherkin@263
  1231
asherkin@263
  1232
        if (!IsValidUncheckedType(sym->effective())) {
asherkin@263
  1233
            char name[255];
asherkin@263
  1234
            cc_.reportError(node->pos(), Message_InvalidUncheckedCastTo,
asherkin@263
  1235
                            GetTypeName(sym->type(), name, sizeof(name)));
asherkin@263
  1236
            return;
asherkin@263
  1237
        }
asherkin@263
  1238
        if (!IsValidUncheckedType(hir->type())) {
asherkin@263
  1239
            char name[255];
asherkin@263
  1240
            cc_.reportError(node->pos(), Message_InvalidUncheckedCastFrom,
asherkin@263
  1241
                            GetTypeName(hir->type(), name, sizeof(name)));
asherkin@263
  1242
            return;
asherkin@263
  1243
        }
asherkin@263
  1244
asherkin@263
  1245
        hir_ = new (pool_) HUnary(node, sym->effective(), TOK_LABEL, hir);
asherkin@263
  1246
        break;
asherkin@263
  1247
      }
asherkin@263
  1248
    }
asherkin@263
  1249
}
asherkin@263
  1250
asherkin@263
  1251
void
asherkin@263
  1252
SemanticAnalysis::import(HIR *hir, FieldExpression *node)
asherkin@263
  1253
{
asherkin@263
  1254
    // Note that we don't care if the hir type is "importable", since we
asherkin@263
  1255
    // can only do anything if the important has a constant binding.
asherkin@263
  1256
    unsigned index = hir->toImport()->index();
asherkin@263
  1257
    Local<Importable> importable(zone_, unit_->module()->getImportAt(index));
asherkin@263
  1258
asherkin@263
  1259
    if (importable->isPackage()) {
asherkin@263
  1260
        // Look at what's already loaded and see if anything exists. We
asherkin@263
  1261
        // can't create stuff on-demand here since it's too late to add
asherkin@263
  1262
        // new translation units to the CompileContext.
asherkin@263
  1263
        Local<Package> package(zone_, Package::cast(importable));
asherkin@263
  1264
        Local<Importable> found(zone_, package->import(node->field()));
asherkin@263
  1265
        if (!found) {
asherkin@263
  1266
            cc_.reportError(node->pos(), Message_PackageDoesNotHaveMember, node->field()->chars());
asherkin@263
  1267
            return;
asherkin@263
  1268
        }
asherkin@263
  1269
asherkin@263
  1270
        // If we did find something, make sure it is in our import list.
asherkin@263
  1271
        // If we didn't explicitly import it, we have no right to use it.
asherkin@263
  1272
        for (index = 0; index < unit_->module()->imports(); index++) {
asherkin@263
  1273
            if (unit_->module()->getImportAt(index) == importable)
asherkin@263
  1274
                break;
asherkin@263
  1275
        }
asherkin@263
  1276
asherkin@263
  1277
        if (index == unit_->module()->imports()) {
asherkin@263
  1278
            cc_.reportError(node->pos(), Message_ImportWasNeverImported, node->field()->chars());
asherkin@263
  1279
            return;
asherkin@263
  1280
        }
asherkin@263
  1281
asherkin@263
  1282
        // Just create a new HImport.
asherkin@263
  1283
        hir_ = new (pool_) HImport(node, hir->type(), index);
asherkin@263
  1284
    } else {
asherkin@263
  1285
        // Modules can never have sub-modules or sub-packages.
asherkin@263
  1286
        unsigned index;
asherkin@263
  1287
        Local<Module> module(zone_, Module::cast(importable));
asherkin@263
  1288
        Local<StructMap> map(zone_, module->globals()->map());
asherkin@263
  1289
        Local<Descriptor> desc(zone_, map->type()->lookupField(node->field(), &index));
asherkin@263
  1290
        if (!desc) {
asherkin@263
  1291
            cc_.reportError(node->pos(), Message_FieldNotFound, node->field()->chars());
asherkin@263
  1292
            return;
asherkin@263
  1293
        }
asherkin@263
  1294
        hir_ = new (pool_) HField(node, hir, desc->type(), index);
asherkin@263
  1295
    }
asherkin@263
  1296
}
asherkin@263
  1297
asherkin@263
  1298
void
asherkin@263
  1299
SemanticAnalysis::visitFieldExpression(FieldExpression *node)
asherkin@263
  1300
{
asherkin@263
  1301
    HIR *hir = eval(node->left());
asherkin@263
  1302
    if (!hir)
asherkin@263
  1303
        return;
asherkin@263
  1304
asherkin@263
  1305
    if (hir->isImport()) {
asherkin@263
  1306
        import(hir, node);
asherkin@263
  1307
        return;
asherkin@263
  1308
    }
asherkin@263
  1309
asherkin@263
  1310
    char name[255];
asherkin@263
  1311
    cc_.reportError(node->pos(), Message_InvalidFieldExpression,
asherkin@263
  1312
                    GetTypeName(hir->type(), name, sizeof(name)));
asherkin@263
  1313
}
asherkin@263
  1314
asherkin@263
  1315
static inline TokenKind
asherkin@263
  1316
ToCompare(Expression *expr)
asherkin@263
  1317
{
asherkin@263
  1318
    if (expr->isBinaryExpression()) {
asherkin@263
  1319
        BinaryExpression *b = expr->toBinaryExpression();
asherkin@263
  1320
        if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
asherkin@263
  1321
            return b->token();
asherkin@263
  1322
    } else if (expr->isUnaryExpression()) {
asherkin@263
  1323
        UnaryExpression *u = expr->toUnaryExpression();
asherkin@263
  1324
        if (u->token() == TOK_NOT)
asherkin@263
  1325
            return TOK_NOT;
asherkin@263
  1326
    }
asherkin@263
  1327
    return TOK_EOF;
asherkin@263
  1328
}
asherkin@263
  1329
asherkin@263
  1330
void
asherkin@263
  1331
SemanticAnalysis::visitForTest(Expression *expr,
asherkin@263
  1332
                               Label *trueBranch,
asherkin@263
  1333
                               Label *falseBranch,
asherkin@263
  1334
                               Label *fallthrough)
asherkin@263
  1335
{
asherkin@263
  1336
    Local<Type> boolType(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
asherkin@263
  1337
asherkin@263
  1338
    TokenKind token = ToCompare(expr);
asherkin@263
  1339
    if (token == TOK_EOF || token == TOK_NOT) {
asherkin@263
  1340
        HIR *hir;
asherkin@263
  1341
        if (token == TOK_EOF)
asherkin@263
  1342
            hir = eval(expr);
asherkin@263
  1343
        else
asherkin@263
  1344
            hir = eval(expr->toUnaryExpression()->expression());
asherkin@263
  1345
        if (!hir)
asherkin@263
  1346
            return;
asherkin@263
  1347
        if ((hir = coerce(hir, boolType, Coerce_Arg)) == NULL)
asherkin@263
  1348
            return;
asherkin@263
  1349
asherkin@263
  1350
        discharge(hir);
asherkin@263
  1351
asherkin@263
  1352
        bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
asherkin@263
  1353
        Opcode op = jumpOnTrue ? OP_JT : OP_JF;
asherkin@263
  1354
        
asherkin@263
  1355
        if (fallthrough == falseBranch)
asherkin@263
  1356
            __ j(op, trueBranch);
asherkin@263
  1357
        else
asherkin@263
  1358
            __ j(op, falseBranch);
asherkin@263
  1359
        return;
asherkin@263
  1360
    }
asherkin@263
  1361
asherkin@263
  1362
    BinaryExpression *bin = expr->toBinaryExpression();
asherkin@263
  1363
    if (token == TOK_AND || token == TOK_OR) {
asherkin@263
  1364
        Label next;
asherkin@263
  1365
        if (token == TOK_AND)
asherkin@263
  1366
            visitForTest(bin->left(), &next, falseBranch, &next);
asherkin@263
  1367
        else
asherkin@263
  1368
            visitForTest(bin->left(), trueBranch, &next, &next);
asherkin@263
  1369
        __ bind(&next);
asherkin@263
  1370
        visitForTest(bin->right(), trueBranch, falseBranch, fallthrough);
asherkin@263
  1371
        return;
asherkin@263
  1372
    }
asherkin@263
  1373
asherkin@263
  1374
    HIR *left = eval(bin->left());
asherkin@263
  1375
    HIR *right = eval(bin->right());
asherkin@263
  1376
    if (!left || !right)
asherkin@263
  1377
        return;
asherkin@263
  1378
asherkin@263
  1379
    HIR *hir = binary(bin, bin->token(), left, right);
asherkin@263
  1380
    if (!hir)
asherkin@263
  1381
        return;
asherkin@263
  1382
    if ((hir = coerce(hir, boolType, Coerce_Arg)) == NULL)
asherkin@263
  1383
        return;
asherkin@263
  1384
asherkin@263
  1385
    discharge(hir);
asherkin@263
  1386
    if (fallthrough == trueBranch)
asherkin@263
  1387
        __ j(OP_JF, falseBranch);
asherkin@263
  1388
    else
asherkin@263
  1389
        __ j(OP_JT, trueBranch);
asherkin@263
  1390
}
asherkin@263
  1391
asherkin@263
  1392
HLValue *
asherkin@263
  1393
SemanticAnalysis::lvalue(Expression *expr)
asherkin@263
  1394
{
asherkin@263
  1395
    HIR *hir = eval(expr);
asherkin@263
  1396
    if (!hir)
asherkin@263
  1397
        return NULL;
asherkin@263
  1398
    
asherkin@263
  1399
    if (!hir->isLValue()) {
asherkin@263
  1400
        cc_.reportError(expr->pos(), Message_ExpectedLValue);
asherkin@263
  1401
        return NULL;
asherkin@263
  1402
    }
asherkin@263
  1403
asherkin@263
  1404
    return static_cast<HLValue *>(hir);
asherkin@263
  1405
}
asherkin@263
  1406
asherkin@263
  1407
HIR *
asherkin@263
  1408
SemanticAnalysis::eval(Expression *expr)
asherkin@263
  1409
{
asherkin@263
  1410
    assert(!hir_);
asherkin@263
  1411
    expr->accept(this);
asherkin@263
  1412
asherkin@263
  1413
    HIR *hir = hir_;
asherkin@263
  1414
    hir_ = NULL;
asherkin@263
  1415
asherkin@263
  1416
    return hir;
asherkin@263
  1417
}
asherkin@263
  1418
asherkin@263
  1419
void
asherkin@263
  1420
SemanticAnalysis::discharge(HIR *hir)
asherkin@263
  1421
{
asherkin@263
  1422
    EmitHIR(zone_, emitter_, hir);
asherkin@263
  1423
}