Skip to content

Commit

Permalink
Manually emit stack frames in EnumValueOfRewriteRule (#33)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpenilla authored Sep 27, 2024
1 parent 30a9960 commit 5569ad7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
methodGenerator.visitLookupSwitchInsn(lookupSwitchEndLabel, lookupSwitchKeys, labels);
for (int i = 0; i < labels.length; i++) {
methodGenerator.mark(labels[i]);
// LocalVariableSorter will insert the trailing int local for this and all following visitFrame calls; adding it manually would cause duplicate locals in the frame
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
// generate case
final List<String> matchingStrings = hashToField.get(lookupSwitchKeys[i]);
if (matchingStrings.size() == 1) {
Expand Down Expand Up @@ -93,11 +95,13 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
methodGenerator.goTo(lookupSwitchEndLabel);
if (nestedLabels[j] != lookupSwitchEndLabel) {
methodGenerator.mark(nestedLabels[j]); // mark start of next label (except last one)
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
}
}
}
}
methodGenerator.mark(lookupSwitchEndLabel);
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
methodGenerator.loadLocal(tableSwitchIndexLocal);
final Label[] tableSwitchLabels = new Label[tableSwitchIndexToRenamedField.length];
for (int i = 0; i < tableSwitchLabels.length; i++) {
Expand All @@ -108,12 +112,15 @@ public void generateMethod(final GeneratorAdapterFactory factory, final MethodCa
methodGenerator.visitTableSwitchInsn(0, tableSwitchIndexToRenamedField.length - 1, tableSwitchDefaultLabel, tableSwitchLabels);
for (int i = 0; i < tableSwitchIndexToRenamedField.length; i++) {
methodGenerator.mark(tableSwitchLabels[i]);
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
methodGenerator.push(tableSwitchIndexToRenamedField[i]);
methodGenerator.goTo(tableSwitchEndLabel);
}
methodGenerator.mark(tableSwitchDefaultLabel);
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 1, new Object[]{"java/lang/String"});
methodGenerator.loadArg(0); // default to the passed in value
methodGenerator.mark(tableSwitchEndLabel);
methodGenerator.visitFrame(Opcodes.F_NEW, 1, new Object[]{"java/lang/String"}, 2, new Object[]{"java/lang/String", "java/lang/String"});
methodGenerator.invokeStatic(Type.getType(original.owner().descriptorString()), new Method(original.name(), original.descriptor().descriptorString()));
methodGenerator.returnValue();
methodGenerator.endMethod();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/papermc/asm/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public byte[] process(final byte[] bytes) {
final ClassReader classReader = new ClassReader(bytes);
final ClassWriter classWriter;
if (this.copyFromClassReader()) {
classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
} else {
classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
}
classReader.accept(this.factory.createVisitor(classWriter), 0);
return classWriter.toByteArray();
Expand Down

0 comments on commit 5569ad7

Please sign in to comment.