PageRenderTime 53ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rlib/jit_hooks.py

http://github.com/pypy/pypy
Python | 110 lines | 85 code | 25 blank | 0 comment | 4 complexity | 6b4e7c28ba51ad879fefb69483155f7c MD5 | raw file
  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. hop.exception_cannot_occur()
  21. return hop.genop('jit_marker', [c_name, c_func] + args_v,
  22. resulttype=hop.r_result)
  23. return helper
  24. return wrapper
  25. def _cast_to_box(llref):
  26. from pypy.jit.metainterp.history import AbstractValue
  27. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  28. return cast_base_ptr_to_instance(AbstractValue, ptr)
  29. def _cast_to_resop(llref):
  30. from pypy.jit.metainterp.resoperation import AbstractResOp
  31. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  32. return cast_base_ptr_to_instance(AbstractResOp, ptr)
  33. @specialize.argtype(0)
  34. def _cast_to_gcref(obj):
  35. return lltype.cast_opaque_ptr(llmemory.GCREF,
  36. cast_instance_to_base_ptr(obj))
  37. def emptyval():
  38. return lltype.nullptr(llmemory.GCREF.TO)
  39. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  40. def resop_new(no, llargs, llres):
  41. from pypy.jit.metainterp.history import ResOperation
  42. args = [_cast_to_box(llargs[i]) for i in range(len(llargs))]
  43. if llres:
  44. res = _cast_to_box(llres)
  45. else:
  46. res = None
  47. return _cast_to_gcref(ResOperation(no, args, res))
  48. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  49. def boxint_new(no):
  50. from pypy.jit.metainterp.history import BoxInt
  51. return _cast_to_gcref(BoxInt(no))
  52. @register_helper(annmodel.SomeInteger())
  53. def resop_getopnum(llop):
  54. return _cast_to_resop(llop).getopnum()
  55. @register_helper(annmodel.SomeString(can_be_None=True))
  56. def resop_getopname(llop):
  57. return llstr(_cast_to_resop(llop).getopname())
  58. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  59. def resop_getarg(llop, no):
  60. return _cast_to_gcref(_cast_to_resop(llop).getarg(no))
  61. @register_helper(annmodel.s_None)
  62. def resop_setarg(llop, no, llbox):
  63. _cast_to_resop(llop).setarg(no, _cast_to_box(llbox))
  64. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  65. def resop_getresult(llop):
  66. return _cast_to_gcref(_cast_to_resop(llop).result)
  67. @register_helper(annmodel.s_None)
  68. def resop_setresult(llop, llbox):
  69. _cast_to_resop(llop).result = _cast_to_box(llbox)
  70. @register_helper(annmodel.SomeInteger())
  71. def box_getint(llbox):
  72. return _cast_to_box(llbox).getint()
  73. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  74. def box_clone(llbox):
  75. return _cast_to_gcref(_cast_to_box(llbox).clonebox())
  76. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  77. def box_constbox(llbox):
  78. return _cast_to_gcref(_cast_to_box(llbox).constbox())
  79. @register_helper(annmodel.SomePtr(llmemory.GCREF))
  80. def box_nonconstbox(llbox):
  81. return _cast_to_gcref(_cast_to_box(llbox).nonconstbox())
  82. @register_helper(annmodel.SomeBool())
  83. def box_isconst(llbox):
  84. from pypy.jit.metainterp.history import Const
  85. return isinstance(_cast_to_box(llbox), Const)