| 1 |
from rpython.memory.gctransform.transform import GCTransformer, mallocHelpers |
| 2 |
from rpython.memory.gctransform.support import (get_rtti, |
| 3 |
_static_deallocator_body_for_type, LLTransformerOp, ll_call_destructor, |
| 4 |
ll_report_finalizer_error) |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 6 |
from rpython.flowspace.model import Constant |
| 7 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 8 |
from rpython.rtyper import rmodel |
| 9 |
|
| 10 |
|
| 11 |
class BoehmGCTransformer(GCTransformer): |
| 12 |
malloc_zero_filled = True |
| 13 |
FINALIZER_PTR = lltype.Ptr(lltype.FuncType([llmemory.GCREF], lltype.Void)) |
| 14 |
|
| 15 |
def __init__(self, translator, inline=False): |
| 16 |
super(BoehmGCTransformer, self).__init__(translator, inline=inline) |
| 17 |
self.finalizer_funcptrs = {} |
| 18 |
|
| 19 |
atomic_mh = mallocHelpers(gckind='gc') |
| 20 |
atomic_mh.allocate = lambda size: llop.boehm_malloc_atomic(llmemory.GCREF, size) |
| 21 |
ll_malloc_fixedsize_atomic = atomic_mh._ll_malloc_fixedsize |
| 22 |
|
| 23 |
mh = mallocHelpers(gckind='gc') |
| 24 |
mh.allocate = lambda size: llop.boehm_malloc(llmemory.GCREF, size) |
| 25 |
ll_malloc_fixedsize = mh._ll_malloc_fixedsize |
| 26 |
|
| 27 |
|
| 28 |
ll_malloc_varsize_no_length = mh.ll_malloc_varsize_no_length |
| 29 |
ll_malloc_varsize = mh.ll_malloc_varsize |
| 30 |
|
| 31 |
fields = [] |
| 32 |
if translator and translator.config.translation.reverse_debugger: |
| 33 |
fields.append(("hash", lltype.Signed)) |
| 34 |
fields.append(("uid", lltype.SignedLongLong)) |
| 35 |
hints = {'hints': {'gcheader': True}} |
| 36 |
self.HDR = lltype.Struct("header", *fields, **hints) |
| 37 |
|
| 38 |
if self.translator: |
| 39 |
self.malloc_fixedsize_ptr = self.inittime_helper( |
| 40 |
ll_malloc_fixedsize, [lltype.Signed], llmemory.GCREF) |
| 41 |
self.malloc_fixedsize_atomic_ptr = self.inittime_helper( |
| 42 |
ll_malloc_fixedsize_atomic, [lltype.Signed], llmemory.GCREF) |
| 43 |
self.malloc_varsize_no_length_ptr = self.inittime_helper( |
| 44 |
ll_malloc_varsize_no_length, [lltype.Signed]*3, llmemory.GCREF, inline=False) |
| 45 |
self.malloc_varsize_ptr = self.inittime_helper( |
| 46 |
ll_malloc_varsize, [lltype.Signed]*4, llmemory.GCREF, inline=False) |
| 47 |
if self.translator.config.translation.rweakref: |
| 48 |
(ll_weakref_create, ll_weakref_deref, |
| 49 |
self.WEAKLINK, self.convert_weakref_to |
| 50 |
) = build_weakref(self.translator.config) |
| 51 |
self.weakref_create_ptr = self.inittime_helper( |
| 52 |
ll_weakref_create, [llmemory.GCREF], llmemory.WeakRefPtr, |
| 53 |
inline=False) |
| 54 |
self.weakref_deref_ptr = self.inittime_helper( |
| 55 |
ll_weakref_deref, [llmemory.WeakRefPtr], llmemory.GCREF) |
| 56 |
|
| 57 |
if not translator.config.translation.reverse_debugger: |
| 58 |
def ll_identityhash(addr): |
| 59 |
h = ~llmemory.cast_adr_to_int(addr) |
| 60 |
return h |
| 61 |
self.identityhash_ptr = self.inittime_helper( |
| 62 |
ll_identityhash, [llmemory.Address], lltype.Signed, |
| 63 |
inline=False) |
| 64 |
self.NO_HEADER = True |
| 65 |
|
| 66 |
self.mixlevelannotator.finish() |
| 67 |
self.mixlevelannotator.backend_optimize() |
| 68 |
|
| 69 |
self.finalizer_triggers = [] |
| 70 |
self.finalizer_queue_indexes = {} |
| 71 |
|
| 72 |
def gct_fv_gc_malloc(self, hop, flags, TYPE, c_size): |
| 73 |
|
| 74 |
if TYPE._is_atomic(): |
| 75 |
funcptr = self.malloc_fixedsize_atomic_ptr |
| 76 |
opname = 'boehm_malloc_atomic' |
| 77 |
else: |
| 78 |
funcptr = self.malloc_fixedsize_ptr |
| 79 |
opname = 'boehm_malloc' |
| 80 |
tr = self.translator |
| 81 |
if tr and tr.config.translation.reverse_debugger: |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
v_raw = hop.genop(opname, [c_size], resulttype=llmemory.GCREF) |
| 87 |
else: |
| 88 |
v_raw = hop.genop("direct_call", |
| 89 |
[funcptr, c_size], |
| 90 |
resulttype=llmemory.GCREF) |
| 91 |
finalizer_ptr = self.finalizer_funcptr_for_type(TYPE) |
| 92 |
if finalizer_ptr: |
| 93 |
c_finalizer_ptr = Constant(finalizer_ptr, self.FINALIZER_PTR) |
| 94 |
hop.genop("boehm_register_finalizer", [v_raw, c_finalizer_ptr]) |
| 95 |
return v_raw |
| 96 |
|
| 97 |
def gct_fv_gc_malloc_varsize(self, hop, flags, TYPE, v_length, c_const_size, c_item_size, |
| 98 |
c_offset_to_length): |
| 99 |
|
| 100 |
if c_offset_to_length is None: |
| 101 |
v_raw = hop.genop("direct_call", |
| 102 |
[self.malloc_varsize_no_length_ptr, v_length, |
| 103 |
c_const_size, c_item_size], |
| 104 |
resulttype=llmemory.GCREF) |
| 105 |
else: |
| 106 |
v_raw = hop.genop("direct_call", |
| 107 |
[self.malloc_varsize_ptr, v_length, |
| 108 |
c_const_size, c_item_size, c_offset_to_length], |
| 109 |
resulttype=llmemory.GCREF) |
| 110 |
return v_raw |
| 111 |
|
| 112 |
def finalizer_funcptr_for_type(self, TYPE): |
| 113 |
if TYPE in self.finalizer_funcptrs: |
| 114 |
return self.finalizer_funcptrs[TYPE] |
| 115 |
|
| 116 |
rtti = get_rtti(TYPE) |
| 117 |
if rtti is not None and hasattr(rtti._obj, 'destructor_funcptr'): |
| 118 |
destrptr = rtti._obj.destructor_funcptr |
| 119 |
DESTR_ARG = lltype.typeOf(destrptr).TO.ARGS[0] |
| 120 |
else: |
| 121 |
destrptr = None |
| 122 |
DESTR_ARG = None |
| 123 |
|
| 124 |
if destrptr: |
| 125 |
EXC_INSTANCE_TYPE = self.translator.rtyper.exceptiondata.lltype_of_exception_value |
| 126 |
typename = TYPE.__name__ |
| 127 |
revdb = self.translator.config.translation.reverse_debugger |
| 128 |
def ll_finalizer(gcref): |
| 129 |
exc_instance = llop.gc_fetch_exception(EXC_INSTANCE_TYPE) |
| 130 |
if revdb: |
| 131 |
llop.revdb_call_destructor(lltype.Void, gcref) |
| 132 |
v = lltype.cast_opaque_ptr(DESTR_ARG, gcref) |
| 133 |
ll_call_destructor(destrptr, v, typename) |
| 134 |
llop.gc_restore_exception(lltype.Void, exc_instance) |
| 135 |
fptr = self.annotate_finalizer(ll_finalizer, [llmemory.GCREF], lltype.Void) |
| 136 |
else: |
| 137 |
fptr = lltype.nullptr(self.FINALIZER_PTR.TO) |
| 138 |
|
| 139 |
self.finalizer_funcptrs[TYPE] = fptr |
| 140 |
return fptr |
| 141 |
|
| 142 |
def get_finalizer_queue_index(self, hop): |
| 143 |
fq_tag = hop.spaceop.args[0].value |
| 144 |
assert 'FinalizerQueue TAG' in fq_tag.expr |
| 145 |
fq = fq_tag.default |
| 146 |
try: |
| 147 |
index = self.finalizer_queue_indexes[fq] |
| 148 |
except KeyError: |
| 149 |
index = len(self.finalizer_queue_indexes) |
| 150 |
assert index == len(self.finalizer_triggers) |
| 151 |
|
| 152 |
def ll_finalizer_trigger(): |
| 153 |
try: |
| 154 |
fq.finalizer_trigger() |
| 155 |
except Exception as e: |
| 156 |
ll_report_finalizer_error(e) |
| 157 |
ll_trigger = self.annotate_finalizer(ll_finalizer_trigger, [], |
| 158 |
lltype.Void) |
| 159 |
self.finalizer_triggers.append(ll_trigger) |
| 160 |
self.finalizer_queue_indexes[fq] = index |
| 161 |
return index |
| 162 |
|
| 163 |
def gct_gc_fq_register(self, hop): |
| 164 |
index = self.get_finalizer_queue_index(hop) |
| 165 |
c_index = rmodel.inputconst(lltype.Signed, index) |
| 166 |
v_ptr = hop.spaceop.args[1] |
| 167 |
hop.genop("boehm_fq_register", [c_index, v_ptr]) |
| 168 |
|
| 169 |
def gct_gc_fq_next_dead(self, hop): |
| 170 |
index = self.get_finalizer_queue_index(hop) |
| 171 |
c_index = rmodel.inputconst(lltype.Signed, index) |
| 172 |
hop.genop("boehm_fq_next_dead", [c_index], |
| 173 |
resultvar = hop.spaceop.result) |
| 174 |
|
| 175 |
def gct_weakref_create(self, hop): |
| 176 |
v_instance, = hop.spaceop.args |
| 177 |
v_gcref = hop.genop("cast_opaque_ptr", [v_instance], |
| 178 |
resulttype=llmemory.GCREF) |
| 179 |
v_wref = hop.genop("direct_call", |
| 180 |
[self.weakref_create_ptr, v_gcref], |
| 181 |
resulttype=llmemory.WeakRefPtr) |
| 182 |
hop.cast_result(v_wref) |
| 183 |
|
| 184 |
def gct_zero_everything_inside(self, hop): |
| 185 |
pass |
| 186 |
|
| 187 |
def gct_zero_gc_pointers_inside(self, hop): |
| 188 |
pass |
| 189 |
|
| 190 |
def gct_weakref_deref(self, hop): |
| 191 |
v_wref, = hop.spaceop.args |
| 192 |
v_gcref = hop.genop("direct_call", |
| 193 |
[self.weakref_deref_ptr, v_wref], |
| 194 |
resulttype=llmemory.GCREF) |
| 195 |
hop.cast_result(v_gcref) |
| 196 |
|
| 197 |
def gct_gc_writebarrier_before_copy(self, hop): |
| 198 |
|
| 199 |
op = hop.spaceop |
| 200 |
hop.genop("same_as", |
| 201 |
[rmodel.inputconst(lltype.Bool, True)], |
| 202 |
resultvar=op.result) |
| 203 |
|
| 204 |
def gct_gc_move_out_of_nursery(self, hop): |
| 205 |
hop.genop("same_as", [hop.spaceop.args[0]], resultvar=hop.spaceop.result) |
| 206 |
|
| 207 |
def gct_gc_identityhash(self, hop): |
| 208 |
v_obj = hop.spaceop.args[0] |
| 209 |
if not self.translator.config.translation.reverse_debugger: |
| 210 |
v_addr = hop.genop("cast_ptr_to_adr", [v_obj], |
| 211 |
resulttype=llmemory.Address) |
| 212 |
hop.genop("direct_call", [self.identityhash_ptr, v_addr], |
| 213 |
resultvar=hop.spaceop.result) |
| 214 |
else: |
| 215 |
hop.genop("revdb_identityhash", [v_obj], |
| 216 |
resultvar=hop.spaceop.result) |
| 217 |
|
| 218 |
def gct_gc_id(self, hop): |
| 219 |
|
| 220 |
v_int = hop.genop('cast_ptr_to_int', [hop.spaceop.args[0]], |
| 221 |
resulttype = lltype.Signed) |
| 222 |
hop.genop('int_invert', [v_int], resultvar=hop.spaceop.result) |
| 223 |
|
| 224 |
def gcheader_initdata(self, obj): |
| 225 |
if not self.translator.config.translation.reverse_debugger: |
| 226 |
return GCTransform.gcheader_initdata(self, obj) |
| 227 |
hdr = lltype.malloc(self.HDR, immortal=True, zero=True) |
| 228 |
return hdr._obj |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def build_weakref(config): |
| 237 |
revdb = config.translation.reverse_debugger |
| 238 |
if not revdb: |
| 239 |
WEAKLINK = lltype.Struct('WEAKLINK', |
| 240 |
('addr', llmemory.Address)) |
| 241 |
else: |
| 242 |
|
| 243 |
WEAKLINK = lltype.Struct('REVDB_WEAKLINK', |
| 244 |
('addr', llmemory.Address), |
| 245 |
('off_prev', lltype.SignedLongLong)) |
| 246 |
sizeof_weakreflink = llmemory.sizeof(WEAKLINK) |
| 247 |
empty_weaklink = lltype.malloc(WEAKLINK, immortal=True, zero=True) |
| 248 |
|
| 249 |
def ll_weakref_create(target_gcref): |
| 250 |
if revdb: |
| 251 |
plink = llop.revdb_weakref_create(lltype.Ptr(WEAKLINK), |
| 252 |
target_gcref) |
| 253 |
else: |
| 254 |
link = llop.boehm_malloc_atomic(llmemory.Address, |
| 255 |
sizeof_weakreflink) |
| 256 |
if not link: |
| 257 |
raise MemoryError |
| 258 |
plink = llmemory.cast_adr_to_ptr(link, lltype.Ptr(WEAKLINK)) |
| 259 |
plink.addr = llmemory.cast_ptr_to_adr(target_gcref) |
| 260 |
llop.boehm_disappearing_link(lltype.Void, link, target_gcref) |
| 261 |
return llmemory.cast_ptr_to_weakrefptr(plink) |
| 262 |
|
| 263 |
def ll_weakref_deref(wref): |
| 264 |
plink = llmemory.cast_weakrefptr_to_ptr(lltype.Ptr(WEAKLINK), wref) |
| 265 |
if revdb: |
| 266 |
result = llop.revdb_weakref_deref(llmemory.GCREF, plink) |
| 267 |
else: |
| 268 |
result = llmemory.cast_adr_to_ptr(plink.addr, llmemory.GCREF) |
| 269 |
return result |
| 270 |
|
| 271 |
def convert_weakref_to(targetptr): |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if not targetptr: |
| 277 |
return empty_weaklink |
| 278 |
else: |
| 279 |
plink = lltype.malloc(WEAKLINK, immortal=True, zero=True) |
| 280 |
plink.addr = llmemory.cast_ptr_to_adr(targetptr) |
| 281 |
return plink |
| 282 |
|
| 283 |
return ll_weakref_create, ll_weakref_deref, WEAKLINK, convert_weakref_to |