PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rtyper/test/test_rptr.py

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