Lines
100 %
Functions
Branches
//! `to_stack` codegen for `+ - * / MOD`. An all-literal call const-folds; a
//! runtime call compiles each non-literal operand to a local (which yields
//! its real wasm type — including closure-call results the eval path cannot
//! reduce) and hands the operands to the shared emitter in [`super::emit`],
//! which applies the ADR-0028 dimension rules.
use super::ArithOp;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{
compile_for_effect, compile_raising, compile_to_local, eval_value, is_division_by_zero,
push_number,
};
use crate::error::Result;
use crate::runtime::SymbolTable;
use super::emit::{Slot, check_arity, emit_slots};
use super::eval::{Operands, fold_add, fold_div, fold_mod, fold_mul, fold_sub, try_fold};
/// Compile each operand once: a constant (syntactic, or one that resolves to
/// a `Number`) stays unemitted with the dimension its kind fixes; everything
/// else is emitted to a fresh local, capturing its real wasm type and its side
/// effects in operand order. The constant probe runs on a clone so it applies
/// no live mutation.
fn collect_slots(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<Vec<Slot>> {
let mut slots = Vec::with_capacity(args.len());
for arg in args {
match eval_value(&mut symbols.clone(), arg) {
Ok(Expr::Number(n)) => {
// A non-literal that *resolves* to a number (e.g. `(setf x 1)`)
// still emits its effects on the live table, in operand order,
// before the constant is recorded.
if !matches!(arg, Expr::Number(_)) {
compile_for_effect(ctx, emit, symbols, arg)?;
}
slots.push(Slot::Literal(n));
_ if compile_raising(ctx, emit, symbols, arg)? => slots.push(Slot::Raises),
_ => slots.push(match compile_to_local(ctx, emit, symbols, arg)? {
Some((local, ty)) => Slot::Runtime(local, ty),
None => Slot::Raises,
}),
Ok(slots)
pub(super) fn compile_add_to_stack(
) -> Result<WasmType> {
compile_arith_to_stack(ctx, emit, symbols, args, ArithOp::Add)
pub(super) fn compile_sub_to_stack(
compile_arith_to_stack(ctx, emit, symbols, args, ArithOp::Sub)
pub(super) fn compile_mul_to_stack(
compile_arith_to_stack(ctx, emit, symbols, args, ArithOp::Mul)
pub(super) fn compile_div_to_stack(
compile_arith_to_stack(ctx, emit, symbols, args, ArithOp::Div)
pub(super) fn compile_mod_to_stack(
compile_arith_to_stack(ctx, emit, symbols, args, ArithOp::Mod)
fn compile_arith_to_stack(
op: ArithOp,
check_arity(op, args.len())?;
if let Some(operands) = try_fold(symbols, args, op)? {
let nums = &operands.values;
let folded = match op {
ArithOp::Add => fold_add(nums),
ArithOp::Sub => fold_sub(nums),
ArithOp::Mul => fold_mul(nums),
ArithOp::Div => fold_div(nums, operands.is_index()),
ArithOp::Mod => fold_mod(nums, operands.is_index()),
return match folded {
Ok(value) => push_number(ctx, emit, operands.kinded(value)),
Err(err) if is_division_by_zero(&err) => {
emit_constant_operation(ctx, emit, op, &operands)
Err(other) => Err(other),
let slots = collect_slots(ctx, emit, symbols, args)?;
emit_slots(ctx, emit, op, &slots)
/// An all-constant division by zero is emitted at runtime, so the condition is
/// the catchable `DIVISION-BY-ZERO` trap, not a compile error (ISS-065).
fn emit_constant_operation(
operands: &Operands,
let slots: Vec<Slot> = operands.nums.iter().copied().map(Slot::Literal).collect();