use core::marker::PhantomData; /// An index into the operand lane used by `T`. #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "archive", serde(transparent))] #[repr(transparent)] pub struct OperandIdx { index: u32, #[cfg_attr(feature = "archive", serde(skip))] marker: PhantomData, } /// An index into the 64-bit operand lane. pub type Operand64Idx = OperandIdx; /// An index into the 128-bit operand lane. pub type Operand128Idx = OperandIdx; impl OperandIdx { #[inline] #[doc(hidden)] pub const fn new(index: u32) -> Self { Self { index, marker: PhantomData } } /// Returns the underlying lane index. #[inline] pub const fn index(self) -> usize { self.index as usize } } impl Copy for OperandIdx {} impl Clone for OperandIdx { fn clone(&self) -> Self { *self } } impl PartialEq for OperandIdx { fn eq(&self, other: &Self) -> bool { self.index == other.index } } impl Eq for OperandIdx {} #[cfg(feature = "debug")] impl core::fmt::Debug for OperandIdx { fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.index.fmt(formatter) } } macro_rules! define_operand { ($name:ident, $len:literal) => { #[doc = concat!("A typed packed ", $len, "-byte operand value.")] #[repr(C)] pub struct $name { pub(crate) bytes: [u8; $len], marker: PhantomData, } #[allow(dead_code)] impl $name { #[inline] pub(crate) fn write_u8(&mut self, value: u8) { const { assert!(OFFSET < $len) }; self.bytes[OFFSET] = value; } #[inline] pub(crate) fn write_u16(&mut self, value: u16) { const { assert!(OFFSET + 2 <= $len) }; self.bytes[OFFSET..OFFSET + 2].copy_from_slice(&value.to_le_bytes()); } #[inline] pub(crate) fn write_u32(&mut self, value: u32) { const { assert!(OFFSET + 4 <= $len) }; self.bytes[OFFSET..OFFSET + 4].copy_from_slice(&value.to_le_bytes()); } #[inline] pub(crate) fn write_u64(&mut self, value: u64) { const { assert!(OFFSET + 8 <= $len) }; self.bytes[OFFSET..OFFSET + 8].copy_from_slice(&value.to_le_bytes()); } #[inline(always)] #[doc(hidden)] pub fn cast(self) -> $name { $name { bytes: self.bytes, marker: PhantomData } } } impl Copy for $name {} impl Clone for $name { fn clone(&self) -> Self { *self } } impl PartialEq for $name { fn eq(&self, other: &Self) -> bool { self.bytes == other.bytes } } impl Eq for $name {} impl PartialOrd for $name { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for $name { fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.bytes.cmp(&other.bytes) } } impl Default for $name { fn default() -> Self { Self { bytes: [0; $len], marker: PhantomData } } } #[cfg(feature = "debug")] impl core::fmt::Debug for $name { fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.bytes.fmt(formatter) } } #[cfg(feature = "archive")] impl serde::Serialize for $name { fn serialize(&self, serializer: S) -> Result { self.bytes.serialize(serializer) } } #[cfg(feature = "archive")] impl<'de, T> serde::Deserialize<'de> for $name { fn deserialize>(deserializer: D) -> Result { Ok(Self { bytes: <[u8; $len]>::deserialize(deserializer)?, marker: PhantomData }) } } }; } define_operand!(Operand64, 8); define_operand!(Operand128, 16); impl Operand64Idx { /// Resolves this index to a copied 64-bit packed operand. #[inline(always)] pub fn resolve(self, data: &crate::WasmFunctionData) -> Operand64 { data.operands64[self.index as usize].cast() } } impl Operand128Idx { /// Resolves this index to a copied 128-bit packed operand. #[inline(always)] pub fn resolve(self, data: &crate::WasmFunctionData) -> Operand128 { data.operands128[self.index as usize].cast() } } macro_rules! field_ty { (u8) => { u8 }; (u16) => { u16 }; (u32) => { u32 }; (u64) => { u64 }; (i32) => { i32 }; (i64) => { i64 }; (idx128) => { Operand128Idx<[u8; 16]> }; } macro_rules! field_size { (u8) => {{ 1 }}; (u16) => {{ 2 }}; (u32) => {{ 4 }}; (u64) => {{ 8 }}; (i32) => {{ 4 }}; (i64) => {{ 8 }}; (idx128) => {{ 4 }}; } macro_rules! read_field { ($self:ident, u8, $offset:expr) => {{ $self.bytes[$offset] }}; ($self:ident, u16, $offset:expr) => {{ u16::from_le_bytes($crate::operands::read_bytes!($self, $offset, 2)) }}; ($self:ident, u32, $offset:expr) => {{ u32::from_le_bytes($crate::operands::read_bytes!($self, $offset, 4)) }}; ($self:ident, u64, $offset:expr) => {{ u64::from_le_bytes($crate::operands::read_bytes!($self, $offset, 8)) }}; ($self:ident, i32, $offset:expr) => {{ read_field!($self, u32, $offset) as i32 }}; ($self:ident, i64, $offset:expr) => {{ read_field!($self, u64, $offset) as i64 }}; ($self:ident, idx128, $offset:expr) => {{ Operand128Idx::new(read_field!($self, u32, $offset)) }}; } macro_rules! read_bytes { ($self:ident, $offset:expr, $len:literal) => {{ match <[u8; $len]>::try_from(&$self.bytes[$offset..$offset + $len]) { Ok(bytes) => bytes, Err(_) => { core::hint::cold_path(); unreachable!("invalid operand field read") } } }}; } 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) }}; ($self:ident, u32, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value) }}; ($self:ident, u64, $offset:expr, $value:expr) => {{ $self.write_u64::<{ $offset }>($value) }}; ($self:ident, i32, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value as u32) }}; ($self:ident, i64, $offset:expr, $value:expr) => {{ $self.write_u64::<{ $offset }>($value as u64) }}; ($self:ident, idx128, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value.index) }}; } pub(crate) use {read_bytes, read_field, write_field}; impl Operand64 { /// Returns the jump target stored at the start of a retargetable operand. #[inline(always)] pub fn target(&self) -> u32 { read_field!(self, u32, 0) } /// Replaces the jump target stored at the start of a retargetable operand. #[inline] pub fn with_target(mut self, target: u32) -> Self { write_field!(self, u32, 0, target); self } } impl Operand128 { /// Returns the jump target stored at the start of a retargetable operand. #[inline(always)] pub fn target(&self) -> u32 { read_field!(self, u32, 0) } /// Replaces the jump target stored at the start of a retargetable operand. #[inline] pub fn with_target(mut self, target: u32) -> Self { write_field!(self, u32, 0, target); self } } macro_rules! packed_layout { ($operand:ident, $lane:ident, $field:tt) => { impl $operand { #[inline] pub fn new(value: field_ty!($field)) -> Self { let mut operand = Self::default(); write_field!(operand, $field, 0, value); operand } #[inline(always)] pub fn value(&self) -> field_ty!($field) { read_field!(self, $field, 0) } } }; ($operand:ident, $lane:ident, $a:tt, $b:tt) => { impl $operand<(field_ty!($a), field_ty!($b))> { #[inline] pub fn new(a: field_ty!($a), b: field_ty!($b)) -> Self { let mut operand = Self::default(); write_field!(operand, $a, 0, a); 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)) } } }; ($operand:ident, $lane:ident, $a:tt, $b:tt, $c:tt) => { impl $operand<(field_ty!($a), field_ty!($b), field_ty!($c))> { #[inline] pub fn new(a: field_ty!($a), b: field_ty!($b), c: field_ty!($c)) -> Self { let mut operand = Self::default(); write_field!(operand, $a, 0, a); write_field!(operand, $b, field_size!($a), b); 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)) } } }; } packed_layout!(Operand64, operands64, i64); packed_layout!(Operand64, operands64, u32, u32); packed_layout!(Operand64, operands64, u16, u32); packed_layout!(Operand64, operands64, u32, u16); packed_layout!(Operand64, operands64, u16, u16, u16); packed_layout!(Operand64, operands64, u16, u16, u32); packed_layout!(Operand64, operands64, u32, i32); 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); packed_layout!(Operand128, operands128, u32, i64); packed_layout!(Operand128, operands128, u32, i32, u16); impl Operand128<[u8; 16]> { #[inline] 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::>() == 4); assert!(core::mem::size_of::() == 8); assert!(core::mem::size_of::() == 16); };