Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#585 from boriel/bugfix/relative_jump_…
Browse files Browse the repository at this point in the history
…to_tmp_label

fix: fix temporary labels not correctly resolved
  • Loading branch information
boriel committed Nov 11, 2021
2 parents cc6eaa8 + 4a18a9d commit 450da44
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/zxbasm/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Memory:
MAX_MEM = 65535 # Max memory limit
_tmp_labels: Dict[int, Dict[str, Label]]
_tmp_labels_lines: List[int]
_tmp_pending_labels: List[Label]

def __init__(self, org: int = 0):
"""Initializes the origin of code.
Expand Down Expand Up @@ -137,6 +138,7 @@ def resolve_temporary_label(self, label: Label):
def clear_temporary_labels(self):
self._tmp_labels_lines = []
self._tmp_labels = defaultdict(dict)
self._tmp_pending_labels = []

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

for label in self.global_labels.values():
if label.is_temporary:
self.resolve_temporary_label(label)
for label in self._tmp_pending_labels:
self.resolve_temporary_label(label)
if not label.defined:
error(label.lineno, "Undefined temporary label '%s'" % label.name)

for label in self.global_labels.values():
if not label.defined:
label_type = "temporary" if label.is_temporary else "GLOBAL"
error(label.lineno, f"Undefined {label_type} label '%s'" % label.name)
error(label.lineno, "Undefined GLOBAL label '%s'" % label.name)

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

ex_label, namespace = Memory.id_name(label)
result = Label(ex_label, lineno, namespace=namespace)
if result.is_temporary:
self._tmp_pending_labels.append(result)
return result

for local_label in self.local_labels[::-1]:
result = local_label.get(ex_label)
if result is not None:
return result
lbl = local_label.get(ex_label)
if lbl is not None:
return lbl

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

return result
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/jp1f_bad.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
jp 1f
nop
1:
jp 1f
nop
1:
Binary file added tests/functional/jp1f_bad.bin
Binary file not shown.
Binary file modified tests/functional/tmp_label4.bin
Binary file not shown.

0 comments on commit 450da44

Please sign in to comment.