jitcraft/Opcodes.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:31:14 2012 -0700 (2012-06-30)
changeset 14 0784ac5cc347
parent 13 627b6b94e774
child 16 23627f8bc421
permissions -rw-r--r--
Added support for non-array references.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include "Opcodes.h"
    19 #include "Utility.h"
    20 #include "Code.h"
    21 #include "Zone.h"
    22 #include "Array.h"
    23 #include "Heap-inl.h"
    24 
    25 using namespace jc;
    26 
    27 OpInfo jc::OpcodeInfo[] =
    28 {
    29 #define OPDEF(name, number, text, length, uses, defs) \
    30     {text, length, uses, defs},
    31 #include "Opcodes.tbl"
    32 #undef OPDEF
    33 };
    34 
    35 Code *
    36 Code::New(Zone *zone, ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    37           FixedArray *maps, unsigned maxStackDepth, Module *module)
    38 {
    39     Object *obj = zone->allocate(MapKind_Code, sizeof(Code), Heap::Tenure_Old);
    40     if (!obj)
    41         return NULL;
    42 
    43     Code *code = Code::cast(obj);
    44     code->bytecode_ = bytecode;
    45     code->locals_ = locals;
    46     code->args_ = args;
    47     code->maps_ = maps;
    48     code->maxStackDepth_ = maxStackDepth;
    49     code->module_ = module;
    50     return code;
    51 }
    52 
    53 // All jumps must be the same length.
    54 STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
    55 STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
    56 STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
    57 STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
    58 STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
    59 STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
    60 STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
    61 STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
    62 
    63 void
    64 jc::Disassemble(Code *code, FILE *fp)
    65 {
    66     bytecode *pc = code->start();
    67     for (;;) {
    68         Opcode op = Opcode(*pc);
    69 
    70         fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
    71 
    72         switch (op) {
    73           case OP_NOP:
    74           case OP_RETURN:
    75           case OP_ADD:
    76           case OP_SUB:
    77           case OP_MUL:
    78           case OP_DIV:
    79           case OP_ADD_F:
    80           case OP_SUB_F:
    81           case OP_MUL_F:
    82           case OP_DIV_F:
    83           case OP_CVT_I2F:
    84           case OP_POP:
    85           case OP_GETARRAY_I:
    86           case OP_SETARRAY_I:
    87           case OP_GETARRAY_F:
    88           case OP_SETARRAY_F:
    89           case OP_GETARRAY_P:
    90           case OP_BOUNDS_STA:
    91           case OP_SHL:
    92           case OP_SHR:
    93           case OP_USHR:
    94           case OP_BITAND:
    95           case OP_BITOR:
    96           case OP_BITXOR:
    97           case OP_GETREF_I:
    98           case OP_SETREF_I:
    99           case OP_GETREF_F:
   100           case OP_SETREF_F:
   101             break;
   102 
   103           case OP_STOP:
   104             fprintf(fp, "\n");
   105             return;
   106 
   107           case OP_INT:
   108             fprintf(fp, "%d", READ_INT32(pc));
   109             break;
   110 
   111           case OP_FLOAT:
   112             fprintf(fp, "%f", READ_FLOAT(pc));
   113             break;
   114 
   115           case OP_JL:
   116           case OP_JLE:
   117           case OP_JG:
   118           case OP_JGE:
   119           case OP_JEQ:
   120           case OP_JNE:
   121           case OP_JT:
   122           case OP_JF:
   123           case OP_JUMP:
   124             fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   125             break;
   126 
   127           case OP_LIFOARRAY:
   128           {
   129             // :TODO: use Local
   130             ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc)));
   131             fprintf(fp, "%s:", map->type()->innermost()->tag()
   132                                ? map->type()->innermost()->tag()
   133                                : "_");
   134             for (; map; map = map->contained()) {
   135                 Type *type = map->type();
   136                 fprintf(fp, "[%d]", type->elements());
   137             }
   138             break;
   139           }
   140 
   141           case OP_GETLOCAL:
   142           case OP_SETLOCAL:
   143           case OP_GETARG:
   144           case OP_SETARG:
   145           case OP_SAVELIFO:
   146           case OP_UNWINDLIFO:
   147           case OP_CALLGLOBAL:
   148           case OP_VARREF_LIFO:
   149           case OP_ARGREF_LIFO:
   150             fprintf(fp, "%u", READ_UINT32(pc));
   151             break;
   152 
   153           default:
   154             // Unknown opcode!
   155             assert(false);
   156             return;
   157         }
   158 
   159         fprintf(fp, "\n");
   160         pc += OpcodeInfo[op].length;
   161         assert(pc < code->start() + code->length());
   162     }
   163 }