revmc_builtins/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#[allow(unused_macros)]
macro_rules! tri {
    ($e:expr) => {
        match $e {
            Ok(x) => x,
            Err(_) => return InstructionResult::InvalidOperandOOG,
        }
    };
}

macro_rules! try_opt {
    ($e:expr) => {
        match $e {
            Some(x) => x,
            None => return InstructionResult::InvalidOperandOOG,
        }
    };
}

macro_rules! try_host {
    ($e:expr) => {
        match $e {
            Some(x) => x,
            None => return InstructionResult::FatalExternalError,
        }
    };
}

macro_rules! try_ir {
    ($e:expr) => {
        match $e {
            InstructionResult::Continue => {}
            ir => return ir,
        }
    };
}

macro_rules! gas {
    ($ecx:expr, $gas:expr) => {
        if !$ecx.gas.record_cost($gas) {
            return InstructionResult::OutOfGas;
        }
    };
}

macro_rules! gas_opt {
    ($ecx:expr, $gas:expr) => {
        match $gas {
            Some(gas) => gas!($ecx, gas),
            None => return InstructionResult::OutOfGas,
        }
    };
}

macro_rules! ensure_non_staticcall {
    ($ecx:expr) => {
        if $ecx.is_static {
            return InstructionResult::StateChangeDuringStaticCall;
        }
    };
}

macro_rules! ensure_memory {
    ($ecx:expr, $offset:expr, $len:expr) => {
        try_ir!(ensure_memory($ecx, $offset, $len))
    };
}

/// Same as `read_words_rev`, but returns the arguments in the order they were passed.
macro_rules! read_words {
    ($sp:expr, $($words:ident),+ $(,)?) => {
        let rev![$($words),+] = unsafe { read_words_rev($sp) };
    };
}

macro_rules! pop {
    ($sp:expr; $($x:ident),* $(,)?) => {
        $(
            $sp = $sp.sub(1);
            let $x = &mut *$sp;
        )*
    };
}

macro_rules! try_into_usize {
    ($x:expr) => {
        match $x.to_u256().as_limbs() {
            x => {
                if (x[0] > usize::MAX as u64) | (x[1] != 0) | (x[2] != 0) | (x[3] != 0) {
                    return InstructionResult::InvalidOperandOOG;
                }
                x[0] as usize
            }
        }
    };
}

// Credits: <https://github.com/AuroransSolis/rustconf-2023/blob/665a645d751dfe0e483261e3abca25ab4bb9e13a/reverse-tokens/src/main.rs>
macro_rules! rev {
	(@rev [$first:tt$(, $rest:tt)*] [$($rev:tt),*]) => {
		rev! {
			@rev [$($rest),*][$first $(, $rev)*]
		}
	};
	(@rev [] [$($rev:tt),*]) => {
		[$($rev)*] // NOTE: Extra `[]` to make this an array pattern.
	};
	($($tt:tt)+) => {
		rev! {
			@rev [$($tt),+] []
		}
	};
}

macro_rules! debug_unreachable {
    ($($t:tt)*) => {
        if cfg!(debug_assertions) {
            unreachable!($($t)*);
        } else {
            unsafe { core::hint::unreachable_unchecked() };
        }
    };
}

macro_rules! assume {
    ($e:expr $(,)?) => {
        if !$e {
            debug_unreachable!(stringify!($e));
        }
    };

    ($e:expr, $($t:tt)+) => {
        if !$e {
            debug_unreachable!($($t)+);
        }
    };
}