1
//! `SPLIT-*` accessors — `VALUE-NUM`, `VALUE-DENOM`, `VALUE` (combined
2
//! ratio), `RECONCILE-STATE`, `RECONCILE-DATE`. Each takes an entity
3
//! index and reads a single field from `SplitData`.
4

            
5
use crate::ast::{Expr, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::compiler::expr::{LOCAL_TEMP_I32, eval_value};
9
use crate::error::Result;
10
use crate::runtime::SymbolTable;
11

            
12
use super::{arity_check, compile_idx_to_stack, emit_entity_data_offset};
13

            
14
832
pub(super) fn split_value_num(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
15
832
    arity_check("SPLIT-VALUE-NUM", args, 1)?;
16
832
    eval_value(symbols, &args[0])?;
17
832
    Ok(Expr::WasmRuntime(WasmType::Ratio))
18
832
}
19

            
20
419
pub(super) fn compile_split_value_num_to_stack(
21
419
    ctx: &mut CompileContext,
22
419
    emit: &mut FunctionEmitter,
23
419
    symbols: &mut SymbolTable,
24
419
    args: &[Expr],
25
419
) -> Result<WasmType> {
26
419
    arity_check("SPLIT-VALUE-NUM", args, 1)?;
27
419
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
28
416
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
29
416
    emit.i64_load(32); // value_num at SplitData offset 32
30
416
    emit.call(ctx.ids.ratio_from_i64);
31
416
    Ok(WasmType::Ratio)
32
419
}
33

            
34
832
pub(super) fn split_value_denom(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
35
832
    arity_check("SPLIT-VALUE-DENOM", args, 1)?;
36
832
    eval_value(symbols, &args[0])?;
37
832
    Ok(Expr::WasmRuntime(WasmType::Ratio))
38
832
}
39

            
40
416
pub(super) fn compile_split_value_denom_to_stack(
41
416
    ctx: &mut CompileContext,
42
416
    emit: &mut FunctionEmitter,
43
416
    symbols: &mut SymbolTable,
44
416
    args: &[Expr],
45
416
) -> Result<WasmType> {
46
416
    arity_check("SPLIT-VALUE-DENOM", args, 1)?;
47
416
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
48
416
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
49
416
    emit.i64_load(40); // value_denom at SplitData offset 40
50
416
    emit.call(ctx.ids.ratio_from_i64);
51
416
    Ok(WasmType::Ratio)
52
416
}
53

            
54
/// (split-value idx) — reads both `value_num` and `value_denom`, returns proper Ratio
55
13342
pub(super) fn split_value(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
56
13342
    arity_check("SPLIT-VALUE", args, 1)?;
57
13342
    eval_value(symbols, &args[0])?;
58
13342
    Ok(Expr::WasmRuntime(WasmType::Ratio))
59
13342
}
60

            
61
2918
pub(super) fn compile_split_value_to_stack(
62
2918
    ctx: &mut CompileContext,
63
2918
    emit: &mut FunctionEmitter,
64
2918
    symbols: &mut SymbolTable,
65
2918
    args: &[Expr],
66
2918
) -> Result<WasmType> {
67
2918
    arity_check("SPLIT-VALUE", args, 1)?;
68
2918
    let temp = LOCAL_TEMP_I32;
69
    // Compute data offset, save in temp
70
2918
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
71
2918
    emit_entity_data_offset(ctx, emit, temp)?;
72
2918
    emit.local_set(temp);
73
    // Load value_num (i64 at offset 32)
74
2918
    emit.local_get(temp);
75
2918
    emit.i64_load(32);
76
    // Load value_denom (i64 at offset 40)
77
2918
    emit.local_get(temp);
78
2918
    emit.i64_load(40);
79
    // Construct ratio from num and denom
80
2918
    emit.call(ctx.ids.ratio_new);
81
2918
    Ok(WasmType::Ratio)
82
2918
}
83

            
84
2496
pub(super) fn split_reconcile_state(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
85
2496
    arity_check("SPLIT-RECONCILE-STATE", args, 1)?;
86
2496
    eval_value(symbols, &args[0])?;
87
2496
    Ok(Expr::WasmRuntime(WasmType::I32))
88
2496
}
89

            
90
832
pub(super) fn compile_split_reconcile_state_to_stack(
91
832
    ctx: &mut CompileContext,
92
832
    emit: &mut FunctionEmitter,
93
832
    symbols: &mut SymbolTable,
94
832
    args: &[Expr],
95
832
) -> Result<WasmType> {
96
832
    arity_check("SPLIT-RECONCILE-STATE", args, 1)?;
97
832
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
98
832
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
99
832
    emit.i32_load8_u(48); // reconcile_state at SplitData offset 48
100
832
    Ok(WasmType::I32)
101
832
}
102

            
103
832
pub(super) fn split_reconcile_date(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
104
832
    arity_check("SPLIT-RECONCILE-DATE", args, 1)?;
105
832
    eval_value(symbols, &args[0])?;
106
832
    Ok(Expr::WasmRuntime(WasmType::Ratio))
107
832
}
108

            
109
416
pub(super) fn compile_split_reconcile_date_to_stack(
110
416
    ctx: &mut CompileContext,
111
416
    emit: &mut FunctionEmitter,
112
416
    symbols: &mut SymbolTable,
113
416
    args: &[Expr],
114
416
) -> Result<WasmType> {
115
416
    arity_check("SPLIT-RECONCILE-DATE", args, 1)?;
116
416
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
117
416
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
118
416
    emit.i64_load(56); // reconcile_date at SplitData offset 56
119
416
    emit.call(ctx.ids.ratio_from_i64);
120
416
    Ok(WasmType::Ratio)
121
416
}
122

            
123
7508
pub(super) fn split_account_name(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
124
7508
    arity_check("SPLIT-ACCOUNT-NAME", args, 1)?;
125
7507
    eval_value(symbols, &args[0])?;
126
7507
    Ok(Expr::WasmRuntime(WasmType::StringRef))
127
7508
}
128

            
129
/// `(split-account-name idx)` — copies the posting account's display name out
130
/// of the strings pool into a fresh `(array (mut i8))`, reading the
131
/// `account_name_offset` (u32) / `account_name_len` (u32) pair the serializer
132
/// wrote into `SplitData`. Mirrors the tag string reader; the length is a u32
133
/// here (a full `i32.load`), not the u16 a tag carries.
134
2502
pub(super) fn compile_split_account_name_to_stack(
135
2502
    ctx: &mut CompileContext,
136
2502
    emit: &mut FunctionEmitter,
137
2502
    symbols: &mut SymbolTable,
138
2502
    args: &[Expr],
139
2502
) -> Result<WasmType> {
140
    use std::mem::offset_of;
141
2502
    arity_check("SPLIT-ACCOUNT-NAME", args, 1)?;
142

            
143
2502
    let data_local = ctx.alloc_local(WasmType::I32)?;
144
2502
    let str_off_local = ctx.alloc_local(WasmType::I32)?;
145
2502
    let str_len_local = ctx.alloc_local(WasmType::I32)?;
146
2502
    let pool_local = ctx.alloc_local(WasmType::I32)?;
147
2502
    let arr_local = ctx.alloc_local(WasmType::StringRef)?;
148
2502
    let idx_local = ctx.alloc_local(WasmType::I32)?;
149

            
150
2502
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
151
2502
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
152
2502
    emit.local_set(data_local);
153

            
154
2502
    emit.local_get(data_local);
155
2502
    emit.i32_load(offset_of!(scripting_format::SplitData, account_name_offset) as u64);
156
2502
    emit.local_set(str_off_local);
157

            
158
2502
    emit.local_get(data_local);
159
2502
    emit.i32_load(offset_of!(scripting_format::SplitData, account_name_len) as u64);
160
2502
    emit.local_set(str_len_local);
161

            
162
2502
    emit.call(ctx.ids.get_input_offset()?);
163
2502
    emit.i32_load(offset_of!(scripting_format::GlobalHeader, strings_pool_offset) as u64);
164
2502
    emit.local_set(pool_local);
165

            
166
2502
    let i8_array_idx = ctx.ids.ty_i8_array;
167
2502
    emit.local_get(str_len_local);
168
2502
    emit.array_new_default(i8_array_idx);
169
2502
    emit.local_set(arr_local);
170

            
171
2502
    emit.i32_const(0);
172
2502
    emit.local_set(idx_local);
173
2502
    emit.block_start();
174
2502
    emit.loop_start();
175

            
176
2502
    emit.local_get(idx_local);
177
2502
    emit.local_get(str_len_local);
178
2502
    emit.i32_ge_u();
179
2502
    emit.br_if(1);
180

            
181
2502
    emit.local_get(arr_local);
182
2502
    emit.local_get(idx_local);
183
2502
    emit.local_get(pool_local);
184
2502
    emit.local_get(str_off_local);
185
2502
    emit.i32_add();
186
2502
    emit.local_get(idx_local);
187
2502
    emit.i32_add();
188
2502
    emit.i32_load8_u(0);
189
2502
    emit.array_set(i8_array_idx);
190

            
191
2502
    emit.local_get(idx_local);
192
2502
    emit.i32_const(1);
193
2502
    emit.i32_add();
194
2502
    emit.local_set(idx_local);
195
2502
    emit.br(0);
196
2502
    emit.block_end()?; // loop
197
2502
    emit.block_end()?; // block
198

            
199
2502
    emit.local_get(arr_local);
200
2502
    Ok(WasmType::StringRef)
201
2502
}