From ebcfe4c4dc20abaf23b35f197706ccd7a54ba1d1 Mon Sep 17 00:00:00 2001 From: agra Date: Tue, 19 May 2026 22:26:58 +0300 Subject: [PATCH] =?UTF-8?q?ffi=201.18:=20#jni=5Fcall(s32)=20=E2=86=92=20Ca?= =?UTF-8?q?llIntMethod=20(slot=2049)=20=E2=80=94=20make-green?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One-line addition to the `call_method_offset` switch in `emit_llvm.zig` — `.s32 => 49` (CallIntMethod). The 1.17 caching infrastructure handles the rest: GetObjectClass → NewGlobalRef → GetMethodID populate the shared `@SX_JNI_{CLS,MID}_` pair on miss; per-call lowering loads the cached jmethodID and dispatches through vtable slot 49 with an `i32` return. IR snapshot at `tests/expected/ffi-jni-call-04-jint-return.ir` updated: the `ret i32 undef` placeholder is replaced by the full lazy-cache + CallIntMethod sequence keyed on `("getCount", "()I")`. Pre-1.18 snapshot was 1d7ea72. --- src/ir/emit_llvm.zig | 1 + tests/expected/ffi-jni-call-04-jint-return.ir | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ir/emit_llvm.zig b/src/ir/emit_llvm.zig index 23f9375..71bf732 100644 --- a/src/ir/emit_llvm.zig +++ b/src/ir/emit_llvm.zig @@ -1220,6 +1220,7 @@ pub const LLVMEmitter = struct { const ret_ty_id = instruction.ty; const call_method_offset: u32 = switch (ret_ty_id) { .void => 61, // CallVoidMethod + .s32 => 49, // CallIntMethod else => { self.mapRef(c.LLVMGetUndef(self.toLLVMType(instruction.ty))); return; diff --git a/tests/expected/ffi-jni-call-04-jint-return.ir b/tests/expected/ffi-jni-call-04-jint-return.ir index ecb89cb..da50320 100644 --- a/tests/expected/ffi-jni-call-04-jint-return.ir +++ b/tests/expected/ffi-jni-call-04-jint-return.ir @@ -3,6 +3,8 @@ @g_should_call = internal global i1 false @str = private unnamed_addr constant [9 x i8] c"getCount\00", align 1 @str.1 = private unnamed_addr constant [4 x i8] c"()I\00", align 1 +@SX_JNI_CLS_getCount____I = internal global ptr null +@SX_JNI_MID_getCount____I = 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 i32 undef + %jni.ifs = load ptr, ptr %load, align 8 + %jni.cached.mid = load ptr, ptr @SX_JNI_MID_getCount____I, 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_getCount____I, 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_getCount____I, 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 49 + %jni.callfn = load ptr, ptr %5, align 8 + %jni.ret = call i32 %jni.callfn(ptr %load, ptr %loadN, ptr %jni.mid) + ret i32 %jni.ret } ; Function Attrs: nounwind