| 1 |
from rpython.rtyper.rmodel import inputconst, log |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 3 |
from rpython.rtyper.rclass import (FieldListAccessor, InstanceRepr) |
| 4 |
|
| 5 |
|
| 6 |
class VirtualizableInstanceRepr(InstanceRepr): |
| 7 |
|
| 8 |
def _super(self): |
| 9 |
return super(VirtualizableInstanceRepr, self) |
| 10 |
|
| 11 |
def __init__(self, rtyper, classdef): |
| 12 |
self._super().__init__(rtyper, classdef) |
| 13 |
classdesc = classdef.classdesc |
| 14 |
if classdesc.get_param('_virtualizable2_'): |
| 15 |
raise Exception("_virtualizable2_ is now called _virtualizable_, " |
| 16 |
"please rename") |
| 17 |
if classdesc.get_param('_virtualizable_', inherit=False): |
| 18 |
basedesc = classdesc.basedesc |
| 19 |
assert (basedesc is None or |
| 20 |
basedesc.get_param('_virtualizable_') is None) |
| 21 |
self.top_of_virtualizable_hierarchy = True |
| 22 |
self.accessor = FieldListAccessor() |
| 23 |
else: |
| 24 |
self.top_of_virtualizable_hierarchy = False |
| 25 |
|
| 26 |
def _setup_repr_llfields(self): |
| 27 |
llfields = [] |
| 28 |
if self.top_of_virtualizable_hierarchy: |
| 29 |
llfields.append(('vable_token', llmemory.GCREF)) |
| 30 |
return llfields |
| 31 |
|
| 32 |
def _setup_repr(self): |
| 33 |
if self.top_of_virtualizable_hierarchy: |
| 34 |
hints = {'virtualizable_accessor': self.accessor} |
| 35 |
llfields = self._setup_repr_llfields() |
| 36 |
if llfields: |
| 37 |
self._super()._setup_repr(llfields, hints=hints) |
| 38 |
else: |
| 39 |
self._super()._setup_repr(hints = hints) |
| 40 |
vfields = self.classdef.classdesc.get_param('_virtualizable_') |
| 41 |
self.my_redirected_fields = self._parse_field_list( |
| 42 |
vfields, self.accessor, hints) |
| 43 |
else: |
| 44 |
self._super()._setup_repr() |
| 45 |
|
| 46 |
|
| 47 |
self.my_redirected_fields = self.rbase.my_redirected_fields |
| 48 |
|
| 49 |
def hook_access_field(self, vinst, cname, llops, flags): |
| 50 |
|
| 51 |
if self.my_redirected_fields.get(cname.value): |
| 52 |
cflags = inputconst(lltype.Void, flags) |
| 53 |
llops.genop('jit_force_virtualizable', [vinst, cname, cflags]) |
| 54 |
|
| 55 |
|
| 56 |
def replace_force_virtualizable_with_call(graphs, VTYPEPTR, funcptr): |
| 57 |
|
| 58 |
c_funcptr = inputconst(lltype.typeOf(funcptr), funcptr) |
| 59 |
count = 0 |
| 60 |
for graph in graphs: |
| 61 |
for block in graph.iterblocks(): |
| 62 |
if not block.operations: |
| 63 |
continue |
| 64 |
newoplist = [] |
| 65 |
for i, op in enumerate(block.operations): |
| 66 |
if (op.opname == 'jit_force_virtualizable' and |
| 67 |
op.args[0].concretetype == VTYPEPTR): |
| 68 |
if op.args[-1].value.get('access_directly', False): |
| 69 |
continue |
| 70 |
op.opname = 'direct_call' |
| 71 |
op.args = [c_funcptr, op.args[0]] |
| 72 |
count += 1 |
| 73 |
newoplist.append(op) |
| 74 |
block.operations = newoplist |
| 75 |
log("replaced %d 'jit_force_virtualizable' with %r" % (count, funcptr)) |