|
| 1 | +#![allow(clippy::wrong_self_convention)] |
| 2 | + |
| 3 | +use super::instruction; |
| 4 | +use crate::{instructions::control, primitives::Spec, Host, Interpreter}; |
| 5 | +use std::boxed::Box; |
| 6 | + |
| 7 | +/// EVM opcode function signature. |
| 8 | +pub type Instruction<H> = fn(&mut Interpreter, &mut H); |
| 9 | + |
| 10 | +/// Instruction table is list of instruction function pointers mapped to 256 EVM opcodes. |
| 11 | +pub type InstructionTable<H> = [Instruction<H>; 256]; |
| 12 | + |
| 13 | +/// EVM dynamic opcode function signature. |
| 14 | +pub type DynInstruction<'a, H> = dyn Fn(&mut Interpreter, &mut H) + 'a; |
| 15 | + |
| 16 | +/// EVM boxed dynamic opcode function signature. |
| 17 | +pub type BoxedInstruction<'a, H> = Box<DynInstruction<'a, H>>; |
| 18 | + |
| 19 | +/// A table of boxed instructions. |
| 20 | +pub type BoxedInstructionTable<'a, H> = [BoxedInstruction<'a, H>; 256]; |
| 21 | + |
| 22 | +/// Either a plain, static instruction table, or a boxed, dynamic instruction table. |
| 23 | +/// |
| 24 | +/// Note that `Plain` variant is about 10-20% faster in Interpreter execution. |
| 25 | +pub enum InstructionTables<'a, H: ?Sized> { |
| 26 | + Plain(InstructionTable<H>), |
| 27 | + Boxed(BoxedInstructionTable<'a, H>), |
| 28 | +} |
| 29 | + |
| 30 | +impl<'a, H: Host + ?Sized> InstructionTables<'a, H> { |
| 31 | + /// Creates a plain instruction table for the given spec. See [`make_instruction_table`]. |
| 32 | + #[inline] |
| 33 | + pub const fn new_plain<SPEC: Spec>() -> Self { |
| 34 | + Self::Plain(make_instruction_table::<H, SPEC>()) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a, H: Host + ?Sized + 'a> InstructionTables<'a, H> { |
| 39 | + /// Inserts the instruction into the table with the specified index. |
| 40 | + #[inline] |
| 41 | + pub fn insert(&mut self, opcode: u8, instruction: Instruction<H>) { |
| 42 | + match self { |
| 43 | + Self::Plain(table) => table[opcode as usize] = instruction, |
| 44 | + Self::Boxed(table) => table[opcode as usize] = Box::new(instruction), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Converts the current instruction table to a boxed variant if it is not already, and returns |
| 49 | + /// a mutable reference to the boxed table. |
| 50 | + #[inline] |
| 51 | + pub fn to_boxed(&mut self) -> &mut BoxedInstructionTable<'a, H> { |
| 52 | + self.to_boxed_with(|i| Box::new(i)) |
| 53 | + } |
| 54 | + |
| 55 | + /// Converts the current instruction table to a boxed variant if it is not already with `f`, |
| 56 | + /// and returns a mutable reference to the boxed table. |
| 57 | + #[inline] |
| 58 | + pub fn to_boxed_with<F>(&mut self, f: F) -> &mut BoxedInstructionTable<'a, H> |
| 59 | + where |
| 60 | + F: FnMut(Instruction<H>) -> BoxedInstruction<'a, H>, |
| 61 | + { |
| 62 | + match self { |
| 63 | + Self::Plain(_) => self.to_boxed_with_slow(f), |
| 64 | + Self::Boxed(boxed) => boxed, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + #[cold] |
| 69 | + fn to_boxed_with_slow<F>(&mut self, f: F) -> &mut BoxedInstructionTable<'a, H> |
| 70 | + where |
| 71 | + F: FnMut(Instruction<H>) -> BoxedInstruction<'a, H>, |
| 72 | + { |
| 73 | + let Self::Plain(table) = self else { |
| 74 | + unreachable!() |
| 75 | + }; |
| 76 | + *self = Self::Boxed(make_boxed_instruction_table(table, f)); |
| 77 | + let Self::Boxed(boxed) = self else { |
| 78 | + unreachable!() |
| 79 | + }; |
| 80 | + boxed |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns a mutable reference to the boxed instruction at the specified index. |
| 84 | + #[inline] |
| 85 | + pub fn get_boxed(&mut self, opcode: u8) -> &mut BoxedInstruction<'a, H> { |
| 86 | + &mut self.to_boxed()[opcode as usize] |
| 87 | + } |
| 88 | + |
| 89 | + /// Inserts a boxed instruction into the table at the specified index. |
| 90 | + #[inline] |
| 91 | + pub fn insert_boxed(&mut self, opcode: u8, instruction: BoxedInstruction<'a, H>) { |
| 92 | + *self.get_boxed(opcode) = instruction; |
| 93 | + } |
| 94 | + |
| 95 | + /// Replaces a boxed instruction into the table at the specified index, returning the previous |
| 96 | + /// instruction. |
| 97 | + #[inline] |
| 98 | + pub fn replace_boxed( |
| 99 | + &mut self, |
| 100 | + opcode: u8, |
| 101 | + instruction: BoxedInstruction<'a, H>, |
| 102 | + ) -> BoxedInstruction<'a, H> { |
| 103 | + core::mem::replace(self.get_boxed(opcode), instruction) |
| 104 | + } |
| 105 | + |
| 106 | + /// Replaces a single instruction in the table at the specified index with `f`. |
| 107 | + #[inline] |
| 108 | + pub fn update_boxed<F>(&mut self, opcode: u8, f: F) |
| 109 | + where |
| 110 | + F: Fn(&DynInstruction<'a, H>, &mut Interpreter, &mut H) + 'a, |
| 111 | + { |
| 112 | + update_boxed_instruction(self.get_boxed(opcode), f) |
| 113 | + } |
| 114 | + |
| 115 | + /// Replaces every instruction in the table by calling `f`. |
| 116 | + #[inline] |
| 117 | + pub fn update_all<F>(&mut self, f: F) |
| 118 | + where |
| 119 | + F: Fn(&DynInstruction<'a, H>, &mut Interpreter, &mut H) + Copy + 'a, |
| 120 | + { |
| 121 | + // Don't go through `to_boxed` to avoid allocating the plain table twice. |
| 122 | + match self { |
| 123 | + Self::Plain(_) => { |
| 124 | + self.to_boxed_with(|prev| Box::new(move |i, h| f(&prev, i, h))); |
| 125 | + } |
| 126 | + Self::Boxed(boxed) => boxed |
| 127 | + .iter_mut() |
| 128 | + .for_each(|instruction| update_boxed_instruction(instruction, f)), |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// Make instruction table. |
| 134 | +#[inline] |
| 135 | +pub const fn make_instruction_table<H: Host + ?Sized, SPEC: Spec>() -> InstructionTable<H> { |
| 136 | + // Force const-eval of the table creation, making this function trivial. |
| 137 | + // TODO: Replace this with a `const {}` block once it is stable. |
| 138 | + struct ConstTable<H: Host + ?Sized, SPEC: Spec> { |
| 139 | + _host: core::marker::PhantomData<H>, |
| 140 | + _spec: core::marker::PhantomData<SPEC>, |
| 141 | + } |
| 142 | + impl<H: Host + ?Sized, SPEC: Spec> ConstTable<H, SPEC> { |
| 143 | + const NEW: InstructionTable<H> = { |
| 144 | + let mut tables: InstructionTable<H> = [control::unknown; 256]; |
| 145 | + let mut i = 0; |
| 146 | + while i < 256 { |
| 147 | + tables[i] = instruction::<H, SPEC>(i as u8); |
| 148 | + i += 1; |
| 149 | + } |
| 150 | + tables |
| 151 | + }; |
| 152 | + } |
| 153 | + ConstTable::<H, SPEC>::NEW |
| 154 | +} |
| 155 | + |
| 156 | +/// Make boxed instruction table that calls `f` closure for every instruction. |
| 157 | +#[inline] |
| 158 | +pub fn make_boxed_instruction_table<'a, H, FN>( |
| 159 | + table: &InstructionTable<H>, |
| 160 | + mut f: FN, |
| 161 | +) -> BoxedInstructionTable<'a, H> |
| 162 | +where |
| 163 | + H: Host + ?Sized, |
| 164 | + FN: FnMut(Instruction<H>) -> BoxedInstruction<'a, H>, |
| 165 | +{ |
| 166 | + core::array::from_fn(|i| f(table[i])) |
| 167 | +} |
| 168 | + |
| 169 | +/// Updates a boxed instruction with a new one. |
| 170 | +#[inline] |
| 171 | +pub fn update_boxed_instruction<'a, H, F>(instruction: &mut BoxedInstruction<'a, H>, f: F) |
| 172 | +where |
| 173 | + H: Host + ?Sized + 'a, |
| 174 | + F: Fn(&DynInstruction<'a, H>, &mut Interpreter, &mut H) + 'a, |
| 175 | +{ |
| 176 | + // NOTE: This first allocation gets elided by the compiler. |
| 177 | + let prev = core::mem::replace(instruction, Box::new(|_, _| {})); |
| 178 | + *instruction = Box::new(move |i, h| f(&prev, i, h)); |
| 179 | +} |
0 commit comments