src/Opcodes.cpp
changeset 263 ba85a47ee414
parent 256 3c184218462d
     1.1 --- a/src/Opcodes.cpp	Mon Jun 10 00:46:29 2013 +0100
     1.2 +++ b/src/Opcodes.cpp	Mon Jun 10 01:04:43 2013 +0100
     1.3 @@ -1,350 +1,350 @@
     1.4 -/* vim: set ts=4 sw=4 tw=99 et:
     1.5 - *
     1.6 - * Copyright (C) 2012 David Anderson
     1.7 - *
     1.8 - * This file is part of JITCraft.
     1.9 - *
    1.10 - * JITCraft is free software: you can redistribute it and/or modify it under
    1.11 - * the terms of the GNU General Public License as published by the Free
    1.12 - * Software Foundation, either version 3 of the License, or (at your option)
    1.13 - * any later version.
    1.14 - * 
    1.15 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 - *
    1.19 - * You should have received a copy of the GNU General Public License along with
    1.20 - * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.21 - */
    1.22 -#include <stdlib.h>
    1.23 -#include "Opcodes.h"
    1.24 -#include "Utility.h"
    1.25 -#include "Code.h"
    1.26 -#include "Zone.h"
    1.27 -#include "Array.h"
    1.28 -#include "Strings.h"
    1.29 -#include "CompactBuffer.h"
    1.30 -#include "Structures.h"
    1.31 -#include "Modules.h"
    1.32 -#include "Heap-inl.h"
    1.33 -
    1.34 -using namespace ke;
    1.35 -
    1.36 -OpInfo ke::OpcodeInfo[] =
    1.37 -{
    1.38 -#define OPDEF(name, text, length, uses, defs) \
    1.39 -    {text, length, uses, defs},
    1.40 -#include "Opcodes.tbl"
    1.41 -#undef OPDEF
    1.42 -};
    1.43 -
    1.44 -Code *
    1.45 -Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    1.46 -          Handle<FixedArray> objects, Handle<FixedArray> strings, unsigned maxStackDepth,
    1.47 -          unsigned maxLoopDepth, Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap)
    1.48 -{
    1.49 -    Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
    1.50 -    if (!code)
    1.51 -        return NULL;
    1.52 -
    1.53 -    code->bytecode_ = bytecode;
    1.54 -    code->locals_ = locals;
    1.55 -    code->objects_ = objects;
    1.56 -    code->maxStackDepth_ = maxStackDepth;
    1.57 -    code->module_ = NULL;
    1.58 -    code->safepoints_ = safepoints;
    1.59 -    code->sourcemap_ = sourcemap;
    1.60 -    code->lineNumber_ = 0;
    1.61 -    code->strings_ = strings;
    1.62 -    code->maxLoopDepth_ = maxLoopDepth;
    1.63 -    return code;
    1.64 -}
    1.65 -
    1.66 -static int
    1.67 -CompareSafepoints(const void *a, const void *b)
    1.68 -{
    1.69 -    const unsigned *sa = reinterpret_cast<const unsigned *>(a);
    1.70 -    const unsigned *sb = reinterpret_cast<const unsigned *>(b);
    1.71 -    if (*sa > *sb)
    1.72 -        return 1;
    1.73 -    if (*sa == *sb)
    1.74 -        return 0;
    1.75 -    return -1;
    1.76 -}
    1.77 -
    1.78 -uint8 *
    1.79 -Code::safepointAt(unsigned pcOffset)
    1.80 -{
    1.81 -    // Only specific opcodes may have safepoints. Asking for a safepoint at
    1.82 -    // any other position indicates a failure to mark that position as a
    1.83 -    // safepoint, which is a bug.
    1.84 -#ifndef NDEBUG
    1.85 -    Opcode op = Opcode(*(start() + pcOffset));
    1.86 -    assert(op == OP_CALL ||
    1.87 -           op == OP_NEWFIXED ||
    1.88 -           op == OP_NEWEMPTY ||
    1.89 -           op == OP_NEWSIZED ||
    1.90 -           op == OP_VARREF_LIFO ||
    1.91 -           op == OP_ARGREF_LIFO ||
    1.92 -           op == OP_SLOTREF_LIFO ||
    1.93 -           op == OP_COPYARRAY ||
    1.94 -           op == OP_CALLNATIVE ||
    1.95 -           op == OP_PUBLIC);
    1.96 -#endif
    1.97 -
    1.98 -    if (!safepoints_)
    1.99 -        return NULL;
   1.100 -
   1.101 -    size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
   1.102 -    assert(!(safepoints_->length() % bytesPerSafepoint));
   1.103 -
   1.104 -    size_t count = safepoints_->length() / bytesPerSafepoint;
   1.105 -
   1.106 -    void *result = bsearch(&pcOffset,
   1.107 -                           safepoints_->raw(),
   1.108 -                           count,
   1.109 -                           bytesPerSafepoint,
   1.110 -                           CompareSafepoints);
   1.111 -
   1.112 -    if (!result)
   1.113 -        return NULL;
   1.114 -
   1.115 -    assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
   1.116 -
   1.117 -    return Address(result) + sizeof(unsigned);
   1.118 -}
   1.119 -
   1.120 -SourcePosition
   1.121 -Code::pcToSource(unsigned pc)
   1.122 -{
   1.123 -    assert(pc < bytecode_->length());
   1.124 -
   1.125 -    CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
   1.126 -
   1.127 -    unsigned line = 1;
   1.128 -    unsigned col = 0;
   1.129 -    unsigned curPc = 0;
   1.130 -    while (!reader.done()) {
   1.131 -        curPc += reader.readUint32();
   1.132 -        if (curPc > pc)
   1.133 -            break;
   1.134 -
   1.135 -        line = reader.readUint32();
   1.136 -        col = reader.readUint32();
   1.137 -    }
   1.138 -
   1.139 -    return SourcePosition(lineNumber_ + line, col);
   1.140 -}
   1.141 -
   1.142 -// All jumps must be the same length.
   1.143 -STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
   1.144 -STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
   1.145 -
   1.146 -void
   1.147 -ke::Disassemble(Code *code, FILE *fp)
   1.148 -{
   1.149 -    bytecode *pc = code->start();
   1.150 -    for (;;) {
   1.151 -        Opcode op = Opcode(*pc);
   1.152 -
   1.153 -        fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
   1.154 -
   1.155 -        switch (op) {
   1.156 -          case OP_NOP:
   1.157 -          case OP_RETURN:
   1.158 -          case OP_RETURNVOID:
   1.159 -          case OP_ADD:
   1.160 -          case OP_SUB:
   1.161 -          case OP_MUL:
   1.162 -          case OP_DIV:
   1.163 -          case OP_ADD_F:
   1.164 -          case OP_SUB_F:
   1.165 -          case OP_MUL_F:
   1.166 -          case OP_DIV_F:
   1.167 -          case OP_CVT_I2F:
   1.168 -          case OP_CVT_I2B:
   1.169 -          case OP_POP:
   1.170 -          case OP_GETELEM:
   1.171 -          case OP_SETELEM:
   1.172 -          case OP_SHL:
   1.173 -          case OP_SHR:
   1.174 -          case OP_USHR:
   1.175 -          case OP_BITAND:
   1.176 -          case OP_BITOR:
   1.177 -          case OP_BITXOR:
   1.178 -          case OP_GETREF_I:
   1.179 -          case OP_SETREF_I:
   1.180 -          case OP_GETREF_F:
   1.181 -          case OP_SETREF_F:
   1.182 -          case OP_SLOTREF_LIFO:
   1.183 -          case OP_COPYARRAY:
   1.184 -          case OP_COPYSTRUCT:
   1.185 -          case OP_DUP:
   1.186 -          case OP_DUP2:
   1.187 -          case OP_SWAP:
   1.188 -          case OP_ROLL3:
   1.189 -          case OP_PICK2:
   1.190 -          case OP_PICK3:
   1.191 -          case OP_CALL:
   1.192 -          case OP_LT:
   1.193 -          case OP_LE:
   1.194 -          case OP_GE:
   1.195 -          case OP_GT:
   1.196 -          case OP_EQ:
   1.197 -          case OP_NE:
   1.198 -          case OP_LT_F:
   1.199 -          case OP_LE_F:
   1.200 -          case OP_GE_F:
   1.201 -          case OP_GT_F:
   1.202 -          case OP_EQ_F:
   1.203 -          case OP_NE_F:
   1.204 -          case OP_BITNOT:
   1.205 -          case OP_NOT:
   1.206 -          case OP_NOT_F:
   1.207 -          case OP_NEG:
   1.208 -          case OP_NEG_F:
   1.209 -          case OP_MOD:
   1.210 -          case OP_BITCAST:
   1.211 -          case OP_TRUE:
   1.212 -          case OP_FALSE:
   1.213 -          case OP_SIZEOF:
   1.214 -          case OP_PUBLIC:
   1.215 -          case OP_LEAVELOOP:
   1.216 -          case OP_DUPARRAY:
   1.217 -          case OP_DEPARRAY:
   1.218 -            break;
   1.219 -
   1.220 -          case OP_ENTERLOOP:
   1.221 -            if (pc[1])
   1.222 -              fprintf(fp, "; lifo region");
   1.223 -            break;
   1.224 -
   1.225 -          case OP_STOP:
   1.226 -            fprintf(fp, "\n");
   1.227 -            break;
   1.228 -
   1.229 -          case OP_INT:
   1.230 -            fprintf(fp, "%d", READ_INT32(pc));
   1.231 -            break;
   1.232 -
   1.233 -          case OP_FLOAT:
   1.234 -            fprintf(fp, "%f", READ_FLOAT(pc));
   1.235 -            break;
   1.236 -
   1.237 -          case OP_JT:
   1.238 -          case OP_JF:
   1.239 -          case OP_JUMP:
   1.240 -          case OP_CONTINUE:
   1.241 -          case OP_BREAK:
   1.242 -            fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   1.243 -            break;
   1.244 -
   1.245 -          case OP_NEWFIXED:
   1.246 -          case OP_NEWEMPTY:
   1.247 -          case OP_NEWSIZED:
   1.248 -          {
   1.249 -            Heap::Tenure tenure = (Heap::Tenure)pc[1];
   1.250 -            //ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
   1.251 -            if (tenure == Heap::Tenure_Stack)
   1.252 -                fprintf(fp, " ; lifo");
   1.253 -            else if (tenure == Heap::Tenure_Old)
   1.254 -                fprintf(fp, " ; pretenured");
   1.255 -            else if (tenure == Heap::Tenure_Default)
   1.256 -                fprintf(fp, " ; newborn");
   1.257 -            break;
   1.258 -          }
   1.259 -
   1.260 -          case OP_NEWSTRUCT:
   1.261 -          {
   1.262 -            Heap::Tenure tenure = (Heap::Tenure)pc[1];
   1.263 -            StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
   1.264 -            fprintf(fp, "%s", StructType::cast(map->type())->name()->chars());
   1.265 -            if (tenure == Heap::Tenure_Stack)
   1.266 -                fprintf(fp, " ; lifo");
   1.267 -            else if (tenure == Heap::Tenure_Old)
   1.268 -                fprintf(fp, " ; pretenured");
   1.269 -            else if (tenure == Heap::Tenure_Default)
   1.270 -                fprintf(fp, " ; newborn");
   1.271 -            break;
   1.272 -          }
   1.273 -
   1.274 -          case OP_GETLOCAL:
   1.275 -          case OP_SETLOCAL:
   1.276 -          case OP_GETARG:
   1.277 -          case OP_SETARG:
   1.278 -          case OP_SAVELIFO:
   1.279 -          case OP_UNWINDLIFO:
   1.280 -          case OP_VARREF_LIFO:
   1.281 -          case OP_ARGREF_LIFO:
   1.282 -          case OP_OBJECT:
   1.283 -          case OP_GETFIELD:
   1.284 -          case OP_SETFIELD:
   1.285 -            fprintf(fp, "%u", READ_UINT32(pc));
   1.286 -            break;
   1.287 -
   1.288 -          case OP_CALLNATIVE:
   1.289 -          {
   1.290 -            unsigned argc = READ_UINT32(pc);
   1.291 -            fprintf(fp, " ; %u args", argc);
   1.292 -            break;
   1.293 -          }
   1.294 -
   1.295 -          case OP_TABLESWITCH:
   1.296 -          {
   1.297 -            int low = READ_TABLESWITCH_LOW(pc);
   1.298 -            int high = READ_TABLESWITCH_HIGH(pc);
   1.299 -            fprintf(fp, "%d, %d\n", low, high);
   1.300 -            break;
   1.301 -          }
   1.302 -
   1.303 -          case OP_GETGLOBAL:
   1.304 -          case OP_SETGLOBAL:
   1.305 -          {
   1.306 -            unsigned index = READ_UINT32(pc);
   1.307 -            fprintf(fp, "%s", String::cast(code->strings()->at(index))->chars());
   1.308 -            break;
   1.309 -          }
   1.310 -
   1.311 -          case OP_IMPORT:
   1.312 -          {
   1.313 -            unsigned index = READ_UINT32(pc);
   1.314 -            FixedArray *path = code->module()->importPathAt(index);
   1.315 -            fprintf(fp, " ; ");
   1.316 -            for (unsigned i = 0; i < path->length(); i++) {
   1.317 -                fprintf(fp, "%s", String::cast(path->at(i))->chars());
   1.318 -                if (i != path->length() - 1)
   1.319 -                    fprintf(fp, ".");
   1.320 -            }
   1.321 -            break;
   1.322 -          }
   1.323 -
   1.324 -          default:
   1.325 -            // Unknown opcode!
   1.326 -            assert(false);
   1.327 -            return;
   1.328 -        }
   1.329 -
   1.330 -        if (op == OP_STOP)
   1.331 -            break;
   1.332 -
   1.333 -        fprintf(fp, "\n");
   1.334 -        pc += OpcodeInfo[op].length;
   1.335 -        assert(pc < code->start() + code->length());
   1.336 -    }
   1.337 -
   1.338 -    fprintf(fp, "\nSafepoints:\n");
   1.339 -    ByteArray *safepoints = code->safepoints();
   1.340 -    if (safepoints) {
   1.341 -        size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
   1.342 -        for (size_t i = 0; i < nsafepoints; i++) {
   1.343 -            Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
   1.344 -            fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
   1.345 -            unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
   1.346 -            for (unsigned j = 0; j < code->maxStackDepth(); j++) {
   1.347 -                if (bits[j / 8] & (1 << (j % 8)))
   1.348 -                    fprintf(fp, "%d ", j);
   1.349 -            }
   1.350 -            fprintf(fp, "\n");
   1.351 -        }
   1.352 -    }
   1.353 -}
   1.354 +/* vim: set ts=4 sw=4 tw=99 et:
   1.355 + *
   1.356 + * Copyright (C) 2012 David Anderson
   1.357 + *
   1.358 + * This file is part of JITCraft.
   1.359 + *
   1.360 + * JITCraft is free software: you can redistribute it and/or modify it under
   1.361 + * the terms of the GNU General Public License as published by the Free
   1.362 + * Software Foundation, either version 3 of the License, or (at your option)
   1.363 + * any later version.
   1.364 + * 
   1.365 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
   1.366 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.367 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   1.368 + *
   1.369 + * You should have received a copy of the GNU General Public License along with
   1.370 + * JITCraft. If not, see http://www.gnu.org/licenses/.
   1.371 + */
   1.372 +#include <stdlib.h>
   1.373 +#include "Opcodes.h"
   1.374 +#include "Utility.h"
   1.375 +#include "Code.h"
   1.376 +#include "Zone.h"
   1.377 +#include "Array.h"
   1.378 +#include "Strings.h"
   1.379 +#include "CompactBuffer.h"
   1.380 +#include "Structures.h"
   1.381 +#include "Modules.h"
   1.382 +#include "Heap-inl.h"
   1.383 +
   1.384 +using namespace ke;
   1.385 +
   1.386 +OpInfo ke::OpcodeInfo[] =
   1.387 +{
   1.388 +#define OPDEF(name, text, length, uses, defs) \
   1.389 +    {text, length, uses, defs},
   1.390 +#include "Opcodes.tbl"
   1.391 +#undef OPDEF
   1.392 +};
   1.393 +
   1.394 +Code *
   1.395 +Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
   1.396 +          Handle<FixedArray> objects, Handle<FixedArray> strings, unsigned maxStackDepth,
   1.397 +          unsigned maxLoopDepth, Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap)
   1.398 +{
   1.399 +    Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
   1.400 +    if (!code)
   1.401 +        return NULL;
   1.402 +
   1.403 +    code->bytecode_ = bytecode;
   1.404 +    code->locals_ = locals;
   1.405 +    code->objects_ = objects;
   1.406 +    code->maxStackDepth_ = maxStackDepth;
   1.407 +    code->module_ = NULL;
   1.408 +    code->safepoints_ = safepoints;
   1.409 +    code->sourcemap_ = sourcemap;
   1.410 +    code->lineNumber_ = 0;
   1.411 +    code->strings_ = strings;
   1.412 +    code->maxLoopDepth_ = maxLoopDepth;
   1.413 +    return code;
   1.414 +}
   1.415 +
   1.416 +static int
   1.417 +CompareSafepoints(const void *a, const void *b)
   1.418 +{
   1.419 +    const unsigned *sa = reinterpret_cast<const unsigned *>(a);
   1.420 +    const unsigned *sb = reinterpret_cast<const unsigned *>(b);
   1.421 +    if (*sa > *sb)
   1.422 +        return 1;
   1.423 +    if (*sa == *sb)
   1.424 +        return 0;
   1.425 +    return -1;
   1.426 +}
   1.427 +
   1.428 +uint8 *
   1.429 +Code::safepointAt(unsigned pcOffset)
   1.430 +{
   1.431 +    // Only specific opcodes may have safepoints. Asking for a safepoint at
   1.432 +    // any other position indicates a failure to mark that position as a
   1.433 +    // safepoint, which is a bug.
   1.434 +#ifndef NDEBUG
   1.435 +    Opcode op = Opcode(*(start() + pcOffset));
   1.436 +    assert(op == OP_CALL ||
   1.437 +           op == OP_NEWFIXED ||
   1.438 +           op == OP_NEWEMPTY ||
   1.439 +           op == OP_NEWSIZED ||
   1.440 +           op == OP_VARREF_LIFO ||
   1.441 +           op == OP_ARGREF_LIFO ||
   1.442 +           op == OP_SLOTREF_LIFO ||
   1.443 +           op == OP_COPYARRAY ||
   1.444 +           op == OP_CALLNATIVE ||
   1.445 +           op == OP_PUBLIC);
   1.446 +#endif
   1.447 +
   1.448 +    if (!safepoints_)
   1.449 +        return NULL;
   1.450 +
   1.451 +    size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
   1.452 +    assert(!(safepoints_->length() % bytesPerSafepoint));
   1.453 +
   1.454 +    size_t count = safepoints_->length() / bytesPerSafepoint;
   1.455 +
   1.456 +    void *result = bsearch(&pcOffset,
   1.457 +                           safepoints_->raw(),
   1.458 +                           count,
   1.459 +                           bytesPerSafepoint,
   1.460 +                           CompareSafepoints);
   1.461 +
   1.462 +    if (!result)
   1.463 +        return NULL;
   1.464 +
   1.465 +    assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
   1.466 +
   1.467 +    return Address(result) + sizeof(unsigned);
   1.468 +}
   1.469 +
   1.470 +SourcePosition
   1.471 +Code::pcToSource(unsigned pc)
   1.472 +{
   1.473 +    assert(pc < bytecode_->length());
   1.474 +
   1.475 +    CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
   1.476 +
   1.477 +    unsigned line = 1;
   1.478 +    unsigned col = 0;
   1.479 +    unsigned curPc = 0;
   1.480 +    while (!reader.done()) {
   1.481 +        curPc += reader.readUint32();
   1.482 +        if (curPc > pc)
   1.483 +            break;
   1.484 +
   1.485 +        line = reader.readUint32();
   1.486 +        col = reader.readUint32();
   1.487 +    }
   1.488 +
   1.489 +    return SourcePosition(lineNumber_ + line, col);
   1.490 +}
   1.491 +
   1.492 +// All jumps must be the same length.
   1.493 +STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
   1.494 +STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
   1.495 +
   1.496 +void
   1.497 +ke::Disassemble(Code *code, FILE *fp)
   1.498 +{
   1.499 +    bytecode *pc = code->start();
   1.500 +    for (;;) {
   1.501 +        Opcode op = Opcode(*pc);
   1.502 +
   1.503 +        fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
   1.504 +
   1.505 +        switch (op) {
   1.506 +          case OP_NOP:
   1.507 +          case OP_RETURN:
   1.508 +          case OP_RETURNVOID:
   1.509 +          case OP_ADD:
   1.510 +          case OP_SUB:
   1.511 +          case OP_MUL:
   1.512 +          case OP_DIV:
   1.513 +          case OP_ADD_F:
   1.514 +          case OP_SUB_F:
   1.515 +          case OP_MUL_F:
   1.516 +          case OP_DIV_F:
   1.517 +          case OP_CVT_I2F:
   1.518 +          case OP_CVT_I2B:
   1.519 +          case OP_POP:
   1.520 +          case OP_GETELEM:
   1.521 +          case OP_SETELEM:
   1.522 +          case OP_SHL:
   1.523 +          case OP_SHR:
   1.524 +          case OP_USHR:
   1.525 +          case OP_BITAND:
   1.526 +          case OP_BITOR:
   1.527 +          case OP_BITXOR:
   1.528 +          case OP_GETREF_I:
   1.529 +          case OP_SETREF_I:
   1.530 +          case OP_GETREF_F:
   1.531 +          case OP_SETREF_F:
   1.532 +          case OP_SLOTREF_LIFO:
   1.533 +          case OP_COPYARRAY:
   1.534 +          case OP_COPYSTRUCT:
   1.535 +          case OP_DUP:
   1.536 +          case OP_DUP2:
   1.537 +          case OP_SWAP:
   1.538 +          case OP_ROLL3:
   1.539 +          case OP_PICK2:
   1.540 +          case OP_PICK3:
   1.541 +          case OP_CALL:
   1.542 +          case OP_LT:
   1.543 +          case OP_LE:
   1.544 +          case OP_GE:
   1.545 +          case OP_GT:
   1.546 +          case OP_EQ:
   1.547 +          case OP_NE:
   1.548 +          case OP_LT_F:
   1.549 +          case OP_LE_F:
   1.550 +          case OP_GE_F:
   1.551 +          case OP_GT_F:
   1.552 +          case OP_EQ_F:
   1.553 +          case OP_NE_F:
   1.554 +          case OP_BITNOT:
   1.555 +          case OP_NOT:
   1.556 +          case OP_NOT_F:
   1.557 +          case OP_NEG:
   1.558 +          case OP_NEG_F:
   1.559 +          case OP_MOD:
   1.560 +          case OP_BITCAST:
   1.561 +          case OP_TRUE:
   1.562 +          case OP_FALSE:
   1.563 +          case OP_SIZEOF:
   1.564 +          case OP_PUBLIC:
   1.565 +          case OP_LEAVELOOP:
   1.566 +          case OP_DUPARRAY:
   1.567 +          case OP_DEPARRAY:
   1.568 +            break;
   1.569 +
   1.570 +          case OP_ENTERLOOP:
   1.571 +            if (pc[1])
   1.572 +              fprintf(fp, "; lifo region");
   1.573 +            break;
   1.574 +
   1.575 +          case OP_STOP:
   1.576 +            fprintf(fp, "\n");
   1.577 +            break;
   1.578 +
   1.579 +          case OP_INT:
   1.580 +            fprintf(fp, "%d", READ_INT32(pc));
   1.581 +            break;
   1.582 +
   1.583 +          case OP_FLOAT:
   1.584 +            fprintf(fp, "%f", READ_FLOAT(pc));
   1.585 +            break;
   1.586 +
   1.587 +          case OP_JT:
   1.588 +          case OP_JF:
   1.589 +          case OP_JUMP:
   1.590 +          case OP_CONTINUE:
   1.591 +          case OP_BREAK:
   1.592 +            fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   1.593 +            break;
   1.594 +
   1.595 +          case OP_NEWFIXED:
   1.596 +          case OP_NEWEMPTY:
   1.597 +          case OP_NEWSIZED:
   1.598 +          {
   1.599 +            Heap::Tenure tenure = (Heap::Tenure)pc[1];
   1.600 +            //ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
   1.601 +            if (tenure == Heap::Tenure_Stack)
   1.602 +                fprintf(fp, " ; lifo");
   1.603 +            else if (tenure == Heap::Tenure_Old)
   1.604 +                fprintf(fp, " ; pretenured");
   1.605 +            else if (tenure == Heap::Tenure_Default)
   1.606 +                fprintf(fp, " ; newborn");
   1.607 +            break;
   1.608 +          }
   1.609 +
   1.610 +          case OP_NEWSTRUCT:
   1.611 +          {
   1.612 +            Heap::Tenure tenure = (Heap::Tenure)pc[1];
   1.613 +            StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
   1.614 +            fprintf(fp, "%s", StructType::cast(map->type())->name()->chars());
   1.615 +            if (tenure == Heap::Tenure_Stack)
   1.616 +                fprintf(fp, " ; lifo");
   1.617 +            else if (tenure == Heap::Tenure_Old)
   1.618 +                fprintf(fp, " ; pretenured");
   1.619 +            else if (tenure == Heap::Tenure_Default)
   1.620 +                fprintf(fp, " ; newborn");
   1.621 +            break;
   1.622 +          }
   1.623 +
   1.624 +          case OP_GETLOCAL:
   1.625 +          case OP_SETLOCAL:
   1.626 +          case OP_GETARG:
   1.627 +          case OP_SETARG:
   1.628 +          case OP_SAVELIFO:
   1.629 +          case OP_UNWINDLIFO:
   1.630 +          case OP_VARREF_LIFO:
   1.631 +          case OP_ARGREF_LIFO:
   1.632 +          case OP_OBJECT:
   1.633 +          case OP_GETFIELD:
   1.634 +          case OP_SETFIELD:
   1.635 +            fprintf(fp, "%u", READ_UINT32(pc));
   1.636 +            break;
   1.637 +
   1.638 +          case OP_CALLNATIVE:
   1.639 +          {
   1.640 +            unsigned argc = READ_UINT32(pc);
   1.641 +            fprintf(fp, " ; %u args", argc);
   1.642 +            break;
   1.643 +          }
   1.644 +
   1.645 +          case OP_TABLESWITCH:
   1.646 +          {
   1.647 +            int low = READ_TABLESWITCH_LOW(pc);
   1.648 +            int high = READ_TABLESWITCH_HIGH(pc);
   1.649 +            fprintf(fp, "%d, %d\n", low, high);
   1.650 +            break;
   1.651 +          }
   1.652 +
   1.653 +          case OP_GETGLOBAL:
   1.654 +          case OP_SETGLOBAL:
   1.655 +          {
   1.656 +            unsigned index = READ_UINT32(pc);
   1.657 +            fprintf(fp, "%s", String::cast(code->strings()->at(index))->chars());
   1.658 +            break;
   1.659 +          }
   1.660 +
   1.661 +          case OP_IMPORT:
   1.662 +          {
   1.663 +            unsigned index = READ_UINT32(pc);
   1.664 +            FixedArray *path = code->module()->importPathAt(index);
   1.665 +            fprintf(fp, " ; ");
   1.666 +            for (unsigned i = 0; i < path->length(); i++) {
   1.667 +                fprintf(fp, "%s", String::cast(path->at(i))->chars());
   1.668 +                if (i != path->length() - 1)
   1.669 +                    fprintf(fp, ".");
   1.670 +            }
   1.671 +            break;
   1.672 +          }
   1.673 +
   1.674 +          default:
   1.675 +            // Unknown opcode!
   1.676 +            assert(false);
   1.677 +            return;
   1.678 +        }
   1.679 +
   1.680 +        if (op == OP_STOP)
   1.681 +            break;
   1.682 +
   1.683 +        fprintf(fp, "\n");
   1.684 +        pc += OpcodeInfo[op].length;
   1.685 +        assert(pc < code->start() + code->length());
   1.686 +    }
   1.687 +
   1.688 +    fprintf(fp, "\nSafepoints:\n");
   1.689 +    ByteArray *safepoints = code->safepoints();
   1.690 +    if (safepoints) {
   1.691 +        size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
   1.692 +        for (size_t i = 0; i < nsafepoints; i++) {
   1.693 +            Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
   1.694 +            fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
   1.695 +            unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
   1.696 +            for (unsigned j = 0; j < code->maxStackDepth(); j++) {
   1.697 +                if (bits[j / 8] & (1 << (j % 8)))
   1.698 +                    fprintf(fp, "%d ", j);
   1.699 +            }
   1.700 +            fprintf(fp, "\n");
   1.701 +        }
   1.702 +    }
   1.703 +}