Skip to content

Commit

Permalink
Test typechecking of branch instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
iafisher committed Dec 13, 2018
1 parent 71e3635 commit 52a08de
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 8 deletions.
38 changes: 30 additions & 8 deletions hera/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ def typecheck(program):
def typecheck_one(op):
"""Type-check a single HERA op and return a list of errors encountered."""
params = _types_map.get(op.name)
# Generate parameters for branching instructions programmatically.
if params is None and op.name.upper().startswith("B"):
# TODO: This might hide an error for non-existent branching instructions, e.g. BYR.
if op.name.upper().endswith("R") and len(op.name) > 2:
params = (I8,)
else:
params = (REGISTER_OR_LABEL,)

if params is not None:
return check_types(op.name, params, op.args)
else:
Expand Down Expand Up @@ -78,6 +70,36 @@ def typecheck_one(op):
"STORE": (REGISTER, U5, REGISTER),
"CALL": (REGISTER, REGISTER_OR_LABEL),
"RETURN": (REGISTER, REGISTER_OR_LABEL),
"BR": (REGISTER_OR_LABEL,),
"BRR": (I8,),
"BL": (REGISTER_OR_LABEL,),
"BLR": (I8,),
"BGE": (REGISTER_OR_LABEL,),
"BGER": (I8,),
"BLE": (REGISTER_OR_LABEL,),
"BLER": (I8,),
"BG": (REGISTER_OR_LABEL,),
"BGR": (I8,),
"BULE": (REGISTER_OR_LABEL,),
"BULER": (I8,),
"BUG": (REGISTER_OR_LABEL,),
"BUGR": (I8,),
"BZ": (REGISTER_OR_LABEL,),
"BZR": (I8,),
"BNZ": (REGISTER_OR_LABEL,),
"BNZR": (I8,),
"BC": (REGISTER_OR_LABEL,),
"BCR": (I8,),
"BNC": (REGISTER_OR_LABEL,),
"BNCR": (I8,),
"BS": (REGISTER_OR_LABEL,),
"BSR": (I8,),
"BNS": (REGISTER_OR_LABEL,),
"BNSR": (I8,),
"BV": (REGISTER_OR_LABEL,),
"BVR": (I8,),
"BNV": (REGISTER_OR_LABEL,),
"BNVR": (I8,),
}


Expand Down
150 changes: 150 additions & 0 deletions test/test_typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,156 @@ def test_typecheck_RETURN():
assert typecheck_one(Op("RETURN", [R("R12"), SYM("f")])) == []


def test_typecheck_BR():
assert typecheck_one(Op("BR", [R("R11")])) == []
assert typecheck_one(Op("BR", [SYM("l")])) == []


def test_typecheck_BRR():
assert typecheck_one(Op("BRR", [0xFF])) == []
assert typecheck_one(Op("BRR", [-0x7F])) == []


def test_typecheck_BL():
assert typecheck_one(Op("BL", [R("R11")])) == []
assert typecheck_one(Op("BL", [SYM("l")])) == []


def test_typecheck_BLR():
assert typecheck_one(Op("BLR", [0xFF])) == []
assert typecheck_one(Op("BLR", [-0x7F])) == []


def test_typecheck_BGE():
assert typecheck_one(Op("BGE", [R("R11")])) == []
assert typecheck_one(Op("BGE", [SYM("l")])) == []


def test_typecheck_BGER():
assert typecheck_one(Op("BGER", [0xFF])) == []
assert typecheck_one(Op("BGER", [-0x7F])) == []


def test_typecheck_BLE():
assert typecheck_one(Op("BLE", [R("R11")])) == []
assert typecheck_one(Op("BLE", [SYM("l")])) == []


def test_typecheck_BLER():
assert typecheck_one(Op("BLER", [0xFF])) == []
assert typecheck_one(Op("BLER", [-0x7F])) == []


def test_typecheck_BG():
assert typecheck_one(Op("BG", [R("R11")])) == []
assert typecheck_one(Op("BG", [SYM("l")])) == []


def test_typecheck_BGR():
assert typecheck_one(Op("BGR", [0xFF])) == []
assert typecheck_one(Op("BGR", [-0x7F])) == []


def test_typecheck_BULE():
assert typecheck_one(Op("BULE", [R("R11")])) == []
assert typecheck_one(Op("BULE", [SYM("l")])) == []


def test_typecheck_BULER():
assert typecheck_one(Op("BULER", [0xFF])) == []
assert typecheck_one(Op("BULER", [-0x7F])) == []


def test_typecheck_BUG():
assert typecheck_one(Op("BUG", [R("R11")])) == []
assert typecheck_one(Op("BUG", [SYM("l")])) == []


def test_typecheck_BUGR():
assert typecheck_one(Op("BUGR", [0xFF])) == []
assert typecheck_one(Op("BUGR", [-0x7F])) == []


def test_typecheck_BZ():
assert typecheck_one(Op("BZ", [R("R11")])) == []
assert typecheck_one(Op("BZ", [SYM("l")])) == []


def test_typecheck_BZR():
assert typecheck_one(Op("BZR", [0xFF])) == []
assert typecheck_one(Op("BZR", [-0x7F])) == []


def test_typecheck_BNZ():
assert typecheck_one(Op("BNZ", [R("R11")])) == []
assert typecheck_one(Op("BNZ", [SYM("l")])) == []


def test_typecheck_BNZR():
assert typecheck_one(Op("BNZR", [0xFF])) == []
assert typecheck_one(Op("BNZR", [-0x7F])) == []


def test_typecheck_BC():
assert typecheck_one(Op("BC", [R("R11")])) == []
assert typecheck_one(Op("BC", [SYM("l")])) == []


def test_typecheck_BCR():
assert typecheck_one(Op("BCR", [0xFF])) == []
assert typecheck_one(Op("BCR", [-0x7F])) == []


def test_typecheck_BNC():
assert typecheck_one(Op("BNC", [R("R11")])) == []
assert typecheck_one(Op("BNC", [SYM("l")])) == []


def test_typecheck_BNCR():
assert typecheck_one(Op("BNCR", [0xFF])) == []
assert typecheck_one(Op("BNCR", [-0x7F])) == []


def test_typecheck_BS():
assert typecheck_one(Op("BS", [R("R11")])) == []
assert typecheck_one(Op("BS", [SYM("l")])) == []


def test_typecheck_BSR():
assert typecheck_one(Op("BSR", [0xFF])) == []
assert typecheck_one(Op("BSR", [-0x7F])) == []


def test_typecheck_BNS():
assert typecheck_one(Op("BNS", [R("R11")])) == []
assert typecheck_one(Op("BNS", [SYM("l")])) == []


def test_typecheck_BNSR():
assert typecheck_one(Op("BNSR", [0xFF])) == []
assert typecheck_one(Op("BNSR", [-0x7F])) == []


def test_typecheck_BV():
assert typecheck_one(Op("BV", [R("R11")])) == []
assert typecheck_one(Op("BV", [SYM("l")])) == []


def test_typecheck_BVR():
assert typecheck_one(Op("BVR", [0xFF])) == []
assert typecheck_one(Op("BVR", [-0x7F])) == []


def test_typecheck_BNV():
assert typecheck_one(Op("BNV", [R("R11")])) == []
assert typecheck_one(Op("BNV", [SYM("l")])) == []


def test_typecheck_BNVR():
assert typecheck_one(Op("BNVR", [0xFF])) == []
assert typecheck_one(Op("BNVR", [-0x7F])) == []


def test_typecheck_single_error():
# Second argument to SETHI is out of range.
program = [
Expand Down

0 comments on commit 52a08de

Please sign in to comment.