Skip to content

Truly independent web browser

273 KB
Raw Blame
# Copyright (c) 2026-present, the Ladybird developers.
# SPDX-License-Identifier: BSD-2-Clause

# The in-place WebAssembly interpreter. Handlers run on a function's original code bytes: `ip` points at the
# current instruction, whose immediates the handler decodes itself, and `stp` points at the next entry of the
# function's side table, which branches read their targets from (see validator.rs). Frames live on the machine
# stack (see abi.rs): locals at fp + 8 * i, the frame header below fp, and the operand stack below the header in
# 8-byte slots, with `vsp` pointing at the top slot.
#
# Slots hold i32 and f32 values zero-extended, i64 and f64 values and references in full, and v128 values in two
# slots, low half at the lower address.

# Decoding immediates. The validator guarantees they are well-formed, so only their lengths and values matter.

# An unsigned LEB128 value and its length. Most immediates are a single byte, so the loop over the others is out of
# line.
inline fn read_leb(bytes: Sequence<u8>) -> (u64, u64) {
    let first: u64 = alias(bytes[0]);
    let mut result: u64 = first;
    let mut length: u64 = 1;
    if first >= 128 @cold {
        result = first & 127;
        let mut shift: u64 = 7;
        let mut byte: u64 = first;
        do {
            byte = alias(bytes[length]);
            let bits = (byte & 127) << shift;
            result = result | bits;
            shift = shift + 7;
            length = length + 1;
        } while byte >= 128;
    }
    (result, length)
}

# A signed LEB128 value, sign-extended to 64 bits, and its length.
inline fn read_signed_leb(bytes: Sequence<u8>) -> (u64, u64) {
    let first: u64 = alias(bytes[0]);
    let moved = first << 57;
    let signed: i64 = alias(moved);
    let extended = signed >> 57;
    let mut result: u64 = alias(extended);
    let mut length: u64 = 1;
    if first >= 128 @cold {
        result = first & 127;
        let mut shift: u64 = 7;
        let mut byte: u64 = first;
        do {
            byte = alias(bytes[length]);
            let bits = (byte & 127) << shift;
            result = result | bits;
            shift = shift + 7;
            length = length + 1;
        } while byte >= 128;
        if shift < 64 {
            if byte & 64 != 0 {
                let all_ones: u64 = 0xffffffffffffffff;
                let extension = all_ones << shift;
                result = result | extension;
            }
        }
    }
    (result, length)
}

# The length of a LEB128 value.
inline fn leb_length(bytes: Sequence<u8>) -> u64 {
    let first: u64 = alias(bytes[0]);
    let mut length: u64 = 1;
    if first >= 128 @cold {
        let mut byte: u64 = first;
        while byte >= 128 {
            byte = alias(bytes[length]);
            length = length + 1;
        }
    }
    length
}

# The length of a block type immediate: a value type, which takes one byte unless it is a reference type with a heap
# type, the empty type, or a type index.
inline fn block_type_length(bytes: Sequence<u8>) -> u64 {
    let first: u64 = alias(bytes[0]);
    let mut length: u64 = 1;
    if first >= 0x63 @cold {
        if first <= 0x64 {
            let start: u64 = alias(bytes);
            let heap_bytes: Sequence<u8> = alias(start + 1);
            let heap_length = leb_length(heap_bytes);
            length = heap_length + 1;
        }
        if first >= 128 {
            length = leb_length(bytes);
        }
    }
    length
}

inline fn immediates() -> Sequence<u8> {
    let current: u64 = ip;
    let bytes: Sequence<u8> = alias(current + 1);
    bytes
}

# The immediates after a prefix opcode and its LEB128 sub-opcode, and the offset from the instruction to them.
inline fn prefixed_immediates() -> (Sequence<u8>, u64) {
    let length = leb_length(immediates());
    let current: u64 = ip;
    let offset = length + 1;
    let bytes: Sequence<u8> = alias(current + offset);
    (bytes, offset)
}

inline fn advance(length: u64) {
    let current: u64 = ip;
    let next = current + length;
    ip = next;
    dispatch;
}

# The operand stack.

inline fn stack() -> Sequence<u64> {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    slots
}

inline fn push(value: u64) {
    let top: u64 = vsp;
    let next = top - 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = value;
}

inline fn pop() -> u64 {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let value = slots[0];
    let next = top + 8;
    vsp = next;
    value
}

inline fn pop32() -> u32 {
    let value = pop();
    u32(value)
}

inline fn drop_slots(count: u64) {
    let top: u64 = vsp;
    let next = top + (count << 3);
    vsp = next;
}

# Operand helpers for numeric instructions: the two operands of a binary instruction, whose result replaces the
# first of them, and the operand of a unary instruction, whose result replaces it.

inline fn binary_operands() -> (u64, u64) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let rhs = slots[0];
    let lhs = slots[1];
    let next = top + 8;
    vsp = next;
    (lhs, rhs)
}

inline fn binary32() -> (u32, u32) {
    let [lhs, rhs] = binary_operands();
    let narrow_lhs = u32(lhs);
    let narrow_rhs = u32(rhs);
    (narrow_lhs, narrow_rhs)
}

inline fn unary_operand() -> u64 {
    let slots = stack();
    slots[0]
}

inline fn unary32() -> u32 {
    let value = unary_operand();
    u32(value)
}

inline fn set_top(value: u64) {
    let slots = stack();
    slots[0] = value;
}

inline fn result(value: u64) {
    set_top(value);
    dispatch_next;
}

inline fn result32(value: u32) {
    let wide: u64 = alias(value);
    set_top(wide);
    dispatch_next;
}

inline fn signed64(value: u64) -> i64 {
    let signed: i64 = alias(value);
    signed
}

inline fn unsigned64(value: u64) -> u64 {
    value
}

# The frame.

inline fn frame_word(offset: u64) -> u64 {
    let frame: u64 = fp;
    let mut value: u64;
    load64(value, [frame, offset]);
    value
}

inline fn context() -> u64 {
    let frame: u64 = fp;
    let mut value: u64;
    load64(value, [frame, FRAME_CONTEXT]);
    value
}

inline fn instance() -> u64 {
    let frame: u64 = fp;
    let mut value: u64;
    load64(value, [frame, FRAME_INSTANCE]);
    value
}

# Leave the interpreter with a trap: return to the entry the runtime made, with the trap code in its record.
inline fn trap_with(code: u64, context_pointer: u64) {
    let mut record: u64;
    load64(record, [context_pointer, CONTEXT_ENTRY_RECORD]);
    store32([record, ENTRY_TRAP], code);
    leave(context_pointer, record);
}

inline fn trap(code: u64) {
    trap_with(code, context());
}

# Restore the state of the entry that `record` describes and leave the interpreter.
inline fn leave(context_pointer: u64, record: u64) {
    let mut entry_fp: u64;
    load64(entry_fp, [context_pointer, CONTEXT_ENTRY_FP]);
    let mut saved_fp: u64;
    load64(saved_fp, [record, ENTRY_SAVED_ENTRY_FP]);
    let mut saved_record: u64;
    load64(saved_record, [record, ENTRY_SAVED_ENTRY_RECORD]);
    store64([context_pointer, CONTEXT_ENTRY_FP], saved_fp);
    store64([context_pointer, CONTEXT_ENTRY_RECORD], saved_record);
    fp = entry_fp;
    exit();
}

# Calls and returns.

# OPTIMIZATION: Shifts align without the scratch registers a 64-bit `and` with an immediate reserves, which the call
# handlers do not have to spare.
inline fn align_down_16(address: u64) -> u64 {
    let shifted = address >> 4;
    shifted << 4
}

# Touch the stack from `current` down to `bottom`, below which a new frame is about to end, if the frame reaches into a
# page below that of `current`. Windows commits a thread's stack as code touches the guard page below its committed
# part, and code that skips past the guard page faults. Every page down to that of the stack pointer is committed, so
# probing the pages in between from the top down keeps that so; a frame within the stack pointer's page needs none.
# An atomic load is never removed, so it serves as the probe.
inline fn probe_stack(current: u64, bottom: u64) {
    let current_page = current >> 12;
    let bottom_page = bottom >> 12;
    if bottom_page != current_page @cold {
        let mut address = current;
        while address > bottom {
            let _ = atomic_load8([address, 0]);
            address = address - 4096;
        }
        let _ = atomic_load8([bottom, 0]);
    }
}

# Call a host function through the runtime, with its arguments on top of the operand stack.
inline fn call_host_function(function: u64, return_ip: u64, context_pointer: u64) {
    store64([context_pointer, CONTEXT_ARGUMENTS], function);
    let top: u64 = vsp;
    store64([context_pointer, CONTEXT_ARGUMENT_1], top);
    store64([context_pointer, CONTEXT_ARGUMENT_2], return_ip);
    store64([context_pointer, CONTEXT_ARGUMENT_3], fp);
    # The helper may clobber every allocatable register, so nothing but its result and the context is kept across it.
    let saved_context = context();
    ip = return_ip;
    let new_top: u64 = call_helper(wasm_runtime_call_host, context_pointer);
    guard new_top != 0 else @cold {
        trap(TRAP_RUNTIME);
    };
    if new_top == 1 @cold {
        resume_at_handler(saved_context);
    }
    vsp = new_top;
    dispatch;
}

# Call a function with compiled code, with its arguments on top of the operand stack. The runtime runs it and replaces
# the arguments with its results; a trap in it leaves the interpreter from the compiled code. An exception it throws
# continues at the call, as with host functions. A tail call it makes of a function that does not run compiled comes
# back to the interpreter, which makes it in the compiled function's place (see CallPending).
inline fn call_compiled_function(function: u64, return_ip: u64, context_pointer: u64) {
    store64([context_pointer, CONTEXT_ARGUMENTS], function);
    let top: u64 = vsp;
    store64([context_pointer, CONTEXT_ARGUMENT_1], top);
    store64([context_pointer, CONTEXT_ARGUMENT_2], return_ip);
    store64([context_pointer, CONTEXT_ARGUMENT_3], fp);
    # The helper may clobber every allocatable register, so nothing but its result and the context is kept across it.
    let saved_context = context();
    ip = return_ip;
    let new_top: u64 = call_helper(wasm_runtime_call_compiled, context_pointer);
    guard new_top != 0 else @cold {
        trap(TRAP_RUNTIME);
    };
    if new_top == 1 @cold {
        resume_at_handler(saved_context);
    }
    if new_top == 2 @cold {
        let pending_context = context();
        let mut pending_ip: u64;
        load64(pending_ip, [pending_context, CONTEXT_ARGUMENT_4]);
        ip = pending_ip;
        dispatch;
    }
    vsp = new_top;
    dispatch;
}

# Continue at the exception handler the runtime found, which it left in the context's arguments.
inline fn resume_at_handler(context_pointer: u64) {
    let mut handler_fp: u64;
    load64(handler_fp, [context_pointer, CONTEXT_ARGUMENTS]);
    let mut handler_ip: u64;
    load64(handler_ip, [context_pointer, CONTEXT_ARGUMENT_1]);
    let mut handler_stp: u64;
    load64(handler_stp, [context_pointer, CONTEXT_ARGUMENT_2]);
    let mut handler_vsp: u64;
    load64(handler_vsp, [context_pointer, CONTEXT_ARGUMENT_3]);
    let mut handler_instance: u64;
    load64(handler_instance, [handler_fp, FRAME_INSTANCE]);
    let mut handler_memory_base: u64;
    load64(handler_memory_base, [handler_instance, INSTANCE_MEMORY0_BASE]);
    fp = handler_fp;
    ip = handler_ip;
    stp = handler_stp;
    vsp = handler_vsp;
    memory_base = handler_memory_base;
    dispatch;
}

# Throw the exception `exception` refers to, or a new one with `tag` and the values on top of the operand stack.
inline fn throw_exception(tag: u64, exception: u64) {
    let context_pointer = context();
    store64([context_pointer, CONTEXT_ARGUMENTS], tag);
    let top: u64 = vsp;
    store64([context_pointer, CONTEXT_ARGUMENT_1], top);
    store64([context_pointer, CONTEXT_ARGUMENT_2], ip);
    store64([context_pointer, CONTEXT_ARGUMENT_3], fp);
    store64([context_pointer, CONTEXT_ARGUMENT_4], exception);
    let caught: u64 = call_helper(wasm_runtime_throw, context_pointer);
    guard caught != 0 else @cold {
        trap(TRAP_RUNTIME);
    };
    resume_at_handler(context_pointer);
}

handler Throw() @opcode(0x08) {
    let [tag_index, _] = read_leb(immediates());
    let instance_pointer = instance();
    let mut tags_pointer: u64;
    load64(tags_pointer, [instance_pointer, INSTANCE_TAGS]);
    let tags: Sequence<u64> = alias(tags_pointer);
    let tag = tags[tag_index];
    throw_exception(tag, 0);
}

handler ThrowRef() @opcode(0x0a) {
    let exception = pop();
    guard exception != 0 else @cold {
        trap(TRAP_NULL_REFERENCE);
    };
    throw_exception(0, exception);
}

# A try_table runs like a block; its handlers are found by the runtime when something throws.
handler TryTable() @opcode(0x1f) {
    let block_length = block_type_length(immediates());
    let current: u64 = ip;
    let count_bytes: Sequence<u8> = alias(current + 1 + block_length);
    let [count, count_length] = read_leb(count_bytes);
    let mut position = current + 1 + block_length + count_length;
    let mut remaining = count;
    while remaining != 0 {
        let clause: Sequence<u8> = alias(position);
        let kind: u64 = alias(clause[0]);
        position = position + 1;
        if kind < 2 {
            let tag_bytes: Sequence<u8> = alias(position);
            let tag_length = leb_length(tag_bytes);
            position = position + tag_length;
        }
        let label_bytes: Sequence<u8> = alias(position);
        let label_length = leb_length(label_bytes);
        position = position + label_length;
        remaining = remaining - 1;
    }
    ip = position;
    dispatch;
}

# Enter a function with v128 parameters or locals, whose frame has storage for them (see call_function).
inline fn call_vector_function(function: u64, return_ip: u64, context_pointer: u64) {
    let mut locals_size: u64;
    load32(locals_size, [function, FUNCTION_LOCALS_SIZE]);
    let mut frame_size: u64;
    load32(frame_size, [function, FUNCTION_FRAME_SIZE]);
    let caller_vsp: u64 = vsp;
    let new_fp = align_down_16(caller_vsp - locals_size);
    # The stack pointer only ever moves down while WebAssembly runs: returns leave it where the callee had it, and a
    # frame that would end above it ends at it instead. Spilled values rely on that when they move into the new
    # frame (see spill.rs in Flap).
    let current_sp: u64 = sp;
    let mut bottom = new_fp - frame_size;
    if current_sp < bottom {
        bottom = current_sp;
    }
    let mut limit: u64;
    load64(limit, [context_pointer, CONTEXT_STACK_LIMIT]);
    guard bottom >= limit else @cold {
        trap_with(TRAP_STACK_EXHAUSTED, context_pointer);
    };
    probe_stack(current_sp, bottom);
    sp = bottom;
    store64([new_fp, FRAME_SAVED_FP], fp);
    store64([new_fp, FRAME_RETURN_IP], return_ip);
    store64([new_fp, FRAME_SAVED_STP], stp);
    store64([new_fp, FRAME_FUNCTION], function);
    store64([new_fp, FRAME_SAVED_MEMORY_BASE], memory_base);
    store64([new_fp, FRAME_CONTEXT], context_pointer);
    fp = new_fp;

    # Copy the arguments into the first locals and clear the others. A v128 local's slot gets the address of its
    # storage, at the offset from fp the function's vector storage table gives; scalar locals have offset zero.
    let mut param_slots: u64;
    load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
    let mut storage_table: u64;
    load64(storage_table, [function, FUNCTION_VECTOR_STORAGE]);
    let offsets: Sequence<u32> = alias(storage_table);
    let arguments = stack();
    let locals: Sequence<u64> = alias(new_fp);
    let mut index: u64 = 0;
    let mut source: u64 = param_slots;
    while source != 0 {
        let offset: u64 = alias(offsets[index]);
        if offset == 0 {
            source = source - 1;
            locals[index] = arguments[source];
        } else {
            source = source - 2;
            let storage = new_fp + offset;
            let high_source = source + 1;
            let low = arguments[source];
            let high = arguments[high_source];
            store64([storage, 0], low);
            store64([storage, 8], high);
            locals[index] = storage;
        }
        index = index + 1;
    }
    let return_vsp = caller_vsp + (param_slots << 3);
    store64([new_fp, FRAME_RETURN_VSP], return_vsp);
    let mut local_count: u64;
    load32(local_count, [function, FUNCTION_LOCAL_COUNT]);
    while index < local_count {
        let offset: u64 = alias(offsets[index]);
        if offset == 0 {
            locals[index] = 0;
        } else {
            let storage = new_fp + offset;
            store64([storage, 0], 0);
            store64([storage, 8], 0);
            locals[index] = storage;
        }
        index = index + 1;
    }

    let mut callee_instance: u64;
    load64(callee_instance, [function, FUNCTION_INSTANCE]);
    store64([new_fp, FRAME_INSTANCE], callee_instance);
    let mut code: u64;
    load64(code, [function, FUNCTION_CODE]);
    let mut side_table: u64;
    load64(side_table, [function, FUNCTION_SIDE_TABLE]);
    let mut callee_memory_base: u64;
    load64(callee_memory_base, [callee_instance, INSTANCE_MEMORY0_BASE]);
    let empty_stack = new_fp - FRAME_HEADER_SIZE;
    ip = code;
    stp = side_table;
    memory_base = callee_memory_base;
    vsp = empty_stack;
    dispatch;
}

# Call `function` with its arguments on top of the operand stack, continuing at `return_ip` when it returns: charge
# its tier-up budget, then enter it.
inline fn call_function(function: u64, return_ip: u64, context_pointer: u64) {
    let mut callee: u64 = function;
    let mut callee_return_ip: u64 = return_ip;
    let mut callee_context: u64 = context_pointer;
    let mut budget: u64;
    load32(budget, [callee, FUNCTION_TIER_UP_BUDGET]);
    if budget < TIER_UP_CALL_COST @cold {
        # The runtime compiles the function, or not, and resets its budget. It may clobber every allocatable register,
        # so the call's operands wait in the context.
        store64([callee_context, CONTEXT_ARGUMENTS], callee);
        store64([callee_context, CONTEXT_ARGUMENT_1], callee_return_ip);
        store64([callee_context, CONTEXT_ARGUMENT_2], 0);
        let _ignored: u64 = call_helper(wasm_runtime_tier_up, callee_context);
        callee_context = context();
        load64(callee, [callee_context, CONTEXT_ARGUMENTS]);
        load64(callee_return_ip, [callee_context, CONTEXT_ARGUMENT_1]);
        load32(budget, [callee, FUNCTION_TIER_UP_BUDGET]);
    }
    store32([callee, FUNCTION_TIER_UP_BUDGET], budget - TIER_UP_CALL_COST);
    enter_function(callee, callee_return_ip, callee_context);
}

# Enter `function` with its arguments on top of the operand stack, continuing at `return_ip` when it returns.
#
# OPTIMIZATION: The caller's state goes into the new frame's header as soon as the frame's address is known, so that
# its registers can be reused while the arguments are copied.
inline fn enter_function(function: u64, return_ip: u64, context_pointer: u64) {
    let mut kind: u64;
    load32(kind, [function, FUNCTION_KIND]);
    if kind != FUNCTION_KIND_WASM @cold {
        if kind == FUNCTION_KIND_HOST {
            call_host_function(function, return_ip, context_pointer);
        }
        if kind == FUNCTION_KIND_COMPILED {
            call_compiled_function(function, return_ip, context_pointer);
        }
        call_vector_function(function, return_ip, context_pointer);
    }
    let mut locals_size: u64;
    load32(locals_size, [function, FUNCTION_LOCALS_SIZE]);
    let mut frame_size: u64;
    load32(frame_size, [function, FUNCTION_FRAME_SIZE]);
    let caller_vsp: u64 = vsp;
    let new_fp = align_down_16(caller_vsp - locals_size);
    # The stack pointer only ever moves down while WebAssembly runs: returns leave it where the callee had it, and a
    # frame that would end above it ends at it instead. Spilled values rely on that when they move into the new
    # frame (see spill.rs in Flap).
    let current_sp: u64 = sp;
    let mut bottom = new_fp - frame_size;
    if current_sp < bottom {
        bottom = current_sp;
    }
    let mut limit: u64;
    load64(limit, [context_pointer, CONTEXT_STACK_LIMIT]);
    guard bottom >= limit else @cold {
        trap_with(TRAP_STACK_EXHAUSTED, context_pointer);
    };
    probe_stack(current_sp, bottom);
    sp = bottom;
    store64([new_fp, FRAME_SAVED_FP], fp);
    store64([new_fp, FRAME_RETURN_IP], return_ip);
    store64([new_fp, FRAME_SAVED_STP], stp);
    store64([new_fp, FRAME_FUNCTION], function);
    store64([new_fp, FRAME_SAVED_MEMORY_BASE], memory_base);
    store64([new_fp, FRAME_CONTEXT], context_pointer);
    fp = new_fp;

    # Copy the arguments into the first locals and clear the others.
    let mut param_slots: u64;
    load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
    let arguments = stack();
    let locals: Sequence<u64> = alias(new_fp);
    let mut index: u64 = 0;
    let mut source: u64 = param_slots;
    while index < param_slots {
        source = source - 1;
        locals[index] = arguments[source];
        index = index + 1;
    }
    let return_vsp = caller_vsp + (param_slots << 3);
    store64([new_fp, FRAME_RETURN_VSP], return_vsp);
    let mut local_count: u64;
    load32(local_count, [function, FUNCTION_LOCAL_COUNT]);
    while index < local_count {
        locals[index] = 0;
        index = index + 1;
    }

    let mut callee_instance: u64;
    load64(callee_instance, [function, FUNCTION_INSTANCE]);
    store64([new_fp, FRAME_INSTANCE], callee_instance);
    let mut code: u64;
    load64(code, [function, FUNCTION_CODE]);
    let mut side_table: u64;
    load64(side_table, [function, FUNCTION_SIDE_TABLE]);
    let mut callee_memory_base: u64;
    load64(callee_memory_base, [callee_instance, INSTANCE_MEMORY0_BASE]);
    let empty_stack = new_fp - FRAME_HEADER_SIZE;
    ip = code;
    stp = side_table;
    memory_base = callee_memory_base;
    vsp = empty_stack;
    dispatch;
}

# Move `count` slots from the top of the operand stack to just below `destination`, as the new top of the stack. The
# slots move up, so the highest one is copied first.
inline fn move_top_slots(count: u64, destination: u64) -> u64 {
    let new_top = destination - (count << 3);
    let source = stack();
    let target: Sequence<u64> = alias(new_top);
    let mut index: u64 = count;
    while index != 0 {
        index = index - 1;
        target[index] = source[index];
    }
    new_top
}

# Return from the current function with its results on top of the operand stack. The results land where the
# arguments were, which may overlap the frame header, so the header is read first.
inline fn return_from_function() {
    let frame: u64 = fp;
    let mut function: u64;
    load64(function, [frame, FRAME_FUNCTION]);
    let mut return_vsp: u64;
    load64(return_vsp, [frame, FRAME_RETURN_VSP]);
    let mut return_ip: u64;
    load64(return_ip, [frame, FRAME_RETURN_IP]);
    ip = return_ip;
    let mut saved_stp: u64;
    load64(saved_stp, [frame, FRAME_SAVED_STP]);
    stp = saved_stp;
    let mut saved_memory_base: u64;
    load64(saved_memory_base, [frame, FRAME_SAVED_MEMORY_BASE]);
    memory_base = saved_memory_base;
    let mut saved_fp: u64;
    load64(saved_fp, [frame, FRAME_SAVED_FP]);
    fp = saved_fp;
    let mut result_slots: u64;
    load32(result_slots, [function, FUNCTION_RESULT_SLOTS]);
    let new_top = move_top_slots(result_slots, return_vsp);
    vsp = new_top;
    dispatch;
}

# Replace the current frame with a call to `function`, whose arguments are on top of the operand stack: move them to
# where the current function's caller put its arguments, and call from the caller's state.
inline fn tail_call(function: u64) {
    let context_pointer = context();
    let frame: u64 = fp;
    let mut return_vsp: u64;
    load64(return_vsp, [frame, FRAME_RETURN_VSP]);
    let mut return_ip: u64;
    load64(return_ip, [frame, FRAME_RETURN_IP]);
    let mut saved_stp: u64;
    load64(saved_stp, [frame, FRAME_SAVED_STP]);
    stp = saved_stp;
    let mut saved_memory_base: u64;
    load64(saved_memory_base, [frame, FRAME_SAVED_MEMORY_BASE]);
    memory_base = saved_memory_base;
    let mut saved_fp: u64;
    load64(saved_fp, [frame, FRAME_SAVED_FP]);
    fp = saved_fp;
    let mut param_slots: u64;
    load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
    let new_top = move_top_slots(param_slots, return_vsp);
    vsp = new_top;
    call_function(function, return_ip, context_pointer);
}

# Call the function that compiled code tail called, which the runtime left in the context's first argument with its
# argument slots, in operand stack order, where the second points, so that its results end at `end`, and continue at
# `return_ip` when it returns. Its arguments may need more room than those of the function it replaces.
inline fn call_pending(end: u64, return_ip: u64) {
    let context_pointer = context();
    let mut function: u64;
    load64(function, [context_pointer, CONTEXT_ARGUMENTS]);
    let mut arguments_pointer: u64;
    load64(arguments_pointer, [context_pointer, CONTEXT_ARGUMENT_1]);
    let mut param_slots: u64;
    load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
    let new_top = end - (param_slots << 3);
    let current_sp: u64 = sp;
    # The handlers' spill area is right above the stack pointer, so it must stay below the arguments.
    let bottom = align_down_16(new_top - SPILL_AREA_SIZE);
    if bottom < current_sp {
        let mut limit: u64;
        load64(limit, [context_pointer, CONTEXT_STACK_LIMIT]);
        guard bottom >= limit else @cold {
            trap_with(TRAP_STACK_EXHAUSTED, context_pointer);
        };
        probe_stack(current_sp, bottom);
        sp = bottom;
    }
    let arguments: Sequence<u64> = alias(arguments_pointer);
    let slots: Sequence<u64> = alias(new_top);
    let mut index: u64 = 0;
    while index < param_slots {
        slots[index] = arguments[index];
        index = index + 1;
    }
    vsp = new_top;
    call_function(function, return_ip, context_pointer);
}

# Branches.

# Take the branch whose side-table entry is at `entry`, from the instruction at `branch_ip`.
inline fn take_branch(entry: u64, branch_ip: u64) {
    let mut drop: u64;
    load32(drop, [entry, 12]);
    if drop != 0 {
        let mut keep: u64;
        load32(keep, [entry, 8]);
        let top: u64 = vsp;
        let new_top = top + (drop << 3);
        let destination = new_top + (keep << 3);
        let moved_top = move_top_slots(keep, destination);
        vsp = moved_top;
    }
    let mut pc_delta: u64;
    load32(pc_delta, [entry, 0]);
    let mut stp_delta: u64;
    load32(stp_delta, [entry, 4]);
    let narrow_pc_delta = i32(pc_delta);
    let signed_pc_delta = i64(narrow_pc_delta);
    let narrow_stp_delta = i32(stp_delta);
    let signed_stp_delta = i64(narrow_stp_delta);
    let pc_offset: u64 = alias(signed_pc_delta);
    let stp_offset: u64 = alias(signed_stp_delta);
    let target_ip = branch_ip + pc_offset;
    let target_stp = entry + (stp_offset << 4);
    ip = target_ip;
    stp = target_stp;
    dispatch;
}

inline fn branch() {
    let entry: u64 = stp;
    let current: u64 = ip;
    take_branch(entry, current);
}

inline fn skip_entry_and_advance(length: u64) {
    let current_stp: u64 = stp;
    let next_stp = current_stp + 16;
    stp = next_stp;
    advance(length);
}

# The pseudo-instructions the runtime enters and leaves the interpreter with.

# Enter: `vsp` holds the entry record. Build a frame for the runtime below the entry frame and push the arguments.
# The runtime's code continues with a CallEntry pseudo-instruction, which calls the function, and an Exit one, which
# the function returns to.
handler Enter() @opcode(0xf0) {
    let record: u64 = vsp;
    let mut context_pointer: u64;
    load64(context_pointer, [record, ENTRY_CONTEXT]);
    let mut previous_fp: u64;
    load64(previous_fp, [context_pointer, CONTEXT_ENTRY_FP]);
    store64([record, ENTRY_SAVED_ENTRY_FP], previous_fp);
    let mut previous_record: u64;
    load64(previous_record, [context_pointer, CONTEXT_ENTRY_RECORD]);
    store64([record, ENTRY_SAVED_ENTRY_RECORD], previous_record);
    store64([context_pointer, CONTEXT_ENTRY_FP], fp);
    store64([context_pointer, CONTEXT_ENTRY_RECORD], record);
    store32([record, ENTRY_TRAP], 0);

    let mut function: u64;
    load64(function, [record, ENTRY_FUNCTION]);
    let mut param_slots: u64;
    load32(param_slots, [function, FUNCTION_PARAM_SLOTS]);
    let mut result_slots: u64;
    load32(result_slots, [function, FUNCTION_RESULT_SLOTS]);
    let mut operand_slots: u64 = param_slots;
    if result_slots > param_slots {
        operand_slots = result_slots;
    }
    let entry_sp: u64 = sp;
    let runtime_fp = align_down_16(entry_sp - FRAME_HEADER_SIZE - SPILL_AREA_SIZE);
    let arguments_top = runtime_fp - FRAME_HEADER_SIZE - (param_slots << 3);
    let new_sp = align_down_16(runtime_fp - FRAME_HEADER_SIZE - (operand_slots << 3) - SPILL_AREA_SIZE);
    let mut limit: u64;
    load64(limit, [context_pointer, CONTEXT_STACK_LIMIT]);
    guard new_sp >= limit else @cold {
        trap_with(TRAP_STACK_EXHAUSTED, context_pointer);
    };
    # The native code that entered the interpreter touched the stack above its frame, but maybe not the page of the
    # entry frame's stack pointer.
    let _ = atomic_load8([entry_sp, 0]);
    probe_stack(entry_sp, new_sp);
    sp = new_sp;
    fp = runtime_fp;
    store64([runtime_fp, FRAME_INSTANCE], 0);
    store64([runtime_fp, FRAME_CONTEXT], context_pointer);
    store64([runtime_fp, FRAME_FUNCTION], function);
    vsp = arguments_top;
    let mut arguments_pointer: u64;
    load64(arguments_pointer, [record, ENTRY_ARGUMENTS]);
    let arguments: Sequence<u64> = alias(arguments_pointer);
    let slots: Sequence<u64> = alias(arguments_top);
    let mut index: u64 = 0;
    while index < param_slots {
        slots[index] = arguments[index];
        index = index + 1;
    }
    advance(1);
}

# Call the function of the entry record, which Enter left in the runtime's frame.
handler CallEntry() @opcode(0xf3) {
    let context_pointer = context();
    let function = frame_word(FRAME_FUNCTION);
    let current: u64 = ip;
    let exit_ip = current + 1;
    call_function(function, exit_ip, context_pointer);
}

# The function returned to the runtime's frame with its results on top of the operand stack.
handler Exit() @opcode(0xf1) {
    let context_pointer = context();
    let mut record: u64;
    load64(record, [context_pointer, CONTEXT_ENTRY_RECORD]);
    let mut function: u64;
    load64(function, [record, ENTRY_FUNCTION]);
    let mut result_slots: u64;
    load32(result_slots, [function, FUNCTION_RESULT_SLOTS]);
    let mut results_pointer: u64;
    load64(results_pointer, [record, ENTRY_RESULTS]);
    let results: Sequence<u64> = alias(results_pointer);
    let values = stack();
    let mut index: u64 = 0;
    while index < result_slots {
        results[index] = values[index];
        index = index + 1;
    }
    leave(context_pointer, record);
}

# The runtime's fault handler continues here when a memory access faults.
handler MemoryFault() @opcode(0xf2) {
    trap(TRAP_MEMORY_OUT_OF_BOUNDS);
}

# Compiled code traps here, with fp the frame of the code that trapped, whose header has the context, and the trap
# code in the context's first argument.
handler TrapFromNative() @opcode(0xef) {
    let context_pointer = context();
    let mut code: u64;
    load64(code, [context_pointer, CONTEXT_ARGUMENTS]);
    trap_with(code, context_pointer);
}

# Compiled code that call_compiled_function() called, or that continued a frame at a loop, tail called a function that
# does not run compiled, which the runtime left pending (see call_pending()), with where the results of the function
# it replaces go in the context's third argument and the address its call returns to in the fourth.
handler CallPending() @opcode(0xee) {
    let context_pointer = context();
    let mut end: u64;
    load64(end, [context_pointer, CONTEXT_ARGUMENT_2]);
    let mut return_ip: u64;
    load64(return_ip, [context_pointer, CONTEXT_ARGUMENT_3]);
    call_pending(end, return_ip);
}

# Bytes that are no instruction. Validation rejects every one of them, so this is never reached.
handler Fallback() @fallback {
    trap(TRAP_UNREACHABLE);
}

# Control instructions.

handler Unreachable() @opcode(0x00) {
    trap(TRAP_UNREACHABLE);
}

handler Nop() @opcode(0x01) {
    dispatch_next;
}

handler Block() @opcode(0x02) {
    let length = block_type_length(immediates());
    advance(length + 1);
}

handler Loop() @opcode(0x03) {
    # Another thread stops the code of a store by making its stack limit the highest address. Calls notice, but only
    # this stops a loop that does not call.
    let interrupt_context = context();
    let mut stack_limit: u64;
    load64(stack_limit, [interrupt_context, CONTEXT_STACK_LIMIT]);
    if stack_limit + 1 == 0 @cold {
        trap_with(TRAP_STACK_EXHAUSTED, interrupt_context);
    }
    let function = frame_word(FRAME_FUNCTION);
    let mut budget: u64;
    load32(budget, [function, FUNCTION_TIER_UP_BUDGET]);
    if budget == 0 @cold {
        let context_pointer = context();
        store64([context_pointer, CONTEXT_ARGUMENTS], function);
        let current: u64 = ip;
        store64([context_pointer, CONTEXT_ARGUMENT_1], current);
        store64([context_pointer, CONTEXT_ARGUMENT_2], 1);
        let frame: u64 = fp;
        store64([context_pointer, CONTEXT_ARGUMENT_3], frame);
        # The runtime may run the rest of the function compiled from here (OSR), leaving its results on the operand
        # stack, or a tail call it made pending.
        let results_top: u64 = call_helper(wasm_runtime_tier_up, context_pointer);
        if results_top == 1 {
            resume_at_handler(context_pointer);
        }
        if results_top == 2 {
            trap(TRAP_RUNTIME);
        }
        if results_top == 3 {
            # The frame's compiled code tail called a function that does not run compiled, which replaces the frame.
            let frame: u64 = fp;
            let mut saved_stp: u64;
            load64(saved_stp, [frame, FRAME_SAVED_STP]);
            stp = saved_stp;
            let mut saved_memory_base: u64;
            load64(saved_memory_base, [frame, FRAME_SAVED_MEMORY_BASE]);
            memory_base = saved_memory_base;
            let mut saved_fp: u64;
            load64(saved_fp, [frame, FRAME_SAVED_FP]);
            fp = saved_fp;
            let pending_context = context();
            let mut pending_ip: u64;
            load64(pending_ip, [pending_context, CONTEXT_ARGUMENT_4]);
            ip = pending_ip;
            dispatch;
        }
        if results_top != 0 {
            vsp = results_top;
            return_from_function();
        }
        let skipped = block_type_length(immediates());
        advance(skipped + 1);
    }
    store32([function, FUNCTION_TIER_UP_BUDGET], budget - 1);
    let length = block_type_length(immediates());
    advance(length + 1);
}

handler If() @opcode(0x04) {
    let condition = pop32();
    if condition == 0 {
        branch();
    }
    let length = block_type_length(immediates());
    skip_entry_and_advance(length + 1);
}

# Reached at the end of an if's first arm: skip the second one.
handler Else() @opcode(0x05) {
    branch();
}

# Legacy exception handling. A try runs like a block, and the runtime finds its catch handlers when something throws
# inside it. Its handlers start after their catch or catch_all instruction, which runs only when the try's body or the
# handler before it completes, and skips the remaining handlers like else skips an if's second arm.
handler Try() @opcode(0x06) {
    let length = block_type_length(immediates());
    advance(length + 1);
}

handler Catch() @opcode(0x07) {
    branch();
}

handler CatchAll() @opcode(0x19) {
    branch();
}

# Reached at the end of a try's body, which delegate ends like end does.
handler Delegate() @opcode(0x18) {
    let [_, length] = read_leb(immediates());
    advance(length + 1);
}

# Rethrow the exception a catch or catch_all handler caught. The runtime kept it in a local of the frame for the
# handler, which the side-table entry of the rethrow names as its `keep` count.
handler Rethrow() @opcode(0x09) {
    let entry: u64 = stp;
    let mut local_index: u64;
    load32(local_index, [entry, 8]);
    let locals: Sequence<u64> = alias(fp);
    let exception = locals[local_index];
    throw_exception(0, exception);
}

handler End() @opcode(0x0b) {
    let frame: u64 = fp;
    let mut function: u64;
    load64(function, [frame, FRAME_FUNCTION]);
    let mut code_end: u64;
    load64(code_end, [function, FUNCTION_CODE_END]);
    let current: u64 = ip;
    if current == code_end {
        return_from_function();
    }
    dispatch_next;
}

handler Br() @opcode(0x0c) {
    branch();
}

handler BrIf() @opcode(0x0d) {
    let condition = pop32();
    if condition != 0 {
        branch();
    }
    let length = leb_length(immediates());
    skip_entry_and_advance(length + 1);
}

handler BrTable() @opcode(0x0e) {
    let [count, _] = read_leb(immediates());
    let popped = pop32();
    let index: u64 = alias(popped);
    let mut chosen = index;
    if index > count {
        chosen = count;
    }
    let first: u64 = stp;
    let entry = first + (chosen << 4);
    let current: u64 = ip;
    take_branch(entry, current);
}

handler Return() @opcode(0x0f) {
    return_from_function();
}

inline fn function_at(index: u64) -> u64 {
    let instance_pointer = instance();
    let mut functions_pointer: u64;
    load64(functions_pointer, [instance_pointer, INSTANCE_FUNCTIONS]);
    let functions: Sequence<u64> = alias(functions_pointer);
    functions[index]
}

handler Call() @opcode(0x10) {
    let [function_index, length] = read_leb(immediates());
    let function = function_at(function_index);
    let current: u64 = ip;
    let return_ip = current + length + 1;
    call_function(function, return_ip, context());
}

# The function a call_indirect calls, after checking that it exists and has the expected type.
inline fn indirect_callee(bytes: Sequence<u8>) -> (u64, u64) {
    let [type_index, type_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let table_bytes: Sequence<u8> = alias(start + type_length);
    let [table_index, table_length] = read_leb(table_bytes);
    let element = pop();
    let instance_pointer = instance();
    let mut tables_pointer: u64;
    load64(tables_pointer, [instance_pointer, INSTANCE_TABLES]);
    let tables: Sequence<u64> = alias(tables_pointer);
    let table = tables[table_index];
    let mut size: u64;
    load64(size, [table, TABLE_SIZE]);
    guard element < size else @cold {
        trap(TRAP_UNDEFINED_ELEMENT);
    };
    let mut elements_pointer: u64;
    load64(elements_pointer, [table, TABLE_ELEMENTS]);
    let elements: Sequence<u64> = alias(elements_pointer);
    let function = elements[element];
    guard function != 0 else @cold {
        trap(TRAP_UNINITIALIZED_ELEMENT);
    };
    let mut identities_pointer: u64;
    load64(identities_pointer, [instance_pointer, INSTANCE_TYPE_IDENTITIES]);
    let identities: Sequence<u32> = alias(identities_pointer);
    let expected = identities[type_index];
    let mut actual: u32;
    load32(actual, [function, FUNCTION_TYPE_IDENTITY]);
    if actual != expected @cold {
        # A function whose type is a subtype of the expected one may be called too.
        let wide_actual: u64 = alias(actual);
        let wide_expected: u64 = alias(expected);
        let actual_rtt = rtt_of_identity(wide_actual);
        let matches = rtt_is_subtype(actual_rtt, wide_expected);
        guard matches != 0 else @cold {
            trap(TRAP_INDIRECT_CALL_TYPE_MISMATCH);
        };
    }
    (function, type_length + table_length)
}

handler CallIndirect() @opcode(0x11) {
    let [function, length] = indirect_callee(immediates());
    let current: u64 = ip;
    let return_ip = current + length + 1;
    call_function(function, return_ip, context());
}

handler ReturnCall() @opcode(0x12) {
    let [function_index, _] = read_leb(immediates());
    let function = function_at(function_index);
    tail_call(function);
}

handler ReturnCallIndirect() @opcode(0x13) {
    let [function, _] = indirect_callee(immediates());
    tail_call(function);
}

handler CallRef() @opcode(0x14) {
    let length = leb_length(immediates());
    let function = pop();
    guard function != 0 else @cold {
        trap(TRAP_NULL_REFERENCE);
    };
    let current: u64 = ip;
    let return_ip = current + length + 1;
    call_function(function, return_ip, context());
}

handler ReturnCallRef() @opcode(0x15) {
    let function = pop();
    guard function != 0 else @cold {
        trap(TRAP_NULL_REFERENCE);
    };
    tail_call(function);
}

handler Drop() @opcode(0x1a) {
    drop_slots(1);
    dispatch_next;
}

handler Select() @opcode(0x1b) {
    let condition = pop32();
    let [first, second] = binary_operands();
    if condition == 0 {
        set_top(second);
    }
    dispatch_next;
}

handler SelectTyped() @opcode(0x1c) {
    let bytes = immediates();
    # The count of types, which is 1, may take more than one byte.
    let length = leb_length(bytes);
    let type_byte: u64 = alias(bytes[length]);
    let current: u64 = ip;
    let mut type_length: u64 = 1;
    if type_byte >= 0x63 @cold {
        if type_byte <= 0x64 {
            let heap_bytes: Sequence<u8> = alias(current + 2 + length);
            let heap_length = leb_length(heap_bytes);
            type_length = heap_length + 1;
        }
    }
    let condition = pop32();
    if type_byte == 0x7b @cold {
        # A v128 takes two slots.
        if condition == 0 {
            let slots = stack();
            slots[2] = slots[0];
            slots[3] = slots[1];
        }
        drop_slots(2);
        advance(1 + length + type_length);
    }
    let [first, second] = binary_operands();
    if condition == 0 {
        set_top(second);
    }
    advance(1 + length + type_length);
}

# Locals and globals.

handler LocalGet() @opcode(0x20) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let value = locals[index];
    push(value);
    advance(length + 1);
}

handler LocalSet() @opcode(0x21) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let value = pop();
    locals[index] = value;
    advance(length + 1);
}

handler LocalTee() @opcode(0x22) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let value = unary_operand();
    locals[index] = value;
    advance(length + 1);
}

inline fn global_address(index: u64) -> u64 {
    let instance_pointer = instance();
    let mut globals_pointer: u64;
    load64(globals_pointer, [instance_pointer, INSTANCE_GLOBALS]);
    let globals: Sequence<u64> = alias(globals_pointer);
    globals[index]
}

handler GlobalGet() @opcode(0x23) {
    let [index, length] = read_leb(immediates());
    let address = global_address(index);
    let mut value: u64;
    load64(value, [address, 0]);
    push(value);
    advance(length + 1);
}

handler GlobalSet() @opcode(0x24) {
    let [index, length] = read_leb(immediates());
    let address = global_address(index);
    let value = pop();
    store64([address, 0], value);
    advance(length + 1);
}

# Tables.

inline fn table_at(index: u64) -> u64 {
    let instance_pointer = instance();
    let mut tables_pointer: u64;
    load64(tables_pointer, [instance_pointer, INSTANCE_TABLES]);
    let tables: Sequence<u64> = alias(tables_pointer);
    tables[index]
}

inline fn table_element_address(table: u64, index: u64) -> u64 {
    let mut size: u64;
    load64(size, [table, TABLE_SIZE]);
    guard index < size else @cold {
        trap(TRAP_TABLE_OUT_OF_BOUNDS);
    };
    let mut elements: u64;
    load64(elements, [table, TABLE_ELEMENTS]);
    elements + (index << 3)
}

handler TableGet() @opcode(0x25) {
    let [table_index, length] = read_leb(immediates());
    let table = table_at(table_index);
    let index = unary_operand();
    let address = table_element_address(table, index);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length + 1);
}

handler TableSet() @opcode(0x26) {
    let [table_index, length] = read_leb(immediates());
    let table = table_at(table_index);
    let [index, value] = binary_operands();
    drop_slots(1);
    let address = table_element_address(table, index);
    store64([address, 0], value);
    advance(length + 1);
}

# Memory 0. The memory is reserved with guard pages for every address and offset a memory32 access can form, so an
# out-of-bounds access faults, and the runtime's fault handler turns that into a trap. Other memories check bounds.

inline fn memory_at(index: u64) -> u64 {
    let instance_pointer = instance();
    let mut memories_pointer: u64;
    load64(memories_pointer, [instance_pointer, INSTANCE_MEMORIES]);
    let memories: Sequence<u64> = alias(memories_pointer);
    memories[index]
}

# Check that `size` bytes from `address` are in the memory, and return their address.
inline fn checked_memory_address(memory: u64, address: u64, size: u64) -> u64 {
    let mut memory_size: u64;
    load64(memory_size, [memory, MEMORY_SIZE]);
    let end = address + size;
    guard end >= address else @cold {
        trap(TRAP_MEMORY_OUT_OF_BOUNDS);
    };
    guard end <= memory_size else @cold {
        trap(TRAP_MEMORY_OUT_OF_BOUNDS);
    };
    let mut base: u64;
    load64(base, [memory, MEMORY_BASE]);
    base + address
}

# The address of a `size`-byte memory access, and the length of the instruction.
# The address of an access to memory 0, and the length of the instruction. Accesses that cannot rely on memory 0's
# guard pages were quickened into checked forms (see quickening.rs), so the alignment is followed by the offset.
inline fn memory_address(address_operand: u64, size: u64) -> (u64, u64) {
    let current: u64 = ip;
    let offset_bytes: Sequence<u8> = alias(current + 2);
    let [offset, offset_length] = read_leb(offset_bytes);
    let index: u64 = alias(u32(address_operand));
    let base: u64 = memory_base;
    let address = base + index + offset;
    (address, offset_length + 2)
}

# The address of a quickened, bounds-checked access of `size` bytes (`0xFF sub [memory] offset`), and the length of
# its immediates after the sub-opcode.
inline fn checked_access_address(address_operand: u64, size: u64, has_memory_index: u64) -> (u64, u64) {
    let current: u64 = ip;
    let mut memory_index: u64 = 0;
    let mut index_length: u64 = 0;
    if has_memory_index != 0 {
        let index_bytes: Sequence<u8> = alias(current + 2);
        let [decoded_index, decoded_length] = read_leb(index_bytes);
        memory_index = decoded_index;
        index_length = decoded_length;
    }
    let offset_bytes: Sequence<u8> = alias(current + 2 + index_length);
    let [offset, offset_length] = read_leb(offset_bytes);
    let memory = memory_at(memory_index);
    let effective = address_operand + offset;
    guard effective >= address_operand else @cold {
        trap(TRAP_MEMORY_OUT_OF_BOUNDS);
    };
    let address = checked_memory_address(memory, effective, size);
    (address, index_length + offset_length + 2)
}

handler Quickened() @opcode(0xff) {
    let bytes = immediates();
    let sub_opcode: u64 = alias(bytes[0]);
    dispatch_secondary(0xff, sub_opcode);
}

inline fn store_operands() -> (u64, u64) {
    let [address_operand, value] = binary_operands();
    drop_slots(1);
    (address_operand, value)
}


handler I32Load() @opcode(0x28) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 4);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I64Load() @opcode(0x29) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 8);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler F32Load() @opcode(0x2a) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 4);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler F64Load() @opcode(0x2b) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 8);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I32Load8S() @opcode(0x2c) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 1);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler I32Load8U() @opcode(0x2d) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 1);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I32Load16S() @opcode(0x2e) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 2);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler I32Load16U() @opcode(0x2f) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 2);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I64Load8S() @opcode(0x30) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 1);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler I64Load8U() @opcode(0x31) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 1);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I64Load16S() @opcode(0x32) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 2);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler I64Load16U() @opcode(0x33) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 2);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I64Load32S() @opcode(0x34) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 4);
    let mut value: u64;
    load32(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler I64Load32U() @opcode(0x35) {
    let address_operand = unary_operand();
    let [address, length] = memory_address(address_operand, 4);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler I32Store() @opcode(0x36) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 4);
    store32([address, 0], value);
    advance(length);
}

handler I64Store() @opcode(0x37) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 8);
    store64([address, 0], value);
    advance(length);
}

handler F32Store() @opcode(0x38) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 4);
    store32([address, 0], value);
    advance(length);
}

handler F64Store() @opcode(0x39) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 8);
    store64([address, 0], value);
    advance(length);
}

handler I32Store8() @opcode(0x3a) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 1);
    store8([address, 0], value);
    advance(length);
}

handler I32Store16() @opcode(0x3b) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 2);
    store16([address, 0], value);
    advance(length);
}

handler I64Store8() @opcode(0x3c) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 1);
    store8([address, 0], value);
    advance(length);
}

handler I64Store16() @opcode(0x3d) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 2);
    store16([address, 0], value);
    advance(length);
}

handler I64Store32() @opcode(0x3e) {
    let [address_operand, value] = store_operands();
    let [address, length] = memory_address(address_operand, 4);
    store32([address, 0], value);
    advance(length);
}

# Bounds-checked accesses, for other memories and for memory64 (quickening rule 1). The sub-opcode is the original
# opcode, plus 0x40 if a memory index follows.

handler CheckedI32Load() @opcode(0xff, 0x28) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32LoadIndexed() @opcode(0xff, 0x68) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load() @opcode(0xff, 0x29) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64LoadIndexed() @opcode(0xff, 0x69) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedF32Load() @opcode(0xff, 0x2a) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedF32LoadIndexed() @opcode(0xff, 0x6a) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedF64Load() @opcode(0xff, 0x2b) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedF64LoadIndexed() @opcode(0xff, 0x6b) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut value: u64;
    load64(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32Load8S() @opcode(0xff, 0x2c) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler CheckedI32Load8SIndexed() @opcode(0xff, 0x6c) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler CheckedI32Load8U() @opcode(0xff, 0x2d) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32Load8UIndexed() @opcode(0xff, 0x6d) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32Load16S() @opcode(0xff, 0x2e) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler CheckedI32Load16SIndexed() @opcode(0xff, 0x6e) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = u32(value);
    let extended: u64 = alias(narrow);
    set_top(extended);
    advance(length);
}

handler CheckedI32Load16U() @opcode(0xff, 0x2f) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32Load16UIndexed() @opcode(0xff, 0x6f) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load8S() @opcode(0xff, 0x30) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load8SIndexed() @opcode(0xff, 0x70) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let mut value: u64;
    load8s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load8U() @opcode(0xff, 0x31) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load8UIndexed() @opcode(0xff, 0x71) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let mut value: u64;
    load8(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load16S() @opcode(0xff, 0x32) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load16SIndexed() @opcode(0xff, 0x72) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let mut value: u64;
    load16s(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load16U() @opcode(0xff, 0x33) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load16UIndexed() @opcode(0xff, 0x73) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let mut value: u64;
    load16(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load32S() @opcode(0xff, 0x34) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut value: u64;
    load32(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load32SIndexed() @opcode(0xff, 0x74) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut value: u64;
    load32(value, [address, 0]);
    let narrow = i32(value);
    let extended: u64 = alias(i64(narrow));
    set_top(extended);
    advance(length);
}

handler CheckedI64Load32U() @opcode(0xff, 0x35) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI64Load32UIndexed() @opcode(0xff, 0x75) {
    let address_operand = unary_operand();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut value: u64;
    load32(value, [address, 0]);
    set_top(value);
    advance(length);
}

handler CheckedI32Store() @opcode(0xff, 0x36) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    store32([address, 0], value);
    advance(length);
}

handler CheckedI32StoreIndexed() @opcode(0xff, 0x76) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    store32([address, 0], value);
    advance(length);
}

handler CheckedI64Store() @opcode(0xff, 0x37) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    store64([address, 0], value);
    advance(length);
}

handler CheckedI64StoreIndexed() @opcode(0xff, 0x77) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    store64([address, 0], value);
    advance(length);
}

handler CheckedF32Store() @opcode(0xff, 0x38) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    store32([address, 0], value);
    advance(length);
}

handler CheckedF32StoreIndexed() @opcode(0xff, 0x78) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    store32([address, 0], value);
    advance(length);
}

handler CheckedF64Store() @opcode(0xff, 0x39) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    store64([address, 0], value);
    advance(length);
}

handler CheckedF64StoreIndexed() @opcode(0xff, 0x79) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    store64([address, 0], value);
    advance(length);
}

handler CheckedI32Store8() @opcode(0xff, 0x3a) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    store8([address, 0], value);
    advance(length);
}

handler CheckedI32Store8Indexed() @opcode(0xff, 0x7a) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    store8([address, 0], value);
    advance(length);
}

handler CheckedI32Store16() @opcode(0xff, 0x3b) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    store16([address, 0], value);
    advance(length);
}

handler CheckedI32Store16Indexed() @opcode(0xff, 0x7b) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    store16([address, 0], value);
    advance(length);
}

handler CheckedI64Store8() @opcode(0xff, 0x3c) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    store8([address, 0], value);
    advance(length);
}

handler CheckedI64Store8Indexed() @opcode(0xff, 0x7c) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    store8([address, 0], value);
    advance(length);
}

handler CheckedI64Store16() @opcode(0xff, 0x3d) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    store16([address, 0], value);
    advance(length);
}

handler CheckedI64Store16Indexed() @opcode(0xff, 0x7d) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    store16([address, 0], value);
    advance(length);
}

handler CheckedI64Store32() @opcode(0xff, 0x3e) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    store32([address, 0], value);
    advance(length);
}

handler CheckedI64Store32Indexed() @opcode(0xff, 0x7e) {
    let [address_operand, value] = store_operands();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    store32([address, 0], value);
    advance(length);
}

handler MemorySize() @opcode(0x3f) {
    let [memory_index, length] = read_leb(immediates());
    let memory = memory_at(memory_index);
    let mut size: u64;
    load64(size, [memory, MEMORY_SIZE]);
    push(size >> 16);
    advance(length + 1);
}

handler MemoryGrow() @opcode(0x40) {
    let [memory_index, length] = read_leb(immediates());
    let memory = memory_at(memory_index);
    let delta = unary_operand();
    let context_pointer = context();
    store64([context_pointer, CONTEXT_ARGUMENTS], memory);
    store64([context_pointer, CONTEXT_ARGUMENT_1], delta);
    let previous: u64 = call_helper(wasm_runtime_memory_grow, context_pointer);
    # The result is an i64 for a memory64, whose -1 must not be truncated.
    let grown_memory = memory_at(memory_index);
    let mut address64: u64;
    load64(address64, [grown_memory, MEMORY_ADDRESS64]);
    let mut result: u64 = previous;
    if address64 == 0 {
        let narrow = u32(previous);
        result = alias(narrow);
    }
    set_top(result);
    advance(length + 1);
}

handler I32Const() @opcode(0x41) {
    let [value, length] = read_signed_leb(immediates());
    let narrow = u32(value);
    let wide: u64 = alias(narrow);
    push(wide);
    advance(length + 1);
}

handler I64Const() @opcode(0x42) {
    let [value, length] = read_signed_leb(immediates());
    push(value);
    advance(length + 1);
}

# Constants quickened into fixed-width forms (rule 3 in quickening.rs): the value is in the bytes after the opcode,
# sign-extended from their width.

inline fn sign_extended(value: u64, bits: u64) -> u64 {
    let width: u64 = 64;
    let shift = width - bits;
    let moved = value << shift;
    let signed = signed64(moved);
    let extended = signed >> signed64(shift);
    let result: u64 = alias(extended);
    result
}

inline fn push_i32(value: u64) {
    let narrow = u32(value);
    let wide: u64 = alias(narrow);
    push(wide);
}

# A 5- to 7-byte value: its low 4 bytes, and the rest from 4 bytes that end where it does.
inline fn fixed_value_over_4(length: u64) -> u64 {
    let current: u64 = ip;
    let mut low: u64;
    load32(low, [current, 1]);
    let end_word = current + length - 3;
    let mut high: u64;
    load32(high, [end_word, 0]);
    let word: u64 = 8;
    let unused = (word - length) << 3;
    let rest = high >> unused;
    let value = low | (rest << 32);
    sign_extended(value, length << 3)
}

handler I32ConstFixed2() @opcode(0xe0) {
    let current: u64 = ip;
    let mut value: u64;
    load16(value, [current, 1]);
    push_i32(sign_extended(value, 16));
    advance(3);
}

handler I32ConstFixed3() @opcode(0xe1) {
    # The byte after the value belongs to the next instruction, which a function body always has.
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push_i32(sign_extended(value, 24));
    advance(4);
}

handler I32ConstFixed4() @opcode(0xe2) {
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push(value);
    advance(5);
}

handler I32ConstFixed5() @opcode(0xe3) {
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push(value);
    advance(6);
}

handler I64ConstFixed2() @opcode(0xe4) {
    let current: u64 = ip;
    let mut value: u64;
    load16(value, [current, 1]);
    push(sign_extended(value, 16));
    advance(3);
}

handler I64ConstFixed3() @opcode(0xe5) {
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push(sign_extended(value, 24));
    advance(4);
}

handler I64ConstFixed4() @opcode(0xe6) {
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push(sign_extended(value, 32));
    advance(5);
}

handler I64ConstFixed5() @opcode(0xe7) {
    push(fixed_value_over_4(5));
    advance(6);
}

handler I64ConstFixed6() @opcode(0xe8) {
    push(fixed_value_over_4(6));
    advance(7);
}

handler I64ConstFixed7() @opcode(0xe9) {
    push(fixed_value_over_4(7));
    advance(8);
}

handler I64ConstFixed8() @opcode(0xea) {
    let current: u64 = ip;
    let mut value: u64;
    load64(value, [current, 1]);
    push(value);
    advance(9);
}

handler I64ConstFixed9() @opcode(0xeb) {
    let current: u64 = ip;
    let mut value: u64;
    load64(value, [current, 1]);
    push(value);
    advance(10);
}

handler I64ConstFixed10() @opcode(0xec) {
    let current: u64 = ip;
    let mut value: u64;
    load64(value, [current, 1]);
    push(value);
    advance(11);
}

handler F32Const() @opcode(0x43) {
    let current: u64 = ip;
    let mut value: u64;
    load32(value, [current, 1]);
    push(value);
    advance(5);
}

handler F64Const() @opcode(0x44) {
    let current: u64 = ip;
    let mut value: u64;
    load64(value, [current, 1]);
    push(value);
    advance(9);
}


handler I32Eqz() @opcode(0x45) {
    let operand = u32(unary_operand());
    let mut value: u64 = 0;
    if operand == 0 {
        value = 1;
    }
    result(value);
}

handler I32Eq() @opcode(0x46) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a == b {
        value = 1;
    }
    result(value);
}

handler I32Ne() @opcode(0x47) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a != b {
        value = 1;
    }
    result(value);
}

handler I32LtS() @opcode(0x48) {
    let [lhs, rhs] = binary_operands();
    let a = i32(lhs);
    let b = i32(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler I32LtU() @opcode(0x49) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler I32GtS() @opcode(0x4a) {
    let [lhs, rhs] = binary_operands();
    let a = i32(lhs);
    let b = i32(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler I32GtU() @opcode(0x4b) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler I32LeS() @opcode(0x4c) {
    let [lhs, rhs] = binary_operands();
    let a = i32(lhs);
    let b = i32(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler I32LeU() @opcode(0x4d) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler I32GeS() @opcode(0x4e) {
    let [lhs, rhs] = binary_operands();
    let a = i32(lhs);
    let b = i32(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}

handler I32GeU() @opcode(0x4f) {
    let [lhs, rhs] = binary_operands();
    let a = u32(lhs);
    let b = u32(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}

handler I64Eqz() @opcode(0x50) {
    let operand = unsigned64(unary_operand());
    let mut value: u64 = 0;
    if operand == 0 {
        value = 1;
    }
    result(value);
}

handler I64Eq() @opcode(0x51) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a == b {
        value = 1;
    }
    result(value);
}

handler I64Ne() @opcode(0x52) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a != b {
        value = 1;
    }
    result(value);
}

handler I64LtS() @opcode(0x53) {
    let [lhs, rhs] = binary_operands();
    let a = signed64(lhs);
    let b = signed64(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler I64LtU() @opcode(0x54) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler I64GtS() @opcode(0x55) {
    let [lhs, rhs] = binary_operands();
    let a = signed64(lhs);
    let b = signed64(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler I64GtU() @opcode(0x56) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler I64LeS() @opcode(0x57) {
    let [lhs, rhs] = binary_operands();
    let a = signed64(lhs);
    let b = signed64(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler I64LeU() @opcode(0x58) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler I64GeS() @opcode(0x59) {
    let [lhs, rhs] = binary_operands();
    let a = signed64(lhs);
    let b = signed64(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}

handler I64GeU() @opcode(0x5a) {
    let [lhs, rhs] = binary_operands();
    let a = unsigned64(lhs);
    let b = unsigned64(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}

handler F32Eq() @opcode(0x5b) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a == b {
        value = 1;
    }
    result(value);
}

handler F32Ne() @opcode(0x5c) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a != b {
        value = 1;
    }
    result(value);
}

handler F32Lt() @opcode(0x5d) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler F32Gt() @opcode(0x5e) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler F32Le() @opcode(0x5f) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler F32Ge() @opcode(0x60) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}

handler F64Eq() @opcode(0x61) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a == b {
        value = 1;
    }
    result(value);
}

handler F64Ne() @opcode(0x62) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a != b {
        value = 1;
    }
    result(value);
}

handler F64Lt() @opcode(0x63) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a < b {
        value = 1;
    }
    result(value);
}

handler F64Gt() @opcode(0x64) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a > b {
        value = 1;
    }
    result(value);
}

handler F64Le() @opcode(0x65) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a <= b {
        value = 1;
    }
    result(value);
}

handler F64Ge() @opcode(0x66) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = 0;
    if a >= b {
        value = 1;
    }
    result(value);
}
# Floats are kept as their bits. f32 comparisons and roundings go through f64, which holds every f32 exactly.

inline fn f32_value(bits: u64) -> f64 {
    let narrow = u32(bits);
    let single = f32_from_bits(narrow);
    promote_f32(single)
}

inline fn f32_operand(bits: u64) -> f32 {
    let narrow = u32(bits);
    f32_from_bits(narrow)
}

inline fn f64_value(bits: u64) -> f64 {
    f64_from_bits(bits)
}

inline fn f32_result(value: f32) {
    let bits = f32_to_bits(value);
    result32(bits);
}

inline fn f64_result(value: f64) {
    let bits = f64_to_bits(value);
    result(bits);
}


handler I32Clz() @opcode(0x67) {
    let operand = unary32();
    let value = clz32(operand);
    result32(value);
}

handler I32Ctz() @opcode(0x68) {
    let operand = unary32();
    let value = ctz32(operand);
    result32(value);
}

handler I32Popcnt() @opcode(0x69) {
    let operand = unary32();
    let value = popcnt32(operand);
    result32(value);
}

handler I32Add() @opcode(0x6a) {
    let [lhs, rhs] = binary32();
    let value = lhs + rhs;
    result32(value);
}

handler I32Sub() @opcode(0x6b) {
    let [lhs, rhs] = binary32();
    let value = lhs - rhs;
    result32(value);
}

handler I32Mul() @opcode(0x6c) {
    let [lhs, rhs] = binary32();
    let value = lhs * rhs;
    result32(value);
}

handler I32And() @opcode(0x71) {
    let [lhs, rhs] = binary32();
    let value = lhs & rhs;
    result32(value);
}

handler I32Or() @opcode(0x72) {
    let [lhs, rhs] = binary32();
    let value = lhs | rhs;
    result32(value);
}

handler I32Xor() @opcode(0x73) {
    let [lhs, rhs] = binary32();
    let value = lhs ^ rhs;
    result32(value);
}

handler I32DivS() @opcode(0x6d) {
    let [lhs, rhs] = binary32();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    if rhs == 0xffffffff @cold {
        guard lhs != 0x80000000 else @cold {
            trap(TRAP_INTEGER_OVERFLOW);
        };
    }
    let value = div_s32(lhs, rhs);
    result32(value);
}

handler I32DivU() @opcode(0x6e) {
    let [lhs, rhs] = binary32();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    let value = div_u32(lhs, rhs);
    result32(value);
}

handler I32RemS() @opcode(0x6f) {
    let [lhs, rhs] = binary32();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    # The remainder of dividing by -1 is 0, but the division of the smallest value by it would fault.
    if rhs == 0xffffffff @cold {
        result(0);
    }
    let value = rem_s32(lhs, rhs);
    result32(value);
}

handler I32RemU() @opcode(0x70) {
    let [lhs, rhs] = binary32();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    let value = rem_u32(lhs, rhs);
    result32(value);
}

handler I32Shl() @opcode(0x74) {
    let [lhs, rhs] = binary32();
    let value = lhs << (rhs & 31);
    result32(value);
}

handler I32ShrS() @opcode(0x75) {
    let [lhs, rhs] = binary32();
    let narrow = i32(lhs);
    let wide = i64(narrow);
    let count = rhs & 31;
    let signed_count = i64(i32(count));
    let shifted = wide >> signed_count;
    let value = u32(shifted);
    result32(value);
}

handler I32ShrU() @opcode(0x76) {
    let [lhs, rhs] = binary32();
    let value = lhs >> (rhs & 31);
    result32(value);
}

handler I32Rotl() @opcode(0x77) {
    let [lhs, rhs] = binary32();
    let value = rotl32(lhs, rhs);
    result32(value);
}

handler I32Rotr() @opcode(0x78) {
    let [lhs, rhs] = binary32();
    let value = rotr32(lhs, rhs);
    result32(value);
}

handler I64Clz() @opcode(0x79) {
    let operand = unary_operand();
    let value = clz64(operand);
    result(value);
}

handler I64Ctz() @opcode(0x7a) {
    let operand = unary_operand();
    let value = ctz64(operand);
    result(value);
}

handler I64Popcnt() @opcode(0x7b) {
    let operand = unary_operand();
    let value = popcnt64(operand);
    result(value);
}

handler I64Add() @opcode(0x7c) {
    let [lhs, rhs] = binary_operands();
    let value = lhs + rhs;
    result(value);
}

handler I64Sub() @opcode(0x7d) {
    let [lhs, rhs] = binary_operands();
    let value = lhs - rhs;
    result(value);
}

handler I64Mul() @opcode(0x7e) {
    let [lhs, rhs] = binary_operands();
    let value = lhs * rhs;
    result(value);
}

handler I64And() @opcode(0x83) {
    let [lhs, rhs] = binary_operands();
    let value = lhs & rhs;
    result(value);
}

handler I64Or() @opcode(0x84) {
    let [lhs, rhs] = binary_operands();
    let value = lhs | rhs;
    result(value);
}

handler I64Xor() @opcode(0x85) {
    let [lhs, rhs] = binary_operands();
    let value = lhs ^ rhs;
    result(value);
}

handler I64DivS() @opcode(0x7f) {
    let [lhs, rhs] = binary_operands();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    if rhs == 0xffffffffffffffff @cold {
        guard lhs != 0x8000000000000000 else @cold {
            trap(TRAP_INTEGER_OVERFLOW);
        };
    }
    let value = div_s64(lhs, rhs);
    result(value);
}

handler I64DivU() @opcode(0x80) {
    let [lhs, rhs] = binary_operands();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    let value = div_u64(lhs, rhs);
    result(value);
}

handler I64RemS() @opcode(0x81) {
    let [lhs, rhs] = binary_operands();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    if rhs == 0xffffffffffffffff @cold {
        result(0);
    }
    let value = rem_s64(lhs, rhs);
    result(value);
}

handler I64RemU() @opcode(0x82) {
    let [lhs, rhs] = binary_operands();
    guard rhs != 0 else @cold {
        trap(TRAP_INTEGER_DIVIDE_BY_ZERO);
    };
    let value = rem_u64(lhs, rhs);
    result(value);
}

handler I64Shl() @opcode(0x86) {
    let [lhs, rhs] = binary_operands();
    let value = lhs << (rhs & 63);
    result(value);
}

handler I64ShrS() @opcode(0x87) {
    let [lhs, rhs] = binary_operands();
    let signed = signed64(lhs);
    let count = signed64(rhs & 63);
    let shifted = signed >> count;
    let value: u64 = alias(shifted);
    result(value);
}

handler I64ShrU() @opcode(0x88) {
    let [lhs, rhs] = binary_operands();
    let value = lhs >> (rhs & 63);
    result(value);
}

handler I64Rotl() @opcode(0x89) {
    let [lhs, rhs] = binary_operands();
    let value = rotl64(lhs, rhs);
    result(value);
}

handler I64Rotr() @opcode(0x8a) {
    let [lhs, rhs] = binary_operands();
    let value = rotr64(lhs, rhs);
    result(value);
}

handler F32Abs() @opcode(0x8b) {
    let operand = unary32();
    let value = operand & 0x7fffffff;
    result32(value);
}

handler F32Neg() @opcode(0x8c) {
    let operand = unary32();
    let value = operand ^ 0x80000000;
    result32(value);
}

handler F32Ceil() @opcode(0x8d) {
    let operand = f32_value(unary_operand());
    let rounded = ceil_f64(operand);
    let value = demote_f64(rounded);
    f32_result(value);
}

handler F32Floor() @opcode(0x8e) {
    let operand = f32_value(unary_operand());
    let rounded = floor_f64(operand);
    let value = demote_f64(rounded);
    f32_result(value);
}

handler F32Trunc() @opcode(0x8f) {
    let operand = f32_value(unary_operand());
    let rounded = trunc_f64(operand);
    let value = demote_f64(rounded);
    f32_result(value);
}

handler F32Nearest() @opcode(0x90) {
    let operand = f32_value(unary_operand());
    let rounded = nearest_f64(operand);
    let value = demote_f64(rounded);
    f32_result(value);
}

handler F32Sqrt() @opcode(0x91) {
    let operand = f32_operand(unary_operand());
    let value = sqrt_f32(operand);
    f32_result(value);
}

handler F32Add() @opcode(0x92) {
    let [lhs, rhs] = binary_operands();
    let a = f32_operand(lhs);
    let b = f32_operand(rhs);
    let value = add_f32(a, b);
    f32_result(value);
}

handler F32Sub() @opcode(0x93) {
    let [lhs, rhs] = binary_operands();
    let a = f32_operand(lhs);
    let b = f32_operand(rhs);
    let value = sub_f32(a, b);
    f32_result(value);
}

handler F32Mul() @opcode(0x94) {
    let [lhs, rhs] = binary_operands();
    let a = f32_operand(lhs);
    let b = f32_operand(rhs);
    let value = mul_f32(a, b);
    f32_result(value);
}

handler F32Div() @opcode(0x95) {
    let [lhs, rhs] = binary_operands();
    let a = f32_operand(lhs);
    let b = f32_operand(rhs);
    let value = div_f32(a, b);
    f32_result(value);
}

handler F32Min() @opcode(0x96) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = lhs;
    if b < a {
        value = rhs;
    }
    if a == b {
        # Equal values only differ in the sign of zero, which the minimum takes from either operand that has it.
        value = lhs | rhs;
    }
    if a != a @cold {
        let x = f32_operand(lhs);
        let y = f32_operand(rhs);
        let sum = add_f32(x, y);
        let nan_bits = f32_to_bits(sum);
        let wide_nan: u64 = alias(nan_bits);
        value = wide_nan;
    }
    if b != b @cold {
        let x = f32_operand(lhs);
        let y = f32_operand(rhs);
        let sum = add_f32(x, y);
        let nan_bits = f32_to_bits(sum);
        let wide_nan: u64 = alias(nan_bits);
        value = wide_nan;
    }
    result(value);
}

handler F32Max() @opcode(0x97) {
    let [lhs, rhs] = binary_operands();
    let a = f32_value(lhs);
    let b = f32_value(rhs);
    let mut value: u64 = lhs;
    if b > a {
        value = rhs;
    }
    if a == b {
        # Equal values only differ in the sign of zero, which the minimum takes from either operand that has it.
        value = lhs & rhs;
    }
    if a != a @cold {
        let x = f32_operand(lhs);
        let y = f32_operand(rhs);
        let sum = add_f32(x, y);
        let nan_bits = f32_to_bits(sum);
        let wide_nan: u64 = alias(nan_bits);
        value = wide_nan;
    }
    if b != b @cold {
        let x = f32_operand(lhs);
        let y = f32_operand(rhs);
        let sum = add_f32(x, y);
        let nan_bits = f32_to_bits(sum);
        let wide_nan: u64 = alias(nan_bits);
        value = wide_nan;
    }
    result(value);
}

handler F32Copysign() @opcode(0x98) {
    let [lhs, rhs] = binary32();
    let value = (lhs & 0x7fffffff) | (rhs & 0x80000000);
    result32(value);
}

handler F64Abs() @opcode(0x99) {
    let operand = unary_operand();
    let value = operand & 0x7fffffffffffffff;
    result(value);
}

handler F64Neg() @opcode(0x9a) {
    let operand = unary_operand();
    let value = operand ^ 0x8000000000000000;
    result(value);
}

handler F64Ceil() @opcode(0x9b) {
    let operand = f64_value(unary_operand());
    let value = ceil_f64(operand);
    f64_result(value);
}

handler F64Floor() @opcode(0x9c) {
    let operand = f64_value(unary_operand());
    let value = floor_f64(operand);
    f64_result(value);
}

handler F64Trunc() @opcode(0x9d) {
    let operand = f64_value(unary_operand());
    let value = trunc_f64(operand);
    f64_result(value);
}

handler F64Nearest() @opcode(0x9e) {
    let operand = f64_value(unary_operand());
    let value = nearest_f64(operand);
    f64_result(value);
}

handler F64Sqrt() @opcode(0x9f) {
    let operand = f64_value(unary_operand());
    let value = sqrt_f64(operand);
    f64_result(value);
}

handler F64Add() @opcode(0xa0) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let value = a + b;
    f64_result(value);
}

handler F64Sub() @opcode(0xa1) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let value = a - b;
    f64_result(value);
}

handler F64Mul() @opcode(0xa2) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let value = a * b;
    f64_result(value);
}

handler F64Div() @opcode(0xa3) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let value = a / b;
    f64_result(value);
}

handler F64Min() @opcode(0xa4) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = lhs;
    if b < a {
        value = rhs;
    }
    if a == b {
        # Equal values only differ in the sign of zero, which the minimum takes from either operand that has it.
        value = lhs | rhs;
    }
    if a != a @cold {
        let x = f64_value(lhs);
        let y = f64_value(rhs);
        let sum = x + y;
        value = f64_to_bits(sum);
    }
    if b != b @cold {
        let x = f64_value(lhs);
        let y = f64_value(rhs);
        let sum = x + y;
        value = f64_to_bits(sum);
    }
    result(value);
}

handler F64Max() @opcode(0xa5) {
    let [lhs, rhs] = binary_operands();
    let a = f64_value(lhs);
    let b = f64_value(rhs);
    let mut value: u64 = lhs;
    if b > a {
        value = rhs;
    }
    if a == b {
        # Equal values only differ in the sign of zero, which the minimum takes from either operand that has it.
        value = lhs & rhs;
    }
    if a != a @cold {
        let x = f64_value(lhs);
        let y = f64_value(rhs);
        let sum = x + y;
        value = f64_to_bits(sum);
    }
    if b != b @cold {
        let x = f64_value(lhs);
        let y = f64_value(rhs);
        let sum = x + y;
        value = f64_to_bits(sum);
    }
    result(value);
}

handler F64Copysign() @opcode(0xa6) {
    let [lhs, rhs] = binary_operands();
    let value = (lhs & 0x7fffffffffffffff) | (rhs & 0x8000000000000000);
    result(value);
}
# Conversions.

# Truncate a float to an integer, trapping unless the result is in the open interval (lower, upper).
inline fn checked_truncation(value: f64, lower: u64, upper: u64) -> f64 {
    guard value == value else @cold {
        trap(TRAP_INVALID_CONVERSION);
    };
    let lower_bound = f64_from_bits(lower);
    let upper_bound = f64_from_bits(upper);
    guard value > lower_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    guard value < upper_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    value
}

# Truncate a non-negative float below 2^64 to an unsigned 64-bit integer.
inline fn truncate_unsigned64(value: f64) -> u64 {
    let two_to_63 = f64_from_bits(0x43e0000000000000);
    let mut truncated = truncate_f64_to_s64(value);
    if value >= two_to_63 @cold {
        let reduced = value - two_to_63;
        let low = truncate_f64_to_s64(reduced);
        truncated = low ^ 0x8000000000000000;
    }
    truncated
}

# Truncate a float with saturation: NaN becomes zero, and values out of range the nearest bound. The result is in
# range if the value is in the open interval (lower, upper).
inline fn saturating_truncation(value: f64, lower: u64, upper: u64, minimum: u64, maximum: u64, unsigned: u64) -> u64 {
    let mut truncated: u64 = minimum;
    let lower_bound = f64_from_bits(lower);
    if value > lower_bound {
        truncated = maximum;
        let upper_bound = f64_from_bits(upper);
        if value < upper_bound {
            if unsigned != 0 {
                truncated = truncate_unsigned64(value);
            } else {
                truncated = truncate_f64_to_s64(value);
            }
        }
    }
    if value != value {
        truncated = 0;
    }
    truncated
}

handler I32WrapI64() @opcode(0xa7) {
    let value = unary32();
    result32(value);
}

handler I32TruncF32S() @opcode(0xa8) {
    let operand = f32_value(unary_operand());
    let checked = checked_truncation(operand, 0xc1e0000000200000, 0x41e0000000000000);
    let truncated = truncate_f64_to_s64(checked);
    let value = u32(truncated);
    result32(value);
}

handler I32TruncF32U() @opcode(0xa9) {
    let operand = f32_value(unary_operand());
    let checked = checked_truncation(operand, 0xbff0000000000000, 0x41f0000000000000);
    let truncated = truncate_f64_to_s64(checked);
    let value = u32(truncated);
    result32(value);
}

handler I32TruncF64S() @opcode(0xaa) {
    let operand = f64_value(unary_operand());
    let checked = checked_truncation(operand, 0xc1e0000000200000, 0x41e0000000000000);
    let truncated = truncate_f64_to_s64(checked);
    let value = u32(truncated);
    result32(value);
}

handler I32TruncF64U() @opcode(0xab) {
    let operand = f64_value(unary_operand());
    let checked = checked_truncation(operand, 0xbff0000000000000, 0x41f0000000000000);
    let truncated = truncate_f64_to_s64(checked);
    let value = u32(truncated);
    result32(value);
}

handler I64TruncF32S() @opcode(0xae) {
    let operand = f32_value(unary_operand());
    guard operand == operand else @cold {
        trap(TRAP_INVALID_CONVERSION);
    };
    let lower_bound = f64_from_bits(0xc3e0000000000000);
    let upper_bound = f64_from_bits(0x43e0000000000000);
    guard operand >= lower_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    guard operand < upper_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    let value = truncate_f64_to_s64(operand);
    result(value);
}

handler I64TruncF32U() @opcode(0xaf) {
    let operand = f32_value(unary_operand());
    let checked = checked_truncation(operand, 0xbff0000000000000, 0x43f0000000000000);
    let value = truncate_unsigned64(checked);
    result(value);
}

handler I64TruncF64S() @opcode(0xb0) {
    let operand = f64_value(unary_operand());
    guard operand == operand else @cold {
        trap(TRAP_INVALID_CONVERSION);
    };
    let lower_bound = f64_from_bits(0xc3e0000000000000);
    let upper_bound = f64_from_bits(0x43e0000000000000);
    guard operand >= lower_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    guard operand < upper_bound else @cold {
        trap(TRAP_INTEGER_OVERFLOW);
    };
    let value = truncate_f64_to_s64(operand);
    result(value);
}

handler I64TruncF64U() @opcode(0xb1) {
    let operand = f64_value(unary_operand());
    let checked = checked_truncation(operand, 0xbff0000000000000, 0x43f0000000000000);
    let value = truncate_unsigned64(checked);
    result(value);
}

handler I64ExtendI32S() @opcode(0xac) {
    let operand = unary32();
    let narrow = i32(operand);
    let value: u64 = alias(i64(narrow));
    result(value);
}

handler I64ExtendI32U() @opcode(0xad) {
    let operand = unary32();
    let value: u64 = alias(operand);
    result(value);
}

handler F32ConvertI32S() @opcode(0xb2) {
    let operand = unary32();
    let narrow = i32(operand);
    let wide: u64 = alias(i64(narrow));
    let value = s64_to_f32(wide);
    f32_result(value);
}

handler F32ConvertI32U() @opcode(0xb3) {
    let operand = unary32();
    let wide: u64 = alias(operand);
    let value = s64_to_f32(wide);
    f32_result(value);
}

handler F32ConvertI64S() @opcode(0xb4) {
    let operand = unary_operand();
    let value = s64_to_f32(operand);
    f32_result(value);
}

handler F32ConvertI64U() @opcode(0xb5) {
    let operand = unary_operand();
    let mut value = s64_to_f32(operand);
    if signed64(operand) < 0 @cold {
        # Halve the value, keeping the lowest bit so that rounding stays correct, and double the result.
        let half = (operand >> 1) | (operand & 1);
        let half_value = s64_to_f32(half);
        value = add_f32(half_value, half_value);
    }
    f32_result(value);
}

handler F64ConvertI32S() @opcode(0xb7) {
    let operand = unary32();
    let narrow = i32(operand);
    let wide: u64 = alias(i64(narrow));
    let value = s64_to_f64(wide);
    f64_result(value);
}

handler F64ConvertI32U() @opcode(0xb8) {
    let operand = unary32();
    let wide: u64 = alias(operand);
    let value = s64_to_f64(wide);
    f64_result(value);
}

handler F64ConvertI64S() @opcode(0xb9) {
    let operand = unary_operand();
    let value = s64_to_f64(operand);
    f64_result(value);
}

handler F64ConvertI64U() @opcode(0xba) {
    let operand = unary_operand();
    let mut value = s64_to_f64(operand);
    if signed64(operand) < 0 @cold {
        # Halve the value, keeping the lowest bit so that rounding stays correct, and double the result.
        let half = (operand >> 1) | (operand & 1);
        let half_value = s64_to_f64(half);
        value = half_value + half_value;
    }
    f64_result(value);
}

handler F32DemoteF64() @opcode(0xb6) {
    let operand = f64_value(unary_operand());
    let value = demote_f64(operand);
    f32_result(value);
}

handler F64PromoteF32() @opcode(0xbb) {
    let operand = f32_operand(unary_operand());
    let value = promote_f32(operand);
    f64_result(value);
}

handler I32ReinterpretF32() @opcode(0xbc) {
    dispatch_next;
}

handler I64ReinterpretF64() @opcode(0xbd) {
    dispatch_next;
}

handler F32ReinterpretI32() @opcode(0xbe) {
    dispatch_next;
}

handler F64ReinterpretI64() @opcode(0xbf) {
    dispatch_next;
}

handler I32Extend8S() @opcode(0xc0) {
    let operand = unary_operand();
    let moved = operand << 56;
    let signed = signed64(moved);
    let extended = signed >> 56;
    let value = u32(extended);
    result32(value);
}

handler I32Extend16S() @opcode(0xc1) {
    let operand = unary_operand();
    let moved = operand << 48;
    let signed = signed64(moved);
    let extended = signed >> 48;
    let value = u32(extended);
    result32(value);
}

handler I64Extend8S() @opcode(0xc2) {
    let operand = unary_operand();
    let moved = operand << 56;
    let signed = signed64(moved);
    let extended = signed >> 56;
    let value: u64 = alias(extended);
    result(value);
}

handler I64Extend16S() @opcode(0xc3) {
    let operand = unary_operand();
    let moved = operand << 48;
    let signed = signed64(moved);
    let extended = signed >> 48;
    let value: u64 = alias(extended);
    result(value);
}

handler I64Extend32S() @opcode(0xc4) {
    let operand = unary_operand();
    let moved = operand << 32;
    let signed = signed64(moved);
    let extended = signed >> 32;
    let value: u64 = alias(extended);
    result(value);
}

# References. A reference is the address of what it refers to, or zero for null.

handler RefNull() @opcode(0xd0) {
    let length = leb_length(immediates());
    push(0);
    advance(length + 1);
}

handler RefIsNull() @opcode(0xd1) {
    let operand = unary_operand();
    let mut value: u64 = 0;
    if operand == 0 {
        value = 1;
    }
    result(value);
}

handler RefFunc() @opcode(0xd2) {
    let [function_index, length] = read_leb(immediates());
    let function = function_at(function_index);
    push(function);
    advance(length + 1);
}

handler RefEq() @opcode(0xd3) {
    let [lhs, rhs] = binary_operands();
    let mut value: u64 = 0;
    if lhs == rhs {
        value = 1;
    }
    result(value);
}

handler RefAsNonNull() @opcode(0xd4) {
    let operand = unary_operand();
    guard operand != 0 else @cold {
        trap(TRAP_NULL_REFERENCE);
    };
    dispatch_next;
}

handler BrOnNull() @opcode(0xd5) {
    let operand = unary_operand();
    if operand == 0 {
        drop_slots(1);
        branch();
    }
    let length = leb_length(immediates());
    skip_entry_and_advance(length + 1);
}

handler BrOnNonNull() @opcode(0xd6) {
    let operand = unary_operand();
    if operand != 0 {
        branch();
    }
    drop_slots(1);
    let length = leb_length(immediates());
    skip_entry_and_advance(length + 1);
}

# The 0xFC prefix: saturating truncations, bulk memory, and table instructions.

handler PrefixFC() @opcode(0xfc) {
    let [sub_opcode, _] = read_leb(immediates());
    dispatch_secondary(0xfc, sub_opcode);
}

inline fn prefixed_advance(offset: u64) {
    advance(offset);
}

handler I32TruncSatF32S() @opcode(0xfc, 0) {
    let [_, offset] = prefixed_immediates();
    let operand = f32_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let narrow = u32(truncated);
    let value: u64 = alias(narrow);
    set_top(value);
    advance(offset);
}

handler I32TruncSatF32U() @opcode(0xfc, 1) {
    let [_, offset] = prefixed_immediates();
    let operand = f32_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let narrow = u32(truncated);
    let value: u64 = alias(narrow);
    set_top(value);
    advance(offset);
}

handler I32TruncSatF64S() @opcode(0xfc, 2) {
    let [_, offset] = prefixed_immediates();
    let operand = f64_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let narrow = u32(truncated);
    let value: u64 = alias(narrow);
    set_top(value);
    advance(offset);
}

handler I32TruncSatF64U() @opcode(0xfc, 3) {
    let [_, offset] = prefixed_immediates();
    let operand = f64_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let narrow = u32(truncated);
    let value: u64 = alias(narrow);
    set_top(value);
    advance(offset);
}

handler I64TruncSatF32S() @opcode(0xfc, 4) {
    let [_, offset] = prefixed_immediates();
    let operand = f32_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xc3e0000000000001, 0x43e0000000000000, 0x8000000000000000, 0x7fffffffffffffff, 0);
    let value = truncated;
    set_top(value);
    advance(offset);
}

handler I64TruncSatF32U() @opcode(0xfc, 5) {
    let [_, offset] = prefixed_immediates();
    let operand = f32_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xbff0000000000000, 0x43f0000000000000, 0, 0xffffffffffffffff, 1);
    let value = truncated;
    set_top(value);
    advance(offset);
}

handler I64TruncSatF64S() @opcode(0xfc, 6) {
    let [_, offset] = prefixed_immediates();
    let operand = f64_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xc3e0000000000001, 0x43e0000000000000, 0x8000000000000000, 0x7fffffffffffffff, 0);
    let value = truncated;
    set_top(value);
    advance(offset);
}

handler I64TruncSatF64U() @opcode(0xfc, 7) {
    let [_, offset] = prefixed_immediates();
    let operand = f64_value(unary_operand());
    let truncated = saturating_truncation(operand, 0xbff0000000000000, 0x43f0000000000000, 0, 0xffffffffffffffff, 1);
    let value = truncated;
    set_top(value);
    advance(offset);
}
# Bulk memory and tables. The operands are checked against the bounds before anything is written, so these do not
# rely on guard pages.

inline fn three_operands() -> (u64, u64, u64) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let third = slots[0];
    let second = slots[1];
    let first = slots[2];
    let next = top + 24;
    vsp = next;
    (first, second, third)
}

# Trap unless `count` items from `start` are within `size`. The operands of memory64 and table64 operations are 64-bit,
# so the end can wrap around.
inline fn check_range(start: u64, count: u64, size: u64, code: u64) {
    let end = start + count;
    guard end >= start else @cold {
        trap(code);
    };
    guard end <= size else @cold {
        trap(code);
    };
}

inline fn segment_at(segments_offset: u64, index: u64) -> u64 {
    let instance_pointer = instance();
    let mut segments: u64;
    load64(segments, [instance_pointer, segments_offset]);
    segments + (index << 4)
}

# Copy `count` bytes from `source` to `destination`, which may overlap.
inline fn copy_bytes(destination: u64, source: u64, count: u64) {
    let to: Sequence<u8> = alias(destination);
    let from: Sequence<u8> = alias(source);
    let mut index: u64 = 0;
    if destination <= source {
        while index < count {
            let byte = from[index];
            to[index] = byte;
            index = index + 1;
        }
    } else {
        index = count;
        while index != 0 {
            index = index - 1;
            let byte = from[index];
            to[index] = byte;
        }
    }
}

# Copy `count` table elements from `source` to `destination`, which may overlap.
inline fn copy_elements(destination: u64, source: u64, count: u64) {
    let to: Sequence<u64> = alias(destination);
    let from: Sequence<u64> = alias(source);
    let mut index: u64 = 0;
    if destination <= source {
        while index < count {
            to[index] = from[index];
            index = index + 1;
        }
    } else {
        index = count;
        while index != 0 {
            index = index - 1;
            to[index] = from[index];
        }
    }
}

handler MemoryInit() @opcode(0xfc, 8) {
    let [bytes, offset] = prefixed_immediates();
    let [data_index, data_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let memory_bytes: Sequence<u8> = alias(start + data_length);
    let [memory_index, memory_length] = read_leb(memory_bytes);
    let [destination, source, count] = three_operands();
    let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    check_range(source, count, segment_length, TRAP_MEMORY_OUT_OF_BOUNDS);
    let memory = memory_at(memory_index);
    let mut memory_size: u64;
    load64(memory_size, [memory, MEMORY_SIZE]);
    check_range(destination, count, memory_size, TRAP_MEMORY_OUT_OF_BOUNDS);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    let mut base: u64;
    load64(base, [memory, MEMORY_BASE]);
    copy_bytes(base + destination, contents + source, count);
    advance(offset + data_length + memory_length);
}

handler DataDrop() @opcode(0xfc, 9) {
    let [bytes, offset] = prefixed_immediates();
    let [data_index, data_length] = read_leb(bytes);
    let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);
    store64([segment, SEGMENT_LENGTH], 0);
    advance(offset + data_length);
}

handler MemoryCopy() @opcode(0xfc, 10) {
    let [bytes, offset] = prefixed_immediates();
    let [destination_index, destination_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let source_bytes: Sequence<u8> = alias(start + destination_length);
    let [source_index, source_length] = read_leb(source_bytes);
    let [destination, source, count] = three_operands();
    let destination_memory = memory_at(destination_index);
    let source_memory = memory_at(source_index);
    let mut destination_size: u64;
    load64(destination_size, [destination_memory, MEMORY_SIZE]);
    check_range(destination, count, destination_size, TRAP_MEMORY_OUT_OF_BOUNDS);
    let mut source_size: u64;
    load64(source_size, [source_memory, MEMORY_SIZE]);
    check_range(source, count, source_size, TRAP_MEMORY_OUT_OF_BOUNDS);
    let mut destination_base: u64;
    load64(destination_base, [destination_memory, MEMORY_BASE]);
    let mut source_base: u64;
    load64(source_base, [source_memory, MEMORY_BASE]);
    copy_bytes(destination_base + destination, source_base + source, count);
    advance(offset + destination_length + source_length);
}

handler MemoryFill() @opcode(0xfc, 11) {
    let [bytes, offset] = prefixed_immediates();
    let [memory_index, memory_length] = read_leb(bytes);
    let [destination, value, count] = three_operands();
    let memory = memory_at(memory_index);
    let mut memory_size: u64;
    load64(memory_size, [memory, MEMORY_SIZE]);
    check_range(destination, count, memory_size, TRAP_MEMORY_OUT_OF_BOUNDS);
    let mut base: u64;
    load64(base, [memory, MEMORY_BASE]);
    let start = base + destination;
    let end = start + count;
    let mut address: u64 = start;
    while address < end {
        store8([address, 0], value);
        address = address + 1;
    }
    advance(offset + memory_length);
}

handler TableInit() @opcode(0xfc, 12) {
    let [bytes, offset] = prefixed_immediates();
    let [element_index, element_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let table_bytes: Sequence<u8> = alias(start + element_length);
    let [table_index, table_length] = read_leb(table_bytes);
    let [destination, source, count] = three_operands();
    let segment = segment_at(INSTANCE_ELEMENT_SEGMENTS, element_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    check_range(source, count, segment_length, TRAP_TABLE_OUT_OF_BOUNDS);
    let table = table_at(table_index);
    let mut table_size: u64;
    load64(table_size, [table, TABLE_SIZE]);
    check_range(destination, count, table_size, TRAP_TABLE_OUT_OF_BOUNDS);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    let mut elements: u64;
    load64(elements, [table, TABLE_ELEMENTS]);
    copy_elements(elements + (destination << 3), contents + (source << 3), count);
    advance(offset + element_length + table_length);
}

handler ElemDrop() @opcode(0xfc, 13) {
    let [bytes, offset] = prefixed_immediates();
    let [element_index, element_length] = read_leb(bytes);
    let segment = segment_at(INSTANCE_ELEMENT_SEGMENTS, element_index);
    store64([segment, SEGMENT_LENGTH], 0);
    advance(offset + element_length);
}

handler TableCopy() @opcode(0xfc, 14) {
    let [bytes, offset] = prefixed_immediates();
    let [destination_index, destination_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let source_bytes: Sequence<u8> = alias(start + destination_length);
    let [source_index, source_length] = read_leb(source_bytes);
    let [destination, source, count] = three_operands();
    let destination_table = table_at(destination_index);
    let source_table = table_at(source_index);
    let mut destination_size: u64;
    load64(destination_size, [destination_table, TABLE_SIZE]);
    check_range(destination, count, destination_size, TRAP_TABLE_OUT_OF_BOUNDS);
    let mut source_size: u64;
    load64(source_size, [source_table, TABLE_SIZE]);
    check_range(source, count, source_size, TRAP_TABLE_OUT_OF_BOUNDS);
    let mut destination_elements: u64;
    load64(destination_elements, [destination_table, TABLE_ELEMENTS]);
    let mut source_elements: u64;
    load64(source_elements, [source_table, TABLE_ELEMENTS]);
    copy_elements(destination_elements + (destination << 3), source_elements + (source << 3), count);
    advance(offset + destination_length + source_length);
}

handler TableGrow() @opcode(0xfc, 15) {
    let [bytes, offset] = prefixed_immediates();
    let [table_index, table_length] = read_leb(bytes);
    let [initial, delta] = binary_operands();
    let table = table_at(table_index);
    let context_pointer = context();
    store64([context_pointer, CONTEXT_ARGUMENTS], table);
    store64([context_pointer, CONTEXT_ARGUMENT_1], delta);
    store64([context_pointer, CONTEXT_ARGUMENT_2], initial);
    let previous: u64 = call_helper(wasm_runtime_table_grow, context_pointer);
    # The result is an i64 for a table64, whose -1 must not be truncated.
    let grown_table = table_at(table_index);
    let mut address64: u64;
    load64(address64, [grown_table, TABLE_ADDRESS64]);
    let mut result: u64 = previous;
    if address64 == 0 {
        let narrow = u32(previous);
        result = alias(narrow);
    }
    set_top(result);
    advance(offset + table_length);
}

handler TableSize() @opcode(0xfc, 16) {
    let [bytes, offset] = prefixed_immediates();
    let [table_index, table_length] = read_leb(bytes);
    let table = table_at(table_index);
    let mut size: u64;
    load64(size, [table, TABLE_SIZE]);
    push(size);
    advance(offset + table_length);
}

handler TableFill() @opcode(0xfc, 17) {
    let [bytes, offset] = prefixed_immediates();
    let [table_index, table_length] = read_leb(bytes);
    let [destination, value, count] = three_operands();
    let table = table_at(table_index);
    let mut table_size: u64;
    load64(table_size, [table, TABLE_SIZE]);
    check_range(destination, count, table_size, TRAP_TABLE_OUT_OF_BOUNDS);
    let mut elements_pointer: u64;
    load64(elements_pointer, [table, TABLE_ELEMENTS]);
    let elements: Sequence<u64> = alias(elements_pointer + (destination << 3));
    let mut index: u64 = 0;
    while index < count {
        elements[index] = value;
        index = index + 1;
    }
    advance(offset + table_length);
}
# SIMD (the 0xFD prefix). A v128 takes two slots on the operand stack, its low half in the lower one, so a vector on
# top of the stack is a 16-byte value at vsp. Lane operations that have no vector instruction work on that memory
# directly.

handler PrefixFD() @opcode(0xfd) {
    let [sub_opcode, _] = read_leb(immediates());
    dispatch_secondary(0xfd, sub_opcode);
}

inline fn top_vector() -> v128 {
    let top: u64 = vsp;
    let mut value: v128;
    load128(value, [top, 0]);
    value
}

inline fn set_top_vector(value: v128) {
    let top: u64 = vsp;
    store128([top, 0], value);
}

# The two vector operands of a binary instruction, whose result replaces the first.
inline fn binary_vectors() -> (v128, v128) {
    let top: u64 = vsp;
    let mut rhs: v128;
    load128(rhs, [top, 0]);
    let mut lhs: v128;
    load128(lhs, [top, 16]);
    let next = top + 16;
    vsp = next;
    (lhs, rhs)
}

inline fn vector_result(value: v128, offset: u64) {
    set_top_vector(value);
    advance(offset);
}

inline fn splat_u64(value: u64) -> v128 {
    v128_from_u64x2(value, value)
}

inline fn splat_i8(value: u64) -> v128 {
    let byte = value & 0xff;
    let wide = byte * 0x0101010101010101;
    splat_u64(wide)
}

inline fn splat_i16(value: u64) -> v128 {
    let half = value & 0xffff;
    let wide = half * 0x0001000100010001;
    splat_u64(wide)
}

inline fn splat_i32(value: u64) -> v128 {
    let word = value & 0xffffffff;
    let wide = word * 0x0000000100000001;
    splat_u64(wide)
}

inline fn zero_vector() -> v128 {
    v128_from_u64x2(0, 0)
}

inline fn ones_vector() -> v128 {
    v128_from_u64x2(0xffffffffffffffff, 0xffffffffffffffff)
}

inline fn not_vector(value: v128) -> v128 {
    v128_xor(value, ones_vector())
}

# The shift count of a vector shift, modulo the lane width, as a vector whose low 64 bits hold it.
inline fn shift_count(mask: u64) -> v128 {
    let count = pop();
    let masked = count & mask;
    v128_from_u64x2(masked, 0)
}

# Replace the vector on top of the stack with two 64-bit halves.
inline fn set_top_halves(low: u64, high: u64) {
    let top: u64 = vsp;
    store64([top, 0], low);
    store64([top, 8], high);
}

# Push a vector given as two 64-bit halves, replacing `popped` slots.
inline fn replace_with_halves(popped: u64, low: u64, high: u64) {
    let top: u64 = vsp;
    let next = top + (popped << 3) - 16;
    vsp = next;
    store64([next, 0], low);
    store64([next, 8], high);
}

# The address of a SIMD memory access to memory 0, whose memory argument starts `offset` bytes after the instruction,
# and the length of the instruction.
inline fn simd_memory_address(address_operand: u64, offset: u64) -> (u64, u64) {
    let current: u64 = ip;
    let alignment_bytes: Sequence<u8> = alias(current + offset);
    let alignment_length = leb_length(alignment_bytes);
    let offset_bytes: Sequence<u8> = alias(current + offset + alignment_length);
    let [displacement, displacement_length] = read_leb(offset_bytes);
    let index: u64 = alias(u32(address_operand));
    let base: u64 = memory_base;
    let address = base + index + displacement;
    (address, offset + alignment_length + displacement_length)
}

handler V128Load() @opcode(0xfd, 0x0) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut value: v128;
    load128(value, [address, 0]);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load8x8S() @opcode(0xfd, 0x1) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load8x8U() @opcode(0xfd, 0x2) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load16x4S() @opcode(0xfd, 0x3) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load16x4U() @opcode(0xfd, 0x4) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load32x2S() @opcode(0xfd, 0x5) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load32x2U() @opcode(0xfd, 0x6) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler V128Load8Splat() @opcode(0xfd, 0x7) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load8(lane, [address, 0]);
    let wide = lane * 0x0101010101010101;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler V128Load16Splat() @opcode(0xfd, 0x8) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load16(lane, [address, 0]);
    let wide = lane * 0x0001000100010001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler V128Load32Splat() @opcode(0xfd, 0x9) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load32(lane, [address, 0]);
    let wide = lane * 0x0000000100000001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler V128Load64Splat() @opcode(0xfd, 0xa) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load64(lane, [address, 0]);
    let wide = lane;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler V128Load32Zero() @opcode(0xfd, 0x5c) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load32(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler V128Load64Zero() @opcode(0xfd, 0x5d) {
    let [_, offset] = prefixed_immediates();
    let address_operand = pop();
    let [address, length] = simd_memory_address(address_operand, offset);
    let mut lane: u64;
    load64(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler V128Store() @opcode(0xfd, 0xb) {
    let [_, offset] = prefixed_immediates();
    let value = top_vector();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let next = top + 24;
    vsp = next;
    let [address, length] = simd_memory_address(address_operand, offset);
    store128([address, 0], value);
    advance(length);
}

handler V128Load8Lane() @opcode(0xfd, 0x54) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load8(value, [address, 0]);
    let lane_address = top + (lane * 1);
    store8([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler V128Load16Lane() @opcode(0xfd, 0x55) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load16(value, [address, 0]);
    let lane_address = top + (lane * 2);
    store16([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler V128Load32Lane() @opcode(0xfd, 0x56) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load32(value, [address, 0]);
    let lane_address = top + (lane * 4);
    store32([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler V128Load64Lane() @opcode(0xfd, 0x57) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load64(value, [address, 0]);
    let lane_address = top + (lane * 8);
    store64([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler V128Store8Lane() @opcode(0xfd, 0x58) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 1);
    let mut value: u64;
    load8(value, [lane_address, 0]);
    store8([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler V128Store16Lane() @opcode(0xfd, 0x59) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 2);
    let mut value: u64;
    load16(value, [lane_address, 0]);
    store16([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler V128Store32Lane() @opcode(0xfd, 0x5a) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 4);
    let mut value: u64;
    load32(value, [lane_address, 0]);
    store32([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler V128Store64Lane() @opcode(0xfd, 0x5b) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = simd_memory_address(address_operand, offset);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 8);
    let mut value: u64;
    load64(value, [lane_address, 0]);
    store64([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler V128Const() @opcode(0xfd, 0xc) {
    let [bytes, offset] = prefixed_immediates();
    let start: u64 = alias(bytes);
    let mut low: u64;
    load64(low, [start, 0]);
    let mut high: u64;
    load64(high, [start, 8]);
    replace_with_halves(0, low, high);
    advance(offset + 16);
}

handler I8x16Shuffle() @opcode(0xfd, 0xd) {
    let [bytes, offset] = prefixed_immediates();
    let start: u64 = alias(bytes);
    let mut mask: v128;
    load128(mask, [start, 0]);
    let [a, b] = binary_vectors();
    # Lanes 0 to 15 pick from a and 16 to 31 from b; a selector with its top bit set picks zero.
    let from_b = v128_gt_s_i8(mask, splat_i8(15));
    let mask_a = v128_or(mask, from_b);
    let mask_b = v128_sub_i8(mask, splat_i8(16));
    let picked_a = v128_shuffle_i8(a, mask_a);
    let picked_b = v128_shuffle_i8(b, mask_b);
    let value = v128_or(picked_a, picked_b);
    vector_result(value, offset + 16);
}

handler I8x16Swizzle() @opcode(0xfd, 0xe) {
    let [_, offset] = prefixed_immediates();
    let [a, selectors] = binary_vectors();
    # Selectors of 16 and more pick zero, which they do once their top bit is set.
    let mask = v128_add_sat_u_i8(selectors, splat_i8(0x70));
    let value = v128_shuffle_i8(a, mask);
    vector_result(value, offset);
}

handler I8x16RelaxedSwizzle() @opcode(0xfd, 0x100) {
    let [_, offset] = prefixed_immediates();
    let [a, selectors] = binary_vectors();
    # Selectors of 16 and more pick zero, which they do once their top bit is set.
    let mask = v128_add_sat_u_i8(selectors, splat_i8(0x70));
    let value = v128_shuffle_i8(a, mask);
    vector_result(value, offset);
}

handler I8x16Splat() @opcode(0xfd, 0xf) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let masked = lane & 0xff;
    let wide = masked * 0x0101010101010101;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler I16x8Splat() @opcode(0xfd, 0x10) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let masked = lane & 0xffff;
    let wide = masked * 0x0001000100010001;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler I32x4Splat() @opcode(0xfd, 0x11) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let masked = lane & 0xffffffff;
    let wide = masked * 0x0000000100000001;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler I64x2Splat() @opcode(0xfd, 0x12) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let wide = lane;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler F32x4Splat() @opcode(0xfd, 0x13) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let masked = lane & 0xffffffff;
    let wide = masked * 0x0000000100000001;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler F64x2Splat() @opcode(0xfd, 0x14) {
    let [_, offset] = prefixed_immediates();
    let lane = unary_operand();
    let wide = lane;
    replace_with_halves(1, wide, wide);
    advance(offset);
}

handler I8x16ExtractLaneS() @opcode(0xfd, 0x15) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 1);
    let mut value: u64;
    load8s(value, [lane_address, 0]);
    let narrow = u32(value);
    let result: u64 = alias(narrow);
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I8x16ExtractLaneU() @opcode(0xfd, 0x16) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 1);
    let mut value: u64;
    load8(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I16x8ExtractLaneS() @opcode(0xfd, 0x18) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 2);
    let mut value: u64;
    load16s(value, [lane_address, 0]);
    let narrow = u32(value);
    let result: u64 = alias(narrow);
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I16x8ExtractLaneU() @opcode(0xfd, 0x19) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 2);
    let mut value: u64;
    load16(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I32x4ExtractLane() @opcode(0xfd, 0x1b) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 4);
    let mut value: u64;
    load32(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I64x2ExtractLane() @opcode(0xfd, 0x1d) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 8);
    let mut value: u64;
    load64(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler F32x4ExtractLane() @opcode(0xfd, 0x1f) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 4);
    let mut value: u64;
    load32(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler F64x2ExtractLane() @opcode(0xfd, 0x21) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let top: u64 = vsp;
    let lane_address = top + (lane * 8);
    let mut value: u64;
    load64(value, [lane_address, 0]);
    let result = value;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset + 1);
}

handler I8x16ReplaceLane() @opcode(0xfd, 0x17) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 1);
    store8([lane_address, 0], value);
    advance(offset + 1);
}

handler I16x8ReplaceLane() @opcode(0xfd, 0x1a) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 2);
    store16([lane_address, 0], value);
    advance(offset + 1);
}

handler I32x4ReplaceLane() @opcode(0xfd, 0x1c) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 4);
    store32([lane_address, 0], value);
    advance(offset + 1);
}

handler I64x2ReplaceLane() @opcode(0xfd, 0x1e) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 8);
    store64([lane_address, 0], value);
    advance(offset + 1);
}

handler F32x4ReplaceLane() @opcode(0xfd, 0x20) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 4);
    store32([lane_address, 0], value);
    advance(offset + 1);
}

handler F64x2ReplaceLane() @opcode(0xfd, 0x22) {
    let [bytes, offset] = prefixed_immediates();
    let lane: u64 = alias(bytes[0]);
    let value = pop();
    let top: u64 = vsp;
    let lane_address = top + (lane * 8);
    store64([lane_address, 0], value);
    advance(offset + 1);
}

handler I8x16Eq() @opcode(0xfd, 0x23) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i8(a, b);
    vector_result(value, offset);
}

handler I8x16Ne() @opcode(0xfd, 0x24) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i8(a, b));
    vector_result(value, offset);
}

handler I8x16LtS() @opcode(0xfd, 0x25) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i8(b, a);
    vector_result(value, offset);
}

handler I8x16LtU() @opcode(0xfd, 0x26) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i8(v128_max_u_i8(a, b), a));
    vector_result(value, offset);
}

handler I8x16GtS() @opcode(0xfd, 0x27) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i8(a, b);
    vector_result(value, offset);
}

handler I8x16GtU() @opcode(0xfd, 0x28) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i8(v128_min_u_i8(a, b), a));
    vector_result(value, offset);
}

handler I8x16LeS() @opcode(0xfd, 0x29) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i8(v128_min_s_i8(a, b), a);
    vector_result(value, offset);
}

handler I8x16LeU() @opcode(0xfd, 0x2a) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i8(v128_min_u_i8(a, b), a);
    vector_result(value, offset);
}

handler I8x16GeS() @opcode(0xfd, 0x2b) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i8(v128_max_s_i8(a, b), a);
    vector_result(value, offset);
}

handler I8x16GeU() @opcode(0xfd, 0x2c) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i8(v128_max_u_i8(a, b), a);
    vector_result(value, offset);
}

handler I16x8Eq() @opcode(0xfd, 0x2d) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i16(a, b);
    vector_result(value, offset);
}

handler I16x8Ne() @opcode(0xfd, 0x2e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i16(a, b));
    vector_result(value, offset);
}

handler I16x8LtS() @opcode(0xfd, 0x2f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i16(b, a);
    vector_result(value, offset);
}

handler I16x8LtU() @opcode(0xfd, 0x30) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i16(v128_max_u_i16(a, b), a));
    vector_result(value, offset);
}

handler I16x8GtS() @opcode(0xfd, 0x31) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i16(a, b);
    vector_result(value, offset);
}

handler I16x8GtU() @opcode(0xfd, 0x32) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i16(v128_min_u_i16(a, b), a));
    vector_result(value, offset);
}

handler I16x8LeS() @opcode(0xfd, 0x33) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i16(v128_min_s_i16(a, b), a);
    vector_result(value, offset);
}

handler I16x8LeU() @opcode(0xfd, 0x34) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i16(v128_min_u_i16(a, b), a);
    vector_result(value, offset);
}

handler I16x8GeS() @opcode(0xfd, 0x35) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i16(v128_max_s_i16(a, b), a);
    vector_result(value, offset);
}

handler I16x8GeU() @opcode(0xfd, 0x36) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i16(v128_max_u_i16(a, b), a);
    vector_result(value, offset);
}

handler I32x4Eq() @opcode(0xfd, 0x37) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i32(a, b);
    vector_result(value, offset);
}

handler I32x4Ne() @opcode(0xfd, 0x38) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i32(a, b));
    vector_result(value, offset);
}

handler I32x4LtS() @opcode(0xfd, 0x39) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i32(b, a);
    vector_result(value, offset);
}

handler I32x4LtU() @opcode(0xfd, 0x3a) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i32(v128_max_u_i32(a, b), a));
    vector_result(value, offset);
}

handler I32x4GtS() @opcode(0xfd, 0x3b) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i32(a, b);
    vector_result(value, offset);
}

handler I32x4GtU() @opcode(0xfd, 0x3c) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i32(v128_min_u_i32(a, b), a));
    vector_result(value, offset);
}

handler I32x4LeS() @opcode(0xfd, 0x3d) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i32(v128_min_s_i32(a, b), a);
    vector_result(value, offset);
}

handler I32x4LeU() @opcode(0xfd, 0x3e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i32(v128_min_u_i32(a, b), a);
    vector_result(value, offset);
}

handler I32x4GeS() @opcode(0xfd, 0x3f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i32(v128_max_s_i32(a, b), a);
    vector_result(value, offset);
}

handler I32x4GeU() @opcode(0xfd, 0x40) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i32(v128_max_u_i32(a, b), a);
    vector_result(value, offset);
}

handler F32x4Eq() @opcode(0xfd, 0x41) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Ne() @opcode(0xfd, 0x42) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_ne_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Lt() @opcode(0xfd, 0x43) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_lt_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Gt() @opcode(0xfd, 0x44) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_lt_f32(b, a);
    vector_result(value, offset);
}

handler F32x4Le() @opcode(0xfd, 0x45) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_le_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Ge() @opcode(0xfd, 0x46) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_le_f32(b, a);
    vector_result(value, offset);
}

handler F64x2Eq() @opcode(0xfd, 0x47) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Ne() @opcode(0xfd, 0x48) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_ne_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Lt() @opcode(0xfd, 0x49) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_lt_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Gt() @opcode(0xfd, 0x4a) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_lt_f64(b, a);
    vector_result(value, offset);
}

handler F64x2Le() @opcode(0xfd, 0x4b) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_le_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Ge() @opcode(0xfd, 0x4c) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_le_f64(b, a);
    vector_result(value, offset);
}

handler V128Not() @opcode(0xfd, 0x4d) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = not_vector(a);
    vector_result(value, offset);
}

handler V128And() @opcode(0xfd, 0x4e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_and(a, b);
    vector_result(value, offset);
}

handler V128AndNot() @opcode(0xfd, 0x4f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_andnot(b, a);
    vector_result(value, offset);
}

handler V128Or() @opcode(0xfd, 0x50) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_or(a, b);
    vector_result(value, offset);
}

handler V128Xor() @opcode(0xfd, 0x51) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_xor(a, b);
    vector_result(value, offset);
}

handler V128Bitselect() @opcode(0xfd, 0x52) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut mask: v128;
    load128(mask, [top, 0]);
    let mut second: v128;
    load128(second, [top, 16]);
    let mut first: v128;
    load128(first, [top, 32]);
    let next = top + 32;
    vsp = next;
    let chosen_first = v128_and(first, mask);
    let chosen_second = v128_andnot(mask, second);
    let value = v128_or(chosen_first, chosen_second);
    vector_result(value, offset);
}

handler I8x16RelaxedLaneselect() @opcode(0xfd, 0x109) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut mask: v128;
    load128(mask, [top, 0]);
    let mut second: v128;
    load128(second, [top, 16]);
    let mut first: v128;
    load128(first, [top, 32]);
    let next = top + 32;
    vsp = next;
    let chosen_first = v128_and(first, mask);
    let chosen_second = v128_andnot(mask, second);
    let value = v128_or(chosen_first, chosen_second);
    vector_result(value, offset);
}

handler I16x8RelaxedLaneselect() @opcode(0xfd, 0x10a) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut mask: v128;
    load128(mask, [top, 0]);
    let mut second: v128;
    load128(second, [top, 16]);
    let mut first: v128;
    load128(first, [top, 32]);
    let next = top + 32;
    vsp = next;
    let chosen_first = v128_and(first, mask);
    let chosen_second = v128_andnot(mask, second);
    let value = v128_or(chosen_first, chosen_second);
    vector_result(value, offset);
}

handler I32x4RelaxedLaneselect() @opcode(0xfd, 0x10b) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut mask: v128;
    load128(mask, [top, 0]);
    let mut second: v128;
    load128(second, [top, 16]);
    let mut first: v128;
    load128(first, [top, 32]);
    let next = top + 32;
    vsp = next;
    let chosen_first = v128_and(first, mask);
    let chosen_second = v128_andnot(mask, second);
    let value = v128_or(chosen_first, chosen_second);
    vector_result(value, offset);
}

handler I64x2RelaxedLaneselect() @opcode(0xfd, 0x10c) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut mask: v128;
    load128(mask, [top, 0]);
    let mut second: v128;
    load128(second, [top, 16]);
    let mut first: v128;
    load128(first, [top, 32]);
    let next = top + 32;
    vsp = next;
    let chosen_first = v128_and(first, mask);
    let chosen_second = v128_andnot(mask, second);
    let value = v128_or(chosen_first, chosen_second);
    vector_result(value, offset);
}

handler V128AnyTrue() @opcode(0xfd, 0x53) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let low = v128_low_u64(a);
    let high = v128_high_u64(a);
    let combined = low | high;
    let mut result: u64 = 0;
    if combined != 0 {
        result = 1;
    }
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I8x16AllTrue() @opcode(0xfd, 0x63) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let zero_lanes = v128_eq_i8(a, zero_vector());
    let zero_mask = v128_bitmask_i8(zero_lanes);
    let mut result: u64 = 0;
    if zero_mask == 0 {
        result = 1;
    }
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I16x8AllTrue() @opcode(0xfd, 0x83) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let zero_lanes = v128_eq_i16(a, zero_vector());
    let zero_mask = v128_bitmask_i8(zero_lanes);
    let mut result: u64 = 0;
    if zero_mask == 0 {
        result = 1;
    }
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I32x4AllTrue() @opcode(0xfd, 0xa3) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let zero_lanes = v128_eq_i32(a, zero_vector());
    let zero_mask = v128_bitmask_i8(zero_lanes);
    let mut result: u64 = 0;
    if zero_mask == 0 {
        result = 1;
    }
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I64x2AllTrue() @opcode(0xfd, 0xc3) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let zero_lanes = v128_eq_i64(a, zero_vector());
    let zero_mask = v128_bitmask_i8(zero_lanes);
    let mut result: u64 = 0;
    if zero_mask == 0 {
        result = 1;
    }
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I8x16Bitmask() @opcode(0xfd, 0x64) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let result = v128_bitmask_i8(a);
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I16x8Bitmask() @opcode(0xfd, 0x84) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let narrowed = v128_narrow_s_i16(a, a);
    let mask = v128_bitmask_i8(narrowed);
    let result = mask & 0xff;
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I32x4Bitmask() @opcode(0xfd, 0xa4) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let result = v128_bitmask_f32(a);
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler I64x2Bitmask() @opcode(0xfd, 0xc4) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let result = v128_bitmask_f64(a);
    replace_with_halves(2, 0, 0);
    let top: u64 = vsp;
    let next = top + 8;
    vsp = next;
    let slots: Sequence<u64> = alias(next);
    slots[0] = result;
    advance(offset);
}

handler F32x4DemoteF64x2Zero() @opcode(0xfd, 0x5e) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_demote_f64(a);
    vector_result(value, offset);
}

handler F64x2PromoteLowF32x4() @opcode(0xfd, 0x5f) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_promote_low_f32(a);
    vector_result(value, offset);
}

handler I8x16Abs() @opcode(0xfd, 0x60) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_abs_i8(a);
    vector_result(value, offset);
}

handler I8x16Neg() @opcode(0xfd, 0x61) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sub_i8(zero_vector(), a);
    vector_result(value, offset);
}

handler I16x8Abs() @opcode(0xfd, 0x80) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_abs_i16(a);
    vector_result(value, offset);
}

handler I16x8Neg() @opcode(0xfd, 0x81) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sub_i16(zero_vector(), a);
    vector_result(value, offset);
}

handler I32x4Abs() @opcode(0xfd, 0xa0) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_abs_i32(a);
    vector_result(value, offset);
}

handler I32x4Neg() @opcode(0xfd, 0xa1) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sub_i32(zero_vector(), a);
    vector_result(value, offset);
}

handler I64x2Abs() @opcode(0xfd, 0xc0) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let sign = v128_gt_s_i64(zero_vector(), a);
    let value = v128_sub_i64(v128_xor(a, sign), sign);
    vector_result(value, offset);
}

handler I64x2Neg() @opcode(0xfd, 0xc1) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sub_i64(zero_vector(), a);
    vector_result(value, offset);
}

handler I8x16Popcnt() @opcode(0xfd, 0x62) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let low = slots[0];
    let high = slots[1];
    let low_1 = low - ((low >> 1) & 0x5555555555555555);
    let low_2 = (low_1 & 0x3333333333333333) + ((low_1 >> 2) & 0x3333333333333333);
    let low_3 = (low_2 + (low_2 >> 4)) & 0x0f0f0f0f0f0f0f0f;
    let high_1 = high - ((high >> 1) & 0x5555555555555555);
    let high_2 = (high_1 & 0x3333333333333333) + ((high_1 >> 2) & 0x3333333333333333);
    let high_3 = (high_2 + (high_2 >> 4)) & 0x0f0f0f0f0f0f0f0f;
    slots[0] = low_3;
    slots[1] = high_3;
    advance(offset);
}

handler I8x16NarrowI16x8S() @opcode(0xfd, 0x65) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_narrow_s_i16(a, b);
    vector_result(value, offset);
}

handler I8x16NarrowI16x8U() @opcode(0xfd, 0x66) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_narrow_u_i16(a, b);
    vector_result(value, offset);
}

handler I16x8NarrowI32x4S() @opcode(0xfd, 0x85) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_narrow_s_i32(a, b);
    vector_result(value, offset);
}

handler I16x8NarrowI32x4U() @opcode(0xfd, 0x86) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_narrow_u_i32(a, b);
    vector_result(value, offset);
}

handler F32x4Ceil() @opcode(0xfd, 0x67) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_ceil_f32(a);
    vector_result(value, offset);
}

handler F32x4Floor() @opcode(0xfd, 0x68) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_floor_f32(a);
    vector_result(value, offset);
}

handler F32x4Trunc() @opcode(0xfd, 0x69) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_trunc_f32(a);
    vector_result(value, offset);
}

handler F32x4Nearest() @opcode(0xfd, 0x6a) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_nearest_f32(a);
    vector_result(value, offset);
}

handler F64x2Ceil() @opcode(0xfd, 0x74) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_ceil_f64(a);
    vector_result(value, offset);
}

handler F64x2Floor() @opcode(0xfd, 0x75) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_floor_f64(a);
    vector_result(value, offset);
}

handler F64x2Trunc() @opcode(0xfd, 0x7a) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_trunc_f64(a);
    vector_result(value, offset);
}

handler F64x2Nearest() @opcode(0xfd, 0x94) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_nearest_f64(a);
    vector_result(value, offset);
}

handler I16x8Shl() @opcode(0xfd, 0x8b) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(15);
    let a = top_vector();
    let value = v128_shl_i16(a, count);
    vector_result(value, offset);
}

handler I16x8ShrS() @opcode(0xfd, 0x8c) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(15);
    let a = top_vector();
    let value = v128_shr_s_i16(a, count);
    vector_result(value, offset);
}

handler I16x8ShrU() @opcode(0xfd, 0x8d) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(15);
    let a = top_vector();
    let value = v128_shr_u_i16(a, count);
    vector_result(value, offset);
}

handler I32x4Shl() @opcode(0xfd, 0xab) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(31);
    let a = top_vector();
    let value = v128_shl_i32(a, count);
    vector_result(value, offset);
}

handler I32x4ShrS() @opcode(0xfd, 0xac) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(31);
    let a = top_vector();
    let value = v128_shr_s_i32(a, count);
    vector_result(value, offset);
}

handler I32x4ShrU() @opcode(0xfd, 0xad) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(31);
    let a = top_vector();
    let value = v128_shr_u_i32(a, count);
    vector_result(value, offset);
}

handler I64x2Shl() @opcode(0xfd, 0xcb) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(63);
    let a = top_vector();
    let value = v128_shl_i64(a, count);
    vector_result(value, offset);
}

handler I64x2ShrU() @opcode(0xfd, 0xcd) {
    let [_, offset] = prefixed_immediates();
    let count = shift_count(63);
    let a = top_vector();
    let value = v128_shr_u_i64(a, count);
    vector_result(value, offset);
}

handler I64x2ShrS() @opcode(0xfd, 0xcc) {
    let [_, offset] = prefixed_immediates();
    let raw_count = pop();
    let masked = raw_count & 63;
    let count = v128_from_u64x2(masked, 0);
    let lane_bits: u64 = 64;
    let complement = lane_bits - masked;
    let fill_count = v128_from_u64x2(complement, 0);
    let a = top_vector();
    # Shift logically, and fill the vacated bits from the sign.
    let sign = v128_gt_s_i64(zero_vector(), a);
    let shifted = v128_shr_u_i64(a, count);
    let fill = v128_shl_i64(sign, fill_count);
    let value = v128_or(shifted, fill);
    vector_result(value, offset);
}

handler I8x16Shl() @opcode(0xfd, 0x6b) {
    let [_, offset] = prefixed_immediates();
    let raw_count = pop();
    let masked = raw_count & 7;
    let count = v128_from_u64x2(masked, 0);
    let a = top_vector();
    # Shift 16-bit lanes and clear the bits that crossed into the next byte.
    let shifted = v128_shl_i16(a, count);
    let all_bits: u64 = 0xff;
    let byte_mask = (all_bits << masked) & 0xff;
    let value = v128_and(shifted, splat_i8(byte_mask));
    vector_result(value, offset);
}

handler I8x16ShrU() @opcode(0xfd, 0x6d) {
    let [_, offset] = prefixed_immediates();
    let raw_count = pop();
    let masked = raw_count & 7;
    let count = v128_from_u64x2(masked, 0);
    let a = top_vector();
    let shifted = v128_shr_u_i16(a, count);
    let all_bits: u64 = 0xff;
    let byte_mask = all_bits >> masked;
    let value = v128_and(shifted, splat_i8(byte_mask));
    vector_result(value, offset);
}

handler I8x16ShrS() @opcode(0xfd, 0x6c) {
    let [_, offset] = prefixed_immediates();
    let raw_count = pop();
    let masked = raw_count & 7;
    let count = v128_from_u64x2(masked + 8, 0);
    let a = top_vector();
    # Each byte goes into the high byte of a 16-bit lane, which shifts arithmetically, and narrows back.
    let low = v128_interleave_low_i8(a, a);
    let high = v128_interleave_high_i8(a, a);
    let shifted_low = v128_shr_s_i16(low, count);
    let shifted_high = v128_shr_s_i16(high, count);
    let value = v128_narrow_s_i16(shifted_low, shifted_high);
    vector_result(value, offset);
}

handler I8x16Add() @opcode(0xfd, 0x6e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_i8(a, b);
    vector_result(value, offset);
}

handler I8x16AddSatS() @opcode(0xfd, 0x6f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_sat_s_i8(a, b);
    vector_result(value, offset);
}

handler I8x16AddSatU() @opcode(0xfd, 0x70) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_sat_u_i8(a, b);
    vector_result(value, offset);
}

handler I8x16Sub() @opcode(0xfd, 0x71) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_i8(a, b);
    vector_result(value, offset);
}

handler I8x16SubSatS() @opcode(0xfd, 0x72) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_sat_s_i8(a, b);
    vector_result(value, offset);
}

handler I8x16SubSatU() @opcode(0xfd, 0x73) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_sat_u_i8(a, b);
    vector_result(value, offset);
}

handler I16x8Add() @opcode(0xfd, 0x8e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_i16(a, b);
    vector_result(value, offset);
}

handler I16x8AddSatS() @opcode(0xfd, 0x8f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_sat_s_i16(a, b);
    vector_result(value, offset);
}

handler I16x8AddSatU() @opcode(0xfd, 0x90) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_sat_u_i16(a, b);
    vector_result(value, offset);
}

handler I16x8Sub() @opcode(0xfd, 0x91) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_i16(a, b);
    vector_result(value, offset);
}

handler I16x8SubSatS() @opcode(0xfd, 0x92) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_sat_s_i16(a, b);
    vector_result(value, offset);
}

handler I16x8SubSatU() @opcode(0xfd, 0x93) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_sat_u_i16(a, b);
    vector_result(value, offset);
}

handler I8x16MinS() @opcode(0xfd, 0x76) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_s_i8(a, b);
    vector_result(value, offset);
}

handler I8x16MinU() @opcode(0xfd, 0x77) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_u_i8(a, b);
    vector_result(value, offset);
}

handler I8x16MaxS() @opcode(0xfd, 0x78) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_s_i8(a, b);
    vector_result(value, offset);
}

handler I8x16MaxU() @opcode(0xfd, 0x79) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_u_i8(a, b);
    vector_result(value, offset);
}

handler I16x8MinS() @opcode(0xfd, 0x96) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_s_i16(a, b);
    vector_result(value, offset);
}

handler I16x8MinU() @opcode(0xfd, 0x97) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_u_i16(a, b);
    vector_result(value, offset);
}

handler I16x8MaxS() @opcode(0xfd, 0x98) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_s_i16(a, b);
    vector_result(value, offset);
}

handler I16x8MaxU() @opcode(0xfd, 0x99) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_u_i16(a, b);
    vector_result(value, offset);
}

handler I32x4MinS() @opcode(0xfd, 0xb6) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_s_i32(a, b);
    vector_result(value, offset);
}

handler I32x4MinU() @opcode(0xfd, 0xb7) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_u_i32(a, b);
    vector_result(value, offset);
}

handler I32x4MaxS() @opcode(0xfd, 0xb8) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_s_i32(a, b);
    vector_result(value, offset);
}

handler I32x4MaxU() @opcode(0xfd, 0xb9) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_u_i32(a, b);
    vector_result(value, offset);
}

handler I8x16AvgrU() @opcode(0xfd, 0x7b) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_avgr_u_i8(a, b);
    vector_result(value, offset);
}

handler I16x8AvgrU() @opcode(0xfd, 0x9b) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_avgr_u_i16(a, b);
    vector_result(value, offset);
}

handler I16x8ExtaddPairwiseI8x16S() @opcode(0xfd, 0x7c) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_madd_u8_s8(splat_i8(1), a);
    vector_result(value, offset);
}

handler I16x8ExtaddPairwiseI8x16U() @opcode(0xfd, 0x7d) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_madd_u8_s8(a, splat_i8(1));
    vector_result(value, offset);
}

handler I32x4ExtaddPairwiseI16x8S() @opcode(0xfd, 0x7e) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_madd_i16(a, splat_i16(1));
    vector_result(value, offset);
}

handler I32x4ExtaddPairwiseI16x8U() @opcode(0xfd, 0x7f) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_add_i32(v128_madd_i16(v128_xor(a, splat_i16(0x8000)), splat_i16(1)), splat_i32(0x10000));
    vector_result(value, offset);
}

handler I16x8Q15mulrSatS() @opcode(0xfd, 0x82) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # The only product that overflows is -1 * -1, which gives 0x8000 and must saturate to 0x7fff.
    let product = v128_mulhrs_i16(a, b);
    let overflowed = v128_eq_i16(product, splat_i16(0x8000));
    let value = v128_xor(product, overflowed);
    vector_result(value, offset);
}

handler I16x8RelaxedQ15mulrS() @opcode(0xfd, 0x111) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # The only product that overflows is -1 * -1, which gives 0x8000 and must saturate to 0x7fff.
    let product = v128_mulhrs_i16(a, b);
    let overflowed = v128_eq_i16(product, splat_i16(0x8000));
    let value = v128_xor(product, overflowed);
    vector_result(value, offset);
}

handler I16x8ExtendLowI8x16S() @opcode(0xfd, 0x87) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i8(a);
    vector_result(value, offset);
}

handler I16x8ExtendHighI8x16S() @opcode(0xfd, 0x88) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i8(v128_high_half(a));
    vector_result(value, offset);
}

handler I16x8ExtendLowI8x16U() @opcode(0xfd, 0x89) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i8(a);
    vector_result(value, offset);
}

handler I16x8ExtendHighI8x16U() @opcode(0xfd, 0x8a) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i8(v128_high_half(a));
    vector_result(value, offset);
}

handler I32x4ExtendLowI16x8S() @opcode(0xfd, 0xa7) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i16(a);
    vector_result(value, offset);
}

handler I32x4ExtendHighI16x8S() @opcode(0xfd, 0xa8) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i16(v128_high_half(a));
    vector_result(value, offset);
}

handler I32x4ExtendLowI16x8U() @opcode(0xfd, 0xa9) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i16(a);
    vector_result(value, offset);
}

handler I32x4ExtendHighI16x8U() @opcode(0xfd, 0xaa) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i16(v128_high_half(a));
    vector_result(value, offset);
}

handler I64x2ExtendLowI32x4S() @opcode(0xfd, 0xc7) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i32(a);
    vector_result(value, offset);
}

handler I64x2ExtendHighI32x4S() @opcode(0xfd, 0xc8) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_s_i32(v128_high_half(a));
    vector_result(value, offset);
}

handler I64x2ExtendLowI32x4U() @opcode(0xfd, 0xc9) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i32(a);
    vector_result(value, offset);
}

handler I64x2ExtendHighI32x4U() @opcode(0xfd, 0xca) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_extend_low_u_i32(v128_high_half(a));
    vector_result(value, offset);
}

handler I16x8Mul() @opcode(0xfd, 0x95) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i16(a, b);
    vector_result(value, offset);
}

handler I16x8ExtmulLowI8x16S() @opcode(0xfd, 0x9c) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i16(v128_extend_low_s_i8(a), v128_extend_low_s_i8(b));
    vector_result(value, offset);
}

handler I16x8ExtmulHighI8x16S() @opcode(0xfd, 0x9d) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i16(v128_extend_low_s_i8(v128_high_half(a)), v128_extend_low_s_i8(v128_high_half(b)));
    vector_result(value, offset);
}

handler I16x8ExtmulLowI8x16U() @opcode(0xfd, 0x9e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i16(v128_extend_low_u_i8(a), v128_extend_low_u_i8(b));
    vector_result(value, offset);
}

handler I16x8ExtmulHighI8x16U() @opcode(0xfd, 0x9f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i16(v128_extend_low_u_i8(v128_high_half(a)), v128_extend_low_u_i8(v128_high_half(b)));
    vector_result(value, offset);
}

handler I32x4ExtmulLowI16x8S() @opcode(0xfd, 0xbc) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i32(v128_extend_low_s_i16(a), v128_extend_low_s_i16(b));
    vector_result(value, offset);
}

handler I32x4ExtmulHighI16x8S() @opcode(0xfd, 0xbd) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i32(v128_extend_low_s_i16(v128_high_half(a)), v128_extend_low_s_i16(v128_high_half(b)));
    vector_result(value, offset);
}

handler I32x4ExtmulLowI16x8U() @opcode(0xfd, 0xbe) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i32(v128_extend_low_u_i16(a), v128_extend_low_u_i16(b));
    vector_result(value, offset);
}

handler I32x4ExtmulHighI16x8U() @opcode(0xfd, 0xbf) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i32(v128_extend_low_u_i16(v128_high_half(a)), v128_extend_low_u_i16(v128_high_half(b)));
    vector_result(value, offset);
}

handler I64x2ExtmulLowI32x4U() @opcode(0xfd, 0xde) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_u32_to_u64(v128_extend_low_u_i32(a), v128_extend_low_u_i32(b));
    vector_result(value, offset);
}

handler I64x2ExtmulHighI32x4U() @opcode(0xfd, 0xdf) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_u32_to_u64(v128_extend_low_u_i32(v128_high_half(a)), v128_extend_low_u_i32(v128_high_half(b)));
    vector_result(value, offset);
}

handler I64x2ExtmulLowI32x4S() @opcode(0xfd, 0xdc) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut b0: u64;
    load32(b0, [top, 0]);
    let mut b1: u64;
    load32(b1, [top, 4]);
    let mut a0: u64;
    load32(a0, [top, 16]);
    let mut a1: u64;
    load32(a1, [top, 20]);
    let extended_a0 = sign_extend_32(a0);
    let product0 = extended_a0 * sign_extend_32(b0);
    let extended_a1 = sign_extend_32(a1);
    let product1 = extended_a1 * sign_extend_32(b1);
    let next = top + 16;
    vsp = next;
    set_top_halves(product0, product1);
    advance(offset);
}

handler I64x2ExtmulHighI32x4S() @opcode(0xfd, 0xdd) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut b0: u64;
    load32(b0, [top, 8]);
    let mut b1: u64;
    load32(b1, [top, 12]);
    let mut a0: u64;
    load32(a0, [top, 24]);
    let mut a1: u64;
    load32(a1, [top, 28]);
    let extended_a0 = sign_extend_32(a0);
    let product0 = extended_a0 * sign_extend_32(b0);
    let extended_a1 = sign_extend_32(a1);
    let product1 = extended_a1 * sign_extend_32(b1);
    let next = top + 16;
    vsp = next;
    set_top_halves(product0, product1);
    advance(offset);
}

handler I32x4Add() @opcode(0xfd, 0xae) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_i32(a, b);
    vector_result(value, offset);
}

handler I32x4Sub() @opcode(0xfd, 0xb1) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_i32(a, b);
    vector_result(value, offset);
}

handler I32x4Mul() @opcode(0xfd, 0xb5) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_i32(a, b);
    vector_result(value, offset);
}

handler I32x4DotI16x8S() @opcode(0xfd, 0xba) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_madd_i16(a, b);
    vector_result(value, offset);
}

handler I64x2Add() @opcode(0xfd, 0xce) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_i64(a, b);
    vector_result(value, offset);
}

handler I64x2Sub() @opcode(0xfd, 0xd1) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_i64(a, b);
    vector_result(value, offset);
}

handler I64x2Mul() @opcode(0xfd, 0xd5) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let a_low = slots[2];
    let a_high = slots[3];
    let low = a_low * slots[0];
    let high = a_high * slots[1];
    let next = top + 16;
    vsp = next;
    set_top_halves(low, high);
    advance(offset);
}

handler I64x2Eq() @opcode(0xfd, 0xd6) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_eq_i64(a, b);
    vector_result(value, offset);
}

handler I64x2Ne() @opcode(0xfd, 0xd7) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_eq_i64(a, b));
    vector_result(value, offset);
}

handler I64x2LtS() @opcode(0xfd, 0xd8) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i64(b, a);
    vector_result(value, offset);
}

handler I64x2GtS() @opcode(0xfd, 0xd9) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_gt_s_i64(a, b);
    vector_result(value, offset);
}

handler I64x2LeS() @opcode(0xfd, 0xda) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_gt_s_i64(a, b));
    vector_result(value, offset);
}

handler I64x2GeS() @opcode(0xfd, 0xdb) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = not_vector(v128_gt_s_i64(b, a));
    vector_result(value, offset);
}

handler F32x4Abs() @opcode(0xfd, 0xe0) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_and(a, splat_u64(0x7fffffff7fffffff));
    vector_result(value, offset);
}

handler F32x4Neg() @opcode(0xfd, 0xe1) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_xor(a, splat_u64(0x8000000080000000));
    vector_result(value, offset);
}

handler F32x4Sqrt() @opcode(0xfd, 0xe3) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sqrt_f32(a);
    vector_result(value, offset);
}

handler F32x4Add() @opcode(0xfd, 0xe4) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Sub() @opcode(0xfd, 0xe5) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Mul() @opcode(0xfd, 0xe6) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Div() @opcode(0xfd, 0xe7) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_div_f32(a, b);
    vector_result(value, offset);
}

handler F32x4Min() @opcode(0xfd, 0xe8) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN or -0 in either operand makes it into the result, and NaN lanes canonical.
    let first = v128_min_f32(a, b);
    let second = v128_min_f32(b, a);
    let merged = v128_or(first, second);
    let nan_lanes = v128_unord_f32(merged, merged);
    let with_nans = v128_or(merged, nan_lanes);
    let payload = v128_shr_u_i32(nan_lanes, v128_from_u64x2(10, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F32x4Max() @opcode(0xfd, 0xe9) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN in either operand makes it into the result, +0 beats -0, and NaN lanes are canonical.
    let first = v128_max_f32(a, b);
    let second = v128_max_f32(b, a);
    let differences = v128_xor(first, second);
    let propagated = v128_or(second, differences);
    let with_nans = v128_sub_f32(propagated, differences);
    let nan_lanes = v128_unord_f32(differences, with_nans);
    let payload = v128_shr_u_i32(nan_lanes, v128_from_u64x2(10, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F32x4Pmin() @opcode(0xfd, 0xea) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_f32(b, a);
    vector_result(value, offset);
}

handler F32x4Pmax() @opcode(0xfd, 0xeb) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_f32(b, a);
    vector_result(value, offset);
}

handler F32x4RelaxedMin() @opcode(0xfd, 0x10d) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN or -0 in either operand makes it into the result, and NaN lanes canonical.
    let first = v128_min_f32(a, b);
    let second = v128_min_f32(b, a);
    let merged = v128_or(first, second);
    let nan_lanes = v128_unord_f32(merged, merged);
    let with_nans = v128_or(merged, nan_lanes);
    let payload = v128_shr_u_i32(nan_lanes, v128_from_u64x2(10, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F32x4RelaxedMax() @opcode(0xfd, 0x10e) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN in either operand makes it into the result, +0 beats -0, and NaN lanes are canonical.
    let first = v128_max_f32(a, b);
    let second = v128_max_f32(b, a);
    let differences = v128_xor(first, second);
    let propagated = v128_or(second, differences);
    let with_nans = v128_sub_f32(propagated, differences);
    let nan_lanes = v128_unord_f32(differences, with_nans);
    let payload = v128_shr_u_i32(nan_lanes, v128_from_u64x2(10, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F32x4RelaxedMadd() @opcode(0xfd, 0x105) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut addend: v128;
    load128(addend, [top, 0]);
    let mut b: v128;
    load128(b, [top, 16]);
    let mut a: v128;
    load128(a, [top, 32]);
    let next = top + 32;
    vsp = next;
    let product = v128_mul_f32(a, b);
    let value = v128_add_f32(product, addend);
    vector_result(value, offset);
}

handler F32x4RelaxedNmadd() @opcode(0xfd, 0x106) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut addend: v128;
    load128(addend, [top, 0]);
    let mut b: v128;
    load128(b, [top, 16]);
    let mut a: v128;
    load128(a, [top, 32]);
    let next = top + 32;
    vsp = next;
    let product = v128_mul_f32(a, b);
    let value = v128_sub_f32(addend, product);
    vector_result(value, offset);
}

handler F64x2Abs() @opcode(0xfd, 0xec) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_and(a, splat_u64(0x7fffffffffffffff));
    vector_result(value, offset);
}

handler F64x2Neg() @opcode(0xfd, 0xed) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_xor(a, splat_u64(0x8000000000000000));
    vector_result(value, offset);
}

handler F64x2Sqrt() @opcode(0xfd, 0xef) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_sqrt_f64(a);
    vector_result(value, offset);
}

handler F64x2Add() @opcode(0xfd, 0xf0) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_add_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Sub() @opcode(0xfd, 0xf1) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_sub_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Mul() @opcode(0xfd, 0xf2) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_mul_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Div() @opcode(0xfd, 0xf3) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_div_f64(a, b);
    vector_result(value, offset);
}

handler F64x2Min() @opcode(0xfd, 0xf4) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN or -0 in either operand makes it into the result, and NaN lanes canonical.
    let first = v128_min_f64(a, b);
    let second = v128_min_f64(b, a);
    let merged = v128_or(first, second);
    let nan_lanes = v128_unord_f64(merged, merged);
    let with_nans = v128_or(merged, nan_lanes);
    let payload = v128_shr_u_i64(nan_lanes, v128_from_u64x2(13, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F64x2Max() @opcode(0xfd, 0xf5) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN in either operand makes it into the result, +0 beats -0, and NaN lanes are canonical.
    let first = v128_max_f64(a, b);
    let second = v128_max_f64(b, a);
    let differences = v128_xor(first, second);
    let propagated = v128_or(second, differences);
    let with_nans = v128_sub_f64(propagated, differences);
    let nan_lanes = v128_unord_f64(differences, with_nans);
    let payload = v128_shr_u_i64(nan_lanes, v128_from_u64x2(13, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F64x2Pmin() @opcode(0xfd, 0xf6) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_min_f64(b, a);
    vector_result(value, offset);
}

handler F64x2Pmax() @opcode(0xfd, 0xf7) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_max_f64(b, a);
    vector_result(value, offset);
}

handler F64x2RelaxedMin() @opcode(0xfd, 0x10f) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN or -0 in either operand makes it into the result, and NaN lanes canonical.
    let first = v128_min_f64(a, b);
    let second = v128_min_f64(b, a);
    let merged = v128_or(first, second);
    let nan_lanes = v128_unord_f64(merged, merged);
    let with_nans = v128_or(merged, nan_lanes);
    let payload = v128_shr_u_i64(nan_lanes, v128_from_u64x2(13, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F64x2RelaxedMax() @opcode(0xfd, 0x110) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    # Both orders, so that a NaN in either operand makes it into the result, +0 beats -0, and NaN lanes are canonical.
    let first = v128_max_f64(a, b);
    let second = v128_max_f64(b, a);
    let differences = v128_xor(first, second);
    let propagated = v128_or(second, differences);
    let with_nans = v128_sub_f64(propagated, differences);
    let nan_lanes = v128_unord_f64(differences, with_nans);
    let payload = v128_shr_u_i64(nan_lanes, v128_from_u64x2(13, 0));
    let value = v128_andnot(payload, with_nans);
    vector_result(value, offset);
}

handler F64x2RelaxedMadd() @opcode(0xfd, 0x107) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut addend: v128;
    load128(addend, [top, 0]);
    let mut b: v128;
    load128(b, [top, 16]);
    let mut a: v128;
    load128(a, [top, 32]);
    let next = top + 32;
    vsp = next;
    let product = v128_mul_f64(a, b);
    let value = v128_add_f64(product, addend);
    vector_result(value, offset);
}

handler F64x2RelaxedNmadd() @opcode(0xfd, 0x108) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut addend: v128;
    load128(addend, [top, 0]);
    let mut b: v128;
    load128(b, [top, 16]);
    let mut a: v128;
    load128(a, [top, 32]);
    let next = top + 32;
    vsp = next;
    let product = v128_mul_f64(a, b);
    let value = v128_sub_f64(addend, product);
    vector_result(value, offset);
}

handler F32x4ConvertI32x4S() @opcode(0xfd, 0xfa) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_convert_s_i32_f32(a);
    vector_result(value, offset);
}

handler F64x2ConvertLowI32x4S() @opcode(0xfd, 0xfe) {
    let [_, offset] = prefixed_immediates();
    let a = top_vector();
    let value = v128_convert_low_s_i32_f64(a);
    vector_result(value, offset);
}

handler I32x4TruncSatF32x4S() @opcode(0xfd, 0xf8) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load32(bits0, [top, 0]);
    let value0 = f32_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits1: u64;
    load32(bits1, [top, 4]);
    let value1 = f32_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits2: u64;
    load32(bits2, [top, 8]);
    let value2 = f32_value(bits2);
    let truncated2 = saturating_truncation(value2, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits3: u64;
    load32(bits3, [top, 12]);
    let value3 = f32_value(bits3);
    let truncated3 = saturating_truncation(value3, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store32([top, 8], truncated2);
    store32([top, 12], truncated3);
    advance(offset);
}

handler I32x4TruncSatF32x4U() @opcode(0xfd, 0xf9) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load32(bits0, [top, 0]);
    let value0 = f32_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits1: u64;
    load32(bits1, [top, 4]);
    let value1 = f32_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits2: u64;
    load32(bits2, [top, 8]);
    let value2 = f32_value(bits2);
    let truncated2 = saturating_truncation(value2, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits3: u64;
    load32(bits3, [top, 12]);
    let value3 = f32_value(bits3);
    let truncated3 = saturating_truncation(value3, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store32([top, 8], truncated2);
    store32([top, 12], truncated3);
    advance(offset);
}

handler I32x4TruncSatF64x2SZero() @opcode(0xfd, 0xfc) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load64(bits0, [top, 0]);
    let value0 = f64_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits1: u64;
    load64(bits1, [top, 8]);
    let value1 = f64_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store64([top, 8], 0);
    advance(offset);
}

handler I32x4TruncSatF64x2UZero() @opcode(0xfd, 0xfd) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load64(bits0, [top, 0]);
    let value0 = f64_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits1: u64;
    load64(bits1, [top, 8]);
    let value1 = f64_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store64([top, 8], 0);
    advance(offset);
}

handler I32x4RelaxedTruncF32x4S() @opcode(0xfd, 0x101) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load32(bits0, [top, 0]);
    let value0 = f32_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits1: u64;
    load32(bits1, [top, 4]);
    let value1 = f32_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits2: u64;
    load32(bits2, [top, 8]);
    let value2 = f32_value(bits2);
    let truncated2 = saturating_truncation(value2, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits3: u64;
    load32(bits3, [top, 12]);
    let value3 = f32_value(bits3);
    let truncated3 = saturating_truncation(value3, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store32([top, 8], truncated2);
    store32([top, 12], truncated3);
    advance(offset);
}

handler I32x4RelaxedTruncF32x4U() @opcode(0xfd, 0x102) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load32(bits0, [top, 0]);
    let value0 = f32_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits1: u64;
    load32(bits1, [top, 4]);
    let value1 = f32_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits2: u64;
    load32(bits2, [top, 8]);
    let value2 = f32_value(bits2);
    let truncated2 = saturating_truncation(value2, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits3: u64;
    load32(bits3, [top, 12]);
    let value3 = f32_value(bits3);
    let truncated3 = saturating_truncation(value3, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store32([top, 8], truncated2);
    store32([top, 12], truncated3);
    advance(offset);
}

handler I32x4RelaxedTruncF64x2SZero() @opcode(0xfd, 0x103) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load64(bits0, [top, 0]);
    let value0 = f64_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    let mut bits1: u64;
    load64(bits1, [top, 8]);
    let value1 = f64_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xc1e0000000200000, 0x41e0000000000000, 0x80000000, 0x7fffffff, 0);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store64([top, 8], 0);
    advance(offset);
}

handler I32x4RelaxedTruncF64x2UZero() @opcode(0xfd, 0x104) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut bits0: u64;
    load64(bits0, [top, 0]);
    let value0 = f64_value(bits0);
    let truncated0 = saturating_truncation(value0, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    let mut bits1: u64;
    load64(bits1, [top, 8]);
    let value1 = f64_value(bits1);
    let truncated1 = saturating_truncation(value1, 0xbff0000000000000, 0x41f0000000000000, 0, 0xffffffff, 1);
    store32([top, 0], truncated0);
    store32([top, 4], truncated1);
    store64([top, 8], 0);
    advance(offset);
}

handler F32x4ConvertI32x4U() @opcode(0xfd, 0xfb) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut lane0: u64;
    load32(lane0, [top, 0]);
    let converted0 = s64_to_f32(lane0);
    let bits0 = f32_to_bits(converted0);
    let mut lane1: u64;
    load32(lane1, [top, 4]);
    let converted1 = s64_to_f32(lane1);
    let bits1 = f32_to_bits(converted1);
    let mut lane2: u64;
    load32(lane2, [top, 8]);
    let converted2 = s64_to_f32(lane2);
    let bits2 = f32_to_bits(converted2);
    let mut lane3: u64;
    load32(lane3, [top, 12]);
    let converted3 = s64_to_f32(lane3);
    let bits3 = f32_to_bits(converted3);
    store32([top, 0], bits0);
    store32([top, 4], bits1);
    store32([top, 8], bits2);
    store32([top, 12], bits3);
    advance(offset);
}

handler F64x2ConvertLowI32x4U() @opcode(0xfd, 0xff) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut lane0: u64;
    load32(lane0, [top, 0]);
    let mut lane1: u64;
    load32(lane1, [top, 4]);
    let converted0 = s64_to_f64(lane0);
    let converted1 = s64_to_f64(lane1);
    let bits0 = f64_to_bits(converted0);
    let bits1 = f64_to_bits(converted1);
    set_top_halves(bits0, bits1);
    advance(offset);
}

handler I16x8RelaxedDotI8x16I7x16S() @opcode(0xfd, 0x112) {
    let [_, offset] = prefixed_immediates();
    let [a, b] = binary_vectors();
    let value = v128_madd_u8_s8(b, a);
    vector_result(value, offset);
}

handler I32x4RelaxedDotI8x16I7x16AddS() @opcode(0xfd, 0x113) {
    let [_, offset] = prefixed_immediates();
    let top: u64 = vsp;
    let mut addend: v128;
    load128(addend, [top, 0]);
    let mut b: v128;
    load128(b, [top, 16]);
    let mut a: v128;
    load128(a, [top, 32]);
    let next = top + 32;
    vsp = next;
    let pairs = v128_madd_u8_s8(b, a);
    let quads = v128_madd_i16(pairs, splat_i16(1));
    let value = v128_add_i32(quads, addend);
    vector_result(value, offset);
}

# Bounds-checked SIMD accesses, for other memories and for memory64 (quickening rule 1).

handler CheckedV128Load() @opcode(0xff, 0x80) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 16, 0);
    let mut value: v128;
    load128(value, [address, 0]);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128LoadIndexed() @opcode(0xff, 0xa0) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 16, 1);
    let mut value: v128;
    load128(value, [address, 0]);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load8x8S() @opcode(0xff, 0x81) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load8x8SIndexed() @opcode(0xff, 0xa1) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load8x8U() @opcode(0xff, 0x82) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load8x8UIndexed() @opcode(0xff, 0xa2) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i8(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load16x4S() @opcode(0xff, 0x83) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load16x4SIndexed() @opcode(0xff, 0xa3) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load16x4U() @opcode(0xff, 0x84) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load16x4UIndexed() @opcode(0xff, 0xa4) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i16(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load32x2S() @opcode(0xff, 0x85) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load32x2SIndexed() @opcode(0xff, 0xa5) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_s_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load32x2U() @opcode(0xff, 0x86) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load32x2UIndexed() @opcode(0xff, 0xa6) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut half: u64;
    load64(half, [address, 0]);
    let narrow = v128_from_u64x2(half, 0);
    let value = v128_extend_low_u_i32(narrow);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length);
}

handler CheckedV128Load8Splat() @opcode(0xff, 0x87) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let mut lane: u64;
    load8(lane, [address, 0]);
    let wide = lane * 0x0101010101010101;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load8SplatIndexed() @opcode(0xff, 0xa7) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let mut lane: u64;
    load8(lane, [address, 0]);
    let wide = lane * 0x0101010101010101;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load16Splat() @opcode(0xff, 0x88) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let mut lane: u64;
    load16(lane, [address, 0]);
    let wide = lane * 0x0001000100010001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load16SplatIndexed() @opcode(0xff, 0xa8) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let mut lane: u64;
    load16(lane, [address, 0]);
    let wide = lane * 0x0001000100010001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load32Splat() @opcode(0xff, 0x89) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut lane: u64;
    load32(lane, [address, 0]);
    let wide = lane * 0x0000000100000001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load32SplatIndexed() @opcode(0xff, 0xa9) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut lane: u64;
    load32(lane, [address, 0]);
    let wide = lane * 0x0000000100000001;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load64Splat() @opcode(0xff, 0x8a) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut lane: u64;
    load64(lane, [address, 0]);
    let wide = lane;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load64SplatIndexed() @opcode(0xff, 0xaa) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut lane: u64;
    load64(lane, [address, 0]);
    let wide = lane;
    replace_with_halves(0, wide, wide);
    advance(length);
}

handler CheckedV128Load32Zero() @opcode(0xff, 0x94) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let mut lane: u64;
    load32(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler CheckedV128Load32ZeroIndexed() @opcode(0xff, 0xb4) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let mut lane: u64;
    load32(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler CheckedV128Load64Zero() @opcode(0xff, 0x95) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let mut lane: u64;
    load64(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler CheckedV128Load64ZeroIndexed() @opcode(0xff, 0xb5) {
    let address_operand = pop();
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let mut lane: u64;
    load64(lane, [address, 0]);
    replace_with_halves(0, lane, 0);
    advance(length);
}

handler CheckedV128Store() @opcode(0xff, 0x8b) {
    let value = top_vector();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let next = top + 24;
    vsp = next;
    let [address, length] = checked_access_address(address_operand, 16, 0);
    store128([address, 0], value);
    advance(length);
}

handler CheckedV128StoreIndexed() @opcode(0xff, 0xab) {
    let value = top_vector();
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let next = top + 24;
    vsp = next;
    let [address, length] = checked_access_address(address_operand, 16, 1);
    store128([address, 0], value);
    advance(length);
}

handler CheckedV128Load8Lane() @opcode(0xff, 0x8c) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load8(value, [address, 0]);
    let lane_address = top + (lane * 1);
    store8([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load8LaneIndexed() @opcode(0xff, 0xac) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load8(value, [address, 0]);
    let lane_address = top + (lane * 1);
    store8([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load16Lane() @opcode(0xff, 0x8d) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load16(value, [address, 0]);
    let lane_address = top + (lane * 2);
    store16([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load16LaneIndexed() @opcode(0xff, 0xad) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load16(value, [address, 0]);
    let lane_address = top + (lane * 2);
    store16([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load32Lane() @opcode(0xff, 0x8e) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load32(value, [address, 0]);
    let lane_address = top + (lane * 4);
    store32([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load32LaneIndexed() @opcode(0xff, 0xae) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load32(value, [address, 0]);
    let lane_address = top + (lane * 4);
    store32([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load64Lane() @opcode(0xff, 0x8f) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load64(value, [address, 0]);
    let lane_address = top + (lane * 8);
    store64([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Load64LaneIndexed() @opcode(0xff, 0xaf) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let mut value: u64;
    load64(value, [address, 0]);
    let lane_address = top + (lane * 8);
    store64([lane_address, 0], value);
    # The vector moves up into the address's slot.
    let low = slots[0];
    let high = slots[1];
    slots[2] = high;
    slots[1] = low;
    let next = top + 8;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store8Lane() @opcode(0xff, 0x90) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 1, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 1);
    let mut value: u64;
    load8(value, [lane_address, 0]);
    store8([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store8LaneIndexed() @opcode(0xff, 0xb0) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 1, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 1);
    let mut value: u64;
    load8(value, [lane_address, 0]);
    store8([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store16Lane() @opcode(0xff, 0x91) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 2, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 2);
    let mut value: u64;
    load16(value, [lane_address, 0]);
    store16([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store16LaneIndexed() @opcode(0xff, 0xb1) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 2, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 2);
    let mut value: u64;
    load16(value, [lane_address, 0]);
    store16([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store32Lane() @opcode(0xff, 0x92) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 4, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 4);
    let mut value: u64;
    load32(value, [lane_address, 0]);
    store32([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store32LaneIndexed() @opcode(0xff, 0xb2) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 4, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 4);
    let mut value: u64;
    load32(value, [lane_address, 0]);
    store32([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store64Lane() @opcode(0xff, 0x93) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 8, 0);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 8);
    let mut value: u64;
    load64(value, [lane_address, 0]);
    store64([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

handler CheckedV128Store64LaneIndexed() @opcode(0xff, 0xb3) {
    let top: u64 = vsp;
    let slots: Sequence<u64> = alias(top);
    let address_operand = slots[2];
    let [address, length] = checked_access_address(address_operand, 8, 1);
    let current: u64 = ip;
    let lane_bytes: Sequence<u8> = alias(current + length);
    let lane: u64 = alias(lane_bytes[0]);
    let lane_address = top + (lane * 8);
    let mut value: u64;
    load64(value, [lane_address, 0]);
    store64([address, 0], value);
    let next = top + 24;
    vsp = next;
    advance(length + 1);
}

inline fn sign_extend_32(value: u64) -> u64 {
    let narrow = i32(value);
    let wide: u64 = alias(i64(narrow));
    wide
}

# The v128 forms of instructions that move values (quickening rule 2). The scalar slot of a v128 local holds the
# address of its 16-byte storage.

handler LocalGetV128() @opcode(0xf4) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let storage = locals[index];
    let mut value: v128;
    load128(value, [storage, 0]);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length + 1);
}

handler LocalSetV128() @opcode(0xf5) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let storage = locals[index];
    let value = top_vector();
    store128([storage, 0], value);
    drop_slots(2);
    advance(length + 1);
}

handler LocalTeeV128() @opcode(0xf6) {
    let [index, length] = read_leb(immediates());
    let locals: Sequence<u64> = alias(fp);
    let storage = locals[index];
    let value = top_vector();
    store128([storage, 0], value);
    advance(length + 1);
}

handler GlobalGetV128() @opcode(0xf7) {
    let [index, length] = read_leb(immediates());
    let address = global_address(index);
    let mut value: v128;
    load128(value, [address, 0]);
    let top: u64 = vsp;
    let next = top - 16;
    vsp = next;
    store128([next, 0], value);
    advance(length + 1);
}

handler GlobalSetV128() @opcode(0xf8) {
    let [index, length] = read_leb(immediates());
    let address = global_address(index);
    let value = top_vector();
    store128([address, 0], value);
    drop_slots(2);
    advance(length + 1);
}

handler DropV128() @opcode(0xf9) {
    drop_slots(2);
    dispatch_next;
}

handler SelectV128() @opcode(0xfa) {
    let condition = pop32();
    let [first, second] = binary_vectors();
    if condition == 0 {
        set_top_vector(second);
    }
    dispatch_next;
}

# The GC proposal (the 0xFB prefix). See gc.rs for how references and objects are encoded.

handler PrefixFB() @opcode(0xfb) {
    let [sub_opcode, _] = read_leb(immediates());
    dispatch_secondary(0xfb, sub_opcode);
}

# The runtime type of the module's type `type_index`.
inline fn rtt_of_type(type_index: u64) -> u64 {
    let instance_pointer = instance();
    let mut identities_pointer: u64;
    load64(identities_pointer, [instance_pointer, INSTANCE_TYPE_IDENTITIES]);
    let identities: Sequence<u32> = alias(identities_pointer);
    let identity: u64 = alias(identities[type_index]);
    rtt_of_identity(identity)
}

inline fn rtt_of_identity(identity: u64) -> u64 {
    let context_pointer = context();
    let mut rtts_pointer: u64;
    load64(rtts_pointer, [context_pointer, CONTEXT_RTTS]);
    let rtts: Sequence<u64> = alias(rtts_pointer);
    rtts[identity]
}

inline fn identity_of_type(type_index: u64) -> u64 {
    let instance_pointer = instance();
    let mut identities_pointer: u64;
    load64(identities_pointer, [instance_pointer, INSTANCE_TYPE_IDENTITIES]);
    let identities: Sequence<u32> = alias(identities_pointer);
    let identity: u64 = alias(identities[type_index]);
    identity
}

# Allocate an object whose type is `rtt`, with `size` bytes of zeroed data and, for an array, `length` elements.
inline fn allocate_object(rtt: u64, size: u64, length: u64) -> u64 {
    let context_pointer = context();
    store64([context_pointer, CONTEXT_ARGUMENTS], rtt);
    store64([context_pointer, CONTEXT_ARGUMENT_1], size);
    store64([context_pointer, CONTEXT_ARGUMENT_2], length);
    let object: u64 = call_helper(wasm_runtime_allocate, context_pointer);
    guard object != 0 else @cold {
        trap(TRAP_RUNTIME);
    };
    object
}

# Whether `subtype` is the runtime type `identity` or one of its subtypes.
inline fn rtt_is_subtype(subtype: u64, identity: u64) -> u64 {
    let target = rtt_of_identity(identity);
    let mut target_depth: u64;
    load32(target_depth, [target, RTT_DEPTH]);
    let mut depth: u64;
    load32(depth, [subtype, RTT_DEPTH]);
    let mut result: u64 = 0;
    if depth >= target_depth {
        let mut display_pointer: u64;
        load64(display_pointer, [subtype, RTT_DISPLAY]);
        let display: Sequence<u32> = alias(display_pointer);
        let ancestor: u64 = alias(display[target_depth]);
        if ancestor == identity {
            result = 1;
        }
    }
    result
}

# A packed value sign-extended to an i32.
inline fn sign_extend_packed(value: u64, kind: u64) -> u64 {
    let mut shift: u64 = 48;
    if kind == STORAGE_I8 {
        shift = 56;
    }
    let moved = value << shift;
    let signed = signed64(moved);
    let extended = signed >> signed64(shift);
    let narrow = u32(extended);
    let result: u64 = alias(narrow);
    result
}

# The bits of the field or element at `address`, whose storage is `kind`, zero-extended (the low half of a v128).
inline fn load_stored(address: u64, kind: u64) -> u64 {
    let mut value: u64 = 0;
    if kind == STORAGE_I8 {
        load8(value, [address, 0]);
    } else {
        if kind == STORAGE_I16 {
            load16(value, [address, 0]);
        } else {
            if kind == STORAGE_32 {
                load32(value, [address, 0]);
            } else {
                load64(value, [address, 0]);
            }
        }
    }
    value
}

# Store the bits of `value` that a field or element at `address` whose storage is `kind`, not v128, holds.
inline fn store_number(address: u64, kind: u64, value: u64) {
    if kind == STORAGE_I8 {
        store8([address, 0], value);
    } else {
        if kind == STORAGE_I16 {
            store16([address, 0], value);
        } else {
            if kind == STORAGE_32 {
                store32([address, 0], value);
            } else {
                store64([address, 0], value);
            }
        }
    }
}

# Push the value of the field or element at `address`, whose storage is `kind`, replacing `popped` slots.
inline fn push_stored(address: u64, kind: u64, popped: u64) {
    # The low half of a v128 is a load of 64 bits too.
    let low = load_stored(address, kind);
    if kind == STORAGE_V128 {
        let mut high: u64;
        load64(high, [address, 8]);
        replace_with_halves(popped, low, high);
    } else {
        let top: u64 = vsp;
        let next = top + (popped << 3) - 8;
        vsp = next;
        store64([next, 0], low);
    }
}

# Store the value on top of the operand stack into the field or element at `address`, whose storage is `kind`.
inline fn pop_into_storage(address: u64, kind: u64) {
    let low = pop();
    if kind == STORAGE_V128 {
        let high = pop();
        store64([address, 0], low);
        store64([address, 8], high);
    } else {
        store_number(address, kind, low);
    }
}

# The value of a v128 or other element, as two halves.
inline fn pop_storage_value(kind: u64) -> (u64, u64) {
    let low = pop();
    let mut high: u64 = 0;
    if kind == STORAGE_V128 {
        high = pop();
    }
    (low, high)
}

inline fn store_storage_value(address: u64, kind: u64, low: u64, high: u64) {
    if kind == STORAGE_V128 {
        store64([address, 0], low);
        store64([address, 8], high);
    } else {
        store_number(address, kind, low);
    }
}

# The bytes an element of `kind` takes, in an array and in a data segment alike.
inline fn storage_size(kind: u64) -> u64 {
    let mut size: u64 = 8;
    if kind == STORAGE_I8 {
        size = 1;
    }
    if kind == STORAGE_I16 {
        size = 2;
    }
    if kind == STORAGE_32 {
        size = 4;
    }
    if kind == STORAGE_V128 {
        size = 16;
    }
    size
}

# Copy `count` elements of `kind` from a data segment's bytes at `source` into elements at `destination`, which
# are stored as the segment holds them.
inline fn copy_data_elements(destination: u64, source: u64, count: u64, kind: u64) {
    copy_bytes(destination, source, count * storage_size(kind));
}

inline fn non_null(reference: u64) -> u64 {
    guard reference != 0 else @cold {
        trap(TRAP_NULL_REFERENCE);
    };
    reference
}

inline fn array_length(array: u64) -> u64 {
    let mut length: u64;
    load64(length, [array, OBJECT_LENGTH_OFFSET]);
    length
}

inline fn object_data(object: u64) -> u64 {
    let mut data: u64;
    load64(data, [object, OBJECT_DATA_OFFSET]);
    data
}

inline fn array_element_kind(array: u64) -> u64 {
    let mut rtt: u64;
    load64(rtt, [array, 0]);
    let mut kinds: u64;
    load64(kinds, [rtt, RTT_FIELD_KINDS]);
    let mut kind: u64;
    load8(kind, [kinds, 0]);
    kind
}

inline fn check_array_range(start: u64, count: u64, length: u64) {
    let end = start + count;
    guard end >= start else @cold {
        trap(TRAP_ARRAY_OUT_OF_BOUNDS);
    };
    guard end <= length else @cold {
        trap(TRAP_ARRAY_OUT_OF_BOUNDS);
    };
}

# The bytes an element of `array` takes.
inline fn array_element_size(array: u64) -> u64 {
    let mut rtt: u64;
    load64(rtt, [array, 0]);
    let mut size: u64;
    load32(size, [rtt, RTT_SIZE]);
    size
}

inline fn element_address(array: u64, index: u64, size: u64) -> u64 {
    let data = object_data(array);
    data + (index * size)
}

# The two LEB128 immediates of a GC instruction, and the length of the instruction.
inline fn two_immediates() -> (u64, u64, u64) {
    let [bytes, offset] = prefixed_immediates();
    let [first, first_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let second_bytes: Sequence<u8> = alias(start + first_length);
    let [second, second_length] = read_leb(second_bytes);
    (first, second, offset + first_length + second_length)
}

inline fn one_immediate() -> (u64, u64) {
    let [bytes, offset] = prefixed_immediates();
    let [value, length] = read_leb(bytes);
    (value, offset + length)
}

handler StructNew() @opcode(0xfb, 0x0) {
    let [type_index, length] = one_immediate();
    let rtt = rtt_of_type(type_index);
    let mut size: u64;
    load32(size, [rtt, RTT_SIZE]);
    let object = allocate_object(rtt, size, 0);
    let data = object_data(object);
    let mut field_count: u64;
    load32(field_count, [rtt, RTT_FIELD_COUNT]);
    let mut offsets_pointer: u64;
    load64(offsets_pointer, [rtt, RTT_FIELD_OFFSETS]);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let offsets: Sequence<u32> = alias(offsets_pointer);
    let kinds: Sequence<u8> = alias(kinds_pointer);
    # The last field is on top.
    let mut index = field_count;
    while index != 0 {
        index = index - 1;
        let offset: u64 = alias(offsets[index]);
        let kind: u64 = alias(kinds[index]);
        pop_into_storage(data + offset, kind);
    }
    push(object);
    advance(length);
}

handler StructNewDefault() @opcode(0xfb, 0x1) {
    let [type_index, length] = one_immediate();
    let rtt = rtt_of_type(type_index);
    let mut size: u64;
    load32(size, [rtt, RTT_SIZE]);
    let object = allocate_object(rtt, size, 0);
    push(object);
    advance(length);
}

# The address and storage kind of field `field_index` of a struct of the module's type `type_index`.
inline fn field_of(object: u64, type_index: u64, field_index: u64) -> (u64, u64) {
    let rtt = rtt_of_type(type_index);
    let mut offsets_pointer: u64;
    load64(offsets_pointer, [rtt, RTT_FIELD_OFFSETS]);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let offsets: Sequence<u32> = alias(offsets_pointer);
    let kinds: Sequence<u8> = alias(kinds_pointer);
    let offset: u64 = alias(offsets[field_index]);
    let kind: u64 = alias(kinds[field_index]);
    let data = object_data(object);
    (data + offset, kind)
}

handler StructGet() @opcode(0xfb, 0x2) {
    let [type_index, field_index, length] = two_immediates();
    let object = non_null(unary_operand());
    let [address, kind] = field_of(object, type_index, field_index);
    push_stored(address, kind, 1);
    advance(length);
}

handler StructGetS() @opcode(0xfb, 0x3) {
    let [type_index, field_index, length] = two_immediates();
    let object = non_null(unary_operand());
    let [address, kind] = field_of(object, type_index, field_index);
    let value = load_stored(address, kind);
    let extended = sign_extend_packed(value, kind);
    set_top(extended);
    advance(length);
}

handler StructGetU() @opcode(0xfb, 0x4) {
    let [type_index, field_index, length] = two_immediates();
    let object = non_null(unary_operand());
    let [address, kind] = field_of(object, type_index, field_index);
    let value = load_stored(address, kind);
    set_top(value);
    advance(length);
}

handler StructSet() @opcode(0xfb, 0x5) {
    let [type_index, field_index, length] = two_immediates();
    let rtt = rtt_of_type(type_index);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let kinds: Sequence<u8> = alias(kinds_pointer);
    let kind: u64 = alias(kinds[field_index]);
    let [low, high] = pop_storage_value(kind);
    let object = non_null(pop());
    let [address, _] = field_of(object, type_index, field_index);
    store_storage_value(address, kind, low, high);
    advance(length);
}

# Allocate an array of `count` elements of the module's type `type_index`.
inline fn allocate_array(type_index: u64, count: u64) -> u64 {
    let rtt = rtt_of_type(type_index);
    let mut element_size: u64;
    load32(element_size, [rtt, RTT_SIZE]);
    let size = count * element_size;
    allocate_object(rtt, size, count)
}

handler ArrayNew() @opcode(0xfb, 0x6) {
    let [type_index, length] = one_immediate();
    let count = pop32();
    let wide_count: u64 = alias(count);
    let array = allocate_array(type_index, wide_count);
    let kind = array_element_kind(array);
    let size = array_element_size(array);
    let [low, high] = pop_storage_value(kind);
    let mut index: u64 = 0;
    while index < wide_count {
        let address = element_address(array, index, size);
        store_storage_value(address, kind, low, high);
        index = index + 1;
    }
    push(array);
    advance(length);
}

handler ArrayNewDefault() @opcode(0xfb, 0x7) {
    let [type_index, length] = one_immediate();
    let count = pop32();
    let wide_count: u64 = alias(count);
    let array = allocate_array(type_index, wide_count);
    push(array);
    advance(length);
}

handler ArrayNewFixed() @opcode(0xfb, 0x8) {
    let [type_index, count, length] = two_immediates();
    let array = allocate_array(type_index, count);
    let kind = array_element_kind(array);
    let size = array_element_size(array);
    # The last element is on top.
    let mut index = count;
    while index != 0 {
        index = index - 1;
        let address = element_address(array, index, size);
        pop_into_storage(address, kind);
    }
    push(array);
    advance(length);
}

handler ArrayNewData() @opcode(0xfb, 0x9) {
    let [type_index, data_index, length] = two_immediates();
    let count = pop32();
    let source = pop32();
    let wide_count: u64 = alias(count);
    let wide_source: u64 = alias(source);
    let rtt = rtt_of_type(type_index);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let mut kind: u64;
    load8(kind, [kinds_pointer, 0]);
    let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    let byte_count = wide_count * storage_size(kind);
    check_range(wide_source, byte_count, segment_length, TRAP_MEMORY_OUT_OF_BOUNDS);
    let array = allocate_array(type_index, wide_count);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    copy_data_elements(object_data(array), contents + wide_source, wide_count, kind);
    push(array);
    advance(length);
}

handler ArrayNewElem() @opcode(0xfb, 0xa) {
    let [type_index, element_index, length] = two_immediates();
    let count = pop32();
    let source = pop32();
    let wide_count: u64 = alias(count);
    let wide_source: u64 = alias(source);
    let segment = segment_at(INSTANCE_ELEMENT_SEGMENTS, element_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    check_range(wide_source, wide_count, segment_length, TRAP_TABLE_OUT_OF_BOUNDS);
    let array = allocate_array(type_index, wide_count);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    copy_elements(object_data(array), contents + (wide_source << 3), wide_count);
    push(array);
    advance(length);
}

# The address and kind of an element, after checking the array for null and the index against its length.
inline fn checked_element(array: u64, index: u64) -> (u64, u64) {
    let checked = non_null(array);
    let element_count = array_length(checked);
    guard index < element_count else @cold {
        trap(TRAP_ARRAY_OUT_OF_BOUNDS);
    };
    let kind = array_element_kind(checked);
    let address = element_address(checked, index, array_element_size(checked));
    (address, kind)
}

handler ArrayGet() @opcode(0xfb, 0xb) {
    let [_, length] = one_immediate();
    let [array, index] = binary_operands();
    let [address, kind] = checked_element(array, index);
    push_stored(address, kind, 1);
    advance(length);
}

handler ArrayGetS() @opcode(0xfb, 0xc) {
    let [_, length] = one_immediate();
    let [array, index] = binary_operands();
    let [address, kind] = checked_element(array, index);
    let value = load_stored(address, kind);
    let extended = sign_extend_packed(value, kind);
    set_top(extended);
    advance(length);
}

handler ArrayGetU() @opcode(0xfb, 0xd) {
    let [_, length] = one_immediate();
    let [array, index] = binary_operands();
    let [address, kind] = checked_element(array, index);
    let value = load_stored(address, kind);
    set_top(value);
    advance(length);
}

handler ArraySet() @opcode(0xfb, 0xe) {
    let [type_index, length] = one_immediate();
    let rtt = rtt_of_type(type_index);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let mut kind: u64;
    load8(kind, [kinds_pointer, 0]);
    let [low, high] = pop_storage_value(kind);
    let index = pop();
    let array = pop();
    let [address, _] = checked_element(array, index);
    store_storage_value(address, kind, low, high);
    advance(length);
}

handler ArrayLen() @opcode(0xfb, 0xf) {
    let [_, offset] = prefixed_immediates();
    let array = non_null(unary_operand());
    let element_count = array_length(array);
    set_top(element_count);
    advance(offset);
}

handler ArrayFill() @opcode(0xfb, 0x10) {
    let [type_index, length] = one_immediate();
    let rtt = rtt_of_type(type_index);
    let mut kinds_pointer: u64;
    load64(kinds_pointer, [rtt, RTT_FIELD_KINDS]);
    let mut kind: u64;
    load8(kind, [kinds_pointer, 0]);
    let count = pop();
    let [low, high] = pop_storage_value(kind);
    let start = pop();
    let array = non_null(pop());
    check_array_range(start, count, array_length(array));
    let size = array_element_size(array);
    let mut index: u64 = 0;
    while index < count {
        let address = element_address(array, start + index, size);
        store_storage_value(address, kind, low, high);
        index = index + 1;
    }
    advance(length);
}

handler ArrayCopy() @opcode(0xfb, 0x11) {
    let [_, _, length] = two_immediates();
    let count = pop();
    let source_start = pop();
    let source = non_null(pop());
    let destination_start = pop();
    let destination = non_null(pop());
    check_array_range(source_start, count, array_length(source));
    check_array_range(destination_start, count, array_length(destination));
    let kind = array_element_kind(destination);
    let size = storage_size(kind);
    let from = element_address(source, source_start, size);
    let to = element_address(destination, destination_start, size);
    copy_bytes(to, from, count * size);
    advance(length);
}

handler ArrayInitData() @opcode(0xfb, 0x12) {
    let [_, data_index, length] = two_immediates();
    let count = pop();
    let source = pop();
    let destination_start = pop();
    let array = non_null(pop());
    let kind = array_element_kind(array);
    check_array_range(destination_start, count, array_length(array));
    let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    let byte_count = count * storage_size(kind);
    check_range(source, byte_count, segment_length, TRAP_MEMORY_OUT_OF_BOUNDS);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    let to = element_address(array, destination_start, array_element_size(array));
    copy_data_elements(to, contents + source, count, kind);
    advance(length);
}

handler ArrayInitElem() @opcode(0xfb, 0x13) {
    let [_, element_index, length] = two_immediates();
    let count = pop();
    let source = pop();
    let destination_start = pop();
    let array = non_null(pop());
    check_array_range(destination_start, count, array_length(array));
    let segment = segment_at(INSTANCE_ELEMENT_SEGMENTS, element_index);
    let mut segment_length: u64;
    load64(segment_length, [segment, SEGMENT_LENGTH]);
    check_range(source, count, segment_length, TRAP_TABLE_OUT_OF_BOUNDS);
    let mut contents: u64;
    load64(contents, [segment, SEGMENT_CONTENTS]);
    let to = element_address(array, destination_start, 8);
    copy_elements(to, contents + (source << 3), count);
    advance(length);
}

# Whether `reference` matches the heap type encoded at `bytes`, with null matching if `nullable` is set, and the
# length of the encoding.
inline fn reference_matches(reference: u64, bytes: Sequence<u8>, nullable: u64) -> (u64, u64) {
    let first: u64 = alias(bytes[0]);
    let mut matches: u64 = 0;
    let mut length: u64 = 1;
    # Abstract heap types are single bytes from 0x40 to 0x7F, negative as s33 values.
    let biased = first - 0x40;
    if biased < 0x40 {
        if reference != 0 {
            let tag = reference & 3;
            let low_bit = reference & 1;
            # any, func, extern, and exn take any non-null reference of their hierarchy.
            if first == 0x6e {
                matches = 1;
            }
            if first == 0x70 {
                matches = 1;
            }
            if first == 0x6f {
                matches = 1;
            }
            if first == 0x69 {
                matches = 1;
            }
            if first == 0x6d {
                if low_bit != 0 {
                    matches = 1;
                }
                if tag == 0 {
                    matches = 1;
                }
            }
            if first == 0x6c {
                matches = low_bit;
            }
            if first == 0x6b {
                if tag == 0 {
                    let mut rtt: u64;
                    load64(rtt, [reference, 0]);
                    let mut kind: u64;
                    load32(kind, [rtt, RTT_KIND]);
                    if kind == RTT_KIND_STRUCT {
                        matches = 1;
                    }
                }
            }
            if first == 0x6a {
                if tag == 0 {
                    let mut rtt: u64;
                    load64(rtt, [reference, 0]);
                    let mut kind: u64;
                    load32(kind, [rtt, RTT_KIND]);
                    if kind == RTT_KIND_ARRAY {
                        matches = 1;
                    }
                }
            }
        }
    } else {
        let [type_index, index_length] = read_leb(bytes);
        length = index_length;
        if reference != 0 {
            let identity = identity_of_type(type_index);
            let target = rtt_of_identity(identity);
            let mut target_kind: u64;
            load32(target_kind, [target, RTT_KIND]);
            if target_kind == RTT_KIND_FUNCTION {
                let mut function_identity: u64;
                load32(function_identity, [reference, FUNCTION_TYPE_IDENTITY]);
                let actual = rtt_of_identity(function_identity);
                matches = rtt_is_subtype(actual, identity);
            } else {
                let tag = reference & 3;
                if tag == 0 {
                    let mut actual: u64;
                    load64(actual, [reference, 0]);
                    matches = rtt_is_subtype(actual, identity);
                }
            }
        }
    }
    if reference == 0 {
        matches = nullable;
    }
    (matches, length)
}

inline fn test_reference(nullable: u64) {
    let [bytes, offset] = prefixed_immediates();
    let reference = unary_operand();
    let [matches, length] = reference_matches(reference, bytes, nullable);
    set_top(matches);
    advance(offset + length);
}

inline fn cast_reference(nullable: u64) {
    let [bytes, offset] = prefixed_immediates();
    let reference = unary_operand();
    let [matches, length] = reference_matches(reference, bytes, nullable);
    guard matches != 0 else @cold {
        trap(TRAP_CAST_FAILURE);
    };
    advance(offset + length);
}

handler RefTest() @opcode(0xfb, 0x14) {
    test_reference(0);
}

handler RefTestNull() @opcode(0xfb, 0x15) {
    test_reference(1);
}

handler RefCast() @opcode(0xfb, 0x16) {
    cast_reference(0);
}

handler RefCastNull() @opcode(0xfb, 0x17) {
    cast_reference(1);
}

# br_on_cast and br_on_cast_fail: `0xFB sub flags label source target`. The branch is taken when the cast's outcome
# is `taken_on`.
inline fn branch_on_cast(taken_on: u64) {
    let [bytes, offset] = prefixed_immediates();
    let flags: u64 = alias(bytes[0]);
    let start: u64 = alias(bytes);
    let label_bytes: Sequence<u8> = alias(start + 1);
    let label_length = leb_length(label_bytes);
    let source_bytes: Sequence<u8> = alias(start + 1 + label_length);
    let source_length = heap_type_length(source_bytes);
    let target_bytes: Sequence<u8> = alias(start + 1 + label_length + source_length);
    let reference = unary_operand();
    let nullable = (flags >> 1) & 1;
    let [matches, target_length] = reference_matches(reference, target_bytes, nullable);
    if matches == taken_on {
        branch();
    }
    skip_entry_and_advance(offset + 1 + label_length + source_length + target_length);
}

inline fn heap_type_length(bytes: Sequence<u8>) -> u64 {
    let first: u64 = alias(bytes[0]);
    let mut length: u64 = 1;
    let biased = first - 0x40;
    if biased >= 0x40 {
        length = leb_length(bytes);
    }
    length
}

handler BrOnCast() @opcode(0xfb, 0x18) {
    branch_on_cast(1);
}

handler BrOnCastFail() @opcode(0xfb, 0x19) {
    branch_on_cast(0);
}

# References keep their bits between the any and extern hierarchies.
handler AnyConvertExtern() @opcode(0xfb, 0x1a) {
    let [_, offset] = prefixed_immediates();
    advance(offset);
}

handler ExternConvertAny() @opcode(0xfb, 0x1b) {
    let [_, offset] = prefixed_immediates();
    advance(offset);
}

handler RefI31() @opcode(0xfb, 0x1c) {
    let [_, offset] = prefixed_immediates();
    let value = unary_operand();
    let bits = (value & 0x7fffffff) << 1;
    let reference = bits | 1;
    set_top(reference);
    advance(offset);
}

handler I31GetS() @opcode(0xfb, 0x1d) {
    let [_, offset] = prefixed_immediates();
    let reference = non_null(unary_operand());
    let moved = reference << 32;
    let signed = signed64(moved);
    let extended = signed >> 33;
    let narrow = u32(extended);
    let value: u64 = alias(narrow);
    set_top(value);
    advance(offset);
}

handler I31GetU() @opcode(0xfb, 0x1e) {
    let [_, offset] = prefixed_immediates();
    let reference = non_null(unary_operand());
    let value = (reference >> 1) & 0x7fffffff;
    set_top(value);
    advance(offset);
}

# Atomics (the 0xFE prefix). Their accesses are always bounds checked and then checked for alignment, in that order,
# whatever memory they access.

handler PrefixFE() @opcode(0xfe) {
    let [sub_opcode, _] = read_leb(immediates());
    dispatch_secondary(0xfe, sub_opcode);
}

# The memory, address of a `size`-byte atomic access, and length of the instruction.
inline fn atomic_access(address_operand: u64, size: u64) -> (u64, u64, u64) {
    let [bytes, prefix_length] = prefixed_immediates();
    # The flags may take more than one byte; their bit 6 says a memory index follows.
    let [flags, alignment_length] = read_leb(bytes);
    let start: u64 = alias(bytes);
    let mut memory_index: u64 = 0;
    let mut index_length: u64 = 0;
    let has_memory_index = flags & 0x40;
    if has_memory_index != 0 {
        let index_bytes: Sequence<u8> = alias(start + alignment_length);
        let [decoded_index, decoded_length] = read_leb(index_bytes);
        memory_index = decoded_index;
        index_length = decoded_length;
    }
    let offset_bytes: Sequence<u8> = alias(start + alignment_length + index_length);
    let [offset, offset_length] = read_leb(offset_bytes);
    let memory = memory_at(memory_index);
    let effective = address_operand + offset;
    guard effective >= address_operand else @cold {
        trap(TRAP_MEMORY_OUT_OF_BOUNDS);
    };
    let address = checked_memory_address(memory, effective, size);
    let misalignment = effective & (size - 1);
    guard misalignment == 0 else @cold {
        trap(TRAP_UNALIGNED_ATOMIC);
    };
    (memory, address, prefix_length + alignment_length + index_length + offset_length)
}

inline fn width_mask(size: u64) -> u64 {
    let mut mask: u64 = 0xffffffffffffffff;
    if size == 1 {
        mask = 0xff;
    }
    if size == 2 {
        mask = 0xffff;
    }
    if size == 4 {
        mask = 0xffffffff;
    }
    mask
}

handler AtomicNotify() @opcode(0xfe, 0x0) {
    let count = pop();
    let address_operand = unary_operand();
    let [memory, address, length] = atomic_access(address_operand, 4);
    let mut shared: u64;
    load64(shared, [memory, MEMORY_SHARED]);
    let mut woken: u64 = 0;
    # Only a shared memory can have threads waiting on it.
    if shared != 0 {
        let context_pointer = context();
        store64([context_pointer, CONTEXT_ARGUMENTS], address);
        store64([context_pointer, CONTEXT_ARGUMENT_1], count);
        woken = call_helper(wasm_runtime_notify, context_pointer);
    }
    set_top(woken);
    advance(length);
}

inline fn atomic_wait(size: u64) {
    let timeout = pop();
    let expected = pop();
    let address_operand = unary_operand();
    let [memory, address, length] = atomic_access(address_operand, size);
    let mut shared: u64;
    load64(shared, [memory, MEMORY_SHARED]);
    guard shared != 0 else @cold {
        trap(TRAP_EXPECTED_SHARED_MEMORY);
    };
    let context_pointer = context();
    store64([context_pointer, CONTEXT_ARGUMENTS], address);
    store64([context_pointer, CONTEXT_ARGUMENT_1], expected);
    store64([context_pointer, CONTEXT_ARGUMENT_2], timeout);
    store64([context_pointer, CONTEXT_ARGUMENT_3], size);
    let result: u64 = call_helper(wasm_runtime_wait, context_pointer);
    guard result != WAIT_CANNOT_BLOCK else @cold {
        trap(TRAP_RUNTIME);
    };
    set_top(result);
    advance(length);
}

handler AtomicWait32() @opcode(0xfe, 0x1) {
    atomic_wait(4);
}

handler AtomicWait64() @opcode(0xfe, 0x2) {
    atomic_wait(8);
}

# Locked instructions order everything around them, so a fence needs nothing more.
handler AtomicFence() @opcode(0xfe, 0x3) {
    let [_, offset] = prefixed_immediates();
    atomic_fence();
    advance(offset + 1);
}

handler I32AtomicLoad() @opcode(0xfe, 0x10) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 4);
    let value = atomic_load32([address, 0]);
    set_top(value);
    advance(length);
}

handler I64AtomicLoad() @opcode(0xfe, 0x11) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 8);
    let value = atomic_load64([address, 0]);
    set_top(value);
    advance(length);
}

handler I32AtomicLoad8U() @opcode(0xfe, 0x12) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 1);
    let value = atomic_load8([address, 0]);
    set_top(value);
    advance(length);
}

handler I32AtomicLoad16U() @opcode(0xfe, 0x13) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 2);
    let value = atomic_load16([address, 0]);
    set_top(value);
    advance(length);
}

handler I64AtomicLoad8U() @opcode(0xfe, 0x14) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 1);
    let value = atomic_load8([address, 0]);
    set_top(value);
    advance(length);
}

handler I64AtomicLoad16U() @opcode(0xfe, 0x15) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 2);
    let value = atomic_load16([address, 0]);
    set_top(value);
    advance(length);
}

handler I64AtomicLoad32U() @opcode(0xfe, 0x16) {
    let address_operand = unary_operand();
    let [_, address, length] = atomic_access(address_operand, 4);
    let value = atomic_load32([address, 0]);
    set_top(value);
    advance(length);
}

handler I32AtomicStore() @opcode(0xfe, 0x17) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg32([address, 0], value);
    advance(length);
}

handler I64AtomicStore() @opcode(0xfe, 0x18) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg64([address, 0], value);
    advance(length);
}

handler I32AtomicStore8() @opcode(0xfe, 0x19) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg8([address, 0], value);
    advance(length);
}

handler I32AtomicStore16() @opcode(0xfe, 0x1a) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg16([address, 0], value);
    advance(length);
}

handler I64AtomicStore8() @opcode(0xfe, 0x1b) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg8([address, 0], value);
    advance(length);
}

handler I64AtomicStore16() @opcode(0xfe, 0x1c) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg16([address, 0], value);
    advance(length);
}

handler I64AtomicStore32() @opcode(0xfe, 0x1d) {
    let [address_operand, value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    # An exchange is a sequentially consistent store.
    let _ = atomic_xchg32([address, 0], value);
    advance(length);
}

handler I32AtomicRmwAdd() @opcode(0xfe, 0x1e) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let old = atomic_add32([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmwAdd() @opcode(0xfe, 0x1f) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    let old = atomic_add64([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmw8AddU() @opcode(0xfe, 0x20) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let old = atomic_add8([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmw16AddU() @opcode(0xfe, 0x21) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let old = atomic_add16([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw8AddU() @opcode(0xfe, 0x22) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let old = atomic_add8([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw16AddU() @opcode(0xfe, 0x23) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let old = atomic_add16([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw32AddU() @opcode(0xfe, 0x24) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let old = atomic_add32([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmwSub() @opcode(0xfe, 0x25) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add32([address, 0], negated);
    push(old);
    advance(length);
}

handler I64AtomicRmwSub() @opcode(0xfe, 0x26) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add64([address, 0], negated);
    push(old);
    advance(length);
}

handler I32AtomicRmw8SubU() @opcode(0xfe, 0x27) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add8([address, 0], negated);
    push(old);
    advance(length);
}

handler I32AtomicRmw16SubU() @opcode(0xfe, 0x28) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add16([address, 0], negated);
    push(old);
    advance(length);
}

handler I64AtomicRmw8SubU() @opcode(0xfe, 0x29) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add8([address, 0], negated);
    push(old);
    advance(length);
}

handler I64AtomicRmw16SubU() @opcode(0xfe, 0x2a) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add16([address, 0], negated);
    push(old);
    advance(length);
}

handler I64AtomicRmw32SubU() @opcode(0xfe, 0x2b) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let zero: u64 = 0;
    let negated = zero - value;
    let old = atomic_add32([address, 0], negated);
    push(old);
    advance(length);
}

handler I32AtomicRmwAnd() @opcode(0xfe, 0x2c) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmwAnd() @opcode(0xfe, 0x2d) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load64(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg64([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw8AndU() @opcode(0xfe, 0x2e) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw16AndU() @opcode(0xfe, 0x2f) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw8AndU() @opcode(0xfe, 0x30) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw16AndU() @opcode(0xfe, 0x31) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw32AndU() @opcode(0xfe, 0x32) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old & value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmwOr() @opcode(0xfe, 0x33) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmwOr() @opcode(0xfe, 0x34) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load64(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg64([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw8OrU() @opcode(0xfe, 0x35) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw16OrU() @opcode(0xfe, 0x36) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw8OrU() @opcode(0xfe, 0x37) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw16OrU() @opcode(0xfe, 0x38) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw32OrU() @opcode(0xfe, 0x39) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old | value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmwXor() @opcode(0xfe, 0x3a) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmwXor() @opcode(0xfe, 0x3b) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load64(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg64([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw8XorU() @opcode(0xfe, 0x3c) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmw16XorU() @opcode(0xfe, 0x3d) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw8XorU() @opcode(0xfe, 0x3e) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load8(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg8([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw16XorU() @opcode(0xfe, 0x3f) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load16(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg16([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I64AtomicRmw32XorU() @opcode(0xfe, 0x40) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    # Retry until no other thread changed the location between the read and the exchange.
    let mut old: u64;
    load32(old, [address, 0]);
    let mut done: u64 = 0;
    while done == 0 {
        let updated = (old ^ value) & mask;
        let replaced = atomic_cmpxchg32([address, 0], old, updated);
        if replaced == old {
            done = 1;
        } else {
            old = replaced;
        }
    }
    push(old);
    advance(length);
}

handler I32AtomicRmwXchg() @opcode(0xfe, 0x41) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let old = atomic_xchg32([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmwXchg() @opcode(0xfe, 0x42) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    let mask = width_mask(8);
    let value = raw_value & mask;
    let old = atomic_xchg64([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmw8XchgU() @opcode(0xfe, 0x43) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let old = atomic_xchg8([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmw16XchgU() @opcode(0xfe, 0x44) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let old = atomic_xchg16([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw8XchgU() @opcode(0xfe, 0x45) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    let mask = width_mask(1);
    let value = raw_value & mask;
    let old = atomic_xchg8([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw16XchgU() @opcode(0xfe, 0x46) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    let mask = width_mask(2);
    let value = raw_value & mask;
    let old = atomic_xchg16([address, 0], value);
    push(old);
    advance(length);
}

handler I64AtomicRmw32XchgU() @opcode(0xfe, 0x47) {
    let [address_operand, raw_value] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    let mask = width_mask(4);
    let value = raw_value & mask;
    let old = atomic_xchg32([address, 0], value);
    push(old);
    advance(length);
}

handler I32AtomicRmwCmpxchg() @opcode(0xfe, 0x48) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    # Both values wrap at the access width.
    let mask = width_mask(4);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg32([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I64AtomicRmwCmpxchg() @opcode(0xfe, 0x49) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 8);
    # Both values wrap at the access width.
    let mask = width_mask(8);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg64([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I32AtomicRmw8CmpxchgU() @opcode(0xfe, 0x4a) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    # Both values wrap at the access width.
    let mask = width_mask(1);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg8([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I32AtomicRmw16CmpxchgU() @opcode(0xfe, 0x4b) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    # Both values wrap at the access width.
    let mask = width_mask(2);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg16([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I64AtomicRmw8CmpxchgU() @opcode(0xfe, 0x4c) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 1);
    # Both values wrap at the access width.
    let mask = width_mask(1);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg8([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I64AtomicRmw16CmpxchgU() @opcode(0xfe, 0x4d) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 2);
    # Both values wrap at the access width.
    let mask = width_mask(2);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg16([address, 0], expected, wrapped);
    push(old);
    advance(length);
}

handler I64AtomicRmw32CmpxchgU() @opcode(0xfe, 0x4e) {
    let replacement = pop();
    let [address_operand, raw_expected] = store_operands();
    let [_, address, length] = atomic_access(address_operand, 4);
    # Both values wrap at the access width.
    let mask = width_mask(4);
    let expected = raw_expected & mask;
    let wrapped = replacement & mask;
    let old = atomic_cmpxchg32([address, 0], expected, wrapped);
    push(old);
    advance(length);
}