Browse Source

Fix ReduceNestingTransform

pull/2069/head
Daniel Grunwald 5 years ago
parent
commit
e083d43fb9
  1. 19
      ICSharpCode.Decompiler/IL/ControlFlow/ConditionDetection.cs
  2. 2
      ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs

19
ICSharpCode.Decompiler/IL/ControlFlow/ConditionDetection.cs

@ -347,7 +347,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// ///
/// Assumes ifInst does not have an else block /// Assumes ifInst does not have an else block
/// </summary> /// </summary>
internal static void InvertIf(Block block, IfInstruction ifInst, ILTransformContext context) internal static void InvertIf(Block block, IfInstruction ifInst, ILTransformContext context, bool forceBlock = true)
{ {
Debug.Assert(ifInst.Parent == block); Debug.Assert(ifInst.Parent == block);
@ -361,12 +361,17 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
//save a copy //save a copy
var thenInst = ifInst.TrueInst; var thenInst = ifInst.TrueInst;
// extract "else...; exit". if (ifInst != block.Instructions.SecondToLastOrDefault() || forceBlock) {
// Note that this will only extract instructions that were previously inlined from another block // extract "else...; exit".
// (via InlineExitBranch), so the instructions are already fully-transformed. // Note that this will only extract instructions that were previously inlined from another block
// So it's OK to move them into a nested block again (which hides them from the following block transforms). // (via InlineExitBranch), so the instructions are already fully-transformed.
ifInst.TrueInst = ExtractBlock(block, block.Instructions.IndexOf(ifInst) + 1, block.Instructions.Count); // So it's OK to move them into a nested block again (which hides them from the following block transforms).
ifInst.TrueInst = ExtractBlock(block, block.Instructions.IndexOf(ifInst) + 1, block.Instructions.Count);
} else {
block.Instructions.RemoveAt(block.Instructions.Count - 1);
ifInst.TrueInst = exitInst;
}
if (thenInst is Block thenBlock) { if (thenInst is Block thenBlock) {
block.Instructions.AddRange(thenBlock.Instructions); block.Instructions.AddRange(thenBlock.Instructions);

2
ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs

@ -213,7 +213,7 @@ namespace ICSharpCode.Decompiler.IL
// use the same exit the block has. If the block already has one (such as a leave from a try), keep it in place // use the same exit the block has. If the block already has one (such as a leave from a try), keep it in place
EnsureEndPointUnreachable(ifInst.TrueInst, block.Instructions.Last()); EnsureEndPointUnreachable(ifInst.TrueInst, block.Instructions.Last());
ConditionDetection.InvertIf(block, ifInst, context); ConditionDetection.InvertIf(block, ifInst, context, forceBlock: false);
// ensure the exit inst of the if instruction is a keyword // ensure the exit inst of the if instruction is a keyword
Debug.Assert(!(ifInst.TrueInst is Block)); Debug.Assert(!(ifInst.TrueInst is Block));

Loading…
Cancel
Save