diff options
| author | Henry <mail@henrygressmann.de> | 2026-08-28 00:57:49 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-08-28 01:23:25 +0200 |
| commit | e73c92160041ed0db5bda81e5c55b0e4c6293a87 (patch) | |
| tree | 682d53e8037758359158bebb7806d99803a10657 /crates/types/src | |
| parent | bec01366093debe42285e52861ad2d46ee55c037 (diff) | |
chore: simplify ToWasmTypes traits, general cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/types/src')
| -rw-r--r-- | crates/types/src/instructions.rs | 74 | ||||
| -rw-r--r-- | crates/types/src/operands.rs | 50 | ||||
| -rw-r--r-- | crates/types/src/value.rs | 7 |
3 files changed, 50 insertions, 81 deletions
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index 9330af6..b92a0f4 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -1,7 +1,7 @@ use alloc::boxed::Box; use super::{FuncAddr, GlobalAddr, LocalAddr, TableAddr, TagAddr, TypeAddr, ValueCounts}; -use crate::operands::read_field; +use crate::operands::{read_field, write_field}; use crate::{ DataAddr, ElemAddr, MemAddr, ModuleFuncIdx, Operand64, Operand64Idx, Operand128, Operand128Idx, OperandIdx, RefType, }; @@ -188,8 +188,8 @@ impl Operand128<MemoryOperand> { #[inline] pub fn new(offset: u64, memory: MemAddr) -> Self { let mut operand = Self::default(); - operand.write_u64::<0>(offset); - operand.write_u32::<8>(memory); + write_field!(operand, u64, 0, offset); + write_field!(operand, u32, 8, memory); operand } @@ -211,8 +211,8 @@ impl Operand64<CompactMemoryOperand> { #[inline] pub fn new(offset: u32, memory: u16) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(offset); - operand.write_u16::<4>(memory); + write_field!(operand, u32, 0, offset); + write_field!(operand, u16, 4, memory); operand } @@ -233,9 +233,9 @@ impl Operand128<BranchTableOperand> { #[inline] pub fn new(target: u32, start: u32, len: u32) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(target); - operand.write_u32::<4>(start); - operand.write_u32::<8>(len); + write_field!(operand, u32, 0, target); + write_field!(operand, u32, 4, start); + write_field!(operand, u32, 8, len); operand } #[inline(always)] @@ -252,9 +252,9 @@ impl Operand128<MemoryFillOperand> { #[inline] pub fn new(memory: MemAddr, byte: u8, value: i32) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(memory); - operand.write_u8::<4>(byte); - operand.write_u32::<5>(value as u32); + write_field!(operand, u32, 0, memory); + write_field!(operand, u8, 4, byte); + write_field!(operand, i32, 5, value); operand } #[inline(always)] @@ -275,10 +275,10 @@ impl Operand128<LocalUpdateOperand> { #[inline] pub fn new(target: u32, value: i32, local: LocalAddr, on_zero: bool) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(target); - operand.write_u32::<4>(value as u32); - operand.write_u16::<8>(local); - operand.write_u8::<10>(u8::from(on_zero)); + write_field!(operand, u32, 0, target); + write_field!(operand, i32, 4, value); + write_field!(operand, u16, 8, local); + write_field!(operand, u8, 10, u8::from(on_zero)); operand } #[inline(always)] @@ -299,10 +299,10 @@ impl Operand128<GlobalUpdateOperand> { #[inline] pub fn new(target: u32, value: i32, global: GlobalAddr, on_zero: bool) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(target); - operand.write_u32::<4>(value as u32); - operand.write_u32::<8>(global); - operand.write_u8::<12>(u8::from(on_zero)); + write_field!(operand, u32, 0, target); + write_field!(operand, i32, 4, value); + write_field!(operand, u32, 8, global); + write_field!(operand, u8, 12, u8::from(on_zero)); operand } #[inline(always)] @@ -323,10 +323,10 @@ impl Operand128<LocalUpdateCmpOperand> { #[inline] pub fn new(target: u32, value: i32, local: LocalAddr, right: LocalAddr) -> Self { let mut operand = Self::default(); - operand.write_u32::<0>(target); - operand.write_u32::<4>(value as u32); - operand.write_u16::<8>(local); - operand.write_u16::<10>(right); + write_field!(operand, u32, 0, target); + write_field!(operand, i32, 4, value); + write_field!(operand, u16, 8, local); + write_field!(operand, u16, 10, right); operand } #[inline(always)] @@ -771,8 +771,6 @@ const _: () = assert!(core::mem::size_of::<Instruction>() == 8); #[cfg(test)] mod tests { - use alloc::vec; - use super::*; #[test] @@ -795,30 +793,4 @@ mod tests { assert_eq!(value.local(), u16::MAX); assert!(value.on_zero()); } - - #[test] - fn v128_operand_views_round_trip_max_bytes() { - let value = Operand128Idx::<[u8; 16]>::new(0); - let local = Operand64Idx::<(u16, Operand128Idx<[u8; 16]>)>::new(0); - let global = Operand64Idx::<(u32, Operand128Idx<[u8; 16]>)>::new(1); - let set = Operand64Idx::<(u16, u16, Operand128Idx<[u8; 16]>)>::new(2); - let data = super::super::WasmFunctionData { - operands128: vec![Operand128::<[u8; 16]>::new([u8::MAX; 16]).cast()].into_boxed_slice(), - operands64: vec![ - Operand64::<(u16, Operand128Idx<[u8; 16]>)>::new(u16::MAX, value).cast(), - Operand64::<(u32, Operand128Idx<[u8; 16]>)>::new(u32::MAX, value).cast(), - Operand64::<(u16, u16, Operand128Idx<[u8; 16]>)>::new(u16::MAX, u16::MAX, value).cast(), - ] - .into_boxed_slice(), - ..Default::default() - }; - - assert_eq!(value.resolve(&data).value(), [u8::MAX; 16]); - assert_eq!((local.resolve(&data).a(), local.resolve(&data).b()), (u16::MAX, value)); - assert_eq!((global.resolve(&data).a(), global.resolve(&data).b()), (u32::MAX, value)); - assert_eq!( - (set.resolve(&data).a(), set.resolve(&data).b(), set.resolve(&data).c()), - (u16::MAX, u16::MAX, value) - ); - } } diff --git a/crates/types/src/operands.rs b/crates/types/src/operands.rs index 1bc4a28..edbab26 100644 --- a/crates/types/src/operands.rs +++ b/crates/types/src/operands.rs @@ -129,20 +129,14 @@ macro_rules! define_operand { #[cfg(feature = "archive")] impl<T> serde::Serialize for $name<T> { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: serde::Serializer, - { + fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { self.bytes.serialize(serializer) } } #[cfg(feature = "archive")] impl<'de, T> serde::Deserialize<'de> for $name<T> { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: serde::Deserializer<'de>, - { + fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { Ok(Self { bytes: <[u8; $len]>::deserialize(deserializer)?, marker: PhantomData }) } } @@ -153,19 +147,18 @@ define_operand!(Operand64, 8); define_operand!(Operand128, 16); macro_rules! unwrap_or_unreachable { - ($value:expr) => {{ unwrap_or_unreachable!($value, "entered unreachable code") }}; - ($value:expr, $message:literal) => {{ + ($value:expr) => {{ match $value { Some(value) => value, - None => operand_unreachable($message), + None => { core::hint::cold_path(); unreachable!() }, + } + }}; + ($value:expr, $($arg:tt)+) => {{ + match $value { + Some(value) => value, + None => { core::hint::cold_path(); unreachable!($($arg)+) }, } }}; -} - -#[cold] -#[inline(never)] -pub(crate) fn operand_unreachable(message: &'static str) -> ! { - unreachable!("{message}") } impl<T> Operand64Idx<T> { @@ -207,17 +200,19 @@ macro_rules! read_field { ($self:ident, i64, $offset:expr) => {{ read_field!($self, u64, $offset) as i64 }}; ($self:ident, idx128, $offset:expr) => {{ Operand128Idx::new(read_field!($self, u32, $offset)) }}; } -pub(crate) use read_field; macro_rules! read_bytes { ($self:ident, $offset:expr, $len:literal) => {{ match <[u8; $len]>::try_from(&$self.bytes[$offset..$offset + $len]) { Ok(bytes) => bytes, - Err(_) => $crate::operands::operand_unreachable("invalid operand field"), + Err(_) => { + core::hint::cold_path(); + unreachable!("invalid operand field read") + } } }}; } -pub(crate) use read_bytes; + macro_rules! write_field { ($self:ident, u8, $offset:expr, $value:expr) => {{ $self.write_u8::<{ $offset }>($value) }}; ($self:ident, u16, $offset:expr, $value:expr) => {{ $self.write_u16::<{ $offset }>($value) }}; @@ -228,6 +223,8 @@ macro_rules! write_field { ($self:ident, idx128, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value.index) }}; } +pub(crate) use {read_bytes, read_field, write_field}; + impl<T> Operand64<T> { /// Returns the jump target stored at the start of a retargetable operand. #[inline(always)] @@ -238,7 +235,7 @@ impl<T> Operand64<T> { /// Replaces the jump target stored at the start of a retargetable operand. #[inline] pub fn with_target(mut self, target: u32) -> Self { - self.write_u32::<0>(target); + write_field!(self, u32, 0, target); self } } @@ -253,7 +250,7 @@ impl<T> Operand128<T> { /// Replaces the jump target stored at the start of a retargetable operand. #[inline] pub fn with_target(mut self, target: u32) -> Self { - self.write_u32::<0>(target); + write_field!(self, u32, 0, target); self } } @@ -267,6 +264,7 @@ macro_rules! packed_layout { write_field!(operand, $field, 0, value); operand } + #[inline(always)] pub fn value(&self) -> field_ty!($field) { read_field!(self, $field, 0) @@ -282,10 +280,12 @@ macro_rules! packed_layout { write_field!(operand, $b, field_size!($a), b); operand } + #[inline(always)] pub fn a(&self) -> field_ty!($a) { read_field!(self, $a, 0) } + #[inline(always)] pub fn b(&self) -> field_ty!($b) { read_field!(self, $b, field_size!($a)) @@ -302,14 +302,17 @@ macro_rules! packed_layout { write_field!(operand, $c, field_size!($a) + field_size!($b), c); operand } + #[inline(always)] pub fn a(&self) -> field_ty!($a) { read_field!(self, $a, 0) } + #[inline(always)] pub fn b(&self) -> field_ty!($b) { read_field!(self, $b, field_size!($a)) } + #[inline(always)] pub fn c(&self) -> field_ty!($c) { read_field!(self, $c, field_size!($a) + field_size!($b)) @@ -329,7 +332,6 @@ packed_layout!(Operand64, operands64, u32, u16, u16); packed_layout!(Operand64, operands64, u16, idx128); packed_layout!(Operand64, operands64, u32, idx128); packed_layout!(Operand64, operands64, u16, u16, idx128); - packed_layout!(Operand128, operands128, u16, u64); packed_layout!(Operand128, operands128, u32, u64); packed_layout!(Operand128, operands128, u16, u16, u64); @@ -341,11 +343,13 @@ impl Operand128<[u8; 16]> { pub fn new(value: [u8; 16]) -> Self { Self { bytes: value, marker: PhantomData } } + #[inline(always)] pub fn value(&self) -> [u8; 16] { self.bytes } } + const _: () = { assert!(core::mem::size_of::<Operand64Idx<i64>>() == 4); assert!(core::mem::size_of::<Operand64>() == 8); diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 847e5e1..3a396a2 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -21,13 +21,6 @@ pub enum WasmType { Ref(RefType), } -impl WasmType { - #[inline] - pub const fn is_simd(self) -> bool { - matches!(self, Self::V128) - } -} - impl From<StorageType> for WasmType { fn from(value: StorageType) -> Self { match value { |
