Skip to content

Commit

Permalink
Fix #1424: Complex from/where contains compiler generated names
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Feb 20, 2019
1 parent 5962d46 commit 0758c7e
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions ICSharpCode.Decompiler/IL/Transforms/CombineExitsTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,38 @@ public void Run(ILFunction function, ILTransformContext context)
{
if (!(function.Body is BlockContainer container && container.Blocks.Count == 1))
return;
var block = container.EntryPoint;
CombineExits(container.EntryPoint);
}

static Leave CombineExits(Block block)
{
if (!(block.Instructions.SecondToLastOrDefault() is IfInstruction ifInst && block.Instructions.LastOrDefault() is Leave leaveElse))
return;
return null;
if (!ifInst.FalseInst.MatchNop())
return;
if (!(Block.Unwrap(ifInst.TrueInst) is Leave leave))
return;
return null;
// try to unwrap true branch to single instruction:
var trueInstruction = Block.Unwrap(ifInst.TrueInst);
// if the true branch is a block with multiple instructions:
// try to apply the combine exits transform to the nested block
// and then continue on that transformed block.
// Example:
// if (cond) {
// if (cond2) {
// leave (value)
// }
// leave (value2)
// }
// leave (value3)
// =>
// leave (if (cond) value else if (cond2) value2 else value3)
if (trueInstruction is Block nestedBlock && nestedBlock.Instructions.Count == 2)
trueInstruction = CombineExits(nestedBlock);
if (!(trueInstruction is Leave leave))
return null;
if (!(leave.IsLeavingFunction && leaveElse.IsLeavingFunction))
return;
return null;
if (leave.Value.MatchNop() || leaveElse.Value.MatchNop())
return;
return null;
// if (cond) {
// leave (value)
// }
Expand All @@ -48,6 +69,7 @@ public void Run(ILFunction function, ILTransformContext context)
combinedLeave.AddILRange(leave);
ifInst.ReplaceWith(combinedLeave);
block.Instructions.RemoveAt(combinedLeave.ChildIndex + 1);
return combinedLeave;
}
}
}

0 comments on commit 0758c7e

Please sign in to comment.