simplify getting IMM_WIDTH for LogicalFlagsMOpImm

This commit is contained in:
Jacob Lifshay 2026-01-25 20:25:25 -08:00
parent 0824b63d31
commit 2ad469e331
Signed by: programmerjake
SSH key fingerprint: SHA256:HnFTLGpSm4Q4Fj502oCFisjZSoakwEuTsJJMSke63RQ

View file

@ -156,6 +156,13 @@ pub trait CommonMOpTrait: MOpTrait {
) -> Expr<Self::Mapped<NewDestReg, NewSrcRegWidth>>; ) -> Expr<Self::Mapped<NewDestReg, NewSrcRegWidth>>;
} }
pub type CommonMOpFor<T> = CommonMOp<
<T as CommonMOpTrait>::PrefixPad,
<T as CommonMOpTrait>::CommonMOpTraitDestReg,
<T as CommonMOpTrait>::CommonMOpTraitSrcRegWidth,
<T as CommonMOpTrait>::SrcCount,
>;
impl<T: CommonMOpTrait> MOpTrait for T { impl<T: CommonMOpTrait> MOpTrait for T {
type Mapped<NewDestReg: Type, NewSrcRegWidth: Size> = type Mapped<NewDestReg: Type, NewSrcRegWidth: Size> =
T::CommonMOpTraitMapped<NewDestReg, NewSrcRegWidth>; T::CommonMOpTraitMapped<NewDestReg, NewSrcRegWidth>;
@ -984,8 +991,8 @@ pub struct LogicalFlagsMOpImm {
#[test] #[test]
fn test_logical_flags_mop_imm_fits() { fn test_logical_flags_mop_imm_fits() {
let needed_width = LogicalFlagsMOpImm.canonical().bit_width(); let needed_width = LogicalFlagsMOpImm.canonical().bit_width();
type TheMOpCommon = LogicalFlagsMOpCommon<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>; let imm_width =
let imm_width = TheMOpCommon::IMM_WIDTH; CommonMOpFor::<LogicalFlagsMOp<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>>::IMM_WIDTH;
assert!( assert!(
needed_width <= imm_width, needed_width <= imm_width,
"needed_width={needed_width} imm_width={imm_width}", "needed_width={needed_width} imm_width={imm_width}",
@ -1083,7 +1090,7 @@ impl LogicalFlagsMOpImm {
#[track_caller] #[track_caller]
pub fn from_imm(imm: impl ToExpr<Type = SInt>) -> Expr<Self> { pub fn from_imm(imm: impl ToExpr<Type = SInt>) -> Expr<Self> {
let imm_ty = let imm_ty =
LogicalFlagsMOpCommon::<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>::imm_ty(); CommonMOpFor::<LogicalFlagsMOp<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>>::imm_ty();
let imm = imm.to_expr(); let imm = imm.to_expr();
assert_eq!(imm_ty, imm.ty(), "imm must have the correct width"); assert_eq!(imm_ty, imm.ty(), "imm must have the correct width");
imm.cast_to(UInt[LogicalFlagsMOpImm.canonical().bit_width()]) imm.cast_to(UInt[LogicalFlagsMOpImm.canonical().bit_width()])
@ -1091,7 +1098,7 @@ impl LogicalFlagsMOpImm {
} }
pub fn to_imm(this: impl ToExpr<Type = Self>) -> Expr<SInt> { pub fn to_imm(this: impl ToExpr<Type = Self>) -> Expr<SInt> {
let imm_ty = let imm_ty =
LogicalFlagsMOpCommon::<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>::imm_ty(); CommonMOpFor::<LogicalFlagsMOp<MOpDestReg, ConstUsize<{ MOpRegNum::WIDTH }>>>::imm_ty();
this.to_expr().cast_to_bits().cast_to(imm_ty) this.to_expr().cast_to_bits().cast_to(imm_ty)
} }
fn flags_operation_impl<Flags, I, IU, C, CU, Lut, U, B, AF>( fn flags_operation_impl<Flags, I, IU, C, CU, Lut, U, B, AF>(
@ -1384,10 +1391,6 @@ impl LogicalFlagsMOpImm {
} }
} }
#[hdl]
type LogicalFlagsMOpCommon<DestReg: Type, SrcRegWidth: Size> =
CommonMOp<ConstUsize<0>, DestReg, SrcRegWidth, ConstUsize<3>>;
common_mop_struct! { common_mop_struct! {
#[mapped(<NewDestReg, NewSrcRegWidth> LogicalFlagsMOp<NewDestReg, NewSrcRegWidth>)] #[mapped(<NewDestReg, NewSrcRegWidth> LogicalFlagsMOp<NewDestReg, NewSrcRegWidth>)]
#[hdl(cmp_eq)] #[hdl(cmp_eq)]
@ -1461,7 +1464,7 @@ common_mop_struct! {
/// ``` /// ```
pub struct LogicalFlagsMOp<DestReg: Type, SrcRegWidth: Size> { pub struct LogicalFlagsMOp<DestReg: Type, SrcRegWidth: Size> {
#[common] #[common]
pub common: LogicalFlagsMOpCommon<DestReg, SrcRegWidth>, pub common: CommonMOp<ConstUsize<0>, DestReg, SrcRegWidth, ConstUsize<3>>,
pub lut: Lut4, pub lut: Lut4,
} }
} }
@ -1511,9 +1514,7 @@ impl<DestReg: Type, SrcRegWidth: Size> LogicalFlagsMOp<DestReg, SrcRegWidth> {
} }
#[hdl] #[hdl]
pub fn imm(this: impl ToExpr<Type = Self>) -> Expr<LogicalFlagsMOpImm> { pub fn imm(this: impl ToExpr<Type = Self>) -> Expr<LogicalFlagsMOpImm> {
LogicalFlagsMOpImm::from_imm(LogicalFlagsMOpCommon::<DestReg, SrcRegWidth>::imm( LogicalFlagsMOpImm::from_imm(CommonMOpFor::<Self>::imm(this.to_expr().common))
this.to_expr().common,
))
} }
#[hdl] #[hdl]
pub fn logical_flags<Target: MOpTrait>( pub fn logical_flags<Target: MOpTrait>(