From dba883e300ebb7c01b5c645921e78c3409b8c665 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 19 Jul 2024 17:12:29 -0700 Subject: [PATCH] split .cast() into cast_as_type and cast to be more useful when casting to deduced fixed types --- crates/fayalite/src/expr/ops.rs | 19 +++++++++++-------- crates/fayalite/src/int.rs | 5 ++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/crates/fayalite/src/expr/ops.rs b/crates/fayalite/src/expr/ops.rs index 9c20385..5900e30 100644 --- a/crates/fayalite/src/expr/ops.rs +++ b/crates/fayalite/src/expr/ops.rs @@ -11,7 +11,7 @@ use crate::{ }, int::{ DynInt, DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, Int, - IntCmp, IntTypeTrait, IntValue, UInt, UIntType, + IntCmp, IntType, IntTypeTrait, IntValue, UInt, UIntType, }, intern::{Intern, Interned}, reset::{ @@ -888,7 +888,7 @@ fixed_ary_op! { }, fn simulate(&self, sim_state: &mut SimState) -> _ { - self.value.simulate(sim_state).cast(self.ty.canonical()) + self.value.simulate(sim_state).cast_as_type(self.ty.canonical()) } fn expr_enum(&self) -> _ { @@ -935,7 +935,7 @@ impl< >, > Expr> { - pub fn cast< + pub fn cast_as_type< ToType: IntTypeTrait< CanonicalType = DynIntType<::Signed>, CanonicalValue = DynInt<::Signed>, @@ -946,17 +946,20 @@ impl< ) -> Expr { CastInt::::new_unchecked(self.canonical(), ty).to_expr() } + pub fn cast(self) -> Expr> { + self.cast_as_type(IntType::new()) + } pub fn as_same_width_uint(self) -> Expr> { - self.cast(self.ty().as_same_width_uint()) + self.cast_as_type(self.ty().as_same_width_uint()) } pub fn as_same_width_sint(self) -> Expr> { - self.cast(self.ty().as_same_width_sint()) + self.cast_as_type(self.ty().as_same_width_sint()) } pub fn as_same_value_uint(self) -> Expr { - self.cast(self.ty().as_same_value_uint()) + self.cast_as_type(self.ty().as_same_value_uint()) } pub fn as_same_value_sint(self) -> Expr { - self.cast(self.ty().as_same_value_sint()) + self.cast_as_type(self.ty().as_same_value_sint()) } } @@ -1020,7 +1023,7 @@ impl< return false.to_expr(); } let index = index.min(width - 1); - self.slice(index..=index).cast(UIntType::new()) + self.slice(index..=index).cast() } pub fn is_negative(self) -> Expr> { if T::Signed::VALUE { diff --git a/crates/fayalite/src/int.rs b/crates/fayalite/src/int.rs index adfa0df..4f31480 100644 --- a/crates/fayalite/src/int.rs +++ b/crates/fayalite/src/int.rs @@ -211,7 +211,7 @@ impl< IntValue::with_type(new_type, self.into_uint_value()) } } - pub fn cast< + pub fn cast_as_type< NewType: IntTypeTrait< CanonicalType = DynIntType<::Signed>, CanonicalValue = DynInt<::Signed>, @@ -226,6 +226,9 @@ impl< IntValue::with_type(new_type, self.uint_value()) } } + pub fn cast(self) -> Int { + self.cast_as_type(IntType::new()) + } pub fn as_same_width_uint(self) -> IntValue { IntValue { ty: self.ty.as_same_width_uint(),