1
//! `NOT` and `NULL?` predicates. Both natives short-circuit at compile time
2
//! on constant operands and emit a wasm `i32_eqz` / `ref.is_null` check on
3
//! runtime values.
4

            
5
use crate::ast::{Expr, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::compiler::expr::{
9
    NilRepr, compile_for_stack, eval_value, fold_call, nil_repr, serialize_stack_to_output,
10
};
11
use crate::error::{Error, Result};
12
use crate::runtime::SymbolTable;
13

            
14
use super::shared::{bool_result, emit_bool};
15

            
16
46154
pub(super) fn not_fn(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
17
46154
    if args.len() != 1 {
18
1
        return Err(Error::Arity {
19
1
            name: "NOT".to_string(),
20
1
            expected: 1,
21
1
            actual: args.len(),
22
1
        });
23
46153
    }
24
46153
    let val = eval_value(symbols, &args[0])?;
25
45733
    if val.wasm_type().is_some() {
26
40925
        return Ok(Expr::WasmRuntime(WasmType::Bool));
27
4808
    }
28
4808
    Ok(bool_result(val.is_nil()))
29
46154
}
30

            
31
80220
pub(super) fn null_p(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
32
80220
    if args.len() != 1 {
33
1
        return Err(Error::Arity {
34
1
            name: "NULL?".to_string(),
35
1
            expected: 1,
36
1
            actual: args.len(),
37
1
        });
38
80219
    }
39
80219
    let val = eval_value(symbols, &args[0])?;
40
80219
    if val.wasm_type().is_some() {
41
76870
        return Ok(Expr::WasmRuntime(WasmType::Bool));
42
3349
    }
43
3349
    Ok(bool_result(matches!(val, Expr::Nil)))
44
80220
}
45

            
46
2528
pub(super) fn compile_not(
47
2528
    ctx: &mut CompileContext,
48
2528
    emit: &mut FunctionEmitter,
49
2528
    symbols: &mut SymbolTable,
50
2528
    args: &[Expr],
51
2528
) -> Result<()> {
52
2528
    check_arity("NOT", args)?;
53
2526
    if let Some(folded) = fold_call(symbols, "NOT", args)? {
54
634
        return emit_bool(ctx, emit, !folded.is_nil());
55
1892
    }
56
1892
    let ty = compile_nil_test(ctx, emit, symbols, &args[0])?;
57
1472
    serialize_stack_to_output(ctx, emit, ty)
58
2528
}
59

            
60
69308
fn check_arity(name: &str, args: &[Expr]) -> Result<()> {
61
69308
    if args.len() == 1 {
62
69304
        return Ok(());
63
4
    }
64
4
    Err(Error::Arity {
65
4
        name: name.to_string(),
66
4
        expected: 1,
67
4
        actual: args.len(),
68
4
    })
69
69308
}
70

            
71
/// Compiles `arg` and tests the result for nil.
72
65533
fn compile_nil_test(
73
65533
    ctx: &mut CompileContext,
74
65533
    emit: &mut FunctionEmitter,
75
65533
    symbols: &mut SymbolTable,
76
65533
    arg: &Expr,
77
65533
) -> Result<WasmType> {
78
65533
    let ty = compile_for_stack(ctx, emit, symbols, arg)?;
79
65101
    emit_nil_test(emit, &ty);
80
65101
    Ok(WasmType::Bool)
81
65533
}
82

            
83
1668
pub(super) fn compile_null_p(
84
1668
    ctx: &mut CompileContext,
85
1668
    emit: &mut FunctionEmitter,
86
1668
    symbols: &mut SymbolTable,
87
1668
    args: &[Expr],
88
1668
) -> Result<()> {
89
1668
    check_arity("NULL?", args)?;
90
1668
    if let Some(folded) = fold_call(symbols, "NULL?", args)? {
91
1252
        return emit_bool(ctx, emit, !folded.is_nil());
92
416
    }
93
416
    let ty = compile_nil_test(ctx, emit, symbols, &args[0])?;
94
416
    serialize_stack_to_output(ctx, emit, ty)
95
1668
}
96

            
97
27582
pub(super) fn compile_not_to_stack(
98
27582
    ctx: &mut CompileContext,
99
27582
    emit: &mut FunctionEmitter,
100
27582
    symbols: &mut SymbolTable,
101
27582
    args: &[Expr],
102
27582
) -> Result<WasmType> {
103
27582
    check_arity("NOT", args)?;
104
27580
    if let Some(folded) = fold_call(symbols, "NOT", args)? {
105
425
        emit.i32_const(i32::from(!folded.is_nil()));
106
425
        return Ok(WasmType::Bool);
107
27155
    }
108
27155
    compile_nil_test(ctx, emit, symbols, &args[0])
109
27582
}
110

            
111
/// A bool is nil at zero, every reference (a pair, a string, an anyref cell's
112
/// null nil, …) at null, and an index is never nil (a count of zero is still a
113
/// count) — its operand runs for effect and the test is false.
114
65101
fn emit_nil_test(emit: &mut FunctionEmitter, ty: &WasmType) {
115
65101
    match nil_repr(ty) {
116
        NilRepr::Never => {
117
1889
            emit.drop_value();
118
1889
            emit.i32_const(0);
119
        }
120
25416
        NilRepr::I32Zero => emit.i32_eqz(),
121
37796
        NilRepr::Null => emit.ref_is_null(),
122
    }
123
65101
}
124

            
125
37530
pub(super) fn compile_null_p_to_stack(
126
37530
    ctx: &mut CompileContext,
127
37530
    emit: &mut FunctionEmitter,
128
37530
    symbols: &mut SymbolTable,
129
37530
    args: &[Expr],
130
37530
) -> Result<WasmType> {
131
37530
    check_arity("NULL?", args)?;
132
37530
    if let Some(folded) = fold_call(symbols, "NULL?", args)? {
133
1460
        emit.i32_const(i32::from(!folded.is_nil()));
134
1460
        return Ok(WasmType::Bool);
135
36070
    }
136
36070
    compile_nil_test(ctx, emit, symbols, &args[0])
137
37530
}