| 1 |
from rpython.rtyper.llinterp import LLInterpreter |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 4 |
|
| 5 |
def maybe_on_top_of_llinterp(rtyper, fnptr): |
| 6 |
|
| 7 |
|
| 8 |
def process_args(args): |
| 9 |
real_args = [] |
| 10 |
ARGS = lltype.typeOf(funcobj).ARGS |
| 11 |
i = 0 |
| 12 |
for ARG in ARGS: |
| 13 |
if ARG is lltype.Void: |
| 14 |
real_args.append(None) |
| 15 |
else: |
| 16 |
if ARG is lltype.Float: |
| 17 |
real_args.append(args[i]) |
| 18 |
elif isinstance(ARG, lltype.Primitive): |
| 19 |
real_args.append(lltype.cast_primitive(ARG, args[i])) |
| 20 |
elif isinstance(ARG, lltype.Ptr): |
| 21 |
if ARG.TO._gckind == 'gc': |
| 22 |
real_args.append(lltype.cast_opaque_ptr(ARG, args[i])) |
| 23 |
else: |
| 24 |
real_args.append(rffi.cast(ARG, args[i])) |
| 25 |
else: |
| 26 |
raise Exception("Unexpected arg: %s" % ARG) |
| 27 |
i += 1 |
| 28 |
return real_args |
| 29 |
|
| 30 |
funcobj = fnptr._obj |
| 31 |
if hasattr(funcobj, 'graph'): |
| 32 |
|
| 33 |
|
| 34 |
try: |
| 35 |
llinterp = rtyper._on_top_of_llinterp_llinterp |
| 36 |
except AttributeError: |
| 37 |
llinterp = LLInterpreter(rtyper) |
| 38 |
rtyper._on_top_of_llinterp_llinterp = llinterp |
| 39 |
def on_top_of_llinterp(*args): |
| 40 |
real_args = process_args(args) |
| 41 |
return llinterp.eval_graph(funcobj.graph, real_args) |
| 42 |
else: |
| 43 |
assert hasattr(funcobj, '_callable') |
| 44 |
def on_top_of_llinterp(*args): |
| 45 |
args = process_args(args) |
| 46 |
return funcobj._callable(*args) |
| 47 |
return on_top_of_llinterp |
| 48 |
|
| 49 |
class Entry(ExtRegistryEntry): |
| 50 |
_about_ = maybe_on_top_of_llinterp |
| 51 |
def compute_result_annotation(self, s_rtyper, s_fnptr): |
| 52 |
return s_fnptr |
| 53 |
def specialize_call(self, hop): |
| 54 |
hop.exception_cannot_occur() |
| 55 |
return hop.inputarg(hop.args_r[1], arg=1) |