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
#[cfg(feature = "simd")]
use crate::core::simd::{ImmLaneIdx16, ImmLaneIdx2, ImmLaneIdx4, ImmLaneIdx8};
#[cfg(all(feature = "simd", doc))]
use crate::core::V128;
use crate::{core::TrapCode, index::*, primitive::Offset64Hi, *};
use ::core::num::{NonZeroI32, NonZeroI64, NonZeroU32, NonZeroU64};
macro_rules! define_enum {
(
$(
$( #[doc = $doc:literal] )*
#[snake_name($snake_name:ident)]
$name:ident
$(
{
$(
@ $result_name:ident: $result_ty:ty,
)?
$(
$( #[$field_docs:meta] )*
$field_name:ident: $field_ty:ty
),*
$(,)?
}
)?
),* $(,)?
) => {
/// A Wasmi instruction.
///
/// Wasmi instructions are composed of so-called instruction words.
/// This type represents all such words and for simplicity we call the type [`Instruction`], still.
///
/// Most instructions are composed of a single instruction word. An example of
/// this is [`Instruction::I32Add`]. However, some instructions, like the `select` instructions
/// are composed of two or more instruction words.
///
/// The Wasmi bytecode translation makes sure that instructions always appear in valid sequences.
/// The Wasmi executor relies on the guarantees that the Wasmi translator provides.
///
/// The documentation of each [`Instruction`] describes its encoding in the
/// `#Encoding` section of its documentation if it requires more than a single
/// instruction for its encoding.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[repr(u16)]
pub enum Instruction {
$(
$( #[doc = $doc] )*
$name
$(
{
$(
/// The register(s) storing the result of the instruction.
$result_name: $result_ty,
)?
$(
$( #[$field_docs] )*
$field_name: $field_ty
),*
}
)?
),*
}
impl Instruction {
$(
#[doc = concat!("Creates a new [`Instruction::", stringify!($name), "`].")]
pub fn $snake_name(
$(
$( $result_name: impl Into<$result_ty>, )?
$( $field_name: impl Into<$field_ty> ),*
)?
) -> Self {
Self::$name {
$(
$( $result_name: $result_name.into(), )?
$( $field_name: $field_name.into() ),*
)?
}
}
)*
}
};
}
for_each_op::for_each_op!(define_enum);
/// Helper trait for [`Instruction::result`] method implementation.
trait IntoReg: Sized {
/// Converts `self` into a [`Reg`] if possible.
fn into_reg(self) -> Option<Reg> {
None
}
}
impl IntoReg for Reg {
fn into_reg(self) -> Option<Reg> {
Some(self)
}
}
impl IntoReg for [Reg; 2] {}
impl IntoReg for RegSpan {}
impl<const N: u16> IntoReg for FixedRegSpan<N> {}
impl IntoReg for () {}
macro_rules! define_result {
(
$(
$( #[doc = $doc:literal] )*
#[snake_name($snake_name:ident)]
$name:ident
$(
{
$(
@ $result_name:ident: $result_ty:ty,
)?
$(
$( #[$field_docs:meta] )*
$field_name:ident: $field_ty:ty
),*
$(,)?
}
)?
),* $(,)?
) => {
impl Instruction {
/// Returns the result [`Reg`] for `self`.
///
/// Returns `None` if `self` does not statically return a single [`Reg`].
pub fn result(&self) -> Option<$crate::Reg> {
match *self {
$(
Self::$name { $( $( $result_name, )? )* .. } => {
IntoReg::into_reg((
$( $( $result_name )? )*
))
}
)*
}
}
}
};
}
for_each_op::for_each_op!(define_result);
impl Instruction {
/// Creates a new [`Instruction::ReturnReg2`] for the given [`Reg`] indices.
pub fn return_reg2_ext(reg0: impl Into<Reg>, reg1: impl Into<Reg>) -> Self {
Self::return_reg2([reg0.into(), reg1.into()])
}
/// Creates a new [`Instruction::ReturnReg3`] for the given [`Reg`] indices.
pub fn return_reg3_ext(
reg0: impl Into<Reg>,
reg1: impl Into<Reg>,
reg2: impl Into<Reg>,
) -> Self {
Self::return_reg3([reg0.into(), reg1.into(), reg2.into()])
}
/// Creates a new [`Instruction::ReturnMany`] for the given [`Reg`] indices.
pub fn return_many_ext(
reg0: impl Into<Reg>,
reg1: impl Into<Reg>,
reg2: impl Into<Reg>,
) -> Self {
Self::return_many([reg0.into(), reg1.into(), reg2.into()])
}
/// Creates a new [`Instruction::Copy2`].
pub fn copy2_ext(results: RegSpan, value0: impl Into<Reg>, value1: impl Into<Reg>) -> Self {
let span = FixedRegSpan::new(results).unwrap_or_else(|_| {
panic!("encountered invalid `results` `RegSpan` for `Copy2`: {results:?}")
});
Self::copy2(span, [value0.into(), value1.into()])
}
/// Creates a new [`Instruction::CopyMany`].
pub fn copy_many_ext(results: RegSpan, head0: impl Into<Reg>, head1: impl Into<Reg>) -> Self {
Self::copy_many(results, [head0.into(), head1.into()])
}
/// Creates a new [`Instruction::CopyManyNonOverlapping`].
pub fn copy_many_non_overlapping_ext(
results: RegSpan,
head0: impl Into<Reg>,
head1: impl Into<Reg>,
) -> Self {
Self::copy_many_non_overlapping(results, [head0.into(), head1.into()])
}
/// Creates a new [`Instruction::Register2`] instruction parameter.
pub fn register2_ext(reg0: impl Into<Reg>, reg1: impl Into<Reg>) -> Self {
Self::register2([reg0.into(), reg1.into()])
}
/// Creates a new [`Instruction::Register3`] instruction parameter.
pub fn register3_ext(reg0: impl Into<Reg>, reg1: impl Into<Reg>, reg2: impl Into<Reg>) -> Self {
Self::register3([reg0.into(), reg1.into(), reg2.into()])
}
/// Creates a new [`Instruction::RegisterList`] instruction parameter.
pub fn register_list_ext(
reg0: impl Into<Reg>,
reg1: impl Into<Reg>,
reg2: impl Into<Reg>,
) -> Self {
Self::register_list([reg0.into(), reg1.into(), reg2.into()])
}
/// Creates a new [`Instruction::RegisterAndImm32`] from the given `reg` and `offset_hi`.
pub fn register_and_offset_hi(reg: impl Into<Reg>, offset_hi: Offset64Hi) -> Self {
Self::register_and_imm32(reg, offset_hi.0)
}
/// Returns `Some` [`Reg`] and [`Offset64Hi`] if encoded properly.
///
/// # Errors
///
/// Returns back `self` if it was an incorrect [`Instruction`].
/// This allows for a better error message to inform the user.
pub fn filter_register_and_offset_hi(self) -> Result<(Reg, Offset64Hi), Self> {
if let Instruction::RegisterAndImm32 { reg, imm } = self {
return Ok((reg, Offset64Hi(u32::from(imm))));
}
Err(self)
}
/// Creates a new [`Instruction::RegisterAndImm32`] from the given `reg` and `offset_hi`.
pub fn register_and_lane<LaneType>(reg: impl Into<Reg>, lane: LaneType) -> Self
where
LaneType: Into<u8>,
{
Self::register_and_imm32(reg, u32::from(lane.into()))
}
/// Returns `Some` [`Reg`] and a `lane` index if encoded properly.
///
/// # Errors
///
/// Returns back `self` if it was an incorrect [`Instruction`].
/// This allows for a better error message to inform the user.
pub fn filter_register_and_lane<LaneType>(self) -> Result<(Reg, LaneType), Self>
where
LaneType: TryFrom<u8>,
{
if let Instruction::RegisterAndImm32 { reg, imm } = self {
let lane_index = u32::from(imm) as u8;
let Ok(lane) = LaneType::try_from(lane_index) else {
panic!("encountered out of bounds lane index: {}", lane_index)
};
return Ok((reg, lane));
}
Err(self)
}
/// Creates a new [`Instruction::Imm16AndImm32`] from the given `value` and `offset_hi`.
pub fn imm16_and_offset_hi(value: impl Into<AnyConst16>, offset_hi: Offset64Hi) -> Self {
Self::imm16_and_imm32(value, offset_hi.0)
}
/// Returns `Some` [`Reg`] and [`Offset64Hi`] if encoded properly.
///
/// # Errors
///
/// Returns back `self` if it was an incorrect [`Instruction`].
/// This allows for a better error message to inform the user.
pub fn filter_imm16_and_offset_hi<T>(self) -> Result<(T, Offset64Hi), Self>
where
T: From<AnyConst16>,
{
if let Instruction::Imm16AndImm32 { imm16, imm32 } = self {
return Ok((T::from(imm16), Offset64Hi(u32::from(imm32))));
}
Err(self)
}
/// Creates a new [`Instruction::Imm16AndImm32`] from the given `lane` and `memory` index.
pub fn lane_and_memory_index(value: impl Into<u8>, memory: Memory) -> Self {
Self::imm16_and_imm32(u16::from(value.into()), u32::from(memory))
}
/// Returns `Some` lane and [`index::Memory`] if encoded properly.
///
/// # Errors
///
/// Returns back `self` if it was an incorrect [`Instruction`].
/// This allows for a better error message to inform the user.
pub fn filter_lane_and_memory<LaneType>(self) -> Result<(LaneType, index::Memory), Self>
where
LaneType: TryFrom<u8>,
{
if let Instruction::Imm16AndImm32 { imm16, imm32 } = self {
let Ok(lane) = LaneType::try_from(i16::from(imm16) as u16 as u8) else {
return Err(self);
};
return Ok((lane, index::Memory::from(u32::from(imm32))));
}
Err(self)
}
}
#[test]
fn size_of() {
// Note: In case this test starts failing:
//
// There currently is a bug in the Rust compiler that causes
// Rust `enum` definitions with `#[repr(uN)]` to be incorrectly
// sized: https://github.com/rust-lang/rust/issues/53657
//
// Until that bug is fixed we need to order the `enum` variant
// fields in a precise order to end up with the correct `enum` size.
assert_eq!(::core::mem::size_of::<Instruction>(), 8);
assert_eq!(::core::mem::align_of::<Instruction>(), 4);
}