File: test_tl.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (145 lines) | stat: -rw-r--r-- 4,106 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import py
from rpython.jit.codewriter.policy import StopAtXPolicy
from rpython.jit.metainterp.test.support import LLJitMixin


class ToyLanguageTests:

    def test_tlr(self):
        from rpython.jit.tl.tlr import interpret, SQUARE

        codes = ["", SQUARE]
        def main(n, a):
            code = codes[n]
            return interpret(code, a)

        res = self.meta_interp(main, [1, 10])
        assert res == 100

    def _get_main(self):
        from rpython.jit.tl.tl import interp_without_call
        from rpython.jit.tl.tlopcode import compile

        code = compile('''
                PUSH 1   #  accumulator
                PUSH 7   #  N

            start:
                PICK 0
                PUSH 1
                LE
                BR_COND exit

                SWAP
                PICK 1
                MUL
                SWAP
                PUSH 1
                SUB
                PUSH 1
                BR_COND start

            exit:
                POP
                RETURN
        ''')

        code2 = compile('''
                PUSHARG
            start:
                PUSH 1
                SUB
                PICK 0
                PUSH 1
                LE
                BR_COND exit
                PUSH 1
                BR_COND start
            exit:
                RETURN
        ''')

        codes = [code, code2]
        def main(n, inputarg):
            code = codes[n]
            return interp_without_call(code, inputarg=inputarg)
        return main

    def test_tl_base(self):
        # 'backendopt=True' is used on lltype to kill unneeded access
        # to the class, which generates spurious 'guard_class'
        main = self._get_main()
        res = self.meta_interp(main, [0, 6], listops=True,
                               backendopt=True)
        assert res == 5040
        self.check_simple_loop({'jump': 1, 'int_le': 1,
                                'int_mul': 1, 'guard_false': 1, 'int_sub': 1})

    def test_tl_2(self):
        main = self._get_main()
        res = self.meta_interp(main, [1, 10], listops=True,
                               backendopt=True)
        assert res == main(1, 10)
        self.check_simple_loop({'int_le': 1, 'int_sub': 1, 'jump': 1,
                                'guard_false': 1})

    def test_tl_call(self, listops=True, policy=None):
        from rpython.jit.tl.tl import interp
        from rpython.jit.tl.tlopcode import compile

        code = compile('''
              PUSHARG
          start:
              PUSH 1
              SUB
              PICK 0
              PUSH 0
              CALL func
              POP
              GT
              BR_COND start
          exit:
              RETURN
          func:
              PUSH 0
          inside:
              PUSH 1
              ADD
              PICK 0
              PUSH 5
              LE
              BR_COND inside
              PUSH 1
              RETURN
              ''')
        assert interp(code, inputarg=100) == 0
        codes = [code, '']
        def main(num, arg):
            return interp(codes[num], inputarg=arg)

        res = self.meta_interp(main, [0, 20], enable_opts='',
                               listops=listops, backendopt=True, policy=policy)
        assert res == 0

    def test_tl_call_full_of_residuals(self):
        # This forces most methods of Stack to be ignored and treated as
        # residual calls.  It tests that the right thing occurs in this
        # case too.
        from rpython.jit.tl.tl import Stack
        methods = [Stack.put,
                   Stack.pick,
                   Stack.append,
                   Stack.pop]
        for meth in methods:
            meth_func = meth.im_func
            assert not hasattr(meth_func, '_jit_look_inside_')
            meth_func._jit_look_inside_ = False
        try:
            self.test_tl_call(listops=False)
        finally:
            for meth in methods:
                meth_func = meth.im_func
                del meth_func._jit_look_inside_

class TestLLtype(ToyLanguageTests, LLJitMixin):
    pass