measuring

main
_ 2023-10-02 18:54:09 -05:00
parent 11fd5b6cbc
commit 52df317326
1 changed files with 28 additions and 17 deletions

View File

@ -261,7 +261,8 @@ impl State {
/// Short form to get access to a register within our window /// Short form to get access to a register within our window
pub fn reg (&self, i: u8) -> &Value { pub fn reg (&self, i: u8) -> &Value {
&self.registers [self.stack_top.register_offset + i as usize] let idx = self.stack_top.register_offset + i as usize;
&self.registers [idx]
} }
pub fn reg_mut (&mut self, i: u8) -> &mut Value { pub fn reg_mut (&mut self, i: u8) -> &mut Value {
@ -487,18 +488,28 @@ impl State {
true true
} }
pub fn step (&mut self) -> Result <Option <StepOutput>, StepError> fn constants (&self) -> &[Value] {
{ &self.chunk.blocks.get (self.stack_top.block_idx).unwrap ().constants
}
fn decode (&self) -> Instruction {
let block = self.chunk.blocks.get (self.stack_top.block_idx).unwrap (); let block = self.chunk.blocks.get (self.stack_top.block_idx).unwrap ();
let instruction = match block.instructions.get (self.stack_top.program_counter) { match block.instructions.get (self.stack_top.program_counter) {
Some (x) => *x, Some (x) => *x,
None => { None => {
dbg! (&self.stack, &self.stack_top); dbg! (&self.stack, &self.stack_top);
panic! ("program_counter went out of bounds"); panic! ("program_counter went out of bounds");
} }
}; }
}
let k = &block.constants;
fn incr_pc (&mut self) {
self.stack_top.program_counter += 1;
}
pub fn step (&mut self) -> Result <Option <StepOutput>, StepError>
{
let instruction = self.decode ();
let make_step_error = |msg| { let make_step_error = |msg| {
self.make_step_error (msg, &instruction) self.make_step_error (msg, &instruction)
@ -567,10 +578,10 @@ impl State {
self.stack_top.program_counter += 1; self.stack_top.program_counter += 1;
} }
}, },
Instruction::EqK (a, b, k_flag) => { Instruction::EqK (a, b, k) => {
let b = usize::from (b); let b = usize::from (b);
if (*self.reg (a) == k [b]) != k_flag { if (*self.reg (a) == self.constants ()[b]) != k {
self.stack_top.program_counter += 1; self.stack_top.program_counter += 1;
} }
}, },
@ -630,7 +641,7 @@ impl State {
let table = value.as_table ().expect ("GetTabUp only works on tables").borrow (); let table = value.as_table ().expect ("GetTabUp only works on tables").borrow ();
let key = match k.get (c).unwrap () { let key = match self.constants ().get (c).unwrap () {
Value::String (s) => *s, Value::String (s) => *s,
_ => panic! ("GetTabUp only supports string keys"), _ => panic! ("GetTabUp only supports string keys"),
}; };
@ -692,7 +703,7 @@ impl State {
Instruction::LoadK (a, bx) => { Instruction::LoadK (a, bx) => {
let bx = usize::try_from (bx).unwrap (); let bx = usize::try_from (bx).unwrap ();
*self.reg_mut (a) = k [bx].clone (); *self.reg_mut (a) = self.constants ()[bx].clone ();
}, },
Instruction::LoadNil (a) => { Instruction::LoadNil (a) => {
*self.reg_mut (a) = Value::Nil; *self.reg_mut (a) = Value::Nil;
@ -711,7 +722,7 @@ impl State {
}, },
Instruction::ModK (a, b, c) => { Instruction::ModK (a, b, c) => {
let b = self.reg (b).as_int().unwrap (); let b = self.reg (b).as_int().unwrap ();
let c = k [usize::from (c)].as_int ().unwrap (); let c = self.constants ()[usize::from (c)].as_int ().unwrap ();
*self.reg_mut (a) = (b % c).into (); *self.reg_mut (a) = (b % c).into ();
}, },
@ -731,7 +742,7 @@ impl State {
}, },
Instruction::MulK (a, b, c) => { Instruction::MulK (a, b, c) => {
let v_b = self.reg (b); let v_b = self.reg (b);
let v_c = &k [usize::from (c)]; let v_c = &self.constants ()[usize::from (c)];
let x = if let (Some (v_b), Some (v_c)) = (v_b.as_int (), v_c.as_int ()) let x = if let (Some (v_b), Some (v_c)) = (v_b.as_int (), v_c.as_int ())
{ {
@ -832,7 +843,7 @@ impl State {
}, },
Instruction::SetI (a, b, c, k_flag) => { Instruction::SetI (a, b, c, k_flag) => {
let value = if k_flag { let value = if k_flag {
&k [usize::from (c)] &self.constants ()[usize::from (c)]
} }
else { else {
self.reg (c) self.reg (c)
@ -863,15 +874,15 @@ impl State {
let b = usize::try_from (b).unwrap (); let b = usize::try_from (b).unwrap ();
let value = if k_flag { let value = if k_flag {
&k [usize::from (c)] &self.constants ()[usize::from (c)]
} }
else { else {
self.reg (c) self.reg (c)
} }
.clone (); .clone ();
let key = self.constants ().get (b).unwrap ().as_str ().expect ("SetTabUp K[B] must be a string");
let table = self.upvalues.get_mut (a).unwrap ().as_table ().unwrap (); let table = self.upvalues.get_mut (a).unwrap ().as_table ().unwrap ();
let key = k.get (b).unwrap ().as_str ().expect ("SetTabUp K[B] must be a string");
table.borrow_mut ().insert_str (key, value); table.borrow_mut ().insert_str (key, value);
}, },
Instruction::Sub (a, b, c) => { Instruction::Sub (a, b, c) => {
@ -983,7 +994,7 @@ impl State {
Instruction::VarArgPrep (_) => (), Instruction::VarArgPrep (_) => (),
} }
self.stack_top.program_counter += 1; self.incr_pc ();
Ok (None) Ok (None)
} }