1use core::marker::PhantomData;
2
3#[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "archive", serde(transparent))]
6#[repr(transparent)]
7pub struct OperandIdx<T, const WIDTH: usize> {
8 index: u32,
9 #[cfg_attr(feature = "archive", serde(skip))]
10 marker: PhantomData<T>,
11}
12
13pub type Operand64Idx<T> = OperandIdx<T, 8>;
15
16pub type Operand128Idx<T> = OperandIdx<T, 16>;
18
19impl<T, const WIDTH: usize> OperandIdx<T, WIDTH> {
20 #[inline]
21 #[doc(hidden)]
22 pub const fn new(index: u32) -> Self {
23 Self { index, marker: PhantomData }
24 }
25
26 #[inline]
28 pub const fn index(self) -> usize {
29 self.index as usize
30 }
31}
32
33impl<T, const WIDTH: usize> Copy for OperandIdx<T, WIDTH> {}
34impl<T, const WIDTH: usize> Clone for OperandIdx<T, WIDTH> {
35 fn clone(&self) -> Self {
36 *self
37 }
38}
39impl<T, const WIDTH: usize> PartialEq for OperandIdx<T, WIDTH> {
40 fn eq(&self, other: &Self) -> bool {
41 self.index == other.index
42 }
43}
44impl<T, const WIDTH: usize> Eq for OperandIdx<T, WIDTH> {}
45
46#[cfg(feature = "debug")]
47impl<T, const WIDTH: usize> core::fmt::Debug for OperandIdx<T, WIDTH> {
48 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49 self.index.fmt(formatter)
50 }
51}
52
53macro_rules! define_operand {
54 ($name:ident, $len:literal) => {
55 #[doc = concat!("A typed packed ", $len, "-byte operand value.")]
56 #[repr(C)]
57 pub struct $name<T = ()> {
58 pub(crate) bytes: [u8; $len],
59 marker: PhantomData<T>,
60 }
61
62 #[allow(dead_code)]
63 impl<T> $name<T> {
64 #[inline]
65 pub(crate) fn write_u8<const OFFSET: usize>(&mut self, value: u8) {
66 const { assert!(OFFSET < $len) };
67 self.bytes[OFFSET] = value;
68 }
69
70 #[inline]
71 pub(crate) fn write_u16<const OFFSET: usize>(&mut self, value: u16) {
72 const { assert!(OFFSET + 2 <= $len) };
73 self.bytes[OFFSET..OFFSET + 2].copy_from_slice(&value.to_le_bytes());
74 }
75
76 #[inline]
77 pub(crate) fn write_u32<const OFFSET: usize>(&mut self, value: u32) {
78 const { assert!(OFFSET + 4 <= $len) };
79 self.bytes[OFFSET..OFFSET + 4].copy_from_slice(&value.to_le_bytes());
80 }
81
82 #[inline]
83 pub(crate) fn write_u64<const OFFSET: usize>(&mut self, value: u64) {
84 const { assert!(OFFSET + 8 <= $len) };
85 self.bytes[OFFSET..OFFSET + 8].copy_from_slice(&value.to_le_bytes());
86 }
87
88 #[inline(always)]
89 #[doc(hidden)]
90 pub fn cast<U>(self) -> $name<U> {
91 $name { bytes: self.bytes, marker: PhantomData }
92 }
93 }
94
95 impl<T> Copy for $name<T> {}
96 impl<T> Clone for $name<T> {
97 fn clone(&self) -> Self {
98 *self
99 }
100 }
101 impl<T> PartialEq for $name<T> {
102 fn eq(&self, other: &Self) -> bool {
103 self.bytes == other.bytes
104 }
105 }
106 impl<T> Eq for $name<T> {}
107 impl<T> PartialOrd for $name<T> {
108 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
109 Some(self.cmp(other))
110 }
111 }
112 impl<T> Ord for $name<T> {
113 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
114 self.bytes.cmp(&other.bytes)
115 }
116 }
117 impl<T> Default for $name<T> {
118 fn default() -> Self {
119 Self { bytes: [0; $len], marker: PhantomData }
120 }
121 }
122
123 #[cfg(feature = "debug")]
124 impl<T> core::fmt::Debug for $name<T> {
125 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
126 self.bytes.fmt(formatter)
127 }
128 }
129
130 #[cfg(feature = "archive")]
131 impl<T> serde::Serialize for $name<T> {
132 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
133 self.bytes.serialize(serializer)
134 }
135 }
136
137 #[cfg(feature = "archive")]
138 impl<'de, T> serde::Deserialize<'de> for $name<T> {
139 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
140 Ok(Self { bytes: <[u8; $len]>::deserialize(deserializer)?, marker: PhantomData })
141 }
142 }
143 };
144}
145
146define_operand!(Operand64, 8);
147define_operand!(Operand128, 16);
148
149impl<T> Operand64Idx<T> {
150 #[inline(always)]
152 pub fn resolve(self, data: &crate::WasmFunctionData) -> Operand64<T> {
153 data.operands64[self.index as usize].cast()
154 }
155}
156
157impl<T> Operand128Idx<T> {
158 #[inline(always)]
160 pub fn resolve(self, data: &crate::WasmFunctionData) -> Operand128<T> {
161 data.operands128[self.index as usize].cast()
162 }
163}
164
165macro_rules! field_ty {
166 (u8) => { u8 }; (u16) => { u16 }; (u32) => { u32 }; (u64) => { u64 };
167 (i32) => { i32 }; (i64) => { i64 }; (idx128) => { Operand128Idx<[u8; 16]> };
168}
169macro_rules! field_size {
170 (u8) => {{ 1 }};
171 (u16) => {{ 2 }};
172 (u32) => {{ 4 }};
173 (u64) => {{ 8 }};
174 (i32) => {{ 4 }};
175 (i64) => {{ 8 }};
176 (idx128) => {{ 4 }};
177}
178
179macro_rules! read_field {
180 ($self:ident, u8, $offset:expr) => {{ $self.bytes[$offset] }};
181 ($self:ident, u16, $offset:expr) => {{ u16::from_le_bytes($crate::operands::read_bytes!($self, $offset, 2)) }};
182 ($self:ident, u32, $offset:expr) => {{ u32::from_le_bytes($crate::operands::read_bytes!($self, $offset, 4)) }};
183 ($self:ident, u64, $offset:expr) => {{ u64::from_le_bytes($crate::operands::read_bytes!($self, $offset, 8)) }};
184 ($self:ident, i32, $offset:expr) => {{ read_field!($self, u32, $offset) as i32 }};
185 ($self:ident, i64, $offset:expr) => {{ read_field!($self, u64, $offset) as i64 }};
186 ($self:ident, idx128, $offset:expr) => {{ Operand128Idx::new(read_field!($self, u32, $offset)) }};
187}
188
189macro_rules! read_bytes {
190 ($self:ident, $offset:expr, $len:literal) => {{
191 match <[u8; $len]>::try_from(&$self.bytes[$offset..$offset + $len]) {
192 Ok(bytes) => bytes,
193 Err(_) => {
194 core::hint::cold_path();
195 unreachable!("invalid operand field read")
196 }
197 }
198 }};
199}
200
201macro_rules! write_field {
202 ($self:ident, u8, $offset:expr, $value:expr) => {{ $self.write_u8::<{ $offset }>($value) }};
203 ($self:ident, u16, $offset:expr, $value:expr) => {{ $self.write_u16::<{ $offset }>($value) }};
204 ($self:ident, u32, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value) }};
205 ($self:ident, u64, $offset:expr, $value:expr) => {{ $self.write_u64::<{ $offset }>($value) }};
206 ($self:ident, i32, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value as u32) }};
207 ($self:ident, i64, $offset:expr, $value:expr) => {{ $self.write_u64::<{ $offset }>($value as u64) }};
208 ($self:ident, idx128, $offset:expr, $value:expr) => {{ $self.write_u32::<{ $offset }>($value.index) }};
209}
210
211pub(crate) use {read_bytes, read_field, write_field};
212
213impl<T> Operand64<T> {
214 #[inline(always)]
216 pub fn target(&self) -> u32 {
217 read_field!(self, u32, 0)
218 }
219
220 #[inline]
222 pub fn with_target(mut self, target: u32) -> Self {
223 write_field!(self, u32, 0, target);
224 self
225 }
226}
227
228impl<T> Operand128<T> {
229 #[inline(always)]
231 pub fn target(&self) -> u32 {
232 read_field!(self, u32, 0)
233 }
234
235 #[inline]
237 pub fn with_target(mut self, target: u32) -> Self {
238 write_field!(self, u32, 0, target);
239 self
240 }
241}
242
243macro_rules! packed_layout {
244 ($operand:ident, $lane:ident, $field:tt) => {
245 impl $operand<field_ty!($field)> {
246 #[inline]
247 pub fn new(value: field_ty!($field)) -> Self {
248 let mut operand = Self::default();
249 write_field!(operand, $field, 0, value);
250 operand
251 }
252
253 #[inline(always)]
254 pub fn value(&self) -> field_ty!($field) {
255 read_field!(self, $field, 0)
256 }
257 }
258 };
259 ($operand:ident, $lane:ident, $a:tt, $b:tt) => {
260 impl $operand<(field_ty!($a), field_ty!($b))> {
261 #[inline]
262 pub fn new(a: field_ty!($a), b: field_ty!($b)) -> Self {
263 let mut operand = Self::default();
264 write_field!(operand, $a, 0, a);
265 write_field!(operand, $b, field_size!($a), b);
266 operand
267 }
268
269 #[inline(always)]
270 pub fn a(&self) -> field_ty!($a) {
271 read_field!(self, $a, 0)
272 }
273
274 #[inline(always)]
275 pub fn b(&self) -> field_ty!($b) {
276 read_field!(self, $b, field_size!($a))
277 }
278 }
279 };
280 ($operand:ident, $lane:ident, $a:tt, $b:tt, $c:tt) => {
281 impl $operand<(field_ty!($a), field_ty!($b), field_ty!($c))> {
282 #[inline]
283 pub fn new(a: field_ty!($a), b: field_ty!($b), c: field_ty!($c)) -> Self {
284 let mut operand = Self::default();
285 write_field!(operand, $a, 0, a);
286 write_field!(operand, $b, field_size!($a), b);
287 write_field!(operand, $c, field_size!($a) + field_size!($b), c);
288 operand
289 }
290
291 #[inline(always)]
292 pub fn a(&self) -> field_ty!($a) {
293 read_field!(self, $a, 0)
294 }
295
296 #[inline(always)]
297 pub fn b(&self) -> field_ty!($b) {
298 read_field!(self, $b, field_size!($a))
299 }
300
301 #[inline(always)]
302 pub fn c(&self) -> field_ty!($c) {
303 read_field!(self, $c, field_size!($a) + field_size!($b))
304 }
305 }
306 };
307}
308
309packed_layout!(Operand64, operands64, i64);
310packed_layout!(Operand64, operands64, u32, u32);
311packed_layout!(Operand64, operands64, u16, u32);
312packed_layout!(Operand64, operands64, u32, u16);
313packed_layout!(Operand64, operands64, u16, u16, u16);
314packed_layout!(Operand64, operands64, u16, u16, u32);
315packed_layout!(Operand64, operands64, u32, i32);
316packed_layout!(Operand64, operands64, u32, u16, u16);
317packed_layout!(Operand64, operands64, u16, idx128);
318packed_layout!(Operand64, operands64, u32, idx128);
319packed_layout!(Operand64, operands64, u16, u16, idx128);
320packed_layout!(Operand128, operands128, u16, u64);
321packed_layout!(Operand128, operands128, u32, u64);
322packed_layout!(Operand128, operands128, u16, u16, u64);
323packed_layout!(Operand128, operands128, u32, i64);
324packed_layout!(Operand128, operands128, u32, i32, u16);
325
326impl Operand128<[u8; 16]> {
327 #[inline]
328 pub fn new(value: [u8; 16]) -> Self {
329 Self { bytes: value, marker: PhantomData }
330 }
331
332 #[inline(always)]
333 pub fn value(&self) -> [u8; 16] {
334 self.bytes
335 }
336}
337
338const _: () = {
339 assert!(core::mem::size_of::<Operand64Idx<i64>>() == 4);
340 assert!(core::mem::size_of::<Operand64>() == 8);
341 assert!(core::mem::size_of::<Operand128>() == 16);
342};