formawasm 0.0.1-beta

Backend that compiles a typed FormaLang IR module into a WebAssembly component.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Lowering of [`IrExpr::FunctionCall`] (direct calls) and
//! [`IrExpr::MethodCall`] static + virtual dispatch.
//!
//! Static dispatch resolves to a direct `call <wasm_index>`. Virtual
//! dispatch loads a funcref-table slot index from the receiver's
//! per-trait vtable in linear memory, then `call_indirect`s against
//! the module's method funcref table. Indirect closure calls live
//! alongside in [`super::call::lower_call_closure`].

use formalang::ir::{DispatchKind, ImplTarget, IrExpr, ResolvedType};
use wasm_encoder::{InstructionSink, MemArg};

use super::{LowerContext, LowerError, lower_expr};
use crate::layout::VTABLE_SLOT_SIZE;
use crate::module::MEMORY_INDEX;
use crate::module_lowering::impl_target_key;
use crate::types::{CLOSURE_ENV_OFFSET, CLOSURE_FUNCREF_OFFSET};

/// Lower an [`IrExpr::FunctionCall`] onto `sink`.
///
/// Each argument is lowered in declaration order, then a
/// `call <wasm_index>` instruction is emitted. The wasm index comes
/// from the [`super::FunctionMap`] in `ctx`.
pub fn lower_function_call(
    expr: &IrExpr,
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
) -> Result<(), LowerError> {
    let IrExpr::FunctionCall {
        path,
        function_id,
        args,
        ..
    } = expr
    else {
        return Err(LowerError::NotYetImplemented {
            what: "lower_function_call called with non-FunctionCall expression".to_owned(),
        });
    };

    // Frontend passes (DeadCodeElimination, MonomorphisePass) can
    // renumber `module.functions` and leave the IR's `function_id`
    // pointing at a stale slot — *and* overloads compound the
    // problem because every overload shares the same source name.
    // When a module is in context, resolve by
    // *(name, argument-label set)*: a candidate is the function
    // whose name matches the path's last segment AND whose parameter
    // names cover every labelled argument the call passes. Validate
    // `function_id` against that filter first; fall back to a search
    // when it disagrees. Without module context (legacy hand-built
    // tests), trust `function_id` as authoritative.
    let module = ctx.module().ok();
    let id = if let Some(m) = module {
        let path_last = path.last().map(String::as_str);
        let arg_labels: Vec<&str> = args
            .iter()
            .filter_map(|(label, _)| label.as_deref())
            .collect();
        let matches_signature = |f: &formalang::ir::IrFunction| -> bool {
            Some(f.name.as_str()) == path_last
                && f.params.len() == args.len()
                && arg_labels
                    .iter()
                    .all(|label| f.params.iter().any(|p| p.name == *label))
        };
        let id_valid = function_id.and_then(|id| {
            let f = m.functions.get(id.0 as usize)?;
            if matches_signature(f) { Some(id) } else { None }
        });
        let resolved_id = id_valid.or_else(|| {
            m.functions.iter().enumerate().find_map(|(i, f)| {
                if matches_signature(f) {
                    u32::try_from(i).ok().map(formalang::ir::FunctionId)
                } else {
                    None
                }
            })
        });
        resolved_id
            .or(*function_id)
            .ok_or_else(|| LowerError::UnresolvedFunctionCall { path: path.clone() })?
    } else {
        function_id.ok_or_else(|| LowerError::UnresolvedFunctionCall { path: path.clone() })?
    };
    let wasm_idx = ctx
        .functions
        .get(id)
        .ok_or(LowerError::UnknownFunction(id))?;

    // Look up the callee in the IR module so each argument can coerce
    // to its parameter's declared type (Some-wrap widens a plain T
    // into an Optional<T> param at the call site).
    let callee = module.and_then(|m| m.functions.get(id.0 as usize));
    for (param_name, arg) in args {
        let target = callee.and_then(|f| {
            param_name.as_ref().and_then(|n| {
                f.params
                    .iter()
                    .find(|p| p.name == *n)
                    .and_then(|p| p.ty.as_ref())
            })
        });
        if let Some(t) = target {
            super::optional::lower_coerced(arg, t, sink, ctx)?;
        } else {
            lower_expr(arg, sink, ctx)?;
        }
    }
    sink.call(wasm_idx);
    Ok(())
}

/// Lower an [`IrExpr::CallClosure`] onto `sink`.
///
/// The `closure` sub-expression evaluates to a base pointer for an
/// 8-byte `(funcref_idx: i32, env_ptr: i32)` value built by
/// [`super::lower_closure_ref`]. The lowering:
///
/// 1. Park the closure value's base pointer in a fresh i32 scratch
///    local so we can read from it twice.
/// 2. Push `env_ptr` (loaded from offset 4) as the first argument —
///    the lifted top-level function takes the env struct in slot 0.
/// 3. Lower each explicit argument in declaration order.
/// 4. Push `funcref_idx` (loaded from offset 0) as the table index.
/// 5. Emit `call_indirect` against the funcref table, with a wasm
///    type signature derived from the closure's `ResolvedType::Closure`.
///
/// The funcref table and per-closure type signatures are wired by
/// [`crate::module_lowering`] before any function bodies are
/// lowered; this helper looks up the matching type index through
/// [`LowerContext::closure_type_index`].
pub fn lower_call_closure(
    expr: &IrExpr,
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
) -> Result<(), LowerError> {
    use formalang::ir::ResolvedType;
    use wasm_encoder::{MemArg, ValType};

    let IrExpr::CallClosure { closure, args, .. } = expr else {
        return Err(LowerError::NotYetImplemented {
            what: "lower_call_closure called with non-CallClosure expression".to_owned(),
        });
    };

    let closure_ty = closure.ty().clone();
    let ResolvedType::Closure { .. } = &closure_ty else {
        return Err(LowerError::NotYetImplemented {
            what: format!("CallClosure on non-closure-typed value {closure_ty:?}"),
        });
    };

    let table_idx = ctx.closure_table_index()?;
    let type_idx = ctx.closure_type_index(&closure_ty)?;

    let base_local = ctx.next_scratch_local(ValType::I32)?;
    lower_expr(closure, sink, ctx)?;
    sink.local_set(base_local);

    // env_ptr arg first (the lifted function's first parameter).
    sink.local_get(base_local);
    sink.i32_load(MemArg {
        offset: u64::from(CLOSURE_ENV_OFFSET),
        align: 2, // log2(4)
        memory_index: MEMORY_INDEX,
    });

    for (_, arg) in args {
        lower_expr(arg, sink, ctx)?;
    }

    // Funcref index for call_indirect.
    sink.local_get(base_local);
    sink.i32_load(MemArg {
        offset: u64::from(CLOSURE_FUNCREF_OFFSET),
        align: 2,
        memory_index: MEMORY_INDEX,
    });
    sink.call_indirect(table_idx, type_idx);
    Ok(())
}

/// Lower an [`IrExpr::MethodCall`] onto `sink`.
///
/// Static dispatch pushes the receiver pointer (implicit first
/// parameter), then explicit args in declaration order, then emits a
/// single `call <wasm_index>` resolved through the [`super::MethodMap`]
/// in `ctx`.
///
/// Virtual dispatch resolves the receiver's concrete type at compile
/// time (`Struct` / `Enum`), looks up the matching `(trait_id, target)`
/// vtable's absolute byte offset, loads the funcref-table slot at
/// `vtable_base + method_idx * VTABLE_SLOT_SIZE`, pushes the receiver
/// alongside its explicit args (with Optional coercion against the
/// trait method's declared parameter types), then emits `call_indirect`
/// against the module's method funcref table. The trait method's wasm
/// signature was pre-registered in the type section by the module-
/// level pass.
pub fn lower_method_call(
    expr: &IrExpr,
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
) -> Result<(), LowerError> {
    let IrExpr::MethodCall {
        receiver,
        method_idx,
        args,
        dispatch,
        ..
    } = expr
    else {
        return Err(LowerError::NotYetImplemented {
            what: "lower_method_call called with non-MethodCall expression".to_owned(),
        });
    };

    match dispatch {
        DispatchKind::Static { impl_id } => {
            lower_static_method_call(*impl_id, *method_idx, receiver, args, sink, ctx)
        }
        DispatchKind::Virtual {
            trait_id,
            method_name: _,
        } => lower_virtual_method_call(*trait_id, *method_idx, receiver, args, sink, ctx),
    }
}

fn lower_static_method_call(
    impl_id: formalang::ir::ImplId,
    method_idx: formalang::ir::MethodIdx,
    receiver: &IrExpr,
    args: &[(Option<String>, IrExpr)],
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
) -> Result<(), LowerError> {
    let methods = ctx
        .methods
        .ok_or(LowerError::MissingContext { what: "methods" })?;
    let wasm_idx = methods
        .get((impl_id, method_idx))
        .ok_or(LowerError::UnknownMethod {
            impl_id,
            method_idx,
        })?;

    lower_expr(receiver, sink, ctx)?;
    // Look up the method's signature in the impl block so each argument
    // can coerce to its parameter's declared type.
    let method_sig = ctx
        .module()
        .ok()
        .and_then(|m| m.impls.get(impl_id.0 as usize))
        .and_then(|i| i.functions.get(method_idx.0 as usize));
    for (param_name, arg) in args {
        let target = method_sig.and_then(|sig| {
            param_name.as_ref().and_then(|n| {
                sig.params
                    .iter()
                    .find(|p| p.name == *n)
                    .and_then(|p| p.ty.as_ref())
            })
        });
        if let Some(t) = target {
            super::optional::lower_coerced(arg, t, sink, ctx)?;
        } else {
            lower_expr(arg, sink, ctx)?;
        }
    }
    sink.call(wasm_idx);
    Ok(())
}

/// Dispatch a method call where the receiver carries a trait
/// type — its concrete impl was erased at the assignment site.
/// The receiver evaluates to a pointer at an 8-byte fat-pointer
/// cell `(vtable_offset, data_ptr)` materialised by
/// `super::optional::materialize_trait_fat_pointer`. We load
/// both fields, push `data_ptr` as the call's first arg, lower
/// the explicit args, then load the funcref at
/// `vtable_offset + method_idx * VTABLE_SLOT_SIZE` and
/// `call_indirect`.
fn lower_trait_typed_dispatch(
    method_idx: formalang::ir::MethodIdx,
    receiver: &IrExpr,
    args: &[(Option<String>, IrExpr)],
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
    trait_id: formalang::ir::TraitId,
) -> Result<(), LowerError> {
    use crate::layout::{POINTER_SIZE, VTABLE_SLOT_SIZE};
    use crate::module::MEMORY_INDEX;
    use wasm_encoder::{MemArg, ValType};

    let table_idx = ctx.method_table_index()?;
    let type_idx = ctx.virtual_call_type_index(trait_id, method_idx)?;

    // Park the cell pointer in a scratch local so we can read
    // both fields and re-push the data pointer for the call.
    let cell_scratch = ctx.next_scratch_local(ValType::I32)?;
    lower_expr(receiver, sink, ctx)?;
    sink.local_set(cell_scratch);

    let mem_arg = |off: u64| MemArg {
        offset: off,
        align: 2,
        memory_index: MEMORY_INDEX,
    };

    // Push data_ptr (the actual struct/enum receiver).
    sink.local_get(cell_scratch);
    sink.i32_load(mem_arg(u64::from(POINTER_SIZE)));

    // Lower explicit args, coercing against the trait method's
    // declared param types (mirrors the static path).
    let trait_method_sig = ctx
        .module()
        .ok()
        .and_then(|m| m.traits.get(trait_id.0 as usize))
        .and_then(|t| t.methods.get(method_idx.0 as usize));
    for (param_name, arg) in args {
        let target_ty = trait_method_sig.and_then(|sig| {
            param_name.as_ref().and_then(|n| {
                sig.params
                    .iter()
                    .find(|p| p.name == *n)
                    .and_then(|p| p.ty.as_ref())
            })
        });
        if let Some(t) = target_ty {
            super::optional::lower_coerced(arg, t, sink, ctx)?;
        } else {
            lower_expr(arg, sink, ctx)?;
        }
    }

    // Load funcref at vtable_offset + method_idx * VTABLE_SLOT_SIZE.
    sink.local_get(cell_scratch);
    sink.i32_load(mem_arg(0)); // vtable_offset
    let method_byte_off = u64::from(method_idx.0)
        .checked_mul(u64::from(VTABLE_SLOT_SIZE))
        .ok_or_else(|| LowerError::NotYetImplemented {
            what: "vtable slot byte offset overflow".to_owned(),
        })?;
    let method_byte_off_signed = i32::try_from(method_byte_off).unwrap_or(i32::MAX);
    sink.i32_const(method_byte_off_signed);
    sink.i32_add();
    sink.i32_load(MemArg {
        offset: 0,
        align: 2,
        memory_index: MEMORY_INDEX,
    });
    sink.call_indirect(table_idx, type_idx);
    Ok(())
}

fn lower_virtual_method_call(
    trait_id: formalang::ir::TraitId,
    method_idx: formalang::ir::MethodIdx,
    receiver: &IrExpr,
    args: &[(Option<String>, IrExpr)],
    sink: &mut InstructionSink<'_>,
    ctx: &LowerContext<'_>,
) -> Result<(), LowerError> {
    // A trait-typed receiver carries an 8-byte fat-pointer cell
    // `(vtable_offset, data_ptr)`. Detect it and dispatch via a
    // dynamic vtable load: load the cell pointer's first i32 to
    // get vtable_offset, the second to get data_ptr, then walk
    // method_idx slots into the vtable. This is the only path
    // where vtable_base isn't statically known at compile time.
    if let ResolvedType::Trait(_) = receiver.ty() {
        return lower_trait_typed_dispatch(method_idx, receiver, args, sink, ctx, trait_id);
    }
    let target = if let Some(sid) = crate::compound::struct_id_of(receiver.ty()) {
        // Covers both `ResolvedType::Struct(sid)` and the
        // `Generic { base: Struct(sid), .. }` form, so a monomorphic
        // instantiation dispatches through the same vtable as its
        // declaration.
        ImplTarget::Struct(sid)
    } else if let Some(eid) = crate::compound::enum_id_of(receiver.ty()) {
        ImplTarget::Enum(eid)
    } else {
        return Err(LowerError::UnsupportedVirtualReceiver {
            ty: receiver.ty().clone(),
        });
    };
    let table_idx = ctx.method_table_index()?;
    let type_idx = ctx.virtual_call_type_index(trait_id, method_idx)?;
    let vtable_base = ctx.vtable_offset(trait_id, impl_target_key(target))?;

    // Slot byte offset = vtable_base + method_idx * VTABLE_SLOT_SIZE.
    // Computed at compile time so the runtime cost is a single
    // i32.const + i32.load before the call_indirect.
    let slot_offset = u64::from(vtable_base)
        .checked_add(
            u64::from(method_idx.0)
                .checked_mul(u64::from(VTABLE_SLOT_SIZE))
                .ok_or_else(|| LowerError::NotYetImplemented {
                    what: "vtable slot offset overflow".to_owned(),
                })?,
        )
        .ok_or_else(|| LowerError::NotYetImplemented {
            what: "vtable slot offset overflow".to_owned(),
        })?;

    // Push the receiver pointer (implicit first arg of every trait
    // method) and the explicit args, coercing each against the
    // trait method's declared parameter type so Optional widening
    // matches static dispatch.
    lower_expr(receiver, sink, ctx)?;
    let trait_method_sig = ctx
        .module()
        .ok()
        .and_then(|m| m.traits.get(trait_id.0 as usize))
        .and_then(|t| t.methods.get(method_idx.0 as usize));
    for (param_name, arg) in args {
        let target_ty = trait_method_sig.and_then(|sig| {
            param_name.as_ref().and_then(|n| {
                sig.params
                    .iter()
                    .find(|p| p.name == *n)
                    .and_then(|p| p.ty.as_ref())
            })
        });
        if let Some(t) = target_ty {
            super::optional::lower_coerced(arg, t, sink, ctx)?;
        } else {
            lower_expr(arg, sink, ctx)?;
        }
    }

    // Load the funcref-table slot from the vtable cell, then
    // call_indirect.
    sink.i32_const(0)
        .i32_load(MemArg {
            offset: slot_offset,
            align: 2, // log2(4)
            memory_index: MEMORY_INDEX,
        })
        .call_indirect(table_idx, type_idx);
    Ok(())
}