Skip to content

Commit 5569ad7

Browse files
authored
Manually emit stack frames in EnumValueOfRewriteRule (#33)
This is emitting uncompressed/expanded frames which will need to be computed/compressed on write, but should still be better than recomputing frames for everything.
1 parent 30a9960 commit 5569ad7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/main/java/io/papermc/asm/rules/rename/EnumValueOfRewriteRule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
6666
methodGenerator.visitLookupSwitchInsn(lookupSwitchEndLabel, lookupSwitchKeys, labels);
6767
for (int i = 0; i < labels.length; i++) {
6868
methodGenerator.mark(labels[i]);
69+
// LocalVariableSorter will insert the trailing int local for this and all following visitFrame calls; adding it manually would cause duplicate locals in the frame
70+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
6971
// generate case
7072
final List<String> matchingStrings = hashToField.get(lookupSwitchKeys[i]);
7173
if (matchingStrings.size() == 1) {
@@ -93,11 +95,13 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
9395
methodGenerator.goTo(lookupSwitchEndLabel);
9496
if (nestedLabels[j] != lookupSwitchEndLabel) {
9597
methodGenerator.mark(nestedLabels[j]); // mark start of next label (except last one)
98+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
9699
}
97100
}
98101
}
99102
}
100103
methodGenerator.mark(lookupSwitchEndLabel);
104+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
101105
methodGenerator.loadLocal(tableSwitchIndexLocal);
102106
final Label[] tableSwitchLabels = new Label[tableSwitchIndexToRenamedField.length];
103107
for (int i = 0; i < tableSwitchLabels.length; i++) {
@@ -108,12 +112,15 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
108112
methodGenerator.visitTableSwitchInsn(0, tableSwitchIndexToRenamedField.length - 1, tableSwitchDefaultLabel, tableSwitchLabels);
109113
for (int i = 0; i < tableSwitchIndexToRenamedField.length; i++) {
110114
methodGenerator.mark(tableSwitchLabels[i]);
115+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
111116
methodGenerator.push(tableSwitchIndexToRenamedField[i]);
112117
methodGenerator.goTo(tableSwitchEndLabel);
113118
}
114119
methodGenerator.mark(tableSwitchDefaultLabel);
120+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
115121
methodGenerator.loadArg(0); // default to the passed in value
116122
methodGenerator.mark(tableSwitchEndLabel);
123+
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 2, new Object[]{"java/lang/String", "java/lang/String"});
117124
methodGenerator.invokeStatic(Type.getType(original.owner().descriptorString()), new Method(original.name(), original.descriptor().descriptorString()));
118125
methodGenerator.returnValue();
119126
methodGenerator.endMethod();

src/test/java/io/papermc/asm/TestUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public byte[] process(final byte[] bytes) {
5454
final ClassReader classReader = new ClassReader(bytes);
5555
final ClassWriter classWriter;
5656
if (this.copyFromClassReader()) {
57-
classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
57+
classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
5858
} else {
59-
classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
59+
classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
6060
}
6161
classReader.accept(this.factory.createVisitor(classWriter), 0);
6262
return classWriter.toByteArray();

0 commit comments

Comments
 (0)