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
30pub use context_interface::cfg::gas::{self, *};
31
32use crate::{interpreter_types::InterpreterTypes, Host, InstructionContext, InstructionExecResult};
33use primitives::hardfork::SpecId;
34
35#[derive(Debug)]
37pub struct Instruction<W: InterpreterTypes, H: ?Sized> {
38 fn_: fn(InstructionContext<'_, H, W>) -> InstructionExecResult,
39}
40
41impl<W: InterpreterTypes, H: Host + ?Sized> Instruction<W, H> {
42 #[inline]
44 pub const fn new(fn_: fn(InstructionContext<'_, H, W>) -> InstructionExecResult) -> Self {
45 Self { fn_ }
46 }
47
48 #[inline]
50 pub const fn unknown() -> Self {
51 Self {
52 fn_: control::unknown,
53 }
54 }
55
56 #[inline(always)]
58 pub fn execute(self, ctx: InstructionContext<'_, H, W>) -> InstructionExecResult {
59 (self.fn_)(ctx)
60 }
61}
62
63impl<W: InterpreterTypes, H: Host + ?Sized> Copy for Instruction<W, H> {}
64impl<W: InterpreterTypes, H: Host + ?Sized> Clone for Instruction<W, H> {
65 fn clone(&self) -> Self {
66 *self
67 }
68}
69
70pub type InstructionTable<W, H> = [Instruction<W, H>; 256];
72
73pub type GasTable = [u16; 256];
75
76#[inline]
78pub const fn instruction_table<WIRE: InterpreterTypes, H: Host>() -> InstructionTable<WIRE, H> {
79 const { instruction_table_impl::<WIRE, H>() }
80}
81
82#[inline]
84pub const fn gas_table() -> GasTable {
85 const { gas_table_impl() }
86}
87
88#[inline]
90pub const fn gas_table_spec(spec: SpecId) -> GasTable {
91 use bytecode::opcode::*;
92 use SpecId::*;
93 let mut table = gas_table();
94
95 if spec.is_enabled_in(TANGERINE) {
96 table[SLOAD as usize] = 200;
98 table[BALANCE as usize] = 400;
99 table[EXTCODESIZE as usize] = 700;
100 table[EXTCODECOPY as usize] = 700;
101 table[CALL as usize] = 700;
102 table[CALLCODE as usize] = 700;
103 table[DELEGATECALL as usize] = 700;
104 table[STATICCALL as usize] = 700;
105 table[SELFDESTRUCT as usize] = 5000;
106 }
107
108 if spec.is_enabled_in(ISTANBUL) {
109 table[SLOAD as usize] = gas::ISTANBUL_SLOAD_GAS as u16;
111 table[BALANCE as usize] = 700;
112 table[EXTCODEHASH as usize] = 700;
113 }
114
115 if spec.is_enabled_in(BERLIN) {
116 table[SLOAD as usize] = gas::WARM_STORAGE_READ_COST as u16;
118 table[BALANCE as usize] = gas::WARM_STORAGE_READ_COST as u16;
119 table[EXTCODESIZE as usize] = gas::WARM_STORAGE_READ_COST as u16;
120 table[EXTCODEHASH as usize] = gas::WARM_STORAGE_READ_COST as u16;
121 table[EXTCODECOPY as usize] = gas::WARM_STORAGE_READ_COST as u16;
122 table[CALL as usize] = gas::WARM_STORAGE_READ_COST as u16;
123 table[CALLCODE as usize] = gas::WARM_STORAGE_READ_COST as u16;
124 table[DELEGATECALL as usize] = gas::WARM_STORAGE_READ_COST as u16;
125 table[STATICCALL as usize] = gas::WARM_STORAGE_READ_COST as u16;
126 }
127
128 table
129}
130
131const fn instruction_table_impl<WIRE: InterpreterTypes, H: Host>() -> InstructionTable<WIRE, H> {
132 use bytecode::opcode::*;
133 let mut table = [Instruction::unknown(); 256];
134
135 table[STOP as usize] = Instruction::new(control::stop);
136 table[ADD as usize] = Instruction::new(arithmetic::add);
137 table[MUL as usize] = Instruction::new(arithmetic::mul);
138 table[SUB as usize] = Instruction::new(arithmetic::sub);
139 table[DIV as usize] = Instruction::new(arithmetic::div);
140 table[SDIV as usize] = Instruction::new(arithmetic::sdiv);
141 table[MOD as usize] = Instruction::new(arithmetic::rem);
142 table[SMOD as usize] = Instruction::new(arithmetic::smod);
143 table[ADDMOD as usize] = Instruction::new(arithmetic::addmod);
144 table[MULMOD as usize] = Instruction::new(arithmetic::mulmod);
145 table[EXP as usize] = Instruction::new(arithmetic::exp);
146 table[SIGNEXTEND as usize] = Instruction::new(arithmetic::signextend);
147
148 table[LT as usize] = Instruction::new(bitwise::lt);
149 table[GT as usize] = Instruction::new(bitwise::gt);
150 table[SLT as usize] = Instruction::new(bitwise::slt);
151 table[SGT as usize] = Instruction::new(bitwise::sgt);
152 table[EQ as usize] = Instruction::new(bitwise::eq);
153 table[ISZERO as usize] = Instruction::new(bitwise::iszero);
154 table[AND as usize] = Instruction::new(bitwise::bitand);
155 table[OR as usize] = Instruction::new(bitwise::bitor);
156 table[XOR as usize] = Instruction::new(bitwise::bitxor);
157 table[NOT as usize] = Instruction::new(bitwise::not);
158 table[BYTE as usize] = Instruction::new(bitwise::byte);
159 table[SHL as usize] = Instruction::new(bitwise::shl);
160 table[SHR as usize] = Instruction::new(bitwise::shr);
161 table[SAR as usize] = Instruction::new(bitwise::sar);
162 table[CLZ as usize] = Instruction::new(bitwise::clz);
163
164 table[KECCAK256 as usize] = Instruction::new(system::keccak256);
165
166 table[ADDRESS as usize] = Instruction::new(system::address);
167 table[BALANCE as usize] = Instruction::new(host::balance);
168 table[ORIGIN as usize] = Instruction::new(tx_info::origin);
169 table[CALLER as usize] = Instruction::new(system::caller);
170 table[CALLVALUE as usize] = Instruction::new(system::callvalue);
171 table[CALLDATALOAD as usize] = Instruction::new(system::calldataload);
172 table[CALLDATASIZE as usize] = Instruction::new(system::calldatasize);
173 table[CALLDATACOPY as usize] = Instruction::new(system::calldatacopy);
174 table[CODESIZE as usize] = Instruction::new(system::codesize);
175 table[CODECOPY as usize] = Instruction::new(system::codecopy);
176
177 table[GASPRICE as usize] = Instruction::new(tx_info::gasprice);
178 table[EXTCODESIZE as usize] = Instruction::new(host::extcodesize);
179 table[EXTCODECOPY as usize] = Instruction::new(host::extcodecopy);
180 table[RETURNDATASIZE as usize] = Instruction::new(system::returndatasize);
181 table[RETURNDATACOPY as usize] = Instruction::new(system::returndatacopy);
182 table[EXTCODEHASH as usize] = Instruction::new(host::extcodehash);
183 table[BLOCKHASH as usize] = Instruction::new(host::blockhash);
184 table[COINBASE as usize] = Instruction::new(block_info::coinbase);
185 table[TIMESTAMP as usize] = Instruction::new(block_info::timestamp);
186 table[NUMBER as usize] = Instruction::new(block_info::block_number);
187 table[DIFFICULTY as usize] = Instruction::new(block_info::difficulty);
188 table[GASLIMIT as usize] = Instruction::new(block_info::gaslimit);
189 table[CHAINID as usize] = Instruction::new(block_info::chainid);
190 table[SELFBALANCE as usize] = Instruction::new(host::selfbalance);
191 table[BASEFEE as usize] = Instruction::new(block_info::basefee);
192 table[BLOBHASH as usize] = Instruction::new(tx_info::blob_hash);
193 table[BLOBBASEFEE as usize] = Instruction::new(block_info::blob_basefee);
194 table[SLOTNUM as usize] = Instruction::new(block_info::slot_num);
195
196 table[POP as usize] = Instruction::new(stack::pop);
197 table[MLOAD as usize] = Instruction::new(memory::mload);
198 table[MSTORE as usize] = Instruction::new(memory::mstore);
199 table[MSTORE8 as usize] = Instruction::new(memory::mstore8);
200 table[SLOAD as usize] = Instruction::new(host::sload);
201 table[SSTORE as usize] = Instruction::new(host::sstore);
202 table[JUMP as usize] = Instruction::new(control::jump);
203 table[JUMPI as usize] = Instruction::new(control::jumpi);
204 table[PC as usize] = Instruction::new(control::pc);
205 table[MSIZE as usize] = Instruction::new(memory::msize);
206 table[GAS as usize] = Instruction::new(system::gas);
207 table[JUMPDEST as usize] = Instruction::new(control::jumpdest);
208 table[TLOAD as usize] = Instruction::new(host::tload);
209 table[TSTORE as usize] = Instruction::new(host::tstore);
210 table[MCOPY as usize] = Instruction::new(memory::mcopy);
211
212 table[PUSH0 as usize] = Instruction::new(stack::push0);
213 table[PUSH1 as usize] = Instruction::new(stack::push::<1, _, _>);
214 table[PUSH2 as usize] = Instruction::new(stack::push::<2, _, _>);
215 table[PUSH3 as usize] = Instruction::new(stack::push::<3, _, _>);
216 table[PUSH4 as usize] = Instruction::new(stack::push::<4, _, _>);
217 table[PUSH5 as usize] = Instruction::new(stack::push::<5, _, _>);
218 table[PUSH6 as usize] = Instruction::new(stack::push::<6, _, _>);
219 table[PUSH7 as usize] = Instruction::new(stack::push::<7, _, _>);
220 table[PUSH8 as usize] = Instruction::new(stack::push::<8, _, _>);
221 table[PUSH9 as usize] = Instruction::new(stack::push::<9, _, _>);
222 table[PUSH10 as usize] = Instruction::new(stack::push::<10, _, _>);
223 table[PUSH11 as usize] = Instruction::new(stack::push::<11, _, _>);
224 table[PUSH12 as usize] = Instruction::new(stack::push::<12, _, _>);
225 table[PUSH13 as usize] = Instruction::new(stack::push::<13, _, _>);
226 table[PUSH14 as usize] = Instruction::new(stack::push::<14, _, _>);
227 table[PUSH15 as usize] = Instruction::new(stack::push::<15, _, _>);
228 table[PUSH16 as usize] = Instruction::new(stack::push::<16, _, _>);
229 table[PUSH17 as usize] = Instruction::new(stack::push::<17, _, _>);
230 table[PUSH18 as usize] = Instruction::new(stack::push::<18, _, _>);
231 table[PUSH19 as usize] = Instruction::new(stack::push::<19, _, _>);
232 table[PUSH20 as usize] = Instruction::new(stack::push::<20, _, _>);
233 table[PUSH21 as usize] = Instruction::new(stack::push::<21, _, _>);
234 table[PUSH22 as usize] = Instruction::new(stack::push::<22, _, _>);
235 table[PUSH23 as usize] = Instruction::new(stack::push::<23, _, _>);
236 table[PUSH24 as usize] = Instruction::new(stack::push::<24, _, _>);
237 table[PUSH25 as usize] = Instruction::new(stack::push::<25, _, _>);
238 table[PUSH26 as usize] = Instruction::new(stack::push::<26, _, _>);
239 table[PUSH27 as usize] = Instruction::new(stack::push::<27, _, _>);
240 table[PUSH28 as usize] = Instruction::new(stack::push::<28, _, _>);
241 table[PUSH29 as usize] = Instruction::new(stack::push::<29, _, _>);
242 table[PUSH30 as usize] = Instruction::new(stack::push::<30, _, _>);
243 table[PUSH31 as usize] = Instruction::new(stack::push::<31, _, _>);
244 table[PUSH32 as usize] = Instruction::new(stack::push::<32, _, _>);
245
246 table[DUP1 as usize] = Instruction::new(stack::dup::<1, _, _>);
247 table[DUP2 as usize] = Instruction::new(stack::dup::<2, _, _>);
248 table[DUP3 as usize] = Instruction::new(stack::dup::<3, _, _>);
249 table[DUP4 as usize] = Instruction::new(stack::dup::<4, _, _>);
250 table[DUP5 as usize] = Instruction::new(stack::dup::<5, _, _>);
251 table[DUP6 as usize] = Instruction::new(stack::dup::<6, _, _>);
252 table[DUP7 as usize] = Instruction::new(stack::dup::<7, _, _>);
253 table[DUP8 as usize] = Instruction::new(stack::dup::<8, _, _>);
254 table[DUP9 as usize] = Instruction::new(stack::dup::<9, _, _>);
255 table[DUP10 as usize] = Instruction::new(stack::dup::<10, _, _>);
256 table[DUP11 as usize] = Instruction::new(stack::dup::<11, _, _>);
257 table[DUP12 as usize] = Instruction::new(stack::dup::<12, _, _>);
258 table[DUP13 as usize] = Instruction::new(stack::dup::<13, _, _>);
259 table[DUP14 as usize] = Instruction::new(stack::dup::<14, _, _>);
260 table[DUP15 as usize] = Instruction::new(stack::dup::<15, _, _>);
261 table[DUP16 as usize] = Instruction::new(stack::dup::<16, _, _>);
262
263 table[SWAP1 as usize] = Instruction::new(stack::swap::<1, _, _>);
264 table[SWAP2 as usize] = Instruction::new(stack::swap::<2, _, _>);
265 table[SWAP3 as usize] = Instruction::new(stack::swap::<3, _, _>);
266 table[SWAP4 as usize] = Instruction::new(stack::swap::<4, _, _>);
267 table[SWAP5 as usize] = Instruction::new(stack::swap::<5, _, _>);
268 table[SWAP6 as usize] = Instruction::new(stack::swap::<6, _, _>);
269 table[SWAP7 as usize] = Instruction::new(stack::swap::<7, _, _>);
270 table[SWAP8 as usize] = Instruction::new(stack::swap::<8, _, _>);
271 table[SWAP9 as usize] = Instruction::new(stack::swap::<9, _, _>);
272 table[SWAP10 as usize] = Instruction::new(stack::swap::<10, _, _>);
273 table[SWAP11 as usize] = Instruction::new(stack::swap::<11, _, _>);
274 table[SWAP12 as usize] = Instruction::new(stack::swap::<12, _, _>);
275 table[SWAP13 as usize] = Instruction::new(stack::swap::<13, _, _>);
276 table[SWAP14 as usize] = Instruction::new(stack::swap::<14, _, _>);
277 table[SWAP15 as usize] = Instruction::new(stack::swap::<15, _, _>);
278 table[SWAP16 as usize] = Instruction::new(stack::swap::<16, _, _>);
279
280 table[DUPN as usize] = Instruction::new(stack::dupn);
281 table[SWAPN as usize] = Instruction::new(stack::swapn);
282 table[EXCHANGE as usize] = Instruction::new(stack::exchange);
283
284 table[LOG0 as usize] = Instruction::new(host::log::<0, _>);
285 table[LOG1 as usize] = Instruction::new(host::log::<1, _>);
286 table[LOG2 as usize] = Instruction::new(host::log::<2, _>);
287 table[LOG3 as usize] = Instruction::new(host::log::<3, _>);
288 table[LOG4 as usize] = Instruction::new(host::log::<4, _>);
289
290 table[CREATE as usize] = Instruction::new(contract::create::<false, _, _>);
291 table[CALL as usize] = Instruction::new(contract::call::<CALL, _, _>);
292 table[CALLCODE as usize] = Instruction::new(contract::call::<CALLCODE, _, _>);
293 table[RETURN as usize] = Instruction::new(control::ret);
294 table[DELEGATECALL as usize] = Instruction::new(contract::call::<DELEGATECALL, _, _>);
295 table[CREATE2 as usize] = Instruction::new(contract::create::<true, _, _>);
296
297 table[STATICCALL as usize] = Instruction::new(contract::call::<STATICCALL, _, _>);
298 table[REVERT as usize] = Instruction::new(control::revert);
299 table[INVALID as usize] = Instruction::new(control::invalid);
300 table[SELFDESTRUCT as usize] = Instruction::new(host::selfdestruct);
301 table
302}
303
304const fn gas_table_impl() -> GasTable {
305 use bytecode::opcode::*;
306 let mut table = [0u16; 256];
307
308 table[STOP as usize] = 0;
309 table[ADD as usize] = 3;
310 table[MUL as usize] = 5;
311 table[SUB as usize] = 3;
312 table[DIV as usize] = 5;
313 table[SDIV as usize] = 5;
314 table[MOD as usize] = 5;
315 table[SMOD as usize] = 5;
316 table[ADDMOD as usize] = 8;
317 table[MULMOD as usize] = 8;
318 table[EXP as usize] = gas::EXP as u16; table[SIGNEXTEND as usize] = 5;
320
321 table[LT as usize] = 3;
322 table[GT as usize] = 3;
323 table[SLT as usize] = 3;
324 table[SGT as usize] = 3;
325 table[EQ as usize] = 3;
326 table[ISZERO as usize] = 3;
327 table[AND as usize] = 3;
328 table[OR as usize] = 3;
329 table[XOR as usize] = 3;
330 table[NOT as usize] = 3;
331 table[BYTE as usize] = 3;
332 table[SHL as usize] = 3;
333 table[SHR as usize] = 3;
334 table[SAR as usize] = 3;
335 table[CLZ as usize] = 5;
336
337 table[KECCAK256 as usize] = gas::KECCAK256 as u16;
338
339 table[ADDRESS as usize] = 2;
340 table[BALANCE as usize] = 20;
341 table[ORIGIN as usize] = 2;
342 table[CALLER as usize] = 2;
343 table[CALLVALUE as usize] = 2;
344 table[CALLDATALOAD as usize] = 3;
345 table[CALLDATASIZE as usize] = 2;
346 table[CALLDATACOPY as usize] = 3;
347 table[CODESIZE as usize] = 2;
348 table[CODECOPY as usize] = 3;
349
350 table[GASPRICE as usize] = 2;
351 table[EXTCODESIZE as usize] = 20;
352 table[EXTCODECOPY as usize] = 20;
353 table[RETURNDATASIZE as usize] = 2;
354 table[RETURNDATACOPY as usize] = 3;
355 table[EXTCODEHASH as usize] = 400;
356 table[BLOCKHASH as usize] = 20;
357 table[COINBASE as usize] = 2;
358 table[TIMESTAMP as usize] = 2;
359 table[NUMBER as usize] = 2;
360 table[DIFFICULTY as usize] = 2;
361 table[GASLIMIT as usize] = 2;
362 table[CHAINID as usize] = 2;
363 table[SELFBALANCE as usize] = 5;
364 table[BASEFEE as usize] = 2;
365 table[BLOBHASH as usize] = 3;
366 table[BLOBBASEFEE as usize] = 2;
367 table[SLOTNUM as usize] = 2;
368
369 table[POP as usize] = 2;
370 table[MLOAD as usize] = 3;
371 table[MSTORE as usize] = 3;
372 table[MSTORE8 as usize] = 3;
373 table[SLOAD as usize] = 50;
374 table[SSTORE as usize] = 0;
377 table[JUMP as usize] = 8;
378 table[JUMPI as usize] = 10;
379 table[PC as usize] = 2;
380 table[MSIZE as usize] = 2;
381 table[GAS as usize] = 2;
382 table[JUMPDEST as usize] = 1;
383 table[TLOAD as usize] = 100;
384 table[TSTORE as usize] = 100;
385 table[MCOPY as usize] = 3; table[PUSH0 as usize] = 2;
388 table[PUSH1 as usize] = 3;
389 table[PUSH2 as usize] = 3;
390 table[PUSH3 as usize] = 3;
391 table[PUSH4 as usize] = 3;
392 table[PUSH5 as usize] = 3;
393 table[PUSH6 as usize] = 3;
394 table[PUSH7 as usize] = 3;
395 table[PUSH8 as usize] = 3;
396 table[PUSH9 as usize] = 3;
397 table[PUSH10 as usize] = 3;
398 table[PUSH11 as usize] = 3;
399 table[PUSH12 as usize] = 3;
400 table[PUSH13 as usize] = 3;
401 table[PUSH14 as usize] = 3;
402 table[PUSH15 as usize] = 3;
403 table[PUSH16 as usize] = 3;
404 table[PUSH17 as usize] = 3;
405 table[PUSH18 as usize] = 3;
406 table[PUSH19 as usize] = 3;
407 table[PUSH20 as usize] = 3;
408 table[PUSH21 as usize] = 3;
409 table[PUSH22 as usize] = 3;
410 table[PUSH23 as usize] = 3;
411 table[PUSH24 as usize] = 3;
412 table[PUSH25 as usize] = 3;
413 table[PUSH26 as usize] = 3;
414 table[PUSH27 as usize] = 3;
415 table[PUSH28 as usize] = 3;
416 table[PUSH29 as usize] = 3;
417 table[PUSH30 as usize] = 3;
418 table[PUSH31 as usize] = 3;
419 table[PUSH32 as usize] = 3;
420
421 table[DUP1 as usize] = 3;
422 table[DUP2 as usize] = 3;
423 table[DUP3 as usize] = 3;
424 table[DUP4 as usize] = 3;
425 table[DUP5 as usize] = 3;
426 table[DUP6 as usize] = 3;
427 table[DUP7 as usize] = 3;
428 table[DUP8 as usize] = 3;
429 table[DUP9 as usize] = 3;
430 table[DUP10 as usize] = 3;
431 table[DUP11 as usize] = 3;
432 table[DUP12 as usize] = 3;
433 table[DUP13 as usize] = 3;
434 table[DUP14 as usize] = 3;
435 table[DUP15 as usize] = 3;
436 table[DUP16 as usize] = 3;
437
438 table[SWAP1 as usize] = 3;
439 table[SWAP2 as usize] = 3;
440 table[SWAP3 as usize] = 3;
441 table[SWAP4 as usize] = 3;
442 table[SWAP5 as usize] = 3;
443 table[SWAP6 as usize] = 3;
444 table[SWAP7 as usize] = 3;
445 table[SWAP8 as usize] = 3;
446 table[SWAP9 as usize] = 3;
447 table[SWAP10 as usize] = 3;
448 table[SWAP11 as usize] = 3;
449 table[SWAP12 as usize] = 3;
450 table[SWAP13 as usize] = 3;
451 table[SWAP14 as usize] = 3;
452 table[SWAP15 as usize] = 3;
453 table[SWAP16 as usize] = 3;
454
455 table[DUPN as usize] = 3;
456 table[SWAPN as usize] = 3;
457 table[EXCHANGE as usize] = 3;
458
459 table[LOG0 as usize] = gas::LOG as u16;
460 table[LOG1 as usize] = gas::LOG as u16;
461 table[LOG2 as usize] = gas::LOG as u16;
462 table[LOG3 as usize] = gas::LOG as u16;
463 table[LOG4 as usize] = gas::LOG as u16;
464
465 table[CREATE as usize] = 0;
466 table[CALL as usize] = 40;
467 table[CALLCODE as usize] = 40;
468 table[RETURN as usize] = 0;
469 table[DELEGATECALL as usize] = 40;
470 table[CREATE2 as usize] = 0;
471
472 table[STATICCALL as usize] = 40;
473 table[REVERT as usize] = 0;
474 table[INVALID as usize] = 0;
475 table[SELFDESTRUCT as usize] = 0;
476 table
477}
478
479#[cfg(test)]
480mod tests {
481 use super::instruction_table;
482 use crate::{host::DummyHost, interpreter::EthInterpreter};
483 use bytecode::opcode::*;
484
485 #[test]
486 fn all_instructions_and_opcodes_used() {
487 let unknown_instruction = 0x0C_usize;
489 let instr_table = instruction_table::<EthInterpreter, DummyHost>();
490
491 let unknown_istr = instr_table[unknown_instruction];
492 for (i, instr) in instr_table.iter().enumerate() {
493 let is_opcode_unknown = OpCode::new(i as u8).is_none();
494 let is_instr_unknown = std::ptr::fn_addr_eq(instr.fn_, unknown_istr.fn_);
496 assert_eq!(
497 is_instr_unknown, is_opcode_unknown,
498 "Opcode 0x{i:X?} is not handled",
499 );
500 }
501 }
502}