eva_asm/instruction/mod.rs
1//! EVM instruction set.
2
3mod arithmetic;
4mod bitwise;
5mod block;
6mod environment;
7mod flow;
8mod logging;
9mod memory;
10mod stack;
11mod storage;
12mod system;
13
14use std::{
15 cmp::Eq as EqTrait,
16 fmt::{Debug, Display},
17 hash::Hash,
18};
19
20pub use arithmetic::*;
21pub use bitwise::*;
22pub use block::*;
23pub use environment::*;
24pub use flow::*;
25pub use logging::*;
26pub use memory::*;
27pub use stack::*;
28pub use storage::*;
29pub use system::*;
30
31use crate::opcode::OpCode;
32
33/// General instruction information.
34pub trait Instruction: Display + Debug + Clone + Copy + PartialEq + EqTrait + Hash {
35 /// Return the operation code associated with this instruction.
36 fn opcode(&self) -> OpCode;
37
38 /// Returns a value signifying whether this instruction is of the type `PUSHx`.
39 ///
40 /// # Example
41 /// ```
42 /// # use eva_asm::instruction::{Push, Gas, InstructionMeta};
43 /// assert_eq!(Push::new([0; 10]).is_push(), true);
44 /// assert_eq!(Gas.is_push(), false);
45 /// ```
46 #[must_use]
47 #[inline]
48 fn is_push(&self) -> bool {
49 self.opcode().is_push()
50 }
51
52 /// Returns a value signifying whether this instruction is of the type `DUPx`.
53 ///
54 /// # Example
55 /// ```
56 /// # use eva_asm::instruction::{Dup, Gas, InstructionMeta};
57 /// assert_eq!(Dup::<10>::new().is_dup(), true);
58 /// assert_eq!(Gas.is_dup(), false);
59 /// ```
60 #[must_use]
61 #[inline]
62 fn is_dup(&self) -> bool {
63 self.opcode().is_dup()
64 }
65
66 /// Returns a value signifying whether this instruction is of the type `SWAPx`.
67 ///
68 /// # Example
69 /// ```
70 /// # use eva_asm::instruction::{Swap, Gas, InstructionMeta};
71 /// assert_eq!(Swap::<10>::new().is_swap(), true);
72 /// assert_eq!(Gas.is_swap(), false);
73 /// ```
74 #[must_use]
75 #[inline]
76 fn is_swap(&self) -> bool {
77 self.opcode().is_swap()
78 }
79
80 /// Returns a value signifying whether this instruction is of the type `LOGx`.
81 ///
82 /// # Example
83 /// ```
84 /// # use eva_asm::instruction::{Log, Gas, InstructionMeta};
85 /// assert_eq!(Log::<3>::new().is_log(), true);
86 /// assert_eq!(Gas.is_log(), false);
87 /// ```
88 #[must_use]
89 #[inline]
90 fn is_log(&self) -> bool {
91 self.opcode().is_log()
92 }
93
94 /// Returns [`true`] for instructions that terminate execution of the smart contract.
95 ///
96 /// # Example
97 /// ```
98 /// # use eva_asm::instruction::{Return, Unknown, Gas, InstructionMeta};
99 /// assert_eq!(Return.is_terminator(), true);
100 /// assert_eq!(Unknown(0xF).is_terminator(), true);
101 /// assert_eq!(Gas.is_terminator(), false);
102 /// ```
103 #[must_use]
104 #[inline]
105 fn is_terminator(&self) -> bool {
106 self.opcode().is_terminator()
107 }
108}