Skip to content

Commit 4a18a9d

Browse files
committed
fix: fix temporary labels not correctly resolved
1 parent cc6eaa8 commit 4a18a9d

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

src/zxbasm/memory.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Memory:
1919
MAX_MEM = 65535 # Max memory limit
2020
_tmp_labels: Dict[int, Dict[str, Label]]
2121
_tmp_labels_lines: List[int]
22+
_tmp_pending_labels: List[Label]
2223

2324
def __init__(self, org: int = 0):
2425
"""Initializes the origin of code.
@@ -137,6 +138,7 @@ def resolve_temporary_label(self, label: Label):
137138
def clear_temporary_labels(self):
138139
self._tmp_labels_lines = []
139140
self._tmp_labels = defaultdict(dict)
141+
self._tmp_pending_labels = []
140142

141143
def add_instruction(self, instr: Asm):
142144
"""This will insert an asm instruction at the current memory position
@@ -160,13 +162,14 @@ def dump(self):
160162
OUTPUT = []
161163
align = []
162164

163-
for label in self.global_labels.values():
164-
if label.is_temporary:
165-
self.resolve_temporary_label(label)
165+
for label in self._tmp_pending_labels:
166+
self.resolve_temporary_label(label)
167+
if not label.defined:
168+
error(label.lineno, "Undefined temporary label '%s'" % label.name)
166169

170+
for label in self.global_labels.values():
167171
if not label.defined:
168-
label_type = "temporary" if label.is_temporary else "GLOBAL"
169-
error(label.lineno, f"Undefined {label_type} label '%s'" % label.name)
172+
error(label.lineno, "Undefined GLOBAL label '%s'" % label.name)
170173

171174
for i in range(org, max(self.memory_bytes.keys()) + 1):
172175
if gl.has_errors:
@@ -241,13 +244,16 @@ def get_label(self, label: str, lineno: int) -> Label:
241244
"""
242245

243246
ex_label, namespace = Memory.id_name(label)
247+
result = Label(ex_label, lineno, namespace=namespace)
248+
if result.is_temporary:
249+
self._tmp_pending_labels.append(result)
250+
return result
244251

245252
for local_label in self.local_labels[::-1]:
246-
result = local_label.get(ex_label)
247-
if result is not None:
248-
return result
253+
lbl = local_label.get(ex_label)
254+
if lbl is not None:
255+
return lbl
249256

250-
result = Label(ex_label, lineno, namespace=namespace)
251257
self.local_labels[-1][ex_label] = result # HINT: no namespace
252258

253259
return result

tests/functional/jp1f_bad.asm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
jp 1f
2+
nop
3+
1:
4+
jp 1f
5+
nop
6+
1:

tests/functional/jp1f_bad.bin

8 Bytes
Binary file not shown.

tests/functional/tmp_label4.bin

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)