Skip to content

Commit

Permalink
Don't push returning ifs by default, enable in recovery passes
Browse files Browse the repository at this point in the history
  • Loading branch information
leibnitz27 committed Jul 18, 2014
1 parent 4732561 commit 26de9d5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
24 changes: 18 additions & 6 deletions src/org/benf/cfr/reader/bytecode/CodeAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private AnalysisResult(DecompilerComments comments, Op04StructuredStatement code
);

private static final RecoveryOptions recover0a = new RecoveryOptions(recover0,
new RecoveryOption.TrooleanRO(OptionsImpl.FORCE_COND_PROPAGATE, Troolean.TRUE, DecompilerComment.COND_PROPAGATE)
new RecoveryOption.TrooleanRO(OptionsImpl.FORCE_COND_PROPAGATE, Troolean.TRUE, DecompilerComment.COND_PROPAGATE),
new RecoveryOption.TrooleanRO(OptionsImpl.FORCE_RETURNING_IFS, Troolean.TRUE, DecompilerComment.RETURNING_IFS)
);

private static final RecoveryOptions recover1 = new RecoveryOptions(recover0,
Expand All @@ -106,10 +107,15 @@ private AnalysisResult(DecompilerComments comments, Op04StructuredStatement code
);

private static final RecoveryOptions recover2 = new RecoveryOptions(recover1,
new RecoveryOption.BooleanRO(OptionsImpl.COMMENT_MONITORS, Boolean.TRUE, BytecodeMeta.testFlag(BytecodeMeta.CodeInfoFlag.USES_MONITORS), DecompilerComment.COMMENT_MONITORS),
new RecoveryOption.TrooleanRO(OptionsImpl.FORCE_RETURNING_IFS, Troolean.TRUE, DecompilerComment.RETURNING_IFS)
);

private static final RecoveryOptions recover3 = new RecoveryOptions(recover2,
new RecoveryOption.BooleanRO(OptionsImpl.COMMENT_MONITORS, Boolean.TRUE, BytecodeMeta.testFlag(BytecodeMeta.CodeInfoFlag.USES_MONITORS), DecompilerComment.COMMENT_MONITORS)
);

private static final RecoveryOptions[] recoveryOptionsArr = new RecoveryOptions[]{recover0, recover0a, recover1, recover2};
private static final RecoveryOptions[] recoveryOptionsArr = new RecoveryOptions[]{recover0, recover0a, recover1, recover2, recover3};

/*
* This method should not throw. If it does, something serious has gone wrong.
Expand Down Expand Up @@ -450,7 +456,9 @@ private AnalysisResult getAnalysisInner(List<Op01WithProcessedDataAndByteJumps>
}

if (options.getOption(OptionsImpl.FORCE_TOPSORT) == Troolean.TRUE) {
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, true);
if (options.getOption(OptionsImpl.FORCE_RETURNING_IFS) == Troolean.TRUE) {
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, true);
}
op03SimpleParseNodes = Cleaner.removeUnreachableCode(op03SimpleParseNodes, false);
op03SimpleParseNodes = Op03Blocks.topologicalSort(method, op03SimpleParseNodes, comments, options);
Op03SimpleStatement.removePointlessJumps(op03SimpleParseNodes);
Expand All @@ -469,7 +477,9 @@ private AnalysisResult getAnalysisInner(List<Op01WithProcessedDataAndByteJumps>
Op03SimpleStatement.combineTryCatchEnds(op03SimpleParseNodes);
Op03SimpleStatement.rewriteTryBackJumps(op03SimpleParseNodes);
Op03SimpleStatement.identifyFinally(options, method, op03SimpleParseNodes, blockIdentifierFactory);
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, true);
if (options.getOption(OptionsImpl.FORCE_RETURNING_IFS) == Troolean.TRUE) {
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, true);
}
}

/*
Expand Down Expand Up @@ -546,7 +556,9 @@ private AnalysisResult getAnalysisInner(List<Op01WithProcessedDataAndByteJumps>

// Replacing returning ifs early (above, aggressively) interferes with some nice output.
// Normally we'd do it AFTER loops.
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, false);
if (options.getOption(OptionsImpl.FORCE_RETURNING_IFS) == Troolean.TRUE) {
Op03SimpleStatement.replaceReturningIfs(op03SimpleParseNodes, false);
}

if (showOpsLevel == SHOW_L3_LOOPS1) {
debugDumper.newln().newln();
Expand Down Expand Up @@ -737,7 +749,7 @@ private AnalysisResult getAnalysisInner(List<Op01WithProcessedDataAndByteJumps>

Op04StructuredStatement.removePrimitiveDeconversion(options, method, block);
// Tidy variable names
Op04StructuredStatement.tidyVariableNames(method, block);
Op04StructuredStatement.tidyVariableNames(method, block, comments);

/*
* Now finally run some extra checks to spot wierdness.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,13 @@ public static void inlinePossibles(Op04StructuredStatement root) {
root.transform(new Inliner(), new StructuredScope());
}

public static void tidyVariableNames(Method method, Op04StructuredStatement root) {
new VariableNameTidier(method).transform(root);
public static void tidyVariableNames(Method method, Op04StructuredStatement root, DecompilerComments comments) {
VariableNameTidier variableNameTidier = new VariableNameTidier(method);
variableNameTidier.transform(root);

if (variableNameTidier.isClassRenamed()) {
comments.addComment(DecompilerComment.CLASS_RENAMED);
}
}

public static void removePointlessReturn(Op04StructuredStatement root) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.benf.cfr.reader.bytecode.analysis.variables.Keywords;
import org.benf.cfr.reader.bytecode.analysis.variables.NamedVariable;
import org.benf.cfr.reader.entities.Method;
import org.benf.cfr.reader.util.ListFactory;
import org.benf.cfr.reader.util.MapFactory;
import org.benf.cfr.reader.util.Predicate;
import org.benf.cfr.reader.util.SetFactory;
import org.benf.cfr.reader.util.*;
import org.benf.cfr.reader.util.functors.UnaryFunction;

import java.util.LinkedList;
Expand All @@ -34,6 +31,7 @@
public class VariableNameTidier implements StructuredStatementTransformer {

private final Method method;
private boolean classRenamed = false;

public VariableNameTidier(Method method) {
this.method = method;
Expand All @@ -49,6 +47,9 @@ public void transform(Op04StructuredStatement root) {
root.transform(this, structuredScopeWithVars);
}

public boolean isClassRenamed() {
return classRenamed;
}

@Override
public StructuredStatement transform(StructuredStatement in, StructuredScope scope) {
Expand Down Expand Up @@ -198,7 +199,9 @@ public void defineLocalClassHere(StructuredStatement statement, SentinelLocalCla
postfixedVarName = getNext(name);
} while (alreadyDefined(mkLcMojo(postfixedVarName)));
scope.getFirst().defineHere(mkLcMojo(postfixedVarName));
// TODO : This is bad - passes will interfere with each other!
method.markUsedLocalClassType(type, postfixedVarName);
classRenamed = true;
}

public void defineHere(StructuredStatement statement, LocalVariable localVariable) {
Expand Down
2 changes: 2 additions & 0 deletions src/org/benf/cfr/reader/util/DecompilerComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public class DecompilerComment implements Dumpable {
public static DecompilerComment UNABLE_TO_STRUCTURE = new DecompilerComment("Unable to fully structure code", true);
public static DecompilerComment AGGRESSIVE_TOPOLOGICAL_SORT = new DecompilerComment("Enabled aggressive block sorting");
public static DecompilerComment COND_PROPAGATE = new DecompilerComment("Enabled force condition propagation");
public static DecompilerComment RETURNING_IFS = new DecompilerComment("Lifted jumps to return sites");
public static DecompilerComment PRUNE_EXCEPTIONS = new DecompilerComment("Enabled unnecessary exception pruning");
public static DecompilerComment COMMENT_MONITORS = new DecompilerComment("Converted monitor instructions to comments");
public static DecompilerComment PARAMETER_CORRUPTION = new DecompilerComment("WARNING - Possible parameter corruption");
public static DecompilerComment TRY_BACKEDGE_REMOVED = new DecompilerComment("WARNING - Removed back jump from a try to a catch block - possible behaviour change.");
public static DecompilerComment TYPE_CLASHES = new DecompilerComment("Could not resolve type clashes", true);
public static DecompilerComment LOOSE_CATCH_BLOCK = new DecompilerComment("Loose catch block", true);
public static DecompilerComment CLASS_RENAMED = new DecompilerComment("Inner class renamed, behaviour may change", true);

private final String comment;
private final String summaryMessage;
Expand Down
6 changes: 5 additions & 1 deletion src/org/benf/cfr/reader/util/getopt/OptionsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ public String getDefaultValue() {
public static final PermittedOptionProvider.Argument<Troolean> FORCE_COND_PROPAGATE = new PermittedOptionProvider.Argument<Troolean>(
"forcecondpropagate", defaultNeitherTrooleanDecoder,
"Pull results of deterministic jumps back through some constant assignments");
public static final PermittedOptionProvider.Argument<Troolean> FORCE_RETURNING_IFS = new PermittedOptionProvider.Argument<Troolean>(
"forcereturningifs", defaultNeitherTrooleanDecoder,
"Move return up to jump site");
public static final PermittedOptionProvider.Argument<Troolean> FORCE_PRUNE_EXCEPTIONS = new PermittedOptionProvider.Argument<Troolean>(
"forceexceptionprune", defaultNeitherTrooleanDecoder,
"Try to extend and merge exceptions more aggressively");
Expand Down Expand Up @@ -313,7 +316,8 @@ public List<String> getFlags() {
DECOMPILER_COMMENTS, FORCE_TOPSORT, FORCE_PRUNE_EXCEPTIONS, OUTPUT_DIR,
SUGAR_STRINGBUFFER, SUGAR_STRINGBUILDER, SILENT, RECOVER, ECLIPSE, OVERRIDES,
FORCE_AGGRESSIVE_EXCEPTION_AGG, FORCE_COND_PROPAGATE, HIDE_UTF8, HIDE_LONGSTRINGS, COMMENT_MONITORS,
ALLOW_CORRECTING, LABELLED_BLOCKS, JAVA_4_CLASS_OBJECTS, HIDE_LANG_IMPORTS, FORCE_PASS, RECOVER_TYPECLASHES, HELP);
ALLOW_CORRECTING, LABELLED_BLOCKS, JAVA_4_CLASS_OBJECTS, HIDE_LANG_IMPORTS, FORCE_PASS, RECOVER_TYPECLASHES,
FORCE_RETURNING_IFS, HELP);
}

@Override
Expand Down

0 comments on commit 26de9d5

Please sign in to comment.