comptime VM: real lowering-time Context — allocating + List-building type-fns run on the VM (issue 0141)

The VM can now evaluate a comptime type-fn that allocates at lowering time (the
0141 family) — the legacy interp cannot. Four changes:

- runComptimeTypeFunc (lower/comptime.zig): force the CAllocator->Allocator thunks
  to exist (getOrCreateThunks, idempotent, guarded) BEFORE eval. A type-fn const
  runs at scanDecls (Pass 1), before Pass 1c builds the default-context global +
  thunks, so the comptime allocator was otherwise null.
- materializeDefaultContext: build a REAL context at lowering time when the global
  is absent — find the two thunks by name and lay their func-refs into the inline
  Allocator value at the head of Context, so context.allocator.alloc_bytes
  dispatches call_indirect -> thunk -> native VM malloc.
- aggType: deref a pointer base_type (the List write path emits struct_gep with
  base_type = *Struct; fieldOffset panicked on the pointer — now derefs, no panic).
- subslice: handle a [*]T many-pointer / *T base (a List's items field — the base
  IS the data pointer).

Verified end-to-end (manual probe): a compiler-API type-fn building its []Member in
a List(Member) runs HANDLED on the VM and mints (green=7) — the 0141 List-growth
pattern. Can't be a corpus test yet (gate-OFF/legacy can't allocate at lowering
time — the dual-path bind), so locked in via VM unit tests (many-pointer subslice;
struct_gep with a pointer base_type). 697/0 both gates + all unit tests.
This commit is contained in:
agra
2026-06-18 15:04:55 +03:00
parent 3c0e0852a8
commit eb68d9ed94
5 changed files with 165 additions and 17 deletions

View File

@@ -346,6 +346,29 @@ when reached (sentinels or accessor fns; see the design doc Risks).
`List` growth; orthogonal, see `current/CHECKPOINT-METATYPE.md`.)
## Log
- **Phase 3 P3.4 step 6 (VM plan) — REAL lowering-time Context: allocating + List-building type-fns now run HANDLED on the VM (2026-06-18).**
The VM can now evaluate a comptime type-fn that ALLOCATES at lowering time (the 0141 family) —
the legacy interp cannot. Four changes: (1) `runComptimeTypeFunc` (lower/comptime.zig) FORCES the
CAllocator→Allocator thunks to exist (`getOrCreateThunks`, idempotent, guarded by Allocator/
CAllocator registered) BEFORE eval — a type-fn const runs at scanDecls (Pass 1), before Pass 1c
builds the default-context global + thunks, so the comptime allocator was otherwise null;
(2) `materializeDefaultContext` builds a REAL context at lowering time when the global is absent —
finds the two thunks by name (`findFuncByName`) and lays their func-refs into the inline
`Allocator` value `{ctx=null, alloc_fn@+ptr, dealloc_fn@+2*ptr}` at the head of `Context`, so
`context.allocator.alloc_bytes` dispatches `call_indirect` → thunk → native VM `malloc`;
(3) `aggType` now DEREFS a pointer `base_type` (the List write path emits `struct_gep` with
`base_type = *Struct` — `fieldOffset` panicked on the pointer; now derefs to the pointee, no
panic); (4) `subslice` handles a `[*]T` many-pointer / `*T` base (a List's `items` field — the
base IS the data pointer). **Verified end-to-end (manual probe):** a compiler-API type-fn that
builds its `[]Member` in a `List(Member)` (`.append` ×3, then `register_type(handle, kind,
vs.items[0..vs.len])`) runs **HANDLED on the VM** and mints correctly (`green=7`) — the exact
0141 List-growth pattern, on the VM. **Can't be a corpus test yet** (gate-OFF/legacy still can't
allocate at lowering time — the dual-path bind), so locked in via VM unit tests instead
(many-pointer subslice; `struct_gep` with a pointer `base_type`). **697/0 BOTH gates + all unit
tests, EXIT=0.** On `reify`. **Remaining for the original 0141 repro (uses metatype `define`/
`make_enum` → `call_builtin` → legacy fallback → legacy fails):** re-express the metatype over the
compiler-API so the whole type-fn runs on the VM (no `call_builtin`). THEN the repro works on the
VM — and the dual-path bind resolves only at the VM-default-flip + legacy-deletion end-state.
- **Phase 3 P3.4 — investigation: the "real lowering-time Context" is BLOCKED by issue 0141 (2026-06-18).**
Probed whether the VM needs a REAL lowering-time `Context` (CAllocator thunk func-refs) for
allocating type-fns. **Finding: lowering-time comptime ALLOCATION fails in the LEGACY interp