diff --git a/crates/fayalite/src/expr.rs b/crates/fayalite/src/expr.rs index 0ffaf53..43d2a51 100644 --- a/crates/fayalite/src/expr.rs +++ b/crates/fayalite/src/expr.rs @@ -4,7 +4,7 @@ use crate::{ array::ArrayType, bundle::{BundleType, BundleValue, DynBundle, DynBundleType, FieldType}, enum_::{DynEnumType, EnumType, EnumValue}, - int::{DynSIntType, DynUIntType, FixedOrDynIntType, IntValue, UInt, UIntType}, + int::{DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, IntValue, UInt, UIntType}, intern::{Intern, Interned, InternedCompare, PtrEqWithTypeId, SupportsPtrEqWithTypeId}, memory::{DynPortType, MemPort, PortType}, module::{ @@ -288,7 +288,12 @@ impl Hash for Expr { impl Expr> where - T: FixedOrDynIntType<1, Signed = ConstBool>, + T: FixedOrDynIntType< + 1, + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, { pub fn as_bool(self) -> Expr> { assert_eq!(self.canonical_type().width, 1); diff --git a/crates/fayalite/src/expr/ops.rs b/crates/fayalite/src/expr/ops.rs index a2c242d..2ffe5d3 100644 --- a/crates/fayalite/src/expr/ops.rs +++ b/crates/fayalite/src/expr/ops.rs @@ -10,8 +10,8 @@ use crate::{ TargetPathDynArrayElement, TargetPathElement, ToExpr, }, int::{ - DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, Int, IntCmp, - IntTypeTrait, IntValue, UInt, UIntType, + DynInt, DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, Int, + IntCmp, IntTypeTrait, IntValue, UInt, UIntType, }, intern::{Intern, Interned}, reset::{ @@ -242,7 +242,7 @@ unary_op! { #[method = not] impl Not for _ where ( - T: IntTypeTrait, + T: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, ) { fn expr_enum(&self) -> _ { @@ -262,7 +262,7 @@ unary_op! { #[method = neg] impl Neg for _ where ( - T: IntTypeTrait>, + T: IntTypeTrait, CanonicalType = DynSIntType, CanonicalValue = DynSInt>, ) { fn expr_enum(&self) -> _ { @@ -361,48 +361,48 @@ binary_op! { #[method = bitand, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = BitAndU, expr_enum_s = BitAndS] impl BitAnd for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } binary_op! { #[method = bitor, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = BitOrU, expr_enum_s = BitOrS] impl BitOr for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } binary_op! { #[method = bitxor, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = BitXorU, expr_enum_s = BitXorS] impl BitXor for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } binary_op! { #[method = add, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = AddU, expr_enum_s = AddS] impl Add for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } binary_op! { #[method = sub, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = SubU, expr_enum_s = SubS] impl Sub for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } binary_op! { #[method = mul, rhs_to_canonical_dyn = to_canonical_dyn, expr_enum_u = MulU, expr_enum_s = MulS] impl Mul for _ where - L: IntTypeTrait, - R: IntTypeTrait, + L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, } macro_rules! dyn_shift_op { @@ -413,7 +413,7 @@ macro_rules! dyn_shift_op { fixed_ary_op! { pub struct $DynOp where ( - LhsType: IntTypeTrait, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, ) { pub lhs: Expr, @@ -458,8 +458,8 @@ macro_rules! dyn_shift_op { impl, LhsType, RhsType, Rhs: Value> ops::$Op> for Expr where - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + RhsType: IntTypeTrait, CanonicalType = DynUIntType, CanonicalValue = DynUInt>, { type Output = Expr<< as ops::$Op>>::Output as ValuelessTr>::Value>; @@ -470,8 +470,8 @@ macro_rules! dyn_shift_op { impl> ops::$Op> for IntValue where - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + RhsType: IntTypeTrait, CanonicalType = DynUIntType, CanonicalValue = DynUInt>, { type Output = Expr<< as ops::$Op>>::Output as ValuelessTr>::Value>; @@ -490,7 +490,7 @@ macro_rules! fixed_shift_op { fixed_ary_op! { pub struct $FixedOp where ( - LhsType: IntTypeTrait, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, ) { pub lhs: Expr, @@ -532,7 +532,7 @@ macro_rules! fixed_shift_op { impl, LhsType> ops::$Op for Expr where - LhsType: IntTypeTrait, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, { type Output = Expr<< as ops::$Op>::Output as ValuelessTr>::Value>; @@ -568,9 +568,9 @@ macro_rules! cmp_op { $(fixed_ary_op! { pub struct $CmpOp where ( - LhsType: IntTypeTrait, - RhsType: IntTypeTrait, - Output: FixedOrDynIntType<1, Signed = ConstBool>, + LhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>, ) { pub lhs: Expr, @@ -612,7 +612,7 @@ macro_rules! cmp_op { } })* - impl, Rhs: Value, Lhs: Value> IntCmp> for Expr { + impl::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Rhs: Value, Lhs: Value> IntCmp> for Expr { type Output = Expr>; $(fn $fn(self, rhs: Expr) -> Self::Output { @@ -620,7 +620,7 @@ macro_rules! cmp_op { })* } - impl, Rhs: Value, Lhs: Value> IntCmp for Expr { + impl::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Rhs: Value, Lhs: Value> IntCmp for Expr { type Output = Expr>; $(fn $fn(self, rhs: Rhs) -> Self::Output { @@ -628,7 +628,7 @@ macro_rules! cmp_op { })* } - impl, Rhs: Value> IntCmp> for IntValue { + impl::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Rhs: Value> IntCmp> for IntValue { type Output = Expr>; $(fn $fn(self, rhs: Expr) -> Self::Output { @@ -658,8 +658,8 @@ cmp_op! { fixed_ary_op! { pub struct CastInt where ( - FromType: IntTypeTrait, - ToType: IntTypeTrait, + FromType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, + ToType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, ) { pub value: Expr, @@ -705,9 +705,18 @@ fixed_ary_op! { } } -impl Expr> { +impl< + FromType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Expr> +{ pub fn cast< - ToType: IntTypeTrait::Signed>>, + ToType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, >( self, ty: ToType, @@ -731,7 +740,7 @@ impl Expr> { fixed_ary_op! { pub struct Slice where ( - Base: IntTypeTrait, + Base: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, ) { pub base: Expr, @@ -765,7 +774,13 @@ fixed_ary_op! { } } -impl Expr> { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Expr> +{ pub fn slice>(self, index: I) -> Expr { Slice::::new_unchecked( self.canonical(), @@ -841,7 +856,14 @@ impl_built_in_range!( std::ops::RangeInclusive); impl_built_in_range!( std::ops::RangeTo); impl_built_in_range!( std::ops::RangeToInclusive); -impl> ExprIndex for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + I: BuiltInRange, + > ExprIndex for IntValue +{ type OutputValue = DynUInt; fn expr_index(this: Expr, index: I) -> Expr { @@ -849,7 +871,13 @@ impl> ExprIndex for IntValue { } } -impl ExprIndex for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > ExprIndex for IntValue +{ type OutputValue = UInt<1>; fn expr_index(this: Expr, index: usize) -> Expr { @@ -862,7 +890,7 @@ macro_rules! reduce_bit_op { fixed_ary_op! { pub struct $name where ( - Output: FixedOrDynIntType<1, Signed = ConstBool>, + Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>, ) { pub arg: Expr, @@ -892,7 +920,14 @@ reduce_bit_op!(ReduceBitAnd; |bits| bits.all()); reduce_bit_op!(ReduceBitOr; |bits| bits.any()); reduce_bit_op!(ReduceBitXor; |bits| bits.count_ones() % 2 == 1); -impl>> Expr> { +impl< + T: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, + > Expr> +{ pub fn reduce_bitand(self) -> Expr> { ReduceBitAnd::>::new_unchecked(self.canonical()).to_expr() } @@ -939,7 +974,9 @@ fixed_ary_op! { }, #[target] target: Option> = base.target().map(|base| { - Intern::intern_sized(base.join(TargetPathElement::intern_sized(TargetPathBundleField{name:field.name}.into()))) + Intern::intern_sized(base.join( + TargetPathElement::intern_sized(TargetPathBundleField{ name: field.name }.into()), + )) }), fn simulate(&self, sim_state: &mut SimState) -> _ { todo!() @@ -985,9 +1022,9 @@ fixed_ary_op! { #[cache] base_ty: DynEnumType = base.canonical_type(), #[type] - ty: VariantTy = VariantTy::from_dyn_canonical_type(base_ty.variants()[variant_index].ty.unwrap_or_else( - || ().canonical_dyn(), - )), + ty: VariantTy = VariantTy::from_dyn_canonical_type( + base_ty.variants()[variant_index].ty.unwrap_or_else(|| ().canonical_dyn()), + ), #[cache] literal_bits: Result, ()> = { base.to_literal_bits().map(|base| { @@ -1086,7 +1123,9 @@ fixed_ary_op! { } fn expr_enum(&self) -> _ { - ExprEnum::CastBitsTo(CastBitsTo::new_unchecked(self.value, self.ty.canonical_dyn()).intern_sized()) + ExprEnum::CastBitsTo( + CastBitsTo::new_unchecked(self.value, self.ty.canonical_dyn()).intern_sized(), + ) } fn to_literal_bits(&self) -> Result, ()> { @@ -1105,7 +1144,14 @@ where } } -impl>> Expr> { +impl< + T: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, + > Expr> +{ pub fn cast_bits_to(self, ty: U::Type) -> Expr where U::Type: Type, @@ -1115,11 +1161,11 @@ impl>> Expr> { } macro_rules! cast_bit_to_typed_bit { - (pub struct $Op:ident$(<$T:ident: $Bound:ident<$BoundArg:tt>>)?; $FromType:ty => $ToType:ty) => { + (pub struct $Op:ident$(<$T:ident: $Bound:path>)?; $FromType:ty => $ToType:ty) => { fixed_ary_op! { pub struct $Op<$($T,)?> where ( - $($T: $Bound<$BoundArg>,)? + $($T: $Bound,)? ) { pub value: Expr<<$FromType as Type>::Value>, @@ -1244,7 +1290,7 @@ where } } -cast_bit_to_typed_bit!(pub struct CastClockToBit>; ClockType => ToType); +cast_bit_to_typed_bit!(pub struct CastClockToBit::Signed>, CanonicalValue = DynInt<::Signed>>>; ClockType => ToType); impl ToBit for Clock { fn to_bit(v: Expr) -> Expr> { @@ -1252,7 +1298,7 @@ impl ToBit for Clock { } } -cast_bit_to_typed_bit!(pub struct CastSyncResetToBit>; SyncResetType => ToType); +cast_bit_to_typed_bit!(pub struct CastSyncResetToBit::Signed>, CanonicalValue = DynInt<::Signed>>>; SyncResetType => ToType); impl ToBit for SyncReset { fn to_bit(v: Expr) -> Expr> { @@ -1260,7 +1306,7 @@ impl ToBit for SyncReset { } } -cast_bit_to_typed_bit!(pub struct CastAsyncResetToBit>; AsyncResetType => ToType); +cast_bit_to_typed_bit!(pub struct CastAsyncResetToBit::Signed>, CanonicalValue = DynInt<::Signed>>>; AsyncResetType => ToType); impl ToBit for AsyncReset { fn to_bit(v: Expr) -> Expr> { @@ -1268,7 +1314,7 @@ impl ToBit for AsyncReset { } } -cast_bit_to_typed_bit!(pub struct CastResetToBit>; ResetType => ToType); +cast_bit_to_typed_bit!(pub struct CastResetToBit::Signed>, CanonicalValue = DynInt<::Signed>>>; ResetType => ToType); impl ToBit for Reset { fn to_bit(v: Expr) -> Expr> { @@ -1355,7 +1401,9 @@ fixed_ary_op! { }, #[target] target: Option> = base.target().map(|base| { - Intern::intern_sized(base.join(TargetPathElement::ArrayElement(TargetPathArrayElement { index }).intern())) + Intern::intern_sized(base.join( + TargetPathElement::ArrayElement(TargetPathArrayElement { index }).intern(), + )) }), fn simulate(&self, sim_state: &mut SimState) -> _ { todo!() @@ -1398,21 +1446,28 @@ fixed_ary_op! { let base_ty = base.ty(); let element_bit_width = base_ty.element().bit_width(); base.to_literal_bits().ok().zip(index.to_literal_bits().ok()).and_then(|(base, index)| { - let start = DynUInt::from_bit_slice(&index).uint_value().to_usize()?.checked_mul(element_bit_width)?; + let start = DynUInt::from_bit_slice(&index) + .uint_value() + .to_usize()? + .checked_mul(element_bit_width)?; let end = start.checked_add(element_bit_width)?; Some(base.get(start..end)?.intern()) }).ok_or(()) }, #[target] target: Option> = base.target().map(|base| { - Intern::intern_sized(base.join(TargetPathElement::DynArrayElement(TargetPathDynArrayElement {}).intern())) + Intern::intern_sized(base.join( + TargetPathElement::DynArrayElement(TargetPathDynArrayElement {}).intern(), + )) }), fn simulate(&self, sim_state: &mut SimState) -> _ { todo!() } fn expr_enum(&self) -> _ { - ExprEnum::DynArrayIndex(DynArrayIndex::new_unchecked(self.base, self.index).intern_sized()) + ExprEnum::DynArrayIndex( + DynArrayIndex::new_unchecked(self.base, self.index).intern_sized(), + ) } fn to_literal_bits(&self) -> Result, ()> { @@ -1432,7 +1487,11 @@ impl DynArrayIndex { impl< VA: ?Sized + ValueArrayOrSlice, - Idx: IntTypeTrait, CanonicalType = DynUIntType>, + Idx: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > ExprIndex> for Array { type OutputValue = VA::Element; @@ -1445,7 +1504,11 @@ impl< impl< VA: ?Sized + ValueArrayOrSlice, - Idx: IntTypeTrait, CanonicalType = DynUIntType>, + Idx: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > ExprIndex>> for Array { type OutputValue = VA::Element; @@ -1555,7 +1618,8 @@ fixed_ary_op! { ty }, #[cache] - literal_bits: Result, ()> = ty.variant_to_bits(variant_index, variant_value.as_ref()), + literal_bits: Result, ()> = + ty.variant_to_bits(variant_index, variant_value.as_ref()), fn simulate(&self, sim_state: &mut SimState) -> _ { todo!() } diff --git a/crates/fayalite/src/int.rs b/crates/fayalite/src/int.rs index 63f9f6f..adfa0df 100644 --- a/crates/fayalite/src/int.rs +++ b/crates/fayalite/src/int.rs @@ -35,7 +35,13 @@ pub type Int = IntValue>; pub type UInt = Int, WIDTH>; pub type SInt = Int, WIDTH>; -impl ToExpr for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > ToExpr for IntValue +{ type Type = T; fn ty(&self) -> Self::Type { @@ -47,13 +53,25 @@ impl ToExpr for IntValue { } } -impl fmt::Display for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > fmt::Display for IntValue +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.to_canonical(), f) } } -impl fmt::Debug for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > fmt::Debug for IntValue +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let neg; let (sign, magnitude) = if self.is_negative() { @@ -93,7 +111,13 @@ impl DynInt { } } -impl IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > IntValue +{ pub fn with_type>(ty: T, value: I) -> Self { let int_type = ty.canonical(); let mut value: BigInt = value.into(); @@ -172,14 +196,30 @@ impl IntValue { let width = self.ty.width(); T::Signed::VALUE && width > 0 && self.uint_value.bit((width - 1).try_into().unwrap()) } - pub fn into_cast(self, new_type: NewType) -> IntValue { + pub fn into_cast< + NewType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + >( + self, + new_type: NewType, + ) -> IntValue { if new_type.width() > self.ty.width() && self.is_negative() { IntValue::with_type(new_type, self.into_value()) } else { IntValue::with_type(new_type, self.into_uint_value()) } } - pub fn cast(&self, new_type: NewType) -> IntValue { + pub fn cast< + NewType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + >( + &self, + new_type: NewType, + ) -> IntValue { if new_type.width() > self.ty.width() && self.is_negative() { IntValue::with_type(new_type, self.value()) } else { @@ -221,7 +261,14 @@ impl IntValue { assert!(index < self.ty.width(), "bit index out of range"); self.uint_value.set_bit(index.try_into().unwrap(), value) } - pub fn set_slice, RhsType: IntTypeTrait>>( + pub fn set_slice< + I: RangeBounds, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, + >( &mut self, index: I, value: impl Into>, @@ -240,7 +287,11 @@ impl IntValue { } pub fn with_replaced_slice< I: RangeBounds, - RhsType: IntTypeTrait>, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, >( mut self, index: I, @@ -249,7 +300,12 @@ impl IntValue { self.set_slice(index, value); self } - pub fn concat( + pub fn concat< + HighType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + >( &self, high_part: IntValue, ) -> IntValue> { @@ -279,7 +335,13 @@ impl IntValue { } } -impl Value for IntValue { +impl< + Ty: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Value for IntValue +{ fn to_canonical(&self) -> ::CanonicalValue { IntValue { ty: self.ty.canonical(), @@ -295,7 +357,13 @@ impl Value for IntValue { } } impl Copy for ToBitsMemoize {} - impl Memoize for ToBitsMemoize> { + impl< + Ty: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Memoize for ToBitsMemoize> + { type Input = IntValue; type InputOwned = IntValue; type Output = Interned; @@ -321,8 +389,15 @@ macro_rules! impl_add_sub { ($Op:ident::$op:ident) => { impl $Op> for IntValue where - LhsType: IntTypeTrait, - RhsType: IntTypeTrait::Signed>, + LhsType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = ::Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, { type Output = IntValue>; @@ -334,8 +409,15 @@ macro_rules! impl_add_sub { impl $Op> for Valueless where - LhsType: IntTypeTrait, - RhsType: IntTypeTrait::Signed>, + LhsType: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = ::Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, { type Output = Valueless>; @@ -357,8 +439,16 @@ macro_rules! impl_bitwise { ($Op:ident::$op:ident) => { impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, > $Op> for IntValue { type Output = IntValue>; @@ -371,8 +461,16 @@ macro_rules! impl_bitwise { impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, > $Op> for Valueless { type Output = Valueless>; @@ -390,8 +488,16 @@ impl_add_sub!(Sub::sub); impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, > Mul> for IntValue { type Output = IntValue>; @@ -403,8 +509,16 @@ impl< impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, > Mul> for Valueless { type Output = Valueless>; @@ -425,8 +539,16 @@ impl_bitwise!(BitXor::bitxor); impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > Shl> for IntValue { type Output = IntValue>; @@ -447,8 +569,16 @@ impl< impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > Shl> for Valueless { type Output = Valueless>; @@ -469,8 +599,16 @@ impl< impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > Shr> for IntValue { type Output = IntValue>; @@ -488,8 +626,12 @@ impl< impl< Signed: GenericConstBool, - LhsType: IntTypeTrait, - RhsType: IntTypeTrait>, + LhsType: IntTypeTrait>, + RhsType: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, > Shr> for Valueless { type Output = Valueless>; @@ -501,8 +643,14 @@ impl< } } -impl> Shl - for IntValue +impl< + Signed: GenericConstBool, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Shl for IntValue { type Output = IntValue>; @@ -512,8 +660,14 @@ impl> Shl> Shl - for Valueless +impl< + Signed: GenericConstBool, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Shl for Valueless { type Output = Valueless>; @@ -528,8 +682,14 @@ impl> Shl> Shr - for IntValue +impl< + Signed: GenericConstBool, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Shr for IntValue { type Output = IntValue>; @@ -541,8 +701,14 @@ impl> Shr> Shr - for Valueless +impl< + Signed: GenericConstBool, + LhsType: IntTypeTrait< + Signed = Signed, + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Shr for Valueless { type Output = Valueless>; @@ -552,7 +718,14 @@ impl> Shr>> Neg for IntValue { +impl< + T: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynSIntType, + CanonicalValue = DynSInt, + >, + > Neg for IntValue +{ type Output = IntValue; fn neg(self) -> Self::Output { let ty = self.valueless().neg().ty; @@ -560,7 +733,14 @@ impl>> Neg for IntValue { } } -impl>> Neg for Valueless { +impl< + T: IntTypeTrait< + Signed = ConstBool, + CanonicalType = DynSIntType, + CanonicalValue = DynSInt, + >, + > Neg for Valueless +{ type Output = Valueless; fn neg(self) -> Self::Output { let ty = DynIntType::new( @@ -573,14 +753,26 @@ impl>> Neg for Valueless { } } -impl Not for IntValue { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Not for IntValue +{ type Output = IntValue; fn not(self) -> Self::Output { IntValue::with_type(self.valueless().not().ty, Not::not(self.into_value())) } } -impl Not for Valueless { +impl< + T: IntTypeTrait< + CanonicalType = DynIntType<::Signed>, + CanonicalValue = DynInt<::Signed>, + >, + > Not for Valueless +{ type Output = Valueless; fn not(self) -> Self::Output { self @@ -626,7 +818,15 @@ impl_int!(i32, true); impl_int!(i64, true); impl_int!(i128, true); -impl>> From for IntValue { +impl< + T: FixedOrDynIntType< + 1, + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, + > From for IntValue +{ fn from(v: bool) -> Self { IntValue::with_type(T::new(), v) } @@ -665,11 +865,7 @@ pub trait IntTypeTrait: + Eq + Hash + fmt::Debug - + Type< - Value = IntValue, - CanonicalType = DynIntType<::Signed>, - CanonicalValue = IntValue::Signed>>, - > + + Type> + Connect + Connect::Signed>> { @@ -678,11 +874,15 @@ pub trait IntTypeTrait: Signed = ConstBool, SameWidthUInt = Self::SameWidthUInt, SameWidthSInt = Self::SameWidthSInt, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, >; type SameWidthSInt: IntTypeTrait< Signed = ConstBool, SameWidthUInt = Self::SameWidthUInt, SameWidthSInt = Self::SameWidthSInt, + CanonicalType = DynSIntType, + CanonicalValue = DynSInt, >; fn from_width_unchecked(width: usize) -> Self; fn width(self) -> usize; @@ -735,12 +935,12 @@ pub trait IntTypeTrait: start..end } fn slice_and_shift>(self, index: I) -> (DynUIntType, usize) { - let range = self.canonical().slice_index_to_range(index); + let range = self.slice_index_to_range(index); let width = range.end - range.start; (DynUIntType::new(width), range.start) } fn slice>(self, index: I) -> DynUIntType { - self.canonical().slice_and_shift(index).0 + self.slice_and_shift(index).0 } } diff --git a/crates/fayalite/src/module.rs b/crates/fayalite/src/module.rs index 99bf99b..1537b3a 100644 --- a/crates/fayalite/src/module.rs +++ b/crates/fayalite/src/module.rs @@ -11,7 +11,7 @@ use crate::{ ops::VariantAccess, Expr, Flow, Target, TargetBase, TargetPathArrayElement, TargetPathBundleField, TargetPathElement, ToExpr, }, - int::{FixedOrDynIntType, UInt}, + int::{DynUInt, DynUIntType, FixedOrDynIntType, UInt}, intern::{Intern, Interned}, memory::{Mem, MemBuilder, MemBuilderTarget, PortName}, reg::Reg, @@ -2254,7 +2254,12 @@ where #[track_caller] pub fn if_(&mut self, cond: impl ToExpr) -> IfScope where - Ty: FixedOrDynIntType<1, Signed = ConstBool>, + Ty: FixedOrDynIntType< + 1, + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, { self.if_with_loc(cond, SourceLocation::caller()) } @@ -2264,7 +2269,12 @@ where source_location: SourceLocation, ) -> IfScope where - Ty: FixedOrDynIntType<1, Signed = ConstBool>, + Ty: FixedOrDynIntType< + 1, + Signed = ConstBool, + CanonicalType = DynUIntType, + CanonicalValue = DynUInt, + >, { let cond = cond.to_expr().as_bool(); let outer_block = self.block_stack.top(); diff --git a/crates/fayalite/src/module/transform/visit.rs b/crates/fayalite/src/module/transform/visit.rs index 88af5c3..95c4772 100644 --- a/crates/fayalite/src/module/transform/visit.rs +++ b/crates/fayalite/src/module/transform/visit.rs @@ -11,7 +11,10 @@ use crate::{ ops, Expr, ExprEnum, Literal, Target, TargetBase, TargetChild, TargetPathArrayElement, TargetPathBundleField, TargetPathDynArrayElement, TargetPathElement, ToExpr, }, - int::{DynInt, DynIntType, FixedOrDynIntType, IntType, IntTypeTrait}, + int::{ + DynInt, DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, IntType, + IntTypeTrait, + }, intern::{Intern, Interned}, memory::{Mem, MemPort, PortKind, PortName, PortType, ReadUnderWrite}, module::{ diff --git a/crates/fayalite/src/valueless.rs b/crates/fayalite/src/valueless.rs index 4aa6437..d34905e 100644 --- a/crates/fayalite/src/valueless.rs +++ b/crates/fayalite/src/valueless.rs @@ -68,10 +68,9 @@ impl Valueless { &self, high_part: Valueless, ) -> Valueless> { - let self_type = self.ty.canonical(); let ty = DynIntType::new( - self_type - .width + self.ty + .width() .checked_add(high_part.ty.width()) .expect("result has too many bits"), ); diff --git a/crates/fayalite/visit_types.json b/crates/fayalite/visit_types.json index 9a8e524..0304e06 100644 --- a/crates/fayalite/visit_types.json +++ b/crates/fayalite/visit_types.json @@ -376,7 +376,7 @@ "$constructor": "ops::Not::new_unchecked", "arg": "Visible" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::Neg": { "data": { @@ -384,7 +384,7 @@ "$constructor": "ops::Neg::new_unchecked", "arg": "Visible" }, - "generics": ">>" + "generics": ", CanonicalType = DynSIntType, CanonicalValue = DynSInt>>" }, "ops::BitAnd": { "data": { @@ -395,7 +395,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::BitOr": { @@ -407,7 +407,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::BitXor": { @@ -419,7 +419,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::Add": { @@ -431,7 +431,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::Sub": { @@ -443,7 +443,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::Mul": { @@ -455,7 +455,7 @@ }, "generics": { "generics": "", - "where": "L: IntTypeTrait, R: IntTypeTrait" + "where": "L: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, R: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>" } }, "ops::DynShl": { @@ -465,7 +465,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::DynShr": { "data": { @@ -474,7 +474,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::FixedShl": { "data": { @@ -483,7 +483,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::FixedShr": { "data": { @@ -492,7 +492,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::CmpLt": { "data": { @@ -501,7 +501,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CmpLe": { "data": { @@ -510,7 +510,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CmpGt": { "data": { @@ -519,7 +519,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CmpGe": { "data": { @@ -528,7 +528,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CmpEq": { "data": { @@ -537,7 +537,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CmpNe": { "data": { @@ -546,7 +546,7 @@ "lhs": "Visible", "rhs": "Visible" }, - "generics": ", Output: FixedOrDynIntType<1, Signed = ConstBool>>" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, RhsType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>, Output: FixedOrDynIntType<1, Signed = ConstBool, CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::CastInt": { "data": { @@ -555,7 +555,7 @@ "value": "Visible", "ty()": "Visible" }, - "generics": "", + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>, ToType: IntTypeTrait::Signed>, CanonicalValue = DynInt<::Signed>>>", "fold_where": "ToType: Fold", "visit_where": "ToType: Visit" }, @@ -566,7 +566,7 @@ "base": "Visible", "range": "Opaque" }, - "generics": "" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::ReduceBitAnd": { "data": { @@ -574,7 +574,7 @@ "$constructor": "ops::ReduceBitAnd::new_unchecked", "arg": "Visible" }, - "generics": ">>" + "generics": ", CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::ReduceBitOr": { "data": { @@ -582,7 +582,7 @@ "$constructor": "ops::ReduceBitOr::new_unchecked", "arg": "Visible" }, - "generics": ">>" + "generics": ", CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::ReduceBitXor": { "data": { @@ -590,7 +590,7 @@ "$constructor": "ops::ReduceBitXor::new_unchecked", "arg": "Visible" }, - "generics": ">>" + "generics": ", CanonicalType = DynUIntType, CanonicalValue = DynUInt>>" }, "ops::FieldAccess": { "data": { @@ -696,7 +696,7 @@ "$constructor": "ops::CastClockToBit::new_unchecked", "value": "Visible" }, - "generics": ">" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::CastSyncResetToBit": { "data": { @@ -704,7 +704,7 @@ "$constructor": "ops::CastSyncResetToBit::new_unchecked", "value": "Visible" }, - "generics": ">" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::CastAsyncResetToBit": { "data": { @@ -712,7 +712,7 @@ "$constructor": "ops::CastAsyncResetToBit::new_unchecked", "value": "Visible" }, - "generics": ">" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "ops::CastResetToBit": { "data": { @@ -720,7 +720,7 @@ "$constructor": "ops::CastResetToBit::new_unchecked", "value": "Visible" }, - "generics": ">" + "generics": "::Signed>, CanonicalValue = DynInt<::Signed>>>" }, "BlockId": { "data": {