compiler 0.0.2-preview.1

The Zena self-hosted compiler

Readme Files Dependencies Versions

compiler#

The Zena compiler, written in Zena. It parses, type-checks, and compiles Zena source code to WebAssembly (Wasm GC) and WebAssembly Text (WAT).

Usage#

Parsing a Single File#

Use compiler/parser to parse Zena source text into an AST (Module):

import { parse } from 'compiler/parser';

let ast = parse('export let answer = 42;', 'example.zena');

Loading and Checking a Program#

Use compiler (createCompiler and checkCompilation) with a CompilerHost to resolve imports across a module graph and type-check a program:

import {
  createCompiler, checkCompilation, buildStdlibConfig
} from 'compiler';
import { FsCompilerHost } from 'compiler/fs-host';

let stdlibRoot = '/stdlib';
let host = new FsCompilerHost(stdlibRoot);
let packages = buildStdlibConfig(stdlibRoot, 'host');
let compiler = createCompiler(host, {
  stdlibRoot,
  packages,
});

let result = await compiler.compile({
  entry: '/workspace/main.zena',
  target: 'host',
});
let checked = checkCompilation(result);

Compiling a File to WebAssembly#

Use compiler/compile-file to compile an entry file on disk to a .wasm or .wat output file:

import { compileFile } from 'compiler/compile-file';

let ok = await compileFile({
  entry: 'src/main.zena',
  outPath: 'out/main.wasm',
  stdlibRoot: '/stdlib',
});

Libraries#

  • compiler / compiler/compiler — compiler entry point (Compiler, createCompiler, checkCompilation, ProgramCheckResult)
  • compiler/compile-file — end-to-end file compilation (compileFile, CompileFileOptions)
  • compiler/tokenizer — lexer and token definitions (tokenize, Token, TokenType)
  • compiler/parser — recursive-descent parser (parse, Parser)
  • compiler/ast — AST node definitions (Node, Module, Statement, Expression, TypeAnnotation)
  • compiler/visitor — AST traversal helpers (walk, Visitor)
  • compiler/scope — lexical scope and symbol resolution (ScopeBuilder, ScopeResult, Symbol)
  • compiler/checker — static type checker (checkModule, CheckResult)
  • compiler/semantic-model — semantic queries over a checked module (SemanticModel)
  • compiler/types — compiler type representations (Type, ClassType, FunctionType, typeToString)
  • compiler/diagnostics — compiler diagnostics and source spans (Diagnostic, DiagnosticCode, DiagnosticSeverity, SourceSpan)
  • compiler/package-manifest — zena-package.json manifest and workspace loader (loadWorkspace, loadPackageMap)
  • compiler/module-resolver — package and relative module specifier resolution (ModuleResolver, PackageConfig)
  • compiler/library-loader — module graph loader (LibraryLoader, CompilerHost, SourceFile)
  • compiler/codegen/binary-generator — WebAssembly GC binary emitter (BinaryGenerator)
  • compiler/codegen/wat-generator — WebAssembly Text (WAT) emitter (WatGenerator)

License#

MIT

47.4 KiB Raw
/**
 * Scalar and reference operator lowering, split out of lowering.zena:
 * binary operators (with the ==-dispatch ladder: null compares,
 * identity, class operator members, erased/identity/reference
 * equality tiers, numeric promotion), unary operators, scalar `as`
 * casts, and the operator->IrOp table. Env-coupled pieces stay
 * behind LoweringContext closures: `??` (env merges), class operator
 * member calls, and fat-pointer unwrapping. Bodies are streaming's
 * shapes exactly.
 */
import {
  AsExpression, BinaryExpression, Expression, NullLiteral,
  NumberLiteral, UnaryExpression,
} from '../../ast.zena';
import {
  AnyRefType, BooleanType, ClassType, EqRefType, F32Type, F64Type,
  I32Type, I64Type, InterfaceType, NullType, RecordType, Type, TypeParameterType,
  TupleType, U32Type, U64Type, UnionType, baseType,
  typeToString, isNarrowIntType, isUnsignedIntType, narrowIntBits,
  isVoidOrNeverType,
} from '../../types.zena';
import {
  ValType, ValTypeAnyRef, ValTypeExternRef, ValTypeF32, ValTypeF64, ValTypeI32,
  ValTypeI64, ValTypeNullRef, ValTypeRef, ValTypeRefExtern, ValTypeRefNull, ValTypeV128,
  isExternValType, isRefValType, refTargetOf, valTypeEquals,
} from '../wasm.zena';
import { WasmStruct } from '../wasm-module.zena';
import { InlineLayout, eraseTypeParameters, getNonNullableType, inlineLayoutOf, typeToValTypeWithSub } from '../type-mapping.zena';
import { getClassKey, parseFloatLiteral, parseI64OrU64Literal, unwrapAliases } from '../codegen-utils.zena';
import { IrOp } from './ir.zena';
import {
  LoweringContext, ZirUnsupported, noValues, oneValue, twoValues,
} from './lowering-context.zena';
import { lowerClassEquality, lowerErasedEq, lowerInlineEq, lowerValueEq } from './equality.zena';
import { valueEqOperand } from '../value-equality.zena';
import { lowerNullishCoalesce } from './control-flow.zena';
import { lowerBigIntLiteral } from './templates.zena';
import { exactLiteralFields, isTupleTag, lowerExpr, lowerIntoSlots, tupleTagTest } from './lowering.zena';
import { growable } from 'zena:core';


/** Bail with the lowering-side message shape (reason @function). */
function bail(cx: LoweringContext, reason: String): never {
  throw new ZirUnsupported(`${reason} @${cx.func.name ?? '?'}`);
}

/** Base type of the substituted checked type (unions unwrapped). */
function substitutedBaseType(cx: LoweringContext, expr: Expression): Type | null {
  let t = cx.substitutedNodeType(expr);
  if (t == null) {
    return null;
  }
  return baseType(t);
}

/** Whether the operand's checked type is unsigned (name-driven: only
 * these pick the _u operator variants). u8/u16 count — they are held
 * zero-extended, so the unsigned comparison is the correct one even
 * though the operands have already widened to i32. */
export function isUnsignedNode(cx: LoweringContext, expr: Expression): boolean {
  let bt = substitutedBaseType(cx, expr);
  return bt != null && isUnsignedIntType(bt);
}

export function mapBinaryOp(cx: LoweringContext, operator: String, vt: ValType,
    unsigned: boolean): IrOp {
  if (vt is ValTypeI32) {
    if (operator == "+") { return IrOp.i32_add; }
    if (operator == "-") { return IrOp.i32_sub; }
    if (operator == "*") { return IrOp.i32_mul; }
    // No "/" here: Zena division always yields a float (the checker
    // promotes i32/i32 to f32), which lowering does not model yet.
    if (operator == "%") { return if (unsigned) IrOp.i32_rem_u else IrOp.i32_rem_s; }
    if (operator == "&") { return IrOp.i32_and; }
    if (operator == "|") { return IrOp.i32_or; }
    if (operator == "^") { return IrOp.i32_xor; }
    if (operator == "<<") { return IrOp.i32_shl; }
    if (operator == ">>") { return if (unsigned) IrOp.i32_shr_u else IrOp.i32_shr_s; }
    if (operator == ">>>") { return IrOp.i32_shr_u; }
    if (operator == "==") { return IrOp.i32_eq; }
    if (operator == "!=") { return IrOp.i32_ne; }
    if (operator == "<") { return if (unsigned) IrOp.i32_lt_u else IrOp.i32_lt_s; }
    if (operator == "<=") { return if (unsigned) IrOp.i32_le_u else IrOp.i32_le_s; }
    if (operator == ">") { return if (unsigned) IrOp.i32_gt_u else IrOp.i32_gt_s; }
    if (operator == ">=") { return if (unsigned) IrOp.i32_ge_u else IrOp.i32_ge_s; }
  } else if (vt is ValTypeI64) {
    if (operator == "+") { return IrOp.i64_add; }
    if (operator == "-") { return IrOp.i64_sub; }
    if (operator == "*") { return IrOp.i64_mul; }
    if (operator == "%") { return if (unsigned) IrOp.i64_rem_u else IrOp.i64_rem_s; }
    if (operator == "&") { return IrOp.i64_and; }
    if (operator == "|") { return IrOp.i64_or; }
    if (operator == "^") { return IrOp.i64_xor; }
    if (operator == "<<") { return IrOp.i64_shl; }
    if (operator == ">>") { return if (unsigned) IrOp.i64_shr_u else IrOp.i64_shr_s; }
    if (operator == ">>>") { return IrOp.i64_shr_u; }
    if (operator == "==") { return IrOp.i64_eq; }
    if (operator == "!=") { return IrOp.i64_ne; }
    if (operator == "<") { return if (unsigned) IrOp.i64_lt_u else IrOp.i64_lt_s; }
    if (operator == "<=") { return if (unsigned) IrOp.i64_le_u else IrOp.i64_le_s; }
    if (operator == ">") { return if (unsigned) IrOp.i64_gt_u else IrOp.i64_gt_s; }
    if (operator == ">=") { return if (unsigned) IrOp.i64_ge_u else IrOp.i64_ge_s; }
  } else if (vt is ValTypeF64) {
    if (operator == "+") { return IrOp.f64_add; }
    if (operator == "-") { return IrOp.f64_sub; }
    if (operator == "*") { return IrOp.f64_mul; }
    if (operator == "/") { return IrOp.f64_div; }
    if (operator == "==") { return IrOp.f64_eq; }
    if (operator == "!=") { return IrOp.f64_ne; }
    if (operator == "<") { return IrOp.f64_lt; }
    if (operator == "<=") { return IrOp.f64_le; }
    if (operator == ">") { return IrOp.f64_gt; }
    if (operator == ">=") { return IrOp.f64_ge; }
  } else if (vt is ValTypeF32) {
    if (operator == "+") { return IrOp.f32_add; }
    if (operator == "-") { return IrOp.f32_sub; }
    if (operator == "*") { return IrOp.f32_mul; }
    if (operator == "/") { return IrOp.f32_div; }
    if (operator == "==") { return IrOp.f32_eq; }
    if (operator == "!=") { return IrOp.f32_ne; }
    if (operator == "<") { return IrOp.f32_lt; }
    if (operator == "<=") { return IrOp.f32_le; }
    if (operator == ">") { return IrOp.f32_gt; }
    if (operator == ">=") { return IrOp.f32_ge; }
  }
  cx.fail('operator ' + operator);
}

/** Requires a scalar (or scalar-represented, e.g. numeric enum) operand. */
function requireScalarNode(cx: LoweringContext, expr: Expression) {
  let bt = substitutedBaseType(cx, expr);
  if (bt == null) {
    bail(cx, 'untyped operand');
  }
  let ok = bt is I32Type || bt is I64Type || bt is U32Type ||
      bt is U64Type || bt is F32Type || bt is F64Type ||
      bt is BooleanType || isNarrowIntType(bt);
  if (ok) {
    return;
  }
  // Semantic types with scalar RUNTIME representations — numeric
  // enums foremost — operate as their representation, exactly as
  // streaming compiles them. Signedness stays name-driven: only
  // u32/u64 (isUnsignedNode) pick the _u operator variants.
  let vt = typeToValTypeWithSub(cx.wasm, bt, cx.func);
  let reprOk = vt is ValTypeI32 || vt is ValTypeI64 || vt is ValTypeF32 ||
      vt is ValTypeF64;
  if (!reprOk) {
    bail(cx, 'operand type ' + typeToString(bt));
  }
}

/**
 * Numeric conversions between scalar wasm types, shared by `as` casts
 * and mixed-operand promotion. `srcUnsigned` says the value is a u32 or
 * u64, so it widens by zero-extending and converts to a float by its
 * unsigned value; `dstUnsigned` says a float becomes a u32 or u64, which
 * saturates at 0 rather than at the signed minimum. Wasm valtypes do not
 * carry signedness, so the callers read it from the checked types.
 */
function scalarConvert(cx: LoweringContext, value: i32, dstVt: ValType,
    srcUnsigned: boolean, dstUnsigned: boolean): i32 {
  let srcVt = cx.valueTypeOf(value);
  if (valTypeEquals(srcVt, dstVt)) {
    return value;
  }
  let op = if (srcVt is ValTypeI32) {
    if (dstVt is ValTypeI64) {
      if (srcUnsigned) IrOp.i64_extend_i32_u else IrOp.i64_extend_i32_s
    } else if (dstVt is ValTypeF64) {
      if (srcUnsigned) IrOp.f64_convert_i32_u else IrOp.f64_convert_i32_s
    } else if (dstVt is ValTypeF32) {
      if (srcUnsigned) IrOp.f32_convert_i32_u else IrOp.f32_convert_i32_s
    } else {
      IrOp.op_count
    }
  } else if (srcVt is ValTypeI64) {
    if (dstVt is ValTypeI32) {
      IrOp.i32_wrap_i64
    } else if (dstVt is ValTypeF64) {
      if (srcUnsigned) IrOp.f64_convert_i64_u else IrOp.f64_convert_i64_s
    } else if (dstVt is ValTypeF32) {
      if (srcUnsigned) IrOp.f32_convert_i64_u else IrOp.f32_convert_i64_s
    } else {
      IrOp.op_count
    }
  } else if (srcVt is ValTypeF64) {
    if (dstVt is ValTypeI32) {
      if (dstUnsigned) IrOp.i32_trunc_sat_f64_u else IrOp.i32_trunc_sat_f64_s
    } else if (dstVt is ValTypeI64) {
      if (dstUnsigned) IrOp.i64_trunc_sat_f64_u else IrOp.i64_trunc_sat_f64_s
    } else if (dstVt is ValTypeF32) {
      IrOp.f32_demote_f64
    } else {
      IrOp.op_count
    }
  } else if (srcVt is ValTypeF32) {
    if (dstVt is ValTypeI32) {
      if (dstUnsigned) IrOp.i32_trunc_sat_f32_u else IrOp.i32_trunc_sat_f32_s
    } else if (dstVt is ValTypeI64) {
      if (dstUnsigned) IrOp.i64_trunc_sat_f32_u else IrOp.i64_trunc_sat_f32_s
    } else if (dstVt is ValTypeF64) {
      IrOp.f64_promote_f32
    } else {
      IrOp.op_count
    }
  } else {
    IrOp.op_count
  };
  if (op == IrOp.op_count) {
    cx.fail('cast kind');
  }
  return cx.b.emit(op, dstVt, value, -1);
}

/** Whether the checked type's runtime representation is a wasm
 * reference (class, record, tuple, distinct-over-ref, ref union). */
function refReprType(cx: LoweringContext, t: Type) {
  let vt = typeToValTypeWithSub(cx.wasm, t, cx.func);
  return isRefValType(vt);
}

/**
 * True when `t` is a reference type with no operator== anywhere on
 * it, so == means identity. Nullable unions strip their null;
 * unions of multiple non-null members stay conservative (false).
 */
function refIdentityEqType(tIn: Type): boolean {
  var t = tIn;
  if (t is UnionType) {
    let ut = t;
    var nonNull: Type | null = null;
    var count = 0;
    for (let m in ut.types) {
      if (!(m is NullType)) {
        nonNull = m;
        count += 1;
      }
    }
    if (count != 1 || nonNull == null) {
      return false;
    }
    t = baseType(nonNull);
  }
  if (t is ClassType) {
    return !t.members.has('==');
  }
  // An interface value's class is not known statically: its `==` is the
  // runtime class's, through the erased comparison.
  return false;
}

/**
 * The class `==` compares through when the operand's type is a class or a
 * class or null: a struct-backed class, not an extension class (whose values
 * are another type's) and not a type parameter.
 */
function equalityClassOf(cx: LoweringContext, tIn: Type): ClassType | null {
  var t = tIn;
  if (t is UnionType) {
    var nonNull: Type | null = null;
    var count = 0;
    for (let m in t.types) {
      if (!(m is NullType)) {
        nonNull = m;
        count += 1;
      }
    }
    if (count != 1 || nonNull == null) {
      return null;
    }
    t = baseType(nonNull);
  }
  if (!(t is ClassType)) {
    return null;
  }
  let ct = eraseTypeParameters(cx.wasm, t) as ClassType;
  if (ct.isExtension) {
    return null;
  }
  if (!cx.wasm.classInfos.has(getClassKey(ct))) {
    return null;
  }
  return ct;
}

/**
 * A record or tuple operand of `==`: a record literal of exactly the
 * operand's record type lowers straight to its fields, held as the
 * record (`explodedRecords`), so the comparison reads the fields and the
 * record is never built. Anything else lowers as usual.
 */
function lowerValueEqOperand(cx: LoweringContext, e: Expression, t: Type) {
  let bt = baseType(t);
  if (bt is RecordType) {
    let d = cx.recordDispatchOf(bt);
    let fields = exactLiteralFields(cx, e, d);
    if (fields != null) {
      return cx.heldRecord(d, fields);
    }
  }
  return lowerExpr(cx, e, null);
}

/** Short-circuit && and || as a value-producing diamond. */
function lowerLogical(cx: LoweringContext, bin: BinaryExpression) {
  let left = lowerExpr(cx, bin.left, null);
  if (!cx.reachable) {
    return -1;
  }
  let evalRight = cx.b.newBlock();
  let merge = cx.b.newBlock();
  let result = cx.b.blockParam(merge, new ValTypeI32());
  if (bin.operator == "&&") {
    let zero = cx.b.constI32(0, new ValTypeI32());
    cx.b.brIf(left, evalRight, noValues(), merge, oneValue(zero));
  } else {
    let one = cx.b.constI32(1, new ValTypeI32());
    cx.b.brIf(left, merge, oneValue(one), evalRight, noValues());
  }
  cx.b.switchTo(evalRight);
  let right = lowerExpr(cx, bin.right, null);
  if (cx.reachable) {
    cx.b.br(merge, oneValue(right));
  }
  cx.b.switchTo(merge);
  cx.reachable = true;
  return result;
}

export function lowerBinary(cx: LoweringContext, bin: BinaryExpression): i32 {
  if (bin.operator == "&&" || bin.operator == "||") {
    return lowerLogical(cx, bin);
  }
  if (bin.operator == "??") {
    return lowerNullishCoalesce(cx, bin);
  }
  // Null comparisons: x == null / x != null (either side).
  let leftIsNull = bin.left is NullLiteral;
  let rightIsNull = bin.right is NullLiteral;
  if ((leftIsNull || rightIsNull) &&
      (bin.operator == "==" || bin.operator == "!=" ||
       bin.operator == "===" || bin.operator == "!==")) {
    if (leftIsNull && rightIsNull) {
      // null == null needs no test at all.
      let same = bin.operator == "==" || bin.operator == "===";
      return cx.b.constI32(if (same) 1 else 0, new ValTypeI32());
    }
    let other = if (leftIsNull) bin.right else bin.left;
    let value = lowerExpr(cx, other, null);
    let vt = cx.valueTypeOf(value);
    if (!isRefValType(vt)) {
      // The checker rejects null comparisons against non-nullable
      // scalars, so this only guards lowering bugs.
      bail(cx, 'null compare on non-ref');
    }
    let isNull = cx.b.emit(IrOp.ref_is_null, new ValTypeI32(), value, -1);
    if (bin.operator == "!=" || bin.operator == "!==") {
      return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), isNull, -1);
    }
    return isNull;
  }
  // Zero-width equality: `void` is a unit type — every value of it is
  // the same (absent) value — so comparing two is a constant. Arises
  // in generic code at a zero-width instantiation
  // (`GrowableArray<void>.contains` comparing `this[i] == value`).
  if (bin.operator == "==" || bin.operator == "!=" ||
      bin.operator == "===" || bin.operator == "!==") {
    let zlb = substitutedBaseType(cx, bin.left);
    let zrb = substitutedBaseType(cx, bin.right);
    if (zlb != null && zrb != null &&
        isVoidOrNeverType(zlb) && isVoidOrNeverType(zrb)) {
      lowerExpr(cx, bin.left, null);
      lowerExpr(cx, bin.right, null);
      let same = bin.operator == "==" || bin.operator == "===";
      return cx.b.constI32(if (same) 1 else 0, new ValTypeI32());
    }
  }
  // Reference identity: === / !== between eq-typed references.
  if (bin.operator == "===" || bin.operator == "!==") {
    var left = lowerExpr(cx, bin.left, null);
    var right = lowerExpr(cx, bin.right, null);
    // Fat-pointer identity is the INSTANCE's, not the pointer's: an
    // adapted record view or a re-packed interface value IS the same
    // object (the bootstrap compiler unwraps both the same way).
    let lb = substitutedBaseType(cx, bin.left);
    if (lb != null) {
      left = cx.unwrapFatPointer(lb, left);
    }
    let rb = substitutedBaseType(cx, bin.right);
    if (rb != null) {
      right = cx.unwrapFatPointer(rb, right);
    }
    let lt = cx.valueTypeOf(left);
    let rt = cx.valueTypeOf(right);
    if (!isRefValType(lt) || !isRefValType(rt)) {
      // Scalar identity is plain numeric equality (streaming emits
      // the same eq instruction for == and ===).
      if (isRefValType(lt) || isRefValType(rt) || !valTypeEquals(lt, rt)) {
        bail(cx, 'identity compare on non-refs');
      }
      let eqOp = mapBinaryOp(cx, '==', lt, false);
      let sameScalar = cx.b.binary(eqOp, new ValTypeI32(), left, right);
      if (bin.operator == "!==") {
        return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), sameScalar, -1);
      }
      return sameScalar;
    }
    let same = cx.b.binary(IrOp.ref_eq, new ValTypeI32(), left, right);
    if (bin.operator == "!==") {
      return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
    }
    return same;
  }
  // Class operator members: `a + b` / `a == b` where the left
  // operand's class declares the operator resolve to a method call
  // (member-lookup.md §5.5 — operators fold into method
  // resolution). Mirrors streaming: `!=` is `==` inverted; direct
  // call when devirtualizable, else bail (streaming dispatches
  // virtually there). String concat and String equality are the
  // dominant users.
  let leftBase = substitutedBaseType(cx, bin.left);
  // `==` on an inline tuple or union compares slot by slot, both operands
  // in the inline one's layout, so neither is boxed (#812).
  if ((bin.operator == "==" || bin.operator == "!=") && leftBase != null) {
    let rightType = substitutedBaseType(cx, bin.right);
    let leftLayout = inlineLayoutOf(cx.wasm, leftBase);
    let layout = leftLayout ??
        (if (rightType != null) inlineLayoutOf(cx.wasm, rightType) else null);
    if (layout != null) {
      let shape = layout;
      let left = lowerIntoSlots(cx, bin.left, shape, shape.slots);
      let right = lowerIntoSlots(cx, bin.right, shape, shape.slots);
      let same = lowerInlineEq(cx, shape, left, right);
      if (bin.operator == "!=") {
        return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
      }
      return same;
    }
  }
  // `==` on a class is a virtual call whose root default is identity
  // (equality.md, "Virtual equality with an identity default"); null
  // operands compare first.
  if ((bin.operator == "==" || bin.operator == "!=") && leftBase != null) {
    let eqCt = equalityClassOf(cx, leftBase);
    if (eqCt != null) {
      let left = lowerExpr(cx, bin.left, null);
      let right = lowerExpr(cx, bin.right, null);
      if (isRefValType(cx.valueTypeOf(left)) && isRefValType(cx.valueTypeOf(right))) {
        let same = lowerClassEquality(cx, left, right, eqCt);
        if (bin.operator == "!=") {
          return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
        }
        return same;
      }
      bail(cx, 'class eq operands');
    }
  }
  // `==` on records and tuples compares them by value, field by field
  // (equality.md, "Structural equality for records and tuples").
  if ((bin.operator == "==" || bin.operator == "!=") && leftBase != null) {
    let rightBase = substitutedBaseType(cx, bin.right);
    if (rightBase != null && valueEqOperand(leftBase) != null &&
        valueEqOperand(rightBase as Type) != null) {
      let left = lowerValueEqOperand(cx, bin.left, leftBase);
      let right = lowerValueEqOperand(cx, bin.right, rightBase as Type);
      let same = lowerValueEq(cx, leftBase, left, rightBase as Type, right);
      if (bin.operator == "!=") {
        return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
      }
      return same;
    }
  }
  if (leftBase != null) {
    if (leftBase is ClassType) {
      let opMember = if (bin.operator == "!=") "==" else bin.operator;
      let leftCt = eraseTypeParameters(cx.wasm, leftBase) as ClassType;
      let chosenFt = cx.func.model.getResolvedOverload(bin);
      if (leftCt.members.has(opMember)) {
        let left = lowerExpr(cx, bin.left, null);
        let right = lowerExpr(cx, bin.right, null);
        let raw = cx.operatorMemberOn(opMember, left, right, leftCt, chosenFt);
        if (bin.operator == "!=") {
          return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), raw, -1);
        }
        return raw;
      }
      if ((bin.operator == "<" || bin.operator == "<=" ||
           bin.operator == ">" || bin.operator == ">=") &&
          leftCt.members.has("compareTo")) {
        let left = lowerExpr(cx, bin.left, null);
        let right = lowerExpr(cx, bin.right, null);
        let cmp = cx.operatorMemberOn("compareTo", left, right, leftCt, chosenFt);
        let zero = cx.b.constI32(0, new ValTypeI32());
        let cmpOp = if (bin.operator == "<") {
          IrOp.i32_lt_s
        } else if (bin.operator == "<=") {
          IrOp.i32_le_s
        } else if (bin.operator == ">") {
          IrOp.i32_gt_s
        } else {
          IrOp.i32_ge_s
        };
        return cx.b.binary(cmpOp, new ValTypeI32(), cmp, zero);
      }
    }
  }
  // == / != on ERASED operands (an unbounded T, anyref, eqref):
  // streaming's runtime type-dispatch diamond — identity, then
  // null, then String content, then boxed-primitive unwraps.
  if ((bin.operator == "==" || bin.operator == "!=") &&
      leftBase != null) {
    if (leftBase is TypeParameterType || leftBase is AnyRefType ||
        leftBase is EqRefType) {
      let left = lowerExpr(cx, bin.left, null);
      let right = lowerExpr(cx, bin.right, null);
      if (!isRefValType(cx.valueTypeOf(left)) ||
          !isRefValType(cx.valueTypeOf(right))) {
        bail(cx, 'erased eq operands');
      }
      let same = lowerErasedEq(cx, left, right);
      if (bin.operator == "!=") {
        return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
      }
      return same;
    }
  }
  // == / != on references whose type declares NO operator== compare
  // identity (streaming's ref.eq fallback). The member branch above
  // already claimed types with a content-equality operator; nullable
  // unions strip their null before the check, so `Node | null`
  // compares by identity while `String | null` keeps bailing (its
  // non-null member owns an ==).
  if ((bin.operator == "==" || bin.operator == "!=") &&
      leftBase != null &&
      refIdentityEqType(leftBase)) {
    var left = lowerExpr(cx, bin.left, null);
    var right = lowerExpr(cx, bin.right, null);
    if (!isRefValType(cx.valueTypeOf(left)) ||
        !isRefValType(cx.valueTypeOf(right))) {
      bail(cx, 'identity compare on non-refs');
    }
    left = cx.unwrapFatPointer(leftBase, left);
    let rBase = substitutedBaseType(cx, bin.right);
    if (rBase != null) {
      right = cx.unwrapFatPointer(rBase, right);
    }
    let same = cx.b.binary(IrOp.ref_eq, new ValTypeI32(), left, right);
    if (bin.operator == "!=") {
      return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
    }
    return same;
  }
  // ==/!= on anything reference-shaped that earlier arms did not
  // claim: streaming's fallback diamond (identity, then null, then
  // String content, then boxed-primitive unwraps) — which is exactly
  // lowerErasedEq. Covers nullable Strings, distinct types over
  // String (their runtime values ARE Strings, so the diamond's
  // ref.test finds them), records and tuples (identity; records
  // through their instances), ByteArray, and ref-only unions.
  if (bin.operator == "==" || bin.operator == "!=") {
    let lb2 = substitutedBaseType(cx, bin.left);
    let rb2 = substitutedBaseType(cx, bin.right);
    let lvt = if (lb2 != null) typeToValTypeWithSub(cx.wasm, lb2, cx.func) else null;
    let rvt = if (rb2 != null) typeToValTypeWithSub(cx.wasm, rb2, cx.func) else null;
    if (lvt is ValTypeV128 && rvt is ValTypeV128) {
      let left = lowerExpr(cx, bin.left, null);
      let right = lowerExpr(cx, bin.right, null);
      let eqVec = cx.b.emit(IrOp.simd_binary, new ValTypeV128(), cx.b.pushExtra(twoValues(left, right)), 0x23);
      let same = cx.b.emit(IrOp.simd_unary, new ValTypeI32(), eqVec, 0x63);
      if (bin.operator == "!=") {
        return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
      }
      return same;
    }
    if (lb2 != null && rb2 != null &&
        refReprType(cx, lb2) && refReprType(cx, rb2)) {
      var eqL = lowerExpr(cx, bin.left, null);
      var eqR = lowerExpr(cx, bin.right, null);
      eqL = cx.unwrapFatPointer(lb2, eqL);
      eqR = cx.unwrapFatPointer(rb2, eqR);
      if (isRefValType(cx.valueTypeOf(eqL)) &&
          isRefValType(cx.valueTypeOf(eqR))) {
        let same = lowerErasedEq(cx, eqL, eqR);
        if (bin.operator == "!=") {
          return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), same, -1);
        }
        return same;
      }
      bail(cx, 'eq operand repr');
    }
  }
  requireScalarNode(cx, bin.left);
  requireScalarNode(cx, bin.right);
  var left = lowerExpr(cx, bin.left, null);
  var right = lowerExpr(cx, bin.right, null);
  var leftType = cx.valueTypeOf(left);
  let rightType = cx.valueTypeOf(right);
  if (!valTypeEquals(leftType, rightType)) {
    // Mixed numeric operands promote to the wider type, mirroring
    // streaming's table: f64 wins; f32 wins unless the other side
    // is i64 (then f64); i64 wins over i32.
    let target: ValType | null = if (leftType is ValTypeF64 || rightType is ValTypeF64) {
      new ValTypeF64()
    } else if (leftType is ValTypeF32 || rightType is ValTypeF32) {
      if (leftType is ValTypeI64 || rightType is ValTypeI64) {
        new ValTypeF64()
      } else {
        new ValTypeF32()
      }
    } else if (leftType is ValTypeI64 || rightType is ValTypeI64) {
      new ValTypeI64()
    } else {
      null
    };
    if (target == null) {
      bail(cx, 'mixed operand types');
    }
    left = scalarConvert(cx, left, target, isUnsignedNode(cx, bin.left), false);
    right = scalarConvert(cx, right, target, isUnsignedNode(cx, bin.right), false);
    leftType = target;
  }
  // Integer division: the checker promotes the RESULT to a float
  // (i32/i32 checks as f32), so `/` never appears in the integer op
  // tables — convert both operands to the checked result type and
  // divide there, exactly as streaming does.
  if (bin.operator == "/" &&
      (leftType is ValTypeI32 || leftType is ValTypeI64)) {
    let resVt = cx.exprVt(bin);
    if (!(resVt is ValTypeF32 || resVt is ValTypeF64)) {
      bail(cx, 'integer division result type');
    }
    let lConv = scalarConvert(cx, left, resVt, isUnsignedNode(cx, bin.left), false);
    let rConv = scalarConvert(cx, right, resVt, isUnsignedNode(cx, bin.right), false);
    let divOp = if (resVt is ValTypeF32) IrOp.f32_div else IrOp.f64_div;
    return cx.b.binary(divOp, resVt, lConv, rConv);
  }
  if (bin.operator == "**") {
    let unsigned = isUnsignedNode(cx, bin.left) || isUnsignedNode(cx, bin.right);
    if (leftType is ValTypeI32 || leftType is ValTypeI64) {
      return lowerIntPow(cx, left, right, leftType, unsigned);
    }
    if (leftType is ValTypeF32 || leftType is ValTypeF64) {
      return lowerFloatPow(cx, left, right, leftType);
    }
    bail(cx, 'pow on non-numeric scalar');
  }
  let unsigned = isUnsignedNode(cx, bin.left) || isUnsignedNode(cx, bin.right);
  let op = mapBinaryOp(cx, bin.operator, leftType, unsigned);
  let isComparison = bin.operator == "==" || bin.operator == "!=" ||
      bin.operator == "<" || bin.operator == "<=" ||
      bin.operator == ">" || bin.operator == ">=";
  let resultType = if (isComparison) new ValTypeI32() as ValType else leftType;
  return cx.b.binary(op, resultType, left, right);
}

export function lowerIntPow(cx: LoweringContext, baseVal: i32, expVal: i32,
    vt: ValType, unsigned: boolean): i32 {
  let is64 = vt is ValTypeI64;
  let zero = if (is64) cx.b.constI64(0 as i64, vt) else cx.b.constI32(0, vt);
  let one = if (is64) cx.b.constI64(1 as i64, vt) else cx.b.constI32(1, vt);
  let shiftOne = if (is64) cx.b.constI64(1 as i64, vt) else cx.b.constI32(1, vt);

  let mulOp = if (is64) IrOp.i64_mul else IrOp.i32_mul;
  let andOp = if (is64) IrOp.i64_and else IrOp.i32_and;
  let shrOp = if (is64) (if (unsigned) IrOp.i64_shr_u else IrOp.i64_shr_s)
              else (if (unsigned) IrOp.i32_shr_u else IrOp.i32_shr_s);
  let neOp = if (is64) IrOp.i64_ne else IrOp.i32_ne;
  let gtOp = if (is64) (if (unsigned) IrOp.i64_gt_u else IrOp.i64_gt_s)
             else (if (unsigned) IrOp.i32_gt_u else IrOp.i32_gt_s);

  let header = cx.b.newBlock();
  let loopBody = cx.b.newBlock();
  let mulB = cx.b.newBlock();
  let nextB = cx.b.newBlock();
  let exitB = cx.b.newBlock();

  let accParam = cx.b.blockParam(header, vt);
  let baseParam = cx.b.blockParam(header, vt);
  let expParam = cx.b.blockParam(header, vt);

  let initArgs = growable<i32>([one, baseVal, expVal]);
  cx.b.br(header, initArgs);

  cx.b.switchTo(header);
  let cond = cx.b.binary(gtOp, new ValTypeI32(), expParam, zero);
  let exitArgs = growable<i32>([accParam]);
  cx.b.brIf(cond, loopBody, noValues(), exitB, exitArgs);

  cx.b.switchTo(loopBody);
  let isOddBit = cx.b.binary(andOp, vt, expParam, one);
  let isOdd = cx.b.binary(neOp, new ValTypeI32(), isOddBit, zero);
  let nextAccParam = cx.b.blockParam(nextB, vt);
  let notOddArgs = growable<i32>([accParam]);
  cx.b.brIf(isOdd, mulB, noValues(), nextB, notOddArgs);

  cx.b.switchTo(mulB);
  let newAcc = cx.b.binary(mulOp, vt, accParam, baseParam);
  let oddArgs = growable<i32>([newAcc]);
  cx.b.br(nextB, oddArgs);

  cx.b.switchTo(nextB);
  let nextBase = cx.b.binary(mulOp, vt, baseParam, baseParam);
  let nextExp = cx.b.binary(shrOp, vt, expParam, shiftOne);
  let loopArgs = growable<i32>([nextAccParam, nextBase, nextExp]);
  cx.b.br(header, loopArgs);

  let result = cx.b.blockParam(exitB, vt);
  cx.b.switchTo(exitB);
  return result;
}

export function lowerFloatPow(cx: LoweringContext, baseVal: i32, expVal: i32,
    vt: ValType): i32 {
  let is64 = vt is ValTypeF64;
  let oneF = if (is64) cx.b.constF64(1.0, vt) else cx.b.constF32(1.0, vt);
  let mulOp = if (is64) IrOp.f64_mul else IrOp.f32_mul;
  let divOp = if (is64) IrOp.f64_div else IrOp.f32_div;
  let truncOp = if (is64) IrOp.i32_trunc_sat_f64_s else IrOp.i32_trunc_sat_f32_s;
  let eqOp = if (is64) IrOp.f64_eq else IrOp.f32_eq;
  let sqrtOp = if (is64) IrOp.f64_sqrt else IrOp.f32_sqrt;

  let zeroI = cx.b.constI32(0, new ValTypeI32());
  let oneI = cx.b.constI32(1, new ValTypeI32());
  let halfF = if (is64) cx.b.constF64(0.5, vt) else cx.b.constF32(0.5, vt);

  // Check if exp == 0.5 for sqrt
  let isHalf = cx.b.binary(eqOp, new ValTypeI32(), expVal, halfF);
  let notSqrtB = cx.b.newBlock();
  let sqrtB = cx.b.newBlock();
  let finalMergeB = cx.b.newBlock();
  let finalResult = cx.b.blockParam(finalMergeB, vt);

  cx.b.brIf(isHalf, sqrtB, noValues(), notSqrtB, noValues());

  cx.b.switchTo(sqrtB);
  let sqrtRes = cx.b.emit(sqrtOp, vt, baseVal, -1);
  cx.b.br(finalMergeB, oneValue(sqrtRes));

  cx.b.switchTo(notSqrtB);
  // Truncate exponent to integer
  let expI = cx.b.emit(truncOp, new ValTypeI32(), expVal, -1);
  let isNeg = cx.b.binary(IrOp.i32_lt_s, new ValTypeI32(), expI, zeroI);
  let negB = cx.b.newBlock();
  let posB = cx.b.newBlock();
  let absExpParam = cx.b.blockParam(posB, new ValTypeI32());

  cx.b.brIf(isNeg, negB, noValues(), posB, oneValue(expI));

  cx.b.switchTo(negB);
  let posExp = cx.b.binary(IrOp.i32_sub, new ValTypeI32(), zeroI, expI);
  cx.b.br(posB, oneValue(posExp));

  cx.b.switchTo(posB);
  // Repeated squaring on float base
  let header = cx.b.newBlock();
  let loopBody = cx.b.newBlock();
  let mulB = cx.b.newBlock();
  let nextB = cx.b.newBlock();
  let exitB = cx.b.newBlock();

  let accParam = cx.b.blockParam(header, vt);
  let baseParam = cx.b.blockParam(header, vt);
  let expParam = cx.b.blockParam(header, new ValTypeI32());

  let initArgs = growable<i32>([oneF, baseVal, absExpParam]);
  cx.b.br(header, initArgs);

  cx.b.switchTo(header);
  let cond = cx.b.binary(IrOp.i32_gt_s, new ValTypeI32(), expParam, zeroI);
  let exitArgs = growable<i32>([accParam]);
  cx.b.brIf(cond, loopBody, noValues(), exitB, exitArgs);

  cx.b.switchTo(loopBody);
  let isOddBit = cx.b.binary(IrOp.i32_and, new ValTypeI32(), expParam, oneI);
  let isOdd = cx.b.binary(IrOp.i32_ne, new ValTypeI32(), isOddBit, zeroI);
  let nextAccParam = cx.b.blockParam(nextB, vt);
  let notOddArgs = growable<i32>([accParam]);
  cx.b.brIf(isOdd, mulB, noValues(), nextB, notOddArgs);

  cx.b.switchTo(mulB);
  let newAcc = cx.b.binary(mulOp, vt, accParam, baseParam);
  let oddArgs = growable<i32>([newAcc]);
  cx.b.br(nextB, oddArgs);

  cx.b.switchTo(nextB);
  let nextBase = cx.b.binary(mulOp, vt, baseParam, baseParam);
  let nextExp = cx.b.binary(IrOp.i32_shr_u, new ValTypeI32(), expParam, oneI);
  let loopArgs = growable<i32>([nextAccParam, nextBase, nextExp]);
  cx.b.br(header, loopArgs);

  let rawRes = cx.b.blockParam(exitB, vt);
  cx.b.switchTo(exitB);

  // If isNeg, return 1.0 / rawRes, else return rawRes
  let invertB = cx.b.newBlock();
  let doneB = cx.b.newBlock();
  let powResParam = cx.b.blockParam(doneB, vt);

  cx.b.brIf(isNeg, invertB, noValues(), doneB, oneValue(rawRes));

  cx.b.switchTo(invertB);
  let inverted = cx.b.binary(divOp, vt, oneF, rawRes);
  cx.b.br(doneB, oneValue(inverted));

  cx.b.switchTo(doneB);
  cx.b.br(finalMergeB, oneValue(powResParam));

  cx.b.switchTo(finalMergeB);
  return finalResult;
}

export function lowerUnary(cx: LoweringContext, un: UnaryExpression): i32 {
  if (un.operator == "-" && un.argument is NumberLiteral &&
      isRefValType(cx.exprVt(un))) {
    return lowerBigIntLiteral(cx, (un.argument as NumberLiteral).raw, true);
  }
  let argBase = substitutedBaseType(cx, un.argument);
  if (un.operator == "-" && argBase != null && argBase is ClassType) {
    let argCt = eraseTypeParameters(cx.wasm, argBase) as ClassType;
    if (argCt.members.has("-")) {
      let operand = lowerExpr(cx, un.argument, null);
      let chosenFt = cx.func.model.getResolvedOverload(un);
      return cx.unaryOperatorMemberOn("-", operand, argCt, chosenFt);
    }
  }
  let value = lowerExpr(cx, un.argument, null);
  if (!cx.reachable) {
    return -1;
  }
  let vt = cx.valueTypeOf(value);
  if (un.operator == "!") {
    if (!(vt is ValTypeI32)) {
      bail(cx, 'logical not on non-i32');
    }
    return cx.b.emit(IrOp.i32_eqz, new ValTypeI32(), value, -1);
  }
  if (un.operator == "-") {
    if (vt is ValTypeI32) {
      let zero = cx.b.constI32(0, vt);
      return cx.b.binary(IrOp.i32_sub, vt, zero, value);
    }
    if (vt is ValTypeI64) {
      let zero = cx.b.constI64(0 as i64, vt);
      return cx.b.binary(IrOp.i64_sub, vt, zero, value);
    }
    if (vt is ValTypeF64) {
      return cx.b.emit(IrOp.f64_neg, vt, value, -1);
    }
    if (vt is ValTypeF32) {
      return cx.b.emit(IrOp.f32_neg, vt, value, -1);
    }
    bail(cx, 'negate type');
  }
  if (un.operator == "~") {
    if (vt is ValTypeI32) {
      let minusOne = cx.b.constI32(-1, vt);
      return cx.b.binary(IrOp.i32_xor, vt, value, minusOne);
    }
    if (vt is ValTypeI64) {
      let minusOne = cx.b.constI64(-1 as i64, vt);
      return cx.b.binary(IrOp.i64_xor, vt, value, minusOne);
    }
    bail(cx, 'bitwise not type');
  }
  cx.fail('unary operator');
}

/**
 * Re-normalize a value that has just been cast to a narrow integer type.
 * Narrow values ride in an i32 in canonical form — unsigned zero-
 * extended, signed sign-extended — which is what lets every later
 * promotion and comparison be plain 32-bit work. A cast is the only
 * operation that can break that invariant, so it is the only one that
 * has to restore it.
 *
 * The sign extension is spelled as a shift pair rather than
 * `i32.extend8_s`/`extend16_s`: those opcodes are not in the IR's op
 * set, and adding them would mean threading two more entries through
 * the contiguous enum bands that `isBinaryNumericOp` decodes by range —
 * a poor trade for saving one instruction on a rare operation.
 */
function normalizeNarrow(cx: LoweringContext, value: i32, target: Type | null) {
  if (target == null) {
    return value;
  }
  if (!isNarrowIntType(target)) {
    return value;
  }
  let bits = narrowIntBits(target);
  let vt = new ValTypeI32() as ValType;
  if (isUnsignedIntType(target)) {
    let mask = if (bits == 8) 255 else 65535;
    return cx.b.binary(IrOp.i32_and, vt, value, cx.b.constI32(mask, vt));
  }
  let shift = cx.b.constI32(32 - bits, vt);
  let shifted = cx.b.binary(IrOp.i32_shl, vt, value, shift);
  return cx.b.binary(IrOp.i32_shr_s, vt, shifted, shift);
}

/** The narrow type an `as` expression targets, or null if it is not one. */
function narrowCastTarget(cx: LoweringContext, expr: AsExpression): Type | null {
  let t = cx.substitutedNodeType(expr);
  if (t == null) {
    return null;
  }
  let bt = baseType(t);
  return if (isNarrowIntType(bt)) bt else null;
}

/**
 * Scalar `as` casts. Same-representation casts (i32 as u32, boolean as
 * i32) are no-ops; cross-representation casts go through
 * `scalarConvert`, unsigned where the source or destination type is, with
 * saturating truncation from floats. A cast TO a narrow integer
 * additionally re-normalizes (`normalizeNarrow`), including when the wasm
 * type does not change.
 */
/**
 * A cast to a reference type from a value that has no reference
 * representation in this specialization: `v as anyref` checks for any
 * T, and T turned out to be a primitive or `void`. Zena never boxes
 * implicitly, so the cast cannot succeed, and it traps when it runs,
 * like any failing `as`.
 *
 * It still has to compile, because generic code routinely guards it
 * with a test that is false for this T (`if (v is String)`). The code
 * after the cast lowers against a placeholder, in a block nothing
 * branches to.
 */
function impossibleRefCast(cx: LoweringContext, dstVt: ValType) {
  cx.b.unreachable();
  cx.b.switchTo(cx.b.newBlock());
  return cx.b.emit(IrOp.ref_null, dstVt, -1, -1);
}

/**
 * `x as (1, Size)` on a union of tuples: the members can share a struct,
 * so the cast below would pass a `(0, Color)`. The literal in field 0
 * has to match too, and a value whose literal does not traps as a failed
 * cast: a `ref.cast` to `nullref` fails for every value but null, so the
 * trap reads as a cast failure rather than a bare `unreachable` (#600).
 */
function checkTupleTag(cx: LoweringContext, expr: AsExpression, value: i32): void {
  let srcOpt = cx.substitutedNodeType(expr.expression);
  let dstOpt = cx.substitutedNodeType(expr);
  if (srcOpt == null || dstOpt == null) {
    return;
  }
  let dst = baseType(dstOpt);
  if (!(unwrapAliases(srcOpt) is UnionType) || !(dst is TupleType)) {
    return;
  }
  if (dst.elementTypes.length == 0 || !isTupleTag(dst.elementTypes[0])) {
    return;
  }
  let ok = tupleTagTest(cx, value, dst);
  let pass = cx.b.newBlock();
  let fail = cx.b.newBlock();
  cx.b.brIf(ok, pass, noValues(), fail, noValues());
  cx.b.switchTo(fail);
  let nullref = new ValTypeNullRef() as ValType;
  cx.b.emit(IrOp.ref_cast, nullref, value, cx.b.internType(nullref));
  // A null source passes that cast; the cast below would trap on it.
  cx.b.unreachable();
  cx.b.switchTo(pass);
}

/**
 * `value`, cast by `expr` to `dstVt`, checked for the elements its tuple
 * type narrows (`tupleElementChecks`): tuples that meet share a struct
 * (#1137), so the cast of the struct alone does not check them. A
 * nullable destination checks a non-null value only.
 */
function checkedTupleCast(cx: LoweringContext, expr: AsExpression, value: i32,
    dstVt: ValType) {
  let dstSem = cx.substitutedNodeType(expr);
  if (dstSem == null) {
    return value;
  }
  let target = getNonNullableType(baseType(dstSem));
  let st = refTargetOf(dstVt);
  if (!(target is TupleType) || !(st is WasmStruct) || !cx.tupleNarrows(st, target)) {
    return value;
  }
  if (dstVt is ValTypeRef) {
    cx.tupleElementChecks(value, st, target, true);
    return value;
  }
  let isNull = cx.b.emit(IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1);
  let check = cx.b.newBlock();
  let done = cx.b.newBlock();
  cx.b.brIf(isNull, done, noValues(), check, noValues());
  cx.b.switchTo(check);
  cx.tupleElementChecks(cx.stripNonNull(value), st, target, true);
  cx.b.br(done, noValues());
  cx.b.switchTo(done);
  return value;
}

export function lowerAs(cx: LoweringContext, expr: AsExpression): i32 {
  // A cast to `void` reads back a slot that holds nothing: `slot as T`
  // with a `WithDefault<T>` slot at `T = void`. Its operand still runs.
  let target = cx.substitutedNodeType(expr);
  if (target != null && isVoidOrNeverType(target)) {
    lowerExpr(cx, expr.expression, null);
    return -1;
  }
  let dstVt = cx.exprVt(expr);
  let narrowTarget = narrowCastTarget(cx, expr);
  // Literal-through-cast (`9223372036854775807 as u64`,
  // `1.5 as f32`): the bare literal checks as i32/f64 and cannot
  // lower alone at that width — parse it directly at the cast's
  // width, exactly like streaming's handledLiteral path.
  if (dstVt is ValTypeI64) {
    if (expr.expression is NumberLiteral) {
      if (let (true, wide) = parseI64OrU64Literal(
          (expr.expression as NumberLiteral).raw)) {
        return cx.b.constI64(wide, dstVt);
      }
    }
    if (expr.expression is UnaryExpression) {
      let un = expr.expression as UnaryExpression;
      if (un.operator == '-' && un.argument is NumberLiteral) {
        if (let (true, wideNeg) = parseI64OrU64Literal(
            '-' + (un.argument as NumberLiteral).raw)) {
          return cx.b.constI64(wideNeg, dstVt);
        }
      }
    }
  }
  if (dstVt is ValTypeF64 && expr.expression is NumberLiteral) {
    return cx.b.constF64(
        parseFloatLiteral((expr.expression as NumberLiteral).raw), dstVt);
  }
  if (dstVt is ValTypeF32 && expr.expression is NumberLiteral) {
    return cx.b.constF32(
        parseFloatLiteral((expr.expression as NumberLiteral).raw), dstVt);
  }
  let value = lowerExpr(cx, expr.expression, null);
  if (!cx.reachable) {
    return -1;
  }
  if (value < 0 && cx.reachable && isRefValType(dstVt)) {
    // A `void` operand: `v as anyref` where this specialization made
    // T void. Like a primitive (below), it can never be a reference.
    return impossibleRefCast(cx, dstVt);
  }
  if (value < 0) {
    // A zero-width operand (a read the checker typed `never` or
    // `void`) has no value to cast; bail loudly rather than index the
    // value table with the sentinel.
    bail(cx, 'as operand produced no value');
  }
  if (cx.func.model.getRecordAdaptation(expr) != null) {
    // `as` to a narrower closed record type projects: the conversion
    // the checker recorded on this node builds the narrower view once
    // the cast's value is back. A cast to the target's fat pointer
    // first would trap, since the value is the wider shape.
    return value;
  }
  checkTupleTag(cx, expr, value);
  let srcVt = cx.valueTypeOf(value);
  if (valTypeEquals(srcVt, dstVt)) {
    // Same wasm type, but possibly a different narrow width: `x as u8`
    // is i32-to-i32 and still has to truncate.
    return normalizeNarrow(cx, value, narrowTarget);
  }
  if (isRefValType(srcVt) && isRefValType(dstVt)) {
    // Reference casts, mirroring streaming: subtype-compatible
    // values pass through uncast; everything else is a ref.cast —
    // null-preserving for nullable targets, trapping for non-null
    // ones (emission picks the form from the interned target type).
    // Conformance is nullability-STRICT: a nullable source `as` a
    // non-null target must emit the trapping cast, not pass through.
    let conforms = cx.refConforms(srcVt, dstVt);
    let recorder = cx.b.recorder;
    if (recorder != null) {
      // Whether the cast is needed can differ between the classes a
      // type parameter stands for (template.zena).
      recorder.noteConforms(srcVt, dstVt, conforms);
    }
    if (conforms) {
      return checkedTupleCast(cx, expr, value, dstVt);
    }
    // A tuple to a tuple of other element types is a new tuple, each
    // element cast to its target type (#483): the two structs are
    // unrelated, so a cast of the tuple itself would always trap.
    let copied = cx.copyTuple(value, srcVt, dstVt, 'tuple cast');
    if (copied >= 0) {
      return copied;
    }
    // The `extern` and `any` hierarchies are disjoint, so a cast between
    // them is a conversion, never a `ref.cast`. Both conversions are
    // total: `x as externref` wraps any GC reference, and `e as anyref`
    // (or `e as C`) unwraps a host reference, with the class cast after
    // it trapping on a host value that is not a module object.
    let srcExtern = isExternValType(srcVt);
    let dstExtern = isExternValType(dstVt);
    if (dstExtern && !srcExtern) {
      var converted = cx.b.emit(IrOp.extern_convert_any,
          new ValTypeExternRef() as ValType, value, -1);
      if (dstVt is ValTypeRefExtern) {
        converted = cx.b.emit(IrOp.ref_as_non_null, dstVt, converted, -1);
      }
      return converted;
    }
    if (srcExtern && !dstExtern) {
      let unwrapped = cx.b.emit(IrOp.any_convert_extern,
          new ValTypeAnyRef() as ValType, value, -1);
      if (cx.refConforms(cx.valueTypeOf(unwrapped), dstVt)) {
        return unwrapped;
      }
      return cx.b.emit(IrOp.ref_cast, dstVt, unwrapped,
          cx.b.internType(dstVt));
    }
    var operand = value;
    let dstSemOpt = cx.substitutedNodeType(expr);
    // `expr as I` where the destination is an interface and this node
    // carries an interface adaptation: the conversion is the pack,
    // applied by the adaptation hook when this expression's value is
    // consumed. Return the class value uncast — a ref.cast of it to
    // the fat-pointer type runs before the pack and traps (pinned by
    // interfaces/as-interface-in-array-literal.zena). The destination
    // gate matters: a class DOWNCAST can carry an adaptation too
    // (`x.typeArguments as GrowableArray<Type>` passed to an Array<T>
    // parameter re-packs for the consumer), and there the cast must
    // still run first — skipping it hands the adaptation the
    // still-wrapped fat pointer and double-wraps it.
    // `I | null` is a destination interface too — the adaptation packs
    // under a null guard, and a ref.cast of the class value to the
    // fat-pointer type ahead of it would trap just the same.
    if (dstSemOpt != null &&
        getNonNullableType(baseType(dstSemOpt)) is InterfaceType &&
        cx.func.model.getInterfaceAdaptation(expr) != null) {
      return value;
    }
    // `x as I` on a value the checker could not pack statically (an
    // `anyref`, a class the value might not be): the value's class
    // implements `I` or the cast traps, read from its type info
    // (docs/design/interface-type-tests.md). Without a class that can
    // answer, the ref.cast below traps unless the value is an `I`
    // fat pointer already, which is the same answer.
    if (dstSemOpt != null) {
      let dstBase = getNonNullableType(baseType(dstSemOpt));
      if (dstBase is InterfaceType && cx.typeTestHasSlot(dstBase)) {
        return cx.typeTestPack(value, dstBase, dstVt is ValTypeRefNull);
      }
    }
    // A value held as an interface (or a record) is a fat POINTER — the
    // instance paired with a vtable — so casting it to a concrete class
    // casts the pair, and traps. Unwrap to the instance first, the way
    // identity compares already do. Only when the destination is a
    // class: interface-to-interface is a re-pack, not a cast.
    if (dstSemOpt != null && baseType(dstSemOpt) is ClassType) {
      let srcSemOpt = cx.substitutedNodeType(expr.expression);
      if (srcSemOpt != null) {
        operand = cx.unwrapFatPointer(baseType(srcSemOpt), value);
      }
    }
    return checkedTupleCast(cx, expr, cx.b.emit(IrOp.ref_cast, dstVt, operand,
        cx.b.internType(dstVt)), dstVt);
  }
  if (isRefValType(srcVt) &&
      (dstVt is ValTypeI32 || dstVt is ValTypeI64 ||
       dstVt is ValTypeF32 || dstVt is ValTypeF64)) {
    // The checker rejects reference-to-primitive casts, so a value of
    // reference shape reaching a scalar destination is a compiler bug
    // rather than something to guess a representation for. This used
    // to unwrap a `Box<T>` chosen from the target type, which made
    // `"hi" as i32` compile and trap on a class the program never
    // named.
    bail(cx, 'reference operand for a scalar cast');
  }
  if (isRefValType(dstVt) && !isRefValType(srcVt)) {
    // `v as anyref` where this specialization made T a primitive.
    return impossibleRefCast(cx, dstVt);
  }
  let dstSem = cx.substitutedNodeType(expr);
  let dstUnsigned = dstSem != null && isUnsignedIntType(baseType(dstSem));
  return normalizeNarrow(cx,
      scalarConvert(cx, value, dstVt, isUnsignedNode(cx, expr.expression), dstUnsigned),
      narrowTarget);
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19