PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rlib/jit_hooks.py

https://bitbucket.org/pypy/pypy/
Python | 168 lines | 128 code | 38 blank | 2 comment | 6 complexity | cb297fd4c1cbf199aa2cf09fec43902f MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.annotator import model as annmodel
  2. from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
  3. from rpython.rlib.objectmodel import specialize
  4. from rpython.rtyper.annlowlevel import (cast_instance_to_base_ptr,
  5. cast_base_ptr_to_instance, llstr)
  6. from rpython.rtyper.extregistry import ExtRegistryEntry
  7. from rpython.rtyper.lltypesystem import llmemory, lltype
  8. from rpython.flowspace.model import Constant
  9. from rpython.rtyper import rclass
  10. def register_helper(s_result):
  11. def wrapper(helper):
  12. class Entry(ExtRegistryEntry):
  13. _about_ = helper
  14. def compute_result_annotation(self, *args):
  15. if (isinstance(s_result, annmodel.SomeObject) or
  16. s_result is None):
  17. return s_result
  18. return lltype_to_annotation(s_result)
  19. def specialize_call(self, hop):
  20. from rpython.rtyper.lltypesystem import lltype
  21. c_func = hop.inputconst(lltype.Void, helper)
  22. c_name = hop.inputconst(lltype.Void, 'access_helper')
  23. args_v = [hop.inputarg(arg, arg=i)
  24. for i, arg in enumerate(hop.args_r)]
  25. hop.exception_cannot_occur()
  26. return hop.genop('jit_marker', [c_name, c_func] + args_v,
  27. resulttype=hop.r_result)
  28. return helper
  29. return wrapper
  30. def _cast_to_box(llref):
  31. from rpython.jit.metainterp.history import AbstractValue
  32. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  33. return cast_base_ptr_to_instance(AbstractValue, ptr)
  34. def _cast_to_resop(llref):
  35. from rpython.jit.metainterp.resoperation import AbstractResOp
  36. ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref)
  37. return cast_base_ptr_to_instance(AbstractResOp, ptr)
  38. @specialize.argtype(0)
  39. def _cast_to_gcref(obj):
  40. return lltype.cast_opaque_ptr(llmemory.GCREF,
  41. cast_instance_to_base_ptr(obj))
  42. def emptyval():
  43. return lltype.nullptr(llmemory.GCREF.TO)
  44. @register_helper(SomePtr(llmemory.GCREF))
  45. def resop_new(no, llargs, llres):
  46. from rpython.jit.metainterp.history import ResOperation
  47. args = [_cast_to_box(llargs[i]) for i in range(len(llargs))]
  48. if llres:
  49. res = _cast_to_box(llres)
  50. else:
  51. res = None
  52. return _cast_to_gcref(ResOperation(no, args, res))
  53. @register_helper(annmodel.SomeInteger())
  54. def resop_getopnum(llop):
  55. return _cast_to_resop(llop).getopnum()
  56. @register_helper(annmodel.SomeString(can_be_None=True))
  57. def resop_getopname(llop):
  58. return llstr(_cast_to_resop(llop).getopname())
  59. @register_helper(SomePtr(llmemory.GCREF))
  60. def resop_getarg(llop, no):
  61. return _cast_to_gcref(_cast_to_resop(llop).getarg(no))
  62. @register_helper(annmodel.s_None)
  63. def resop_setarg(llop, no, llbox):
  64. _cast_to_resop(llop).setarg(no, _cast_to_box(llbox))
  65. @register_helper(annmodel.SomeInteger())
  66. def box_getint(llbox):
  67. return _cast_to_box(llbox).getint()
  68. @register_helper(SomePtr(llmemory.GCREF))
  69. def box_clone(llbox):
  70. return _cast_to_gcref(_cast_to_box(llbox).clonebox())
  71. @register_helper(SomePtr(llmemory.GCREF))
  72. def box_constbox(llbox):
  73. return _cast_to_gcref(_cast_to_box(llbox).constbox())
  74. @register_helper(SomePtr(llmemory.GCREF))
  75. def box_nonconstbox(llbox):
  76. return _cast_to_gcref(_cast_to_box(llbox).nonconstbox())
  77. @register_helper(annmodel.SomeBool())
  78. def box_isconst(llbox):
  79. from rpython.jit.metainterp.history import Const
  80. return isinstance(_cast_to_box(llbox), Const)
  81. @register_helper(annmodel.SomeBool())
  82. def box_isint(llbox):
  83. from rpython.jit.metainterp.history import INT
  84. return _cast_to_box(llbox).type == INT
  85. # ------------------------- stats interface ---------------------------
  86. @register_helper(annmodel.SomeBool())
  87. def stats_set_debug(warmrunnerdesc, flag):
  88. return warmrunnerdesc.metainterp_sd.cpu.set_debug(flag)
  89. @register_helper(annmodel.SomeInteger())
  90. def stats_get_counter_value(warmrunnerdesc, no):
  91. return warmrunnerdesc.metainterp_sd.profiler.get_counter(no)
  92. @register_helper(annmodel.SomeFloat())
  93. def stats_get_times_value(warmrunnerdesc, no):
  94. return warmrunnerdesc.metainterp_sd.profiler.get_times(no)
  95. LOOP_RUN_CONTAINER = lltype.GcArray(lltype.Struct('elem',
  96. ('type', lltype.Char),
  97. ('number', lltype.Signed),
  98. ('counter', lltype.Signed)))
  99. @register_helper(lltype.Ptr(LOOP_RUN_CONTAINER))
  100. def stats_get_loop_run_times(warmrunnerdesc):
  101. return warmrunnerdesc.metainterp_sd.cpu.get_all_loop_runs()
  102. @register_helper(annmodel.SomeInteger(unsigned=True))
  103. def stats_asmmemmgr_allocated(warmrunnerdesc):
  104. return warmrunnerdesc.metainterp_sd.cpu.asmmemmgr.get_stats()[0]
  105. @register_helper(annmodel.SomeInteger(unsigned=True))
  106. def stats_asmmemmgr_used(warmrunnerdesc):
  107. return warmrunnerdesc.metainterp_sd.cpu.asmmemmgr.get_stats()[1]
  108. # ---------------------- jitcell interface ----------------------
  109. def _new_hook(name, resulttype):
  110. def hook(name, *greenkey):
  111. raise Exception("need to run translated")
  112. hook.func_name = name
  113. class GetJitCellEntry(ExtRegistryEntry):
  114. _about_ = hook
  115. def compute_result_annotation(self, s_name, *args_s):
  116. assert s_name.is_constant()
  117. return resulttype
  118. def specialize_call(self, hop):
  119. c_jitdriver = Constant(hop.args_s[0].const, concretetype=lltype.Void)
  120. c_name = Constant(name, concretetype=lltype.Void)
  121. hop.exception_cannot_occur()
  122. args_v = [hop.inputarg(arg, arg=i + 1)
  123. for i, arg in enumerate(hop.args_r[1:])]
  124. return hop.genop('jit_marker', [c_name, c_jitdriver] + args_v,
  125. resulttype=hop.r_result)
  126. return hook
  127. get_jitcell_at_key = _new_hook('get_jitcell_at_key', SomePtr(llmemory.GCREF))
  128. trace_next_iteration = _new_hook('trace_next_iteration', None)
  129. dont_trace_here = _new_hook('dont_trace_here', None)
  130. trace_next_iteration_hash = _new_hook('trace_next_iteration_hash', None)