Skip to content

Commit

Permalink
Fix "surrounding loops" calculation being reversed
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Jul 11, 2024
1 parent a64b27e commit 5aea601
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Underanalyzer/Decompiler/ControlFlow/Branches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public static Dictionary<Block, Loop> FindSurroundingLoops(
List<Block> blocks, Dictionary<int, Block> blockByAddress, List<Loop> loops)
{
// Assign blocks to loops.
// We assume that loops are sorted so that nested loops come after outer loops.
// We assume that loops are sorted so that nested loops come before outer loops.
// We go in reverse so that inner loops override outer loops.
Dictionary<Block, Loop> surroundingLoops = [];
foreach (Loop l in loops)
for (int i = loops.Count - 1; i >= 0; i--)
{
Loop l = loops[i];
Block startBlock = blockByAddress[l.StartAddress];
Block endBlock = blockByAddress[l.EndAddress];
for (int blockIndex = startBlock.BlockIndex; blockIndex < endBlock.BlockIndex; blockIndex++)
Expand Down
44 changes: 44 additions & 0 deletions UnderanalyzerTest/DecompileContext.DecompileToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2225,4 +2225,48 @@ callv.v 1
"""
);
}

[Fact]
public void TestWithWhileIfBreak()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
push.v builtin.a
pushi.e -9
pushenv [5]

:[1]
push.v builtin.b
conv.v.b
bf [5]

:[2]
push.v builtin.c
conv.v.b
bf [4]

:[3]
b [5]

:[4]
b [1]

:[5]
popenv [1]
""",
"""
with (a)
{
while (b)
{
if (c)
{
break;
}
}
}
"""
);
}
}

0 comments on commit 5aea601

Please sign in to comment.