Lines
98.58 %
Functions
100 %
Branches
//! Structural `EQUAL` helpers. `anyref_equal` compares two boxed values by
//! their runtime shape; `pair_equal` walks two lists, comparing the cars with
//! it. [`CompileContext::shape_compare`] is the one shape-to-comparison table
//! the generated helpers and the identity emitter share.
use super::CompileContext;
use crate::ast::WasmType;
use crate::error::Result;
use wasm_encoder::{AbstractHeapType, BlockType, Function, HeapType, Instruction, ValType};
/// How two values of one reference shape compare once both have it.
pub(crate) struct ShapeCompare {
pub shape: u32,
/// The helper comparing the values; `None` when identity is the test.
pub call: Option<u32>,
}
const I32_RESULT: BlockType = BlockType::Result(ValType::I32);
const I31: HeapType = HeapType::Abstract {
shared: false,
ty: AbstractHeapType::I31,
};
const EQ: HeapType = HeapType::Abstract {
ty: AbstractHeapType::Eq,
impl CompileContext {
/// `None` for a type with no reference shape (`I32` rides `i31`, `Bool`
/// is `t` / `nil`).
pub(crate) fn shape_compare(
&self,
ty: WasmType,
structural: bool,
) -> Result<Option<ShapeCompare>> {
let (shape, call) = match ty {
WasmType::Ratio => (self.ids.ty_ratio, Some(self.ids.ratio_eq)),
WasmType::Commodity => (self.ids.ty_commodity, Some(self.ids.commodity_eq)),
WasmType::StringRef => (self.ids.ty_i8_array, Some(self.ids.string_eq)),
WasmType::SymbolRef => (self.ids.ty_symbol, Some(self.ids.symbol_eq)),
WasmType::PairRef(_) => (self.ids.ty_pair, structural.then_some(self.ids.pair_equal)),
WasmType::EntityRef(kind) => (self.ids.entity_type(kind), None),
WasmType::StructRef(_) => (self.ids.ty_struct, None),
WasmType::Closure(sig) => (self.closure_sig(sig)?.closure_type_idx, None),
WasmType::I32 | WasmType::Bool | WasmType::AnyRef => return Ok(None),
Ok(Some(ShapeCompare { shape, call }))
pub(super) fn declare_pair_equal(&mut self) -> Result<()> {
let (pair, anyref) = (self.pair_ref(), self.anyref());
self.register_function("pair_equal", &[pair, pair], &[ValType::I32])?;
self.register_function(
"anyref_equal",
&[anyref, anyref, ValType::I32],
&[ValType::I32],
)
.map(drop)
pub(super) fn build_pair_equal(&mut self) -> Result<()> {
self.build_pair_equal_body();
self.build_anyref_equal_body()
/// Walks both lists: equal when they end together and every car pair is
/// `anyref_equal` (deep).
fn build_pair_equal_body(&mut self) {
const A: u32 = 0;
const B: u32 = 1;
let pair_idx = self.ids.ty_pair;
let field = |list: u32, field_index: u32, f: &mut Function| {
f.instruction(&Instruction::LocalGet(list));
f.instruction(&Instruction::StructGet {
struct_type_index: pair_idx,
field_index,
});
let mut f = Function::new([]);
f.instruction(&Instruction::Loop(BlockType::Empty));
for (this, other) in [(A, B), (B, A)] {
f.instruction(&Instruction::LocalGet(this));
f.instruction(&Instruction::RefIsNull);
f.instruction(&Instruction::If(BlockType::Empty));
f.instruction(&Instruction::LocalGet(other));
f.instruction(&Instruction::Return);
f.instruction(&Instruction::End);
field(A, 0, &mut f);
field(B, 0, &mut f);
f.instruction(&Instruction::I32Const(1));
f.instruction(&Instruction::Call(self.ids.anyref_equal));
f.instruction(&Instruction::I32Eqz);
f.instruction(&Instruction::I32Const(0));
for list in [A, B] {
field(list, 1, &mut f);
f.instruction(&Instruction::LocalSet(list));
f.instruction(&Instruction::Br(0));
self.pending_helpers.push(f);
/// `(a, b, deep)`: `nil` equals only `nil`; an index compares as `i31`; a
/// shape with a value comparison compares by it; lists compare
/// structurally when `deep`, else by identity; anything else by identity.
fn build_anyref_equal_body(&mut self) -> Result<()> {
const DEEP: u32 = 2;
f.instruction(&Instruction::LocalGet(A));
f.instruction(&Instruction::If(I32_RESULT));
f.instruction(&Instruction::LocalGet(B));
f.instruction(&Instruction::Else);
let mut open = 1;
open += self.emit_shape_arm(&mut f, I31, |f| {
for value in [A, B] {
f.instruction(&Instruction::LocalGet(value));
f.instruction(&Instruction::RefCastNonNull(I31));
f.instruction(&Instruction::I31GetS);
f.instruction(&Instruction::I32Eq);
for ty in [
WasmType::Ratio,
WasmType::Commodity,
WasmType::StringRef,
WasmType::SymbolRef,
] {
if let Some(ShapeCompare {
shape,
call: Some(call),
}) = self.shape_compare(ty, true)?
{
open += self.emit_shape_arm(&mut f, HeapType::Concrete(shape), |f| {
cast_both(f, shape);
f.instruction(&Instruction::Call(call));
let pair_equal = self.ids.pair_equal;
open += self.emit_shape_arm(&mut f, HeapType::Concrete(pair_idx), |f| {
f.instruction(&Instruction::LocalGet(DEEP));
cast_both(f, pair_idx);
f.instruction(&Instruction::Call(pair_equal));
f.instruction(&Instruction::RefEq);
f.instruction(&Instruction::RefCastNonNull(EQ));
f.instruction(&Instruction::RefCastNullable(EQ));
for _ in 0..open {
Ok(())
/// Opens one `if` arm of the cascade: when both values have `shape`,
/// `compare` runs on them (it casts them itself).
/// Returns the number of arms left open for the caller to close.
fn emit_shape_arm(
f: &mut Function,
shape: HeapType,
compare: impl FnOnce(&mut Function),
) -> usize {
for value in [0, 1] {
f.instruction(&Instruction::RefTestNonNull(shape));
compare(f);
1
fn cast_both(f: &mut Function, shape_idx: u32) {
f.instruction(&Instruction::RefCastNonNull(HeapType::Concrete(shape_idx)));