Esempio n. 1
0
def test_convert_iftojump_nested():
    code = [
        p.IfOp("ez", [
            p.IfOp("ez", [p.AssignOp("inbox", "emp")], [p.OutboxOp()]),
        ], [
            p.IfOp("neg", [p.AssignOp("inbox", "emp")], [p.OutboxOp()])
        ])]
    expected_ast = [
        p.JumpCondOp(condition="jez", label_name="_hrm_1"),
          p.JumpCondOp(condition="jneg", label_name="_hrm_2"),
            p.OutboxOp(),
            p.JumpOp("_hrm_endif_2"),
          p.LabelStmt("_hrm_2"),
            p.AssignOp("inbox", "emp"),
          p.LabelStmt("_hrm_endif_2"),
       p.JumpOp("_hrm_endif_1"),
       p.LabelStmt("_hrm_1"),
          p.JumpCondOp(condition="jez", label_name="_hrm_3"),
            p.OutboxOp(),
            p.JumpOp("_hrm_endif_3"),
          p.LabelStmt("_hrm_3"),
            p.AssignOp("inbox", "emp"),
          p.LabelStmt("_hrm_endif_3"),
       p.LabelStmt("_hrm_endif_1")]
    ast = c.convert_iftojump(code)
    assert ast == expected_ast
def test_convert_jmpthenlabel_check():
    code = [
        p.LabelStmt("alreadySorted"),
        p.AssignOp(src="inbox", dst="emp"),
        p.IfOp("neg", [p.JumpOp("alreadySorted")], []),
        p.IfOp("ez", [p.JumpOp("alreadySorted")],
               [p.AssignOp(src="0", dst="emp")])
    ]
    expected_ast = [
        p.LabelStmt("alreadySorted"),
        p.AssignOp(src="inbox", dst="emp"),
        p.JumpCondOp(condition="jneg", label_name="alreadySorted"),
        # pass
        #    p.JumpOp("_hrm_endif_1"),
        # p.LabelStmt("_hrm_endif_1"),
        p.JumpCondOp(condition="jez", label_name="alreadySorted"),
        p.AssignOp(src="0", dst="emp")
        #    p.JumpOp("_hrm_unreachable"),
        # p.LabelStmt("_hrm_unreachable")
    ]
    result_ast = conversion.convert_ifnz_to_ifez(code)
    result_ast = conversion.convert_iftojump(result_ast)
    result_ast = conversion.compress_jumps(result_ast)
    result_ast = conversion.remove_unreachable_code(result_ast)

    ast = conversion.fix_jmp_then_label(result_ast)

    assert ast == expected_ast
Esempio n. 3
0
def test_convert_iftojump_nothingchanged():
    code = [
        p.AssignOp("inbox", "emp"),
        p.SubOp("0"),
        p.OutboxOp()
    ]
    expected_ast = code
    ast = c.convert_iftojump(code)
    assert ast == expected_ast
Esempio n. 4
0
def test_convert_iftojump_onlyfalsebranch():
    code = [
        p.IfOp("ez", [], [
            p.OutboxOp()
        ])]
    expected_ast = [
        p.JumpCondOp(condition="jez", label_name="_hrm_1"),
        p.OutboxOp(),
        p.JumpOp("_hrm_endif_1"),
        p.LabelStmt("_hrm_1"),
        p.LabelStmt("_hrm_endif_1")
    ]
    ast = c.convert_iftojump(code)
    assert ast == expected_ast
Esempio n. 5
0
def test_convert_iftojump_onlytruebranch_consecutive():
    code = [
        p.IfOp("ez", [p.OutboxOp()], []),
        p.IfOp("ez", [p.OutboxOp()], [])]
    expected_ast = [
        # first IF
        p.JumpCondOp(condition="jez", label_name="_hrm_1"),
        p.JumpOp("_hrm_endif_1"),
        p.LabelStmt("_hrm_1"),
        p.OutboxOp(),
        p.LabelStmt("_hrm_endif_1"),
        # second IF
        p.JumpCondOp(condition="jez", label_name="_hrm_2"),
        p.JumpOp("_hrm_endif_2"),
        p.LabelStmt("_hrm_2"),
        p.OutboxOp(),
        p.LabelStmt("_hrm_endif_2")]
    ast = c.convert_iftojump(code)
    assert ast == expected_ast
Esempio n. 6
0
def calculate_optimized_ast(fhandle, args):
    result_ast = p.parse_it(fhandle)

    checker.perform_label_checks(result_ast)
    checker.perform_variable_checks(result_ast)

    result_ast = conversion.convert_ifnz_to_ifez(result_ast)
    result_ast = conversion.convert_iftojump(result_ast)
    if not args.no_jump_compression:
        result_ast = conversion.compress_jumps(result_ast)
    if not args.no_unreachable:
        result_ast = conversion.remove_unreachable_code(result_ast)
    if not args.no_jmp_then_label:
        unchanged = False
        while not unchanged:
            new_ast = conversion.fix_jmp_then_label(result_ast)
            unchanged = new_ast == result_ast
            result_ast = new_ast

    return result_ast
Esempio n. 7
0
def test_convert_ifnojump_no_ifs_remaining():
    code = [p.IfOp("ez", [p.OutboxOp()], [p.AssignOp("inbox", "emp")])]
    ast = c.convert_iftojump(code)
    assert not [ast_item for ast_item in ast if type(ast_item) == p.IfOp]