Skip to content
Browse files

Replace endfinally with jump. Closes #232

  • Loading branch information...
1 parent 3020bc9 commit 822e473df3db428f42b8e010a896f0e1c4b325e2 @dsrbecky dsrbecky committed Jun 28, 2011
Showing with 30 additions and 0 deletions.
  1. +29 −0 ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
  2. +1 −0 ICSharpCode.Decompiler/ILAst/ILAstTypes.cs
View
29 ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
@@ -55,6 +55,7 @@ public enum ILAstOptimizationStep
FindLoops,
FindConditions,
FlattenNestedMovableBlocks,
+ RemoveEndFinally,
RemoveRedundantCode2,
GotoRemoval,
DuplicateReturns,
@@ -182,6 +183,9 @@ public void Optimize(DecompilerContext context, ILBlock method, ILAstOptimizatio
if (abortBeforeStep == ILAstOptimizationStep.FlattenNestedMovableBlocks) return;
FlattenBasicBlocks(method);
+ if (abortBeforeStep == ILAstOptimizationStep.RemoveEndFinally) return;
+ RemoveEndFinally(method);
+
if (abortBeforeStep == ILAstOptimizationStep.RemoveRedundantCode2) return;
RemoveRedundantCode(method);
@@ -539,6 +543,25 @@ void FlattenBasicBlocks(ILNode node)
}
/// <summary>
+ /// Replace endfinally with jump to the end of the finally block
+ /// </summary>
+ void RemoveEndFinally(ILBlock method)
+ {
+ // Go thought the list in reverse so that we do the nested blocks first
+ foreach(var tryCatch in method.GetSelfAndChildrenRecursive<ILTryCatchBlock>(tc => tc.FinallyBlock != null).Reverse()) {
+ ILLabel label = new ILLabel() { Name = "EndFinally_" + nextLabelIndex++ };
+ tryCatch.FinallyBlock.Body.Add(label);
+ foreach(var block in tryCatch.FinallyBlock.GetSelfAndChildrenRecursive<ILBlock>()) {
+ for (int i = 0; i < block.Body.Count; i++) {
+ if (block.Body[i].Match(ILCode.Endfinally)) {
+ block.Body[i] = new ILExpression(ILCode.Br, label).WithILRanges(((ILExpression)block.Body[i]).ILRanges);
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
/// Reduce the nesting of conditions.
/// It should be done on flat data that already had most gotos removed
/// </summary>
@@ -770,6 +793,12 @@ public static bool CanBeExpressionStatement(this ILExpression expr)
}
}
+ public static ILExpression WithILRanges(this ILExpression expr, IEnumerable<ILRange> ilranges)
+ {
+ expr.ILRanges.AddRange(ilranges);
+ return expr;
+ }
+
public static void RemoveTail(this List<ILNode> body, params ILCode[] codes)
{
for (int i = 0; i < codes.Length; i++) {
View
1 ICSharpCode.Decompiler/ILAst/ILAstTypes.cs
@@ -43,6 +43,7 @@ public abstract class ILNode
void AccumulateSelfAndChildrenRecursive<T>(List<T> list, Func<T, bool> predicate) where T:ILNode
{
+ // Note: RemoveEndFinally depends on self coming before children
T thisAsT = this as T;
if (thisAsT != null && (predicate == null || predicate(thisAsT)))
list.Add(thisAsT);

0 comments on commit 822e473

Please sign in to comment.
Something went wrong with that request. Please try again.