From f387bdd3964a8392e80ae24f7dc671b37e27f60b Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Tue, 3 May 2022 16:54:14 +1000 Subject: [PATCH] Support decoding all instructions --- src/vstc/virtual_machine/bytecode_decoder.rs | 19 +------ src/vstc/virtual_machine/instruction.rs | 52 ++++++++++++++++++++ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/vstc/virtual_machine/bytecode_decoder.rs b/src/vstc/virtual_machine/bytecode_decoder.rs index 5f7f8b9..7f6eee5 100644 --- a/src/vstc/virtual_machine/bytecode_decoder.rs +++ b/src/vstc/virtual_machine/bytecode_decoder.rs @@ -193,23 +193,6 @@ impl BytecodeDecoder { } pub fn decode_instruction(&mut self) -> Instruction { - use Instruction::*; - - return match self.decode_byte() { - 0x00 => End, - 0x01 => Mov, - 0x02 => OpInc, - 0x04 => OpPlus, - 0x05 => OpMinus, - 0x06 => OpMul, - 0x08 => OpMod, - 0x0d => OpTripleNe, - 0x11 => OpLess, - 0x21 => Call, - 0x27 => Jmp, - 0x28 => JmpIf, - - _ => std::panic!("Not implemented"), - } + return Instruction::from_byte(self.decode_byte()); } } diff --git a/src/vstc/virtual_machine/instruction.rs b/src/vstc/virtual_machine/instruction.rs index 3b403a4..dfb9394 100644 --- a/src/vstc/virtual_machine/instruction.rs +++ b/src/vstc/virtual_machine/instruction.rs @@ -45,3 +45,55 @@ pub enum Instruction { Jmp = 0x27, JmpIf = 0x28, } + +impl Instruction { + pub fn from_byte(byte: u8) -> Instruction { + use Instruction::*; + + return match byte { + 0x00 => End, + 0x01 => Mov, + 0x02 => OpInc, + 0x03 => OpDec, + 0x04 => OpPlus, + 0x05 => OpMinus, + 0x06 => OpMul, + 0x07 => OpDiv, + 0x08 => OpMod, + 0x09 => OpExp, + 0x0a => OpEq, + 0x0b => OpNe, + 0x0c => OpTripleEq, + 0x0d => OpTripleNe, + 0x0e => OpAnd, + 0x0f => OpOr, + 0x10 => OpNot, + 0x11 => OpLess, + 0x12 => OpLessEq, + 0x13 => OpGreater, + 0x14 => OpGreaterEq, + 0x15 => OpNullishCoalesce, + 0x16 => OpOptionalChain, + 0x17 => OpBitAnd, + 0x18 => OpBitOr, + 0x19 => OpBitNot, + 0x1a => OpBitXor, + 0x1b => OpLeftShift, + 0x1c => OpRightShift, + 0x1d => OpRightShiftUnsigned, + 0x1e => TypeOf, + 0x1f => InstanceOf, + 0x20 => In, + 0x21 => Call, + 0x22 => Apply, + 0x23 => Bind, + 0x24 => Sub, + 0x25 => SubMov, + 0x26 => SubCall, + 0x27 => Jmp, + 0x28 => JmpIf, + + _ => std::panic!("Unrecognized instruction: {}", byte), + }; + } +}