Browse Source

Terminating all optimizations with exceptions

pull/1/head^2
David Srbecký 18 years ago
parent
commit
af14439362
  1. 12
      lib/NRefactory/Project/Src/Ast/NodeCollection.cs
  2. 10
      src/AstMetodBodyBuilder.cs
  3. 9
      src/ControlFlow/Node-Optimize.cs
  4. 6
      src/ControlFlow/Node-Transforms.cs
  5. 19
      src/Options.cs
  6. 2
      src/StackExpressionCollection.cs

12
lib/NRefactory/Project/Src/Ast/NodeCollection.cs

@ -60,13 +60,21 @@ namespace ICSharpCode.NRefactory.Ast
public INode First { public INode First {
get { get {
return this[0]; if (this.Count > 0) {
return this[0];
} else {
return null;
}
} }
} }
public INode Last { public INode Last {
get { get {
return this[this.Count - 1]; if (this.Count > 0) {
return this[this.Count - 1];
} else {
return null;
}
} }
} }

10
src/AstMetodBodyBuilder.cs

@ -33,10 +33,16 @@ namespace Decompiler
ByteCodeCollection body = new ByteCodeCollection(methodDef); ByteCodeCollection body = new ByteCodeCollection(methodDef);
StackExpressionCollection exprCollection = new StackExpressionCollection(body); StackExpressionCollection exprCollection = new StackExpressionCollection(body);
exprCollection.Optimize(); try {
exprCollection.Optimize();
} catch (StopOptimizations) {
}
MethodBodyGraph bodyGraph = new MethodBodyGraph(exprCollection); MethodBodyGraph bodyGraph = new MethodBodyGraph(exprCollection);
bodyGraph.Optimize(); try {
bodyGraph.Optimize();
} catch (StopOptimizations) {
}
List<string> intNames = new List<string>(new string[] {"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"}); List<string> intNames = new List<string>(new string[] {"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"});

9
src/ControlFlow/Node-Optimize.cs

@ -22,7 +22,6 @@ namespace Decompiler.ControlFlow
Reset: Reset:
foreach(Node child in this.Childs) { foreach(Node child in this.Childs) {
if (child.Predecessors.Count == 1) { if (child.Predecessors.Count == 1) {
if (Options.ReduceGraph <= 0) return;
Node predecessor = child.Predecessors[0]; Node predecessor = child.Predecessors[0];
Node mergedNode; Node mergedNode;
if (child.Successors.Contains(predecessor)) { if (child.Successors.Contains(predecessor)) {
@ -36,7 +35,7 @@ namespace Decompiler.ControlFlow
} }
// If the result is single acyclic node, eliminate it // If the result is single acyclic node, eliminate it
if (this.Childs.Count == 1 && this.HeadChild is AcyclicGraph) { if (this.Childs.Count == 1 && this.HeadChild is AcyclicGraph) {
if (Options.ReduceGraph-- <= 0) return; Options.NotifyReducingGraph();
Node headChild = this.HeadChild; Node headChild = this.HeadChild;
this.Childs.Remove(this.HeadChild); this.Childs.Remove(this.HeadChild);
headChild.Childs.MoveTo(this); headChild.Childs.MoveTo(this);
@ -103,18 +102,18 @@ namespace Decompiler.ControlFlow
falseNodes.RemoveRange(commonReachable); falseNodes.RemoveRange(commonReachable);
// Replace the basic block with condition node // Replace the basic block with condition node
if (Options.ReduceGraph-- <= 0) return; Options.NotifyReducingGraph();
Node conditionParent = condition.Parent; Node conditionParent = condition.Parent;
int conditionIndex = condition.Index; int conditionIndex = condition.Index;
ConditionalNode conditionalNode = new ConditionalNode(condition); ConditionalNode conditionalNode = new ConditionalNode(condition);
conditionalNode.MoveTo(conditionParent, conditionIndex); conditionalNode.MoveTo(conditionParent, conditionIndex);
if (Options.ReduceGraph-- <= 0) return; Options.NotifyReducingGraph();
trueNodes.MoveTo(conditionalNode.TrueBody); trueNodes.MoveTo(conditionalNode.TrueBody);
if (Options.ReduceGraph-- <= 0) return;
// We can exit the 'true' part of Loop or MethodBody conviently using 'break' or 'return' // We can exit the 'true' part of Loop or MethodBody conviently using 'break' or 'return'
if (commonReachable.Count > 0 && (conditionalNode.Parent is Loop || conditionalNode.Parent is MethodBodyGraph)) { if (commonReachable.Count > 0 && (conditionalNode.Parent is Loop || conditionalNode.Parent is MethodBodyGraph)) {
Options.NotifyReducingGraph();
falseNodes.MoveTo(conditionalNode.FalseBody); falseNodes.MoveTo(conditionalNode.FalseBody);
} }

6
src/ControlFlow/Node-Transforms.cs

@ -35,12 +35,12 @@ namespace Decompiler.ControlFlow
Node mergedNode = new T(); Node mergedNode = new T();
// Add the merged node // Add the merged node
if (Options.ReduceGraph-- <= 0) return mergedNode; Options.NotifyReducingGraph();
int headIndex = this.Childs.IndexOf(nodes[0]); int headIndex = this.Childs.IndexOf(nodes[0]);
this.Childs.Insert(headIndex, mergedNode); this.Childs.Insert(headIndex, mergedNode);
foreach(Node node in nodes) { foreach(Node node in nodes) {
if (Options.ReduceGraph-- <= 0) return mergedNode; Options.NotifyReducingGraph();
node.MoveTo(mergedNode); node.MoveTo(mergedNode);
} }
@ -52,7 +52,7 @@ namespace Decompiler.ControlFlow
Reset: Reset:
foreach(Node child in this.Childs) { foreach(Node child in this.Childs) {
if (child is AcyclicGraph) { if (child is AcyclicGraph) {
if (Options.ReduceGraph-- <= 0) return; Options.NotifyReducingGraph();
child.Childs.MoveTo(this, child.Index); child.Childs.MoveTo(this, child.Index);
child.Remove(); child.Remove();
goto Reset; goto Reset;

19
src/Options.cs

@ -12,5 +12,24 @@ namespace Decompiler
public static bool ReduceAstJumps = true; public static bool ReduceAstJumps = true;
public static bool ReduceAstLoops = true; public static bool ReduceAstLoops = true;
public static bool ReduceAstOther = true; public static bool ReduceAstOther = true;
public static void NotifyCollapsingExpression()
{
if (CollapseExpression-- <= 0) {
throw new StopOptimizations();
}
}
public static void NotifyReducingGraph()
{
if (ReduceGraph-- <= 0) {
throw new StopOptimizations();
}
}
}
class StopOptimizations: Exception
{
} }
} }

2
src/StackExpressionCollection.cs

@ -27,7 +27,7 @@ namespace Decompiler
!expr.IsBranchTarget && !expr.IsBranchTarget &&
prevExpr.IsClosed) prevExpr.IsClosed)
{ {
if (Options.CollapseExpression-- <= 0) return; Options.NotifyCollapsingExpression();
this.RemoveAt(i - 1); i--; this.RemoveAt(i - 1); i--;
expr.LastArguments.Insert(0, prevExpr); expr.LastArguments.Insert(0, prevExpr);
i--; i--;

Loading…
Cancel
Save