Skip to main content

zksync_vm2/instruction_handlers/
monomorphization.rs

1/// Select an instantiation of a generic function based on runtime variables
2/// For example `monomorphize!(load [H] match_reg_imm src match_boolean increment)`
3macro_rules! monomorphize {
4    ($function_name: ident [$($types: tt)*] $next_matcher: ident $($rest: ident)*) => {
5        $next_matcher!([$($types)*] $($rest)* parameterize $function_name)
6    };
7
8    ($function_name: ident $($rest: ident)*) => {
9        monomorphize!($function_name [] $($rest)*)
10    };
11}
12
13macro_rules! match_source {
14    ([ $($types: tt)* ] $input_type: ident $next_matcher: ident $($rest: ident)*) => {
15        match $input_type {
16            AnySource::Register1(_) => $next_matcher!([$($types)* Register1] $($rest)*),
17            AnySource::Immediate1(_) => $next_matcher!([$($types)* Immediate1] $($rest)*),
18            AnySource::AbsoluteStack(_) => $next_matcher!([$($types)* AbsoluteStack] $($rest)*),
19            AnySource::RelativeStack(_) => $next_matcher!([$($types)* RelativeStack] $($rest)*),
20            AnySource::AdvanceStackPointer(_) => $next_matcher!([$($types)* AdvanceStackPointer] $($rest)*),
21            AnySource::CodePage(_) => $next_matcher!([$($types)* CodePage] $($rest)*),
22        }
23    };
24}
25
26macro_rules! match_reg_imm {
27    ([ $($types: tt)* ] $input_type: ident $next_matcher: ident $($rest: ident)*) => {
28        match $input_type {
29            RegisterOrImmediate::Register1(_) => $next_matcher!([$($types)* Register1] $($rest)*),
30            RegisterOrImmediate::Immediate1(_) => $next_matcher!([$($types)* Immediate1] $($rest)*),
31        }
32    };
33}
34
35macro_rules! match_destination {
36    ([ $($types: tt)* ] $input_type: ident $next_matcher: ident $($rest: ident)*) => {
37        match $input_type {
38            AnyDestination::Register1(_) => $next_matcher!([$($types)* Register1] $($rest)*),
39            AnyDestination::AbsoluteStack(_) => $next_matcher!([$($types)* AbsoluteStack] $($rest)*),
40            AnyDestination::RelativeStack(_) => $next_matcher!([$($types)* RelativeStack] $($rest)*),
41            AnyDestination::AdvanceStackPointer(_) => $next_matcher!([$($types)* AdvanceStackPointer] $($rest)*),
42        }
43    };
44}
45
46macro_rules! match_boolean {
47    ([ $($types: tt)* ] $increment: ident $next_matcher: ident $($rest: ident)*) => {
48        if $increment {
49            $next_matcher!([$($types)* {true}] $($rest)*)
50        } else {
51            $next_matcher!([$($types)* {false}] $($rest)*)
52        }
53    };
54}
55
56macro_rules! parameterize {
57    ([$($types: tt)*] $function_name:ident) => {
58        $function_name::<$($types),*>
59    };
60}
61
62pub(crate) use match_boolean;
63pub(crate) use match_destination;
64pub(crate) use match_reg_imm;
65pub(crate) use match_source;
66pub(crate) use monomorphize;
67pub(crate) use parameterize;