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

/pypy/rlib/jit_hooks.py

https://bitbucket.org/pypy/pypy/
Python | 109 lines | 84 code | 25 blank | 0 comment | 4 complexity | 8568cd4e74fac8694e6e10b54d5c7142 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from pypy.rpython.extregistry import ExtRegistryEntry
  2. from pypy.annotation import model as annmodel
  3. from pypy.rpython.lltypesystem import llmemory, lltype
  4. from pypy.rpython.lltypesystem import rclass
  5. from pypy.rpython.annlowlevel import cast_instance_to_base_ptr,\
  6. cast_base_ptr_to_instance, llstr, hlstr
  7. from pypy.rlib.objectmodel import specialize
  8. def register_helper(s_result):
  9. def wrapper(helper):
  10. class Entry(ExtRegistryEntry):
  11. _about_ = helper
  12. def compute_result_annotation(self, *args):
  13. return s_result
  14. def specialize_call(self, hop):
  15. from pypy.rpython.lltypesystem import lltype
  16. c_func = hop.inputconst(lltype.Void, helper)
  17. c_name = hop.inputconst(lltype.Void, 'access_helper')
  18. args_v = [hop.inputarg(arg, arg=i)
  19. for i, arg in enumerate(hop.args_r)]
  20. return hop.genop('jit_marker', [c_name, c_func] + args_v,
  21. resulttype=hop.r_result)
  22. return helper
  23. return wrapper
  24. def _cast_to_box(llref):
  25. from pypy.jit.metainterp.history import AbstractValue
  26. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  27. return cast_base_ptr_to_instance(AbstractValue, ptr)
  28. def _cast_to_resop(llref):
  29. from pypy.jit.metainterp.resoperation import AbstractResOp
  30. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  31. return cast_base_ptr_to_instance(AbstractResOp, ptr)
  32. @specialize.argtype(0)
  33. def _cast_to_gcref(obj):
  34. return lltype.cast_opaque_ptr(llmemory.GCREF,
  35. cast_instance_to_base_ptr(obj))
  36. def emptyval():
  37. return lltype.nullptr(llmemory.GCREF.TO)
  38. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  39. def resop_new(no, llargs, llres):
  40. from pypy.jit.metainterp.history import ResOperation
  41. args = [_cast_to_box(llargs[i]) for i in range(len(llargs))]
  42. if llres:
  43. res = _cast_to_box(llres)
  44. else:
  45. res = None
  46. return _cast_to_gcref(ResOperation(no, args, res))
  47. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  48. def boxint_new(no):
  49. from pypy.jit.metainterp.history import BoxInt
  50. return _cast_to_gcref(BoxInt(no))
  51. @register_helper(annmodel.SomeInteger())
  52. def resop_getopnum(llop):
  53. return _cast_to_resop(llop).getopnum()
  54. @register_helper(annmodel.SomeString(can_be_None=True))
  55. def resop_getopname(llop):
  56. return llstr(_cast_to_resop(llop).getopname())
  57. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  58. def resop_getarg(llop, no):
  59. return _cast_to_gcref(_cast_to_resop(llop).getarg(no))
  60. @register_helper(annmodel.s_None)
  61. def resop_setarg(llop, no, llbox):
  62. _cast_to_resop(llop).setarg(no, _cast_to_box(llbox))
  63. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  64. def resop_getresult(llop):
  65. return _cast_to_gcref(_cast_to_resop(llop).result)
  66. @register_helper(annmodel.s_None)
  67. def resop_setresult(llop, llbox):
  68. _cast_to_resop(llop).result = _cast_to_box(llbox)
  69. @register_helper(annmodel.SomeInteger())
  70. def box_getint(llbox):
  71. return _cast_to_box(llbox).getint()
  72. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  73. def box_clone(llbox):
  74. return _cast_to_gcref(_cast_to_box(llbox).clonebox())
  75. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  76. def box_constbox(llbox):
  77. return _cast_to_gcref(_cast_to_box(llbox).constbox())
  78. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  79. def box_nonconstbox(llbox):
  80. return _cast_to_gcref(_cast_to_box(llbox).nonconstbox())
  81. @register_helper(annmodel.SomeBool())
  82. def box_isconst(llbox):
  83. from pypy.jit.metainterp.history import Const
  84. return isinstance(_cast_to_box(llbox), Const)