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