From f09766cfeae1178542c051841f8987be731daec0 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:13:17 +0300 Subject: [PATCH] Remove duplicated code is dst deduction (#1382) * Merge compute_dst_deductions & deduce_dst * Unwrap res value in patten match Co-authored-by: Mario Rugiero --------- Co-authored-by: Mario Rugiero --- vm/src/vm/vm_core.rs | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index c1c8aea0a7..4d42d74826 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -316,13 +316,14 @@ impl VirtualMachine { fn deduce_dst( &self, instruction: &Instruction, - res: Option<&MaybeRelocatable>, - ) -> Option { - match instruction.opcode { - Opcode::AssertEq => res.cloned(), - Opcode::Call => Some(self.get_fp().into()), - _ => None, - } + res: &Option, + ) -> Result { + 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( @@ -545,20 +546,6 @@ impl VirtualMachine { Ok(op1) } - fn compute_dst_deductions( - &self, - instruction: &Instruction, - res: &Option, - ) -> Result { - 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( @@ -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 { @@ -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() ); } @@ -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] @@ -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() ); } @@ -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]