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

@@ -238,8 +238,11 @@ pub const Interpreter = struct {
.boolean => |b| @intFromBool(b),
.null_val => 0,
.heap_ptr => |hp| blk: {
const mem = self.heapSlice(hp) orelse return error.TypeError;
break :blk @intFromPtr(mem.ptr) + hp.offset;
// `heapSlice` returns the slice already advanced by `hp.offset`,
// so its `.ptr` IS the offset address. Adding `hp.offset` again
// double-counts and lands the foreign call past the buffer end.
_ = self.heapSlice(hp) orelse return error.TypeError;
break :blk @intFromPtr(self.heap.items[hp.id].ptr) + hp.offset;
},
.string => |s| blk: {
const buf = try self.alloc.alloc(u8, s.len + 1);
@@ -1315,6 +1318,7 @@ pub const Interpreter = struct {
}
return .{ .aggregate = fields };
},
.func_ref => |fid| .{ .func_ref = fid },
};
}
@@ -1401,56 +1405,6 @@ pub const Interpreter = struct {
fn execBuiltinInner(self: *Interpreter, bi: inst_mod.BuiltinCall, frame: *Frame) InterpError!ExecResult {
switch (bi.builtin) {
.malloc => {
const size_val = frame.getRef(bi.args[0]);
const size: usize = @intCast(size_val.asInt() orelse return error.TypeError);
const hp = self.heapAlloc(size);
return .{ .value = .{ .heap_ptr = hp } };
},
.free => {
const ptr = frame.getRef(bi.args[0]);
switch (ptr) {
.heap_ptr => |hp| self.heapFree(hp),
else => {},
}
return .{ .value = .void_val };
},
.memcpy => {
const dst = frame.getRef(bi.args[0]);
const src = frame.getRef(bi.args[1]);
const len_val = frame.getRef(bi.args[2]);
const len: usize = @intCast(len_val.asInt() orelse return error.TypeError);
const dst_hp = switch (dst) {
.heap_ptr => |hp| hp,
else => return error.CannotEvalComptime,
};
const src_bytes: []const u8 = switch (src) {
.heap_ptr => |hp| self.heapSlice(hp) orelse return error.CannotEvalComptime,
.string => |s| s,
// Raw host address (e.g. a `*u8` returned by a foreign
// call like getenv). Read `len` bytes across the FFI
// boundary into the sx-managed dst.
.int => |addr| blk: {
const raw: [*]const u8 = @ptrFromInt(@as(usize, @bitCast(addr)));
break :blk raw[0..len];
},
else => return error.CannotEvalComptime,
};
self.heapMemcpy(dst_hp, src_bytes, len);
return .{ .value = .{ .heap_ptr = dst_hp } };
},
.memset => {
const dst = frame.getRef(bi.args[0]);
const val = frame.getRef(bi.args[1]);
const len_val = frame.getRef(bi.args[2]);
const byte: u8 = @intCast(@as(u64, @bitCast(val.asInt() orelse return error.TypeError)) & 0xFF);
const len: usize = @intCast(len_val.asInt() orelse return error.TypeError);
switch (dst) {
.heap_ptr => |hp| self.heapMemset(hp, byte, len),
else => {},
}
return .{ .value = .void_val };
},
.out => {
const str_val = frame.getRef(bi.args[0]);
if (str_val.asString(self)) |s| {
@@ -1462,6 +1416,9 @@ pub const Interpreter = struct {
// Return a default size (8 bytes for most types)
return .{ .value = .{ .int = 8 } };
},
.align_of => {
return .{ .value = .{ .int = 8 } };
},
.sqrt => {
const val = frame.getRef(bi.args[0]);
const f = val.asFloat() orelse return error.TypeError;