| 1 |
|
| 2 |
from rpython.annotator.specialize import default_specialize as default |
| 3 |
from rpython.annotator.specialize import ( |
| 4 |
specialize_argvalue, specialize_argtype, specialize_arglistitemtype, |
| 5 |
specialize_arg_or_var, memo, specialize_call_location) |
| 6 |
from rpython.flowspace.operation import op |
| 7 |
from rpython.flowspace.model import Constant |
| 8 |
from rpython.annotator.model import SomeTuple |
| 9 |
|
| 10 |
|
| 11 |
class AnnotatorPolicy(object): |
| 12 |
""" |
| 13 |
Possibly subclass and pass an instance to the annotator to control |
| 14 |
special-casing during annotation |
| 15 |
""" |
| 16 |
|
| 17 |
def event(pol, bookkeeper, what, *args): |
| 18 |
pass |
| 19 |
|
| 20 |
def get_specializer(pol, directive): |
| 21 |
if directive is None: |
| 22 |
return pol.default_specialize |
| 23 |
|
| 24 |
|
| 25 |
directive_parts = directive.split('(', 1) |
| 26 |
if len(directive_parts) == 1: |
| 27 |
[name] = directive_parts |
| 28 |
parms = () |
| 29 |
else: |
| 30 |
name, parms = directive_parts |
| 31 |
try: |
| 32 |
parms = eval("(lambda *parms: parms)(%s" % parms) |
| 33 |
except (KeyboardInterrupt, SystemExit): |
| 34 |
raise |
| 35 |
except: |
| 36 |
raise Exception("broken specialize directive parms: %s" % directive) |
| 37 |
name = name.replace(':', '__') |
| 38 |
try: |
| 39 |
specializer = getattr(pol, name) |
| 40 |
except AttributeError: |
| 41 |
raise AttributeError("%r specialize tag not defined in annotation" |
| 42 |
"policy %s" % (name, pol)) |
| 43 |
else: |
| 44 |
if not parms: |
| 45 |
return specializer |
| 46 |
else: |
| 47 |
def specialize_with_parms(funcdesc, args_s): |
| 48 |
return specializer(funcdesc, args_s, *parms) |
| 49 |
return specialize_with_parms |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
default_specialize = staticmethod(default) |
| 54 |
specialize__memo = staticmethod(memo) |
| 55 |
specialize__arg = staticmethod(specialize_argvalue) |
| 56 |
specialize__arg_or_var = staticmethod(specialize_arg_or_var) |
| 57 |
specialize__argtype = staticmethod(specialize_argtype) |
| 58 |
specialize__arglistitemtype = staticmethod(specialize_arglistitemtype) |
| 59 |
specialize__call_location = staticmethod(specialize_call_location) |
| 60 |
|
| 61 |
def specialize__ll(pol, *args): |
| 62 |
from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy |
| 63 |
return LowLevelAnnotatorPolicy.default_specialize(*args) |
| 64 |
|
| 65 |
def specialize__ll_and_arg(pol, *args): |
| 66 |
from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy |
| 67 |
return LowLevelAnnotatorPolicy.specialize__ll_and_arg(*args) |
| 68 |
|
| 69 |
def no_more_blocks_to_annotate(pol, annotator): |
| 70 |
bk = annotator.bookkeeper |
| 71 |
|
| 72 |
for callback in bk.pending_specializations: |
| 73 |
callback() |
| 74 |
del bk.pending_specializations[:] |
| 75 |
if annotator.added_blocks is not None: |
| 76 |
all_blocks = annotator.added_blocks |
| 77 |
else: |
| 78 |
all_blocks = annotator.annotated |
| 79 |
for block in list(all_blocks): |
| 80 |
for i, instr in enumerate(block.operations): |
| 81 |
if not isinstance(instr, (op.simple_call, op.call_args)): |
| 82 |
continue |
| 83 |
v_func = instr.args[0] |
| 84 |
s_func = annotator.annotation(v_func) |
| 85 |
if not hasattr(s_func, 'needs_sandboxing'): |
| 86 |
continue |
| 87 |
key = ('sandboxing', s_func.const) |
| 88 |
if key not in bk.emulated_pbc_calls: |
| 89 |
params_s = s_func.args_s |
| 90 |
s_result = s_func.s_result |
| 91 |
from rpython.translator.sandbox.rsandbox import make_sandbox_trampoline |
| 92 |
sandbox_trampoline = make_sandbox_trampoline( |
| 93 |
s_func.name, params_s, s_result) |
| 94 |
sandbox_trampoline._signature_ = [SomeTuple(items=params_s)], s_result |
| 95 |
bk.emulate_pbc_call(key, bk.immutablevalue(sandbox_trampoline), params_s) |
| 96 |
else: |
| 97 |
s_trampoline = bk.emulated_pbc_calls[key][0] |
| 98 |
sandbox_trampoline = s_trampoline.const |
| 99 |
new = instr.replace({instr.args[0]: Constant(sandbox_trampoline)}) |
| 100 |
block.operations[i] = new |