1
//! A `LIST` holding a lambda literal. Closure creation reads its captures by
2
//! value, so every non-literal member is evaluated into a local in source
3
//! order first; the chain's element type is then read off the compiled values.
4

            
5
use crate::ast::{Expr, PairElement, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::compiler::expr::{compile_for_stack, emit_box_anyref, emit_box_cell};
9
use crate::compiler::special::record_closure_result;
10
use crate::error::Result;
11
use crate::runtime::SymbolTable;
12

            
13
use super::cons::{close_pair_chain, push_pair_car};
14
use super::infer::chain_pair_element;
15

            
16
enum Member {
17
    Literal,
18
    Spilled { local: u32, ty: WasmType },
19
}
20

            
21
/// A macro body runs at expansion time, where a lambda stays data.
22
12603267
pub(super) fn lambda_runs_at_runtime(symbols: &SymbolTable, values: &[Expr]) -> bool {
23
12603267
    !symbols.is_macro_expansion() && values.iter().any(|value| matches!(value, Expr::Lambda(..)))
24
12603267
}
25

            
26
27765
pub(super) fn compile_spilled_chain(
27
27765
    ctx: &mut CompileContext,
28
27765
    emit: &mut FunctionEmitter,
29
27765
    symbols: &mut SymbolTable,
30
27765
    args: &[Expr],
31
27765
    resolved: &[Expr],
32
27765
) -> Result<WasmType> {
33
27765
    let members = args
34
27765
        .iter()
35
40060
        .map(|arg| spill_member(ctx, emit, symbols, arg))
36
211
        .collect::<Result<Vec<_>>>()?;
37
27554
    let typed: Vec<Expr> = members
38
27554
        .iter()
39
27554
        .zip(resolved)
40
39849
        .map(|(member, value)| match member {
41
8336
            Member::Literal => value.clone(),
42
31513
            Member::Spilled { ty, .. } => Expr::WasmRuntime(*ty),
43
39849
        })
44
        .collect();
45
27554
    let elem = chain_pair_element(&typed)?;
46
27554
    for (member, arg) in members.iter().zip(args) {
47
39849
        match member {
48
8336
            Member::Literal => push_pair_car(ctx, emit, symbols, arg, elem)?,
49
31513
            Member::Spilled { local, ty } => push_spilled(ctx, emit, *local, *ty, elem),
50
        }
51
    }
52
27554
    close_pair_chain(ctx, emit, args.len());
53
27554
    Ok(WasmType::PairRef(elem))
54
27765
}
55

            
56
40060
fn spill_member(
57
40060
    ctx: &mut CompileContext,
58
40060
    emit: &mut FunctionEmitter,
59
40060
    symbols: &mut SymbolTable,
60
40060
    arg: &Expr,
61
40060
) -> Result<Member> {
62
40060
    if is_literal(arg) {
63
8336
        return Ok(Member::Literal);
64
31724
    }
65
31724
    let ty = compile_for_stack(ctx, emit, symbols, arg)?;
66
31513
    let local = ctx.alloc_local(ty)?;
67
31513
    emit.local_set(local);
68
31513
    record_closure_result(symbols, ctx, &Expr::WasmLocal(local, ty))?;
69
31513
    Ok(Member::Spilled { local, ty })
70
40060
}
71

            
72
40060
fn is_literal(arg: &Expr) -> bool {
73
40060
    matches!(
74
40060
        arg,
75
        Expr::Number(_)
76
            | Expr::String(_)
77
            | Expr::T
78
            | Expr::Nil
79
            | Expr::Keyword(_)
80
            | Expr::Quote(_)
81
            | Expr::Datum(_)
82
    )
83
40060
}
84

            
85
31513
fn push_spilled(
86
31513
    ctx: &CompileContext,
87
31513
    emit: &mut FunctionEmitter,
88
31513
    local: u32,
89
31513
    ty: WasmType,
90
31513
    elem: PairElement,
91
31513
) {
92
31513
    emit.local_get(local);
93
31513
    match elem {
94
10627
        PairElement::AnyRef => emit_box_anyref(ctx, emit, ty),
95
20886
        _ => emit_box_cell(ctx, emit, elem, elem),
96
    }
97
31513
}