Debian Codegraph @

third_party/rust/wasm-smith/src/core/code_builder.rs

raw
1 use super::{
2 CompositeInnerType, Elements, FuncType, Instruction, InstructionKind::*, InstructionKinds,
3 Module, ValType,
4 };
5 use crate::{unique_string, MemoryOffsetChoices};
6 use arbitrary::{Result, Unstructured};
7 use std::collections::{BTreeMap, BTreeSet};
8 use std::rc::Rc;
9 use wasm_encoder::{
10 AbstractHeapType, ArrayType, BlockType, Catch, ConstExpr, ExportKind, FieldType, GlobalType,
11 HeapType, MemArg, RefType, StorageType, StructType,
12 };
13 mod no_traps;
14
15 macro_rules! instructions {
16 (
17 $(
18 ($predicate:expr, $generator_fn:ident, $instruction_kind:ident $(, $cost:tt)?),
19 )*
20 ) => {
21 static NUM_OPTIONS: usize = instructions!(
22 @count;
23 $( $generator_fn )*
24 );
25
26 fn choose_instruction(
27 u: &mut Unstructured<'_>,
28 module: &Module,
29 allowed_instructions: InstructionKinds,
30 builder: &mut CodeBuilder,
31 ) -> Option<
32 fn(&mut Unstructured<'_>, &Module, &mut CodeBuilder, &mut Vec<Instruction>) -> Result<()>
33 > {
34 builder.allocs.options.clear();
35 let mut cost = 0;
36 // Unroll the loop that checks whether each instruction is valid in
37 // the current context and, if it is valid, pushes it onto our
38 // options. Unrolling this loops lets us avoid dynamic calls through
39 // function pointers and, furthermore, each call site can be branch
40 // predicted and even inlined. This saved us about 30% of time in
41 // the `corpus` benchmark.
42 $(
43 let predicate: Option<fn(&Module, &mut CodeBuilder) -> bool> = $predicate;
44 if predicate.map_or(true, |f| f(module, builder))
45 && allowed_instructions.contains($instruction_kind) {
46 builder.allocs.options.push(($generator_fn, cost));
47 cost += 1000 $(- $cost)?;
48 }
49 )*
50
51 // If there aren't actually any candidate instructions due to
52 // various filters in place then return `None` to indicate the
53 // situation.
54 if cost == 0 {
55 return None;
56 }
57
58 let i = u.int_in_range(0..=cost).ok()?;
59 let idx = builder
60 .allocs
61 .options
62 .binary_search_by_key(&i,|p| p.1)
63 .unwrap_or_else(|i| i - 1);
64 Some(builder.allocs.options[idx].0)
65 }
66 };
67
68 ( @count; ) => {
69 0
70 };
71 ( @count; $x:ident $( $xs:ident )* ) => {
72 1 + instructions!( @count; $( $xs )* )
73 };
74 }
75
76 // The static set of options of instruction to generate that could be valid at
77 // some given time. One entry per Wasm instruction.
78 //
79 // Each entry is made up of up to three parts:
80 //
81 // 1. A predicate for whether this is a valid choice, if any. `None` means that
82 // the choice is always applicable.
83 //
84 // 2. The function to generate the instruction, given that we've made this
85 // choice.
86 //
87 // 3. The `InstructionKind` the instruction belongs to; this allows filtering
88 // out instructions by category.
89 //
90 // 4. An optional number used to weight how often this instruction is chosen.
91 // Higher numbers are less likely to be chosen, and number specified must be
92 // less than 1000.
93 instructions! {
94 // Control instructions.
95 (Some(unreachable_valid), unreachable, Control, 990),
96 (None, nop, Control, 800),
97 (None, block, Control),
98 (None, r#loop, Control),
99 (Some(try_table_valid), try_table, Control),
100 (Some(if_valid), r#if, Control),
101 (Some(else_valid), r#else, Control),
102 (Some(end_valid), end, Control),
103 (Some(br_valid), br, Control),
104 (Some(br_if_valid), br_if, Control),
105 (Some(br_table_valid), br_table, Control),
106 (Some(return_valid), r#return, Control, 900),
107 (Some(call_valid), call, Control),
108 (Some(call_ref_valid), call_ref, Control),
109 (Some(call_indirect_valid), call_indirect, Control),
110 (Some(return_call_valid), return_call, Control),
111 (Some(return_call_ref_valid), return_call_ref, Control),
112 (Some(return_call_indirect_valid), return_call_indirect, Control),
113 (Some(throw_valid), throw, Control, 850),
114 (Some(throw_ref_valid), throw_ref, Control, 850),
115 (Some(br_on_null_valid), br_on_null, Control),
116 (Some(br_on_non_null_valid), br_on_non_null, Control),
117 (Some(br_on_cast_valid), br_on_cast, Control),
118 (Some(br_on_cast_fail_valid), br_on_cast_fail, Control),
119 // Parametric instructions.
120 (Some(drop_valid), drop, Parametric, 990),
121 (Some(select_valid), select, Parametric),
122 // Variable instructions.
123 (Some(local_get_valid), local_get, Variable),
124 (Some(local_set_valid), local_set, Variable),
125 (Some(local_set_valid), local_tee, Variable),
126 (Some(global_get_valid), global_get, Variable),
127 (Some(global_set_valid), global_set, Variable),
128 // Memory instructions.
129 (Some(have_memory_and_offset), i32_load, MemoryInt),
130 (Some(have_memory_and_offset), i64_load, MemoryInt),
131 (Some(have_memory_and_offset), f32_load, Memory),
132 (Some(have_memory_and_offset), f64_load, Memory),
133 (Some(have_memory_and_offset), i32_load_8_s, MemoryInt),
134 (Some(have_memory_and_offset), i32_load_8_u, MemoryInt),
135 (Some(have_memory_and_offset), i32_load_16_s, MemoryInt),
136 (Some(have_memory_and_offset), i32_load_16_u, MemoryInt),
137 (Some(have_memory_and_offset), i64_load_8_s, MemoryInt),
138 (Some(have_memory_and_offset), i64_load_16_s, MemoryInt),
139 (Some(have_memory_and_offset), i64_load_32_s, MemoryInt),
140 (Some(have_memory_and_offset), i64_load_8_u, MemoryInt),
141 (Some(have_memory_and_offset), i64_load_16_u, MemoryInt),
142 (Some(have_memory_and_offset), i64_load_32_u, MemoryInt),
143 (Some(i32_store_valid), i32_store, MemoryInt),
144 (Some(i64_store_valid), i64_store, MemoryInt),
145 (Some(f32_store_valid), f32_store, Memory),
146 (Some(f64_store_valid), f64_store, Memory),
147 (Some(i32_store_valid), i32_store_8, MemoryInt),
148 (Some(i32_store_valid), i32_store_16, MemoryInt),
149 (Some(i64_store_valid), i64_store_8, MemoryInt),
150 (Some(i64_store_valid), i64_store_16, MemoryInt),
151 (Some(i64_store_valid), i64_store_32, MemoryInt),
152 (Some(have_memory), memory_size, MemoryInt),
153 (Some(memory_grow_valid), memory_grow, MemoryInt),
154 (Some(memory_init_valid), memory_init, MemoryInt),
155 (Some(data_drop_valid), data_drop, MemoryInt),
156 (Some(memory_copy_valid), memory_copy, MemoryInt),
157 (Some(memory_fill_valid), memory_fill, MemoryInt),
158 // Numeric instructions.
159 (None, i32_const, NumericInt),
160 (None, i64_const, NumericInt),
161 (None, f32_const, Numeric),
162 (None, f64_const, Numeric),
163 (Some(i32_on_stack), i32_eqz, NumericInt),
164 (Some(i32_i32_on_stack), i32_eq, NumericInt),
165 (Some(i32_i32_on_stack), i32_ne, NumericInt),
166 (Some(i32_i32_on_stack), i32_lt_s, NumericInt),
167 (Some(i32_i32_on_stack), i32_lt_u, NumericInt),
168 (Some(i32_i32_on_stack), i32_gt_s, NumericInt),
169 (Some(i32_i32_on_stack), i32_gt_u, NumericInt),
170 (Some(i32_i32_on_stack), i32_le_s, NumericInt),
171 (Some(i32_i32_on_stack), i32_le_u, NumericInt),
172 (Some(i32_i32_on_stack), i32_ge_s, NumericInt),
173 (Some(i32_i32_on_stack), i32_ge_u, NumericInt),
174 (Some(i64_on_stack), i64_eqz, NumericInt),
175 (Some(i64_i64_on_stack), i64_eq, NumericInt),
176 (Some(i64_i64_on_stack), i64_ne, NumericInt),
177 (Some(i64_i64_on_stack), i64_lt_s, NumericInt),
178 (Some(i64_i64_on_stack), i64_lt_u, NumericInt),
179 (Some(i64_i64_on_stack), i64_gt_s, NumericInt),
180 (Some(i64_i64_on_stack), i64_gt_u, NumericInt),
181 (Some(i64_i64_on_stack), i64_le_s, NumericInt),
182 (Some(i64_i64_on_stack), i64_le_u, NumericInt),
183 (Some(i64_i64_on_stack), i64_ge_s, NumericInt),
184 (Some(i64_i64_on_stack), i64_ge_u, NumericInt),
185 (Some(f32_f32_on_stack), f32_eq, Numeric),
186 (Some(f32_f32_on_stack), f32_ne, Numeric),
187 (Some(f32_f32_on_stack), f32_lt, Numeric),
188 (Some(f32_f32_on_stack), f32_gt, Numeric),
189 (Some(f32_f32_on_stack), f32_le, Numeric),
190 (Some(f32_f32_on_stack), f32_ge, Numeric),
191 (Some(f64_f64_on_stack), f64_eq, Numeric),
192 (Some(f64_f64_on_stack), f64_ne, Numeric),
193 (Some(f64_f64_on_stack), f64_lt, Numeric),
194 (Some(f64_f64_on_stack), f64_gt, Numeric),
195 (Some(f64_f64_on_stack), f64_le, Numeric),
196 (Some(f64_f64_on_stack), f64_ge, Numeric),
197 (Some(i32_on_stack), i32_clz, NumericInt),
198 (Some(i32_on_stack), i32_ctz, NumericInt),
199 (Some(i32_on_stack), i32_popcnt, NumericInt),
200 (Some(i32_i32_on_stack), i32_add, NumericInt),
201 (Some(i32_i32_on_stack), i32_sub, NumericInt),
202 (Some(i32_i32_on_stack), i32_mul, NumericInt),
203 (Some(i32_i32_on_stack), i32_div_s, NumericInt),
204 (Some(i32_i32_on_stack), i32_div_u, NumericInt),
205 (Some(i32_i32_on_stack), i32_rem_s, NumericInt),
206 (Some(i32_i32_on_stack), i32_rem_u, NumericInt),
207 (Some(i32_i32_on_stack), i32_and, NumericInt),
208 (Some(i32_i32_on_stack), i32_or, NumericInt),
209 (Some(i32_i32_on_stack), i32_xor, NumericInt),
210 (Some(i32_i32_on_stack), i32_shl, NumericInt),
211 (Some(i32_i32_on_stack), i32_shr_s, NumericInt),
212 (Some(i32_i32_on_stack), i32_shr_u, NumericInt),
213 (Some(i32_i32_on_stack), i32_rotl, NumericInt),
214 (Some(i32_i32_on_stack), i32_rotr, NumericInt),
215 (Some(i64_on_stack), i64_clz, NumericInt),
216 (Some(i64_on_stack), i64_ctz, NumericInt),
217 (Some(i64_on_stack), i64_popcnt, NumericInt),
218 (Some(i64_i64_on_stack), i64_add, NumericInt),
219 (Some(i64_i64_on_stack), i64_sub, NumericInt),
220 (Some(i64_i64_on_stack), i64_mul, NumericInt),
221 (Some(i64_i64_on_stack), i64_div_s, NumericInt),
222 (Some(i64_i64_on_stack), i64_div_u, NumericInt),
223 (Some(i64_i64_on_stack), i64_rem_s, NumericInt),
224 (Some(i64_i64_on_stack), i64_rem_u, NumericInt),
225 (Some(i64_i64_on_stack), i64_and, NumericInt),
226 (Some(i64_i64_on_stack), i64_or, NumericInt),
227 (Some(i64_i64_on_stack), i64_xor, NumericInt),
228 (Some(i64_i64_on_stack), i64_shl, NumericInt),
229 (Some(i64_i64_on_stack), i64_shr_s, NumericInt),
230 (Some(i64_i64_on_stack), i64_shr_u, NumericInt),
231 (Some(i64_i64_on_stack), i64_rotl, NumericInt),
232 (Some(i64_i64_on_stack), i64_rotr, NumericInt),
233 (Some(f32_on_stack), f32_abs, Numeric),
234 (Some(f32_on_stack), f32_neg, Numeric),
235 (Some(f32_on_stack), f32_ceil, Numeric),
236 (Some(f32_on_stack), f32_floor, Numeric),
237 (Some(f32_on_stack), f32_trunc, Numeric),
238 (Some(f32_on_stack), f32_nearest, Numeric),
239 (Some(f32_on_stack), f32_sqrt, Numeric),
240 (Some(f32_f32_on_stack), f32_add, Numeric),
241 (Some(f32_f32_on_stack), f32_sub, Numeric),
242 (Some(f32_f32_on_stack), f32_mul, Numeric),
243 (Some(f32_f32_on_stack), f32_div, Numeric),
244 (Some(f32_f32_on_stack), f32_min, Numeric),
245 (Some(f32_f32_on_stack), f32_max, Numeric),
246 (Some(f32_f32_on_stack), f32_copysign, Numeric),
247 (Some(f64_on_stack), f64_abs, Numeric),
248 (Some(f64_on_stack), f64_neg, Numeric),
249 (Some(f64_on_stack), f64_ceil, Numeric),
250 (Some(f64_on_stack), f64_floor, Numeric),
251 (Some(f64_on_stack), f64_trunc, Numeric),
252 (Some(f64_on_stack), f64_nearest, Numeric),
253 (Some(f64_on_stack), f64_sqrt, Numeric),
254 (Some(f64_f64_on_stack), f64_add, Numeric),
255 (Some(f64_f64_on_stack), f64_sub, Numeric),
256 (Some(f64_f64_on_stack), f64_mul, Numeric),
257 (Some(f64_f64_on_stack), f64_div, Numeric),
258 (Some(f64_f64_on_stack), f64_min, Numeric),
259 (Some(f64_f64_on_stack), f64_max, Numeric),
260 (Some(f64_f64_on_stack), f64_copysign, Numeric),
261 (Some(i64_on_stack), i32_wrap_i64, NumericInt),
262 (Some(f32_on_stack), i32_trunc_f32_s, Numeric),
263 (Some(f32_on_stack), i32_trunc_f32_u, Numeric),
264 (Some(f64_on_stack), i32_trunc_f64_s, Numeric),
265 (Some(f64_on_stack), i32_trunc_f64_u, Numeric),
266 (Some(i32_on_stack), i64_extend_i32_s, NumericInt),
267 (Some(i32_on_stack), i64_extend_i32_u, NumericInt),
268 (Some(f32_on_stack), i64_trunc_f32_s, Numeric),
269 (Some(f32_on_stack), i64_trunc_f32_u, Numeric),
270 (Some(f64_on_stack), i64_trunc_f64_s, Numeric),
271 (Some(f64_on_stack), i64_trunc_f64_u, Numeric),
272 (Some(i32_on_stack), f32_convert_i32_s, Numeric),
273 (Some(i32_on_stack), f32_convert_i32_u, Numeric),
274 (Some(i64_on_stack), f32_convert_i64_s, Numeric),
275 (Some(i64_on_stack), f32_convert_i64_u, Numeric),
276 (Some(f64_on_stack), f32_demote_f64, Numeric),
277 (Some(i32_on_stack), f64_convert_i32_s, Numeric),
278 (Some(i32_on_stack), f64_convert_i32_u, Numeric),
279 (Some(i64_on_stack), f64_convert_i64_s, Numeric),
280 (Some(i64_on_stack), f64_convert_i64_u, Numeric),
281 (Some(f32_on_stack), f64_promote_f32, Numeric),
282 (Some(f32_on_stack), i32_reinterpret_f32, Numeric),
283 (Some(f64_on_stack), i64_reinterpret_f64, Numeric),
284 (Some(i32_on_stack), f32_reinterpret_i32, Numeric),
285 (Some(i64_on_stack), f64_reinterpret_i64, Numeric),
286 (Some(extendable_i32_on_stack), i32_extend_8_s, NumericInt),
287 (Some(extendable_i32_on_stack), i32_extend_16_s, NumericInt),
288 (Some(extendable_i64_on_stack), i64_extend_8_s, NumericInt),
289 (Some(extendable_i64_on_stack), i64_extend_16_s, NumericInt),
290 (Some(extendable_i64_on_stack), i64_extend_32_s, NumericInt),
291 (Some(nontrapping_f32_on_stack), i32_trunc_sat_f32_s, Numeric),
292 (Some(nontrapping_f32_on_stack), i32_trunc_sat_f32_u, Numeric),
293 (Some(nontrapping_f64_on_stack), i32_trunc_sat_f64_s, Numeric),
294 (Some(nontrapping_f64_on_stack), i32_trunc_sat_f64_u, Numeric),
295 (Some(nontrapping_f32_on_stack), i64_trunc_sat_f32_s, Numeric),
296 (Some(nontrapping_f32_on_stack), i64_trunc_sat_f32_u, Numeric),
297 (Some(nontrapping_f64_on_stack), i64_trunc_sat_f64_s, Numeric),
298 (Some(nontrapping_f64_on_stack), i64_trunc_sat_f64_u, Numeric),
299 // Reference instructions.
300 (Some(ref_null_valid), ref_null, Reference),
301 (Some(ref_func_valid), ref_func, Reference),
302 (Some(ref_as_non_null_valid), ref_as_non_null, Reference),
303 (Some(ref_eq_valid), ref_eq, Reference),
304 (Some(ref_test_valid), ref_test, Reference),
305 (Some(ref_cast_valid), ref_cast, Reference),
306 (Some(ref_is_null_valid), ref_is_null, Reference),
307 (Some(table_fill_valid), table_fill, Reference),
308 (Some(table_set_valid), table_set, Reference),
309 (Some(table_get_valid), table_get, Reference),
310 (Some(table_size_valid), table_size, Reference),
311 (Some(table_grow_valid), table_grow, Reference),
312 (Some(table_copy_valid), table_copy, Reference),
313 (Some(table_init_valid), table_init, Reference),
314 (Some(elem_drop_valid), elem_drop, Reference),
315 // Aggregate instructions.
316 (Some(struct_new_valid), struct_new, Aggregate),
317 (Some(struct_new_default_valid), struct_new_default, Aggregate),
318 (Some(struct_get_valid), struct_get, Aggregate),
319 (Some(struct_set_valid), struct_set, Aggregate),
320 (Some(array_new_valid), array_new, Aggregate),
321 (Some(array_new_fixed_valid), array_new_fixed, Aggregate),
322 (Some(array_new_default_valid), array_new_default, Aggregate),
323 (Some(array_new_data_valid), array_new_data, Aggregate),
324 (Some(array_new_elem_valid), array_new_elem, Aggregate),
325 (Some(array_get_valid), array_get, Aggregate),
326 (Some(array_set_valid), array_set, Aggregate),
327 (Some(array_len_valid), array_len, Aggregate),
328 (Some(array_fill_valid), array_fill, Aggregate),
329 (Some(array_copy_valid), array_copy, Aggregate),
330 (Some(array_init_data_valid), array_init_data, Aggregate),
331 (Some(array_init_elem_valid), array_init_elem, Aggregate),
332 (Some(ref_i31_valid), ref_i31, Aggregate),
333 (Some(i31_get_valid), i31_get, Aggregate),
334 (Some(any_convert_extern_valid), any_convert_extern, Aggregate),
335 (Some(extern_convert_any_valid), extern_convert_any, Aggregate),
336 // SIMD instructions.
337 (Some(simd_have_memory_and_offset), v128_load, VectorInt),
338 (Some(simd_have_memory_and_offset), v128_load8x8s, VectorInt),
339 (Some(simd_have_memory_and_offset), v128_load8x8u, VectorInt),
340 (Some(simd_have_memory_and_offset), v128_load16x4s, VectorInt),
341 (Some(simd_have_memory_and_offset), v128_load16x4u, VectorInt),
342 (Some(simd_have_memory_and_offset), v128_load32x2s, VectorInt),
343 (Some(simd_have_memory_and_offset), v128_load32x2u, VectorInt),
344 (Some(simd_have_memory_and_offset), v128_load8_splat, VectorInt),
345 (Some(simd_have_memory_and_offset), v128_load16_splat, VectorInt),
346 (Some(simd_have_memory_and_offset), v128_load32_splat, VectorInt),
347 (Some(simd_have_memory_and_offset), v128_load64_splat, VectorInt),
348 (Some(simd_have_memory_and_offset), v128_load32_zero, VectorInt),
349 (Some(simd_have_memory_and_offset), v128_load64_zero, VectorInt),
350 (Some(simd_v128_store_valid), v128_store, VectorInt),
351 (Some(simd_load_lane_valid), v128_load8_lane, VectorInt),
352 (Some(simd_load_lane_valid), v128_load16_lane, VectorInt),
353 (Some(simd_load_lane_valid), v128_load32_lane, VectorInt),
354 (Some(simd_load_lane_valid), v128_load64_lane, VectorInt),
355 (Some(simd_store_lane_valid), v128_store8_lane, VectorInt),
356 (Some(simd_store_lane_valid), v128_store16_lane, VectorInt),
357 (Some(simd_store_lane_valid), v128_store32_lane, VectorInt),
358 (Some(simd_store_lane_valid), v128_store64_lane, VectorInt),
359 (Some(simd_enabled), v128_const, VectorInt),
360 (Some(simd_v128_v128_on_stack), i8x16_shuffle, VectorInt),
361 (Some(simd_v128_on_stack), i8x16_extract_lane_s, VectorInt),
362 (Some(simd_v128_on_stack), i8x16_extract_lane_u, VectorInt),
363 (Some(simd_v128_i32_on_stack), i8x16_replace_lane, VectorInt),
364 (Some(simd_v128_on_stack), i16x8_extract_lane_s, VectorInt),
365 (Some(simd_v128_on_stack), i16x8_extract_lane_u, VectorInt),
366 (Some(simd_v128_i32_on_stack), i16x8_replace_lane, VectorInt),
367 (Some(simd_v128_on_stack), i32x4_extract_lane, VectorInt),
368 (Some(simd_v128_i32_on_stack), i32x4_replace_lane, VectorInt),
369 (Some(simd_v128_on_stack), i64x2_extract_lane, VectorInt),
370 (Some(simd_v128_i64_on_stack), i64x2_replace_lane, VectorInt),
371 (Some(simd_v128_on_stack), f32x4_extract_lane, Vector),
372 (Some(simd_v128_f32_on_stack), f32x4_replace_lane, Vector),
373 (Some(simd_v128_on_stack), f64x2_extract_lane, Vector),
374 (Some(simd_v128_f64_on_stack), f64x2_replace_lane, Vector),
375 (Some(simd_i32_on_stack), i8x16_splat, VectorInt),
376 (Some(simd_i32_on_stack), i16x8_splat, VectorInt),
377 (Some(simd_i32_on_stack), i32x4_splat, VectorInt),
378 (Some(simd_i64_on_stack), i64x2_splat, VectorInt),
379 (Some(simd_f32_on_stack), f32x4_splat, Vector),
380 (Some(simd_f64_on_stack), f64x2_splat, Vector),
381 (Some(simd_v128_v128_on_stack), i8x16_swizzle, VectorInt),
382 (Some(simd_v128_v128_on_stack_relaxed), i8x16_relaxed_swizzle, VectorInt),
383 (Some(simd_v128_v128_v128_on_stack), v128_bitselect, VectorInt),
384 (Some(simd_v128_v128_v128_on_stack_relaxed), i8x16_relaxed_laneselect, VectorInt),
385 (Some(simd_v128_v128_v128_on_stack_relaxed), i16x8_relaxed_laneselect, VectorInt),
386 (Some(simd_v128_v128_v128_on_stack_relaxed), i32x4_relaxed_laneselect, VectorInt),
387 (Some(simd_v128_v128_v128_on_stack_relaxed), i64x2_relaxed_laneselect, VectorInt),
388 (Some(simd_v128_v128_on_stack), i8x16_eq, VectorInt),
389 (Some(simd_v128_v128_on_stack), i8x16_ne, VectorInt),
390 (Some(simd_v128_v128_on_stack), i8x16_lt_s, VectorInt),
391 (Some(simd_v128_v128_on_stack), i8x16_lt_u, VectorInt),
392 (Some(simd_v128_v128_on_stack), i8x16_gt_s, VectorInt),
393 (Some(simd_v128_v128_on_stack), i8x16_gt_u, VectorInt),
394 (Some(simd_v128_v128_on_stack), i8x16_le_s, VectorInt),
395 (Some(simd_v128_v128_on_stack), i8x16_le_u, VectorInt),
396 (Some(simd_v128_v128_on_stack), i8x16_ge_s, VectorInt),
397 (Some(simd_v128_v128_on_stack), i8x16_ge_u, VectorInt),
398 (Some(simd_v128_v128_on_stack), i16x8_eq, VectorInt),
399 (Some(simd_v128_v128_on_stack), i16x8_ne, VectorInt),
400 (Some(simd_v128_v128_on_stack), i16x8_lt_s, VectorInt),
401 (Some(simd_v128_v128_on_stack), i16x8_lt_u, VectorInt),
402 (Some(simd_v128_v128_on_stack), i16x8_gt_s, VectorInt),
403 (Some(simd_v128_v128_on_stack), i16x8_gt_u, VectorInt),
404 (Some(simd_v128_v128_on_stack), i16x8_le_s, VectorInt),
405 (Some(simd_v128_v128_on_stack), i16x8_le_u, VectorInt),
406 (Some(simd_v128_v128_on_stack), i16x8_ge_s, VectorInt),
407 (Some(simd_v128_v128_on_stack), i16x8_ge_u, VectorInt),
408 (Some(simd_v128_v128_on_stack), i32x4_eq, VectorInt),
409 (Some(simd_v128_v128_on_stack), i32x4_ne, VectorInt),
410 (Some(simd_v128_v128_on_stack), i32x4_lt_s, VectorInt),
411 (Some(simd_v128_v128_on_stack), i32x4_lt_u, VectorInt),
412 (Some(simd_v128_v128_on_stack), i32x4_gt_s, VectorInt),
413 (Some(simd_v128_v128_on_stack), i32x4_gt_u, VectorInt),
414 (Some(simd_v128_v128_on_stack), i32x4_le_s, VectorInt),
415 (Some(simd_v128_v128_on_stack), i32x4_le_u, VectorInt),
416 (Some(simd_v128_v128_on_stack), i32x4_ge_s, VectorInt),
417 (Some(simd_v128_v128_on_stack), i32x4_ge_u, VectorInt),
418 (Some(simd_v128_v128_on_stack), i64x2_eq, VectorInt),
419 (Some(simd_v128_v128_on_stack), i64x2_ne, VectorInt),
420 (Some(simd_v128_v128_on_stack), i64x2_lt_s, VectorInt),
421 (Some(simd_v128_v128_on_stack), i64x2_gt_s, VectorInt),
422 (Some(simd_v128_v128_on_stack), i64x2_le_s, VectorInt),
423 (Some(simd_v128_v128_on_stack), i64x2_ge_s, VectorInt),
424 (Some(simd_v128_v128_on_stack), f32x4_eq, Vector),
425 (Some(simd_v128_v128_on_stack), f32x4_ne, Vector),
426 (Some(simd_v128_v128_on_stack), f32x4_lt, Vector),
427 (Some(simd_v128_v128_on_stack), f32x4_gt, Vector),
428 (Some(simd_v128_v128_on_stack), f32x4_le, Vector),
429 (Some(simd_v128_v128_on_stack), f32x4_ge, Vector),
430 (Some(simd_v128_v128_on_stack), f64x2_eq, Vector),
431 (Some(simd_v128_v128_on_stack), f64x2_ne, Vector),
432 (Some(simd_v128_v128_on_stack), f64x2_lt, Vector),
433 (Some(simd_v128_v128_on_stack), f64x2_gt, Vector),
434 (Some(simd_v128_v128_on_stack), f64x2_le, Vector),
435 (Some(simd_v128_v128_on_stack), f64x2_ge, Vector),
436 (Some(simd_v128_on_stack), v128_not, VectorInt),
437 (Some(simd_v128_v128_on_stack), v128_and, VectorInt),
438 (Some(simd_v128_v128_on_stack), v128_and_not, VectorInt),
439 (Some(simd_v128_v128_on_stack), v128_or, VectorInt),
440 (Some(simd_v128_v128_on_stack), v128_xor, VectorInt),
441 (Some(simd_v128_v128_on_stack), v128_any_true, VectorInt),
442 (Some(simd_v128_on_stack), i8x16_abs, VectorInt),
443 (Some(simd_v128_on_stack), i8x16_neg, VectorInt),
444 (Some(simd_v128_on_stack), i8x16_popcnt, VectorInt),
445 (Some(simd_v128_on_stack), i8x16_all_true, VectorInt),
446 (Some(simd_v128_on_stack), i8x16_bitmask, VectorInt),
447 (Some(simd_v128_v128_on_stack), i8x16_narrow_i16x8s, VectorInt),
448 (Some(simd_v128_v128_on_stack), i8x16_narrow_i16x8u, VectorInt),
449 (Some(simd_v128_i32_on_stack), i8x16_shl, VectorInt),
450 (Some(simd_v128_i32_on_stack), i8x16_shr_s, VectorInt),
451 (Some(simd_v128_i32_on_stack), i8x16_shr_u, VectorInt),
452 (Some(simd_v128_v128_on_stack), i8x16_add, VectorInt),
453 (Some(simd_v128_v128_on_stack), i8x16_add_sat_s, VectorInt),
454 (Some(simd_v128_v128_on_stack), i8x16_add_sat_u, VectorInt),
455 (Some(simd_v128_v128_on_stack), i8x16_sub, VectorInt),
456 (Some(simd_v128_v128_on_stack), i8x16_sub_sat_s, VectorInt),
457 (Some(simd_v128_v128_on_stack), i8x16_sub_sat_u, VectorInt),
458 (Some(simd_v128_v128_on_stack), i8x16_min_s, VectorInt),
459 (Some(simd_v128_v128_on_stack), i8x16_min_u, VectorInt),
460 (Some(simd_v128_v128_on_stack), i8x16_max_s, VectorInt),
461 (Some(simd_v128_v128_on_stack), i8x16_max_u, VectorInt),
462 (Some(simd_v128_v128_on_stack), i8x16_avgr_u, VectorInt),
463 (Some(simd_v128_on_stack), i16x8_extadd_pairwise_i8x16s, VectorInt),
464 (Some(simd_v128_on_stack), i16x8_extadd_pairwise_i8x16u, VectorInt),
465 (Some(simd_v128_on_stack), i16x8_abs, VectorInt),
466 (Some(simd_v128_on_stack), i16x8_neg, VectorInt),
467 (Some(simd_v128_v128_on_stack), i16x8q15_mulr_sat_s, VectorInt),
468 (Some(simd_v128_on_stack), i16x8_all_true, VectorInt),
469 (Some(simd_v128_on_stack), i16x8_bitmask, VectorInt),
470 (Some(simd_v128_v128_on_stack), i16x8_narrow_i32x4s, VectorInt),
471 (Some(simd_v128_v128_on_stack), i16x8_narrow_i32x4u, VectorInt),
472 (Some(simd_v128_on_stack), i16x8_extend_low_i8x16s, VectorInt),
473 (Some(simd_v128_on_stack), i16x8_extend_high_i8x16s, VectorInt),
474 (Some(simd_v128_on_stack), i16x8_extend_low_i8x16u, VectorInt),
475 (Some(simd_v128_on_stack), i16x8_extend_high_i8x16u, VectorInt),
476 (Some(simd_v128_i32_on_stack), i16x8_shl, VectorInt),
477 (Some(simd_v128_i32_on_stack), i16x8_shr_s, VectorInt),
478 (Some(simd_v128_i32_on_stack), i16x8_shr_u, VectorInt),
479 (Some(simd_v128_v128_on_stack), i16x8_add, VectorInt),
480 (Some(simd_v128_v128_on_stack), i16x8_add_sat_s, VectorInt),
481 (Some(simd_v128_v128_on_stack), i16x8_add_sat_u, VectorInt),
482 (Some(simd_v128_v128_on_stack), i16x8_sub, VectorInt),
483 (Some(simd_v128_v128_on_stack), i16x8_sub_sat_s, VectorInt),
484 (Some(simd_v128_v128_on_stack), i16x8_sub_sat_u, VectorInt),
485 (Some(simd_v128_v128_on_stack), i16x8_mul, VectorInt),
486 (Some(simd_v128_v128_on_stack), i16x8_min_s, VectorInt),
487 (Some(simd_v128_v128_on_stack), i16x8_min_u, VectorInt),
488 (Some(simd_v128_v128_on_stack), i16x8_max_s, VectorInt),
489 (Some(simd_v128_v128_on_stack), i16x8_max_u, VectorInt),
490 (Some(simd_v128_v128_on_stack), i16x8_avgr_u, VectorInt),
491 (Some(simd_v128_v128_on_stack), i16x8_extmul_low_i8x16s, VectorInt),
492 (Some(simd_v128_v128_on_stack), i16x8_extmul_high_i8x16s, VectorInt),
493 (Some(simd_v128_v128_on_stack), i16x8_extmul_low_i8x16u, VectorInt),
494 (Some(simd_v128_v128_on_stack), i16x8_extmul_high_i8x16u, VectorInt),
495 (Some(simd_v128_on_stack), i32x4_extadd_pairwise_i16x8s, VectorInt),
496 (Some(simd_v128_on_stack), i32x4_extadd_pairwise_i16x8u, VectorInt),
497 (Some(simd_v128_on_stack), i32x4_abs, VectorInt),
498 (Some(simd_v128_on_stack), i32x4_neg, VectorInt),
499 (Some(simd_v128_on_stack), i32x4_all_true, VectorInt),
500 (Some(simd_v128_on_stack), i32x4_bitmask, VectorInt),
501 (Some(simd_v128_on_stack), i32x4_extend_low_i16x8s, VectorInt),
502 (Some(simd_v128_on_stack), i32x4_extend_high_i16x8s, VectorInt),
503 (Some(simd_v128_on_stack), i32x4_extend_low_i16x8u, VectorInt),
504 (Some(simd_v128_on_stack), i32x4_extend_high_i16x8u, VectorInt),
505 (Some(simd_v128_i32_on_stack), i32x4_shl, VectorInt),
506 (Some(simd_v128_i32_on_stack), i32x4_shr_s, VectorInt),
507 (Some(simd_v128_i32_on_stack), i32x4_shr_u, VectorInt),
508 (Some(simd_v128_v128_on_stack), i32x4_add, VectorInt),
509 (Some(simd_v128_v128_on_stack), i32x4_sub, VectorInt),
510 (Some(simd_v128_v128_on_stack), i32x4_mul, VectorInt),
511 (Some(simd_v128_v128_on_stack), i32x4_min_s, VectorInt),
512 (Some(simd_v128_v128_on_stack), i32x4_min_u, VectorInt),
513 (Some(simd_v128_v128_on_stack), i32x4_max_s, VectorInt),
514 (Some(simd_v128_v128_on_stack), i32x4_max_u, VectorInt),
515 (Some(simd_v128_v128_on_stack), i32x4_dot_i16x8s, VectorInt),
516 (Some(simd_v128_v128_on_stack), i32x4_extmul_low_i16x8s, VectorInt),
517 (Some(simd_v128_v128_on_stack), i32x4_extmul_high_i16x8s, VectorInt),
518 (Some(simd_v128_v128_on_stack), i32x4_extmul_low_i16x8u, VectorInt),
519 (Some(simd_v128_v128_on_stack), i32x4_extmul_high_i16x8u, VectorInt),
520 (Some(simd_v128_on_stack), i64x2_abs, VectorInt),
521 (Some(simd_v128_on_stack), i64x2_neg, VectorInt),
522 (Some(simd_v128_on_stack), i64x2_all_true, VectorInt),
523 (Some(simd_v128_on_stack), i64x2_bitmask, VectorInt),
524 (Some(simd_v128_on_stack), i64x2_extend_low_i32x4s, VectorInt),
525 (Some(simd_v128_on_stack), i64x2_extend_high_i32x4s, VectorInt),
526 (Some(simd_v128_on_stack), i64x2_extend_low_i32x4u, VectorInt),
527 (Some(simd_v128_on_stack), i64x2_extend_high_i32x4u, VectorInt),
528 (Some(simd_v128_i32_on_stack), i64x2_shl, VectorInt),
529 (Some(simd_v128_i32_on_stack), i64x2_shr_s, VectorInt),
530 (Some(simd_v128_i32_on_stack), i64x2_shr_u, VectorInt),
531 (Some(simd_v128_v128_on_stack), i64x2_add, VectorInt),
532 (Some(simd_v128_v128_on_stack), i64x2_sub, VectorInt),
533 (Some(simd_v128_v128_on_stack), i64x2_mul, VectorInt),
534 (Some(simd_v128_v128_on_stack), i64x2_extmul_low_i32x4s, VectorInt),
535 (Some(simd_v128_v128_on_stack), i64x2_extmul_high_i32x4s, VectorInt),
536 (Some(simd_v128_v128_on_stack), i64x2_extmul_low_i32x4u, VectorInt),
537 (Some(simd_v128_v128_on_stack), i64x2_extmul_high_i32x4u, VectorInt),
538 (Some(simd_v128_on_stack), f32x4_ceil, Vector),
539 (Some(simd_v128_on_stack), f32x4_floor, Vector),
540 (Some(simd_v128_on_stack), f32x4_trunc, Vector),
541 (Some(simd_v128_on_stack), f32x4_nearest, Vector),
542 (Some(simd_v128_on_stack), f32x4_abs, Vector),
543 (Some(simd_v128_on_stack), f32x4_neg, Vector),
544 (Some(simd_v128_on_stack), f32x4_sqrt, Vector),
545 (Some(simd_v128_v128_on_stack), f32x4_add, Vector),
546 (Some(simd_v128_v128_on_stack), f32x4_sub, Vector),
547 (Some(simd_v128_v128_on_stack), f32x4_mul, Vector),
548 (Some(simd_v128_v128_on_stack), f32x4_div, Vector),
549 (Some(simd_v128_v128_on_stack), f32x4_min, Vector),
550 (Some(simd_v128_v128_on_stack), f32x4_max, Vector),
551 (Some(simd_v128_v128_on_stack), f32x4p_min, Vector),
552 (Some(simd_v128_v128_on_stack), f32x4p_max, Vector),
553 (Some(simd_v128_on_stack), f64x2_ceil, Vector),
554 (Some(simd_v128_on_stack), f64x2_floor, Vector),
555 (Some(simd_v128_on_stack), f64x2_trunc, Vector),
556 (Some(simd_v128_on_stack), f64x2_nearest, Vector),
557 (Some(simd_v128_on_stack), f64x2_abs, Vector),
558 (Some(simd_v128_on_stack), f64x2_neg, Vector),
559 (Some(simd_v128_on_stack), f64x2_sqrt, Vector),
560 (Some(simd_v128_v128_on_stack), f64x2_add, Vector),
561 (Some(simd_v128_v128_on_stack), f64x2_sub, Vector),
562 (Some(simd_v128_v128_on_stack), f64x2_mul, Vector),
563 (Some(simd_v128_v128_on_stack), f64x2_div, Vector),
564 (Some(simd_v128_v128_on_stack), f64x2_min, Vector),
565 (Some(simd_v128_v128_on_stack), f64x2_max, Vector),
566 (Some(simd_v128_v128_on_stack), f64x2p_min, Vector),
567 (Some(simd_v128_v128_on_stack), f64x2p_max, Vector),
568 (Some(simd_v128_on_stack), i32x4_trunc_sat_f32x4s, Vector),
569 (Some(simd_v128_on_stack), i32x4_trunc_sat_f32x4u, Vector),
570 (Some(simd_v128_on_stack), f32x4_convert_i32x4s, Vector),
571 (Some(simd_v128_on_stack), f32x4_convert_i32x4u, Vector),
572 (Some(simd_v128_on_stack), i32x4_trunc_sat_f64x2s_zero, Vector),
573 (Some(simd_v128_on_stack), i32x4_trunc_sat_f64x2u_zero, Vector),
574 (Some(simd_v128_on_stack), f64x2_convert_low_i32x4s, Vector),
575 (Some(simd_v128_on_stack), f64x2_convert_low_i32x4u, Vector),
576 (Some(simd_v128_on_stack), f32x4_demote_f64x2_zero, Vector),
577 (Some(simd_v128_on_stack), f64x2_promote_low_f32x4, Vector),
578 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f32x4s, Vector),
579 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f32x4u, Vector),
580 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f64x2s_zero, Vector),
581 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f64x2u_zero, Vector),
582 (Some(simd_v128_v128_v128_on_stack_relaxed), f32x4_relaxed_madd, Vector),
583 (Some(simd_v128_v128_v128_on_stack_relaxed), f32x4_relaxed_nmadd, Vector),
584 (Some(simd_v128_v128_v128_on_stack_relaxed), f64x2_relaxed_madd, Vector),
585 (Some(simd_v128_v128_v128_on_stack_relaxed), f64x2_relaxed_nmadd, Vector),
586 (Some(simd_v128_v128_on_stack_relaxed), f32x4_relaxed_min, Vector),
587 (Some(simd_v128_v128_on_stack_relaxed), f32x4_relaxed_max, Vector),
588 (Some(simd_v128_v128_on_stack_relaxed), f64x2_relaxed_min, Vector),
589 (Some(simd_v128_v128_on_stack_relaxed), f64x2_relaxed_max, Vector),
590 (Some(simd_v128_v128_on_stack_relaxed), i16x8_relaxed_q15mulr_s, VectorInt),
591 (Some(simd_v128_v128_on_stack_relaxed), i16x8_relaxed_dot_i8x16_i7x16_s, VectorInt),
592 (Some(simd_v128_v128_v128_on_stack_relaxed), i32x4_relaxed_dot_i8x16_i7x16_add_s, VectorInt),
593 (Some(wide_arithmetic_binop128_on_stack), i64_add128, NumericInt),
594 (Some(wide_arithmetic_binop128_on_stack), i64_sub128, NumericInt),
595 (Some(wide_arithmetic_mul_wide_on_stack), i64_mul_wide_s, NumericInt),
596 (Some(wide_arithmetic_mul_wide_on_stack), i64_mul_wide_u, NumericInt),
597 }
598
599 pub(crate) struct CodeBuilderAllocations {
600 // The control labels in scope right now.
601 controls: Vec<Control>,
602
603 // The types on the operand stack right now.
604 operands: Vec<Option<ValType>>,
605
606 // Dynamic set of options of instruction we can generate that are known to
607 // be valid right now.
608 options: Vec<(
609 fn(&mut Unstructured, &Module, &mut CodeBuilder, &mut Vec<Instruction>) -> Result<()>,
610 u32,
611 )>,
612
613 // Cached information about the module that we're generating functions for,
614 // used to speed up validity checks. The mutable globals map is a map of the
615 // type of global to the global indices which have that type (and they're
616 // all mutable).
617 mutable_globals: BTreeMap<ValType, Vec<u32>>,
618
619 // Like mutable globals above this is a map from function types to the list
620 // of functions that have that function type.
621 functions: BTreeMap<Rc<FuncType>, Vec<u32>>,
622
623 // Like functions above this is a map from tag types to the list of tags
624 // have that tag type.
625 tags: BTreeMap<Vec<ValType>, Vec<u32>>,
626
627 // Tables in this module which have a funcref element type.
628 table32_with_funcref: Vec<u32>,
629 table64_with_funcref: Vec<u32>,
630
631 // Functions that are referenced in the module through globals and segments.
632 referenced_functions: Vec<u32>,
633
634 // Precomputed tables/element segments that can be used for `table.init`,
635 // stored as (segment, table).
636 table32_init: Vec<(u32, u32)>,
637 table64_init: Vec<(u32, u32)>,
638
639 // Precomputed valid tables to copy between, stored in (src, dst) order.
640 table_copy_32_to_32: Vec<(u32, u32)>,
641 table_copy_32_to_64: Vec<(u32, u32)>,
642 table_copy_64_to_32: Vec<(u32, u32)>,
643 table_copy_64_to_64: Vec<(u32, u32)>,
644
645 // Lists of table/memory indices which are either 32-bit or 64-bit. This is
646 // used for faster lookup in validating instructions to know which memories
647 // have which types. For example if there are no 64-bit memories then we
648 // shouldn't ever look for i64 on the stack for `i32.load`.
649 memory32: Vec<u32>,
650 memory64: Vec<u32>,
651 table32: Vec<u32>,
652 table64: Vec<u32>,
653
654 // State used when dropping operands to avoid dropping them into the ether
655 // but instead folding their final values into module state, at this time
656 // chosen to be exported globals.
657 globals_cnt: u32,
658 new_globals: Vec<(ValType, ConstExpr)>,
659 global_dropped_i32: Option<u32>,
660 global_dropped_i64: Option<u32>,
661 global_dropped_f32: Option<u32>,
662 global_dropped_f64: Option<u32>,
663 global_dropped_v128: Option<u32>,
664
665 // Indicates that additional exports cannot be generated. This will be true
666 // if the `Config` specifies exactly which exports should be present.
667 disallow_exporting: bool,
668 }
669
670 pub(crate) struct CodeBuilder<'a> {
671 func_ty: &'a FuncType,
672 locals: &'a mut Vec<ValType>,
673 allocs: &'a mut CodeBuilderAllocations,
674
675 // Temporary locals injected and used by nan canonicalization. Note that
676 // this list of extra locals is appended to `self.locals` at the end of code
677 // generation, and it's kept separate here to avoid using these locals in
678 // `local.get` and similar instructions.
679 extra_locals: Vec<ValType>,
680 f32_scratch: Option<usize>,
681 f64_scratch: Option<usize>,
682 v128_scratch: Option<usize>,
683 }
684
685 /// A control frame.
686 #[derive(Debug, Clone)]
687 struct Control {
688 kind: ControlKind,
689 /// Value types that must be on the stack when entering this control frame.
690 params: Vec<ValType>,
691 /// Value types that are left on the stack when exiting this control frame.
692 results: Vec<ValType>,
693 /// How far down the operand stack instructions inside this control frame
694 /// can reach.
695 height: usize,
696 }
697
698 impl Control {
699 fn label_types(&self) -> &[ValType] {
700 if self.kind == ControlKind::Loop {
701 &self.params
702 } else {
703 &self.results
704 }
705 }
706 }
707
708 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
709 enum ControlKind {
710 Block,
711 If,
712 Loop,
713 TryTable,
714 }
715
716 enum Float {
717 F32,
718 F64,
719 F32x4,
720 F64x2,
721 }
722
723 impl CodeBuilderAllocations {
724 pub(crate) fn new(module: &Module, disallow_exporting: bool) -> Self {
725 let mut mutable_globals = BTreeMap::new();
726 for (i, global) in module.globals.iter().enumerate() {
727 if global.mutable {
728 mutable_globals
729 .entry(global.val_type)
730 .or_insert(Vec::new())
731 .push(i as u32);
732 }
733 }
734
735 let mut tags = BTreeMap::new();
736 for (idx, tag_type) in module.tags() {
737 tags.entry(tag_type.func_type.params.to_vec())
738 .or_insert(Vec::new())
739 .push(idx);
740 }
741
742 let mut functions = BTreeMap::new();
743 for (idx, func) in module.funcs() {
744 functions
745 .entry(func.clone())
746 .or_insert(Vec::new())
747 .push(idx);
748 }
749
750 let mut table32_with_funcref = Vec::new();
751 let mut table64_with_funcref = Vec::new();
752 let mut table32_tys = Vec::new();
753 let mut table64_tys = Vec::new();
754 for (i, table) in module.tables.iter().enumerate() {
755 let funcref_dst = if table.table64 {
756 table64_tys.push(table.element_type);
757 &mut table64_with_funcref
758 } else {
759 table32_tys.push(table.element_type);
760 &mut table32_with_funcref
761 };
762 if table.element_type == RefType::FUNCREF {
763 funcref_dst.push(i as u32);
764 }
765 }
766
767 let mut referenced_functions = BTreeSet::new();
768 for (_, expr) in module.defined_globals.iter() {
769 if let Some(i) = expr.get_ref_func() {
770 referenced_functions.insert(i);
771 }
772 }
773
774 let mut table32_init = Vec::new();
775 let mut table64_init = Vec::new();
776 for (i, g) in module.elems.iter().enumerate() {
777 match &g.items {
778 Elements::Expressions(e) => {
779 let iter = e.iter().filter_map(|e| e.get_ref_func());
780 referenced_functions.extend(iter);
781 }
782 Elements::Functions(e) => {
783 referenced_functions.extend(e.iter().cloned());
784 }
785 }
786 for (j, table) in module.tables.iter().enumerate() {
787 if module.ref_type_is_sub_type(g.ty, table.element_type) {
788 let dst = if table.table64 {
789 &mut table64_init
790 } else {
791 &mut table32_init
792 };
793 dst.push((i as u32, j as u32));
794 }
795 }
796 }
797
798 let mut memory32 = Vec::new();
799 let mut memory64 = Vec::new();
800 for (i, mem) in module.memories.iter().enumerate() {
801 if mem.memory64 {
802 memory64.push(i as u32);
803 } else {
804 memory32.push(i as u32);
805 }
806 }
807
808 let mut table32 = Vec::new();
809 let mut table64 = Vec::new();
810 let mut table_copy_32_to_32 = Vec::new();
811 let mut table_copy_32_to_64 = Vec::new();
812 let mut table_copy_64_to_32 = Vec::new();
813 let mut table_copy_64_to_64 = Vec::new();
814 for (i, t) in module.tables.iter().enumerate() {
815 if t.table64 {
816 table64.push(i as u32);
817 } else {
818 table32.push(i as u32);
819 }
820
821 for (j, t2) in module.tables.iter().enumerate() {
822 if module.val_type_is_sub_type(t.element_type.into(), t2.element_type.into()) {
823 let dst = match (t.table64, t2.table64) {
824 (false, false) => &mut table_copy_32_to_32,
825 (false, true) => &mut table_copy_32_to_64,
826 (true, false) => &mut table_copy_64_to_32,
827 (true, true) => &mut table_copy_64_to_64,
828 };
829 dst.push((i as u32, j as u32));
830 }
831 }
832 }
833
834 let mut global_dropped_i32 = None;
835 let mut global_dropped_i64 = None;
836 let mut global_dropped_f32 = None;
837 let mut global_dropped_f64 = None;
838 let mut global_dropped_v128 = None;
839
840 // If we can't export additional globals, try to use existing exported
841 // mutable globals for dropped values.
842 if disallow_exporting {
843 for (_, kind, index) in module.exports.iter() {
844 if *kind == ExportKind::Global {
845 let ty = module.globals[*index as usize];
846 if ty.mutable {
847 match ty.val_type {
848 ValType::I32 => {
849 if global_dropped_i32.is_none() {
850 global_dropped_i32 = Some(*index)
851 } else {
852 global_dropped_f32 = Some(*index)
853 }
854 }
855 ValType::I64 => {
856 if global_dropped_i64.is_none() {
857 global_dropped_i64 = Some(*index)
858 } else {
859 global_dropped_f64 = Some(*index)
860 }
861 }
862 ValType::V128 => global_dropped_v128 = Some(*index),
863 _ => {}
864 }
865 }
866 }
867 }
868 }
869
870 CodeBuilderAllocations {
871 controls: Vec::with_capacity(4),
872 operands: Vec::with_capacity(16),
873 options: Vec::with_capacity(NUM_OPTIONS),
874 functions,
875 tags,
876 mutable_globals,
877 table32_with_funcref,
878 table64_with_funcref,
879 referenced_functions: referenced_functions.into_iter().collect(),
880 table32_init,
881 table64_init,
882 table_copy_32_to_32,
883 table_copy_32_to_64,
884 table_copy_64_to_32,
885 table_copy_64_to_64,
886 memory32,
887 memory64,
888 table32,
889 table64,
890
891 global_dropped_i32,
892 global_dropped_i64,
893 global_dropped_f32,
894 global_dropped_f64,
895 global_dropped_v128,
896 globals_cnt: module.globals.len() as u32,
897 new_globals: Vec::new(),
898 disallow_exporting,
899 }
900 }
901
902 pub(crate) fn builder<'a>(
903 &'a mut self,
904 func_ty: &'a FuncType,
905 locals: &'a mut Vec<ValType>,
906 ) -> CodeBuilder<'a> {
907 self.controls.clear();
908 self.controls.push(Control {
909 kind: ControlKind::Block,
910 params: vec![],
911 results: func_ty.results.to_vec(),
912 height: 0,
913 });
914
915 self.operands.clear();
916 self.options.clear();
917
918 CodeBuilder {
919 func_ty,
920 locals,
921 allocs: self,
922 extra_locals: Vec::new(),
923 f32_scratch: None,
924 f64_scratch: None,
925 v128_scratch: None,
926 }
927 }
928
929 pub fn finish(self, u: &mut Unstructured<'_>, module: &mut Module) -> arbitrary::Result<()> {
930 // Any globals injected as part of dropping operands on the stack get
931 // injected into the module here. Each global is then exported, most of
932 // the time (if additional exports are allowed), to ensure it's part of
933 // the "image" of this module available for differential execution for
934 // example.
935 for (ty, init) in self.new_globals {
936 let global_idx = module.globals.len() as u32;
937 module.globals.push(GlobalType {
938 val_type: ty,
939 mutable: true,
940 shared: false,
941 });
942 module.defined_globals.push((global_idx, init));
943
944 if self.disallow_exporting || u.ratio(1, 100).unwrap_or(false) {
945 continue;
946 }
947
948 let name = unique_string(1_000, &mut module.export_names, u)?;
949 module.add_arbitrary_export(name, ExportKind::Global, global_idx)?;
950 }
951 Ok(())
952 }
953 }
954
955 impl CodeBuilder<'_> {
956 fn pop_control(&mut self) -> Control {
957 let control = self.allocs.controls.pop().unwrap();
958
959 // Pop the actual types on the stack (which could be subtypes of the
960 // declared types) and then push the declared types. This avoids us
961 // accidentally generating code that relies on erased subtypes.
962 for _ in &control.results {
963 self.pop_operand();
964 }
965 for ty in &control.results {
966 self.push_operand(Some(*ty));
967 }
968
969 control
970 }
971
972 fn push_control(
973 &mut self,
974 kind: ControlKind,
975 params: impl Into<Vec<ValType>>,
976 results: impl Into<Vec<ValType>>,
977 ) {
978 let params = params.into();
979 let results = results.into();
980
981 // Similar to in `pop_control`, we want to pop the actual argument types
982 // off the stack (which could be subtypes of the declared parameter
983 // types) and then push the parameter types. This effectively does type
984 // erasure of any subtyping that exists so that we don't accidentally
985 // generate code that relies on the specific subtypes.
986 for _ in &params {
987 self.pop_operand();
988 }
989 self.push_operands(&params);
990
991 let height = self.allocs.operands.len() - params.len();
992 self.allocs.controls.push(Control {
993 kind,
994 params,
995 results,
996 height,
997 });
998 }
999
1000 /// Get the operands that are in-scope within the current control frame.
1001 #[inline]
1002 fn operands(&self) -> &[Option<ValType>] {
1003 let height = self.allocs.controls.last().map_or(0, |c| c.height);
1004 &self.allocs.operands[height..]
1005 }
1006
1007 /// Pop a single operand from the stack, regardless of expected type.
1008 #[inline]
1009 fn pop_operand(&mut self) -> Option<ValType> {
1010 self.allocs.operands.pop().unwrap()
1011 }
1012
1013 #[inline]
1014 fn pop_operands(&mut self, module: &Module, to_pop: &[ValType]) {
1015 debug_assert!(self.types_on_stack(module, to_pop));
1016 self.allocs
1017 .operands
1018 .truncate(self.allocs.operands.len() - to_pop.len());
1019 }
1020
1021 #[inline]
1022 fn push_operands(&mut self, to_push: &[ValType]) {
1023 self.allocs
1024 .operands
1025 .extend(to_push.iter().copied().map(Some));
1026 }
1027
1028 #[inline]
1029 fn push_operand(&mut self, ty: Option<ValType>) {
1030 self.allocs.operands.push(ty);
1031 }
1032
1033 fn pop_label_types(&mut self, module: &Module, target: u32) {
1034 let target = usize::try_from(target).unwrap();
1035 let control = &self.allocs.controls[self.allocs.controls.len() - 1 - target];
1036 debug_assert!(self.label_types_on_stack(module, control));
1037 self.allocs
1038 .operands
1039 .truncate(self.allocs.operands.len() - control.label_types().len());
1040 }
1041
1042 fn push_label_types(&mut self, target: u32) {
1043 let target = usize::try_from(target).unwrap();
1044 let control = &self.allocs.controls[self.allocs.controls.len() - 1 - target];
1045 self.allocs
1046 .operands
1047 .extend(control.label_types().iter().copied().map(Some));
1048 }
1049
1050 /// Pop the target label types, and then push them again.
1051 ///
1052 /// This is not a no-op due to subtyping: if we have a `T <: U` on the
1053 /// stack, and the target label's type is `[U]`, then this will erase the
1054 /// information about `T` and subsequent operations may only operate on `U`.
1055 fn pop_push_label_types(&mut self, module: &Module, target: u32) {
1056 self.pop_label_types(module, target);
1057 self.push_label_types(target)
1058 }
1059
1060 fn label_types_on_stack(&self, module: &Module, to_check: &Control) -> bool {
1061 self.types_on_stack(module, to_check.label_types())
1062 }
1063
1064 /// Is the given type on top of the stack?
1065 #[inline]
1066 fn type_on_stack(&self, module: &Module, ty: ValType) -> bool {
1067 self.type_on_stack_at(module, 0, ty)
1068 }
1069
1070 /// Is the given type on the stack at the given index (indexing from the top
1071 /// of the stack towards the bottom).
1072 #[inline]
1073 fn type_on_stack_at(&self, module: &Module, at: usize, expected: ValType) -> bool {
1074 let operands = self.operands();
1075 if at >= operands.len() {
1076 return false;
1077 }
1078 match operands[operands.len() - 1 - at] {
1079 None => true,
1080 Some(actual) => module.val_type_is_sub_type(actual, expected),
1081 }
1082 }
1083
1084 /// Are the given types on top of the stack?
1085 #[inline]
1086 fn types_on_stack(&self, module: &Module, types: &[ValType]) -> bool {
1087 self.operands().len() >= types.len()
1088 && types
1089 .iter()
1090 .rev()
1091 .enumerate()
1092 .all(|(idx, ty)| self.type_on_stack_at(module, idx, *ty))
1093 }
1094
1095 /// Are the given field types on top of the stack?
1096 #[inline]
1097 fn field_types_on_stack(&self, module: &Module, types: &[FieldType]) -> bool {
1098 self.operands().len() >= types.len()
1099 && types
1100 .iter()
1101 .rev()
1102 .enumerate()
1103 .all(|(idx, ty)| self.type_on_stack_at(module, idx, ty.element_type.unpack()))
1104 }
1105
1106 /// Is the given field type on top of the stack?
1107 #[inline]
1108 fn field_type_on_stack(&self, module: &Module, ty: FieldType) -> bool {
1109 self.type_on_stack(module, ty.element_type.unpack())
1110 }
1111
1112 /// Is the given field type on the stack at the given position (indexed from
1113 /// the top of the stack)?
1114 #[inline]
1115 fn field_type_on_stack_at(&self, module: &Module, at: usize, ty: FieldType) -> bool {
1116 self.type_on_stack_at(module, at, ty.element_type.unpack())
1117 }
1118
1119 /// Get the ref type on the top of the operand stack, if any.
1120 ///
1121 /// * `None` means no reftype on the stack.
1122 /// * `Some(None)` means that the stack is polymorphic.
1123 /// * `Some(Some(r))` means that `r` is the ref type on top of the stack.
1124 fn ref_type_on_stack(&self) -> Option<Option<RefType>> {
1125 match self.operands().last().copied()? {
1126 Some(ValType::Ref(r)) => Some(Some(r)),
1127 Some(_) => None,
1128 None => Some(None),
1129 }
1130 }
1131
1132 /// Is there a `(ref null? <index>)` on the stack at the given position? If
1133 /// so return its nullability and type index.
1134 fn concrete_ref_type_on_stack_at(&self, at: usize) -> Option<(bool, u32)> {
1135 match self.operands().iter().copied().rev().nth(at)?? {
1136 ValType::Ref(RefType {
1137 nullable,
1138 heap_type: HeapType::Concrete(ty),
1139 }) => Some((nullable, ty)),
1140 _ => None,
1141 }
1142 }
1143
1144 /// Is there a `(ref null? <index>)` at the given stack position that
1145 /// references a concrete array type?
1146 fn concrete_array_ref_type_on_stack_at(
1147 &self,
1148 module: &Module,
1149 at: usize,
1150 ) -> Option<(bool, u32, ArrayType)> {
1151 let (nullable, ty) = self.concrete_ref_type_on_stack_at(at)?;
1152 match &module.ty(ty).composite_type.inner {
1153 CompositeInnerType::Array(a) => Some((nullable, ty, *a)),
1154 _ => None,
1155 }
1156 }
1157
1158 /// Is there a `(ref null? <index>)` at the given stack position that
1159 /// references a concrete struct type?
1160 fn concrete_struct_ref_type_on_stack_at<'a>(
1161 &self,
1162 module: &'a Module,
1163 at: usize,
1164 ) -> Option<(bool, u32, &'a StructType)> {
1165 let (nullable, ty) = self.concrete_ref_type_on_stack_at(at)?;
1166 match &module.ty(ty).composite_type.inner {
1167 CompositeInnerType::Struct(s) => Some((nullable, ty, s)),
1168 _ => None,
1169 }
1170 }
1171
1172 /// Pop a reference type from the stack and return it.
1173 ///
1174 /// When in unreachable code and the stack is polymorphic, returns `None`.
1175 fn pop_ref_type(&mut self) -> Option<RefType> {
1176 let ref_ty = self.ref_type_on_stack().unwrap();
1177 self.pop_operand();
1178 ref_ty
1179 }
1180
1181 /// Pops a `(ref null? <index>)` from the stack and return its nullability
1182 /// and type index.
1183 fn pop_concrete_ref_type(&mut self) -> (bool, u32) {
1184 let ref_ty = self.pop_ref_type().unwrap();
1185 match ref_ty.heap_type {
1186 HeapType::Concrete(i) => (ref_ty.nullable, i),
1187 _ => panic!("not a concrete ref type"),
1188 }
1189 }
1190
1191 /// Get the `(ref null? <index>)` type on the top of the stack that
1192 /// references a function type, if any.
1193 fn concrete_funcref_on_stack(&self, module: &Module) -> Option<RefType> {
1194 match self.operands().last().copied()?? {
1195 ValType::Ref(r) => match r.heap_type {
1196 HeapType::Concrete(idx) => match &module.ty(idx).composite_type.inner {
1197 CompositeInnerType::Func(_) => Some(r),
1198 CompositeInnerType::Struct(_) | CompositeInnerType::Array(_) => None,
1199 },
1200 _ => None,
1201 },
1202 _ => None,
1203 }
1204 }
1205
1206 /// Is there a `(ref null? <index>)` on the top of the stack that references
1207 /// a struct type with at least one field?
1208 fn non_empty_struct_ref_on_stack(&self, module: &Module, allow_null_refs: bool) -> bool {
1209 match self.operands().last() {
1210 Some(Some(ValType::Ref(RefType {
1211 nullable,
1212 heap_type: HeapType::Concrete(idx),
1213 }))) => match &module.ty(*idx).composite_type.inner {
1214 CompositeInnerType::Struct(s) => {
1215 !s.fields.is_empty() && (!nullable || allow_null_refs)
1216 }
1217 _ => false,
1218 },
1219 _ => false,
1220 }
1221 }
1222
1223 #[inline(never)]
1224 fn arbitrary_block_type(&self, u: &mut Unstructured, module: &Module) -> Result<BlockType> {
1225 let mut options: Vec<Box<dyn Fn(&mut Unstructured) -> Result<BlockType>>> = vec![
1226 Box::new(|_| Ok(BlockType::Empty)),
1227 Box::new(|u| Ok(BlockType::Result(module.arbitrary_valtype(u)?))),
1228 ];
1229 if module.config.multi_value_enabled {
1230 for (i, ty) in module.func_types() {
1231 if self.types_on_stack(module, &ty.params) {
1232 options.push(Box::new(move |_| Ok(BlockType::FunctionType(i as u32))));
1233 }
1234 }
1235 }
1236 let f = u.choose(&options)?;
1237 f(u)
1238 }
1239
1240 pub(crate) fn arbitrary(
1241 mut self,
1242 u: &mut Unstructured,
1243 module: &Module,
1244 ) -> Result<Vec<Instruction>> {
1245 let max_instructions = module.config.max_instructions;
1246 let allowed_instructions = if module.config.allow_floats {
1247 module.config.allowed_instructions
1248 } else {
1249 module.config.allowed_instructions.without_floats()
1250 };
1251 let mut instructions = vec![];
1252
1253 while !self.allocs.controls.is_empty() {
1254 let keep_going = instructions.len() < max_instructions && u.arbitrary::<u8>()? != 0;
1255 if !keep_going {
1256 self.end_active_control_frames(
1257 u,
1258 module,
1259 &mut instructions,
1260 module.config.disallow_traps,
1261 )?;
1262 break;
1263 }
1264
1265 match choose_instruction(u, module, allowed_instructions, &mut self) {
1266 Some(f) => {
1267 f(u, module, &mut self, &mut instructions)?;
1268 }
1269 // Choosing an instruction can fail because there is not enough
1270 // underlying data, so we really cannot generate any more
1271 // instructions. In this case we swallow that error and instead
1272 // just terminate our wasm function's frames.
1273 None => {
1274 self.end_active_control_frames(
1275 u,
1276 module,
1277 &mut instructions,
1278 module.config.disallow_traps,
1279 )?;
1280 break;
1281 }
1282 }
1283
1284 // If the configuration for this module requests nan
1285 // canonicalization then perform that here based on whether or not
1286 // the previous instruction needs canonicalization. Note that this
1287 // is based off Cranelift's pass for nan canonicalization for which
1288 // instructions to canonicalize, but the general idea is most
1289 // floating-point operations.
1290 if module.config.canonicalize_nans {
1291 match instructions.last().unwrap() {
1292 Instruction::F32Ceil
1293 | Instruction::F32Floor
1294 | Instruction::F32Nearest
1295 | Instruction::F32Sqrt
1296 | Instruction::F32Trunc
1297 | Instruction::F32Div
1298 | Instruction::F32Max
1299 | Instruction::F32Min
1300 | Instruction::F32Mul
1301 | Instruction::F32Sub
1302 | Instruction::F32Add => self.canonicalize_nan(Float::F32, &mut instructions),
1303 Instruction::F64Ceil
1304 | Instruction::F64Floor
1305 | Instruction::F64Nearest
1306 | Instruction::F64Sqrt
1307 | Instruction::F64Trunc
1308 | Instruction::F64Div
1309 | Instruction::F64Max
1310 | Instruction::F64Min
1311 | Instruction::F64Mul
1312 | Instruction::F64Sub
1313 | Instruction::F64Add => self.canonicalize_nan(Float::F64, &mut instructions),
1314 Instruction::F32x4Ceil
1315 | Instruction::F32x4Floor
1316 | Instruction::F32x4Nearest
1317 | Instruction::F32x4Sqrt
1318 | Instruction::F32x4Trunc
1319 | Instruction::F32x4Div
1320 | Instruction::F32x4Max
1321 | Instruction::F32x4Min
1322 | Instruction::F32x4Mul
1323 | Instruction::F32x4Sub
1324 | Instruction::F32x4Add => {
1325 self.canonicalize_nan(Float::F32x4, &mut instructions)
1326 }
1327 Instruction::F64x2Ceil
1328 | Instruction::F64x2Floor
1329 | Instruction::F64x2Nearest
1330 | Instruction::F64x2Sqrt
1331 | Instruction::F64x2Trunc
1332 | Instruction::F64x2Div
1333 | Instruction::F64x2Max
1334 | Instruction::F64x2Min
1335 | Instruction::F64x2Mul
1336 | Instruction::F64x2Sub
1337 | Instruction::F64x2Add => {
1338 self.canonicalize_nan(Float::F64x2, &mut instructions)
1339 }
1340 _ => {}
1341 }
1342 }
1343 }
1344
1345 self.locals.extend(self.extra_locals.drain(..));
1346
1347 Ok(instructions)
1348 }
1349
1350 fn canonicalize_nan(&mut self, ty: Float, ins: &mut Vec<Instruction>) {
1351 // We'll need to temporarily save the top of the stack into a local, so
1352 // figure out that local here. Note that this tries to use the same
1353 // local if canonicalization happens more than once in a function.
1354 let (local, val_ty) = match ty {
1355 Float::F32 => (&mut self.f32_scratch, ValType::F32),
1356 Float::F64 => (&mut self.f64_scratch, ValType::F64),
1357 Float::F32x4 | Float::F64x2 => (&mut self.v128_scratch, ValType::V128),
1358 };
1359 let local = match *local {
1360 Some(i) => i as u32,
1361 None => self.alloc_local(val_ty),
1362 };
1363
1364 // Save the previous instruction's result into a local. This also leaves
1365 // a value on the stack as `val1` for the `select` instruction.
1366 ins.push(Instruction::LocalTee(local));
1367
1368 // The `val2` value input to the `select` below, our nan pattern.
1369 //
1370 // The nan patterns here are chosen to be a canonical representation
1371 // which is still NaN but the wasm will always produce the same bits of
1372 // a nan so if the wasm takes a look at the nan inside it'll always see
1373 // the same representation.
1374 const CANON_32BIT_NAN: u32 = 0b01111111110000000000000000000000;
1375 const CANON_64BIT_NAN: u64 =
1376 0b0111111111111000000000000000000000000000000000000000000000000000;
1377 ins.push(match ty {
1378 Float::F32 => Instruction::F32Const(f32::from_bits(CANON_32BIT_NAN)),
1379 Float::F64 => Instruction::F64Const(f64::from_bits(CANON_64BIT_NAN)),
1380 Float::F32x4 => {
1381 let nan = CANON_32BIT_NAN as i128;
1382 let nan = nan | (nan << 32) | (nan << 64) | (nan << 96);
1383 Instruction::V128Const(nan)
1384 }
1385 Float::F64x2 => {
1386 let nan = CANON_64BIT_NAN as i128;
1387 let nan = nan | (nan << 64);
1388 Instruction::V128Const(nan)
1389 }
1390 });
1391
1392 // the condition of the `select`, which is the float's equality test
1393 // with itself.
1394 ins.push(Instruction::LocalGet(local));
1395 ins.push(Instruction::LocalGet(local));
1396 ins.push(match ty {
1397 Float::F32 => Instruction::F32Eq,
1398 Float::F64 => Instruction::F64Eq,
1399 Float::F32x4 => Instruction::F32x4Eq,
1400 Float::F64x2 => Instruction::F64x2Eq,
1401 });
1402
1403 // Select the result. If the condition is nonzero (aka the float is
1404 // equal to itself) it picks `val1`, otherwise if zero (aka the float
1405 // is nan) it picks `val2`.
1406 ins.push(match ty {
1407 Float::F32 | Float::F64 => Instruction::Select,
1408 Float::F32x4 | Float::F64x2 => Instruction::V128Bitselect,
1409 });
1410 }
1411
1412 fn alloc_local(&mut self, ty: ValType) -> u32 {
1413 let val = self.locals.len() + self.func_ty.params.len() + self.extra_locals.len();
1414 self.extra_locals.push(ty);
1415 u32::try_from(val).unwrap()
1416 }
1417
1418 fn end_active_control_frames(
1419 &mut self,
1420 u: &mut Unstructured<'_>,
1421 module: &Module,
1422 instructions: &mut Vec<Instruction>,
1423 disallow_traps: bool,
1424 ) -> Result<()> {
1425 while !self.allocs.controls.is_empty() {
1426 // Ensure that this label is valid by placing the right types onto
1427 // the operand stack for the end of the label.
1428 self.guarantee_label_results(u, module, instructions, disallow_traps)?;
1429
1430 // Remove the label and clear the operand stack since the label has
1431 // been removed.
1432 let label = self.allocs.controls.pop().unwrap();
1433 self.allocs.operands.truncate(label.height);
1434
1435 // If this is an `if` that is not stack neutral, then it
1436 // must have an `else`. Generate synthetic results here in the same
1437 // manner we did above.
1438 if label.kind == ControlKind::If && label.params != label.results {
1439 instructions.push(Instruction::Else);
1440 self.allocs.controls.push(label.clone());
1441 self.allocs
1442 .operands
1443 .extend(label.params.into_iter().map(Some));
1444 self.guarantee_label_results(u, module, instructions, disallow_traps)?;
1445 self.allocs.controls.pop();
1446 self.allocs.operands.truncate(label.height);
1447 }
1448
1449 // The last control frame for the function return does not
1450 // need an `end` instruction.
1451 if !self.allocs.controls.is_empty() {
1452 instructions.push(Instruction::End);
1453 }
1454
1455 // Place the results of the label onto the operand stack for use
1456 // after the label.
1457 self.allocs
1458 .operands
1459 .extend(label.results.into_iter().map(Some));
1460 }
1461 Ok(())
1462 }
1463
1464 /// Modifies the instruction stream to guarantee that the current control
1465 /// label's results are on the stack and ready for the control label to return.
1466 fn guarantee_label_results(
1467 &mut self,
1468 u: &mut Unstructured<'_>,
1469 module: &Module,
1470 instructions: &mut Vec<Instruction>,
1471 disallow_traps: bool,
1472 ) -> Result<()> {
1473 let operands = self.operands();
1474 let label = self.allocs.controls.last().unwrap();
1475
1476 // Already done, yay!
1477 if label.results.len() == operands.len() && self.types_on_stack(module, &label.results) {
1478 return Ok(());
1479 }
1480
1481 // Generating an unreachable instruction is always a valid way to
1482 // generate any types for a label, but it's not too interesting, so
1483 // don't favor it.
1484 if !disallow_traps && u.ratio(1, u16::MAX)? {
1485 instructions.push(Instruction::Unreachable);
1486 return Ok(());
1487 }
1488
1489 // Arbitrarily massage the stack to get the expected results. First we
1490 // drop all extraneous results to we're only dealing with those we want
1491 // to deal with. Afterwards we start at the bottom of the stack and move
1492 // up, figuring out what matches and what doesn't. As soon as something
1493 // doesn't match we throw out that and everything else remaining,
1494 // filling in results with dummy values.
1495 let operands = operands.to_vec();
1496 let mut operands = operands.as_slice();
1497 let label_results = label.results.to_vec();
1498 while operands.len() > label_results.len() {
1499 self.drop_operand(u, *operands.last().unwrap(), instructions)?;
1500 operands = &operands[..operands.len() - 1];
1501 }
1502 for (i, expected) in label_results.iter().enumerate() {
1503 if let Some(actual) = operands.get(i) {
1504 if Some(*expected) == *actual {
1505 continue;
1506 }
1507 for ty in operands[i..].iter().rev() {
1508 self.drop_operand(u, *ty, instructions)?;
1509 }
1510 operands = &[];
1511 }
1512 instructions.push(module.arbitrary_const_instruction(*expected, u)?);
1513 }
1514 Ok(())
1515 }
1516
1517 fn drop_operand(
1518 &mut self,
1519 u: &mut Unstructured<'_>,
1520 ty: Option<ValType>,
1521 instructions: &mut Vec<Instruction>,
1522 ) -> Result<()> {
1523 if !self.mix_operand_into_global(u, ty, instructions)? {
1524 instructions.push(Instruction::Drop);
1525 }
1526 Ok(())
1527 }
1528
1529 /// Attempts to drop the top operand on the stack by "mixing" it into a
1530 /// global.
1531 ///
1532 /// This is done to avoid dropping values on the floor to ensure that
1533 /// everything is part of some computation somewhere. Otherwise, for
1534 /// example, most function results are dropped on the floor as the stack
1535 /// won't happen to match the function type that we're generating.
1536 ///
1537 /// This will return `true` if the operand has been dropped, and `false` if
1538 /// it didn't for one reason or another.
1539 fn mix_operand_into_global(
1540 &mut self,
1541 u: &mut Unstructured<'_>,
1542 ty: Option<ValType>,
1543 instructions: &mut Vec<Instruction>,
1544 ) -> Result<bool> {
1545 // If the type of this operand isn't known, for example if it's relevant
1546 // to unreachable code, then it can't be combined, so return `false`.
1547 let ty = match ty {
1548 Some(ty) => ty,
1549 None => return Ok(false),
1550 };
1551
1552 // Use the input stream to allow a small chance of dropping the value
1553 // without combining it.
1554 if u.ratio(1, 100)? {
1555 return Ok(false);
1556 }
1557
1558 // Depending on the type lookup or inject a global to place this value
1559 // into.
1560 let (global, combine) = match ty {
1561 ValType::I32 => {
1562 let global = *self.allocs.global_dropped_i32.get_or_insert_with(|| {
1563 self.allocs.new_globals.push((ty, ConstExpr::i32_const(0)));
1564 inc(&mut self.allocs.globals_cnt)
1565 });
1566 (global, Instruction::I32Xor)
1567 }
1568 ValType::I64 => {
1569 let global = *self.allocs.global_dropped_i64.get_or_insert_with(|| {
1570 self.allocs.new_globals.push((ty, ConstExpr::i64_const(0)));
1571 inc(&mut self.allocs.globals_cnt)
1572 });
1573 (global, Instruction::I64Xor)
1574 }
1575 ValType::F32 => {
1576 let global = *self.allocs.global_dropped_f32.get_or_insert_with(|| {
1577 self.allocs
1578 .new_globals
1579 .push((ValType::I32, ConstExpr::i32_const(0)));
1580 inc(&mut self.allocs.globals_cnt)
1581 });
1582 instructions.push(Instruction::I32ReinterpretF32);
1583 (global, Instruction::I32Xor)
1584 }
1585 ValType::F64 => {
1586 let global = *self.allocs.global_dropped_f64.get_or_insert_with(|| {
1587 self.allocs
1588 .new_globals
1589 .push((ValType::I64, ConstExpr::i64_const(0)));
1590 inc(&mut self.allocs.globals_cnt)
1591 });
1592 instructions.push(Instruction::I64ReinterpretF64);
1593 (global, Instruction::I64Xor)
1594 }
1595 ValType::V128 => {
1596 let global = *self.allocs.global_dropped_v128.get_or_insert_with(|| {
1597 self.allocs.new_globals.push((ty, ConstExpr::v128_const(0)));
1598 inc(&mut self.allocs.globals_cnt)
1599 });
1600 (global, Instruction::V128Xor)
1601 }
1602
1603 // Don't know how to combine reference types at this time, so just
1604 // let it get dropped.
1605 ValType::Ref(_) => return Ok(false),
1606 };
1607 instructions.push(Instruction::GlobalGet(global));
1608 instructions.push(combine);
1609 instructions.push(Instruction::GlobalSet(global));
1610
1611 return Ok(true);
1612
1613 fn inc(val: &mut u32) -> u32 {
1614 let ret = *val;
1615 *val += 1;
1616 ret
1617 }
1618 }
1619 }
1620
1621 #[inline]
1622 fn unreachable_valid(module: &Module, _: &mut CodeBuilder) -> bool {
1623 !module.config.disallow_traps
1624 }
1625
1626 fn unreachable(
1627 _: &mut Unstructured,
1628 _: &Module,
1629 _: &mut CodeBuilder,
1630 instructions: &mut Vec<Instruction>,
1631 ) -> Result<()> {
1632 instructions.push(Instruction::Unreachable);
1633 Ok(())
1634 }
1635
1636 fn nop(
1637 _: &mut Unstructured,
1638 _: &Module,
1639 _: &mut CodeBuilder,
1640 instructions: &mut Vec<Instruction>,
1641 ) -> Result<()> {
1642 instructions.push(Instruction::Nop);
1643 Ok(())
1644 }
1645
1646 fn block(
1647 u: &mut Unstructured,
1648 module: &Module,
1649 builder: &mut CodeBuilder,
1650 instructions: &mut Vec<Instruction>,
1651 ) -> Result<()> {
1652 let block_ty = builder.arbitrary_block_type(u, module)?;
1653 let (params, results) = module.params_results(&block_ty);
1654 builder.push_control(ControlKind::Block, params, results);
1655 instructions.push(Instruction::Block(block_ty));
1656 Ok(())
1657 }
1658
1659 #[inline]
1660 fn try_table_valid(module: &Module, _: &mut CodeBuilder) -> bool {
1661 module.config.exceptions_enabled
1662 }
1663
1664 fn try_table(
1665 u: &mut Unstructured,
1666 module: &Module,
1667 builder: &mut CodeBuilder,
1668 instructions: &mut Vec<Instruction>,
1669 ) -> Result<()> {
1670 let block_ty = builder.arbitrary_block_type(u, module)?;
1671
1672 let mut catch_options: Vec<
1673 Box<dyn Fn(&mut Unstructured<'_>, &mut CodeBuilder<'_>) -> Result<Catch>>,
1674 > = Vec::new();
1675
1676 for (i, ctrl) in builder.allocs.controls.iter().rev().enumerate() {
1677 let i = i as u32;
1678
1679 let label_types = ctrl.label_types();
1680
1681 // Empty labels are candidates for a `catch_all` since nothing is
1682 // pushed in that case.
1683 if label_types.is_empty() {
1684 catch_options.push(Box::new(move |_, _| Ok(Catch::All { label: i })));
1685 }
1686
1687 // Labels with just an `externref` are suitable for `catch_all_refs`,
1688 // which first pushes nothing since there's no tag and then pushes
1689 // the caught exception value.
1690 if label_types == [ValType::EXNREF] {
1691 catch_options.push(Box::new(move |_, _| Ok(Catch::AllRef { label: i })));
1692 }
1693
1694 // If there is a tag which exactly matches the types of the label we're
1695 // looking at then that tag can be used as part of a `catch` branch.
1696 // That tag's parameters, which are the except values, are pushed
1697 // for the label.
1698 if builder.allocs.tags.contains_key(label_types) {
1699 let label_types = label_types.to_vec();
1700 catch_options.push(Box::new(move |u, builder| {
1701 Ok(Catch::One {
1702 tag: *u.choose(&builder.allocs.tags[&label_types])?,
1703 label: i,
1704 })
1705 }));
1706 }
1707
1708 // And finally the last type of catch label, `catch_ref`. If the label
1709 // ends with `exnref`, then use everything except the last `exnref` to
1710 // see if there's a matching tag. If so then `catch_ref` can be used
1711 // with that tag when branching to this label.
1712 if let Some((&ValType::EXNREF, rest)) = label_types.split_last() {
1713 if builder.allocs.tags.contains_key(rest) {
1714 let rest = rest.to_vec();
1715 catch_options.push(Box::new(move |u, builder| {
1716 Ok(Catch::OneRef {
1717 tag: *u.choose(&builder.allocs.tags[&rest])?,
1718 label: i,
1719 })
1720 }));
1721 }
1722 }
1723 }
1724
1725 let mut catches = Vec::new();
1726 if catch_options.len() > 0 {
1727 for _ in 0..u.int_in_range(0..=10)? {
1728 catches.push(u.choose(&mut catch_options)?(u, builder)?);
1729 }
1730 }
1731
1732 let (params, results) = module.params_results(&block_ty);
1733 builder.push_control(ControlKind::TryTable, params, results);
1734
1735 instructions.push(Instruction::TryTable(block_ty, catches.into()));
1736 Ok(())
1737 }
1738
1739 fn r#loop(
1740 u: &mut Unstructured,
1741 module: &Module,
1742 builder: &mut CodeBuilder,
1743 instructions: &mut Vec<Instruction>,
1744 ) -> Result<()> {
1745 let block_ty = builder.arbitrary_block_type(u, module)?;
1746 let (params, results) = module.params_results(&block_ty);
1747 builder.push_control(ControlKind::Loop, params, results);
1748 instructions.push(Instruction::Loop(block_ty));
1749 Ok(())
1750 }
1751
1752 #[inline]
1753 fn if_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1754 builder.type_on_stack(module, ValType::I32)
1755 }
1756
1757 fn r#if(
1758 u: &mut Unstructured,
1759 module: &Module,
1760 builder: &mut CodeBuilder,
1761 instructions: &mut Vec<Instruction>,
1762 ) -> Result<()> {
1763 builder.pop_operands(module, &[ValType::I32]);
1764 let block_ty = builder.arbitrary_block_type(u, module)?;
1765 let (params, results) = module.params_results(&block_ty);
1766 builder.push_control(ControlKind::If, params, results);
1767 instructions.push(Instruction::If(block_ty));
1768 Ok(())
1769 }
1770
1771 #[inline]
1772 fn else_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1773 let last_control = builder.allocs.controls.last().unwrap();
1774 last_control.kind == ControlKind::If
1775 && builder.operands().len() == last_control.results.len()
1776 && builder.types_on_stack(module, &last_control.results)
1777 }
1778
1779 fn r#else(
1780 _: &mut Unstructured,
1781 module: &Module,
1782 builder: &mut CodeBuilder,
1783 instructions: &mut Vec<Instruction>,
1784 ) -> Result<()> {
1785 let control = builder.pop_control();
1786 builder.pop_operands(module, &control.results);
1787 builder.push_operands(&control.params);
1788 builder.push_control(ControlKind::Block, control.params, control.results);
1789 instructions.push(Instruction::Else);
1790 Ok(())
1791 }
1792
1793 #[inline]
1794 fn end_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1795 // Note: first control frame is the function return's control frame, which
1796 // does not have an associated `end`.
1797 if builder.allocs.controls.len() <= 1 {
1798 return false;
1799 }
1800 let control = builder.allocs.controls.last().unwrap();
1801 builder.operands().len() == control.results.len()
1802 && builder.types_on_stack(module, &control.results)
1803 // `if`s that don't leave the stack as they found it must have an
1804 // `else`.
1805 && !(control.kind == ControlKind::If && control.params != control.results)
1806 }
1807
1808 fn end(
1809 _: &mut Unstructured,
1810 _: &Module,
1811 builder: &mut CodeBuilder,
1812 instructions: &mut Vec<Instruction>,
1813 ) -> Result<()> {
1814 builder.pop_control();
1815 instructions.push(Instruction::End);
1816 Ok(())
1817 }
1818
1819 #[inline]
1820 fn br_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1821 builder
1822 .allocs
1823 .controls
1824 .iter()
1825 .any(|l| builder.label_types_on_stack(module, l))
1826 }
1827
1828 fn br(
1829 u: &mut Unstructured,
1830 module: &Module,
1831 builder: &mut CodeBuilder,
1832 instructions: &mut Vec<Instruction>,
1833 ) -> Result<()> {
1834 let n = builder
1835 .allocs
1836 .controls
1837 .iter()
1838 .filter(|l| builder.label_types_on_stack(module, l))
1839 .count();
1840 debug_assert!(n > 0);
1841 let i = u.int_in_range(0..=n - 1)?;
1842 let (target, _) = builder
1843 .allocs
1844 .controls
1845 .iter()
1846 .rev()
1847 .enumerate()
1848 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1849 .nth(i)
1850 .unwrap();
1851 let target = u32::try_from(target).unwrap();
1852 builder.pop_label_types(module, target);
1853 instructions.push(Instruction::Br(target));
1854 Ok(())
1855 }
1856
1857 #[inline]
1858 fn br_if_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1859 if !builder.type_on_stack(module, ValType::I32) {
1860 return false;
1861 }
1862 let ty = builder.allocs.operands.pop().unwrap();
1863 let is_valid = builder
1864 .allocs
1865 .controls
1866 .iter()
1867 .any(|l| builder.label_types_on_stack(module, l));
1868 builder.allocs.operands.push(ty);
1869 is_valid
1870 }
1871
1872 fn br_if(
1873 u: &mut Unstructured,
1874 module: &Module,
1875 builder: &mut CodeBuilder,
1876 instructions: &mut Vec<Instruction>,
1877 ) -> Result<()> {
1878 builder.pop_operands(module, &[ValType::I32]);
1879
1880 let n = builder
1881 .allocs
1882 .controls
1883 .iter()
1884 .filter(|l| builder.label_types_on_stack(module, l))
1885 .count();
1886 debug_assert!(n > 0);
1887 let i = u.int_in_range(0..=n - 1)?;
1888 let (target, _) = builder
1889 .allocs
1890 .controls
1891 .iter()
1892 .rev()
1893 .enumerate()
1894 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1895 .nth(i)
1896 .unwrap();
1897 let target = u32::try_from(target).unwrap();
1898 builder.pop_push_label_types(module, target);
1899 instructions.push(Instruction::BrIf(target));
1900 Ok(())
1901 }
1902
1903 #[inline]
1904 fn br_table_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1905 if !builder.type_on_stack(module, ValType::I32) {
1906 return false;
1907 }
1908 let ty = builder.allocs.operands.pop().unwrap();
1909 let is_valid = br_valid(module, builder);
1910 builder.allocs.operands.push(ty);
1911 is_valid
1912 }
1913
1914 fn br_table(
1915 u: &mut Unstructured,
1916 module: &Module,
1917 builder: &mut CodeBuilder,
1918 instructions: &mut Vec<Instruction>,
1919 ) -> Result<()> {
1920 builder.pop_operands(module, &[ValType::I32]);
1921
1922 let n = builder
1923 .allocs
1924 .controls
1925 .iter()
1926 .filter(|l| builder.label_types_on_stack(module, l))
1927 .count();
1928 debug_assert!(n > 0);
1929
1930 let i = u.int_in_range(0..=n - 1)?;
1931 let (default_target, _) = builder
1932 .allocs
1933 .controls
1934 .iter()
1935 .rev()
1936 .enumerate()
1937 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1938 .nth(i)
1939 .unwrap();
1940 let control = &builder.allocs.controls[builder.allocs.controls.len() - 1 - default_target];
1941
1942 let targets = builder
1943 .allocs
1944 .controls
1945 .iter()
1946 .rev()
1947 .enumerate()
1948 .filter(|(_, l)| l.label_types() == control.label_types())
1949 .map(|(t, _)| t as u32)
1950 .collect();
1951
1952 let tys = control.label_types().to_vec();
1953 builder.pop_operands(module, &tys);
1954
1955 instructions.push(Instruction::BrTable(targets, default_target as u32));
1956 Ok(())
1957 }
1958
1959 #[inline]
1960 fn return_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1961 builder.label_types_on_stack(module, &builder.allocs.controls[0])
1962 }
1963
1964 fn r#return(
1965 _: &mut Unstructured,
1966 module: &Module,
1967 builder: &mut CodeBuilder,
1968 instructions: &mut Vec<Instruction>,
1969 ) -> Result<()> {
1970 let results = builder.allocs.controls[0].results.clone();
1971 builder.pop_operands(module, &results);
1972 instructions.push(Instruction::Return);
1973 Ok(())
1974 }
1975
1976 #[inline]
1977 fn call_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1978 builder
1979 .allocs
1980 .functions
1981 .keys()
1982 .any(|func_ty| builder.types_on_stack(module, &func_ty.params))
1983 }
1984
1985 fn call(
1986 u: &mut Unstructured,
1987 module: &Module,
1988 builder: &mut CodeBuilder,
1989 instructions: &mut Vec<Instruction>,
1990 ) -> Result<()> {
1991 let candidates = builder
1992 .allocs
1993 .functions
1994 .iter()
1995 .filter(|(func_ty, _)| builder.types_on_stack(module, &func_ty.params))
1996 .flat_map(|(_, v)| v.iter().copied())
1997 .collect::<Vec<_>>();
1998 assert!(candidates.len() > 0);
1999 let i = u.int_in_range(0..=candidates.len() - 1)?;
2000 let (func_idx, ty) = module.funcs().nth(candidates[i] as usize).unwrap();
2001 builder.pop_operands(module, &ty.params);
2002 builder.push_operands(&ty.results);
2003 instructions.push(Instruction::Call(func_idx as u32));
2004 Ok(())
2005 }
2006
2007 #[inline]
2008 fn call_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2009 if !module.config.gc_enabled {
2010 return false;
2011 }
2012 let funcref = match builder.concrete_funcref_on_stack(module) {
2013 Some(f) => f,
2014 None => return false,
2015 };
2016 if module.config.disallow_traps && funcref.nullable {
2017 return false;
2018 }
2019 match funcref.heap_type {
2020 HeapType::Concrete(idx) => {
2021 let ty = builder.allocs.operands.pop().unwrap();
2022 let params = &module.ty(idx).unwrap_func().params;
2023 let valid = builder.types_on_stack(module, params);
2024 builder.allocs.operands.push(ty);
2025 valid
2026 }
2027 _ => unreachable!(),
2028 }
2029 }
2030
2031 fn call_ref(
2032 _u: &mut Unstructured,
2033 module: &Module,
2034 builder: &mut CodeBuilder,
2035 instructions: &mut Vec<Instruction>,
2036 ) -> Result<()> {
2037 let heap_ty = match builder.pop_operand() {
2038 Some(ValType::Ref(r)) => r.heap_type,
2039 _ => unreachable!(),
2040 };
2041 let idx = match heap_ty {
2042 HeapType::Concrete(idx) => idx,
2043 _ => unreachable!(),
2044 };
2045 let func_ty = match &module.ty(idx).composite_type.inner {
2046 CompositeInnerType::Func(f) => f,
2047 _ => unreachable!(),
2048 };
2049 builder.pop_operands(module, &func_ty.params);
2050 builder.push_operands(&func_ty.results);
2051 instructions.push(Instruction::CallRef(idx));
2052 Ok(())
2053 }
2054
2055 #[inline]
2056 fn call_indirect_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2057 call_indirect_valid_impl(module, builder, false)
2058 }
2059
2060 fn call_indirect_valid_impl(
2061 module: &Module,
2062 builder: &mut CodeBuilder,
2063 is_return_call: bool,
2064 ) -> bool {
2065 if module.config.disallow_traps {
2066 // We have no way to reflect, at run time, on a `funcref` in
2067 // the `i`th slot in a table and dynamically avoid trapping
2068 // `call_indirect`s. Therefore, we can't emit *any*
2069 // `call_indirect` instructions if we want to avoid traps.
2070 return false;
2071 }
2072 let can_call32 = builder.type_on_stack(module, ValType::I32)
2073 && builder.allocs.table32_with_funcref.len() > 0;
2074 let can_call64 = builder.type_on_stack(module, ValType::I64)
2075 && builder.allocs.table64_with_funcref.len() > 0;
2076 if !can_call32 && !can_call64 {
2077 return false;
2078 }
2079 let ty = builder.allocs.operands.pop().unwrap();
2080 let is_valid = module.func_types().any(|(_, ty)| {
2081 builder.types_on_stack(module, &ty.params)
2082 && (!is_return_call || builder.allocs.controls[0].label_types() == &ty.results)
2083 });
2084 builder.allocs.operands.push(ty);
2085 is_valid
2086 }
2087
2088 fn call_indirect(
2089 u: &mut Unstructured,
2090 module: &Module,
2091 builder: &mut CodeBuilder,
2092 instructions: &mut Vec<Instruction>,
2093 ) -> Result<()> {
2094 let table = select_call_indirect_table(u, module, builder)?;
2095
2096 let choices = module
2097 .func_types()
2098 .filter(|(_, ty)| builder.types_on_stack(module, &ty.params))
2099 .collect::<Vec<_>>();
2100 let (type_idx, ty) = u.choose(&choices)?;
2101 builder.pop_operands(module, &ty.params);
2102 builder.push_operands(&ty.results);
2103 instructions.push(Instruction::CallIndirect {
2104 type_index: *type_idx as u32,
2105 table_index: table,
2106 });
2107 Ok(())
2108 }
2109
2110 fn select_call_indirect_table(
2111 u: &mut Unstructured,
2112 module: &Module,
2113 builder: &mut CodeBuilder,
2114 ) -> Result<u32> {
2115 let tables = if builder.type_on_stack(module, ValType::I32) {
2116 builder.pop_operands(module, &[ValType::I32]);
2117 &builder.allocs.table32_with_funcref
2118 } else {
2119 builder.pop_operands(module, &[ValType::I64]);
2120 &builder.allocs.table64_with_funcref
2121 };
2122 Ok(*u.choose(tables)?)
2123 }
2124
2125 #[inline]
2126 fn return_call_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2127 if !module.config.tail_call_enabled {
2128 return false;
2129 }
2130
2131 builder.allocs.functions.keys().any(|func_ty| {
2132 builder.types_on_stack(module, &func_ty.params)
2133 && builder.allocs.controls[0].label_types() == &func_ty.results
2134 })
2135 }
2136
2137 fn return_call(
2138 u: &mut Unstructured,
2139 module: &Module,
2140 builder: &mut CodeBuilder,
2141 instructions: &mut Vec<Instruction>,
2142 ) -> Result<()> {
2143 let candidates = builder
2144 .allocs
2145 .functions
2146 .iter()
2147 .filter(|(func_ty, _)| {
2148 builder.types_on_stack(module, &func_ty.params)
2149 && builder.allocs.controls[0].label_types() == &func_ty.results
2150 })
2151 .flat_map(|(_, v)| v.iter().copied())
2152 .collect::<Vec<_>>();
2153 assert!(candidates.len() > 0);
2154 let i = u.int_in_range(0..=candidates.len() - 1)?;
2155 let (func_idx, ty) = module.funcs().nth(candidates[i] as usize).unwrap();
2156 builder.pop_operands(module, &ty.params);
2157 builder.push_operands(&ty.results);
2158 instructions.push(Instruction::ReturnCall(func_idx as u32));
2159 Ok(())
2160 }
2161
2162 #[inline]
2163 fn return_call_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2164 if !module.config.gc_enabled {
2165 return false;
2166 }
2167
2168 let ref_ty = match builder.concrete_funcref_on_stack(module) {
2169 None => return false,
2170 Some(r) if r.nullable && module.config.disallow_traps => return false,
2171 Some(r) => r,
2172 };
2173
2174 let idx = match ref_ty.heap_type {
2175 HeapType::Concrete(idx) => idx,
2176 _ => unreachable!(),
2177 };
2178 let func_ty = match &module.ty(idx).composite_type.inner {
2179 CompositeInnerType::Func(f) => f,
2180 CompositeInnerType::Array(_) | CompositeInnerType::Struct(_) => return false,
2181 };
2182
2183 let ty = builder.allocs.operands.pop().unwrap();
2184 let valid = builder.types_on_stack(module, &func_ty.params)
2185 && builder.func_ty.results == func_ty.results;
2186 builder.allocs.operands.push(ty);
2187 valid
2188 }
2189
2190 fn return_call_ref(
2191 _u: &mut Unstructured,
2192 module: &Module,
2193 builder: &mut CodeBuilder,
2194 instructions: &mut Vec<Instruction>,
2195 ) -> Result<()> {
2196 let heap_ty = match builder.pop_operand() {
2197 Some(ValType::Ref(r)) => r.heap_type,
2198 _ => unreachable!(),
2199 };
2200 let idx = match heap_ty {
2201 HeapType::Concrete(idx) => idx,
2202 _ => unreachable!(),
2203 };
2204 let func_ty = match &module.ty(idx).composite_type.inner {
2205 CompositeInnerType::Func(f) => f,
2206 _ => unreachable!(),
2207 };
2208 builder.pop_operands(module, &func_ty.params);
2209 builder.push_operands(&func_ty.results);
2210 instructions.push(Instruction::ReturnCallRef(idx));
2211 Ok(())
2212 }
2213
2214 #[inline]
2215 fn return_call_indirect_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2216 if !module.config.tail_call_enabled {
2217 return false;
2218 }
2219 call_indirect_valid_impl(module, builder, true)
2220 }
2221
2222 fn return_call_indirect(
2223 u: &mut Unstructured,
2224 module: &Module,
2225 builder: &mut CodeBuilder,
2226 instructions: &mut Vec<Instruction>,
2227 ) -> Result<()> {
2228 let table = select_call_indirect_table(u, module, builder)?;
2229
2230 let choices = module
2231 .func_types()
2232 .filter(|(_, ty)| {
2233 builder.types_on_stack(module, &ty.params)
2234 && builder.allocs.controls[0].label_types() == &ty.results
2235 })
2236 .collect::<Vec<_>>();
2237 let (type_idx, ty) = u.choose(&choices)?;
2238 builder.pop_operands(module, &ty.params);
2239 builder.push_operands(&ty.results);
2240 instructions.push(Instruction::ReturnCallIndirect {
2241 type_index: *type_idx as u32,
2242 table_index: table,
2243 });
2244 Ok(())
2245 }
2246
2247 #[inline]
2248 fn throw_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2249 module.config.exceptions_enabled
2250 && builder
2251 .allocs
2252 .tags
2253 .keys()
2254 .any(|k| builder.types_on_stack(module, k))
2255 }
2256
2257 fn throw(
2258 u: &mut Unstructured,
2259 module: &Module,
2260 builder: &mut CodeBuilder,
2261 instructions: &mut Vec<Instruction>,
2262 ) -> Result<()> {
2263 let candidates = builder
2264 .allocs
2265 .tags
2266 .iter()
2267 .filter(|(k, _)| builder.types_on_stack(module, k))
2268 .flat_map(|(_, v)| v.iter().copied())
2269 .collect::<Vec<_>>();
2270 assert!(candidates.len() > 0);
2271 let i = u.int_in_range(0..=candidates.len() - 1)?;
2272 let (tag_idx, tag_type) = module.tags().nth(candidates[i] as usize).unwrap();
2273 // Tags have no results, throwing cannot return
2274 assert!(tag_type.func_type.results.len() == 0);
2275 builder.pop_operands(module, &tag_type.func_type.params);
2276 instructions.push(Instruction::Throw(tag_idx as u32));
2277 Ok(())
2278 }
2279
2280 #[inline]
2281 fn throw_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2282 module.config.exceptions_enabled && builder.types_on_stack(module, &[ValType::EXNREF])
2283 }
2284
2285 fn throw_ref(
2286 _u: &mut Unstructured,
2287 module: &Module,
2288 builder: &mut CodeBuilder,
2289 instructions: &mut Vec<Instruction>,
2290 ) -> Result<()> {
2291 builder.pop_operands(module, &[ValType::EXNREF]);
2292 instructions.push(Instruction::ThrowRef);
2293 Ok(())
2294 }
2295
2296 #[inline]
2297 fn br_on_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2298 if !module.config.gc_enabled {
2299 return false;
2300 }
2301 if builder.ref_type_on_stack().is_none() {
2302 return false;
2303 }
2304 let ty = builder.allocs.operands.pop().unwrap();
2305 let valid = br_valid(module, builder);
2306 builder.allocs.operands.push(ty);
2307 valid
2308 }
2309
2310 fn br_on_null(
2311 u: &mut Unstructured,
2312 module: &Module,
2313 builder: &mut CodeBuilder,
2314 instructions: &mut Vec<Instruction>,
2315 ) -> Result<()> {
2316 let heap_type = match builder.pop_ref_type() {
2317 Some(r) => r.heap_type,
2318 None => {
2319 if !module.types.is_empty() && u.arbitrary()? {
2320 HeapType::Concrete(u.int_in_range(0..=u32::try_from(module.types.len()).unwrap())?)
2321 } else {
2322 use AbstractHeapType::*;
2323 let ty = *u.choose(&[
2324 Func, Extern, Any, None, NoExtern, NoFunc, Eq, Struct, Array, I31,
2325 ])?;
2326 // TODO: handle shared
2327 HeapType::Abstract { shared: false, ty }
2328 }
2329 }
2330 };
2331
2332 let n = builder
2333 .allocs
2334 .controls
2335 .iter()
2336 .filter(|l| builder.label_types_on_stack(module, l))
2337 .count();
2338 debug_assert!(n > 0);
2339
2340 let i = u.int_in_range(0..=n - 1)?;
2341 let (target, _) = builder
2342 .allocs
2343 .controls
2344 .iter()
2345 .rev()
2346 .enumerate()
2347 .filter(|(_, l)| builder.label_types_on_stack(module, l))
2348 .nth(i)
2349 .unwrap();
2350 let target = u32::try_from(target).unwrap();
2351
2352 builder.pop_push_label_types(module, target);
2353 builder.push_operands(&[ValType::Ref(RefType {
2354 nullable: false,
2355 heap_type,
2356 })]);
2357
2358 instructions.push(Instruction::BrOnNull(target));
2359 Ok(())
2360 }
2361
2362 fn is_valid_br_on_non_null_control(
2363 module: &Module,
2364 control: &Control,
2365 builder: &CodeBuilder,
2366 ) -> bool {
2367 let ref_ty = match control.label_types().last() {
2368 Some(ValType::Ref(r)) => *r,
2369 Some(_) | None => return false,
2370 };
2371 let nullable_ref_ty = RefType {
2372 nullable: true,
2373 ..ref_ty
2374 };
2375 builder.type_on_stack(module, ValType::Ref(nullable_ref_ty))
2376 && control
2377 .label_types()
2378 .iter()
2379 .rev()
2380 .enumerate()
2381 .skip(1)
2382 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2383 }
2384
2385 #[inline]
2386 fn br_on_non_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2387 module.config.gc_enabled
2388 && builder
2389 .allocs
2390 .controls
2391 .iter()
2392 .any(|l| is_valid_br_on_non_null_control(module, l, builder))
2393 }
2394
2395 fn br_on_non_null(
2396 u: &mut Unstructured,
2397 module: &Module,
2398 builder: &mut CodeBuilder,
2399 instructions: &mut Vec<Instruction>,
2400 ) -> Result<()> {
2401 let n = builder
2402 .allocs
2403 .controls
2404 .iter()
2405 .filter(|l| is_valid_br_on_non_null_control(module, l, builder))
2406 .count();
2407 debug_assert!(n > 0);
2408
2409 let i = u.int_in_range(0..=n - 1)?;
2410 let (target, _) = builder
2411 .allocs
2412 .controls
2413 .iter()
2414 .rev()
2415 .enumerate()
2416 .filter(|(_, l)| is_valid_br_on_non_null_control(module, l, builder))
2417 .nth(i)
2418 .unwrap();
2419 let target = u32::try_from(target).unwrap();
2420
2421 builder.pop_push_label_types(module, target);
2422 builder.pop_ref_type();
2423 instructions.push(Instruction::BrOnNonNull(target));
2424 Ok(())
2425 }
2426
2427 fn is_valid_br_on_cast_control(
2428 module: &Module,
2429 builder: &CodeBuilder,
2430 control: &Control,
2431 from_ref_ty: Option<RefType>,
2432 ) -> bool {
2433 // The last label type is a sub type of the type we are casting from...
2434 let to_ref_ty = match control.label_types().last() {
2435 Some(ValType::Ref(r)) => *r,
2436 _ => return false,
2437 };
2438 if let Some(from_ty) = from_ref_ty {
2439 if !module.ref_type_is_sub_type(to_ref_ty, from_ty) {
2440 return false;
2441 }
2442 }
2443 // ... and the rest of the label types are on the stack.
2444 control
2445 .label_types()
2446 .iter()
2447 .rev()
2448 .enumerate()
2449 .skip(1)
2450 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2451 }
2452
2453 #[inline]
2454 fn br_on_cast_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2455 let from_ref_ty = match builder.ref_type_on_stack() {
2456 None => return false,
2457 Some(r) => r,
2458 };
2459 module.config.gc_enabled
2460 && builder
2461 .allocs
2462 .controls
2463 .iter()
2464 .any(|l| is_valid_br_on_cast_control(module, builder, l, from_ref_ty))
2465 }
2466
2467 /// Compute the [type difference] between the two given ref types.
2468 ///
2469 /// [type difference]: https://webassembly.github.io/gc/core/valid/conventions.html#aux-reftypediff
2470 fn ref_type_difference(a: RefType, b: RefType) -> RefType {
2471 RefType {
2472 nullable: if b.nullable { false } else { a.nullable },
2473 heap_type: a.heap_type,
2474 }
2475 }
2476
2477 fn br_on_cast(
2478 u: &mut Unstructured,
2479 module: &Module,
2480 builder: &mut CodeBuilder,
2481 instructions: &mut Vec<Instruction>,
2482 ) -> Result<()> {
2483 let from_ref_type = builder.ref_type_on_stack().unwrap();
2484
2485 let n = builder
2486 .allocs
2487 .controls
2488 .iter()
2489 .filter(|l| is_valid_br_on_cast_control(module, builder, l, from_ref_type))
2490 .count();
2491 debug_assert!(n > 0);
2492
2493 let i = u.int_in_range(0..=n - 1)?;
2494 let (relative_depth, control) = builder
2495 .allocs
2496 .controls
2497 .iter()
2498 .rev()
2499 .enumerate()
2500 .filter(|(_, l)| is_valid_br_on_cast_control(module, builder, l, from_ref_type))
2501 .nth(i)
2502 .unwrap();
2503 let relative_depth = u32::try_from(relative_depth).unwrap();
2504
2505 let num_label_types = control.label_types().len();
2506 let to_ref_type = match control.label_types().last() {
2507 Some(ValType::Ref(r)) => *r,
2508 _ => unreachable!(),
2509 };
2510
2511 let to_ref_type = module.arbitrary_matching_ref_type(u, to_ref_type)?;
2512 let from_ref_type = from_ref_type.unwrap_or(to_ref_type);
2513 let from_ref_type = module.arbitrary_super_type_of_ref_type(u, from_ref_type)?;
2514
2515 // Do `pop_push_label_types` but without its debug assert that the types are
2516 // on the stack, since we know that we have a `from_ref_type` but the label
2517 // requires a `to_ref_type`.
2518 for _ in 0..num_label_types {
2519 builder.pop_operand();
2520 }
2521 builder.push_label_types(relative_depth);
2522
2523 // Replace the label's `to_ref_type` with the type difference.
2524 builder.pop_operand();
2525 builder.push_operands(&[ValType::Ref(ref_type_difference(
2526 from_ref_type,
2527 to_ref_type,
2528 ))]);
2529
2530 instructions.push(Instruction::BrOnCast {
2531 from_ref_type,
2532 to_ref_type,
2533 relative_depth,
2534 });
2535 Ok(())
2536 }
2537
2538 fn is_valid_br_on_cast_fail_control(
2539 module: &Module,
2540 builder: &CodeBuilder,
2541 control: &Control,
2542 from_ref_type: Option<RefType>,
2543 ) -> bool {
2544 control
2545 .label_types()
2546 .last()
2547 .map_or(false, |label_ty| match (label_ty, from_ref_type) {
2548 (ValType::Ref(label_ty), Some(from_ty)) => {
2549 module.ref_type_is_sub_type(from_ty, *label_ty)
2550 }
2551 (ValType::Ref(_), None) => true,
2552 _ => false,
2553 })
2554 && control
2555 .label_types()
2556 .iter()
2557 .rev()
2558 .enumerate()
2559 .skip(1)
2560 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2561 }
2562
2563 #[inline]
2564 fn br_on_cast_fail_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2565 let from_ref_ty = match builder.ref_type_on_stack() {
2566 None => return false,
2567 Some(r) => r,
2568 };
2569 module.config.gc_enabled
2570 && builder
2571 .allocs
2572 .controls
2573 .iter()
2574 .any(|l| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_ty))
2575 }
2576
2577 fn br_on_cast_fail(
2578 u: &mut Unstructured,
2579 module: &Module,
2580 builder: &mut CodeBuilder,
2581 instructions: &mut Vec<Instruction>,
2582 ) -> Result<()> {
2583 let from_ref_type = builder.ref_type_on_stack().unwrap();
2584
2585 let n = builder
2586 .allocs
2587 .controls
2588 .iter()
2589 .filter(|l| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_type))
2590 .count();
2591 debug_assert!(n > 0);
2592
2593 let i = u.int_in_range(0..=n - 1)?;
2594 let (relative_depth, control) = builder
2595 .allocs
2596 .controls
2597 .iter()
2598 .rev()
2599 .enumerate()
2600 .filter(|(_, l)| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_type))
2601 .nth(i)
2602 .unwrap();
2603 let relative_depth = u32::try_from(relative_depth).unwrap();
2604
2605 let from_ref_type =
2606 from_ref_type.unwrap_or_else(|| match control.label_types().last().unwrap() {
2607 ValType::Ref(r) => *r,
2608 _ => unreachable!(),
2609 });
2610 let to_ref_type = module.arbitrary_matching_ref_type(u, from_ref_type)?;
2611
2612 // Pop-push the label types and then replace its last reference type with
2613 // our `to_ref_type`.
2614 builder.pop_push_label_types(module, relative_depth);
2615 builder.pop_operand();
2616 builder.push_operand(Some(ValType::Ref(to_ref_type)));
2617
2618 instructions.push(Instruction::BrOnCastFail {
2619 from_ref_type,
2620 to_ref_type,
2621 relative_depth,
2622 });
2623 Ok(())
2624 }
2625
2626 #[inline]
2627 fn drop_valid(_module: &Module, builder: &mut CodeBuilder) -> bool {
2628 !builder.operands().is_empty()
2629 }
2630
2631 fn drop(
2632 u: &mut Unstructured,
2633 _module: &Module,
2634 builder: &mut CodeBuilder,
2635 instructions: &mut Vec<Instruction>,
2636 ) -> Result<()> {
2637 let ty = builder.pop_operand();
2638 builder.drop_operand(u, ty, instructions)?;
2639 Ok(())
2640 }
2641
2642 #[inline]
2643 fn select_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2644 if !(builder.operands().len() >= 3 && builder.type_on_stack(module, ValType::I32)) {
2645 return false;
2646 }
2647 let t = builder.operands()[builder.operands().len() - 2];
2648 let u = builder.operands()[builder.operands().len() - 3];
2649 t.is_none() || u.is_none() || t == u
2650 }
2651
2652 fn select(
2653 _: &mut Unstructured,
2654 _module: &Module,
2655 builder: &mut CodeBuilder,
2656 instructions: &mut Vec<Instruction>,
2657 ) -> Result<()> {
2658 builder.pop_operand();
2659 let t = builder.pop_operand();
2660 let u = builder.pop_operand();
2661 let ty = t.or(u);
2662 builder.allocs.operands.push(ty);
2663 match ty {
2664 Some(ty @ ValType::Ref(_)) => instructions.push(Instruction::TypedSelect(ty)),
2665 Some(ValType::I32) | Some(ValType::I64) | Some(ValType::F32) | Some(ValType::F64)
2666 | Some(ValType::V128) | None => instructions.push(Instruction::Select),
2667 }
2668 Ok(())
2669 }
2670
2671 #[inline]
2672 fn local_get_valid(_module: &Module, builder: &mut CodeBuilder) -> bool {
2673 !builder.func_ty.params.is_empty() || !builder.locals.is_empty()
2674 }
2675
2676 fn local_get(
2677 u: &mut Unstructured,
2678 _module: &Module,
2679 builder: &mut CodeBuilder,
2680 instructions: &mut Vec<Instruction>,
2681 ) -> Result<()> {
2682 let num_params = builder.func_ty.params.len();
2683 let n = num_params + builder.locals.len();
2684 debug_assert!(n > 0);
2685 let i = u.int_in_range(0..=n - 1)?;
2686 builder.allocs.operands.push(Some(if i < num_params {
2687 builder.func_ty.params[i]
2688 } else {
2689 builder.locals[i - num_params]
2690 }));
2691 instructions.push(Instruction::LocalGet(i as u32));
2692 Ok(())
2693 }
2694
2695 #[inline]
2696 fn local_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2697 builder
2698 .func_ty
2699 .params
2700 .iter()
2701 .chain(builder.locals.iter())
2702 .any(|ty| builder.type_on_stack(module, *ty))
2703 }
2704
2705 fn local_set(
2706 u: &mut Unstructured,
2707 module: &Module,
2708 builder: &mut CodeBuilder,
2709 instructions: &mut Vec<Instruction>,
2710 ) -> Result<()> {
2711 let n = builder
2712 .func_ty
2713 .params
2714 .iter()
2715 .chain(builder.locals.iter())
2716 .filter(|ty| builder.type_on_stack(module, **ty))
2717 .count();
2718 debug_assert!(n > 0);
2719 let i = u.int_in_range(0..=n - 1)?;
2720 let (j, _) = builder
2721 .func_ty
2722 .params
2723 .iter()
2724 .chain(builder.locals.iter())
2725 .enumerate()
2726 .filter(|(_, ty)| builder.type_on_stack(module, **ty))
2727 .nth(i)
2728 .unwrap();
2729 builder.allocs.operands.pop();
2730 instructions.push(Instruction::LocalSet(j as u32));
2731 Ok(())
2732 }
2733
2734 fn local_tee(
2735 u: &mut Unstructured,
2736 module: &Module,
2737 builder: &mut CodeBuilder,
2738 instructions: &mut Vec<Instruction>,
2739 ) -> Result<()> {
2740 let n = builder
2741 .func_ty
2742 .params
2743 .iter()
2744 .chain(builder.locals.iter())
2745 .filter(|ty| builder.type_on_stack(module, **ty))
2746 .count();
2747 debug_assert!(n > 0);
2748 let i = u.int_in_range(0..=n - 1)?;
2749 let (j, ty) = builder
2750 .func_ty
2751 .params
2752 .iter()
2753 .chain(builder.locals.iter())
2754 .enumerate()
2755 .filter(|(_, ty)| builder.type_on_stack(module, **ty))
2756 .nth(i)
2757 .unwrap();
2758 builder.allocs.operands.pop();
2759 instructions.push(Instruction::LocalTee(j as u32));
2760 builder.push_operand(Some(*ty));
2761 Ok(())
2762 }
2763
2764 #[inline]
2765 fn global_get_valid(module: &Module, _: &mut CodeBuilder) -> bool {
2766 module.globals.len() > 0
2767 }
2768
2769 fn global_get(
2770 u: &mut Unstructured,
2771 module: &Module,
2772 builder: &mut CodeBuilder,
2773 instructions: &mut Vec<Instruction>,
2774 ) -> Result<()> {
2775 debug_assert!(module.globals.len() > 0);
2776 let global_idx = u.int_in_range(0..=module.globals.len() - 1)?;
2777 builder
2778 .allocs
2779 .operands
2780 .push(Some(module.globals[global_idx].val_type));
2781 instructions.push(Instruction::GlobalGet(global_idx as u32));
2782 Ok(())
2783 }
2784
2785 #[inline]
2786 fn global_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2787 builder
2788 .allocs
2789 .mutable_globals
2790 .iter()
2791 .any(|(ty, _)| builder.type_on_stack(module, *ty))
2792 }
2793
2794 fn global_set(
2795 u: &mut Unstructured,
2796 module: &Module,
2797 builder: &mut CodeBuilder,
2798 instructions: &mut Vec<Instruction>,
2799 ) -> Result<()> {
2800 let candidates = builder
2801 .allocs
2802 .mutable_globals
2803 .iter()
2804 .find(|(ty, _)| builder.type_on_stack(module, **ty))
2805 .unwrap()
2806 .1;
2807 let i = u.int_in_range(0..=candidates.len() - 1)?;
2808 builder.allocs.operands.pop();
2809 instructions.push(Instruction::GlobalSet(candidates[i]));
2810 Ok(())
2811 }
2812
2813 #[inline]
2814 fn have_memory(module: &Module, _: &mut CodeBuilder) -> bool {
2815 module.memories.len() > 0
2816 }
2817
2818 #[inline]
2819 fn have_memory_and_offset(module: &Module, builder: &mut CodeBuilder) -> bool {
2820 (builder.allocs.memory32.len() > 0 && builder.type_on_stack(module, ValType::I32))
2821 || (builder.allocs.memory64.len() > 0 && builder.type_on_stack(module, ValType::I64))
2822 }
2823
2824 #[inline]
2825 fn have_data(module: &Module, _: &mut CodeBuilder) -> bool {
2826 module.data.len() > 0
2827 }
2828
2829 fn i32_load(
2830 u: &mut Unstructured,
2831 module: &Module,
2832 builder: &mut CodeBuilder,
2833 instructions: &mut Vec<Instruction>,
2834 ) -> Result<()> {
2835 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
2836 builder.allocs.operands.push(Some(ValType::I32));
2837 if module.config.disallow_traps {
2838 no_traps::load(Instruction::I32Load(memarg), module, builder, instructions);
2839 } else {
2840 instructions.push(Instruction::I32Load(memarg));
2841 }
2842 Ok(())
2843 }
2844
2845 fn i64_load(
2846 u: &mut Unstructured,
2847 module: &Module,
2848 builder: &mut CodeBuilder,
2849 instructions: &mut Vec<Instruction>,
2850 ) -> Result<()> {
2851 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
2852 builder.allocs.operands.push(Some(ValType::I64));
2853 if module.config.disallow_traps {
2854 no_traps::load(Instruction::I64Load(memarg), module, builder, instructions);
2855 } else {
2856 instructions.push(Instruction::I64Load(memarg));
2857 }
2858 Ok(())
2859 }
2860
2861 fn f32_load(
2862 u: &mut Unstructured,
2863 module: &Module,
2864 builder: &mut CodeBuilder,
2865 instructions: &mut Vec<Instruction>,
2866 ) -> Result<()> {
2867 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
2868 builder.allocs.operands.push(Some(ValType::F32));
2869 if module.config.disallow_traps {
2870 no_traps::load(Instruction::F32Load(memarg), module, builder, instructions);
2871 } else {
2872 instructions.push(Instruction::F32Load(memarg));
2873 }
2874 Ok(())
2875 }
2876
2877 fn f64_load(
2878 u: &mut Unstructured,
2879 module: &Module,
2880 builder: &mut CodeBuilder,
2881 instructions: &mut Vec<Instruction>,
2882 ) -> Result<()> {
2883 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
2884 builder.allocs.operands.push(Some(ValType::F64));
2885 if module.config.disallow_traps {
2886 no_traps::load(Instruction::F64Load(memarg), module, builder, instructions);
2887 } else {
2888 instructions.push(Instruction::F64Load(memarg));
2889 }
2890 Ok(())
2891 }
2892
2893 fn i32_load_8_s(
2894 u: &mut Unstructured,
2895 module: &Module,
2896 builder: &mut CodeBuilder,
2897 instructions: &mut Vec<Instruction>,
2898 ) -> Result<()> {
2899 let memarg = mem_arg(u, module, builder, &[0])?;
2900 builder.allocs.operands.push(Some(ValType::I32));
2901 if module.config.disallow_traps {
2902 no_traps::load(
2903 Instruction::I32Load8S(memarg),
2904 module,
2905 builder,
2906 instructions,
2907 );
2908 } else {
2909 instructions.push(Instruction::I32Load8S(memarg));
2910 }
2911 Ok(())
2912 }
2913
2914 fn i32_load_8_u(
2915 u: &mut Unstructured,
2916 module: &Module,
2917 builder: &mut CodeBuilder,
2918 instructions: &mut Vec<Instruction>,
2919 ) -> Result<()> {
2920 let memarg = mem_arg(u, module, builder, &[0])?;
2921 builder.allocs.operands.push(Some(ValType::I32));
2922 if module.config.disallow_traps {
2923 no_traps::load(
2924 Instruction::I32Load8U(memarg),
2925 module,
2926 builder,
2927 instructions,
2928 );
2929 } else {
2930 instructions.push(Instruction::I32Load8U(memarg));
2931 }
2932 Ok(())
2933 }
2934
2935 fn i32_load_16_s(
2936 u: &mut Unstructured,
2937 module: &Module,
2938 builder: &mut CodeBuilder,
2939 instructions: &mut Vec<Instruction>,
2940 ) -> Result<()> {
2941 let memarg = mem_arg(u, module, builder, &[0, 1])?;
2942 builder.allocs.operands.push(Some(ValType::I32));
2943 if module.config.disallow_traps {
2944 no_traps::load(
2945 Instruction::I32Load16S(memarg),
2946 module,
2947 builder,
2948 instructions,
2949 );
2950 } else {
2951 instructions.push(Instruction::I32Load16S(memarg));
2952 }
2953 Ok(())
2954 }
2955
2956 fn i32_load_16_u(
2957 u: &mut Unstructured,
2958 module: &Module,
2959 builder: &mut CodeBuilder,
2960 instructions: &mut Vec<Instruction>,
2961 ) -> Result<()> {
2962 let memarg = mem_arg(u, module, builder, &[0, 1])?;
2963 builder.allocs.operands.push(Some(ValType::I32));
2964 if module.config.disallow_traps {
2965 no_traps::load(
2966 Instruction::I32Load16U(memarg),
2967 module,
2968 builder,
2969 instructions,
2970 );
2971 } else {
2972 instructions.push(Instruction::I32Load16U(memarg));
2973 }
2974 Ok(())
2975 }
2976
2977 fn i64_load_8_s(
2978 u: &mut Unstructured,
2979 module: &Module,
2980 builder: &mut CodeBuilder,
2981 instructions: &mut Vec<Instruction>,
2982 ) -> Result<()> {
2983 let memarg = mem_arg(u, module, builder, &[0])?;
2984 builder.allocs.operands.push(Some(ValType::I64));
2985 if module.config.disallow_traps {
2986 no_traps::load(
2987 Instruction::I64Load8S(memarg),
2988 module,
2989 builder,
2990 instructions,
2991 );
2992 } else {
2993 instructions.push(Instruction::I64Load8S(memarg));
2994 }
2995 Ok(())
2996 }
2997
2998 fn i64_load_16_s(
2999 u: &mut Unstructured,
3000 module: &Module,
3001 builder: &mut CodeBuilder,
3002 instructions: &mut Vec<Instruction>,
3003 ) -> Result<()> {
3004 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3005 builder.allocs.operands.push(Some(ValType::I64));
3006 if module.config.disallow_traps {
3007 no_traps::load(
3008 Instruction::I64Load16S(memarg),
3009 module,
3010 builder,
3011 instructions,
3012 );
3013 } else {
3014 instructions.push(Instruction::I64Load16S(memarg));
3015 }
3016 Ok(())
3017 }
3018
3019 fn i64_load_32_s(
3020 u: &mut Unstructured,
3021 module: &Module,
3022 builder: &mut CodeBuilder,
3023 instructions: &mut Vec<Instruction>,
3024 ) -> Result<()> {
3025 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3026 builder.allocs.operands.push(Some(ValType::I64));
3027 if module.config.disallow_traps {
3028 no_traps::load(
3029 Instruction::I64Load32S(memarg),
3030 module,
3031 builder,
3032 instructions,
3033 );
3034 } else {
3035 instructions.push(Instruction::I64Load32S(memarg));
3036 }
3037 Ok(())
3038 }
3039
3040 fn i64_load_8_u(
3041 u: &mut Unstructured,
3042 module: &Module,
3043 builder: &mut CodeBuilder,
3044 instructions: &mut Vec<Instruction>,
3045 ) -> Result<()> {
3046 let memarg = mem_arg(u, module, builder, &[0])?;
3047 builder.allocs.operands.push(Some(ValType::I64));
3048 if module.config.disallow_traps {
3049 no_traps::load(
3050 Instruction::I64Load8U(memarg),
3051 module,
3052 builder,
3053 instructions,
3054 );
3055 } else {
3056 instructions.push(Instruction::I64Load8U(memarg));
3057 }
3058 Ok(())
3059 }
3060
3061 fn i64_load_16_u(
3062 u: &mut Unstructured,
3063 module: &Module,
3064 builder: &mut CodeBuilder,
3065 instructions: &mut Vec<Instruction>,
3066 ) -> Result<()> {
3067 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3068 builder.allocs.operands.push(Some(ValType::I64));
3069 if module.config.disallow_traps {
3070 no_traps::load(
3071 Instruction::I64Load16U(memarg),
3072 module,
3073 builder,
3074 instructions,
3075 );
3076 } else {
3077 instructions.push(Instruction::I64Load16U(memarg));
3078 }
3079 Ok(())
3080 }
3081
3082 fn i64_load_32_u(
3083 u: &mut Unstructured,
3084 module: &Module,
3085 builder: &mut CodeBuilder,
3086 instructions: &mut Vec<Instruction>,
3087 ) -> Result<()> {
3088 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3089 builder.allocs.operands.push(Some(ValType::I64));
3090 if module.config.disallow_traps {
3091 no_traps::load(
3092 Instruction::I64Load32U(memarg),
3093 module,
3094 builder,
3095 instructions,
3096 );
3097 } else {
3098 instructions.push(Instruction::I64Load32U(memarg));
3099 }
3100 Ok(())
3101 }
3102
3103 #[inline]
3104 fn store_valid(module: &Module, builder: &mut CodeBuilder, f: impl Fn() -> ValType) -> bool {
3105 (builder.allocs.memory32.len() > 0 && builder.types_on_stack(module, &[ValType::I32, f()]))
3106 || (builder.allocs.memory64.len() > 0
3107 && builder.types_on_stack(module, &[ValType::I64, f()]))
3108 }
3109
3110 #[inline]
3111 fn i32_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3112 store_valid(module, builder, || ValType::I32)
3113 }
3114
3115 fn i32_store(
3116 u: &mut Unstructured,
3117 module: &Module,
3118 builder: &mut CodeBuilder,
3119 instructions: &mut Vec<Instruction>,
3120 ) -> Result<()> {
3121 builder.pop_operands(module, &[ValType::I32]);
3122 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3123 if module.config.disallow_traps {
3124 no_traps::store(Instruction::I32Store(memarg), module, builder, instructions);
3125 } else {
3126 instructions.push(Instruction::I32Store(memarg));
3127 }
3128 Ok(())
3129 }
3130
3131 #[inline]
3132 fn i64_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3133 store_valid(module, builder, || ValType::I64)
3134 }
3135
3136 fn i64_store(
3137 u: &mut Unstructured,
3138 module: &Module,
3139 builder: &mut CodeBuilder,
3140 instructions: &mut Vec<Instruction>,
3141 ) -> Result<()> {
3142 builder.pop_operands(module, &[ValType::I64]);
3143 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
3144 if module.config.disallow_traps {
3145 no_traps::store(Instruction::I64Store(memarg), module, builder, instructions);
3146 } else {
3147 instructions.push(Instruction::I64Store(memarg));
3148 }
3149 Ok(())
3150 }
3151
3152 #[inline]
3153 fn f32_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3154 store_valid(module, builder, || ValType::F32)
3155 }
3156
3157 fn f32_store(
3158 u: &mut Unstructured,
3159 module: &Module,
3160 builder: &mut CodeBuilder,
3161 instructions: &mut Vec<Instruction>,
3162 ) -> Result<()> {
3163 builder.pop_operands(module, &[ValType::F32]);
3164 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3165 if module.config.disallow_traps {
3166 no_traps::store(Instruction::F32Store(memarg), module, builder, instructions);
3167 } else {
3168 instructions.push(Instruction::F32Store(memarg));
3169 }
3170 Ok(())
3171 }
3172
3173 #[inline]
3174 fn f64_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3175 store_valid(module, builder, || ValType::F64)
3176 }
3177
3178 fn f64_store(
3179 u: &mut Unstructured,
3180 module: &Module,
3181 builder: &mut CodeBuilder,
3182 instructions: &mut Vec<Instruction>,
3183 ) -> Result<()> {
3184 builder.pop_operands(module, &[ValType::F64]);
3185 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
3186 if module.config.disallow_traps {
3187 no_traps::store(Instruction::F64Store(memarg), module, builder, instructions);
3188 } else {
3189 instructions.push(Instruction::F64Store(memarg));
3190 }
3191 Ok(())
3192 }
3193
3194 fn i32_store_8(
3195 u: &mut Unstructured,
3196 module: &Module,
3197 builder: &mut CodeBuilder,
3198 instructions: &mut Vec<Instruction>,
3199 ) -> Result<()> {
3200 builder.pop_operands(module, &[ValType::I32]);
3201 let memarg = mem_arg(u, module, builder, &[0])?;
3202 if module.config.disallow_traps {
3203 no_traps::store(
3204 Instruction::I32Store8(memarg),
3205 module,
3206 builder,
3207 instructions,
3208 );
3209 } else {
3210 instructions.push(Instruction::I32Store8(memarg));
3211 }
3212 Ok(())
3213 }
3214
3215 fn i32_store_16(
3216 u: &mut Unstructured,
3217 module: &Module,
3218 builder: &mut CodeBuilder,
3219 instructions: &mut Vec<Instruction>,
3220 ) -> Result<()> {
3221 builder.pop_operands(module, &[ValType::I32]);
3222 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3223 if module.config.disallow_traps {
3224 no_traps::store(
3225 Instruction::I32Store16(memarg),
3226 module,
3227 builder,
3228 instructions,
3229 );
3230 } else {
3231 instructions.push(Instruction::I32Store16(memarg));
3232 }
3233 Ok(())
3234 }
3235
3236 fn i64_store_8(
3237 u: &mut Unstructured,
3238 module: &Module,
3239 builder: &mut CodeBuilder,
3240 instructions: &mut Vec<Instruction>,
3241 ) -> Result<()> {
3242 builder.pop_operands(module, &[ValType::I64]);
3243 let memarg = mem_arg(u, module, builder, &[0])?;
3244 if module.config.disallow_traps {
3245 no_traps::store(
3246 Instruction::I64Store8(memarg),
3247 module,
3248 builder,
3249 instructions,
3250 );
3251 } else {
3252 instructions.push(Instruction::I64Store8(memarg));
3253 }
3254 Ok(())
3255 }
3256
3257 fn i64_store_16(
3258 u: &mut Unstructured,
3259 module: &Module,
3260 builder: &mut CodeBuilder,
3261 instructions: &mut Vec<Instruction>,
3262 ) -> Result<()> {
3263 builder.pop_operands(module, &[ValType::I64]);
3264 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3265 if module.config.disallow_traps {
3266 no_traps::store(
3267 Instruction::I64Store16(memarg),
3268 module,
3269 builder,
3270 instructions,
3271 );
3272 } else {
3273 instructions.push(Instruction::I64Store16(memarg));
3274 }
3275 Ok(())
3276 }
3277
3278 fn i64_store_32(
3279 u: &mut Unstructured,
3280 module: &Module,
3281 builder: &mut CodeBuilder,
3282 instructions: &mut Vec<Instruction>,
3283 ) -> Result<()> {
3284 builder.pop_operands(module, &[ValType::I64]);
3285 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3286 if module.config.disallow_traps {
3287 no_traps::store(
3288 Instruction::I64Store32(memarg),
3289 module,
3290 builder,
3291 instructions,
3292 );
3293 } else {
3294 instructions.push(Instruction::I64Store32(memarg));
3295 }
3296 Ok(())
3297 }
3298
3299 fn memory_size(
3300 u: &mut Unstructured,
3301 module: &Module,
3302 builder: &mut CodeBuilder,
3303 instructions: &mut Vec<Instruction>,
3304 ) -> Result<()> {
3305 let i = u.int_in_range(0..=module.memories.len() - 1)?;
3306 let ty = if module.memories[i].memory64 {
3307 ValType::I64
3308 } else {
3309 ValType::I32
3310 };
3311 builder.push_operands(&[ty]);
3312 instructions.push(Instruction::MemorySize(i as u32));
3313 Ok(())
3314 }
3315
3316 #[inline]
3317 fn memory_grow_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3318 (builder.allocs.memory32.len() > 0 && builder.type_on_stack(module, ValType::I32))
3319 || (builder.allocs.memory64.len() > 0 && builder.type_on_stack(module, ValType::I64))
3320 }
3321
3322 fn memory_grow(
3323 u: &mut Unstructured,
3324 module: &Module,
3325 builder: &mut CodeBuilder,
3326 instructions: &mut Vec<Instruction>,
3327 ) -> Result<()> {
3328 let ty = if builder.type_on_stack(module, ValType::I32) {
3329 ValType::I32
3330 } else {
3331 ValType::I64
3332 };
3333 let index = memory_index(u, builder, ty)?;
3334 builder.pop_operands(module, &[ty]);
3335 builder.push_operands(&[ty]);
3336 instructions.push(Instruction::MemoryGrow(index));
3337 Ok(())
3338 }
3339
3340 #[inline]
3341 fn memory_init_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3342 module.config.bulk_memory_enabled
3343 && have_data(module, builder)
3344 && !module.config.disallow_traps // Non-trapping memory init not yet implemented
3345 && (builder.allocs.memory32.len() > 0
3346 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
3347 || (builder.allocs.memory64.len() > 0
3348 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])))
3349 }
3350
3351 fn memory_init(
3352 u: &mut Unstructured,
3353 module: &Module,
3354 builder: &mut CodeBuilder,
3355 instructions: &mut Vec<Instruction>,
3356 ) -> Result<()> {
3357 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3358 let ty = if builder.type_on_stack(module, ValType::I32) {
3359 ValType::I32
3360 } else {
3361 ValType::I64
3362 };
3363 let mem = memory_index(u, builder, ty)?;
3364 let data_index = data_index(u, module)?;
3365 builder.pop_operands(module, &[ty]);
3366 instructions.push(Instruction::MemoryInit { mem, data_index });
3367 Ok(())
3368 }
3369
3370 #[inline]
3371 fn memory_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3372 module.config.bulk_memory_enabled
3373 && !module.config.disallow_traps // Non-trapping memory fill generation not yet implemented
3374 && (builder.allocs.memory32.len() > 0
3375 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
3376 || (builder.allocs.memory64.len() > 0
3377 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I64])))
3378 }
3379
3380 fn memory_fill(
3381 u: &mut Unstructured,
3382 module: &Module,
3383 builder: &mut CodeBuilder,
3384 instructions: &mut Vec<Instruction>,
3385 ) -> Result<()> {
3386 let ty = if builder.type_on_stack(module, ValType::I32) {
3387 ValType::I32
3388 } else {
3389 ValType::I64
3390 };
3391 let mem = memory_index(u, builder, ty)?;
3392 builder.pop_operands(module, &[ty, ValType::I32, ty]);
3393 instructions.push(Instruction::MemoryFill(mem));
3394 Ok(())
3395 }
3396
3397 #[inline]
3398 fn memory_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3399 if !module.config.bulk_memory_enabled {
3400 return false;
3401 }
3402
3403 // The non-trapping case for memory copy has not yet been implemented,
3404 // so we are excluding them for now
3405 if module.config.disallow_traps {
3406 return false;
3407 }
3408
3409 let n32 = builder.allocs.memory32.len();
3410 let n64 = builder.allocs.memory64.len();
3411
3412 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) && n64 > 0 {
3413 return true;
3414 }
3415 if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) && n32 > 0 {
3416 return true;
3417 }
3418 if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
3419 && n32 > 0
3420 && n64 > 0
3421 {
3422 return true;
3423 }
3424 if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32])
3425 && n32 > 0
3426 && n64 > 0
3427 {
3428 return true;
3429 }
3430
3431 false
3432 }
3433
3434 fn memory_copy(
3435 u: &mut Unstructured,
3436 module: &Module,
3437 builder: &mut CodeBuilder,
3438 instructions: &mut Vec<Instruction>,
3439 ) -> Result<()> {
3440 let (src, dst) = gen_copy_src_and_dst(module, builder);
3441 let src_mem = src.choose(u, &builder.allocs.memory32, &builder.allocs.memory64)?;
3442 let dst_mem = dst.choose(u, &builder.allocs.memory32, &builder.allocs.memory64)?;
3443 instructions.push(Instruction::MemoryCopy { dst_mem, src_mem });
3444 Ok(())
3445 }
3446
3447 enum CopyIndexSize {
3448 I32,
3449 I64,
3450 }
3451
3452 impl CopyIndexSize {
3453 fn choose(&self, u: &mut Unstructured<'_>, n32: &[u32], n64: &[u32]) -> Result<u32> {
3454 Ok(match self {
3455 CopyIndexSize::I32 => *u.choose(n32)?,
3456 CopyIndexSize::I64 => *u.choose(n64)?,
3457 })
3458 }
3459 }
3460
3461 fn gen_copy_src_and_dst(
3462 module: &Module,
3463 builder: &mut CodeBuilder,
3464 ) -> (CopyIndexSize, CopyIndexSize) {
3465 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) {
3466 builder.pop_operands(module, &[ValType::I64, ValType::I64, ValType::I64]);
3467 (CopyIndexSize::I64, CopyIndexSize::I64)
3468 } else if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) {
3469 builder.pop_operands(module, &[ValType::I32, ValType::I32, ValType::I32]);
3470 (CopyIndexSize::I32, CopyIndexSize::I32)
3471 } else if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32]) {
3472 builder.pop_operands(module, &[ValType::I64, ValType::I32, ValType::I32]);
3473 (CopyIndexSize::I32, CopyIndexSize::I64)
3474 } else if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32]) {
3475 builder.pop_operands(module, &[ValType::I32, ValType::I64, ValType::I32]);
3476 (CopyIndexSize::I64, CopyIndexSize::I32)
3477 } else {
3478 unreachable!()
3479 }
3480 }
3481
3482 #[inline]
3483 fn data_drop_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3484 have_data(module, builder) && module.config.bulk_memory_enabled
3485 }
3486
3487 fn data_drop(
3488 u: &mut Unstructured,
3489 module: &Module,
3490 _builder: &mut CodeBuilder,
3491 instructions: &mut Vec<Instruction>,
3492 ) -> Result<()> {
3493 instructions.push(Instruction::DataDrop(data_index(u, module)?));
3494 Ok(())
3495 }
3496
3497 fn i32_const(
3498 u: &mut Unstructured,
3499 module: &Module,
3500 builder: &mut CodeBuilder,
3501 instructions: &mut Vec<Instruction>,
3502 ) -> Result<()> {
3503 builder.push_operands(&[ValType::I32]);
3504 instructions.push(module.arbitrary_const_instruction(ValType::I32, u)?);
3505 Ok(())
3506 }
3507
3508 fn i64_const(
3509 u: &mut Unstructured,
3510 module: &Module,
3511 builder: &mut CodeBuilder,
3512 instructions: &mut Vec<Instruction>,
3513 ) -> Result<()> {
3514 builder.push_operands(&[ValType::I64]);
3515 instructions.push(module.arbitrary_const_instruction(ValType::I64, u)?);
3516 Ok(())
3517 }
3518
3519 fn f32_const(
3520 u: &mut Unstructured,
3521 module: &Module,
3522 builder: &mut CodeBuilder,
3523 instructions: &mut Vec<Instruction>,
3524 ) -> Result<()> {
3525 builder.push_operands(&[ValType::F32]);
3526 instructions.push(module.arbitrary_const_instruction(ValType::F32, u)?);
3527 Ok(())
3528 }
3529
3530 fn f64_const(
3531 u: &mut Unstructured,
3532 module: &Module,
3533 builder: &mut CodeBuilder,
3534 instructions: &mut Vec<Instruction>,
3535 ) -> Result<()> {
3536 builder.push_operands(&[ValType::F64]);
3537 instructions.push(module.arbitrary_const_instruction(ValType::F64, u)?);
3538 Ok(())
3539 }
3540
3541 #[inline]
3542 fn i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3543 builder.type_on_stack(module, ValType::I32)
3544 }
3545
3546 fn i32_eqz(
3547 _: &mut Unstructured,
3548 module: &Module,
3549 builder: &mut CodeBuilder,
3550 instructions: &mut Vec<Instruction>,
3551 ) -> Result<()> {
3552 builder.pop_operands(module, &[ValType::I32]);
3553 builder.push_operands(&[ValType::I32]);
3554 instructions.push(Instruction::I32Eqz);
3555 Ok(())
3556 }
3557
3558 #[inline]
3559 fn i32_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3560 builder.types_on_stack(module, &[ValType::I32, ValType::I32])
3561 }
3562
3563 fn i32_eq(
3564 _: &mut Unstructured,
3565 module: &Module,
3566 builder: &mut CodeBuilder,
3567 instructions: &mut Vec<Instruction>,
3568 ) -> Result<()> {
3569 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3570 builder.push_operands(&[ValType::I32]);
3571 instructions.push(Instruction::I32Eq);
3572 Ok(())
3573 }
3574
3575 fn i32_ne(
3576 _: &mut Unstructured,
3577 module: &Module,
3578 builder: &mut CodeBuilder,
3579 instructions: &mut Vec<Instruction>,
3580 ) -> Result<()> {
3581 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3582 builder.push_operands(&[ValType::I32]);
3583 instructions.push(Instruction::I32Ne);
3584 Ok(())
3585 }
3586
3587 fn i32_lt_s(
3588 _: &mut Unstructured,
3589 module: &Module,
3590 builder: &mut CodeBuilder,
3591 instructions: &mut Vec<Instruction>,
3592 ) -> Result<()> {
3593 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3594 builder.push_operands(&[ValType::I32]);
3595 instructions.push(Instruction::I32LtS);
3596 Ok(())
3597 }
3598
3599 fn i32_lt_u(
3600 _: &mut Unstructured,
3601 module: &Module,
3602 builder: &mut CodeBuilder,
3603 instructions: &mut Vec<Instruction>,
3604 ) -> Result<()> {
3605 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3606 builder.push_operands(&[ValType::I32]);
3607 instructions.push(Instruction::I32LtU);
3608 Ok(())
3609 }
3610
3611 fn i32_gt_s(
3612 _: &mut Unstructured,
3613 module: &Module,
3614 builder: &mut CodeBuilder,
3615 instructions: &mut Vec<Instruction>,
3616 ) -> Result<()> {
3617 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3618 builder.push_operands(&[ValType::I32]);
3619 instructions.push(Instruction::I32GtS);
3620 Ok(())
3621 }
3622
3623 fn i32_gt_u(
3624 _: &mut Unstructured,
3625 module: &Module,
3626 builder: &mut CodeBuilder,
3627 instructions: &mut Vec<Instruction>,
3628 ) -> Result<()> {
3629 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3630 builder.push_operands(&[ValType::I32]);
3631 instructions.push(Instruction::I32GtU);
3632 Ok(())
3633 }
3634
3635 fn i32_le_s(
3636 _: &mut Unstructured,
3637 module: &Module,
3638 builder: &mut CodeBuilder,
3639 instructions: &mut Vec<Instruction>,
3640 ) -> Result<()> {
3641 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3642 builder.push_operands(&[ValType::I32]);
3643 instructions.push(Instruction::I32LeS);
3644 Ok(())
3645 }
3646
3647 fn i32_le_u(
3648 _: &mut Unstructured,
3649 module: &Module,
3650 builder: &mut CodeBuilder,
3651 instructions: &mut Vec<Instruction>,
3652 ) -> Result<()> {
3653 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3654 builder.push_operands(&[ValType::I32]);
3655 instructions.push(Instruction::I32LeU);
3656 Ok(())
3657 }
3658
3659 fn i32_ge_s(
3660 _: &mut Unstructured,
3661 module: &Module,
3662 builder: &mut CodeBuilder,
3663 instructions: &mut Vec<Instruction>,
3664 ) -> Result<()> {
3665 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3666 builder.push_operands(&[ValType::I32]);
3667 instructions.push(Instruction::I32GeS);
3668 Ok(())
3669 }
3670
3671 fn i32_ge_u(
3672 _: &mut Unstructured,
3673 module: &Module,
3674 builder: &mut CodeBuilder,
3675 instructions: &mut Vec<Instruction>,
3676 ) -> Result<()> {
3677 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3678 builder.push_operands(&[ValType::I32]);
3679 instructions.push(Instruction::I32GeU);
3680 Ok(())
3681 }
3682
3683 #[inline]
3684 fn i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3685 builder.types_on_stack(module, &[ValType::I64])
3686 }
3687
3688 fn i64_eqz(
3689 _: &mut Unstructured,
3690 module: &Module,
3691 builder: &mut CodeBuilder,
3692 instructions: &mut Vec<Instruction>,
3693 ) -> Result<()> {
3694 builder.pop_operands(module, &[ValType::I64]);
3695 builder.push_operands(&[ValType::I32]);
3696 instructions.push(Instruction::I64Eqz);
3697 Ok(())
3698 }
3699
3700 #[inline]
3701 fn i64_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3702 builder.types_on_stack(module, &[ValType::I64, ValType::I64])
3703 }
3704
3705 fn i64_eq(
3706 _: &mut Unstructured,
3707 module: &Module,
3708 builder: &mut CodeBuilder,
3709 instructions: &mut Vec<Instruction>,
3710 ) -> Result<()> {
3711 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3712 builder.push_operands(&[ValType::I32]);
3713 instructions.push(Instruction::I64Eq);
3714 Ok(())
3715 }
3716
3717 fn i64_ne(
3718 _: &mut Unstructured,
3719 module: &Module,
3720 builder: &mut CodeBuilder,
3721 instructions: &mut Vec<Instruction>,
3722 ) -> Result<()> {
3723 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3724 builder.push_operands(&[ValType::I32]);
3725 instructions.push(Instruction::I64Ne);
3726 Ok(())
3727 }
3728
3729 fn i64_lt_s(
3730 _: &mut Unstructured,
3731 module: &Module,
3732 builder: &mut CodeBuilder,
3733 instructions: &mut Vec<Instruction>,
3734 ) -> Result<()> {
3735 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3736 builder.push_operands(&[ValType::I32]);
3737 instructions.push(Instruction::I64LtS);
3738 Ok(())
3739 }
3740
3741 fn i64_lt_u(
3742 _: &mut Unstructured,
3743 module: &Module,
3744 builder: &mut CodeBuilder,
3745 instructions: &mut Vec<Instruction>,
3746 ) -> Result<()> {
3747 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3748 builder.push_operands(&[ValType::I32]);
3749 instructions.push(Instruction::I64LtU);
3750 Ok(())
3751 }
3752
3753 fn i64_gt_s(
3754 _: &mut Unstructured,
3755 module: &Module,
3756 builder: &mut CodeBuilder,
3757 instructions: &mut Vec<Instruction>,
3758 ) -> Result<()> {
3759 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3760 builder.push_operands(&[ValType::I32]);
3761 instructions.push(Instruction::I64GtS);
3762 Ok(())
3763 }
3764
3765 fn i64_gt_u(
3766 _: &mut Unstructured,
3767 module: &Module,
3768 builder: &mut CodeBuilder,
3769 instructions: &mut Vec<Instruction>,
3770 ) -> Result<()> {
3771 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3772 builder.push_operands(&[ValType::I32]);
3773 instructions.push(Instruction::I64GtU);
3774 Ok(())
3775 }
3776
3777 fn i64_le_s(
3778 _: &mut Unstructured,
3779 module: &Module,
3780 builder: &mut CodeBuilder,
3781 instructions: &mut Vec<Instruction>,
3782 ) -> Result<()> {
3783 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3784 builder.push_operands(&[ValType::I32]);
3785 instructions.push(Instruction::I64LeS);
3786 Ok(())
3787 }
3788
3789 fn i64_le_u(
3790 _: &mut Unstructured,
3791 module: &Module,
3792 builder: &mut CodeBuilder,
3793 instructions: &mut Vec<Instruction>,
3794 ) -> Result<()> {
3795 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3796 builder.push_operands(&[ValType::I32]);
3797 instructions.push(Instruction::I64LeU);
3798 Ok(())
3799 }
3800
3801 fn i64_ge_s(
3802 _: &mut Unstructured,
3803 module: &Module,
3804 builder: &mut CodeBuilder,
3805 instructions: &mut Vec<Instruction>,
3806 ) -> Result<()> {
3807 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3808 builder.push_operands(&[ValType::I32]);
3809 instructions.push(Instruction::I64GeS);
3810 Ok(())
3811 }
3812
3813 fn i64_ge_u(
3814 _: &mut Unstructured,
3815 module: &Module,
3816 builder: &mut CodeBuilder,
3817 instructions: &mut Vec<Instruction>,
3818 ) -> Result<()> {
3819 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3820 builder.push_operands(&[ValType::I32]);
3821 instructions.push(Instruction::I64GeU);
3822 Ok(())
3823 }
3824
3825 fn f32_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3826 builder.types_on_stack(module, &[ValType::F32, ValType::F32])
3827 }
3828
3829 fn f32_eq(
3830 _: &mut Unstructured,
3831 module: &Module,
3832 builder: &mut CodeBuilder,
3833 instructions: &mut Vec<Instruction>,
3834 ) -> Result<()> {
3835 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3836 builder.push_operands(&[ValType::I32]);
3837 instructions.push(Instruction::F32Eq);
3838 Ok(())
3839 }
3840
3841 fn f32_ne(
3842 _: &mut Unstructured,
3843 module: &Module,
3844 builder: &mut CodeBuilder,
3845 instructions: &mut Vec<Instruction>,
3846 ) -> Result<()> {
3847 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3848 builder.push_operands(&[ValType::I32]);
3849 instructions.push(Instruction::F32Ne);
3850 Ok(())
3851 }
3852
3853 fn f32_lt(
3854 _: &mut Unstructured,
3855 module: &Module,
3856 builder: &mut CodeBuilder,
3857 instructions: &mut Vec<Instruction>,
3858 ) -> Result<()> {
3859 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3860 builder.push_operands(&[ValType::I32]);
3861 instructions.push(Instruction::F32Lt);
3862 Ok(())
3863 }
3864
3865 fn f32_gt(
3866 _: &mut Unstructured,
3867 module: &Module,
3868 builder: &mut CodeBuilder,
3869 instructions: &mut Vec<Instruction>,
3870 ) -> Result<()> {
3871 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3872 builder.push_operands(&[ValType::I32]);
3873 instructions.push(Instruction::F32Gt);
3874 Ok(())
3875 }
3876
3877 fn f32_le(
3878 _: &mut Unstructured,
3879 module: &Module,
3880 builder: &mut CodeBuilder,
3881 instructions: &mut Vec<Instruction>,
3882 ) -> Result<()> {
3883 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3884 builder.push_operands(&[ValType::I32]);
3885 instructions.push(Instruction::F32Le);
3886 Ok(())
3887 }
3888
3889 fn f32_ge(
3890 _: &mut Unstructured,
3891 module: &Module,
3892 builder: &mut CodeBuilder,
3893 instructions: &mut Vec<Instruction>,
3894 ) -> Result<()> {
3895 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3896 builder.push_operands(&[ValType::I32]);
3897 instructions.push(Instruction::F32Ge);
3898 Ok(())
3899 }
3900
3901 fn f64_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3902 builder.types_on_stack(module, &[ValType::F64, ValType::F64])
3903 }
3904
3905 fn f64_eq(
3906 _: &mut Unstructured,
3907 module: &Module,
3908 builder: &mut CodeBuilder,
3909 instructions: &mut Vec<Instruction>,
3910 ) -> Result<()> {
3911 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3912 builder.push_operands(&[ValType::I32]);
3913 instructions.push(Instruction::F64Eq);
3914 Ok(())
3915 }
3916
3917 fn f64_ne(
3918 _: &mut Unstructured,
3919 module: &Module,
3920 builder: &mut CodeBuilder,
3921 instructions: &mut Vec<Instruction>,
3922 ) -> Result<()> {
3923 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3924 builder.push_operands(&[ValType::I32]);
3925 instructions.push(Instruction::F64Ne);
3926 Ok(())
3927 }
3928
3929 fn f64_lt(
3930 _: &mut Unstructured,
3931 module: &Module,
3932 builder: &mut CodeBuilder,
3933 instructions: &mut Vec<Instruction>,
3934 ) -> Result<()> {
3935 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3936 builder.push_operands(&[ValType::I32]);
3937 instructions.push(Instruction::F64Lt);
3938 Ok(())
3939 }
3940
3941 fn f64_gt(
3942 _: &mut Unstructured,
3943 module: &Module,
3944 builder: &mut CodeBuilder,
3945 instructions: &mut Vec<Instruction>,
3946 ) -> Result<()> {
3947 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3948 builder.push_operands(&[ValType::I32]);
3949 instructions.push(Instruction::F64Gt);
3950 Ok(())
3951 }
3952
3953 fn f64_le(
3954 _: &mut Unstructured,
3955 module: &Module,
3956 builder: &mut CodeBuilder,
3957 instructions: &mut Vec<Instruction>,
3958 ) -> Result<()> {
3959 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3960 builder.push_operands(&[ValType::I32]);
3961 instructions.push(Instruction::F64Le);
3962 Ok(())
3963 }
3964
3965 fn f64_ge(
3966 _: &mut Unstructured,
3967 module: &Module,
3968 builder: &mut CodeBuilder,
3969 instructions: &mut Vec<Instruction>,
3970 ) -> Result<()> {
3971 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3972 builder.push_operands(&[ValType::I32]);
3973 instructions.push(Instruction::F64Ge);
3974 Ok(())
3975 }
3976
3977 fn i32_clz(
3978 _: &mut Unstructured,
3979 module: &Module,
3980 builder: &mut CodeBuilder,
3981 instructions: &mut Vec<Instruction>,
3982 ) -> Result<()> {
3983 builder.pop_operands(module, &[ValType::I32]);
3984 builder.push_operands(&[ValType::I32]);
3985 instructions.push(Instruction::I32Clz);
3986 Ok(())
3987 }
3988
3989 fn i32_ctz(
3990 _: &mut Unstructured,
3991 module: &Module,
3992 builder: &mut CodeBuilder,
3993 instructions: &mut Vec<Instruction>,
3994 ) -> Result<()> {
3995 builder.pop_operands(module, &[ValType::I32]);
3996 builder.push_operands(&[ValType::I32]);
3997 instructions.push(Instruction::I32Ctz);
3998 Ok(())
3999 }
4000
4001 fn i32_popcnt(
4002 _: &mut Unstructured,
4003 module: &Module,
4004 builder: &mut CodeBuilder,
4005 instructions: &mut Vec<Instruction>,
4006 ) -> Result<()> {
4007 builder.pop_operands(module, &[ValType::I32]);
4008 builder.push_operands(&[ValType::I32]);
4009 instructions.push(Instruction::I32Popcnt);
4010 Ok(())
4011 }
4012
4013 fn i32_add(
4014 _: &mut Unstructured,
4015 module: &Module,
4016 builder: &mut CodeBuilder,
4017 instructions: &mut Vec<Instruction>,
4018 ) -> Result<()> {
4019 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4020 builder.push_operands(&[ValType::I32]);
4021 instructions.push(Instruction::I32Add);
4022 Ok(())
4023 }
4024
4025 fn i32_sub(
4026 _: &mut Unstructured,
4027 module: &Module,
4028 builder: &mut CodeBuilder,
4029 instructions: &mut Vec<Instruction>,
4030 ) -> Result<()> {
4031 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4032 builder.push_operands(&[ValType::I32]);
4033 instructions.push(Instruction::I32Sub);
4034 Ok(())
4035 }
4036
4037 fn i32_mul(
4038 _: &mut Unstructured,
4039 module: &Module,
4040 builder: &mut CodeBuilder,
4041 instructions: &mut Vec<Instruction>,
4042 ) -> Result<()> {
4043 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4044 builder.push_operands(&[ValType::I32]);
4045 instructions.push(Instruction::I32Mul);
4046 Ok(())
4047 }
4048
4049 fn i32_div_s(
4050 _: &mut Unstructured,
4051 module: &Module,
4052 builder: &mut CodeBuilder,
4053 instructions: &mut Vec<Instruction>,
4054 ) -> Result<()> {
4055 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4056 builder.push_operands(&[ValType::I32]);
4057 if module.config.disallow_traps {
4058 no_traps::signed_div_rem(Instruction::I32DivS, builder, instructions);
4059 } else {
4060 instructions.push(Instruction::I32DivS);
4061 }
4062 Ok(())
4063 }
4064
4065 fn i32_div_u(
4066 _: &mut Unstructured,
4067 module: &Module,
4068 builder: &mut CodeBuilder,
4069 instructions: &mut Vec<Instruction>,
4070 ) -> Result<()> {
4071 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4072 builder.push_operands(&[ValType::I32]);
4073 if module.config.disallow_traps {
4074 no_traps::unsigned_div_rem(Instruction::I32DivU, builder, instructions);
4075 } else {
4076 instructions.push(Instruction::I32DivU);
4077 }
4078 Ok(())
4079 }
4080
4081 fn i32_rem_s(
4082 _: &mut Unstructured,
4083 module: &Module,
4084 builder: &mut CodeBuilder,
4085 instructions: &mut Vec<Instruction>,
4086 ) -> Result<()> {
4087 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4088 builder.push_operands(&[ValType::I32]);
4089 if module.config.disallow_traps {
4090 no_traps::signed_div_rem(Instruction::I32RemS, builder, instructions);
4091 } else {
4092 instructions.push(Instruction::I32RemS);
4093 }
4094 Ok(())
4095 }
4096
4097 fn i32_rem_u(
4098 _: &mut Unstructured,
4099 module: &Module,
4100 builder: &mut CodeBuilder,
4101 instructions: &mut Vec<Instruction>,
4102 ) -> Result<()> {
4103 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4104 builder.push_operands(&[ValType::I32]);
4105 if module.config.disallow_traps {
4106 no_traps::unsigned_div_rem(Instruction::I32RemU, builder, instructions);
4107 } else {
4108 instructions.push(Instruction::I32RemU);
4109 }
4110 Ok(())
4111 }
4112
4113 fn i32_and(
4114 _: &mut Unstructured,
4115 module: &Module,
4116 builder: &mut CodeBuilder,
4117 instructions: &mut Vec<Instruction>,
4118 ) -> Result<()> {
4119 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4120 builder.push_operands(&[ValType::I32]);
4121 instructions.push(Instruction::I32And);
4122 Ok(())
4123 }
4124
4125 fn i32_or(
4126 _: &mut Unstructured,
4127 module: &Module,
4128 builder: &mut CodeBuilder,
4129 instructions: &mut Vec<Instruction>,
4130 ) -> Result<()> {
4131 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4132 builder.push_operands(&[ValType::I32]);
4133 instructions.push(Instruction::I32Or);
4134 Ok(())
4135 }
4136
4137 fn i32_xor(
4138 _: &mut Unstructured,
4139 module: &Module,
4140 builder: &mut CodeBuilder,
4141 instructions: &mut Vec<Instruction>,
4142 ) -> Result<()> {
4143 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4144 builder.push_operands(&[ValType::I32]);
4145 instructions.push(Instruction::I32Xor);
4146 Ok(())
4147 }
4148
4149 fn i32_shl(
4150 _: &mut Unstructured,
4151 module: &Module,
4152 builder: &mut CodeBuilder,
4153 instructions: &mut Vec<Instruction>,
4154 ) -> Result<()> {
4155 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4156 builder.push_operands(&[ValType::I32]);
4157 instructions.push(Instruction::I32Shl);
4158 Ok(())
4159 }
4160
4161 fn i32_shr_s(
4162 _: &mut Unstructured,
4163 module: &Module,
4164 builder: &mut CodeBuilder,
4165 instructions: &mut Vec<Instruction>,
4166 ) -> Result<()> {
4167 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4168 builder.push_operands(&[ValType::I32]);
4169 instructions.push(Instruction::I32ShrS);
4170 Ok(())
4171 }
4172
4173 fn i32_shr_u(
4174 _: &mut Unstructured,
4175 module: &Module,
4176 builder: &mut CodeBuilder,
4177 instructions: &mut Vec<Instruction>,
4178 ) -> Result<()> {
4179 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4180 builder.push_operands(&[ValType::I32]);
4181 instructions.push(Instruction::I32ShrU);
4182 Ok(())
4183 }
4184
4185 fn i32_rotl(
4186 _: &mut Unstructured,
4187 module: &Module,
4188 builder: &mut CodeBuilder,
4189 instructions: &mut Vec<Instruction>,
4190 ) -> Result<()> {
4191 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4192 builder.push_operands(&[ValType::I32]);
4193 instructions.push(Instruction::I32Rotl);
4194 Ok(())
4195 }
4196
4197 fn i32_rotr(
4198 _: &mut Unstructured,
4199 module: &Module,
4200 builder: &mut CodeBuilder,
4201 instructions: &mut Vec<Instruction>,
4202 ) -> Result<()> {
4203 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4204 builder.push_operands(&[ValType::I32]);
4205 instructions.push(Instruction::I32Rotr);
4206 Ok(())
4207 }
4208
4209 fn i64_clz(
4210 _: &mut Unstructured,
4211 module: &Module,
4212 builder: &mut CodeBuilder,
4213 instructions: &mut Vec<Instruction>,
4214 ) -> Result<()> {
4215 builder.pop_operands(module, &[ValType::I64]);
4216 builder.push_operands(&[ValType::I64]);
4217 instructions.push(Instruction::I64Clz);
4218 Ok(())
4219 }
4220
4221 fn i64_ctz(
4222 _: &mut Unstructured,
4223 module: &Module,
4224 builder: &mut CodeBuilder,
4225 instructions: &mut Vec<Instruction>,
4226 ) -> Result<()> {
4227 builder.pop_operands(module, &[ValType::I64]);
4228 builder.push_operands(&[ValType::I64]);
4229 instructions.push(Instruction::I64Ctz);
4230 Ok(())
4231 }
4232
4233 fn i64_popcnt(
4234 _: &mut Unstructured,
4235 module: &Module,
4236 builder: &mut CodeBuilder,
4237 instructions: &mut Vec<Instruction>,
4238 ) -> Result<()> {
4239 builder.pop_operands(module, &[ValType::I64]);
4240 builder.push_operands(&[ValType::I64]);
4241 instructions.push(Instruction::I64Popcnt);
4242 Ok(())
4243 }
4244
4245 fn i64_add(
4246 _: &mut Unstructured,
4247 module: &Module,
4248 builder: &mut CodeBuilder,
4249 instructions: &mut Vec<Instruction>,
4250 ) -> Result<()> {
4251 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4252 builder.push_operands(&[ValType::I64]);
4253 instructions.push(Instruction::I64Add);
4254 Ok(())
4255 }
4256
4257 fn i64_sub(
4258 _: &mut Unstructured,
4259 module: &Module,
4260 builder: &mut CodeBuilder,
4261 instructions: &mut Vec<Instruction>,
4262 ) -> Result<()> {
4263 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4264 builder.push_operands(&[ValType::I64]);
4265 instructions.push(Instruction::I64Sub);
4266 Ok(())
4267 }
4268
4269 fn i64_mul(
4270 _: &mut Unstructured,
4271 module: &Module,
4272 builder: &mut CodeBuilder,
4273 instructions: &mut Vec<Instruction>,
4274 ) -> Result<()> {
4275 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4276 builder.push_operands(&[ValType::I64]);
4277 instructions.push(Instruction::I64Mul);
4278 Ok(())
4279 }
4280
4281 fn i64_div_s(
4282 _: &mut Unstructured,
4283 module: &Module,
4284 builder: &mut CodeBuilder,
4285 instructions: &mut Vec<Instruction>,
4286 ) -> Result<()> {
4287 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4288 builder.push_operands(&[ValType::I64]);
4289 if module.config.disallow_traps {
4290 no_traps::signed_div_rem(Instruction::I64DivS, builder, instructions);
4291 } else {
4292 instructions.push(Instruction::I64DivS);
4293 }
4294 Ok(())
4295 }
4296
4297 fn i64_div_u(
4298 _: &mut Unstructured,
4299 module: &Module,
4300 builder: &mut CodeBuilder,
4301 instructions: &mut Vec<Instruction>,
4302 ) -> Result<()> {
4303 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4304 builder.push_operands(&[ValType::I64]);
4305 if module.config.disallow_traps {
4306 no_traps::unsigned_div_rem(Instruction::I64DivU, builder, instructions);
4307 } else {
4308 instructions.push(Instruction::I64DivU);
4309 }
4310 Ok(())
4311 }
4312
4313 fn i64_rem_s(
4314 _: &mut Unstructured,
4315 module: &Module,
4316 builder: &mut CodeBuilder,
4317 instructions: &mut Vec<Instruction>,
4318 ) -> Result<()> {
4319 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4320 builder.push_operands(&[ValType::I64]);
4321 if module.config.disallow_traps {
4322 no_traps::signed_div_rem(Instruction::I64RemS, builder, instructions);
4323 } else {
4324 instructions.push(Instruction::I64RemS);
4325 }
4326 Ok(())
4327 }
4328
4329 fn i64_rem_u(
4330 _: &mut Unstructured,
4331 module: &Module,
4332 builder: &mut CodeBuilder,
4333 instructions: &mut Vec<Instruction>,
4334 ) -> Result<()> {
4335 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4336 builder.push_operands(&[ValType::I64]);
4337 if module.config.disallow_traps {
4338 no_traps::unsigned_div_rem(Instruction::I64RemU, builder, instructions);
4339 } else {
4340 instructions.push(Instruction::I64RemU);
4341 }
4342 Ok(())
4343 }
4344
4345 fn i64_and(
4346 _: &mut Unstructured,
4347 module: &Module,
4348 builder: &mut CodeBuilder,
4349 instructions: &mut Vec<Instruction>,
4350 ) -> Result<()> {
4351 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4352 builder.push_operands(&[ValType::I64]);
4353 instructions.push(Instruction::I64And);
4354 Ok(())
4355 }
4356
4357 fn i64_or(
4358 _: &mut Unstructured,
4359 module: &Module,
4360 builder: &mut CodeBuilder,
4361 instructions: &mut Vec<Instruction>,
4362 ) -> Result<()> {
4363 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4364 builder.push_operands(&[ValType::I64]);
4365 instructions.push(Instruction::I64Or);
4366 Ok(())
4367 }
4368
4369 fn i64_xor(
4370 _: &mut Unstructured,
4371 module: &Module,
4372 builder: &mut CodeBuilder,
4373 instructions: &mut Vec<Instruction>,
4374 ) -> Result<()> {
4375 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4376 builder.push_operands(&[ValType::I64]);
4377 instructions.push(Instruction::I64Xor);
4378 Ok(())
4379 }
4380
4381 fn i64_shl(
4382 _: &mut Unstructured,
4383 module: &Module,
4384 builder: &mut CodeBuilder,
4385 instructions: &mut Vec<Instruction>,
4386 ) -> Result<()> {
4387 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4388 builder.push_operands(&[ValType::I64]);
4389 instructions.push(Instruction::I64Shl);
4390 Ok(())
4391 }
4392
4393 fn i64_shr_s(
4394 _: &mut Unstructured,
4395 module: &Module,
4396 builder: &mut CodeBuilder,
4397 instructions: &mut Vec<Instruction>,
4398 ) -> Result<()> {
4399 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4400 builder.push_operands(&[ValType::I64]);
4401 instructions.push(Instruction::I64ShrS);
4402 Ok(())
4403 }
4404
4405 fn i64_shr_u(
4406 _: &mut Unstructured,
4407 module: &Module,
4408 builder: &mut CodeBuilder,
4409 instructions: &mut Vec<Instruction>,
4410 ) -> Result<()> {
4411 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4412 builder.push_operands(&[ValType::I64]);
4413 instructions.push(Instruction::I64ShrU);
4414 Ok(())
4415 }
4416
4417 fn i64_rotl(
4418 _: &mut Unstructured,
4419 module: &Module,
4420 builder: &mut CodeBuilder,
4421 instructions: &mut Vec<Instruction>,
4422 ) -> Result<()> {
4423 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4424 builder.push_operands(&[ValType::I64]);
4425 instructions.push(Instruction::I64Rotl);
4426 Ok(())
4427 }
4428
4429 fn i64_rotr(
4430 _: &mut Unstructured,
4431 module: &Module,
4432 builder: &mut CodeBuilder,
4433 instructions: &mut Vec<Instruction>,
4434 ) -> Result<()> {
4435 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4436 builder.push_operands(&[ValType::I64]);
4437 instructions.push(Instruction::I64Rotr);
4438 Ok(())
4439 }
4440
4441 #[inline]
4442 fn f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4443 builder.types_on_stack(module, &[ValType::F32])
4444 }
4445
4446 fn f32_abs(
4447 _: &mut Unstructured,
4448 module: &Module,
4449 builder: &mut CodeBuilder,
4450 instructions: &mut Vec<Instruction>,
4451 ) -> Result<()> {
4452 builder.pop_operands(module, &[ValType::F32]);
4453 builder.push_operands(&[ValType::F32]);
4454 instructions.push(Instruction::F32Abs);
4455 Ok(())
4456 }
4457
4458 fn f32_neg(
4459 _: &mut Unstructured,
4460 module: &Module,
4461 builder: &mut CodeBuilder,
4462 instructions: &mut Vec<Instruction>,
4463 ) -> Result<()> {
4464 builder.pop_operands(module, &[ValType::F32]);
4465 builder.push_operands(&[ValType::F32]);
4466 instructions.push(Instruction::F32Neg);
4467 Ok(())
4468 }
4469
4470 fn f32_ceil(
4471 _: &mut Unstructured,
4472 module: &Module,
4473 builder: &mut CodeBuilder,
4474 instructions: &mut Vec<Instruction>,
4475 ) -> Result<()> {
4476 builder.pop_operands(module, &[ValType::F32]);
4477 builder.push_operands(&[ValType::F32]);
4478 instructions.push(Instruction::F32Ceil);
4479 Ok(())
4480 }
4481
4482 fn f32_floor(
4483 _: &mut Unstructured,
4484 module: &Module,
4485 builder: &mut CodeBuilder,
4486 instructions: &mut Vec<Instruction>,
4487 ) -> Result<()> {
4488 builder.pop_operands(module, &[ValType::F32]);
4489 builder.push_operands(&[ValType::F32]);
4490 instructions.push(Instruction::F32Floor);
4491 Ok(())
4492 }
4493
4494 fn f32_trunc(
4495 _: &mut Unstructured,
4496 module: &Module,
4497 builder: &mut CodeBuilder,
4498 instructions: &mut Vec<Instruction>,
4499 ) -> Result<()> {
4500 builder.pop_operands(module, &[ValType::F32]);
4501 builder.push_operands(&[ValType::F32]);
4502 instructions.push(Instruction::F32Trunc);
4503 Ok(())
4504 }
4505
4506 fn f32_nearest(
4507 _: &mut Unstructured,
4508 module: &Module,
4509 builder: &mut CodeBuilder,
4510 instructions: &mut Vec<Instruction>,
4511 ) -> Result<()> {
4512 builder.pop_operands(module, &[ValType::F32]);
4513 builder.push_operands(&[ValType::F32]);
4514 instructions.push(Instruction::F32Nearest);
4515 Ok(())
4516 }
4517
4518 fn f32_sqrt(
4519 _: &mut Unstructured,
4520 module: &Module,
4521 builder: &mut CodeBuilder,
4522 instructions: &mut Vec<Instruction>,
4523 ) -> Result<()> {
4524 builder.pop_operands(module, &[ValType::F32]);
4525 builder.push_operands(&[ValType::F32]);
4526 instructions.push(Instruction::F32Sqrt);
4527 Ok(())
4528 }
4529
4530 fn f32_add(
4531 _: &mut Unstructured,
4532 module: &Module,
4533 builder: &mut CodeBuilder,
4534 instructions: &mut Vec<Instruction>,
4535 ) -> Result<()> {
4536 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4537 builder.push_operands(&[ValType::F32]);
4538 instructions.push(Instruction::F32Add);
4539 Ok(())
4540 }
4541
4542 fn f32_sub(
4543 _: &mut Unstructured,
4544 module: &Module,
4545 builder: &mut CodeBuilder,
4546 instructions: &mut Vec<Instruction>,
4547 ) -> Result<()> {
4548 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4549 builder.push_operands(&[ValType::F32]);
4550 instructions.push(Instruction::F32Sub);
4551 Ok(())
4552 }
4553
4554 fn f32_mul(
4555 _: &mut Unstructured,
4556 module: &Module,
4557 builder: &mut CodeBuilder,
4558 instructions: &mut Vec<Instruction>,
4559 ) -> Result<()> {
4560 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4561 builder.push_operands(&[ValType::F32]);
4562 instructions.push(Instruction::F32Mul);
4563 Ok(())
4564 }
4565
4566 fn f32_div(
4567 _: &mut Unstructured,
4568 module: &Module,
4569 builder: &mut CodeBuilder,
4570 instructions: &mut Vec<Instruction>,
4571 ) -> Result<()> {
4572 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4573 builder.push_operands(&[ValType::F32]);
4574 instructions.push(Instruction::F32Div);
4575 Ok(())
4576 }
4577
4578 fn f32_min(
4579 _: &mut Unstructured,
4580 module: &Module,
4581 builder: &mut CodeBuilder,
4582 instructions: &mut Vec<Instruction>,
4583 ) -> Result<()> {
4584 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4585 builder.push_operands(&[ValType::F32]);
4586 instructions.push(Instruction::F32Min);
4587 Ok(())
4588 }
4589
4590 fn f32_max(
4591 _: &mut Unstructured,
4592 module: &Module,
4593 builder: &mut CodeBuilder,
4594 instructions: &mut Vec<Instruction>,
4595 ) -> Result<()> {
4596 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4597 builder.push_operands(&[ValType::F32]);
4598 instructions.push(Instruction::F32Max);
4599 Ok(())
4600 }
4601
4602 fn f32_copysign(
4603 _: &mut Unstructured,
4604 module: &Module,
4605 builder: &mut CodeBuilder,
4606 instructions: &mut Vec<Instruction>,
4607 ) -> Result<()> {
4608 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4609 builder.push_operands(&[ValType::F32]);
4610 instructions.push(Instruction::F32Copysign);
4611 Ok(())
4612 }
4613
4614 #[inline]
4615 fn f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4616 builder.types_on_stack(module, &[ValType::F64])
4617 }
4618
4619 fn f64_abs(
4620 _: &mut Unstructured,
4621 module: &Module,
4622 builder: &mut CodeBuilder,
4623 instructions: &mut Vec<Instruction>,
4624 ) -> Result<()> {
4625 builder.pop_operands(module, &[ValType::F64]);
4626 builder.push_operands(&[ValType::F64]);
4627 instructions.push(Instruction::F64Abs);
4628 Ok(())
4629 }
4630
4631 fn f64_neg(
4632 _: &mut Unstructured,
4633 module: &Module,
4634 builder: &mut CodeBuilder,
4635 instructions: &mut Vec<Instruction>,
4636 ) -> Result<()> {
4637 builder.pop_operands(module, &[ValType::F64]);
4638 builder.push_operands(&[ValType::F64]);
4639 instructions.push(Instruction::F64Neg);
4640 Ok(())
4641 }
4642
4643 fn f64_ceil(
4644 _: &mut Unstructured,
4645 module: &Module,
4646 builder: &mut CodeBuilder,
4647 instructions: &mut Vec<Instruction>,
4648 ) -> Result<()> {
4649 builder.pop_operands(module, &[ValType::F64]);
4650 builder.push_operands(&[ValType::F64]);
4651 instructions.push(Instruction::F64Ceil);
4652 Ok(())
4653 }
4654
4655 fn f64_floor(
4656 _: &mut Unstructured,
4657 module: &Module,
4658 builder: &mut CodeBuilder,
4659 instructions: &mut Vec<Instruction>,
4660 ) -> Result<()> {
4661 builder.pop_operands(module, &[ValType::F64]);
4662 builder.push_operands(&[ValType::F64]);
4663 instructions.push(Instruction::F64Floor);
4664 Ok(())
4665 }
4666
4667 fn f64_trunc(
4668 _: &mut Unstructured,
4669 module: &Module,
4670 builder: &mut CodeBuilder,
4671 instructions: &mut Vec<Instruction>,
4672 ) -> Result<()> {
4673 builder.pop_operands(module, &[ValType::F64]);
4674 builder.push_operands(&[ValType::F64]);
4675 instructions.push(Instruction::F64Trunc);
4676 Ok(())
4677 }
4678
4679 fn f64_nearest(
4680 _: &mut Unstructured,
4681 module: &Module,
4682 builder: &mut CodeBuilder,
4683 instructions: &mut Vec<Instruction>,
4684 ) -> Result<()> {
4685 builder.pop_operands(module, &[ValType::F64]);
4686 builder.push_operands(&[ValType::F64]);
4687 instructions.push(Instruction::F64Nearest);
4688 Ok(())
4689 }
4690
4691 fn f64_sqrt(
4692 _: &mut Unstructured,
4693 module: &Module,
4694 builder: &mut CodeBuilder,
4695 instructions: &mut Vec<Instruction>,
4696 ) -> Result<()> {
4697 builder.pop_operands(module, &[ValType::F64]);
4698 builder.push_operands(&[ValType::F64]);
4699 instructions.push(Instruction::F64Sqrt);
4700 Ok(())
4701 }
4702
4703 fn f64_add(
4704 _: &mut Unstructured,
4705 module: &Module,
4706 builder: &mut CodeBuilder,
4707 instructions: &mut Vec<Instruction>,
4708 ) -> Result<()> {
4709 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4710 builder.push_operands(&[ValType::F64]);
4711 instructions.push(Instruction::F64Add);
4712 Ok(())
4713 }
4714
4715 fn f64_sub(
4716 _: &mut Unstructured,
4717 module: &Module,
4718 builder: &mut CodeBuilder,
4719 instructions: &mut Vec<Instruction>,
4720 ) -> Result<()> {
4721 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4722 builder.push_operands(&[ValType::F64]);
4723 instructions.push(Instruction::F64Sub);
4724 Ok(())
4725 }
4726
4727 fn f64_mul(
4728 _: &mut Unstructured,
4729 module: &Module,
4730 builder: &mut CodeBuilder,
4731 instructions: &mut Vec<Instruction>,
4732 ) -> Result<()> {
4733 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4734 builder.push_operands(&[ValType::F64]);
4735 instructions.push(Instruction::F64Mul);
4736 Ok(())
4737 }
4738
4739 fn f64_div(
4740 _: &mut Unstructured,
4741 module: &Module,
4742 builder: &mut CodeBuilder,
4743 instructions: &mut Vec<Instruction>,
4744 ) -> Result<()> {
4745 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4746 builder.push_operands(&[ValType::F64]);
4747 instructions.push(Instruction::F64Div);
4748 Ok(())
4749 }
4750
4751 fn f64_min(
4752 _: &mut Unstructured,
4753 module: &Module,
4754 builder: &mut CodeBuilder,
4755 instructions: &mut Vec<Instruction>,
4756 ) -> Result<()> {
4757 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4758 builder.push_operands(&[ValType::F64]);
4759 instructions.push(Instruction::F64Min);
4760 Ok(())
4761 }
4762
4763 fn f64_max(
4764 _: &mut Unstructured,
4765 module: &Module,
4766 builder: &mut CodeBuilder,
4767 instructions: &mut Vec<Instruction>,
4768 ) -> Result<()> {
4769 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4770 builder.push_operands(&[ValType::F64]);
4771 instructions.push(Instruction::F64Max);
4772 Ok(())
4773 }
4774
4775 fn f64_copysign(
4776 _: &mut Unstructured,
4777 module: &Module,
4778 builder: &mut CodeBuilder,
4779 instructions: &mut Vec<Instruction>,
4780 ) -> Result<()> {
4781 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4782 builder.push_operands(&[ValType::F64]);
4783 instructions.push(Instruction::F64Copysign);
4784 Ok(())
4785 }
4786
4787 fn i32_wrap_i64(
4788 _: &mut Unstructured,
4789 module: &Module,
4790 builder: &mut CodeBuilder,
4791 instructions: &mut Vec<Instruction>,
4792 ) -> Result<()> {
4793 builder.pop_operands(module, &[ValType::I64]);
4794 builder.push_operands(&[ValType::I32]);
4795 instructions.push(Instruction::I32WrapI64);
4796 Ok(())
4797 }
4798
4799 fn nontrapping_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4800 module.config.saturating_float_to_int_enabled && f32_on_stack(module, builder)
4801 }
4802
4803 fn i32_trunc_f32_s(
4804 _: &mut Unstructured,
4805 module: &Module,
4806 builder: &mut CodeBuilder,
4807 instructions: &mut Vec<Instruction>,
4808 ) -> Result<()> {
4809 builder.pop_operands(module, &[ValType::F32]);
4810 builder.push_operands(&[ValType::I32]);
4811 if module.config.disallow_traps {
4812 no_traps::trunc(Instruction::I32TruncF32S, builder, instructions);
4813 } else {
4814 instructions.push(Instruction::I32TruncF32S);
4815 }
4816 Ok(())
4817 }
4818
4819 fn i32_trunc_f32_u(
4820 _: &mut Unstructured,
4821 module: &Module,
4822 builder: &mut CodeBuilder,
4823 instructions: &mut Vec<Instruction>,
4824 ) -> Result<()> {
4825 builder.pop_operands(module, &[ValType::F32]);
4826 builder.push_operands(&[ValType::I32]);
4827 if module.config.disallow_traps {
4828 no_traps::trunc(Instruction::I32TruncF32U, builder, instructions);
4829 } else {
4830 instructions.push(Instruction::I32TruncF32U);
4831 }
4832 Ok(())
4833 }
4834
4835 fn nontrapping_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4836 module.config.saturating_float_to_int_enabled && f64_on_stack(module, builder)
4837 }
4838
4839 fn i32_trunc_f64_s(
4840 _: &mut Unstructured,
4841 module: &Module,
4842 builder: &mut CodeBuilder,
4843 instructions: &mut Vec<Instruction>,
4844 ) -> Result<()> {
4845 builder.pop_operands(module, &[ValType::F64]);
4846 builder.push_operands(&[ValType::I32]);
4847 if module.config.disallow_traps {
4848 no_traps::trunc(Instruction::I32TruncF64S, builder, instructions);
4849 } else {
4850 instructions.push(Instruction::I32TruncF64S);
4851 }
4852 Ok(())
4853 }
4854
4855 fn i32_trunc_f64_u(
4856 _: &mut Unstructured,
4857 module: &Module,
4858 builder: &mut CodeBuilder,
4859 instructions: &mut Vec<Instruction>,
4860 ) -> Result<()> {
4861 builder.pop_operands(module, &[ValType::F64]);
4862 builder.push_operands(&[ValType::I32]);
4863 if module.config.disallow_traps {
4864 no_traps::trunc(Instruction::I32TruncF64U, builder, instructions);
4865 } else {
4866 instructions.push(Instruction::I32TruncF64U);
4867 }
4868 Ok(())
4869 }
4870
4871 fn i64_extend_i32_s(
4872 _: &mut Unstructured,
4873 module: &Module,
4874 builder: &mut CodeBuilder,
4875 instructions: &mut Vec<Instruction>,
4876 ) -> Result<()> {
4877 builder.pop_operands(module, &[ValType::I32]);
4878 builder.push_operands(&[ValType::I64]);
4879 instructions.push(Instruction::I64ExtendI32S);
4880 Ok(())
4881 }
4882
4883 fn i64_extend_i32_u(
4884 _: &mut Unstructured,
4885 module: &Module,
4886 builder: &mut CodeBuilder,
4887 instructions: &mut Vec<Instruction>,
4888 ) -> Result<()> {
4889 builder.pop_operands(module, &[ValType::I32]);
4890 builder.push_operands(&[ValType::I64]);
4891 instructions.push(Instruction::I64ExtendI32U);
4892 Ok(())
4893 }
4894
4895 fn i64_trunc_f32_s(
4896 _: &mut Unstructured,
4897 module: &Module,
4898 builder: &mut CodeBuilder,
4899 instructions: &mut Vec<Instruction>,
4900 ) -> Result<()> {
4901 builder.pop_operands(module, &[ValType::F32]);
4902 builder.push_operands(&[ValType::I64]);
4903 if module.config.disallow_traps {
4904 no_traps::trunc(Instruction::I64TruncF32S, builder, instructions);
4905 } else {
4906 instructions.push(Instruction::I64TruncF32S);
4907 }
4908 Ok(())
4909 }
4910
4911 fn i64_trunc_f32_u(
4912 _: &mut Unstructured,
4913 module: &Module,
4914 builder: &mut CodeBuilder,
4915 instructions: &mut Vec<Instruction>,
4916 ) -> Result<()> {
4917 builder.pop_operands(module, &[ValType::F32]);
4918 builder.push_operands(&[ValType::I64]);
4919 if module.config.disallow_traps {
4920 no_traps::trunc(Instruction::I64TruncF32U, builder, instructions);
4921 } else {
4922 instructions.push(Instruction::I64TruncF32U);
4923 }
4924 Ok(())
4925 }
4926
4927 fn i64_trunc_f64_s(
4928 _: &mut Unstructured,
4929 module: &Module,
4930 builder: &mut CodeBuilder,
4931 instructions: &mut Vec<Instruction>,
4932 ) -> Result<()> {
4933 builder.pop_operands(module, &[ValType::F64]);
4934 builder.push_operands(&[ValType::I64]);
4935 if module.config.disallow_traps {
4936 no_traps::trunc(Instruction::I64TruncF64S, builder, instructions);
4937 } else {
4938 instructions.push(Instruction::I64TruncF64S);
4939 }
4940 Ok(())
4941 }
4942
4943 fn i64_trunc_f64_u(
4944 _: &mut Unstructured,
4945 module: &Module,
4946 builder: &mut CodeBuilder,
4947 instructions: &mut Vec<Instruction>,
4948 ) -> Result<()> {
4949 builder.pop_operands(module, &[ValType::F64]);
4950 builder.push_operands(&[ValType::I64]);
4951 if module.config.disallow_traps {
4952 no_traps::trunc(Instruction::I64TruncF64U, builder, instructions);
4953 } else {
4954 instructions.push(Instruction::I64TruncF64U);
4955 }
4956 Ok(())
4957 }
4958
4959 fn f32_convert_i32_s(
4960 _: &mut Unstructured,
4961 module: &Module,
4962 builder: &mut CodeBuilder,
4963 instructions: &mut Vec<Instruction>,
4964 ) -> Result<()> {
4965 builder.pop_operands(module, &[ValType::I32]);
4966 builder.push_operands(&[ValType::F32]);
4967 instructions.push(Instruction::F32ConvertI32S);
4968 Ok(())
4969 }
4970
4971 fn f32_convert_i32_u(
4972 _: &mut Unstructured,
4973 module: &Module,
4974 builder: &mut CodeBuilder,
4975 instructions: &mut Vec<Instruction>,
4976 ) -> Result<()> {
4977 builder.pop_operands(module, &[ValType::I32]);
4978 builder.push_operands(&[ValType::F32]);
4979 instructions.push(Instruction::F32ConvertI32U);
4980 Ok(())
4981 }
4982
4983 fn f32_convert_i64_s(
4984 _: &mut Unstructured,
4985 module: &Module,
4986 builder: &mut CodeBuilder,
4987 instructions: &mut Vec<Instruction>,
4988 ) -> Result<()> {
4989 builder.pop_operands(module, &[ValType::I64]);
4990 builder.push_operands(&[ValType::F32]);
4991 instructions.push(Instruction::F32ConvertI64S);
4992 Ok(())
4993 }
4994
4995 fn f32_convert_i64_u(
4996 _: &mut Unstructured,
4997 module: &Module,
4998 builder: &mut CodeBuilder,
4999 instructions: &mut Vec<Instruction>,
5000 ) -> Result<()> {
5001 builder.pop_operands(module, &[ValType::I64]);
5002 builder.push_operands(&[ValType::F32]);
5003 instructions.push(Instruction::F32ConvertI64U);
5004 Ok(())
5005 }
5006
5007 fn f32_demote_f64(
5008 _: &mut Unstructured,
5009 module: &Module,
5010 builder: &mut CodeBuilder,
5011 instructions: &mut Vec<Instruction>,
5012 ) -> Result<()> {
5013 builder.pop_operands(module, &[ValType::F64]);
5014 builder.push_operands(&[ValType::F32]);
5015 instructions.push(Instruction::F32DemoteF64);
5016 Ok(())
5017 }
5018
5019 fn f64_convert_i32_s(
5020 _: &mut Unstructured,
5021 module: &Module,
5022 builder: &mut CodeBuilder,
5023 instructions: &mut Vec<Instruction>,
5024 ) -> Result<()> {
5025 builder.pop_operands(module, &[ValType::I32]);
5026 builder.push_operands(&[ValType::F64]);
5027 instructions.push(Instruction::F64ConvertI32S);
5028 Ok(())
5029 }
5030
5031 fn f64_convert_i32_u(
5032 _: &mut Unstructured,
5033 module: &Module,
5034 builder: &mut CodeBuilder,
5035 instructions: &mut Vec<Instruction>,
5036 ) -> Result<()> {
5037 builder.pop_operands(module, &[ValType::I32]);
5038 builder.push_operands(&[ValType::F64]);
5039 instructions.push(Instruction::F64ConvertI32U);
5040 Ok(())
5041 }
5042
5043 fn f64_convert_i64_s(
5044 _: &mut Unstructured,
5045 module: &Module,
5046 builder: &mut CodeBuilder,
5047 instructions: &mut Vec<Instruction>,
5048 ) -> Result<()> {
5049 builder.pop_operands(module, &[ValType::I64]);
5050 builder.push_operands(&[ValType::F64]);
5051 instructions.push(Instruction::F64ConvertI64S);
5052 Ok(())
5053 }
5054
5055 fn f64_convert_i64_u(
5056 _: &mut Unstructured,
5057 module: &Module,
5058 builder: &mut CodeBuilder,
5059 instructions: &mut Vec<Instruction>,
5060 ) -> Result<()> {
5061 builder.pop_operands(module, &[ValType::I64]);
5062 builder.push_operands(&[ValType::F64]);
5063 instructions.push(Instruction::F64ConvertI64U);
5064 Ok(())
5065 }
5066
5067 fn f64_promote_f32(
5068 _: &mut Unstructured,
5069 module: &Module,
5070 builder: &mut CodeBuilder,
5071 instructions: &mut Vec<Instruction>,
5072 ) -> Result<()> {
5073 builder.pop_operands(module, &[ValType::F32]);
5074 builder.push_operands(&[ValType::F64]);
5075 instructions.push(Instruction::F64PromoteF32);
5076 Ok(())
5077 }
5078
5079 fn i32_reinterpret_f32(
5080 _: &mut Unstructured,
5081 module: &Module,
5082 builder: &mut CodeBuilder,
5083 instructions: &mut Vec<Instruction>,
5084 ) -> Result<()> {
5085 builder.pop_operands(module, &[ValType::F32]);
5086 builder.push_operands(&[ValType::I32]);
5087 instructions.push(Instruction::I32ReinterpretF32);
5088 Ok(())
5089 }
5090
5091 fn i64_reinterpret_f64(
5092 _: &mut Unstructured,
5093 module: &Module,
5094 builder: &mut CodeBuilder,
5095 instructions: &mut Vec<Instruction>,
5096 ) -> Result<()> {
5097 builder.pop_operands(module, &[ValType::F64]);
5098 builder.push_operands(&[ValType::I64]);
5099 instructions.push(Instruction::I64ReinterpretF64);
5100 Ok(())
5101 }
5102
5103 fn f32_reinterpret_i32(
5104 _: &mut Unstructured,
5105 module: &Module,
5106 builder: &mut CodeBuilder,
5107 instructions: &mut Vec<Instruction>,
5108 ) -> Result<()> {
5109 builder.pop_operands(module, &[ValType::I32]);
5110 builder.push_operands(&[ValType::F32]);
5111 instructions.push(Instruction::F32ReinterpretI32);
5112 Ok(())
5113 }
5114
5115 fn f64_reinterpret_i64(
5116 _: &mut Unstructured,
5117 module: &Module,
5118 builder: &mut CodeBuilder,
5119 instructions: &mut Vec<Instruction>,
5120 ) -> Result<()> {
5121 builder.pop_operands(module, &[ValType::I64]);
5122 builder.push_operands(&[ValType::F64]);
5123 instructions.push(Instruction::F64ReinterpretI64);
5124 Ok(())
5125 }
5126
5127 fn extendable_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
5128 module.config.sign_extension_ops_enabled && i32_on_stack(module, builder)
5129 }
5130
5131 fn i32_extend_8_s(
5132 _: &mut Unstructured,
5133 module: &Module,
5134 builder: &mut CodeBuilder,
5135 instructions: &mut Vec<Instruction>,
5136 ) -> Result<()> {
5137 builder.pop_operands(module, &[ValType::I32]);
5138 builder.push_operands(&[ValType::I32]);
5139 instructions.push(Instruction::I32Extend8S);
5140 Ok(())
5141 }
5142
5143 fn i32_extend_16_s(
5144 _: &mut Unstructured,
5145 module: &Module,
5146 builder: &mut CodeBuilder,
5147 instructions: &mut Vec<Instruction>,
5148 ) -> Result<()> {
5149 builder.pop_operands(module, &[ValType::I32]);
5150 builder.push_operands(&[ValType::I32]);
5151 instructions.push(Instruction::I32Extend16S);
5152 Ok(())
5153 }
5154
5155 fn extendable_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
5156 module.config.sign_extension_ops_enabled && i64_on_stack(module, builder)
5157 }
5158
5159 fn i64_extend_8_s(
5160 _: &mut Unstructured,
5161 module: &Module,
5162 builder: &mut CodeBuilder,
5163 instructions: &mut Vec<Instruction>,
5164 ) -> Result<()> {
5165 builder.pop_operands(module, &[ValType::I64]);
5166 builder.push_operands(&[ValType::I64]);
5167 instructions.push(Instruction::I64Extend8S);
5168 Ok(())
5169 }
5170
5171 fn i64_extend_16_s(
5172 _: &mut Unstructured,
5173 module: &Module,
5174 builder: &mut CodeBuilder,
5175 instructions: &mut Vec<Instruction>,
5176 ) -> Result<()> {
5177 builder.pop_operands(module, &[ValType::I64]);
5178 builder.push_operands(&[ValType::I64]);
5179 instructions.push(Instruction::I64Extend16S);
5180 Ok(())
5181 }
5182
5183 fn i64_extend_32_s(
5184 _: &mut Unstructured,
5185 module: &Module,
5186 builder: &mut CodeBuilder,
5187 instructions: &mut Vec<Instruction>,
5188 ) -> Result<()> {
5189 builder.pop_operands(module, &[ValType::I64]);
5190 builder.push_operands(&[ValType::I64]);
5191 instructions.push(Instruction::I64Extend32S);
5192 Ok(())
5193 }
5194
5195 fn i32_trunc_sat_f32_s(
5196 _: &mut Unstructured,
5197 module: &Module,
5198 builder: &mut CodeBuilder,
5199 instructions: &mut Vec<Instruction>,
5200 ) -> Result<()> {
5201 builder.pop_operands(module, &[ValType::F32]);
5202 builder.push_operands(&[ValType::I32]);
5203 instructions.push(Instruction::I32TruncSatF32S);
5204 Ok(())
5205 }
5206
5207 fn i32_trunc_sat_f32_u(
5208 _: &mut Unstructured,
5209 module: &Module,
5210 builder: &mut CodeBuilder,
5211 instructions: &mut Vec<Instruction>,
5212 ) -> Result<()> {
5213 builder.pop_operands(module, &[ValType::F32]);
5214 builder.push_operands(&[ValType::I32]);
5215 instructions.push(Instruction::I32TruncSatF32U);
5216 Ok(())
5217 }
5218
5219 fn i32_trunc_sat_f64_s(
5220 _: &mut Unstructured,
5221 module: &Module,
5222 builder: &mut CodeBuilder,
5223 instructions: &mut Vec<Instruction>,
5224 ) -> Result<()> {
5225 builder.pop_operands(module, &[ValType::F64]);
5226 builder.push_operands(&[ValType::I32]);
5227 instructions.push(Instruction::I32TruncSatF64S);
5228 Ok(())
5229 }
5230
5231 fn i32_trunc_sat_f64_u(
5232 _: &mut Unstructured,
5233 module: &Module,
5234 builder: &mut CodeBuilder,
5235 instructions: &mut Vec<Instruction>,
5236 ) -> Result<()> {
5237 builder.pop_operands(module, &[ValType::F64]);
5238 builder.push_operands(&[ValType::I32]);
5239 instructions.push(Instruction::I32TruncSatF64U);
5240 Ok(())
5241 }
5242
5243 fn i64_trunc_sat_f32_s(
5244 _: &mut Unstructured,
5245 module: &Module,
5246 builder: &mut CodeBuilder,
5247 instructions: &mut Vec<Instruction>,
5248 ) -> Result<()> {
5249 builder.pop_operands(module, &[ValType::F32]);
5250 builder.push_operands(&[ValType::I64]);
5251 instructions.push(Instruction::I64TruncSatF32S);
5252 Ok(())
5253 }
5254
5255 fn i64_trunc_sat_f32_u(
5256 _: &mut Unstructured,
5257 module: &Module,
5258 builder: &mut CodeBuilder,
5259 instructions: &mut Vec<Instruction>,
5260 ) -> Result<()> {
5261 builder.pop_operands(module, &[ValType::F32]);
5262 builder.push_operands(&[ValType::I64]);
5263 instructions.push(Instruction::I64TruncSatF32U);
5264 Ok(())
5265 }
5266
5267 fn i64_trunc_sat_f64_s(
5268 _: &mut Unstructured,
5269 module: &Module,
5270 builder: &mut CodeBuilder,
5271 instructions: &mut Vec<Instruction>,
5272 ) -> Result<()> {
5273 builder.pop_operands(module, &[ValType::F64]);
5274 builder.push_operands(&[ValType::I64]);
5275 instructions.push(Instruction::I64TruncSatF64S);
5276 Ok(())
5277 }
5278
5279 fn i64_trunc_sat_f64_u(
5280 _: &mut Unstructured,
5281 module: &Module,
5282 builder: &mut CodeBuilder,
5283 instructions: &mut Vec<Instruction>,
5284 ) -> Result<()> {
5285 builder.pop_operands(module, &[ValType::F64]);
5286 builder.push_operands(&[ValType::I64]);
5287 instructions.push(Instruction::I64TruncSatF64U);
5288 Ok(())
5289 }
5290
5291 fn memory_offset(u: &mut Unstructured, module: &Module, memory_index: u32) -> Result<u64> {
5292 let MemoryOffsetChoices(a, b, c) = module.config.memory_offset_choices;
5293 assert!(a + b + c != 0);
5294
5295 let memory_type = &module.memories[memory_index as usize];
5296 let min = memory_type
5297 .minimum
5298 .saturating_mul(crate::page_size(memory_type).into());
5299 let max = memory_type
5300 .maximum
5301 .map(|max| max.saturating_mul(crate::page_size(memory_type).into()))
5302 .unwrap_or(u64::MAX);
5303
5304 let (min, max, true_max) = match (memory_type.memory64, module.config.disallow_traps) {
5305 (true, false) => {
5306 // 64-bit memories can use the limits calculated above as-is
5307 (min, max, u64::MAX)
5308 }
5309 (false, false) => {
5310 // 32-bit memories can't represent a full 4gb offset, so if that's the
5311 // min/max sizes then we need to switch the m to `u32::MAX`.
5312 (
5313 u64::from(u32::try_from(min).unwrap_or(u32::MAX)),
5314 u64::from(u32::try_from(max).unwrap_or(u32::MAX)),
5315 u64::from(u32::MAX),
5316 )
5317 }
5318 // The logic for non-trapping versions of load/store involves pushing
5319 // the offset + load/store size onto the stack as either an i32 or i64
5320 // value. So even though offsets can normally be as high as u32 or u64,
5321 // we need to limit them to lower in order for our non-trapping logic to
5322 // work. 16 is the number of bytes of the largest load type (V128).
5323 (true, true) => {
5324 let no_trap_max = (i64::MAX - 16) as u64;
5325 (min.min(no_trap_max), no_trap_max, no_trap_max)
5326 }
5327 (false, true) => {
5328 let no_trap_max = (i32::MAX - 16) as u64;
5329 (min.min(no_trap_max), no_trap_max, no_trap_max)
5330 }
5331 };
5332
5333 let choice = u.int_in_range(0..=a + b + c - 1)?;
5334 if choice < a {
5335 u.int_in_range(0..=min)
5336 } else if choice < a + b {
5337 u.int_in_range(min..=max)
5338 } else {
5339 u.int_in_range(max..=true_max)
5340 }
5341 }
5342
5343 fn mem_arg(
5344 u: &mut Unstructured,
5345 module: &Module,
5346 builder: &mut CodeBuilder,
5347 alignments: &[u32],
5348 ) -> Result<MemArg> {
5349 let memory_index = if builder.type_on_stack(module, ValType::I32) {
5350 builder.pop_operands(module, &[ValType::I32]);
5351 memory_index(u, builder, ValType::I32)?
5352 } else {
5353 builder.pop_operands(module, &[ValType::I64]);
5354 memory_index(u, builder, ValType::I64)?
5355 };
5356 let offset = memory_offset(u, module, memory_index)?;
5357 let align = *u.choose(alignments)?;
5358 Ok(MemArg {
5359 memory_index,
5360 offset,
5361 align,
5362 })
5363 }
5364
5365 fn memory_index(u: &mut Unstructured, builder: &CodeBuilder, ty: ValType) -> Result<u32> {
5366 if ty == ValType::I32 {
5367 Ok(*u.choose(&builder.allocs.memory32)?)
5368 } else {
5369 Ok(*u.choose(&builder.allocs.memory64)?)
5370 }
5371 }
5372
5373 fn data_index(u: &mut Unstructured, module: &Module) -> Result<u32> {
5374 let data = module.data.len() as u32;
5375 assert!(data > 0);
5376 if data == 1 {
5377 Ok(0)
5378 } else {
5379 u.int_in_range(0..=data - 1)
5380 }
5381 }
5382
5383 #[inline]
5384 fn ref_null_valid(module: &Module, _: &mut CodeBuilder) -> bool {
5385 module.config.reference_types_enabled
5386 }
5387
5388 fn ref_null(
5389 u: &mut Unstructured,
5390 module: &Module,
5391 builder: &mut CodeBuilder,
5392 instructions: &mut Vec<Instruction>,
5393 ) -> Result<()> {
5394 let mut choices = vec![RefType::EXTERNREF, RefType::FUNCREF];
5395 if module.config.exceptions_enabled {
5396 choices.push(RefType::EXNREF);
5397 }
5398 if module.config.gc_enabled {
5399 use AbstractHeapType::*;
5400 let r = |heap_type| RefType {
5401 nullable: true,
5402 heap_type,
5403 };
5404 let a = |abstract_heap_type| HeapType::Abstract {
5405 shared: false, // TODO: handle shared
5406 ty: abstract_heap_type,
5407 };
5408 choices.push(r(a(Any)));
5409 choices.push(r(a(Eq)));
5410 choices.push(r(a(Array)));
5411 choices.push(r(a(Struct)));
5412 choices.push(r(a(I31)));
5413 choices.push(r(a(None)));
5414 choices.push(r(a(NoFunc)));
5415 choices.push(r(a(NoExtern)));
5416 for i in 0..module.types.len() {
5417 let i = u32::try_from(i).unwrap();
5418 choices.push(r(HeapType::Concrete(i)));
5419 }
5420 }
5421 let ty = *u.choose(&choices)?;
5422 builder.push_operand(Some(ty.into()));
5423 instructions.push(Instruction::RefNull(ty.heap_type));
5424 Ok(())
5425 }
5426
5427 #[inline]
5428 fn ref_func_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5429 module.config.reference_types_enabled && builder.allocs.referenced_functions.len() > 0
5430 }
5431
5432 fn ref_func(
5433 u: &mut Unstructured,
5434 module: &Module,
5435 builder: &mut CodeBuilder,
5436 instructions: &mut Vec<Instruction>,
5437 ) -> Result<()> {
5438 let i = *u.choose(&builder.allocs.referenced_functions)?;
5439 let ty = module.funcs[usize::try_from(i).unwrap()].0;
5440 builder.push_operand(Some(ValType::Ref(if module.config.gc_enabled {
5441 RefType {
5442 nullable: false,
5443 heap_type: HeapType::Concrete(ty),
5444 }
5445 } else {
5446 RefType::FUNCREF
5447 })));
5448 instructions.push(Instruction::RefFunc(i));
5449 Ok(())
5450 }
5451
5452 #[inline]
5453 fn ref_as_non_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5454 module.config.gc_enabled && builder.ref_type_on_stack().is_some()
5455 }
5456
5457 fn ref_as_non_null(
5458 u: &mut Unstructured,
5459 module: &Module,
5460 builder: &mut CodeBuilder,
5461 instructions: &mut Vec<Instruction>,
5462 ) -> Result<()> {
5463 let ref_ty = match builder.pop_ref_type() {
5464 Some(r) => r,
5465 None => module.arbitrary_ref_type(u)?,
5466 };
5467 builder.push_operand(Some(ValType::Ref(RefType {
5468 nullable: false,
5469 heap_type: ref_ty.heap_type,
5470 })));
5471 instructions.push(Instruction::RefAsNonNull);
5472 Ok(())
5473 }
5474
5475 #[inline]
5476 fn ref_eq_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5477 let eq_ref = ValType::Ref(RefType::EQREF);
5478 module.config.gc_enabled && builder.types_on_stack(module, &[eq_ref, eq_ref])
5479 }
5480
5481 fn ref_eq(
5482 _u: &mut Unstructured,
5483 _module: &Module,
5484 builder: &mut CodeBuilder,
5485 instructions: &mut Vec<Instruction>,
5486 ) -> Result<()> {
5487 builder.pop_operand();
5488 builder.pop_operand();
5489 builder.push_operand(Some(ValType::I32));
5490 instructions.push(Instruction::RefEq);
5491 Ok(())
5492 }
5493
5494 #[inline]
5495 fn ref_test_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5496 module.config.gc_enabled && builder.ref_type_on_stack().is_some()
5497 }
5498
5499 fn ref_test(
5500 u: &mut Unstructured,
5501 module: &Module,
5502 builder: &mut CodeBuilder,
5503 instructions: &mut Vec<Instruction>,
5504 ) -> Result<()> {
5505 let ref_ty = match builder.pop_ref_type() {
5506 Some(r) => r,
5507 None => module.arbitrary_ref_type(u)?,
5508 };
5509 builder.push_operand(Some(ValType::I32));
5510
5511 let sub_ty = module.arbitrary_matching_heap_type(u, ref_ty.heap_type)?;
5512 instructions.push(if !ref_ty.nullable || u.arbitrary()? {
5513 Instruction::RefTestNonNull(sub_ty)
5514 } else {
5515 Instruction::RefTestNullable(sub_ty)
5516 });
5517 Ok(())
5518 }
5519
5520 #[inline]
5521 fn ref_cast_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5522 !module.config.disallow_traps
5523 && module.config.gc_enabled
5524 && builder.ref_type_on_stack().is_some()
5525 }
5526
5527 fn ref_cast(
5528 u: &mut Unstructured,
5529 module: &Module,
5530 builder: &mut CodeBuilder,
5531 instructions: &mut Vec<Instruction>,
5532 ) -> Result<()> {
5533 let ref_ty = match builder.pop_ref_type() {
5534 Some(r) => r,
5535 None => module.arbitrary_ref_type(u)?,
5536 };
5537 let sub_ty = RefType {
5538 nullable: if !ref_ty.nullable {
5539 false
5540 } else {
5541 u.arbitrary()?
5542 },
5543 heap_type: module.arbitrary_matching_heap_type(u, ref_ty.heap_type)?,
5544 };
5545 builder.push_operand(Some(ValType::Ref(sub_ty)));
5546
5547 instructions.push(if !sub_ty.nullable {
5548 Instruction::RefCastNonNull(sub_ty.heap_type)
5549 } else {
5550 Instruction::RefCastNullable(sub_ty.heap_type)
5551 });
5552 Ok(())
5553 }
5554
5555 #[inline]
5556 fn ref_is_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5557 module.config.reference_types_enabled
5558 && (builder.type_on_stack(module, ValType::EXTERNREF)
5559 || builder.type_on_stack(module, ValType::FUNCREF))
5560 }
5561
5562 fn ref_is_null(
5563 _: &mut Unstructured,
5564 _module: &Module,
5565 builder: &mut CodeBuilder,
5566 instructions: &mut Vec<Instruction>,
5567 ) -> Result<()> {
5568 builder.pop_ref_type();
5569 builder.push_operands(&[ValType::I32]);
5570 instructions.push(Instruction::RefIsNull);
5571 Ok(())
5572 }
5573
5574 #[inline]
5575 fn table_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5576 module.config.reference_types_enabled
5577 && module.config.bulk_memory_enabled
5578 && !module.config.disallow_traps // Non-trapping table fill generation not yet implemented
5579 && table_fill_candidates(module, builder).next().is_some()
5580 }
5581
5582 fn table_fill_candidates<'a>(
5583 module: &'a Module,
5584 builder: &'a CodeBuilder,
5585 ) -> impl Iterator<Item = u32> + 'a {
5586 module
5587 .tables
5588 .iter()
5589 .enumerate()
5590 .filter(move |(_, t)| {
5591 builder.types_on_stack(
5592 module,
5593 &[t.index_type(), t.element_type.into(), t.index_type()],
5594 )
5595 })
5596 .map(|(i, _)| i as u32)
5597 }
5598
5599 fn table_fill(
5600 u: &mut Unstructured,
5601 module: &Module,
5602 builder: &mut CodeBuilder,
5603 instructions: &mut Vec<Instruction>,
5604 ) -> Result<()> {
5605 let table = *u.choose(&table_fill_candidates(module, builder).collect::<Vec<_>>())?;
5606 let ty = &module.tables[table as usize];
5607 builder.pop_operands(
5608 module,
5609 &[ty.index_type(), ty.element_type.into(), ty.index_type()],
5610 );
5611 instructions.push(Instruction::TableFill(table));
5612 Ok(())
5613 }
5614
5615 #[inline]
5616 fn table_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5617 module.config.reference_types_enabled
5618 && !module.config.disallow_traps // Non-trapping table.set generation not yet implemented
5619 && table_set_candidates(module, builder).next().is_some()
5620 }
5621
5622 fn table_set_candidates<'a>(
5623 module: &'a Module,
5624 builder: &'a CodeBuilder,
5625 ) -> impl Iterator<Item = u32> + 'a {
5626 module
5627 .tables
5628 .iter()
5629 .enumerate()
5630 .filter(move |(_, t)| {
5631 builder.types_on_stack(module, &[t.index_type(), t.element_type.into()])
5632 })
5633 .map(|(i, _)| i as u32)
5634 }
5635
5636 fn table_set(
5637 u: &mut Unstructured,
5638 module: &Module,
5639 builder: &mut CodeBuilder,
5640 instructions: &mut Vec<Instruction>,
5641 ) -> Result<()> {
5642 let table = *u.choose(&table_set_candidates(module, builder).collect::<Vec<_>>())?;
5643 let ty = &module.tables[table as usize];
5644 builder.pop_operands(module, &[ty.index_type(), ty.element_type.into()]);
5645 instructions.push(Instruction::TableSet(table));
5646 Ok(())
5647 }
5648
5649 #[inline]
5650 fn table_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5651 if !module.config.reference_types_enabled {
5652 return false;
5653 }
5654 // Non-trapping table.get generation not yet implemented
5655 if module.config.disallow_traps {
5656 return false;
5657 }
5658 if builder.type_on_stack(module, ValType::I32) && builder.allocs.table32.len() > 0 {
5659 return true;
5660 }
5661 if builder.type_on_stack(module, ValType::I64) && builder.allocs.table64.len() > 0 {
5662 return true;
5663 }
5664 false
5665 }
5666
5667 fn table_get(
5668 u: &mut Unstructured,
5669 module: &Module,
5670 builder: &mut CodeBuilder,
5671 instructions: &mut Vec<Instruction>,
5672 ) -> Result<()> {
5673 let candidates = if builder.type_on_stack(module, ValType::I32) {
5674 builder.pop_operands(module, &[ValType::I32]);
5675 &builder.allocs.table32
5676 } else {
5677 builder.pop_operands(module, &[ValType::I64]);
5678 &builder.allocs.table64
5679 };
5680 let idx = *u.choose(candidates)?;
5681 let ty = module.tables[idx as usize].element_type;
5682 builder.push_operands(&[ty.into()]);
5683 instructions.push(Instruction::TableGet(idx as u32));
5684 Ok(())
5685 }
5686
5687 #[inline]
5688 fn table_size_valid(module: &Module, _: &mut CodeBuilder) -> bool {
5689 module.config.reference_types_enabled && module.tables.len() > 0
5690 }
5691
5692 fn table_size(
5693 u: &mut Unstructured,
5694 module: &Module,
5695 builder: &mut CodeBuilder,
5696 instructions: &mut Vec<Instruction>,
5697 ) -> Result<()> {
5698 let table = u.int_in_range(0..=module.tables.len() - 1)?;
5699 let ty = &module.tables[table];
5700 builder.push_operands(&[ty.index_type()]);
5701 instructions.push(Instruction::TableSize(table as u32));
5702 Ok(())
5703 }
5704
5705 #[inline]
5706 fn table_grow_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5707 module.config.reference_types_enabled && table_grow_candidates(module, builder).next().is_some()
5708 }
5709
5710 fn table_grow_candidates<'a>(
5711 module: &'a Module,
5712 builder: &'a CodeBuilder,
5713 ) -> impl Iterator<Item = u32> + 'a {
5714 module
5715 .tables
5716 .iter()
5717 .enumerate()
5718 .filter(move |(_, t)| {
5719 builder.types_on_stack(module, &[t.element_type.into(), t.index_type()])
5720 })
5721 .map(|(i, _)| i as u32)
5722 }
5723
5724 fn table_grow(
5725 u: &mut Unstructured,
5726 module: &Module,
5727 builder: &mut CodeBuilder,
5728 instructions: &mut Vec<Instruction>,
5729 ) -> Result<()> {
5730 let table = *u.choose(&table_grow_candidates(module, builder).collect::<Vec<_>>())?;
5731 let ty = &module.tables[table as usize];
5732 builder.pop_operands(module, &[ty.element_type.into(), ty.index_type()]);
5733 builder.push_operands(&[ty.index_type()]);
5734 instructions.push(Instruction::TableGrow(table));
5735 Ok(())
5736 }
5737
5738 #[inline]
5739 fn table_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5740 if !module.config.bulk_memory_enabled {
5741 return false;
5742 }
5743 // Non-trapping table.copy generation not yet implemented
5744 if module.config.disallow_traps {
5745 return false;
5746 }
5747 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) {
5748 return builder.allocs.table_copy_64_to_64.len() > 0;
5749 }
5750 if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) {
5751 return builder.allocs.table_copy_32_to_32.len() > 0;
5752 }
5753 if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32]) {
5754 return builder.allocs.table_copy_32_to_64.len() > 0;
5755 }
5756 if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32]) {
5757 return builder.allocs.table_copy_64_to_32.len() > 0;
5758 }
5759 false
5760 }
5761
5762 fn table_copy(
5763 u: &mut Unstructured,
5764 module: &Module,
5765 builder: &mut CodeBuilder,
5766 instructions: &mut Vec<Instruction>,
5767 ) -> Result<()> {
5768 use CopyIndexSize::*;
5769
5770 let (src_table, dst_table) = match gen_copy_src_and_dst(module, builder) {
5771 (I32, I32) => *u.choose(&builder.allocs.table_copy_32_to_32)?,
5772 (I32, I64) => *u.choose(&builder.allocs.table_copy_32_to_64)?,
5773 (I64, I32) => *u.choose(&builder.allocs.table_copy_64_to_32)?,
5774 (I64, I64) => *u.choose(&builder.allocs.table_copy_64_to_64)?,
5775 };
5776 instructions.push(Instruction::TableCopy {
5777 src_table,
5778 dst_table,
5779 });
5780 Ok(())
5781 }
5782
5783 #[inline]
5784 fn table_init_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5785 if !module.config.bulk_memory_enabled {
5786 return false;
5787 }
5788 // Non-trapping table.init generation not yet implemented.
5789 if module.config.disallow_traps {
5790 return false;
5791 }
5792 if builder.allocs.table32_init.len() > 0
5793 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
5794 {
5795 return true;
5796 }
5797 if builder.allocs.table64_init.len() > 0
5798 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
5799 {
5800 return true;
5801 }
5802 false
5803 }
5804
5805 fn table_init(
5806 u: &mut Unstructured,
5807 module: &Module,
5808 builder: &mut CodeBuilder,
5809 instructions: &mut Vec<Instruction>,
5810 ) -> Result<()> {
5811 let candidates = if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
5812 {
5813 builder.pop_operands(module, &[ValType::I64, ValType::I32, ValType::I32]);
5814 &builder.allocs.table64_init
5815 } else {
5816 builder.pop_operands(module, &[ValType::I32, ValType::I32, ValType::I32]);
5817 &builder.allocs.table32_init
5818 };
5819 let (elem_index, table) = *u.choose(&candidates)?;
5820 instructions.push(Instruction::TableInit { elem_index, table });
5821 Ok(())
5822 }
5823
5824 #[inline]
5825 fn elem_drop_valid(module: &Module, _builder: &mut CodeBuilder) -> bool {
5826 module.config.bulk_memory_enabled && module.elems.len() > 0
5827 }
5828
5829 fn elem_drop(
5830 u: &mut Unstructured,
5831 module: &Module,
5832 _builder: &mut CodeBuilder,
5833 instructions: &mut Vec<Instruction>,
5834 ) -> Result<()> {
5835 let segment = u.int_in_range(0..=module.elems.len() - 1)? as u32;
5836 instructions.push(Instruction::ElemDrop(segment));
5837 Ok(())
5838 }
5839
5840 #[inline]
5841 fn struct_new_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5842 module.config.gc_enabled
5843 && module
5844 .struct_types
5845 .iter()
5846 .copied()
5847 .any(|i| builder.field_types_on_stack(module, &module.ty(i).unwrap_struct().fields))
5848 }
5849
5850 fn struct_new(
5851 u: &mut Unstructured,
5852 module: &Module,
5853 builder: &mut CodeBuilder,
5854 instructions: &mut Vec<Instruction>,
5855 ) -> Result<()> {
5856 let n = module
5857 .struct_types
5858 .iter()
5859 .filter(|i| builder.field_types_on_stack(module, &module.ty(**i).unwrap_struct().fields))
5860 .count();
5861 debug_assert!(n > 0);
5862 let i = u.int_in_range(0..=n - 1)?;
5863 let ty = module
5864 .struct_types
5865 .iter()
5866 .copied()
5867 .filter(|i| builder.field_types_on_stack(module, &module.ty(*i).unwrap_struct().fields))
5868 .nth(i)
5869 .unwrap();
5870
5871 for _ in module.ty(ty).unwrap_struct().fields.iter() {
5872 builder.pop_operand();
5873 }
5874 builder.push_operand(Some(ValType::Ref(RefType {
5875 nullable: false,
5876 heap_type: HeapType::Concrete(ty),
5877 })));
5878
5879 instructions.push(Instruction::StructNew(ty));
5880 Ok(())
5881 }
5882
5883 #[inline]
5884 fn struct_new_default_valid(module: &Module, _builder: &mut CodeBuilder) -> bool {
5885 module.config.gc_enabled
5886 && module.struct_types.iter().copied().any(|i| {
5887 module
5888 .ty(i)
5889 .unwrap_struct()
5890 .fields
5891 .iter()
5892 .all(|f| f.element_type.is_defaultable())
5893 })
5894 }
5895
5896 fn struct_new_default(
5897 u: &mut Unstructured,
5898 module: &Module,
5899 builder: &mut CodeBuilder,
5900 instructions: &mut Vec<Instruction>,
5901 ) -> Result<()> {
5902 let n = module
5903 .struct_types
5904 .iter()
5905 .filter(|i| {
5906 module
5907 .ty(**i)
5908 .unwrap_struct()
5909 .fields
5910 .iter()
5911 .all(|f| f.element_type.is_defaultable())
5912 })
5913 .count();
5914 debug_assert!(n > 0);
5915 let i = u.int_in_range(0..=n - 1)?;
5916 let ty = module
5917 .struct_types
5918 .iter()
5919 .copied()
5920 .filter(|i| {
5921 module
5922 .ty(*i)
5923 .unwrap_struct()
5924 .fields
5925 .iter()
5926 .all(|f| f.element_type.is_defaultable())
5927 })
5928 .nth(i)
5929 .unwrap();
5930
5931 builder.push_operand(Some(ValType::Ref(RefType {
5932 nullable: false,
5933 heap_type: HeapType::Concrete(ty),
5934 })));
5935
5936 instructions.push(Instruction::StructNewDefault(ty));
5937 Ok(())
5938 }
5939
5940 #[inline]
5941 fn struct_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5942 module.config.gc_enabled
5943 && !module.config.disallow_traps
5944 && builder.non_empty_struct_ref_on_stack(module, !module.config.disallow_traps)
5945 }
5946
5947 fn struct_get(
5948 u: &mut Unstructured,
5949 module: &Module,
5950 builder: &mut CodeBuilder,
5951 instructions: &mut Vec<Instruction>,
5952 ) -> Result<()> {
5953 let (_, struct_type_index) = builder.pop_concrete_ref_type();
5954 let struct_ty = module.ty(struct_type_index).unwrap_struct();
5955 let num_fields = u32::try_from(struct_ty.fields.len()).unwrap();
5956 debug_assert!(num_fields > 0);
5957 let field_index = u.int_in_range(0..=num_fields - 1)?;
5958 let (val_ty, ext) = match struct_ty.fields[usize::try_from(field_index).unwrap()].element_type {
5959 StorageType::I8 | StorageType::I16 => (ValType::I32, Some(u.arbitrary()?)),
5960 StorageType::Val(v) => (v, None),
5961 };
5962 builder.push_operand(Some(val_ty));
5963 instructions.push(match ext {
5964 None => Instruction::StructGet {
5965 struct_type_index,
5966 field_index,
5967 },
5968 Some(true) => Instruction::StructGetS {
5969 struct_type_index,
5970 field_index,
5971 },
5972 Some(false) => Instruction::StructGetU {
5973 struct_type_index,
5974 field_index,
5975 },
5976 });
5977 Ok(())
5978 }
5979
5980 #[inline]
5981 fn struct_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5982 if !module.config.gc_enabled {
5983 return false;
5984 }
5985 match builder.concrete_struct_ref_type_on_stack_at(module, 1) {
5986 None => return false,
5987 Some((true, _, _)) if module.config.disallow_traps => return false,
5988 Some((_, _, ty)) => ty
5989 .fields
5990 .iter()
5991 .any(|f| f.mutable && builder.type_on_stack(module, f.element_type.unpack())),
5992 }
5993 }
5994
5995 fn struct_set(
5996 u: &mut Unstructured,
5997 module: &Module,
5998 builder: &mut CodeBuilder,
5999 instructions: &mut Vec<Instruction>,
6000 ) -> Result<()> {
6001 let val_ty = builder.pop_operand();
6002 let (_, struct_type_index) = builder.pop_concrete_ref_type();
6003 let struct_ty = module.ty(struct_type_index).unwrap_struct();
6004
6005 let valid_field = |f: &FieldType| -> bool {
6006 match val_ty {
6007 None => f.mutable,
6008 Some(val_ty) => {
6009 f.mutable && module.val_type_is_sub_type(val_ty, f.element_type.unpack())
6010 }
6011 }
6012 };
6013
6014 let n = struct_ty.fields.iter().filter(|f| valid_field(f)).count();
6015 debug_assert!(n > 0);
6016 let i = u.int_in_range(0..=n - 1)?;
6017 let (field_index, _) = struct_ty
6018 .fields
6019 .iter()
6020 .enumerate()
6021 .filter(|(_, f)| valid_field(f))
6022 .nth(i)
6023 .unwrap();
6024 let field_index = u32::try_from(field_index).unwrap();
6025
6026 instructions.push(Instruction::StructSet {
6027 struct_type_index,
6028 field_index,
6029 });
6030 Ok(())
6031 }
6032
6033 #[inline]
6034 fn array_new_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6035 module.config.gc_enabled
6036 && builder.type_on_stack(module, ValType::I32)
6037 && module
6038 .array_types
6039 .iter()
6040 .any(|i| builder.field_type_on_stack_at(module, 1, module.ty(*i).unwrap_array().0))
6041 }
6042
6043 fn array_new(
6044 u: &mut Unstructured,
6045 module: &Module,
6046 builder: &mut CodeBuilder,
6047 instructions: &mut Vec<Instruction>,
6048 ) -> Result<()> {
6049 let n = module
6050 .array_types
6051 .iter()
6052 .filter(|i| builder.field_type_on_stack_at(module, 1, module.ty(**i).unwrap_array().0))
6053 .count();
6054 debug_assert!(n > 0);
6055 let i = u.int_in_range(0..=n - 1)?;
6056 let ty = module
6057 .array_types
6058 .iter()
6059 .copied()
6060 .filter(|i| builder.field_type_on_stack_at(module, 1, module.ty(*i).unwrap_array().0))
6061 .nth(i)
6062 .unwrap();
6063
6064 builder.pop_operand();
6065 builder.pop_operand();
6066 builder.push_operand(Some(ValType::Ref(RefType {
6067 nullable: false,
6068 heap_type: HeapType::Concrete(ty),
6069 })));
6070
6071 instructions.push(Instruction::ArrayNew(ty));
6072 Ok(())
6073 }
6074
6075 #[inline]
6076 fn array_new_fixed_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6077 module.config.gc_enabled
6078 && module
6079 .array_types
6080 .iter()
6081 .any(|i| builder.field_type_on_stack(module, module.ty(*i).unwrap_array().0))
6082 }
6083
6084 fn array_new_fixed(
6085 u: &mut Unstructured,
6086 module: &Module,
6087 builder: &mut CodeBuilder,
6088 instructions: &mut Vec<Instruction>,
6089 ) -> Result<()> {
6090 let n = module
6091 .array_types
6092 .iter()
6093 .filter(|i| builder.field_type_on_stack(module, module.ty(**i).unwrap_array().0))
6094 .count();
6095 debug_assert!(n > 0);
6096 let i = u.int_in_range(0..=n - 1)?;
6097 let array_type_index = module
6098 .array_types
6099 .iter()
6100 .copied()
6101 .filter(|i| builder.field_type_on_stack(module, module.ty(*i).unwrap_array().0))
6102 .nth(i)
6103 .unwrap();
6104 let elem_ty = module
6105 .ty(array_type_index)
6106 .unwrap_array()
6107 .0
6108 .element_type
6109 .unpack();
6110
6111 let m = (0..builder.operands().len())
6112 .take_while(|i| builder.type_on_stack_at(module, *i, elem_ty))
6113 .count();
6114 debug_assert!(m > 0);
6115 let array_size = u.int_in_range(0..=m - 1)?;
6116 let array_size = u32::try_from(array_size).unwrap();
6117
6118 for _ in 0..array_size {
6119 builder.pop_operand();
6120 }
6121 builder.push_operand(Some(ValType::Ref(RefType {
6122 nullable: false,
6123 heap_type: HeapType::Concrete(array_type_index),
6124 })));
6125
6126 instructions.push(Instruction::ArrayNewFixed {
6127 array_type_index,
6128 array_size,
6129 });
6130 Ok(())
6131 }
6132
6133 #[inline]
6134 fn array_new_default_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6135 module.config.gc_enabled
6136 && builder.type_on_stack(module, ValType::I32)
6137 && module
6138 .array_types
6139 .iter()
6140 .any(|i| module.ty(*i).unwrap_array().0.element_type.is_defaultable())
6141 }
6142
6143 fn array_new_default(
6144 u: &mut Unstructured,
6145 module: &Module,
6146 builder: &mut CodeBuilder,
6147 instructions: &mut Vec<Instruction>,
6148 ) -> Result<()> {
6149 let n = module
6150 .array_types
6151 .iter()
6152 .filter(|i| {
6153 module
6154 .ty(**i)
6155 .unwrap_array()
6156 .0
6157 .element_type
6158 .is_defaultable()
6159 })
6160 .count();
6161 debug_assert!(n > 0);
6162 let i = u.int_in_range(0..=n - 1)?;
6163 let array_type_index = module
6164 .array_types
6165 .iter()
6166 .copied()
6167 .filter(|i| module.ty(*i).unwrap_array().0.element_type.is_defaultable())
6168 .nth(i)
6169 .unwrap();
6170
6171 builder.pop_operand();
6172 builder.push_operand(Some(ValType::Ref(RefType {
6173 nullable: false,
6174 heap_type: HeapType::Concrete(array_type_index),
6175 })));
6176 instructions.push(Instruction::ArrayNewDefault(array_type_index));
6177 Ok(())
6178 }
6179
6180 #[inline]
6181 fn array_new_data_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6182 module.config.gc_enabled
6183 && module.config.bulk_memory_enabled // Requires data count section
6184 && !module.config.disallow_traps
6185 && !module.data.is_empty()
6186 && builder.types_on_stack(module, &[ValType::I32, ValType::I32])
6187 && module.array_types.iter().any(|i| {
6188 let ty = module.ty(*i).unwrap_array().0.element_type.unpack();
6189 ty.is_numeric() | ty.is_vector()
6190 })
6191 }
6192
6193 fn array_new_data(
6194 u: &mut Unstructured,
6195 module: &Module,
6196 builder: &mut CodeBuilder,
6197 instructions: &mut Vec<Instruction>,
6198 ) -> Result<()> {
6199 let n = module
6200 .array_types
6201 .iter()
6202 .filter(|i| {
6203 let ty = module.ty(**i).unwrap_array().0.element_type.unpack();
6204 ty.is_numeric() | ty.is_vector()
6205 })
6206 .count();
6207 debug_assert!(n > 0);
6208 let i = u.int_in_range(0..=n - 1)?;
6209 let array_type_index = module
6210 .array_types
6211 .iter()
6212 .copied()
6213 .filter(|i| {
6214 let ty = module.ty(*i).unwrap_array().0.element_type.unpack();
6215 ty.is_numeric() | ty.is_vector()
6216 })
6217 .nth(i)
6218 .unwrap();
6219
6220 let m = module.data.len();
6221 debug_assert!(m > 0);
6222 let array_data_index = u.int_in_range(0..=m - 1)?;
6223 let array_data_index = u32::try_from(array_data_index).unwrap();
6224
6225 builder.pop_operand();
6226 builder.pop_operand();
6227 builder.push_operand(Some(ValType::Ref(RefType {
6228 nullable: false,
6229 heap_type: HeapType::Concrete(array_type_index),
6230 })));
6231 instructions.push(Instruction::ArrayNewData {
6232 array_type_index,
6233 array_data_index,
6234 });
6235 Ok(())
6236 }
6237
6238 fn module_has_elem_segment_of_array_type(module: &Module, ty: &ArrayType) -> bool {
6239 module
6240 .elems
6241 .iter()
6242 .any(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), ty.0.element_type.unpack()))
6243 }
6244
6245 #[inline]
6246 fn array_new_elem_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6247 module.config.gc_enabled
6248 && !module.config.disallow_traps
6249 && builder.types_on_stack(module, &[ValType::I32, ValType::I32])
6250 && module
6251 .array_types
6252 .iter()
6253 .any(|i| module_has_elem_segment_of_array_type(module, module.ty(*i).unwrap_array()))
6254 }
6255
6256 fn array_new_elem(
6257 u: &mut Unstructured,
6258 module: &Module,
6259 builder: &mut CodeBuilder,
6260 instructions: &mut Vec<Instruction>,
6261 ) -> Result<()> {
6262 let n = module
6263 .array_types
6264 .iter()
6265 .filter(|i| module_has_elem_segment_of_array_type(module, module.ty(**i).unwrap_array()))
6266 .count();
6267 debug_assert!(n > 0);
6268 let i = u.int_in_range(0..=n - 1)?;
6269 let array_type_index = module
6270 .array_types
6271 .iter()
6272 .copied()
6273 .filter(|i| module_has_elem_segment_of_array_type(module, module.ty(*i).unwrap_array()))
6274 .nth(i)
6275 .unwrap();
6276 let elem_ty = module
6277 .ty(array_type_index)
6278 .unwrap_array()
6279 .0
6280 .element_type
6281 .unpack();
6282
6283 let m = module
6284 .elems
6285 .iter()
6286 .filter(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6287 .count();
6288 debug_assert!(m > 0);
6289 let j = u.int_in_range(0..=m - 1)?;
6290 let (array_elem_index, _) = module
6291 .elems
6292 .iter()
6293 .enumerate()
6294 .filter(|(_, elem)| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6295 .nth(j)
6296 .unwrap();
6297 let array_elem_index = u32::try_from(array_elem_index).unwrap();
6298
6299 builder.pop_operand();
6300 builder.pop_operand();
6301 builder.push_operand(Some(ValType::Ref(RefType {
6302 nullable: false,
6303 heap_type: HeapType::Concrete(array_type_index),
6304 })));
6305
6306 instructions.push(Instruction::ArrayNewElem {
6307 array_type_index,
6308 array_elem_index,
6309 });
6310 Ok(())
6311 }
6312
6313 #[inline]
6314 fn array_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6315 module.config.gc_enabled
6316 && !module.config.disallow_traps // TODO: add support for disallowing traps
6317 && builder.type_on_stack(module, ValType::I32)
6318 && builder.concrete_array_ref_type_on_stack_at(module, 1).is_some()
6319 }
6320
6321 fn array_get(
6322 u: &mut Unstructured,
6323 module: &Module,
6324 builder: &mut CodeBuilder,
6325 instructions: &mut Vec<Instruction>,
6326 ) -> Result<()> {
6327 builder.pop_operand();
6328 let (_, array_type_index) = builder.pop_concrete_ref_type();
6329 let elem_ty = module.ty(array_type_index).unwrap_array().0.element_type;
6330 builder.push_operand(Some(elem_ty.unpack()));
6331 instructions.push(match elem_ty {
6332 StorageType::I8 | StorageType::I16 => {
6333 if u.arbitrary()? {
6334 Instruction::ArrayGetS(array_type_index)
6335 } else {
6336 Instruction::ArrayGetU(array_type_index)
6337 }
6338 }
6339 StorageType::Val(_) => Instruction::ArrayGet(array_type_index),
6340 });
6341 Ok(())
6342 }
6343
6344 #[inline]
6345 fn array_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6346 if !module.config.gc_enabled
6347 // TODO: implement disallowing traps.
6348 || module.config.disallow_traps
6349 || !builder.type_on_stack_at(module, 1, ValType::I32)
6350 {
6351 return false;
6352 }
6353 match builder.concrete_array_ref_type_on_stack_at(module, 2) {
6354 None => false,
6355 Some((_nullable, _idx, array_ty)) => {
6356 array_ty.0.mutable && builder.field_type_on_stack(module, array_ty.0)
6357 }
6358 }
6359 }
6360
6361 fn array_set(
6362 _u: &mut Unstructured,
6363 _module: &Module,
6364 builder: &mut CodeBuilder,
6365 instructions: &mut Vec<Instruction>,
6366 ) -> Result<()> {
6367 builder.pop_operand();
6368 builder.pop_operand();
6369 let (_, ty) = builder.pop_concrete_ref_type();
6370 instructions.push(Instruction::ArraySet(ty));
6371 Ok(())
6372 }
6373
6374 #[inline]
6375 fn array_len_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6376 module.config.gc_enabled && builder.type_on_stack(module, ValType::Ref(RefType::ARRAYREF))
6377 }
6378
6379 fn array_len(
6380 _u: &mut Unstructured,
6381 _module: &Module,
6382 builder: &mut CodeBuilder,
6383 instructions: &mut Vec<Instruction>,
6384 ) -> Result<()> {
6385 builder.pop_operand();
6386 builder.push_operand(Some(ValType::I32));
6387 instructions.push(Instruction::ArrayLen);
6388 Ok(())
6389 }
6390
6391 #[inline]
6392 fn array_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6393 if !module.config.gc_enabled
6394 // TODO: add support for disallowing traps
6395 || module.config.disallow_traps
6396 || !builder.type_on_stack_at(module, 0, ValType::I32)
6397 || !builder.type_on_stack_at(module, 2, ValType::I32)
6398 {
6399 return false;
6400 }
6401 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6402 None => return false,
6403 Some((_, _, array_ty)) => {
6404 array_ty.0.mutable && builder.field_type_on_stack_at(module, 1, array_ty.0)
6405 }
6406 }
6407 }
6408
6409 fn array_fill(
6410 _u: &mut Unstructured,
6411 _module: &Module,
6412 builder: &mut CodeBuilder,
6413 instructions: &mut Vec<Instruction>,
6414 ) -> Result<()> {
6415 builder.pop_operand();
6416 builder.pop_operand();
6417 builder.pop_operand();
6418 let (_, ty) = builder.pop_concrete_ref_type();
6419 instructions.push(Instruction::ArrayFill(ty));
6420 Ok(())
6421 }
6422
6423 #[inline]
6424 fn array_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6425 if !module.config.gc_enabled
6426 // TODO: add support for disallowing traps
6427 || module.config.disallow_traps
6428 || !builder.type_on_stack_at(module, 0, ValType::I32)
6429 || !builder.type_on_stack_at(module, 1, ValType::I32)
6430 || !builder.type_on_stack_at(module, 3, ValType::I32)
6431 {
6432 return false;
6433 }
6434 let x = match builder.concrete_array_ref_type_on_stack_at(module, 4) {
6435 None => return false,
6436 Some((_, _, x)) => x,
6437 };
6438 if !x.0.mutable {
6439 return false;
6440 }
6441 let y = match builder.concrete_array_ref_type_on_stack_at(module, 2) {
6442 None => return false,
6443 Some((_, _, y)) => y,
6444 };
6445 match (x.0.element_type, y.0.element_type) {
6446 (StorageType::I8, StorageType::I8) => true,
6447 (StorageType::I8, _) => false,
6448 (StorageType::I16, StorageType::I16) => true,
6449 (StorageType::I16, _) => false,
6450 (StorageType::Val(x), StorageType::Val(y)) => module.val_type_is_sub_type(y, x),
6451 (StorageType::Val(_), _) => false,
6452 }
6453 }
6454
6455 fn array_copy(
6456 _u: &mut Unstructured,
6457 _module: &Module,
6458 builder: &mut CodeBuilder,
6459 instructions: &mut Vec<Instruction>,
6460 ) -> Result<()> {
6461 builder.pop_operand();
6462 builder.pop_operand();
6463 let (_, array_type_index_src) = builder.pop_concrete_ref_type();
6464 builder.pop_operand();
6465 let (_, array_type_index_dst) = builder.pop_concrete_ref_type();
6466 instructions.push(Instruction::ArrayCopy {
6467 array_type_index_dst,
6468 array_type_index_src,
6469 });
6470 Ok(())
6471 }
6472
6473 #[inline]
6474 fn array_init_data_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6475 if !module.config.gc_enabled
6476 || !module.config.bulk_memory_enabled // Requires data count section
6477 || module.config.disallow_traps
6478 || module.data.is_empty()
6479 || !builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
6480 {
6481 return false;
6482 }
6483 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6484 None => return false,
6485 Some((_, _, ty)) => {
6486 let elem_ty = ty.0.element_type.unpack();
6487 ty.0.mutable && (elem_ty.is_numeric() || elem_ty.is_vector())
6488 }
6489 }
6490 }
6491
6492 fn array_init_data(
6493 u: &mut Unstructured,
6494 module: &Module,
6495 builder: &mut CodeBuilder,
6496 instructions: &mut Vec<Instruction>,
6497 ) -> Result<()> {
6498 builder.pop_operand();
6499 builder.pop_operand();
6500 builder.pop_operand();
6501 let (_, array_type_index) = builder.pop_concrete_ref_type();
6502
6503 let n = module.data.len();
6504 debug_assert!(n > 0);
6505 let array_data_index = u.int_in_range(0..=n - 1)?;
6506 let array_data_index = u32::try_from(array_data_index).unwrap();
6507
6508 instructions.push(Instruction::ArrayInitData {
6509 array_type_index,
6510 array_data_index,
6511 });
6512 Ok(())
6513 }
6514
6515 #[inline]
6516 fn array_init_elem_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6517 if !module.config.gc_enabled
6518 || module.config.disallow_traps
6519 || !builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
6520 {
6521 return false;
6522 }
6523 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6524 None => return false,
6525 Some((_, _, array_ty)) => {
6526 array_ty.0.mutable && module_has_elem_segment_of_array_type(module, &array_ty)
6527 }
6528 }
6529 }
6530
6531 fn array_init_elem(
6532 u: &mut Unstructured,
6533 module: &Module,
6534 builder: &mut CodeBuilder,
6535 instructions: &mut Vec<Instruction>,
6536 ) -> Result<()> {
6537 builder.pop_operand();
6538 builder.pop_operand();
6539 builder.pop_operand();
6540 let (_, array_type_index) = builder.pop_concrete_ref_type();
6541
6542 let elem_ty = module
6543 .ty(array_type_index)
6544 .unwrap_array()
6545 .0
6546 .element_type
6547 .unpack();
6548
6549 let n = module
6550 .elems
6551 .iter()
6552 .filter(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6553 .count();
6554 debug_assert!(n > 0);
6555 let j = u.int_in_range(0..=n - 1)?;
6556 let (array_elem_index, _) = module
6557 .elems
6558 .iter()
6559 .enumerate()
6560 .filter(|(_, elem)| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6561 .nth(j)
6562 .unwrap();
6563 let array_elem_index = u32::try_from(array_elem_index).unwrap();
6564
6565 instructions.push(Instruction::ArrayInitElem {
6566 array_type_index,
6567 array_elem_index,
6568 });
6569 Ok(())
6570 }
6571
6572 #[inline]
6573 fn ref_i31_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6574 module.config.gc_enabled && builder.type_on_stack(module, ValType::I32)
6575 }
6576
6577 fn ref_i31(
6578 _u: &mut Unstructured,
6579 _module: &Module,
6580 builder: &mut CodeBuilder,
6581 instructions: &mut Vec<Instruction>,
6582 ) -> Result<()> {
6583 builder.pop_operand();
6584 builder.push_operand(Some(ValType::Ref(RefType::I31REF)));
6585 instructions.push(Instruction::RefI31);
6586 Ok(())
6587 }
6588
6589 #[inline]
6590 fn i31_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6591 module.config.gc_enabled
6592 && builder.type_on_stack(
6593 module,
6594 ValType::Ref(RefType {
6595 nullable: true,
6596 heap_type: HeapType::I31,
6597 }),
6598 )
6599 }
6600
6601 fn i31_get(
6602 u: &mut Unstructured,
6603 _module: &Module,
6604 builder: &mut CodeBuilder,
6605 instructions: &mut Vec<Instruction>,
6606 ) -> Result<()> {
6607 builder.pop_operand();
6608 builder.push_operand(Some(ValType::I32));
6609 instructions.push(if u.arbitrary()? {
6610 Instruction::I31GetS
6611 } else {
6612 Instruction::I31GetU
6613 });
6614 Ok(())
6615 }
6616
6617 #[inline]
6618 fn any_convert_extern_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6619 module.config.gc_enabled
6620 && builder.type_on_stack(
6621 module,
6622 ValType::Ref(RefType {
6623 nullable: true,
6624 heap_type: HeapType::EXTERN,
6625 }),
6626 )
6627 }
6628
6629 fn any_convert_extern(
6630 u: &mut Unstructured,
6631 _module: &Module,
6632 builder: &mut CodeBuilder,
6633 instructions: &mut Vec<Instruction>,
6634 ) -> Result<()> {
6635 let nullable = match builder.pop_ref_type() {
6636 None => u.arbitrary()?,
6637 Some(r) => r.nullable,
6638 };
6639 builder.push_operand(Some(ValType::Ref(RefType {
6640 nullable,
6641 heap_type: HeapType::ANY,
6642 })));
6643 instructions.push(Instruction::AnyConvertExtern);
6644 Ok(())
6645 }
6646
6647 #[inline]
6648 fn extern_convert_any_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6649 module.config.gc_enabled && builder.type_on_stack(module, ValType::Ref(RefType::ANYREF))
6650 }
6651
6652 fn extern_convert_any(
6653 u: &mut Unstructured,
6654 _module: &Module,
6655 builder: &mut CodeBuilder,
6656 instructions: &mut Vec<Instruction>,
6657 ) -> Result<()> {
6658 let nullable = match builder.pop_ref_type() {
6659 None => u.arbitrary()?,
6660 Some(r) => r.nullable,
6661 };
6662 let ty = if nullable {
6663 RefType::EXTERNREF.nullable(true)
6664 } else {
6665 RefType::EXTERNREF
6666 };
6667 builder.push_operand(Some(ValType::Ref(ty)));
6668 instructions.push(Instruction::ExternConvertAny);
6669 Ok(())
6670 }
6671
6672 fn lane_index(u: &mut Unstructured, number_of_lanes: u8) -> Result<u8> {
6673 u.int_in_range(0..=(number_of_lanes - 1))
6674 }
6675
6676 #[inline]
6677 fn simd_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6678 !module.config.disallow_traps
6679 && module.config.simd_enabled
6680 && builder.types_on_stack(module, &[ValType::V128])
6681 }
6682
6683 #[inline]
6684 fn simd_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6685 !module.config.disallow_traps
6686 && module.config.relaxed_simd_enabled
6687 && builder.types_on_stack(module, &[ValType::V128])
6688 }
6689
6690 #[inline]
6691 fn simd_v128_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6692 !module.config.disallow_traps
6693 && module.config.simd_enabled
6694 && builder.types_on_stack(module, &[ValType::V128, ValType::V128])
6695 }
6696
6697 #[inline]
6698 fn simd_v128_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6699 !module.config.disallow_traps
6700 && module.config.relaxed_simd_enabled
6701 && builder.types_on_stack(module, &[ValType::V128, ValType::V128])
6702 }
6703
6704 #[inline]
6705 fn simd_v128_v128_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6706 !module.config.disallow_traps
6707 && module.config.simd_enabled
6708 && builder.types_on_stack(module, &[ValType::V128, ValType::V128, ValType::V128])
6709 }
6710
6711 #[inline]
6712 fn simd_v128_v128_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6713 !module.config.disallow_traps
6714 && module.config.relaxed_simd_enabled
6715 && builder.types_on_stack(module, &[ValType::V128, ValType::V128, ValType::V128])
6716 }
6717
6718 #[inline]
6719 fn simd_v128_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6720 !module.config.disallow_traps
6721 && module.config.simd_enabled
6722 && builder.types_on_stack(module, &[ValType::V128, ValType::I32])
6723 }
6724
6725 #[inline]
6726 fn simd_v128_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6727 !module.config.disallow_traps
6728 && module.config.simd_enabled
6729 && builder.types_on_stack(module, &[ValType::V128, ValType::I64])
6730 }
6731
6732 #[inline]
6733 fn simd_v128_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6734 !module.config.disallow_traps
6735 && module.config.simd_enabled
6736 && builder.types_on_stack(module, &[ValType::V128, ValType::F32])
6737 }
6738
6739 #[inline]
6740 fn simd_v128_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6741 !module.config.disallow_traps
6742 && module.config.simd_enabled
6743 && builder.types_on_stack(module, &[ValType::V128, ValType::F64])
6744 }
6745
6746 #[inline]
6747 fn simd_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6748 !module.config.disallow_traps
6749 && module.config.simd_enabled
6750 && builder.type_on_stack(module, ValType::I32)
6751 }
6752
6753 #[inline]
6754 fn simd_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6755 !module.config.disallow_traps
6756 && module.config.simd_enabled
6757 && builder.type_on_stack(module, ValType::I64)
6758 }
6759
6760 #[inline]
6761 fn simd_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6762 !module.config.disallow_traps
6763 && module.config.simd_enabled
6764 && builder.type_on_stack(module, ValType::F32)
6765 }
6766
6767 #[inline]
6768 fn simd_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6769 !module.config.disallow_traps
6770 && module.config.simd_enabled
6771 && builder.type_on_stack(module, ValType::F64)
6772 }
6773
6774 #[inline]
6775 fn simd_have_memory_and_offset(module: &Module, builder: &mut CodeBuilder) -> bool {
6776 !module.config.disallow_traps
6777 && module.config.simd_enabled
6778 && have_memory_and_offset(module, builder)
6779 }
6780
6781 #[inline]
6782 fn simd_have_memory_and_offset_and_v128(module: &Module, builder: &mut CodeBuilder) -> bool {
6783 !module.config.disallow_traps
6784 && module.config.simd_enabled
6785 && store_valid(module, builder, || ValType::V128)
6786 }
6787
6788 #[inline]
6789 fn simd_load_lane_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6790 // The SIMD non-trapping case is not yet implemented.
6791 !module.config.disallow_traps && simd_have_memory_and_offset_and_v128(module, builder)
6792 }
6793
6794 #[inline]
6795 fn simd_v128_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6796 !module.config.disallow_traps
6797 && module.config.simd_enabled
6798 && store_valid(module, builder, || ValType::V128)
6799 }
6800
6801 #[inline]
6802 fn simd_store_lane_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6803 // The SIMD non-trapping case is not yet implemented.
6804 !module.config.disallow_traps && simd_v128_store_valid(module, builder)
6805 }
6806
6807 #[inline]
6808 fn simd_enabled(module: &Module, _: &mut CodeBuilder) -> bool {
6809 module.config.simd_enabled
6810 }
6811
6812 macro_rules! simd_load {
6813 ($instruction:ident, $generator_fn_name:ident, $alignments:expr) => {
6814 fn $generator_fn_name(
6815 u: &mut Unstructured,
6816 module: &Module,
6817 builder: &mut CodeBuilder,
6818
6819 instructions: &mut Vec<Instruction>,
6820 ) -> Result<()> {
6821 let memarg = mem_arg(u, module, builder, $alignments)?;
6822 builder.push_operands(&[ValType::V128]);
6823 if module.config.disallow_traps {
6824 no_traps::load(
6825 Instruction::$instruction(memarg),
6826 module,
6827 builder,
6828 instructions,
6829 );
6830 } else {
6831 instructions.push(Instruction::$instruction(memarg));
6832 }
6833 Ok(())
6834 }
6835 };
6836 }
6837
6838 simd_load!(V128Load, v128_load, &[0, 1, 2, 3, 4]);
6839 simd_load!(V128Load8x8S, v128_load8x8s, &[0, 1, 2, 3]);
6840 simd_load!(V128Load8x8U, v128_load8x8u, &[0, 1, 2, 3]);
6841 simd_load!(V128Load16x4S, v128_load16x4s, &[0, 1, 2, 3]);
6842 simd_load!(V128Load16x4U, v128_load16x4u, &[0, 1, 2, 3]);
6843 simd_load!(V128Load32x2S, v128_load32x2s, &[0, 1, 2, 3]);
6844 simd_load!(V128Load32x2U, v128_load32x2u, &[0, 1, 2, 3]);
6845 simd_load!(V128Load8Splat, v128_load8_splat, &[0]);
6846 simd_load!(V128Load16Splat, v128_load16_splat, &[0, 1]);
6847 simd_load!(V128Load32Splat, v128_load32_splat, &[0, 1, 2]);
6848 simd_load!(V128Load64Splat, v128_load64_splat, &[0, 1, 2, 3]);
6849 simd_load!(V128Load32Zero, v128_load32_zero, &[0, 1, 2]);
6850 simd_load!(V128Load64Zero, v128_load64_zero, &[0, 1, 2, 3]);
6851
6852 fn v128_store(
6853 u: &mut Unstructured,
6854 module: &Module,
6855 builder: &mut CodeBuilder,
6856 instructions: &mut Vec<Instruction>,
6857 ) -> Result<()> {
6858 builder.pop_operands(module, &[ValType::V128]);
6859 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3, 4])?;
6860 if module.config.disallow_traps {
6861 no_traps::store(
6862 Instruction::V128Store(memarg),
6863 module,
6864 builder,
6865 instructions,
6866 );
6867 } else {
6868 instructions.push(Instruction::V128Store(memarg));
6869 }
6870 Ok(())
6871 }
6872
6873 macro_rules! simd_load_lane {
6874 ($instruction:ident, $generator_fn_name:ident, $alignments:expr, $number_of_lanes:expr) => {
6875 fn $generator_fn_name(
6876 u: &mut Unstructured,
6877 module: &Module,
6878 builder: &mut CodeBuilder,
6879 instructions: &mut Vec<Instruction>,
6880 ) -> Result<()> {
6881 builder.pop_operands(module, &[ValType::V128]);
6882 let memarg = mem_arg(u, module, builder, $alignments)?;
6883 builder.push_operands(&[ValType::V128]);
6884 instructions.push(Instruction::$instruction {
6885 memarg,
6886 lane: lane_index(u, $number_of_lanes)?,
6887 });
6888 Ok(())
6889 }
6890 };
6891 }
6892
6893 simd_load_lane!(V128Load8Lane, v128_load8_lane, &[0], 16);
6894 simd_load_lane!(V128Load16Lane, v128_load16_lane, &[0, 1], 8);
6895 simd_load_lane!(V128Load32Lane, v128_load32_lane, &[0, 1, 2], 4);
6896 simd_load_lane!(V128Load64Lane, v128_load64_lane, &[0, 1, 2, 3], 2);
6897
6898 macro_rules! simd_store_lane {
6899 ($instruction:ident, $generator_fn_name:ident, $alignments:expr, $number_of_lanes:expr) => {
6900 fn $generator_fn_name(
6901 u: &mut Unstructured,
6902 module: &Module,
6903 builder: &mut CodeBuilder,
6904 instructions: &mut Vec<Instruction>,
6905 ) -> Result<()> {
6906 builder.pop_operands(module, &[ValType::V128]);
6907 let memarg = mem_arg(u, module, builder, $alignments)?;
6908 instructions.push(Instruction::$instruction {
6909 memarg,
6910 lane: lane_index(u, $number_of_lanes)?,
6911 });
6912 Ok(())
6913 }
6914 };
6915 }
6916
6917 simd_store_lane!(V128Store8Lane, v128_store8_lane, &[0], 16);
6918 simd_store_lane!(V128Store16Lane, v128_store16_lane, &[0, 1], 8);
6919 simd_store_lane!(V128Store32Lane, v128_store32_lane, &[0, 1, 2], 4);
6920 simd_store_lane!(V128Store64Lane, v128_store64_lane, &[0, 1, 2, 3], 2);
6921
6922 fn v128_const(
6923 u: &mut Unstructured,
6924 _module: &Module,
6925 builder: &mut CodeBuilder,
6926 instructions: &mut Vec<Instruction>,
6927 ) -> Result<()> {
6928 builder.push_operands(&[ValType::V128]);
6929 let c = i128::from_le_bytes(u.arbitrary()?);
6930 instructions.push(Instruction::V128Const(c));
6931 Ok(())
6932 }
6933
6934 fn i8x16_shuffle(
6935 u: &mut Unstructured,
6936 module: &Module,
6937 builder: &mut CodeBuilder,
6938 instructions: &mut Vec<Instruction>,
6939 ) -> Result<()> {
6940 builder.pop_operands(module, &[ValType::V128, ValType::V128]);
6941 builder.push_operands(&[ValType::V128]);
6942 let mut lanes = [0; 16];
6943 for i in 0..16 {
6944 lanes[i] = u.int_in_range(0..=31)?;
6945 }
6946 instructions.push(Instruction::I8x16Shuffle(lanes));
6947 Ok(())
6948 }
6949
6950 macro_rules! simd_lane_access {
6951 ($instruction:ident, $generator_fn_name:ident, $in_types:expr => $out_types:expr, $number_of_lanes:expr) => {
6952 fn $generator_fn_name(
6953 u: &mut Unstructured,
6954 module: &Module,
6955 builder: &mut CodeBuilder,
6956 instructions: &mut Vec<Instruction>,
6957 ) -> Result<()> {
6958 builder.pop_operands(module, $in_types);
6959 builder.push_operands($out_types);
6960 instructions.push(Instruction::$instruction(lane_index(u, $number_of_lanes)?));
6961 Ok(())
6962 }
6963 };
6964 }
6965
6966 simd_lane_access!(I8x16ExtractLaneS, i8x16_extract_lane_s, &[ValType::V128] => &[ValType::I32], 16);
6967 simd_lane_access!(I8x16ExtractLaneU, i8x16_extract_lane_u, &[ValType::V128] => &[ValType::I32], 16);
6968 simd_lane_access!(I8x16ReplaceLane, i8x16_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 16);
6969 simd_lane_access!(I16x8ExtractLaneS, i16x8_extract_lane_s, &[ValType::V128] => &[ValType::I32], 8);
6970 simd_lane_access!(I16x8ExtractLaneU, i16x8_extract_lane_u, &[ValType::V128] => &[ValType::I32], 8);
6971 simd_lane_access!(I16x8ReplaceLane, i16x8_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 8);
6972 simd_lane_access!(I32x4ExtractLane, i32x4_extract_lane, &[ValType::V128] => &[ValType::I32], 4);
6973 simd_lane_access!(I32x4ReplaceLane, i32x4_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 4);
6974 simd_lane_access!(I64x2ExtractLane, i64x2_extract_lane, &[ValType::V128] => &[ValType::I64], 2);
6975 simd_lane_access!(I64x2ReplaceLane, i64x2_replace_lane, &[ValType::V128, ValType::I64] => &[ValType::V128], 2);
6976 simd_lane_access!(F32x4ExtractLane, f32x4_extract_lane, &[ValType::V128] => &[ValType::F32], 4);
6977 simd_lane_access!(F32x4ReplaceLane, f32x4_replace_lane, &[ValType::V128, ValType::F32] => &[ValType::V128], 4);
6978 simd_lane_access!(F64x2ExtractLane, f64x2_extract_lane, &[ValType::V128] => &[ValType::F64], 2);
6979 simd_lane_access!(F64x2ReplaceLane, f64x2_replace_lane, &[ValType::V128, ValType::F64] => &[ValType::V128], 2);
6980
6981 macro_rules! simd_binop {
6982 ($instruction:ident, $generator_fn_name:ident) => {
6983 fn $generator_fn_name(
6984 _: &mut Unstructured,
6985 module: &Module,
6986 builder: &mut CodeBuilder,
6987
6988 instructions: &mut Vec<Instruction>,
6989 ) -> Result<()> {
6990 builder.pop_operands(module, &[ValType::V128, ValType::V128]);
6991 builder.push_operands(&[ValType::V128]);
6992 instructions.push(Instruction::$instruction);
6993 Ok(())
6994 }
6995 };
6996 }
6997
6998 macro_rules! simd_unop {
6999 ($instruction:ident, $generator_fn_name:ident) => {
7000 simd_unop!($instruction, $generator_fn_name, V128 -> V128);
7001 };
7002
7003 ($instruction:ident, $generator_fn_name:ident, $in_type:ident -> $out_type:ident) => {
7004 fn $generator_fn_name(
7005 _: &mut Unstructured,
7006 module: &Module,
7007 builder: &mut CodeBuilder,
7008
7009 instructions: &mut Vec<Instruction>, ) -> Result<()> {
7010 builder.pop_operands(module, &[ValType::$in_type]);
7011 builder.push_operands(&[ValType::$out_type]);
7012 instructions.push(Instruction::$instruction);
7013 Ok(())
7014 }
7015 };
7016 }
7017
7018 macro_rules! simd_ternop {
7019 ($instruction:ident, $generator_fn_name:ident) => {
7020 fn $generator_fn_name(
7021 _: &mut Unstructured,
7022 module: &Module,
7023 builder: &mut CodeBuilder,
7024
7025 instructions: &mut Vec<Instruction>,
7026 ) -> Result<()> {
7027 builder.pop_operands(module, &[ValType::V128, ValType::V128, ValType::V128]);
7028 builder.push_operands(&[ValType::V128]);
7029 instructions.push(Instruction::$instruction);
7030 Ok(())
7031 }
7032 };
7033 }
7034
7035 macro_rules! simd_shift {
7036 ($instruction:ident, $generator_fn_name:ident) => {
7037 fn $generator_fn_name(
7038 _: &mut Unstructured,
7039 module: &Module,
7040 builder: &mut CodeBuilder,
7041
7042 instructions: &mut Vec<Instruction>,
7043 ) -> Result<()> {
7044 builder.pop_operands(module, &[ValType::V128, ValType::I32]);
7045 builder.push_operands(&[ValType::V128]);
7046 instructions.push(Instruction::$instruction);
7047 Ok(())
7048 }
7049 };
7050 }
7051
7052 simd_unop!(I8x16Splat, i8x16_splat, I32 -> V128);
7053 simd_unop!(I16x8Splat, i16x8_splat, I32 -> V128);
7054 simd_unop!(I32x4Splat, i32x4_splat, I32 -> V128);
7055 simd_unop!(I64x2Splat, i64x2_splat, I64 -> V128);
7056 simd_unop!(F32x4Splat, f32x4_splat, F32 -> V128);
7057 simd_unop!(F64x2Splat, f64x2_splat, F64 -> V128);
7058 simd_binop!(I8x16Swizzle, i8x16_swizzle);
7059 simd_binop!(I8x16Eq, i8x16_eq);
7060 simd_binop!(I8x16Ne, i8x16_ne);
7061 simd_binop!(I8x16LtS, i8x16_lt_s);
7062 simd_binop!(I8x16LtU, i8x16_lt_u);
7063 simd_binop!(I8x16GtS, i8x16_gt_s);
7064 simd_binop!(I8x16GtU, i8x16_gt_u);
7065 simd_binop!(I8x16LeS, i8x16_le_s);
7066 simd_binop!(I8x16LeU, i8x16_le_u);
7067 simd_binop!(I8x16GeS, i8x16_ge_s);
7068 simd_binop!(I8x16GeU, i8x16_ge_u);
7069 simd_binop!(I16x8Eq, i16x8_eq);
7070 simd_binop!(I16x8Ne, i16x8_ne);
7071 simd_binop!(I16x8LtS, i16x8_lt_s);
7072 simd_binop!(I16x8LtU, i16x8_lt_u);
7073 simd_binop!(I16x8GtS, i16x8_gt_s);
7074 simd_binop!(I16x8GtU, i16x8_gt_u);
7075 simd_binop!(I16x8LeS, i16x8_le_s);
7076 simd_binop!(I16x8LeU, i16x8_le_u);
7077 simd_binop!(I16x8GeS, i16x8_ge_s);
7078 simd_binop!(I16x8GeU, i16x8_ge_u);
7079 simd_binop!(I32x4Eq, i32x4_eq);
7080 simd_binop!(I32x4Ne, i32x4_ne);
7081 simd_binop!(I32x4LtS, i32x4_lt_s);
7082 simd_binop!(I32x4LtU, i32x4_lt_u);
7083 simd_binop!(I32x4GtS, i32x4_gt_s);
7084 simd_binop!(I32x4GtU, i32x4_gt_u);
7085 simd_binop!(I32x4LeS, i32x4_le_s);
7086 simd_binop!(I32x4LeU, i32x4_le_u);
7087 simd_binop!(I32x4GeS, i32x4_ge_s);
7088 simd_binop!(I32x4GeU, i32x4_ge_u);
7089 simd_binop!(I64x2Eq, i64x2_eq);
7090 simd_binop!(I64x2Ne, i64x2_ne);
7091 simd_binop!(I64x2LtS, i64x2_lt_s);
7092 simd_binop!(I64x2GtS, i64x2_gt_s);
7093 simd_binop!(I64x2LeS, i64x2_le_s);
7094 simd_binop!(I64x2GeS, i64x2_ge_s);
7095 simd_binop!(F32x4Eq, f32x4_eq);
7096 simd_binop!(F32x4Ne, f32x4_ne);
7097 simd_binop!(F32x4Lt, f32x4_lt);
7098 simd_binop!(F32x4Gt, f32x4_gt);
7099 simd_binop!(F32x4Le, f32x4_le);
7100 simd_binop!(F32x4Ge, f32x4_ge);
7101 simd_binop!(F64x2Eq, f64x2_eq);
7102 simd_binop!(F64x2Ne, f64x2_ne);
7103 simd_binop!(F64x2Lt, f64x2_lt);
7104 simd_binop!(F64x2Gt, f64x2_gt);
7105 simd_binop!(F64x2Le, f64x2_le);
7106 simd_binop!(F64x2Ge, f64x2_ge);
7107 simd_unop!(V128Not, v128_not);
7108 simd_binop!(V128And, v128_and);
7109 simd_binop!(V128AndNot, v128_and_not);
7110 simd_binop!(V128Or, v128_or);
7111 simd_binop!(V128Xor, v128_xor);
7112 simd_unop!(V128AnyTrue, v128_any_true, V128 -> I32);
7113 simd_unop!(I8x16Abs, i8x16_abs);
7114 simd_unop!(I8x16Neg, i8x16_neg);
7115 simd_unop!(I8x16Popcnt, i8x16_popcnt);
7116 simd_unop!(I8x16AllTrue, i8x16_all_true, V128 -> I32);
7117 simd_unop!(I8x16Bitmask, i8x16_bitmask, V128 -> I32);
7118 simd_binop!(I8x16NarrowI16x8S, i8x16_narrow_i16x8s);
7119 simd_binop!(I8x16NarrowI16x8U, i8x16_narrow_i16x8u);
7120 simd_shift!(I8x16Shl, i8x16_shl);
7121 simd_shift!(I8x16ShrS, i8x16_shr_s);
7122 simd_shift!(I8x16ShrU, i8x16_shr_u);
7123 simd_binop!(I8x16Add, i8x16_add);
7124 simd_binop!(I8x16AddSatS, i8x16_add_sat_s);
7125 simd_binop!(I8x16AddSatU, i8x16_add_sat_u);
7126 simd_binop!(I8x16Sub, i8x16_sub);
7127 simd_binop!(I8x16SubSatS, i8x16_sub_sat_s);
7128 simd_binop!(I8x16SubSatU, i8x16_sub_sat_u);
7129 simd_binop!(I8x16MinS, i8x16_min_s);
7130 simd_binop!(I8x16MinU, i8x16_min_u);
7131 simd_binop!(I8x16MaxS, i8x16_max_s);
7132 simd_binop!(I8x16MaxU, i8x16_max_u);
7133 simd_binop!(I8x16AvgrU, i8x16_avgr_u);
7134 simd_unop!(I16x8ExtAddPairwiseI8x16S, i16x8_extadd_pairwise_i8x16s);
7135 simd_unop!(I16x8ExtAddPairwiseI8x16U, i16x8_extadd_pairwise_i8x16u);
7136 simd_unop!(I16x8Abs, i16x8_abs);
7137 simd_unop!(I16x8Neg, i16x8_neg);
7138 simd_binop!(I16x8Q15MulrSatS, i16x8q15_mulr_sat_s);
7139 simd_unop!(I16x8AllTrue, i16x8_all_true, V128 -> I32);
7140 simd_unop!(I16x8Bitmask, i16x8_bitmask, V128 -> I32);
7141 simd_binop!(I16x8NarrowI32x4S, i16x8_narrow_i32x4s);
7142 simd_binop!(I16x8NarrowI32x4U, i16x8_narrow_i32x4u);
7143 simd_unop!(I16x8ExtendLowI8x16S, i16x8_extend_low_i8x16s);
7144 simd_unop!(I16x8ExtendHighI8x16S, i16x8_extend_high_i8x16s);
7145 simd_unop!(I16x8ExtendLowI8x16U, i16x8_extend_low_i8x16u);
7146 simd_unop!(I16x8ExtendHighI8x16U, i16x8_extend_high_i8x16u);
7147 simd_shift!(I16x8Shl, i16x8_shl);
7148 simd_shift!(I16x8ShrS, i16x8_shr_s);
7149 simd_shift!(I16x8ShrU, i16x8_shr_u);
7150 simd_binop!(I16x8Add, i16x8_add);
7151 simd_binop!(I16x8AddSatS, i16x8_add_sat_s);
7152 simd_binop!(I16x8AddSatU, i16x8_add_sat_u);
7153 simd_binop!(I16x8Sub, i16x8_sub);
7154 simd_binop!(I16x8SubSatS, i16x8_sub_sat_s);
7155 simd_binop!(I16x8SubSatU, i16x8_sub_sat_u);
7156 simd_binop!(I16x8Mul, i16x8_mul);
7157 simd_binop!(I16x8MinS, i16x8_min_s);
7158 simd_binop!(I16x8MinU, i16x8_min_u);
7159 simd_binop!(I16x8MaxS, i16x8_max_s);
7160 simd_binop!(I16x8MaxU, i16x8_max_u);
7161 simd_binop!(I16x8AvgrU, i16x8_avgr_u);
7162 simd_binop!(I16x8ExtMulLowI8x16S, i16x8_extmul_low_i8x16s);
7163 simd_binop!(I16x8ExtMulHighI8x16S, i16x8_extmul_high_i8x16s);
7164 simd_binop!(I16x8ExtMulLowI8x16U, i16x8_extmul_low_i8x16u);
7165 simd_binop!(I16x8ExtMulHighI8x16U, i16x8_extmul_high_i8x16u);
7166 simd_unop!(I32x4ExtAddPairwiseI16x8S, i32x4_extadd_pairwise_i16x8s);
7167 simd_unop!(I32x4ExtAddPairwiseI16x8U, i32x4_extadd_pairwise_i16x8u);
7168 simd_unop!(I32x4Abs, i32x4_abs);
7169 simd_unop!(I32x4Neg, i32x4_neg);
7170 simd_unop!(I32x4AllTrue, i32x4_all_true, V128 -> I32);
7171 simd_unop!(I32x4Bitmask, i32x4_bitmask, V128 -> I32);
7172 simd_unop!(I32x4ExtendLowI16x8S, i32x4_extend_low_i16x8s);
7173 simd_unop!(I32x4ExtendHighI16x8S, i32x4_extend_high_i16x8s);
7174 simd_unop!(I32x4ExtendLowI16x8U, i32x4_extend_low_i16x8u);
7175 simd_unop!(I32x4ExtendHighI16x8U, i32x4_extend_high_i16x8u);
7176 simd_shift!(I32x4Shl, i32x4_shl);
7177 simd_shift!(I32x4ShrS, i32x4_shr_s);
7178 simd_shift!(I32x4ShrU, i32x4_shr_u);
7179 simd_binop!(I32x4Add, i32x4_add);
7180 simd_binop!(I32x4Sub, i32x4_sub);
7181 simd_binop!(I32x4Mul, i32x4_mul);
7182 simd_binop!(I32x4MinS, i32x4_min_s);
7183 simd_binop!(I32x4MinU, i32x4_min_u);
7184 simd_binop!(I32x4MaxS, i32x4_max_s);
7185 simd_binop!(I32x4MaxU, i32x4_max_u);
7186 simd_binop!(I32x4DotI16x8S, i32x4_dot_i16x8s);
7187 simd_binop!(I32x4ExtMulLowI16x8S, i32x4_extmul_low_i16x8s);
7188 simd_binop!(I32x4ExtMulHighI16x8S, i32x4_extmul_high_i16x8s);
7189 simd_binop!(I32x4ExtMulLowI16x8U, i32x4_extmul_low_i16x8u);
7190 simd_binop!(I32x4ExtMulHighI16x8U, i32x4_extmul_high_i16x8u);
7191 simd_unop!(I64x2Abs, i64x2_abs);
7192 simd_unop!(I64x2Neg, i64x2_neg);
7193 simd_unop!(I64x2AllTrue, i64x2_all_true, V128 -> I32);
7194 simd_unop!(I64x2Bitmask, i64x2_bitmask, V128 -> I32);
7195 simd_unop!(I64x2ExtendLowI32x4S, i64x2_extend_low_i32x4s);
7196 simd_unop!(I64x2ExtendHighI32x4S, i64x2_extend_high_i32x4s);
7197 simd_unop!(I64x2ExtendLowI32x4U, i64x2_extend_low_i32x4u);
7198 simd_unop!(I64x2ExtendHighI32x4U, i64x2_extend_high_i32x4u);
7199 simd_shift!(I64x2Shl, i64x2_shl);
7200 simd_shift!(I64x2ShrS, i64x2_shr_s);
7201 simd_shift!(I64x2ShrU, i64x2_shr_u);
7202 simd_binop!(I64x2Add, i64x2_add);
7203 simd_binop!(I64x2Sub, i64x2_sub);
7204 simd_binop!(I64x2Mul, i64x2_mul);
7205 simd_binop!(I64x2ExtMulLowI32x4S, i64x2_extmul_low_i32x4s);
7206 simd_binop!(I64x2ExtMulHighI32x4S, i64x2_extmul_high_i32x4s);
7207 simd_binop!(I64x2ExtMulLowI32x4U, i64x2_extmul_low_i32x4u);
7208 simd_binop!(I64x2ExtMulHighI32x4U, i64x2_extmul_high_i32x4u);
7209 simd_unop!(F32x4Ceil, f32x4_ceil);
7210 simd_unop!(F32x4Floor, f32x4_floor);
7211 simd_unop!(F32x4Trunc, f32x4_trunc);
7212 simd_unop!(F32x4Nearest, f32x4_nearest);
7213 simd_unop!(F32x4Abs, f32x4_abs);
7214 simd_unop!(F32x4Neg, f32x4_neg);
7215 simd_unop!(F32x4Sqrt, f32x4_sqrt);
7216 simd_binop!(F32x4Add, f32x4_add);
7217 simd_binop!(F32x4Sub, f32x4_sub);
7218 simd_binop!(F32x4Mul, f32x4_mul);
7219 simd_binop!(F32x4Div, f32x4_div);
7220 simd_binop!(F32x4Min, f32x4_min);
7221 simd_binop!(F32x4Max, f32x4_max);
7222 simd_binop!(F32x4PMin, f32x4p_min);
7223 simd_binop!(F32x4PMax, f32x4p_max);
7224 simd_unop!(F64x2Ceil, f64x2_ceil);
7225 simd_unop!(F64x2Floor, f64x2_floor);
7226 simd_unop!(F64x2Trunc, f64x2_trunc);
7227 simd_unop!(F64x2Nearest, f64x2_nearest);
7228 simd_unop!(F64x2Abs, f64x2_abs);
7229 simd_unop!(F64x2Neg, f64x2_neg);
7230 simd_unop!(F64x2Sqrt, f64x2_sqrt);
7231 simd_binop!(F64x2Add, f64x2_add);
7232 simd_binop!(F64x2Sub, f64x2_sub);
7233 simd_binop!(F64x2Mul, f64x2_mul);
7234 simd_binop!(F64x2Div, f64x2_div);
7235 simd_binop!(F64x2Min, f64x2_min);
7236 simd_binop!(F64x2Max, f64x2_max);
7237 simd_binop!(F64x2PMin, f64x2p_min);
7238 simd_binop!(F64x2PMax, f64x2p_max);
7239 simd_unop!(I32x4TruncSatF32x4S, i32x4_trunc_sat_f32x4s);
7240 simd_unop!(I32x4TruncSatF32x4U, i32x4_trunc_sat_f32x4u);
7241 simd_unop!(F32x4ConvertI32x4S, f32x4_convert_i32x4s);
7242 simd_unop!(F32x4ConvertI32x4U, f32x4_convert_i32x4u);
7243 simd_unop!(I32x4TruncSatF64x2SZero, i32x4_trunc_sat_f64x2s_zero);
7244 simd_unop!(I32x4TruncSatF64x2UZero, i32x4_trunc_sat_f64x2u_zero);
7245 simd_unop!(F64x2ConvertLowI32x4S, f64x2_convert_low_i32x4s);
7246 simd_unop!(F64x2ConvertLowI32x4U, f64x2_convert_low_i32x4u);
7247 simd_unop!(F32x4DemoteF64x2Zero, f32x4_demote_f64x2_zero);
7248 simd_unop!(F64x2PromoteLowF32x4, f64x2_promote_low_f32x4);
7249 simd_ternop!(V128Bitselect, v128_bitselect);
7250 simd_binop!(I8x16RelaxedSwizzle, i8x16_relaxed_swizzle);
7251 simd_unop!(I32x4RelaxedTruncF32x4S, i32x4_relaxed_trunc_f32x4s);
7252 simd_unop!(I32x4RelaxedTruncF32x4U, i32x4_relaxed_trunc_f32x4u);
7253 simd_unop!(I32x4RelaxedTruncF64x2SZero, i32x4_relaxed_trunc_f64x2s_zero);
7254 simd_unop!(I32x4RelaxedTruncF64x2UZero, i32x4_relaxed_trunc_f64x2u_zero);
7255 simd_ternop!(F32x4RelaxedMadd, f32x4_relaxed_madd);
7256 simd_ternop!(F32x4RelaxedNmadd, f32x4_relaxed_nmadd);
7257 simd_ternop!(F64x2RelaxedMadd, f64x2_relaxed_madd);
7258 simd_ternop!(F64x2RelaxedNmadd, f64x2_relaxed_nmadd);
7259 simd_ternop!(I8x16RelaxedLaneselect, i8x16_relaxed_laneselect);
7260 simd_ternop!(I16x8RelaxedLaneselect, i16x8_relaxed_laneselect);
7261 simd_ternop!(I32x4RelaxedLaneselect, i32x4_relaxed_laneselect);
7262 simd_ternop!(I64x2RelaxedLaneselect, i64x2_relaxed_laneselect);
7263 simd_binop!(F32x4RelaxedMin, f32x4_relaxed_min);
7264 simd_binop!(F32x4RelaxedMax, f32x4_relaxed_max);
7265 simd_binop!(F64x2RelaxedMin, f64x2_relaxed_min);
7266 simd_binop!(F64x2RelaxedMax, f64x2_relaxed_max);
7267 simd_binop!(I16x8RelaxedQ15mulrS, i16x8_relaxed_q15mulr_s);
7268 simd_binop!(I16x8RelaxedDotI8x16I7x16S, i16x8_relaxed_dot_i8x16_i7x16_s);
7269 simd_ternop!(
7270 I32x4RelaxedDotI8x16I7x16AddS,
7271 i32x4_relaxed_dot_i8x16_i7x16_add_s
7272 );
7273
7274 #[inline]
7275 fn wide_arithmetic_binop128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
7276 module.config.wide_arithmetic_enabled && builder.types_on_stack(module, &[ValType::I64; 4])
7277 }
7278
7279 #[inline]
7280 fn wide_arithmetic_mul_wide_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
7281 module.config.wide_arithmetic_enabled && builder.types_on_stack(module, &[ValType::I64; 2])
7282 }
7283
7284 fn i64_add128(
7285 _: &mut Unstructured,
7286 module: &Module,
7287 builder: &mut CodeBuilder,
7288 instructions: &mut Vec<Instruction>,
7289 ) -> Result<()> {
7290 builder.pop_operands(module, &[ValType::I64; 4]);
7291 builder.push_operands(&[ValType::I64; 2]);
7292 instructions.push(Instruction::I64Add128);
7293 Ok(())
7294 }
7295
7296 fn i64_sub128(
7297 _: &mut Unstructured,
7298 module: &Module,
7299 builder: &mut CodeBuilder,
7300 instructions: &mut Vec<Instruction>,
7301 ) -> Result<()> {
7302 builder.pop_operands(module, &[ValType::I64; 4]);
7303 builder.push_operands(&[ValType::I64; 2]);
7304 instructions.push(Instruction::I64Sub128);
7305 Ok(())
7306 }
7307
7308 fn i64_mul_wide_s(
7309 _: &mut Unstructured,
7310 module: &Module,
7311 builder: &mut CodeBuilder,
7312 instructions: &mut Vec<Instruction>,
7313 ) -> Result<()> {
7314 builder.pop_operands(module, &[ValType::I64; 2]);
7315 builder.push_operands(&[ValType::I64; 2]);
7316 instructions.push(Instruction::I64MulWideS);
7317 Ok(())
7318 }
7319
7320 fn i64_mul_wide_u(
7321 _: &mut Unstructured,
7322 module: &Module,
7323 builder: &mut CodeBuilder,
7324 instructions: &mut Vec<Instruction>,
7325 ) -> Result<()> {
7326 builder.pop_operands(module, &[ValType::I64; 2]);
7327 builder.push_operands(&[ValType::I64; 2]);
7328 instructions.push(Instruction::I64MulWideU);
7329 Ok(())
7330 }