/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn 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.
 * 
 * SourcePawn 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
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_opcodes_h_
#define _include_opcodes_h_

#include <assert.h>
#include <stdio.h>

namespace ke {
    
struct SourcePosition
{
    int line;
    int col;

    SourcePosition()
      : line(-1),
        col(-1)
    {
    }

    SourcePosition(unsigned line, unsigned col)
      : line(line),
        col(col)
    {
    }
};

enum Opcode
{
#define OPDEF(name, text, length, uses, defs)   \
    name,
#include "Opcodes.tbl"
#undef OPDEF
    OPCODES_TOTAL
};

#define OPDEF(name, text, length, uses, defs)   \
static const unsigned name##_LENGTH = length;
#include "Opcodes.tbl"
#undef OPDEF

struct OpInfo
{
    const char *name;
    int length;
    int nuses;
    int ndefs;
};

typedef unsigned char bytecode;

extern OpInfo OpcodeInfo[];

static const unsigned MAX_TABLESWITCH_ENTRIES = 256;

static inline unsigned
TableSwitchEntries(int low, int high)
{
    return unsigned(high - low) + 1;
}

static inline int
READ_INT32(bytecode *pc)
{
    return *reinterpret_cast<int *>(pc + 1);
}
static inline unsigned
READ_UINT32(bytecode *pc)
{
    return *reinterpret_cast<unsigned *>(pc + 1);
}
static inline float
READ_FLOAT(bytecode *pc)
{
    return *reinterpret_cast<float *>(pc + 1);
}

static inline int
READ_TABLESWITCH_LOW(bytecode *pc)
{
    return READ_INT32(pc);
}

static inline int
READ_TABLESWITCH_HIGH(bytecode *pc)
{
    return READ_INT32(pc + sizeof(int));
}

class Code;

void Disassemble(Code *code, FILE *fp);

}

#endif // _include_opcodes_h_
