From 12094a8376c9ba0e74497817f4a96f1946091151 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 11 Jul 2020 23:23:06 +0200 Subject: [PATCH] Support extraction out of control-flow IfInstruction even if it isn't using a Block as TrueInst/FalseInst. --- ICSharpCode.Decompiler/IL/Transforms/FixLoneIsInst.cs | 3 +++ ICSharpCode.Decompiler/IL/Transforms/ILExtraction.cs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/ICSharpCode.Decompiler/IL/Transforms/FixLoneIsInst.cs b/ICSharpCode.Decompiler/IL/Transforms/FixLoneIsInst.cs index 1f6b9856c..3b270f62b 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/FixLoneIsInst.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/FixLoneIsInst.cs @@ -46,6 +46,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms if (isInst.Parent.MatchCompEqualsNull(out _) || isInst.Parent.MatchCompNotEqualsNull(out _)) { continue; // supported pattern "expr is T" } + if (isInst.Parent is Block { Kind: BlockKind.ControlFlow }) { + continue; // supported via StatementBuilder.VisitIsInst + } instructionsToFix.Add(isInst); } // Need to delay fixing until we're done with iteration, because Extract() modifies parents diff --git a/ICSharpCode.Decompiler/IL/Transforms/ILExtraction.cs b/ICSharpCode.Decompiler/IL/Transforms/ILExtraction.cs index 84878611f..644c43a7f 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/ILExtraction.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/ILExtraction.cs @@ -97,6 +97,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms ctx.FlagsBeingMoved = instToExtract.Flags; ILInstruction inst = instToExtract; while (inst != null) { + if (inst.Parent is IfInstruction ifInst && inst.SlotInfo != IfInstruction.ConditionSlot) { + // this context doesn't support extraction, but maybe we can create a block here? + if (ifInst.ResultType == StackType.Void) { + Block newBlock = new Block(); + inst.ReplaceWith(newBlock); + newBlock.Instructions.Add(inst); + } + } if (inst.Parent is Block block && block.Kind == BlockKind.ControlFlow) { // We've reached the target block, and extraction is possible all the way. int insertIndex = inst.ChildIndex;