Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve run context functions #17

Merged
merged 4 commits into from
May 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 25 additions & 49 deletions src/vm/run_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::compiler::instruction::Instruction;
use crate::compiler::instruction::Op1Addr;
use crate::compiler::instruction::Register;
use crate::vm::memory_dict::Memory;
use crate::vm::instruction::Instruction;
use crate::vm::instruction::Op1Addr;
use crate::vm::instruction::Register;
use crate::vm::memory::Memory;
use crate::vm::relocatable::MaybeRelocatable;
use crate::vm::vm_core::VirtualMachineError;
use num_bigint::BigInt;
Expand Down Expand Up @@ -35,63 +35,39 @@ impl RunContext {
};
}

pub fn compute_dst_addr(
&self,
instruction: &Instruction,
) -> Result<MaybeRelocatable, VirtualMachineError> {
pub fn compute_dst_addr(&self, instruction: &Instruction) -> MaybeRelocatable {
let base_addr = match instruction.dst_register {
Register::AP => Some(&self.ap),
Register::FP => Some(&self.fp),
};
match base_addr {
Some(addr) => {
return Ok(addr.add_num_addr(instruction.off0.clone(), Some(self.prime.clone())))
}
_ => return Err(VirtualMachineError::InvalidDstRegError),
Register::AP => &self.ap,
Register::FP => &self.fp,
};
return base_addr.add_num_addr(instruction.off0.clone(), Some(self.prime.clone()));
}

pub fn compute_op0_addr(
&self,
instruction: &Instruction,
) -> Result<MaybeRelocatable, VirtualMachineError> {
pub fn compute_op0_addr(&self, instruction: &Instruction) -> MaybeRelocatable {
let base_addr = match instruction.op0_register {
Register::AP => Some(&self.ap),
Register::FP => Some(&self.fp),
Register::AP => &self.ap,
Register::FP => &self.fp,
};
if let Some(addr) = base_addr {
return Ok(addr.add_num_addr(instruction.off1.clone(), Some(self.prime.clone())));
} else {
return Err(VirtualMachineError::InvalidOp0RegError);
}
return base_addr.add_num_addr(instruction.off1.clone(), Some(self.prime.clone()));
}

pub fn compute_op1_addr(
&self,
instruction: &Instruction,
op0: Option<MaybeRelocatable>,
) -> Result<MaybeRelocatable, VirtualMachineError> {
let base_addr: Option<&MaybeRelocatable>;
match instruction.op1_addr {
Op1Addr::FP => base_addr = Some(&self.fp),
Op1Addr::AP => base_addr = Some(&self.ap),
Op1Addr::IMM => {
if instruction.off2 == BigInt::from_i32(1).unwrap() {
base_addr = Some(&self.pc);
}
return Err(VirtualMachineError::ImmShouldBe1Error);
}
Op1Addr::OP0 => {
match op0 {
Some(addr) => return Ok((addr + instruction.off1.clone()) % self.prime.clone()),
None => return Err(VirtualMachineError::UnknownOp0Error),
};
}
}
if let Some(addr) = base_addr {
return Ok(addr.add_num_addr(instruction.off1.clone(), Some(self.prime.clone())));
} else {
return Err(VirtualMachineError::InvalidOp1RegError);
}
let base_addr = match instruction.op1_addr {
Op1Addr::FP => &self.fp,
Op1Addr::AP => &self.ap,
Op1Addr::IMM => match instruction.off2 == BigInt::from_i32(1).unwrap() {
true => &self.pc,
false => return Err(VirtualMachineError::ImmShouldBe1Error),
},
Op1Addr::OP0 => match op0 {
Some(addr) => return Ok(addr + instruction.off1.clone() % self.prime.clone()),
None => return Err(VirtualMachineError::UnknownOp0Error),
},
};
return Ok(base_addr.add_num_addr(instruction.off1.clone(), Some(self.prime.clone())));
}
}