mem: implicit-context foundation + many compiler fixes
The session-long set of changes that lay the groundwork for the
Jai-literal implicit-Context-parameter refactor. Lots of accumulated
work; the new arrival is the implicit-ctx foundation (steps 1+2 of
the plan in current/CHECKPOINT-MEM.md):
Step 1 — `CAllocator :: struct {}` stateless allocator in
library/modules/allocators.sx, delegating directly to
libc_malloc/libc_free. `ConstantValue` in src/ir/inst.zig gains a
`func_ref: FuncId` leaf so nested aggregates can carry function
pointers (the inline Allocator value's fn-ptr fields). Switch
sites updated in emit_llvm.zig, print.zig, interp.zig.
Step 2 — `emitDefaultContextGlobal` in src/ir/lower.zig synthesises
a static `__sx_default_context` global with a nested-aggregate
init_val pointing at the CAllocator → Allocator thunks. The
second-pass `initVtableGlobals` in emit_llvm.zig is generalised
to handle `.aggregate` init_vals (re-emits after func_map is
populated so func_ref leaves resolve to real symbols).
Also folded in from earlier work this session:
- Phase 1.1: `xx value` heap-copy in `buildProtocolValue` routes
through `context.allocator` via the new `allocViaContext` helper.
- interp.zig: `marshalForeignArg` double-offset bug fixed —
`heapSlice` already adds `hp.offset` to the slice ptr, so the
extra `+ hp.offset` was scribbling memcpy/memset into adjacent
heap state, corrupting `heap.items[0]`. Symptom: `build_format`
at comptime produced zero bytes, all `print` calls failed.
- Lazy lowering: `lazyLowerFunction` now declares foreign-body
functions as extern stubs in the local (comptime) module so
cross-module foreign calls resolve.
- Allocator API: all stdlib allocators on one-line `init() -> *T`
(CAllocator/GPA: libc-backed; Arena/TrackingAllocator: parent-
backed; BufAlloc: embeds state at head of user buffer).
- issues 0038 (transitive #import), 0039 (chess + stdlib migration
fallout), 0040 (generic struct method dot-dispatch), 0041
(pointer types as type-arg), 0042 (alias name resolution) — all
fixed; regression tests in examples/.
- Diagnostic: `emitError` now embeds the lowering's
`current_source_file` and enclosing function in the literal
message; SX_TRACE_UNRESOLVED=1 dumps a Zig stack trace at the
emit site so misattributed spans can't hide where the failure
is.
- tools/verify-step.sh (all-platforms gate) and tools/scratch.sh
(interp/codegen parity tester) added.
Test suite: 152 example tests pass; chess builds + screenshots on
macOS / iOS sim / Android.
This commit is contained in:
@@ -1141,6 +1141,12 @@ END;
|
||||
print("sizeof-f64: {}\n", size_of(f64));
|
||||
print("sizeof-struct: {}\n", size_of(Point));
|
||||
|
||||
// align_of
|
||||
print("alignof-u8: {}\n", align_of(u8));
|
||||
print("alignof-s32: {}\n", align_of(s32));
|
||||
print("alignof-s64: {}\n", align_of(s64));
|
||||
print("alignof-struct: {}\n", align_of(Point));
|
||||
|
||||
// type_of + category matching
|
||||
tv := 42;
|
||||
ttype := type_of(tv);
|
||||
@@ -1667,29 +1673,30 @@ END;
|
||||
|
||||
// ── GPA ─────────────────────────────────────────────────
|
||||
{
|
||||
gpa_state : GPA = .{ alloc_count = 0 };
|
||||
gpa := gpa_state.create();
|
||||
p1 := gpa.alloc(64);
|
||||
p2 := gpa.alloc(128);
|
||||
print("gpa allocs: {}\n", gpa_state.alloc_count); // gpa allocs: 2
|
||||
gpa.dealloc(p1);
|
||||
gpa.dealloc(p2);
|
||||
print("gpa final: {}\n", gpa_state.alloc_count); // gpa final: 0
|
||||
gpa := GPA.init();
|
||||
a : Allocator = xx gpa;
|
||||
p1 := a.alloc(64);
|
||||
p2 := a.alloc(128);
|
||||
print("gpa allocs: {}\n", gpa.alloc_count); // gpa allocs: 2
|
||||
a.dealloc(p1);
|
||||
a.dealloc(p2);
|
||||
print("gpa final: {}\n", gpa.alloc_count); // gpa final: 0
|
||||
}
|
||||
|
||||
// ── Arena backed by GPA (multi-chunk) ───────────────────
|
||||
{
|
||||
gpa_state3 : GPA = .{ alloc_count = 0 };
|
||||
gpa3 := gpa_state3.create();
|
||||
arena_state : Arena = ---;
|
||||
arena := arena_state.create(gpa3, 32);
|
||||
gpa3 := GPA.init();
|
||||
arena := Arena.init(xx gpa3, 32);
|
||||
a : Allocator = xx arena;
|
||||
// First chunk fits 80 usable bytes
|
||||
a1 := arena.alloc(40);
|
||||
a2 := arena.alloc(40);
|
||||
print("arena chunks: {}\n", gpa_state3.alloc_count); // arena chunks: 1
|
||||
a1 := a.alloc(40);
|
||||
a2 := a.alloc(40);
|
||||
// Counts: Arena struct itself + first chunk = 2 (Arena.init
|
||||
// allocates its own state through the parent allocator).
|
||||
print("arena chunks: {}\n", gpa3.alloc_count); // arena chunks: 2
|
||||
// Overflow → new chunk
|
||||
a3 := arena.alloc(16);
|
||||
print("arena overflow: {}\n", gpa_state3.alloc_count); // arena overflow: 2
|
||||
a3 := a.alloc(16);
|
||||
print("arena overflow: {}\n", gpa3.alloc_count); // arena overflow: 3
|
||||
// Verify memory works across chunks
|
||||
p1 : [*]u8 = xx a1;
|
||||
p3 : [*]u8 = xx a3;
|
||||
@@ -1698,27 +1705,27 @@ END;
|
||||
print("arena a1: {}\n", p1[0]); // arena a1: 42
|
||||
print("arena a3: {}\n", p3[0]); // arena a3: 99
|
||||
// Reset retains newest chunk
|
||||
arena_state.reset();
|
||||
print("arena reset idx: {}\n", arena_state.end_index); // arena reset idx: 0
|
||||
print("arena reset gpa: {}\n", gpa_state3.alloc_count);// arena reset gpa: 1
|
||||
// Deinit frees all
|
||||
arena_state.deinit();
|
||||
print("arena deinit: {}\n", gpa_state3.alloc_count); // arena deinit: 0
|
||||
arena.reset();
|
||||
print("arena reset idx: {}\n", arena.end_index); // arena reset idx: 0
|
||||
print("arena reset gpa: {}\n", gpa3.alloc_count); // arena reset gpa: 2
|
||||
// Deinit frees all chunks + the Arena state itself
|
||||
arena.deinit();
|
||||
print("arena deinit: {}\n", gpa3.alloc_count); // arena deinit: 0
|
||||
}
|
||||
|
||||
// ── BufAlloc from stack array ───────────────────────────
|
||||
{
|
||||
stack_buf : [128]u8 = ---;
|
||||
buf_state : BufAlloc = ---;
|
||||
bufalloc := buf_state.create(@stack_buf[0], 128);
|
||||
b1 := bufalloc.alloc(24);
|
||||
b2 := bufalloc.alloc(24);
|
||||
print("buf pos: {}\n", buf_state.pos); // buf pos: 48
|
||||
b3 := bufalloc.alloc(200);
|
||||
buf := BufAlloc.init(@stack_buf[0], 128);
|
||||
a : Allocator = xx buf;
|
||||
b1 := a.alloc(24);
|
||||
b2 := a.alloc(24);
|
||||
print("buf pos: {}\n", buf.pos); // buf pos: 48
|
||||
b3 := a.alloc(200);
|
||||
b3_i : s64 = xx b3;
|
||||
print("buf overflow: {}\n", b3_i); // buf overflow: 0
|
||||
buf_state.reset();
|
||||
print("buf reset: {}\n", buf_state.pos); // buf reset: 0
|
||||
buf.reset();
|
||||
print("buf reset: {}\n", buf.pos); // buf reset: 0
|
||||
}
|
||||
|
||||
{
|
||||
@@ -2446,8 +2453,8 @@ END;
|
||||
step3 :: (a: Allocator) -> *void { a.alloc(8); }
|
||||
step2 :: (a: Allocator) -> *void { step3(a); }
|
||||
step1 :: (a: Allocator) -> *void { step2(a); }
|
||||
gpa_e6 : GPA = .{ alloc_count = 0 };
|
||||
a_e6 : Allocator = xx @gpa_e6;
|
||||
gpa_e6 := GPA.init();
|
||||
a_e6 : Allocator = xx gpa_e6;
|
||||
ptr_e6 := step1(a_e6);
|
||||
print("closure-chain-call: {}\n", ptr_e6 != null);
|
||||
a_e6.dealloc(ptr_e6);
|
||||
@@ -2498,11 +2505,9 @@ END;
|
||||
print("closure-slice: {} {} {}\n", f_a7(0), f_a7(1), f_a7(2));
|
||||
|
||||
// C5.L1: arena bulk free (closures allocated on arena, freed in bulk)
|
||||
gpa_l1 : GPA = .{ alloc_count = 0 };
|
||||
a_l1 : Allocator = xx @gpa_l1;
|
||||
arena_l1 : Arena = ---;
|
||||
arena_alloc := arena_l1.create(a_l1, 4096);
|
||||
push Context.{ allocator = arena_alloc } {
|
||||
gpa_l1 := GPA.init();
|
||||
arena_l1 := Arena.init(xx gpa_l1, 4096);
|
||||
push Context.{ allocator = xx arena_l1 } {
|
||||
n_l1 : s32 = 5;
|
||||
f_l1 := closure((x: s32) -> s32 => x + n_l1);
|
||||
print("closure-arena: {}\n", f_l1(10));
|
||||
@@ -2510,8 +2515,8 @@ END;
|
||||
arena_l1.deinit();
|
||||
|
||||
// C5.L2: GPA manual free (verify env alloc/dealloc)
|
||||
gpa_l2 : GPA = .{ alloc_count = 0 };
|
||||
a_l2 : Allocator = xx @gpa_l2;
|
||||
gpa_l2 := GPA.init();
|
||||
a_l2 : Allocator = xx gpa_l2;
|
||||
n_l2 : s32 = 7;
|
||||
result_l2 : s32 = 0;
|
||||
push Context.{ allocator = a_l2 } {
|
||||
@@ -2660,8 +2665,8 @@ END;
|
||||
print("closure-neg: {}\n", neg_fn(val_e16));
|
||||
|
||||
// C5.E17: closure with protocol value capture (#inline protocol)
|
||||
gpa_e17 : GPA = .{ alloc_count = 0 };
|
||||
a_e17 : Allocator = xx @gpa_e17;
|
||||
gpa_e17 := GPA.init();
|
||||
a_e17 : Allocator = xx gpa_e17;
|
||||
alloc_fn := closure((size: s64) -> *void => a_e17.alloc(size));
|
||||
ptr_e17 := alloc_fn(32);
|
||||
print("closure-proto-cap: {}\n", ptr_e17 != null);
|
||||
|
||||
Reference in New Issue
Block a user