From dd989dbe9546e35686adc505bf660471bd772ae4 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 21:57:30 +0000 Subject: [PATCH 1/2] Implement `jal`, fixup high bits of `j` while doing so --- optimism/src/mips/interpreter.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 302730b529..83d750cd23 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -683,7 +683,7 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi 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)) @@ -699,23 +699,21 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi let pos = env.alloc_scratch(); unsafe { env.bitmask(&instruction, 26, 0, pos) } }; + let instruction_pointer_high_bits = { + // FIXME: Requires a range check + let pos = env.alloc_scratch(); + unsafe { env.bitmask(&instruction, 32, 28, pos) } + }; + let target_addr = + (instruction_pointer_high_bits * Env::constant(1 << 28)) + (addr * Env::constant(1 << 2)); match instr { - JTypeInstruction::Jump => { - // > The address stored in a j instruction is 26 bits of the address - // > associated with the specified label. The 26 bits are achieved by - // > dropping the high-order 4 bits of the address and the low-order 2 - // > 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(next_instruction_pointer); - env.set_next_instruction_pointer(addr * Env::constant(4)); - // REMOVEME: when all jtype instructions are implemented. - return; + JTypeInstruction::Jump => (), + JTypeInstruction::JumpAndLink => { + env.write_register(&Env::constant(31), instruction_pointer + Env::constant(8)); } - JTypeInstruction::JumpAndLink => (), }; - // REMOVEME: when all jtype instructions are implemented. - env.set_halted(Env::constant(1)); + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(target_addr); } pub fn interpret_itype(env: &mut Env, instr: ITypeInstruction) { From d24bbc6cb75edee5d24d9ed9b6153333d6b9f956 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 6 Dec 2023 15:46:17 +0000 Subject: [PATCH 2/2] Mask from `next_instruction_pointer` instead of the instruction --- 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 83d750cd23..dde123cdfd 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -702,7 +702,7 @@ pub fn interpret_jtype(env: &mut Env, instr: JTypeInstructi let instruction_pointer_high_bits = { // FIXME: Requires a range check let pos = env.alloc_scratch(); - unsafe { env.bitmask(&instruction, 32, 28, pos) } + unsafe { env.bitmask(&next_instruction_pointer, 32, 28, pos) } }; let target_addr = (instruction_pointer_high_bits * Env::constant(1 << 28)) + (addr * Env::constant(1 << 2));