PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/jit/codewriter/test/test_list.py

https://github.com/lalitjsraks/pypy
Python | 232 lines | 221 code | 8 blank | 3 comment | 9 complexity | aaba5cdf128475dfacdc5df47611b3c2 MD5 | raw file
  1. from pypy.rpython.lltypesystem import lltype
  2. from pypy.translator.unsimplify import varoftype
  3. from pypy.objspace.flow.model import Constant, SpaceOperation
  4. from pypy.jit.codewriter.jtransform import Transformer, NotSupported
  5. from pypy.jit.codewriter.flatten import GraphFlattener
  6. from pypy.jit.codewriter.format import assert_format
  7. from pypy.jit.codewriter.test.test_flatten import fake_regallocs
  8. from pypy.jit.metainterp.history import AbstractDescr
  9. # ____________________________________________________________
  10. FIXEDLIST = lltype.Ptr(lltype.GcArray(lltype.Signed))
  11. VARLIST = lltype.Ptr(lltype.GcStruct('VARLIST',
  12. ('length', lltype.Signed),
  13. ('items', FIXEDLIST),
  14. adtmeths={"ITEM": lltype.Signed}))
  15. class FakeCPU:
  16. class arraydescrof(AbstractDescr):
  17. def __init__(self, ARRAY):
  18. self.ARRAY = ARRAY
  19. def __repr__(self):
  20. return '<ArrayDescr>'
  21. class fielddescrof(AbstractDescr):
  22. def __init__(self, STRUCT, fieldname):
  23. self.STRUCT = STRUCT
  24. self.fieldname = fieldname
  25. def __repr__(self):
  26. return '<FieldDescr %s>' % self.fieldname
  27. class sizeof(AbstractDescr):
  28. def __init__(self, STRUCT):
  29. self.STRUCT = STRUCT
  30. def __repr__(self):
  31. return '<SizeDescr>'
  32. class FakeCallControl:
  33. class getcalldescr(AbstractDescr):
  34. def __init__(self, op):
  35. self.op = op
  36. def __repr__(self):
  37. return '<CallDescr>'
  38. def builtin_test(oopspec_name, args, RESTYPE, expected):
  39. v_result = varoftype(RESTYPE)
  40. tr = Transformer(FakeCPU(), FakeCallControl())
  41. tr.immutable_arrays = {}
  42. tr.vable_array_vars = {}
  43. if '/' in oopspec_name:
  44. oopspec_name, property = oopspec_name.split('/')
  45. def force_flags(op):
  46. if property == 'NONNEG': return True, False
  47. if property == 'NEG': return False, False
  48. if property == 'CANRAISE': return False, True
  49. raise ValueError(property)
  50. tr._get_list_nonneg_canraise_flags = force_flags
  51. op = SpaceOperation('direct_call',
  52. [Constant("myfunc", lltype.Void)] + args,
  53. v_result)
  54. try:
  55. oplist = tr._handle_list_call(op, oopspec_name, args)
  56. except NotSupported:
  57. assert expected is NotSupported
  58. else:
  59. assert expected is not NotSupported
  60. assert oplist is not None
  61. flattener = GraphFlattener(None, fake_regallocs())
  62. if not isinstance(oplist, list):
  63. oplist = [oplist]
  64. for op1 in oplist:
  65. flattener.serialize_op(op1)
  66. assert_format(flattener.ssarepr, expected)
  67. # ____________________________________________________________
  68. # Fixed lists
  69. def test_newlist():
  70. builtin_test('newlist', [], FIXEDLIST,
  71. """new_array <ArrayDescr>, $0 -> %r0""")
  72. builtin_test('newlist', [Constant(5, lltype.Signed)], FIXEDLIST,
  73. """new_array <ArrayDescr>, $5 -> %r0""")
  74. builtin_test('newlist', [varoftype(lltype.Signed)], FIXEDLIST,
  75. """new_array <ArrayDescr>, %i0 -> %r0""")
  76. builtin_test('newlist', [Constant(5, lltype.Signed),
  77. Constant(0, lltype.Signed)], FIXEDLIST,
  78. """new_array <ArrayDescr>, $5 -> %r0""")
  79. builtin_test('newlist', [Constant(5, lltype.Signed),
  80. Constant(1, lltype.Signed)], FIXEDLIST,
  81. NotSupported)
  82. builtin_test('newlist', [Constant(5, lltype.Signed),
  83. varoftype(lltype.Signed)], FIXEDLIST,
  84. NotSupported)
  85. def test_fixed_ll_arraycopy():
  86. builtin_test('list.ll_arraycopy',
  87. [varoftype(FIXEDLIST),
  88. varoftype(FIXEDLIST),
  89. varoftype(lltype.Signed),
  90. varoftype(lltype.Signed),
  91. varoftype(lltype.Signed)],
  92. lltype.Void, """
  93. arraycopy <CallDescr>, $'myfunc', %r0, %r1, %i0, %i1, %i2, <ArrayDescr>
  94. """)
  95. def test_fixed_getitem():
  96. builtin_test('list.getitem/NONNEG',
  97. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  98. lltype.Signed, """
  99. getarrayitem_gc_i %r0, <ArrayDescr>, %i0 -> %i1
  100. """)
  101. builtin_test('list.getitem/NEG',
  102. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  103. lltype.Signed, """
  104. -live-
  105. check_neg_index %r0, <ArrayDescr>, %i0 -> %i1
  106. getarrayitem_gc_i %r0, <ArrayDescr>, %i1 -> %i2
  107. """)
  108. builtin_test('list.getitem/CANRAISE',
  109. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  110. lltype.Signed, NotSupported)
  111. def test_fixed_getitem_foldable():
  112. builtin_test('list.getitem_foldable/NONNEG',
  113. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  114. lltype.Signed, """
  115. getarrayitem_gc_pure_i %r0, <ArrayDescr>, %i0 -> %i1
  116. """)
  117. builtin_test('list.getitem_foldable/NEG',
  118. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  119. lltype.Signed, """
  120. -live-
  121. check_neg_index %r0, <ArrayDescr>, %i0 -> %i1
  122. getarrayitem_gc_pure_i %r0, <ArrayDescr>, %i1 -> %i2
  123. """)
  124. builtin_test('list.getitem_foldable/CANRAISE',
  125. [varoftype(FIXEDLIST), varoftype(lltype.Signed)],
  126. lltype.Signed, NotSupported)
  127. def test_fixed_setitem():
  128. builtin_test('list.setitem/NONNEG', [varoftype(FIXEDLIST),
  129. varoftype(lltype.Signed),
  130. varoftype(lltype.Signed)],
  131. lltype.Void, """
  132. setarrayitem_gc_i %r0, <ArrayDescr>, %i0, %i1
  133. """)
  134. builtin_test('list.setitem/NEG', [varoftype(FIXEDLIST),
  135. varoftype(lltype.Signed),
  136. varoftype(lltype.Signed)],
  137. lltype.Void, """
  138. -live-
  139. check_neg_index %r0, <ArrayDescr>, %i0 -> %i1
  140. setarrayitem_gc_i %r0, <ArrayDescr>, %i1, %i2
  141. """)
  142. builtin_test('list.setitem/CANRAISE', [varoftype(FIXEDLIST),
  143. varoftype(lltype.Signed),
  144. varoftype(lltype.Signed)],
  145. lltype.Void, NotSupported)
  146. def test_fixed_len():
  147. builtin_test('list.len', [varoftype(FIXEDLIST)], lltype.Signed,
  148. """arraylen_gc %r0, <ArrayDescr> -> %i0""")
  149. def test_fixed_len_foldable():
  150. builtin_test('list.len_foldable', [varoftype(FIXEDLIST)], lltype.Signed,
  151. """arraylen_gc %r0, <ArrayDescr> -> %i0""")
  152. # ____________________________________________________________
  153. # Resizable lists
  154. def test_resizable_newlist():
  155. alldescrs = ("<SizeDescr>, <FieldDescr length>,"
  156. " <FieldDescr items>, <ArrayDescr>")
  157. builtin_test('newlist', [], VARLIST,
  158. """newlist """+alldescrs+""", $0 -> %r0""")
  159. builtin_test('newlist', [Constant(5, lltype.Signed)], VARLIST,
  160. """newlist """+alldescrs+""", $5 -> %r0""")
  161. builtin_test('newlist', [varoftype(lltype.Signed)], VARLIST,
  162. """newlist """+alldescrs+""", %i0 -> %r0""")
  163. builtin_test('newlist', [Constant(5, lltype.Signed),
  164. Constant(0, lltype.Signed)], VARLIST,
  165. """newlist """+alldescrs+""", $5 -> %r0""")
  166. builtin_test('newlist', [Constant(5, lltype.Signed),
  167. Constant(1, lltype.Signed)], VARLIST,
  168. NotSupported)
  169. builtin_test('newlist', [Constant(5, lltype.Signed),
  170. varoftype(lltype.Signed)], VARLIST,
  171. NotSupported)
  172. def test_resizable_getitem():
  173. builtin_test('list.getitem/NONNEG',
  174. [varoftype(VARLIST), varoftype(lltype.Signed)],
  175. lltype.Signed, """
  176. getlistitem_gc_i %r0, <FieldDescr items>, <ArrayDescr>, %i0 -> %i1
  177. """)
  178. builtin_test('list.getitem/NEG',
  179. [varoftype(VARLIST), varoftype(lltype.Signed)],
  180. lltype.Signed, """
  181. -live-
  182. check_resizable_neg_index %r0, <FieldDescr length>, %i0 -> %i1
  183. getlistitem_gc_i %r0, <FieldDescr items>, <ArrayDescr>, %i1 -> %i2
  184. """)
  185. builtin_test('list.getitem/CANRAISE',
  186. [varoftype(VARLIST), varoftype(lltype.Signed)],
  187. lltype.Signed, NotSupported)
  188. def test_resizable_setitem():
  189. builtin_test('list.setitem/NONNEG', [varoftype(VARLIST),
  190. varoftype(lltype.Signed),
  191. varoftype(lltype.Signed)],
  192. lltype.Void, """
  193. setlistitem_gc_i %r0, <FieldDescr items>, <ArrayDescr>, %i0, %i1
  194. """)
  195. builtin_test('list.setitem/NEG', [varoftype(VARLIST),
  196. varoftype(lltype.Signed),
  197. varoftype(lltype.Signed)],
  198. lltype.Void, """
  199. -live-
  200. check_resizable_neg_index %r0, <FieldDescr length>, %i0 -> %i1
  201. setlistitem_gc_i %r0, <FieldDescr items>, <ArrayDescr>, %i1, %i2
  202. """)
  203. builtin_test('list.setitem/CANRAISE', [varoftype(VARLIST),
  204. varoftype(lltype.Signed),
  205. varoftype(lltype.Signed)],
  206. lltype.Void, NotSupported)
  207. def test_resizable_len():
  208. builtin_test('list.len', [varoftype(VARLIST)], lltype.Signed,
  209. """getfield_gc_i %r0, <FieldDescr length> -> %i0""")
  210. def test_resizable_unsupportedop():
  211. builtin_test('list.foobar', [varoftype(VARLIST)], lltype.Signed,
  212. NotSupported)