PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/jit/tool/test/test_oparser.py

https://bitbucket.org/pypy/pypy/
Python | 285 lines | 276 code | 8 blank | 1 comment | 0 complexity | 27266cf20495b1621244b0f848ffe678 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. import sys
  3. from pypy.rpython.lltypesystem import lltype, llmemory
  4. from pypy.jit.tool.oparser import parse, OpParser
  5. from pypy.jit.metainterp.resoperation import rop
  6. from pypy.jit.metainterp.history import AbstractDescr, BoxInt, JitCellToken,\
  7. TargetToken
  8. class BaseTestOparser(object):
  9. OpParser = None
  10. def parse(self, *args, **kwds):
  11. kwds['OpParser'] = self.OpParser
  12. return parse(*args, **kwds)
  13. def test_basic_parse(self):
  14. x = """
  15. [i0, i1]
  16. # a comment
  17. i2 = int_add(i0, i1)
  18. i3 = int_sub(i2, 3) # another comment
  19. finish() # (tricky)
  20. """
  21. loop = self.parse(x)
  22. assert len(loop.operations) == 3
  23. assert [op.getopnum() for op in loop.operations] == [rop.INT_ADD, rop.INT_SUB,
  24. rop.FINISH]
  25. assert len(loop.inputargs) == 2
  26. assert loop.operations[-1].getdescr()
  27. def test_const_ptr_subops(self):
  28. x = """
  29. [p0]
  30. guard_class(p0, ConstClass(vtable)) []
  31. """
  32. S = lltype.Struct('S')
  33. vtable = lltype.nullptr(S)
  34. loop = self.parse(x, None, locals())
  35. assert len(loop.operations) == 1
  36. assert loop.operations[0].getdescr()
  37. assert loop.operations[0].getfailargs() == []
  38. def test_descr(self):
  39. class Xyz(AbstractDescr):
  40. I_am_a_descr = True # for the mock case
  41. x = """
  42. [p0]
  43. i1 = getfield_gc(p0, descr=stuff)
  44. """
  45. stuff = Xyz()
  46. loop = self.parse(x, None, locals())
  47. assert loop.operations[0].getdescr() is stuff
  48. def test_after_fail(self):
  49. x = """
  50. [i0]
  51. guard_value(i0, 3) []
  52. i1 = int_add(1, 2)
  53. """
  54. loop = self.parse(x, None, {})
  55. assert len(loop.operations) == 2
  56. def test_descr_setfield(self):
  57. class Xyz(AbstractDescr):
  58. I_am_a_descr = True # for the mock case
  59. x = """
  60. [p0]
  61. setfield_gc(p0, 3, descr=stuff)
  62. """
  63. stuff = Xyz()
  64. loop = self.parse(x, None, locals())
  65. assert loop.operations[0].getdescr() is stuff
  66. def test_boxname(self):
  67. x = """
  68. [i42]
  69. i50 = int_add(i42, 1)
  70. """
  71. loop = self.parse(x, None, {})
  72. assert str(loop.inputargs[0]) == 'i42'
  73. assert str(loop.operations[0].result) == 'i50'
  74. def test_getboxes(self):
  75. x = """
  76. [i0]
  77. i1 = int_add(i0, 10)
  78. """
  79. loop = self.parse(x, None, {})
  80. boxes = loop.getboxes()
  81. assert boxes.i0 is loop.inputargs[0]
  82. assert boxes.i1 is loop.operations[0].result
  83. def test_setvalues(self):
  84. x = """
  85. [i0]
  86. i1 = int_add(i0, 10)
  87. """
  88. loop = self.parse(x, None, {})
  89. loop.setvalues(i0=32, i1=42)
  90. assert loop.inputargs[0].value == 32
  91. assert loop.operations[0].result.value == 42
  92. def test_getvar_const_ptr(self):
  93. x = '''
  94. []
  95. call(ConstPtr(func_ptr))
  96. '''
  97. TP = lltype.GcArray(lltype.Signed)
  98. NULL = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.nullptr(TP))
  99. loop = self.parse(x, None, {'func_ptr' : NULL})
  100. assert loop.operations[0].getarg(0).value == NULL
  101. def test_jump_target(self):
  102. x = '''
  103. []
  104. jump()
  105. '''
  106. loop = self.parse(x)
  107. assert loop.operations[0].getdescr() is loop.original_jitcell_token
  108. def test_jump_target_other(self):
  109. looptoken = JitCellToken()
  110. looptoken.I_am_a_descr = True # for the mock case
  111. x = '''
  112. []
  113. jump(descr=looptoken)
  114. '''
  115. loop = self.parse(x, namespace=locals())
  116. assert loop.operations[0].getdescr() is looptoken
  117. def test_floats(self):
  118. x = '''
  119. [f0]
  120. f1 = float_add(f0, 3.5)
  121. '''
  122. loop = self.parse(x)
  123. box = loop.operations[0].getarg(0)
  124. # we cannot use isinstance, because in case of mock the class will be
  125. # constructed on the fly
  126. assert box.__class__.__name__ == 'BoxFloat'
  127. def test_debug_merge_point(self):
  128. x = '''
  129. []
  130. debug_merge_point(0, "info")
  131. debug_merge_point(0, 'info')
  132. debug_merge_point(1, '<some ('other.')> info')
  133. debug_merge_point(0, '(stuff) #1')
  134. '''
  135. loop = self.parse(x)
  136. assert loop.operations[0].getarg(1)._get_str() == 'info'
  137. assert loop.operations[1].getarg(1)._get_str() == 'info'
  138. assert loop.operations[2].getarg(1)._get_str() == "<some ('other.')> info"
  139. assert loop.operations[3].getarg(1)._get_str() == "(stuff) #1"
  140. def test_descr_with_obj_print(self):
  141. x = '''
  142. [p0]
  143. setfield_gc(p0, 1, descr=<SomeDescr>)
  144. '''
  145. loop = self.parse(x)
  146. # assert did not explode
  147. example_loop_log = '''\
  148. # bridge out of Guard12, 6 ops
  149. [i0, i1, i2]
  150. i4 = int_add(i0, 2)
  151. i6 = int_sub(i1, 1)
  152. i8 = int_gt(i6, 3)
  153. guard_true(i8, descr=<Guard15>) [i4, i6]
  154. debug_merge_point('(no jitdriver.get_printable_location!)', 0)
  155. jump(i6, i4, descr=<Loop0>)
  156. '''
  157. def test_parse_no_namespace(self):
  158. loop = self.parse(self.example_loop_log, no_namespace=True)
  159. def test_attach_comment_to_loop(self):
  160. loop = self.parse(self.example_loop_log, no_namespace=True)
  161. assert loop.comment == ' # bridge out of Guard12, 6 ops'
  162. def test_parse_new_with_comma(self):
  163. # this is generated by PYPYJITLOG, check that we can handle it
  164. x = '''
  165. []
  166. p0 = new(, descr=<SizeDescr 12>)
  167. '''
  168. loop = self.parse(x)
  169. assert loop.operations[0].getopname() == 'new'
  170. def test_no_fail_args(self):
  171. x = '''
  172. [i0]
  173. guard_true(i0, descr=<Guard0>)
  174. '''
  175. loop = self.parse(x, nonstrict=True)
  176. assert loop.operations[0].getfailargs() == []
  177. def test_no_inputargs(self):
  178. x = '''
  179. i2 = int_add(i0, i1)
  180. '''
  181. loop = self.parse(x, nonstrict=True)
  182. assert loop.inputargs == []
  183. assert loop.operations[0].getopname() == 'int_add'
  184. def test_offsets(self):
  185. x = """
  186. [i0, i1]
  187. +10: i2 = int_add(i0, i1)
  188. i3 = int_add(i2, 3)
  189. """
  190. # +30: --end of the loop--
  191. loop = self.parse(x)
  192. assert loop.operations[0].offset == 10
  193. assert not hasattr(loop.operations[1], 'offset')
  194. def test_last_offset(self):
  195. x = """
  196. [i0, i1]
  197. +10: i2 = int_add(i0, i1)
  198. i3 = int_add(i2, 3)
  199. +30: --end of the loop--
  200. """
  201. loop = self.parse(x)
  202. assert len(loop.operations) == 2
  203. assert loop.last_offset == 30
  204. class TestOpParser(BaseTestOparser):
  205. OpParser = OpParser
  206. def test_boxkind(self):
  207. x = """
  208. [sum0]
  209. """
  210. loop = self.parse(x, None, {}, boxkinds={'sum': BoxInt})
  211. b = loop.getboxes()
  212. assert isinstance(b.sum0, BoxInt)
  213. def test_label(self):
  214. x = """
  215. [i0]
  216. label(i0, descr=1)
  217. jump(i0, descr=1)
  218. """
  219. loop = self.parse(x)
  220. assert loop.operations[0].getdescr() is loop.operations[1].getdescr()
  221. assert isinstance(loop.operations[0].getdescr(), TargetToken)
  222. class ForbiddenModule(object):
  223. def __init__(self, name, old_mod):
  224. self.name = name
  225. self.old_mod = old_mod
  226. def __getattr__(self, attr):
  227. assert False, "You should not import module %s" % self.name
  228. class TestOpParserWithMock(BaseTestOparser):
  229. class OpParser(OpParser):
  230. use_mock_model = True
  231. def setup_class(cls):
  232. forbidden_mods = [
  233. 'pypy.jit.metainterp.history',
  234. 'pypy.rpython.lltypesystem.lltype',
  235. ]
  236. for modname in forbidden_mods:
  237. if modname in sys.modules:
  238. newmod = ForbiddenModule(modname, sys.modules[modname])
  239. sys.modules[modname] = newmod
  240. def teardown_class(cls):
  241. for modname, mod in sys.modules.iteritems():
  242. if isinstance(mod, ForbiddenModule):
  243. sys.modules[modname] = mod.old_mod