revm_interpreter/
instructions.rs

1//! EVM opcode implementations.
2
3#[macro_use]
4pub mod macros;
5/// Arithmetic operations (ADD, SUB, MUL, DIV, etc.).
6pub mod arithmetic;
7/// Bitwise operations (AND, OR, XOR, NOT, etc.).
8pub mod bitwise;
9/// Block information instructions (COINBASE, TIMESTAMP, etc.).
10pub mod block_info;
11/// Contract operations (CALL, CREATE, DELEGATECALL, etc.).
12pub mod contract;
13/// Control flow instructions (JUMP, JUMPI, REVERT, etc.).
14pub mod control;
15/// Host environment interactions (SLOAD, SSTORE, LOG, etc.).
16pub mod host;
17/// Signed 256-bit integer operations.
18pub mod i256;
19/// Memory operations (MLOAD, MSTORE, MSIZE, etc.).
20pub mod memory;
21/// Stack operations (PUSH, POP, DUP, SWAP, etc.).
22pub mod stack;
23/// System information instructions (ADDRESS, CALLER, etc.).
24pub mod system;
25/// Transaction information instructions (ORIGIN, GASPRICE, etc.).
26pub mod tx_info;
27/// Utility functions and helpers for instruction implementation.
28pub mod utility;
29
30use crate::{interpreter_types::InterpreterTypes, Host, InstructionContext};
31
32/// EVM opcode function signature.
33pub type Instruction<W, H> = fn(InstructionContext<'_, H, W>);
34
35/// Instruction table is list of instruction function pointers mapped to 256 EVM opcodes.
36pub type InstructionTable<W, H> = [Instruction<W, H>; 256];
37
38/// Returns the instruction table for the given spec.
39pub const fn instruction_table<WIRE: InterpreterTypes, H: Host + ?Sized>(
40) -> [Instruction<WIRE, H>; 256] {
41    use bytecode::opcode::*;
42    let mut table = [control::unknown as Instruction<WIRE, H>; 256];
43
44    table[STOP as usize] = control::stop;
45    table[ADD as usize] = arithmetic::add;
46    table[MUL as usize] = arithmetic::mul;
47    table[SUB as usize] = arithmetic::sub;
48    table[DIV as usize] = arithmetic::div;
49    table[SDIV as usize] = arithmetic::sdiv;
50    table[MOD as usize] = arithmetic::rem;
51    table[SMOD as usize] = arithmetic::smod;
52    table[ADDMOD as usize] = arithmetic::addmod;
53    table[MULMOD as usize] = arithmetic::mulmod;
54    table[EXP as usize] = arithmetic::exp;
55    table[SIGNEXTEND as usize] = arithmetic::signextend;
56
57    table[LT as usize] = bitwise::lt;
58    table[GT as usize] = bitwise::gt;
59    table[SLT as usize] = bitwise::slt;
60    table[SGT as usize] = bitwise::sgt;
61    table[EQ as usize] = bitwise::eq;
62    table[ISZERO as usize] = bitwise::iszero;
63    table[AND as usize] = bitwise::bitand;
64    table[OR as usize] = bitwise::bitor;
65    table[XOR as usize] = bitwise::bitxor;
66    table[NOT as usize] = bitwise::not;
67    table[BYTE as usize] = bitwise::byte;
68    table[SHL as usize] = bitwise::shl;
69    table[SHR as usize] = bitwise::shr;
70    table[SAR as usize] = bitwise::sar;
71    table[CLZ as usize] = bitwise::clz;
72
73    table[KECCAK256 as usize] = system::keccak256;
74
75    table[ADDRESS as usize] = system::address;
76    table[BALANCE as usize] = host::balance;
77    table[ORIGIN as usize] = tx_info::origin;
78    table[CALLER as usize] = system::caller;
79    table[CALLVALUE as usize] = system::callvalue;
80    table[CALLDATALOAD as usize] = system::calldataload;
81    table[CALLDATASIZE as usize] = system::calldatasize;
82    table[CALLDATACOPY as usize] = system::calldatacopy;
83    table[CODESIZE as usize] = system::codesize;
84    table[CODECOPY as usize] = system::codecopy;
85
86    table[GASPRICE as usize] = tx_info::gasprice;
87    table[EXTCODESIZE as usize] = host::extcodesize;
88    table[EXTCODECOPY as usize] = host::extcodecopy;
89    table[RETURNDATASIZE as usize] = system::returndatasize;
90    table[RETURNDATACOPY as usize] = system::returndatacopy;
91    table[EXTCODEHASH as usize] = host::extcodehash;
92    table[BLOCKHASH as usize] = host::blockhash;
93    table[COINBASE as usize] = block_info::coinbase;
94    table[TIMESTAMP as usize] = block_info::timestamp;
95    table[NUMBER as usize] = block_info::block_number;
96    table[DIFFICULTY as usize] = block_info::difficulty;
97    table[GASLIMIT as usize] = block_info::gaslimit;
98    table[CHAINID as usize] = block_info::chainid;
99    table[SELFBALANCE as usize] = host::selfbalance;
100    table[BASEFEE as usize] = block_info::basefee;
101    table[BLOBHASH as usize] = tx_info::blob_hash;
102    table[BLOBBASEFEE as usize] = block_info::blob_basefee;
103
104    table[POP as usize] = stack::pop;
105    table[MLOAD as usize] = memory::mload;
106    table[MSTORE as usize] = memory::mstore;
107    table[MSTORE8 as usize] = memory::mstore8;
108    table[SLOAD as usize] = host::sload;
109    table[SSTORE as usize] = host::sstore;
110    table[JUMP as usize] = control::jump;
111    table[JUMPI as usize] = control::jumpi;
112    table[PC as usize] = control::pc;
113    table[MSIZE as usize] = memory::msize;
114    table[GAS as usize] = system::gas;
115    table[JUMPDEST as usize] = control::jumpdest;
116    table[TLOAD as usize] = host::tload;
117    table[TSTORE as usize] = host::tstore;
118    table[MCOPY as usize] = memory::mcopy;
119
120    table[PUSH0 as usize] = stack::push0;
121    table[PUSH1 as usize] = stack::push::<1, _, _>;
122    table[PUSH2 as usize] = stack::push::<2, _, _>;
123    table[PUSH3 as usize] = stack::push::<3, _, _>;
124    table[PUSH4 as usize] = stack::push::<4, _, _>;
125    table[PUSH5 as usize] = stack::push::<5, _, _>;
126    table[PUSH6 as usize] = stack::push::<6, _, _>;
127    table[PUSH7 as usize] = stack::push::<7, _, _>;
128    table[PUSH8 as usize] = stack::push::<8, _, _>;
129    table[PUSH9 as usize] = stack::push::<9, _, _>;
130    table[PUSH10 as usize] = stack::push::<10, _, _>;
131    table[PUSH11 as usize] = stack::push::<11, _, _>;
132    table[PUSH12 as usize] = stack::push::<12, _, _>;
133    table[PUSH13 as usize] = stack::push::<13, _, _>;
134    table[PUSH14 as usize] = stack::push::<14, _, _>;
135    table[PUSH15 as usize] = stack::push::<15, _, _>;
136    table[PUSH16 as usize] = stack::push::<16, _, _>;
137    table[PUSH17 as usize] = stack::push::<17, _, _>;
138    table[PUSH18 as usize] = stack::push::<18, _, _>;
139    table[PUSH19 as usize] = stack::push::<19, _, _>;
140    table[PUSH20 as usize] = stack::push::<20, _, _>;
141    table[PUSH21 as usize] = stack::push::<21, _, _>;
142    table[PUSH22 as usize] = stack::push::<22, _, _>;
143    table[PUSH23 as usize] = stack::push::<23, _, _>;
144    table[PUSH24 as usize] = stack::push::<24, _, _>;
145    table[PUSH25 as usize] = stack::push::<25, _, _>;
146    table[PUSH26 as usize] = stack::push::<26, _, _>;
147    table[PUSH27 as usize] = stack::push::<27, _, _>;
148    table[PUSH28 as usize] = stack::push::<28, _, _>;
149    table[PUSH29 as usize] = stack::push::<29, _, _>;
150    table[PUSH30 as usize] = stack::push::<30, _, _>;
151    table[PUSH31 as usize] = stack::push::<31, _, _>;
152    table[PUSH32 as usize] = stack::push::<32, _, _>;
153
154    table[DUP1 as usize] = stack::dup::<1, _, _>;
155    table[DUP2 as usize] = stack::dup::<2, _, _>;
156    table[DUP3 as usize] = stack::dup::<3, _, _>;
157    table[DUP4 as usize] = stack::dup::<4, _, _>;
158    table[DUP5 as usize] = stack::dup::<5, _, _>;
159    table[DUP6 as usize] = stack::dup::<6, _, _>;
160    table[DUP7 as usize] = stack::dup::<7, _, _>;
161    table[DUP8 as usize] = stack::dup::<8, _, _>;
162    table[DUP9 as usize] = stack::dup::<9, _, _>;
163    table[DUP10 as usize] = stack::dup::<10, _, _>;
164    table[DUP11 as usize] = stack::dup::<11, _, _>;
165    table[DUP12 as usize] = stack::dup::<12, _, _>;
166    table[DUP13 as usize] = stack::dup::<13, _, _>;
167    table[DUP14 as usize] = stack::dup::<14, _, _>;
168    table[DUP15 as usize] = stack::dup::<15, _, _>;
169    table[DUP16 as usize] = stack::dup::<16, _, _>;
170
171    table[SWAP1 as usize] = stack::swap::<1, _, _>;
172    table[SWAP2 as usize] = stack::swap::<2, _, _>;
173    table[SWAP3 as usize] = stack::swap::<3, _, _>;
174    table[SWAP4 as usize] = stack::swap::<4, _, _>;
175    table[SWAP5 as usize] = stack::swap::<5, _, _>;
176    table[SWAP6 as usize] = stack::swap::<6, _, _>;
177    table[SWAP7 as usize] = stack::swap::<7, _, _>;
178    table[SWAP8 as usize] = stack::swap::<8, _, _>;
179    table[SWAP9 as usize] = stack::swap::<9, _, _>;
180    table[SWAP10 as usize] = stack::swap::<10, _, _>;
181    table[SWAP11 as usize] = stack::swap::<11, _, _>;
182    table[SWAP12 as usize] = stack::swap::<12, _, _>;
183    table[SWAP13 as usize] = stack::swap::<13, _, _>;
184    table[SWAP14 as usize] = stack::swap::<14, _, _>;
185    table[SWAP15 as usize] = stack::swap::<15, _, _>;
186    table[SWAP16 as usize] = stack::swap::<16, _, _>;
187
188    table[LOG0 as usize] = host::log::<0, _>;
189    table[LOG1 as usize] = host::log::<1, _>;
190    table[LOG2 as usize] = host::log::<2, _>;
191    table[LOG3 as usize] = host::log::<3, _>;
192    table[LOG4 as usize] = host::log::<4, _>;
193
194    table[CREATE as usize] = contract::create::<_, false, _>;
195    table[CALL as usize] = contract::call;
196    table[CALLCODE as usize] = contract::call_code;
197    table[RETURN as usize] = control::ret;
198    table[DELEGATECALL as usize] = contract::delegate_call;
199    table[CREATE2 as usize] = contract::create::<_, true, _>;
200
201    table[STATICCALL as usize] = contract::static_call;
202    table[REVERT as usize] = control::revert;
203    table[INVALID as usize] = control::invalid;
204    table[SELFDESTRUCT as usize] = host::selfdestruct;
205    table
206}
207
208#[cfg(test)]
209mod tests {
210    use super::instruction_table;
211    use crate::{host::DummyHost, interpreter::EthInterpreter};
212    use bytecode::opcode::*;
213
214    #[test]
215    fn all_instructions_and_opcodes_used() {
216        // known unknown instruction we compare it with other instructions from table.
217        let unknown_instruction = 0x0C_usize;
218        let instr_table = instruction_table::<EthInterpreter, DummyHost>();
219
220        let unknown_istr = instr_table[unknown_instruction];
221        for (i, instr) in instr_table.iter().enumerate() {
222            let is_opcode_unknown = OpCode::new(i as u8).is_none();
223            //
224            let is_instr_unknown = std::ptr::fn_addr_eq(*instr, unknown_istr);
225            assert_eq!(
226                is_instr_unknown, is_opcode_unknown,
227                "Opcode 0x{i:X?} is not handled",
228            );
229        }
230    }
231}