PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/jit/tool/oparser_model.py

https://bitbucket.org/pypy/pypy/
Python | 172 lines | 133 code | 39 blank | 0 comment | 11 complexity | f1dc5d765fc57e493bd0f956f63ac0db MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. class Boxes(object):
  2. pass
  3. def get_real_model():
  4. class LoopModel(object):
  5. from rpython.jit.metainterp.history import TreeLoop, JitCellToken
  6. from rpython.jit.metainterp.history import ConstInt, ConstPtr, ConstFloat
  7. from rpython.jit.metainterp.history import BasicFailDescr, BasicFinalDescr, TargetToken
  8. from rpython.jit.metainterp.typesystem import llhelper
  9. from rpython.jit.metainterp.opencoder import Trace
  10. from rpython.jit.metainterp.history import get_const_ptr_for_string
  11. from rpython.jit.metainterp.history import get_const_ptr_for_unicode
  12. get_const_ptr_for_string = staticmethod(get_const_ptr_for_string)
  13. get_const_ptr_for_unicode = staticmethod(get_const_ptr_for_unicode)
  14. @staticmethod
  15. def convert_to_floatstorage(arg):
  16. from rpython.jit.codewriter import longlong
  17. return longlong.getfloatstorage(float(arg))
  18. @staticmethod
  19. def ptr_to_int(obj):
  20. from rpython.jit.codewriter.heaptracker import adr2int
  21. from rpython.rtyper.lltypesystem import llmemory
  22. return adr2int(llmemory.cast_ptr_to_adr(obj))
  23. return LoopModel
  24. def get_mock_model():
  25. class MockLoopModel(object):
  26. class TreeLoop(object):
  27. def __init__(self, name):
  28. self.name = name
  29. class JitCellToken(object):
  30. I_am_a_descr = True
  31. class TargetToken(object):
  32. def __init__(self, jct):
  33. pass
  34. class BasicFailDescr(object):
  35. I_am_a_descr = True
  36. final_descr = False
  37. class BasicFinalDescr(object):
  38. I_am_a_descr = True
  39. final_descr = True
  40. class Box(object):
  41. _counter = 0
  42. type = 'b'
  43. def __init__(self, value=0):
  44. self.value = value
  45. def __repr__(self):
  46. result = str(self)
  47. result += '(%s)' % self.value
  48. return result
  49. def __str__(self):
  50. if not hasattr(self, '_str'):
  51. self._str = '%s%d' % (self.type, Box._counter)
  52. Box._counter += 1
  53. return self._str
  54. class BoxInt(Box):
  55. type = 'i'
  56. class BoxFloat(Box):
  57. type = 'f'
  58. class BoxRef(Box):
  59. type = 'p'
  60. class BoxVector(Box):
  61. type = 'V'
  62. class Const(object):
  63. bytesize = 8
  64. signed = True
  65. def __init__(self, value=None):
  66. self.value = value
  67. def _get_str(self):
  68. return str(self.value)
  69. def is_constant(self):
  70. return True
  71. class ConstInt(Const):
  72. datatype = 'i'
  73. pass
  74. class ConstPtr(Const):
  75. datatype = 'r'
  76. pass
  77. class ConstFloat(Const):
  78. datatype = 'f'
  79. signed = False
  80. pass
  81. @classmethod
  82. def get_const_ptr_for_string(cls, s):
  83. return cls.ConstPtr(s)
  84. @classmethod
  85. def get_const_ptr_for_unicode(cls, s):
  86. return cls.ConstPtr(s)
  87. @staticmethod
  88. def convert_to_floatstorage(arg):
  89. return float(arg)
  90. @staticmethod
  91. def ptr_to_int(obj):
  92. return id(obj)
  93. class llhelper(object):
  94. pass
  95. MockLoopModel.llhelper.BoxRef = MockLoopModel.BoxRef
  96. return MockLoopModel
  97. def get_model(use_mock):
  98. if use_mock:
  99. model = get_mock_model()
  100. else:
  101. model = get_real_model()
  102. class ExtendedTreeLoop(model.TreeLoop):
  103. def as_json(self):
  104. return {
  105. 'comment': self.comment,
  106. 'name': self.name,
  107. 'operations': [op.as_json() for op in self.operations],
  108. 'inputargs': self.inputargs,
  109. 'last_offset': self.last_offset
  110. }
  111. def getboxes(self):
  112. def opboxes(operations):
  113. for op in operations:
  114. yield op.result
  115. for box in op.getarglist():
  116. yield box
  117. def allboxes():
  118. for box in self.inputargs:
  119. yield box
  120. for box in opboxes(self.operations):
  121. yield box
  122. boxes = Boxes()
  123. for box in allboxes():
  124. if isinstance(box, model.Box):
  125. name = str(box)
  126. setattr(boxes, name, box)
  127. return boxes
  128. def setvalues(self, **kwds):
  129. boxes = self.getboxes()
  130. for name, value in kwds.iteritems():
  131. getattr(boxes, name).value = value
  132. model.ExtendedTreeLoop = ExtendedTreeLoop
  133. return model