From 7e8770cc4e418ef1a8cdfd9b1f59ee14b07cc6c9 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Fri, 28 Jun 2024 23:19:05 +0200 Subject: chore: restructure runtime Signed-off-by: Henry Gressmann --- crates/types/src/archive.rs | 6 +-- crates/types/src/instructions.rs | 101 +++++---------------------------------- crates/types/src/lib.rs | 4 +- crates/types/src/value.rs | 32 +------------ 4 files changed, 17 insertions(+), 126 deletions(-) (limited to 'crates/types') diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs index 52146e7..0a57f4c 100644 --- a/crates/types/src/archive.rs +++ b/crates/types/src/archive.rs @@ -57,11 +57,7 @@ impl TinyWasmModule { /// Creates a TinyWasmModule from a slice of bytes. pub fn from_twasm(wasm: &[u8]) -> Result { let len = validate_magic(wasm)?; - let root = check_archived_root::(&wasm[len..]).map_err(|_e| { - crate::log::error!("Invalid archive: {}", _e); - TwasmError::InvalidArchive - })?; - + let root = check_archived_root::(&wasm[len..]).map_err(|_e| TwasmError::InvalidArchive)?; Ok(root.deserialize(&mut rkyv::Infallible).unwrap()) } diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index d19326d..dd201ad 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -1,50 +1,6 @@ use super::{FuncAddr, GlobalAddr, LabelAddr, LocalAddr, TableAddr, TypeAddr, ValType}; use crate::{DataAddr, ElemAddr, MemAddr}; -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))] -pub enum BlockArgs { - Empty, - Type(ValType), - 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 From for BlockArgsPacked { - fn from(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) - } -} - -impl From for BlockArgs { - fn from(packed: BlockArgsPacked) -> Self { - match packed.0[0] { - 0 => BlockArgs::Empty, - 1 => BlockArgs::Type(ValType::from_byte(packed.0[1]).unwrap()), - 2 => BlockArgs::FuncType(u32::from_le_bytes(packed.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))] @@ -106,9 +62,19 @@ pub enum Instruction { // See Unreachable, Nop, - Block(BlockArgs, EndOffset), - Loop(BlockArgs, EndOffset), - If(BlockArgsPacked, ElseOffset, EndOffset), // If else offset is 0 if there is no else block + + Block(EndOffset), + BlockWithType(ValType, EndOffset), + BlockWithFuncType(TypeAddr, EndOffset), + + Loop(EndOffset), + LoopWithType(ValType, EndOffset), + LoopWithFuncType(TypeAddr, EndOffset), + + If(ElseOffset, EndOffset), + IfWithType(ValType, ElseOffset, EndOffset), + IfWithFuncType(TypeAddr, ElseOffset, EndOffset), + Else(EndOffset), EndBlockFrame, Br(LabelAddr), @@ -233,44 +199,3 @@ pub enum Instruction { DataDrop(DataAddr), ElemDrop(ElemAddr), } - -#[cfg(test)] -mod test_blockargs_packed { - use super::*; - - #[test] - fn test_empty() { - let packed: BlockArgsPacked = BlockArgs::Empty.into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::Empty); - } - - #[test] - fn test_val_type_i32() { - let packed: BlockArgsPacked = BlockArgs::Type(ValType::I32).into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::Type(ValType::I32)); - } - - #[test] - fn test_val_type_i64() { - let packed: BlockArgsPacked = BlockArgs::Type(ValType::I64).into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::Type(ValType::I64)); - } - - #[test] - fn test_val_type_f32() { - let packed: BlockArgsPacked = BlockArgs::Type(ValType::F32).into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::Type(ValType::F32)); - } - - #[test] - fn test_val_type_f64() { - let packed: BlockArgsPacked = BlockArgs::Type(ValType::F64).into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::Type(ValType::F64)); - } - - #[test] - fn test_func_type() { - let packed: BlockArgsPacked = BlockArgs::FuncType(0x12345678).into(); - assert_eq!(BlockArgs::from(packed), BlockArgs::FuncType(0x12345678)); - } -} diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 785b13b..cdee7e6 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -14,7 +14,7 @@ use core::{fmt::Debug, ops::Range}; // log for logging (optional). #[cfg(feature = "logging")] -#[allow(clippy::single_component_path_imports)] +#[allow(clippy::single_component_path_imports, unused)] use log; #[cfg(not(feature = "logging"))] @@ -125,7 +125,7 @@ pub type ExternAddr = Addr; // additional internal addresses pub type TypeAddr = Addr; -pub type LocalAddr = Addr; +pub type LocalAddr = u16; // there can't be more than 50.000 locals in a function pub type LabelAddr = Addr; pub type ModuleInstanceAddr = Addr; diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 6c422d0..c418338 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -146,31 +146,6 @@ impl ValType { pub fn is_simd(&self) -> bool { matches!(self, ValType::V128) } - - pub(crate) fn to_byte(self) -> u8 { - match self { - ValType::I32 => 0x7F, - ValType::I64 => 0x7E, - ValType::F32 => 0x7D, - ValType::F64 => 0x7C, - ValType::V128 => 0x7B, - 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), - 0x7B => Some(ValType::V128), - 0x70 => Some(ValType::RefFunc), - 0x6F => Some(ValType::RefExtern), - _ => None, - } - } } macro_rules! impl_conversion_for_wasmvalue { @@ -202,9 +177,4 @@ macro_rules! impl_conversion_for_wasmvalue { } } -impl_conversion_for_wasmvalue! { - i32 => I32, - i64 => I64, - f32 => F32, - f64 => F64 -} +impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, u128 => V128 } -- cgit v1.3.1