Debian Codegraph @

rpython/rtyper/test/test_llann.py

raw symbols SCIP
1 import py
2
3 from rpython.annotator import model as annmodel
4 from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
5 from rpython.conftest import option
6 from rpython.rtyper.annlowlevel import (annotate_lowlevel_helper,
7 MixLevelHelperAnnotator, PseudoHighLevelCallable, llhelper,
8 cast_instance_to_base_ptr, cast_base_ptr_to_instance)
9 from rpython.rtyper.llinterp import LLInterpreter
10 from rpython.rtyper.lltypesystem.lltype import *
11 from rpython.rtyper.rclass import fishllattr, OBJECTPTR
12 from rpython.rtyper.test.test_llinterp import interpret
13 from rpython.translator.translator import TranslationContext
14
15
16 # helpers
17
18 def annotated_calls(ann, ops=('simple_call,')):
19 for block in ann.annotated:
20 for op in block.operations:
21 if op.opname in ops:
22 yield op
23
24
25 def derived(op, orig):
26 if op.args[0].value.__name__.startswith(orig):
27 return op.args[0].value
28 else:
29 return None
30
31
32 class TestLowLevelAnnotateTestCase:
33 from rpython.annotator.annrpython import RPythonAnnotator
34
35 def annotate(self, ll_function, argtypes):
36 self.a = self.RPythonAnnotator()
37 graph = annotate_lowlevel_helper(self.a, ll_function, argtypes)
38 if option.view:
39 self.a.translator.view()
40 return self.a.binding(graph.getreturnvar())
41
42 def test_simple(self):
43 S = GcStruct("s", ('v', Signed))
44 def llf():
45 s = malloc(S)
46 return s.v
47 s = self.annotate(llf, [])
48 assert s.knowntype == int
49
50 def test_simple2(self):
51 S = Struct("s", ('v', Signed))
52 S2 = GcStruct("s2", ('a', S), ('b', S))
53 def llf():
54 s = malloc(S2)
55 return s.a.v+s.b.v
56 s = self.annotate(llf, [])
57 assert s.knowntype == int
58
59 def test_array(self):
60 A = GcArray(('v', Signed))
61 def llf():
62 a = malloc(A, 1)
63 return a[0].v
64 s = self.annotate(llf, [])
65 assert s.knowntype == int
66
67 def test_array_longlong(self):
68 from rpython.rlib.rarithmetic import r_longlong
69 A = GcArray(('v', Signed))
70 one = r_longlong(1)
71 def llf():
72 a = malloc(A, one)
73 return a[0].v
74 s = self.annotate(llf, [])
75 assert s.knowntype == int
76
77 def test_prim_array(self):
78 A = GcArray(Signed)
79 def llf():
80 a = malloc(A, 1)
81 return a[0]
82 s = self.annotate(llf, [])
83 assert s.knowntype == int
84
85 def test_prim_array_setitem(self):
86 A = GcArray(Signed)
87 def llf():
88 a = malloc(A, 1)
89 a[0] = 3
90 return a[0]
91 s = self.annotate(llf, [])
92 assert s.knowntype == int
93
94 def test_cast_simple_widening(self):
95 S2 = Struct("s2", ('a', Signed))
96 S1 = Struct("s1", ('sub1', S2), ('sub2', S2))
97 PS1 = Ptr(S1)
98 PS2 = Ptr(S2)
99 def llf(p1):
100 p2 = p1.sub1
101 p3 = cast_pointer(PS1, p2)
102 return p3
103 s = self.annotate(llf, [SomePtr(PS1)])
104 assert isinstance(s, SomePtr)
105 assert s.ll_ptrtype == PS1
106
107 def test_cast_simple_widening_from_gc(self):
108 S2 = GcStruct("s2", ('a', Signed))
109 S1 = GcStruct("s1", ('sub1', S2), ('x', Signed))
110 PS1 = Ptr(S1)
111 def llf():
112 p1 = malloc(S1)
113 p2 = p1.sub1
114 p3 = cast_pointer(PS1, p2)
115 return p3
116 s = self.annotate(llf, [])
117 assert isinstance(s, SomePtr)
118 assert s.ll_ptrtype == PS1
119
120 def test_cast_pointer(self):
121 S3 = GcStruct("s3", ('a', Signed))
122 S2 = GcStruct("s3", ('sub', S3))
123 S1 = GcStruct("s1", ('sub', S2))
124 PS1 = Ptr(S1)
125 PS2 = Ptr(S2)
126 PS3 = Ptr(S3)
127 def llf():
128 p1 = malloc(S1)
129 p2 = p1.sub
130 p3 = p2.sub
131 p12 = cast_pointer(PS1, p2)
132 p13 = cast_pointer(PS1, p3)
133 p21 = cast_pointer(PS2, p1)
134 p23 = cast_pointer(PS2, p3)
135 p31 = cast_pointer(PS3, p1)
136 p32 = cast_pointer(PS3, p2)
137 return p12, p13, p21, p23, p31, p32
138 s = self.annotate(llf, [])
139 assert [x.ll_ptrtype for x in s.items] == [PS1, PS1, PS2, PS2, PS3, PS3]
140
141
142 def test_array_length(self):
143 A = GcArray(('v', Signed))
144 def llf():
145 a = malloc(A, 1)
146 return len(a)
147 s = self.annotate(llf, [])
148 assert s.knowntype == int
149
150 def test_funcptr(self):
151 F = FuncType((Signed,), Signed)
152 PF = Ptr(F)
153 def llf(p):
154 return p(0)
155 s = self.annotate(llf, [SomePtr(PF)])
156 assert s.knowntype == int
157
158
159 def test_ll_calling_ll(self):
160 A = GcArray(Float)
161 B = GcArray(Signed)
162 def ll_make(T, n):
163 x = malloc(T, n)
164 return x
165 def ll_get(T, x, i):
166 return x[i]
167 def llf():
168 a = ll_make(A, 3)
169 b = ll_make(B, 2)
170 a[0] = 1.0
171 b[1] = 3
172 y0 = ll_get(A, a, 1)
173 y1 = ll_get(B, b, 1)
174 #
175 a2 = ll_make(A, 4)
176 a2[0] = 2.0
177 return ll_get(A, a2, 1)
178 s = self.annotate(llf, [])
179 a = self.a
180 assert s == annmodel.SomeFloat()
181
182 seen = {}
183 ngraphs = len(a.translator.graphs)
184
185 vTs = []
186 for call in annotated_calls(a):
187 if derived(call, "ll_"):
188
189 func, T = [x.value for x in call.args[0:2]]
190 if (func, T) in seen:
191 continue
192 seen[func, T] = True
193
194 desc = a.bookkeeper.getdesc(func)
195 g = desc.specialize([a.binding(x) for x in call.args[1:]])
196
197 args = g.getargs()
198 rv = g.getreturnvar()
199 if func is ll_get:
200 vT, vp, vi = args
201 assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
202 assert a.binding(vi).knowntype == int
203 assert a.binding(vp).ll_ptrtype.TO == T
204 assert a.binding(rv) == lltype_to_annotation(T.OF)
205 elif func is ll_make:
206 vT, vn = args
207 assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
208 assert a.binding(vn).knowntype == int
209 assert a.binding(rv).ll_ptrtype.TO == T
210 else:
211 assert False, func
212 vTs.append(vT)
213
214 assert len(seen) == 4
215
216 return a, vTs # reused by a test in test_rtyper
217
218 def test_ll_calling_ll2(self):
219 A = GcArray(Float)
220 B = GcArray(Signed)
221 def ll_make(T, n):
222 x = malloc(T, n)
223 return x
224 def ll_get(x, i):
225 return x[i]
226 def makelen4(T):
227 return ll_make(T, 4)
228 def llf():
229 a = ll_make(A, 3)
230 b = ll_make(B, 2)
231 a[0] = 1.0
232 b[1] = 3
233 y0 = ll_get(a, 1)
234 y1 = ll_get(b, 1)
235 #
236 a2 = makelen4(A)
237 a2[0] = 2.0
238 return ll_get(a2, 1)
239 s = self.annotate(llf, [])
240 a = self.a
241 assert s == annmodel.SomeFloat()
242
243 seen = {}
244
245 def q(v):
246 s = a.binding(v)
247 if s.is_constant():
248 return s.const
249 else:
250 return s.ll_ptrtype
251
252 vTs = []
253
254 for call in annotated_calls(a):
255 if derived(call, "ll_") or derived(call, "makelen4"):
256
257 func, T = [q(x) for x in call.args[0:2]]
258 if (func, T) in seen:
259 continue
260 seen[func, T] = True
261
262 desc = a.bookkeeper.getdesc(func)
263 g = desc.specialize([a.binding(x) for x in call.args[1:]])
264
265 args = g.getargs()
266 rv = g.getreturnvar()
267
268 if func is ll_make:
269 vT, vn = args
270 assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
271 assert a.binding(vn).knowntype == int
272 assert a.binding(rv).ll_ptrtype.TO == T
273 vTs.append(vT)
274 elif func is makelen4:
275 vT, = args
276 assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
277 assert a.binding(rv).ll_ptrtype.TO == T
278 vTs.append(vT)
279 elif func is ll_get:
280 vp, vi = args
281 assert a.binding(vi).knowntype == int
282 assert a.binding(vp).ll_ptrtype == T
283 assert a.binding(rv) == lltype_to_annotation(
284 T.TO.OF)
285 else:
286 assert False, func
287
288 assert len(seen) == 5
289
290 return a, vTs # reused by a test in test_rtyper
291
292 def test_ll_stararg(self):
293 A = GcArray(Float)
294 B = GcArray(Signed)
295 def ll_sum(*args):
296 result = 0
297 if len(args) > 0:
298 result += args[0]
299 if len(args) > 1:
300 result += args[1]
301 if len(args) > 2:
302 result += args[2]
303 if len(args) > 3:
304 result += args[3]
305 return result
306 def llf():
307 a = ll_sum()
308 b = ll_sum(4, 5)
309 c = ll_sum(2.5)
310 d = ll_sum(4, 5.25)
311 e = ll_sum(1000, 200, 30, 4)
312 f = ll_sum(1000, 200, 30, 5)
313 return a, b, c, d, e, f
314 s = self.annotate(llf, [])
315 assert isinstance(s, annmodel.SomeTuple)
316 assert s.items[0].knowntype is int
317 assert s.items[0].const == 0
318 assert s.items[1].knowntype is int
319 assert s.items[2].knowntype is float
320 assert s.items[3].knowntype is float
321 assert s.items[4].knowntype is int
322 assert s.items[5].knowntype is int
323
324 def test_str_vs_ptr(self):
325 S = GcStruct('s', ('x', Signed))
326 def ll_stuff(x):
327 if x is None or isinstance(x, str):
328 return 2
329 else:
330 return 3
331 def llf():
332 x = ll_stuff("hello")
333 y = ll_stuff(nullptr(S))
334 return x, y
335 s = self.annotate(llf, [])
336 assert isinstance(s, annmodel.SomeTuple)
337 assert s.items[0].is_constant()
338 assert s.items[0].const == 2
339 assert s.items[1].is_constant()
340 assert s.items[1].const == 3
341
342 def test_getRuntimeTypeInfo(self):
343 S = GcStruct('s', ('x', Signed), rtti=True)
344 def llf():
345 return getRuntimeTypeInfo(S)
346 s = self.annotate(llf, [])
347 assert isinstance(s, SomePtr)
348 assert s.ll_ptrtype == Ptr(RuntimeTypeInfo)
349 assert s.const == getRuntimeTypeInfo(S)
350
351 def test_runtime_type_info(self):
352 S = GcStruct('s', ('x', Signed), rtti=True)
353 def llf(p):
354 return runtime_type_info(p)
355 s = self.annotate(llf, [SomePtr(Ptr(S))])
356 assert isinstance(s, SomePtr)
357 assert s.ll_ptrtype == Ptr(RuntimeTypeInfo)
358
359 def test_cast_primitive(self):
360 def llf(u):
361 return cast_primitive(Signed, u)
362 s = self.annotate(llf, [annmodel.SomeInteger(unsigned=True)])
363 assert s.knowntype == int
364 def llf(s):
365 return cast_primitive(Unsigned, s)
366 s = self.annotate(llf, [annmodel.SomeInteger()])
367 assert s.unsigned == True
368
369 def test_pbctype(self):
370 TYPE = Void
371 TYPE2 = Signed
372 def g(lst):
373 n = lst[0]
374 if isinstance(TYPE, Number):
375 result = 123
376 else:
377 result = 456
378 if isinstance(TYPE2, Number):
379 result += 1
380 return result + n
381 def llf():
382 lst = [5]
383 g(lst)
384 lst.append(6)
385 self.annotate(llf, [])
386
387 def test_adtmeths(self):
388 def h_length(s):
389 return s.foo
390 S = GcStruct("S", ('foo', Signed),
391 adtmeths={"h_length": h_length,
392 "stuff": 12})
393 def llf():
394 s = malloc(S)
395 s.foo = 321
396 return s.h_length()
397 s = self.annotate(llf, [])
398 assert s.knowntype == int and not s.is_constant()
399
400 def llf():
401 s = malloc(S)
402 return s.stuff
403 s = self.annotate(llf, [])
404 assert s.is_constant() and s.const == 12
405
406
407 def test_pseudohighlevelcallable():
408 t = TranslationContext()
409 t.buildannotator()
410 rtyper = t.buildrtyper()
411 rtyper.specialize()
412 a = MixLevelHelperAnnotator(rtyper)
413
414 class A:
415 value = 5
416 def double(self):
417 return self.value * 2
418
419 def fn1(a):
420 a2 = A()
421 a2.value = a.double()
422 return a2
423
424 s_A, r_A = a.s_r_instanceof(A)
425 fn1ptr = a.delayedfunction(fn1, [s_A], s_A)
426 pseudo = PseudoHighLevelCallable(fn1ptr, [s_A], s_A)
427
428 def fn2(n):
429 a = A()
430 a.value = n
431 a2 = pseudo(a)
432 return a2.value
433
434 graph = a.getgraph(fn2, [annmodel.SomeInteger()], annmodel.SomeInteger())
435 a.finish()
436
437 llinterp = LLInterpreter(rtyper)
438 res = llinterp.eval_graph(graph, [21])
439 assert res == 42
440
441
442 def test_llhelper():
443 S = GcStruct('S', ('x', Signed), ('y', Signed))
444 def f(s,z):
445 return s.x*s.y+z
446
447 def g(s):
448 return s.x+s.y
449
450 F = Ptr(FuncType([Ptr(S), Signed], Signed))
451 G = Ptr(FuncType([Ptr(S)], Signed))
452
453 def h(x, y, z):
454 s = malloc(S)
455 s.x = x
456 s.y = y
457 fptr = llhelper(F, f)
458 gptr = llhelper(G, g)
459 assert typeOf(fptr) == F
460 return fptr(s, z)+fptr(s, z*2)+gptr(s)
461
462 res = interpret(h, [8, 5, 2])
463 assert res == 99
464
465 def test_llhelper_multiple_functions():
466 S = GcStruct('S', ('x', Signed), ('y', Signed))
467 def f(s):
468 return s.x - s.y
469 def g(s):
470 return s.x + s.y
471
472 F = Ptr(FuncType([Ptr(S)], Signed))
473
474 myfuncs = [f, g]
475
476 def h(x, y, z):
477 s = malloc(S)
478 s.x = x
479 s.y = y
480 fptr = llhelper(F, myfuncs[z])
481 assert typeOf(fptr) == F
482 return fptr(s)
483
484 res = interpret(h, [80, 5, 0])
485 assert res == 75
486 res = interpret(h, [80, 5, 1])
487 assert res == 85
488
489
490 def test_cast_instance_to_base_ptr():
491 class A:
492 def __init__(self, x, y):
493 self.x = x
494 self.y = y
495
496 def f(x, y):
497 if x > 20:
498 a = None
499 else:
500 a = A(x, y)
501 a1 = cast_instance_to_base_ptr(a)
502 return a1
503
504 res = interpret(f, [5, 10])
505 assert typeOf(res) == OBJECTPTR
506 assert fishllattr(res, 'x') == 5
507 assert fishllattr(res, 'y') == 10
508
509 res = interpret(f, [25, 10])
510 assert res == nullptr(OBJECTPTR.TO)
511
512
513 def test_cast_base_ptr_to_instance():
514 class A:
515 def __init__(self, x, y):
516 self.x = x
517 self.y = y
518
519 def f(x, y):
520 if x > 20:
521 a = None
522 else:
523 a = A(x, y)
524 a1 = cast_instance_to_base_ptr(a)
525 b = cast_base_ptr_to_instance(A, a1)
526 return a is b
527
528 assert f(5, 10) is True
529 assert f(25, 10) is True
530
531 res = interpret(f, [5, 10])
532 assert res is True
533 res = interpret(f, [25, 10])
534 assert res is True