From b5694cc42daecc60a9f941f0c46c3a044f260758 Mon Sep 17 00:00:00 2001 From: agra Date: Tue, 19 May 2026 22:38:09 +0300 Subject: [PATCH] =?UTF-8?q?ffi=201.22:=20#jni=5Fcall(*void)=20=E2=86=92=20?= =?UTF-8?q?CallObjectMethod=20(slot=2034)=20=E2=80=94=20make-green?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes the return-type matrix. Pointer-return types aren't a simple `TypeId` enum case (they're user-defined types interned into the table), so the dispatch checks `TypeInfo.pointer | .many_pointer` ahead of the primitive switch: const is_pointer_ret = switch (types.get(ret_ty_id)) { .pointer, .many_pointer => true, else => false, }; const offset = if (is_pointer_ret) Jni.CallObjectMethod else switch (ret_ty_id) { .void => ..., .s32 => ..., ... }; LocalRef cleanup deferred: returned jobjects are JNI LocalRefs bounded by the native frame. Chains of calls within one frame consume them inline; cross-frame use must promote via `NewGlobalRef` (already wired in the slot-interning path from 1.17). The chess Android backend will consume objects inline, matching the manual pattern in `sx_android_jni.c`. Return-type matrix done: void, s32, s64, f64, bool, *void all dispatch through their respective vtable slots. Static dispatch (1.23) is next. --- src/ir/emit_llvm.zig | 8 +++++- .../ffi-jni-call-08-jobject-return.ir | 28 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ir/emit_llvm.zig b/src/ir/emit_llvm.zig index 784ece2..63bb96e 100644 --- a/src/ir/emit_llvm.zig +++ b/src/ir/emit_llvm.zig @@ -1239,7 +1239,13 @@ pub const LLVMEmitter = struct { return; } const ret_ty_id = instruction.ty; - const call_method_offset: u32 = switch (ret_ty_id) { + const is_pointer_ret = switch (self.ir_mod.types.get(ret_ty_id)) { + .pointer, .many_pointer => true, + else => false, + }; + const call_method_offset: u32 = if (is_pointer_ret) + Jni.CallObjectMethod + else switch (ret_ty_id) { .void => Jni.CallVoidMethod, .s32 => Jni.CallIntMethod, .s64 => Jni.CallLongMethod, diff --git a/tests/expected/ffi-jni-call-08-jobject-return.ir b/tests/expected/ffi-jni-call-08-jobject-return.ir index a116f6f..7c4db6e 100644 --- a/tests/expected/ffi-jni-call-08-jobject-return.ir +++ b/tests/expected/ffi-jni-call-08-jobject-return.ir @@ -3,6 +3,8 @@ @g_should_call = internal global i1 false @str = private unnamed_addr constant [10 x i8] c"getWindow\00", align 1 @str.1 = private unnamed_addr constant [24 x i8] c"()Landroid/view/Window;\00", align 1 +@SX_JNI_CLS_getWindow____Landroid_view_Window_ = internal global ptr null +@SX_JNI_MID_getWindow____Landroid_view_Window_ = internal global ptr null @str.2 = private unnamed_addr constant [4 x i8] c"ok\0A\00", align 1 @str.3 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1 @@ -205,7 +207,31 @@ entry: store ptr %1, ptr %allocaN, align 8 %load = load ptr, ptr %alloca, align 8 %loadN = load ptr, ptr %allocaN, align 8 - ret ptr undef + %jni.ifs = load ptr, ptr %load, align 8 + %jni.cached.mid = load ptr, ptr @SX_JNI_MID_getWindow____Landroid_view_Window_, align 8 + %jni.is.cached = icmp ne ptr %jni.cached.mid, null + br i1 %jni.is.cached, label %jni.cont, label %jni.miss + +jni.miss: ; preds = %entry + %2 = getelementptr inbounds ptr, ptr %jni.ifs, i32 31 + %jni.GetObjectClass = load ptr, ptr %2, align 8 + %jni.cls = call ptr %jni.GetObjectClass(ptr %load, ptr %loadN) + %3 = getelementptr inbounds ptr, ptr %jni.ifs, i32 21 + %jni.NewGlobalRef = load ptr, ptr %3, align 8 + %jni.global.cls = call ptr %jni.NewGlobalRef(ptr %load, ptr %jni.cls) + store ptr %jni.global.cls, ptr @SX_JNI_CLS_getWindow____Landroid_view_Window_, align 8 + %4 = getelementptr inbounds ptr, ptr %jni.ifs, i32 33 + %jni.GetMethodID = load ptr, ptr %4, align 8 + %jni.fresh.mid = call ptr %jni.GetMethodID(ptr %load, ptr %jni.global.cls, ptr @str, ptr @str.1) + store ptr %jni.fresh.mid, ptr @SX_JNI_MID_getWindow____Landroid_view_Window_, align 8 + br label %jni.cont + +jni.cont: ; preds = %jni.miss, %entry + %jni.mid = phi ptr [ %jni.cached.mid, %entry ], [ %jni.fresh.mid, %jni.miss ] + %5 = getelementptr inbounds ptr, ptr %jni.ifs, i32 34 + %jni.callfn = load ptr, ptr %5, align 8 + %jni.ret = call ptr %jni.callfn(ptr %load, ptr %loadN, ptr %jni.mid) + ret ptr %jni.ret } ; Function Attrs: nounwind