Skip to content

Commit

Permalink
Fixed issue with else statement at the end of a loop not appearing
Browse files Browse the repository at this point in the history
  • Loading branch information
JarivdKaap committed May 2, 2021
1 parent 21a3779 commit 61a6fe4
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Analyze(Function f)
jmp.Block = currentBlock;
currentBlock = new BasicBlock();
f.Blocks.Add(currentBlock);
// Look if there's a label, this will be the start of the new block
if (i + 1 < f.Instructions.Count && f.Instructions[i + 1] is Label l)
{
labelBasicBlockMap.Add(l, currentBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public void Analyze(Function f)
ifStatement.TrueBody = bb;
}
}
if (node.Successors[1] != node.Follow && node.Successors[1].Instructions.Any())
if (node.Successors[1] != node.Follow)
{
ifStatement.FalseBody = node.Successors[1];
ifStatement.FalseBody.MarkCodegenerated(f.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Analyze(Function f)
{
foreach (var b in f.Blocks)
{
if (b.Instructions.Count > 0 && b.Instructions.Last() is Jump {PostTakenAssignment: { }} jmp)
if (b.Instructions.Any() && b.Instructions.Last() is Jump {PostTakenAssignment: { }} jmp)
{
b.Successors[1].Instructions.Insert(0, jmp.PostTakenAssignment);
jmp.PostTakenAssignment.PropagateAlways = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ HashSet<BasicBlock> Visit(BasicBlock b)
}

// if ((nodeType(m) == 2-way) ^ (inHeadLatch(m) == false)
if (b.Successors.Count() == 2 && b.Instructions.Last() is Jump jmp && (!b.IsLoopHead || b.LoopType != CFG.LoopType.LoopPretested))
if (b.Successors.Count() == 2 && b.Instructions.Last() is Jump jmp && (!b.IsLoopHead || b.LoopType != LoopType.LoopPretested))
{
int maxEdges = 0;
BasicBlock maxNode = null;
Expand Down Expand Up @@ -86,6 +86,14 @@ HashSet<BasicBlock> Visit(BasicBlock b)
maxNode = b.Successors[1];
}

if (maxNode == null && b.Successors[0].Successors.Count == 1 && b.Successors[1].Successors.Count == 1 &&
b.Successors[0].Successors[0] == b.Successors[1].Successors[0] &&
b.Instructions.Any() && b.Instructions.Last() is Jump j && j.BlockDest == b.Successors[1] &&
b.Successors[0].Instructions.Any() && b.Successors[0].Instructions.Last() is Jump j2 && j2.BlockDest == b.Successors[0].Successors[0] &&
b.Successors[0].Successors[0].IsLoopHead)
{
maxNode = b.Successors[1].Successors[0];
}

if (maxNode != null)
{
Expand Down
3 changes: 0 additions & 3 deletions CoDHVKDecompiler.Decompiler/IR/Instruction/Jump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ public override string ToString()
if (Conditional)
{
ret += $@"if {Condition} else ";
#if DEBUG
ret += "-- to: " + Dest;
#endif
}
if (BlockDest != null)
{
Expand Down

0 comments on commit 61a6fe4

Please sign in to comment.