Browse Source

Replace endfinally with jump. Closes #232

pull/252/head
David Srbecký 14 years ago
parent
commit
822e473df3
  1. 29
      ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
  2. 1
      ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

29
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

@ -55,6 +55,7 @@ namespace ICSharpCode.Decompiler.ILAst @@ -55,6 +55,7 @@ namespace ICSharpCode.Decompiler.ILAst
FindLoops,
FindConditions,
FlattenNestedMovableBlocks,
RemoveEndFinally,
RemoveRedundantCode2,
GotoRemoval,
DuplicateReturns,
@ -182,6 +183,9 @@ namespace ICSharpCode.Decompiler.ILAst @@ -182,6 +183,9 @@ namespace ICSharpCode.Decompiler.ILAst
if (abortBeforeStep == ILAstOptimizationStep.FlattenNestedMovableBlocks) return;
FlattenBasicBlocks(method);
if (abortBeforeStep == ILAstOptimizationStep.RemoveEndFinally) return;
RemoveEndFinally(method);
if (abortBeforeStep == ILAstOptimizationStep.RemoveRedundantCode2) return;
RemoveRedundantCode(method);
@ -538,6 +542,25 @@ namespace ICSharpCode.Decompiler.ILAst @@ -538,6 +542,25 @@ namespace ICSharpCode.Decompiler.ILAst
}
}
/// <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
@ -770,6 +793,12 @@ namespace ICSharpCode.Decompiler.ILAst @@ -770,6 +793,12 @@ namespace ICSharpCode.Decompiler.ILAst
}
}
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++) {

1
ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

@ -43,6 +43,7 @@ namespace ICSharpCode.Decompiler.ILAst @@ -43,6 +43,7 @@ namespace ICSharpCode.Decompiler.ILAst
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);

Loading…
Cancel
Save