Skip to content

Commit

Permalink
debug 2024-12-09
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Dec 11, 2024
1 parent 631a429 commit 2526f5a
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions pulley/src/interp/match_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
use super::*;
use crate::decode::unwrap_uninhabited;

const DEBUG: bool = true;

impl Interpreter<'_> {
pub fn run(mut self) -> Done {
if DEBUG {
return self.run_debug();
}
let mut decoder = Decoder::new();
loop {
// Here `decode_one` will call the appropriate `OpVisitor` method on
Expand All @@ -35,4 +40,81 @@ impl Interpreter<'_> {
}
}
}

fn run_debug(self) -> Done {
let mut decoder = Decoder::new();
let mut visitor = Debug(self);
loop {
#[cfg(feature = "std")]
print!("\t{:?}\t", visitor.bytecode().as_ptr());
match unwrap_uninhabited(decoder.decode_one(&mut visitor)) {
ControlFlow::Continue(()) => {}
ControlFlow::Break(done) => break done,
}
#[cfg(feature = "std")]
for (i, regs) in visitor.0.state.x_regs.chunks(4).enumerate() {
print!("\t\t");
for (j, reg) in regs.iter().enumerate() {
let n = i * 4 + j;
let val = reg.get_u64();
let reg = XReg::new(n as u8).unwrap();
print!(" {reg:>3}={val:#018x}");
}
println!();
}
}
}
}

#[repr(transparent)]
struct Debug<'a>(Interpreter<'a>);

macro_rules! debug_then_delegate {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( {
$(
$( #[$field_attr:meta] )*
$field:ident : $field_ty:ty
),*
} )? ;
)*
) => {
$(
$( #[$attr] )*
fn $snake_name(&mut self $( $( , $field : $field_ty )* )? ) -> Self::Return {
#[cfg(feature = "std")]
println!(
concat!(
stringify!($snake_name),
$(
$(
" ",
stringify!($field),
"={:?}",
)*
)?
),
$($($field),*)?
);
self.0.$snake_name($( $($field),* )?)
}
)*
}
}

impl<'a> OpVisitor for Debug<'a> {
type BytecodeStream = <Interpreter<'a> as OpVisitor>::BytecodeStream;
type Return = <Interpreter<'a> as OpVisitor>::Return;

fn bytecode(&mut self) -> &mut Self::BytecodeStream {
self.0.bytecode()
}

for_each_op!(debug_then_delegate);
}

impl<'a> ExtendedOpVisitor for Debug<'a> {
for_each_extended_op!(debug_then_delegate);
}

0 comments on commit 2526f5a

Please sign in to comment.