Files
sx/tests/expected/ffi-objc-call-03-selector-sharing.ir
agra 29784c22a8 mem: implicit-context foundation + many compiler fixes
The session-long set of changes that lay the groundwork for the
Jai-literal implicit-Context-parameter refactor. Lots of accumulated
work; the new arrival is the implicit-ctx foundation (steps 1+2 of
the plan in current/CHECKPOINT-MEM.md):

  Step 1 — `CAllocator :: struct {}` stateless allocator in
    library/modules/allocators.sx, delegating directly to
    libc_malloc/libc_free. `ConstantValue` in src/ir/inst.zig gains a
    `func_ref: FuncId` leaf so nested aggregates can carry function
    pointers (the inline Allocator value's fn-ptr fields). Switch
    sites updated in emit_llvm.zig, print.zig, interp.zig.

  Step 2 — `emitDefaultContextGlobal` in src/ir/lower.zig synthesises
    a static `__sx_default_context` global with a nested-aggregate
    init_val pointing at the CAllocator → Allocator thunks. The
    second-pass `initVtableGlobals` in emit_llvm.zig is generalised
    to handle `.aggregate` init_vals (re-emits after func_map is
    populated so func_ref leaves resolve to real symbols).

Also folded in from earlier work this session:

  - Phase 1.1: `xx value` heap-copy in `buildProtocolValue` routes
    through `context.allocator` via the new `allocViaContext` helper.
  - interp.zig: `marshalForeignArg` double-offset bug fixed —
    `heapSlice` already adds `hp.offset` to the slice ptr, so the
    extra `+ hp.offset` was scribbling memcpy/memset into adjacent
    heap state, corrupting `heap.items[0]`. Symptom: `build_format`
    at comptime produced zero bytes, all `print` calls failed.
  - Lazy lowering: `lazyLowerFunction` now declares foreign-body
    functions as extern stubs in the local (comptime) module so
    cross-module foreign calls resolve.
  - Allocator API: all stdlib allocators on one-line `init() -> *T`
    (CAllocator/GPA: libc-backed; Arena/TrackingAllocator: parent-
    backed; BufAlloc: embeds state at head of user buffer).
  - issues 0038 (transitive #import), 0039 (chess + stdlib migration
    fallout), 0040 (generic struct method dot-dispatch), 0041
    (pointer types as type-arg), 0042 (alias name resolution) — all
    fixed; regression tests in examples/.
  - Diagnostic: `emitError` now embeds the lowering's
    `current_source_file` and enclosing function in the literal
    message; SX_TRACE_UNRESOLVED=1 dumps a Zig stack trace at the
    emit site so misattributed spans can't hide where the failure
    is.
  - tools/verify-step.sh (all-platforms gate) and tools/scratch.sh
    (interp/codegen parity tester) added.

Test suite: 152 example tests pass; chess builds + screenshots on
macOS / iOS sim / Android.
2026-05-24 22:59:20 +03:00

444 lines
14 KiB
Plaintext

@context = internal global { { ptr, ptr, ptr }, ptr } zeroinitializer
@OS = internal global i64 0
@ARCH = internal global i64 0
@POINTER_SIZE = internal global i64 8
@__sx_default_context = internal global { { ptr, ptr, ptr }, ptr } { { ptr, ptr, ptr } { ptr null, ptr @__thunk_CAllocator_Allocator_alloc, ptr @__thunk_CAllocator_Allocator_dealloc }, ptr null }
@OBJC_SELECTOR_REFERENCES_init = internal global ptr null
@OBJC_SELECTOR_REFERENCES_release = internal global ptr null
@str = private unnamed_addr constant [4 x i8] c"ok\0A\00", align 1
@str.1 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@OBJC_METH_VAR_NAME_ = private unnamed_addr constant [5 x i8] c"init\00"
@OBJC_METH_VAR_NAME_.2 = private unnamed_addr constant [8 x i8] c"release\00"
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__sx_objc_selector_init, ptr null }]
; Function Attrs: nounwind
declare void @out(ptr) #0
declare ptr @malloc(i64)
declare void @free(ptr)
declare ptr @memcpy(ptr, ptr, i64)
declare ptr @memset(ptr, i32, i64)
; Function Attrs: nounwind
define internal ptr @CAllocator.alloc(ptr %0, i64 %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca i64, align 8
store i64 %1, ptr %allocaN, align 8
%load = load i64, ptr %allocaN, align 8
%call = call ptr @malloc(i64 %load)
ret ptr %call
}
; Function Attrs: nounwind
define internal void @CAllocator.dealloc(ptr %0, ptr %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca ptr, align 8
store ptr %1, ptr %allocaN, align 8
%load = load ptr, ptr %allocaN, align 8
call void @free(ptr %load)
ret void
}
; Function Attrs: nounwind
declare ptr @GPA.init() #0
; Function Attrs: nounwind
define internal ptr @GPA.alloc(ptr %0, i64 %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca i64, align 8
store i64 %1, ptr %allocaN, align 8
%load = load ptr, ptr %alloca, align 8
%gep = getelementptr inbounds { i64 }, ptr %load, i32 0, i32 0
%loadN = load i64, ptr %gep, align 8
%add = add i64 %loadN, 1
store i64 %add, ptr %gep, align 8
%loadN = load i64, ptr %allocaN, align 8
%call = call ptr @malloc(i64 %loadN)
ret ptr %call
}
; Function Attrs: nounwind
define internal void @GPA.dealloc(ptr %0, ptr %1) #0 {
entry:
%alloca = alloca ptr, align 8
store ptr %0, ptr %alloca, align 8
%allocaN = alloca ptr, align 8
store ptr %1, ptr %allocaN, align 8
%load = load ptr, ptr %alloca, align 8
%gep = getelementptr inbounds { i64 }, ptr %load, i32 0, i32 0
%loadN = load i64, ptr %gep, align 8
%sub = sub i64 %loadN, 1
store i64 %sub, ptr %gep, align 8
%loadN = load ptr, ptr %allocaN, align 8
call void @free(ptr %loadN)
ret void
}
; Function Attrs: nounwind
declare void @Arena.add_chunk(ptr, i64) #0
; Function Attrs: nounwind
declare ptr @Arena.init(ptr, i64) #0
; Function Attrs: nounwind
declare void @Arena.reset(ptr) #0
; Function Attrs: nounwind
declare void @Arena.deinit(ptr) #0
; Function Attrs: nounwind
declare ptr @Arena.alloc(ptr, i64) #0
; Function Attrs: nounwind
declare void @Arena.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
declare ptr @BufAlloc.init(ptr, i64) #0
; Function Attrs: nounwind
declare void @BufAlloc.reset(ptr) #0
; Function Attrs: nounwind
declare ptr @BufAlloc.alloc(ptr, i64) #0
; Function Attrs: nounwind
declare void @BufAlloc.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
declare ptr @TrackingAllocator.init(ptr) #0
; Function Attrs: nounwind
declare i64 @TrackingAllocator.leak_count(ptr) #0
; Function Attrs: nounwind
declare void @TrackingAllocator.report(ptr) #0
; Function Attrs: nounwind
declare ptr @TrackingAllocator.alloc(ptr, i64) #0
; Function Attrs: nounwind
declare void @TrackingAllocator.dealloc(ptr, ptr) #0
; Function Attrs: nounwind
define internal { ptr, i64 } @cstring(i64 %0) #0 {
entry:
%alloca = alloca i64, align 8
store i64 %0, ptr %alloca, align 8
%load = load i64, ptr %alloca, align 8
%add = add i64 %load, 1
%heap = call ptr @malloc(i64 %add)
%allocaN = alloca ptr, align 8
store ptr %heap, ptr %allocaN, align 8
%loadN = load ptr, ptr %allocaN, align 8
%loadN = load i64, ptr %alloca, align 8
%addN = add i64 %loadN, 1
%1 = call ptr @memset(ptr %loadN, i32 0, i64 %addN)
%allocaN = alloca { ptr, i64 }, align 8
store { ptr, i64 } undef, ptr %allocaN, align 8
%loadN = load ptr, ptr %allocaN, align 8
%gep = getelementptr inbounds { ptr, i64 }, ptr %allocaN, i32 0, i32 0
%pti = ptrtoint ptr %loadN to i64
store i64 %pti, ptr %gep, align 8
%loadN = load i64, ptr %alloca, align 8
%gepN = getelementptr inbounds { ptr, i64 }, ptr %allocaN, i32 0, i32 1
store i64 %loadN, ptr %gepN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
ret { ptr, i64 } %loadN
}
; Function Attrs: nounwind
declare ptr @int_to_string(i64) #0
; Function Attrs: nounwind
declare ptr @bool_to_string(i1) #0
; Function Attrs: nounwind
declare ptr @float_to_string(double) #0
; Function Attrs: nounwind
declare void @hex_group(ptr, i64, i64) #0
; Function Attrs: nounwind
declare ptr @int_to_hex_string(i64) #0
; Function Attrs: nounwind
define internal { ptr, i64 } @concat({ ptr, i64 } %0, { ptr, i64 } %1) #0 {
entry:
%alloca = alloca { ptr, i64 }, align 8
store { ptr, i64 } %0, ptr %alloca, align 8
%allocaN = alloca { ptr, i64 }, align 8
store { ptr, i64 } %1, ptr %allocaN, align 8
%load = load { ptr, i64 }, ptr %alloca, align 8
%len = extractvalue { ptr, i64 } %load, 1
%allocaN = alloca i64, align 8
store i64 %len, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%lenN = extractvalue { ptr, i64 } %loadN, 1
%allocaN = alloca i64, align 8
store i64 %lenN, ptr %allocaN, align 8
%loadN = load i64, ptr %allocaN, align 8
%loadN = load i64, ptr %allocaN, align 8
%add = add i64 %loadN, %loadN
%call = call { ptr, i64 } @cstring(i64 %add)
%allocaN = alloca { ptr, i64 }, align 8
store { ptr, i64 } %call, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%dptr = extractvalue { ptr, i64 } %loadN, 0
%loadN = load { ptr, i64 }, ptr %alloca, align 8
%dptrN = extractvalue { ptr, i64 } %loadN, 0
%loadN = load i64, ptr %allocaN, align 8
%callN = call ptr @memcpy(ptr %dptr, ptr %dptrN, i64 %loadN)
%loadN = load i64, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%igp.data = extractvalue { ptr, i64 } %loadN, 0
%igp.ptr = getelementptr i8, ptr %igp.data, i64 %loadN
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%dptrN = extractvalue { ptr, i64 } %loadN, 0
%loadN = load i64, ptr %allocaN, align 8
%callN = call ptr @memcpy(ptr %igp.ptr, ptr %dptrN, i64 %loadN)
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
ret { ptr, i64 } %loadN
}
; Function Attrs: nounwind
define internal { ptr, i64 } @substr({ ptr, i64 } %0, i64 %1, i64 %2) #0 {
entry:
%alloca = alloca { ptr, i64 }, align 8
store { ptr, i64 } %0, ptr %alloca, align 8
%allocaN = alloca i64, align 8
store i64 %1, ptr %allocaN, align 8
%allocaN = alloca i64, align 8
store i64 %2, ptr %allocaN, align 8
%load = load i64, ptr %allocaN, align 8
%call = call { ptr, i64 } @cstring(i64 %load)
%allocaN = alloca { ptr, i64 }, align 8
store { ptr, i64 } %call, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
%dptr = extractvalue { ptr, i64 } %loadN, 0
%loadN = load i64, ptr %allocaN, align 8
%loadN = load { ptr, i64 }, ptr %alloca, align 8
%igp.data = extractvalue { ptr, i64 } %loadN, 0
%igp.ptr = getelementptr i8, ptr %igp.data, i64 %loadN
%loadN = load i64, ptr %allocaN, align 8
%callN = call ptr @memcpy(ptr %dptr, ptr %igp.ptr, i64 %loadN)
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
ret { ptr, i64 } %loadN
}
; Function Attrs: nounwind
declare ptr @xml_escape(ptr) #0
; Function Attrs: nounwind
declare ptr @path_join(ptr) #0
; Function Attrs: nounwind
declare ptr @any_to_string([2 x i64]) #0
; Function Attrs: nounwind
declare ptr @build_format(ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.add_link_flag(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.add_framework(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_output_path(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_wasm_shell(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.add_asset_dir(i64, ptr, ptr) #0
; Function Attrs: nounwind
declare i64 @BuildOptions.asset_dir_count(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.asset_dir_src_at(i64, i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.asset_dir_dest_at(i64, i64) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_post_link_callback(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_post_link_module(i64, ptr) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.binary_path(i64) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_bundle_path(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_bundle_id(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_codesign_identity(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_provisioning_profile(i64, ptr) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.bundle_path(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.bundle_id(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.codesign_identity(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.provisioning_profile(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.target_triple(i64) #0
; Function Attrs: nounwind
declare i1 @BuildOptions.is_macos(i64) #0
; Function Attrs: nounwind
declare i1 @BuildOptions.is_ios(i64) #0
; Function Attrs: nounwind
declare i1 @BuildOptions.is_ios_device(i64) #0
; Function Attrs: nounwind
declare i1 @BuildOptions.is_ios_simulator(i64) #0
; Function Attrs: nounwind
declare i1 @BuildOptions.is_android(i64) #0
; Function Attrs: nounwind
declare i64 @BuildOptions.framework_count(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.framework_at(i64, i64) #0
; Function Attrs: nounwind
declare i64 @BuildOptions.framework_path_count(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.framework_path_at(i64, i64) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_manifest_path(i64, ptr) #0
; Function Attrs: nounwind
declare void @BuildOptions.set_keystore_path(i64, ptr) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.manifest_path(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.keystore_path(i64) #0
; Function Attrs: nounwind
declare i64 @BuildOptions.jni_main_count(i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.jni_main_foreign_path_at(i64, i64) #0
; Function Attrs: nounwind
declare ptr @BuildOptions.jni_main_java_source_at(i64, i64) #0
; Function Attrs: nounwind
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
%siN = insertvalue { ptr, ptr, ptr } %si, ptr @__thunk_GPA_Allocator_alloc, 1
%siN = insertvalue { ptr, ptr, ptr } %siN, ptr @__thunk_GPA_Allocator_dealloc, 2
%siN = insertvalue { { ptr, ptr, ptr }, ptr } undef, { ptr, ptr, ptr } %siN, 0
%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 void @objc_msgSend(ptr null, ptr %load)
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
call void @objc_msgSend(ptr null, ptr %loadN)
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
call void @objc_msgSend(ptr null, ptr %loadN)
%loadN = load ptr, ptr @OBJC_SELECTOR_REFERENCES_release, align 8
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
%gepN = getelementptr inbounds { ptr, i64 }, ptr %allocaN, i32 0, i32 1
store i64 0, ptr %gepN, align 8
%allocaN = alloca { ptr, i64 }, align 8
store { ptr, i64 } { ptr @str, i64 3 }, ptr %allocaN, align 8
%allocaN = alloca { ptr, i64 }, align 8
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
%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
%str.len = extractvalue { ptr, i64 } %loadN, 1
%0 = call i64 @write(i32 1, ptr %str.ptr, i64 %str.len)
ret i32 0
}
; Function Attrs: nounwind
define internal ptr @__thunk_CAllocator_Allocator_alloc(ptr %0, i64 %1) #0 {
entry:
%call = call ptr @CAllocator.alloc(ptr %0, i64 %1)
ret ptr %call
}
; Function Attrs: nounwind
define internal void @__thunk_CAllocator_Allocator_dealloc(ptr %0, ptr %1) #0 {
entry:
call void @CAllocator.dealloc(ptr %0, ptr %1)
ret void
}
; Function Attrs: nounwind
define internal ptr @__thunk_GPA_Allocator_alloc(ptr %0, i64 %1) #0 {
entry:
%call = call ptr @GPA.alloc(ptr %0, i64 %1)
ret ptr %call
}
; Function Attrs: nounwind
define internal void @__thunk_GPA_Allocator_dealloc(ptr %0, ptr %1) #0 {
entry:
call void @GPA.dealloc(ptr %0, ptr %1)
ret void
}
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_)
store ptr %sel, ptr @OBJC_SELECTOR_REFERENCES_init, align 8
%selN = call ptr @sel_registerName(ptr @OBJC_METH_VAR_NAME_.2)
store ptr %selN, ptr @OBJC_SELECTOR_REFERENCES_release, align 8
ret void
}