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 @@ -60,13 +60,21 @@ namespace ICSharpCode.NRefactory.Ast
public INode First {
get {
return this[0];
if (this.Count > 0) {
return this[0];
} else {
return null;
}
}
}
public INode Last {
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 @@ -33,10 +33,16 @@ namespace Decompiler
ByteCodeCollection body = new ByteCodeCollection(methodDef);
StackExpressionCollection exprCollection = new StackExpressionCollection(body);
exprCollection.Optimize();
try {
exprCollection.Optimize();
} catch (StopOptimizations) {
}
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"});

9
src/ControlFlow/Node-Optimize.cs

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

6
src/ControlFlow/Node-Transforms.cs

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

19
src/Options.cs

@ -12,5 +12,24 @@ namespace Decompiler @@ -12,5 +12,24 @@ namespace Decompiler
public static bool ReduceAstJumps = true;
public static bool ReduceAstLoops = 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 @@ -27,7 +27,7 @@ namespace Decompiler
!expr.IsBranchTarget &&
prevExpr.IsClosed)
{
if (Options.CollapseExpression-- <= 0) return;
Options.NotifyCollapsingExpression();
this.RemoveAt(i - 1); i--;
expr.LastArguments.Insert(0, prevExpr);
i--;

Loading…
Cancel
Save