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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user