| 1 | import pytest |
| 2 | try: |
| 3 | import rply |
| 4 | import z3 |
| 5 | except ImportError: |
| 6 | pytest.skip('rply or z3 not installed') |
| 7 | |
| 8 | from rpython.rlib.rarithmetic import LONG_BIT, r_uint, intmask, ovfcheck, uint_mul_high |
| 9 | |
| 10 | from rpython.jit.metainterp.ruleopt.codegen import * |
| 11 | from rpython.jit.metainterp.ruleopt.parse import * |
| 12 | |
| 13 | import os |
| 14 | with open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "real.rules")) as f: |
| 15 | ALLRULES = f.read() |
| 16 | |
| 17 | |
| 18 | def test_generate_commutative_rules(): |
| 19 | s = """\ |
| 20 | add_zero: int_add(x, 0) |
| 21 | => x |
| 22 | """ |
| 23 | ast = parse(s) |
| 24 | patterns = list(generate_commutative_patterns(ast.rules[0].pattern)) |
| 25 | assert str(patterns[0]) == "int_add(x, 0)" |
| 26 | assert str(patterns[1]) == "int_add(0, x)" |
| 27 | assert len(patterns) == 2 |
| 28 | |
| 29 | s = """\ |
| 30 | add_reassoc_consts: int_add(int_add(x, C1), C2) |
| 31 | C = C1 + C2 |
| 32 | => int_add(x, C) |
| 33 | """ |
| 34 | ast = parse(s) |
| 35 | patterns = list(generate_commutative_patterns(ast.rules[0].pattern)) |
| 36 | assert [str(p) for p in patterns] == [ |
| 37 | 'int_add(int_add(x, C1), C2)', |
| 38 | 'int_add(C2, int_add(x, C1))', |
| 39 | 'int_add(int_add(C1, x), C2)', |
| 40 | 'int_add(C2, int_add(C1, x))' |
| 41 | ] |
| 42 | |
| 43 | def test_generate_commutative_rules_only_when_necessary(): |
| 44 | s = """\ |
| 45 | or_x_x: int_or(x, x) |
| 46 | => x |
| 47 | """ |
| 48 | ast = parse(s) |
| 49 | patterns = list(generate_commutative_patterns(ast.rules[0].pattern)) |
| 50 | assert str(patterns[0]) == "int_or(x, x)" |
| 51 | assert len(patterns) == 1 |
| 52 | |
| 53 | def test_sort_patterns(): |
| 54 | s = """\ |
| 55 | int_sub_zero: int_sub(x, 0) |
| 56 | => x |
| 57 | int_sub_x_x: int_sub(x, x) |
| 58 | => 0 |
| 59 | int_sub_add: int_sub(int_add(x, y), y) |
| 60 | => x |
| 61 | int_sub_zero_neg: int_sub(0, x) |
| 62 | => int_neg(x) |
| 63 | """ |
| 64 | ast = parse(s) |
| 65 | rules = sort_rules(ast.rules) |
| 66 | assert [r.name for r in rules] == ['int_sub_zero', 'int_sub_zero_neg', 'int_sub_add', 'int_sub_x_x'] |
| 67 | |
| 68 | def test_create_matcher(): |
| 69 | s = """\ |
| 70 | sub_from_zero: int_sub(0, x) |
| 71 | => int_neg(x) |
| 72 | |
| 73 | sub_add_consts: int_sub(int_add(x, C1), C2) |
| 74 | C = C2 - C1 |
| 75 | => int_sub(x, C) |
| 76 | |
| 77 | sub_add_consts: int_sub(int_add(C1, x), C2) |
| 78 | C = C2 - C1 |
| 79 | => int_sub(x, C) |
| 80 | """ |
| 81 | ast = parse(s) |
| 82 | matcher = create_matcher(ast.rules) |
| 83 | assert isinstance(matcher, IsConstMatcher) |
| 84 | assert matcher.name == "arg_0" |
| 85 | assert matcher.ifyes.rules[0].name == "sub_from_zero" |
| 86 | assert matcher.ifyes.bindings == {(('int_sub', 0),): 'arg_0', (('int_sub', 1),): 'arg_1', (('int_sub', 0), 'C'): 'C_arg_0'} |
| 87 | assert matcher.nextmatcher is None |
| 88 | ifno = matcher.ifno |
| 89 | assert isinstance(ifno, OpMatcher) |
| 90 | assert ifno.name == "arg_0" |
| 91 | |
| 92 | |
| 93 | def test_generate_code_many(): |
| 94 | codegen = Codegen() |
| 95 | res = codegen.generate_code(parse(ALLRULES)) |
| 96 | print(res) |
| 97 |