Skip to content

Commit

Permalink
Merge branch 'main' into add-cairo1-running-example
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Aug 17, 2023
2 parents 1a77b5f + f09766c commit 0184126
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,14 @@ impl VirtualMachine {
fn deduce_dst(
&self,
instruction: &Instruction,
res: Option<&MaybeRelocatable>,
) -> Option<MaybeRelocatable> {
match instruction.opcode {
Opcode::AssertEq => res.cloned(),
Opcode::Call => Some(self.get_fp().into()),
_ => None,
}
res: &Option<MaybeRelocatable>,
) -> Result<MaybeRelocatable, VirtualMachineError> {
let dst = match (instruction.opcode, res) {
(Opcode::AssertEq, Some(res)) => res.clone(),
(Opcode::Call, _) => MaybeRelocatable::from(self.run_context.get_fp()),
_ => return Err(VirtualMachineError::NoDst),
};
Ok(dst)
}

fn opcode_assertions(
Expand Down Expand Up @@ -545,20 +546,6 @@ impl VirtualMachine {
Ok(op1)
}

fn compute_dst_deductions(
&self,
instruction: &Instruction,
res: &Option<MaybeRelocatable>,
) -> Result<MaybeRelocatable, VirtualMachineError> {
let dst_op = match instruction.opcode {
Opcode::AssertEq if res.is_some() => Option::clone(res),
Opcode::Call => Some(MaybeRelocatable::from(self.run_context.get_fp())),
_ => self.deduce_dst(instruction, res.as_ref()),
};
let dst = dst_op.ok_or(VirtualMachineError::NoDst)?;
Ok(dst)
}

/// Compute operands and result, trying to deduce them if normal memory access returns a None
/// value.
pub fn compute_operands(
Expand Down Expand Up @@ -609,7 +596,7 @@ impl VirtualMachine {
Some(dst) => dst,
None => {
deduced_operands.set_dst(true);
self.compute_dst_deductions(instruction, &res)?
self.deduce_dst(instruction, &res)?
}
};
let accessed_addresses = OperandsAddresses {
Expand Down Expand Up @@ -2451,8 +2438,8 @@ mod tests {

let res = MaybeRelocatable::Int(Felt252::new(7));
assert_eq!(
Some(MaybeRelocatable::Int(Felt252::new(7))),
vm.deduce_dst(&instruction, Some(&res))
MaybeRelocatable::Int(Felt252::new(7)),
vm.deduce_dst(&instruction, &Some(res)).unwrap()
);
}

Expand All @@ -2475,7 +2462,7 @@ mod tests {

let vm = vm!();

assert_eq!(None, vm.deduce_dst(&instruction, None));
assert!(vm.deduce_dst(&instruction, &None).is_err());
}

#[test]
Expand All @@ -2498,8 +2485,8 @@ mod tests {
let vm = vm!();

assert_eq!(
Some(MaybeRelocatable::from((1, 0))),
vm.deduce_dst(&instruction, None)
MaybeRelocatable::from((1, 0)),
vm.deduce_dst(&instruction, &None).unwrap()
);
}

Expand All @@ -2522,7 +2509,7 @@ mod tests {

let vm = vm!();

assert_eq!(None, vm.deduce_dst(&instruction, None));
assert!(vm.deduce_dst(&instruction, &None).is_err());
}

#[test]
Expand Down

0 comments on commit 0184126

Please sign in to comment.