From 3929d9038f3da75dbba8678cc8f2ba703b83b331 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 28 Feb 2024 19:22:57 +0100 Subject: tiny_vm: remove additional cache lookup after translating a new bb --- examples/tiny_vm.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/tiny_vm.rs b/examples/tiny_vm.rs index b441a4a..86f5eaf 100644 --- a/examples/tiny_vm.rs +++ b/examples/tiny_vm.rs @@ -237,22 +237,27 @@ impl TinyVm { /// _basic blocks_ on demand. pub fn jit(&mut self) { 'outer: loop { - if let Some(bb_fn) = self.jit_cache[self.pc] { - match bb_fn(self.regs.as_mut_ptr(), self.dmem.as_mut_ptr()) { - JitRet(0, insn) => { - self.pc += insn as usize; - self.icnt += insn as usize; - break 'outer; - } - JitRet(insn, reenter_pc) => { - self.pc = reenter_pc as usize; - self.icnt += insn as usize; - } - } + let bb_fn = if let Some(bb_fn) = self.jit_cache[self.pc] { + bb_fn } else { let bb_fn = self.translate_next_bb(); self.jit_cache[self.pc] = Some(bb_fn); //println!("[0x{:02x}] translated bb at {:p}", self.pc, bb_fn); + bb_fn + }; + + match bb_fn(self.regs.as_mut_ptr(), self.dmem.as_mut_ptr()) { + // HALT instruction hit. + JitRet(0, insn) => { + self.pc += insn as usize; + self.icnt += insn as usize; + break 'outer; + } + // End of basic block, re-enter. + JitRet(insn, reenter_pc) => { + self.pc = reenter_pc as usize; + self.icnt += insn as usize; + } } } } -- cgit v1.2.3