Skip to content

Commit

Permalink
refactor: create RunState::execute to hide OP_TABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Jan 7, 2025
1 parent 0f84054 commit 92ea53a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/debugger/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ fn eval_inner(state: &mut RunState, line: &'static str) -> Result<()> {
let mut asm = AsmLine::new(0, stmt, Span::dummy());
asm.backpatch()?;

// Emit
// Compile and execute
let instr = asm.emit()?;
// Execute
RunState::OP_TABLE[(instr >> 12) as usize](state, instr);
state.execute(instr);

Ok(())
}
Expand Down
10 changes: 7 additions & 3 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,22 @@ impl RunEnvironment {
}

let instr = self.state.mem[self.state.pc as usize];
let opcode = (instr >> 12) as usize;
// PC incremented before instruction is performed
self.state.pc += 1;
RunState::OP_TABLE[opcode](&mut self.state, instr);
self.state.execute(instr);
}

Output::Normal.start_new_line();
}
}

impl RunState {
pub const OP_TABLE: [fn(&mut RunState, u16); 16] = [
pub fn execute(&mut self, instr: u16) {
let opcode = (instr >> 12) as usize;
RunState::OP_TABLE[opcode](self, instr);
}

const OP_TABLE: [fn(&mut RunState, u16); 16] = [
Self::br, // 0x0
Self::add, // 0x1
Self::ld, // 0x2
Expand Down

0 comments on commit 92ea53a

Please sign in to comment.