Skip to content

Commit

Permalink
Fix assembler output for branches + debugging operations
Browse files Browse the repository at this point in the history
Fixes: #142
  • Loading branch information
iafisher committed Jan 14, 2021
1 parent 8ff5134 commit 6fbd781
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Nothing yet!


## [1.0.6] - 2021-01-13
### Fixed
- The assembler produces correct offsets for branches in the presence of debugging operations.


## [1.0.5] - 2020-12-??
### Fixed
- The assembler no longer produces code for debugging instructions like `print_reg`.
Expand Down
18 changes: 14 additions & 4 deletions hera/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
Token,
)
from .op import (
LABEL,
RTI,
SWI,
AbstractOperation,
DataOperation,
DebuggingOperation,
AbstractOperation,
RegisterBranch,
RelativeBranch,
LABEL,
RTI,
SWI,
)
from .utils import out_of_range

Expand Down Expand Up @@ -65,6 +65,12 @@ def check(
if isinstance(op, DataOperation):
data.append(op)
else:
if isinstance(op, DebuggingOperation) and settings.mode in (
"assemble",
"preprocess",
):
continue

code.append(op)

return (Program(data, code, symbol_table, debug_info), messages)
Expand Down Expand Up @@ -175,6 +181,10 @@ def get_labels(
dc += op.args[0]
elif op.args[0] in constants:
dc += constants[op.args[0]]
elif settings.mode in ("assemble", "preprocess") and isinstance(
op, DebuggingOperation
):
continue
else:
pc += operation_length(op)

Expand Down
4 changes: 4 additions & 0 deletions test/assets/asm/branch_label.hera
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BR(l)
print("Hello, world") // This should not affect the branch offset.
ADD(R0, R0, R0)
LABEL(l)
4 changes: 4 additions & 0 deletions test/assets/asm/branch_label.hera.lcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eb04
fb00
100b
a000
12 changes: 11 additions & 1 deletion test/test_assembler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest.mock import patch
from io import StringIO
from unittest.mock import patch

from hera.main import main

Expand Down Expand Up @@ -34,6 +34,16 @@ def test_assemble_branch(capsys):
assert captured.out == f.read()


def test_assemble_labelled_branch(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/branch_label.hera"])

captured = capsys.readouterr()
assert captured.err == ""

with open("test/assets/asm/branch_label.hera.lcode") as f:
assert captured.out == f.read()


def test_assemble_relative_branch(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/rel_branch.hera"])

Expand Down

0 comments on commit 6fbd781

Please sign in to comment.