Skip to content

Commit 720c784

Browse files
OliveIsAWordXDOneDude
authored andcommitted
Decompiler removes redundant ifs and withs
1 parent f51e55e commit 720c784

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

UndertaleModLib/Decompiler/Decompiler.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,7 +3635,14 @@ public static Block FindFirstMeetPoint(Block ifStart, Dictionary<Block, List<Blo
36353635
}
36363636

36373637
// Process the base decompilation: clean up, make it readable, identify structures
3638-
private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref Block block, Dictionary<uint, Block> blocks, Dictionary<Block, List<Block>> loops, Dictionary<Block, List<Block>> reverseDominators, List<Block> alreadyVisited, Block currentLoop = null, Block stopAt = null, Block breakTo = null, bool decompileTheLoop = false, uint depth = 0)
3638+
private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref Block block,
3639+
Dictionary<uint, Block> blocks,
3640+
Dictionary<Block, List<Block>> loops,
3641+
Dictionary<Block, List<Block>> reverseDominators,
3642+
List<Block> alreadyVisited, Block currentLoop = null,
3643+
string currentEnv = null,
3644+
Block stopAt = null, Block breakTo = null,
3645+
bool decompileTheLoop = false, uint depth = 0)
36393646
{
36403647
if (depth > 200)
36413648
throw new Exception("Excessive recursion while processing blocks.");
@@ -3657,7 +3664,7 @@ private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref
36573664
}
36583665
else
36593666
{
3660-
LoopHLStatement statement = new LoopHLStatement() { Block = HLDecompileBlocks(context, ref block, blocks, loops, reverseDominators, alreadyVisited, block, null, block.nextBlockFalse, true, depth + 1) };
3667+
LoopHLStatement statement = new LoopHLStatement() { Block = HLDecompileBlocks(context, ref block, blocks, loops, reverseDominators, alreadyVisited, block, currentEnv, null, block.nextBlockFalse, true, depth + 1) };
36613668
output.Statements.Add(statement);
36623669
continue;
36633670
}
@@ -3754,7 +3761,7 @@ private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref
37543761

37553762
Block switchEnd = context.GlobalContext.Data.GeneralInfo.BytecodeVersion == 17 ? meetPoint : DetermineSwitchEnd(temp, caseEntries.Count > (i + 1) ? caseEntries.ElementAt(i + 1).Key : null, meetPoint);
37563763

3757-
HLSwitchCaseStatement result = new HLSwitchCaseStatement(x.Value, HLDecompileBlocks(context, ref temp, blocks, loops, reverseDominators, alreadyVisited, currentLoop, switchEnd, switchEnd, false, depth + 1));
3764+
HLSwitchCaseStatement result = new HLSwitchCaseStatement(x.Value, HLDecompileBlocks(context, ref temp, blocks, loops, reverseDominators, alreadyVisited, currentLoop, currentEnv, switchEnd, switchEnd, false, depth + 1));
37583765
cases.Add(result);
37593766
if (result.CaseExpressions.Contains(null))
37603767
defaultCase = result;
@@ -3777,7 +3784,7 @@ private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref
37773784
Block switchEnd = blocks[instructionId];
37783785

37793786
Block start = meetPoint;
3780-
defaultCase.Block = HLDecompileBlocks(context, ref start, blocks, loops, reverseDominators, alreadyVisited, currentLoop, switchEnd, switchEnd, false, depth + 1);
3787+
defaultCase.Block = HLDecompileBlocks(context, ref start, blocks, loops, reverseDominators, alreadyVisited, currentLoop, currentEnv, switchEnd, switchEnd, false, depth + 1);
37813788
block = start; // Start changed in HLDecompileBlocks.
37823789
}
37833790
else
@@ -3809,11 +3816,22 @@ private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref
38093816
DebugUtil.Assert(!block.conditionalExit, "Block ending with pushenv does not have a conditional exit");
38103817
PushEnvStatement stmt = (block.Statements.Last() as PushEnvStatement);
38113818
block = block.nextBlockTrue;
3812-
output.Statements.Add(new WithHLStatement()
3813-
{
3814-
NewEnv = stmt.NewEnv,
3815-
Block = HLDecompileBlocks(context, ref block, blocks, loops, reverseDominators, alreadyVisited, null, stopAt, null, false, depth + 1)
3816-
});
3819+
string envCheck = stmt.ToString();
3820+
BlockHLStatement with_stmt = HLDecompileBlocks(context, ref block, blocks, loops, reverseDominators, alreadyVisited, null, envCheck, stopAt, null, false, depth + 1);
3821+
if (!Equals(currentEnv, envCheck)) {
3822+
output.Statements.Add(new WithHLStatement()
3823+
{
3824+
NewEnv = stmt.NewEnv,
3825+
Block = with_stmt,
3826+
});
3827+
} else {
3828+
if (with_stmt is BlockHLStatement with_block) {
3829+
foreach (Statement s in with_block.Statements)
3830+
output.Statements.Add(s);
3831+
} else {
3832+
output.Statements.Add(with_stmt);
3833+
}
3834+
}
38173835
if (block == null)
38183836
break;
38193837
}
@@ -3836,9 +3854,10 @@ private static BlockHLStatement HLDecompileBlocks(DecompileContext context, ref
38363854
cond.condition = block.ConditionStatement;
38373855

38383856
Block blTrue = block.nextBlockTrue, blFalse = block.nextBlockFalse;
3839-
cond.trueBlock = HLDecompileBlocks(context, ref blTrue, blocks, loops, reverseDominators, alreadyVisited, currentLoop, meetPoint, breakTo, false, depth + 1);
3840-
cond.falseBlock = HLDecompileBlocks(context, ref blFalse, blocks, loops, reverseDominators, alreadyVisited, currentLoop, meetPoint, breakTo, false, depth + 1);
3841-
output.Statements.Add(cond); // Add the if statement.
3857+
cond.trueBlock = HLDecompileBlocks(context, ref blTrue, blocks, loops, reverseDominators, alreadyVisited, currentLoop, currentEnv, meetPoint, breakTo, false, depth + 1);
3858+
cond.falseBlock = HLDecompileBlocks(context, ref blFalse, blocks, loops, reverseDominators, alreadyVisited, currentLoop, currentEnv, meetPoint, breakTo, false, depth + 1);
3859+
if (!cond.condition.IsDuplicationSafe() || cond.trueBlock.Statements.Count != 0 || cond.falseBlock.Statements.Count != 0)
3860+
output.Statements.Add(cond); // Add the if statement.
38423861
block = meetPoint;
38433862
}
38443863
else

0 commit comments

Comments
 (0)