diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 49d129cf07..a9efa6e8c9 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -1,9 +1,4 @@ -use crate::core::{F32, F64}; -use core::{ - fmt::Debug, - marker::PhantomData, - num::{NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU16, NonZeroU32, NonZeroU64}, -}; +use core::{fmt::Debug, marker::PhantomData, num::NonZero}; /// Error that may occur upon converting values to [`Const16`]. #[derive(Debug, Copy, Clone)] @@ -58,173 +53,61 @@ impl PartialEq for Const16 { impl Eq for Const16 {} -impl From for Const16 { - fn from(value: i16) -> Self { - Self::new(AnyConst16::from(value)) - } -} - -impl From for Const16 { - fn from(value: u16) -> Self { - Self::new(AnyConst16::from(value)) - } -} - -impl From for Const16 { - fn from(value: i16) -> Self { - Self::new(AnyConst16::from(value)) - } -} - -impl From for Const16 { - fn from(value: u16) -> Self { - Self::new(AnyConst16::from(value)) - } -} - -impl From for Const16 { - fn from(value: NonZeroI16) -> Self { - Self::new(AnyConst16::from(value.get())) - } -} - -impl From for Const16 { - fn from(value: NonZeroU16) -> Self { - Self::new(AnyConst16::from(value.get())) - } -} - -impl From for Const16 { - fn from(value: NonZeroI16) -> Self { - Self::new(AnyConst16::from(value.get())) - } -} - -impl From for Const16 { - fn from(value: NonZeroU16) -> Self { - Self::new(AnyConst16::from(value.get())) - } -} - -impl From> for i32 { - fn from(value: Const16) -> Self { - Self::from(value.inner) - } -} - -impl From> for u32 { - fn from(value: Const16) -> Self { - Self::from(value.inner) - } -} - -impl From> for i64 { - fn from(value: Const16) -> Self { - Self::from(value.inner) - } -} - -impl From> for u64 { - fn from(value: Const16) -> Self { - Self::from(value.inner) - } -} - -impl From> for NonZeroI32 { - fn from(value: Const16) -> Self { - // SAFETY: Due to construction of `Const16` we are guaranteed - // that `value.inner` is a valid non-zero value. - unsafe { Self::new_unchecked(i32::from(value.inner)) } - } -} - -impl From> for NonZeroU32 { - fn from(value: Const16) -> Self { - // SAFETY: Due to construction of `Const16` we are guaranteed - // that `value.inner` is a valid non-zero value. - unsafe { Self::new_unchecked(u32::from(value.inner)) } - } -} - -impl From> for NonZeroI64 { - fn from(value: Const16) -> Self { - // SAFETY: Due to construction of `Const16` we are guaranteed - // that `value.inner` is a valid non-zero value. - unsafe { Self::new_unchecked(i64::from(value.inner)) } - } -} - -impl From> for NonZeroU64 { - fn from(value: Const16) -> Self { - // SAFETY: Due to construction of `Const16` we are guaranteed - // that `value.inner` is a valid non-zero value. - unsafe { Self::new_unchecked(u64::from(value.inner)) } - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: i32) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroI32) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: u32) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroU32) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: i64) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroI64) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: u64) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroU64) -> Result { - AnyConst16::try_from(value).map(Self::new) - } -} +macro_rules! impl_const16_from { + ( $( ($from:ty, $to:ty) ),* $(,)? ) => { + $( + impl From<$from> for Const16<$to> { + fn from(value: $from) -> Self { + Self::new(AnyConst16::from(value)) + } + } + + impl From> for Const16> { + fn from(value: NonZero<$from>) -> Self { + Self::new(AnyConst16::from(value.get())) + } + } + )* + } +} +impl_const16_from!((i16, i32), (u16, u32), (i16, i64), (u16, u64),); + +macro_rules! impl_const16_from { + ( $($ty:ty),* ) => { + $( + impl From> for $ty { + fn from(value: Const16) -> Self { + Self::from(value.inner) + } + } + + impl From>> for NonZero<$ty> { + fn from(value: Const16) -> Self { + // SAFETY: Due to construction of `Const16` we are guaranteed + // that `value.inner` is a valid non-zero value. + unsafe { Self::new_unchecked(<$ty as From>::from(value.inner)) } + } + } + + impl TryFrom<$ty> for Const16<$ty> { + type Error = OutOfBoundsConst; + + fn try_from(value: $ty) -> Result { + AnyConst16::try_from(value).map(Self::new) + } + } + + impl TryFrom> for Const16> { + type Error = OutOfBoundsConst; + + fn try_from(value: NonZero<$ty>) -> Result { + AnyConst16::try_from(value).map(Self::new) + } + } + )* + }; +} +impl_const16_from!(i32, u32, i64, u64); /// A typed 32-bit encoded constant value. pub struct Const32 { @@ -278,95 +161,38 @@ impl PartialEq for Const32 { impl Eq for Const32 {} -impl From for Const32 { - fn from(value: i32) -> Self { - Self::new(AnyConst32::from(value)) - } -} - -impl From for Const32 { - fn from(value: u32) -> Self { - Self::new(AnyConst32::from(value)) - } -} - -impl From for Const32 { - fn from(value: i32) -> Self { - Self::new(AnyConst32::from(value)) - } -} - -impl From for Const32 { - fn from(value: u32) -> Self { - Self::new(AnyConst32::from(value)) - } -} - -impl From for Const32 { - fn from(value: f32) -> Self { - Self::new(AnyConst32::from(value)) - } -} - -impl From> for i32 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl From> for u32 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl From> for i64 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl From> for u64 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl From> for f32 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl From> for f64 { - fn from(value: Const32) -> Self { - Self::from(value.inner) - } -} - -impl TryFrom for Const32 { - type Error = OutOfBoundsConst; - - fn try_from(value: i64) -> Result { - AnyConst32::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const32 { - type Error = OutOfBoundsConst; - - fn try_from(value: u64) -> Result { - AnyConst32::try_from(value).map(Self::new) - } -} - -impl TryFrom for Const32 { - type Error = OutOfBoundsConst; +macro_rules! impl_const32 { + ( $ty:ty, $($rest:tt)* ) => { + impl_const32!(@ $ty, $ty); + impl_const32!($($rest)*); + }; + ( $ty64:ty as $ty32:ty, $($rest:tt)* ) => { + impl TryFrom<$ty64> for Const32<$ty64> { + type Error = OutOfBoundsConst; + + fn try_from(value: $ty64) -> Result { + AnyConst32::try_from(value).map(Self::new) + } + } + impl_const32!(@ $ty64, $ty32); + impl_const32!($($rest)*); + }; + ( @ $ty:ty, $ty32:ty ) => { + impl From<$ty32> for Const32<$ty> { + fn from(value: $ty32) -> Self { + Self::new(AnyConst32::from(value)) + } + } - fn try_from(value: f64) -> Result { - AnyConst32::try_from(value).map(Self::new) - } + impl From> for $ty { + fn from(value: Const32) -> Self { + Self::from(value.inner) + } + } + }; + () => {}; } +impl_const32!(i32, u32, i64 as i32, u64 as u32, f32, f64 as f32,); /// A 16-bit constant value of any type. /// @@ -376,143 +202,102 @@ impl TryFrom for Const32 { /// Upon use the small 16-bit value has to be sign-extended to /// the actual integer type, e.g. `i32` or `i64`. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub struct AnyConst16(i16); - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: i32) -> Result { - i16::try_from(value) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } +pub struct AnyConst16 { + bits: u16, } -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: u32) -> Result { - u16::try_from(value) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) +impl AnyConst16 { + /// Creates a new [`AnyConst16`] from the given `bits`. + fn from_bits(bits: u16) -> Self { + Self { bits } } } -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; +macro_rules! impl_any_const16 { + ( $( $ty:ty as $ty16:ty ),* $(,)? ) => { + $( + impl TryFrom<$ty> for AnyConst16 { + type Error = OutOfBoundsConst; - fn try_from(value: i64) -> Result { - i16::try_from(value) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} + fn try_from(value: $ty) -> Result { + <$ty16>::try_from(value) + .map(Self::from) + .map_err(|_| OutOfBoundsConst) + } + } -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; + impl TryFrom> for AnyConst16 { + type Error = OutOfBoundsConst; - fn try_from(value: u64) -> Result { - u16::try_from(value) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroI32) -> Result { - NonZeroI16::try_from(value) - .map(NonZeroI16::get) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroU32) -> Result { - NonZeroU16::try_from(value) - .map(NonZeroU16::get) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroI64) -> Result { - NonZeroI16::try_from(value) - .map(NonZeroI16::get) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: NonZeroU64) -> Result { - NonZeroU16::try_from(value) - .map(NonZeroU16::get) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } + fn try_from(value: NonZero<$ty>) -> Result { + >::try_from(value) + .map(>::get) + .map(Self::from) + .map_err(|_| OutOfBoundsConst) + } + } + )* + }; } +impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16); impl From for AnyConst16 { fn from(value: i8) -> Self { - Self(value as u8 as u16 as i16) + Self::from_bits(value as u8 as u16) } } impl From for AnyConst16 { fn from(value: i16) -> Self { - Self(value) + Self::from_bits(value as u16) } } impl From for AnyConst16 { fn from(value: u16) -> Self { - Self::from(value as i16) + Self::from_bits(value) } } impl From for i8 { fn from(value: AnyConst16) -> Self { - value.0 as i8 + value.bits as i8 } } impl From for i16 { fn from(value: AnyConst16) -> Self { - value.0 + u16::from(value) as i16 + } +} + +impl From for u16 { + fn from(value: AnyConst16) -> Self { + value.bits } } impl From for i32 { fn from(value: AnyConst16) -> Self { - Self::from(value.0) + Self::from(i16::from(value)) } } impl From for i64 { fn from(value: AnyConst16) -> Self { - Self::from(value.0) + Self::from(i16::from(value)) } } impl From for u32 { fn from(value: AnyConst16) -> Self { - Self::from(value.0 as u16) + Self::from(u16::from(value)) } } impl From for u64 { fn from(value: AnyConst16) -> Self { - Self::from(value.0 as u16) + Self::from(u16::from(value)) } } @@ -524,7 +309,16 @@ impl From for u64 { /// Upon use the small 32-bit value has to be sign-extended to /// the actual integer type, e.g. `i32` or `i64`. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub struct AnyConst32(u32); +pub struct AnyConst32 { + bits: u32, +} + +impl AnyConst32 { + /// Creates a new [`AnyConst32`] from the given `bits`. + fn from_bits(bits: u32) -> Self { + Self { bits } + } +} impl TryFrom for AnyConst32 { type Error = OutOfBoundsConst; @@ -564,63 +358,40 @@ impl From> for AnyConst32 { } } -impl From for AnyConst32 { - fn from(value: bool) -> Self { - Self::from(u32::from(value)) - } -} - -impl From for AnyConst32 { - fn from(value: i8) -> Self { - Self::from(value as u32) - } -} - -impl From for AnyConst32 { - fn from(value: i16) -> Self { - Self::from(value as u32) - } -} - -impl From for AnyConst32 { - fn from(value: i32) -> Self { - Self::from(value as u32) - } -} - -impl From for AnyConst32 { - fn from(value: u32) -> Self { - Self(value) - } +macro_rules! impl_from_for_anyconst32 { + ( $($ty:ty),* $(,)? ) => { + $( + impl From<$ty> for AnyConst32 { + fn from(value: $ty) -> Self { + Self::from_bits(value as _) + } + } + )* + }; } +impl_from_for_anyconst32!(bool, i8, u8, i16, u16, i32, u32); impl From for AnyConst32 { fn from(value: f32) -> Self { - Self::from(F32::from(value)) - } -} - -impl From for AnyConst32 { - fn from(value: F32) -> Self { - Self::from(value.to_bits()) + Self::from_bits(f32::to_bits(value)) } } impl From for i32 { fn from(value: AnyConst32) -> Self { - value.0 as _ + u32::from(value) as _ } } -impl From for u32 { +impl From for i64 { fn from(value: AnyConst32) -> Self { - value.0 + Self::from(i32::from(value)) } } -impl From for i64 { +impl From for u32 { fn from(value: AnyConst32) -> Self { - Self::from(i32::from(value)) + value.bits } } @@ -632,24 +403,12 @@ impl From for u64 { impl From for f32 { fn from(value: AnyConst32) -> Self { - f32::from_bits(u32::from(value)) - } -} - -impl From for F32 { - fn from(value: AnyConst32) -> Self { - F32::from(f32::from(value)) + Self::from_bits(u32::from(value)) } } impl From for f64 { fn from(value: AnyConst32) -> Self { - f64::from(f32::from_bits(u32::from(value))) - } -} - -impl From for F64 { - fn from(value: AnyConst32) -> Self { - F64::from(f64::from(value)) + Self::from(f32::from(value)) } } diff --git a/crates/wasmi/src/engine/translator/tests/wasm_type.rs b/crates/wasmi/src/engine/translator/tests/wasm_type.rs index 3d43736add..34eb3ceb58 100644 --- a/crates/wasmi/src/engine/translator/tests/wasm_type.rs +++ b/crates/wasmi/src/engine/translator/tests/wasm_type.rs @@ -1,7 +1,7 @@ use crate::core::ValType; use crate::{ - core::{UntypedVal, F32}, + core::UntypedVal, ir::{Const32, Instruction, Reg}, }; use core::fmt::Display; @@ -60,7 +60,7 @@ impl WasmTy for f32 { const VALUE_TYPE: ValType = ValType::F32; fn return_imm_instr(&self) -> Instruction { - Instruction::return_imm32(F32::from(*self)) + Instruction::return_imm32(*self) } }