UnderminersTeam / UndertaleModTool

The most complete tool for modding, decompiling and unpacking Undertale (and other GameMaker games!)
GNU General Public License v3.0
1.14k stars 217 forks source link

decompiler regression with GML SWITCH instruction #1059

Open themabus opened 2 years ago

themabus commented 2 years ago

Describe the bug

bitwise operation in Switch statement causes decompiler exception. happens in 674c5d8 and 0.4.0.4, was fine in 0.3.5.8

switch(x>>y) {
    case 0: x=1; break;
    case 1: x=2; break;
    case 2: x=3; break;
    default: x=10;
}
/* EXCEPTION!
   System.InvalidOperationException: The stack got misaligned? Error 2: Attempted to duplicate 16 bytes, only found 8
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block, List`1 tempvars, Stack`1 workQueue) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2052
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2618
   at UndertaleModLib.Decompiler.Decompiler.Decompile(UndertaleCode code, GlobalDecompileContext globalContext) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 3769
   at UndertaleModTool.UndertaleCodeEditor.<>c__DisplayClass31_1.<DecompileCode>b__1() in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModTool\Editors\UndertaleCodeEditor.xaml.cs:line 456
*/

increment operation in Switch statement causes different kind of decompiler exception. happens in 674c5d8 and 0.4.0.4, was fine in 0.3.5.8

switch(x++) {
    case 0: x=1; break;
    case 1: x=10; break;
    case 2: x=100; break;
    default: x=0;
}
/* EXCEPTION!
   System.InvalidOperationException: Stack empty.
   at System.Collections.Generic.Stack`1.ThrowForEmptyStack()
   at System.Collections.Generic.Stack`1.Pop()
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block, List`1 tempvars, Stack`1 workQueue) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2031
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2618
   at UndertaleModLib.Decompiler.Decompiler.Decompile(UndertaleCode code, GlobalDecompileContext globalContext) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 3769
   at UndertaleModTool.UndertaleCodeEditor.<>c__DisplayClass31_1.<DecompileCode>b__1() in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModTool\Editors\UndertaleCodeEditor.xaml.cs:line 456
*/

Reproducing steps

decompile script0/script1 from attached data.win bug_stack.zip

Setup Details

674c5d8 GMS 1.4.9999 Win10 x64

themabus commented 2 years ago
switch (x > 0) {
    case 0:
        y++;
    break;
}

like above - this can be decompiled on v0.3.0 - v0.3.5.8, but not on v0.4.0.4 or BleedingEdge:

/* EXCEPTION!
   System.NotImplementedException: Unknown size for data type Boolean
   at UndertaleModLib.Decompiler.Decompiler.GetTypeSize(DataType type) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 1919
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block, List`1 tempvars, Stack`1 workQueue) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2028
   at UndertaleModLib.Decompiler.Decompiler.DecompileFromBlock(DecompileContext context, Dictionary`2 blocks, Block block) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 2618
   at UndertaleModLib.Decompiler.Decompiler.Decompile(UndertaleCode code, GlobalDecompileContext globalContext) in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModLib\Decompiler\Decompiler.cs:line 3769
   at UndertaleModTool.UndertaleCodeEditor.<>c__DisplayClass31_1.<DecompileCode>b__1() in D:\a\UndertaleModTool\UndertaleModTool\UndertaleModTool\Editors\UndertaleCodeEditor.xaml.cs:line 456
*/
Reycko commented 1 year ago

i hope this gets fixed sometime soon