Debian Codegraph @

rpython/jit/tl/test/test_tl.py

raw symbols SCIP
1 import py
2 import operator
3 from rpython.jit.tl.tlopcode import *
4
5 from rpython.translator.c.test import test_boehm
6 from rpython.annotator import policy
7
8 def list2bytecode(insn):
9 return ''.join([chr(i & 0xff) for i in insn])
10
11 # actual tests go here
12
13 class TestTL(test_boehm.AbstractGCTestClass):
14 from rpython.jit.tl.tl import interp
15 interp = staticmethod(interp)
16
17 def test_tl_push(self):
18 assert self.interp(list2bytecode([PUSH, 16])) == 16
19
20 def test_tl_pop(self):
21 assert self.interp( list2bytecode([PUSH,16, PUSH,42, PUSH,100, POP]) ) == 42
22
23 def test_tl_add(self):
24 assert self.interp( list2bytecode([PUSH,42, PUSH,100, ADD]) ) == 142
25 assert self.interp( list2bytecode([PUSH,16, PUSH,42, PUSH,100, ADD]) ) == 142
26
27 def test_tl_error(self):
28 py.test.raises(IndexError, self.interp, list2bytecode([POP]))
29 py.test.raises(IndexError, self.interp, list2bytecode([ADD]))
30 py.test.raises(IndexError, self.interp, list2bytecode([PUSH,100, ADD]))
31
32 def test_tl_invalid_codetype(self):
33 py.test.raises(TypeError, self.interp,[INVALID])
34
35 def test_tl_invalid_bytecode(self):
36 py.test.raises(RuntimeError, self.interp, list2bytecode([INVALID]))
37
38 def test_tl_translatable(self):
39 code = list2bytecode([PUSH,42, PUSH,100, ADD])
40 fn = self.getcompiled(self.interp, [str, int, int])
41 assert self.interp(code, 0, 0) == fn(code, 0, 0)
42
43 def test_swap(self):
44 code = [PUSH,42, PUSH, 84]
45 assert self.interp(list2bytecode(code)) == 84
46 code.append(SWAP)
47 assert self.interp(list2bytecode(code)) == 42
48 code.append(POP)
49 assert self.interp(list2bytecode(code)) == 84
50
51 def test_pick(self):
52 values = [7, 8, 9]
53 code = []
54 for v in values[::-1]:
55 code.extend([PUSH, v])
56
57 for i, v in enumerate(values):
58 assert self.interp(list2bytecode(code + [PICK,i])) == v
59
60 def test_put(self):
61 values = [1,2,7,-3]
62 code = [PUSH,0] * len(values)
63 for i, v in enumerate(values):
64 code += [PUSH,v, PUT,i]
65
66 for i, v in enumerate(values):
67 assert self.interp(list2bytecode(code + [PICK,i])) == v
68
69 ops = [ (ADD, operator.add, ((2, 4), (1, 1), (-1, 1))),
70 (SUB, operator.sub, ((2, 4), (4, 2), (1, 1))),
71 (MUL, operator.mul, ((2, 4), (4, 2), (1, 1), (-1, 6), (0, 5))),
72 (DIV, operator.div, ((2, 4), (4, 2), (1, 1), (-4, -2), (0, 9), (9, -3))),
73 (EQ, operator.eq, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
74 (NE, operator.ne, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
75 (LT, operator.lt, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
76 (LE, operator.le, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
77 (GT, operator.gt, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
78 (GE, operator.ge, ((0, 0), (0, 1), (1, 0), (1, 1), (-1, 0), (0, -1), (-1, -1), (1, -1), (-1, 1))),
79 ]
80
81 def test_ops(self):
82 for insn, pyop, values in self.ops:
83 for first, second in values:
84 code = [PUSH, first, PUSH, second, insn]
85 assert self.interp(list2bytecode(code)) == pyop(first, second)
86
87
88 def test_branch_forward(self):
89 assert self.interp(list2bytecode([PUSH,1, PUSH,0, BR_COND,2, PUSH,-1])) == -1
90 assert self.interp(list2bytecode([PUSH,1, PUSH,1, BR_COND,2, PUSH,-1])) == 1
91 assert self.interp(list2bytecode([PUSH,1, PUSH,-1, BR_COND,2, PUSH,-1])) == 1
92
93 def test_branch_backwards(self):
94 assert self.interp(list2bytecode([PUSH,0, PUSH,1, BR_COND,6, PUSH,-1, PUSH,3, BR_COND,4, PUSH,2, BR_COND,-10])) == -1
95
96 def test_branch0(self):
97 assert self.interp(list2bytecode([PUSH,7, PUSH,1, BR_COND,0])) == 7
98
99 def test_return(self):
100 assert py.test.raises(IndexError, self.interp, list2bytecode([RETURN]))
101 assert self.interp(list2bytecode([PUSH,7, RETURN, PUSH,5])) == 7
102
103 def test_rot(self):
104
105 code = [PUSH,1, PUSH,2, PUSH,3, ROLL, 3]
106 assert self.interp(list2bytecode(code)) == 1
107 assert self.interp(list2bytecode(code + [POP])) == 3
108 assert self.interp(list2bytecode(code + [POP, POP])) == 2
109
110 py.test.raises(IndexError, self.interp, list2bytecode([PUSH,1, PUSH,2, PUSH,3, ROLL,4]))
111
112 code = [PUSH,1, PUSH,2, PUSH,3, ROLL, -3]
113 assert self.interp(list2bytecode(code)) == 2
114 assert self.interp(list2bytecode(code + [POP])) == 1
115 assert self.interp(list2bytecode(code + [POP, POP])) == 3
116
117 py.test.raises(IndexError, self.interp, list2bytecode([PUSH,1, PUSH,2, PUSH,3, ROLL,-4]))
118
119 def test_call_ret(self):
120 assert self.interp(list2bytecode([CALL,1, RETURN, PUSH,2])) == 2
121 assert self.interp(list2bytecode([PUSH,6, CALL,2, MUL, RETURN, PUSH,7, RETURN])) == 42
122
123 def test_compile_branch_backwards(self):
124 code = compile("""
125 main:
126 PUSH 0
127 PUSH 1
128 BR_COND somename
129 label1:
130 PUSH -1
131 PUSH 3
132 BR_COND end
133 somename: ;
134 PUSH 2 //
135 BR_COND label1//
136 end:// comment
137 //
138 //
139 //comment
140 """)
141 assert code == list2bytecode([PUSH,0, PUSH,1, BR_COND,6, PUSH,-1, PUSH,3, BR_COND,4, PUSH,2, BR_COND,-10])
142
143 def test_compile_call_ret(self):
144 code = compile("""PUSH 1
145 CALL func1
146 PUSH 3
147 CALL func2
148 RETURN
149
150 func1:
151 PUSH 2
152 RETURN # comment
153
154 func2:
155 PUSH 4 ;comment
156 PUSH 5
157 ADD
158 RETURN""")
159 assert code == list2bytecode([PUSH,1, CALL,5, PUSH,3, CALL,4, RETURN,
160 PUSH,2, RETURN,
161 PUSH,4, PUSH,5, ADD, RETURN])
162
163 def test_factorial_seven(self):
164 code = compile('''
165 PUSH 1 # accumulator
166 PUSH 7 # N
167
168 start:
169 PICK 0
170 PUSH 1
171 LE
172 BR_COND exit
173
174 SWAP
175 PICK 1
176 MUL
177 SWAP
178 PUSH 1
179 SUB
180 PUSH 1
181 BR_COND start
182
183 exit:
184 POP
185 RETURN
186 ''')
187 res = self.interp(code)
188 assert res == 5040
189
190 def test_factorial_seven_harder(self):
191 code = compile('''
192 PUSH 1 # accumulator
193 PUSH 7 # N
194
195 start:
196 PICK 0
197 PUSH 1
198 LE
199 PUSH exit
200 BR_COND_STK
201
202 SWAP
203 PICK 1
204 MUL
205 SWAP
206 PUSH 1
207 SUB
208 PUSH 1
209 BR_COND start
210
211 exit:
212 NOP # BR_COND_STK skips this instruction
213 POP
214 RETURN
215 ''')
216 res = self.interp(code)
217 assert res == 5040
218
219
220
221 def test_factorial_with_arg(self):
222 code = compile(FACTORIAL_SOURCE) # see below
223 res = self.interp(code, 0, 6)
224 assert res == 720
225
226 def test_translate_factorial(self):
227 py.test.skip("?")
228 # use py.test --benchmark to do the benchmarking
229 code = compile(FACTORIAL_SOURCE)
230 interp = self.interp
231 def driver():
232 bench = Benchmark()
233 while 1:
234 res = interp(code, 0, 2500)
235 if bench.stop():
236 break
237 return res
238
239 fn = self.getcompiled(driver, [])
240 res = fn()
241 assert res == 0 # too many powers of 2 to be anything else
242
243 FACTORIAL_SOURCE = '''
244 PUSH 1 # accumulator
245 PUSHARG
246
247 start:
248 PICK 0
249 PUSH 1
250 LE
251 BR_COND exit
252
253 SWAP
254 PICK 1
255 MUL
256 SWAP
257 PUSH 1
258 SUB
259 PUSH 1
260 BR_COND start
261
262 exit:
263 POP
264 RETURN
265 '''
266
267 if __name__ == '__main__':
268 code = compile(FACTORIAL_SOURCE)
269 print ','.join([str(ord(c)) for c in code])