xref: /wasmtime-44.0.1/pulley/src/opcode.rs (revision 4ac1bedf)
1 //! Pulley opcodes (without operands).
2 
3 macro_rules! define_opcode {
4     (
5         $(
6             $( #[$attr:meta] )*
7             $snake_name:ident = $name:ident $( {
8                 $(
9                     $( #[$field_attr:meta] )*
10                     $field:ident : $field_ty:ty
11                 ),*
12             } )? ;
13         )*
14     ) => {
15         /// An opcode without its immediates and operands.
16         #[repr(u8)]
17         #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
18         pub enum Opcode {
19             $(
20                 $( #[$attr] )*
21                 $name,
22             )*
23             /// The extended-op opcode. An `ExtendedOpcode` follows this opcode.
24             ExtendedOp,
25         }
26 
27         impl Opcode {
28             /// The value of the maximum defined opcode.
29             pub const MAX: u8 = define_opcode!( @max $( $name )* ) + 1;
30         }
31     };
32 
33     ( @max $x:ident ) => { 0 };
34     ( @max $x:ident $( $xs:ident )* ) => { 1 + define_opcode!(@max $( $xs )* ) };
35 }
36 for_each_op!(define_opcode);
37 
38 impl Opcode {
39     /// Create a new `Opcode` from the given byte.
40     ///
41     /// Returns `None` if `byte` is not a valid opcode.
42     pub fn new(byte: u8) -> Option<Self> {
43         if byte <= Self::MAX {
44             Some(unsafe { Self::unchecked_new(byte) })
45         } else {
46             None
47         }
48     }
49 
50     /// Like `new` but does not check whether `byte` is a valid opcode.
51     ///
52     /// # Safety
53     ///
54     /// It is unsafe to pass a `byte` that is not a valid opcode.
55     pub unsafe fn unchecked_new(byte: u8) -> Self {
56         debug_assert!(byte <= Self::MAX);
57         core::mem::transmute(byte)
58     }
59 }
60 
61 macro_rules! define_extended_opcode {
62     (
63         $(
64             $( #[$attr:meta] )*
65             $snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
66         )*
67     ) => {
68         /// An extended opcode.
69         #[repr(u16)]
70         #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
71         pub enum ExtendedOpcode {
72             $(
73                 $( #[$attr] )*
74                     $name,
75             )*
76         }
77 
78         impl ExtendedOpcode {
79             /// The value of the maximum defined extended opcode.
80             pub const MAX: u16 = define_opcode!( @max $( $name )* );
81         }
82     };
83 
84     ( @max $x:ident ) => { 0 };
85     ( @max $x:ident $( $xs:ident )* ) => { 1 + define_opcode!(@max $( $xs )* ) };
86 }
87 for_each_extended_op!(define_extended_opcode);
88 
89 impl ExtendedOpcode {
90     pub(crate) const ENCODED_SIZE_OF_TRAP: usize = 3;
91 
92     /// Create a new `ExtendedOpcode` from the given bytes.
93     ///
94     /// Returns `None` if `bytes` is not a valid extended opcode.
95     pub fn new(bytes: u16) -> Option<Self> {
96         if bytes <= Self::MAX {
97             Some(unsafe { Self::unchecked_new(bytes) })
98         } else {
99             None
100         }
101     }
102 
103     /// Like `new` but does not check whether `bytes` is a valid opcode.
104     ///
105     /// # Safety
106     ///
107     /// It is unsafe to pass `bytes` that is not a valid opcode.
108     pub unsafe fn unchecked_new(byte: u16) -> Self {
109         debug_assert!(byte <= Self::MAX);
110         core::mem::transmute(byte)
111     }
112 }
113 
114 #[cfg(all(test, feature = "encode"))]
115 mod tests {
116     use super::*;
117     use alloc::vec::Vec;
118 
119     #[test]
120     fn encoded_size_of_trap() {
121         let mut buf = Vec::new();
122         crate::encode::trap(&mut buf);
123         assert_eq!(ExtendedOpcode::ENCODED_SIZE_OF_TRAP, buf.len());
124     }
125 }
126