ffi M3.1 + M1.2 A.3 refactor: self=Obj-C id, self.field via ivar; SxAppDelegate migrated

Two coupled changes that unblock the uikit_register_classes
migration:

1) M1.2 A.3 — body's 'self' is the Obj-C id (opaque), NOT the
   state struct. Matches Apple's ObjC semantics where 'self' IS
   the object. Cocoa idiom 'xx self → id' works at runtime calls
   (addObserver:, etc.); previously the trampoline replaced
   'self' with the state-struct pointer, breaking any runtime
   call that expected an id.

   '*Self' substitution in resolveTypeWithBindings now points at
   foreignClassStructType(fcd) — the opaque class stub — instead
   of objcDefinedStateStructType(fcd).

   'self.field' access on a sx-defined class instance field is
   rewritten by lowerFieldAccess to go through the __sx_state
   ivar:
     state = object_getIvar(self, load(__<Cls>_state_ivar))
     val   = struct_gep(state, field_idx) → load

   Both read (lowerFieldAccess) and write (lowerAssignment) take
   this path. Compound ops (+=, -=, etc.) are supported via
   storeOrCompound. The lookup is filtered: skip property fields
   (those still go through the M2.2 msgSend getter/setter
   dispatch) and foreign classes (no state).

   New helpers in lower.zig:
   - lookupObjcDefinedStateFieldOnPointer — match check.
   - lowerObjcDefinedStateForObj — emit the object_getIvar +
     ivar-global-load idiom (shared between read + write paths).
   - lowerObjcDefinedStateFieldRead — the load path.

   Also moved the @llvm.global_ctors registration out of the
   sx-defined class-pair init constructor — global_ctors fires
   DURING dyld's framework load, before UIKit registers its Obj-C
   classes. objc_getClass("UIResponder") returned null, super
   was null, objc_registerClassPair crashed. main's entry block
   is post-framework-load but pre-user-code — exactly the right
   window. New helper injectCtorIntoMain.

2) M3.1 — SxAppDelegate migrated to declarative #objc_class.
   uikit_register_classes' hand-rolled objc_allocateClassPair +
   class_addMethod for SxAppDelegate is gone; the compiler
   synthesises the class at module init. The method bodies
   forward to the existing legacy IMP free functions
   (uikit_did_finish_launching, uikit_keyboard_will_change_frame)
   so we don't have to inline 70+ lines of keyboard-frame logic
   right now.

   Also adds UIResponder foreign-class declaration and chains
   UIView / UITextField to it via #extends UIResponder so the
   methods that previously lived on UITextField directly
   (becomeFirstResponder etc.) move to their proper home.

Chess on iOS-sim: board renders, full state intact. 183 example
tests + zig build test green.
This commit is contained in:
agra
2026-05-26 07:32:57 +03:00
parent ea32f8a27a
commit 66f84f67b8
4 changed files with 222 additions and 63 deletions

View File

@@ -35,7 +35,6 @@
@OBJC_METH_VAR_TYPE_.21 = private unnamed_addr constant [4 x i8] c"v@:\00"
@OBJC_METH_VAR_NAME_.22 = private unnamed_addr constant [6 x i8] c"alloc\00"
@OBJC_METH_VAR_TYPE_.23 = private unnamed_addr constant [4 x i8] c"@@:\00"
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__sx_objc_defined_class_init, ptr null }]
; Function Attrs: nounwind
declare void @out(ptr) #0
@@ -739,7 +738,9 @@ entry:
%alloca = alloca ptr, align 8
store ptr %1, ptr %alloca, align 8
%load = load ptr, ptr %alloca, align 8
%gep = getelementptr inbounds { i32 }, ptr %load, i32 0, i32 0
%loadN = load ptr, ptr @__SxFoo_state_ivar, align 8
%call = call ptr @object_getIvar(ptr %load, ptr %loadN)
%gep = getelementptr inbounds { i32 }, ptr %call, i32 0, i32 0
%loadN = load i32, ptr %gep, align 4
%add = add i32 %loadN, 1
store i32 %add, ptr %gep, align 4
@@ -792,6 +793,9 @@ entry:
ret { ptr, i64 } %call
}
; Function Attrs: nounwind
declare ptr @object_getIvar(ptr, ptr) #0
; Function Attrs: nounwind
define ptr @__SxFoo_alloc_imp(ptr %0, ptr %1) #0 {
entry:
@@ -827,9 +831,6 @@ entry:
ret void
}
; Function Attrs: nounwind
declare ptr @object_getIvar(ptr, ptr) #0
; Function Attrs: nounwind
declare ptr @sel_registerName(ptr) #0
@@ -839,9 +840,7 @@ declare void @objc_msgSendSuper2(ptr, ptr) #0
; Function Attrs: nounwind
define void @__SxFoo_bump_imp(ptr %0, ptr %1) #0 {
entry:
%load = load ptr, ptr @__SxFoo_state_ivar, align 8
%call = call ptr @object_getIvar(ptr %0, ptr %load)
call void @SxFoo.bump(ptr @__sx_default_context, ptr %call)
call void @SxFoo.bump(ptr @__sx_default_context, ptr %0)
ret void
}