From ac40cf8d3b8dc03b318bdc42934a7bf42ac572b3 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 11:09:18 +0200 Subject: [PATCH 01/13] use macros to generate Const16 impls --- crates/ir/src/immeditate.rs | 228 ++++++++++-------------------------- 1 file changed, 61 insertions(+), 167 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 49d129cf07..f338ba16d2 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -1,5 +1,6 @@ use crate::core::{F32, F64}; use core::{ + num::NonZero, fmt::Debug, marker::PhantomData, num::{NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU16, NonZeroU32, NonZeroU64}, @@ -58,173 +59,66 @@ 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 { From 005b131f7801c993ed1019f590739174c4ea3187 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:05:36 +0200 Subject: [PATCH 02/13] use macro for Const32 from impls --- crates/ir/src/immeditate.rs | 124 +++++++++++------------------------- 1 file changed, 37 insertions(+), 87 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index f338ba16d2..a735dfb38a 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -172,95 +172,45 @@ 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) - } -} +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; -impl TryFrom for Const32 { - 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. /// From 592106faaa3e3cf63f8690a8834d455712019417 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:06:20 +0200 Subject: [PATCH 03/13] use macro to generate AnyConst16 from impls --- crates/ir/src/immeditate.rs | 108 ++++++++++-------------------------- 1 file changed, 28 insertions(+), 80 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index a735dfb38a..de49a6d9c1 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -3,7 +3,6 @@ use core::{ num::NonZero, fmt::Debug, marker::PhantomData, - num::{NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU16, NonZeroU32, NonZeroU64}, }; /// Error that may occur upon converting values to [`Const16`]. @@ -222,89 +221,38 @@ impl_const32!( #[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) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: u32) -> Result { - u16::try_from(value) - .map(Self::from) - .map_err(|_| OutOfBoundsConst) - } -} - -impl TryFrom for AnyConst16 { - type Error = OutOfBoundsConst; - - fn try_from(value: i64) -> Result { - i16::try_from(value) - .map(Self::from) - .map_err(|_| 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; +macro_rules! impl_any_const16 { + ( $( $ty:ty as $ty16:ty ),* $(,)? ) => { + $( + impl TryFrom<$ty> for AnyConst16 { + type Error = OutOfBoundsConst; - fn try_from(value: NonZeroI64) -> Result { - NonZeroI16::try_from(value) - .map(NonZeroI16::get) - .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: 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 { From 4b26c71a39761271e967fa5da8af7a6503015f4b Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:14:07 +0200 Subject: [PATCH 04/13] use u16 in AnyConst16 --- crates/ir/src/immeditate.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index de49a6d9c1..d494d88f86 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -219,7 +219,7 @@ impl_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); +pub struct AnyConst16(u16); macro_rules! impl_any_const16 { ( $( $ty:ty as $ty16:ty ),* $(,)? ) => { @@ -256,19 +256,19 @@ impl_any_const16!( impl From for AnyConst16 { fn from(value: i8) -> Self { - Self(value as u8 as u16 as i16) + Self(value as u8 as u16) } } impl From for AnyConst16 { fn from(value: i16) -> Self { - Self(value) + Self(value as u16) } } impl From for AnyConst16 { fn from(value: u16) -> Self { - Self::from(value as i16) + Self(value) } } @@ -279,6 +279,12 @@ impl From for i8 { } impl From for i16 { + fn from(value: AnyConst16) -> Self { + u16::from(value) as i16 + } +} + +impl From for u16 { fn from(value: AnyConst16) -> Self { value.0 } @@ -286,25 +292,25 @@ impl From for i16 { 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)) } } From f393ecdda3474211016d39b5ee537e40906227d2 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:16:29 +0200 Subject: [PATCH 05/13] use named bits field for AnyConst16 --- crates/ir/src/immeditate.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index d494d88f86..0c87d6d689 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -219,7 +219,9 @@ impl_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(u16); +pub struct AnyConst16 { + bits: u16, +} macro_rules! impl_any_const16 { ( $( $ty:ty as $ty16:ty ),* $(,)? ) => { @@ -254,27 +256,34 @@ impl_any_const16!( u64 as u16, ); +impl AnyConst16 { + /// Creates a new [`AnyConst16`] from the given `bits`. + fn from_bits(bits: u16) -> Self { + Self { bits } + } +} + impl From for AnyConst16 { fn from(value: i8) -> Self { - Self(value as u8 as u16) + Self::from_bits(value as u8 as u16) } } impl From for AnyConst16 { fn from(value: i16) -> Self { - Self(value as u16) + Self::from_bits(value as u16) } } impl From for AnyConst16 { fn from(value: u16) -> Self { - Self(value) + Self::from_bits(value) } } impl From for i8 { fn from(value: AnyConst16) -> Self { - value.0 as i8 + value.bits as i8 } } @@ -286,7 +295,7 @@ impl From for i16 { impl From for u16 { fn from(value: AnyConst16) -> Self { - value.0 + value.bits } } From cd8294e655177e998fafae51b44a5acfcbbe7efb Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:18:28 +0200 Subject: [PATCH 06/13] use named bits field in AnyConst32 --- crates/ir/src/immeditate.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 0c87d6d689..4f82488653 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -331,7 +331,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; @@ -397,7 +406,7 @@ impl From for AnyConst32 { impl From for AnyConst32 { fn from(value: u32) -> Self { - Self(value) + Self::from_bits(value) } } @@ -415,13 +424,13 @@ impl From for AnyConst32 { impl From for i32 { fn from(value: AnyConst32) -> Self { - value.0 as _ + value.bits as _ } } impl From for u32 { fn from(value: AnyConst32) -> Self { - value.0 + value.bits } } From 3838a81343b673e7e273662c5b3fc5900a9d66e5 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:19:10 +0200 Subject: [PATCH 07/13] apply rustfmt --- crates/ir/src/immeditate.rs | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 4f82488653..89038d6514 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -1,9 +1,5 @@ use crate::core::{F32, F64}; -use core::{ - num::NonZero, - fmt::Debug, - marker::PhantomData, -}; +use core::{fmt::Debug, marker::PhantomData, num::NonZero}; /// Error that may occur upon converting values to [`Const16`]. #[derive(Debug, Copy, Clone)] @@ -75,12 +71,7 @@ macro_rules! impl_const16_from { )* } } -impl_const16_from!( - (i16, i32), - (u16, u32), - (i16, i64), - (u16, u64), -); +impl_const16_from!((i16, i32), (u16, u32), (i16, i64), (u16, u64),); macro_rules! impl_const16_from { ( $($ty:ty),* ) => { @@ -202,14 +193,7 @@ macro_rules! impl_const32 { }; () => {}; } -impl_const32!( - i32, - u32, - i64 as i32, - u64 as u32, - f32, - f64 as f32, -); +impl_const32!(i32, u32, i64 as i32, u64 as u32, f32, f64 as f32,); /// A 16-bit constant value of any type. /// @@ -249,12 +233,7 @@ macro_rules! impl_any_const16 { )* }; } -impl_any_const16!( - i32 as i16, - u32 as u16, - i64 as i16, - u64 as u16, -); +impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16,); impl AnyConst16 { /// Creates a new [`AnyConst16`] from the given `bits`. From c75b57d96293e3c409fde4d389ab729fbd3db1c6 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:20:20 +0200 Subject: [PATCH 08/13] remove unnecessary comma --- crates/ir/src/immeditate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 89038d6514..8fd5a835b0 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -233,7 +233,7 @@ macro_rules! impl_any_const16 { )* }; } -impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16,); +impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16); impl AnyConst16 { /// Creates a new [`AnyConst16`] from the given `bits`. From 4fa73ce816018ca1f57a64f1e0df4058626d31a6 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:20:45 +0200 Subject: [PATCH 09/13] move AnyConst16::from_bits up --- crates/ir/src/immeditate.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 8fd5a835b0..9c6c1e5e0f 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -207,6 +207,13 @@ pub struct AnyConst16 { bits: u16, } +impl AnyConst16 { + /// Creates a new [`AnyConst16`] from the given `bits`. + fn from_bits(bits: u16) -> Self { + Self { bits } + } +} + macro_rules! impl_any_const16 { ( $( $ty:ty as $ty16:ty ),* $(,)? ) => { $( @@ -235,13 +242,6 @@ macro_rules! impl_any_const16 { } impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16); -impl AnyConst16 { - /// Creates a new [`AnyConst16`] from the given `bits`. - fn from_bits(bits: u16) -> Self { - Self { bits } - } -} - impl From for AnyConst16 { fn from(value: i8) -> Self { Self::from_bits(value as u8 as u16) From 6e86a2cca10e033eee77c6c5e635f0ab4d35d5f1 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:30:18 +0200 Subject: [PATCH 10/13] remove F{32,64} from impls from AnyConst32 --- crates/ir/src/immeditate.rs | 21 ------------------- .../src/engine/translator/tests/wasm_type.rs | 4 ++-- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 9c6c1e5e0f..fd73095cf9 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -1,4 +1,3 @@ -use crate::core::{F32, F64}; use core::{fmt::Debug, marker::PhantomData, num::NonZero}; /// Error that may occur upon converting values to [`Const16`]. @@ -395,12 +394,6 @@ impl From for AnyConst32 { } } -impl From for AnyConst32 { - fn from(value: F32) -> Self { - Self::from(value.to_bits()) - } -} - impl From for i32 { fn from(value: AnyConst32) -> Self { value.bits as _ @@ -427,24 +420,10 @@ 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)) } } 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)) } } 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) } } From ab3cfbb17ce3180acd5de2a31d9252e4a4717c45 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:30:28 +0200 Subject: [PATCH 11/13] simplify AnyConst32 from impls --- crates/ir/src/immeditate.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index fd73095cf9..e91b76bb24 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -360,43 +360,43 @@ impl From> for AnyConst32 { impl From for AnyConst32 { fn from(value: bool) -> Self { - Self::from(u32::from(value)) + Self::from_bits(value as _) } } impl From for AnyConst32 { fn from(value: i8) -> Self { - Self::from(value as u32) + Self::from_bits(value as _) } } impl From for AnyConst32 { fn from(value: i16) -> Self { - Self::from(value as u32) + Self::from_bits(value as _) } } impl From for AnyConst32 { fn from(value: i32) -> Self { - Self::from(value as u32) + Self::from_bits(value as _) } } impl From for AnyConst32 { fn from(value: u32) -> Self { - Self::from_bits(value) + Self::from_bits(value as _) } } impl From for AnyConst32 { fn from(value: f32) -> Self { - Self::from(F32::from(value)) + Self::from_bits(f32::to_bits(value)) } } impl From for i32 { fn from(value: AnyConst32) -> Self { - value.bits as _ + u32::from(value) as _ } } @@ -420,10 +420,12 @@ impl From for u64 { impl From for f32 { fn from(value: AnyConst32) -> Self { + Self::from_bits(u32::from(value)) } } impl From for f64 { fn from(value: AnyConst32) -> Self { + Self::from(f32::from(value)) } } From 85a6f61c59b93c5b581bb98fdc38dc9028fa2ed6 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:32:55 +0200 Subject: [PATCH 12/13] generate from impls for AnyConst32 via macro --- crates/ir/src/immeditate.rs | 39 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index e91b76bb24..3150b1e59f 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -358,35 +358,18 @@ impl From> for AnyConst32 { } } -impl From for AnyConst32 { - fn from(value: bool) -> Self { - Self::from_bits(value as _) - } -} - -impl From for AnyConst32 { - fn from(value: i8) -> Self { - Self::from_bits(value as _) - } -} - -impl From for AnyConst32 { - fn from(value: i16) -> Self { - Self::from_bits(value as _) - } -} - -impl From for AnyConst32 { - fn from(value: i32) -> Self { - Self::from_bits(value as _) - } -} - -impl From for AnyConst32 { - fn from(value: u32) -> Self { - Self::from_bits(value as _) - } +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 { From 2a55fda9f179db09119af0174b0799e1bf2fbc55 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 1 Jul 2025 12:35:35 +0200 Subject: [PATCH 13/13] swap method order --- crates/ir/src/immeditate.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ir/src/immeditate.rs b/crates/ir/src/immeditate.rs index 3150b1e59f..a9efa6e8c9 100644 --- a/crates/ir/src/immeditate.rs +++ b/crates/ir/src/immeditate.rs @@ -383,15 +383,15 @@ impl From for i32 { } } -impl From for u32 { +impl From for i64 { fn from(value: AnyConst32) -> Self { - value.bits + 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 } }