jitcraft/Opcodes.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 00:07:40 2012 -0700 (2012-07-01)
changeset 19 c685db6c98e6
parent 17 8aa89dd41e25
child 21 af5d8e985502
permissions -rw-r--r--
Miscellanious opcode cleanup.
     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_O:
    90           case OP_BOUNDS:
    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           case OP_SLOTREF_LIFO:
   102           case OP_ARRAYREF_LIFO:
   103             break;
   104 
   105           case OP_STOP:
   106             fprintf(fp, "\n");
   107             return;
   108 
   109           case OP_INT:
   110             fprintf(fp, "%d", READ_INT32(pc));
   111             break;
   112 
   113           case OP_FLOAT:
   114             fprintf(fp, "%f", READ_FLOAT(pc));
   115             break;
   116 
   117           case OP_JL:
   118           case OP_JLE:
   119           case OP_JG:
   120           case OP_JGE:
   121           case OP_JEQ:
   122           case OP_JNE:
   123           case OP_JT:
   124           case OP_JF:
   125           case OP_JUMP:
   126             fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
   127             break;
   128 
   129           case OP_NEWARRAY_LIFO:
   130           {
   131             // :TODO: use Local
   132             ArrayMap *map = ArrayMap::cast(code->map(READ_UINT32(pc)));
   133             fprintf(fp, "%s:", map->type()->innermost()->tag()
   134                                ? map->type()->innermost()->tag()
   135                                : "_");
   136             for (; map; map = map->contained()) {
   137                 Type *type = map->type();
   138                 fprintf(fp, "[%d]", type->elements());
   139             }
   140             break;
   141           }
   142 
   143           case OP_GETLOCAL:
   144           case OP_SETLOCAL:
   145           case OP_GETARG:
   146           case OP_SETARG:
   147           case OP_SAVELIFO:
   148           case OP_UNWINDLIFO:
   149           case OP_CALLGLOBAL:
   150           case OP_VARREF_LIFO:
   151           case OP_ARGREF_LIFO:
   152             fprintf(fp, "%u", READ_UINT32(pc));
   153             break;
   154 
   155           default:
   156             // Unknown opcode!
   157             assert(false);
   158             return;
   159         }
   160 
   161         fprintf(fp, "\n");
   162         pc += OpcodeInfo[op].length;
   163         assert(pc < code->start() + code->length());
   164     }
   165 }