1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/jitcraft/Opcodes.cpp Mon Jun 25 02:57:28 2012 -0700
1.3 @@ -0,0 +1,117 @@
1.4 +/*
1.5 + * Copyright (C) 2012 David Anderson
1.6 + *
1.7 + * This file is part of JITCraft.
1.8 + *
1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#include "Opcodes.h"
1.22 +#include "Utility.h"
1.23 +#include "Code.h"
1.24 +
1.25 +using namespace jc;
1.26 +
1.27 +OpInfo jc::OpcodeInfo[] =
1.28 +{
1.29 +#define OPDEF(name, number, text, length, uses, defs) \
1.30 + {text, length, uses, defs},
1.31 +#include "Opcodes.tbl"
1.32 +#undef OPDEF
1.33 +};
1.34 +
1.35 +// All jumps must be the same length.
1.36 +STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
1.37 +STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
1.38 +STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
1.39 +STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
1.40 +STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
1.41 +STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
1.42 +STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
1.43 +STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);
1.44 +
1.45 +void
1.46 +jc::Disassemble(Code *code, FILE *fp)
1.47 +{
1.48 + bytecode *pc = code->start();
1.49 + for (;;) {
1.50 + Opcode op = Opcode(*pc);
1.51 +
1.52 + fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);
1.53 +
1.54 + switch (op) {
1.55 + case OP_NOP:
1.56 + case OP_RETURN:
1.57 + case OP_ADD:
1.58 + case OP_SUB:
1.59 + case OP_MUL:
1.60 + case OP_DIV:
1.61 + case OP_POP:
1.62 + case OP_LD_STA_I:
1.63 + case OP_LD_STA_P:
1.64 + case OP_ST_STA_I:
1.65 + case OP_BOUNDS_STA:
1.66 + break;
1.67 +
1.68 + case OP_STOP:
1.69 + fprintf(fp, "\n");
1.70 + return;
1.71 +
1.72 + case OP_INT:
1.73 + fprintf(fp, "%d", READ_INT32(pc));
1.74 + break;
1.75 +
1.76 + case OP_JL:
1.77 + case OP_JLE:
1.78 + case OP_JG:
1.79 + case OP_JGE:
1.80 + case OP_JEQ:
1.81 + case OP_JNE:
1.82 + case OP_JT:
1.83 + case OP_JF:
1.84 + case OP_JUMP:
1.85 + fprintf(fp, "%d (%u)", READ_INT32(pc), unsigned((pc + READ_INT32(pc)) - code->start()));
1.86 + break;
1.87 +
1.88 + case OP_STACKARRAY:
1.89 + {
1.90 + unsigned width = pc[1];
1.91 + unsigned dims = pc[2];
1.92 +
1.93 + for (unsigned i = 0; i < dims; i++)
1.94 + fprintf(fp, "[%u]", READ_UINT32(pc + 2 + (i * sizeof(unsigned))));
1.95 + fprintf(fp, " ; (width = %s)\n", (width == 1 ? "char" : "cell"));
1.96 + pc += 3 + dims * sizeof(unsigned);
1.97 + continue;
1.98 + }
1.99 +
1.100 + case OP_GETLOCAL:
1.101 + case OP_SETLOCAL:
1.102 + case OP_GETARG:
1.103 + case OP_SETARG:
1.104 + fprintf(fp, "%u", READ_UINT32(pc));
1.105 + break;
1.106 +
1.107 + case OP_CALLGLOBAL:
1.108 + fprintf(fp, "%u", READ_UINT32(pc));
1.109 + break;
1.110 +
1.111 + default:
1.112 + // Unknown opcode!
1.113 + assert(false);
1.114 + return;
1.115 + }
1.116 +
1.117 + fprintf(fp, "\n");
1.118 + pc += OpcodeInfo[op].length;
1.119 + }
1.120 +}