refactor: modernize stack analyser switches

This commit is contained in:
Algent committed 2026-09-09 12:57:58 +02:00
1 parent c60abe790c
commit c3424df09c
2 files changed
+79 -168

No files matched your search

+8 -4
View File
@@ -3,7 +3,7 @@
## Current state
The scoped integration is implemented on `algent/java`, building on baseline `65d0cd0`. The three regression fixes
are preserved. `build.gradle` adds two tasks; `StackAnalyserLogic.visitInsn` uses the validated Java 21 pattern switch,
are preserved. `build.gradle` adds two tasks; `StackAnalyserLogic` uses pattern and arrow-rule switches,
`JavaTraitRegistration` and `ClassInfoLookup` use Java 21 pattern variables, and `ScalaSignatureParser` uses switch
expressions. The nine Scala sources, Scala 2.11.5 dependency, source layout, and normal Gradle entry points remain in
place. Production tasks do not read a frozen jar or any files under `run/jvmdg-trial/`.
@@ -75,9 +75,9 @@ Recommended order:
1. Start with package-private helpers called directly by retained Scala. `JavaTraitRegistration.java` and
`ClassInfoLookup.java` are completed follow-ups; pattern variables remove their checked casts without changing the
Scala-facing declarations. `StackAnalyserLogic.java` remains the original proven example.
2. Continue only where modern syntax produces a concrete control-flow gain. `ScalaSignatureParser.java` is complete:
its two result-producing switches are now switch expressions. Prefer simplifying the remaining opcode switches in
the already-modern `StackAnalyserLogic.java` before adding another build exclusion. `ScalaTraitRegistration.java`
2. Continue only where modern syntax produces a concrete control-flow gain. `ScalaSignatureParser.java` and the
remaining opcode switches in the already-modern `StackAnalyserLogic.java` are complete. The next cohesive internal
group is the three hollow/post microblock logic helpers called only by retained Scala. `ScalaTraitRegistration.java`
should not move merely to restyle its erased `Some` checks.
3. Consider public core implementations such as `RedstoneInteractions$.java`, `TileMultipart.java` and the registries
only after the internal batches. Modern method bodies are technically possible, but their published ABI and frozen
@@ -112,6 +112,10 @@ Forge tests and the 134-dump comparison; all 450 packaged classes remain version
and it has no executable JVM Downgrader API reference. Modern string concatenation in the same internal class lowers
to six private helper methods; these are compiler implementation details, not consumer entry points.
The follow-up `StackAnalyserLogic` batch converts constant classification to a pattern switch expression and removes
fall-through syntax from its opcode switches. It adds no build exclusion and preserves the same 576 JVM tests, 289
Java 8 Forge tests and all 134 generated ASM hashes.
### fastutil audit
GTNHLib 0.11.44 currently contributes `it.unimi.dsi:fastutil:8.5.18` to `compileClasspath`. It does **not** contribute
@@ -56,18 +56,20 @@ final class StackAnalyserLogic {
static Type constType(Const constant) {
Object value = constant.c();
if (value instanceof Byte) return BYTE_TYPE;
if (value instanceof Short) return SHORT_TYPE;
if (value instanceof Integer) return INT_TYPE;
if (value instanceof Long) return LONG_TYPE;
if (value instanceof Float) return FLOAT_TYPE;
if (value instanceof Double) return DOUBLE_TYPE;
if (value instanceof Character) return CHAR_TYPE;
if (value instanceof Boolean) return BOOLEAN_TYPE;
if (value instanceof String) return getObjectType("java/lang/String");
if (value == null) return getObjectType("java/lang/Object");
// The failure message rereads the virtual accessor after classification.
throw new IllegalArgumentException("Unknown const " + constant.c());
return switch (value) {
case Byte ignored -> BYTE_TYPE;
case Short ignored -> SHORT_TYPE;
case Integer ignored -> INT_TYPE;
case Long ignored -> LONG_TYPE;
case Float ignored -> FLOAT_TYPE;
case Double ignored -> DOUBLE_TYPE;
case Character ignored -> CHAR_TYPE;
case Boolean ignored -> BOOLEAN_TYPE;
case String ignored -> getObjectType("java/lang/String");
case null -> getObjectType("java/lang/Object");
// The failure message rereads the virtual accessor after classification.
default -> throw new IllegalArgumentException("Unknown const " + constant.c());
};
}
static void initialize(StackAnalyser a, Type owner, MethodNode method,
@@ -128,121 +130,53 @@ final class StackAnalyserLogic {
case InsnNode ignored -> {
int op = insn.getOpcode();
switch (op) {
case ACONST_NULL:
a.push(new Const(null, insn));
break;
case ICONST_M1:
a.push(new Const(-1, insn));
break;
case ICONST_0:
a.push(new Const(0, insn));
break;
case ICONST_1:
a.push(new Const(1, insn));
break;
case ICONST_2:
a.push(new Const(2, insn));
break;
case ICONST_3:
a.push(new Const(3, insn));
break;
case ICONST_4:
a.push(new Const(4, insn));
break;
case ICONST_5:
a.push(new Const(5, insn));
break;
case LCONST_0:
a.push(new Const(0L, insn));
break;
case LCONST_1:
a.push(new Const(1L, insn));
break;
case FCONST_0:
a.push(new Const(0f, insn));
break;
case FCONST_1:
a.push(new Const(1f, insn));
break;
case FCONST_2:
a.push(new Const(2f, insn));
break;
case DCONST_0:
a.push(new Const(0d, insn));
break;
case DCONST_1:
a.push(new Const(1d, insn));
break;
case POP:
rawPop(a);
break;
case POP2:
case ACONST_NULL -> a.push(new Const(null, insn));
case ICONST_M1 -> a.push(new Const(-1, insn));
case ICONST_0 -> a.push(new Const(0, insn));
case ICONST_1 -> a.push(new Const(1, insn));
case ICONST_2 -> a.push(new Const(2, insn));
case ICONST_3 -> a.push(new Const(3, insn));
case ICONST_4 -> a.push(new Const(4, insn));
case ICONST_5 -> a.push(new Const(5, insn));
case LCONST_0 -> a.push(new Const(0L, insn));
case LCONST_1 -> a.push(new Const(1L, insn));
case FCONST_0 -> a.push(new Const(0f, insn));
case FCONST_1 -> a.push(new Const(1f, insn));
case FCONST_2 -> a.push(new Const(2f, insn));
case DCONST_0 -> a.push(new Const(0d, insn));
case DCONST_1 -> a.push(new Const(1d, insn));
case POP -> rawPop(a);
case POP2 -> {
rawPop(a);
rawPop(a);
break;
case DUP:
a.push(peek(a));
break;
case DUP_X1:
a.insert(2, peek(a));
break;
case DUP_X2:
a.insert(3, peek(a));
break;
case DUP2:
}
case DUP -> a.push(peek(a));
case DUP_X1 -> a.insert(2, peek(a));
case DUP_X2 -> a.insert(3, peek(a));
case DUP2 -> {
a.push(a.peek(1));
a.push(a.peek(1));
break;
case DUP2_X1:
}
case DUP2_X1 -> {
a.insert(3, a.peek(1));
a.insert(3, peek(a));
break;
case DUP2_X2:
}
case DUP2_X2 -> {
a.insert(4, a.peek(1));
a.insert(4, peek(a));
break;
case SWAP:
a.push(a.pop(1));
break;
}
case SWAP -> a.push(a.pop(1));
// Preserve the reference's inferred types, including conversions to int reporting double.
case L2I:
case F2I:
case D2I:
a.push(new PrimitiveCast(pop(a), DOUBLE_TYPE, insn));
break;
case I2L:
case F2L:
case D2L:
a.push(new PrimitiveCast(pop(a), LONG_TYPE, insn));
break;
case I2F:
case L2F:
case D2F:
a.push(new PrimitiveCast(pop(a), FLOAT_TYPE, insn));
break;
case I2D:
case L2D:
case F2D:
a.push(new PrimitiveCast(pop(a), DOUBLE_TYPE, insn));
break;
case I2B:
a.push(new PrimitiveCast(pop(a), BYTE_TYPE, insn));
break;
case I2C:
a.push(new PrimitiveCast(pop(a), CHAR_TYPE, insn));
break;
case I2S:
a.push(new PrimitiveCast(pop(a), SHORT_TYPE, insn));
break;
case ARRAYLENGTH:
a.push(new ArrayLength(pop(a), insn));
break;
case ATHROW:
case MONITORENTER:
case MONITOREXIT:
pop(a);
break;
default:
case L2I, F2I, D2I -> a.push(new PrimitiveCast(pop(a), DOUBLE_TYPE, insn));
case I2L, F2L, D2L -> a.push(new PrimitiveCast(pop(a), LONG_TYPE, insn));
case I2F, L2F, D2F -> a.push(new PrimitiveCast(pop(a), FLOAT_TYPE, insn));
case I2D, L2D, F2D -> a.push(new PrimitiveCast(pop(a), DOUBLE_TYPE, insn));
case I2B -> a.push(new PrimitiveCast(pop(a), BYTE_TYPE, insn));
case I2C -> a.push(new PrimitiveCast(pop(a), CHAR_TYPE, insn));
case I2S -> a.push(new PrimitiveCast(pop(a), SHORT_TYPE, insn));
case ARRAYLENGTH -> a.push(new ArrayLength(pop(a), insn));
case ATHROW, MONITORENTER, MONITOREXIT -> pop(a);
default -> {
if (op >= IALOAD && op <= SALOAD) {
a.push(new ArrayLoad(pop(a), pop(a), insn));
} else if (op >= IASTORE && op <= SASTORE) {
@@ -256,18 +190,14 @@ final class StackAnalyserLogic {
} else if (op >= IRETURN && op <= ARETURN) {
pop(a);
}
}
}
}
case IntInsnNode intInsn -> {
switch (insn.getOpcode()) {
case BIPUSH:
a.push(new Const((byte) intInsn.operand, insn));
break;
case SIPUSH:
a.push(new Const((short) intInsn.operand, insn));
break;
default:
throw new MatchError(insn.getOpcode());
case BIPUSH -> a.push(new Const((byte) intInsn.operand, insn));
case SIPUSH -> a.push(new Const((short) intInsn.operand, insn));
default -> throw new MatchError(insn.getOpcode());
}
}
case LdcInsnNode ldc -> {
@@ -313,56 +243,33 @@ final class StackAnalyserLogic {
case LookupSwitchInsnNode ignored -> pop(a);
case FieldInsnNode field -> {
switch (insn.getOpcode()) {
case GETSTATIC:
a.push(new GetField(null, field, insn));
break;
case PUTSTATIC:
pop(a);
break;
case GETFIELD:
a.push(new GetField(pop(a), field, insn));
break;
case PUTFIELD:
case GETSTATIC -> a.push(new GetField(null, field, insn));
case PUTSTATIC -> pop(a);
case GETFIELD -> a.push(new GetField(pop(a), field, insn));
case PUTFIELD -> {
pop(a);
pop(a);
break;
default:
throw new MatchError(insn.getOpcode());
}
default -> throw new MatchError(insn.getOpcode());
}
}
case MethodInsnNode method -> {
switch (insn.getOpcode()) {
case INVOKEVIRTUAL:
case INVOKESPECIAL:
case INVOKEINTERFACE:
case INVOKEVIRTUAL, INVOKESPECIAL, INVOKEINTERFACE ->
a.push(new Invoke(insn.getOpcode(), a.popArgs(method.desc), pop(a), method, insn));
break;
case INVOKESTATIC:
case INVOKESTATIC ->
a.push(new Invoke(insn.getOpcode(), a.popArgs(method.desc), null, method, insn));
break;
default:
throw new MatchError(insn.getOpcode());
default -> throw new MatchError(insn.getOpcode());
}
}
case TypeInsnNode typeInsn -> {
switch (insn.getOpcode()) {
case NEW:
a.push(new New(getObjectType(typeInsn.desc), insn));
break;
case NEWARRAY:
a.push(new NewArray(pop(a), getObjectType(typeInsn.desc), insn));
break;
case ANEWARRAY:
a.push(new NewArray(pop(a), getObjectType("[" + typeInsn.desc), insn));
break;
case CHECKCAST:
a.push(new Cast(pop(a), getObjectType(typeInsn.desc), insn));
break;
case INSTANCEOF:
a.push(new UnaryOp(INSTANCEOF, pop(a), insn));
break;
default:
throw new MatchError(insn.getOpcode());
case NEW -> a.push(new New(getObjectType(typeInsn.desc), insn));
case NEWARRAY -> a.push(new NewArray(pop(a), getObjectType(typeInsn.desc), insn));
case ANEWARRAY -> a.push(new NewArray(pop(a), getObjectType("[" + typeInsn.desc), insn));
case CHECKCAST -> a.push(new Cast(pop(a), getObjectType(typeInsn.desc), insn));
case INSTANCEOF -> a.push(new UnaryOp(INSTANCEOF, pop(a), insn));
default -> throw new MatchError(insn.getOpcode());
}
}
case MultiANewArrayInsnNode multi -> {