Skip to content

Commit

Permalink
Emit more informative errors for direct use of program counter
Browse files Browse the repository at this point in the history
Resolves #43
  • Loading branch information
iafisher committed Dec 24, 2018
1 parent 504f5d9 commit 3549a84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
16 changes: 12 additions & 4 deletions hera/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,18 @@ def check_types(name, expected, got, symtab):

ordinals = ["first", "second", "third"]
for ordinal, pattern, arg in zip(ordinals, expected, got):
prefix = "{} arg to {} ".format(ordinal, name)
error = check_one_type(pattern, arg, symtab)
if error:
emit_error(prefix + error, line=arg.line, column=arg.column)
if isinstance(arg, Token) and arg.type == "REGISTER" and arg.lower() == "pc":
# TODO: This error-handling logic is a little messy.
emit_error(
"program counter cannot be accessed or changed directly",
line=arg.line,
column=arg.column,
)
else:
prefix = "{} arg to {} ".format(ordinal, name)
error = check_one_type(pattern, arg, symtab)
if error:
emit_error(prefix + error, line=arg.line, column=arg.column)


def check_one_type(pattern, arg, symtab):
Expand Down
2 changes: 1 addition & 1 deletion hera/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def to_u32(n):
return n


NAMED_REGISTERS = {"rt": 11, "fp": 14, "sp": 15, "pc_ret": 13, "fp_alt": 12}
NAMED_REGISTERS = {"rt": 11, "fp": 14, "sp": 15, "pc_ret": 13, "fp_alt": 12, "pc": -1}


def register_to_index(rname):
Expand Down
10 changes: 10 additions & 0 deletions test/test_typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ def test_check_types_with_wrong_type():
assert "not an integer" in mock_emit_error.call_args[0][0]


def test_check_types_with_program_counter():
with patch("hera.utils._emit_msg") as mock_emit_error:
check_types(SYM(), [REGISTER], [R("PC")], {})
assert mock_emit_error.call_count == 1
assert (
"program counter cannot be accessed or changed directly"
in mock_emit_error.call_args[0][0]
)


def test_check_one_type_with_u16_out_of_range():
assert check_one_type(U16, IntToken(65536), {}) == "out of range"

Expand Down
2 changes: 2 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def test_register_to_index_with_named_registers():
assert register_to_index("pc_ret") == 13
assert register_to_index("FP_alt") == 12
assert register_to_index("fp_alt") == 12
# Make sure program counter does not correspond to actual register.
assert not (0 <= register_to_index("pc") <= 15)


def test_register_to_index_with_invalid_register():
Expand Down

0 comments on commit 3549a84

Please sign in to comment.