changeset 492:6ef61d6707a1

Fix an issue where decompiler could create if blocks that couldn't exist due to if-break rewrite.
author tehtmi
date Mon, 10 Apr 2023 23:02:30 -0700
parents 20a8c7b79549
children 3bf1a16c5c25
files src/unluac/decompile/ControlFlowHandler.java src/unluac/test/TestFiles.java test/src/52_goto06.lua
diffstat 3 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/unluac/decompile/ControlFlowHandler.java	Mon Apr 10 22:08:16 2023 -0700
+++ b/src/unluac/decompile/ControlFlowHandler.java	Mon Apr 10 23:02:30 2023 -0700
@@ -779,11 +779,18 @@
     if(!stack.isEmpty() && stack_reach(state, stack) <= line) {
       Branch top = stack.pop();
       int literalEnd = state.code.target(top.targetFirst - 1);
-      block = new IfThenEndBlock(
-        state.function, state.r, top.cond, top.targetFirst, top.targetSecond,
-        get_close_type(state, top.targetSecond - 1), top.targetSecond - 1,
-        literalEnd != top.targetSecond
-      );
+      if(state.function.header.version.useifbreakrewrite.get() && top.targetFirst + 1 == top.targetSecond && is_jmp(state, top.targetFirst)) {
+        // If this were actually an if statement, it would have been rewritten. It hasn't been, so it isn't...
+        block = new IfThenEndBlock(state.function, state.r, top.cond.inverse(), top.targetFirst - 1, top.targetFirst - 1);
+        block.addStatement(new Goto(state.function, top.targetFirst - 1, top.targetSecond));
+        state.labels[top.targetSecond] = true;
+      } else {
+        block = new IfThenEndBlock(
+          state.function, state.r, top.cond, top.targetFirst, top.targetSecond,
+          get_close_type(state, top.targetSecond - 1), top.targetSecond - 1,
+          literalEnd != top.targetSecond
+        );
+      }
       state.blocks.add(block);
       remove_branch(state, top);
     }
--- a/src/unluac/test/TestFiles.java	Mon Apr 10 22:08:16 2023 -0700
+++ b/src/unluac/test/TestFiles.java	Mon Apr 10 23:02:30 2023 -0700
@@ -261,6 +261,7 @@
     new TestFile("52_goto03"),
     new TestFile("52_goto04"),
     new TestFile("52_goto05"),
+    new TestFile("52_goto06"),
     new TestFile("53_expression"),
     new TestFile("53_expression02"),
     new TestFile("54_tbc01"),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/src/52_goto06.lua	Mon Apr 10 23:02:30 2023 -0700
@@ -0,0 +1,7 @@
+for x = 1, 9 do
+  print(x)
+  if test1() then goto continue end
+  if test2() then goto continue end
+  break
+  ::continue::
+end