comptime VM: lowering-time default context (P3.4 step 1)

materializeDefaultContext now falls back to a zeroed Context (found by name) when
the __sx_default_context global is absent — i.e. at lowering time, where the global
isn't emitted yet. A type-fn that never touches the allocator runs past context
setup; one that allocates reads a null alloc_fn (zeroed) and call_indirect on the
null func-ref bails to legacy (a real lowering-time context with the CAllocator
thunk func-refs is a follow-up).

Measurement (SX_COMPTIME_FLAT_TRACE): the bail moved deeper — make_enum now bails
at const_type (the Type-literal op, unported); register_type type-fns bail at the
welded write call. No table mutation before either bail (write fns bail before
minting), so parity holds: both gates 697/0, no crashes.

Next: model the const_type op + the Type-return bridge + the VM-native write side,
which together let a type-fn run end-to-end on the VM.
This commit is contained in:
agra
2026-06-18 12:12:10 +03:00
parent 9d041b5136
commit 6473a4e227
2 changed files with 28 additions and 1 deletions

View File

@@ -334,7 +334,19 @@ pub const Vm = struct {
if (g.init_val) |iv| try self.layoutConst(table, iv, g.ty, addr);
return addr;
}
return self.failMsg("comptime VM: no __sx_default_context global to materialize the implicit context");
// No `__sx_default_context` global yet — this is the LOWERING-time path
// (the global is emitted later, at codegen). Materialize a ZEROED `Context`
// of the right size instead: a type-fn that never touches the allocator
// ignores it; one that DOES allocate reads a null `alloc_fn` (zeroed) and
// `call_indirect` on the null func-ref bails → legacy fallback (which has a
// real default context). A real lowering-time context (with the CAllocator
// thunk func-refs, so allocating type-fns also run on the VM) is a follow-up.
// `internString` of an existing name is idempotent (pool-only, no layout
// change) — the same `@constCast` the reader handlers use on the table.
const ctx_name = @constCast(table).internString("Context");
const ctx_ty = table.findByName(ctx_name) orelse
return self.failMsg("comptime VM: no Context type to materialize the implicit context");
return self.machine.allocBytes(table.typeSizeBytes(ctx_ty), table.typeAlignBytes(ctx_ty)); // zeroed
}
/// Lay a static `ConstantValue` of type `ty` into flat memory at `addr` (the