From 924aeffed9781fc7637677b51fd4f09dc9e99aad Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 16:43:54 +0000 Subject: [PATCH 1/3] Add accessors for next_instruction_pointer --- optimism/src/mips/interpreter.rs | 4 ++++ optimism/src/mips/witness.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 8df5b3368b..1d1c179ccd 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -401,6 +401,10 @@ pub trait InterpreterEnv { fn get_instruction_pointer(&self) -> Self::Variable; + fn set_next_instruction_pointer(&mut self, ip: Self::Variable); + + fn get_next_instruction_pointer(&self) -> Self::Variable; + fn constant(x: u32) -> Self::Variable; /// Extract the bits from the variable `x` between `highest_bit` and `lowest_bit`, and store diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index c033d97205..b0d86f605a 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -204,13 +204,20 @@ impl InterpreterEnv for Env { fn set_instruction_pointer(&mut self, ip: Self::Variable) { self.instruction_pointer = ip; - // Set next instruction pointer? } fn get_instruction_pointer(&self) -> Self::Variable { self.instruction_pointer } + fn set_next_instruction_pointer(&mut self, ip: Self::Variable) { + self.next_instruction_pointer = ip; + } + + fn get_next_instruction_pointer(&self) -> Self::Variable { + self.next_instruction_pointer + } + fn constant(x: u32) -> Self::Variable { x } From 92d76271e0615aac45e62ab12169db69e671c258 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 16:45:11 +0000 Subject: [PATCH 2/3] Use a shared instruction pointer --- optimism/src/mips/interpreter.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 1d1c179ccd..2d392ad18e 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -484,8 +484,8 @@ pub fn interpret_rtype(env: &mut Env, instr: RTypeInstructi } pub fn interpret_jtype(env: &mut Env, instr: JTypeInstruction) { + let instruction_pointer = env.get_instruction_pointer(); let instruction = { - let instruction_pointer = env.get_instruction_pointer(); let v0 = env.read_memory(&instruction_pointer); let v1 = env.read_memory(&(instruction_pointer.clone() + Env::constant(1))); let v2 = env.read_memory(&(instruction_pointer.clone() + Env::constant(2))); @@ -524,12 +524,12 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi } pub fn interpret_itype(env: &mut Env, instr: ITypeInstruction) { + let instruction_pointer = env.get_instruction_pointer(); let instruction = { - let instruction_pointer = env.get_instruction_pointer(); let v0 = env.read_memory(&instruction_pointer); let v1 = env.read_memory(&(instruction_pointer.clone() + Env::constant(1))); let v2 = env.read_memory(&(instruction_pointer.clone() + Env::constant(2))); - let v3 = env.read_memory(&(instruction_pointer + Env::constant(3))); + let v3 = env.read_memory(&(instruction_pointer.clone() + Env::constant(3))); (v0 * Env::constant(1 << 24)) + (v1 * Env::constant(1 << 16)) + (v2 * Env::constant(1 << 8)) @@ -564,7 +564,7 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi let register_rs = env.read_register(&rs); let res = register_rs + immediate; env.write_register(&rt, res); - env.set_instruction_pointer(env.get_instruction_pointer() + Env::constant(4u32)); + env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); // TODO: update next_instruction_pointer // REMOVEME: when all itype instructions are implemented. return; @@ -574,7 +574,7 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi let register_rs = env.read_register(&rs); let res = register_rs + immediate; env.write_register(&rt, res); - env.set_instruction_pointer(env.get_instruction_pointer() + Env::constant(4u32)); + env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); // TODO: update next_instruction_pointer // REMOVEME: when all itype instructions are implemented. return; @@ -588,7 +588,7 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi // lui $reg, [most significant 16 bits of immediate] let immediate_value = immediate * Env::constant(1 << 16); env.write_register(&rt, immediate_value); - env.set_instruction_pointer(env.get_instruction_pointer() + Env::constant(4u32)); + env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); // TODO: update next_instruction_pointer // REMOVEME: when all itype instructions are implemented. return; @@ -621,7 +621,7 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi value ); env.write_register(&dest, value); - env.set_instruction_pointer(env.get_instruction_pointer() + Env::constant(4u32)); + env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); // TODO: update next_instruction_pointer // REMOVEME: when all itype instructions are implemented. return; From f117bc080b047a0e0efeb1eddc69ba11ef1213c4 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 16:47:43 +0000 Subject: [PATCH 3/3] Respect branch delay slot --- optimism/src/mips/interpreter.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 2d392ad18e..a2909a55c3 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -485,6 +485,7 @@ pub fn interpret_rtype(env: &mut Env, instr: RTypeInstructi pub fn interpret_jtype(env: &mut Env, instr: JTypeInstruction) { let instruction_pointer = env.get_instruction_pointer(); + let next_instruction_pointer = env.get_next_instruction_pointer(); let instruction = { let v0 = env.read_memory(&instruction_pointer); let v1 = env.read_memory(&(instruction_pointer.clone() + Env::constant(1))); @@ -513,7 +514,8 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi // > bits (which would always be 00, since addresses are always // > divisible by 4). // Source: https://max.cs.kzoo.edu/cs230/Resources/MIPS/MachineXL/InstructionFormats.html - env.set_instruction_pointer(addr * Env::constant(4)); + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(addr * Env::constant(4)); // REMOVEME: when all jtype instructions are implemented. return; } @@ -525,6 +527,7 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi pub fn interpret_itype(env: &mut Env, instr: ITypeInstruction) { let instruction_pointer = env.get_instruction_pointer(); + let next_instruction_pointer = env.get_next_instruction_pointer(); let instruction = { let v0 = env.read_memory(&instruction_pointer); let v1 = env.read_memory(&(instruction_pointer.clone() + Env::constant(1))); @@ -564,8 +567,8 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi let register_rs = env.read_register(&rs); let res = register_rs + immediate; env.write_register(&rt, res); - env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); - // TODO: update next_instruction_pointer + env.set_instruction_pointer(next_instruction_pointer.clone()); + env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32)); // REMOVEME: when all itype instructions are implemented. return; } @@ -574,8 +577,8 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi let register_rs = env.read_register(&rs); let res = register_rs + immediate; env.write_register(&rt, res); - env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); - // TODO: update next_instruction_pointer + env.set_instruction_pointer(next_instruction_pointer.clone()); + env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32)); // REMOVEME: when all itype instructions are implemented. return; } @@ -588,8 +591,8 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi // lui $reg, [most significant 16 bits of immediate] let immediate_value = immediate * Env::constant(1 << 16); env.write_register(&rt, immediate_value); - env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); - // TODO: update next_instruction_pointer + env.set_instruction_pointer(next_instruction_pointer.clone()); + env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32)); // REMOVEME: when all itype instructions are implemented. return; } @@ -621,8 +624,8 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi value ); env.write_register(&dest, value); - env.set_instruction_pointer(instruction_pointer + Env::constant(4u32)); - // TODO: update next_instruction_pointer + env.set_instruction_pointer(next_instruction_pointer.clone()); + env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32)); // REMOVEME: when all itype instructions are implemented. return; }