From 33b5d5950750a358c22de72a256f18188d4d1712 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 7 May 2026 21:36:47 -0700 Subject: [PATCH] improve debug formatting of PRegValue and PRegFlags --- crates/cpu/src/register.rs | 58 ++++++++++++++++++----- crates/cpu/tests/rename_execute_retire.rs | 12 +---- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/crates/cpu/src/register.rs b/crates/cpu/src/register.rs index ea0ebf9..a27b84a 100644 --- a/crates/cpu/src/register.rs +++ b/crates/cpu/src/register.rs @@ -6,7 +6,7 @@ use fayalite::{ expr::CastToImpl, int::{BoolOrIntType, UIntInRange}, prelude::*, - ty::StaticType, + ty::{SimValueDebug, StaticType}, }; use std::{any::Any, fmt}; @@ -49,6 +49,7 @@ pub trait PRegFlagsViewTrait: Type + PRegFlagsViewTraitSealed { fn view_into_view_unused(view: Self::View) -> ViewUnused; fn debug_fmt<'a, T: 'a, F: FnMut(&'a T, bool) -> Option, D: fmt::Debug>( view: &'a Self::View, + type_name: Option<&str>, field: F, f: &mut fmt::Formatter<'_>, ) -> fmt::Result; @@ -475,6 +476,7 @@ macro_rules! impl_view_trait { fn debug_fmt<'a, T: 'a, F: FnMut(&'a T, bool) -> Option, D: fmt::Debug>( view: &'a Self::View, + type_name: Option<&str>, mut field: F, f: &mut fmt::Formatter<'_>, ) -> fmt::Result { @@ -482,7 +484,7 @@ macro_rules! impl_view_trait { $unused: ViewUnused([$($unused_field,)*]), $($view_field: $flags_field,)* } = view; - let mut debug_struct = f.debug_struct(stringify!($FlagsMode)); + let mut debug_struct = f.debug_struct(type_name.unwrap_or(stringify!($FlagsMode))); #[allow(unused_mut)] let mut any_skipped = false; $(if let Some(v) = field($flags_field, true) { @@ -774,25 +776,40 @@ impl PRegFlags { #[hdl] pub fn debug_fmt( this: &SimValue, + type_name: Option<&str>, f: &mut fmt::Formatter<'_>, ) -> fmt::Result { if let Some(this) = ::downcast_ref::>(this) { V::debug_fmt( &PRegFlags::view_sim_ref::(this), + type_name, |v, _| v.then_some(*v), f, ) } else { - V::debug_fmt(&PRegFlags::view_sim_ref::(this), |v, _| Some(*v), f) + V::debug_fmt( + &PRegFlags::view_sim_ref::(this), + type_name, + |v, _| Some(*v), + f, + ) } } #[hdl] - pub fn debug_fmt_mode(this: &SimValue, mode: &SimValue) -> impl fmt::Debug { + pub fn debug_fmt_mode( + this: &SimValue, + short_name: bool, + mode: &SimValue, + ) -> impl fmt::Debug { fmt::from_fn(move |f| { #[hdl(sim)] match mode { - FlagsMode::PowerISA(_) => Self::debug_fmt::(this, f), - FlagsMode::X86(_) => Self::debug_fmt::(this, f), + FlagsMode::PowerISA(_) => { + Self::debug_fmt::(this, short_name.then_some("Pwr"), f) + } + FlagsMode::X86(_) => { + Self::debug_fmt::(this, short_name.then_some("X86"), f) + } } }) } @@ -819,7 +836,7 @@ impl PRegFlags { pub const FLAG_COUNT: usize = PRegFlagsAllUnused::UNUSED_INNER_LEN; } -#[hdl(cmp_eq)] +#[hdl(cmp_eq, custom_debug(sim))] /// Unit output register's value -- a combination of an integer/fp register /// and flags register and CR field. /// @@ -830,6 +847,15 @@ pub struct PRegValue { pub flags: PRegFlags, } +impl SimValueDebug for PRegValue { + fn sim_value_debug( + value: &::SimValue, + f: &mut fmt::Formatter<'_>, + ) -> fmt::Result { + fmt::Debug::fmt(&Self::debug_fmt::(value, Some("Pwr")), f) + } +} + impl PRegValue { #[hdl] pub fn zeroed() -> Expr { @@ -848,7 +874,10 @@ impl PRegValue { } } #[hdl] - pub fn debug_fmt(this: &SimValue) -> impl fmt::Debug { + pub fn debug_fmt( + this: &::SimValue, + flags_type_name: Option<&str>, + ) -> impl fmt::Debug { fmt::from_fn(move |f| { #[hdl(sim)] let Self { int_fp, flags } = this; @@ -856,19 +885,26 @@ impl PRegValue { .field("int_fp", int_fp) .field( "flags", - &fmt::from_fn(|f| PRegFlags::debug_fmt::(flags, f)), + &fmt::from_fn(|f| PRegFlags::debug_fmt::(flags, flags_type_name, f)), ) .finish() }) } #[hdl] - pub fn debug_fmt_mode(this: &SimValue, mode: &SimValue) -> impl fmt::Debug { + pub fn debug_fmt_mode( + this: &SimValue, + flags_short_name: bool, + mode: &SimValue, + ) -> impl fmt::Debug { fmt::from_fn(move |f| { #[hdl(sim)] let Self { int_fp, flags } = this; f.debug_struct("PRegValue") .field("int_fp", int_fp) - .field("flags", &PRegFlags::debug_fmt_mode(flags, mode)) + .field( + "flags", + &PRegFlags::debug_fmt_mode(flags, flags_short_name, mode), + ) .finish() }) } diff --git a/crates/cpu/tests/rename_execute_retire.rs b/crates/cpu/tests/rename_execute_retire.rs index 1962aaa..97c26ef 100644 --- a/crates/cpu/tests/rename_execute_retire.rs +++ b/crates/cpu/tests/rename_execute_retire.rs @@ -1835,17 +1835,9 @@ impl MockUnitOp { execution_state.run_mop(&self.mop, &self.src_values, self.config); assert!(output.is_some() || caused_cancel.is_some()); println!("try_run: {:#x}: {:?}", self.mop.pc.as_int(), self.mop.mop); - println!( - "<- {:?}", - self.src_values - .each_ref() - .map(PRegValue::debug_fmt::), - ); + println!("<- {:?}", self.src_values); self.output_ready = output.map(|(dest_value, predictor_op)| { - println!( - "-> {:?}", - PRegValue::debug_fmt::(&dest_value), - ); + println!("-> {dest_value:?}"); #[hdl(sim)] let NextPcPredictorOp::<_> { call_stack_op,