From 4faaf5bb222947a95b854d733a3cbe3bf588b597 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 27 Feb 2024 14:35:40 +0100 Subject: reduce instruction enum size even more Signed-off-by: Henry Gressmann --- crates/types/src/archive.rs | 2 - crates/types/src/instructions.rs | 156 +++++++++++++++++++++++++++++---------- crates/types/src/value.rs | 23 ++++++ 3 files changed, 141 insertions(+), 40 deletions(-) (limited to 'crates/types') diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs index bbd2206..273d6ed 100644 --- a/crates/types/src/archive.rs +++ b/crates/types/src/archive.rs @@ -7,10 +7,8 @@ use rkyv::{ Deserialize, }; -// 16 bytes const TWASM_MAGIC_PREFIX: &[u8; 4] = b"TWAS"; const TWASM_VERSION: &[u8; 2] = b"01"; - #[rustfmt::skip] const TWASM_MAGIC: [u8; 16] = [ TWASM_MAGIC_PREFIX[0], TWASM_MAGIC_PREFIX[1], TWASM_MAGIC_PREFIX[2], TWASM_MAGIC_PREFIX[3], TWASM_VERSION[0], TWASM_VERSION[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index a19b4d2..537b8d7 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -9,14 +9,44 @@ pub enum BlockArgs { FuncType(u32), } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))] +/// A packed representation of BlockArgs +/// This is needed to keep the size of the Instruction enum small. +/// Sadly, using #[repr(u8)] on BlockArgs itself is not possible because of the FuncType variant. +pub struct BlockArgsPacked([u8; 5]); // Modifying this directly can cause runtime errors, but no UB +impl BlockArgsPacked { + pub fn new(args: BlockArgs) -> Self { + let mut packed = [0; 5]; + match args { + BlockArgs::Empty => packed[0] = 0, + BlockArgs::Type(t) => { + packed[0] = 1; + packed[1] = t.to_byte(); + } + BlockArgs::FuncType(t) => { + packed[0] = 2; + packed[1..].copy_from_slice(&t.to_le_bytes()); + } + } + Self(packed) + } + pub fn unpack(&self) -> BlockArgs { + match self.0[0] { + 0 => BlockArgs::Empty, + 1 => BlockArgs::Type(ValType::from_byte(self.0[1]).unwrap()), + 2 => BlockArgs::FuncType(u32::from_le_bytes(self.0[1..].try_into().unwrap())), + _ => unreachable!(), + } + } +} + /// Represents a memory immediate in a WebAssembly memory instruction. #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))] pub struct MemoryArg { pub offset: u64, pub mem_addr: MemAddr, - // pub align: u8, - // pub align_max: u8, } type BrTableDefault = u32; @@ -48,21 +78,23 @@ pub enum ConstInstruction { /// This makes it easier to implement the label stack iteratively. /// /// See -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))] +// should be kept as small as possible (16 bytes max) pub enum Instruction { // Custom Instructions BrLabel(LabelAddr), - //== Not implemented yet, to be determined - + // Not implemented yet // LocalGet + I32Const + I32Add // One of the most common patterns in the Rust compiler output - I32LocalGetConstAdd(LocalAddr, i32), + // I32LocalGetConstAdd(LocalAddr, i32), - // LocalGet + I32Const + I32Store - // Also common, helps us skip the stack entirely - // I32LocalGetConstStore(LocalAddr, i32, MemoryArg), // I32Store + LocalGet + I32Const + // Not implemented yet + // LocalGet + I32Const + I32Store => I32LocalGetConstStore + I32Const + // Also common, helps us skip the stack entirely. + // Has to be followed by an I32Const instruction + // I32LocalGetConstStore { local: LocalAddr, offset: i32, mem_addr: MemAddr }, // I32Store + LocalGet + I32Const // I64Xor + I64Const + I64RotL // Commonly used by a few crypto libraries @@ -72,13 +104,13 @@ pub enum Instruction { LocalTeeGet(LocalAddr, LocalAddr), LocalGet2(LocalAddr, LocalAddr), LocalGet3(LocalAddr, LocalAddr, LocalAddr), - LocalGet4(LocalAddr, LocalAddr, LocalAddr, LocalAddr), LocalGetSet(LocalAddr, LocalAddr), - I32AddConst(i32), - I32SubConst(i32), - I64AddConst(i64), - I64SubConst(i64), + // Not implemented yet + // I32AddConst(i32), + // I32SubConst(i32), + // I64AddConst(i64), + // I64SubConst(i64), // Control Instructions // See @@ -86,7 +118,7 @@ pub enum Instruction { Nop, Block(BlockArgs, EndOffset), Loop(BlockArgs, EndOffset), - If(BlockArgs, Option, EndOffset), + If(BlockArgsPacked, ElseOffset, EndOffset), // If else offset is 0 if there is no else block Else(EndOffset), EndBlockFrame, EndFunc, @@ -111,29 +143,29 @@ pub enum Instruction { GlobalSet(GlobalAddr), // Memory Instructions - I32Load(MemoryArg), - I64Load(MemoryArg), - F32Load(MemoryArg), - F64Load(MemoryArg), - I32Load8S(MemoryArg), - I32Load8U(MemoryArg), - I32Load16S(MemoryArg), - I32Load16U(MemoryArg), - I64Load8S(MemoryArg), - I64Load8U(MemoryArg), - I64Load16S(MemoryArg), - I64Load16U(MemoryArg), - I64Load32S(MemoryArg), - I64Load32U(MemoryArg), - I32Store(MemoryArg), - I64Store(MemoryArg), - F32Store(MemoryArg), - F64Store(MemoryArg), - I32Store8(MemoryArg), - I32Store16(MemoryArg), - I64Store8(MemoryArg), - I64Store16(MemoryArg), - I64Store32(MemoryArg), + I32Load { offset: u64, mem_addr: MemAddr }, + I64Load { offset: u64, mem_addr: MemAddr }, + F32Load { offset: u64, mem_addr: MemAddr }, + F64Load { offset: u64, mem_addr: MemAddr }, + I32Load8S { offset: u64, mem_addr: MemAddr }, + I32Load8U { offset: u64, mem_addr: MemAddr }, + I32Load16S { offset: u64, mem_addr: MemAddr }, + I32Load16U { offset: u64, mem_addr: MemAddr }, + I64Load8S { offset: u64, mem_addr: MemAddr }, + I64Load8U { offset: u64, mem_addr: MemAddr }, + I64Load16S { offset: u64, mem_addr: MemAddr }, + I64Load16U { offset: u64, mem_addr: MemAddr }, + I64Load32S { offset: u64, mem_addr: MemAddr }, + I64Load32U { offset: u64, mem_addr: MemAddr }, + I32Store { offset: u64, mem_addr: MemAddr }, + I64Store { offset: u64, mem_addr: MemAddr }, + F32Store { offset: u64, mem_addr: MemAddr }, + F64Store { offset: u64, mem_addr: MemAddr }, + I32Store8 { offset: u64, mem_addr: MemAddr }, + I32Store16 { offset: u64, mem_addr: MemAddr }, + I64Store8 { offset: u64, mem_addr: MemAddr }, + I64Store16 { offset: u64, mem_addr: MemAddr }, + I64Store32 { offset: u64, mem_addr: MemAddr }, MemorySize(MemAddr, u8), MemoryGrow(MemAddr, u8), @@ -302,3 +334,51 @@ pub enum Instruction { MemoryFill(MemAddr), DataDrop(DataAddr), } + +#[cfg(test)] +mod test_blockargs_packed { + use super::*; + + #[test] + fn test_empty() { + let args = BlockArgs::Empty; + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::Empty); + } + + #[test] + fn test_val_type_i32() { + let args = BlockArgs::Type(ValType::I32); + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::Type(ValType::I32)); + } + + #[test] + fn test_val_type_i64() { + let args = BlockArgs::Type(ValType::I64); + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::Type(ValType::I64)); + } + + #[test] + fn test_val_type_f32() { + let args = BlockArgs::Type(ValType::F32); + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::Type(ValType::F32)); + } + + #[test] + fn test_val_type_f64() { + let args = BlockArgs::Type(ValType::F64); + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::Type(ValType::F64)); + } + + #[test] + fn test_func_type() { + let func_type = 123; // Use an arbitrary u32 value + let args = BlockArgs::FuncType(func_type); + let packed = BlockArgsPacked::new(args); + assert_eq!(packed.unpack(), BlockArgs::FuncType(func_type)); + } +} diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 8fd72fb..df062ce 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -141,6 +141,29 @@ impl ValType { pub fn default_value(&self) -> WasmValue { WasmValue::default_for(*self) } + + pub(crate) fn to_byte(&self) -> u8 { + match self { + ValType::I32 => 0x7F, + ValType::I64 => 0x7E, + ValType::F32 => 0x7D, + ValType::F64 => 0x7C, + ValType::RefFunc => 0x70, + ValType::RefExtern => 0x6F, + } + } + + pub(crate) fn from_byte(byte: u8) -> Option { + match byte { + 0x7F => Some(ValType::I32), + 0x7E => Some(ValType::I64), + 0x7D => Some(ValType::F32), + 0x7C => Some(ValType::F64), + 0x70 => Some(ValType::RefFunc), + 0x6F => Some(ValType::RefExtern), + _ => None, + } + } } macro_rules! impl_conversion_for_wasmvalue { -- cgit v1.3.1