1
//! Plain instruction-encoding methods on [`FunctionEmitter`](super::FunctionEmitter)
2
//! that neither open/close a structured-control frame nor change
3
//! reachability, split out of `emit/mod.rs` to keep it under the 500-line
4
//! guideline (plan 0011 Phase 0's `emit/` split). Everything here just
5
//! appends encoded bytes; the operand-stack / control-frame simulator in
6
//! [`super::frame`] is untouched by these calls.
7

            
8
use wasm_encoder::{Encode, HeapType, Instruction, MemArg, MemArgTarget};
9

            
10
use super::FunctionEmitter;
11

            
12
119253970
const fn mem_arg(offset: u64, align: u32) -> MemArg {
13
119253970
    MemArg {
14
119253970
        offset,
15
119253970
        align,
16
119253970
        target: MemArgTarget::Memory(0),
17
119253970
        ordering: None,
18
119253970
    }
19
119253970
}
20

            
21
impl FunctionEmitter {
22
52825
    pub fn memory_init(&mut self, data_idx: u32, dst: u32, len: u32) {
23
52825
        Instruction::I32Const(dst as i32).encode(&mut self.bytes);
24
52825
        Instruction::I32Const(0).encode(&mut self.bytes);
25
52825
        Instruction::I32Const(len as i32).encode(&mut self.bytes);
26
52825
        Instruction::MemoryInit {
27
52825
            mem: 0,
28
52825
            data_index: data_idx,
29
52825
        }
30
52825
        .encode(&mut self.bytes);
31
52825
    }
32

            
33
120492634
    pub fn local_get(&mut self, idx: u32) {
34
120492634
        Instruction::LocalGet(idx).encode(&mut self.bytes);
35
120492634
    }
36

            
37
    /// Pushes the value of a module global: the `$t` singleton (ADR-0033) or
38
    /// a runtime global's home.
39
7839838
    pub fn global_get(&mut self, idx: u32) {
40
7839838
        Instruction::GlobalGet(idx).encode(&mut self.bytes);
41
7839838
    }
42

            
43
8041526
    pub fn global_set(&mut self, idx: u32) {
44
8041526
        Instruction::GlobalSet(idx).encode(&mut self.bytes);
45
8041526
    }
46

            
47
26059839
    pub fn local_set(&mut self, idx: u32) {
48
26059839
        Instruction::LocalSet(idx).encode(&mut self.bytes);
49
26059839
    }
50

            
51
895590
    pub fn local_tee(&mut self, idx: u32) {
52
895590
        Instruction::LocalTee(idx).encode(&mut self.bytes);
53
895590
    }
54

            
55
84695604
    pub fn i32_const(&mut self, value: i32) {
56
84695604
        Instruction::I32Const(value).encode(&mut self.bytes);
57
84695604
    }
58

            
59
301409
    pub fn i64_const(&mut self, value: i64) {
60
301409
        Instruction::I64Const(value).encode(&mut self.bytes);
61
301409
    }
62

            
63
61083151
    pub fn i32_add(&mut self) {
64
61083151
        Instruction::I32Add.encode(&mut self.bytes);
65
61083151
    }
66

            
67
6693769
    pub fn i32_sub(&mut self) {
68
6693769
        Instruction::I32Sub.encode(&mut self.bytes);
69
6693769
    }
70

            
71
3042072
    pub fn i32_eqz(&mut self) {
72
3042072
        Instruction::I32Eqz.encode(&mut self.bytes);
73
3042072
    }
74

            
75
298285
    pub fn i32_eq(&mut self) {
76
298285
        Instruction::I32Eq.encode(&mut self.bytes);
77
298285
    }
78

            
79
16435
    pub fn i32_lt_s(&mut self) {
80
16435
        Instruction::I32LtS.encode(&mut self.bytes);
81
16435
    }
82

            
83
367519
    pub fn i32_ge_s(&mut self) {
84
367519
        Instruction::I32GeS.encode(&mut self.bytes);
85
367519
    }
86

            
87
2222907
    pub fn i32_gt_s(&mut self) {
88
2222907
        Instruction::I32GtS.encode(&mut self.bytes);
89
2222907
    }
90

            
91
1666
    pub fn i32_le_s(&mut self) {
92
1666
        Instruction::I32LeS.encode(&mut self.bytes);
93
1666
    }
94

            
95
339075
    pub fn i32_ne(&mut self) {
96
339075
        Instruction::I32Ne.encode(&mut self.bytes);
97
339075
    }
98

            
99
21233
    pub fn i32_shl(&mut self) {
100
21233
        Instruction::I32Shl.encode(&mut self.bytes);
101
21233
    }
102

            
103
21236
    pub fn i32_or(&mut self) {
104
21236
        Instruction::I32Or.encode(&mut self.bytes);
105
21236
    }
106

            
107
10839
    pub fn i32_and(&mut self) {
108
10839
        Instruction::I32And.encode(&mut self.bytes);
109
10839
    }
110

            
111
    pub fn i64_ne(&mut self) {
112
        Instruction::I64Ne.encode(&mut self.bytes);
113
    }
114

            
115
42
    pub fn i64_eqz(&mut self) {
116
42
        Instruction::I64Eqz.encode(&mut self.bytes);
117
42
    }
118

            
119
42
    pub fn i64_lt_s(&mut self) {
120
42
        Instruction::I64LtS.encode(&mut self.bytes);
121
42
    }
122

            
123
45
    pub fn i64_gt_s(&mut self) {
124
45
        Instruction::I64GtS.encode(&mut self.bytes);
125
45
    }
126

            
127
150061
    pub fn i32_mul(&mut self) {
128
150061
        Instruction::I32Mul.encode(&mut self.bytes);
129
150061
    }
130

            
131
44285662
    pub fn i32_load(&mut self, offset: u64) {
132
44285662
        Instruction::I32Load(mem_arg(offset, 2)).encode(&mut self.bytes);
133
44285662
    }
134

            
135
86600
    pub fn i32_load8_u(&mut self, offset: u64) {
136
86600
        Instruction::I32Load8U(mem_arg(offset, 0)).encode(&mut self.bytes);
137
86600
    }
138

            
139
34748
    pub fn i64_load(&mut self, offset: u64) {
140
34748
        Instruction::I64Load(mem_arg(offset, 3)).encode(&mut self.bytes);
141
34748
    }
142

            
143
1844688
    pub fn i32_ge_u(&mut self) {
144
1844688
        Instruction::I32GeU.encode(&mut self.bytes);
145
1844688
    }
146

            
147
6257128
    pub fn i32_gt_u(&mut self) {
148
6257128
        Instruction::I32GtU.encode(&mut self.bytes);
149
6257128
    }
150

            
151
    /// `select`: the first operand when the i32 condition is nonzero, else
152
    /// the second.
153
6257128
    pub fn select(&mut self) {
154
6257128
        Instruction::Select.encode(&mut self.bytes);
155
6257128
    }
156

            
157
12983006
    pub fn struct_get(&mut self, type_idx: u32, field_idx: u32) {
158
12983006
        Instruction::StructGet {
159
12983006
            struct_type_index: type_idx,
160
12983006
            field_index: field_idx,
161
12983006
        }
162
12983006
        .encode(&mut self.bytes);
163
12983006
    }
164

            
165
5886
    pub fn struct_set(&mut self, type_idx: u32, field_idx: u32) {
166
5886
        Instruction::StructSet {
167
5886
            struct_type_index: type_idx,
168
5886
            field_index: field_idx,
169
5886
        }
170
5886
        .encode(&mut self.bytes);
171
5886
    }
172

            
173
39926545
    pub fn i32_store_raw(&mut self) {
174
39926545
        Instruction::I32Store(mem_arg(0, 2)).encode(&mut self.bytes);
175
39926545
    }
176

            
177
8073030
    pub fn i64_store_raw(&mut self) {
178
8073030
        Instruction::I64Store(mem_arg(0, 3)).encode(&mut self.bytes);
179
8073030
    }
180

            
181
413690
    pub fn i64_extend_i32_u(&mut self) {
182
413690
        Instruction::I64ExtendI32U.encode(&mut self.bytes);
183
413690
    }
184

            
185
861708
    pub fn i64_extend_i32_s(&mut self) {
186
861708
        Instruction::I64ExtendI32S.encode(&mut self.bytes);
187
861708
    }
188

            
189
    pub fn i32_wrap_i64(&mut self) {
190
        Instruction::I32WrapI64.encode(&mut self.bytes);
191
    }
192

            
193
2970407
    pub fn array_new_data(&mut self, type_idx: u32, data_idx: u32) {
194
2970407
        Instruction::ArrayNewData {
195
2970407
            array_type_index: type_idx,
196
2970407
            array_data_index: data_idx,
197
2970407
        }
198
2970407
        .encode(&mut self.bytes);
199
2970407
    }
200

            
201
1796390
    pub fn array_get_u(&mut self, type_idx: u32) {
202
1796390
        Instruction::ArrayGetU(type_idx).encode(&mut self.bytes);
203
1796390
    }
204

            
205
1989792
    pub fn array_new_default(&mut self, type_idx: u32) {
206
1989792
        Instruction::ArrayNewDefault(type_idx).encode(&mut self.bytes);
207
1989792
    }
208

            
209
657919
    pub fn array_get(&mut self, type_idx: u32) {
210
657919
        Instruction::ArrayGet(type_idx).encode(&mut self.bytes);
211
657919
    }
212

            
213
98324
    pub fn array_new_fixed(&mut self, type_idx: u32, size: u32) {
214
98324
        Instruction::ArrayNewFixed {
215
98324
            array_type_index: type_idx,
216
98324
            array_size: size,
217
98324
        }
218
98324
        .encode(&mut self.bytes);
219
98324
    }
220

            
221
2012740
    pub fn array_set(&mut self, type_idx: u32) {
222
2012740
        Instruction::ArraySet(type_idx).encode(&mut self.bytes);
223
2012740
    }
224

            
225
2516875
    pub fn array_len(&mut self) {
226
2516875
        Instruction::ArrayLen.encode(&mut self.bytes);
227
2516875
    }
228

            
229
31788536
    pub fn call(&mut self, func_idx: u32) {
230
31788536
        Instruction::Call(func_idx).encode(&mut self.bytes);
231
31788536
    }
232

            
233
    /// Indirect call through a typed funcref popped from the stack.
234
    /// `type_idx` is the wasm fn-type index; the engine validates the
235
    /// funcref's declared shape against it at compile time. Used by the
236
    /// Tier 1.5 closure call site after `struct.get $closure_<sig>
237
    /// $fn_ref` has produced the funcref.
238
91270
    pub fn call_ref(&mut self, type_idx: u32) {
239
91270
        Instruction::CallRef(type_idx).encode(&mut self.bytes);
240
91270
    }
241

            
242
24911187
    pub fn store_u8_dynamic(&mut self, local_base: u32, offset: u32, value: u8) {
243
24911187
        Instruction::LocalGet(local_base).encode(&mut self.bytes);
244
24911187
        Instruction::I32Const(offset as i32).encode(&mut self.bytes);
245
24911187
        Instruction::I32Add.encode(&mut self.bytes);
246
24911187
        Instruction::I32Const(i32::from(value)).encode(&mut self.bytes);
247
24911187
        self.i32_store8_raw();
248
24911187
    }
249

            
250
20478355
    pub fn store_i32_dynamic(&mut self, local_base: u32, offset: u32, value: i32) {
251
20478355
        Instruction::LocalGet(local_base).encode(&mut self.bytes);
252
20478355
        Instruction::I32Const(offset as i32).encode(&mut self.bytes);
253
20478355
        Instruction::I32Add.encode(&mut self.bytes);
254
20478355
        Instruction::I32Const(value).encode(&mut self.bytes);
255
20478355
        self.i32_store_raw();
256
20478355
    }
257

            
258
5429302
    pub fn store_i64_dynamic(&mut self, local_base: u32, offset: u32, value: i64) {
259
5429302
        Instruction::LocalGet(local_base).encode(&mut self.bytes);
260
5429302
        Instruction::I32Const(offset as i32).encode(&mut self.bytes);
261
5429302
        Instruction::I32Add.encode(&mut self.bytes);
262
5429302
        Instruction::I64Const(value).encode(&mut self.bytes);
263
5429302
        self.i64_store_raw();
264
5429302
    }
265

            
266
26801589
    pub fn i32_store8_raw(&mut self) {
267
26801589
        Instruction::I32Store8(mem_arg(0, 0)).encode(&mut self.bytes);
268
26801589
    }
269

            
270
45796
    pub fn i32_load16_u(&mut self, offset: u64) {
271
45796
        Instruction::I32Load16U(mem_arg(offset, 1)).encode(&mut self.bytes);
272
45796
    }
273

            
274
1026823
    pub fn ref_null(&mut self, type_idx: u32) {
275
1026823
        Instruction::RefNull(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
276
1026823
    }
277

            
278
    /// Emits `ref.null any` — the polymorphic null that satisfies every
279
    /// `(ref null any)` slot. Used as nomi-eval's "no result" return value
280
    /// for empty programs / definition-only forms.
281
712114
    pub fn ref_null_any(&mut self) {
282
712114
        Instruction::RefNull(HeapType::Abstract {
283
712114
            shared: false,
284
712114
            ty: wasm_encoder::AbstractHeapType::Any,
285
712114
        })
286
712114
        .encode(&mut self.bytes);
287
712114
    }
288

            
289
2355627
    pub fn ref_is_null(&mut self) {
290
2355627
        Instruction::RefIsNull.encode(&mut self.bytes);
291
2355627
    }
292

            
293
    /// Unpacks a `(ref i31)` back to its signed i32 value.
294
303843
    pub fn i31_get_s(&mut self) {
295
303843
        Instruction::I31GetS.encode(&mut self.bytes);
296
303843
    }
297

            
298
    /// Downcasts a `(ref null any)` (or any subtype) to a concrete struct
299
    /// type's non-null reference. Traps at runtime if the cast fails.
300
    /// Caller is responsible for ensuring the value's dynamic type
301
    /// matches; the compile-time `PairElement` carries that proof.
302
7966125
    pub fn ref_cast(&mut self, type_idx: u32) {
303
7966125
        Instruction::RefCastNonNull(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
304
7966125
    }
305

            
306
    /// Nullable counterpart to [`ref_cast`]. Use when the value's
307
    /// declared type is `(ref null …)` and a null pointer is a valid
308
    /// runtime outcome — e.g. host fns returning
309
    /// `Option<Rooted<…>>`, which the wasmtime ABI surfaces as a
310
    /// nullable reference. The non-null variant traps on null; this
311
    /// one preserves null and only fails on type mismatch.
312
885234
    pub fn ref_cast_nullable(&mut self, type_idx: u32) {
313
885234
        Instruction::RefCastNullable(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
314
885234
    }
315

            
316
    /// Downcast to the abstract `i31` heap type — paired with
317
    /// [`Self::i31_get_s`] to retrieve the boxed i32.
318
303843
    pub fn ref_cast_i31(&mut self) {
319
303843
        Instruction::RefCastNonNull(HeapType::I31).encode(&mut self.bytes);
320
303843
    }
321

            
322
    /// Non-trapping "is this an `i31`?" test — unlike [`Self::ref_cast_i31`],
323
    /// this returns `false` (rather than trapping) on a mismatch, and accepts
324
    /// null. Used to dynamically classify an `AnyRef` value (ADR-0033).
325
199447
    pub fn ref_test_i31(&mut self) {
326
199447
        Instruction::RefTestNullable(HeapType::I31).encode(&mut self.bytes);
327
199447
    }
328

            
329
    /// Non-trapping test that the operand is a non-null instance of `heap`.
330
48305
    pub fn ref_test_non_null(&mut self, heap: HeapType) {
331
48305
        Instruction::RefTestNonNull(heap).encode(&mut self.bytes);
332
48305
    }
333

            
334
    /// Non-trapping "is this a `(ref null type_idx)`?" test — true for null
335
    /// (member of every nullable type) or a genuine instance, false for
336
    /// anything else. Used to dispatch a dynamically-typed `AnyRef` list
337
    /// operand to `$pair` handling vs. a catchable `TYPE-ERROR` (ADR-0033).
338
3001655
    pub fn ref_test(&mut self, type_idx: u32) {
339
3001655
        Instruction::RefTestNullable(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
340
3001655
    }
341

            
342
    /// Downcasts an `anyref` to the abstract nullable `eq` heap type, the
343
    /// operand type `ref.eq` requires. Used to test identity against the `$t`
344
    /// singleton global (ADR-0033): every anyref the compiler or host
345
    /// produces is an i31, struct or array, all subtypes of `eq`.
346
200478
    pub fn ref_cast_eq(&mut self) {
347
200478
        Instruction::RefCastNullable(HeapType::Abstract {
348
200478
            shared: false,
349
200478
            ty: wasm_encoder::AbstractHeapType::Eq,
350
200478
        })
351
200478
        .encode(&mut self.bytes);
352
200478
    }
353

            
354
    /// Reference identity comparison — pops two `eqref` operands, pushes an
355
    /// i32 boolean. Used to recognise the `$t` singleton by identity rather
356
    /// than by (structurally non-unique) type test (ADR-0033).
357
205294
    pub fn ref_eq(&mut self) {
358
205294
        Instruction::RefEq.encode(&mut self.bytes);
359
205294
    }
360

            
361
527193
    pub fn drop_value(&mut self) {
362
527193
        Instruction::Drop.encode(&mut self.bytes);
363
527193
    }
364

            
365
    /// Pushes a typed funcref pointing at the wasm function with the
366
    /// given index. Tier 1.5 closure construction pairs this with
367
    /// [`Self::struct_new`] to populate a `$closure_<sig>` struct.
368
209940
    pub fn ref_func(&mut self, func_idx: u32) {
369
209940
        Instruction::RefFunc(func_idx).encode(&mut self.bytes);
370
209940
    }
371

            
372
    /// Constructs a struct of the registered struct type. Consumes one
373
    /// stack value per field in declaration order; pushes a
374
    /// `(ref $type_idx)` of the resulting struct.
375
3614370
    pub fn struct_new(&mut self, type_idx: u32) {
376
3614370
        Instruction::StructNew(type_idx).encode(&mut self.bytes);
377
3614370
    }
378
}