1
//! A DEFVAR / DEFPARAMETER of a global that is a cell of the unit (plan 0014
2
//! PERSIST): a run-time store into the cell that sets its bound flag.
3

            
4
use wasm_encoder::BlockType;
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::emit::FunctionEmitter;
9
use crate::error::{Error, Result};
10
use crate::runtime::{CellBinding, HomeKey, SymbolTable};
11

            
12
use super::super::cell_store::compile_store_value;
13
use super::define::{
14
    compile_home_init, expansion_datum, home_base, lifts_closure, register_def_value,
15
};
16

            
17
pub(super) struct CellDefinition<'a> {
18
    pub(super) name: &'a str,
19
    pub(super) doc: Option<String>,
20
    pub(super) init: &'a Expr,
21
    pub(super) probed: &'a Expr,
22
    /// A DEFVAR: the init runs only while the cell is unbound.
23
    pub(super) only_unbound: bool,
24
    pub(super) args: &'a [Expr],
25
}
26

            
27
/// What a macro body reads for the cell once the definition compiled.
28
enum Expansion {
29
    /// A DEFVAR of a bound cell stores nothing, so the earlier read stands.
30
    Keep,
31
    Read(Expr),
32
    /// The definition may or may not store, or its init has no datum.
33
    Unknown,
34
}
35

            
36
/// Stores the init into the cell and sets its bound flag, as in Common Lisp.
37
125777
pub(super) fn define_cell(
38
125777
    ctx: &mut CompileContext,
39
125777
    emit: &mut FunctionEmitter,
40
125777
    symbols: &mut SymbolTable,
41
125777
    def: CellDefinition,
42
125777
) -> Result<()> {
43
125777
    let name = def.name;
44
125777
    let (Some((idx, ty)), Some(flag)) = (symbols.cell_global(name), symbols.cell_flag(name)) else {
45
        return Err(Error::Compile(format!(
46
            "{name}: the unit made no cell for it"
47
        )));
48
    };
49
125777
    let surely = symbols.runs_surely(def.args);
50
125777
    if def.only_unbound && symbols.is_lost_cell(name) {
51
416
        register_def_value(symbols, name, None, def.doc);
52
416
        return Ok(());
53
125361
    }
54
125361
    let expansion = cell_expansion(symbols, &def, surely);
55
125361
    if def.only_unbound {
56
106782
        emit.global_get(flag);
57
106782
        emit.i32_eqz();
58
106782
        emit.if_block(BlockType::Empty);
59
18579
    }
60
125361
    store_init(ctx, emit, symbols, &def, ty)?;
61
124105
    emit.global_set(idx);
62
124105
    emit.i32_const(1);
63
124105
    emit.global_set(flag);
64
124105
    if def.only_unbound {
65
105942
        emit.block_end()?;
66
18163
    }
67
124105
    register_def_value(symbols, name, None, def.doc);
68
124105
    symbols.note_cell_definition(name, surely);
69
124105
    apply_expansion(symbols, name, expansion);
70
124105
    Ok(())
71
125777
}
72

            
73
125361
fn cell_expansion(symbols: &SymbolTable, def: &CellDefinition, surely: bool) -> Expansion {
74
125361
    match (def.only_unbound, symbols.cell_binding(def.name)) {
75
4162
        (true, CellBinding::Bound) => Expansion::Keep,
76
1041
        (true, CellBinding::Maybe) => Expansion::Unknown,
77
120158
        _ if !surely => Expansion::Unknown,
78
116409
        _ => expansion_datum(symbols, def.name, def.probed)
79
116409
            .map_or(Expansion::Unknown, Expansion::Read),
80
    }
81
125361
}
82

            
83
124105
fn apply_expansion(symbols: &mut SymbolTable, name: &str, expansion: Expansion) {
84
124105
    let Some(symbol) = symbols.lookup_mut(name) else {
85
        return;
86
    };
87
124105
    match expansion {
88
4162
        Expansion::Keep => {}
89
7394
        Expansion::Read(datum) => symbol.set_expansion_value(datum),
90
112549
        Expansion::Unknown => symbol.clear_expansion_value(),
91
    }
92
124105
}
93

            
94
/// Compiles the init into the cell: a constant one is recorded as the arm
95
/// that types the cell and built at the element of the cell's list type.
96
125361
fn store_init(
97
125361
    ctx: &mut CompileContext,
98
125361
    emit: &mut FunctionEmitter,
99
125361
    symbols: &mut SymbolTable,
100
125361
    def: &CellDefinition,
101
125361
    ty: WasmType,
102
125361
) -> Result<()> {
103
125361
    if def.probed.is_wasm_runtime() || lifts_closure(def.probed) {
104
61698
        let place = Expr::Symbol(def.name.to_string());
105
61698
        return compile_store_value(ctx, emit, symbols, &place, def.init, ty);
106
63663
    }
107
63663
    for arm in home_base(def.name, def.probed)? {
108
63663
        symbols.note_store(&HomeKey::global(def.name), arm);
109
    }
110
63663
    symbols.note_home_datum(def.name, def.probed);
111
63663
    compile_home_init(ctx, emit, symbols, def.init, def.probed, ty)
112
125361
}