Skip to content

Commit

Permalink
Merge pull request #1420 from o1-labs/feature/mips/add-missing-branch…
Browse files Browse the repository at this point in the history
…-opcodes

Add missing parsing for `bltz` and `bgez`
  • Loading branch information
dannywillems authored Dec 6, 2023
2 parents f8c060d + c0b61fa commit 0313aff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ pub enum ITypeInstruction {
BranchNeq, // bne
BranchLeqZero, // blez
BranchGtZero, // bgtz
BranchLtZero, // bltz
BranchGeqZero, // bgez
AddImmediate, // addi
AddImmediateUnsigned, // addiu
SetLessThanImmediate, // slti
Expand Down Expand Up @@ -954,6 +956,26 @@ pub fn interpret_itype<Env: InterpreterEnv>(env: &mut Env, instr: ITypeInstructi
}
ITypeInstruction::BranchLeqZero => (),
ITypeInstruction::BranchGtZero => (),
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 =
(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)
};
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);
let offset = env.sign_extend(&immediate, 16);
Expand Down
8 changes: 8 additions & 0 deletions optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ impl<Fp: Field> Env<Fp> {
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),
Expand Down

0 comments on commit 0313aff

Please sign in to comment.