src/Opcodes.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 247 7721042bdb67
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 <stdlib.h>
[email protected]
    20
#include "Opcodes.h"
[email protected]
    21
#include "Utility.h"
[email protected]
    22
#include "Code.h"
[email protected]
    23
#include "Zone.h"
[email protected]
    24
#include "Array.h"
[email protected]
    25
#include "Strings.h"
[email protected]
    26
#include "CompactBuffer.h"
[email protected]
    27
#include "Structures.h"
[email protected]
    28
#include "Modules.h"
[email protected]
    29
#include "Heap-inl.h"
[email protected]
    30
[email protected]
    31
using namespace ke;
[email protected]
    32
[email protected]
    33
OpInfo ke::OpcodeInfo[] =
[email protected]
    34
{
[email protected]
    35
#define OPDEF(name, text, length, uses, defs) \
[email protected]
    36
    {text, length, uses, defs},
[email protected]
    37
#include "Opcodes.tbl"
[email protected]
    38
#undef OPDEF
[email protected]
    39
};
[email protected]
    40
[email protected]
    41
Code *
[email protected]
    42
Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
[email protected]
    43
          Handle<FixedArray> objects, Handle<FixedArray> strings, unsigned maxStackDepth,
[email protected]
    44
          unsigned maxLoopDepth, Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap)
[email protected]
    45
{
[email protected]
    46
    Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
[email protected]
    47
    if (!code)
[email protected]
    48
        return NULL;
[email protected]
    49
[email protected]
    50
    code->bytecode_ = bytecode;
[email protected]
    51
    code->locals_ = locals;
[email protected]
    52
    code->objects_ = objects;
[email protected]
    53
    code->maxStackDepth_ = maxStackDepth;
[email protected]
    54
    code->module_ = NULL;
[email protected]
    55
    code->safepoints_ = safepoints;
[email protected]
    56
    code->sourcemap_ = sourcemap;
[email protected]
    57
    code->lineNumber_ = 0;
[email protected]
    58
    code->strings_ = strings;
[email protected]
    59
    code->maxLoopDepth_ = maxLoopDepth;
[email protected]
    60
    return code;
[email protected]
    61
}
[email protected]
    62
[email protected]
    63
static int
[email protected]
    64
CompareSafepoints(const void *a, const void *b)
[email protected]
    65
{
[email protected]
    66
    const unsigned *sa = reinterpret_cast<const unsigned *>(a);
[email protected]
    67
    const unsigned *sb = reinterpret_cast<const unsigned *>(b);
[email protected]
    68
    if (*sa > *sb)
[email protected]
    69
        return 1;
[email protected]
    70
    if (*sa == *sb)
[email protected]
    71
        return 0;
[email protected]
    72
    return -1;
[email protected]
    73
}
[email protected]
    74
[email protected]
    75
uint8 *
[email protected]
    76
Code::safepointAt(unsigned pcOffset)
[email protected]
    77
{
[email protected]
    78
    // Only specific opcodes may have safepoints. Asking for a safepoint at
[email protected]
    79
    // any other position indicates a failure to mark that position as a
[email protected]
    80
    // safepoint, which is a bug.
[email protected]
    81
#ifndef NDEBUG
[email protected]
    82
    Opcode op = Opcode(*(start() + pcOffset));
[email protected]
    83
    assert(op == OP_CALL ||
[email protected]
    84
           op == OP_NEWFIXED ||
[email protected]
    85
           op == OP_NEWEMPTY ||
[email protected]
    86
           op == OP_NEWSIZED ||
[email protected]
    87
           op == OP_VARREF_LIFO ||
[email protected]
    88
           op == OP_ARGREF_LIFO ||
[email protected]
    89
           op == OP_SLOTREF_LIFO ||
[email protected]
    90
           op == OP_COPYARRAY ||
[email protected]
    91
           op == OP_CALLNATIVE ||
[email protected]
    92
           op == OP_PUBLIC);
[email protected]
    93
#endif
[email protected]
    94
[email protected]
    95
    if (!safepoints_)
[email protected]
    96
        return NULL;
[email protected]
    97
[email protected]
    98
    size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
[email protected]
    99
    assert(!(safepoints_->length() % bytesPerSafepoint));
[email protected]
   100
[email protected]
   101
    size_t count = safepoints_->length() / bytesPerSafepoint;
[email protected]
   102
[email protected]
   103
    void *result = bsearch(&pcOffset,
[email protected]
   104
                           safepoints_->raw(),
[email protected]
   105
                           count,
[email protected]
   106
                           bytesPerSafepoint,
[email protected]
   107
                           CompareSafepoints);
[email protected]
   108
[email protected]
   109
    if (!result)
[email protected]
   110
        return NULL;
[email protected]
   111
[email protected]
   112
    assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
[email protected]
   113
[email protected]
   114
    return Address(result) + sizeof(unsigned);
[email protected]
   115
}
[email protected]
   116
[email protected]
   117
SourcePosition
[email protected]
   118
Code::pcToSource(unsigned pc)
[email protected]
   119
{
[email protected]
   120
    assert(pc < bytecode_->length());
[email protected]
   121
[email protected]
   122
    CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
[email protected]
   123
[email protected]
   124
    unsigned line = 1;
[email protected]
   125
    unsigned col = 0;
[email protected]
   126
    unsigned curPc = 0;
[email protected]
   127
    while (!reader.done()) {
[email protected]
   128
        curPc += reader.readUint32();
[email protected]
   129
        if (curPc > pc)
[email protected]
   130
            break;
[email protected]
   131
[email protected]
   132
        line = reader.readUint32();
[email protected]
   133
        col = reader.readUint32();
[email protected]
   134
    }
[email protected]
   135
[email protected]
   136
    return SourcePosition(lineNumber_ + line, col);
[email protected]
   137
}
[email protected]
   138
[email protected]
   139
// All jumps must be the same length.
[email protected]
   140
STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
[email protected]
   141
STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
[email protected]
   142
[email protected]
   143
void
[email protected]
   144
ke::Disassemble(Code *code, FILE *fp)
[email protected]
   145
{
[email protected]
   146
    bytecode *pc = code->start();
[email protected]
   147
    for (;;) {
[email protected]
   148
        Opcode op = Opcode(*pc);
[email protected]
   149
[email protected]
   150
        fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
[email protected]
   151
[email protected]
   152
        switch (op) {
[email protected]
   153
          case OP_NOP:
[email protected]
   154
          case OP_RETURN:
[email protected]
   155
          case OP_RETURNVOID:
[email protected]
   156
          case OP_ADD:
[email protected]
   157
          case OP_SUB:
[email protected]
   158
          case OP_MUL:
[email protected]
   159
          case OP_DIV:
[email protected]
   160
          case OP_ADD_F:
[email protected]
   161
          case OP_SUB_F:
[email protected]
   162
          case OP_MUL_F:
[email protected]
   163
          case OP_DIV_F:
[email protected]
   164
          case OP_CVT_I2F:
[email protected]
   165
          case OP_CVT_I2B:
[email protected]
   166
          case OP_POP:
[email protected]
   167
          case OP_GETELEM:
[email protected]
   168
          case OP_SETELEM:
[email protected]
   169
          case OP_SHL:
[email protected]
   170
          case OP_SHR:
[email protected]
   171
          case OP_USHR:
[email protected]
   172
          case OP_BITAND:
[email protected]
   173
          case OP_BITOR:
[email protected]
   174
          case OP_BITXOR:
[email protected]
   175
          case OP_GETREF_I:
[email protected]
   176
          case OP_SETREF_I:
[email protected]
   177
          case OP_GETREF_F:
[email protected]
   178
          case OP_SETREF_F:
[email protected]
   179
          case OP_SLOTREF_LIFO:
[email protected]
   180
          case OP_COPYARRAY:
[email protected]
   181
          case OP_COPYSTRUCT:
[email protected]
   182
          case OP_DUP:
[email protected]
   183
          case OP_DUP2:
[email protected]
   184
          case OP_SWAP:
[email protected]
   185
          case OP_ROLL3:
[email protected]
   186
          case OP_PICK2:
[email protected]
   187
          case OP_PICK3:
[email protected]
   188
          case OP_CALL:
[email protected]
   189
          case OP_LT:
[email protected]
   190
          case OP_LE:
[email protected]
   191
          case OP_GE:
[email protected]
   192
          case OP_GT:
[email protected]
   193
          case OP_EQ:
[email protected]
   194
          case OP_NE:
[email protected]
   195
          case OP_LT_F:
[email protected]
   196
          case OP_LE_F:
[email protected]
   197
          case OP_GE_F:
[email protected]
   198
          case OP_GT_F:
[email protected]
   199
          case OP_EQ_F:
[email protected]
   200
          case OP_NE_F:
[email protected]
   201
          case OP_BITNOT:
[email protected]
   202
          case OP_NOT:
[email protected]
   203
          case OP_NOT_F:
[email protected]
   204
          case OP_NEG:
[email protected]
   205
          case OP_NEG_F:
[email protected]
   206
          case OP_MOD:
[email protected]
   207
          case OP_BITCAST:
[email protected]
   208
          case OP_TRUE:
[email protected]
   209
          case OP_FALSE:
[email protected]
   210
          case OP_SIZEOF:
[email protected]
   211
          case OP_PUBLIC:
[email protected]
   212
          case OP_LEAVELOOP:
[email protected]
   213
          case OP_DUPARRAY:
[email protected]
   214
          case OP_DEPARRAY:
[email protected]
   215
            break;
[email protected]
   216
[email protected]
   217
          case OP_ENTERLOOP:
[email protected]
   218
            if (pc[1])
[email protected]
   219
              fprintf(fp, "; lifo region");
[email protected]
   220
            break;
[email protected]
   221
[email protected]
   222
          case OP_STOP:
[email protected]
   223
            fprintf(fp, "\n");
[email protected]
   224
            break;
[email protected]
   225
[email protected]
   226
          case OP_INT:
[email protected]
   227
            fprintf(fp, "%d", READ_INT32(pc));
[email protected]
   228
            break;
[email protected]
   229
[email protected]
   230
          case OP_FLOAT:
[email protected]
   231
            fprintf(fp, "%f", READ_FLOAT(pc));
[email protected]
   232
            break;
[email protected]
   233
[email protected]
   234
          case OP_JT:
[email protected]
   235
          case OP_JF:
[email protected]
   236
          case OP_JUMP:
[email protected]
   237
          case OP_CONTINUE:
[email protected]
   238
          case OP_BREAK:
[email protected]
   239
            fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
[email protected]
   240
            break;
[email protected]
   241
[email protected]
   242
          case OP_NEWFIXED:
[email protected]
   243
          case OP_NEWEMPTY:
[email protected]
   244
          case OP_NEWSIZED:
[email protected]
   245
          {
[email protected]
   246
            Heap::Tenure tenure = (Heap::Tenure)pc[1];
[email protected]
   247
            //ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
[email protected]
   248
            if (tenure == Heap::Tenure_Stack)
[email protected]
   249
                fprintf(fp, " ; lifo");
[email protected]
   250
            else if (tenure == Heap::Tenure_Old)
[email protected]
   251
                fprintf(fp, " ; pretenured");
[email protected]
   252
            else if (tenure == Heap::Tenure_Default)
[email protected]
   253
                fprintf(fp, " ; newborn");
[email protected]
   254
            break;
[email protected]
   255
          }
[email protected]
   256
[email protected]
   257
          case OP_NEWSTRUCT:
[email protected]
   258
          {
[email protected]
   259
            Heap::Tenure tenure = (Heap::Tenure)pc[1];
[email protected]
   260
            StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
[email protected]
   261
            fprintf(fp, "%s", StructType::cast(map->type())->name()->chars());
[email protected]
   262
            if (tenure == Heap::Tenure_Stack)
[email protected]
   263
                fprintf(fp, " ; lifo");
[email protected]
   264
            else if (tenure == Heap::Tenure_Old)
[email protected]
   265
                fprintf(fp, " ; pretenured");
[email protected]
   266
            else if (tenure == Heap::Tenure_Default)
[email protected]
   267
                fprintf(fp, " ; newborn");
[email protected]
   268
            break;
[email protected]
   269
          }
[email protected]
   270
[email protected]
   271
          case OP_GETLOCAL:
[email protected]
   272
          case OP_SETLOCAL:
[email protected]
   273
          case OP_GETARG:
[email protected]
   274
          case OP_SETARG:
[email protected]
   275
          case OP_SAVELIFO:
[email protected]
   276
          case OP_UNWINDLIFO:
[email protected]
   277
          case OP_VARREF_LIFO:
[email protected]
   278
          case OP_ARGREF_LIFO:
[email protected]
   279
          case OP_OBJECT:
[email protected]
   280
          case OP_GETFIELD:
[email protected]
   281
          case OP_SETFIELD:
[email protected]
   282
            fprintf(fp, "%u", READ_UINT32(pc));
[email protected]
   283
            break;
[email protected]
   284
[email protected]
   285
          case OP_CALLNATIVE:
[email protected]
   286
          {
[email protected]
   287
            unsigned argc = READ_UINT32(pc);
[email protected]
   288
            fprintf(fp, " ; %u args", argc);
[email protected]
   289
            break;
[email protected]
   290
          }
[email protected]
   291
[email protected]
   292
          case OP_TABLESWITCH:
[email protected]
   293
          {
[email protected]
   294
            int low = READ_TABLESWITCH_LOW(pc);
[email protected]
   295
            int high = READ_TABLESWITCH_HIGH(pc);
[email protected]
   296
            fprintf(fp, "%d, %d\n", low, high);
[email protected]
   297
            break;
[email protected]
   298
          }
[email protected]
   299
[email protected]
   300
          case OP_GETGLOBAL:
[email protected]
   301
          case OP_SETGLOBAL:
[email protected]
   302
          {
[email protected]
   303
            unsigned index = READ_UINT32(pc);
[email protected]
   304
            fprintf(fp, "%s", String::cast(code->strings()->at(index))->chars());
[email protected]
   305
            break;
[email protected]
   306
          }
[email protected]
   307
[email protected]
   308
          case OP_IMPORT:
[email protected]
   309
          {
[email protected]
   310
            unsigned index = READ_UINT32(pc);
[email protected]
   311
            FixedArray *path = code->module()->importPathAt(index);
[email protected]
   312
            fprintf(fp, " ; ");
[email protected]
   313
            for (unsigned i = 0; i < path->length(); i++) {
[email protected]
   314
                fprintf(fp, "%s", String::cast(path->at(i))->chars());
[email protected]
   315
                if (i != path->length() - 1)
[email protected]
   316
                    fprintf(fp, ".");
[email protected]
   317
            }
[email protected]
   318
            break;
[email protected]
   319
          }
[email protected]
   320
[email protected]
   321
          default:
[email protected]
   322
            // Unknown opcode!
[email protected]
   323
            assert(false);
[email protected]
   324
            return;
[email protected]
   325
        }
[email protected]
   326
[email protected]
   327
        if (op == OP_STOP)
[email protected]
   328
            break;
[email protected]
   329
[email protected]
   330
        fprintf(fp, "\n");
[email protected]
   331
        pc += OpcodeInfo[op].length;
[email protected]
   332
        assert(pc < code->start() + code->length());
[email protected]
   333
    }
[email protected]
   334
[email protected]
   335
    fprintf(fp, "\nSafepoints:\n");
[email protected]
   336
    ByteArray *safepoints = code->safepoints();
[email protected]
   337
    if (safepoints) {
[email protected]
   338
        size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
[email protected]
   339
        for (size_t i = 0; i < nsafepoints; i++) {
[email protected]
   340
            Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
[email protected]
   341
            fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
[email protected]
   342
            unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
[email protected]
   343
            for (unsigned j = 0; j < code->maxStackDepth(); j++) {
[email protected]
   344
                if (bits[j / 8] & (1 << (j % 8)))
[email protected]
   345
                    fprintf(fp, "%d ", j);
[email protected]
   346
            }
[email protected]
   347
            fprintf(fp, "\n");
[email protected]
   348
        }
[email protected]
   349
    }
[email protected]
   350
}