1
//! Primitive emit helpers — nil, bool, number, string, symbol — plus
2
//! the `push_ratio` numeric kernel and the `compile_text` machinery
3
//! `compile_string` and `compile_symbol` share.
4

            
5
use scripting_format::ValueType;
6

            
7
use uuid::Uuid;
8

            
9
use crate::ast::{Fraction, Number, WasmType};
10
use crate::compiler::context::CompileContext;
11
use crate::compiler::emit::FunctionEmitter;
12
use crate::compiler::layout::GcLocals;
13
use crate::error::{Error, Result};
14

            
15
use super::{LOCAL_GC_ARR, LOCAL_IDX};
16

            
17
14633
pub(in crate::compiler) fn compile_nil(
18
14633
    ctx: &mut CompileContext,
19
14633
    emit: &mut FunctionEmitter,
20
14633
) -> Result<()> {
21
14633
    ctx.serializer().write_debug_nil(emit)
22
14633
}
23

            
24
/// Emits a typed nil stack value for `ty` — the canonical nil for a
25
/// binding/accumulator/arm-reconciliation result declared with that wasm
26
/// type. Routes through [`nil_repr`] (ADR-0033): a `Never` type (an Index,
27
/// Scalar, Money or Closure) has no valid nil and errors rather than
28
/// fabricate a made-up zero — the nil-punning the Common Lisp boolean model
29
/// retires. The message names the fix regardless of which `Never` type
30
/// triggered it, since the remedy is the same for all of them.
31
192596
pub(in crate::compiler) fn emit_nil_default(
32
192596
    ctx: &CompileContext,
33
192596
    emit: &mut FunctionEmitter,
34
192596
    ty: WasmType,
35
192596
) -> Result<()> {
36
192596
    match ty {
37
        // `nil_repr` classifies these `Never`: no fabricated zero, ever.
38
        // Pinned against this match by
39
        // `nil_repr_covers_every_wasm_type_and_matches_emit_nil_default`.
40
1055
        WasmType::I32 | WasmType::Ratio | WasmType::Commodity | WasmType::Closure(_) => Err(
41
1055
            Error::Compile("nil is not a scalar; initialise with 0".to_string()),
42
1055
        ),
43
        WasmType::Bool => {
44
71815
            emit.i32_const(0);
45
71815
            Ok(())
46
        }
47
        WasmType::StringRef => {
48
10214
            emit.ref_null(ctx.ids.ty_i8_array);
49
10214
            Ok(())
50
        }
51
        WasmType::SymbolRef => {
52
219
            emit.ref_null(ctx.ids.ty_symbol);
53
219
            Ok(())
54
        }
55
        WasmType::PairRef(_) => {
56
80106
            emit.ref_null(ctx.ids.ty_pair);
57
80106
            Ok(())
58
        }
59
        WasmType::EntityRef(kind) => {
60
3159
            emit.ref_null(ctx.ids.entity_type(kind));
61
3159
            Ok(())
62
        }
63
        WasmType::StructRef(_) => {
64
2498
            emit.ref_null(ctx.ids.ty_struct);
65
2498
            Ok(())
66
        }
67
        WasmType::AnyRef => {
68
23530
            emit.ref_null_any();
69
23530
            Ok(())
70
        }
71
    }
72
192596
}
73

            
74
5672
pub(in crate::compiler) fn compile_t(
75
5672
    ctx: &mut CompileContext,
76
5672
    emit: &mut FunctionEmitter,
77
5672
) -> Result<()> {
78
5672
    ctx.serializer().write_debug_t(emit)
79
5672
}
80

            
81
72391
pub(in crate::compiler) fn compile_number(
82
72391
    ctx: &mut CompileContext,
83
72391
    emit: &mut FunctionEmitter,
84
72391
    number: Number,
85
72391
) -> Result<()> {
86
72391
    ctx.serializer().write_debug_number(emit, number)
87
72391
}
88

            
89
817097
fn compile_text(
90
817097
    ctx: &mut CompileContext,
91
817097
    emit: &mut FunctionEmitter,
92
817097
    value_type: ValueType,
93
817097
    s: &str,
94
817097
) -> Result<()> {
95
817097
    let data_idx = ctx.add_data(s.as_bytes())?;
96
817097
    let gc = GcLocals {
97
817097
        type_idx: ctx.ids.ty_i8_array,
98
817097
        arr: LOCAL_GC_ARR,
99
817097
        idx: LOCAL_IDX,
100
817097
    };
101
817097
    let len = u32::try_from(s.len())
102
        .map_err(|_| Error::Compile(format!("string literal of {} bytes exceeds u32", s.len())))?;
103
817097
    ctx.serializer()
104
817097
        .write_debug_string_gc(emit, value_type, data_idx, len, &gc)
105
817097
}
106

            
107
10006
pub(in crate::compiler) fn compile_string(
108
10006
    ctx: &mut CompileContext,
109
10006
    emit: &mut FunctionEmitter,
110
10006
    s: &str,
111
10006
) -> Result<()> {
112
10006
    compile_text(ctx, emit, ValueType::String, s)
113
10006
}
114

            
115
807091
pub(in crate::compiler) fn compile_symbol(
116
807091
    ctx: &mut CompileContext,
117
807091
    emit: &mut FunctionEmitter,
118
807091
    s: &str,
119
807091
) -> Result<()> {
120
807091
    compile_text(ctx, emit, ValueType::Symbol, s)
121
807091
}
122

            
123
/// Pushes a `$symbol` whose name is `name`, the printed name.
124
70575
pub(in crate::compiler) fn push_symbol(
125
70575
    ctx: &mut CompileContext,
126
70575
    emit: &mut FunctionEmitter,
127
70575
    name: &str,
128
70575
) -> Result<WasmType> {
129
70575
    let data_idx = ctx.add_data(name.as_bytes())?;
130
70575
    let len = i32::try_from(name.len())
131
        .map_err(|_| Error::Compile(format!("symbol of {} bytes exceeds i32", name.len())))?;
132
70575
    emit.i32_const(0);
133
70575
    emit.i32_const(len);
134
70575
    emit.array_new_data(ctx.ids.ty_i8_array, data_idx);
135
70575
    emit.struct_new(ctx.ids.ty_symbol);
136
70575
    Ok(WasmType::SymbolRef)
137
70575
}
138

            
139
142755
pub(in crate::compiler) fn push_ratio(
140
142755
    ctx: &CompileContext,
141
142755
    emit: &mut FunctionEmitter,
142
142755
    numer: i64,
143
142755
    denom: i64,
144
142755
) {
145
142755
    emit.i64_const(numer);
146
142755
    emit.i64_const(denom);
147
142755
    emit.call(ctx.ids.ratio_new);
148
142755
}
149

            
150
/// Pushes an atomic money value: the rational and the commodity's UUID halves,
151
/// built by the module's `commodity_new` (null unit term).
152
3953
pub(in crate::compiler) fn push_money(
153
3953
    ctx: &CompileContext,
154
3953
    emit: &mut FunctionEmitter,
155
3953
    amount: Fraction,
156
3953
    commodity_id: Uuid,
157
3953
) {
158
3953
    let (hi, lo) = commodity_id.as_u64_pair();
159
3953
    emit.i64_const(*amount.numer());
160
3953
    emit.i64_const(*amount.denom());
161
3953
    emit.i64_const(hi as i64);
162
3953
    emit.i64_const(lo as i64);
163
3953
    emit.call(ctx.ids.commodity_new);
164
3953
}
165

            
166
#[cfg(test)]
167
#[path = "atoms_tests.rs"]
168
mod tests;