[go: up one dir, main page]

etk_ops/
lib.rs

1//! The EVM Toolkit Operations Crate.
2//!
3//! You can find more information about the command-line tools in
4//! [The ETK Book](https://quilt.github.io/etk/).
5//!
6//! This crate defines Rust types for all the instructions in the Ethereum
7//! Virtual Machine (EVM.)
8#![deny(unsafe_code)]
9#![deny(missing_docs)]
10#![deny(unreachable_pub)]
11#![deny(missing_debug_implementations)]
12
13use snafu::{Backtrace, Snafu};
14
15use std::borrow::{Borrow, BorrowMut};
16
17pub mod london {
18    //! Instructions available in the London hard fork.
19    include!(concat!(env!("OUT_DIR"), "/london.rs"));
20}
21
22pub mod shanghai {
23    //! Instructions available in the Shanghai hard fork.
24    include!(concat!(env!("OUT_DIR"), "/shanghai.rs"));
25}
26
27/// Error that can occur when parsing an operation from a string.
28#[derive(Debug, Snafu)]
29pub struct FromStrError {
30    backtrace: Backtrace,
31    mnemonic: String,
32}
33
34/// Errors that can occur when parsing an operation from a byte slice.
35#[derive(Debug, Snafu)]
36pub enum FromSliceError<E>
37where
38    E: 'static + std::fmt::Display + std::error::Error,
39{
40    /// Converting the byte slice into an immediate failed.
41    ///
42    /// Often means the slice was the wrong length.
43    #[snafu(context(false))]
44    TryInto {
45        /// The source of this error.
46        source: E,
47
48        /// The source location where this error occurred.
49        backtrace: Backtrace,
50    },
51
52    /// The slice is too long for instructions that do not take an immediate argument.
53    NoImmediate {
54        /// The source location where this error occurred.
55        backtrace: Backtrace,
56    },
57}
58
59/// Trait for types that contain an immediate argument.
60pub trait Immediate<const N: usize> {}
61
62impl<const N: usize> Immediate<N> for [u8; N] {}
63
64impl<const N: usize> Immediate<N> for () {}
65
66/// Immediate type for operations that do not have an immediate argument.
67#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
68pub enum Void {}
69
70impl<const N: usize> Immediate<N> for Void {}
71
72/// Trait for describing the types of immediate arguments for operation enums.
73pub trait Immediates {
74    /// A reference type common to all immediate types ([`Self::P1`], [`Self::P2`], ...)
75    ///
76    /// For example, for immediates like `[u8; _]`, a possible `ImmediateRef` type
77    /// is `[u8]`.
78    type ImmediateRef: ?Sized;
79
80    /// A common type that all immediates ([`Self::P1`], [`Self::P2`], ...) can
81    /// be converted into.
82    ///
83    /// For example, for immediates like `[u8; _]`, a possible `Immediate` type
84    /// is `Vec<u8>`.
85    type Immediate: Borrow<Self::ImmediateRef> + BorrowMut<Self::ImmediateRef>;
86
87    /// The type of immediates used by `push1` instructions.
88    type P1: Immediate<1>
89        + Borrow<Self::ImmediateRef>
90        + BorrowMut<Self::ImmediateRef>
91        + Into<Self::Immediate>;
92
93    /// The type of immediates used by `push2` instructions.
94    type P2: Immediate<2>
95        + Borrow<Self::ImmediateRef>
96        + BorrowMut<Self::ImmediateRef>
97        + Into<Self::Immediate>;
98
99    /// The type of immediates used by `push3` instructions.
100    type P3: Immediate<3>
101        + Borrow<Self::ImmediateRef>
102        + BorrowMut<Self::ImmediateRef>
103        + Into<Self::Immediate>;
104
105    /// The type of immediates used by `push4` instructions.
106    type P4: Immediate<4>
107        + Borrow<Self::ImmediateRef>
108        + BorrowMut<Self::ImmediateRef>
109        + Into<Self::Immediate>;
110
111    /// The type of immediates used by `push5` instructions.
112    type P5: Immediate<5>
113        + Borrow<Self::ImmediateRef>
114        + BorrowMut<Self::ImmediateRef>
115        + Into<Self::Immediate>;
116
117    /// The type of immediates used by `push6` instructions.
118    type P6: Immediate<6>
119        + Borrow<Self::ImmediateRef>
120        + BorrowMut<Self::ImmediateRef>
121        + Into<Self::Immediate>;
122
123    /// The type of immediates used by `push7` instructions.
124    type P7: Immediate<7>
125        + Borrow<Self::ImmediateRef>
126        + BorrowMut<Self::ImmediateRef>
127        + Into<Self::Immediate>;
128
129    /// The type of immediates used by `push8` instructions.
130    type P8: Immediate<8>
131        + Borrow<Self::ImmediateRef>
132        + BorrowMut<Self::ImmediateRef>
133        + Into<Self::Immediate>;
134
135    /// The type of immediates used by `push9` instructions.
136    type P9: Immediate<9>
137        + Borrow<Self::ImmediateRef>
138        + BorrowMut<Self::ImmediateRef>
139        + Into<Self::Immediate>;
140
141    /// The type of immediates used by `push10` instructions.
142    type P10: Immediate<10>
143        + Borrow<Self::ImmediateRef>
144        + BorrowMut<Self::ImmediateRef>
145        + Into<Self::Immediate>;
146
147    /// The type of immediates used by `push11` instructions.
148    type P11: Immediate<11>
149        + Borrow<Self::ImmediateRef>
150        + BorrowMut<Self::ImmediateRef>
151        + Into<Self::Immediate>;
152
153    /// The type of immediates used by `push12` instructions.
154    type P12: Immediate<12>
155        + Borrow<Self::ImmediateRef>
156        + BorrowMut<Self::ImmediateRef>
157        + Into<Self::Immediate>;
158
159    /// The type of immediates used by `push13` instructions.
160    type P13: Immediate<13>
161        + Borrow<Self::ImmediateRef>
162        + BorrowMut<Self::ImmediateRef>
163        + Into<Self::Immediate>;
164
165    /// The type of immediates used by `push14` instructions.
166    type P14: Immediate<14>
167        + Borrow<Self::ImmediateRef>
168        + BorrowMut<Self::ImmediateRef>
169        + Into<Self::Immediate>;
170
171    /// The type of immediates used by `push15` instructions.
172    type P15: Immediate<15>
173        + Borrow<Self::ImmediateRef>
174        + BorrowMut<Self::ImmediateRef>
175        + Into<Self::Immediate>;
176
177    /// The type of immediates used by `push16` instructions.
178    type P16: Immediate<16>
179        + Borrow<Self::ImmediateRef>
180        + BorrowMut<Self::ImmediateRef>
181        + Into<Self::Immediate>;
182
183    /// The type of immediates used by `push17` instructions.
184    type P17: Immediate<17>
185        + Borrow<Self::ImmediateRef>
186        + BorrowMut<Self::ImmediateRef>
187        + Into<Self::Immediate>;
188
189    /// The type of immediates used by `push18` instructions.
190    type P18: Immediate<18>
191        + Borrow<Self::ImmediateRef>
192        + BorrowMut<Self::ImmediateRef>
193        + Into<Self::Immediate>;
194
195    /// The type of immediates used by `push19` instructions.
196    type P19: Immediate<19>
197        + Borrow<Self::ImmediateRef>
198        + BorrowMut<Self::ImmediateRef>
199        + Into<Self::Immediate>;
200
201    /// The type of immediates used by `push20` instructions.
202    type P20: Immediate<20>
203        + Borrow<Self::ImmediateRef>
204        + BorrowMut<Self::ImmediateRef>
205        + Into<Self::Immediate>;
206
207    /// The type of immediates used by `push21` instructions.
208    type P21: Immediate<21>
209        + Borrow<Self::ImmediateRef>
210        + BorrowMut<Self::ImmediateRef>
211        + Into<Self::Immediate>;
212
213    /// The type of immediates used by `push22` instructions.
214    type P22: Immediate<22>
215        + Borrow<Self::ImmediateRef>
216        + BorrowMut<Self::ImmediateRef>
217        + Into<Self::Immediate>;
218
219    /// The type of immediates used by `push23` instructions.
220    type P23: Immediate<23>
221        + Borrow<Self::ImmediateRef>
222        + BorrowMut<Self::ImmediateRef>
223        + Into<Self::Immediate>;
224
225    /// The type of immediates used by `push24` instructions.
226    type P24: Immediate<24>
227        + Borrow<Self::ImmediateRef>
228        + BorrowMut<Self::ImmediateRef>
229        + Into<Self::Immediate>;
230
231    /// The type of immediates used by `push25` instructions.
232    type P25: Immediate<25>
233        + Borrow<Self::ImmediateRef>
234        + BorrowMut<Self::ImmediateRef>
235        + Into<Self::Immediate>;
236
237    /// The type of immediates used by `push26` instructions.
238    type P26: Immediate<26>
239        + Borrow<Self::ImmediateRef>
240        + BorrowMut<Self::ImmediateRef>
241        + Into<Self::Immediate>;
242
243    /// The type of immediates used by `push27` instructions.
244    type P27: Immediate<27>
245        + Borrow<Self::ImmediateRef>
246        + BorrowMut<Self::ImmediateRef>
247        + Into<Self::Immediate>;
248
249    /// The type of immediates used by `push28` instructions.
250    type P28: Immediate<28>
251        + Borrow<Self::ImmediateRef>
252        + BorrowMut<Self::ImmediateRef>
253        + Into<Self::Immediate>;
254
255    /// The type of immediates used by `push29` instructions.
256    type P29: Immediate<29>
257        + Borrow<Self::ImmediateRef>
258        + BorrowMut<Self::ImmediateRef>
259        + Into<Self::Immediate>;
260
261    /// The type of immediates used by `push30` instructions.
262    type P30: Immediate<30>
263        + Borrow<Self::ImmediateRef>
264        + BorrowMut<Self::ImmediateRef>
265        + Into<Self::Immediate>;
266
267    /// The type of immediates used by `push31` instructions.
268    type P31: Immediate<31>
269        + Borrow<Self::ImmediateRef>
270        + BorrowMut<Self::ImmediateRef>
271        + Into<Self::Immediate>;
272
273    /// The type of immediates used by `push32` instructions.
274    type P32: Immediate<32>
275        + Borrow<Self::ImmediateRef>
276        + BorrowMut<Self::ImmediateRef>
277        + Into<Self::Immediate>;
278}
279
280impl Immediates for () {
281    type ImmediateRef = ();
282    type Immediate = ();
283
284    type P1 = ();
285    type P2 = ();
286    type P3 = ();
287    type P4 = ();
288    type P5 = ();
289    type P6 = ();
290    type P7 = ();
291    type P8 = ();
292    type P9 = ();
293    type P10 = ();
294    type P11 = ();
295    type P12 = ();
296    type P13 = ();
297    type P14 = ();
298    type P15 = ();
299    type P16 = ();
300    type P17 = ();
301    type P18 = ();
302    type P19 = ();
303    type P20 = ();
304    type P21 = ();
305    type P22 = ();
306    type P23 = ();
307    type P24 = ();
308    type P25 = ();
309    type P26 = ();
310    type P27 = ();
311    type P28 = ();
312    type P29 = ();
313    type P30 = ();
314    type P31 = ();
315    type P32 = ();
316}
317
318impl Immediates for [u8] {
319    type ImmediateRef = [u8];
320    type Immediate = Vec<u8>;
321
322    type P1 = [u8; 1];
323    type P2 = [u8; 2];
324    type P3 = [u8; 3];
325    type P4 = [u8; 4];
326    type P5 = [u8; 5];
327    type P6 = [u8; 6];
328    type P7 = [u8; 7];
329    type P8 = [u8; 8];
330    type P9 = [u8; 9];
331    type P10 = [u8; 10];
332    type P11 = [u8; 11];
333    type P12 = [u8; 12];
334    type P13 = [u8; 13];
335    type P14 = [u8; 14];
336    type P15 = [u8; 15];
337    type P16 = [u8; 16];
338    type P17 = [u8; 17];
339    type P18 = [u8; 18];
340    type P19 = [u8; 19];
341    type P20 = [u8; 20];
342    type P21 = [u8; 21];
343    type P22 = [u8; 22];
344    type P23 = [u8; 23];
345    type P24 = [u8; 24];
346    type P25 = [u8; 25];
347    type P26 = [u8; 26];
348    type P27 = [u8; 27];
349    type P28 = [u8; 28];
350    type P29 = [u8; 29];
351    type P30 = [u8; 30];
352    type P31 = [u8; 31];
353    type P32 = [u8; 32];
354}