PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/rpython/rtyper/lltypesystem/test/test_lloperation.py

https://bitbucket.org/pypy/pypy/
Python | 151 lines | 126 code | 12 blank | 13 comment | 11 complexity | d846f68babe990eb95015632814c83d8 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS, llop, void
  3. from rpython.rtyper.lltypesystem import lltype, opimpl
  4. from rpython.rtyper.llinterp import LLFrame
  5. from rpython.rtyper.test.test_llinterp import interpret
  6. from rpython.rtyper import rclass
  7. from rpython.rlib.rarithmetic import LONGLONG_MASK, r_longlong, r_ulonglong
  8. LL_INTERP_OPERATIONS = [name[3:] for name in LLFrame.__dict__.keys()
  9. if name.startswith('op_')]
  10. # ____________________________________________________________
  11. def test_canfold_opimpl_complete():
  12. for opname, llop in LL_OPERATIONS.items():
  13. assert opname == llop.opname
  14. if llop.canfold:
  15. func = opimpl.get_op_impl(opname)
  16. assert callable(func)
  17. def test_llop_fold():
  18. assert llop.int_add(lltype.Signed, 10, 2) == 12
  19. assert llop.int_add(lltype.Signed, -6, -7) == -13
  20. S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
  21. s1 = lltype.malloc(S1)
  22. s1.x = 123
  23. assert llop.getfield(lltype.Signed, s1, 'x') == 123
  24. S2 = lltype.GcStruct('S2', ('x', lltype.Signed))
  25. s2 = lltype.malloc(S2)
  26. s2.x = 123
  27. py.test.raises(TypeError, "llop.getfield(lltype.Signed, s2, 'x')")
  28. def test_llop_interp():
  29. from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy
  30. def llf(x, y):
  31. return llop.int_add(lltype.Signed, x, y)
  32. res = interpret(llf, [5, 7], policy=LowLevelAnnotatorPolicy())
  33. assert res == 12
  34. def test_llop_with_voids_interp():
  35. from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy
  36. S = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
  37. name_y = void('y')
  38. def llf():
  39. s = lltype.malloc(S)
  40. llop.bare_setfield(lltype.Void, s, void('x'), 3)
  41. llop.bare_setfield(lltype.Void, s, name_y, 2)
  42. return s.x + s.y
  43. res = interpret(llf, [], policy=LowLevelAnnotatorPolicy())
  44. assert res == 5
  45. def test_is_pure():
  46. from rpython.flowspace.model import Variable, Constant
  47. assert llop.bool_not.is_pure([Variable()])
  48. assert llop.debug_assert.is_pure([Variable()])
  49. assert not llop.int_add_ovf.is_pure([Variable(), Variable()])
  50. #
  51. S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
  52. v_s1 = Variable()
  53. v_s1.concretetype = lltype.Ptr(S1)
  54. assert not llop.setfield.is_pure([v_s1, Constant('x'), Variable()])
  55. assert not llop.getfield.is_pure([v_s1, Constant('y')])
  56. #
  57. A1 = lltype.GcArray(lltype.Signed)
  58. v_a1 = Variable()
  59. v_a1.concretetype = lltype.Ptr(A1)
  60. assert not llop.setarrayitem.is_pure([v_a1, Variable(), Variable()])
  61. assert not llop.getarrayitem.is_pure([v_a1, Variable()])
  62. assert llop.getarraysize.is_pure([v_a1])
  63. #
  64. S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
  65. hints={'immutable': True})
  66. v_s2 = Variable()
  67. v_s2.concretetype = lltype.Ptr(S2)
  68. assert not llop.setfield.is_pure([v_s2, Constant('x'), Variable()])
  69. assert llop.getfield.is_pure([v_s2, Constant('y')])
  70. #
  71. A2 = lltype.GcArray(lltype.Signed, hints={'immutable': True})
  72. v_a2 = Variable()
  73. v_a2.concretetype = lltype.Ptr(A2)
  74. assert not llop.setarrayitem.is_pure([v_a2, Variable(), Variable()])
  75. assert llop.getarrayitem.is_pure([v_a2, Variable()])
  76. assert llop.getarraysize.is_pure([v_a2])
  77. #
  78. for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
  79. rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
  80. rclass.IR_QUASIIMMUTABLE_ARRAY]:
  81. accessor = rclass.FieldListAccessor()
  82. S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
  83. hints={'immutable_fields': accessor})
  84. accessor.initialize(S3, {'x': kind})
  85. v_s3 = Variable()
  86. v_s3.concretetype = lltype.Ptr(S3)
  87. assert not llop.setfield.is_pure([v_s3, Constant('x'), Variable()])
  88. assert not llop.setfield.is_pure([v_s3, Constant('y'), Variable()])
  89. assert llop.getfield.is_pure([v_s3, Constant('x')]) is kind
  90. assert not llop.getfield.is_pure([v_s3, Constant('y')])
  91. def test_getfield_pure():
  92. S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
  93. S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
  94. hints={'immutable': True})
  95. accessor = rclass.FieldListAccessor()
  96. #
  97. s1 = lltype.malloc(S1); s1.x = 45
  98. py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
  99. s2 = lltype.malloc(S2); s2.x = 45
  100. assert llop.getfield(lltype.Signed, s2, 'x') == 45
  101. #
  102. py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
  103. assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
  104. #
  105. for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
  106. rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
  107. rclass.IR_QUASIIMMUTABLE_ARRAY]:
  108. #
  109. S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
  110. hints={'immutable_fields': accessor})
  111. accessor.initialize(S3, {'x': kind})
  112. s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47
  113. if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
  114. assert llop.getfield(lltype.Signed, s3, 'x') == 46
  115. assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
  116. else:
  117. py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
  118. py.test.raises(TypeError, llop.getinteriorfield,
  119. lltype.Signed, s3, 'x')
  120. py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
  121. py.test.raises(TypeError, llop.getinteriorfield,
  122. lltype.Signed, s3, 'y')
  123. def test_cast_float_to_ulonglong():
  124. f = 12350000000000000000.0
  125. py.test.raises(OverflowError, r_longlong, f)
  126. r_longlong(f / 2) # does not raise OverflowError
  127. #
  128. x = llop.cast_float_to_ulonglong(lltype.UnsignedLongLong, f)
  129. assert x == r_ulonglong(f)
  130. # ___________________________________________________________________________
  131. # This tests that the LLInterpreter and the LL_OPERATIONS tables are in sync.
  132. def test_table_complete():
  133. for opname in LL_INTERP_OPERATIONS:
  134. assert opname in LL_OPERATIONS
  135. def test_llinterp_complete():
  136. for opname, llop in LL_OPERATIONS.items():
  137. if llop.canrun:
  138. continue
  139. assert opname in LL_INTERP_OPERATIONS