Lines
95.33 %
Functions
70 %
Branches
100 %
//! Identity against an `AnyRef` operand. `AnyRef` is the ADR-0025 widen
//! target and can box a value of any other type, so it is never a disjoint
//! stratum: the test discriminates the boxed value's runtime shape.
use wasm_encoder::{BlockType, ValType};
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{NilRepr, classify_stack_type, emit_box_anyref, nil_repr};
use crate::error::{Error, Result};
use super::IdentityOperands;
use crate::compiler::context::ShapeCompare;
/// Emits the test when exactly one operand classifies as `AnyRef`;
/// `Ok(None)` otherwise. `classify_stack_type` also types a literal
/// operand, which `Expr::wasm_type` would miss.
pub(super) fn try_emit_anyref_identity(
ops: &mut impl IdentityOperands,
a: &Expr,
b: &Expr,
structural: bool,
) -> Result<Option<WasmType>> {
match (classify_stack_type(a), classify_stack_type(b)) {
(Some(WasmType::AnyRef), Some(WasmType::AnyRef)) => {
emit_anyref_vs_anyref(ops, structural).map(Some)
}
(Some(WasmType::Bool), Some(WasmType::AnyRef)) => emit_bool_anyref(ops, true).map(Some),
(Some(WasmType::AnyRef), Some(WasmType::Bool)) => emit_bool_anyref(ops, false).map(Some),
(Some(ty), Some(WasmType::AnyRef)) if ty != WasmType::AnyRef => {
emit_concrete_vs_anyref(ops, true, structural).map(Some)
(Some(WasmType::AnyRef), Some(ty)) if ty != WasmType::AnyRef => {
emit_concrete_vs_anyref(ops, false, structural).map(Some)
_ => Ok(None),
/// A `Bool` against an `AnyRef`: boxing the `Bool` yields the canonical
/// `$t` / null an `AnyRef` holds for it, so `ref.eq` is the whole test.
fn emit_bool_anyref(ops: &mut impl IdentityOperands, bool_is_first: bool) -> Result<WasmType> {
if bool_is_first {
ops.stack(0)?;
box_bool(ops);
ops.stack(1)?;
ops.emit().ref_cast_eq();
} else {
let anyref_local = ops.ctx().alloc_local(WasmType::AnyRef)?;
ops.emit().local_set(anyref_local);
let emit = ops.emit();
emit.local_get(anyref_local);
emit.ref_cast_eq();
ops.emit().ref_eq();
Ok(WasmType::Bool)
fn box_bool(ops: &mut impl IdentityOperands) {
let (ctx, emit) = ops.parts();
emit_box_anyref(ctx, emit, WasmType::Bool);
/// A concrete-typed operand against an `AnyRef`: equal when the `AnyRef`
/// holds the same shape and an equal value. The concrete local is sized from
/// the type the operand actually lowers to, never from its classification,
/// which can differ (a `SETF` coercing its right-hand side).
fn emit_concrete_vs_anyref(
concrete_is_first: bool,
) -> Result<WasmType> {
let first = operand_local(ops, 0, concrete_is_first)?;
let second = operand_local(ops, 1, !concrete_is_first)?;
let ((concrete_local, concrete_ty), (anyref_local, _)) = if concrete_is_first {
(first, second)
(second, first)
};
let locals = Locals {
concrete: concrete_local,
concrete_ty,
anyref: anyref_local,
structural,
match nil_repr(&concrete_ty) {
NilRepr::Never => emit_never_nil_vs_anyref(ctx, emit, locals),
NilRepr::Null => emit_nilable_vs_anyref(ctx, emit, locals),
NilRepr::I32Zero => Err(Error::Compile(
"internal: a Bool against AnyRef must use the boxed identity test".to_string(),
)),
/// Emits operand `index` into a fresh local typed as it lowers (the
/// concrete side) or as `AnyRef`.
fn operand_local(
index: usize,
concrete: bool,
) -> Result<(u32, WasmType)> {
let lowered = ops.stack(index)?;
let ty = if concrete { lowered } else { WasmType::AnyRef };
let local = ops.ctx().alloc_local(ty)?;
ops.emit().local_set(local);
Ok((local, ty))
#[derive(Clone, Copy)]
struct Locals {
concrete: u32,
concrete_ty: WasmType,
anyref: u32,
/// The concrete side is never nil: a null `AnyRef` is `nil` (checked first,
/// since `ratio_eq` / `commodity_eq` trap on null), otherwise shape test and
/// value compare.
fn emit_never_nil_vs_anyref(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
locals: Locals,
emit.local_get(locals.anyref);
emit.ref_is_null();
emit.if_block(BlockType::Result(ValType::I32));
emit.i32_const(0);
emit.else_block();
emit_type_test_and_compare(ctx, emit, locals)?;
emit.block_end()?;
/// The concrete side may be nil: then equal exactly when the `AnyRef` is
/// nil too; otherwise as for a never-nil side.
fn emit_nilable_vs_anyref(
emit.local_get(locals.concrete);
emit_never_nil_vs_anyref(ctx, emit, locals)?;
/// Two `AnyRef`s: equal by their runtime shapes; lists deeply when
/// `structural`.
fn emit_anyref_vs_anyref(ops: &mut impl IdentityOperands, structural: bool) -> Result<WasmType> {
emit.i32_const(i32::from(structural));
emit.call(ctx.ids.anyref_equal);
/// `ref.test`s the non-null `AnyRef` against the concrete type's
/// representation and compares values on a match; `nil` otherwise. An index
/// is boxed as `i31ref`, so it has its own test.
fn emit_type_test_and_compare(
) -> Result<()> {
let i32_result = BlockType::Result(ValType::I32);
if locals.concrete_ty == WasmType::I32 {
emit.ref_test_i31();
emit.if_block(i32_result);
emit.ref_cast_i31();
emit.i31_get_s();
emit.i32_eq();
return Ok(());
let Some(ShapeCompare { shape, call }) =
ctx.shape_compare(locals.concrete_ty, locals.structural)?
else {
return Err(Error::Compile(format!(
"internal: {} has no reference shape test against AnyRef",
locals.concrete_ty
)));
let type_idx = shape;
emit.ref_test(type_idx);
emit.ref_cast_nullable(type_idx);
match call {
Some(fn_idx) => emit.call(fn_idx),
None => emit.ref_eq(),
Ok(())