Skip to content

Commit 86a32df

Browse files
authored
Merge pull request #607 from boriel/fix/on_goto
fix: fix on goto/gosub jump table generation
2 parents aee8044 + 9321d24 commit 86a32df

7 files changed

Lines changed: 493 additions & 9 deletions

File tree

src/arch/z80/translator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
from src.arch.z80 import backend
2626
from src.arch.z80.backend.runtime import Labels as RuntimeLabel
2727
from src.arch.z80.backend._float import _float
28-
from src.arch.z80.translatorvisitor import TranslatorVisitor
28+
from src.arch.z80.translatorvisitor import TranslatorVisitor, JumpTable
2929

3030
from src import symbols
3131
from src.symbols.type_ import Type
3232

3333
__all__ = ["Translator", "VarTranslator", "FunctionTranslator"]
3434

35-
JumpTable = namedtuple("JumpTable", ("label", "addresses"))
3635
LabelledData = namedtuple("LabelledData", ("label", "data"))
3736

3837

src/arch/z80/translatorvisitor.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22

33
from collections import OrderedDict
4+
from typing import NamedTuple, List
5+
46
from src.api.errmsg import syntax_error_not_constant
57
from src.api.errmsg import syntax_error_cant_convert_to_type
68
from src.api.debug import __DEBUG__
@@ -25,6 +27,12 @@
2527

2628
from .backend.runtime import RUNTIME_LABELS
2729
from .backend.runtime import LABEL_REQUIRED_MODULES
30+
from src.ast.tree import ChildrenList
31+
32+
33+
class JumpTable(NamedTuple):
34+
label: str
35+
addresses: ChildrenList
2836

2937

3038
class TranslatorVisitor(TranslatorInstVisitor):
@@ -48,7 +56,7 @@ class TranslatorVisitor(TranslatorInstVisitor):
4856

4957
LOOPS = [] # Defined LOOPS
5058
STRING_LABELS = OrderedDict()
51-
JUMP_TABLES = []
59+
JUMP_TABLES: List[JumpTable] = []
5260

5361
# Type code used in DATA
5462
DATA_TYPES = {"str": 1, "i8": 2, "u8": 3, "i16": 4, "u16": 5, "i32": 6, "u32": 7, "f16": 8, "f": 9}
@@ -163,11 +171,13 @@ def emit_strings(self):
163171

164172
def emit_jump_tables(self):
165173
for table_ in self.JUMP_TABLES:
166-
self.ic_vard(table_.label, [str(len(table_.addresses))] + ["##" + x.mangled for x in table_.addresses])
174+
self.ic_vard(
175+
table_.label, [f"#{str(len(table_.addresses))}"] + [f"##{x.mangled}" for x in table_.addresses]
176+
)
167177

168178
def _visit(self, node):
169179
if isinstance(node, Symbol):
170-
__DEBUG__("Visiting {}".format(node.token), 1)
180+
__DEBUG__(f"Visiting {node.token}", 1)
171181
if node.token in self.ATTR_TMP:
172182
return self.visit_ATTR_TMP(node)
173183

src/ast/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from src.api.errors import Error
1212

13-
__all__ = ["NotAnAstError", "Tree"]
13+
__all__ = ["NotAnAstError", "Tree", "ChildrenList"]
1414

1515

1616
class NotAnAstError(Error):

tests/functional/zx48k/ongosub.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ _a:
101101
DEFB 37h
102102
DEFB 30h
103103
.LABEL.__LABEL0:
104-
DEFB 4h
104+
DEFB 4
105105
DEFW .LABEL._40
106106
DEFW .LABEL._50
107107
DEFW .LABEL._60
108108
DEFW .LABEL._70
109109
.LABEL.__LABEL1:
110-
DEFB 2h
110+
DEFB 2
111111
DEFW .LABEL._50
112112
DEFW .LABEL._60
113113
;; --- end of user code ---

tests/functional/zx48k/ongoto.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ _a:
8484
DEFB 4Eh
8585
DEFB 44h
8686
.LABEL.__LABEL0:
87-
DEFB 3h
87+
DEFB 3
8888
DEFW .LABEL._10
8989
DEFW .LABEL._20
9090
DEFW .LABEL._30

0 commit comments

Comments
 (0)