ffi 1.6: objc_msg_send IR opcode + per-call-site LLVM fn type
102/102 regression tests pass; chess Android + iOS-sim still build
clean. `ffi-objc-call-04-primitive-returns` flips from xfail to
passing with both nil-recv and real-recv flavors of *void / s64
returns exercised.
Key change: a new `objc_msg_send` IR opcode bundles (recv, sel,
extra args) and carries the return type via the `Inst.ty` field.
emit_llvm.zig builds a per-call-site LLVM function type from the
argument Refs' IR types (recv/sel as ptr; extra args through
abiCoerceParamType) and dispatches with LLVMBuildCall2. One
declared `@objc_msgSend` symbol is reused across every return
type — opaque pointers make the function value type-erased, so
each call site picks its own ABI.
before: one (recv, sel) -> ptr LLVM declaration, hard-coded
per call site; only void return wired in 1.3.
after: same declaration, each call site provides a fresh
LLVMBuildCall2 fn-type → s64 / *void / bool / f64
returns all dispatch correctly without separate FuncIds.
Selector init mechanism: stayed with the @llvm.global_ctors
constructor. Investigated clang's
`__DATA,__objc_selrefs` + `externally_initialized` shape — works
for fully-linked binaries (dyld substitutes the SEL at load
time) but **LLVM ORC JIT** (the engine behind `sx run`) doesn't
process Mach-O Obj-C metadata sections, so the slot keeps its
initial value (the method-name string pointer) and dispatch
crashes with "<null selector>". The portable choice: keep the
constructor AND inject a direct call to it at `main`'s entry —
idempotent under dyld (sel_registerName returns the same SEL on
re-registration), required for ORC JIT.
Files touched:
src/ir/inst.zig | new ObjcMsgSend struct + opcode
src/ir/lower.zig | drop the void-only restriction; emit the
new opcode; remove the orphaned
getObjcMsgSendFid path (objc_msgSend
declaration moved to emit_llvm)
src/ir/emit_llvm.zig | objc_msg_send arm (per-call-site
LLVMBuildCall2); lazy `@objc_msgSend`
declaration via getObjcMsgSendValue;
emitObjcSelectorInit refactored to inject
the ctor call at main's entry
src/ir/{print,interp}.zig | switch arms for the new opcode
`ffi-objc-call-03-selector-sharing.ir` snapshot updates to
reflect the new shape (the `call ... @objc_msgSend` call sites
no longer mention a typed wrapper).
This commit is contained in:
@@ -218,6 +218,7 @@ declare i64 @build_options() #0
|
||||
; Function Attrs: nounwind
|
||||
define i32 @main() #0 {
|
||||
entry:
|
||||
call void @__sx_objc_selector_init()
|
||||
%alloca = alloca { i64 }, align 8
|
||||
store { i64 } zeroinitializer, ptr %alloca, align 8
|
||||
%si = insertvalue { ptr, ptr, ptr } undef, ptr %alloca, 0
|
||||
@@ -227,13 +228,13 @@ entry:
|
||||
%siN = insertvalue { { ptr, ptr, ptr }, ptr } %siN, ptr null, 1
|
||||
store { { ptr, ptr, ptr }, ptr } %siN, ptr @context, align 8
|
||||
%load = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
|
||||
%call = call ptr @objc_msgSend(ptr null, ptr %load)
|
||||
call void @objc_msgSend(ptr null, ptr %load)
|
||||
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
|
||||
%callN = call ptr @objc_msgSend(ptr null, ptr %loadN)
|
||||
call void @objc_msgSend(ptr null, ptr %loadN)
|
||||
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
|
||||
%callN = call ptr @objc_msgSend(ptr null, ptr %loadN)
|
||||
call void @objc_msgSend(ptr null, ptr %loadN)
|
||||
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_release, align 8
|
||||
%callN = call ptr @objc_msgSend(ptr null, ptr %loadN)
|
||||
call void @objc_msgSend(ptr null, ptr %loadN)
|
||||
%allocaN = alloca { ptr, i64 }, align 8
|
||||
%gep = getelementptr inbounds { ptr, i64 }, ptr %allocaN, i32 0, i32 0
|
||||
store ptr null, ptr %gep, align 8
|
||||
@@ -245,8 +246,8 @@ entry:
|
||||
store { ptr, i64 } { ptr @str.1, i64 0 }, ptr %allocaN, align 8
|
||||
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
|
||||
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
|
||||
%callN = call { ptr, i64 } @substr({ ptr, i64 } %loadN, i64 0, i64 3)
|
||||
%callN = call { ptr, i64 } @concat({ ptr, i64 } %loadN, { ptr, i64 } %callN)
|
||||
%call = call { ptr, i64 } @substr({ ptr, i64 } %loadN, i64 0, i64 3)
|
||||
%callN = call { ptr, i64 } @concat({ ptr, i64 } %loadN, { ptr, i64 } %call)
|
||||
store { ptr, i64 } %callN, ptr %allocaN, align 8
|
||||
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
|
||||
%str.ptr = extractvalue { ptr, i64 } %loadN, 0
|
||||
@@ -269,14 +270,12 @@ entry:
|
||||
ret void
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @sel_registerName(ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @objc_msgSend(ptr, ptr) #0
|
||||
declare ptr @objc_msgSend(ptr, ptr)
|
||||
|
||||
declare i64 @write(i32, ptr, i64)
|
||||
|
||||
declare ptr @sel_registerName(ptr)
|
||||
|
||||
define internal void @__sx_objc_selector_init() {
|
||||
entry:
|
||||
%sel = call ptr @sel_registerName(ptr @OBJC_METH_VAR_NAME_)
|
||||
|
||||
@@ -1 +1 @@
|
||||
1
|
||||
0
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
/Users/agra/projects/sx/examples/ffi-objc-call-04-primitive-returns.sx: error: #objc_call: only `void` return + (recv, selector) is lowered today; non-void / arg-bearing arities land in later phase-1 steps
|
||||
nil class = true
|
||||
nil hash = 0
|
||||
meta non-null = true
|
||||
hash non-zero = true
|
||||
|
||||
Reference in New Issue
Block a user