Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Dec 5, 2024
1 parent bb2eb94 commit 2045330
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
63 changes: 61 additions & 2 deletions pulley/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,10 @@ impl Decoder {
/// An `OpVisitor` combinator to sequence one visitor and then another.
pub struct SequencedVisitor<'a, F, V1, V2> {
join: F,
v1: &'a mut V1,
v2: &'a mut V2,
/// TODO
pub v1: &'a mut V1,
/// TODO
pub v2: &'a mut V2,
}

impl<'a, F, V1, V2> SequencedVisitor<'a, F, V1, V2> {
Expand All @@ -485,6 +487,9 @@ impl<'a, F, V1, V2> SequencedVisitor<'a, F, V1, V2> {
}
}

/// TODO
pub struct DebugVisitor<T>(pub T);

macro_rules! define_decoder {
(
$(
Expand Down Expand Up @@ -609,6 +614,37 @@ macro_rules! define_decoder {
}
)*
}

impl<T> OpVisitor for DebugVisitor<T>
where T: BytecodeStream,
{
type BytecodeStream = T;

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

type Return = ();

$(
$( #[$attr] )*
fn $snake_name(&mut self $( $( , $field : $field_ty )* )? ) {
println!(
concat!(
stringify!($snake_name),
$(
$(
" ",
stringify!($field),
"={:?}",
)*
)?
),
$($($field),*)?
);
}
)*
}
};
}
for_each_op!(define_decoder);
Expand Down Expand Up @@ -679,6 +715,29 @@ macro_rules! define_extended_decoder {
}
)*
}

impl<T> ExtendedOpVisitor for DebugVisitor<T>
where T: BytecodeStream,
{
$(
$( #[$attr] )*
fn $snake_name(&mut self $( $( , $field : $field_ty )* )? ) {
println!(
concat!(
stringify!($snake_name),
$(
$(
" ",
stringify!($field),
"={:?}",
)*
)?
),
$($($field),*)?
);
}
)*
}
};
}
for_each_extended_op!(define_extended_decoder);
Expand Down
29 changes: 29 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,28 @@ impl Interpreter<'_> {
}
}
}

fn run_debug(mut self) -> Done {
let mut decoder = Decoder::new();
let mut debug = DebugVisitor(*self.bytecode());
let mut visitor = SequencedVisitor::new(|(), r| r, &mut debug, &mut self);
loop {
print!("\t{:?}\t", visitor.bytecode().as_ptr());
match unwrap_uninhabited(decoder.decode_one(&mut visitor)) {
ControlFlow::Continue(()) => {}
ControlFlow::Break(done) => break done,
}
for (i, regs) in visitor.v2.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!();
}
// println!("{:?}", visitor.v2.state);
}
}
}

0 comments on commit 2045330

Please sign in to comment.