submilli-engine 0.6.2

Compiles Submilli programs, a TypeScript subset, to WebAssembly and runs them under capability policy
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Runtime type descriptors: what every generic call passes per type
//! parameter, in its trailing `$types` environment.
//!
//! A descriptor is a `$TypeDescriptor` struct, not a closure: its `test` field
//! is the type's structural predicate (a `$closure_1` whose body is
//! [`body`]), beside the type's name and its JSON Schema when it has one
//! (`codegen::descriptor_schema`). Compiled generics read the predicate (`test_parameter`);
//! host generics read all of it (`stdlib::abi::read_type_argument`). Every
//! field is a function of the type, so a closed type's descriptor is built
//! once into a lazily initialised global and its identity holds for the
//! `ref.eq` tests of recursive validators.

use super::function_emitter::{FunctionEmitter, cast};
use super::{CodegenCtx, field_guards, symbol_table::SymbolTable};
use crate::{Ident, Span, Type, TypedAst, TypedExprKind};
use std::collections::BTreeSet;
use wasm_encoder::{
    BlockType, ConstExpr, Function, GlobalSection, GlobalType, HeapType, Instruction, RefType,
    ValType,
};

pub fn environment_type(
    symbols: &SymbolTable,
) -> Result<ValType, crate::compiler_error::CompilerFailure> {
    Ok(ValType::Ref(RefType {
        nullable: false,
        heap_type: HeapType::Concrete(
            symbols
                .intrinsic_type_indices()
                .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
                .object_fields,
        ),
    }))
}

pub fn parameters(ty: &Type) -> Vec<String> {
    let mut names = BTreeSet::new();
    collect_parameters(ty, &mut names);
    names.into_iter().collect()
}

fn collect_parameters(ty: &Type, names: &mut BTreeSet<String>) {
    match ty.lowered() {
        Type::TypeVar(name) | Type::GenericParam { name, .. } => {
            names.insert(name.clone());
        }
        Type::Array(element) => collect_parameters(element, names),
        Type::Tuple(crate::types::TupleType {
            elements: types, ..
        })
        | Type::Union(types) => {
            for ty in types {
                collect_parameters(ty, names);
            }
        }
        Type::ClassRef { args, .. }
        | Type::InterfaceRef { args, .. }
        | Type::AliasRef { args, .. } => {
            for ty in args {
                collect_parameters(ty, names);
            }
        }
        Type::Object { fields, index } => {
            if let Some(i) = index {
                collect_parameters(&i.value, names);
            }
            for field in fields.values() {
                collect_parameters(&field.ty, names);
            }
        }
        Type::Function { params, ret, .. } => {
            for ty in params {
                collect_parameters(ty, names);
            }
            collect_parameters(ret, names);
        }
        _ => {}
    }
}

pub fn bind(
    emitter: &mut FunctionEmitter,
    names: &[String],
    local: u32,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    for (slot, name) in names.iter().enumerate() {
        emitter
            .runtime_type_params
            .insert(name.clone(), (local, crate::codegen::wasm_u32(slot)?));
    }
    Ok(())
}

pub fn allocate(
    ta: &TypedAst,
    extra: &BTreeSet<Type>,
    symbols: &mut SymbolTable,
    next: &mut u32,
) -> Result<Vec<(Type, u32)>, crate::compiler_error::CompilerFailure> {
    let mut targets = BTreeSet::from([Type::Unknown]);
    targets.extend(extra.iter().cloned());
    let mut has_generic_calls = false;
    for index in ta.expr_ids().map_err(crate::codegen::arena_failure)? {
        match &ta
            .try_expr(index)
            .map_err(crate::codegen::arena_failure)?
            .kind
        {
            TypedExprKind::GenericCall { type_args, .. } => {
                has_generic_calls = true;
                targets.extend(type_args.iter().cloned());
            }
            // A runtime-generic host method (`Response#json<T>`); every
            // other generic method call carries no type arguments.
            TypedExprKind::GenericMethodCall { type_args, .. } if !type_args.is_empty() => {
                has_generic_calls = true;
                targets.extend(type_args.iter().cloned());
            }
            _ => {}
        }
    }
    for contexts in ta.runtime_class_contexts.values() {
        for context in contexts {
            targets.extend(context.args.iter().cloned());
        }
    }
    if !has_generic_calls
        && targets.len() == 1
        && ta.runtime_class_contexts.values().all(Vec::is_empty)
        && !ta
            .runtime_field_guards
            .values()
            .flatten()
            .any(|guard| !parameters(&guard.target).is_empty())
    {
        return Ok(Vec::new());
    }
    let mut descriptors = Vec::new();
    for ty in targets {
        if matches!(ty, Type::TypeVar(_) | Type::GenericParam { .. }) {
            continue;
        }
        symbols.type_descriptor_functions.insert(ty.clone(), *next);
        descriptors.push((ty, *next));
        crate::codegen::next_index(next)?;
    }
    Ok(descriptors)
}

/// Closed types' descriptors are singletons so recursive calls retain effective type identity.
pub fn allocate_globals(
    globals: &mut GlobalSection,
    symbols: &mut SymbolTable,
    next: &mut u32,
) -> Result<u32, crate::compiler_error::CompilerFailure> {
    let closed: Vec<_> = symbols
        .type_descriptor_functions
        .keys()
        .filter(|ty| parameters(ty).is_empty())
        .cloned()
        .collect();
    if closed.is_empty() {
        return Ok(0);
    }
    let descriptor = symbols
        .intrinsic_type_indices()
        .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
        .type_descriptor;
    for ty in &closed {
        symbols.type_descriptor_globals.insert(ty.clone(), *next);
        crate::codegen::next_index(next)?;
        globals.global(
            GlobalType {
                val_type: ValType::Ref(RefType {
                    nullable: true,
                    heap_type: HeapType::Concrete(descriptor),
                }),
                mutable: true,
                shared: false,
            },
            &ConstExpr::ref_null(HeapType::Concrete(descriptor)),
        );
    }
    crate::codegen::wasm_u32(closed.len())
}

pub fn environment(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    types: &[Type],
) -> Result<(), crate::compiler_error::CompilerFailure> {
    for ty in types {
        emit(emitter, ctx, ty)?;
    }
    emitter.instruction(Instruction::ArrayNewFixed {
        array_type_index: ctx
            .symbols
            .intrinsic_type_indices()
            .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
            .object_fields,
        array_size: crate::codegen::wasm_u32(types.len())?,
    });

    Ok(())
}

/// A schema call at a type the program names (`llm.call<Verdict>`)
/// passed the typechecker's schema gate, so codegen must have emitted a schema
/// for that type: the gate and `codegen::descriptor_schema` decide it
/// independently, and a disagreement would compile a call that always throws.
/// A type parameter forwards its caller's descriptor and is checked when the
/// call runs instead.
pub fn require_direct_schema(
    ctx: &CodegenCtx,
    mangled: &crate::MangledName,
    type_args: &[Type],
) -> Result<(), crate::compiler_error::CompilerFailure> {
    let Some(ty) = type_args.first() else {
        return Ok(());
    };
    if !crate::stdlib::checked_call::checked_call(mangled).is_some_and(|c| c.sends_schema())
        || !parameters(ty).is_empty()
        || ctx.symbols.descriptor_schemas.contains_key(ty)
    {
        return Ok(());
    }
    Err(crate::codegen::internal_failure(
        "a type the schema gate admitted has a descriptor schema",
    ))
}

/// A descriptor text from the string pool. Codegen interns the name and
/// schema of every allocated descriptor type before any function is emitted
/// ([`prepare_descriptor_data`]), so a miss is a compiler invariant failure, not a slow
/// path.
fn emit_text(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    text: &str,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    let pool_idx = ctx
        .strings
        .lookup_text(text)
        .ok_or_else(|| crate::codegen::internal_failure("descriptor text interned"))?;
    crate::codegen::function_emitter::emit_pooled_string(emitter, ctx, pool_idx)
}

pub fn capture(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    ty: &Type,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    let types: Vec<_> = parameters(ty).into_iter().map(Type::TypeVar).collect();
    environment(emitter, ctx, &types)?;

    Ok(())
}

/// A generic helper binds declaration parameters, rather than free variables in
/// this particular instantiation. Pair arguments in declaration order, then sort
/// by parameter name to match `parameters()` and the helper's binding slots.
pub fn validator_environment(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    key: &Type,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    if let Some((names, _)) = ctx.symbols.generic_runtime_validator(key)
        && let Type::AliasRef { args, .. } | Type::InterfaceRef { args, .. } = key.lowered()
    {
        if names.len() != args.len() {
            return Err(crate::codegen::internal_failure(
                "validator type argument count mismatch",
            ));
        }
        let mut pairs: Vec<_> = names.iter().zip(args).collect();
        pairs.sort_by_key(|(name, _)| *name);
        environment(
            emitter,
            ctx,
            &pairs
                .into_iter()
                .map(|(_, arg)| arg.clone())
                .collect::<Vec<_>>(),
        )?;
        return Ok(());
    }
    capture(emitter, ctx, key)?;

    Ok(())
}

fn emit(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    ty: &Type,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    if let Type::TypeVar(name) | Type::GenericParam { name, .. } = ty.lowered() {
        if let Some(&(local, slot)) = emitter.runtime_type_params.get(name) {
            emitter.instruction(Instruction::LocalGet(local));
            emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(
                ctx.symbols
                    .intrinsic_type_indices()
                    .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
                    .object_fields,
            )));
            emitter.instruction(Instruction::I32Const(slot as i32));
            emitter.instruction(Instruction::ArrayGet(
                ctx.symbols
                    .intrinsic_type_indices()
                    .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
                    .object_fields,
            ));
            return Ok(());
        }
        // Legacy erased entry points have no descriptor to forward.
        emit(emitter, ctx, &Type::Unknown)?;
        return Ok(());
    }
    if let Some(&global) = ctx.symbols.type_descriptor_globals.get(ty) {
        emitter.instruction(Instruction::GlobalGet(global));
        emitter.instruction(Instruction::RefIsNull);
        emitter.emit_if(BlockType::Empty);
        emit_new_descriptor(emitter, ctx, ty)?;
        emitter.instruction(Instruction::GlobalSet(global));
        emitter.emit_end();
        emitter.instruction(Instruction::GlobalGet(global));
        emitter.instruction(Instruction::RefAsNonNull);
        return Ok(());
    }
    emit_new_descriptor(emitter, ctx, ty)?;

    Ok(())
}

fn emit_new_descriptor(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    ty: &Type,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    let function = ctx
        .symbols
        .type_descriptor_functions
        .get(ty)
        .ok_or_else(|| crate::codegen::internal_failure("type descriptor allocated"))?;
    let intrinsics = ctx
        .symbols
        .intrinsic_type_indices()
        .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?;
    emitter.instruction(Instruction::GlobalGet(
        ctx.symbols
            .global_idx(&crate::mangle::prelude("opaque_vtable"))
            .ok_or_else(|| crate::codegen::internal_failure("opaque vtable imported"))?,
    ));
    // `test`: the predicate closure, capturing its free parameters' descriptors.
    emitter.instruction(Instruction::GlobalGet(
        ctx.symbols
            .closure_vtable_global_idx()
            .ok_or_else(|| crate::codegen::internal_failure("closure vtable"))?,
    ));
    emitter.instruction(Instruction::RefFunc(*function));
    capture(emitter, ctx, ty)?;
    emitter.instruction(Instruction::I64Const(0));
    emitter.instruction(Instruction::StructNew(
        ctx.symbols
            .closure_struct_type_idx(field_guards::signature())
            .ok_or_else(|| crate::codegen::internal_failure("predicate closure type"))?,
    ));
    emit_text(emitter, ctx, &ty.to_string())?;
    match ctx.symbols.descriptor_schemas.get(ty) {
        Some(schema) => emit_text(emitter, ctx, schema)?,
        None => {
            emitter.instruction(Instruction::RefNull(HeapType::Concrete(intrinsics.string)));
        }
    }
    emitter.instruction(Instruction::StructNew(intrinsics.type_descriptor));

    Ok(())
}

/// Compute the JSON Schema of every allocated descriptor type that has one
/// (`codegen::descriptor_schema`), and intern each descriptor's name and
/// schema so [`emit_text`] finds them. Run once [`allocate`] has fixed the
/// descriptor types and before any function is emitted.
pub fn prepare_descriptor_data(
    pool: &mut crate::codegen::string_pool::StringPool,
    ta: &TypedAst,
    symbols: &mut SymbolTable,
) {
    let types: Vec<Type> = symbols.type_descriptor_functions.keys().cloned().collect();
    for ty in types {
        pool.intern_text(&ty.to_string());
        if let Some(schema) = super::descriptor_schema::schema_of(ta, &ty) {
            pool.intern_text(&schema);
            symbols.descriptor_schemas.insert(ty, schema);
        }
    }
}

pub fn test_parameter(
    emitter: &mut FunctionEmitter,
    ctx: &CodegenCtx,
    name: &str,
    value: u32,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    if !emitter.runtime_type_params.contains_key(name) {
        emitter.instruction(Instruction::I32Const(1));
        return Ok(());
    }
    let closure_type = ctx
        .symbols
        .closure_struct_type_idx(field_guards::signature())
        .ok_or_else(|| crate::codegen::internal_failure("predicate closure type"))?;
    let descriptor = ctx
        .symbols
        .intrinsic_type_indices()
        .ok_or_else(|| crate::codegen::internal_failure("intrinsics"))?
        .type_descriptor;
    let local = emitter.add_anonymous_local(ValType::Ref(RefType {
        nullable: false,
        heap_type: HeapType::Concrete(closure_type),
    }))?;
    emit(emitter, ctx, &Type::TypeVar(name.into()))?;
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(descriptor)));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: descriptor,
        field_index: 1,
    });
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(
        closure_type,
    )));
    emitter.instruction(Instruction::LocalSet(local));
    emitter.instruction(Instruction::LocalGet(local));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: closure_type,
        field_index: 2,
    });
    emitter.instruction(Instruction::LocalGet(value));
    emitter.instruction(Instruction::LocalGet(local));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: closure_type,
        field_index: 1,
    });
    emitter.instruction(Instruction::CallRef(
        ctx.symbols
            .closure_func_type_idx(field_guards::signature())
            .ok_or_else(|| crate::codegen::internal_failure("descriptor signature"))?,
    ));
    cast::emit_cast_to(emitter, ctx, &Type::Boolean)?;

    Ok(())
}

pub fn body(
    ctx: &CodegenCtx,
    ty: &Type,
) -> Result<Function, crate::compiler_error::CompilerFailure> {
    let ident = |name: &str| Ident {
        name: name.into(),
        span: Span::at(ctx.file),
    };
    let params = [
        (
            ident("env"),
            ValType::Ref(RefType {
                nullable: false,
                heap_type: HeapType::ANY,
            }),
        ),
        (ident("value"), ctx.symbols.value_type(&Type::Unknown)?),
    ];
    let mut emitter = FunctionEmitter::new(ctx, &params)?;
    bind(&mut emitter, &parameters(ty), 0)?;
    ctx.checking_standalone(ty, || {
        super::cast_check::emit_structural_test(&mut emitter, ctx, 1, ty, ty)
    })?;
    cast::emit_box(&mut emitter, ctx, &Type::Boolean)?;
    emitter.build()
}