Lines
97.1 %
Functions
93.75 %
Branches
100 %
//! Control-flow special forms.
//!
//! Split by form:
//! - [`quote`] — `QUOTE`.
//! - [`if_form`] — `IF` (compile + for-stack + eval).
//! - [`progn`] — `PROGN` (sequencing).
//! - [`and_or`] — `AND` / `OR` short-circuit boolean forms.
//! - [`cond`] — `COND` clause chain (rewrites to nested IF for stack).
//! - [`block`] — `BLOCK` / `RETURN-FROM` lexical labelled exits.
mod and_or;
mod arms;
mod block;
mod block_exits;
mod block_parse;
mod case;
mod cond;
mod cond_runtime_chain;
pub(in crate::compiler) mod error_form;
mod exit_scan;
mod handler_case;
mod if_form;
mod nil_block;
mod progn;
mod quote;
mod skip_region;
mod tagbody;
mod unwind_protect;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{NilRepr, compile_for_stack, nil_repr};
use crate::error::Result;
use crate::runtime::SymbolTable;
use super::SpecialFormSpec;
pub(in crate::compiler) use arms::in_runtime_branch;
pub(in crate::compiler::special) use case::FORMS as CASE_FORMS;
pub(in crate::compiler::special) use nil_block::wrapped_loop;
pub(in crate::compiler) use unwind_protect::protect;
/// The divergence classifier for callers outside the `control` module. It
/// classifies on a clone, so the caller's table never sees the walk's
/// compile-time effects — see [`block_exits::form_diverges`].
pub(in crate::compiler) fn form_diverges_scratch(
symbols: &SymbolTable,
form: &Expr,
) -> Result<bool> {
block_exits::form_diverges(&mut symbols.clone(), form)
}
/// Whether `form`, just evaluated from exit count `exits`, leaves the rest of
/// its sequence dead for the eval surface's BLOCK join: it reached an exit
/// and always diverges. Classified only while a BLOCK is open outside a macro
/// body, which has no codegen to agree with.
pub(in crate::compiler) fn ends_live_code(
exits: u64,
if !symbols.has_eval_block()
|| symbols.is_macro_expansion()
|| symbols.measurements().exits() == exits
{
return Ok(false);
form_diverges_scratch(symbols, form)
/// A form a consumer's expected type reaches: its arms are still free to
/// adopt it.
#[derive(Clone, Copy)]
pub(in crate::compiler) enum ExpectingForm {
Block,
If,
impl ExpectingForm {
/// The BLOCK or IF form `expr` is, with its arguments. Neither name can be
/// redefined, so the head alone decides.
pub(in crate::compiler) fn of(expr: &Expr) -> Option<(Self, &[Expr])> {
let Expr::List(elems) = expr else {
return None;
};
let (Expr::Symbol(name), args) = elems.split_first()? else {
match name.as_str() {
"BLOCK" => Some((Self::Block, args)),
"IF" => Some((Self::If, args)),
_ => None,
pub(in crate::compiler) fn compile(
self,
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
expected: WasmType,
) -> Result<WasmType> {
match self {
Self::Block => block::compile_block_expecting(ctx, emit, symbols, args, Some(expected)),
Self::If => if_form::compile_if_expecting(ctx, emit, symbols, args, Some(expected)),
/// Any value is a valid test (ADR-0033). A runtime value needs an actual wasm
/// branch exactly when it CAN be `nil` at runtime (`nil_repr` is `I32Zero` or
/// `Null`); a `Never`-typed runtime value (index / scalar / money / closure)
/// can never be nil, so it folds true through [`apply_const_test`] instead —
/// compiled for its effects, never branched on. Every IF / COND path (eval,
/// stack, effect) shares this, so no path branches where another folds.
pub(in crate::compiler) fn is_runtime_test(test: &Expr) -> bool {
test.wasm_type()
.is_some_and(|ty| !matches!(nil_repr(&ty), NilRepr::Never))
/// Emits `test` as a bare i32 wasm condition, per the `nil_repr` of the type
/// it compiled to: a `Bool` already IS its own 0/1 condition; a nullable
/// reference's condition is "not null" (`ref.is_null` then `i32.eqz`). A test
/// that compiled to a never-nil type (an operand the probe could not type,
/// such as one that raises) has run; its value is dropped and the condition
/// is true. Shared by every runtime-test compile path (IF / COND / AND / OR).
pub(in crate::compiler) fn compile_test_condition(
test: &Expr,
) -> Result<()> {
let ty = compile_for_stack(ctx, emit, symbols, test)?;
match nil_repr(&ty) {
NilRepr::I32Zero => {}
NilRepr::Null => {
emit.ref_is_null();
emit.i32_eqz();
NilRepr::Never => {
emit.drop_value();
emit.i32_const(1);
Ok(())
/// Applies a const-path IF/COND test exactly once and returns the value to
/// branch on: an index test (always truthy) is compiled for its runtime
/// effects, a constant test's effects are ALSO compiled (not just
/// `eval_value`d) — a `setf` on a runtime local inside the test needs a real
/// `local.set`, which `eval_value` alone never emits, even though the
/// test's own resulting VALUE is statically known. `classified` is the
/// caller's own disposable-clone probe of `form`'s fold, so this returns it
/// directly rather than re-deriving it (which would re-apply the form's
/// compile-time effects a second time).
pub(in crate::compiler) fn apply_const_test(
ctx: &mut crate::compiler::context::CompileContext,
emit: &mut crate::compiler::emit::FunctionEmitter,
classified: &Expr,
) -> Result<Expr> {
crate::compiler::expr::compile_for_effect(ctx, emit, symbols, form)?;
Ok(classified.clone())
pub(super) const FORMS: &[SpecialFormSpec] = &[
SpecialFormSpec {
name: "QUOTE",
eval: quote::eval_quote,
compile: quote::spec_compile_quote,
stack: Some(quote::compile_quote_for_stack),
effect: Some(quote::compile_quote_effect),
},
name: "IF",
eval: if_form::if_form,
compile: if_form::compile_if,
stack: Some(if_form::compile_if_for_stack),
effect: None,
name: "PROGN",
eval: progn::progn_form,
compile: progn::compile_progn,
stack: Some(progn::compile_progn_for_stack),
name: "AND",
eval: and_or::and_form,
compile: and_or::compile_and,
stack: Some(and_or::compile_and_for_stack),
effect: Some(and_or::compile_and_for_effect),
name: "OR",
eval: and_or::or_form,
compile: and_or::compile_or,
stack: Some(and_or::compile_or_for_stack),
effect: Some(and_or::compile_or_for_effect),
name: "COND",
eval: cond::cond_form,
compile: cond::compile_cond,
stack: Some(cond::compile_cond_for_stack),
effect: Some(cond::compile_cond_for_effect),
name: "ERROR",
eval: error_form::eval_error,
compile: error_form::compile_error,
stack: Some(error_form::compile_error_for_stack),
name: "BLOCK",
eval: block::eval_block,
compile: block::compile_block,
stack: Some(block::compile_block_for_stack),
effect: Some(block::compile_block_for_effect),
name: "RETURN-FROM",
eval: block::eval_return_from,
compile: block::compile_return_from,
stack: Some(block::compile_return_from_for_stack),
name: "TAGBODY",
eval: tagbody::eval_tagbody,
compile: tagbody::compile_tagbody,
stack: Some(tagbody::compile_tagbody_for_stack),
effect: Some(tagbody::compile_tagbody_for_effect),
name: "GO",
eval: tagbody::eval_go,
compile: tagbody::compile_go,
stack: Some(tagbody::compile_go_for_stack),
name: "HANDLER-CASE",
eval: handler_case::eval_handler_case,
compile: handler_case::compile_handler_case,
stack: Some(handler_case::compile_handler_case_for_stack),
effect: Some(handler_case::compile_handler_case_for_effect),
name: "UNWIND-PROTECT",
eval: unwind_protect::eval_unwind_protect,
compile: unwind_protect::compile_unwind_protect,
stack: Some(unwind_protect::compile_unwind_protect_for_stack),
effect: Some(unwind_protect::compile_unwind_protect_for_effect),
];
#[cfg(test)]
#[path = "expected_tests.rs"]
mod expected_tests;