Skip to main content

winch_codegen/
visitor.rs

1//! This module is the central place for machine code emission.
2//! It defines an implementation of wasmparser's Visitor trait
3//! for `CodeGen`; which defines a visitor per op-code,
4//! which validates and dispatches to the corresponding
5//! machine code emitter.
6
7use crate::abi::RetArea;
8use crate::codegen::{
9    Callee, CatchInfo, CodeGen, CodeGenError, ConditionalBranch, ControlStackFrame, Emission,
10    FnCall, TryTableInfo, TypeConverter, UnconditionalBranch, control_index,
11};
12use crate::masm::{
13    AtomicWaitKind, DivKind, Extend, ExtractLaneKind, FloatCmpKind, IntCmpKind, LoadKind,
14    MacroAssembler, MulWideKind, OperandSize, RegImm, RemKind, ReplaceLaneKind, RmwOp,
15    RoundingMode, SPOffset, ShiftKind, Signed, SplatKind, SplatLoadKind, StoreKind, TruncKind,
16    V128AbsKind, V128AddKind, V128ConvertKind, V128ExtAddKind, V128ExtMulKind, V128ExtendKind,
17    V128LoadExtendKind, V128MaxKind, V128MinKind, V128MulKind, V128NarrowKind, V128NegKind,
18    V128SubKind, V128TruncKind, VectorCompareKind, VectorEqualityKind, Zero,
19};
20use crate::reg::{Reg, writable};
21use crate::stack::{TypedReg, Val};
22use crate::{Result, bail, format_err};
23use cranelift_codegen::ir::ExceptionTag;
24use regalloc2::RegClass;
25use smallvec::{SmallVec, smallvec};
26use wasmparser::{
27    BlockType, BrTable, HeapType, Ieee32, Ieee64, MemArg, Ordering, TryTable, V128, ValType,
28    VisitOperator, VisitSimdOperator,
29};
30use wasmtime_cranelift::TRAP_INDIRECT_CALL_TO_NULL;
31use wasmtime_environ::{
32    DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, TagIndex, TypeConvert,
33    TypeIndex, WasmCompositeInnerType, WasmHeapType, WasmValType, wasm_unsupported,
34};
35
36/// A macro to define unsupported WebAssembly operators.
37///
38/// This macro calls itself recursively;
39/// 1. It no-ops when matching a supported operator.
40/// 2. Defines the visitor function and panics when
41///    matching an unsupported operator.
42macro_rules! def_unsupported {
43    ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident $ann:tt)*) => {
44        $(
45            def_unsupported!(
46                emit
47                    $op
48
49                fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output {
50                    $($(let _ = $arg;)*)?
51
52                    Err(format_err!(CodeGenError::unimplemented_wasm_instruction()))
53                }
54            );
55        )*
56    };
57
58    (emit I32Const $($rest:tt)*) => {};
59    (emit I64Const $($rest:tt)*) => {};
60    (emit F32Const $($rest:tt)*) => {};
61    (emit F64Const $($rest:tt)*) => {};
62    (emit V128Const $($rest:tt)*) => {};
63    (emit F32Add $($rest:tt)*) => {};
64    (emit F64Add $($rest:tt)*) => {};
65    (emit F32Sub $($rest:tt)*) => {};
66    (emit F64Sub $($rest:tt)*) => {};
67    (emit F32Mul $($rest:tt)*) => {};
68    (emit F64Mul $($rest:tt)*) => {};
69    (emit F32Div $($rest:tt)*) => {};
70    (emit F64Div $($rest:tt)*) => {};
71    (emit F32Min $($rest:tt)*) => {};
72    (emit F64Min $($rest:tt)*) => {};
73    (emit F32Max $($rest:tt)*) => {};
74    (emit F64Max $($rest:tt)*) => {};
75    (emit F32Copysign $($rest:tt)*) => {};
76    (emit F64Copysign $($rest:tt)*) => {};
77    (emit F32Abs $($rest:tt)*) => {};
78    (emit F64Abs $($rest:tt)*) => {};
79    (emit F32Neg $($rest:tt)*) => {};
80    (emit F64Neg $($rest:tt)*) => {};
81    (emit F32Floor $($rest:tt)*) => {};
82    (emit F64Floor $($rest:tt)*) => {};
83    (emit F32Ceil $($rest:tt)*) => {};
84    (emit F64Ceil $($rest:tt)*) => {};
85    (emit F32Nearest $($rest:tt)*) => {};
86    (emit F64Nearest $($rest:tt)*) => {};
87    (emit F32Trunc $($rest:tt)*) => {};
88    (emit F64Trunc $($rest:tt)*) => {};
89    (emit F32Sqrt $($rest:tt)*) => {};
90    (emit F64Sqrt $($rest:tt)*) => {};
91    (emit F32Eq $($rest:tt)*) => {};
92    (emit F64Eq $($rest:tt)*) => {};
93    (emit F32Ne $($rest:tt)*) => {};
94    (emit F64Ne $($rest:tt)*) => {};
95    (emit F32Lt $($rest:tt)*) => {};
96    (emit F64Lt $($rest:tt)*) => {};
97    (emit F32Gt $($rest:tt)*) => {};
98    (emit F64Gt $($rest:tt)*) => {};
99    (emit F32Le $($rest:tt)*) => {};
100    (emit F64Le $($rest:tt)*) => {};
101    (emit F32Ge $($rest:tt)*) => {};
102    (emit F64Ge $($rest:tt)*) => {};
103    (emit F32ConvertI32S $($rest:tt)*) => {};
104    (emit F32ConvertI32U $($rest:tt)*) => {};
105    (emit F32ConvertI64S $($rest:tt)*) => {};
106    (emit F32ConvertI64U $($rest:tt)*) => {};
107    (emit F64ConvertI32S $($rest:tt)*) => {};
108    (emit F64ConvertI32U $($rest:tt)*) => {};
109    (emit F64ConvertI64S $($rest:tt)*) => {};
110    (emit F64ConvertI64U $($rest:tt)*) => {};
111    (emit F32ReinterpretI32 $($rest:tt)*) => {};
112    (emit F64ReinterpretI64 $($rest:tt)*) => {};
113    (emit F32DemoteF64 $($rest:tt)*) => {};
114    (emit F64PromoteF32 $($rest:tt)*) => {};
115    (emit I32Add $($rest:tt)*) => {};
116    (emit I64Add $($rest:tt)*) => {};
117    (emit I32Sub $($rest:tt)*) => {};
118    (emit I32Mul $($rest:tt)*) => {};
119    (emit I32DivS $($rest:tt)*) => {};
120    (emit I32DivU $($rest:tt)*) => {};
121    (emit I64DivS $($rest:tt)*) => {};
122    (emit I64DivU $($rest:tt)*) => {};
123    (emit I64RemU $($rest:tt)*) => {};
124    (emit I64RemS $($rest:tt)*) => {};
125    (emit I32RemU $($rest:tt)*) => {};
126    (emit I32RemS $($rest:tt)*) => {};
127    (emit I64Mul $($rest:tt)*) => {};
128    (emit I64Sub $($rest:tt)*) => {};
129    (emit I32Eq $($rest:tt)*) => {};
130    (emit I64Eq $($rest:tt)*) => {};
131    (emit I32Ne $($rest:tt)*) => {};
132    (emit I64Ne $($rest:tt)*) => {};
133    (emit I32LtS $($rest:tt)*) => {};
134    (emit I64LtS $($rest:tt)*) => {};
135    (emit I32LtU $($rest:tt)*) => {};
136    (emit I64LtU $($rest:tt)*) => {};
137    (emit I32LeS $($rest:tt)*) => {};
138    (emit I64LeS $($rest:tt)*) => {};
139    (emit I32LeU $($rest:tt)*) => {};
140    (emit I64LeU $($rest:tt)*) => {};
141    (emit I32GtS $($rest:tt)*) => {};
142    (emit I64GtS $($rest:tt)*) => {};
143    (emit I32GtU $($rest:tt)*) => {};
144    (emit I64GtU $($rest:tt)*) => {};
145    (emit I32GeS $($rest:tt)*) => {};
146    (emit I64GeS $($rest:tt)*) => {};
147    (emit I32GeU $($rest:tt)*) => {};
148    (emit I64GeU $($rest:tt)*) => {};
149    (emit I32Eqz $($rest:tt)*) => {};
150    (emit I64Eqz $($rest:tt)*) => {};
151    (emit I32And $($rest:tt)*) => {};
152    (emit I64And $($rest:tt)*) => {};
153    (emit I32Or $($rest:tt)*) => {};
154    (emit I64Or $($rest:tt)*) => {};
155    (emit I32Xor $($rest:tt)*) => {};
156    (emit I64Xor $($rest:tt)*) => {};
157    (emit I32Shl $($rest:tt)*) => {};
158    (emit I64Shl $($rest:tt)*) => {};
159    (emit I32ShrS $($rest:tt)*) => {};
160    (emit I64ShrS $($rest:tt)*) => {};
161    (emit I32ShrU $($rest:tt)*) => {};
162    (emit I64ShrU $($rest:tt)*) => {};
163    (emit I32Rotl $($rest:tt)*) => {};
164    (emit I64Rotl $($rest:tt)*) => {};
165    (emit I32Rotr $($rest:tt)*) => {};
166    (emit I64Rotr $($rest:tt)*) => {};
167    (emit I32Clz $($rest:tt)*) => {};
168    (emit I64Clz $($rest:tt)*) => {};
169    (emit I32Ctz $($rest:tt)*) => {};
170    (emit I64Ctz $($rest:tt)*) => {};
171    (emit I32Popcnt $($rest:tt)*) => {};
172    (emit I64Popcnt $($rest:tt)*) => {};
173    (emit I32WrapI64 $($rest:tt)*) => {};
174    (emit I64ExtendI32S $($rest:tt)*) => {};
175    (emit I64ExtendI32U $($rest:tt)*) => {};
176    (emit I32Extend8S $($rest:tt)*) => {};
177    (emit I32Extend16S $($rest:tt)*) => {};
178    (emit I64Extend8S $($rest:tt)*) => {};
179    (emit I64Extend16S $($rest:tt)*) => {};
180    (emit I64Extend32S $($rest:tt)*) => {};
181    (emit I32TruncF32S $($rest:tt)*) => {};
182    (emit I32TruncF32U $($rest:tt)*) => {};
183    (emit I32TruncF64S $($rest:tt)*) => {};
184    (emit I32TruncF64U $($rest:tt)*) => {};
185    (emit I64TruncF32S $($rest:tt)*) => {};
186    (emit I64TruncF32U $($rest:tt)*) => {};
187    (emit I64TruncF64S $($rest:tt)*) => {};
188    (emit I64TruncF64U $($rest:tt)*) => {};
189    (emit I32ReinterpretF32 $($rest:tt)*) => {};
190    (emit I64ReinterpretF64 $($rest:tt)*) => {};
191    (emit LocalGet $($rest:tt)*) => {};
192    (emit LocalSet $($rest:tt)*) => {};
193    (emit Call $($rest:tt)*) => {};
194    (emit ReturnCall $($rest:tt)*) => {};
195    (emit End $($rest:tt)*) => {};
196    (emit Nop $($rest:tt)*) => {};
197    (emit If $($rest:tt)*) => {};
198    (emit Else $($rest:tt)*) => {};
199    (emit Block $($rest:tt)*) => {};
200    (emit Loop $($rest:tt)*) => {};
201    (emit TryTable $($rest:tt)*) => {};
202    (emit Throw $($rest:tt)*) => {};
203    (emit ThrowRef $($rest:tt)*) => {};
204    (emit Br $($rest:tt)*) => {};
205    (emit BrIf $($rest:tt)*) => {};
206    (emit Return $($rest:tt)*) => {};
207    (emit Unreachable $($rest:tt)*) => {};
208    (emit LocalTee $($rest:tt)*) => {};
209    (emit GlobalGet $($rest:tt)*) => {};
210    (emit GlobalSet $($rest:tt)*) => {};
211    (emit Select $($rest:tt)*) => {};
212    (emit TypedSelect $($rest:tt)*) => {};
213    (emit RefNull $($rest:tt)*) => {};
214    (emit RefIsNull $($rest:tt)*) => {};
215    (emit RefFunc $($rest:tt)*) => {};
216    (emit Drop $($rest:tt)*) => {};
217    (emit BrTable $($rest:tt)*) => {};
218    (emit CallIndirect $($rest:tt)*) => {};
219    (emit ReturnCallIndirect $($rest:tt)*) => {};
220    (emit TableInit $($rest:tt)*) => {};
221    (emit TableCopy $($rest:tt)*) => {};
222    (emit TableGet $($rest:tt)*) => {};
223    (emit TableSet $($rest:tt)*) => {};
224    (emit TableGrow $($rest:tt)*) => {};
225    (emit TableSize $($rest:tt)*) => {};
226    (emit TableFill $($rest:tt)*) => {};
227    (emit ElemDrop $($rest:tt)*) => {};
228    (emit MemoryInit $($rest:tt)*) => {};
229    (emit MemoryCopy $($rest:tt)*) => {};
230    (emit DataDrop $($rest:tt)*) => {};
231    (emit MemoryFill $($rest:tt)*) => {};
232    (emit MemorySize $($rest:tt)*) => {};
233    (emit MemoryGrow $($rest:tt)*) => {};
234    (emit I32Load $($rest:tt)*) => {};
235    (emit I32Load8S $($rest:tt)*) => {};
236    (emit I32Load8U $($rest:tt)*) => {};
237    (emit I32Load16S $($rest:tt)*) => {};
238    (emit I32Load16U $($rest:tt)*) => {};
239    (emit I64Load8S $($rest:tt)*) => {};
240    (emit I64Load8U $($rest:tt)*) => {};
241    (emit I64Load16S $($rest:tt)*) => {};
242    (emit I64Load16U $($rest:tt)*) => {};
243    (emit I64Load32S $($rest:tt)*) => {};
244    (emit I64Load32U $($rest:tt)*) => {};
245    (emit I64Load $($rest:tt)*) => {};
246    (emit I32Store $($rest:tt)*) => {};
247    (emit I32Store8 $($rest:tt)*) => {};
248    (emit I32Store16 $($rest:tt)*) => {};
249    (emit I64Store $($rest:tt)*) => {};
250    (emit I64Store8 $($rest:tt)*) => {};
251    (emit I64Store16 $($rest:tt)*) => {};
252    (emit I64Store32 $($rest:tt)*) => {};
253    (emit F32Load $($rest:tt)*) => {};
254    (emit F32Store $($rest:tt)*) => {};
255    (emit F64Load $($rest:tt)*) => {};
256    (emit F64Store $($rest:tt)*) => {};
257    (emit I32TruncSatF32S $($rest:tt)*) => {};
258    (emit I32TruncSatF32U $($rest:tt)*) => {};
259    (emit I32TruncSatF64S $($rest:tt)*) => {};
260    (emit I32TruncSatF64U $($rest:tt)*) => {};
261    (emit I64TruncSatF32S $($rest:tt)*) => {};
262    (emit I64TruncSatF32U $($rest:tt)*) => {};
263    (emit I64TruncSatF64S $($rest:tt)*) => {};
264    (emit I64TruncSatF64U $($rest:tt)*) => {};
265    (emit V128Load $($rest:tt)*) => {};
266    (emit V128Store $($rest:tt)*) => {};
267    (emit I64Add128 $($rest:tt)*) => {};
268    (emit I64Sub128 $($rest:tt)*) => {};
269    (emit I64MulWideS $($rest:tt)*) => {};
270    (emit I64MulWideU $($rest:tt)*) => {};
271    (emit I32AtomicLoad8U $($rest:tt)*) => {};
272    (emit I32AtomicLoad16U $($rest:tt)*) => {};
273    (emit I32AtomicLoad $($rest:tt)*) => {};
274    (emit I64AtomicLoad8U $($rest:tt)*) => {};
275    (emit I64AtomicLoad16U $($rest:tt)*) => {};
276    (emit I64AtomicLoad32U $($rest:tt)*) => {};
277    (emit I64AtomicLoad $($rest:tt)*) => {};
278    (emit V128Load8x8S $($rest:tt)*) => {};
279    (emit V128Load8x8U $($rest:tt)*) => {};
280    (emit V128Load16x4S $($rest:tt)*) => {};
281    (emit V128Load16x4U $($rest:tt)*) => {};
282    (emit V128Load32x2S $($rest:tt)*) => {};
283    (emit V128Load32x2U $($rest:tt)*) => {};
284    (emit V128Load8Splat $($rest:tt)*) => {};
285    (emit V128Load16Splat $($rest:tt)*) => {};
286    (emit V128Load32Splat $($rest:tt)*) => {};
287    (emit V128Load64Splat $($rest:tt)*) => {};
288    (emit I8x16Splat $($rest:tt)*) => {};
289    (emit I16x8Splat $($rest:tt)*) => {};
290    (emit I32x4Splat $($rest:tt)*) => {};
291    (emit I64x2Splat $($rest:tt)*) => {};
292    (emit F32x4Splat $($rest:tt)*) => {};
293    (emit F64x2Splat $($rest:tt)*) => {};
294    (emit I32AtomicStore8 $($rest:tt)*) => {};
295    (emit I32AtomicStore16 $($rest:tt)*) => {};
296    (emit I32AtomicStore $($rest:tt)*) => {};
297    (emit I64AtomicStore8 $($rest:tt)*) => {};
298    (emit I64AtomicStore16 $($rest:tt)*) => {};
299    (emit I64AtomicStore32 $($rest:tt)*) => {};
300    (emit I64AtomicStore $($rest:tt)*) => {};
301    (emit I32AtomicRmw8AddU $($rest:tt)*) => {};
302    (emit I32AtomicRmw16AddU $($rest:tt)*) => {};
303    (emit I32AtomicRmwAdd $($rest:tt)*) => {};
304    (emit I64AtomicRmw8AddU $($rest:tt)*) => {};
305    (emit I64AtomicRmw16AddU $($rest:tt)*) => {};
306    (emit I64AtomicRmw32AddU $($rest:tt)*) => {};
307    (emit I64AtomicRmwAdd $($rest:tt)*) => {};
308    (emit I8x16Shuffle $($rest:tt)*) => {};
309    (emit I8x16Swizzle $($rest:tt)*) => {};
310    (emit I32AtomicRmw8SubU $($rest:tt)*) => {};
311    (emit I32AtomicRmw16SubU $($rest:tt)*) => {};
312    (emit I32AtomicRmwSub $($rest:tt)*) => {};
313    (emit I64AtomicRmw8SubU $($rest:tt)*) => {};
314    (emit I64AtomicRmw16SubU $($rest:tt)*) => {};
315    (emit I64AtomicRmw32SubU $($rest:tt)*) => {};
316    (emit I64AtomicRmwSub $($rest:tt)*) => {};
317    (emit I32AtomicRmw8XchgU $($rest:tt)*) => {};
318    (emit I32AtomicRmw16XchgU $($rest:tt)*) => {};
319    (emit I32AtomicRmwXchg $($rest:tt)*) => {};
320    (emit I64AtomicRmw8XchgU $($rest:tt)*) => {};
321    (emit I64AtomicRmw16XchgU $($rest:tt)*) => {};
322    (emit I64AtomicRmw32XchgU $($rest:tt)*) => {};
323    (emit I64AtomicRmwXchg $($rest:tt)*) => {};
324    (emit I8x16ExtractLaneS $($rest:tt)*) => {};
325    (emit I8x16ExtractLaneU $($rest:tt)*) => {};
326    (emit I16x8ExtractLaneS $($rest:tt)*) => {};
327    (emit I16x8ExtractLaneU $($rest:tt)*) => {};
328    (emit I32x4ExtractLane $($rest:tt)*) => {};
329    (emit I64x2ExtractLane $($rest:tt)*) => {};
330    (emit F32x4ExtractLane $($rest:tt)*) => {};
331    (emit F64x2ExtractLane $($rest:tt)*) => {};
332    (emit I32AtomicRmw8AndU $($rest:tt)*) => {};
333    (emit I32AtomicRmw16AndU $($rest:tt)*) => {};
334    (emit I32AtomicRmwAnd $($rest:tt)*) => {};
335    (emit I64AtomicRmw8AndU $($rest:tt)*) => {};
336    (emit I64AtomicRmw16AndU $($rest:tt)*) => {};
337    (emit I64AtomicRmw32AndU $($rest:tt)*) => {};
338    (emit I64AtomicRmwAnd $($rest:tt)*) => {};
339    (emit I32AtomicRmw8OrU $($rest:tt)*) => {};
340    (emit I32AtomicRmw16OrU $($rest:tt)*) => {};
341    (emit I32AtomicRmwOr $($rest:tt)*) => {};
342    (emit I64AtomicRmw8OrU $($rest:tt)*) => {};
343    (emit I64AtomicRmw16OrU $($rest:tt)*) => {};
344    (emit I64AtomicRmw32OrU $($rest:tt)*) => {};
345    (emit I64AtomicRmwOr $($rest:tt)*) => {};
346    (emit I32AtomicRmw8XorU $($rest:tt)*) => {};
347    (emit I32AtomicRmw16XorU $($rest:tt)*) => {};
348    (emit I32AtomicRmwXor $($rest:tt)*) => {};
349    (emit I64AtomicRmw8XorU $($rest:tt)*) => {};
350    (emit I64AtomicRmw16XorU $($rest:tt)*) => {};
351    (emit I64AtomicRmw32XorU $($rest:tt)*) => {};
352    (emit I64AtomicRmwXor $($rest:tt)*) => {};
353    (emit I8x16ReplaceLane $($rest:tt)*) => {};
354    (emit I16x8ReplaceLane $($rest:tt)*) => {};
355    (emit I32x4ReplaceLane $($rest:tt)*) => {};
356    (emit I64x2ReplaceLane $($rest:tt)*) => {};
357    (emit F32x4ReplaceLane $($rest:tt)*) => {};
358    (emit F64x2ReplaceLane $($rest:tt)*) => {};
359    (emit I32AtomicRmw8CmpxchgU $($rest:tt)*) => {};
360    (emit I32AtomicRmw16CmpxchgU $($rest:tt)*) => {};
361    (emit I32AtomicRmwCmpxchg $($rest:tt)*) => {};
362    (emit I64AtomicRmw8CmpxchgU $($rest:tt)*) => {};
363    (emit I64AtomicRmw16CmpxchgU $($rest:tt)*) => {};
364    (emit I64AtomicRmw32CmpxchgU $($rest:tt)*) => {};
365    (emit I64AtomicRmwCmpxchg $($rest:tt)*) => {};
366    (emit I8x16Eq $($rest:tt)*) => {};
367    (emit I16x8Eq $($rest:tt)*) => {};
368    (emit I32x4Eq $($rest:tt)*) => {};
369    (emit I64x2Eq $($rest:tt)*) => {};
370    (emit F32x4Eq $($rest:tt)*) => {};
371    (emit F64x2Eq $($rest:tt)*) => {};
372    (emit I8x16Ne $($rest:tt)*) => {};
373    (emit I16x8Ne $($rest:tt)*) => {};
374    (emit I32x4Ne $($rest:tt)*) => {};
375    (emit I64x2Ne $($rest:tt)*) => {};
376    (emit F32x4Ne $($rest:tt)*) => {};
377    (emit F64x2Ne $($rest:tt)*) => {};
378    (emit I8x16LtS $($rest:tt)*) => {};
379    (emit I8x16LtU $($rest:tt)*) => {};
380    (emit I16x8LtS $($rest:tt)*) => {};
381    (emit I16x8LtU $($rest:tt)*) => {};
382    (emit I32x4LtS $($rest:tt)*) => {};
383    (emit I32x4LtU $($rest:tt)*) => {};
384    (emit I64x2LtS $($rest:tt)*) => {};
385    (emit F32x4Lt $($rest:tt)*) => {};
386    (emit F64x2Lt $($rest:tt)*) => {};
387    (emit I8x16LeS $($rest:tt)*) => {};
388    (emit I8x16LeU $($rest:tt)*) => {};
389    (emit I16x8LeS $($rest:tt)*) => {};
390    (emit I16x8LeU $($rest:tt)*) => {};
391    (emit I32x4LeS $($rest:tt)*) => {};
392    (emit I32x4LeU $($rest:tt)*) => {};
393    (emit I64x2LeS $($rest:tt)*) => {};
394    (emit F32x4Le $($rest:tt)*) => {};
395    (emit F64x2Le $($rest:tt)*) => {};
396    (emit I8x16GtS $($rest:tt)*) => {};
397    (emit I8x16GtU $($rest:tt)*) => {};
398    (emit I16x8GtS $($rest:tt)*) => {};
399    (emit I16x8GtU $($rest:tt)*) => {};
400    (emit I32x4GtS $($rest:tt)*) => {};
401    (emit I32x4GtU $($rest:tt)*) => {};
402    (emit I64x2GtS $($rest:tt)*) => {};
403    (emit F32x4Gt $($rest:tt)*) => {};
404    (emit F64x2Gt $($rest:tt)*) => {};
405    (emit I8x16GeS $($rest:tt)*) => {};
406    (emit I8x16GeU $($rest:tt)*) => {};
407    (emit I16x8GeS $($rest:tt)*) => {};
408    (emit I16x8GeU $($rest:tt)*) => {};
409    (emit I32x4GeS $($rest:tt)*) => {};
410    (emit I32x4GeU $($rest:tt)*) => {};
411    (emit I64x2GeS $($rest:tt)*) => {};
412    (emit F32x4Ge $($rest:tt)*) => {};
413    (emit F64x2Ge $($rest:tt)*) => {};
414    (emit MemoryAtomicWait32 $($rest:tt)*) => {};
415    (emit MemoryAtomicWait64 $($rest:tt)*) => {};
416    (emit MemoryAtomicNotify $($rest:tt)*) => {};
417    (emit AtomicFence $($rest:tt)*) => {};
418    (emit V128Not $($rest:tt)*) => {};
419    (emit V128And $($rest:tt)*) => {};
420    (emit V128AndNot $($rest:tt)*) => {};
421    (emit V128Or $($rest:tt)*) => {};
422    (emit V128Xor $($rest:tt)*) => {};
423    (emit V128Bitselect $($rest:tt)*) => {};
424    (emit V128AnyTrue $($rest:tt)*) => {};
425    (emit V128Load8Lane $($rest:tt)*) => {};
426    (emit V128Load16Lane $($rest:tt)*) => {};
427    (emit V128Load32Lane $($rest:tt)*) => {};
428    (emit V128Load64Lane $($rest:tt)*) => {};
429    (emit V128Store8Lane $($rest:tt)*) => {};
430    (emit V128Store16Lane $($rest:tt)*) => {};
431    (emit V128Store32Lane $($rest:tt)*) => {};
432    (emit V128Store64Lane $($rest:tt)*) => {};
433    (emit F32x4ConvertI32x4S $($rest:tt)*) => {};
434    (emit F32x4ConvertI32x4U $($rest:tt)*) => {};
435    (emit F64x2ConvertLowI32x4S $($rest:tt)*) => {};
436    (emit F64x2ConvertLowI32x4U $($rest:tt)*) => {};
437    (emit I8x16NarrowI16x8S $($rest:tt)*) => {};
438    (emit I8x16NarrowI16x8U $($rest:tt)*) => {};
439    (emit I16x8NarrowI32x4S $($rest:tt)*) => {};
440    (emit I16x8NarrowI32x4U $($rest:tt)*) => {};
441    (emit F32x4DemoteF64x2Zero $($rest:tt)*) => {};
442    (emit F64x2PromoteLowF32x4 $($rest:tt)*) => {};
443    (emit I16x8ExtendLowI8x16S $($rest:tt)*) => {};
444    (emit I16x8ExtendHighI8x16S $($rest:tt)*) => {};
445    (emit I16x8ExtendLowI8x16U $($rest:tt)*) => {};
446    (emit I16x8ExtendHighI8x16U $($rest:tt)*) => {};
447    (emit I32x4ExtendLowI16x8S $($rest:tt)*) => {};
448    (emit I32x4ExtendHighI16x8S $($rest:tt)*) => {};
449    (emit I32x4ExtendLowI16x8U $($rest:tt)*) => {};
450    (emit I32x4ExtendHighI16x8U $($rest:tt)*) => {};
451    (emit I64x2ExtendLowI32x4S $($rest:tt)*) => {};
452    (emit I64x2ExtendHighI32x4S $($rest:tt)*) => {};
453    (emit I64x2ExtendLowI32x4U $($rest:tt)*) => {};
454    (emit I64x2ExtendHighI32x4U $($rest:tt)*) => {};
455    (emit I8x16Add $($rest:tt)*) => {};
456    (emit I16x8Add $($rest:tt)*) => {};
457    (emit I32x4Add $($rest:tt)*) => {};
458    (emit I64x2Add $($rest:tt)*) => {};
459    (emit I8x16Sub $($rest:tt)*) => {};
460    (emit I16x8Sub $($rest:tt)*) => {};
461    (emit I32x4Sub $($rest:tt)*) => {};
462    (emit I64x2Sub $($rest:tt)*) => {};
463    (emit I16x8Mul $($rest:tt)*) => {};
464    (emit I32x4Mul $($rest:tt)*) => {};
465    (emit I64x2Mul $($rest:tt)*) => {};
466    (emit I8x16AddSatS $($rest:tt)*) => {};
467    (emit I16x8AddSatS $($rest:tt)*) => {};
468    (emit I8x16AddSatU $($rest:tt)*) => {};
469    (emit I16x8AddSatU $($rest:tt)*) => {};
470    (emit I8x16SubSatS $($rest:tt)*) => {};
471    (emit I16x8SubSatS $($rest:tt)*) => {};
472    (emit I8x16SubSatU $($rest:tt)*) => {};
473    (emit I16x8SubSatU $($rest:tt)*) => {};
474    (emit I8x16Abs $($rest:tt)*) => {};
475    (emit I16x8Abs $($rest:tt)*) => {};
476    (emit I32x4Abs $($rest:tt)*) => {};
477    (emit I64x2Abs $($rest:tt)*) => {};
478    (emit F32x4Abs $($rest:tt)*) => {};
479    (emit F64x2Abs $($rest:tt)*) => {};
480    (emit I8x16Neg $($rest:tt)*) => {};
481    (emit I16x8Neg $($rest:tt)*) => {};
482    (emit I32x4Neg $($rest:tt)*) => {};
483    (emit I64x2Neg $($rest:tt)*) => {};
484    (emit I8x16Shl $($rest:tt)*) => {};
485    (emit I16x8Shl $($rest:tt)*) => {};
486    (emit I32x4Shl $($rest:tt)*) => {};
487    (emit I64x2Shl $($rest:tt)*) => {};
488    (emit I8x16ShrU $($rest:tt)*) => {};
489    (emit I16x8ShrU $($rest:tt)*) => {};
490    (emit I32x4ShrU $($rest:tt)*) => {};
491    (emit I64x2ShrU $($rest:tt)*) => {};
492    (emit I8x16ShrS $($rest:tt)*) => {};
493    (emit I16x8ShrS $($rest:tt)*) => {};
494    (emit I32x4ShrS $($rest:tt)*) => {};
495    (emit I64x2ShrS $($rest:tt)*) => {};
496    (emit I16x8Q15MulrSatS $($rest:tt)*) => {};
497    (emit I8x16AllTrue $($rest:tt)*) => {};
498    (emit I16x8AllTrue $($rest:tt)*) => {};
499    (emit I32x4AllTrue $($rest:tt)*) => {};
500    (emit I64x2AllTrue $($rest:tt)*) => {};
501    (emit I8x16Bitmask $($rest:tt)*) => {};
502    (emit I16x8Bitmask $($rest:tt)*) => {};
503    (emit I32x4Bitmask $($rest:tt)*) => {};
504    (emit I64x2Bitmask $($rest:tt)*) => {};
505    (emit I32x4TruncSatF32x4S $($rest:tt)*) => {};
506    (emit I32x4TruncSatF32x4U $($rest:tt)*) => {};
507    (emit I32x4TruncSatF64x2SZero $($rest:tt)*) => {};
508    (emit I32x4TruncSatF64x2UZero $($rest:tt)*) => {};
509    (emit I8x16MinU $($rest:tt)*) => {};
510    (emit I16x8MinU $($rest:tt)*) => {};
511    (emit I32x4MinU $($rest:tt)*) => {};
512    (emit I8x16MinS $($rest:tt)*) => {};
513    (emit I16x8MinS $($rest:tt)*) => {};
514    (emit I32x4MinS $($rest:tt)*) => {};
515    (emit I8x16MaxU $($rest:tt)*) => {};
516    (emit I16x8MaxU $($rest:tt)*) => {};
517    (emit I32x4MaxU $($rest:tt)*) => {};
518    (emit I8x16MaxS $($rest:tt)*) => {};
519    (emit I16x8MaxS $($rest:tt)*) => {};
520    (emit I32x4MaxS $($rest:tt)*) => {};
521    (emit I16x8ExtMulLowI8x16S $($rest:tt)*) => {};
522    (emit I32x4ExtMulLowI16x8S $($rest:tt)*) => {};
523    (emit I64x2ExtMulLowI32x4S $($rest:tt)*) => {};
524    (emit I16x8ExtMulHighI8x16S $($rest:tt)*) => {};
525    (emit I32x4ExtMulHighI16x8S $($rest:tt)*) => {};
526    (emit I64x2ExtMulHighI32x4S $($rest:tt)*) => {};
527    (emit I16x8ExtMulLowI8x16U $($rest:tt)*) => {};
528    (emit I32x4ExtMulLowI16x8U $($rest:tt)*) => {};
529    (emit I64x2ExtMulLowI32x4U $($rest:tt)*) => {};
530    (emit I16x8ExtMulHighI8x16U $($rest:tt)*) => {};
531    (emit I32x4ExtMulHighI16x8U $($rest:tt)*) => {};
532    (emit I64x2ExtMulHighI32x4U $($rest:tt)*) => {};
533    (emit I16x8ExtAddPairwiseI8x16U $($rest:tt)*) => {};
534    (emit I16x8ExtAddPairwiseI8x16S $($rest:tt)*) => {};
535    (emit I32x4ExtAddPairwiseI16x8U $($rest:tt)*) => {};
536    (emit I32x4ExtAddPairwiseI16x8S $($rest:tt)*) => {};
537    (emit I32x4DotI16x8S $($rest:tt)*) => {};
538    (emit I8x16Popcnt $($rest:tt)*) => {};
539    (emit I8x16AvgrU $($rest:tt)*) => {};
540    (emit I16x8AvgrU $($rest:tt)*) => {};
541    (emit F32x4Add $($rest:tt)*) => {};
542    (emit F64x2Add $($rest:tt)*) => {};
543    (emit F32x4Sub $($rest:tt)*) => {};
544    (emit F64x2Sub $($rest:tt)*) => {};
545    (emit F32x4Mul $($rest:tt)*) => {};
546    (emit F64x2Mul $($rest:tt)*) => {};
547    (emit F32x4Div $($rest:tt)*) => {};
548    (emit F64x2Div $($rest:tt)*) => {};
549    (emit F32x4Neg $($rest:tt)*) => {};
550    (emit F64x2Neg $($rest:tt)*) => {};
551    (emit F32x4Sqrt $($rest:tt)*) => {};
552    (emit F64x2Sqrt $($rest:tt)*) => {};
553    (emit F32x4Ceil $($rest:tt)*) => {};
554    (emit F64x2Ceil $($rest:tt)*) => {};
555    (emit F32x4Floor $($rest:tt)*) => {};
556    (emit F64x2Floor $($rest:tt)*) => {};
557    (emit F32x4Nearest $($rest:tt)*) => {};
558    (emit F64x2Nearest $($rest:tt)*) => {};
559    (emit F32x4Trunc $($rest:tt)*) => {};
560    (emit F64x2Trunc $($rest:tt)*) => {};
561    (emit V128Load32Zero $($rest:tt)*) => {};
562    (emit V128Load64Zero $($rest:tt)*) => {};
563    (emit F32x4PMin $($rest:tt)*) => {};
564    (emit F64x2PMin $($rest:tt)*) => {};
565    (emit F32x4PMax $($rest:tt)*) => {};
566    (emit F64x2PMax $($rest:tt)*) => {};
567    (emit F32x4Min $($rest:tt)*) => {};
568    (emit F64x2Min $($rest:tt)*) => {};
569    (emit F32x4Max $($rest:tt)*) => {};
570    (emit F64x2Max $($rest:tt)*) => {};
571
572    (emit $unsupported:tt $($rest:tt)*) => {$($rest)*};
573}
574
575impl<'a, 'translation, 'data, M> VisitOperator<'a> for CodeGen<'a, 'translation, 'data, M, Emission>
576where
577    M: MacroAssembler,
578{
579    type Output = Result<()>;
580
581    fn visit_i32_const(&mut self, val: i32) -> Self::Output {
582        self.context.stack.push(Val::i32(val));
583
584        Ok(())
585    }
586
587    fn visit_i64_const(&mut self, val: i64) -> Self::Output {
588        self.context.stack.push(Val::i64(val));
589        Ok(())
590    }
591
592    fn visit_f32_const(&mut self, val: Ieee32) -> Self::Output {
593        self.context.stack.push(Val::f32(val));
594        Ok(())
595    }
596
597    fn visit_f64_const(&mut self, val: Ieee64) -> Self::Output {
598        self.context.stack.push(Val::f64(val));
599        Ok(())
600    }
601
602    fn visit_f32_add(&mut self) -> Self::Output {
603        self.context.binop(
604            self.masm,
605            OperandSize::S32,
606            &mut |masm: &mut M, dst, src, size| {
607                masm.float_add(writable!(dst), dst, src, size)?;
608                masm.maybe_canonicalize_nan(writable!(dst), size)?;
609                Ok(TypedReg::f32(dst))
610            },
611        )
612    }
613
614    fn visit_f64_add(&mut self) -> Self::Output {
615        self.context.binop(
616            self.masm,
617            OperandSize::S64,
618            &mut |masm: &mut M, dst, src, size| {
619                masm.float_add(writable!(dst), dst, src, size)?;
620                masm.maybe_canonicalize_nan(writable!(dst), size)?;
621                Ok(TypedReg::f64(dst))
622            },
623        )
624    }
625
626    fn visit_f32_sub(&mut self) -> Self::Output {
627        self.context.binop(
628            self.masm,
629            OperandSize::S32,
630            &mut |masm: &mut M, dst, src, size| {
631                masm.float_sub(writable!(dst), dst, src, size)?;
632                masm.maybe_canonicalize_nan(writable!(dst), size)?;
633                Ok(TypedReg::f32(dst))
634            },
635        )
636    }
637
638    fn visit_f64_sub(&mut self) -> Self::Output {
639        self.context.binop(
640            self.masm,
641            OperandSize::S64,
642            &mut |masm: &mut M, dst, src, size| {
643                masm.float_sub(writable!(dst), dst, src, size)?;
644                masm.maybe_canonicalize_nan(writable!(dst), size)?;
645                Ok(TypedReg::f64(dst))
646            },
647        )
648    }
649
650    fn visit_f32_mul(&mut self) -> Self::Output {
651        self.context.binop(
652            self.masm,
653            OperandSize::S32,
654            &mut |masm: &mut M, dst, src, size| {
655                masm.float_mul(writable!(dst), dst, src, size)?;
656                masm.maybe_canonicalize_nan(writable!(dst), size)?;
657                Ok(TypedReg::f32(dst))
658            },
659        )
660    }
661
662    fn visit_f64_mul(&mut self) -> Self::Output {
663        self.context.binop(
664            self.masm,
665            OperandSize::S64,
666            &mut |masm: &mut M, dst, src, size| {
667                masm.float_mul(writable!(dst), dst, src, size)?;
668                masm.maybe_canonicalize_nan(writable!(dst), size)?;
669                Ok(TypedReg::f64(dst))
670            },
671        )
672    }
673
674    fn visit_f32_div(&mut self) -> Self::Output {
675        self.context.binop(
676            self.masm,
677            OperandSize::S32,
678            &mut |masm: &mut M, dst, src, size| {
679                masm.float_div(writable!(dst), dst, src, size)?;
680                masm.maybe_canonicalize_nan(writable!(dst), size)?;
681                Ok(TypedReg::f32(dst))
682            },
683        )
684    }
685
686    fn visit_f64_div(&mut self) -> Self::Output {
687        self.context.binop(
688            self.masm,
689            OperandSize::S64,
690            &mut |masm: &mut M, dst, src, size| {
691                masm.float_div(writable!(dst), dst, src, size)?;
692                masm.maybe_canonicalize_nan(writable!(dst), size)?;
693                Ok(TypedReg::f64(dst))
694            },
695        )
696    }
697
698    fn visit_f32_min(&mut self) -> Self::Output {
699        self.context.binop(
700            self.masm,
701            OperandSize::S32,
702            &mut |masm: &mut M, dst, src, size| {
703                masm.float_min(writable!(dst), dst, src, size)?;
704                masm.maybe_canonicalize_nan(writable!(dst), size)?;
705                Ok(TypedReg::f32(dst))
706            },
707        )
708    }
709
710    fn visit_f64_min(&mut self) -> Self::Output {
711        self.context.binop(
712            self.masm,
713            OperandSize::S64,
714            &mut |masm: &mut M, dst, src, size| {
715                masm.float_min(writable!(dst), dst, src, size)?;
716                masm.maybe_canonicalize_nan(writable!(dst), size)?;
717                Ok(TypedReg::f64(dst))
718            },
719        )
720    }
721
722    fn visit_f32_max(&mut self) -> Self::Output {
723        self.context.binop(
724            self.masm,
725            OperandSize::S32,
726            &mut |masm: &mut M, dst, src, size| {
727                masm.float_max(writable!(dst), dst, src, size)?;
728                masm.maybe_canonicalize_nan(writable!(dst), size)?;
729                Ok(TypedReg::f32(dst))
730            },
731        )
732    }
733
734    fn visit_f64_max(&mut self) -> Self::Output {
735        self.context.binop(
736            self.masm,
737            OperandSize::S64,
738            &mut |masm: &mut M, dst, src, size| {
739                masm.float_max(writable!(dst), dst, src, size)?;
740                masm.maybe_canonicalize_nan(writable!(dst), size)?;
741                Ok(TypedReg::f64(dst))
742            },
743        )
744    }
745
746    fn visit_f32_copysign(&mut self) -> Self::Output {
747        self.context.binop(
748            self.masm,
749            OperandSize::S32,
750            &mut |masm: &mut M, dst, src, size| {
751                masm.float_copysign(writable!(dst), dst, src, size)?;
752                Ok(TypedReg::f32(dst))
753            },
754        )
755    }
756
757    fn visit_f64_copysign(&mut self) -> Self::Output {
758        self.context.binop(
759            self.masm,
760            OperandSize::S64,
761            &mut |masm: &mut M, dst, src, size| {
762                masm.float_copysign(writable!(dst), dst, src, size)?;
763                Ok(TypedReg::f64(dst))
764            },
765        )
766    }
767
768    fn visit_f32_abs(&mut self) -> Self::Output {
769        self.context.unop(self.masm, |masm, reg| {
770            masm.float_abs(writable!(reg), OperandSize::S32)?;
771            Ok(TypedReg::f32(reg))
772        })
773    }
774
775    fn visit_f64_abs(&mut self) -> Self::Output {
776        self.context.unop(self.masm, |masm, reg| {
777            masm.float_abs(writable!(reg), OperandSize::S64)?;
778            Ok(TypedReg::f64(reg))
779        })
780    }
781
782    fn visit_f32_neg(&mut self) -> Self::Output {
783        self.context.unop(self.masm, |masm, reg| {
784            masm.float_neg(writable!(reg), OperandSize::S32)?;
785            Ok(TypedReg::f32(reg))
786        })
787    }
788
789    fn visit_f64_neg(&mut self) -> Self::Output {
790        self.context.unop(self.masm, |masm, reg| {
791            masm.float_neg(writable!(reg), OperandSize::S64)?;
792            Ok(TypedReg::f64(reg))
793        })
794    }
795
796    fn visit_f32_floor(&mut self) -> Self::Output {
797        self.masm.float_round(
798            RoundingMode::Down,
799            &mut self.env,
800            &mut self.context,
801            OperandSize::S32,
802            |env, cx, masm| {
803                let builtin = env.builtins.floor_f32::<M::ABI>()?;
804                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
805            },
806        )?;
807        let result = self.context.pop_to_reg(self.masm, None)?;
808        self.masm
809            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S32)?;
810        self.context.stack.push(result.into());
811        Ok(())
812    }
813
814    fn visit_f64_floor(&mut self) -> Self::Output {
815        self.masm.float_round(
816            RoundingMode::Down,
817            &mut self.env,
818            &mut self.context,
819            OperandSize::S64,
820            |env, cx, masm| {
821                let builtin = env.builtins.floor_f64::<M::ABI>()?;
822                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
823            },
824        )?;
825        let result = self.context.pop_to_reg(self.masm, None)?;
826        self.masm
827            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S64)?;
828        self.context.stack.push(result.into());
829        Ok(())
830    }
831
832    fn visit_f32_ceil(&mut self) -> Self::Output {
833        self.masm.float_round(
834            RoundingMode::Up,
835            &mut self.env,
836            &mut self.context,
837            OperandSize::S32,
838            |env, cx, masm| {
839                let builtin = env.builtins.ceil_f32::<M::ABI>()?;
840                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
841            },
842        )?;
843        let result = self.context.pop_to_reg(self.masm, None)?;
844        self.masm
845            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S32)?;
846        self.context.stack.push(result.into());
847        Ok(())
848    }
849
850    fn visit_f64_ceil(&mut self) -> Self::Output {
851        self.masm.float_round(
852            RoundingMode::Up,
853            &mut self.env,
854            &mut self.context,
855            OperandSize::S64,
856            |env, cx, masm| {
857                let builtin = env.builtins.ceil_f64::<M::ABI>()?;
858                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
859            },
860        )?;
861        let result = self.context.pop_to_reg(self.masm, None)?;
862        self.masm
863            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S64)?;
864        self.context.stack.push(result.into());
865        Ok(())
866    }
867
868    fn visit_f32_nearest(&mut self) -> Self::Output {
869        self.masm.float_round(
870            RoundingMode::Nearest,
871            &mut self.env,
872            &mut self.context,
873            OperandSize::S32,
874            |env, cx, masm| {
875                let builtin = env.builtins.nearest_f32::<M::ABI>()?;
876                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
877            },
878        )?;
879        let result = self.context.pop_to_reg(self.masm, None)?;
880        self.masm
881            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S32)?;
882        self.context.stack.push(result.into());
883        Ok(())
884    }
885
886    fn visit_f64_nearest(&mut self) -> Self::Output {
887        self.masm.float_round(
888            RoundingMode::Nearest,
889            &mut self.env,
890            &mut self.context,
891            OperandSize::S64,
892            |env, cx, masm| {
893                let builtin = env.builtins.nearest_f64::<M::ABI>()?;
894                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
895            },
896        )?;
897        let result = self.context.pop_to_reg(self.masm, None)?;
898        self.masm
899            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S64)?;
900        self.context.stack.push(result.into());
901        Ok(())
902    }
903
904    fn visit_f32_trunc(&mut self) -> Self::Output {
905        self.masm.float_round(
906            RoundingMode::Zero,
907            &mut self.env,
908            &mut self.context,
909            OperandSize::S32,
910            |env, cx, masm| {
911                let builtin = env.builtins.trunc_f32::<M::ABI>()?;
912                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
913            },
914        )?;
915        let result = self.context.pop_to_reg(self.masm, None)?;
916        self.masm
917            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S32)?;
918        self.context.stack.push(result.into());
919        Ok(())
920    }
921
922    fn visit_f64_trunc(&mut self) -> Self::Output {
923        self.masm.float_round(
924            RoundingMode::Zero,
925            &mut self.env,
926            &mut self.context,
927            OperandSize::S64,
928            |env, cx, masm| {
929                let builtin = env.builtins.trunc_f64::<M::ABI>()?;
930                FnCall::emit::<M>(env, masm, cx, Callee::Builtin(builtin))
931            },
932        )?;
933        let result = self.context.pop_to_reg(self.masm, None)?;
934        self.masm
935            .maybe_canonicalize_nan(writable!(result.into()), OperandSize::S64)?;
936        self.context.stack.push(result.into());
937        Ok(())
938    }
939
940    fn visit_f32_sqrt(&mut self) -> Self::Output {
941        self.context.unop(self.masm, |masm, reg| {
942            masm.float_sqrt(writable!(reg), reg, OperandSize::S32)?;
943            masm.maybe_canonicalize_nan(writable!(reg), OperandSize::S32)?;
944            Ok(TypedReg::f32(reg))
945        })
946    }
947
948    fn visit_f64_sqrt(&mut self) -> Self::Output {
949        self.context.unop(self.masm, |masm, reg| {
950            masm.float_sqrt(writable!(reg), reg, OperandSize::S64)?;
951            masm.maybe_canonicalize_nan(writable!(reg), OperandSize::S64)?;
952            Ok(TypedReg::f64(reg))
953        })
954    }
955
956    fn visit_f32_eq(&mut self) -> Self::Output {
957        self.context.float_cmp_op(
958            self.masm,
959            OperandSize::S32,
960            &mut |masm: &mut M, dst, src1, src2, size| {
961                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Eq, size)
962            },
963        )
964    }
965
966    fn visit_f64_eq(&mut self) -> Self::Output {
967        self.context.float_cmp_op(
968            self.masm,
969            OperandSize::S64,
970            &mut |masm: &mut M, dst, src1, src2, size| {
971                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Eq, size)
972            },
973        )
974    }
975
976    fn visit_f32_ne(&mut self) -> Self::Output {
977        self.context.float_cmp_op(
978            self.masm,
979            OperandSize::S32,
980            &mut |masm: &mut M, dst, src1, src2, size| {
981                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Ne, size)
982            },
983        )
984    }
985
986    fn visit_f64_ne(&mut self) -> Self::Output {
987        self.context.float_cmp_op(
988            self.masm,
989            OperandSize::S64,
990            &mut |masm: &mut M, dst, src1, src2, size| {
991                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Ne, size)
992            },
993        )
994    }
995
996    fn visit_f32_lt(&mut self) -> Self::Output {
997        self.context.float_cmp_op(
998            self.masm,
999            OperandSize::S32,
1000            &mut |masm: &mut M, dst, src1, src2, size| {
1001                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Lt, size)
1002            },
1003        )
1004    }
1005
1006    fn visit_f64_lt(&mut self) -> Self::Output {
1007        self.context.float_cmp_op(
1008            self.masm,
1009            OperandSize::S64,
1010            &mut |masm: &mut M, dst, src1, src2, size| {
1011                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Lt, size)
1012            },
1013        )
1014    }
1015
1016    fn visit_f32_gt(&mut self) -> Self::Output {
1017        self.context.float_cmp_op(
1018            self.masm,
1019            OperandSize::S32,
1020            &mut |masm: &mut M, dst, src1, src2, size| {
1021                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Gt, size)
1022            },
1023        )
1024    }
1025
1026    fn visit_f64_gt(&mut self) -> Self::Output {
1027        self.context.float_cmp_op(
1028            self.masm,
1029            OperandSize::S64,
1030            &mut |masm: &mut M, dst, src1, src2, size| {
1031                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Gt, size)
1032            },
1033        )
1034    }
1035
1036    fn visit_f32_le(&mut self) -> Self::Output {
1037        self.context.float_cmp_op(
1038            self.masm,
1039            OperandSize::S32,
1040            &mut |masm: &mut M, dst, src1, src2, size| {
1041                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Le, size)
1042            },
1043        )
1044    }
1045
1046    fn visit_f64_le(&mut self) -> Self::Output {
1047        self.context.float_cmp_op(
1048            self.masm,
1049            OperandSize::S64,
1050            &mut |masm: &mut M, dst, src1, src2, size| {
1051                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Le, size)
1052            },
1053        )
1054    }
1055
1056    fn visit_f32_ge(&mut self) -> Self::Output {
1057        self.context.float_cmp_op(
1058            self.masm,
1059            OperandSize::S32,
1060            &mut |masm: &mut M, dst, src1, src2, size| {
1061                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Ge, size)
1062            },
1063        )
1064    }
1065
1066    fn visit_f64_ge(&mut self) -> Self::Output {
1067        self.context.float_cmp_op(
1068            self.masm,
1069            OperandSize::S64,
1070            &mut |masm: &mut M, dst, src1, src2, size| {
1071                masm.float_cmp_with_set(writable!(dst), src1, src2, FloatCmpKind::Ge, size)
1072            },
1073        )
1074    }
1075
1076    fn visit_f32_convert_i32_s(&mut self) -> Self::Output {
1077        self.context
1078            .convert_op(self.masm, WasmValType::F32, |masm, dst, src, dst_size| {
1079                masm.signed_convert(writable!(dst), src, OperandSize::S32, dst_size)
1080            })
1081    }
1082
1083    fn visit_f32_convert_i32_u(&mut self) -> Self::Output {
1084        self.context.convert_op_with_tmp_reg(
1085            self.masm,
1086            WasmValType::F32,
1087            RegClass::Int,
1088            |masm, dst, src, tmp_gpr, dst_size| {
1089                masm.unsigned_convert(writable!(dst), src, tmp_gpr, OperandSize::S32, dst_size)
1090            },
1091        )
1092    }
1093
1094    fn visit_f32_convert_i64_s(&mut self) -> Self::Output {
1095        self.context
1096            .convert_op(self.masm, WasmValType::F32, |masm, dst, src, dst_size| {
1097                masm.signed_convert(writable!(dst), src, OperandSize::S64, dst_size)
1098            })
1099    }
1100
1101    fn visit_f32_convert_i64_u(&mut self) -> Self::Output {
1102        self.context.convert_op_with_tmp_reg(
1103            self.masm,
1104            WasmValType::F32,
1105            RegClass::Int,
1106            |masm, dst, src, tmp_gpr, dst_size| {
1107                masm.unsigned_convert(writable!(dst), src, tmp_gpr, OperandSize::S64, dst_size)
1108            },
1109        )
1110    }
1111
1112    fn visit_f64_convert_i32_s(&mut self) -> Self::Output {
1113        self.context
1114            .convert_op(self.masm, WasmValType::F64, |masm, dst, src, dst_size| {
1115                masm.signed_convert(writable!(dst), src, OperandSize::S32, dst_size)
1116            })
1117    }
1118
1119    fn visit_f64_convert_i32_u(&mut self) -> Self::Output {
1120        self.context.convert_op_with_tmp_reg(
1121            self.masm,
1122            WasmValType::F64,
1123            RegClass::Int,
1124            |masm, dst, src, tmp_gpr, dst_size| {
1125                masm.unsigned_convert(writable!(dst), src, tmp_gpr, OperandSize::S32, dst_size)
1126            },
1127        )
1128    }
1129
1130    fn visit_f64_convert_i64_s(&mut self) -> Self::Output {
1131        self.context
1132            .convert_op(self.masm, WasmValType::F64, |masm, dst, src, dst_size| {
1133                masm.signed_convert(writable!(dst), src, OperandSize::S64, dst_size)
1134            })
1135    }
1136
1137    fn visit_f64_convert_i64_u(&mut self) -> Self::Output {
1138        self.context.convert_op_with_tmp_reg(
1139            self.masm,
1140            WasmValType::F64,
1141            RegClass::Int,
1142            |masm, dst, src, tmp_gpr, dst_size| {
1143                masm.unsigned_convert(writable!(dst), src, tmp_gpr, OperandSize::S64, dst_size)
1144            },
1145        )
1146    }
1147
1148    fn visit_f32_reinterpret_i32(&mut self) -> Self::Output {
1149        self.context
1150            .convert_op(self.masm, WasmValType::F32, |masm, dst, src, size| {
1151                masm.reinterpret_int_as_float(writable!(dst), src, size)
1152            })
1153    }
1154
1155    fn visit_f64_reinterpret_i64(&mut self) -> Self::Output {
1156        self.context
1157            .convert_op(self.masm, WasmValType::F64, |masm, dst, src, size| {
1158                masm.reinterpret_int_as_float(writable!(dst), src, size)
1159            })
1160    }
1161
1162    fn visit_f32_demote_f64(&mut self) -> Self::Output {
1163        self.context.unop(self.masm, |masm, reg| {
1164            masm.demote(writable!(reg), reg)?;
1165            masm.maybe_canonicalize_nan(writable!(reg), OperandSize::S32)?;
1166            Ok(TypedReg::f32(reg))
1167        })
1168    }
1169
1170    fn visit_f64_promote_f32(&mut self) -> Self::Output {
1171        self.context.unop(self.masm, |masm, reg| {
1172            masm.promote(writable!(reg), reg)?;
1173            masm.maybe_canonicalize_nan(writable!(reg), OperandSize::S64)?;
1174            Ok(TypedReg::f64(reg))
1175        })
1176    }
1177
1178    fn visit_i32_add(&mut self) -> Self::Output {
1179        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1180            masm.add(writable!(dst), dst, src, size)?;
1181            Ok(TypedReg::i32(dst))
1182        })
1183    }
1184
1185    fn visit_i64_add(&mut self) -> Self::Output {
1186        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1187            masm.add(writable!(dst), dst, src, size)?;
1188            Ok(TypedReg::i64(dst))
1189        })
1190    }
1191
1192    fn visit_i32_sub(&mut self) -> Self::Output {
1193        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1194            masm.sub(writable!(dst), dst, src, size)?;
1195            Ok(TypedReg::i32(dst))
1196        })
1197    }
1198
1199    fn visit_i64_sub(&mut self) -> Self::Output {
1200        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1201            masm.sub(writable!(dst), dst, src, size)?;
1202            Ok(TypedReg::i64(dst))
1203        })
1204    }
1205
1206    fn visit_i32_mul(&mut self) -> Self::Output {
1207        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1208            masm.mul(writable!(dst), dst, src, size)?;
1209            Ok(TypedReg::i32(dst))
1210        })
1211    }
1212
1213    fn visit_i64_mul(&mut self) -> Self::Output {
1214        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1215            masm.mul(writable!(dst), dst, src, size)?;
1216            Ok(TypedReg::i64(dst))
1217        })
1218    }
1219
1220    fn visit_i32_div_s(&mut self) -> Self::Output {
1221        use DivKind::*;
1222        use OperandSize::*;
1223
1224        self.masm.div(&mut self.context, Signed, S32)
1225    }
1226
1227    fn visit_i32_div_u(&mut self) -> Self::Output {
1228        use DivKind::*;
1229        use OperandSize::*;
1230
1231        self.masm.div(&mut self.context, Unsigned, S32)
1232    }
1233
1234    fn visit_i64_div_s(&mut self) -> Self::Output {
1235        use DivKind::*;
1236        use OperandSize::*;
1237
1238        self.masm.div(&mut self.context, Signed, S64)
1239    }
1240
1241    fn visit_i64_div_u(&mut self) -> Self::Output {
1242        use DivKind::*;
1243        use OperandSize::*;
1244
1245        self.masm.div(&mut self.context, Unsigned, S64)
1246    }
1247
1248    fn visit_i32_rem_s(&mut self) -> Self::Output {
1249        use OperandSize::*;
1250        use RemKind::*;
1251
1252        self.masm.rem(&mut self.context, Signed, S32)
1253    }
1254
1255    fn visit_i32_rem_u(&mut self) -> Self::Output {
1256        use OperandSize::*;
1257        use RemKind::*;
1258
1259        self.masm.rem(&mut self.context, Unsigned, S32)
1260    }
1261
1262    fn visit_i64_rem_s(&mut self) -> Self::Output {
1263        use OperandSize::*;
1264        use RemKind::*;
1265
1266        self.masm.rem(&mut self.context, Signed, S64)
1267    }
1268
1269    fn visit_i64_rem_u(&mut self) -> Self::Output {
1270        use OperandSize::*;
1271        use RemKind::*;
1272
1273        self.masm.rem(&mut self.context, Unsigned, S64)
1274    }
1275
1276    fn visit_i32_eq(&mut self) -> Self::Output {
1277        self.cmp_i32s(IntCmpKind::Eq)
1278    }
1279
1280    fn visit_i64_eq(&mut self) -> Self::Output {
1281        self.cmp_i64s(IntCmpKind::Eq)
1282    }
1283
1284    fn visit_i32_ne(&mut self) -> Self::Output {
1285        self.cmp_i32s(IntCmpKind::Ne)
1286    }
1287
1288    fn visit_i64_ne(&mut self) -> Self::Output {
1289        self.cmp_i64s(IntCmpKind::Ne)
1290    }
1291
1292    fn visit_i32_lt_s(&mut self) -> Self::Output {
1293        self.cmp_i32s(IntCmpKind::LtS)
1294    }
1295
1296    fn visit_i64_lt_s(&mut self) -> Self::Output {
1297        self.cmp_i64s(IntCmpKind::LtS)
1298    }
1299
1300    fn visit_i32_lt_u(&mut self) -> Self::Output {
1301        self.cmp_i32s(IntCmpKind::LtU)
1302    }
1303
1304    fn visit_i64_lt_u(&mut self) -> Self::Output {
1305        self.cmp_i64s(IntCmpKind::LtU)
1306    }
1307
1308    fn visit_i32_le_s(&mut self) -> Self::Output {
1309        self.cmp_i32s(IntCmpKind::LeS)
1310    }
1311
1312    fn visit_i64_le_s(&mut self) -> Self::Output {
1313        self.cmp_i64s(IntCmpKind::LeS)
1314    }
1315
1316    fn visit_i32_le_u(&mut self) -> Self::Output {
1317        self.cmp_i32s(IntCmpKind::LeU)
1318    }
1319
1320    fn visit_i64_le_u(&mut self) -> Self::Output {
1321        self.cmp_i64s(IntCmpKind::LeU)
1322    }
1323
1324    fn visit_i32_gt_s(&mut self) -> Self::Output {
1325        self.cmp_i32s(IntCmpKind::GtS)
1326    }
1327
1328    fn visit_i64_gt_s(&mut self) -> Self::Output {
1329        self.cmp_i64s(IntCmpKind::GtS)
1330    }
1331
1332    fn visit_i32_gt_u(&mut self) -> Self::Output {
1333        self.cmp_i32s(IntCmpKind::GtU)
1334    }
1335
1336    fn visit_i64_gt_u(&mut self) -> Self::Output {
1337        self.cmp_i64s(IntCmpKind::GtU)
1338    }
1339
1340    fn visit_i32_ge_s(&mut self) -> Self::Output {
1341        self.cmp_i32s(IntCmpKind::GeS)
1342    }
1343
1344    fn visit_i64_ge_s(&mut self) -> Self::Output {
1345        self.cmp_i64s(IntCmpKind::GeS)
1346    }
1347
1348    fn visit_i32_ge_u(&mut self) -> Self::Output {
1349        self.cmp_i32s(IntCmpKind::GeU)
1350    }
1351
1352    fn visit_i64_ge_u(&mut self) -> Self::Output {
1353        self.cmp_i64s(IntCmpKind::GeU)
1354    }
1355
1356    fn visit_i32_eqz(&mut self) -> Self::Output {
1357        use OperandSize::*;
1358
1359        self.context.unop(self.masm, |masm, reg| {
1360            masm.cmp_with_set(writable!(reg), RegImm::i32(0), IntCmpKind::Eq, S32)?;
1361            Ok(TypedReg::i32(reg))
1362        })
1363    }
1364
1365    fn visit_i64_eqz(&mut self) -> Self::Output {
1366        use OperandSize::*;
1367
1368        self.context.unop(self.masm, |masm, reg| {
1369            masm.cmp_with_set(writable!(reg), RegImm::i64(0), IntCmpKind::Eq, S64)?;
1370            Ok(TypedReg::i32(reg)) // Return value for `i64.eqz` is an `i32`.
1371        })
1372    }
1373
1374    fn visit_i32_clz(&mut self) -> Self::Output {
1375        use OperandSize::*;
1376
1377        self.context.unop(self.masm, |masm, reg| {
1378            masm.clz(writable!(reg), reg, S32)?;
1379            Ok(TypedReg::i32(reg))
1380        })
1381    }
1382
1383    fn visit_i64_clz(&mut self) -> Self::Output {
1384        use OperandSize::*;
1385
1386        self.context.unop(self.masm, |masm, reg| {
1387            masm.clz(writable!(reg), reg, S64)?;
1388            Ok(TypedReg::i64(reg))
1389        })
1390    }
1391
1392    fn visit_i32_ctz(&mut self) -> Self::Output {
1393        use OperandSize::*;
1394
1395        self.context.unop(self.masm, |masm, reg| {
1396            masm.ctz(writable!(reg), reg, S32)?;
1397            Ok(TypedReg::i32(reg))
1398        })
1399    }
1400
1401    fn visit_i64_ctz(&mut self) -> Self::Output {
1402        use OperandSize::*;
1403
1404        self.context.unop(self.masm, |masm, reg| {
1405            masm.ctz(writable!(reg), reg, S64)?;
1406            Ok(TypedReg::i64(reg))
1407        })
1408    }
1409
1410    fn visit_i32_and(&mut self) -> Self::Output {
1411        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1412            masm.and(writable!(dst), dst, src, size)?;
1413            Ok(TypedReg::i32(dst))
1414        })
1415    }
1416
1417    fn visit_i64_and(&mut self) -> Self::Output {
1418        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1419            masm.and(writable!(dst), dst, src, size)?;
1420            Ok(TypedReg::i64(dst))
1421        })
1422    }
1423
1424    fn visit_i32_or(&mut self) -> Self::Output {
1425        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1426            masm.or(writable!(dst), dst, src, size)?;
1427            Ok(TypedReg::i32(dst))
1428        })
1429    }
1430
1431    fn visit_i64_or(&mut self) -> Self::Output {
1432        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1433            masm.or(writable!(dst), dst, src, size)?;
1434            Ok(TypedReg::i64(dst))
1435        })
1436    }
1437
1438    fn visit_i32_xor(&mut self) -> Self::Output {
1439        self.context.i32_binop(self.masm, |masm, dst, src, size| {
1440            masm.xor(writable!(dst), dst, src, size)?;
1441            Ok(TypedReg::i32(dst))
1442        })
1443    }
1444
1445    fn visit_i64_xor(&mut self) -> Self::Output {
1446        self.context.i64_binop(self.masm, |masm, dst, src, size| {
1447            masm.xor(writable!(dst), dst, src, size)?;
1448            Ok(TypedReg::i64(dst))
1449        })
1450    }
1451
1452    fn visit_i32_shl(&mut self) -> Self::Output {
1453        use ShiftKind::*;
1454
1455        self.context.i32_shift(self.masm, Shl)
1456    }
1457
1458    fn visit_i64_shl(&mut self) -> Self::Output {
1459        use ShiftKind::*;
1460
1461        self.context.i64_shift(self.masm, Shl)
1462    }
1463
1464    fn visit_i32_shr_s(&mut self) -> Self::Output {
1465        use ShiftKind::*;
1466
1467        self.context.i32_shift(self.masm, ShrS)
1468    }
1469
1470    fn visit_i64_shr_s(&mut self) -> Self::Output {
1471        use ShiftKind::*;
1472
1473        self.context.i64_shift(self.masm, ShrS)
1474    }
1475
1476    fn visit_i32_shr_u(&mut self) -> Self::Output {
1477        use ShiftKind::*;
1478
1479        self.context.i32_shift(self.masm, ShrU)
1480    }
1481
1482    fn visit_i64_shr_u(&mut self) -> Self::Output {
1483        use ShiftKind::*;
1484
1485        self.context.i64_shift(self.masm, ShrU)
1486    }
1487
1488    fn visit_i32_rotl(&mut self) -> Self::Output {
1489        use ShiftKind::*;
1490
1491        self.context.i32_shift(self.masm, Rotl)
1492    }
1493
1494    fn visit_i64_rotl(&mut self) -> Self::Output {
1495        use ShiftKind::*;
1496
1497        self.context.i64_shift(self.masm, Rotl)
1498    }
1499
1500    fn visit_i32_rotr(&mut self) -> Self::Output {
1501        use ShiftKind::*;
1502
1503        self.context.i32_shift(self.masm, Rotr)
1504    }
1505
1506    fn visit_i64_rotr(&mut self) -> Self::Output {
1507        use ShiftKind::*;
1508
1509        self.context.i64_shift(self.masm, Rotr)
1510    }
1511
1512    fn visit_end(&mut self) -> Self::Output {
1513        if !self.context.reachable {
1514            self.handle_unreachable_end()
1515        } else {
1516            let mut control = self.pop_control_frame()?;
1517            if let Some(info) = control.take_try_table_info() {
1518                self.emit_try_table_end(control, info)
1519            } else {
1520                control.emit_end(self.masm, &mut self.context)
1521            }
1522        }
1523    }
1524
1525    fn visit_i32_popcnt(&mut self) -> Self::Output {
1526        use OperandSize::*;
1527        self.masm.popcnt(&mut self.context, S32)
1528    }
1529
1530    fn visit_i64_popcnt(&mut self) -> Self::Output {
1531        use OperandSize::*;
1532
1533        self.masm.popcnt(&mut self.context, S64)
1534    }
1535
1536    fn visit_i32_wrap_i64(&mut self) -> Self::Output {
1537        self.context.unop(self.masm, |masm, reg| {
1538            masm.wrap(writable!(reg), reg)?;
1539            Ok(TypedReg::i32(reg))
1540        })
1541    }
1542
1543    fn visit_i64_extend_i32_s(&mut self) -> Self::Output {
1544        self.context.unop(self.masm, |masm, reg| {
1545            masm.extend(writable!(reg), reg, Extend::<Signed>::I64Extend32.into())?;
1546            Ok(TypedReg::i64(reg))
1547        })
1548    }
1549
1550    fn visit_i64_extend_i32_u(&mut self) -> Self::Output {
1551        self.context.unop(self.masm, |masm, reg| {
1552            masm.extend(writable!(reg), reg, Extend::<Zero>::I64Extend32.into())?;
1553            Ok(TypedReg::i64(reg))
1554        })
1555    }
1556
1557    fn visit_i32_extend8_s(&mut self) -> Self::Output {
1558        self.context.unop(self.masm, |masm, reg| {
1559            masm.extend(writable!(reg), reg, Extend::<Signed>::I32Extend8.into())?;
1560            Ok(TypedReg::i32(reg))
1561        })
1562    }
1563
1564    fn visit_i32_extend16_s(&mut self) -> Self::Output {
1565        self.context.unop(self.masm, |masm, reg| {
1566            masm.extend(writable!(reg), reg, Extend::<Signed>::I32Extend16.into())?;
1567            Ok(TypedReg::i32(reg))
1568        })
1569    }
1570
1571    fn visit_i64_extend8_s(&mut self) -> Self::Output {
1572        self.context.unop(self.masm, |masm, reg| {
1573            masm.extend(writable!(reg), reg, Extend::<Signed>::I64Extend8.into())?;
1574            Ok(TypedReg::i64(reg))
1575        })
1576    }
1577
1578    fn visit_i64_extend16_s(&mut self) -> Self::Output {
1579        self.context.unop(self.masm, |masm, reg| {
1580            masm.extend(writable!(reg), reg, Extend::<Signed>::I64Extend16.into())?;
1581            Ok(TypedReg::i64(reg))
1582        })
1583    }
1584
1585    fn visit_i64_extend32_s(&mut self) -> Self::Output {
1586        self.context.unop(self.masm, |masm, reg| {
1587            masm.extend(writable!(reg), reg, Extend::<Signed>::I64Extend32.into())?;
1588            Ok(TypedReg::i64(reg))
1589        })
1590    }
1591
1592    fn visit_i32_trunc_f32_s(&mut self) -> Self::Output {
1593        use OperandSize::*;
1594
1595        self.context
1596            .convert_op(self.masm, WasmValType::I32, |masm, dst, src, dst_size| {
1597                masm.signed_truncate(writable!(dst), src, S32, dst_size, TruncKind::Unchecked)
1598            })
1599    }
1600
1601    fn visit_i32_trunc_f32_u(&mut self) -> Self::Output {
1602        use OperandSize::*;
1603
1604        self.masm
1605            .unsigned_truncate(&mut self.context, S32, S32, TruncKind::Unchecked)
1606    }
1607
1608    fn visit_i32_trunc_f64_s(&mut self) -> Self::Output {
1609        use OperandSize::*;
1610
1611        self.context
1612            .convert_op(self.masm, WasmValType::I32, |masm, dst, src, dst_size| {
1613                masm.signed_truncate(writable!(dst), src, S64, dst_size, TruncKind::Unchecked)
1614            })
1615    }
1616
1617    fn visit_i32_trunc_f64_u(&mut self) -> Self::Output {
1618        use OperandSize::*;
1619        self.masm
1620            .unsigned_truncate(&mut self.context, S64, S32, TruncKind::Unchecked)
1621    }
1622
1623    fn visit_i64_trunc_f32_s(&mut self) -> Self::Output {
1624        use OperandSize::*;
1625
1626        self.context
1627            .convert_op(self.masm, WasmValType::I64, |masm, dst, src, dst_size| {
1628                masm.signed_truncate(writable!(dst), src, S32, dst_size, TruncKind::Unchecked)
1629            })
1630    }
1631
1632    fn visit_i64_trunc_f32_u(&mut self) -> Self::Output {
1633        use OperandSize::*;
1634
1635        self.masm
1636            .unsigned_truncate(&mut self.context, S32, S64, TruncKind::Unchecked)
1637    }
1638
1639    fn visit_i64_trunc_f64_s(&mut self) -> Self::Output {
1640        use OperandSize::*;
1641
1642        self.context
1643            .convert_op(self.masm, WasmValType::I64, |masm, dst, src, dst_size| {
1644                masm.signed_truncate(writable!(dst), src, S64, dst_size, TruncKind::Unchecked)
1645            })
1646    }
1647
1648    fn visit_i64_trunc_f64_u(&mut self) -> Self::Output {
1649        use OperandSize::*;
1650
1651        self.masm
1652            .unsigned_truncate(&mut self.context, S64, S64, TruncKind::Unchecked)
1653    }
1654
1655    fn visit_i32_reinterpret_f32(&mut self) -> Self::Output {
1656        self.context
1657            .convert_op(self.masm, WasmValType::I32, |masm, dst, src, size| {
1658                masm.reinterpret_float_as_int(writable!(dst), src, size)
1659            })
1660    }
1661
1662    fn visit_i64_reinterpret_f64(&mut self) -> Self::Output {
1663        self.context
1664            .convert_op(self.masm, WasmValType::I64, |masm, dst, src, size| {
1665                masm.reinterpret_float_as_int(writable!(dst), src, size)
1666            })
1667    }
1668
1669    fn visit_local_get(&mut self, index: u32) -> Self::Output {
1670        use WasmValType::*;
1671        let context = &mut self.context;
1672        let slot = context.frame.get_wasm_local(index);
1673        match slot.ty {
1674            I32 | I64 | F32 | F64 | V128 => context.stack.push(Val::local(index, slot.ty)),
1675            Ref(rt) => match rt.heap_type {
1676                WasmHeapType::Func
1677                | WasmHeapType::Extern
1678                | WasmHeapType::Exn
1679                | WasmHeapType::ConcreteExn(_)
1680                | WasmHeapType::NoExn => context.stack.push(Val::local(index, slot.ty)),
1681                _ => bail!(CodeGenError::unsupported_wasm_type()),
1682            },
1683        }
1684
1685        Ok(())
1686    }
1687
1688    fn visit_local_set(&mut self, index: u32) -> Self::Output {
1689        let src = self.emit_set_local(index)?;
1690        self.context.free_reg(src);
1691        Ok(())
1692    }
1693
1694    fn visit_call(&mut self, index: u32) -> Self::Output {
1695        let callee = self.env.callee_from_index(FuncIndex::from_u32(index));
1696        FnCall::emit::<M>(&mut self.env, self.masm, &mut self.context, callee)?;
1697        Ok(())
1698    }
1699
1700    fn visit_return_call(&mut self, index: u32) -> Self::Output {
1701        let callee = self.env.callee_from_index(FuncIndex::from_u32(index));
1702        FnCall::emit_return::<M>(
1703            &mut self.env,
1704            self.masm,
1705            &mut self.context,
1706            self.sig.params_stack_size(),
1707            callee,
1708        )?;
1709        self.context.reachable = false;
1710        Ok(())
1711    }
1712
1713    fn visit_call_indirect(&mut self, type_index: u32, table_index: u32) -> Self::Output {
1714        // Spill now because `emit_table_get` and the `FnCall::emit`
1715        // invocations will both trigger spills since they both call functions.
1716        // However, the machine instructions for the spill emitted by
1717        // `emit_lazy_funcref` will be jumped over if the funcref was previously
1718        // initialized which may result in the machine stack becoming
1719        // unbalanced.
1720        self.context.spill(self.masm)?;
1721
1722        let type_index = TypeIndex::from_u32(type_index);
1723        let table_index = TableIndex::from_u32(table_index);
1724
1725        self.emit_table_get(table_index)?;
1726
1727        // Perform the indirect call.
1728        // This code assumes that [`Self::emit_table_get`] will
1729        // push the funcref to the value stack.
1730        let funcref_ptr = self
1731            .context
1732            .stack
1733            .peek()
1734            .map(|v| v.unwrap_reg())
1735            .ok_or_else(|| CodeGenError::missing_values_in_stack())?;
1736        self.masm
1737            .trapz(funcref_ptr.into(), TRAP_INDIRECT_CALL_TO_NULL)?;
1738        self.emit_typecheck_funcref(funcref_ptr.into(), type_index)?;
1739
1740        let callee = self.env.funcref(type_index);
1741        FnCall::emit::<M>(&mut self.env, self.masm, &mut self.context, callee)?;
1742        Ok(())
1743    }
1744
1745    fn visit_return_call_indirect(&mut self, type_index: u32, table_index: u32) -> Self::Output {
1746        // As with a normal indirect call, spill before table lookup because
1747        // lazy funcref initialization can itself make a call.
1748        self.context.spill(self.masm)?;
1749
1750        let type_index = TypeIndex::from_u32(type_index);
1751        let table_index = TableIndex::from_u32(table_index);
1752
1753        self.emit_table_get(table_index)?;
1754
1755        let funcref_ptr = self
1756            .context
1757            .stack
1758            .peek()
1759            .map(|v| v.unwrap_reg())
1760            .ok_or_else(|| CodeGenError::missing_values_in_stack())?;
1761        self.masm
1762            .trapz(funcref_ptr.into(), TRAP_INDIRECT_CALL_TO_NULL)?;
1763        self.emit_typecheck_funcref(funcref_ptr.into(), type_index)?;
1764
1765        let callee = self.env.funcref(type_index);
1766        FnCall::emit_return::<M>(
1767            &mut self.env,
1768            self.masm,
1769            &mut self.context,
1770            self.sig.params_stack_size(),
1771            callee,
1772        )?;
1773        self.context.reachable = false;
1774        Ok(())
1775    }
1776
1777    fn visit_table_init(&mut self, elem: u32, table: u32) -> Self::Output {
1778        self.emit_table_init(ElemIndex::from_u32(elem), TableIndex::from_u32(table))
1779    }
1780
1781    fn visit_table_copy(&mut self, dst: u32, src: u32) -> Self::Output {
1782        self.emit_table_copy(TableIndex::from_u32(dst), TableIndex::from_u32(src))
1783    }
1784
1785    fn visit_table_get(&mut self, table: u32) -> Self::Output {
1786        let table_index = TableIndex::from_u32(table);
1787        self.emit_table_get(table_index)
1788    }
1789
1790    fn visit_table_grow(&mut self, table: u32) -> Self::Output {
1791        let table_index = TableIndex::from_u32(table);
1792        self.emit_table_grow(table_index)
1793    }
1794
1795    fn visit_table_size(&mut self, table: u32) -> Self::Output {
1796        let table_index = TableIndex::from_u32(table);
1797        let table_data = self.env.resolve_table_data(table_index);
1798        self.emit_compute_table_size(&table_data)
1799    }
1800
1801    fn visit_table_fill(&mut self, table: u32) -> Self::Output {
1802        let table_index = TableIndex::from_u32(table);
1803        self.emit_table_fill(table_index)
1804    }
1805
1806    fn visit_table_set(&mut self, table: u32) -> Self::Output {
1807        let table_index = TableIndex::from_u32(table);
1808        self.emit_table_set(table_index)
1809    }
1810
1811    fn visit_elem_drop(&mut self, index: u32) -> Self::Output {
1812        let index = ElemIndex::from_u32(index);
1813        self.emit_elem_drop(index)
1814    }
1815
1816    fn visit_memory_init(&mut self, data_index: u32, mem: u32) -> Self::Output {
1817        self.emit_memory_init(DataIndex::from_u32(data_index), MemoryIndex::from_u32(mem))
1818    }
1819
1820    fn visit_memory_copy(&mut self, dst_mem: u32, src_mem: u32) -> Self::Output {
1821        self.emit_memory_copy(
1822            MemoryIndex::from_u32(dst_mem),
1823            MemoryIndex::from_u32(src_mem),
1824        )
1825    }
1826
1827    fn visit_memory_fill(&mut self, mem: u32) -> Self::Output {
1828        self.emit_memory_fill(MemoryIndex::from_u32(mem))
1829    }
1830
1831    fn visit_memory_size(&mut self, mem: u32) -> Self::Output {
1832        let heap = self.env.resolve_heap(MemoryIndex::from_u32(mem));
1833        self.emit_compute_memory_size(&heap)
1834    }
1835
1836    fn visit_memory_grow(&mut self, mem: u32) -> Self::Output {
1837        let at = self.context.stack.ensure_index_at(1)?;
1838        let mem = MemoryIndex::from_u32(mem);
1839        // The stack at this point contains: [ delta ]
1840        // The desired state is
1841        //   [ vmctx, delta, index ]
1842        let builtin = self.env.builtins.memory_grow::<M::ABI>()?;
1843        let builtin = self.prepare_builtin_defined_memory_arg(mem, at + 1, builtin)?;
1844
1845        let heap = self.env.resolve_heap(mem);
1846        FnCall::emit::<M>(&mut self.env, self.masm, &mut self.context, builtin)?;
1847
1848        // The memory32_grow builtin returns a pointer type, therefore we must
1849        // ensure that the return type is representative of the address space of
1850        // the heap type.
1851        match (self.env.ptr_type(), heap.index_type()) {
1852            (WasmValType::I64, WasmValType::I64) => Ok(()),
1853            // When the heap type is smaller than the pointer type, we adjust
1854            // the result of the memory32_grow builtin.
1855            (WasmValType::I64, WasmValType::I32) => {
1856                let top: Reg = self.context.pop_to_reg(self.masm, None)?.into();
1857                self.masm.wrap(writable!(top), top)?;
1858                self.context.stack.push(TypedReg::i32(top).into());
1859                Ok(())
1860            }
1861            _ => Err(format_err!(CodeGenError::unsupported_32_bit_platform())),
1862        }
1863    }
1864
1865    fn visit_data_drop(&mut self, data_index: u32) -> Self::Output {
1866        self.emit_data_drop(DataIndex::from_u32(data_index))
1867    }
1868
1869    fn visit_nop(&mut self) -> Self::Output {
1870        Ok(())
1871    }
1872
1873    fn visit_if(&mut self, blockty: BlockType) -> Self::Output {
1874        self.control_frames.push(ControlStackFrame::r#if(
1875            self.env.resolve_block_sig(blockty)?,
1876            self.masm,
1877            &mut self.context,
1878        )?);
1879
1880        Ok(())
1881    }
1882
1883    fn visit_else(&mut self) -> Self::Output {
1884        if !self.context.reachable {
1885            self.handle_unreachable_else()
1886        } else {
1887            let control = self
1888                .control_frames
1889                .last_mut()
1890                .ok_or_else(|| CodeGenError::control_frame_expected())?;
1891            control.emit_else(self.masm, &mut self.context)
1892        }
1893    }
1894
1895    fn visit_block(&mut self, blockty: BlockType) -> Self::Output {
1896        self.control_frames.push(ControlStackFrame::block(
1897            self.env.resolve_block_sig(blockty)?,
1898            self.masm,
1899            &mut self.context,
1900        )?);
1901
1902        Ok(())
1903    }
1904
1905    // Record the handlers that apply to calls within this `try_table`. Their
1906    // landing pads are emitted when the control frame ends.
1907    fn visit_try_table(&mut self, try_table: TryTable) -> Self::Output {
1908        // When this `try_table` is unreachable at entry, its handlers cannot run.
1909        // Use a plain block to match its `end`, without registering catches.
1910        // Unreachable blocks do not record the machine-stack state needed to
1911        // emit landing pads.
1912        if !self.context.reachable {
1913            return self.visit_block(try_table.ty);
1914        }
1915
1916        let checkpoint = self.context.exception_handlers.take_checkpoint();
1917        let mut catches = Vec::with_capacity(try_table.catches.len());
1918
1919        for catch in try_table.catches.iter().rev() {
1920            let (is_ref, tag, target_depth) = match catch {
1921                wasmparser::Catch::One { tag, label } => {
1922                    (false, Some(TagIndex::from_u32(*tag)), *label)
1923                }
1924                wasmparser::Catch::OneRef { tag, label } => {
1925                    (true, Some(TagIndex::from_u32(*tag)), *label)
1926                }
1927                wasmparser::Catch::All { label } => (false, None, *label),
1928                wasmparser::Catch::AllRef { label } => (true, None, *label),
1929            };
1930
1931            let landing_pad = self.masm.get_label()?;
1932
1933            let target = control_index(target_depth, self.control_frames.len())?;
1934            self.control_frames[target].set_as_target();
1935
1936            let exception_tag = tag.map(|tag| ExceptionTag::from_u32(tag.as_u32()));
1937            self.context
1938                .exception_handlers
1939                .add_handler(exception_tag, landing_pad);
1940
1941            catches.push(CatchInfo {
1942                is_ref,
1943                tag,
1944                target_depth,
1945                landing_pad,
1946            });
1947        }
1948        let info = TryTableInfo {
1949            checkpoint,
1950            catches,
1951        };
1952        self.control_frames.push(ControlStackFrame::try_table(
1953            self.env.resolve_block_sig(try_table.ty)?,
1954            info,
1955            self.masm,
1956            &mut self.context,
1957        )?);
1958
1959        Ok(())
1960    }
1961
1962    fn visit_throw(&mut self, tag_index: u32) -> Self::Output {
1963        let tag_index = TagIndex::from_u32(tag_index);
1964        let interned = self.env.translation.module.tags[tag_index]
1965            .exception
1966            .unwrap_module_type_index();
1967        let types = self.env.types;
1968        let exn_ty = match &types[interned].composite_type.inner {
1969            WasmCompositeInnerType::Exn(exn_ty) => exn_ty,
1970            _ => return Err(format_err!(CodeGenError::unsupported_wasm_type())),
1971        };
1972        let layouts = self.require_gc_codegen_config().layouts();
1973
1974        let layout = layouts
1975            .exn_layout(exn_ty)
1976            .map_err(|_| format_err!(CodeGenError::unsupported_wasm_type()))?;
1977
1978        let (gc_ref, object_addr) =
1979            self.emit_exception_alloc(tag_index, interned, &layout, layouts)?;
1980        let gc_ref =
1981            self.emit_store_exception_payload_fields(exn_ty, &layout, gc_ref, object_addr)?;
1982        self.context.stack.push(gc_ref.into());
1983        self.visit_throw_ref()
1984    }
1985
1986    // The exception reference is on top of the value stack. Forward it to the
1987    // runtime, then mark the remaining Wasm code unreachable because throwing
1988    // does not return to this function.
1989    fn visit_throw_ref(&mut self) -> Self::Output {
1990        let throw_ref = self.env.builtins.throw_ref::<M::ABI>()?;
1991        FnCall::emit::<M>(
1992            &mut self.env,
1993            self.masm,
1994            &mut self.context,
1995            Callee::Builtin(throw_ref),
1996        )?;
1997        self.context.reachable = false;
1998        let outermost = &mut self.control_frames[0];
1999        outermost.set_as_target();
2000        Ok(())
2001    }
2002
2003    fn visit_loop(&mut self, blockty: BlockType) -> Self::Output {
2004        self.control_frames.push(ControlStackFrame::r#loop(
2005            self.env.resolve_block_sig(blockty)?,
2006            self.masm,
2007            &mut self.context,
2008        )?);
2009
2010        // Unreachable loops still need a control frame, but must not emit
2011        // runtime-limit checks against a potentially unwound stack frame.
2012        if !self.context.reachable {
2013            return Ok(());
2014        }
2015
2016        self.maybe_emit_epoch_check()?;
2017        self.maybe_emit_fuel_check()
2018    }
2019
2020    fn visit_br(&mut self, depth: u32) -> Self::Output {
2021        let index = control_index(depth, self.control_frames.len())?;
2022        let frame = &mut self.control_frames[index];
2023        self.context
2024            .br::<_, _, UnconditionalBranch>(frame, self.masm, |masm, cx, frame| {
2025                frame.pop_abi_results::<M, _>(cx, masm, |results, _, _| {
2026                    Ok(results.ret_area().copied())
2027                })
2028            })
2029    }
2030
2031    fn visit_br_if(&mut self, depth: u32) -> Self::Output {
2032        let index = control_index(depth, self.control_frames.len())?;
2033        let frame = &mut self.control_frames[index];
2034        frame.set_as_target();
2035
2036        let top = {
2037            let top = self.context.without::<Result<TypedReg>, M, _>(
2038                frame.results::<M>()?.regs(),
2039                self.masm,
2040                |ctx, masm| ctx.pop_to_reg(masm, None),
2041            )??;
2042            // Explicitly save any live registers and locals before setting up
2043            // the branch state.
2044            // In some cases, calculating the `top` value above, will result in
2045            // a spill, thus the following one will result in a no-op.
2046            self.context.spill(self.masm)?;
2047            frame.top_abi_results::<M, _>(
2048                &mut self.context,
2049                self.masm,
2050                |results, context, masm| {
2051                    // In the case of `br_if` there's a possibility that we'll
2052                    // exit early from the block or fallthrough, for
2053                    // a fallthrough, we cannot rely on the pre-computed return area;
2054                    // it must be recalculated so that any values that are
2055                    // generated are correctly placed near the current stack
2056                    // pointer.
2057                    if results.on_stack() {
2058                        let stack_consumed = context.stack.sizeof(results.stack_operands_len());
2059                        let base = masm.sp_offset()?.as_u32() - stack_consumed;
2060                        let offs = base + results.size();
2061                        Ok(Some(RetArea::sp(SPOffset::from_u32(offs))))
2062                    } else {
2063                        Ok(None)
2064                    }
2065                },
2066            )?;
2067            top
2068        };
2069
2070        // Emit instructions to balance the machine stack.
2071        let current_sp_offset = self.masm.sp_offset()?;
2072        let unbalanced = frame.unbalanced(self.masm)?;
2073        let (label, cmp) = if unbalanced {
2074            (self.masm.get_label()?, IntCmpKind::Eq)
2075        } else {
2076            (*frame.label(), IntCmpKind::Ne)
2077        };
2078
2079        self.masm
2080            .branch(cmp, top.reg, top.reg.into(), label, OperandSize::S32)?;
2081        self.context.free_reg(top);
2082
2083        if unbalanced {
2084            self.context
2085                .br::<_, _, ConditionalBranch>(frame, self.masm, |_, _, _| Ok(()))?;
2086
2087            // Restore sp_offset to what it was for falling through and emit
2088            // fallthrough label.
2089            self.masm.reset_stack_pointer(current_sp_offset)?;
2090            self.masm.bind(label)?;
2091        }
2092
2093        Ok(())
2094    }
2095
2096    fn visit_br_table(&mut self, targets: BrTable<'a>) -> Self::Output {
2097        // +1 to account for the default target.
2098        let len = targets.len() + 1;
2099        // SmallVec<[_; 5]> to match the binary emission layer (e.g
2100        // see `JmpTableSeq'), but here we use 5 instead since we
2101        // bundle the default target as the last element in the array.
2102        let mut labels: SmallVec<[_; 5]> = smallvec![];
2103        for _ in 0..len {
2104            labels.push(self.masm.get_label()?);
2105        }
2106
2107        // Find the innermost target and use it as the relative frame
2108        // for result handling below.
2109        //
2110        // This approach ensures that
2111        // 1. The stack pointer offset is correctly positioned
2112        //    according to the expectations of the innermost block end
2113        //    sequence.
2114        // 2. We meet the jump site invariants introduced by
2115        //    `CodegenContext::br`, which take advantage of Wasm
2116        //    semantics given that all jumps are "outward".
2117        let mut innermost = targets.default();
2118        for target in targets.targets() {
2119            let target = target?;
2120            if target < innermost {
2121                innermost = target;
2122            }
2123        }
2124
2125        let innermost_index = control_index(innermost, self.control_frames.len())?;
2126        let innermost_frame = &mut self.control_frames[innermost_index];
2127        let innermost_result = innermost_frame.results::<M>()?;
2128
2129        let (index, tmp) = {
2130            let index_and_tmp = self.context.without::<Result<(TypedReg, _)>, M, _>(
2131                innermost_result.regs(),
2132                self.masm,
2133                |cx, masm| Ok((cx.pop_to_reg(masm, None)?, cx.any_gpr(masm)?)),
2134            )??;
2135
2136            // Materialize any constants or locals into their result
2137            // representation, so that when reachability is restored,
2138            // they are correctly located.  NB: the results are popped
2139            // in function of the innermost branch specified for
2140            // `br_table`, which implies that the machine stack will
2141            // be correctly balanced, by virtue of calling
2142            // `pop_abi_results`.
2143
2144            // It's possible that we need to balance the stack for the
2145            // rest of the targets, which will be done before emitting
2146            // the unconditional jump below.
2147            innermost_frame.pop_abi_results::<M, _>(
2148                &mut self.context,
2149                self.masm,
2150                |results, _, _| Ok(results.ret_area().copied()),
2151            )?;
2152            index_and_tmp
2153        };
2154
2155        self.masm.jmp_table(&labels, index.into(), tmp)?;
2156        // Save the original stack pointer offset; we will reset the stack
2157        // pointer to this offset after jumping to each of the targets. Each
2158        // jump might adjust the stack according to the base offset of the
2159        // target.
2160        let current_sp = self.masm.sp_offset()?;
2161
2162        for (t, l) in targets
2163            .targets()
2164            .chain(std::iter::once(Ok(targets.default())))
2165            .zip(labels.iter())
2166        {
2167            let control_index = control_index(t?, self.control_frames.len())?;
2168            let frame = &mut self.control_frames[control_index];
2169            // Reset the stack pointer to its original offset. This is needed
2170            // because each jump will potentially adjust the stack pointer
2171            // according to the base offset of the target.
2172            self.masm.reset_stack_pointer(current_sp)?;
2173
2174            // NB: We don't perform any result handling as it was
2175            // already taken care of above before jumping to the
2176            // jump table.
2177            self.masm.bind(*l)?;
2178            // Ensure that the stack pointer is correctly positioned before
2179            // jumping to the jump table code.
2180            self.context
2181                .br::<_, _, UnconditionalBranch>(frame, self.masm, |_, _, _| Ok(()))?;
2182        }
2183        // Finally reset the stack pointer to the original location.
2184        // The reachability analysis, will ensure it's correctly located
2185        // once reachability is restored.
2186        self.masm.reset_stack_pointer(current_sp)?;
2187        self.context.reachable = false;
2188        self.context.free_reg(index.reg);
2189        self.context.free_reg(tmp);
2190
2191        Ok(())
2192    }
2193
2194    fn visit_return(&mut self) -> Self::Output {
2195        // Grab the outermost frame, which is the function's body
2196        // frame. We don't rely on [`codegen::control_index`] since
2197        // this frame is implicit and we know that it should exist at
2198        // index 0.
2199        let outermost = &mut self.control_frames[0];
2200        self.context
2201            .br::<_, _, UnconditionalBranch>(outermost, self.masm, |masm, cx, frame| {
2202                frame.pop_abi_results::<M, _>(cx, masm, |results, _, _| {
2203                    Ok(results.ret_area().copied())
2204                })
2205            })
2206    }
2207
2208    fn visit_unreachable(&mut self) -> Self::Output {
2209        self.masm.unreachable()?;
2210        self.context.reachable = false;
2211        // Set the implicit outermost frame as target to perform the necessary
2212        // stack clean up.
2213        let outermost = &mut self.control_frames[0];
2214        outermost.set_as_target();
2215
2216        Ok(())
2217    }
2218
2219    fn visit_local_tee(&mut self, index: u32) -> Self::Output {
2220        let typed_reg = self.emit_set_local(index)?;
2221        self.context.stack.push(typed_reg.into());
2222
2223        Ok(())
2224    }
2225
2226    fn visit_global_get(&mut self, global_index: u32) -> Self::Output {
2227        let index = GlobalIndex::from_u32(global_index);
2228        let (ty, base, offset) = self.emit_get_global_addr(index)?;
2229        let addr = self.masm.address_at_reg(base, offset)?;
2230        let gc_ref = self.context.reg_for_type(ty, self.masm)?;
2231        self.masm.load(addr, writable!(gc_ref), ty.try_into()?)?;
2232        self.context.free_reg(base);
2233
2234        if self.gc_barrier_needed(&ty) {
2235            self.emit_drc_read_barrier(ty, gc_ref)?;
2236        } else {
2237            self.context.stack.push(Val::reg(gc_ref, ty));
2238        }
2239
2240        Ok(())
2241    }
2242
2243    fn visit_global_set(&mut self, global_index: u32) -> Self::Output {
2244        let index = GlobalIndex::from_u32(global_index);
2245        let (ty, base, offset) = self.emit_get_global_addr(index)?;
2246        let addr = self.masm.address_at_reg(base, offset)?;
2247
2248        if self.gc_barrier_needed(&ty) {
2249            self.emit_drc_write_barrier(ty, base, addr)?;
2250        } else {
2251            let typed_reg = self.context.pop_to_reg(self.masm, None)?;
2252            self.masm
2253                .store(typed_reg.reg.into(), addr, ty.try_into()?)?;
2254            self.context.free_reg(typed_reg.reg);
2255            self.context.free_reg(base);
2256        }
2257
2258        Ok(())
2259    }
2260
2261    fn visit_drop(&mut self) -> Self::Output {
2262        self.context.drop_last(1, |regalloc, val| match val {
2263            Val::Reg(tr) => Ok(regalloc.free(tr.reg)),
2264            Val::Memory(m) => self.masm.free_stack(m.slot.size),
2265            _ => Ok(()),
2266        })
2267    }
2268
2269    fn visit_select(&mut self) -> Self::Output {
2270        self.emit_select(None)
2271    }
2272
2273    fn visit_typed_select(&mut self, ty: ValType) -> Self::Output {
2274        let ty = TypeConverter::new(self.env.translation, self.env.types).convert_valtype(ty)?;
2275        self.emit_select(Some(ty))
2276    }
2277
2278    fn visit_ref_null(&mut self, hty: HeapType) -> Self::Output {
2279        match self.env.convert_heap_type(hty)? {
2280            WasmHeapType::Func => {
2281                let ptr_type = self.env.ptr_type();
2282                match ptr_type {
2283                    WasmValType::I64 => self.context.stack.push(Val::i64(0)),
2284                    WasmValType::I32 => self.context.stack.push(Val::i32(0)),
2285                    _ => bail!(CodeGenError::unsupported_wasm_type()),
2286                }
2287                Ok(())
2288            }
2289            WasmHeapType::Extern
2290            | WasmHeapType::Exn
2291            | WasmHeapType::ConcreteExn(_)
2292            | WasmHeapType::NoExn => {
2293                self.context.stack.push(Val::i32(0));
2294                Ok(())
2295            }
2296            _ => Err(format_err!(CodeGenError::unsupported_wasm_type())),
2297        }
2298    }
2299
2300    fn visit_ref_is_null(&mut self) -> Self::Output {
2301        let (zero, size) = match self.env.ptr_type() {
2302            WasmValType::I64 => (RegImm::i64(0), OperandSize::S64),
2303            WasmValType::I32 => (RegImm::i32(0), OperandSize::S32),
2304            _ => bail!(CodeGenError::unsupported_wasm_type()),
2305        };
2306        self.context.unop(self.masm, |masm, reg| {
2307            masm.cmp_with_set(writable!(reg), zero, IntCmpKind::Eq, size)?;
2308            Ok(TypedReg::i32(reg))
2309        })
2310    }
2311
2312    fn visit_ref_func(&mut self, function_index: u32) -> Self::Output {
2313        self.emit_ref_func(FuncIndex::from_u32(function_index))
2314    }
2315
2316    fn visit_i32_load(&mut self, memarg: MemArg) -> Self::Output {
2317        self.emit_wasm_load(
2318            &memarg,
2319            WasmValType::I32,
2320            LoadKind::Operand(OperandSize::S32),
2321        )
2322    }
2323
2324    fn visit_i32_load8_s(&mut self, memarg: MemArg) -> Self::Output {
2325        self.emit_wasm_load(
2326            &memarg,
2327            WasmValType::I32,
2328            LoadKind::ScalarExtend(Extend::<Signed>::I32Extend8.into()),
2329        )
2330    }
2331
2332    fn visit_i32_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2333        self.emit_wasm_load(
2334            &memarg,
2335            WasmValType::I32,
2336            LoadKind::ScalarExtend(Extend::<Zero>::I32Extend8.into()),
2337        )
2338    }
2339
2340    fn visit_i32_load16_s(&mut self, memarg: MemArg) -> Self::Output {
2341        self.emit_wasm_load(
2342            &memarg,
2343            WasmValType::I32,
2344            LoadKind::ScalarExtend(Extend::<Signed>::I32Extend16.into()),
2345        )
2346    }
2347
2348    fn visit_i32_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2349        self.emit_wasm_load(
2350            &memarg,
2351            WasmValType::I32,
2352            LoadKind::ScalarExtend(Extend::<Zero>::I32Extend16.into()),
2353        )
2354    }
2355
2356    fn visit_i32_store(&mut self, memarg: MemArg) -> Self::Output {
2357        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S32))
2358    }
2359
2360    fn visit_i32_store8(&mut self, memarg: MemArg) -> Self::Output {
2361        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S8))
2362    }
2363
2364    fn visit_i32_store16(&mut self, memarg: MemArg) -> Self::Output {
2365        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S16))
2366    }
2367
2368    fn visit_i64_load8_s(&mut self, memarg: MemArg) -> Self::Output {
2369        self.emit_wasm_load(
2370            &memarg,
2371            WasmValType::I64,
2372            LoadKind::ScalarExtend(Extend::<Signed>::I64Extend8.into()),
2373        )
2374    }
2375
2376    fn visit_i64_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2377        self.emit_wasm_load(
2378            &memarg,
2379            WasmValType::I64,
2380            LoadKind::ScalarExtend(Extend::<Zero>::I64Extend8.into()),
2381        )
2382    }
2383
2384    fn visit_i64_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2385        self.emit_wasm_load(
2386            &memarg,
2387            WasmValType::I64,
2388            LoadKind::ScalarExtend(Extend::<Zero>::I64Extend16.into()),
2389        )
2390    }
2391
2392    fn visit_i64_load16_s(&mut self, memarg: MemArg) -> Self::Output {
2393        self.emit_wasm_load(
2394            &memarg,
2395            WasmValType::I64,
2396            LoadKind::ScalarExtend(Extend::<Signed>::I64Extend16.into()),
2397        )
2398    }
2399
2400    fn visit_i64_load32_u(&mut self, memarg: MemArg) -> Self::Output {
2401        self.emit_wasm_load(
2402            &memarg,
2403            WasmValType::I64,
2404            LoadKind::ScalarExtend(Extend::<Zero>::I64Extend32.into()),
2405        )
2406    }
2407
2408    fn visit_i64_load32_s(&mut self, memarg: MemArg) -> Self::Output {
2409        self.emit_wasm_load(
2410            &memarg,
2411            WasmValType::I64,
2412            LoadKind::ScalarExtend(Extend::<Signed>::I64Extend32.into()),
2413        )
2414    }
2415
2416    fn visit_i64_load(&mut self, memarg: MemArg) -> Self::Output {
2417        self.emit_wasm_load(
2418            &memarg,
2419            WasmValType::I64,
2420            LoadKind::Operand(OperandSize::S64),
2421        )
2422    }
2423
2424    fn visit_i64_store(&mut self, memarg: MemArg) -> Self::Output {
2425        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S64))
2426    }
2427
2428    fn visit_i64_store8(&mut self, memarg: MemArg) -> Self::Output {
2429        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S8))
2430    }
2431
2432    fn visit_i64_store16(&mut self, memarg: MemArg) -> Self::Output {
2433        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S16))
2434    }
2435
2436    fn visit_i64_store32(&mut self, memarg: MemArg) -> Self::Output {
2437        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S32))
2438    }
2439
2440    fn visit_f32_load(&mut self, memarg: MemArg) -> Self::Output {
2441        self.emit_wasm_load(
2442            &memarg,
2443            WasmValType::F32,
2444            LoadKind::Operand(OperandSize::S32),
2445        )
2446    }
2447
2448    fn visit_f32_store(&mut self, memarg: MemArg) -> Self::Output {
2449        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S32))
2450    }
2451
2452    fn visit_f64_load(&mut self, memarg: MemArg) -> Self::Output {
2453        self.emit_wasm_load(
2454            &memarg,
2455            WasmValType::F64,
2456            LoadKind::Operand(OperandSize::S64),
2457        )
2458    }
2459
2460    fn visit_f64_store(&mut self, memarg: MemArg) -> Self::Output {
2461        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S64))
2462    }
2463
2464    fn visit_i32_trunc_sat_f32_s(&mut self) -> Self::Output {
2465        use OperandSize::*;
2466
2467        self.context
2468            .convert_op(self.masm, WasmValType::I32, |masm, dst, src, dst_size| {
2469                masm.signed_truncate(writable!(dst), src, S32, dst_size, TruncKind::Checked)
2470            })
2471    }
2472
2473    fn visit_i32_trunc_sat_f32_u(&mut self) -> Self::Output {
2474        use OperandSize::*;
2475
2476        self.masm
2477            .unsigned_truncate(&mut self.context, S32, S32, TruncKind::Checked)
2478    }
2479
2480    fn visit_i32_trunc_sat_f64_s(&mut self) -> Self::Output {
2481        use OperandSize::*;
2482
2483        self.context
2484            .convert_op(self.masm, WasmValType::I32, |masm, dst, src, dst_size| {
2485                masm.signed_truncate(writable!(dst), src, S64, dst_size, TruncKind::Checked)
2486            })
2487    }
2488
2489    fn visit_i32_trunc_sat_f64_u(&mut self) -> Self::Output {
2490        use OperandSize::*;
2491
2492        self.masm
2493            .unsigned_truncate(&mut self.context, S64, S32, TruncKind::Checked)
2494    }
2495
2496    fn visit_i64_trunc_sat_f32_s(&mut self) -> Self::Output {
2497        use OperandSize::*;
2498
2499        self.context
2500            .convert_op(self.masm, WasmValType::I64, |masm, dst, src, dst_size| {
2501                masm.signed_truncate(writable!(dst), src, S32, dst_size, TruncKind::Checked)
2502            })
2503    }
2504
2505    fn visit_i64_trunc_sat_f32_u(&mut self) -> Self::Output {
2506        use OperandSize::*;
2507
2508        self.masm
2509            .unsigned_truncate(&mut self.context, S32, S64, TruncKind::Checked)
2510    }
2511
2512    fn visit_i64_trunc_sat_f64_s(&mut self) -> Self::Output {
2513        use OperandSize::*;
2514
2515        self.context
2516            .convert_op(self.masm, WasmValType::I64, |masm, dst, src, dst_size| {
2517                masm.signed_truncate(writable!(dst), src, S64, dst_size, TruncKind::Checked)
2518            })
2519    }
2520
2521    fn visit_i64_trunc_sat_f64_u(&mut self) -> Self::Output {
2522        use OperandSize::*;
2523
2524        self.masm
2525            .unsigned_truncate(&mut self.context, S64, S64, TruncKind::Checked)
2526    }
2527
2528    fn visit_i64_add128(&mut self) -> Self::Output {
2529        self.context
2530            .binop128(self.masm, |masm, lhs_lo, lhs_hi, rhs_lo, rhs_hi| {
2531                masm.add128(
2532                    writable!(lhs_lo),
2533                    writable!(lhs_hi),
2534                    lhs_lo,
2535                    lhs_hi,
2536                    rhs_lo,
2537                    rhs_hi,
2538                )?;
2539                Ok((TypedReg::i64(lhs_lo), TypedReg::i64(lhs_hi)))
2540            })
2541    }
2542
2543    fn visit_i64_sub128(&mut self) -> Self::Output {
2544        self.context
2545            .binop128(self.masm, |masm, lhs_lo, lhs_hi, rhs_lo, rhs_hi| {
2546                masm.sub128(
2547                    writable!(lhs_lo),
2548                    writable!(lhs_hi),
2549                    lhs_lo,
2550                    lhs_hi,
2551                    rhs_lo,
2552                    rhs_hi,
2553                )?;
2554                Ok((TypedReg::i64(lhs_lo), TypedReg::i64(lhs_hi)))
2555            })
2556    }
2557
2558    fn visit_i64_mul_wide_s(&mut self) -> Self::Output {
2559        self.masm.mul_wide(&mut self.context, MulWideKind::Signed)
2560    }
2561
2562    fn visit_i64_mul_wide_u(&mut self) -> Self::Output {
2563        self.masm.mul_wide(&mut self.context, MulWideKind::Unsigned)
2564    }
2565
2566    fn visit_i32_atomic_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2567        self.emit_wasm_load(
2568            &memarg,
2569            WasmValType::I32,
2570            LoadKind::Atomic(OperandSize::S8, Some(Extend::<Zero>::I32Extend8.into())),
2571        )
2572    }
2573
2574    fn visit_i32_atomic_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2575        self.emit_wasm_load(
2576            &memarg,
2577            WasmValType::I32,
2578            LoadKind::Atomic(OperandSize::S16, Some(Extend::<Zero>::I32Extend16.into())),
2579        )
2580    }
2581
2582    fn visit_i32_atomic_load(&mut self, memarg: MemArg) -> Self::Output {
2583        self.emit_wasm_load(
2584            &memarg,
2585            WasmValType::I32,
2586            LoadKind::Atomic(OperandSize::S32, None),
2587        )
2588    }
2589
2590    fn visit_i64_atomic_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2591        self.emit_wasm_load(
2592            &memarg,
2593            WasmValType::I64,
2594            LoadKind::Atomic(OperandSize::S8, Some(Extend::<Zero>::I64Extend8.into())),
2595        )
2596    }
2597
2598    fn visit_i64_atomic_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2599        self.emit_wasm_load(
2600            &memarg,
2601            WasmValType::I64,
2602            LoadKind::Atomic(OperandSize::S16, Some(Extend::<Zero>::I64Extend16.into())),
2603        )
2604    }
2605
2606    fn visit_i64_atomic_load32_u(&mut self, memarg: MemArg) -> Self::Output {
2607        self.emit_wasm_load(
2608            &memarg,
2609            WasmValType::I64,
2610            LoadKind::Atomic(OperandSize::S32, Some(Extend::<Zero>::I64Extend32.into())),
2611        )
2612    }
2613
2614    fn visit_i64_atomic_load(&mut self, memarg: MemArg) -> Self::Output {
2615        self.emit_wasm_load(
2616            &memarg,
2617            WasmValType::I64,
2618            LoadKind::Atomic(OperandSize::S64, None),
2619        )
2620    }
2621
2622    fn visit_i32_atomic_store(&mut self, memarg: MemArg) -> Self::Output {
2623        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S32))
2624    }
2625
2626    fn visit_i64_atomic_store(&mut self, memarg: MemArg) -> Self::Output {
2627        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S64))
2628    }
2629
2630    fn visit_i32_atomic_store8(&mut self, memarg: MemArg) -> Self::Output {
2631        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S8))
2632    }
2633
2634    fn visit_i32_atomic_store16(&mut self, memarg: MemArg) -> Self::Output {
2635        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S16))
2636    }
2637
2638    fn visit_i64_atomic_store8(&mut self, memarg: MemArg) -> Self::Output {
2639        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S8))
2640    }
2641
2642    fn visit_i64_atomic_store16(&mut self, memarg: MemArg) -> Self::Output {
2643        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S16))
2644    }
2645
2646    fn visit_i64_atomic_store32(&mut self, memarg: MemArg) -> Self::Output {
2647        self.emit_wasm_store(&memarg, StoreKind::Atomic(OperandSize::S32))
2648    }
2649
2650    fn visit_i32_atomic_rmw8_add_u(&mut self, arg: MemArg) -> Self::Output {
2651        self.emit_atomic_rmw(
2652            &arg,
2653            RmwOp::Add,
2654            OperandSize::S8,
2655            Some(Extend::<Zero>::I32Extend8),
2656        )
2657    }
2658
2659    fn visit_i32_atomic_rmw16_add_u(&mut self, arg: MemArg) -> Self::Output {
2660        self.emit_atomic_rmw(
2661            &arg,
2662            RmwOp::Add,
2663            OperandSize::S16,
2664            Some(Extend::<Zero>::I32Extend16),
2665        )
2666    }
2667
2668    fn visit_i32_atomic_rmw_add(&mut self, arg: MemArg) -> Self::Output {
2669        self.emit_atomic_rmw(&arg, RmwOp::Add, OperandSize::S32, None)
2670    }
2671
2672    fn visit_i64_atomic_rmw8_add_u(&mut self, arg: MemArg) -> Self::Output {
2673        self.emit_atomic_rmw(
2674            &arg,
2675            RmwOp::Add,
2676            OperandSize::S8,
2677            Some(Extend::<Zero>::I64Extend8),
2678        )
2679    }
2680
2681    fn visit_i64_atomic_rmw16_add_u(&mut self, arg: MemArg) -> Self::Output {
2682        self.emit_atomic_rmw(
2683            &arg,
2684            RmwOp::Add,
2685            OperandSize::S16,
2686            Some(Extend::<Zero>::I64Extend16),
2687        )
2688    }
2689
2690    fn visit_i64_atomic_rmw32_add_u(&mut self, arg: MemArg) -> Self::Output {
2691        self.emit_atomic_rmw(
2692            &arg,
2693            RmwOp::Add,
2694            OperandSize::S32,
2695            Some(Extend::<Zero>::I64Extend32),
2696        )
2697    }
2698
2699    fn visit_i64_atomic_rmw_add(&mut self, arg: MemArg) -> Self::Output {
2700        self.emit_atomic_rmw(&arg, RmwOp::Add, OperandSize::S64, None)
2701    }
2702
2703    fn visit_i32_atomic_rmw8_sub_u(&mut self, arg: MemArg) -> Self::Output {
2704        self.emit_atomic_rmw(
2705            &arg,
2706            RmwOp::Sub,
2707            OperandSize::S8,
2708            Some(Extend::<Zero>::I32Extend8),
2709        )
2710    }
2711    fn visit_i32_atomic_rmw16_sub_u(&mut self, arg: MemArg) -> Self::Output {
2712        self.emit_atomic_rmw(
2713            &arg,
2714            RmwOp::Sub,
2715            OperandSize::S16,
2716            Some(Extend::<Zero>::I32Extend16),
2717        )
2718    }
2719
2720    fn visit_i32_atomic_rmw_sub(&mut self, arg: MemArg) -> Self::Output {
2721        self.emit_atomic_rmw(&arg, RmwOp::Sub, OperandSize::S32, None)
2722    }
2723
2724    fn visit_i64_atomic_rmw8_sub_u(&mut self, arg: MemArg) -> Self::Output {
2725        self.emit_atomic_rmw(
2726            &arg,
2727            RmwOp::Sub,
2728            OperandSize::S8,
2729            Some(Extend::<Zero>::I64Extend8),
2730        )
2731    }
2732
2733    fn visit_i64_atomic_rmw16_sub_u(&mut self, arg: MemArg) -> Self::Output {
2734        self.emit_atomic_rmw(
2735            &arg,
2736            RmwOp::Sub,
2737            OperandSize::S16,
2738            Some(Extend::<Zero>::I64Extend16),
2739        )
2740    }
2741
2742    fn visit_i64_atomic_rmw32_sub_u(&mut self, arg: MemArg) -> Self::Output {
2743        self.emit_atomic_rmw(
2744            &arg,
2745            RmwOp::Sub,
2746            OperandSize::S32,
2747            Some(Extend::<Zero>::I64Extend32),
2748        )
2749    }
2750
2751    fn visit_i64_atomic_rmw_sub(&mut self, arg: MemArg) -> Self::Output {
2752        self.emit_atomic_rmw(&arg, RmwOp::Sub, OperandSize::S64, None)
2753    }
2754
2755    fn visit_i32_atomic_rmw8_xchg_u(&mut self, arg: MemArg) -> Self::Output {
2756        self.emit_atomic_rmw(
2757            &arg,
2758            RmwOp::Xchg,
2759            OperandSize::S8,
2760            Some(Extend::<Zero>::I32Extend8),
2761        )
2762    }
2763
2764    fn visit_i32_atomic_rmw16_xchg_u(&mut self, arg: MemArg) -> Self::Output {
2765        self.emit_atomic_rmw(
2766            &arg,
2767            RmwOp::Xchg,
2768            OperandSize::S16,
2769            Some(Extend::<Zero>::I32Extend16),
2770        )
2771    }
2772
2773    fn visit_i32_atomic_rmw_xchg(&mut self, arg: MemArg) -> Self::Output {
2774        self.emit_atomic_rmw(&arg, RmwOp::Xchg, OperandSize::S32, None)
2775    }
2776
2777    fn visit_i64_atomic_rmw8_xchg_u(&mut self, arg: MemArg) -> Self::Output {
2778        self.emit_atomic_rmw(
2779            &arg,
2780            RmwOp::Xchg,
2781            OperandSize::S8,
2782            Some(Extend::<Zero>::I64Extend8),
2783        )
2784    }
2785
2786    fn visit_i64_atomic_rmw16_xchg_u(&mut self, arg: MemArg) -> Self::Output {
2787        self.emit_atomic_rmw(
2788            &arg,
2789            RmwOp::Xchg,
2790            OperandSize::S16,
2791            Some(Extend::<Zero>::I64Extend16),
2792        )
2793    }
2794
2795    fn visit_i64_atomic_rmw32_xchg_u(&mut self, arg: MemArg) -> Self::Output {
2796        self.emit_atomic_rmw(
2797            &arg,
2798            RmwOp::Xchg,
2799            OperandSize::S32,
2800            Some(Extend::<Zero>::I64Extend32),
2801        )
2802    }
2803
2804    fn visit_i64_atomic_rmw_xchg(&mut self, arg: MemArg) -> Self::Output {
2805        self.emit_atomic_rmw(&arg, RmwOp::Xchg, OperandSize::S64, None)
2806    }
2807
2808    fn visit_i32_atomic_rmw8_and_u(&mut self, arg: MemArg) -> Self::Output {
2809        self.emit_atomic_rmw(
2810            &arg,
2811            RmwOp::And,
2812            OperandSize::S8,
2813            Some(Extend::<Zero>::I32Extend8),
2814        )
2815    }
2816
2817    fn visit_i32_atomic_rmw16_and_u(&mut self, arg: MemArg) -> Self::Output {
2818        self.emit_atomic_rmw(
2819            &arg,
2820            RmwOp::And,
2821            OperandSize::S16,
2822            Some(Extend::<Zero>::I32Extend16),
2823        )
2824    }
2825
2826    fn visit_i32_atomic_rmw_and(&mut self, arg: MemArg) -> Self::Output {
2827        self.emit_atomic_rmw(&arg, RmwOp::And, OperandSize::S32, None)
2828    }
2829
2830    fn visit_i64_atomic_rmw8_and_u(&mut self, arg: MemArg) -> Self::Output {
2831        self.emit_atomic_rmw(
2832            &arg,
2833            RmwOp::And,
2834            OperandSize::S8,
2835            Some(Extend::<Zero>::I64Extend8),
2836        )
2837    }
2838
2839    fn visit_i64_atomic_rmw16_and_u(&mut self, arg: MemArg) -> Self::Output {
2840        self.emit_atomic_rmw(
2841            &arg,
2842            RmwOp::And,
2843            OperandSize::S16,
2844            Some(Extend::<Zero>::I64Extend16),
2845        )
2846    }
2847
2848    fn visit_i64_atomic_rmw32_and_u(&mut self, arg: MemArg) -> Self::Output {
2849        self.emit_atomic_rmw(
2850            &arg,
2851            RmwOp::And,
2852            OperandSize::S32,
2853            Some(Extend::<Zero>::I64Extend32),
2854        )
2855    }
2856
2857    fn visit_i64_atomic_rmw_and(&mut self, arg: MemArg) -> Self::Output {
2858        self.emit_atomic_rmw(&arg, RmwOp::And, OperandSize::S64, None)
2859    }
2860
2861    fn visit_i32_atomic_rmw8_or_u(&mut self, arg: MemArg) -> Self::Output {
2862        self.emit_atomic_rmw(
2863            &arg,
2864            RmwOp::Or,
2865            OperandSize::S8,
2866            Some(Extend::<Zero>::I32Extend8),
2867        )
2868    }
2869
2870    fn visit_i32_atomic_rmw16_or_u(&mut self, arg: MemArg) -> Self::Output {
2871        self.emit_atomic_rmw(
2872            &arg,
2873            RmwOp::Or,
2874            OperandSize::S16,
2875            Some(Extend::<Zero>::I32Extend16),
2876        )
2877    }
2878
2879    fn visit_i32_atomic_rmw_or(&mut self, arg: MemArg) -> Self::Output {
2880        self.emit_atomic_rmw(&arg, RmwOp::Or, OperandSize::S32, None)
2881    }
2882
2883    fn visit_i64_atomic_rmw8_or_u(&mut self, arg: MemArg) -> Self::Output {
2884        self.emit_atomic_rmw(
2885            &arg,
2886            RmwOp::Or,
2887            OperandSize::S8,
2888            Some(Extend::<Zero>::I64Extend8),
2889        )
2890    }
2891
2892    fn visit_i64_atomic_rmw16_or_u(&mut self, arg: MemArg) -> Self::Output {
2893        self.emit_atomic_rmw(
2894            &arg,
2895            RmwOp::Or,
2896            OperandSize::S16,
2897            Some(Extend::<Zero>::I64Extend16),
2898        )
2899    }
2900
2901    fn visit_i64_atomic_rmw32_or_u(&mut self, arg: MemArg) -> Self::Output {
2902        self.emit_atomic_rmw(
2903            &arg,
2904            RmwOp::Or,
2905            OperandSize::S32,
2906            Some(Extend::<Zero>::I64Extend32),
2907        )
2908    }
2909
2910    fn visit_i64_atomic_rmw_or(&mut self, arg: MemArg) -> Self::Output {
2911        self.emit_atomic_rmw(&arg, RmwOp::Or, OperandSize::S64, None)
2912    }
2913
2914    fn visit_i32_atomic_rmw8_xor_u(&mut self, arg: MemArg) -> Self::Output {
2915        self.emit_atomic_rmw(
2916            &arg,
2917            RmwOp::Xor,
2918            OperandSize::S8,
2919            Some(Extend::<Zero>::I32Extend8),
2920        )
2921    }
2922
2923    fn visit_i32_atomic_rmw16_xor_u(&mut self, arg: MemArg) -> Self::Output {
2924        self.emit_atomic_rmw(
2925            &arg,
2926            RmwOp::Xor,
2927            OperandSize::S16,
2928            Some(Extend::<Zero>::I32Extend16),
2929        )
2930    }
2931
2932    fn visit_i32_atomic_rmw_xor(&mut self, arg: MemArg) -> Self::Output {
2933        self.emit_atomic_rmw(&arg, RmwOp::Xor, OperandSize::S32, None)
2934    }
2935
2936    fn visit_i64_atomic_rmw8_xor_u(&mut self, arg: MemArg) -> Self::Output {
2937        self.emit_atomic_rmw(
2938            &arg,
2939            RmwOp::Xor,
2940            OperandSize::S8,
2941            Some(Extend::<Zero>::I64Extend8),
2942        )
2943    }
2944
2945    fn visit_i64_atomic_rmw16_xor_u(&mut self, arg: MemArg) -> Self::Output {
2946        self.emit_atomic_rmw(
2947            &arg,
2948            RmwOp::Xor,
2949            OperandSize::S16,
2950            Some(Extend::<Zero>::I64Extend16),
2951        )
2952    }
2953
2954    fn visit_i64_atomic_rmw32_xor_u(&mut self, arg: MemArg) -> Self::Output {
2955        self.emit_atomic_rmw(
2956            &arg,
2957            RmwOp::Xor,
2958            OperandSize::S32,
2959            Some(Extend::<Zero>::I64Extend32),
2960        )
2961    }
2962
2963    fn visit_i64_atomic_rmw_xor(&mut self, arg: MemArg) -> Self::Output {
2964        self.emit_atomic_rmw(&arg, RmwOp::Xor, OperandSize::S64, None)
2965    }
2966
2967    fn visit_i32_atomic_rmw8_cmpxchg_u(&mut self, arg: MemArg) -> Self::Output {
2968        self.emit_atomic_cmpxchg(&arg, OperandSize::S8, Some(Extend::I32Extend8))
2969    }
2970
2971    fn visit_i32_atomic_rmw16_cmpxchg_u(&mut self, arg: MemArg) -> Self::Output {
2972        self.emit_atomic_cmpxchg(&arg, OperandSize::S16, Some(Extend::I32Extend16))
2973    }
2974
2975    fn visit_i32_atomic_rmw_cmpxchg(&mut self, arg: MemArg) -> Self::Output {
2976        self.emit_atomic_cmpxchg(&arg, OperandSize::S32, None)
2977    }
2978
2979    fn visit_i64_atomic_rmw8_cmpxchg_u(&mut self, arg: MemArg) -> Self::Output {
2980        self.emit_atomic_cmpxchg(&arg, OperandSize::S8, Some(Extend::I64Extend8))
2981    }
2982
2983    fn visit_i64_atomic_rmw16_cmpxchg_u(&mut self, arg: MemArg) -> Self::Output {
2984        self.emit_atomic_cmpxchg(&arg, OperandSize::S16, Some(Extend::I64Extend16))
2985    }
2986
2987    fn visit_i64_atomic_rmw32_cmpxchg_u(&mut self, arg: MemArg) -> Self::Output {
2988        self.emit_atomic_cmpxchg(&arg, OperandSize::S32, Some(Extend::I64Extend32))
2989    }
2990
2991    fn visit_i64_atomic_rmw_cmpxchg(&mut self, arg: MemArg) -> Self::Output {
2992        self.emit_atomic_cmpxchg(&arg, OperandSize::S64, None)
2993    }
2994
2995    fn visit_memory_atomic_wait32(&mut self, arg: MemArg) -> Self::Output {
2996        self.emit_atomic_wait(&arg, AtomicWaitKind::Wait32)
2997    }
2998
2999    fn visit_memory_atomic_wait64(&mut self, arg: MemArg) -> Self::Output {
3000        self.emit_atomic_wait(&arg, AtomicWaitKind::Wait64)
3001    }
3002
3003    fn visit_memory_atomic_notify(&mut self, arg: MemArg) -> Self::Output {
3004        self.emit_atomic_notify(&arg)
3005    }
3006
3007    fn visit_atomic_fence(&mut self, ordering: Ordering) -> Self::Output {
3008        if ordering != Ordering::SeqCst {
3009            bail!(wasm_unsupported!("acquire-release-atomics"));
3010        }
3011        self.masm.fence()
3012    }
3013
3014    wasmparser::for_each_visit_operator!(def_unsupported);
3015}
3016
3017impl<'a, 'translation, 'data, M> VisitSimdOperator<'a>
3018    for CodeGen<'a, 'translation, 'data, M, Emission>
3019where
3020    M: MacroAssembler,
3021{
3022    fn visit_v128_const(&mut self, val: V128) -> Self::Output {
3023        self.context.stack.push(Val::v128(val.i128()));
3024        Ok(())
3025    }
3026
3027    fn visit_v128_load(&mut self, memarg: MemArg) -> Self::Output {
3028        self.emit_wasm_load(
3029            &memarg,
3030            WasmValType::V128,
3031            LoadKind::Operand(OperandSize::S128),
3032        )
3033    }
3034
3035    fn visit_v128_store(&mut self, memarg: MemArg) -> Self::Output {
3036        self.emit_wasm_store(&memarg, StoreKind::Operand(OperandSize::S128))
3037    }
3038
3039    fn visit_v128_load8x8_s(&mut self, memarg: MemArg) -> Self::Output {
3040        self.emit_wasm_load(
3041            &memarg,
3042            WasmValType::V128,
3043            LoadKind::VectorExtend(V128LoadExtendKind::E8x8S),
3044        )
3045    }
3046
3047    fn visit_v128_load8x8_u(&mut self, memarg: MemArg) -> Self::Output {
3048        self.emit_wasm_load(
3049            &memarg,
3050            WasmValType::V128,
3051            LoadKind::VectorExtend(V128LoadExtendKind::E8x8U),
3052        )
3053    }
3054
3055    fn visit_v128_load16x4_s(&mut self, memarg: MemArg) -> Self::Output {
3056        self.emit_wasm_load(
3057            &memarg,
3058            WasmValType::V128,
3059            LoadKind::VectorExtend(V128LoadExtendKind::E16x4S),
3060        )
3061    }
3062
3063    fn visit_v128_load16x4_u(&mut self, memarg: MemArg) -> Self::Output {
3064        self.emit_wasm_load(
3065            &memarg,
3066            WasmValType::V128,
3067            LoadKind::VectorExtend(V128LoadExtendKind::E16x4U),
3068        )
3069    }
3070
3071    fn visit_v128_load32x2_s(&mut self, memarg: MemArg) -> Self::Output {
3072        self.emit_wasm_load(
3073            &memarg,
3074            WasmValType::V128,
3075            LoadKind::VectorExtend(V128LoadExtendKind::E32x2S),
3076        )
3077    }
3078
3079    fn visit_v128_load32x2_u(&mut self, memarg: MemArg) -> Self::Output {
3080        self.emit_wasm_load(
3081            &memarg,
3082            WasmValType::V128,
3083            LoadKind::VectorExtend(V128LoadExtendKind::E32x2U),
3084        )
3085    }
3086
3087    fn visit_v128_load8_splat(&mut self, memarg: MemArg) -> Self::Output {
3088        self.emit_wasm_load(
3089            &memarg,
3090            WasmValType::V128,
3091            LoadKind::Splat(SplatLoadKind::S8),
3092        )
3093    }
3094
3095    fn visit_v128_load16_splat(&mut self, memarg: MemArg) -> Self::Output {
3096        self.emit_wasm_load(
3097            &memarg,
3098            WasmValType::V128,
3099            LoadKind::Splat(SplatLoadKind::S16),
3100        )
3101    }
3102
3103    fn visit_v128_load32_splat(&mut self, memarg: MemArg) -> Self::Output {
3104        self.emit_wasm_load(
3105            &memarg,
3106            WasmValType::V128,
3107            LoadKind::Splat(SplatLoadKind::S32),
3108        )
3109    }
3110
3111    fn visit_v128_load64_splat(&mut self, memarg: MemArg) -> Self::Output {
3112        self.emit_wasm_load(
3113            &memarg,
3114            WasmValType::V128,
3115            LoadKind::Splat(SplatLoadKind::S64),
3116        )
3117    }
3118
3119    fn visit_i8x16_splat(&mut self) -> Self::Output {
3120        self.masm.splat(&mut self.context, SplatKind::I8x16)
3121    }
3122
3123    fn visit_i16x8_splat(&mut self) -> Self::Output {
3124        self.masm.splat(&mut self.context, SplatKind::I16x8)
3125    }
3126
3127    fn visit_i32x4_splat(&mut self) -> Self::Output {
3128        self.masm.splat(&mut self.context, SplatKind::I32x4)
3129    }
3130
3131    fn visit_i64x2_splat(&mut self) -> Self::Output {
3132        self.masm.splat(&mut self.context, SplatKind::I64x2)
3133    }
3134
3135    fn visit_f32x4_splat(&mut self) -> Self::Output {
3136        self.masm.splat(&mut self.context, SplatKind::F32x4)
3137    }
3138
3139    fn visit_f64x2_splat(&mut self) -> Self::Output {
3140        self.masm.splat(&mut self.context, SplatKind::F64x2)
3141    }
3142
3143    fn visit_i8x16_shuffle(&mut self, lanes: [u8; 16]) -> Self::Output {
3144        let rhs = self.context.pop_to_reg(self.masm, None)?;
3145        let lhs = self.context.pop_to_reg(self.masm, None)?;
3146        self.masm
3147            .shuffle(writable!(lhs.into()), lhs.into(), rhs.into(), lanes)?;
3148        self.context.stack.push(TypedReg::v128(lhs.into()).into());
3149        self.context.free_reg(rhs);
3150        Ok(())
3151    }
3152
3153    fn visit_i8x16_swizzle(&mut self) -> Self::Output {
3154        let rhs = self.context.pop_to_reg(self.masm, None)?;
3155        let lhs = self.context.pop_to_reg(self.masm, None)?;
3156        self.masm
3157            .swizzle(writable!(lhs.into()), lhs.into(), rhs.into())?;
3158        self.context.stack.push(TypedReg::v128(lhs.into()).into());
3159        self.context.free_reg(rhs);
3160        Ok(())
3161    }
3162
3163    fn visit_i8x16_extract_lane_s(&mut self, lane: u8) -> Self::Output {
3164        self.context.extract_lane_op(
3165            self.masm,
3166            ExtractLaneKind::I8x16S,
3167            |masm, src, dst, kind| masm.extract_lane(src, dst, lane, kind),
3168        )
3169    }
3170
3171    fn visit_i8x16_extract_lane_u(&mut self, lane: u8) -> Self::Output {
3172        self.context.extract_lane_op(
3173            self.masm,
3174            ExtractLaneKind::I8x16U,
3175            |masm, src, dst, kind| masm.extract_lane(src, dst, lane, kind),
3176        )
3177    }
3178
3179    fn visit_i16x8_extract_lane_s(&mut self, lane: u8) -> Self::Output {
3180        self.context.extract_lane_op(
3181            self.masm,
3182            ExtractLaneKind::I16x8S,
3183            |masm, src, dst, kind| masm.extract_lane(src, dst, lane, kind),
3184        )
3185    }
3186
3187    fn visit_i16x8_extract_lane_u(&mut self, lane: u8) -> Self::Output {
3188        self.context.extract_lane_op(
3189            self.masm,
3190            ExtractLaneKind::I16x8U,
3191            |masm, src, dst, kind| masm.extract_lane(src, dst, lane, kind),
3192        )
3193    }
3194
3195    fn visit_i32x4_extract_lane(&mut self, lane: u8) -> Self::Output {
3196        self.context
3197            .extract_lane_op(self.masm, ExtractLaneKind::I32x4, |masm, src, dst, kind| {
3198                masm.extract_lane(src, dst, lane, kind)
3199            })
3200    }
3201
3202    fn visit_i64x2_extract_lane(&mut self, lane: u8) -> Self::Output {
3203        self.context
3204            .extract_lane_op(self.masm, ExtractLaneKind::I64x2, |masm, src, dst, kind| {
3205                masm.extract_lane(src, dst, lane, kind)
3206            })
3207    }
3208
3209    fn visit_f32x4_extract_lane(&mut self, lane: u8) -> Self::Output {
3210        self.context
3211            .extract_lane_op(self.masm, ExtractLaneKind::F32x4, |masm, src, dst, kind| {
3212                masm.extract_lane(src, dst, lane, kind)
3213            })
3214    }
3215
3216    fn visit_f64x2_extract_lane(&mut self, lane: u8) -> Self::Output {
3217        self.context
3218            .extract_lane_op(self.masm, ExtractLaneKind::F64x2, |masm, src, dst, kind| {
3219                masm.extract_lane(src, dst, lane, kind)
3220            })
3221    }
3222
3223    fn visit_i8x16_eq(&mut self) -> Self::Output {
3224        self.context
3225            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3226                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::I8x16)?;
3227                Ok(TypedReg::v128(dst))
3228            })
3229    }
3230
3231    fn visit_i16x8_eq(&mut self) -> Self::Output {
3232        self.context
3233            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3234                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::I16x8)?;
3235                Ok(TypedReg::v128(dst))
3236            })
3237    }
3238
3239    fn visit_i32x4_eq(&mut self) -> Self::Output {
3240        self.context
3241            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3242                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::I32x4)?;
3243                Ok(TypedReg::v128(dst))
3244            })
3245    }
3246
3247    fn visit_i64x2_eq(&mut self) -> Self::Output {
3248        self.context
3249            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3250                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::I64x2)?;
3251                Ok(TypedReg::v128(dst))
3252            })
3253    }
3254
3255    fn visit_f32x4_eq(&mut self) -> Self::Output {
3256        self.context
3257            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3258                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::F32x4)?;
3259                Ok(TypedReg::v128(dst))
3260            })
3261    }
3262
3263    fn visit_f64x2_eq(&mut self) -> Self::Output {
3264        self.context
3265            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3266                masm.v128_eq(writable!(dst), dst, src, VectorEqualityKind::F64x2)?;
3267                Ok(TypedReg::v128(dst))
3268            })
3269    }
3270
3271    fn visit_i8x16_ne(&mut self) -> Self::Output {
3272        self.context
3273            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3274                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::I8x16)?;
3275                Ok(TypedReg::v128(dst))
3276            })
3277    }
3278
3279    fn visit_i16x8_ne(&mut self) -> Self::Output {
3280        self.context
3281            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3282                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::I16x8)?;
3283                Ok(TypedReg::v128(dst))
3284            })
3285    }
3286
3287    fn visit_i32x4_ne(&mut self) -> Self::Output {
3288        self.context
3289            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3290                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::I32x4)?;
3291                Ok(TypedReg::v128(dst))
3292            })
3293    }
3294
3295    fn visit_i64x2_ne(&mut self) -> Self::Output {
3296        self.context
3297            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3298                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::I64x2)?;
3299                Ok(TypedReg::v128(dst))
3300            })
3301    }
3302
3303    fn visit_f32x4_ne(&mut self) -> Self::Output {
3304        self.context
3305            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3306                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::F32x4)?;
3307                Ok(TypedReg::v128(dst))
3308            })
3309    }
3310
3311    fn visit_f64x2_ne(&mut self) -> Self::Output {
3312        self.context
3313            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3314                masm.v128_ne(writable!(dst), dst, src, VectorEqualityKind::F64x2)?;
3315                Ok(TypedReg::v128(dst))
3316            })
3317    }
3318
3319    fn visit_i8x16_lt_s(&mut self) -> Self::Output {
3320        self.context
3321            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3322                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I8x16S)?;
3323                Ok(TypedReg::v128(dst))
3324            })
3325    }
3326
3327    fn visit_i8x16_lt_u(&mut self) -> Self::Output {
3328        self.context
3329            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3330                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I8x16U)?;
3331                Ok(TypedReg::v128(dst))
3332            })
3333    }
3334
3335    fn visit_i16x8_lt_s(&mut self) -> Self::Output {
3336        self.context
3337            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3338                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I16x8S)?;
3339                Ok(TypedReg::v128(dst))
3340            })
3341    }
3342
3343    fn visit_i16x8_lt_u(&mut self) -> Self::Output {
3344        self.context
3345            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3346                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I16x8U)?;
3347                Ok(TypedReg::v128(dst))
3348            })
3349    }
3350
3351    fn visit_i32x4_lt_s(&mut self) -> Self::Output {
3352        self.context
3353            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3354                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I32x4S)?;
3355                Ok(TypedReg::v128(dst))
3356            })
3357    }
3358
3359    fn visit_i32x4_lt_u(&mut self) -> Self::Output {
3360        self.context
3361            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3362                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I32x4U)?;
3363                Ok(TypedReg::v128(dst))
3364            })
3365    }
3366
3367    fn visit_i64x2_lt_s(&mut self) -> Self::Output {
3368        self.context
3369            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3370                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::I64x2S)?;
3371                Ok(TypedReg::v128(dst))
3372            })
3373    }
3374
3375    fn visit_f32x4_lt(&mut self) -> Self::Output {
3376        self.context
3377            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3378                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::F32x4)?;
3379                Ok(TypedReg::v128(dst))
3380            })
3381    }
3382
3383    fn visit_f64x2_lt(&mut self) -> Self::Output {
3384        self.context
3385            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3386                masm.v128_lt(writable!(dst), dst, src, VectorCompareKind::F64x2)?;
3387                Ok(TypedReg::v128(dst))
3388            })
3389    }
3390
3391    fn visit_i8x16_le_s(&mut self) -> Self::Output {
3392        self.context
3393            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3394                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I8x16S)?;
3395                Ok(TypedReg::v128(dst))
3396            })
3397    }
3398
3399    fn visit_i8x16_le_u(&mut self) -> Self::Output {
3400        self.context
3401            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3402                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I8x16U)?;
3403                Ok(TypedReg::v128(dst))
3404            })
3405    }
3406
3407    fn visit_i16x8_le_s(&mut self) -> Self::Output {
3408        self.context
3409            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3410                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I16x8S)?;
3411                Ok(TypedReg::v128(dst))
3412            })
3413    }
3414
3415    fn visit_i16x8_le_u(&mut self) -> Self::Output {
3416        self.context
3417            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3418                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I16x8U)?;
3419                Ok(TypedReg::v128(dst))
3420            })
3421    }
3422
3423    fn visit_i32x4_le_s(&mut self) -> Self::Output {
3424        self.context
3425            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3426                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I32x4S)?;
3427                Ok(TypedReg::v128(dst))
3428            })
3429    }
3430
3431    fn visit_i32x4_le_u(&mut self) -> Self::Output {
3432        self.context
3433            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3434                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I32x4U)?;
3435                Ok(TypedReg::v128(dst))
3436            })
3437    }
3438
3439    fn visit_i64x2_le_s(&mut self) -> Self::Output {
3440        self.context
3441            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3442                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::I64x2S)?;
3443                Ok(TypedReg::v128(dst))
3444            })
3445    }
3446
3447    fn visit_f32x4_le(&mut self) -> Self::Output {
3448        self.context
3449            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3450                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::F32x4)?;
3451                Ok(TypedReg::v128(dst))
3452            })
3453    }
3454
3455    fn visit_f64x2_le(&mut self) -> Self::Output {
3456        self.context
3457            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3458                masm.v128_le(writable!(dst), dst, src, VectorCompareKind::F64x2)?;
3459                Ok(TypedReg::v128(dst))
3460            })
3461    }
3462
3463    fn visit_i8x16_gt_s(&mut self) -> Self::Output {
3464        self.context
3465            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3466                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I8x16S)?;
3467                Ok(TypedReg::v128(dst))
3468            })
3469    }
3470
3471    fn visit_i8x16_gt_u(&mut self) -> Self::Output {
3472        self.context
3473            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3474                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I8x16U)?;
3475                Ok(TypedReg::v128(dst))
3476            })
3477    }
3478
3479    fn visit_i16x8_gt_s(&mut self) -> Self::Output {
3480        self.context
3481            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3482                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I16x8S)?;
3483                Ok(TypedReg::v128(dst))
3484            })
3485    }
3486
3487    fn visit_i16x8_gt_u(&mut self) -> Self::Output {
3488        self.context
3489            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3490                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I16x8U)?;
3491                Ok(TypedReg::v128(dst))
3492            })
3493    }
3494
3495    fn visit_i32x4_gt_s(&mut self) -> Self::Output {
3496        self.context
3497            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3498                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I32x4S)?;
3499                Ok(TypedReg::v128(dst))
3500            })
3501    }
3502
3503    fn visit_i32x4_gt_u(&mut self) -> Self::Output {
3504        self.context
3505            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3506                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I32x4U)?;
3507                Ok(TypedReg::v128(dst))
3508            })
3509    }
3510
3511    fn visit_i64x2_gt_s(&mut self) -> Self::Output {
3512        self.context
3513            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3514                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::I64x2S)?;
3515                Ok(TypedReg::v128(dst))
3516            })
3517    }
3518
3519    fn visit_f32x4_gt(&mut self) -> Self::Output {
3520        self.context
3521            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3522                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::F32x4)?;
3523                Ok(TypedReg::v128(dst))
3524            })
3525    }
3526
3527    fn visit_f64x2_gt(&mut self) -> Self::Output {
3528        self.context
3529            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3530                masm.v128_gt(writable!(dst), dst, src, VectorCompareKind::F64x2)?;
3531                Ok(TypedReg::v128(dst))
3532            })
3533    }
3534
3535    fn visit_i8x16_ge_s(&mut self) -> Self::Output {
3536        self.context
3537            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3538                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I8x16S)?;
3539                Ok(TypedReg::v128(dst))
3540            })
3541    }
3542
3543    fn visit_i8x16_ge_u(&mut self) -> Self::Output {
3544        self.context
3545            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3546                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I8x16U)?;
3547                Ok(TypedReg::v128(dst))
3548            })
3549    }
3550
3551    fn visit_i16x8_ge_s(&mut self) -> Self::Output {
3552        self.context
3553            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3554                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I16x8S)?;
3555                Ok(TypedReg::v128(dst))
3556            })
3557    }
3558
3559    fn visit_i16x8_ge_u(&mut self) -> Self::Output {
3560        self.context
3561            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3562                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I16x8U)?;
3563                Ok(TypedReg::v128(dst))
3564            })
3565    }
3566
3567    fn visit_i32x4_ge_s(&mut self) -> Self::Output {
3568        self.context
3569            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3570                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I32x4S)?;
3571                Ok(TypedReg::v128(dst))
3572            })
3573    }
3574
3575    fn visit_i32x4_ge_u(&mut self) -> Self::Output {
3576        self.context
3577            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3578                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I32x4U)?;
3579                Ok(TypedReg::v128(dst))
3580            })
3581    }
3582
3583    fn visit_i64x2_ge_s(&mut self) -> Self::Output {
3584        self.context
3585            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3586                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::I64x2S)?;
3587                Ok(TypedReg::v128(dst))
3588            })
3589    }
3590
3591    fn visit_f32x4_ge(&mut self) -> Self::Output {
3592        self.context
3593            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3594                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::F32x4)?;
3595                Ok(TypedReg::v128(dst))
3596            })
3597    }
3598
3599    fn visit_f64x2_ge(&mut self) -> Self::Output {
3600        self.context
3601            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3602                masm.v128_ge(writable!(dst), dst, src, VectorCompareKind::F64x2)?;
3603                Ok(TypedReg::v128(dst))
3604            })
3605    }
3606
3607    fn visit_i8x16_replace_lane(&mut self, lane: u8) -> Self::Output {
3608        self.context
3609            .replace_lane_op(self.masm, ReplaceLaneKind::I8x16, |masm, src, dst, kind| {
3610                masm.replace_lane(src, dst, lane, kind)
3611            })
3612    }
3613
3614    fn visit_i16x8_replace_lane(&mut self, lane: u8) -> Self::Output {
3615        self.context
3616            .replace_lane_op(self.masm, ReplaceLaneKind::I16x8, |masm, src, dst, kind| {
3617                masm.replace_lane(src, dst, lane, kind)
3618            })
3619    }
3620
3621    fn visit_i32x4_replace_lane(&mut self, lane: u8) -> Self::Output {
3622        self.context
3623            .replace_lane_op(self.masm, ReplaceLaneKind::I32x4, |masm, src, dst, kind| {
3624                masm.replace_lane(src, dst, lane, kind)
3625            })
3626    }
3627
3628    fn visit_i64x2_replace_lane(&mut self, lane: u8) -> Self::Output {
3629        self.context
3630            .replace_lane_op(self.masm, ReplaceLaneKind::I64x2, |masm, src, dst, kind| {
3631                masm.replace_lane(src, dst, lane, kind)
3632            })
3633    }
3634
3635    fn visit_f32x4_replace_lane(&mut self, lane: u8) -> Self::Output {
3636        self.context
3637            .replace_lane_op(self.masm, ReplaceLaneKind::F32x4, |masm, src, dst, kind| {
3638                masm.replace_lane(src, dst, lane, kind)
3639            })
3640    }
3641
3642    fn visit_f64x2_replace_lane(&mut self, lane: u8) -> Self::Output {
3643        self.context
3644            .replace_lane_op(self.masm, ReplaceLaneKind::F64x2, |masm, src, dst, kind| {
3645                masm.replace_lane(src, dst, lane, kind)
3646            })
3647    }
3648
3649    fn visit_v128_not(&mut self) -> Self::Output {
3650        self.context.unop(self.masm, |masm, reg| {
3651            masm.v128_not(writable!(reg))?;
3652            Ok(TypedReg::new(WasmValType::V128, reg))
3653        })
3654    }
3655
3656    fn visit_v128_and(&mut self) -> Self::Output {
3657        self.context
3658            .binop(self.masm, OperandSize::S128, |masm, dst, src, _size| {
3659                masm.v128_and(dst, src, writable!(dst))?;
3660                Ok(TypedReg::new(WasmValType::V128, dst))
3661            })
3662    }
3663
3664    fn visit_v128_andnot(&mut self) -> Self::Output {
3665        self.context
3666            .binop(self.masm, OperandSize::S128, |masm, dst, src, _size| {
3667                // careful here: and_not is *not* commutative: dst = !src1 & src2
3668                masm.v128_and_not(src, dst, writable!(dst))?;
3669                Ok(TypedReg::new(WasmValType::V128, dst))
3670            })
3671    }
3672
3673    fn visit_v128_or(&mut self) -> Self::Output {
3674        self.context
3675            .binop(self.masm, OperandSize::S128, |masm, dst, src, _size| {
3676                // careful here: and_not is *not* commutative: dst = !src1 & src2
3677                masm.v128_or(src, dst, writable!(dst))?;
3678                Ok(TypedReg::new(WasmValType::V128, dst))
3679            })
3680    }
3681
3682    fn visit_v128_xor(&mut self) -> Self::Output {
3683        self.context
3684            .binop(self.masm, OperandSize::S128, |masm, dst, src, _size| {
3685                // careful here: and_not is *not* commutative: dst = !src1 & src2
3686                masm.v128_xor(src, dst, writable!(dst))?;
3687                Ok(TypedReg::new(WasmValType::V128, dst))
3688            })
3689    }
3690
3691    fn visit_v128_bitselect(&mut self) -> Self::Output {
3692        let mask = self.context.pop_to_reg(self.masm, None)?;
3693        let op2 = self.context.pop_to_reg(self.masm, None)?;
3694        let op1 = self.context.pop_to_reg(self.masm, None)?;
3695        let dst = self.context.any_fpr(self.masm)?;
3696
3697        // careful here: bitselect is *not* commutative.
3698        self.masm
3699            .v128_bitselect(op1.reg, op2.reg, mask.reg, writable!(dst))?;
3700
3701        self.context
3702            .stack
3703            .push(TypedReg::new(WasmValType::V128, dst).into());
3704        self.context.free_reg(op1);
3705        self.context.free_reg(op2);
3706        self.context.free_reg(mask);
3707
3708        Ok(())
3709    }
3710
3711    fn visit_v128_any_true(&mut self) -> Self::Output {
3712        let src = self.context.pop_to_reg(self.masm, None)?;
3713        let dst = self.context.any_gpr(self.masm)?;
3714
3715        self.masm.v128_any_true(src.reg, writable!(dst))?;
3716
3717        self.context
3718            .stack
3719            .push(TypedReg::new(WasmValType::I32, dst).into());
3720        self.context.free_reg(src);
3721
3722        Ok(())
3723    }
3724
3725    fn visit_v128_load8_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3726        self.emit_wasm_load(
3727            &arg,
3728            WasmValType::V128,
3729            LoadKind::vector_lane(lane, OperandSize::S8),
3730        )
3731    }
3732
3733    fn visit_v128_load16_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3734        self.emit_wasm_load(
3735            &arg,
3736            WasmValType::V128,
3737            LoadKind::vector_lane(lane, OperandSize::S16),
3738        )
3739    }
3740
3741    fn visit_v128_load32_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3742        self.emit_wasm_load(
3743            &arg,
3744            WasmValType::V128,
3745            LoadKind::vector_lane(lane, OperandSize::S32),
3746        )
3747    }
3748
3749    fn visit_v128_load64_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3750        self.emit_wasm_load(
3751            &arg,
3752            WasmValType::V128,
3753            LoadKind::vector_lane(lane, OperandSize::S64),
3754        )
3755    }
3756
3757    fn visit_v128_store8_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3758        self.emit_wasm_store(&arg, StoreKind::vector_lane(lane, OperandSize::S8))
3759    }
3760
3761    fn visit_v128_store16_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3762        self.emit_wasm_store(&arg, StoreKind::vector_lane(lane, OperandSize::S16))
3763    }
3764
3765    fn visit_v128_store32_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3766        self.emit_wasm_store(&arg, StoreKind::vector_lane(lane, OperandSize::S32))
3767    }
3768
3769    fn visit_v128_store64_lane(&mut self, arg: MemArg, lane: u8) -> Self::Output {
3770        self.emit_wasm_store(&arg, StoreKind::vector_lane(lane, OperandSize::S64))
3771    }
3772
3773    fn visit_f32x4_convert_i32x4_s(&mut self) -> Self::Output {
3774        self.context.unop(self.masm, |masm, reg| {
3775            masm.v128_convert(reg, writable!(reg), V128ConvertKind::I32x4S)?;
3776            Ok(TypedReg::v128(reg))
3777        })
3778    }
3779
3780    fn visit_f32x4_convert_i32x4_u(&mut self) -> Self::Output {
3781        self.context.unop(self.masm, |masm, reg| {
3782            masm.v128_convert(reg, writable!(reg), V128ConvertKind::I32x4U)?;
3783            Ok(TypedReg::v128(reg))
3784        })
3785    }
3786
3787    fn visit_f64x2_convert_low_i32x4_s(&mut self) -> Self::Output {
3788        self.context.unop(self.masm, |masm, reg| {
3789            masm.v128_convert(reg, writable!(reg), V128ConvertKind::I32x4LowS)?;
3790            Ok(TypedReg::v128(reg))
3791        })
3792    }
3793
3794    fn visit_f64x2_convert_low_i32x4_u(&mut self) -> Self::Output {
3795        self.context.unop(self.masm, |masm, reg| {
3796            masm.v128_convert(reg, writable!(reg), V128ConvertKind::I32x4LowU)?;
3797            Ok(TypedReg::v128(reg))
3798        })
3799    }
3800
3801    fn visit_i8x16_narrow_i16x8_s(&mut self) -> Self::Output {
3802        self.context
3803            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3804                masm.v128_narrow(dst, src, writable!(dst), V128NarrowKind::I16x8S)?;
3805                Ok(TypedReg::v128(dst))
3806            })
3807    }
3808
3809    fn visit_i8x16_narrow_i16x8_u(&mut self) -> Self::Output {
3810        self.context
3811            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3812                masm.v128_narrow(dst, src, writable!(dst), V128NarrowKind::I16x8U)?;
3813                Ok(TypedReg::v128(dst))
3814            })
3815    }
3816
3817    fn visit_i16x8_narrow_i32x4_s(&mut self) -> Self::Output {
3818        self.context
3819            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3820                masm.v128_narrow(dst, src, writable!(dst), V128NarrowKind::I32x4S)?;
3821                Ok(TypedReg::v128(dst))
3822            })
3823    }
3824
3825    fn visit_i16x8_narrow_i32x4_u(&mut self) -> Self::Output {
3826        self.context
3827            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3828                masm.v128_narrow(dst, src, writable!(dst), V128NarrowKind::I32x4U)?;
3829                Ok(TypedReg::v128(dst))
3830            })
3831    }
3832
3833    fn visit_f32x4_demote_f64x2_zero(&mut self) -> Self::Output {
3834        self.context.unop(self.masm, |masm, reg| {
3835            masm.v128_demote(reg, writable!(reg))?;
3836            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S32)?;
3837            Ok(TypedReg::v128(reg))
3838        })
3839    }
3840
3841    fn visit_f64x2_promote_low_f32x4(&mut self) -> Self::Output {
3842        self.context.unop(self.masm, |masm, reg| {
3843            masm.v128_promote(reg, writable!(reg))?;
3844            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S64)?;
3845            Ok(TypedReg::v128(reg))
3846        })
3847    }
3848
3849    fn visit_i16x8_extend_low_i8x16_s(&mut self) -> Self::Output {
3850        self.context.unop(self.masm, |masm, reg| {
3851            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI8x16S)?;
3852            Ok(TypedReg::v128(reg))
3853        })
3854    }
3855
3856    fn visit_i16x8_extend_high_i8x16_s(&mut self) -> Self::Output {
3857        self.context.unop(self.masm, |masm, reg| {
3858            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI8x16S)?;
3859            Ok(TypedReg::v128(reg))
3860        })
3861    }
3862
3863    fn visit_i16x8_extend_low_i8x16_u(&mut self) -> Self::Output {
3864        self.context.unop(self.masm, |masm, reg| {
3865            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI8x16U)?;
3866            Ok(TypedReg::v128(reg))
3867        })
3868    }
3869
3870    fn visit_i16x8_extend_high_i8x16_u(&mut self) -> Self::Output {
3871        self.context.unop(self.masm, |masm, reg| {
3872            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI8x16U)?;
3873            Ok(TypedReg::v128(reg))
3874        })
3875    }
3876
3877    fn visit_i32x4_extend_low_i16x8_s(&mut self) -> Self::Output {
3878        self.context.unop(self.masm, |masm, reg| {
3879            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI16x8S)?;
3880            Ok(TypedReg::v128(reg))
3881        })
3882    }
3883
3884    fn visit_i32x4_extend_high_i16x8_s(&mut self) -> Self::Output {
3885        self.context.unop(self.masm, |masm, reg| {
3886            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI16x8S)?;
3887            Ok(TypedReg::v128(reg))
3888        })
3889    }
3890
3891    fn visit_i32x4_extend_low_i16x8_u(&mut self) -> Self::Output {
3892        self.context.unop(self.masm, |masm, reg| {
3893            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI16x8U)?;
3894            Ok(TypedReg::v128(reg))
3895        })
3896    }
3897
3898    fn visit_i32x4_extend_high_i16x8_u(&mut self) -> Self::Output {
3899        self.context.unop(self.masm, |masm, reg| {
3900            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI16x8U)?;
3901            Ok(TypedReg::v128(reg))
3902        })
3903    }
3904
3905    fn visit_i64x2_extend_low_i32x4_s(&mut self) -> Self::Output {
3906        self.context.unop(self.masm, |masm, reg| {
3907            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI32x4S)?;
3908            Ok(TypedReg::v128(reg))
3909        })
3910    }
3911
3912    fn visit_i64x2_extend_high_i32x4_s(&mut self) -> Self::Output {
3913        self.context.unop(self.masm, |masm, reg| {
3914            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI32x4S)?;
3915            Ok(TypedReg::v128(reg))
3916        })
3917    }
3918
3919    fn visit_i64x2_extend_low_i32x4_u(&mut self) -> Self::Output {
3920        self.context.unop(self.masm, |masm, reg| {
3921            masm.v128_extend(reg, writable!(reg), V128ExtendKind::LowI32x4U)?;
3922            Ok(TypedReg::v128(reg))
3923        })
3924    }
3925
3926    fn visit_i64x2_extend_high_i32x4_u(&mut self) -> Self::Output {
3927        self.context.unop(self.masm, |masm, reg| {
3928            masm.v128_extend(reg, writable!(reg), V128ExtendKind::HighI32x4U)?;
3929            Ok(TypedReg::v128(reg))
3930        })
3931    }
3932
3933    fn visit_i8x16_add(&mut self) -> Self::Output {
3934        self.context
3935            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3936                masm.v128_add(dst, src, writable!(dst), V128AddKind::I8x16)?;
3937                Ok(TypedReg::new(WasmValType::V128, dst))
3938            })
3939    }
3940
3941    fn visit_i16x8_add(&mut self) -> Self::Output {
3942        self.context
3943            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3944                masm.v128_add(dst, src, writable!(dst), V128AddKind::I16x8)?;
3945                Ok(TypedReg::new(WasmValType::V128, dst))
3946            })
3947    }
3948
3949    fn visit_i32x4_add(&mut self) -> Self::Output {
3950        self.context
3951            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3952                masm.v128_add(dst, src, writable!(dst), V128AddKind::I32x4)?;
3953                Ok(TypedReg::new(WasmValType::V128, dst))
3954            })
3955    }
3956
3957    fn visit_i64x2_add(&mut self) -> Self::Output {
3958        self.context
3959            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3960                masm.v128_add(dst, src, writable!(dst), V128AddKind::I64x2)?;
3961                Ok(TypedReg::new(WasmValType::V128, dst))
3962            })
3963    }
3964
3965    fn visit_i8x16_sub(&mut self) -> Self::Output {
3966        self.context
3967            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
3968                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I8x16)?;
3969                Ok(TypedReg::new(WasmValType::V128, dst))
3970            })
3971    }
3972
3973    fn visit_i16x8_sub(&mut self) -> Self::Output {
3974        self.context
3975            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
3976                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I16x8)?;
3977                Ok(TypedReg::new(WasmValType::V128, dst))
3978            })
3979    }
3980
3981    fn visit_i32x4_sub(&mut self) -> Self::Output {
3982        self.context
3983            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
3984                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I32x4)?;
3985                Ok(TypedReg::new(WasmValType::V128, dst))
3986            })
3987    }
3988
3989    fn visit_i64x2_sub(&mut self) -> Self::Output {
3990        self.context
3991            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
3992                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I64x2)?;
3993                Ok(TypedReg::new(WasmValType::V128, dst))
3994            })
3995    }
3996
3997    fn visit_i16x8_mul(&mut self) -> Self::Output {
3998        self.masm.v128_mul(&mut self.context, V128MulKind::I16x8)
3999    }
4000
4001    fn visit_i32x4_mul(&mut self) -> Self::Output {
4002        self.masm.v128_mul(&mut self.context, V128MulKind::I32x4)
4003    }
4004
4005    fn visit_i64x2_mul(&mut self) -> Self::Output {
4006        self.masm.v128_mul(&mut self.context, V128MulKind::I64x2)
4007    }
4008
4009    fn visit_i8x16_add_sat_s(&mut self) -> Self::Output {
4010        self.context
4011            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4012                masm.v128_add(dst, src, writable!(dst), V128AddKind::I8x16SatS)?;
4013                Ok(TypedReg::new(WasmValType::V128, dst))
4014            })
4015    }
4016
4017    fn visit_i16x8_add_sat_s(&mut self) -> Self::Output {
4018        self.context
4019            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4020                masm.v128_add(dst, src, writable!(dst), V128AddKind::I16x8SatS)?;
4021                Ok(TypedReg::new(WasmValType::V128, dst))
4022            })
4023    }
4024
4025    fn visit_i8x16_add_sat_u(&mut self) -> Self::Output {
4026        self.context
4027            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4028                masm.v128_add(dst, src, writable!(dst), V128AddKind::I8x16SatU)?;
4029                Ok(TypedReg::new(WasmValType::V128, dst))
4030            })
4031    }
4032
4033    fn visit_i16x8_add_sat_u(&mut self) -> Self::Output {
4034        self.context
4035            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4036                masm.v128_add(dst, src, writable!(dst), V128AddKind::I16x8SatU)?;
4037                Ok(TypedReg::new(WasmValType::V128, dst))
4038            })
4039    }
4040
4041    fn visit_i8x16_sub_sat_s(&mut self) -> Self::Output {
4042        self.context
4043            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4044                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I8x16SatS)?;
4045                Ok(TypedReg::new(WasmValType::V128, dst))
4046            })
4047    }
4048
4049    fn visit_i16x8_sub_sat_s(&mut self) -> Self::Output {
4050        self.context
4051            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4052                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I16x8SatS)?;
4053                Ok(TypedReg::new(WasmValType::V128, dst))
4054            })
4055    }
4056
4057    fn visit_i8x16_sub_sat_u(&mut self) -> Self::Output {
4058        self.context
4059            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4060                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I8x16SatU)?;
4061                Ok(TypedReg::new(WasmValType::V128, dst))
4062            })
4063    }
4064
4065    fn visit_i16x8_sub_sat_u(&mut self) -> Self::Output {
4066        self.context
4067            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4068                masm.v128_sub(dst, src, writable!(dst), V128SubKind::I16x8SatU)?;
4069                Ok(TypedReg::new(WasmValType::V128, dst))
4070            })
4071    }
4072
4073    fn visit_i8x16_abs(&mut self) -> Self::Output {
4074        self.context.unop(self.masm, |masm, reg| {
4075            masm.v128_abs(reg, writable!(reg), V128AbsKind::I8x16)?;
4076            Ok(TypedReg::new(WasmValType::V128, reg))
4077        })
4078    }
4079
4080    fn visit_i16x8_abs(&mut self) -> Self::Output {
4081        self.context.unop(self.masm, |masm, reg| {
4082            masm.v128_abs(reg, writable!(reg), V128AbsKind::I16x8)?;
4083            Ok(TypedReg::new(WasmValType::V128, reg))
4084        })
4085    }
4086
4087    fn visit_i32x4_abs(&mut self) -> Self::Output {
4088        self.context.unop(self.masm, |masm, reg| {
4089            masm.v128_abs(reg, writable!(reg), V128AbsKind::I32x4)?;
4090            Ok(TypedReg::new(WasmValType::V128, reg))
4091        })
4092    }
4093
4094    fn visit_i64x2_abs(&mut self) -> Self::Output {
4095        self.context.unop(self.masm, |masm, reg| {
4096            masm.v128_abs(reg, writable!(reg), V128AbsKind::I64x2)?;
4097            Ok(TypedReg::new(WasmValType::V128, reg))
4098        })
4099    }
4100
4101    fn visit_f32x4_abs(&mut self) -> Self::Output {
4102        self.context.unop(self.masm, |masm, reg| {
4103            masm.v128_abs(reg, writable!(reg), V128AbsKind::F32x4)?;
4104            Ok(TypedReg::new(WasmValType::V128, reg))
4105        })
4106    }
4107
4108    fn visit_f64x2_abs(&mut self) -> Self::Output {
4109        self.context.unop(self.masm, |masm, reg| {
4110            masm.v128_abs(reg, writable!(reg), V128AbsKind::F64x2)?;
4111            Ok(TypedReg::new(WasmValType::V128, reg))
4112        })
4113    }
4114
4115    fn visit_i8x16_neg(&mut self) -> Self::Output {
4116        self.context.unop(self.masm, |masm, op| {
4117            masm.v128_neg(writable!(op), V128NegKind::I8x16)?;
4118            Ok(TypedReg::new(WasmValType::V128, op))
4119        })
4120    }
4121
4122    fn visit_i16x8_neg(&mut self) -> Self::Output {
4123        self.context.unop(self.masm, |masm, op| {
4124            masm.v128_neg(writable!(op), V128NegKind::I16x8)?;
4125            Ok(TypedReg::new(WasmValType::V128, op))
4126        })
4127    }
4128
4129    fn visit_i32x4_neg(&mut self) -> Self::Output {
4130        self.context.unop(self.masm, |masm, op| {
4131            masm.v128_neg(writable!(op), V128NegKind::I32x4)?;
4132            Ok(TypedReg::new(WasmValType::V128, op))
4133        })
4134    }
4135
4136    fn visit_i64x2_neg(&mut self) -> Self::Output {
4137        self.context.unop(self.masm, |masm, op| {
4138            masm.v128_neg(writable!(op), V128NegKind::I64x2)?;
4139            Ok(TypedReg::new(WasmValType::V128, op))
4140        })
4141    }
4142
4143    fn visit_i8x16_shl(&mut self) -> Self::Output {
4144        self.masm
4145            .v128_shift(&mut self.context, OperandSize::S8, ShiftKind::Shl)
4146    }
4147
4148    fn visit_i16x8_shl(&mut self) -> Self::Output {
4149        self.masm
4150            .v128_shift(&mut self.context, OperandSize::S16, ShiftKind::Shl)
4151    }
4152
4153    fn visit_i32x4_shl(&mut self) -> Self::Output {
4154        self.masm
4155            .v128_shift(&mut self.context, OperandSize::S32, ShiftKind::Shl)
4156    }
4157
4158    fn visit_i64x2_shl(&mut self) -> Self::Output {
4159        self.masm
4160            .v128_shift(&mut self.context, OperandSize::S64, ShiftKind::Shl)
4161    }
4162
4163    fn visit_i8x16_shr_u(&mut self) -> Self::Output {
4164        self.masm
4165            .v128_shift(&mut self.context, OperandSize::S8, ShiftKind::ShrU)
4166    }
4167
4168    fn visit_i16x8_shr_u(&mut self) -> Self::Output {
4169        self.masm
4170            .v128_shift(&mut self.context, OperandSize::S16, ShiftKind::ShrU)
4171    }
4172
4173    fn visit_i32x4_shr_u(&mut self) -> Self::Output {
4174        self.masm
4175            .v128_shift(&mut self.context, OperandSize::S32, ShiftKind::ShrU)
4176    }
4177
4178    fn visit_i64x2_shr_u(&mut self) -> Self::Output {
4179        self.masm
4180            .v128_shift(&mut self.context, OperandSize::S64, ShiftKind::ShrU)
4181    }
4182
4183    fn visit_i8x16_shr_s(&mut self) -> Self::Output {
4184        self.masm
4185            .v128_shift(&mut self.context, OperandSize::S8, ShiftKind::ShrS)
4186    }
4187
4188    fn visit_i16x8_shr_s(&mut self) -> Self::Output {
4189        self.masm
4190            .v128_shift(&mut self.context, OperandSize::S16, ShiftKind::ShrS)
4191    }
4192
4193    fn visit_i32x4_shr_s(&mut self) -> Self::Output {
4194        self.masm
4195            .v128_shift(&mut self.context, OperandSize::S32, ShiftKind::ShrS)
4196    }
4197
4198    fn visit_i64x2_shr_s(&mut self) -> Self::Output {
4199        self.masm
4200            .v128_shift(&mut self.context, OperandSize::S64, ShiftKind::ShrS)
4201    }
4202
4203    fn visit_i16x8_q15mulr_sat_s(&mut self) -> Self::Output {
4204        self.context
4205            .binop(self.masm, OperandSize::S16, |masm, dst, src, size| {
4206                masm.v128_q15mulr_sat_s(dst, src, writable!(dst), size)?;
4207                Ok(TypedReg::v128(dst))
4208            })
4209    }
4210
4211    fn visit_i8x16_min_s(&mut self) -> Self::Output {
4212        self.context
4213            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4214                masm.v128_min(src, dst, writable!(dst), V128MinKind::I8x16S)?;
4215                Ok(TypedReg::v128(dst))
4216            })
4217    }
4218
4219    fn visit_i8x16_all_true(&mut self) -> Self::Output {
4220        self.context.v128_all_true_op(self.masm, |masm, src, dst| {
4221            masm.v128_all_true(src, writable!(dst), OperandSize::S8)
4222        })
4223    }
4224
4225    fn visit_i16x8_all_true(&mut self) -> Self::Output {
4226        self.context.v128_all_true_op(self.masm, |masm, src, dst| {
4227            masm.v128_all_true(src, writable!(dst), OperandSize::S16)
4228        })
4229    }
4230
4231    fn visit_i32x4_all_true(&mut self) -> Self::Output {
4232        self.context.v128_all_true_op(self.masm, |masm, src, dst| {
4233            masm.v128_all_true(src, writable!(dst), OperandSize::S32)
4234        })
4235    }
4236
4237    fn visit_i64x2_all_true(&mut self) -> Self::Output {
4238        self.context.v128_all_true_op(self.masm, |masm, src, dst| {
4239            masm.v128_all_true(src, writable!(dst), OperandSize::S64)
4240        })
4241    }
4242
4243    fn visit_i8x16_bitmask(&mut self) -> Self::Output {
4244        self.context.v128_bitmask_op(self.masm, |masm, src, dst| {
4245            masm.v128_bitmask(src, writable!(dst), OperandSize::S8)
4246        })
4247    }
4248
4249    fn visit_i16x8_bitmask(&mut self) -> Self::Output {
4250        self.context.v128_bitmask_op(self.masm, |masm, src, dst| {
4251            masm.v128_bitmask(src, writable!(dst), OperandSize::S16)
4252        })
4253    }
4254
4255    fn visit_i32x4_bitmask(&mut self) -> Self::Output {
4256        self.context.v128_bitmask_op(self.masm, |masm, src, dst| {
4257            masm.v128_bitmask(src, writable!(dst), OperandSize::S32)
4258        })
4259    }
4260
4261    fn visit_i64x2_bitmask(&mut self) -> Self::Output {
4262        self.context.v128_bitmask_op(self.masm, |masm, src, dst| {
4263            masm.v128_bitmask(src, writable!(dst), OperandSize::S64)
4264        })
4265    }
4266
4267    fn visit_i32x4_trunc_sat_f32x4_s(&mut self) -> Self::Output {
4268        self.masm
4269            .v128_trunc(&mut self.context, V128TruncKind::I32x4FromF32x4S)
4270    }
4271
4272    fn visit_i32x4_trunc_sat_f32x4_u(&mut self) -> Self::Output {
4273        self.masm
4274            .v128_trunc(&mut self.context, V128TruncKind::I32x4FromF32x4U)
4275    }
4276
4277    fn visit_i32x4_trunc_sat_f64x2_s_zero(&mut self) -> Self::Output {
4278        self.masm
4279            .v128_trunc(&mut self.context, V128TruncKind::I32x4FromF64x2SZero)
4280    }
4281
4282    fn visit_i32x4_trunc_sat_f64x2_u_zero(&mut self) -> Self::Output {
4283        self.masm
4284            .v128_trunc(&mut self.context, V128TruncKind::I32x4FromF64x2UZero)
4285    }
4286
4287    fn visit_i16x8_min_s(&mut self) -> Self::Output {
4288        self.context
4289            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4290                masm.v128_min(src, dst, writable!(dst), V128MinKind::I16x8S)?;
4291                Ok(TypedReg::v128(dst))
4292            })
4293    }
4294
4295    fn visit_i32x4_dot_i16x8_s(&mut self) -> Self::Output {
4296        self.context
4297            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4298                masm.v128_dot(dst, src, writable!(dst))?;
4299                Ok(TypedReg::v128(dst))
4300            })
4301    }
4302
4303    fn visit_i8x16_popcnt(&mut self) -> Self::Output {
4304        self.masm.v128_popcnt(&mut self.context)
4305    }
4306
4307    fn visit_i8x16_avgr_u(&mut self) -> Self::Output {
4308        self.context
4309            .binop(self.masm, OperandSize::S8, |masm, dst, src, size| {
4310                masm.v128_avgr(dst, src, writable!(dst), size)?;
4311                Ok(TypedReg::v128(dst))
4312            })
4313    }
4314
4315    fn visit_i32x4_min_s(&mut self) -> Self::Output {
4316        self.context
4317            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4318                masm.v128_min(src, dst, writable!(dst), V128MinKind::I32x4S)?;
4319                Ok(TypedReg::v128(dst))
4320            })
4321    }
4322
4323    fn visit_i8x16_min_u(&mut self) -> Self::Output {
4324        self.context
4325            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4326                masm.v128_min(src, dst, writable!(dst), V128MinKind::I8x16U)?;
4327                Ok(TypedReg::v128(dst))
4328            })
4329    }
4330
4331    fn visit_i16x8_avgr_u(&mut self) -> Self::Output {
4332        self.context
4333            .binop(self.masm, OperandSize::S16, |masm, dst, src, size| {
4334                masm.v128_avgr(dst, src, writable!(dst), size)?;
4335                Ok(TypedReg::v128(dst))
4336            })
4337    }
4338
4339    fn visit_i16x8_min_u(&mut self) -> Self::Output {
4340        self.context
4341            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4342                masm.v128_min(src, dst, writable!(dst), V128MinKind::I16x8U)?;
4343                Ok(TypedReg::v128(dst))
4344            })
4345    }
4346
4347    fn visit_i32x4_min_u(&mut self) -> Self::Output {
4348        self.context
4349            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4350                masm.v128_min(src, dst, writable!(dst), V128MinKind::I32x4U)?;
4351                Ok(TypedReg::v128(dst))
4352            })
4353    }
4354
4355    fn visit_i8x16_max_s(&mut self) -> Self::Output {
4356        self.context
4357            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4358                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I8x16S)?;
4359                Ok(TypedReg::v128(dst))
4360            })
4361    }
4362
4363    fn visit_i16x8_max_s(&mut self) -> Self::Output {
4364        self.context
4365            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4366                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I16x8S)?;
4367                Ok(TypedReg::v128(dst))
4368            })
4369    }
4370
4371    fn visit_i32x4_max_s(&mut self) -> Self::Output {
4372        self.context
4373            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4374                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I32x4S)?;
4375                Ok(TypedReg::v128(dst))
4376            })
4377    }
4378
4379    fn visit_i8x16_max_u(&mut self) -> Self::Output {
4380        self.context
4381            .binop(self.masm, OperandSize::S8, |masm, dst, src, _size| {
4382                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I8x16U)?;
4383                Ok(TypedReg::v128(dst))
4384            })
4385    }
4386
4387    fn visit_i16x8_max_u(&mut self) -> Self::Output {
4388        self.context
4389            .binop(self.masm, OperandSize::S16, |masm, dst, src, _size| {
4390                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I16x8U)?;
4391                Ok(TypedReg::v128(dst))
4392            })
4393    }
4394
4395    fn visit_i32x4_max_u(&mut self) -> Self::Output {
4396        self.context
4397            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4398                masm.v128_max(src, dst, writable!(dst), V128MaxKind::I32x4U)?;
4399                Ok(TypedReg::v128(dst))
4400            })
4401    }
4402
4403    fn visit_i16x8_extmul_low_i8x16_s(&mut self) -> Self::Output {
4404        self.masm
4405            .v128_extmul(&mut self.context, V128ExtMulKind::LowI8x16S)
4406    }
4407
4408    fn visit_i32x4_extmul_low_i16x8_s(&mut self) -> Self::Output {
4409        self.masm
4410            .v128_extmul(&mut self.context, V128ExtMulKind::LowI16x8S)
4411    }
4412
4413    fn visit_i64x2_extmul_low_i32x4_s(&mut self) -> Self::Output {
4414        self.masm
4415            .v128_extmul(&mut self.context, V128ExtMulKind::LowI32x4S)
4416    }
4417
4418    fn visit_i16x8_extmul_low_i8x16_u(&mut self) -> Self::Output {
4419        self.masm
4420            .v128_extmul(&mut self.context, V128ExtMulKind::LowI8x16U)
4421    }
4422
4423    fn visit_i32x4_extmul_low_i16x8_u(&mut self) -> Self::Output {
4424        self.masm
4425            .v128_extmul(&mut self.context, V128ExtMulKind::LowI16x8U)
4426    }
4427
4428    fn visit_i64x2_extmul_low_i32x4_u(&mut self) -> Self::Output {
4429        self.masm
4430            .v128_extmul(&mut self.context, V128ExtMulKind::LowI32x4U)
4431    }
4432
4433    fn visit_i16x8_extmul_high_i8x16_u(&mut self) -> Self::Output {
4434        self.masm
4435            .v128_extmul(&mut self.context, V128ExtMulKind::HighI8x16U)
4436    }
4437
4438    fn visit_i32x4_extmul_high_i16x8_u(&mut self) -> Self::Output {
4439        self.masm
4440            .v128_extmul(&mut self.context, V128ExtMulKind::HighI16x8U)
4441    }
4442
4443    fn visit_i64x2_extmul_high_i32x4_u(&mut self) -> Self::Output {
4444        self.masm
4445            .v128_extmul(&mut self.context, V128ExtMulKind::HighI32x4U)
4446    }
4447
4448    fn visit_i16x8_extmul_high_i8x16_s(&mut self) -> Self::Output {
4449        self.masm
4450            .v128_extmul(&mut self.context, V128ExtMulKind::HighI8x16S)
4451    }
4452
4453    fn visit_i32x4_extmul_high_i16x8_s(&mut self) -> Self::Output {
4454        self.masm
4455            .v128_extmul(&mut self.context, V128ExtMulKind::HighI16x8S)
4456    }
4457
4458    fn visit_i64x2_extmul_high_i32x4_s(&mut self) -> Self::Output {
4459        self.masm
4460            .v128_extmul(&mut self.context, V128ExtMulKind::HighI32x4S)
4461    }
4462
4463    fn visit_i16x8_extadd_pairwise_i8x16_s(&mut self) -> Self::Output {
4464        self.context.unop(self.masm, |masm, op| {
4465            masm.v128_extadd_pairwise(op, writable!(op), V128ExtAddKind::I8x16S)?;
4466            Ok(TypedReg::v128(op))
4467        })
4468    }
4469
4470    fn visit_i16x8_extadd_pairwise_i8x16_u(&mut self) -> Self::Output {
4471        self.context.unop(self.masm, |masm, op| {
4472            masm.v128_extadd_pairwise(op, writable!(op), V128ExtAddKind::I8x16U)?;
4473            Ok(TypedReg::v128(op))
4474        })
4475    }
4476
4477    fn visit_i32x4_extadd_pairwise_i16x8_s(&mut self) -> Self::Output {
4478        self.context.unop(self.masm, |masm, op| {
4479            masm.v128_extadd_pairwise(op, writable!(op), V128ExtAddKind::I16x8S)?;
4480            Ok(TypedReg::v128(op))
4481        })
4482    }
4483
4484    fn visit_i32x4_extadd_pairwise_i16x8_u(&mut self) -> Self::Output {
4485        self.context.unop(self.masm, |masm, op| {
4486            masm.v128_extadd_pairwise(op, writable!(op), V128ExtAddKind::I16x8U)?;
4487            Ok(TypedReg::v128(op))
4488        })
4489    }
4490
4491    fn visit_f32x4_add(&mut self) -> Self::Output {
4492        self.context
4493            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4494                masm.v128_add(dst, src, writable!(dst), V128AddKind::F32x4)?;
4495                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S32)?;
4496                Ok(TypedReg::v128(dst))
4497            })
4498    }
4499
4500    fn visit_f64x2_add(&mut self) -> Self::Output {
4501        self.context
4502            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
4503                masm.v128_add(dst, src, writable!(dst), V128AddKind::F64x2)?;
4504                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S64)?;
4505                Ok(TypedReg::v128(dst))
4506            })
4507    }
4508
4509    fn visit_f32x4_sub(&mut self) -> Self::Output {
4510        self.context
4511            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4512                masm.v128_sub(dst, src, writable!(dst), V128SubKind::F32x4)?;
4513                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S32)?;
4514                Ok(TypedReg::v128(dst))
4515            })
4516    }
4517
4518    fn visit_f64x2_sub(&mut self) -> Self::Output {
4519        self.context
4520            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
4521                masm.v128_sub(dst, src, writable!(dst), V128SubKind::F64x2)?;
4522                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S64)?;
4523                Ok(TypedReg::v128(dst))
4524            })
4525    }
4526
4527    fn visit_f32x4_mul(&mut self) -> Self::Output {
4528        self.masm.v128_mul(&mut self.context, V128MulKind::F32x4)?;
4529        let result = self.context.pop_to_reg(self.masm, None)?;
4530        self.masm
4531            .maybe_canonicalize_v128_nan(writable!(result.into()), OperandSize::S32)?;
4532        self.context.stack.push(result.into());
4533        Ok(())
4534    }
4535
4536    fn visit_f64x2_mul(&mut self) -> Self::Output {
4537        self.masm.v128_mul(&mut self.context, V128MulKind::F64x2)?;
4538        let result = self.context.pop_to_reg(self.masm, None)?;
4539        self.masm
4540            .maybe_canonicalize_v128_nan(writable!(result.into()), OperandSize::S64)?;
4541        self.context.stack.push(result.into());
4542        Ok(())
4543    }
4544
4545    fn visit_f32x4_div(&mut self) -> Self::Output {
4546        self.context
4547            .binop(self.masm, OperandSize::S32, |masm, dst, src, size| {
4548                masm.v128_div(dst, src, writable!(dst), size)?;
4549                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S32)?;
4550                Ok(TypedReg::v128(dst))
4551            })
4552    }
4553
4554    fn visit_f64x2_div(&mut self) -> Self::Output {
4555        self.context
4556            .binop(self.masm, OperandSize::S64, |masm, dst, src, size| {
4557                masm.v128_div(dst, src, writable!(dst), size)?;
4558                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S64)?;
4559                Ok(TypedReg::v128(dst))
4560            })
4561    }
4562
4563    fn visit_f32x4_neg(&mut self) -> Self::Output {
4564        self.context.unop(self.masm, |masm, reg| {
4565            masm.v128_neg(writable!(reg), V128NegKind::F32x4)?;
4566            Ok(TypedReg::v128(reg))
4567        })
4568    }
4569
4570    fn visit_f32x4_ceil(&mut self) -> Self::Output {
4571        self.context.unop(self.masm, |masm, reg| {
4572            masm.v128_ceil(reg, writable!(reg), OperandSize::S32)?;
4573            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S32)?;
4574            Ok(TypedReg::v128(reg))
4575        })
4576    }
4577
4578    fn visit_f64x2_neg(&mut self) -> Self::Output {
4579        self.context.unop(self.masm, |masm, reg| {
4580            masm.v128_neg(writable!(reg), V128NegKind::F64x2)?;
4581            Ok(TypedReg::v128(reg))
4582        })
4583    }
4584
4585    fn visit_f64x2_ceil(&mut self) -> Self::Output {
4586        self.context.unop(self.masm, |masm, reg| {
4587            masm.v128_ceil(reg, writable!(reg), OperandSize::S64)?;
4588            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S64)?;
4589            Ok(TypedReg::v128(reg))
4590        })
4591    }
4592
4593    fn visit_f32x4_sqrt(&mut self) -> Self::Output {
4594        self.context.unop(self.masm, |masm, reg| {
4595            masm.v128_sqrt(reg, writable!(reg), OperandSize::S32)?;
4596            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S32)?;
4597            Ok(TypedReg::v128(reg))
4598        })
4599    }
4600
4601    fn visit_f32x4_floor(&mut self) -> Self::Output {
4602        self.context.unop(self.masm, |masm, reg| {
4603            masm.v128_floor(reg, writable!(reg), OperandSize::S32)?;
4604            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S32)?;
4605            Ok(TypedReg::v128(reg))
4606        })
4607    }
4608
4609    fn visit_f64x2_sqrt(&mut self) -> Self::Output {
4610        self.context.unop(self.masm, |masm, reg| {
4611            masm.v128_sqrt(reg, writable!(reg), OperandSize::S64)?;
4612            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S64)?;
4613            Ok(TypedReg::v128(reg))
4614        })
4615    }
4616
4617    fn visit_f64x2_floor(&mut self) -> Self::Output {
4618        self.context.unop(self.masm, |masm, reg| {
4619            masm.v128_floor(reg, writable!(reg), OperandSize::S64)?;
4620            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S64)?;
4621            Ok(TypedReg::v128(reg))
4622        })
4623    }
4624
4625    fn visit_f32x4_nearest(&mut self) -> Self::Output {
4626        self.context.unop(self.masm, |masm, reg| {
4627            masm.v128_nearest(reg, writable!(reg), OperandSize::S32)?;
4628            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S32)?;
4629            Ok(TypedReg::v128(reg))
4630        })
4631    }
4632
4633    fn visit_f64x2_nearest(&mut self) -> Self::Output {
4634        self.context.unop(self.masm, |masm, reg| {
4635            masm.v128_nearest(reg, writable!(reg), OperandSize::S64)?;
4636            masm.maybe_canonicalize_v128_nan(writable!(reg), OperandSize::S64)?;
4637            Ok(TypedReg::v128(reg))
4638        })
4639    }
4640
4641    fn visit_f32x4_trunc(&mut self) -> Self::Output {
4642        self.masm
4643            .v128_trunc(&mut self.context, V128TruncKind::F32x4)?;
4644        let result = self.context.pop_to_reg(self.masm, None)?;
4645        self.masm
4646            .maybe_canonicalize_v128_nan(writable!(result.into()), OperandSize::S32)?;
4647        self.context.stack.push(result.into());
4648        Ok(())
4649    }
4650
4651    fn visit_f64x2_trunc(&mut self) -> Self::Output {
4652        self.masm
4653            .v128_trunc(&mut self.context, V128TruncKind::F64x2)?;
4654        let result = self.context.pop_to_reg(self.masm, None)?;
4655        self.masm
4656            .maybe_canonicalize_v128_nan(writable!(result.into()), OperandSize::S64)?;
4657        self.context.stack.push(result.into());
4658        Ok(())
4659    }
4660
4661    fn visit_v128_load32_zero(&mut self, memarg: MemArg) -> Self::Output {
4662        self.emit_wasm_load(
4663            &memarg,
4664            WasmValType::V128,
4665            LoadKind::VectorZero(OperandSize::S32),
4666        )
4667    }
4668
4669    fn visit_v128_load64_zero(&mut self, memarg: MemArg) -> Self::Output {
4670        self.emit_wasm_load(
4671            &memarg,
4672            WasmValType::V128,
4673            LoadKind::VectorZero(OperandSize::S64),
4674        )
4675    }
4676
4677    fn visit_f32x4_pmin(&mut self) -> Self::Output {
4678        self.context
4679            .binop(self.masm, OperandSize::S32, |masm, dst, src, size| {
4680                masm.v128_pmin(dst, src, writable!(dst), size)?;
4681                Ok(TypedReg::v128(dst))
4682            })
4683    }
4684
4685    fn visit_f64x2_pmin(&mut self) -> Self::Output {
4686        self.context
4687            .binop(self.masm, OperandSize::S64, |masm, dst, src, size| {
4688                masm.v128_pmin(dst, src, writable!(dst), size)?;
4689                Ok(TypedReg::v128(dst))
4690            })
4691    }
4692
4693    fn visit_f32x4_pmax(&mut self) -> Self::Output {
4694        self.context
4695            .binop(self.masm, OperandSize::S32, |masm, dst, src, size| {
4696                masm.v128_pmax(dst, src, writable!(dst), size)?;
4697                Ok(TypedReg::v128(dst))
4698            })
4699    }
4700
4701    fn visit_f64x2_pmax(&mut self) -> Self::Output {
4702        self.context
4703            .binop(self.masm, OperandSize::S64, |masm, dst, src, size| {
4704                masm.v128_pmax(dst, src, writable!(dst), size)?;
4705                Ok(TypedReg::v128(dst))
4706            })
4707    }
4708
4709    fn visit_f32x4_min(&mut self) -> Self::Output {
4710        self.context
4711            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4712                masm.v128_min(dst, src, writable!(dst), V128MinKind::F32x4)?;
4713                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S32)?;
4714                Ok(TypedReg::v128(dst))
4715            })
4716    }
4717
4718    fn visit_f64x2_min(&mut self) -> Self::Output {
4719        self.context
4720            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
4721                masm.v128_min(dst, src, writable!(dst), V128MinKind::F64x2)?;
4722                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S64)?;
4723                Ok(TypedReg::v128(dst))
4724            })
4725    }
4726
4727    fn visit_f32x4_max(&mut self) -> Self::Output {
4728        self.context
4729            .binop(self.masm, OperandSize::S32, |masm, dst, src, _size| {
4730                masm.v128_max(dst, src, writable!(dst), V128MaxKind::F32x4)?;
4731                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S32)?;
4732                Ok(TypedReg::v128(dst))
4733            })
4734    }
4735
4736    fn visit_f64x2_max(&mut self) -> Self::Output {
4737        self.context
4738            .binop(self.masm, OperandSize::S64, |masm, dst, src, _size| {
4739                masm.v128_max(dst, src, writable!(dst), V128MaxKind::F64x2)?;
4740                masm.maybe_canonicalize_v128_nan(writable!(dst), OperandSize::S64)?;
4741                Ok(TypedReg::v128(dst))
4742            })
4743    }
4744
4745    wasmparser::for_each_visit_simd_operator!(def_unsupported);
4746}
4747
4748impl<'a, 'translation, 'data, M> CodeGen<'a, 'translation, 'data, M, Emission>
4749where
4750    M: MacroAssembler,
4751{
4752    fn cmp_i32s(&mut self, kind: IntCmpKind) -> Result<()> {
4753        self.context.i32_binop(self.masm, |masm, dst, src, size| {
4754            masm.cmp_with_set(writable!(dst), src, kind, size)?;
4755            Ok(TypedReg::i32(dst))
4756        })
4757    }
4758
4759    fn cmp_i64s(&mut self, kind: IntCmpKind) -> Result<()> {
4760        self.context
4761            .i64_binop(self.masm, move |masm, dst, src, size| {
4762                masm.cmp_with_set(writable!(dst), src, kind, size)?;
4763                Ok(TypedReg::i32(dst)) // Return value for comparisons is an `i32`.
4764            })
4765    }
4766}
4767
4768impl TryFrom<WasmValType> for OperandSize {
4769    type Error = crate::Error;
4770    fn try_from(ty: WasmValType) -> Result<OperandSize> {
4771        let ty = match ty {
4772            WasmValType::I32 | WasmValType::F32 => OperandSize::S32,
4773            WasmValType::I64 | WasmValType::F64 => OperandSize::S64,
4774            WasmValType::V128 => OperandSize::S128,
4775            WasmValType::Ref(rt) => {
4776                match rt.heap_type {
4777                    // TODO: Hardcoded size, assuming 64-bit support only. Once
4778                    // Wasmtime supports 32-bit architectures, this will need
4779                    // to be updated in such a way that the calculation of the
4780                    // OperandSize will depend on the target's  pointer size.
4781                    WasmHeapType::Func => OperandSize::S64,
4782                    WasmHeapType::Extern
4783                    | WasmHeapType::Exn
4784                    | WasmHeapType::ConcreteExn(_)
4785                    | WasmHeapType::NoExn => OperandSize::S32,
4786                    _ => bail!(CodeGenError::unsupported_wasm_type()),
4787                }
4788            }
4789        };
4790        Ok(ty)
4791    }
4792}