src/Opcodes.cpp
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 20:12:20 2012 -0700 (2012-08-04)
changeset 148 c0740249963c
parent 147 9c544823d090
child 152 153d0cafc1fd
permissions -rw-r--r--
All but interpreter support for getting and setting struct fields.
     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, number, 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, unsigned maxStackDepth, Handle<Module> module,
    43 	      Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
    44           unsigned lineNumber)
    45 {
    46     Local<Code> code(zone, Code::cast(zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old)));
    47     if (!code)
    48         return NULL;
    49 
    50     code->bytecode_ = bytecode;
    51     code->locals_ = locals;
    52     code->objects_ = objects;
    53     code->maxStackDepth_ = maxStackDepth;
    54     code->module_ = module;
    55     code->safepoints_ = safepoints;
    56     code->sourcemap_ = sourcemap;
    57     code->lineNumber_ = lineNumber;
    58     return code;
    59 }
    60 
    61 static int
    62 CompareSafepoints(const void *a, const void *b)
    63 {
    64     const unsigned *sa = reinterpret_cast<const unsigned *>(a);
    65     const unsigned *sb = reinterpret_cast<const unsigned *>(b);
    66     if (*sa > *sb)
    67         return 1;
    68     if (*sa == *sb)
    69         return 0;
    70     return -1;
    71 }
    72 
    73 uint8 *
    74 Code::safepointAt(unsigned pcOffset)
    75 {
    76     // Only specific opcodes may have safepoints. Asking for a safepoint at
    77     // any other position indicates a failure to mark that position as a
    78     // safepoint, which is a bug.
    79 #ifndef NDEBUG
    80     Opcode op = Opcode(*(start() + pcOffset));
    81     assert(op == OP_CALL || op == OP_NEWARRAY || op == OP_VARREF_LIFO ||
    82            op == OP_ARGREF_LIFO || op == OP_SLOTREF_LIFO || op == OP_ARRAYREF_LIFO ||
    83            op == OP_ARRAYCOPY);
    84 #endif
    85 
    86     if (!safepoints_)
    87         return NULL;
    88 
    89     size_t bytesPerSafepoint = BytesPerSafepoint(maxStackDepth());
    90     assert(!(safepoints_->length() % bytesPerSafepoint));
    91 
    92     size_t count = safepoints_->length() / bytesPerSafepoint;
    93 
    94     void *result = bsearch(&pcOffset,
    95                            safepoints_->raw(),
    96                            count,
    97                            bytesPerSafepoint,
    98                            CompareSafepoints);
    99 
   100     if (!result)
   101         return NULL;
   102 
   103     assert(*reinterpret_cast<unsigned *>(result) == pcOffset);
   104 
   105     return Address(result) + sizeof(unsigned);
   106 }
   107 
   108 SourcePosition
   109 Code::pcToSource(unsigned pc)
   110 {
   111     assert(pc < bytecode_->length());
   112 
   113     CompactBufferReader reader(sourcemap_->raw(), sourcemap_->raw() + sourcemap_->length());
   114 
   115     unsigned line = 1;
   116     unsigned col = 0;
   117     unsigned curPc = 0;
   118     while (!reader.done()) {
   119         curPc += reader.readUint32();
   120         if (curPc > pc)
   121             break;
   122 
   123         line = reader.readUint32();
   124         col = reader.readUint32();
   125     }
   126 
   127     return SourcePosition(lineNumber_ + line, col);
   128 }
   129 
   130 // All jumps must be the same length.
   131 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
   132 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
   133 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
   134 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
   135 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
   136 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
   137 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
   138 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
   139 
   140 void
   141 ke::Disassemble(Code *code, FILE *fp)
   142 {
   143     bytecode *pc = code->start();
   144     for (;;) {
   145         Opcode op = Opcode(*pc);
   146 
   147         fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
   148 
   149         switch (op) {
   150           case OP_NOP:
   151           case OP_RETURN:
   152           case OP_RETURNVOID:
   153           case OP_ADD:
   154           case OP_SUB:
   155           case OP_MUL:
   156           case OP_DIV:
   157           case OP_ADD_F:
   158           case OP_SUB_F:
   159           case OP_MUL_F:
   160           case OP_DIV_F:
   161           case OP_CVT_I2F:
   162           case OP_POP:
   163           case OP_GETARRAY_I:
   164           case OP_SETARRAY_I:
   165           case OP_GETARRAY_F:
   166           case OP_SETARRAY_F:
   167           case OP_GETARRAY_C:
   168           case OP_SETARRAY_C:
   169           case OP_GETARRAY_O:
   170           case OP_BOUNDS:
   171           case OP_SHL:
   172           case OP_SHR:
   173           case OP_USHR:
   174           case OP_BITAND:
   175           case OP_BITOR:
   176           case OP_BITXOR:
   177           case OP_GETREF_I:
   178           case OP_SETREF_I:
   179           case OP_GETREF_F:
   180           case OP_SETREF_F:
   181           case OP_SLOTREF_LIFO:
   182           case OP_ARRAYREF_LIFO:
   183           case OP_ARRAYCOPY:
   184           case OP_DUP:
   185           case OP_DUP2:
   186           case OP_SWAP:
   187           case OP_ROLL3:
   188           case OP_PICK2:
   189           case OP_PICK3:
   190           case OP_CALL:
   191           case OP_MODULE:
   192           case OP_LT:
   193           case OP_LE:
   194           case OP_GE:
   195           case OP_GT:
   196           case OP_EQ:
   197           case OP_NE:
   198           case OP_LT_F:
   199           case OP_LE_F:
   200           case OP_GE_F:
   201           case OP_GT_F:
   202           case OP_EQ_F:
   203           case OP_NE_F:
   204           case OP_BITNOT:
   205           case OP_NOT:
   206           case OP_NOT_F:
   207           case OP_NEG:
   208           case OP_NEG_F:
   209           case OP_MOD:
   210           case OP_BITS_I2F:
   211           case OP_BITS_F2I:
   212           case OP_TRUE:
   213           case OP_FALSE:
   214             break;
   215 
   216           case OP_STOP:
   217             fprintf(fp, "\n");
   218             break;
   219 
   220           case OP_INT:
   221             fprintf(fp, "%d", READ_INT32(pc));
   222             break;
   223 
   224           case OP_FLOAT:
   225             fprintf(fp, "%f", READ_FLOAT(pc));
   226             break;
   227 
   228           case OP_JL:
   229           case OP_JLE:
   230           case OP_JG:
   231           case OP_JGE:
   232           case OP_JEQ:
   233           case OP_JNE:
   234           case OP_JT:
   235           case OP_JF:
   236           case OP_JL_F:
   237           case OP_JLE_F:
   238           case OP_JG_F:
   239           case OP_JGE_F:
   240           case OP_JEQ_F:
   241           case OP_JNE_F:
   242           case OP_JT_F:
   243           case OP_JF_F:
   244           case OP_JUMP:
   245             fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   246             break;
   247 
   248           case OP_NEWARRAY:
   249           {
   250             Heap::Tenure tenure = (Heap::Tenure)pc[1];
   251             ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc + 1)));
   252             for (; map; map = map->contained()) {
   253                 Type *type = map->type();
   254                 fprintf(fp, "[%d]", type->elements());
   255             }
   256             if (tenure == Heap::Tenure_Stack)
   257                 fprintf(fp, " ; lifo");
   258             else if (tenure == Heap::Tenure_Old)
   259                 fprintf(fp, " ; pretenured");
   260             break;
   261           }
   262 
   263           case OP_NEWSTRUCT:
   264           {
   265             Heap::Tenure tenure = (Heap::Tenure)pc[1];
   266             StructMap *map = StructMap::cast(code->map(READ_UINT32(pc + 1)));
   267             fprintf(fp, "%s", map->type()->name()->chars());
   268             if (tenure == Heap::Tenure_Stack)
   269                 fprintf(fp, " ; lifo");
   270             else if (tenure == Heap::Tenure_Old)
   271                 fprintf(fp, " ; pretenured");
   272             break;
   273           }
   274 
   275           case OP_GETLOCAL:
   276           case OP_SETLOCAL:
   277           case OP_GETARG:
   278           case OP_SETARG:
   279           case OP_SAVELIFO:
   280           case OP_UNWINDLIFO:
   281           case OP_VARREF_LIFO:
   282           case OP_ARGREF_LIFO:
   283           case OP_OBJECT:
   284           case OP_GETMODULE_FN:
   285           case OP_GETGLOBAL_I:
   286           case OP_GETGLOBAL_F:
   287           case OP_GETGLOBAL_O:
   288           case OP_SETGLOBAL_I:
   289           case OP_SETGLOBAL_F:
   290           case OP_SETGLOBAL_O:
   291           case OP_GETFIELD:
   292           case OP_SETFIELD:
   293             fprintf(fp, "%u", READ_UINT32(pc));
   294             break;
   295 
   296           case OP_CALLNATIVE:
   297             fprintf(fp, "%u %u", READ_UINT32(pc), READ_UINT32(pc + 4));
   298             break;
   299 
   300           default:
   301             // Unknown opcode!
   302             assert(false);
   303             return;
   304         }
   305 
   306         if (op == OP_STOP)
   307             break;
   308 
   309         fprintf(fp, "\n");
   310         pc += OpcodeInfo[op].length;
   311         assert(pc < code->start() + code->length());
   312     }
   313 
   314     fprintf(fp, "\nSafepoints:\n");
   315     ByteArray *safepoints = code->safepoints();
   316     if (safepoints) {
   317         size_t nsafepoints = safepoints->length() / Code::BytesPerSafepoint(code->maxStackDepth());
   318         for (size_t i = 0; i < nsafepoints; i++) {
   319             Address address = safepoints->raw() + Code::BytesPerSafepoint(code->maxStackDepth()) * i;
   320             fprintf(fp, "%4d: ", *reinterpret_cast<unsigned *>(address));
   321             unsigned char *bits = (unsigned char *)(address + sizeof(unsigned));
   322             for (unsigned j = 0; j < code->maxStackDepth(); j++) {
   323                 if (bits[j / 8] & (1 << (j % 8)))
   324                     fprintf(fp, "%d ", j);
   325             }
   326             fprintf(fp, "\n");
   327         }
   328     }
   329 }