Debian Codegraph @

rpython/rtyper/test/test_rptr.py

raw symbols SCIP
1 import sys
2
3 import py
4
5 from rpython.annotator import model as annmodel
6 from rpython.rtyper.llannotation import SomePtr
7 from rpython.annotator.annrpython import RPythonAnnotator
8 from rpython.rlib.rarithmetic import is_valid_int
9 from rpython.rtyper.annlowlevel import annotate_lowlevel_helper, LowLevelAnnotatorPolicy
10 from rpython.rtyper.lltypesystem import llmemory, lltype
11 from rpython.rtyper.rtyper import RPythonTyper
12
13
14 # ____________________________________________________________
15
16 def ll_rtype(llfn, argtypes=[]):
17 a = RPythonAnnotator()
18 graph = annotate_lowlevel_helper(a, llfn, argtypes)
19 s = a.binding(graph.getreturnvar())
20 t = a.translator
21 typer = RPythonTyper(a)
22 typer.specialize()
23 #t.view()
24 t.checkgraphs()
25 return s, t
26
27 def test_cast_pointer():
28 S = lltype.GcStruct('s', ('x', lltype.Signed))
29 S1 = lltype.GcStruct('s1', ('sub', S))
30 S2 = lltype.GcStruct('s2', ('sub', S1))
31 PS = lltype.Ptr(S)
32 PS2 = lltype.Ptr(S2)
33 def lldown(p):
34 return lltype.cast_pointer(PS, p)
35 s, t = ll_rtype(lldown, [SomePtr(PS2)])
36 assert s.ll_ptrtype == PS
37 def llup(p):
38 return lltype.cast_pointer(PS2, p)
39 s, t = ll_rtype(llup, [SomePtr(PS)])
40 assert s.ll_ptrtype == PS2
41
42 def test_runtime_type_info():
43 S = lltype.GcStruct('s', ('x', lltype.Signed), rtti=True)
44 def ll_example(p):
45 return (lltype.runtime_type_info(p),
46 lltype.runtime_type_info(p) == lltype.getRuntimeTypeInfo(S))
47
48 assert ll_example(lltype.malloc(S)) == (lltype.getRuntimeTypeInfo(S), True)
49 s, t = ll_rtype(ll_example, [SomePtr(lltype.Ptr(S))])
50 assert s == annmodel.SomeTuple([SomePtr(lltype.Ptr(lltype.RuntimeTypeInfo)),
51 annmodel.SomeBool()])
52
53 from rpython.rtyper.test.test_llinterp import interpret, gengraph
54
55 def test_adtmeths():
56 policy = LowLevelAnnotatorPolicy()
57
58 def h_newstruct():
59 return lltype.malloc(S)
60
61 S = lltype.GcStruct('s', ('x', lltype.Signed),
62 adtmeths={"h_newstruct": h_newstruct})
63
64 def f():
65 return S.h_newstruct()
66
67 s = interpret(f, [], policy=policy)
68
69 assert lltype.typeOf(s) == lltype.Ptr(S)
70
71 def h_alloc(n):
72 return lltype.malloc(A, n)
73 def h_length(a):
74 return len(a)
75
76 A = lltype.GcArray(lltype.Signed,
77 adtmeths={"h_alloc": h_alloc,
78 "h_length": h_length,
79 'flag': True})
80
81 def f():
82 return A.h_alloc(10)
83
84 a = interpret(f, [], policy=policy)
85
86 assert lltype.typeOf(a) == lltype.Ptr(A)
87 assert len(a) == 10
88
89
90 def f():
91 a = A.h_alloc(10)
92 return a.h_length()
93
94 res = interpret(f, [], policy=policy)
95 assert res == 10
96
97 def f():
98 return A.flag
99 res = interpret(f, [], policy=policy)
100 assert res
101
102 def test_odd_ints():
103 T = lltype.GcStruct('T')
104 S = lltype.GcStruct('S', ('t', T))
105 PT = lltype.Ptr(T)
106 PS = lltype.Ptr(S)
107 def fn(n):
108 s = lltype.cast_int_to_ptr(PS, n)
109 assert lltype.typeOf(s) == PS
110 assert lltype.cast_ptr_to_int(s) == n
111 t = lltype.cast_pointer(PT, s)
112 assert lltype.typeOf(t) == PT
113 assert lltype.cast_ptr_to_int(t) == n
114 assert s == lltype.cast_pointer(PS, t)
115
116 interpret(fn, [11521])
117
118 def test_odd_ints_opaque():
119 T = lltype.GcStruct('T')
120 Q = lltype.GcOpaqueType('Q')
121 PT = lltype.Ptr(T)
122 PQ = lltype.Ptr(Q)
123 def fn(n):
124 t = lltype.cast_int_to_ptr(PT, n)
125 assert lltype.typeOf(t) == PT
126 assert lltype.cast_ptr_to_int(t) == n
127 o = lltype.cast_opaque_ptr(PQ, t)
128 assert lltype.cast_ptr_to_int(o) == n
129
130 fn(13)
131 interpret(fn, [11521])
132
133 def test_ptr():
134 S = lltype.GcStruct('s')
135 def ll_example():
136 return lltype.malloc(lltype.Ptr(S).TO)
137
138 p = interpret(ll_example, [])
139 assert lltype.typeOf(p) == lltype.Ptr(S)
140
141 def test_cast_opaque_ptr():
142 O = lltype.GcOpaqueType('O')
143 Q = lltype.GcOpaqueType('Q')
144 S = lltype.GcStruct('S', ('x', lltype.Signed))
145 def fn():
146 s = lltype.malloc(S)
147 o = lltype.cast_opaque_ptr(lltype.Ptr(O), s)
148 q = lltype.cast_opaque_ptr(lltype.Ptr(Q), o)
149 p = lltype.cast_opaque_ptr(lltype.Ptr(S), q)
150 return p == s
151 res = interpret(fn, [])
152 assert res is True
153
154 O1 = lltype.OpaqueType('O')
155 S1 = lltype.Struct('S1', ('x', lltype.Signed))
156 s1 = lltype.malloc(S1, immortal=True)
157 def fn1():
158 o1 = lltype.cast_opaque_ptr(lltype.Ptr(O1), s1)
159 p1 = lltype.cast_opaque_ptr(lltype.Ptr(S1), o1)
160 return p1 == s1
161 res = interpret(fn1, [])
162 assert res is True
163
164 def test_address():
165 S = lltype.GcStruct('S')
166 p1 = lltype.nullptr(S)
167 p2 = lltype.malloc(S)
168
169 def g(p):
170 return bool(llmemory.cast_ptr_to_adr(p))
171 def fn(n):
172 if n < 0:
173 return g(p1)
174 else:
175 return g(p2)
176
177 res = interpret(fn, [-5])
178 assert res is False
179 res = interpret(fn, [5])
180 assert res is True
181
182 def test_cast_adr_to_int():
183 S = lltype.Struct('S')
184 p = lltype.malloc(S, immortal=True)
185 def fn(n):
186 a = llmemory.cast_ptr_to_adr(p)
187 if n == 2:
188 return llmemory.cast_adr_to_int(a, "emulated")
189 elif n == 4:
190 return llmemory.cast_adr_to_int(a, "symbolic")
191 else:
192 return llmemory.cast_adr_to_int(a, "forced")
193
194 res = interpret(fn, [2])
195 assert is_valid_int(res)
196 assert res == lltype.cast_ptr_to_int(p)
197 #
198 res = interpret(fn, [4])
199 assert isinstance(res, llmemory.AddressAsInt)
200 assert llmemory.cast_int_to_adr(res) == llmemory.cast_ptr_to_adr(p)
201 #
202 res = interpret(fn, [6])
203 assert is_valid_int(res)
204 from rpython.rtyper.lltypesystem import rffi
205 assert res == rffi.cast(lltype.Signed, p)
206
207 def test_flavored_malloc():
208 T = lltype.GcStruct('T', ('y', lltype.Signed))
209 def fn(n):
210 p = lltype.malloc(T, flavor='gc')
211 p.y = n
212 return p.y
213
214 res = interpret(fn, [232])
215 assert res == 232
216
217 S = lltype.Struct('S', ('x', lltype.Signed))
218 def fn(n):
219 p = lltype.malloc(S, flavor='raw')
220 p.x = n
221 result = p.x
222 lltype.free(p, flavor='raw')
223 return result
224
225 res = interpret(fn, [23])
226 assert res == 23
227
228 S = lltype.Struct('S', ('x', lltype.Signed))
229 def fn(n):
230 p = lltype.malloc(S, flavor='raw', track_allocation=False)
231 p.x = n
232 result = p.x
233 return result
234
235 res = interpret(fn, [23])
236 assert res == 23
237
238 S = lltype.Struct('S', ('x', lltype.Signed))
239 def fn(n):
240 p = lltype.malloc(S, flavor='raw', track_allocation=False)
241 p.x = n
242 result = p.x
243 lltype.free(p, flavor='raw', track_allocation=False)
244 return result
245
246 res = interpret(fn, [23])
247 assert res == 23
248
249 def test_memoryerror():
250 A = lltype.Array(lltype.Signed)
251 def fn(n):
252 try:
253 a = lltype.malloc(A, n, flavor='raw')
254 except MemoryError:
255 return -42
256 else:
257 res = len(a)
258 lltype.free(a, flavor='raw')
259 return res
260
261 res = interpret(fn, [123])
262 assert res == 123
263
264 res = interpret(fn, [sys.maxint])
265 assert res == -42
266
267
268 def test_call_ptr():
269 def f(x, y, z):
270 return x+y+z
271 FTYPE = lltype.FuncType([lltype.Signed, lltype.Signed, lltype.Signed], lltype.Signed)
272 fptr = lltype.functionptr(FTYPE, "f", _callable=f)
273
274 def g(x, y, z):
275 tot = 0
276 tot += fptr(x, y, z)
277 tot += fptr(*(x, y, z))
278 tot += fptr(x, *(x, z))
279 return tot
280
281 res = interpret(g, [1, 2, 4])
282 assert res == g(1, 2, 4)
283
284 def wrong(x, y):
285 fptr(*(x, y))
286
287 py.test.raises(TypeError, "interpret(wrong, [1, 2])")
288
289
290 def test_ptr_str():
291 def f():
292 return str(p)
293
294 S = lltype.GcStruct('S', ('x', lltype.Signed))
295 p = lltype.malloc(S)
296
297 res = interpret(f, [])
298 assert res.chars[0] == '0'
299 assert res.chars[1] == 'x'
300
301
302 def test_first_subfield_access_is_cast_pointer():
303 B = lltype.GcStruct("B", ('x', lltype.Signed))
304 C = lltype.GcStruct("C", ('super', B), ('y', lltype.Signed))
305 def f():
306 c = lltype.malloc(C)
307 c.super.x = 1
308 c.y = 2
309 return c.super.x + c.y
310 s, t = ll_rtype(f, [])
311 from rpython.translator.translator import graphof
312 from rpython.flowspace.model import summary
313 graph = graphof(t, f)
314 graphsum = summary(graph)
315 assert 'getsubstruct' not in graphsum
316 assert 'cast_pointer' in graphsum
317
318
319
320 def test_interior_ptr():
321 S = lltype.Struct("S", ('x', lltype.Signed))
322 T = lltype.GcStruct("T", ('s', S))
323 def f():
324 t = lltype.malloc(T)
325 t.s.x = 1
326 return t.s.x
327 res = interpret(f, [])
328 assert res == 1
329
330 def test_interior_ptr_with_index():
331 S = lltype.Struct("S", ('x', lltype.Signed))
332 T = lltype.GcArray(S)
333 def f():
334 t = lltype.malloc(T, 1)
335 t[0].x = 1
336 return t[0].x
337 res = interpret(f, [])
338 assert res == 1
339
340 def test_interior_ptr_convert():
341 S = lltype.Struct("S", ("x", lltype.Signed))
342 T = lltype.GcArray(S)
343 def f(i):
344 t = lltype.malloc(T, 2)
345 if i:
346 x = t[0]
347 else:
348 x = t[1]
349 x.x = 3
350 return t[0].x
351
352 res = interpret(f, [13])
353 assert res == 3
354
355 def test_interior_ptr_with_field_and_index():
356 S = lltype.Struct("S", ('x', lltype.Signed))
357 T = lltype.GcStruct("T", ('items', lltype.Array(S)))
358 def f():
359 t = lltype.malloc(T, 1)
360 t.items[0].x = 1
361 return t.items[0].x
362 res = interpret(f, [])
363 assert res == 1
364
365 def test_interior_ptr_with_index_and_field():
366 S = lltype.Struct("S", ('x', lltype.Signed))
367 T = lltype.Struct("T", ('s', S))
368 U = lltype.GcArray(T)
369 def f():
370 u = lltype.malloc(U, 1)
371 u[0].s.x = 1
372 return u[0].s.x
373 res = interpret(f, [])
374 assert res == 1
375
376 def test_interior_ptr_len():
377 S = lltype.Struct("S", ('x', lltype.Signed))
378 T = lltype.GcStruct("T", ('items', lltype.Array(S)))
379 def f():
380 t = lltype.malloc(T, 1)
381 return len(t.items)
382 res = interpret(f, [])
383 assert res == 1
384
385 def test_interior_ptr_with_setitem():
386 T = lltype.GcStruct("T", ('s', lltype.Array(lltype.Signed)))
387 def f():
388 t = lltype.malloc(T, 1)
389 t.s[0] = 1
390 return t.s[0]
391 res = interpret(f, [])
392 assert res == 1
393
394 def test_isinstance_ptr():
395 S = lltype.GcStruct("S", ('x', lltype.Signed))
396 def f(n):
397 x = isinstance(lltype.Signed, lltype.Ptr)
398 return x + (lltype.typeOf(x) is lltype.Ptr(S)) + len(n)
399 def lltest():
400 f([])
401 return f([1])
402 s, t = ll_rtype(lltest, [])
403 assert s.is_constant() == False
404
405 def test_staticadtmeths():
406 ll_func = lltype.staticAdtMethod(lambda x: x + 42)
407 S = lltype.GcStruct('S', adtmeths={'ll_func': ll_func})
408 def f():
409 return lltype.malloc(S).ll_func(5)
410 s, t = ll_rtype(f, [])
411 graphf = t.graphs[0]
412 for op in graphf.startblock.operations:
413 assert op.opname != 'getfield'