#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Opcode {
Halt = 0x00,
LoadConst = 0x01,
Move = 0x02,
LoadImm = 0x03,
Add = 0x10,
Sub = 0x11,
Mul = 0x12,
Div = 0x13,
Mod = 0x14,
Neg = 0x15,
Eq = 0x18,
Lt = 0x19,
Le = 0x1A,
Jump = 0x20,
Branch = 0x21,
Call = 0x30,
Return = 0x31,
CallNative = 0x32,
Spawn = 0x40,
Yield = 0x41,
Sleep = 0x42,
Exit = 0x43,
SelfPid = 0x44,
Send = 0x50,
Receive = 0x51,
ReceiveTimeout = 0x52,
ReceiveMatch = 0x53,
ReceiveMatchImm = 0x54,
Ask = 0x55,
Trap = 0x60,
Nop = 0x61,
}
impl Opcode {
#[inline]
pub fn from_u8(byte: u8) -> Option<Opcode> {
use Opcode::*;
Some(match byte {
0x00 => Halt,
0x01 => LoadConst,
0x02 => Move,
0x03 => LoadImm,
0x10 => Add,
0x11 => Sub,
0x12 => Mul,
0x13 => Div,
0x14 => Mod,
0x15 => Neg,
0x18 => Eq,
0x19 => Lt,
0x1A => Le,
0x20 => Jump,
0x21 => Branch,
0x30 => Call,
0x31 => Return,
0x32 => CallNative,
0x40 => Spawn,
0x41 => Yield,
0x42 => Sleep,
0x43 => Exit,
0x44 => SelfPid,
0x50 => Send,
0x51 => Receive,
0x52 => ReceiveTimeout,
0x53 => ReceiveMatch,
0x54 => ReceiveMatchImm,
0x55 => Ask,
0x60 => Trap,
0x61 => Nop,
_ => return None,
})
}
#[inline]
pub fn as_u8(self) -> u8 {
self as u8
}
}
impl std::fmt::Display for Opcode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
Opcode::Halt => "Halt",
Opcode::LoadConst => "LoadConst",
Opcode::Move => "Move",
Opcode::LoadImm => "LoadImm",
Opcode::Add => "Add",
Opcode::Sub => "Sub",
Opcode::Mul => "Mul",
Opcode::Div => "Div",
Opcode::Mod => "Mod",
Opcode::Neg => "Neg",
Opcode::Eq => "Eq",
Opcode::Lt => "Lt",
Opcode::Le => "Le",
Opcode::Jump => "Jump",
Opcode::Branch => "Branch",
Opcode::Call => "Call",
Opcode::Return => "Return",
Opcode::CallNative => "CallNative",
Opcode::Spawn => "Spawn",
Opcode::Yield => "Yield",
Opcode::Sleep => "Sleep",
Opcode::Exit => "Exit",
Opcode::SelfPid => "SelfPid",
Opcode::Send => "Send",
Opcode::Receive => "Receive",
Opcode::ReceiveTimeout => "ReceiveTimeout",
Opcode::ReceiveMatch => "ReceiveMatch",
Opcode::ReceiveMatchImm => "ReceiveMatchImm",
Opcode::Ask => "Ask",
Opcode::Trap => "Trap",
Opcode::Nop => "Nop",
};
f.write_str(name)
}
}