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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! The EVM Toolkit Operations Crate.
//!
//! You can find more information about the command-line tools in
//! [The ETK Book](https://quilt.github.io/etk/).
//!
//! This crate defines Rust types for all the instructions in the Ethereum
//! Virtual Machine (EVM.)
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![deny(unreachable_pub)]
#![deny(missing_debug_implementations)]

use snafu::{Backtrace, Snafu};

use std::borrow::{Borrow, BorrowMut};

pub mod london {
    //! Instructions available in the London hard fork.
    include!(concat!(env!("OUT_DIR"), "/london.rs"));
}

pub mod shanghai {
    //! Instructions available in the Shanghai hard fork.
    include!(concat!(env!("OUT_DIR"), "/shanghai.rs"));
}

/// Error that can occur when parsing an operation from a string.
#[derive(Debug, Snafu)]
pub struct FromStrError {
    backtrace: Backtrace,
    mnemonic: String,
}

/// Errors that can occur when parsing an operation from a byte slice.
#[derive(Debug, Snafu)]
pub enum FromSliceError<E>
where
    E: 'static + std::fmt::Display + std::error::Error,
{
    /// Converting the byte slice into an immediate failed.
    ///
    /// Often means the slice was the wrong length.
    #[snafu(context(false))]
    TryInto {
        /// The source of this error.
        source: E,

        /// The source location where this error occurred.
        backtrace: Backtrace,
    },

    /// The slice is too long for instructions that do not take an immediate argument.
    NoImmediate {
        /// The source location where this error occurred.
        backtrace: Backtrace,
    },
}

/// Trait for types that contain an immediate argument.
pub trait Immediate<const N: usize> {}

impl<const N: usize> Immediate<N> for [u8; N] {}

impl<const N: usize> Immediate<N> for () {}

/// Immediate type for operations that do not have an immediate argument.
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum Void {}

impl<const N: usize> Immediate<N> for Void {}

/// Trait for describing the types of immediate arguments for operation enums.
pub trait Immediates {
    /// A reference type common to all immediate types ([`Self::P1`], [`Self::P2`], ...)
    ///
    /// For example, for immediates like `[u8; _]`, a possible `ImmediateRef` type
    /// is `[u8]`.
    type ImmediateRef: ?Sized;

    /// A common type that all immediates ([`Self::P1`], [`Self::P2`], ...) can
    /// be converted into.
    ///
    /// For example, for immediates like `[u8; _]`, a possible `Immediate` type
    /// is `Vec<u8>`.
    type Immediate: Borrow<Self::ImmediateRef> + BorrowMut<Self::ImmediateRef>;

    /// The type of immediates used by `push1` instructions.
    type P1: Immediate<1>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push2` instructions.
    type P2: Immediate<2>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push3` instructions.
    type P3: Immediate<3>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push4` instructions.
    type P4: Immediate<4>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push5` instructions.
    type P5: Immediate<5>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push6` instructions.
    type P6: Immediate<6>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push7` instructions.
    type P7: Immediate<7>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push8` instructions.
    type P8: Immediate<8>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push9` instructions.
    type P9: Immediate<9>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push10` instructions.
    type P10: Immediate<10>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push11` instructions.
    type P11: Immediate<11>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push12` instructions.
    type P12: Immediate<12>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push13` instructions.
    type P13: Immediate<13>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push14` instructions.
    type P14: Immediate<14>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push15` instructions.
    type P15: Immediate<15>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push16` instructions.
    type P16: Immediate<16>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push17` instructions.
    type P17: Immediate<17>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push18` instructions.
    type P18: Immediate<18>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push19` instructions.
    type P19: Immediate<19>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push20` instructions.
    type P20: Immediate<20>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push21` instructions.
    type P21: Immediate<21>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push22` instructions.
    type P22: Immediate<22>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push23` instructions.
    type P23: Immediate<23>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push24` instructions.
    type P24: Immediate<24>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push25` instructions.
    type P25: Immediate<25>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push26` instructions.
    type P26: Immediate<26>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push27` instructions.
    type P27: Immediate<27>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push28` instructions.
    type P28: Immediate<28>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push29` instructions.
    type P29: Immediate<29>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push30` instructions.
    type P30: Immediate<30>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push31` instructions.
    type P31: Immediate<31>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;

    /// The type of immediates used by `push32` instructions.
    type P32: Immediate<32>
        + Borrow<Self::ImmediateRef>
        + BorrowMut<Self::ImmediateRef>
        + Into<Self::Immediate>;
}

impl Immediates for () {
    type ImmediateRef = ();
    type Immediate = ();

    type P1 = ();
    type P2 = ();
    type P3 = ();
    type P4 = ();
    type P5 = ();
    type P6 = ();
    type P7 = ();
    type P8 = ();
    type P9 = ();
    type P10 = ();
    type P11 = ();
    type P12 = ();
    type P13 = ();
    type P14 = ();
    type P15 = ();
    type P16 = ();
    type P17 = ();
    type P18 = ();
    type P19 = ();
    type P20 = ();
    type P21 = ();
    type P22 = ();
    type P23 = ();
    type P24 = ();
    type P25 = ();
    type P26 = ();
    type P27 = ();
    type P28 = ();
    type P29 = ();
    type P30 = ();
    type P31 = ();
    type P32 = ();
}

impl Immediates for [u8] {
    type ImmediateRef = [u8];
    type Immediate = Vec<u8>;

    type P1 = [u8; 1];
    type P2 = [u8; 2];
    type P3 = [u8; 3];
    type P4 = [u8; 4];
    type P5 = [u8; 5];
    type P6 = [u8; 6];
    type P7 = [u8; 7];
    type P8 = [u8; 8];
    type P9 = [u8; 9];
    type P10 = [u8; 10];
    type P11 = [u8; 11];
    type P12 = [u8; 12];
    type P13 = [u8; 13];
    type P14 = [u8; 14];
    type P15 = [u8; 15];
    type P16 = [u8; 16];
    type P17 = [u8; 17];
    type P18 = [u8; 18];
    type P19 = [u8; 19];
    type P20 = [u8; 20];
    type P21 = [u8; 21];
    type P22 = [u8; 22];
    type P23 = [u8; 23];
    type P24 = [u8; 24];
    type P25 = [u8; 25];
    type P26 = [u8; 26];
    type P27 = [u8; 27];
    type P28 = [u8; 28];
    type P29 = [u8; 29];
    type P30 = [u8; 30];
    type P31 = [u8; 31];
    type P32 = [u8; 32];
}