ffi M1.2 A.6: synthesized -dealloc IMP + [super dealloc] chain
For every sx-defined #objc_class, emit a C-callconv -dealloc IMP
that runs at refcount-zero. Frees the sx state struct, nils the
ivar, then chains to [super dealloc] so NSObject's runtime
cleanup (object_dispose, associated-object teardown, KVO, etc.)
runs as usual.
-dealloc IMP (self: id, _cmd: SEL) -> void
state = object_getIvar(self, load @__<Cls>_state_ivar)
free(state) // free(NULL) is safe
object_setIvar(self, ivar, NULL)
sup = alloca { receiver: *void, super_class: *void }
sup.receiver = self
sup.super_class = load @__<Cls>_class
sel_dealloc = sel_registerName("dealloc")
objc_msgSendSuper2(&sup, sel_dealloc)
return
Two new per-class globals:
- '__<Cls>_class' : *void — populated by emit_llvm's
class-pair init constructor with the freshly-allocated Class
pointer (after objc_registerClassPair).
- The existing '__<Cls>_state_ivar' is also consulted to find
the state struct.
The -dealloc IMP is registered on the class itself (instance
method) via class_addMethod with encoding 'v@:'. emit_llvm
ALSO stores cls_val into '__<Cls>_class' so the trampoline
can build the objc_super struct.
internStringConstantGlobal helper added to lower.zig — interns
C strings as [N:0]u8 globals with byte-level aggregate inits.
Used here for the 'dealloc' selector string.
147-objc-class-dealloc-roundtrip.sx verifies end-to-end on
macOS: alloc + release fires the IMP, and a second alloc/release
cycle proves runtime state isn't corrupted. class_getMethod-
Implementation confirms the IMP is registered.
176 example tests pass (+1). zig build test green.
Still gated: sx-side 'obj.method()' calls bail at lower.zig:4407
with the existing diagnostic. A.7 opens the gate — last sub-step
of M1.2.
This commit is contained in:
@@ -648,6 +648,38 @@ pub const LLVMEmitter = struct {
|
||||
var reg_args: [1]c.LLVMValueRef = .{cls_val};
|
||||
_ = c.LLVMBuildCall2(self.builder, register_ty, register_fn, ®_args, 1, "");
|
||||
|
||||
// Cache the class pointer in `__<Cls>_class` global so the
|
||||
// synthesized -dealloc trampoline (M1.2 A.6) can use it for
|
||||
// [super dealloc] dispatch via objc_msgSendSuper2.
|
||||
const class_global_name = std.fmt.allocPrint(self.alloc, "__{s}_class", .{class_name}) catch continue;
|
||||
defer self.alloc.free(class_global_name);
|
||||
const class_global_z = self.alloc.dupeZ(u8, class_global_name) catch continue;
|
||||
defer self.alloc.free(class_global_z);
|
||||
const class_global = c.LLVMGetNamedGlobal(self.llvm_module, class_global_z.ptr);
|
||||
if (class_global != null) {
|
||||
_ = c.LLVMBuildStore(self.builder, cls_val, class_global);
|
||||
}
|
||||
|
||||
// M1.2 A.6 — register the synthesized `-dealloc` IMP on the
|
||||
// class itself (instance method). The runtime fires it at
|
||||
// refcount-zero; the IMP frees __sx_state and chains to
|
||||
// [super dealloc].
|
||||
const dealloc_imp_name = std.fmt.allocPrint(self.alloc, "__{s}_dealloc_imp", .{class_name}) catch continue;
|
||||
defer self.alloc.free(dealloc_imp_name);
|
||||
const dealloc_imp_z = self.alloc.dupeZ(u8, dealloc_imp_name) catch continue;
|
||||
defer self.alloc.free(dealloc_imp_z);
|
||||
const dealloc_imp_fn = c.LLVMGetNamedFunction(self.llvm_module, dealloc_imp_z.ptr);
|
||||
if (dealloc_imp_fn != null) {
|
||||
const dealloc_sel_global = self.emitPrivateCString("dealloc", "OBJC_METH_VAR_NAME_");
|
||||
const dealloc_enc_global = self.emitPrivateCString("v@:", "OBJC_METH_VAR_TYPE_");
|
||||
|
||||
var sel_args: [1]c.LLVMValueRef = .{dealloc_sel_global};
|
||||
const sel_val = c.LLVMBuildCall2(self.builder, sel_reg_ty, sel_reg_fn, &sel_args, 1, "sel_dealloc");
|
||||
|
||||
var add_args: [4]c.LLVMValueRef = .{ cls_val, sel_val, dealloc_imp_fn, dealloc_enc_global };
|
||||
_ = c.LLVMBuildCall2(self.builder, add_method_ty, add_method_fn, &add_args, 4, "");
|
||||
}
|
||||
|
||||
// M1.2 A.5 — register the synthesized `+alloc` IMP on the
|
||||
// metaclass. Class methods live on the metaclass (every
|
||||
// Class object's `isa` points to the metaclass), so we
|
||||
|
||||
Reference in New Issue
Block a user