Lines
93.94 %
Functions
100 %
Branches
//! A DEFVAR / DEFPARAMETER of a global that is a cell of the unit (plan 0014
//! PERSIST): a run-time store into the cell that sets its bound flag.
use wasm_encoder::BlockType;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::runtime::{CellBinding, HomeKey, SymbolTable};
use super::super::cell_store::compile_store_value;
use super::define::{
compile_home_init, expansion_datum, home_base, lifts_closure, register_def_value,
};
pub(super) struct CellDefinition<'a> {
pub(super) name: &'a str,
pub(super) doc: Option<String>,
pub(super) init: &'a Expr,
pub(super) probed: &'a Expr,
/// A DEFVAR: the init runs only while the cell is unbound.
pub(super) only_unbound: bool,
pub(super) args: &'a [Expr],
}
/// What a macro body reads for the cell once the definition compiled.
enum Expansion {
/// A DEFVAR of a bound cell stores nothing, so the earlier read stands.
Keep,
Read(Expr),
/// The definition may or may not store, or its init has no datum.
Unknown,
/// Stores the init into the cell and sets its bound flag, as in Common Lisp.
pub(super) fn define_cell(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
def: CellDefinition,
) -> Result<()> {
let name = def.name;
let (Some((idx, ty)), Some(flag)) = (symbols.cell_global(name), symbols.cell_flag(name)) else {
return Err(Error::Compile(format!(
"{name}: the unit made no cell for it"
)));
let surely = symbols.runs_surely(def.args);
if def.only_unbound && symbols.is_lost_cell(name) {
register_def_value(symbols, name, None, def.doc);
return Ok(());
let expansion = cell_expansion(symbols, &def, surely);
if def.only_unbound {
emit.global_get(flag);
emit.i32_eqz();
emit.if_block(BlockType::Empty);
store_init(ctx, emit, symbols, &def, ty)?;
emit.global_set(idx);
emit.i32_const(1);
emit.global_set(flag);
emit.block_end()?;
symbols.note_cell_definition(name, surely);
apply_expansion(symbols, name, expansion);
Ok(())
fn cell_expansion(symbols: &SymbolTable, def: &CellDefinition, surely: bool) -> Expansion {
match (def.only_unbound, symbols.cell_binding(def.name)) {
(true, CellBinding::Bound) => Expansion::Keep,
(true, CellBinding::Maybe) => Expansion::Unknown,
_ if !surely => Expansion::Unknown,
_ => expansion_datum(symbols, def.name, def.probed)
.map_or(Expansion::Unknown, Expansion::Read),
fn apply_expansion(symbols: &mut SymbolTable, name: &str, expansion: Expansion) {
let Some(symbol) = symbols.lookup_mut(name) else {
return;
match expansion {
Expansion::Keep => {}
Expansion::Read(datum) => symbol.set_expansion_value(datum),
Expansion::Unknown => symbol.clear_expansion_value(),
/// Compiles the init into the cell: a constant one is recorded as the arm
/// that types the cell and built at the element of the cell's list type.
fn store_init(
def: &CellDefinition,
ty: WasmType,
if def.probed.is_wasm_runtime() || lifts_closure(def.probed) {
let place = Expr::Symbol(def.name.to_string());
return compile_store_value(ctx, emit, symbols, &place, def.init, ty);
for arm in home_base(def.name, def.probed)? {
symbols.note_store(&HomeKey::global(def.name), arm);
symbols.note_home_datum(def.name, def.probed);
compile_home_init(ctx, emit, symbols, def.init, def.probed, ty)