Lines
98.33 %
Functions
100 %
Branches
//! The cells a unit makes of the globals it writes (plan 0014 PERSIST): each
//! is allocated, seeded and registered before the first form compiles, with
//! one i32 flag that is 1 once the cell holds a value.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::{CompileContext, LOST_FLAG};
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::fold::stores::StoreScan;
use crate::compiler::join::{Arm, reconcile_arms};
use crate::error::Result;
use crate::runtime::{HomeKey, Seed, Slot, Symbol, SymbolKind, SymbolTable, is_list_source};
use super::super::cell_store::store_arm;
use super::define::{
closure_home, expansion_datum, list_source_home, register_expansion_value, seeded_home,
};
use super::let_compile::record_closure_result;
/// Makes a cell of every global the scan names, in name order. A cell with an
/// earlier unit's constant starts bound to it, one with an earlier unit's value
/// no constant holds starts bound but unreadable (flag `LOST_FLAG`); any other starts unbound, typed
/// by its first writer when that is a literal and by the previous attempt's
/// hint, or provisionally `AnyRef` until a second attempt joins its stores.
pub(super) fn allocate_cells(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
scan: &StoreScan,
) -> Result<()> {
scan.cells()
.iter()
.try_for_each(|name| allocate_cell(ctx, emit, symbols, scan, name))
}
fn allocate_cell(
name: &str,
let flag = ctx.alloc_mutable_global(WasmType::I32)?;
let (seed, lost) = cell_seed(symbols, scan, name);
let kind = match (&seed, &lost) {
(Some(Expr::Lambda(..)), _) => Seed::Closure,
(Some(_), _) => Seed::Constant,
(None, Some(_)) => Seed::Lost,
(None, None) => Seed::Unbound,
symbols.note_cell(name, flag, kind);
match &seed {
Some(constant) => {
let home = seeded_home(ctx, emit, symbols, name, constant)?;
emit.i32_const(1);
emit.global_set(flag);
bind_home(symbols, name, home);
let datum = expansion_datum(symbols, name, constant);
register_expansion_value(symbols, name, datum);
None => {
let home = unbound_home(ctx, symbols, scan, name)?;
match (&home, lost) {
(Expr::WasmGlobal(idx, _), Some(type_name)) => {
emit.i32_const(LOST_FLAG);
ctx.note_lost_cell(*idx, flag, name, &type_name)?;
(Expr::WasmGlobal(idx, _), None) => ctx.note_unbound_cell(*idx, flag, name)?,
_ => {}
if !scan.defines(name) && !symbols.is_special(name) {
symbols.mark_special(name);
symbols.mark_created_global(name);
symbols.note_definition(name, Slot::Value, flag);
Ok(())
/// What a cell starts as: an earlier unit's constant, else the type of a value
/// no constant holds. A lambda seeds a closure only when the unit stores
/// nothing but lambdas to it; any other store would need the cell to hold
/// both, so the lambda is then a value that does not survive the unit. A store
/// of another signature refuses the seed for the next attempt.
fn cell_seed(
symbols: &SymbolTable,
) -> (Option<Expr>, Option<String>) {
match symbols.constant_global(name) {
Some(Expr::Lambda(..))
if !scan.writes_only_lambdas(name) || symbols.is_seed_refused(name) =>
{
(None, Some("closure".to_string()))
Some(source) if is_list_source(&source) => (None, Some("list".to_string())),
Some(constant) => (Some(constant), None),
None => match symbols.instance_global(name) {
Some(instance) => (Some(instance), None),
None => (None, symbols.lost_type(name).map(str::to_string)),
},
/// The home of a cell nothing has written: a closure of the shape the previous
/// attempt stored, else typed by the first literal writer and the previous
/// attempt's hint, else provisionally `AnyRef`.
fn unbound_home(
) -> Result<Expr> {
if let Some(shape) = symbols.closure_hint(name) {
let ty = WasmType::Closure(ctx.intern_closure_signature(&shape.params, shape.result)?);
let home = Expr::WasmGlobal(ctx.alloc_mutable_global(ty)?, ty);
record_closure_result(symbols, ctx, &home)?;
return Ok(home);
let hint = symbols.hint(&HomeKey::global(name)).map(Arm::Value);
let literal = scan.first_literal(name);
let base: Vec<Arm> = hint
.into_iter()
.chain(literal.and_then(store_arm))
.collect();
let ty = match base.is_empty() {
true => {
symbols.note_provisional_home(name);
WasmType::AnyRef
false => {
let ty = reconcile_arms(&base);
symbols.note_home(name, base, literal.cloned(), ty);
ty
Ok(Expr::WasmGlobal(ctx.alloc_mutable_global(ty)?, ty))
/// Gives every lambda an earlier unit left a global that the unit names a
/// closure home, as if the unit began with a DEFVAR of it that ran, so the
/// host carries it on again.
pub(super) fn adopt_closures(
scan.adopted()
.try_for_each(|name| adopt_closure(ctx, emit, symbols, name))
fn adopt_closure(
let Some(lambda) = symbols.constant_global(name) else {
return Ok(());
let home = match is_list_source(&lambda) {
true => list_source_home(ctx, emit, symbols, &lambda)?,
false => closure_home(ctx, emit, symbols, name, &lambda, &lambda)?,
fn bind_home(symbols: &mut SymbolTable, name: &str, home: Expr) {
match symbols.lookup_mut(name) {
Some(symbol) => symbol.set_value(home),
None => symbols.define(Symbol::new(name, SymbolKind::Variable).with_value(home)),