From a27bf92a3d2376c8735d5676cac7301d67380b99 Mon Sep 17 00:00:00 2001 From: Algent Date: Thu, 3 Sep 2026 10:41:45 +0200 Subject: [PATCH] refactor: port ASM bridge helpers to Java --- JAVA_MIGRATION.md | 22 +++++++ JAVA_MIGRATION_HANDOFF.md | 42 +++++++++---- .../multipart/asm/ASMBridgeEmitter.java | 60 +++++++++++++++++++ .../multipart/asm/ASMMixinCompiler.scala | 37 ++---------- 4 files changed, 120 insertions(+), 41 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/asm/ASMBridgeEmitter.java diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 9c77323..b81d9b8 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1437,3 +1437,25 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec files and 9 Scala files / 1,714 nonblank Scala lines. Next is the bounded descriptor/bridge-emission helper unit in `ASMMixinCompiler.scala`: `seperateDesc`, `staticDesc`, `finishBridgeCall`, `writeBridge` and `writeStaticBridge`, with fresh characterization and no composition/trait-rewriting algorithm changes. +- Added eight Forge behavior tests for the descriptor/bridge helpers and committed them separately as `5d611e0`. + All pass against untouched Scala: descriptor splitting and receiver insertion, every argument/return category, + local-slot widths, invocation flags, instruction order, maxima, partial visitor failures, overridable metadata + getter order and descriptor rereads. Generated executable bridges verify virtual, special, interface and + Scala-style static-helper dispatch with mixed wide/reference arguments. The original compiler singleton requires + a Forge `LaunchClassLoader`; these tests therefore use the existing Forge harness. Baseline: 326 JVM / 135 Forge. +- Extracted only `seperateDesc`, `staticDesc`, `finishBridgeCall`, `writeBridge` and `writeStaticBridge` into Java + `ASMBridgeEmitter`, retaining their Scala entry points and exact ABI. No composition, trait-rewriting, stack or + signature-decoding algorithm changed. Existing descriptor-validation gaps, malformed-input exception types, + unnormalized owner strings, independent bridge/callee descriptors and callback order are preserved. +- Saved the pre-port jar/source, reports and 40 generated dumps under ignored `run/migration-bridge-reference/`. + All 16 named compiler APIs match by names/descriptors/modifiers/generic signatures and private fields; 14 named + classfiles are byte-identical. All 214 other named compiler methods, 35 algorithm closures and 143 ASM/generator + disassemblies outside the compiler match. All 40 dump names/hashes match and the dev configuration is unchanged. + One private bridge closure is replaced by the Java helper, keeping 450 packaged classes. No new ledger entry is + needed. Sources total 209 Java files and 9 Scala files / 1,689 nonblank Scala lines. Next: `ASMMixinCompiler.getSuper` + recognition/lookup, with fresh characterization and no algorithm fixes mixed into its port. +- Formatting/checkstyle/build and the full Forge suite pass, including a clean build after stopping Gradle: + 326 JVM / 135 Forge, zero failures/errors/skips. The eight characterization tests are unchanged after the port, + and the clean jar repeats the API/disassembly/dump matches above. The forced Scala-compilation version guard is + retained; all five `@Mod` annotations match both packaged jar versions. External Scala-trait coverage remains green + and the existing manual client checks and Java-source bridge limitations remain outstanding. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 6d337b9..7150824 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -12,8 +12,8 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client | | `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command | -Branch: `algent/java`. Base: `master`. 176 commits including the API-cleanup plan, divergence-log cleanup, ports -through the `ClassInfo` metadata lookup extraction, and the ASM characterization backfill and review fixes. +Branch: `algent/java`. Base: `master`. 178 commits including the API-cleanup plan, divergence-log cleanup, ports +through the ASM descriptor/bridge helper extraction, and the ASM characterization backfill and review fixes. ## The one rule that matters @@ -74,7 +74,7 @@ awk -F'"' '/ seperateDesc(String nameDesc) { + int split = nameDesc.indexOf('('); + return new Tuple2<>(nameDesc.substring(0, split), nameDesc.substring(split)); + } + + static String staticDesc(String owner, String desc) { + Type method = getMethodType(desc); + Type result = method.getReturnType(); + Type receiver = getType("L" + owner + ";"); + Type[] args = method.getArgumentTypes(); + Type[] staticArgs = new Type[args.length + 1]; + staticArgs[0] = receiver; + System.arraycopy(args, 0, staticArgs, 1, args.length); + return getMethodDescriptor(result, staticArgs); + } + + static void finishBridgeCall(MethodVisitor mv, String mvdesc, int opcode, String owner, String name, String desc) { + Type[] args = getArgumentTypes(mvdesc); + Type result = getReturnType(mvdesc); + int localIndex = 1; + for (Type arg : args) { + mv.visitVarInsn(arg.getOpcode(ILOAD), localIndex); + localIndex += StackAnalyser$.MODULE$.width(arg); + } + mv.visitMethodInsn(opcode, owner, name, desc, opcode == INVOKEINTERFACE); + mv.visitInsn(result.getOpcode(IRETURN)); + mv.visitMaxs( + Math.max( + StackAnalyser$.MODULE$.width(Predef$.MODULE$.wrapRefArray(args)) + 1, + StackAnalyser$.MODULE$.width(result)), + StackAnalyser$.MODULE$.width(Predef$.MODULE$.wrapRefArray(args)) + 1); + } + + static void writeBridge(MethodVisitor mv, String mvdesc, int opcode, String owner, String name, String desc) { + mv.visitVarInsn(ALOAD, 0); + finishBridgeCall(mv, mvdesc, opcode, owner, name, desc); + } + + static void writeStaticBridge(MethodNode mv, String name, MixinInfo mixin) { + writeBridge(mv, mv.desc, INVOKESTATIC, mixin.tname(), name, staticDesc(mixin.name(), mv.desc)); + } +} diff --git a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala index 07a75a0..d8ca3b5 100644 --- a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala +++ b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala @@ -236,16 +236,7 @@ object ASMMixinCompiler { name: String, desc: String ) { - val args = getArgumentTypes(mvdesc) - val ret = getReturnType(mvdesc) - var localIndex = 1 - args.foreach { arg => - mv.visitVarInsn(arg.getOpcode(ILOAD), localIndex) - localIndex += width(arg) - } - mv.visitMethodInsn(opcode, owner, name, desc, opcode == INVOKEINTERFACE) - mv.visitInsn(ret.getOpcode(IRETURN)) - mv.visitMaxs(Math.max(width(args) + 1, width(ret)), width(args) + 1) + ASMBridgeEmitter.finishBridgeCall(mv, mvdesc, opcode, owner, name, desc) } def writeBridge( @@ -256,19 +247,11 @@ object ASMMixinCompiler { name: String, desc: String ) { - mv.visitVarInsn(ALOAD, 0) - finishBridgeCall(mv, mvdesc, opcode, owner, name, desc) + ASMBridgeEmitter.writeBridge(mv, mvdesc, opcode, owner, name, desc) } def writeStaticBridge(mv: MethodNode, mname: String, t: MixinInfo) = - writeBridge( - mv, - mv.desc, - INVOKESTATIC, - t.tname, - mname, - staticDesc(t.name, mv.desc) - ) + ASMBridgeEmitter.writeStaticBridge(mv, mname, t) def mixinClasses( name: String, @@ -444,18 +427,10 @@ object ASMMixinCompiler { c } - def seperateDesc(nameDesc: String) = { - val n = nameDesc.indexOf('(') - (nameDesc.substring(0, n), nameDesc.substring(n)) - } + def seperateDesc(nameDesc: String) = ASMBridgeEmitter.seperateDesc(nameDesc) - def staticDesc(owner: String, desc: String) = { - val descT = getMethodType(desc) - getMethodDescriptor( - descT.getReturnType, - getType("L" + owner + ";") +: descT.getArgumentTypes: _* - ) - } + def staticDesc(owner: String, desc: String) = + ASMBridgeEmitter.staticDesc(owner, desc) def getSuper( minsn: MethodInsnNode,