Inline assignments made before a return

Extracted from https://github.com/MinecraftForge/ForgeFlower/blob/master/FernFlower-Patches/0030-Improve-stack-var-processor-output.patch
master
Graham 4 years ago
parent 74c7174bbf
commit 1f2ee3c81f
  1. 26
      src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java

@ -128,6 +128,11 @@ public class SimplifyExprentsHelper {
} }
Exprent next = list.get(index + 1); Exprent next = list.get(index + 1);
if (isAssignmentReturn(current, next)) {
list.remove(index);
res = true;
continue;
}
// constructor invocation // constructor invocation
if (isConstructorInvocationRemote(list, index)) { if (isConstructorInvocationRemote(list, index)) {
@ -335,6 +340,27 @@ public class SimplifyExprentsHelper {
return 0; return 0;
} }
private static boolean isAssignmentReturn(Exprent first, Exprent second) {
//If assignment then exit.
if (first.type == Exprent.EXPRENT_ASSIGNMENT && second.type == Exprent.EXPRENT_EXIT) {
AssignmentExprent assignment = (AssignmentExprent) first;
ExitExprent exit = (ExitExprent) second;
//if simple assign and exit is return and return isn't void
if (assignment.getCondType() == AssignmentExprent.CONDITION_NONE && exit.getExitType() == ExitExprent.EXIT_RETURN && exit.getValue() != null) {
if (assignment.getLeft().type == Exprent.EXPRENT_VAR && exit.getValue().type == Exprent.EXPRENT_VAR) {
VarExprent assignmentLeft = (VarExprent) assignment.getLeft();
VarExprent exitValue = (VarExprent) exit.getValue();
//If the assignment before the return is immediately used in the return, inline it.
if (assignmentLeft.equals(exitValue) && !assignmentLeft.isStack() && !exitValue.isStack()) {
exit.replaceExprent(exitValue, assignment.getRight());
return true;
}
}
}
}
return false;
}
private static boolean isTrivialStackAssignment(Exprent first) { private static boolean isTrivialStackAssignment(Exprent first) {
if (first.type == Exprent.EXPRENT_ASSIGNMENT) { if (first.type == Exprent.EXPRENT_ASSIGNMENT) {
AssignmentExprent asf = (AssignmentExprent)first; AssignmentExprent asf = (AssignmentExprent)first;

Loading…
Cancel
Save