revm_interpreter/
table.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![allow(clippy::wrong_self_convention)]

use crate::{
    instructions::{control, instruction},
    interpreter::Interpreter,
    interpreter_types::InterpreterTypes,
    Host,
};
use std::boxed::Box;

/// EVM opcode function signature.
pub type Instruction<W, H> = for<'a> fn(&'a mut Interpreter<W>, &'a mut H);

/// Instruction table is list of instruction function pointers mapped to 256 EVM opcodes.
pub type InstructionTable<W, H> = [Instruction<W, H>; 256];

/// EVM dynamic opcode function signature.
pub type DynInstruction<W, H> = dyn Fn(&mut Interpreter<W>, &mut H);

/// A table of boxed instructions.
pub type CustomInstructionTable<IT> = [IT; 256];

pub trait CustomInstruction {
    type Wire: InterpreterTypes;
    type Host;

    fn exec(&self, interpreter: &mut Interpreter<Self::Wire>, host: &mut Self::Host);

    fn from_base(instruction: Instruction<Self::Wire, Self::Host>) -> Self;
}

/// Either a plain, static instruction table, or a boxed, dynamic instruction table.
///
/// Note that `Plain` variant is about 10-20% faster in Interpreter execution.
pub enum InstructionTables<
    W: InterpreterTypes,
    H: ?Sized,
    CI: CustomInstruction<Host = H, Wire = W>,
> {
    Plain(Box<InstructionTable<W, H>>),
    Custom(Box<CustomInstructionTable<CI>>),
}

impl<WIRE, H, CI> InstructionTables<WIRE, H, CI>
where
    WIRE: InterpreterTypes,
    H: Host + ?Sized,
    CI: CustomInstruction<Host = H, Wire = WIRE>,
{
    /// Inserts the instruction into the table with the specified index.
    #[inline]
    pub fn insert(&mut self, opcode: u8, instruction: Instruction<WIRE, H>) {
        match self {
            Self::Plain(table) => table[opcode as usize] = instruction,
            Self::Custom(table) => table[opcode as usize] = CI::from_base(instruction),
        }
    }

    /// Converts the current instruction table to a boxed variant if it is not already, and returns
    /// a mutable reference to the boxed table.
    #[inline]
    pub fn to_custom(&mut self) -> &mut CustomInstructionTable<CI> {
        self.to_custom_with(|i| CI::from_base(i))
    }

    /// Converts the current instruction table to a boxed variant if it is not already with `f`,
    /// and returns a mutable reference to the boxed table.
    #[inline]
    pub fn to_custom_with<F>(&mut self, f: F) -> &mut CustomInstructionTable<CI>
    where
        F: FnMut(Instruction<WIRE, H>) -> CI,
    {
        match self {
            Self::Plain(_) => self.to_custom_with_slow(f),
            Self::Custom(boxed) => boxed,
        }
    }

    #[cold]
    fn to_custom_with_slow<F>(&mut self, f: F) -> &mut CustomInstructionTable<CI>
    where
        F: FnMut(Instruction<WIRE, H>) -> CI,
    {
        let Self::Plain(table) = self else {
            unreachable!()
        };
        *self = Self::Custom(Box::new(make_custom_instruction_table(table, f)));
        let Self::Custom(boxed) = self else {
            unreachable!()
        };
        boxed
    }

    /// Returns a mutable reference to the boxed instruction at the specified index.
    #[inline]
    pub fn get_custom(&mut self, opcode: u8) -> &mut CI {
        &mut self.to_custom()[opcode as usize]
    }

    /// Inserts a boxed instruction into the table at the specified index.
    #[inline]
    pub fn insert_custom(&mut self, opcode: u8, instruction: CI) {
        *self.get_custom(opcode) = instruction;
    }

    /// Replaces a boxed instruction into the table at the specified index, returning the previous
    /// instruction.
    #[inline]
    pub fn replace_boxed(&mut self, opcode: u8, instruction: CI) -> CI {
        core::mem::replace(self.get_custom(opcode), instruction)
    }
}

/// Make instruction table.
#[inline]
pub const fn make_instruction_table<WIRE: InterpreterTypes, H: Host + ?Sized>(
) -> InstructionTable<WIRE, H> {
    const {
        let mut tables: InstructionTable<WIRE, H> = [control::unknown; 256];
        let mut i = 0;
        while i < 256 {
            tables[i] = instruction::<WIRE, H>(i as u8);
            i += 1;
        }
        tables
    }
}

/// Make boxed instruction table that calls `f` closure for every instruction.
#[inline]
pub fn make_custom_instruction_table<W, H, FN, CI: CustomInstruction<Wire = W, Host = H>>(
    table: &InstructionTable<W, H>,
    mut f: FN,
) -> CustomInstructionTable<CI>
where
    W: InterpreterTypes,
    H: Host + ?Sized,
    FN: FnMut(Instruction<W, H>) -> CI,
{
    core::array::from_fn(|i| f(table[i]))
}

// TODO
// /// Updates a boxed instruction with a new one.
// #[inline]
// pub fn update_custom_instruction<W, H, F>(
//     instruction: &mut impl CustomInstruction<Wire = W, Host = H>,
//     f: F,
// ) where
//     W: InterpreterTypes,
//     H: Host + ?Sized,
//     F: Fn(&DynInstruction<W, H>, &mut W, &mut H),
// {
//     // Note: This first allocation gets elided by the compiler.
//     let prev = core::mem::replace(instruction, Box::new(|_, _| {}));
//     *instruction = Box::new(move |i, h| f(&prev, i, h));
// }