1
//! A LET-bound lambda called with arguments its emitted closure signature
2
//! cannot take is compiled from its source, so each parameter has the type of
3
//! the argument (0014 LP1, ISS-209). A call that fits keeps the `call_ref`.
4

            
5
use crate::ast::{ClosureSigId, Expr, LambdaParams, Place, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::error::Result;
9
use crate::runtime::SymbolTable;
10

            
11
use crate::compiler::join::{NilRepr, nil_repr};
12

            
13
use super::call::{compile_call_ref, compile_lambda_call, compile_lambda_call_for_stack};
14
use super::fold::probe_value;
15
use super::operand::always_raises;
16
use super::stack::classify_stack_type;
17
use crate::compiler::special::{has_type_evidence, is_capture_free};
18

            
19
/// A call of a closure value: compiled from its source when an argument does
20
/// not fit the emitted signature, else through `call_ref`.
21
39387
pub(super) fn compile_closure_call(
22
39387
    ctx: &mut CompileContext,
23
39387
    emit: &mut FunctionEmitter,
24
39387
    symbols: &mut SymbolTable,
25
39387
    (place, sig): (Place, ClosureSigId),
26
39387
    args: &[Expr],
27
39387
) -> Result<()> {
28
    if let Some((params, body)) =
29
39387
        respecialised(symbols, place, &ctx.closure_sig(sig)?.params, args)?
30
    {
31
654
        return symbols.within_shared_recursion(|symbols| {
32
654
            compile_lambda_call(ctx, emit, symbols, &params, &body, args, None)
33
654
        });
34
38733
    }
35
38733
    let ty = compile_call_ref(ctx, emit, symbols, place, sig, args)?;
36
38101
    crate::compiler::expr::serialize_stack_to_output(ctx, emit, ty)
37
39387
}
38

            
39
/// A call of a closure value for its stack value; see `compile_closure_call`.
40
38372
pub(super) fn compile_closure_call_for_stack(
41
38372
    ctx: &mut CompileContext,
42
38372
    emit: &mut FunctionEmitter,
43
38372
    symbols: &mut SymbolTable,
44
38372
    (place, sig): (Place, ClosureSigId),
45
38372
    args: &[Expr],
46
38372
) -> Result<WasmType> {
47
    if let Some((params, body)) =
48
38372
        respecialised(symbols, place, &ctx.closure_sig(sig)?.params, args)?
49
    {
50
256
        return symbols.within_shared_recursion(|symbols| {
51
256
            compile_lambda_call_for_stack(ctx, emit, symbols, &params, &body, args, None)
52
256
        });
53
38116
    }
54
38116
    compile_call_ref(ctx, emit, symbols, place, sig, args)
55
38372
}
56

            
57
/// The source of the closure held in `place`, iff an argument does not fit the
58
/// parameter the closure was emitted with.
59
127703
pub(in crate::compiler) fn respecialised(
60
127703
    symbols: &SymbolTable,
61
127703
    place: Place,
62
127703
    params: &[WasmType],
63
127703
    args: &[Expr],
64
127703
) -> Result<Option<(LambdaParams, Expr)>> {
65
127703
    let Some(source) = symbols
66
127703
        .closure_body(place)
67
63201
        .filter(|(lambda, body)| is_capture_free(symbols, lambda, body))
68
    else {
69
64509
        return Ok(None);
70
    };
71
63194
    if params.len() != args.len() {
72
416
        return Ok(None);
73
62778
    }
74
62778
    let mut probe = symbols.clone();
75
62778
    for (index, (arg, &param)) in args.iter().zip(params).enumerate() {
76
51920
        if !keeps_closure_call(&mut probe, arg, param, || {
77
256
            has_type_evidence(symbols, &source.0, &source.1, index)
78
256
        })? {
79
2245
            return Ok(Some(source.clone()));
80
49675
        }
81
    }
82
60533
    Ok(None)
83
127703
}
84

            
85
/// A nil argument into a never-nil parameter the body typed keeps the closure
86
/// call, so it reports "nil is not a ..."; a parameter typed only by default
87
/// is recompiled for the argument like any other.
88
51926
fn keeps_closure_call(
89
51926
    probe: &mut SymbolTable,
90
51926
    arg: &Expr,
91
51926
    param: WasmType,
92
51926
    evidence: impl FnOnce() -> bool,
93
51926
) -> Result<bool> {
94
51926
    let value = probe_value(probe, arg)?;
95
51926
    if matches!(value, Expr::Nil) && nil_repr(&param) == NilRepr::Never && evidence() {
96
214
        return Ok(true);
97
51712
    }
98
51712
    call_fits(probe, arg, &value, param)
99
51926
}
100

            
101
/// Whether `compile_for_stack_as(arg, param)` accepts the argument: nil, a
102
/// symbol into a string, an equal stack type.
103
/// Nil into a never-nil parameter does not fit: `compile_for_stack_as` rejects it.
104
51875
fn call_fits(probe: &mut SymbolTable, arg: &Expr, value: &Expr, param: WasmType) -> Result<bool> {
105
51875
    let nil_fits = matches!(value, Expr::Nil) && nil_repr(&param) != NilRepr::Never;
106
51875
    if nil_fits || always_raises(probe, arg)? {
107
452
        return Ok(true);
108
51423
    }
109
51423
    Ok(match classify_stack_type(value) {
110
20
        Some(WasmType::SymbolRef) => matches!(param, WasmType::SymbolRef | WasmType::StringRef),
111
51403
        Some(ty) => ty == param,
112
        None => true,
113
    })
114
51875
}
115

            
116
#[cfg(test)]
117
#[path = "respecialise_tests.rs"]
118
mod tests;