Skip to content

Commit d5e7714

Browse files
authored
Merge pull request #1069 from boriel-basic/fix/local_array_ubound_check
fix: Correctly initalize LBOUND and UBOUND pts for local arrays
2 parents 65b3c9a + 5ecc454 commit d5e7714

4 files changed

Lines changed: 944 additions & 9 deletions

File tree

src/arch/z80/visitor/function_translator.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,19 @@ def visit_FUNCTION(self, node):
6969
lbound_label = local_var.mangled + ".__LBOUND__"
7070
ubound_label = local_var.mangled + ".__UBOUND__"
7171
lbound_needed = not local_var.is_zero_based and (
72-
local_var.is_dynamically_accessed or local_var.lbound_used
72+
local_var.is_dynamically_accessed or local_var.lbound_used or OPTIONS.array_check
7373
)
74+
ubound_needed = local_var.ubound_used or OPTIONS.array_check
75+
bound_ptrs = [lbound_label if lbound_needed else "0", ubound_label if ubound_needed else "0"]
7476

75-
bound_ptrs = [lbound_label if lbound_needed else "0", "0"]
76-
if local_var.ubound_used:
77-
bound_ptrs[1] = ubound_label
78-
79-
if bound_ptrs != ["0", "0"]:
77+
if lbound_needed or ubound_needed:
8078
OPTIONS["__DEFINES"].value["__ZXB_USE_LOCAL_ARRAY_WITH_BOUNDS__"] = ""
8179

8280
if lbound_needed:
8381
l = ["%04X" % bound.lower for bound in local_var.bounds]
8482
bound_tables.append(LabelledData(lbound_label, l))
8583

86-
if local_var.ubound_used:
84+
if ubound_needed:
8785
l = ["%04X" % bound.upper for bound in local_var.bounds]
8886
bound_tables.append(LabelledData(ubound_label, l))
8987

src/arch/z80/visitor/translator_visitor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ def runtime_call(self, label: str, num: int):
128128
if label in LABEL_REQUIRED_MODULES:
129129
backend.REQUIRES.add(LABEL_REQUIRED_MODULES[label])
130130

131-
# This function must be called before emit_strings
132131
def emit_data_blocks(self):
132+
"""Emits the DATA instruction blocks used by READ.
133+
This function must be called before emit_strings() because it will emit access to string variables,
134+
marking them as required.
135+
"""
133136
if not gl.DATA_IS_USED or not gl.DATAS:
134137
return # nothing to do
135138

@@ -254,10 +257,12 @@ def traverse_const(node):
254257
raise InvalidCONSTexpr(node)
255258

256259
@staticmethod
257-
def check_attr(node, n):
260+
def check_attr(node: Symbol, n: int) -> Symbol | None:
258261
"""Check if ATTR has to be normalized
259262
after this instruction has been translated
260263
to intermediate code.
261264
"""
262265
if len(node.children) > n:
263266
return node.children[n]
267+
268+
return None

0 commit comments

Comments
 (0)