mem: drop matchContextAllocCall — interp reaches real memory through libc

Comptime now runs the full Allocator-protocol dispatch chain — the
same IR codegen emits — instead of being short-circuited at lowering
by an AST pattern-match. `context.allocator.alloc(size)` flows
through the protocol thunk into `CAllocator.alloc → libc_malloc`,
returning a real host-libc pointer. The interp picks it up as a raw
`.int` Value and treats it as memory.

The pieces:

- `evalComptimeString` now uses the parent module instead of spinning
  up a fresh ct_module. The parent already has every type, protocol,
  impl, and thunk registered (Allocator, CAllocator, Context, the
  GPA/Tracker thunks), so the dispatch chain runs without a separate
  scan pass. The comptime function is appended to the parent module;
  it's `is_comptime` so codegen skips it.

- Interp gains raw-pointer paths:
  - `index_gep(.aggregate{.int data_ptr, .int len}, idx)` produces a
    new `.byte_ptr` (a new Value variant) — byte-granular pointer that
    `store` writes 1 byte through. Mirrors the existing heap_ptr
    semantics for the same op shape.
  - `index_gep(.int, idx)` returns `.int = p + idx` (byte-addressed).
  - `store(.int_ptr, val)` writes val's bytes via `@ptrFromInt`.
    Handles int (8B), float (8B), bool (1B), null_val (8B of zeros).
  - `store(.byte_ptr, val)` writes a single byte.
  - `marshalForeignArg` handles `.aggregate{.int data, .int len}` and
    `.byte_ptr` — both copy bytes into a null-terminated tmp buffer
    for the C-side call.
  - `asString` reads `len` bytes from a `.int` data field via
    `@ptrFromInt`.
  - `resolveFieldLoad` / `resolveFieldStore` reject field-pointer
    aggregates whose first field is a wide integer (would otherwise
    mis-trigger on a struct stored on the stack with an int pointer
    in field 0).

- `lowerFunction` / `lazyLowerFunction` / `synthesizeJniMainStub`
  bind `current_ctx_ref = &__sx_default_context` for every
  callconv(.c) sx entry — not just `isExportedEntryName`. The JNI
  stubs need this so `context.X` in the body resolves through
  current_ctx_ref now that the pattern-match is gone.

- `matchContextAllocCall` and its dispatch site are deleted.

11 JNI/ObjC `.ir` snapshots regen — the comptime function appended to
the parent module shifts string-pool indices. 153/153 example tests
pass, chess green on macOS / iOS sim / Android.
This commit is contained in:
agra
2026-05-25 10:57:38 +03:00
parent 619aff85f6
commit d415bcceaa
13 changed files with 5483 additions and 1060 deletions

View File

@@ -33,6 +33,12 @@ pub const Value = union(enum) {
closure: ClosureVal,
type_tag: TypeId,
heap_ptr: HeapPtr, // pointer into heap-allocated memory
/// Byte-granular raw pointer. Produced by `index_gep` on a string /
/// `[*]u8` aggregate whose data field is itself a raw integer pointer
/// (e.g. from libc_malloc). Store/load through this variant operate
/// on a single byte — matching the heap_ptr semantics for the same
/// op shape.
byte_ptr: usize,
pub const ClosureVal = struct {
func: FuncId,
@@ -76,7 +82,7 @@ pub const Value = union(enum) {
return switch (self) {
.string => |s| s,
.aggregate => |fields| {
// String fat pointer: { heap_ptr/string, int(len) }
// String fat pointer: { heap_ptr/string/raw_int_ptr, int(len) }
if (fields.len == 2) {
const len: usize = @intCast(fields[1].asInt() orelse return null);
switch (fields[0]) {
@@ -85,6 +91,13 @@ pub const Value = union(enum) {
return if (len <= mem.len) mem[0..len] else null;
},
.string => |s| return if (len <= s.len) s[0..len] else s,
// Raw host pointer (e.g. from CAllocator.alloc →
// libc_malloc). Read `len` bytes back from real
// memory.
.int => |addr| {
const p: [*]const u8 = @ptrFromInt(@as(usize, @bitCast(addr)));
return p[0..len];
},
else => return null,
}
}
@@ -164,6 +177,34 @@ pub const Interpreter = struct {
self.hooks.deinit();
}
/// Write `val` to the raw host address `addr`. Used when the
/// protocol-dispatch chain bottoms out at a foreign-libc-malloc
/// pointer and sx code stores through it. Comptime safety is the
/// caller's responsibility — wild writes will fault.
fn storeAtRawPtr(self: *Interpreter, addr: i64, val: Value) InterpError!void {
_ = self;
const dst: [*]u8 = @ptrFromInt(@as(usize, @bitCast(addr)));
switch (val) {
.int => |v| {
const bytes = std.mem.toBytes(v);
@memcpy(dst[0..bytes.len], &bytes);
},
.float => |v| {
const bytes = std.mem.toBytes(v);
@memcpy(dst[0..bytes.len], &bytes);
},
.boolean => |v| {
dst[0] = if (v) 1 else 0;
},
.null_val => {
const zero: u64 = 0;
const bytes = std.mem.toBytes(zero);
@memcpy(dst[0..bytes.len], &bytes);
},
else => return error.CannotEvalComptime,
}
}
// ── Implicit Context ──────────────────────────────────────────
/// Build the default Context aggregate for top-level interp calls.
@@ -268,6 +309,7 @@ pub const Interpreter = struct {
.int => |i| @bitCast(i),
.boolean => |b| @intFromBool(b),
.null_val => 0,
.byte_ptr => |addr| addr,
.heap_ptr => |hp| blk: {
// `heapSlice` returns the slice already advanced by `hp.offset`,
// so its `.ptr` IS the offset address. Adding `hp.offset` again
@@ -306,6 +348,18 @@ pub const Interpreter = struct {
tmp.append(self.alloc, buf) catch return error.TypeError;
break :blk @intFromPtr(buf.ptr);
},
// Raw host pointer (from libc_malloc-backed
// cstring). Read bytes from real memory and copy
// into a null-terminated buffer the foreign call
// can consume.
.int => |addr| {
const src: [*]const u8 = @ptrFromInt(@as(usize, @bitCast(addr)));
const buf = try self.alloc.alloc(u8, len + 1);
@memcpy(buf[0..len], src[0..len]);
buf[len] = 0;
tmp.append(self.alloc, buf) catch return error.TypeError;
break :blk @intFromPtr(buf.ptr);
},
else => return error.TypeError,
}
}
@@ -606,6 +660,20 @@ pub const Interpreter = struct {
const byte: u8 = @intCast(@as(u64, @bitCast(val.asInt() orelse return error.TypeError)) & 0xFF);
self.heapStoreByte(hp, byte);
},
// Raw host pointer (from foreign call, e.g. libc_malloc).
// 8-byte stride assumed — covers the s64/pointer/f64 cases
// sx hits via comptime protocol erasure. Aggregate stores
// unpack and recurse.
.int => |p| {
try storeAtRawPtr(self, p, val);
},
// Byte-granular pointer (from index_gep on a string).
// Always a 1-byte store — matches the heap_ptr arm.
.byte_ptr => |addr| {
const byte: u8 = @intCast(@as(u64, @bitCast(val.asInt() orelse return error.TypeError)) & 0xFF);
const dst: [*]u8 = @ptrFromInt(addr);
dst[0] = byte;
},
else => return error.CannotEvalComptime,
}
return .{ .value = .void_val };
@@ -1127,6 +1195,15 @@ pub const Interpreter = struct {
.offset = hp.offset + @as(u32, @intCast(offset)),
} } };
},
// Raw host pointer (from foreign call return,
// e.g. libc_malloc). Byte-addressed offset
// matches the heap_ptr branch above — both
// are u8-granular for sx's string/slice ops.
// Producing `.byte_ptr` makes store-through
// this address write a single byte.
.int => |p| {
return .{ .value = .{ .byte_ptr = @intCast(p + offset) } };
},
else => {},
}
}
@@ -1142,6 +1219,12 @@ pub const Interpreter = struct {
.offset = @intCast(offset),
} } };
},
// Raw host pointer base — same byte-addressed offset
// semantics as the aggregate{int_ptr, ...} branch.
.int => |p| {
const offset = idx.asInt() orelse return error.TypeError;
return .{ .value = .{ .int = p + offset } };
},
else => return error.CannotEvalComptime,
}
},
@@ -1415,6 +1498,12 @@ pub const Interpreter = struct {
if (fields.len >= 2) {
const parent_slot_val = fields[0].asInt() orelse return null;
const field_idx_val = fields[1].asInt() orelse return null;
// A real field-pointer's parent_slot is a small frame
// index; a struct aggregate whose first field happens
// to be a wide integer (e.g. a stored pointer-as-int
// or a u64) would otherwise mis-trigger this branch.
if (parent_slot_val < 0 or parent_slot_val > std.math.maxInt(u32)) return null;
if (field_idx_val < 0 or field_idx_val > std.math.maxInt(u32)) return null;
const parent_slot: u32 = @intCast(parent_slot_val);
const field_idx: usize = @intCast(field_idx_val);
const parent = frame.loadSlot(parent_slot);
@@ -1444,6 +1533,11 @@ pub const Interpreter = struct {
if (fields.len >= 2) {
const parent_slot_val = fields[0].asInt() orelse return false;
const field_idx_val = fields[1].asInt() orelse return false;
// Same field-pointer-vs-real-struct disambiguation as
// resolveFieldLoad — a wide integer in fields[0] is a
// stored pointer, not a frame index.
if (parent_slot_val < 0 or parent_slot_val > std.math.maxInt(u32)) return false;
if (field_idx_val < 0 or field_idx_val > std.math.maxInt(u32)) return false;
const parent_slot: u32 = @intCast(parent_slot_val);
const field_idx: usize = @intCast(field_idx_val);
const parent = frame.loadSlot(parent_slot);

View File

@@ -5059,13 +5059,6 @@ pub const Lowering = struct {
}
}
// Pattern-match context.allocator.alloc/dealloc → heap_alloc/heap_free.
// The comptime interp doesn't register the full Allocator
// protocol in ct_module, so the protocol-dispatch chain it
// would otherwise emit can't run. Codegen also benefits —
// direct libc malloc/free, no thunk indirection.
if (self.matchContextAllocCall(fa, args.items)) |ref| return ref;
// Type constructor call: Sx(f32).user(0.5) — obj is a call that returns a type
if (fa.object.data == .call) {
const inner_call = &fa.object.data.call;
@@ -5565,31 +5558,6 @@ pub const Lowering = struct {
return new_args;
}
/// Pattern-match `context.allocator.alloc(size)` → heap_alloc and
/// `context.allocator.dealloc(ptr)` → heap_free. Required because the
/// comptime interpreter doesn't get a full type/protocol registration
/// of the Allocator chain; the protocol-dispatch lowering would
/// produce IR that the interp can't execute. Codegen wins from this
/// short-circuit too (libc malloc/free direct, no thunk indirection
/// for the trivial default-context case).
fn matchContextAllocCall(self: *Lowering, fa: ast.FieldAccess, call_args: []const Ref) ?Ref {
if (!std.mem.eql(u8, fa.field, "alloc") and !std.mem.eql(u8, fa.field, "dealloc")) return null;
if (fa.object.data != .field_access) return null;
const inner = fa.object.data.field_access;
if (!std.mem.eql(u8, inner.field, "allocator")) return null;
if (inner.object.data != .identifier) return null;
if (!std.mem.eql(u8, inner.object.data.identifier.name, "context")) return null;
if (std.mem.eql(u8, fa.field, "alloc")) {
if (call_args.len < 1) return null;
const ptr_void = self.module.types.ptrTo(.void);
return self.builder.emit(.{ .heap_alloc = .{ .operand = call_args[0] } }, ptr_void);
} else {
if (call_args.len < 1) return null;
return self.builder.emit(.{ .heap_free = .{ .operand = call_args[0] } }, .void);
}
}
fn resolveFuncByName(self: *Lowering, name: []const u8) ?FuncId {
// Check foreign name map first (e.g., "c_abs" → "abs")
const effective_name = self.foreign_name_map.get(name) orelse name;
@@ -6473,36 +6441,20 @@ pub const Lowering = struct {
return self.alloc.dupeZ(u8, str) catch null;
}
// Case 2: Evaluate via IR interpreter
// Build a targeted comptime module with only the needed functions
var ct_module = Module.init(self.alloc);
var ct_lowering = Lowering.init(&ct_module);
ct_lowering.main_file = null; // no main file filtering
ct_lowering.comptime_param_nodes = self.comptime_param_nodes;
ct_lowering.fn_ast_map = self.fn_ast_map; // share AST map for lazy resolution
// Inherit the implicit-ctx switch: the parent program uses
// Context, so functions lowered into ct_module must carry
// __sx_ctx too (otherwise the inserted code's `context.X`
// reads can't resolve through current_ctx_ref).
ct_lowering.implicit_ctx_enabled = self.implicit_ctx_enabled;
ct_module.has_implicit_ctx = self.implicit_ctx_enabled;
// Case 2: Evaluate via IR interpreter, reusing the parent module.
// The parent module already has every protocol/struct/impl/thunk
// registered (Allocator, CAllocator, Context, the GPA/Tracker
// thunks), so the interp can run the full protocol-dispatch
// chain that codegen emits. A fresh ct_module would skip the
// scan pass and force every `context.allocator.X` call through
// a `matchContextAllocCall` shortcut to stay runnable.
const ct_func_id = self.createComptimeFunction("__insert", expr, .string);
// Lower only the functions reachable from this expression.
// For a call like build_format(fmt), we need build_format's AST.
if (expr.data == .call) {
self.lowerComptimeDeps(&ct_lowering, expr);
}
// Create a comptime function that evaluates the expression
const ct_func_id = ct_lowering.createComptimeFunction("__insert", expr, .string);
// Run the interpreter
var interp = interp_mod.Interpreter.init(&ct_module, self.alloc);
var interp = interp_mod.Interpreter.init(self.module, self.alloc);
defer interp.deinit();
const result = interp.call(ct_func_id, &.{}) catch return null;
// Extract string value
const str = result.asString(&interp) orelse switch (result) {
.string => |s| s,
else => return null,
@@ -10937,6 +10889,18 @@ pub const Lowering = struct {
self.current_foreign_method = saved_method;
}
// JNI native methods are C-callable entry points — install the
// static default Context so `context.X` reads in the method body
// resolve through `current_ctx_ref`. Mirror the same binding
// `lowerFunction` does for callconv(.c) / isExportedEntryName.
const saved_ctx_ref_jni = self.current_ctx_ref;
defer self.current_ctx_ref = saved_ctx_ref_jni;
if (self.implicit_ctx_enabled) {
if (self.global_names.get("__sx_default_context")) |dctx_gi| {
self.current_ctx_ref = self.builder.emit(.{ .global_addr = dctx_gi.id }, ptr_void);
}
}
const saved_target = self.target_type;
self.target_type = if (ret_ty != .void) ret_ty else null;
if (ret_ty != .void) {