From 9c63692177a97460ab7e66da63980f0f5c5910b3 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 28 Nov 2023 17:50:35 +0000 Subject: [PATCH 1/3] Import just the skeleton of the VM interpreter --- optimism/src/mips/interpreter.rs | 92 ++++++++++++++++++++++++++++++++ optimism/src/mips/witness.rs | 10 +++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 1c83f36328..0bcc6e2dd1 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -93,3 +93,95 @@ pub enum ITypeInstruction { StoreWordLeft, // swl StoreWordRight, // swr } + +pub trait InterpreterEnv {} + +pub fn interpret_instruction(env: &mut Env, instr: Instruction) { + match instr { + Instruction::RType(instr) => interpret_rtype(env, instr), + Instruction::JType(instr) => interpret_jtype(env, instr), + Instruction::IType(instr) => interpret_itype(env, instr), + } +} + +pub fn interpret_rtype(_env: &mut Env, instr: RTypeInstruction) { + match instr { + RTypeInstruction::ShiftLeftLogical => (), + RTypeInstruction::ShiftRightLogical => (), + RTypeInstruction::ShiftRightArithmetic => (), + RTypeInstruction::ShiftLeftLogicalVariable => (), + RTypeInstruction::ShiftRightLogicalVariable => (), + RTypeInstruction::ShiftRightArithmeticVariable => (), + RTypeInstruction::JumpRegister => (), + RTypeInstruction::JumpAndLinkRegister => (), + RTypeInstruction::SyscallMmap => (), + RTypeInstruction::SyscallExitGroup => (), + RTypeInstruction::SyscallReadPreimage => (), + RTypeInstruction::SyscallReadOther => (), + RTypeInstruction::SyscallWriteHint => (), + RTypeInstruction::SyscallWritePreimage => (), + RTypeInstruction::SyscallWriteOther => (), + RTypeInstruction::SyscallFcntl => (), + RTypeInstruction::SyscallOther => (), + RTypeInstruction::MoveZero => (), + RTypeInstruction::MoveNonZero => (), + RTypeInstruction::Sync => (), + RTypeInstruction::MoveFromHi => (), + RTypeInstruction::MoveToHi => (), + RTypeInstruction::MoveFromLo => (), + RTypeInstruction::MoveToLo => (), + RTypeInstruction::Multiply => (), + RTypeInstruction::MultiplyUnsigned => (), + RTypeInstruction::Div => (), + RTypeInstruction::DivUnsigned => (), + RTypeInstruction::Add => (), + RTypeInstruction::AddUnsigned => (), + RTypeInstruction::Sub => (), + RTypeInstruction::SubUnsigned => (), + RTypeInstruction::And => (), + RTypeInstruction::Or => (), + RTypeInstruction::Xor => (), + RTypeInstruction::Nor => (), + RTypeInstruction::SetLessThan => (), + RTypeInstruction::SetLessThanUnsigned => (), + RTypeInstruction::MultiplyToRegister => (), + RTypeInstruction::CountLeadingOnes => (), + RTypeInstruction::CountLeadingZeros => (), + } +} + +pub fn interpret_jtype(_env: &mut Env, instr: JTypeInstruction) { + match instr { + JTypeInstruction::Jump => (), + JTypeInstruction::JumpAndLink => (), + } +} + +pub fn interpret_itype(_env: &mut Env, instr: ITypeInstruction) { + match instr { + ITypeInstruction::BranchEq => (), + ITypeInstruction::BranchNeq => (), + ITypeInstruction::BranchLeqZero => (), + ITypeInstruction::BranchGtZero => (), + ITypeInstruction::AddImmediate => (), + ITypeInstruction::AddImmediateUnsigned => (), + ITypeInstruction::SetLessThanImmediate => (), + ITypeInstruction::SetLessThanImmediateUnsigned => (), + ITypeInstruction::AndImmediate => (), + ITypeInstruction::OrImmediate => (), + ITypeInstruction::XorImmediate => (), + ITypeInstruction::LoadUpperImmediate => (), + ITypeInstruction::Load8 => (), + ITypeInstruction::Load16 => (), + ITypeInstruction::Load32 => (), + ITypeInstruction::Load8Unsigned => (), + ITypeInstruction::Load16Unsigned => (), + ITypeInstruction::LoadWordLeft => (), + ITypeInstruction::LoadWordRight => (), + ITypeInstruction::Store8 => (), + ITypeInstruction::Store16 => (), + ITypeInstruction::Store32 => (), + ITypeInstruction::StoreWordLeft => (), + ITypeInstruction::StoreWordRight => (), + } +} diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 1cb359f773..7fe970a5a7 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -4,7 +4,9 @@ use crate::{ PAGE_SIZE, }, mips::{ - interpreter::{self, ITypeInstruction, Instruction, JTypeInstruction, RTypeInstruction}, + interpreter::{ + self, ITypeInstruction, Instruction, InterpreterEnv, JTypeInstruction, RTypeInstruction, + }, registers::Registers, }, preimage_oracle::PreImageOracle, @@ -93,6 +95,8 @@ fn memory_size(total: usize) -> String { } } +impl InterpreterEnv for Env {} + impl Env { pub fn create(page_size: usize, state: State, preimage_oracle: PreImageOracle) -> Self { let initial_instruction_pointer = state.pc; @@ -270,7 +274,9 @@ impl Env { } pub fn step(&mut self, config: VmConfiguration, metadata: &Meta, start: &Start) { - println!("instruction: {:?}", self.decode_instruction()); + let instruction = self.decode_instruction(); + println!("instruction: {:?}", instruction); + interpreter::interpret_instruction(self, instruction); self.pp_info(config.info_at, metadata, start); From 905e878dfad09d5d058cdc278ab3fbd46a92f037 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 28 Nov 2023 18:41:20 +0000 Subject: [PATCH 2/3] Move halting out to interpreter default behavior --- optimism/src/mips/interpreter.rs | 26 +++++++++++++++++++------- optimism/src/mips/witness.rs | 21 +++++++++++++++++---- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 0bcc6e2dd1..8ac18a104b 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -94,7 +94,13 @@ pub enum ITypeInstruction { StoreWordRight, // swr } -pub trait InterpreterEnv {} +pub trait InterpreterEnv { + type Variable: Clone + std::ops::Add; + + fn constant(x: u32) -> Self::Variable; + + fn set_halted(&mut self, flag: Self::Variable); +} pub fn interpret_instruction(env: &mut Env, instr: Instruction) { match instr { @@ -104,7 +110,7 @@ pub fn interpret_instruction(env: &mut Env, instr: Instruct } } -pub fn interpret_rtype(_env: &mut Env, instr: RTypeInstruction) { +pub fn interpret_rtype(env: &mut Env, instr: RTypeInstruction) { match instr { RTypeInstruction::ShiftLeftLogical => (), RTypeInstruction::ShiftRightLogical => (), @@ -147,17 +153,21 @@ pub fn interpret_rtype(_env: &mut Env, instr: RTypeInstruct RTypeInstruction::MultiplyToRegister => (), RTypeInstruction::CountLeadingOnes => (), RTypeInstruction::CountLeadingZeros => (), - } + }; + // TODO: Don't halt. + env.set_halted(Env::constant(1)); } -pub fn interpret_jtype(_env: &mut Env, instr: JTypeInstruction) { +pub fn interpret_jtype(env: &mut Env, instr: JTypeInstruction) { match instr { JTypeInstruction::Jump => (), JTypeInstruction::JumpAndLink => (), - } + }; + // TODO: Don't halt. + env.set_halted(Env::constant(1)); } -pub fn interpret_itype(_env: &mut Env, instr: ITypeInstruction) { +pub fn interpret_itype(env: &mut Env, instr: ITypeInstruction) { match instr { ITypeInstruction::BranchEq => (), ITypeInstruction::BranchNeq => (), @@ -183,5 +193,7 @@ pub fn interpret_itype(_env: &mut Env, instr: ITypeInstruct ITypeInstruction::Store32 => (), ITypeInstruction::StoreWordLeft => (), ITypeInstruction::StoreWordRight => (), - } + }; + // TODO: Don't halt. + env.set_halted(Env::constant(1)); } diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 7fe970a5a7..a7bc450846 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -95,7 +95,23 @@ fn memory_size(total: usize) -> String { } } -impl InterpreterEnv for Env {} +impl InterpreterEnv for Env { + type Variable = u32; + + fn constant(x: u32) -> Self::Variable { + x + } + + fn set_halted(&mut self, flag: Self::Variable) { + if flag == 0 { + self.halt = false + } else if flag == 1 { + self.halt = true + } else { + panic!("Bad value for flag in set_halted: {}", flag); + } + } +} impl Env { pub fn create(page_size: usize, state: State, preimage_oracle: PreImageOracle) -> Self { @@ -285,9 +301,6 @@ impl Env { self.halt = true; return; } - - // TODO - self.halt = true; } fn should_trigger_at(&self, at: StepFrequency) -> bool { From 26e9f9bc2334b2c7e1c26fd19179ea50c869df01 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 28 Nov 2023 21:34:55 +0000 Subject: [PATCH 3/3] Interpret instructions after printing, for consistency --- optimism/src/mips/witness.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index a7bc450846..8ab5d93433 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -292,7 +292,6 @@ impl Env { pub fn step(&mut self, config: VmConfiguration, metadata: &Meta, start: &Start) { let instruction = self.decode_instruction(); println!("instruction: {:?}", instruction); - interpreter::interpret_instruction(self, instruction); self.pp_info(config.info_at, metadata, start); @@ -301,6 +300,8 @@ impl Env { self.halt = true; return; } + + interpreter::interpret_instruction(self, instruction); } fn should_trigger_at(&self, at: StepFrequency) -> bool {