valeri/src/opcode.cpp

93 lines
2.7 KiB
C++
Raw Normal View History

2024-08-11 01:25:44 +00:00
#include "opcode.hpp"
#include "die.hpp"
struct op_t {
const char* opname;
OpcodeType optype;
};
static const op_t opcode_tbl[] = {};
op_t get_op(Oc op) {
switch (op) {
case Oc::Unknown:
return op_t{0, OpcodeType::Reg0};
case Oc::Add:
return op_t{"add", OpcodeType::Reg3};
case Oc::Sub:
return op_t{"sub", OpcodeType::Reg3};
case Oc::Mul:
return op_t{"mul", OpcodeType::Reg3};
case Oc::Div:
return op_t{"div", OpcodeType::Reg3};
case Oc::Mod:
return op_t{"mod", OpcodeType::Reg3};
case Oc::Mov:
return op_t{"mov", OpcodeType::Reg2};
case Oc::LoadConst:
return op_t{"loadc", OpcodeType::Reg1I};
case Oc::LoadStack:
return op_t{"lfi", OpcodeType::Reg1I};
case Oc::Jump:
return op_t{"jmp", OpcodeType::Reg0I};
case Oc::JumpEqual:
return op_t{"jeq", OpcodeType::Reg2I};
case Oc::JumpLess:
return op_t{"jlt", OpcodeType::Reg2I};
case Oc::JumpLessEqual:
return op_t{"jle", OpcodeType::Reg2I};
case Oc::JumpNotEqual:
return op_t{"jne", OpcodeType::Reg2I};
case Oc::Call:
return op_t{"call", OpcodeType::Reg1I};
case Oc::TailCall:
return op_t{"tailcall", OpcodeType::Reg1I};
case Oc::Ret:
return op_t{"ret", OpcodeType::Reg1};
case Oc::RetNil:
return op_t{"retnil", OpcodeType::Reg0};
case Oc::ShiftRight:
return op_t{"sri", OpcodeType::Reg2I};
case Oc::ShiftLeft:
return op_t{"sli", OpcodeType::Reg2I};
case Oc::BitwiseAnd:
return op_t{"band", OpcodeType::Reg3};
case Oc::BitwiseOr:
return op_t{"bor", OpcodeType::Reg3};
case Oc::BitwiseXor:
return op_t{"bxor", OpcodeType::Reg3};
case Oc::BitwiseNeg:
return op_t{"bneg", OpcodeType::Reg2};
case Oc::MakeArray:
return op_t{"make-array", OpcodeType::Reg1I};
case Oc::ArrayLoad:
return op_t{"al", OpcodeType::Reg3};
case Oc::ArrayStore:
return op_t{"as", OpcodeType::Reg4};
case Oc::Push:
return op_t{"push", OpcodeType::Reg1};
case Oc::Pop:
return op_t{"pop", OpcodeType::Reg1};
case Oc::StackReserve:
return op_t{"sr", OpcodeType::Reg0I};
case Oc::AssertEqual:
return op_t{"assert-equal", OpcodeType::Reg2};
case Oc::MakeDict:
return op_t{"make-dict", OpcodeType::Reg1};
case Oc::SetJump:
return op_t{"setjump", OpcodeType::Reg1I};
case Oc::SetGlobal:
return op_t{"setglobal", OpcodeType::Reg2};
case Oc::GetGlobal:
return op_t{"getglobal", OpcodeType::Reg2};
default:
die("Unknown opcode\n");
}
}
const char* opcode_name(Oc op) { return get_op(op).opname; }
OpcodeType opcode_type(Oc op) { return get_op(op).optype; }