Dedupe InstructionByte

This commit is contained in:
Andrew Morris
2023-03-21 14:00:33 +11:00
parent 20d81f5324
commit cfb72fdbcd
6 changed files with 20 additions and 69 deletions

View File

@@ -3,17 +3,16 @@ use std::rc::Rc;
use num_bigint::BigInt;
use num_bigint::Sign;
use valuescript_common::InstructionByte;
use crate::builtins::BUILTIN_VALS;
use super::instruction::Instruction;
use super::vs_array::VsArray;
use super::vs_class::VsClass;
use super::vs_function::VsFunction;
use super::vs_object::VsObject;
use super::vs_pointer::VsPointer;
use super::vs_value::Val;
use super::vs_value::ValTrait;
use crate::vs_array::VsArray;
use crate::vs_class::VsClass;
use crate::vs_function::VsFunction;
use crate::vs_object::VsObject;
use crate::vs_pointer::VsPointer;
use crate::vs_value::Val;
use crate::vs_value::ValTrait;
pub struct BytecodeDecoder {
// TODO: Enable borrow usage to avoid the rc overhead
@@ -257,7 +256,7 @@ impl BytecodeDecoder {
}));
}
pub fn decode_instruction(&mut self) -> Instruction {
return Instruction::from_byte(self.decode_byte());
pub fn decode_instruction(&mut self) -> InstructionByte {
return InstructionByte::from_byte(self.decode_byte());
}
}

View File

@@ -1,8 +1,9 @@
use std::rc::Rc;
use valuescript_common::InstructionByte;
use crate::bytecode_decoder::BytecodeDecoder;
use crate::bytecode_decoder::BytecodeType;
use crate::instruction::Instruction;
use crate::operations;
use crate::stack_frame::FrameStepOk;
use crate::stack_frame::FrameStepResult;
@@ -93,7 +94,7 @@ impl StackFrameTrait for BytecodeStackFrame {
}
fn step(&mut self) -> FrameStepResult {
use Instruction::*;
use InstructionByte::*;
match self.decoder.decode_instruction() {
End => {

View File

@@ -1,104 +0,0 @@
// TODO: Fix duplication with assembler (requires internal crate?)
#[derive(Debug, Clone)]
pub enum Instruction {
End = 0x00,
Mov = 0x01,
OpInc = 0x02,
OpDec = 0x03,
OpPlus = 0x04,
OpMinus = 0x05,
OpMul = 0x06,
OpDiv = 0x07,
OpMod = 0x08,
OpExp = 0x09,
OpEq = 0x0a,
OpNe = 0x0b,
OpTripleEq = 0x0c,
OpTripleNe = 0x0d,
OpAnd = 0x0e,
OpOr = 0x0f,
OpNot = 0x10,
OpLess = 0x11,
OpLessEq = 0x12,
OpGreater = 0x13,
OpGreaterEq = 0x14,
OpNullishCoalesce = 0x15,
OpOptionalChain = 0x16,
OpBitAnd = 0x17,
OpBitOr = 0x18,
OpBitNot = 0x19,
OpBitXor = 0x1a,
OpLeftShift = 0x1b,
OpRightShift = 0x1c,
OpRightShiftUnsigned = 0x1d,
TypeOf = 0x1e,
InstanceOf = 0x1f,
In = 0x20,
Call = 0x21,
Apply = 0x22,
Bind = 0x23,
Sub = 0x24,
SubMov = 0x25,
SubCall = 0x26,
Jmp = 0x27,
JmpIf = 0x28,
UnaryPlus = 0x29,
UnaryMinus = 0x2a,
New = 0x2b,
}
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,
0x29 => UnaryPlus,
0x2a => UnaryMinus,
0x2b => New,
_ => panic!("Unrecognized instruction: {}", byte),
};
}
}

View File

@@ -5,7 +5,6 @@ mod bytecode_decoder;
mod bytecode_stack_frame;
mod first_stack_frame;
mod helpers;
mod instruction;
mod macros;
mod native_frame_function;
mod native_function;