| 1 |
from rpython.annotator import model as annmodel |
| 2 |
from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation |
| 3 |
from rpython.rtyper.annlowlevel import ( |
| 4 |
cast_instance_to_gcref, cast_gcref_to_instance, llstr) |
| 5 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 6 |
from rpython.rtyper.lltypesystem import llmemory, lltype |
| 7 |
from rpython.flowspace.model import Constant |
| 8 |
|
| 9 |
|
| 10 |
def register_helper(s_result): |
| 11 |
def wrapper(helper): |
| 12 |
class Entry(ExtRegistryEntry): |
| 13 |
_about_ = helper |
| 14 |
|
| 15 |
def compute_result_annotation(self, *args): |
| 16 |
if (isinstance(s_result, annmodel.SomeObject) or |
| 17 |
s_result is None): |
| 18 |
return s_result |
| 19 |
return lltype_to_annotation(s_result) |
| 20 |
|
| 21 |
def specialize_call(self, hop): |
| 22 |
from rpython.rtyper.lltypesystem import lltype |
| 23 |
|
| 24 |
c_func = hop.inputconst(lltype.Void, helper) |
| 25 |
c_name = hop.inputconst(lltype.Void, 'access_helper') |
| 26 |
args_v = [hop.inputarg(arg, arg=i) |
| 27 |
for i, arg in enumerate(hop.args_r)] |
| 28 |
hop.exception_cannot_occur() |
| 29 |
return hop.genop('jit_marker', [c_name, c_func] + args_v, |
| 30 |
resulttype=hop.r_result) |
| 31 |
return helper |
| 32 |
return wrapper |
| 33 |
|
| 34 |
def _cast_to_box(llref): |
| 35 |
from rpython.jit.metainterp.history import AbstractValue |
| 36 |
return cast_gcref_to_instance(AbstractValue, llref) |
| 37 |
|
| 38 |
def _cast_to_resop(llref): |
| 39 |
from rpython.jit.metainterp.resoperation import AbstractResOp |
| 40 |
return cast_gcref_to_instance(AbstractResOp, llref) |
| 41 |
|
| 42 |
_cast_to_gcref = cast_instance_to_gcref |
| 43 |
|
| 44 |
def emptyval(): |
| 45 |
return lltype.nullptr(llmemory.GCREF.TO) |
| 46 |
|
| 47 |
@register_helper(SomePtr(llmemory.GCREF)) |
| 48 |
def resop_new(no, llargs, llres): |
| 49 |
from rpython.jit.metainterp.history import ResOperation |
| 50 |
|
| 51 |
args = [_cast_to_box(llargs[i]) for i in range(len(llargs))] |
| 52 |
if llres: |
| 53 |
res = _cast_to_box(llres) |
| 54 |
else: |
| 55 |
res = None |
| 56 |
return _cast_to_gcref(ResOperation(no, args, res)) |
| 57 |
|
| 58 |
@register_helper(annmodel.SomeInteger()) |
| 59 |
def resop_getopnum(llop): |
| 60 |
return _cast_to_resop(llop).getopnum() |
| 61 |
|
| 62 |
@register_helper(annmodel.SomeString(can_be_None=True)) |
| 63 |
def resop_getopname(llop): |
| 64 |
return llstr(_cast_to_resop(llop).getopname()) |
| 65 |
|
| 66 |
@register_helper(SomePtr(llmemory.GCREF)) |
| 67 |
def resop_getarg(llop, no): |
| 68 |
return _cast_to_gcref(_cast_to_resop(llop).getarg(no)) |
| 69 |
|
| 70 |
@register_helper(annmodel.s_None) |
| 71 |
def resop_setarg(llop, no, llbox): |
| 72 |
_cast_to_resop(llop).setarg(no, _cast_to_box(llbox)) |
| 73 |
|
| 74 |
@register_helper(annmodel.SomeInteger()) |
| 75 |
def box_getint(llbox): |
| 76 |
return _cast_to_box(llbox).getint() |
| 77 |
|
| 78 |
@register_helper(SomePtr(llmemory.GCREF)) |
| 79 |
def box_clone(llbox): |
| 80 |
return _cast_to_gcref(_cast_to_box(llbox).clonebox()) |
| 81 |
|
| 82 |
@register_helper(SomePtr(llmemory.GCREF)) |
| 83 |
def box_constbox(llbox): |
| 84 |
return _cast_to_gcref(_cast_to_box(llbox).constbox()) |
| 85 |
|
| 86 |
@register_helper(SomePtr(llmemory.GCREF)) |
| 87 |
def box_nonconstbox(llbox): |
| 88 |
return _cast_to_gcref(_cast_to_box(llbox).nonconstbox()) |
| 89 |
|
| 90 |
@register_helper(annmodel.SomeBool()) |
| 91 |
def box_isconst(llbox): |
| 92 |
from rpython.jit.metainterp.history import Const |
| 93 |
return isinstance(_cast_to_box(llbox), Const) |
| 94 |
|
| 95 |
@register_helper(annmodel.SomeBool()) |
| 96 |
def box_isint(llbox): |
| 97 |
from rpython.jit.metainterp.history import INT |
| 98 |
return _cast_to_box(llbox).type == INT |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@register_helper(annmodel.SomeBool()) |
| 103 |
def stats_set_debug(warmrunnerdesc, flag): |
| 104 |
return warmrunnerdesc.metainterp_sd.cpu.set_debug(flag) |
| 105 |
|
| 106 |
@register_helper(annmodel.SomeInteger()) |
| 107 |
def stats_get_counter_value(warmrunnerdesc, no): |
| 108 |
return warmrunnerdesc.metainterp_sd.profiler.get_counter(no) |
| 109 |
|
| 110 |
@register_helper(annmodel.SomeFloat()) |
| 111 |
def stats_get_times_value(warmrunnerdesc, no): |
| 112 |
return warmrunnerdesc.metainterp_sd.profiler.get_times(no) |
| 113 |
|
| 114 |
LOOP_RUN_CONTAINER = lltype.GcArray(lltype.Struct('elem', |
| 115 |
('type', lltype.Char), |
| 116 |
('number', lltype.Signed), |
| 117 |
('counter', lltype.Signed))) |
| 118 |
|
| 119 |
@register_helper(lltype.Ptr(LOOP_RUN_CONTAINER)) |
| 120 |
def stats_get_loop_run_times(warmrunnerdesc): |
| 121 |
return warmrunnerdesc.metainterp_sd.cpu.get_all_loop_runs() |
| 122 |
|
| 123 |
@register_helper(annmodel.SomeInteger(unsigned=True)) |
| 124 |
def stats_asmmemmgr_allocated(warmrunnerdesc): |
| 125 |
return warmrunnerdesc.metainterp_sd.cpu.asmmemmgr.get_stats()[0] |
| 126 |
|
| 127 |
@register_helper(annmodel.SomeInteger(unsigned=True)) |
| 128 |
def stats_asmmemmgr_used(warmrunnerdesc): |
| 129 |
return warmrunnerdesc.metainterp_sd.cpu.asmmemmgr.get_stats()[1] |
| 130 |
|
| 131 |
@register_helper(None) |
| 132 |
def stats_memmgr_release_all(warmrunnerdesc): |
| 133 |
warmrunnerdesc.memory_manager.release_all_loops() |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
def _new_hook(name, resulttype): |
| 138 |
def hook(name, *greenkey): |
| 139 |
raise Exception("need to run translated") |
| 140 |
hook.__name__ = name |
| 141 |
|
| 142 |
class GetJitCellEntry(ExtRegistryEntry): |
| 143 |
_about_ = hook |
| 144 |
|
| 145 |
def compute_result_annotation(self, s_name, *args_s): |
| 146 |
assert s_name.is_constant() |
| 147 |
return resulttype |
| 148 |
|
| 149 |
def specialize_call(self, hop): |
| 150 |
c_jitdriver = Constant(hop.args_s[0].const, concretetype=lltype.Void) |
| 151 |
c_name = Constant(name, concretetype=lltype.Void) |
| 152 |
hop.exception_cannot_occur() |
| 153 |
args_v = [hop.inputarg(arg, arg=i + 1) |
| 154 |
for i, arg in enumerate(hop.args_r[1:])] |
| 155 |
return hop.genop('jit_marker', [c_name, c_jitdriver] + args_v, |
| 156 |
resulttype=hop.r_result) |
| 157 |
|
| 158 |
return hook |
| 159 |
|
| 160 |
get_jitcell_at_key = _new_hook('get_jitcell_at_key', SomePtr(llmemory.GCREF)) |
| 161 |
trace_next_iteration = _new_hook('trace_next_iteration', None) |
| 162 |
dont_trace_here = _new_hook('dont_trace_here', None) |
| 163 |
mark_as_being_traced = _new_hook('mark_as_being_traced', None) |
| 164 |
trace_next_iteration_hash = _new_hook('trace_next_iteration_hash', None) |