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:
agra
2026-05-24 22:59:20 +03:00
parent 0ba41b2980
commit 29784c22a8
63 changed files with 3448 additions and 1207 deletions

View File

@@ -3,6 +3,7 @@
@OS = internal global i64 0
@ARCH = internal global i64 0
@POINTER_SIZE = internal global i64 8
@__sx_default_context = internal global { { ptr, ptr, ptr }, ptr } { { ptr, ptr, ptr } { ptr null, ptr @__thunk_CAllocator_Allocator_alloc, ptr @__thunk_CAllocator_Allocator_dealloc }, ptr null }
@OBJC_SELECTOR_REFERENCES_init = internal global ptr null
@OBJC_SELECTOR_REFERENCES_release = internal global ptr null
@str = private unnamed_addr constant [4 x i8] c"ok\0A\00", align 1
@@ -16,14 +17,38 @@ declare void @out(ptr) #0
declare ptr @malloc(i64)
declare void @free(ptr)
declare ptr @memcpy(ptr, ptr, i64)
declare ptr @memset(ptr, i32, i64)
declare void @free(ptr)
; Function Attrs: nounwind
define internal ptr @CAllocator.alloc(ptr %0, i64 %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca i64, align 8
store i64 %1, ptr %allocaN, align 8
%load = load i64, ptr %allocaN, align 8
%call = call ptr @malloc(i64 %load)
ret ptr %call
}
; Function Attrs: nounwind
declare void @GPA.create(ptr sret({ ptr, ptr, ptr }), ptr) #0
define internal void @CAllocator.dealloc(ptr %0, ptr %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca ptr, align 8
store ptr %1, ptr %allocaN, align 8
%load = load ptr, ptr %allocaN, align 8
call void @free(ptr %load)
ret void
}
; Function Attrs: nounwind
declare ptr @GPA.init() #0
; Function Attrs: nounwind
define internal ptr @GPA.alloc(ptr %0, i64 %1) #0 {
@@ -38,8 +63,8 @@ entry:
%add = add i64 %loadN, 1
store i64 %add, ptr %gep, align 8
%loadN = load i64, ptr %allocaN, align 8
%malloc = call ptr @malloc(i64 %loadN)
ret ptr %malloc
%call = call ptr @malloc(i64 %loadN)
ret ptr %call
}
; Function Attrs: nounwind
@@ -63,7 +88,7 @@ entry:
declare void @Arena.add_chunk(ptr, i64) #0
; Function Attrs: nounwind
declare void @Arena.create(ptr sret({ ptr, ptr, ptr }), ptr, ptr, i64) #0
declare ptr @Arena.init(ptr, i64) #0
; Function Attrs: nounwind
declare void @Arena.reset(ptr) #0
@@ -78,7 +103,7 @@ declare ptr @Arena.alloc(ptr, i64) #0
declare void @Arena.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
declare void @BufAlloc.create(ptr sret({ ptr, ptr, ptr }), ptr, ptr, i64) #0
declare ptr @BufAlloc.init(ptr, i64) #0
; Function Attrs: nounwind
declare void @BufAlloc.reset(ptr) #0
@@ -89,6 +114,21 @@ declare ptr @BufAlloc.alloc(ptr, i64) #0
; Function Attrs: nounwind
declare void @BufAlloc.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
declare ptr @TrackingAllocator.init(ptr) #0
; Function Attrs: nounwind
declare i64 @TrackingAllocator.leak_count(ptr) #0
; Function Attrs: nounwind
declare void @TrackingAllocator.report(ptr) #0
; Function Attrs: nounwind
declare ptr @TrackingAllocator.alloc(ptr, i64) #0
; Function Attrs: nounwind
declare void @TrackingAllocator.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
define internal { ptr, i64 } @cstring(i64 %0) #0 {
entry:
@@ -157,7 +197,7 @@ entry:
%loadN = load { ptr, i64 }, ptr %alloca, align 8
%dptrN = extractvalue { ptr, i64 } %loadN, 0
%loadN = load i64, ptr %allocaN, align 8
%2 = call ptr @memcpy(ptr %dptr, ptr %dptrN, i64 %loadN)
%callN = call ptr @memcpy(ptr %dptr, ptr %dptrN, i64 %loadN)
%loadN = load i64, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%igp.data = extractvalue { ptr, i64 } %loadN, 0
@@ -165,7 +205,7 @@ entry:
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%dptrN = extractvalue { ptr, i64 } %loadN, 0
%loadN = load i64, ptr %allocaN, align 8
%3 = call ptr @memcpy(ptr %igp.ptr, ptr %dptrN, i64 %loadN)
%callN = call ptr @memcpy(ptr %igp.ptr, ptr %dptrN, i64 %loadN)
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
ret { ptr, i64 } %loadN
}
@@ -190,7 +230,7 @@ entry:
%igp.data = extractvalue { ptr, i64 } %loadN, 0
%igp.ptr = getelementptr i8, ptr %igp.data, i64 %loadN
%loadN = load i64, ptr %allocaN, align 8
%3 = call ptr @memcpy(ptr %dptr, ptr %igp.ptr, i64 %loadN)
%callN = call ptr @memcpy(ptr %dptr, ptr %igp.ptr, i64 %loadN)
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
ret { ptr, i64 } %loadN
}
@@ -359,6 +399,20 @@ entry:
ret i32 0
}
; Function Attrs: nounwind
define internal ptr @__thunk_CAllocator_Allocator_alloc(ptr %0, i64 %1) #0 {
entry:
%call = call ptr @CAllocator.alloc(ptr %0, i64 %1)
ret ptr %call
}
; Function Attrs: nounwind
define internal void @__thunk_CAllocator_Allocator_dealloc(ptr %0, ptr %1) #0 {
entry:
call void @CAllocator.dealloc(ptr %0, ptr %1)
ret void
}
; Function Attrs: nounwind
define internal ptr @__thunk_GPA_Allocator_alloc(ptr %0, i64 %1) #0 {
entry: