Lines
97.62 %
Functions
94.85 %
Branches
100 %
//! Plain instruction-encoding methods on [`FunctionEmitter`](super::FunctionEmitter)
//! that neither open/close a structured-control frame nor change
//! reachability, split out of `emit/mod.rs` to keep it under the 500-line
//! guideline (plan 0011 Phase 0's `emit/` split). Everything here just
//! appends encoded bytes; the operand-stack / control-frame simulator in
//! [`super::frame`] is untouched by these calls.
use wasm_encoder::{Encode, HeapType, Instruction, MemArg, MemArgTarget};
use super::FunctionEmitter;
const fn mem_arg(offset: u64, align: u32) -> MemArg {
MemArg {
offset,
align,
target: MemArgTarget::Memory(0),
ordering: None,
}
impl FunctionEmitter {
pub fn memory_init(&mut self, data_idx: u32, dst: u32, len: u32) {
Instruction::I32Const(dst as i32).encode(&mut self.bytes);
Instruction::I32Const(0).encode(&mut self.bytes);
Instruction::I32Const(len as i32).encode(&mut self.bytes);
Instruction::MemoryInit {
mem: 0,
data_index: data_idx,
.encode(&mut self.bytes);
pub fn local_get(&mut self, idx: u32) {
Instruction::LocalGet(idx).encode(&mut self.bytes);
/// Pushes the value of a module global: the `$t` singleton (ADR-0033) or
/// a runtime global's home.
pub fn global_get(&mut self, idx: u32) {
Instruction::GlobalGet(idx).encode(&mut self.bytes);
pub fn global_set(&mut self, idx: u32) {
Instruction::GlobalSet(idx).encode(&mut self.bytes);
pub fn local_set(&mut self, idx: u32) {
Instruction::LocalSet(idx).encode(&mut self.bytes);
pub fn local_tee(&mut self, idx: u32) {
Instruction::LocalTee(idx).encode(&mut self.bytes);
pub fn i32_const(&mut self, value: i32) {
Instruction::I32Const(value).encode(&mut self.bytes);
pub fn i64_const(&mut self, value: i64) {
Instruction::I64Const(value).encode(&mut self.bytes);
pub fn i32_add(&mut self) {
Instruction::I32Add.encode(&mut self.bytes);
pub fn i32_sub(&mut self) {
Instruction::I32Sub.encode(&mut self.bytes);
pub fn i32_eqz(&mut self) {
Instruction::I32Eqz.encode(&mut self.bytes);
pub fn i32_eq(&mut self) {
Instruction::I32Eq.encode(&mut self.bytes);
pub fn i32_lt_s(&mut self) {
Instruction::I32LtS.encode(&mut self.bytes);
pub fn i32_ge_s(&mut self) {
Instruction::I32GeS.encode(&mut self.bytes);
pub fn i32_gt_s(&mut self) {
Instruction::I32GtS.encode(&mut self.bytes);
pub fn i32_le_s(&mut self) {
Instruction::I32LeS.encode(&mut self.bytes);
pub fn i32_ne(&mut self) {
Instruction::I32Ne.encode(&mut self.bytes);
pub fn i32_shl(&mut self) {
Instruction::I32Shl.encode(&mut self.bytes);
pub fn i32_or(&mut self) {
Instruction::I32Or.encode(&mut self.bytes);
pub fn i32_and(&mut self) {
Instruction::I32And.encode(&mut self.bytes);
pub fn i64_ne(&mut self) {
Instruction::I64Ne.encode(&mut self.bytes);
pub fn i64_eqz(&mut self) {
Instruction::I64Eqz.encode(&mut self.bytes);
pub fn i64_lt_s(&mut self) {
Instruction::I64LtS.encode(&mut self.bytes);
pub fn i64_gt_s(&mut self) {
Instruction::I64GtS.encode(&mut self.bytes);
pub fn i32_mul(&mut self) {
Instruction::I32Mul.encode(&mut self.bytes);
pub fn i32_load(&mut self, offset: u64) {
Instruction::I32Load(mem_arg(offset, 2)).encode(&mut self.bytes);
pub fn i32_load8_u(&mut self, offset: u64) {
Instruction::I32Load8U(mem_arg(offset, 0)).encode(&mut self.bytes);
pub fn i64_load(&mut self, offset: u64) {
Instruction::I64Load(mem_arg(offset, 3)).encode(&mut self.bytes);
pub fn i32_ge_u(&mut self) {
Instruction::I32GeU.encode(&mut self.bytes);
pub fn i32_gt_u(&mut self) {
Instruction::I32GtU.encode(&mut self.bytes);
/// `select`: the first operand when the i32 condition is nonzero, else
/// the second.
pub fn select(&mut self) {
Instruction::Select.encode(&mut self.bytes);
pub fn struct_get(&mut self, type_idx: u32, field_idx: u32) {
Instruction::StructGet {
struct_type_index: type_idx,
field_index: field_idx,
pub fn struct_set(&mut self, type_idx: u32, field_idx: u32) {
Instruction::StructSet {
pub fn i32_store_raw(&mut self) {
Instruction::I32Store(mem_arg(0, 2)).encode(&mut self.bytes);
pub fn i64_store_raw(&mut self) {
Instruction::I64Store(mem_arg(0, 3)).encode(&mut self.bytes);
pub fn i64_extend_i32_u(&mut self) {
Instruction::I64ExtendI32U.encode(&mut self.bytes);
pub fn i64_extend_i32_s(&mut self) {
Instruction::I64ExtendI32S.encode(&mut self.bytes);
pub fn i32_wrap_i64(&mut self) {
Instruction::I32WrapI64.encode(&mut self.bytes);
pub fn array_new_data(&mut self, type_idx: u32, data_idx: u32) {
Instruction::ArrayNewData {
array_type_index: type_idx,
array_data_index: data_idx,
pub fn array_get_u(&mut self, type_idx: u32) {
Instruction::ArrayGetU(type_idx).encode(&mut self.bytes);
pub fn array_new_default(&mut self, type_idx: u32) {
Instruction::ArrayNewDefault(type_idx).encode(&mut self.bytes);
pub fn array_get(&mut self, type_idx: u32) {
Instruction::ArrayGet(type_idx).encode(&mut self.bytes);
pub fn array_new_fixed(&mut self, type_idx: u32, size: u32) {
Instruction::ArrayNewFixed {
array_size: size,
pub fn array_set(&mut self, type_idx: u32) {
Instruction::ArraySet(type_idx).encode(&mut self.bytes);
pub fn array_len(&mut self) {
Instruction::ArrayLen.encode(&mut self.bytes);
pub fn call(&mut self, func_idx: u32) {
Instruction::Call(func_idx).encode(&mut self.bytes);
/// Indirect call through a typed funcref popped from the stack.
/// `type_idx` is the wasm fn-type index; the engine validates the
/// funcref's declared shape against it at compile time. Used by the
/// Tier 1.5 closure call site after `struct.get $closure_<sig>
/// $fn_ref` has produced the funcref.
pub fn call_ref(&mut self, type_idx: u32) {
Instruction::CallRef(type_idx).encode(&mut self.bytes);
pub fn store_u8_dynamic(&mut self, local_base: u32, offset: u32, value: u8) {
Instruction::LocalGet(local_base).encode(&mut self.bytes);
Instruction::I32Const(offset as i32).encode(&mut self.bytes);
Instruction::I32Const(i32::from(value)).encode(&mut self.bytes);
self.i32_store8_raw();
pub fn store_i32_dynamic(&mut self, local_base: u32, offset: u32, value: i32) {
self.i32_store_raw();
pub fn store_i64_dynamic(&mut self, local_base: u32, offset: u32, value: i64) {
self.i64_store_raw();
pub fn i32_store8_raw(&mut self) {
Instruction::I32Store8(mem_arg(0, 0)).encode(&mut self.bytes);
pub fn i32_load16_u(&mut self, offset: u64) {
Instruction::I32Load16U(mem_arg(offset, 1)).encode(&mut self.bytes);
pub fn ref_null(&mut self, type_idx: u32) {
Instruction::RefNull(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
/// Emits `ref.null any` — the polymorphic null that satisfies every
/// `(ref null any)` slot. Used as nomi-eval's "no result" return value
/// for empty programs / definition-only forms.
pub fn ref_null_any(&mut self) {
Instruction::RefNull(HeapType::Abstract {
shared: false,
ty: wasm_encoder::AbstractHeapType::Any,
})
pub fn ref_is_null(&mut self) {
Instruction::RefIsNull.encode(&mut self.bytes);
/// Unpacks a `(ref i31)` back to its signed i32 value.
pub fn i31_get_s(&mut self) {
Instruction::I31GetS.encode(&mut self.bytes);
/// Downcasts a `(ref null any)` (or any subtype) to a concrete struct
/// type's non-null reference. Traps at runtime if the cast fails.
/// Caller is responsible for ensuring the value's dynamic type
/// matches; the compile-time `PairElement` carries that proof.
pub fn ref_cast(&mut self, type_idx: u32) {
Instruction::RefCastNonNull(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
/// Nullable counterpart to [`ref_cast`]. Use when the value's
/// declared type is `(ref null …)` and a null pointer is a valid
/// runtime outcome — e.g. host fns returning
/// `Option<Rooted<…>>`, which the wasmtime ABI surfaces as a
/// nullable reference. The non-null variant traps on null; this
/// one preserves null and only fails on type mismatch.
pub fn ref_cast_nullable(&mut self, type_idx: u32) {
Instruction::RefCastNullable(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
/// Downcast to the abstract `i31` heap type — paired with
/// [`Self::i31_get_s`] to retrieve the boxed i32.
pub fn ref_cast_i31(&mut self) {
Instruction::RefCastNonNull(HeapType::I31).encode(&mut self.bytes);
/// Non-trapping "is this an `i31`?" test — unlike [`Self::ref_cast_i31`],
/// this returns `false` (rather than trapping) on a mismatch, and accepts
/// null. Used to dynamically classify an `AnyRef` value (ADR-0033).
pub fn ref_test_i31(&mut self) {
Instruction::RefTestNullable(HeapType::I31).encode(&mut self.bytes);
/// Non-trapping test that the operand is a non-null instance of `heap`.
pub fn ref_test_non_null(&mut self, heap: HeapType) {
Instruction::RefTestNonNull(heap).encode(&mut self.bytes);
/// Non-trapping "is this a `(ref null type_idx)`?" test — true for null
/// (member of every nullable type) or a genuine instance, false for
/// anything else. Used to dispatch a dynamically-typed `AnyRef` list
/// operand to `$pair` handling vs. a catchable `TYPE-ERROR` (ADR-0033).
pub fn ref_test(&mut self, type_idx: u32) {
Instruction::RefTestNullable(HeapType::Concrete(type_idx)).encode(&mut self.bytes);
/// Downcasts an `anyref` to the abstract nullable `eq` heap type, the
/// operand type `ref.eq` requires. Used to test identity against the `$t`
/// singleton global (ADR-0033): every anyref the compiler or host
/// produces is an i31, struct or array, all subtypes of `eq`.
pub fn ref_cast_eq(&mut self) {
Instruction::RefCastNullable(HeapType::Abstract {
ty: wasm_encoder::AbstractHeapType::Eq,
/// Reference identity comparison — pops two `eqref` operands, pushes an
/// i32 boolean. Used to recognise the `$t` singleton by identity rather
/// than by (structurally non-unique) type test (ADR-0033).
pub fn ref_eq(&mut self) {
Instruction::RefEq.encode(&mut self.bytes);
pub fn drop_value(&mut self) {
Instruction::Drop.encode(&mut self.bytes);
/// Pushes a typed funcref pointing at the wasm function with the
/// given index. Tier 1.5 closure construction pairs this with
/// [`Self::struct_new`] to populate a `$closure_<sig>` struct.
pub fn ref_func(&mut self, func_idx: u32) {
Instruction::RefFunc(func_idx).encode(&mut self.bytes);
/// Constructs a struct of the registered struct type. Consumes one
/// stack value per field in declaration order; pushes a
/// `(ref $type_idx)` of the resulting struct.
pub fn struct_new(&mut self, type_idx: u32) {
Instruction::StructNew(type_idx).encode(&mut self.bytes);