Skip to main content

zksync_vm2/instruction_handlers/
jump.rs

1use zksync_vm2_interface::{opcodes, Tracer};
2
3use super::{
4    common::boilerplate,
5    monomorphization::{match_source, monomorphize, parameterize},
6};
7use crate::{
8    addressing_modes::{
9        AbsoluteStack, AdvanceStackPointer, AnySource, Arguments, CodePage, Destination,
10        Immediate1, Register1, RelativeStack, Source,
11    },
12    instruction::{ExecutionStatus, Instruction},
13    VirtualMachine, World,
14};
15
16fn jump<T: Tracer, W: World<T>, In: Source>(
17    vm: &mut VirtualMachine<T, W>,
18    world: &mut W,
19    tracer: &mut T,
20) -> ExecutionStatus {
21    boilerplate::<opcodes::Jump, _, _>(vm, world, tracer, |vm, args| {
22        #[allow(clippy::cast_possible_truncation)] // intentional
23        let target = In::get(args, &mut vm.state).low_u32() as u16;
24
25        let next_instruction = vm.state.current_frame.get_pc_as_u16();
26        Register1::set(args, &mut vm.state, next_instruction.into());
27
28        vm.state.current_frame.set_pc_from_u16(target);
29    })
30}
31
32impl<T: Tracer, W: World<T>> Instruction<T, W> {
33    /// Creates a [`Jump`](opcodes::Jump) instruction with the provided params.
34    pub fn from_jump(source: AnySource, destination: Register1, arguments: Arguments) -> Self {
35        Self {
36            handler: monomorphize!(jump [T W] match_source source),
37            arguments: arguments
38                .write_source(&source)
39                .write_destination(&destination),
40        }
41    }
42}