Example #1
0
def test_convert_CALL():
    # Simulates CALL(R12, l) where l = 1000
    ops = helper("CALL(R12, 1000)")
    assert ops == [
        SETLO(Token.R(13), Token.Int(232)),
        SETHI(Token.R(13), Token.Int(3)),
        CALL(Token.R(12), Token.R(13)),
    ]
Example #2
0
def test_convert_register_branch_with_integer():
    # Simulates BR(l) where l = 1000
    ops = helper("BR(1000)")
    assert ops == [
        SETLO(Token.R(11), Token.Int(232)),
        SETHI(Token.R(11), Token.Int(3)),
        BR(Token.R(11)),
    ]
Example #3
0
def test_convert_ops_with_constant():
    oplist, messages = convert_ops([SET(Token.R(1), Token.Sym("n"))], {"n": 100})

    assert len(messages.errors) == 0
    assert oplist == [
        SETLO(Token.R(1), Token.Int(100)),
        SETHI(Token.R(1), Token.Int(0)),
    ]
Example #4
0
def test_typecheck_single_error():
    # Second argument to SETHI is out of range.
    program = [
        SETLO(Token.R(1), Token.Int(10)),
        SETHI(Token.R(1), Token.Int(1000))
    ]
    symbol_table, messages = typecheck(program)

    assert len(messages.errors) == 1
    assert "integer must be in range [-128, 256)" in messages.errors[0][0]
Example #5
0
def test_typecheck_multiple_errors():
    program = [
        ADD(Token.R(1), Token.Int(10)),
        INC(Token.R(3), Token.Int(1), Token.Int(2)),
    ]
    symbol_table, messages = typecheck(program)

    assert len(messages.errors) == 3

    assert "ADD" in messages.errors[0][0]
    assert "too few" in messages.errors[0][0]

    assert "expected register" in messages.errors[1][0]

    assert "INC" in messages.errors[2][0]
    assert "too many" in messages.errors[2][0]
Example #6
0
def test_operation_length_of_CALL_with_register():
    assert operation_length(CALL(Token.R(12), Token.R(13))) == 1
Example #7
0
def test_operation_lentgh_of_FLAGS():
    assert operation_length(FLAGS(Token.R(3))) == 2
Example #8
0
def test_operation_length_of_CALL_with_label():
    assert operation_length(CALL(Token.R(12), Token.Sym("l"))) == 3
Example #9
0
def test_operation_length_of_NEG():
    assert operation_length(NEG(Token.R(7), Token.R(15))) == 2
Example #10
0
def test_operation_length_of_NOT():
    assert operation_length(NOT(Token.R(5), Token.R(7))) == 3
Example #11
0
def test_operation_length_of_MOVE():
    assert operation_length(MOVE(Token.R(1), Token.R(2))) == 1
Example #12
0
def test_operation_length_of_CMP():
    assert operation_length(CMP(Token.R(1), Token.R(0))) == 2
Example #13
0
def test_convert_register_branch_with_register():
    ops = helper("BR(R1)")
    assert ops == [BR(Token.R(1))]
Example #14
0
def test_operation_length_of_SETRF():
    assert operation_length(SETRF(Token.R(1), Token.Int(10))) == 4
Example #15
0
def test_substitute_label_with_SETHI():
    program = SETHI(Token.R(1), Token.Sym("N"))
    labels = {"N": 10}
    assert substitute_label(program, labels) == SETHI(Token.R(1),
                                                      Token.Int(10))
Example #16
0
def test_convert_SET():
    ops = helper("SET(R1, 0b100010101010)")
    assert ops == [
        SETLO(Token.R(1), Token.Int(0b10101010)),
        SETHI(Token.R(1), Token.Int(0b1000)),
    ]
Example #17
0
def test_convert_non_pseudo_op():
    ops = helper("ADD(R1, R2, R3)")
    assert ops == [ADD(Token.R(1), Token.R(2), Token.R(3))]
Example #18
0
def test_convert_FLAGS():
    ops = helper("FLAGS(R14)")
    assert ops == [FOFF(Token.Int(8)), ADD(Token.R(0), Token.R(14), Token.R(0))]
Example #19
0
def test_convert_CMP():
    ops = helper("CMP(R5, R6)")
    assert ops == [FON(Token.Int(8)), SUB(Token.R(0), Token.R(5), Token.R(6))]
Example #20
0
def test_substitute_label_with_other_op():
    program = INC(Token.R(1), Token.Sym("N"))
    labels = {"N": 10}
    assert substitute_label(program, labels) == INC(Token.R(1), Token.Int(10))
Example #21
0
def test_operation_length_of_register_branch_with_register():
    assert operation_length(BNZ(Token.R(1))) == 1