SkyTemple / ExplorerScript

ExplorerScript and SSBScript: Script languages for decompiled SSB (Pokémon Mystery Dungeon Explorers of Sky)
MIT License
16 stars 6 forks source link

[Compiler Bug] Jump annotated with label does not compile offset correctly #61

Open theCapypara opened 1 month ago

theCapypara commented 1 month ago

This code does not decompile when compiled into SSB:

def 0 {
    @foo;
    jump @foo;
    end;
}

SSB representation: 1 routine with:

0 = {SkyTempleSsbOperation} SkyTempleSsbOperation<{'offset': 10, 'op_code': Pmd2ScriptOpCode<Jump(135)>, 'params': [5]}>
1 = {SkyTempleSsbOperation} SkyTempleSsbOperation<{'offset': 14, 'op_code': Pmd2ScriptOpCode<End(112)>, 'params': []}>

This is not valid SSB.

Example code:

from skytemple_files.common.ppmdu_config.data import GAME_VERSION_EOS, GAME_REGION_US
from skytemple_files.common.ppmdu_config.xml_reader import Pmd2XmlReader
from skytemple_files.script.ssb.script_compiler import ScriptCompiler

src = """def 0 {
    @foo;
    jump @foo;
    end;
}"""

pmd2_data = Pmd2XmlReader.load_default(for_version=f"{GAME_VERSION_EOS}_{GAME_REGION_US}")
compiler = ScriptCompiler(pmd2_data)
ssb, _ = compiler.compile_explorerscript(src, "/dev/null")
out, _ = ssb.to_explorerscript()
print(out)

failing during decompilation with:

Traceback (most recent call last):
  File "/home/marco/.config/JetBrains/PyCharm2024.1/scratches/scratch_74.py", line 14, in <module>
    out, _ = ssb.to_explorerscript()
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marco/dev/skytemple/skytemple/files/skytemple_files/script/ssb/model.py", line 249, in to_explorerscript
    ).convert()
      ^^^^^^^^^
  File "/home/marco/dev/skytemple/.venv/lib/python3.12/site-packages/explorerscript/ssb_converting/ssb_decompiler.py", line 125, in convert
    grapher = SsbGraphMinimizer(self._routine_ops, not has_any_calls)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marco/dev/skytemple/.venv/lib/python3.12/site-packages/explorerscript/ssb_converting/decompiler/graph_building/graph_minimizer.py", line 93, in __init__
    self._get_edges(g, rtn, rtn_id, label_indices)
  File "/home/marco/dev/skytemple/.venv/lib/python3.12/site-packages/explorerscript/ssb_converting/decompiler/graph_building/graph_minimizer.py", line 763, in _get_edges
    self._get_edges__add_edge(g, rtn, rtn_id, label_indices, 0, already_visited)
  File "/home/marco/dev/skytemple/.venv/lib/python3.12/site-packages/explorerscript/ssb_converting/decompiler/graph_building/graph_minimizer.py", line 828, in _get_edges__add_edge
    for flow_level, nxt in self._get_edges__get_next_for(g, rtn, rtn_id, flow_level, label_indices, op_i):
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marco/dev/skytemple/.venv/lib/python3.12/site-packages/explorerscript/ssb_converting/decompiler/graph_building/graph_minimizer.py", line 874, in _get_edges__get_next_for
    next_ops.append((flow_level + 1, label_indices[op.label.id]))
                                     ~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0