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