| 1 |
from rpython.rtyper.lltypesystem import lltype |
| 2 |
from rpython.rtyper import rclass |
| 3 |
|
| 4 |
def is_immutable_struct(S): |
| 5 |
return isinstance(S, lltype.GcStruct) and S._hints.get('immutable', False) |
| 6 |
|
| 7 |
def has_gcstruct_a_vtable(GCSTRUCT): |
| 8 |
if not isinstance(GCSTRUCT, lltype.GcStruct): |
| 9 |
return False |
| 10 |
if GCSTRUCT is rclass.OBJECT: |
| 11 |
return False |
| 12 |
while not GCSTRUCT._hints.get('typeptr'): |
| 13 |
_, GCSTRUCT = GCSTRUCT._first_struct() |
| 14 |
if GCSTRUCT is None: |
| 15 |
return False |
| 16 |
return True |
| 17 |
|
| 18 |
def get_vtable_for_gcstruct(gccache, GCSTRUCT): |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if not isinstance(GCSTRUCT, lltype.GcStruct): |
| 23 |
return lltype.nullptr(rclass.OBJECT_VTABLE) |
| 24 |
if not has_gcstruct_a_vtable(GCSTRUCT): |
| 25 |
return lltype.nullptr(rclass.OBJECT_VTABLE) |
| 26 |
setup_cache_gcstruct2vtable(gccache) |
| 27 |
try: |
| 28 |
return gccache._cache_gcstruct2vtable[GCSTRUCT] |
| 29 |
except KeyError: |
| 30 |
return testing_gcstruct2vtable[GCSTRUCT] |
| 31 |
|
| 32 |
def setup_cache_gcstruct2vtable(gccache): |
| 33 |
if not hasattr(gccache, '_cache_gcstruct2vtable'): |
| 34 |
cache = {} |
| 35 |
if gccache.rtyper: |
| 36 |
for rinstance in gccache.rtyper.instance_reprs.values(): |
| 37 |
cache[rinstance.lowleveltype.TO] = rinstance.rclass.getvtable() |
| 38 |
gccache._cache_gcstruct2vtable = cache |
| 39 |
|
| 40 |
def set_testing_vtable_for_gcstruct(GCSTRUCT, vtable, name): |
| 41 |
|
| 42 |
|
| 43 |
vtable.name = rclass.alloc_array_name(name) |
| 44 |
testing_gcstruct2vtable[GCSTRUCT] = vtable |
| 45 |
|
| 46 |
testing_gcstruct2vtable = {} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def all_fielddescrs(gccache, STRUCT, only_gc=False, res=None, |
| 52 |
get_field_descr=None): |
| 53 |
from rpython.jit.backend.llsupport import descr |
| 54 |
|
| 55 |
if get_field_descr is None: |
| 56 |
get_field_descr = descr.get_field_descr |
| 57 |
if res is None: |
| 58 |
res = [] |
| 59 |
|
| 60 |
for name in STRUCT._names: |
| 61 |
FIELD = getattr(STRUCT, name) |
| 62 |
if FIELD is lltype.Void: |
| 63 |
continue |
| 64 |
if name.startswith('c__pad'): |
| 65 |
continue |
| 66 |
if name == 'typeptr': |
| 67 |
continue |
| 68 |
elif isinstance(FIELD, lltype.Struct): |
| 69 |
all_fielddescrs(gccache, FIELD, only_gc, res, get_field_descr) |
| 70 |
elif (not only_gc) or (isinstance(FIELD, lltype.Ptr) and FIELD._needsgc()): |
| 71 |
res.append(get_field_descr(gccache, STRUCT, name)) |
| 72 |
return res |
| 73 |
|
| 74 |
def all_interiorfielddescrs(gccache, ARRAY, get_field_descr=None): |
| 75 |
from rpython.jit.backend.llsupport import descr |
| 76 |
from rpython.jit.codewriter.effectinfo import UnsupportedFieldExc |
| 77 |
|
| 78 |
if get_field_descr is None: |
| 79 |
get_field_descr = descr.get_field_descr |
| 80 |
|
| 81 |
STRUCT = ARRAY.OF |
| 82 |
res = [] |
| 83 |
for name in STRUCT._names: |
| 84 |
FIELD = getattr(STRUCT, name) |
| 85 |
if FIELD is lltype.Void: |
| 86 |
continue |
| 87 |
if name == 'typeptr': |
| 88 |
continue |
| 89 |
elif isinstance(FIELD, lltype.Struct): |
| 90 |
raise UnsupportedFieldExc("unexpected array(struct(struct))") |
| 91 |
res.append(get_field_descr(gccache, ARRAY, name)) |
| 92 |
return res |
| 93 |
|
| 94 |
def gc_fielddescrs(gccache, STRUCT): |
| 95 |
return all_fielddescrs(gccache, STRUCT, True) |
| 96 |
|
| 97 |
def get_fielddescr_index_in(STRUCT, fieldname, cur_index=0): |
| 98 |
for name in STRUCT._names: |
| 99 |
FIELD = getattr(STRUCT, name) |
| 100 |
if FIELD is lltype.Void: |
| 101 |
continue |
| 102 |
if name == 'typeptr': |
| 103 |
continue |
| 104 |
elif isinstance(FIELD, lltype.Struct): |
| 105 |
r = get_fielddescr_index_in(FIELD, fieldname, cur_index) |
| 106 |
if r >= 0: |
| 107 |
return r |
| 108 |
cur_index += -r - 1 |
| 109 |
continue |
| 110 |
elif name == fieldname: |
| 111 |
return cur_index |
| 112 |
cur_index += 1 |
| 113 |
return -cur_index - 1 |