Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
theCapypara opened this issue Jul 12, 2024 · 0 comments

Comments

@theCapypara
Copy link
Member

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant