src/Opcodes.cpp
author David Anderson <danderson@mozilla.com>
Thu Jul 26 00:16:53 2012 -0700 (2012-07-26)
changeset 100 3278e19f064d
parent 93 23189d937087
child 142 a000bafece09
permissions -rw-r--r--
Push build.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <stdlib.h>
    20 #include "Opcodes.h"
    21 #include "Utility.h"
    22 #include "Code.h"
    23 #include "Zone.h"
    24 #include "Array.h"
    25 #include "Strings.h"
    26 #include "CompactBuffer.h"
    27 #include "Heap-inl.h"
    28 
    29 using namespace ke;
    30 
    31 OpInfo ke::OpcodeInfo[] =
    32 {
    33 #define OPDEF(name, number, text, length, uses, defs) \
    34     {text, length, uses, defs},
    35 #include "Opcodes.tbl"
    36 #undef OPDEF
    37 };
    38 
    39 Code *
    40 Code::New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    41           Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module,
    42 	      Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
    43           unsigned lineNumber)
    44 {
    45     Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
    46     if (!code)
    47         return NULL;
    48 
    49     code->bytecode_ = bytecode;
    50     code->locals_ = locals;
    51     code->objects_ = objects;
    52     code->maxStackDepth_ = maxStackDepth;
    53     code->module_ = module;
    54     code->safepoints_ = safepoints;
    55     code->sourcemap_ = sourcemap;
    56     code->lineNumber_ = lineNumber;
    57     return code;
    58 }
    59 
    60 static int
    61 CompareSafepoints(const void *a, const void *b)
    62 {
    63     const unsigned *sa = reinterpret_cast<const unsigned *>(a);
    64     const unsigned *sb = reinterpret_cast<const unsigned *>(b);
    65     if (*sa > *sb)
    66         return 1;
    67     if (*sa == *sb)
    68         return 0;
    69     return -1;
    70 }
    71 
    72 uint8 *
    73 Code::safepointAt(unsigned pcOffset)
    74 {
    75     // Only specific opcodes may have safepoints. Asking for a safepoint at
    76     // any other position indicates a failure to mark that position as a
    77     // safepoint, which is a bug.
    78 #ifndef NDEBUG
    79     Opcode op = Opcode(*(start() + pcOffset));
    80     assert(op == OP_CALL || op == OP_NEWARRAY || op == OP_VARREF_LIFO ||
    81            op == OP_ARGREF_LIFO || op == OP_SLOTREF_LIFO || op == OP_ARRAYREF_LIFO ||
    82            op == OP_ARRAYCOPY);
    83 #endif
    84 
    85     if (!safepoints_)
    86         return NULL;
    87 
    88     size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
    89     assert(!(safepoints_->length() % bytesPerSafepoint));
    90 
    91     size_t count = safepoints_->length() / bytesPerSafepoint;
    92 
    93     void *result = bsearch(&pcOffset,
    94                            safepoints_->raw(),
    95                            count,
    96                            bytesPerSafepoint,
    97                            CompareSafepoints);
    98 
    99     if (!result)
   100         return NULL;
   101 
   102     assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
   103 
   104     return Address(result) + sizeof(unsigned);
   105 }
   106 
   107 SourcePosition
   108 Code::pcToSource(unsigned pc)
   109 {
   110     assert(pc < bytecode_->length());
   111 
   112     CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
   113 
   114     unsigned line = 1;
   115     unsigned col = 0;
   116     unsigned curPc = 0;
   117     while (!reader.done()) {
   118         curPc += reader.readUint32();
   119         if (curPc > pc)
   120             break;
   121 
   122         line = reader.readUint32();
   123         col = reader.readUint32();
   124     }
   125 
   126     return SourcePosition(lineNumber_ + line, col);
   127 }
   128 
   129 // All jumps must be the same length.
   130 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
   131 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
   132 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
   133 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
   134 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
   135 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
   136 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
   137 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
   138 
   139 void
   140 ke::Disassemble(Code *code, FILE *fp)
   141 {
   142     bytecode *pc = code->start();
   143     for (;;) {
   144         Opcode op = Opcode(*pc);
   145 
   146         fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
   147 
   148         switch (op) {
   149           case OP_NOP:
   150           case OP_RETURN:
   151           case OP_RETURNVOID:
   152           case OP_ADD:
   153           case OP_SUB:
   154           case OP_MUL:
   155           case OP_DIV:
   156           case OP_ADD_F:
   157           case OP_SUB_F:
   158           case OP_MUL_F:
   159           case OP_DIV_F:
   160           case OP_CVT_I2F:
   161           case OP_POP:
   162           case OP_GETARRAY_I:
   163           case OP_SETARRAY_I:
   164           case OP_GETARRAY_F:
   165           case OP_SETARRAY_F:
   166           case OP_GETARRAY_C:
   167           case OP_SETARRAY_C:
   168           case OP_GETARRAY_O:
   169           case OP_BOUNDS:
   170           case OP_SHL:
   171           case OP_SHR:
   172           case OP_USHR:
   173           case OP_BITAND:
   174           case OP_BITOR:
   175           case OP_BITXOR:
   176           case OP_GETREF_I:
   177           case OP_SETREF_I:
   178           case OP_GETREF_F:
   179           case OP_SETREF_F:
   180           case OP_SLOTREF_LIFO:
   181           case OP_ARRAYREF_LIFO:
   182           case OP_ARRAYCOPY:
   183           case OP_DUP:
   184           case OP_DUP2:
   185           case OP_SWAP:
   186           case OP_ROLL3:
   187           case OP_PICK2:
   188           case OP_PICK3:
   189           case OP_CALL:
   190           case OP_MODULE:
   191           case OP_LT:
   192           case OP_LE:
   193           case OP_GE:
   194           case OP_GT:
   195           case OP_EQ:
   196           case OP_NE:
   197           case OP_LT_F:
   198           case OP_LE_F:
   199           case OP_GE_F:
   200           case OP_GT_F:
   201           case OP_EQ_F:
   202           case OP_NE_F:
   203           case OP_BITNOT:
   204           case OP_NOT:
   205           case OP_NOT_F:
   206           case OP_NEG:
   207           case OP_NEG_F:
   208           case OP_MOD:
   209             break;
   210 
   211           case OP_STOP:
   212             fprintf(fp, "\n");
   213             break;
   214 
   215           case OP_INT:
   216             fprintf(fp, "%d", READ_INT32(pc));
   217             break;
   218 
   219           case OP_FLOAT:
   220             fprintf(fp, "%f", READ_FLOAT(pc));
   221             break;
   222 
   223           case OP_JL:
   224           case OP_JLE:
   225           case OP_JG:
   226           case OP_JGE:
   227           case OP_JEQ:
   228           case OP_JNE:
   229           case OP_JT:
   230           case OP_JF:
   231           case OP_JL_F:
   232           case OP_JLE_F:
   233           case OP_JG_F:
   234           case OP_JGE_F:
   235           case OP_JEQ_F:
   236           case OP_JNE_F:
   237           case OP_JT_F:
   238           case OP_JF_F:
   239           case OP_JUMP:
   240             fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   241             break;
   242 
   243           case OP_NEWARRAY:
   244           {
   245             Heap::Tenure tenure = (Heap::Tenure)pc[1];
   246             ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
   247             fprintf(fp, "%s:", map->type()->innermost()->tag()
   248                                ? map->type()->innermost()->tag()->chars()
   249                                : "_");
   250             for (; map; map = map->contained()) {
   251                 Type *type = map->type();
   252                 fprintf(fp, "[%d]", type->elements());
   253             }
   254             if (tenure == Heap::Tenure_Stack)
   255                 fprintf(fp, " ; lifo");
   256             else if (tenure == Heap::Tenure_Old)
   257                 fprintf(fp, " ; pretenured");
   258             break;
   259           }
   260 
   261           case OP_GETLOCAL:
   262           case OP_SETLOCAL:
   263           case OP_GETARG:
   264           case OP_SETARG:
   265           case OP_SAVELIFO:
   266           case OP_UNWINDLIFO:
   267           case OP_VARREF_LIFO:
   268           case OP_ARGREF_LIFO:
   269           case OP_OBJECT:
   270           case OP_GETMODULE_FN:
   271           case OP_GETGLOBAL_I:
   272           case OP_GETGLOBAL_F:
   273           case OP_GETGLOBAL_O:
   274           case OP_SETGLOBAL_I:
   275           case OP_SETGLOBAL_F:
   276           case OP_SETGLOBAL_O:
   277             fprintf(fp, "%u", READ_UINT32(pc));
   278             break;
   279 
   280           case OP_CALLNATIVE:
   281             fprintf(fp, "%u %u", READ_UINT32(pc), READ_UINT32(pc + 4));
   282             break;
   283 
   284           default:
   285             // Unknown opcode!
   286             assert(false);
   287             return;
   288         }
   289 
   290         if (op == OP_STOP)
   291             break;
   292 
   293         fprintf(fp, "\n");
   294         pc += OpcodeInfo[op].length;
   295         assert(pc < code->start() + code->length());
   296     }
   297 
   298     fprintf(fp, "\nSafepoints:\n");
   299     ByteArray *safepoints = code->safepoints();
   300     if (safepoints) {
   301         size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
   302         for (size_t i = 0; i < nsafepoints; i++) {
   303             Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
   304             fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
   305             unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
   306             for (unsigned j = 0; j < code->maxStackDepth(); j++) {
   307                 if (bits[j / 8] & (1 << (j % 8)))
   308                     fprintf(fp, "%d ", j);
   309             }
   310             fprintf(fp, "\n");
   311         }
   312     }
   313 }