src/Interpreter.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 263 ba85a47ee414
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of JITCraft.
[email protected]
     6
 *
[email protected]
     7
 * JITCraft is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * JITCraft. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#include <string.h>
[email protected]
    20
#include "Interpreter.h"
[email protected]
    21
#include "Modules.h"
[email protected]
    22
#include "Zone.h"
[email protected]
    23
#include "Types.h"
[email protected]
    24
#include "Array.h"
[email protected]
    25
#include "Reference.h"
[email protected]
    26
#include "Strings.h"
[email protected]
    27
#include "Environments.h"
[email protected]
    28
#include "Structures.h"
[email protected]
    29
#include "NativeTable.h"
[email protected]
    30
#include "Packages.h"
[email protected]
    31
#include "Heap-inl.h"
[email protected]
    32
[email protected]
    33
using namespace ke;
[email protected]
    34
[email protected]
    35
#define PUSH_I32(top, value)    \
[email protected]
    36
    do {                        \
[email protected]
    37
        top.sp->i32 = value;    \
[email protected]
    38
        top.sp++;               \
[email protected]
    39
   } while (0)
[email protected]
    40
[email protected]
    41
#define PUSH_F(top, value)      \
[email protected]
    42
    do {                        \
[email protected]
    43
        top.sp->f = value;      \
[email protected]
    44
        top.sp++;               \
[email protected]
    45
   } while (0)
[email protected]
    46
[email protected]
    47
static inline int
[email protected]
    48
POP_I32(Top &top)
[email protected]
    49
{
[email protected]
    50
    int value = top.sp[-1].i32;
[email protected]
    51
    top.sp--;
[email protected]
    52
    return value;
[email protected]
    53
}
[email protected]
    54
[email protected]
    55
static inline float
[email protected]
    56
POP_F(Top &top)
[email protected]
    57
{
[email protected]
    58
    float value = top.sp[-1].f;
[email protected]
    59
    top.sp--;
[email protected]
    60
    return value;
[email protected]
    61
}
[email protected]
    62
[email protected]
    63
// Pawn's original semantics specify some kind of floor operation, so
[email protected]
    64
// (8 / -3 == -3) and (-8 / 3 == -1). The SourcePawn 1 compiler (spcomp) uses
[email protected]
    65
// the upstream algorithm when constant folding. However the SourcePawn 1 JIT
[email protected]
    66
// does not. My guess is that Borja freaked out seeing the 13-instruction x86
[email protected]
    67
// sequence necessary to get this functionality (I vaguely recall a thread
[email protected]
    68
// where we discussed this with Mark Peter? But the ITB forums are gone...),
[email protected]
    69
// and we just decided to break the damn thing. So SP1 was inconsistent, but
[email protected]
    70
// we are not. And for completeness, we assert that the C compiler does the
[email protected]
    71
// right thing, since C89 leaves it unspecified.
[email protected]
    72
STATIC_ASSERT(8 / -3 == -2);
[email protected]
    73
STATIC_ASSERT(-8 / 3 == -2);
[email protected]
    74
[email protected]
    75
static inline bool
[email protected]
    76
PushNewRef(Zone *zone, Top &top, void *location, Heap::Tenure tenure)
[email protected]
    77
{
[email protected]
    78
    Reference *ref = Reference::NewStack(zone, location, tenure);
[email protected]
    79
    if (!ref)
[email protected]
    80
        return false;
[email protected]
    81
    top.sp->ref = ref;
[email protected]
    82
    top.sp++;
[email protected]
    83
    return true;
[email protected]
    84
}
[email protected]
    85
[email protected]
    86
static void
[email protected]
    87
GetElement(Array *array, int index, LocalSlot *vp)
[email protected]
    88
{
[email protected]
    89
    if (array->type()->contained()->isTraceable()) {
[email protected]
    90
        vp->obj = array->readObject(index);
[email protected]
    91
        return;
[email protected]
    92
    }
[email protected]
    93
[email protected]
    94
    switch (array->type()->contained()->pod()) {
[email protected]
    95
      case PrimitiveType_Float:
[email protected]
    96
        vp->f = array->readFloat(index);
[email protected]
    97
        break;
[email protected]
    98
[email protected]
    99
      case PrimitiveType_Char:
[email protected]
   100
        vp->i32 = array->readChar(index);
[email protected]
   101
        break;
[email protected]
   102
[email protected]
   103
      default:
[email protected]
   104
        assert(array->type()->contained()->pod() == PrimitiveType_Int32);
[email protected]
   105
        vp->i32 = array->readInt32(index);
[email protected]
   106
        break;
[email protected]
   107
    }
[email protected]
   108
}
[email protected]
   109
[email protected]
   110
static void
[email protected]
   111
SetElement(Zone *zone, Array *array, int index, const LocalSlot &v)
[email protected]
   112
{
[email protected]
   113
    if (array->type()->contained()->isTraceable()) {
[email protected]
   114
        array->writeObject(zone, index, v.obj);
[email protected]
   115
        return;
[email protected]
   116
    }
[email protected]
   117
[email protected]
   118
    switch (array->type()->contained()->pod()) {
[email protected]
   119
      case PrimitiveType_Float:
[email protected]
   120
        array->writeFloat(index, v.f);
[email protected]
   121
        break;
[email protected]
   122
[email protected]
   123
      case PrimitiveType_Char:
[email protected]
   124
        array->writeChar(index, v.i32);
[email protected]
   125
        break;
[email protected]
   126
[email protected]
   127
      default:
[email protected]
   128
        assert(array->type()->contained()->pod() == PrimitiveType_Int32);
[email protected]
   129
        array->writeInt32(index, v.i32);
[email protected]
   130
        break;
[email protected]
   131
    }
[email protected]
   132
}
[email protected]
   133
[email protected]
   134
static void
[email protected]
   135
FetchField(Struct *obj, Descriptor *desc, LocalSlot *vp)
[email protected]
   136
{
[email protected]
   137
    // :TODO: assert no GC.
[email protected]
   138
    if (desc->type()->isTraceable()) {
[email protected]
   139
        vp->obj = obj->get<Object *>(desc);
[email protected]
   140
        return;
[email protected]
   141
    }
[email protected]
   142
[email protected]
   143
    switch (desc->type()->pod()) {
[email protected]
   144
      case PrimitiveType_Float:
[email protected]
   145
        vp->f = obj->get<float>(desc);
[email protected]
   146
        break;
[email protected]
   147
[email protected]
   148
      default:
[email protected]
   149
        assert(desc->type()->pod() == PrimitiveType_Int32);
[email protected]
   150
        vp->i32 = obj->get<int>(desc);
[email protected]
   151
        break;
[email protected]
   152
    }
[email protected]
   153
}
[email protected]
   154
[email protected]
   155
static void
[email protected]
   156
GetField(Struct *obj, unsigned index, LocalSlot *vp)
[email protected]
   157
{
[email protected]
   158
    StructType *type = StructType::cast(obj->map()->type());
[email protected]
   159
    Descriptor *desc = Descriptor::cast(type->fields()->at(index));
[email protected]
   160
    FetchField(obj, desc, vp);
[email protected]
   161
}
[email protected]
   162
[email protected]
   163
static void
[email protected]
   164
GetGlobal(Zone *zone, Handle<Struct> obj, Handle<String> name, LocalSlot *vp)
[email protected]
   165
{
[email protected]
   166
    Local<StructType> type(zone, StructType::cast(obj->map()->type()));
[email protected]
   167
    Local<Descriptor> desc(zone, type->lookupField(name, NULL));
[email protected]
   168
    FetchField(obj, desc, vp);
[email protected]
   169
}
[email protected]
   170
[email protected]
   171
static void
[email protected]
   172
StoreField(Zone *zone, Struct *obj, Descriptor *desc, const LocalSlot &v)
[email protected]
   173
{
[email protected]
   174
    // :TODO: assert no GC.
[email protected]
   175
    if (desc->type()->isTraceable()) {
[email protected]
   176
        obj->set(zone, desc, v.obj);
[email protected]
   177
        return;
[email protected]
   178
    }
[email protected]
   179
[email protected]
   180
    switch (desc->type()->pod()) {
[email protected]
   181
      case PrimitiveType_Float:
[email protected]
   182
        obj->set<float>(desc, v.f);
[email protected]
   183
        break;
[email protected]
   184
[email protected]
   185
      default:
[email protected]
   186
        assert(desc->type()->pod() == PrimitiveType_Int32);
[email protected]
   187
        obj->set<int>(desc, v.i32);
[email protected]
   188
        break;
[email protected]
   189
    }
[email protected]
   190
}
[email protected]
   191
[email protected]
   192
static void
[email protected]
   193
SetField(Zone *zone, Struct *obj, unsigned index, const LocalSlot &v)
[email protected]
   194
{
[email protected]
   195
    StructType *type = StructType::cast(obj->map()->type());
[email protected]
   196
    Descriptor *desc = Descriptor::cast(type->fields()->at(index));
[email protected]
   197
    StoreField(zone, obj, desc, v);
[email protected]
   198
}
[email protected]
   199
[email protected]
   200
static void
[email protected]
   201
SetGlobal(Zone *zone, Handle<Struct> obj, Handle<String> name, LocalSlot &v)
[email protected]
   202
{
[email protected]
   203
    Local<StructType> type(zone, StructType::cast(obj->map()->type()));
[email protected]
   204
    Local<Descriptor> desc(zone, type->lookupField(name, NULL));
[email protected]
   205
    StoreField(zone, obj, desc, v);
[email protected]
   206
}
[email protected]
   207
[email protected]
   208
static bool
[email protected]
   209
InvokeNative(Zone *zone, Handle<Native> native, void *identity, LocalSlot *args, unsigned argc,
[email protected]
   210
             int *result)
[email protected]
   211
{
[email protected]
   212
    NativeArgs guard;
[email protected]
   213
[email protected]
   214
    void *address = zone->nativeTable()->address(native->index());
[email protected]
   215
    if (!address) {
[email protected]
   216
        Local<String> name(zone, zone->nativeTable()->name(native->index()));
[email protected]
   217
        zone->reportError(Message_NativeNotBound, name->chars());
[email protected]
   218
        return false;
[email protected]
   219
    }
[email protected]
   220
[email protected]
   221
    if (!zone->stack().pushNativeFrame(native, guard, argc + 1))
[email protected]
   222
        return false;
[email protected]
   223
[email protected]
   224
    intptr_t *cells = guard.args();
[email protected]
   225
[email protected]
   226
    // params[0] == argc, in ISPE
[email protected]
   227
    cells[0] = argc;
[email protected]
   228
[email protected]
   229
    // Pass the formal arguments.
[email protected]
   230
    Local<FunctionType> type(zone, native->type());
[email protected]
   231
    Local<Type> param(zone);
[email protected]
   232
    for (unsigned i = 0; i < type->parameters()->length(); i++) {
[email protected]
   233
        param = type->parameterAt(i);
[email protected]
   234
        
[email protected]
   235
        // For floats, we rely on the aliasing of |i32| and |f|.
[email protected]
   236
        LocalSlot &arg = args[-int(i) - 1];
[email protected]
   237
        if (param->isInt32() || param->isFloat()) {
[email protected]
   238
            cells[i + 1] = arg.i32;
[email protected]
   239
        } else if (param->isReference()) {
[email protected]
   240
            cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
[email protected]
   241
        } else {
[email protected]
   242
            assert(param->isArray());
[email protected]
   243
            cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
[email protected]
   244
        }
[email protected]
   245
    }
[email protected]
   246
[email protected]
   247
    // For variadic arguments, the compiler left either arrays or references.
[email protected]
   248
    for (unsigned i = type->parameters()->length(); i < argc; i++) {
[email protected]
   249
        LocalSlot &arg = args[-int(i) - 1];
[email protected]
   250
[email protected]
   251
        if (arg.obj->is(MapKind_Reference)) {
[email protected]
   252
            cells[i + 1] = reinterpret_cast<intptr_t>(arg.ref->location());
[email protected]
   253
        } else {
[email protected]
   254
            assert(arg.obj->is(MapKind_Array));
[email protected]
   255
            cells[i + 1] = reinterpret_cast<intptr_t>(arg.array->bytes());
[email protected]
   256
        }
[email protected]
   257
    }
[email protected]
   258
[email protected]
   259
    typedef intptr_t (*INVOKE)(void *, intptr_t *);
[email protected]
   260
    INVOKE invoke = (INVOKE)address;
[email protected]
   261
    *result = invoke(identity, cells);
[email protected]
   262
[email protected]
   263
    return !zone->hasPendingException();
[email protected]
   264
}
[email protected]
   265
[email protected]
   266
bool
[email protected]
   267
ke::Interpret(Zone *zone, Frame *entryFrame, LocalSlot *result)
[email protected]
   268
{
[email protected]
   269
    Top &top = *zone->stack().top();
[email protected]
   270
    if (!entryFrame)
[email protected]
   271
        entryFrame = top.fp;
[email protected]
   272
[email protected]
   273
    LocalSlot *locals = entryFrame->locals();
[email protected]
   274
[email protected]
   275
    for (;;) {
[email protected]
   276
        Opcode op = Opcode(*top.pc);
[email protected]
   277
[email protected]
   278
        if (OpcodeInfo[op].nuses > 0)
[email protected]
   279
            assert(top.sp - OpcodeInfo[op].nuses >= top.fp->stackBase());
[email protected]
   280
[email protected]
   281
        switch (op) {
[email protected]
   282
[email protected]
   283
case OP_NOP:
[email protected]
   284
    break;
[email protected]
   285
[email protected]
   286
case OP_INT:
[email protected]
   287
    PUSH_I32(top, READ_INT32(top.pc));
[email protected]
   288
    break;
[email protected]
   289
[email protected]
   290
case OP_FLOAT:
[email protected]
   291
    PUSH_F(top, READ_FLOAT(top.pc));
[email protected]
   292
    break;
[email protected]
   293
[email protected]
   294
case OP_RETURN:
[email protected]
   295
case OP_RETURNVOID:
[email protected]
   296
case OP_STOP:
[email protected]
   297
{
[email protected]
   298
    if (op == OP_RETURN)
[email protected]
   299
        *result = *--top.sp;
[email protected]
   300
[email protected]
   301
    if (top.fp == entryFrame)
[email protected]
   302
        goto done;
[email protected]
   303
[email protected]
   304
    // This must not GC.
[email protected]
   305
    zone->stack().popInlineFrame();
[email protected]
   306
    locals = top.fp->locals();
[email protected]
   307
[email protected]
   308
    if (op == OP_RETURN)
[email protected]
   309
        *top.sp++ = *result;
[email protected]
   310
[email protected]
   311
    top.pc += OpcodeInfo[*top.pc].length;
[email protected]
   312
    continue;
[email protected]
   313
}
[email protected]
   314
[email protected]
   315
case OP_POP:
[email protected]
   316
{
[email protected]
   317
    top.sp--;
[email protected]
   318
    break;
[email protected]
   319
}
[email protected]
   320
[email protected]
   321
case OP_JT:
[email protected]
   322
{
[email protected]
   323
    int value = POP_I32(top);
[email protected]
   324
    if (value) {
[email protected]
   325
        top.pc += READ_INT32(top.pc);
[email protected]
   326
        continue;
[email protected]
   327
    }
[email protected]
   328
    break;
[email protected]
   329
}
[email protected]
   330
[email protected]
   331
case OP_JF:
[email protected]
   332
{
[email protected]
   333
    int value = POP_I32(top);
[email protected]
   334
    if (!value) {
[email protected]
   335
        top.pc += READ_INT32(top.pc);
[email protected]
   336
        continue;
[email protected]
   337
    }
[email protected]
   338
    break;
[email protected]
   339
}
[email protected]
   340
[email protected]
   341
case OP_JUMP:
[email protected]
   342
{
[email protected]
   343
    top.pc += READ_INT32(top.pc);
[email protected]
   344
    continue;
[email protected]
   345
}
[email protected]
   346
[email protected]
   347
case OP_BREAK:
[email protected]
   348
case OP_CONTINUE:
[email protected]
   349
{
[email protected]
   350
    top.fp->leaveLoop(zone);
[email protected]
   351
    top.pc += READ_INT32(top.pc);
[email protected]
   352
    continue;
[email protected]
   353
}
[email protected]
   354
[email protected]
   355
case OP_GETLOCAL:
[email protected]
   356
{
[email protected]
   357
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   358
    assert(slot < top.fp->code()->nlocals());
[email protected]
   359
    *top.sp++ = locals[slot];
[email protected]
   360
    break;
[email protected]
   361
}
[email protected]
   362
[email protected]
   363
case OP_SETLOCAL:
[email protected]
   364
{
[email protected]
   365
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   366
    assert(slot < top.fp->code()->nlocals());
[email protected]
   367
    locals[slot] = top.sp[-1];
[email protected]
   368
    break;
[email protected]
   369
}
[email protected]
   370
[email protected]
   371
case OP_ADD:
[email protected]
   372
{
[email protected]
   373
    top.sp[-2].i32 = top.sp[-2].i32 + top.sp[-1].i32;
[email protected]
   374
    top.sp--;
[email protected]
   375
    break;
[email protected]
   376
}
[email protected]
   377
[email protected]
   378
case OP_SUB:
[email protected]
   379
{
[email protected]
   380
    top.sp[-2].i32 = top.sp[-2].i32 - top.sp[-1].i32;
[email protected]
   381
    top.sp--;
[email protected]
   382
    break;
[email protected]
   383
}
[email protected]
   384
[email protected]
   385
case OP_MUL:
[email protected]
   386
{
[email protected]
   387
    top.sp[-2].i32 = top.sp[-2].i32 * top.sp[-1].i32;
[email protected]
   388
    top.sp--;
[email protected]
   389
    break;
[email protected]
   390
}
[email protected]
   391
[email protected]
   392
case OP_DIV:
[email protected]
   393
{
[email protected]
   394
    if (top.sp[-1].i32 == 0) {
[email protected]
   395
        zone->reportError(Message_IntegerDivideByZero);
[email protected]
   396
        goto error;
[email protected]
   397
    }
[email protected]
   398
    top.sp[-2].i32 = top.sp[-2].i32 / top.sp[-1].i32;
[email protected]
   399
    top.sp--;
[email protected]
   400
    break;
[email protected]
   401
}
[email protected]
   402
[email protected]
   403
case OP_MOD:
[email protected]
   404
{
[email protected]
   405
    if (top.sp[-1].i32 == 0) {
[email protected]
   406
        zone->reportError(Message_IntegerDivideByZero);
[email protected]
   407
        goto error;
[email protected]
   408
    }
[email protected]
   409
    top.sp[-2].i32 = top.sp[-2].i32 % top.sp[-1].i32;
[email protected]
   410
    top.sp--;
[email protected]
   411
    break;
[email protected]
   412
}
[email protected]
   413
[email protected]
   414
case OP_LT:
[email protected]
   415
{
[email protected]
   416
    top.sp[-2].i32 = top.sp[-2].i32 < top.sp[-1].i32;
[email protected]
   417
    top.sp--;
[email protected]
   418
    break;
[email protected]
   419
}
[email protected]
   420
[email protected]
   421
case OP_LE:
[email protected]
   422
{
[email protected]
   423
    top.sp[-2].i32 = top.sp[-2].i32 <= top.sp[-1].i32;
[email protected]
   424
    top.sp--;
[email protected]
   425
    break;
[email protected]
   426
}
[email protected]
   427
[email protected]
   428
case OP_GT:
[email protected]
   429
{
[email protected]
   430
    top.sp[-2].i32 = top.sp[-2].i32 > top.sp[-1].i32;
[email protected]
   431
    top.sp--;
[email protected]
   432
    break;
[email protected]
   433
}
[email protected]
   434
[email protected]
   435
case OP_GE:
[email protected]
   436
{
[email protected]
   437
    top.sp[-2].i32 = top.sp[-2].i32 >= top.sp[-1].i32;
[email protected]
   438
    top.sp--;
[email protected]
   439
    break;
[email protected]
   440
}
[email protected]
   441
[email protected]
   442
case OP_EQ:
[email protected]
   443
{
[email protected]
   444
    top.sp[-2].i32 = (top.sp[-2].i32 == top.sp[-1].i32);
[email protected]
   445
    top.sp--;
[email protected]
   446
    break;
[email protected]
   447
}
[email protected]
   448
[email protected]
   449
case OP_NE:
[email protected]
   450
{
[email protected]
   451
    top.sp[-2].i32 = (top.sp[-2].i32 != top.sp[-1].i32);
[email protected]
   452
    top.sp--;
[email protected]
   453
    break;
[email protected]
   454
}
[email protected]
   455
[email protected]
   456
case OP_ADD_F:
[email protected]
   457
{
[email protected]
   458
    top.sp[-2].f = top.sp[-2].f + top.sp[-1].f;
[email protected]
   459
    top.sp--;
[email protected]
   460
    break;
[email protected]
   461
}
[email protected]
   462
[email protected]
   463
case OP_SUB_F:
[email protected]
   464
{
[email protected]
   465
    top.sp[-2].f = top.sp[-2].f - top.sp[-1].f;
[email protected]
   466
    top.sp--;
[email protected]
   467
    break;
[email protected]
   468
}
[email protected]
   469
[email protected]
   470
case OP_MUL_F:
[email protected]
   471
{
[email protected]
   472
    top.sp[-2].f = top.sp[-2].f * top.sp[-1].f;
[email protected]
   473
    top.sp--;
[email protected]
   474
    break;
[email protected]
   475
}
[email protected]
   476
[email protected]
   477
case OP_DIV_F:
[email protected]
   478
{
[email protected]
   479
    top.sp[-2].f = top.sp[-2].f / top.sp[-1].f;
[email protected]
   480
    top.sp--;
[email protected]
   481
    break;
[email protected]
   482
}
[email protected]
   483
[email protected]
   484
case OP_LT_F:
[email protected]
   485
{
[email protected]
   486
    top.sp[-2].f = top.sp[-2].f < top.sp[-1].f;
[email protected]
   487
    top.sp--;
[email protected]
   488
    break;
[email protected]
   489
}
[email protected]
   490
[email protected]
   491
case OP_LE_F:
[email protected]
   492
{
[email protected]
   493
    top.sp[-2].f = top.sp[-2].f <= top.sp[-1].f;
[email protected]
   494
    top.sp--;
[email protected]
   495
    break;
[email protected]
   496
}
[email protected]
   497
[email protected]
   498
case OP_GT_F:
[email protected]
   499
{
[email protected]
   500
    top.sp[-2].f = top.sp[-2].f > top.sp[-1].f;
[email protected]
   501
    top.sp--;
[email protected]
   502
    break;
[email protected]
   503
}
[email protected]
   504
[email protected]
   505
case OP_GE_F:
[email protected]
   506
{
[email protected]
   507
    top.sp[-2].f = top.sp[-2].f >= top.sp[-1].f;
[email protected]
   508
    top.sp--;
[email protected]
   509
    break;
[email protected]
   510
}
[email protected]
   511
[email protected]
   512
case OP_EQ_F:
[email protected]
   513
{
[email protected]
   514
    top.sp[-2].i32 = (top.sp[-2].f == top.sp[-1].f);
[email protected]
   515
    top.sp--;
[email protected]
   516
    break;
[email protected]
   517
}
[email protected]
   518
[email protected]
   519
case OP_NE_F:
[email protected]
   520
{
[email protected]
   521
    top.sp[-2].i32 = (top.sp[-2].f != top.sp[-1].f);
[email protected]
   522
    top.sp--;
[email protected]
   523
    break;
[email protected]
   524
}
[email protected]
   525
[email protected]
   526
case OP_SHL:
[email protected]
   527
{
[email protected]
   528
    top.sp[-2].i32 = top.sp[-2].i32 << top.sp[-1].i32;
[email protected]
   529
    top.sp--;
[email protected]
   530
    break;
[email protected]
   531
}
[email protected]
   532
[email protected]
   533
case OP_SHR:
[email protected]
   534
{
[email protected]
   535
    top.sp[-2].i32 = top.sp[-2].i32 >> top.sp[-1].i32;
[email protected]
   536
    top.sp--;
[email protected]
   537
    break;
[email protected]
   538
}
[email protected]
   539
[email protected]
   540
case OP_USHR:
[email protected]
   541
{
[email protected]
   542
    top.sp[-2].i32 = unsigned(top.sp[-2].i32) >> unsigned(top.sp[-1].i32);
[email protected]
   543
    top.sp--;
[email protected]
   544
    break;
[email protected]
   545
}
[email protected]
   546
[email protected]
   547
case OP_BITAND:
[email protected]
   548
{
[email protected]
   549
    top.sp[-2].i32 = top.sp[-2].i32 & top.sp[-1].i32;
[email protected]
   550
    top.sp--;
[email protected]
   551
    break;
[email protected]
   552
}
[email protected]
   553
[email protected]
   554
case OP_BITOR:
[email protected]
   555
{
[email protected]
   556
    top.sp[-2].i32 = top.sp[-2].i32 | top.sp[-1].i32;
[email protected]
   557
    top.sp--;
[email protected]
   558
    break;
[email protected]
   559
}
[email protected]
   560
[email protected]
   561
case OP_BITXOR:
[email protected]
   562
{
[email protected]
   563
    top.sp[-2].i32 = top.sp[-2].i32 ^ top.sp[-1].i32;
[email protected]
   564
    top.sp--;
[email protected]
   565
    break;
[email protected]
   566
}
[email protected]
   567
[email protected]
   568
case OP_BITNOT:
[email protected]
   569
{
[email protected]
   570
    top.sp[-1].i32 = ~top.sp[-1].i32;
[email protected]
   571
    break;
[email protected]
   572
}
[email protected]
   573
[email protected]
   574
case OP_NOT:
[email protected]
   575
{
[email protected]
   576
    top.sp[-1].i32 = top.sp[-1].i32 ? 0 : 1;
[email protected]
   577
    break;
dvander[email protected]
   578
}
[email protected]
   579
[email protected]
   580
case OP_NOT_F:
[email protected]
   581
{
[email protected]
   582
    top.sp[-1].i32 = top.sp[-1].f ? 0 : 1;
[email protected]
   583
    break;
[email protected]
   584
}
[email protected]
   585
[email protected]
   586
case OP_NEG:
[email protected]
   587
{
[email protected]
   588
    top.sp[-1].i32 = -top.sp[-1].i32;
[email protected]
   589
    break;
[email protected]
   590
}
[email protected]
   591
[email protected]
   592
case OP_NEG_F:
[email protected]
   593
{
[email protected]
   594
    top.sp[-1].f = -top.sp[-1].f;
[email protected]
   595
    break;
[email protected]
   596
}
[email protected]
   597
[email protected]
   598
case OP_CALL:
[email protected]
   599
{
[email protected]
   600
    Local<Function> newFun(zone, Function::cast(top.sp[-1].obj));
[email protected]
   601
    top.sp--;
[email protected]
   602
[email protected]
   603
    if (!zone->stack().pushInlineFrame(newFun))
[email protected]
   604
        goto error;
[email protected]
   605
[email protected]
   606
    locals = top.fp->locals();
[email protected]
   607
    continue;
[email protected]
   608
}
[email protected]
   609
[email protected]
   610
case OP_GETARG:
[email protected]
   611
{
[email protected]
   612
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   613
    *top.sp++ = top.fp->arg(slot);
[email protected]
   614
    break;
[email protected]
   615
}
[email protected]
   616
[email protected]
   617
case OP_SETARG:
[email protected]
   618
{
[email protected]
   619
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   620
    top.fp->arg(slot) = top.sp[-1];
[email protected]
   621
    break;
[email protected]
   622
}
[email protected]
   623
[email protected]
   624
case OP_GETELEM:
[email protected]
   625
{
[email protected]
   626
    Array *array = Array::cast(top.sp[-2].obj);
[email protected]
   627
    int index = top.sp[-1].i32;
[email protected]
   628
[email protected]
   629
    if (index < 0 || unsigned(index) >= array->length()) {
[email protected]
   630
        zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
[email protected]
   631
        goto error;
[email protected]
   632
    }
[email protected]
   633
[email protected]
   634
    GetElement(array, index, &top.sp[-2]);
[email protected]
   635
    top.sp--;
[email protected]
   636
    break;
[email protected]
   637
}
[email protected]
   638
[email protected]
   639
case OP_SETELEM:
[email protected]
   640
{
[email protected]
   641
    Array *array = Array::cast(top.sp[-3].obj);
[email protected]
   642
    int index = top.sp[-2].i32;
[email protected]
   643
[email protected]
   644
    if (index < 0 || unsigned(index) >= array->length()) {
[email protected]
   645
        zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
[email protected]
   646
        goto error;
[email protected]
   647
    }
[email protected]
   648
[email protected]
   649
    SetElement(zone, array, index, top.sp[-1]);
[email protected]
   650
    top.sp[-3] = top.sp[-1];
[email protected]
   651
    top.sp -= 2;
[email protected]
   652
    break;
[email protected]
   653
}
[email protected]
   654
[email protected]
   655
case OP_CVT_I2F:
[email protected]
   656
{
[email protected]
   657
    int ival = POP_I32(top);
[email protected]
   658
    float fval = (float)ival;
[email protected]
   659
    PUSH_F(top, fval);
[email protected]
   660
    break;
[email protected]
   661
}
[email protected]
   662
[email protected]
   663
case OP_CVT_I2B:
[email protected]
   664
{
[email protected]
   665
    top.sp[-1].i32 = !!top.sp[-1].i32;
[email protected]
   666
    break;
[email protected]
   667
}
[email protected]
   668
[email protected]
   669
case OP_DUPARRAY:
[email protected]
   670
{
[email protected]
   671
    Local<Array> src(zone, Array::cast(top.sp[-1].obj));
[email protected]
   672
    top.sp[-1].obj = Array::DeepCopy(zone, src, Heap::Tenure_Default);
[email protected]
   673
    if (!top.sp[-1].obj)
[email protected]
   674
        goto error;
[email protected]
   675
    break;
[email protected]
   676
}
[email protected]
   677
[email protected]
   678
case OP_NEWFIXED:
[email protected]
   679
{
[email protected]
   680
    Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
[email protected]
   681
    Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
[email protected]
   682
    Array *array = Array::NewFixed(zone, map, tenure);
[email protected]
   683
    if (!array)
[email protected]
   684
        goto error;
[email protected]
   685
    top.sp->obj = array;
[email protected]
   686
    top.sp++;
[email protected]
   687
    break;
[email protected]
   688
}
[email protected]
   689
[email protected]
   690
case OP_NEWEMPTY:
[email protected]
   691
{
[email protected]
   692
    Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
[email protected]
   693
    Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
[email protected]
   694
    Array *array = Array::NewEmpty(zone, map, tenure);
[email protected]
   695
    if (!array)
[email protected]
   696
        goto error;
[email protected]
   697
    top.sp->obj = array;
[email protected]
   698
    top.sp++;
[email protected]
   699
    break;
[email protected]
   700
}
[email protected]
   701
[email protected]
   702
case OP_NEWSIZED:
d[email protected]
   703
{
[email protected]
   704
    Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
[email protected]
   705
    Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
[email protected]
   706
    assert(map->type()->levels() < MAX_ARRAY_DEPTH);
[email protected]
   707
[email protected]
   708
    int dims[MAX_ARRAY_DEPTH];
[email protected]
   709
    for (unsigned i = map->type()->levels() - 1; i < map->type()->levels(); i--)
[email protected]
   710
        dims[i] = POP_I32(top);
[email protected]
   711
[email protected]
   712
    Array *array = Array::NewSized(zone, map, dims, tenure);
[email protected]
   713
    if (!array)
[email protected]
   714
        goto error;
[email protected]
   715
    top.sp->obj = array;
[email protected]
   716
    top.sp++;
[email protected]
   717
    break;
[email protected]
   718
}
[email protected]
   719
[email protected]
   720
case OP_NEWSTRUCT:
[email protected]
   721
{
[email protected]
   722
    Heap::Tenure tenure = (Heap::Tenure)top.pc[1];
[email protected]
   723
    Local<StructMap> map(zone, StructMap::cast(top.fp->code()->map(READ_UINT32(top.pc + 1))));
[email protected]
   724
    top.sp->obj = Struct::New(zone, map, tenure);
[email protected]
   725
    if (!top.sp->obj)
[email protected]
   726
        goto error;
[email protected]
   727
    top.sp++;
[email protected]
   728
    break;
[email protected]
   729
}
[email protected]
   730
[email protected]
   731
case OP_SAVELIFO:
[email protected]
   732
{
[email protected]
   733
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   734
    locals[slot].address = zone->heap().lifoPosition();
[email protected]
   735
    break;
[email protected]
   736
}
[email protected]
   737
[email protected]
   738
case OP_UNWINDLIFO:
[email protected]
   739
{
[email protected]
   740
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   741
    zone->heap().unwindLifo(locals[slot].address);
[email protected]
   742
    break;
[email protected]
   743
}
[email protected]
   744
[email protected]
   745
case OP_VARREF_LIFO:
[email protected]
   746
{
[email protected]
   747
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   748
    if (!PushNewRef(zone, top, &locals[slot], Heap::Tenure_Stack))
[email protected]
   749
        goto error;
[email protected]
   750
    break;
[email protected]
   751
}
[email protected]
   752
[email protected]
   753
case OP_ARGREF_LIFO:
[email protected]
   754
{
[email protected]
   755
    unsigned slot = READ_UINT32(top.pc);
[email protected]
   756
    if (!PushNewRef(zone, top, &top.fp->arg(slot), Heap::Tenure_Stack))
[email protected]
   757
        goto error;
[email protected]
   758
    break;
[email protected]
   759
}
[email protected]
   760
[email protected]
   761
case OP_SLOTREF_LIFO:
[email protected]
   762
{
[email protected]
   763
    Reference *ref = Reference::NewStack(zone, top.sp[-1], Heap::Tenure_Stack);
[email protected]
   764
    if (!ref)
[email protected]
   765
        goto error;
[email protected]
   766
    top.sp[-1].ref = ref;
[email protected]
   767
    break;
[email protected]
   768
}
[email protected]
   769
[email protected]
   770
case OP_GETREF_I:
[email protected]
   771
{
[email protected]
   772
    top.sp[-1].i32 = top.sp[-1].ref->getInt();
[email protected]
   773
    break;
[email protected]
   774
}
[email protected]
   775
[email protected]
   776
case OP_SETREF_I:
[email protected]
   777
{
[email protected]
   778
    top.sp[-2].ref->setInt(top.sp[-1].i32);
[email protected]
   779
    top.sp[-2].i32 = top.sp[-1].i32;
[email protected]
   780
    top.sp--;
[email protected]
   781
    break;
[email protected]
   782
}
[email protected]
   783
[email protected]
   784
case OP_GETREF_F:
[email protected]
   785
{
[email protected]
   786
    top.sp[-1].f = top.sp[-1].ref->getFloat();
[email protected]
   787
    break;
[email protected]
   788
}
[email protected]
   789
[email protected]
   790
case OP_SETREF_F:
[email protected]
   791
{
[email protected]
   792
    top.sp[-2].ref->setFloat(top.sp[-1].f);
[email protected]
   793
    top.sp[-2].f = top.sp[-1].f;
[email protected]
   794
    break;
[email protected]
   795
}
[email protected]
   796
[email protected]
   797
case OP_OBJECT:
[email protected]
   798
{
[email protected]
   799
    top.sp->obj = top.fp->code()->object(READ_UINT32(top.pc));
[email protected]
   800
    top.sp++;
[email protected]
   801
    break;
[email protected]
   802
}
[email protected]
   803
[email protected]
   804
case OP_COPYARRAY:
[email protected]
   805
{
[email protected]
   806
    // Note for this assignment opcode, we return the left-hand side, not the
[email protected]
   807
    // right, since in this case the result of the expression could be different.
[email protected]
   808
    Local<Array> left(zone, Array::cast(top.sp[-2].array));
[email protected]
   809
    Local<Array> right(zone, Array::cast(top.sp[-1].array));
[email protected]
   810
    if (!Array::ShallowCopy(zone, left, right))
[email protected]
   811
        goto error;
[email protected]
   812
    top.sp--;
[email protected]
   813
    break;
[email protected]
   814
}
[email protected]
   815
[email protected]
   816
case OP_DUP:
[email protected]
   817
{
[email protected]
   818
    top.sp++;
[email protected]
   819
    top.sp[-1] = top.sp[-2];
[email protected]
   820
    break;
[email protected]
   821
}
[email protected]
   822
[email protected]
   823
case OP_DUP2:
[email protected]
   824
{
[email protected]
   825
    top.sp += 2;
[email protected]
   826
    top.sp[-2] = top.sp[-4];
[email protected]
   827
    top.sp[-1] = top.sp[-3];
[email protected]
   828
    break;
[email protected]
   829
}
[email protected]
   830
[email protected]
   831
case OP_SWAP:
[email protected]
   832
{
[email protected]
   833
    LocalSlot slot = top.sp[-2];
[email protected]
   834
    top.sp[-2] = top.sp[-1];
[email protected]
   835
    top.sp[-1] = slot;
[email protected]
   836
    break;
[email protected]
   837
}
[email protected]
   838
[email protected]
   839
case OP_ROLL3:
[email protected]
   840
{
[email protected]
   841
    LocalSlot slot = top.sp[-3];
[email protected]
   842
    top.sp[-3] = top.sp[-2];
[email protected]
   843
    top.sp[-2] = top.sp[-1];
[email protected]
   844
    top.sp[-1] = slot;
[email protected]
   845
    break;
[email protected]
   846
}
[email protected]
   847
[email protected]
   848
case OP_PICK2:
[email protected]
   849
{
[email protected]
   850
    top.sp++;
[email protected]
   851
    top.sp[-1] = top.sp[-3];
[email protected]
   852
    break;
[email protected]
   853
}
[email protected]
   854
[email protected]
   855
case OP_PICK3:
[email protected]
   856
{
[email protected]
   857
    top.sp++;
[email protected]
   858
    top.sp[-1] = top.sp[-4];
[email protected]
   859
    break;
[email protected]
   860
}
[email protected]
   861
[email protected]
   862
case OP_CALLNATIVE:
[email protected]
   863
{
[email protected]
   864
    unsigned argc = READ_UINT32(top.pc);
[email protected]
   865
[email protected]
   866
    Local<Native> native(zone, Native::cast(top.sp[-1].obj));
[email protected]
   867
    top.sp--;
[email protected]
   868
[email protected]
   869
    int result;
[email protected]
   870
    void *nativeIdentity = top.fp->code()->module()->parent()->getNativeIdentity();
[email protected]
   871
    if (!InvokeNative(zone, native, nativeIdentity, top.sp, argc, &result))
[email protected]
   872
        goto error;
[email protected]
   873
[email protected]
   874
    top.sp -= argc;
[email protected]
   875
    PUSH_I32(top, result);
[email protected]
   876
    break;
[email protected]
   877
}
[email protected]
   878
[email protected]
   879
case OP_BITCAST:
[email protected]
   880
{
[email protected]
   881
    // Because the stack is a union, this is a no-op.
[email protected]
   882
    break;
[email protected]
   883
}
[email protected]
   884
[email protected]
   885
case OP_TRUE:
[email protected]
   886
{
[email protected]
   887
    top.sp++;
[email protected]
   888
    top.sp[-1].i32 = 1;
[email protected]
   889
    break;
[email protected]
   890
}
[email protected]
   891
[email protected]
   892
case OP_FALSE:
[email protected]
   893
{
[email protected]
   894
    top.sp++;
[email protected]
   895
    top.sp[-1].i32 = 0;
[email protected]
   896
    break;
[email protected]
   897
}
[email protected]
   898
[email protected]
   899
case OP_GETFIELD:
[email protected]
   900
{
[email protected]
   901
    Struct *obj;
[email protected]
   902
    if (top.sp[-1].obj->is(MapKind_Module))
[email protected]
   903
        obj = Module::cast(top.sp[-1].obj)->globals();
[email protected]
   904
    else
[email protected]
   905
        obj = Struct::cast(top.sp[-1].obj);
[email protected]
   906
    GetField(obj, READ_UINT32(top.pc), &top.sp[-1]);
[email protected]
   907
    break;
[email protected]
   908
}
[email protected]
   909
[email protected]
   910
case OP_SETFIELD:
[email protected]
   911
{
[email protected]
   912
    Struct *obj = Struct::cast(top.sp[-2].obj);
[email protected]
   913
    SetField(zone, obj, READ_UINT32(top.pc), top.sp[-1]);
[email protected]
   914
    top.sp[-2] = top.sp[-1];
[email protected]
   915
    top.sp--;
[email protected]
   916
    break;
[email protected]
   917
}
[email protected]
   918
[email protected]
   919
case OP_COPYSTRUCT:
[email protected]
   920
{
[email protected]
   921
    Local<Struct> left(zone, Struct::cast(top.sp[-2].obj));
[email protected]
   922
    Local<Struct> right(zone, Struct::cast(top.sp[-1].obj));
[email protected]
   923
    if (!Struct::Copy(zone, left, right))
[email protected]
   924
        goto error;
[email protected]
   925
    top.sp[-2].obj = top.sp[-1].obj;
[email protected]
   926
    top.sp--;
[email protected]
   927
    break;
[email protected]
   928
}
[email protected]
   929
[email protected]
   930
case OP_TABLESWITCH:
[email protected]
   931
{
[email protected]
   932
    int value = top.sp[-1].i32;
[email protected]
   933
    int low = READ_TABLESWITCH_LOW(top.pc);
[email protected]
   934
    int high = READ_TABLESWITCH_HIGH(top.pc);
[email protected]
   935
[email protected]
   936
    top.sp--;
[email protected]
   937
[email protected]
   938
    unsigned index = unsigned(value - low);
[email protected]
   939
    unsigned tablesize = TableSwitchEntries(low, high);
[email protected]
   940
    if (index < tablesize) {
[email protected]
   941
        top.pc += OP_TABLESWITCH_LENGTH + OP_JUMP_LENGTH;
[email protected]
   942
        top.pc += index * OP_JUMP_LENGTH;
[email protected]
   943
        continue;
[email protected]
   944
    }
[email protected]
   945
    break;
[email protected]
   946
}
[email protected]
   947
[email protected]
   948
case OP_SIZEOF:
[email protected]
   949
{
[email protected]
   950
    Array *array = Array::cast(top.sp[-1].obj);
[email protected]
   951
    top.sp[-1].i32 = array->length();
[email protected]
   952
    break;
[email protected]
   953
}
[email protected]
   954
[email protected]
   955
case OP_PUBLIC:
[email protected]
   956
{
[email protected]
   957
    Local<Function> fun(zone, Function::cast(top.sp[-1].obj));
[email protected]
   958
    zone->registerPublic(fun);
[email protected]
   959
    top.sp--;
[email protected]
   960
    break;
[email protected]
   961
}
[email protected]
   962
[email protected]
   963
case OP_SETGLOBAL:
[email protected]
   964
{
[email protected]
   965
    Local<Code> code(zone, top.fp->code());
[email protected]
   966
    Local<Struct> obj(zone, code->module()->globals());
[email protected]
   967
    Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
[email protected]
   968
    SetGlobal(zone, obj, name, top.sp[-1]);
[email protected]
   969
    break;
[email protected]
   970
}
[email protected]
   971
[email protected]
   972
case OP_GETGLOBAL:
[email protected]
   973
{
[email protected]
   974
    Local<Code> code(zone, top.fp->code());
[email protected]
   975
    Local<Struct> obj(zone, code->module()->globals());
[email protected]
   976
    Local<String> name(zone, String::cast(code->strings()->at(READ_UINT32(top.pc))));
[email protected]
   977
    GetGlobal(zone, obj, name, top.sp);
[email protected]
   978
    top.sp++;
[email protected]
   979
    break;
[email protected]
   980
}
[email protected]
   981
[email protected]
   982
case OP_ENTERLOOP:
[email protected]
   983
{
[email protected]
   984
    top.fp->enterLoop(zone);
[email protected]
   985
    break;
[email protected]
   986
}
[email protected]
   987
[email protected]
   988
case OP_LEAVELOOP:
[email protected]
   989
{
[email protected]
   990
    top.fp->leaveLoop(zone);
[email protected]
   991
    break;
[email protected]
   992
}
[email protected]
   993
[email protected]
   994
case OP_DEPARRAY:
[email protected]
   995
{
[email protected]
   996
    Local<ArrayMap> map(zone, ArrayMap::cast(top.fp->code()->object(READ_UINT32(top.pc))));
[email protected]
   997
    Local<Array> array(zone, Array::cast(top.sp[-2].obj));
[email protected]
   998
    int index = top.sp[-1].i32;
[email protected]
   999
[email protected]
  1000
    // We allow taking 0-length dependent arrays.
[email protected]
  1001
    if (index < 0 || unsigned(index) > array->length()) {
[email protected]
  1002
        zone->reportError(Message_ArrayIndexOutOfBounds, index, array->length());
[email protected]
  1003
        goto error;
[email protected]
  1004
    }
[email protected]
  1005
[email protected]
  1006
    top.sp[-2].obj = DependentArray::New(zone, array, map, index);
[email protected]
  1007
    if (!top.sp[-2].obj)
[email protected]
  1008
        goto error;
[email protected]
  1009
[email protected]
  1010
    top.sp--;
[email protected]
  1011
    break;
[email protected]
  1012
}
[email protected]
  1013
[email protected]
  1014
case OP_IMPORT:
[email protected]
  1015
{
[email protected]
  1016
    unsigned index = READ_UINT32(top.pc);
[email protected]
  1017
[email protected]
  1018
    top.sp++;
[email protected]
  1019
    top.sp[-1].obj = top.fp->code()->module()->getImportAt(index);
[email protected]
  1020
    break;
[email protected]
  1021
}
[email protected]
  1022
[email protected]
  1023
default:
[email protected]
  1024
    assert(false);
[email protected]
  1025
    return false;
[email protected]
  1026
[email protected]
  1027
        }
[email protected]
  1028
[email protected]
  1029
        top.pc += OpcodeInfo[op].length;
[email protected]
  1030
        assert(top.sp >= top.fp->stackBase());
[email protected]
  1031
    }
[email protected]
  1032
[email protected]
  1033
done:
[email protected]
  1034
    return true;
[email protected]
  1035
[email protected]
  1036
error:
[email protected]
  1037
    // Unwind frames until we can continue.
[email protected]
  1038
    while (top.fp != entryFrame)
[email protected]
  1039
        zone->stack().popInlineFrame();
[email protected]
  1040
    return false;
[email protected]
  1041
}