/*
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#include "Opcodes.h"
#include "Utility.h"
#include "Code.h"

using namespace jc;

OpInfo jc::OpcodeInfo[] =
{
#define OPDEF(name, number, text, length, uses, defs) \
    {text, length, uses, defs},
#include "Opcodes.tbl"
#undef OPDEF
};

// All jumps must be the same length.
STATIC_ASSERT(OP_JL_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JLE_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JG_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JGE_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JEQ_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JNE_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JT_LENGTH == OP_JUMP_LENGTH);
STATIC_ASSERT(OP_JF_LENGTH == OP_JUMP_LENGTH);

void
jc::Disassemble(Code *code, FILE *fp)
{
    bytecode *pc = code->start();
    for (;;) {
        Opcode op = Opcode(*pc);

        fprintf(fp, "%-4d %s ", int(pc - code->start()), OpcodeInfo[op].name);

        switch (op) {
          case OP_NOP:
          case OP_RETURN:
          case OP_ADD:
          case OP_SUB:
          case OP_MUL:
          case OP_DIV:
          case OP_ADD_F:
          case OP_SUB_F:
          case OP_MUL_F:
          case OP_DIV_F:
          case OP_CVT_I2F:
          case OP_POP:
          case OP_LD_STA_I:
          case OP_LD_STA_P:
          case OP_ST_STA_I:
          case OP_BOUNDS_STA:
            break;

          case OP_STOP:
            fprintf(fp, "\n");
            return;

          case OP_INT:
            fprintf(fp, "%d", READ_INT32(pc));
            break;

          case OP_FLOAT:
            fprintf(fp, "%f", READ_FLOAT(pc));
            break;

          case OP_JL:
          case OP_JLE:
          case OP_JG:
          case OP_JGE:
          case OP_JEQ:
          case OP_JNE:
          case OP_JT:
          case OP_JF:
          case OP_JUMP:
            fprintf(fp, "%u (%+d)", unsigned((pc + READ_INT32(pc)) - code->start()), READ_INT32(pc));
            break;

          case OP_STACKARRAY:
          {
            unsigned width = pc[1];
            unsigned dims = pc[2];

            for (unsigned i = 0; i < dims; i++)
                fprintf(fp, "[%u]", READ_UINT32(pc + 2 + (i * sizeof(unsigned))));
            fprintf(fp, " ; (width = %s)\n", (width == 1 ? "char" : "cell"));
            pc += 3 + dims * sizeof(unsigned);
            continue;
          }

          case OP_GETLOCAL:
          case OP_SETLOCAL:
          case OP_GETARG:
          case OP_SETARG:
          case OP_UNWIND:
            fprintf(fp, "%u", READ_UINT32(pc));
            break;

          case OP_CALLGLOBAL:
            fprintf(fp, "%u", READ_UINT32(pc));
            break;

          default:
            // Unknown opcode!
            assert(false);
            return;
        }

        fprintf(fp, "\n");
        pc += OpcodeInfo[op].length;
    }
}
