From d4895dd0447ccd4aeb7dafdac0ff00038be49159 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 23:31:22 +0000 Subject: [PATCH 1/3] Add missing parsing for `bltz` and `bgez` --- optimism/src/mips/interpreter.rs | 4 ++++ optimism/src/mips/witness.rs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index b6d4f25b9b..473e01401b 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -134,6 +134,8 @@ pub enum ITypeInstruction { BranchNeq, // bne BranchLeqZero, // blez BranchGtZero, // bgtz + BranchLtZero, // bltz + BranchGeqZero, // bgez AddImmediate, // addi AddImmediateUnsigned, // addiu SetLessThanImmediate, // slti @@ -946,6 +948,8 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi } ITypeInstruction::BranchLeqZero => (), ITypeInstruction::BranchGtZero => (), + ITypeInstruction::BranchLtZero => (), + ITypeInstruction::BranchGeqZero => (), ITypeInstruction::AddImmediate => { let register_rs = env.read_register(&rs); let offset = env.sign_extend(&immediate, 16); diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index b97ef62edc..1ac0217dd4 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -461,6 +461,14 @@ impl Env { panic!("Unhandled instruction {:#X}", instruction) } }, + 0x01 => { + // RegImm instructions + match (instruction >> 16) & 0x1F { + 0x0 => Instruction::IType(ITypeInstruction::BranchLtZero), + 0x1 => Instruction::IType(ITypeInstruction::BranchGeqZero), + _ => panic!("Unhandled instruction {:#X}", instruction), + } + } 0x02 => Instruction::JType(JTypeInstruction::Jump), 0x03 => Instruction::JType(JTypeInstruction::JumpAndLink), 0x04 => Instruction::IType(ITypeInstruction::BranchEq), From 350bccc99fcf5d9314575b1e73dc6e183142ba79 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 23:32:27 +0000 Subject: [PATCH 2/3] Implement `bltz` --- optimism/src/mips/interpreter.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 473e01401b..d767376f32 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -948,7 +948,25 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi } ITypeInstruction::BranchLeqZero => (), ITypeInstruction::BranchGtZero => (), - ITypeInstruction::BranchLtZero => (), + ITypeInstruction::BranchLtZero => { + let offset = env.sign_extend(&(immediate * Env::constant(1 << 2)), 18); + let rs = env.read_register(&rs); + let less_than = { + // FIXME: Requires constraints + let pos = env.alloc_scratch(); + unsafe { env.test_less_than_signed(&rs, &Env::constant(0), pos) } + }; + let offset = + less_than.clone() * Env::constant(4) + (Env::constant(1) - less_than) * offset; + let addr = { + let pos = env.alloc_scratch(); + env.copy(&(next_instruction_pointer.clone() + offset), pos) + }; + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(addr); + // REMOVEME: when all itype instructions are implemented. + return; + } ITypeInstruction::BranchGeqZero => (), ITypeInstruction::AddImmediate => { let register_rs = env.read_register(&rs); From b39a95e82ea868f6a865c6c785e244f3b8647d24 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 6 Dec 2023 01:13:14 +0000 Subject: [PATCH 3/3] Fixup order of optional logic in bltz --- optimism/src/mips/interpreter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index d767376f32..41bee4f5ed 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -957,7 +957,7 @@ pub fn interpret_itype(env: &mut Env, instr: ITypeInstructi unsafe { env.test_less_than_signed(&rs, &Env::constant(0), pos) } }; let offset = - less_than.clone() * Env::constant(4) + (Env::constant(1) - less_than) * offset; + (Env::constant(1) - less_than.clone()) * Env::constant(4) + less_than * offset; let addr = { let pos = env.alloc_scratch(); env.copy(&(next_instruction_pointer.clone() + offset), pos)