Skip to main content

revmc_builtins/
macros.rs

1#[collapse_debuginfo(yes)]
2macro_rules! gas {
3    ($ecx:expr, $gas:expr) => {
4        if !$ecx.gas.record_regular_cost($gas) {
5            core::hint::cold_path();
6            return Err(InstructionResult::OutOfGas.into());
7        }
8    };
9}
10
11#[collapse_debuginfo(yes)]
12macro_rules! state_gas {
13    ($ecx:expr, $gas:expr) => {
14        if !$ecx.gas.record_state_cost($gas) {
15            core::hint::cold_path();
16            return Err(InstructionResult::OutOfGas.into());
17        }
18    };
19}
20
21#[collapse_debuginfo(yes)]
22macro_rules! ensure_non_staticcall {
23    ($ecx:expr) => {
24        if $ecx.is_static {
25            core::hint::cold_path();
26            return Err(InstructionResult::StateChangeDuringStaticCall.into());
27        }
28    };
29}
30
31/// Same as `read_words_rev`, but returns the arguments in the order they were passed.
32#[collapse_debuginfo(yes)]
33macro_rules! read_words {
34    ($sp:expr, $($words:ident),+ $(,)?) => {
35        let rev![$($words),+] = unsafe { read_words_rev($sp) };
36    };
37}
38
39#[collapse_debuginfo(yes)]
40macro_rules! pop {
41    ($sp:expr; $($x:ident),* $(,)?) => {
42        $(
43            $sp = $sp.sub(1);
44            let $x = &mut *$sp;
45        )*
46    };
47}
48
49#[collapse_debuginfo(yes)]
50macro_rules! try_into_usize {
51    ($x:expr) => {
52        match $x.to_u256().as_limbs() {
53            x => {
54                if (x[0] > usize::MAX as u64) | (x[1] != 0) | (x[2] != 0) | (x[3] != 0) {
55                    core::hint::cold_path();
56                    return Err(InstructionResult::InvalidOperandOOG.into());
57                }
58                x[0] as usize
59            }
60        }
61    };
62}
63
64// Credits: <https://github.com/AuroransSolis/rustconf-2023/blob/665a645d751dfe0e483261e3abca25ab4bb9e13a/reverse-tokens/src/main.rs>
65#[collapse_debuginfo(yes)]
66macro_rules! rev {
67	(@rev [$first:tt$(, $rest:tt)*] [$($rev:tt),*]) => {
68		rev! {
69			@rev [$($rest),*][$first $(, $rev)*]
70		}
71	};
72	(@rev [] [$($rev:tt),*]) => {
73		[$($rev)*] // NOTE: Extra `[]` to make this an array pattern.
74	};
75	($($tt:tt)+) => {
76		rev! {
77			@rev [$($tt),+] []
78		}
79	};
80}
81
82#[collapse_debuginfo(yes)]
83macro_rules! debug_unreachable {
84    ($($t:tt)*) => {
85        if cfg!(debug_assertions) {
86            unreachable!($($t)*);
87        } else {
88            unsafe { core::hint::unreachable_unchecked() };
89        }
90    };
91}
92
93#[collapse_debuginfo(yes)]
94macro_rules! assume {
95    ($e:expr $(,)?) => {
96        if !$e {
97            debug_unreachable!(stringify!($e));
98        }
99    };
100
101    ($e:expr, $($t:tt)+) => {
102        if !$e {
103            debug_unreachable!($($t)+);
104        }
105    };
106}