Skip to content

Commit

Permalink
fix: add abort conditions to the trace recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpnz committed Dec 29, 2023
1 parent e508a53 commit 6ed632c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::fmt;
/// ref: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-7.html
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OPCode {
NOP,
/// Nop designates a no operation, it's similar to a NOP (0x90).
Nop,
AconstNULL,
IconstM1,
Iconst0,
Expand Down Expand Up @@ -215,7 +216,7 @@ pub enum OPCode {
impl fmt::Display for OPCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NOP => write!(f, "nop"),
Self::Nop => write!(f, "nop"),
Self::AconstNULL => write!(f, "aconst_null"),
Self::IconstM1 => write!(f, "iconst_m1"),
Self::Iconst0 => write!(f, "iconst_0"),
Expand Down Expand Up @@ -429,7 +430,7 @@ impl fmt::Display for OPCode {
impl From<u8> for OPCode {
fn from(byte: u8) -> Self {
match byte {
0 => Self::NOP,
0 => Self::Nop,
1 => Self::AconstNULL,
2 => Self::IconstM1,
3 => Self::Iconst0,
Expand Down
12 changes: 8 additions & 4 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ impl Runtime {
{
// TODO: Clean up the naming on trace recoder implementation.
let recorded_trace = self.recorder.recording();
// Dump trace to stdout.
for entry in recorded_trace.trace {
println!("{entry}");
}
// Compile recorded trace.
// self.jit_cache.compile(&recorded_trace);
}
Expand All @@ -392,20 +396,20 @@ impl Runtime {
println!("Exiting the Jit @ {pc}");
// Continue execution with updated PC.
} else {
println!("Interpreting");
// println!("Interpreting");
let inst = self.fetch();
self.profiler.count_entry(&pc);

if self.profiler.is_hot(&pc) {
println!("Found a hot loop...");
println!("Recording...");
// println!("Recording...");
self.recorder.init(pc, pc);
}

if self.recorder.is_recording() {
self.recorder.record(pc, inst.clone());
}
println!("Evaling instruction @ {pc}");
// println!("Evaling instruction @ {pc}");
self.eval(&inst)?
}
}
Expand Down Expand Up @@ -1095,7 +1099,7 @@ impl Runtime {
println!("System.out.println : {value:?}");
Ok(())
}
OPCode::GetStatic | OPCode::NOP | OPCode::Dup => Ok(()),
OPCode::GetStatic | OPCode::Nop | OPCode::Dup => Ok(()),
_ => todo!(),
}
} else {
Expand Down
18 changes: 13 additions & 5 deletions src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ impl Recorder {
),
};
if offset > 0 {
println!("Found forward branch, aborting");
return;
} else {
println!("Found backward branch");
let mut branch_target = pc;
branch_target.inc_instruction_index(offset);
if self.trace_start == branch_target {
self.inner_branch_targets.insert(branch_target);
println!(
"Found an inner branch target {branch_target}"
"Found an inner branch target, loop header @ {branch_target}"
);
} else {
self.outer_branch_targets.insert(branch_target);
Expand All @@ -146,7 +148,9 @@ impl Recorder {
| OPCode::IfICmpLt
| OPCode::IfICmpLe
| OPCode::IfICmpNe
| OPCode::IfICmpEq => self.last_instruction_was_branch = true,
| OPCode::IfICmpEq => {
self.last_instruction_was_branch = true;
}
OPCode::InvokeStatic => {
// Check for recursive function calls by comparing the invoked
// method index with the one we are currently recording.
Expand All @@ -159,9 +163,10 @@ impl Recorder {
if self.trace_start.get_method_index() == method_index as usize
{
self.is_recording = false;
// println!("Found recursive call -- abort recording");
println!("Found recursive call -- abort recording");
return;
}
return;
}
OPCode::Iconst0
| OPCode::Iconst1
Expand Down Expand Up @@ -337,7 +342,7 @@ impl Recorder {
);
branch_target.inc_instruction_index(offset);
if branch_target == pc {
// println!("Flipping branch @ {}", branch_entry.inst.get_mnemonic());
println!("Flipping branch @ {}", branch_entry.inst.get_mnemonic());
offset = 3;
branch_target = branch_entry.pc;
branch_target.inc_instruction_index(offset);
Expand All @@ -356,7 +361,10 @@ impl Recorder {
OPCode::IfICmpGt => OPCode::IfICmpLe,
OPCode::IfICmpLe => OPCode::IfICmpGt,
OPCode::IfICmpNe => OPCode::IfICmpEq,
_ => todo!(),
_ => unreachable!(
"Found unsupported branch entry {}",
branch_entry.inst
),
};
let new_branch_taget =
Instruction::new(flipped, branch_entry.inst.get_params());
Expand Down

0 comments on commit 6ed632c

Please sign in to comment.