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