Debian Codegraph @

rpython/jit/metainterp/test/test_tl.py

raw symbols SCIP
1 import py
2 from rpython.jit.codewriter.policy import StopAtXPolicy
3 from rpython.jit.metainterp.test.support import LLJitMixin
4
5
6 class ToyLanguageTests:
7
8 def test_tlr(self):
9 from rpython.jit.tl.tlr import interpret, SQUARE
10
11 codes = ["", SQUARE]
12 def main(n, a):
13 code = codes[n]
14 return interpret(code, a)
15
16 res = self.meta_interp(main, [1, 10])
17 assert res == 100
18
19 def _get_main(self):
20 from rpython.jit.tl.tl import interp_without_call
21 from rpython.jit.tl.tlopcode import compile
22
23 code = compile('''
24 PUSH 1 # accumulator
25 PUSH 7 # N
26
27 start:
28 PICK 0
29 PUSH 1
30 LE
31 BR_COND exit
32
33 SWAP
34 PICK 1
35 MUL
36 SWAP
37 PUSH 1
38 SUB
39 PUSH 1
40 BR_COND start
41
42 exit:
43 POP
44 RETURN
45 ''')
46
47 code2 = compile('''
48 PUSHARG
49 start:
50 PUSH 1
51 SUB
52 PICK 0
53 PUSH 1
54 LE
55 BR_COND exit
56 PUSH 1
57 BR_COND start
58 exit:
59 RETURN
60 ''')
61
62 codes = [code, code2]
63 def main(n, inputarg):
64 code = codes[n]
65 return interp_without_call(code, inputarg=inputarg)
66 return main
67
68 def test_tl_base(self):
69 # 'backendopt=True' is used on lltype to kill unneeded access
70 # to the class, which generates spurious 'guard_class'
71 main = self._get_main()
72 res = self.meta_interp(main, [0, 6], listops=True,
73 backendopt=True)
74 assert res == 5040
75 self.check_simple_loop({'jump': 1, 'int_le': 1,
76 'int_mul': 1, 'guard_false': 1, 'int_sub': 1})
77
78 def test_tl_2(self):
79 main = self._get_main()
80 res = self.meta_interp(main, [1, 10], listops=True,
81 backendopt=True)
82 assert res == main(1, 10)
83 self.check_simple_loop({'int_le': 1, 'int_sub': 1, 'jump': 1,
84 'guard_false': 1})
85
86 def test_tl_call(self, listops=True, policy=None):
87 from rpython.jit.tl.tl import interp
88 from rpython.jit.tl.tlopcode import compile
89
90 code = compile('''
91 PUSHARG
92 start:
93 PUSH 1
94 SUB
95 PICK 0
96 PUSH 0
97 CALL func
98 POP
99 GT
100 BR_COND start
101 exit:
102 RETURN
103 func:
104 PUSH 0
105 inside:
106 PUSH 1
107 ADD
108 PICK 0
109 PUSH 5
110 LE
111 BR_COND inside
112 PUSH 1
113 RETURN
114 ''')
115 assert interp(code, inputarg=100) == 0
116 codes = [code, '']
117 def main(num, arg):
118 return interp(codes[num], inputarg=arg)
119
120 res = self.meta_interp(main, [0, 20], enable_opts='',
121 listops=listops, backendopt=True, policy=policy)
122 assert res == 0
123
124 def test_tl_call_full_of_residuals(self):
125 # This forces most methods of Stack to be ignored and treated as
126 # residual calls. It tests that the right thing occurs in this
127 # case too.
128 from rpython.jit.tl.tl import Stack
129 methods = [Stack.put,
130 Stack.pick,
131 Stack.append,
132 Stack.pop]
133 for meth in methods:
134 meth_func = meth.im_func
135 assert not hasattr(meth_func, '_jit_look_inside_')
136 meth_func._jit_look_inside_ = False
137 try:
138 self.test_tl_call(listops=False)
139 finally:
140 for meth in methods:
141 meth_func = meth.im_func
142 del meth_func._jit_look_inside_
143
144 class TestLLtype(ToyLanguageTests, LLJitMixin):
145 pass