| 1 |
from rpython.memory.gctransform.transform import GCTransformer, mallocHelpers |
| 2 |
from rpython.memory.gctransform.support import find_gc_ptrs_in_type, \ |
| 3 |
get_rtti, _static_deallocator_body_for_type, LLTransformerOp, ll_call_destructor |
| 4 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 5 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 6 |
from rpython.translator.backendopt.support import var_needsgc |
| 7 |
from rpython.rtyper import rmodel |
| 8 |
from rpython.memory.gcheader import GCHeaderBuilder |
| 9 |
from rpython.rlib.rarithmetic import ovfcheck |
| 10 |
from rpython.rtyper.rbuiltin import gen_cast |
| 11 |
import sys |
| 12 |
|
| 13 |
counts = {} |
| 14 |
|
| 15 |
|
| 16 |
ADDRESS_VOID_FUNC = lltype.FuncType([llmemory.Address], lltype.Void) |
| 17 |
|
| 18 |
class RefcountingGCTransformer(GCTransformer): |
| 19 |
malloc_zero_filled = True |
| 20 |
|
| 21 |
HDR = lltype.Struct("header", ("refcount", lltype.Signed)) |
| 22 |
|
| 23 |
def __init__(self, translator): |
| 24 |
super(RefcountingGCTransformer, self).__init__(translator, inline=True) |
| 25 |
self.gcheaderbuilder = GCHeaderBuilder(self.HDR) |
| 26 |
gc_header_offset = self.gcheaderbuilder.size_gc_header |
| 27 |
self.deallocator_graphs_needing_transforming = [] |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
memoryError = MemoryError() |
| 32 |
HDRPTR = lltype.Ptr(self.HDR) |
| 33 |
|
| 34 |
def ll_incref(adr): |
| 35 |
if adr: |
| 36 |
gcheader = llmemory.cast_adr_to_ptr(adr - gc_header_offset, HDRPTR) |
| 37 |
gcheader.refcount = gcheader.refcount + 1 |
| 38 |
def ll_decref(adr, dealloc): |
| 39 |
if adr: |
| 40 |
gcheader = llmemory.cast_adr_to_ptr(adr - gc_header_offset, HDRPTR) |
| 41 |
refcount = gcheader.refcount - 1 |
| 42 |
gcheader.refcount = refcount |
| 43 |
if refcount == 0: |
| 44 |
dealloc(adr) |
| 45 |
def ll_decref_simple(adr): |
| 46 |
if adr: |
| 47 |
gcheader = llmemory.cast_adr_to_ptr(adr - gc_header_offset, HDRPTR) |
| 48 |
refcount = gcheader.refcount - 1 |
| 49 |
if refcount == 0: |
| 50 |
llop.gc_free(lltype.Void, adr) |
| 51 |
else: |
| 52 |
gcheader.refcount = refcount |
| 53 |
def ll_no_pointer_dealloc(adr): |
| 54 |
llop.gc_free(lltype.Void, adr) |
| 55 |
|
| 56 |
mh = mallocHelpers(gckind='gc') |
| 57 |
mh.allocate = llmemory.raw_malloc |
| 58 |
def ll_malloc_fixedsize(size): |
| 59 |
size = gc_header_offset + size |
| 60 |
result = mh._ll_malloc_fixedsize(size) |
| 61 |
llmemory.raw_memclear(result, size) |
| 62 |
result += gc_header_offset |
| 63 |
return result |
| 64 |
def ll_malloc_varsize_no_length(length, size, itemsize): |
| 65 |
try: |
| 66 |
fixsize = gc_header_offset + size |
| 67 |
varsize = ovfcheck(itemsize * length) |
| 68 |
tot_size = ovfcheck(fixsize + varsize) |
| 69 |
except OverflowError: |
| 70 |
raise MemoryError() |
| 71 |
result = mh._ll_malloc_fixedsize(tot_size) |
| 72 |
llmemory.raw_memclear(result, tot_size) |
| 73 |
result += gc_header_offset |
| 74 |
return result |
| 75 |
mh.ll_malloc_varsize_no_length = ll_malloc_varsize_no_length |
| 76 |
ll_malloc_varsize = mh.ll_malloc_varsize |
| 77 |
|
| 78 |
def ll_identityhash(addr): |
| 79 |
h = llmemory.cast_adr_to_int(addr) |
| 80 |
return h |
| 81 |
|
| 82 |
if self.translator: |
| 83 |
self.increfptr = self.inittime_helper( |
| 84 |
ll_incref, [llmemory.Address], lltype.Void) |
| 85 |
self.decref_ptr = self.inittime_helper( |
| 86 |
ll_decref, [llmemory.Address, lltype.Ptr(ADDRESS_VOID_FUNC)], |
| 87 |
lltype.Void) |
| 88 |
self.decref_simple_ptr = self.inittime_helper( |
| 89 |
ll_decref_simple, [llmemory.Address], lltype.Void) |
| 90 |
self.no_pointer_dealloc_ptr = self.inittime_helper( |
| 91 |
ll_no_pointer_dealloc, [llmemory.Address], lltype.Void) |
| 92 |
self.malloc_fixedsize_ptr = self.inittime_helper( |
| 93 |
ll_malloc_fixedsize, [lltype.Signed], llmemory.Address) |
| 94 |
self.malloc_varsize_no_length_ptr = self.inittime_helper( |
| 95 |
ll_malloc_varsize_no_length, [lltype.Signed]*3, llmemory.Address) |
| 96 |
self.malloc_varsize_ptr = self.inittime_helper( |
| 97 |
ll_malloc_varsize, [lltype.Signed]*4, llmemory.Address) |
| 98 |
self.identityhash_ptr = self.inittime_helper( |
| 99 |
ll_identityhash, [llmemory.Address], lltype.Signed, |
| 100 |
inline=False) |
| 101 |
self.mixlevelannotator.finish() |
| 102 |
self.mixlevelannotator.backend_optimize() |
| 103 |
|
| 104 |
self.decref_funcptrs = {} |
| 105 |
self.static_deallocator_funcptrs = {} |
| 106 |
self.dynamic_deallocator_funcptrs = {} |
| 107 |
self.queryptr2dynamic_deallocator_funcptr = {} |
| 108 |
|
| 109 |
def finish_helpers(self, **kwds): |
| 110 |
GCTransformer.finish_helpers(self, **kwds) |
| 111 |
from rpython.translator.backendopt.malloc import remove_mallocs |
| 112 |
seen = {} |
| 113 |
graphs = [] |
| 114 |
for fptr in self.static_deallocator_funcptrs.itervalues(): |
| 115 |
graph = fptr._obj.graph |
| 116 |
if graph in seen: |
| 117 |
continue |
| 118 |
seen[graph] = True |
| 119 |
graphs.append(graph) |
| 120 |
remove_mallocs(self.translator, graphs) |
| 121 |
|
| 122 |
def var_needs_set_transform(self, var): |
| 123 |
return var_needsgc(var) |
| 124 |
|
| 125 |
def push_alive(self, var, llops): |
| 126 |
v_adr = gen_cast(llops, llmemory.Address, var) |
| 127 |
llops.genop("direct_call", [self.increfptr, v_adr]) |
| 128 |
|
| 129 |
def pop_alive(self, var, llops): |
| 130 |
PTRTYPE = var.concretetype |
| 131 |
v_adr = gen_cast(llops, llmemory.Address, var) |
| 132 |
|
| 133 |
dealloc_fptr = self.dynamic_deallocation_funcptr_for_type(PTRTYPE.TO) |
| 134 |
if dealloc_fptr is self.no_pointer_dealloc_ptr.value: |
| 135 |
|
| 136 |
llops.genop("direct_call", [self.decref_simple_ptr, v_adr]) |
| 137 |
else: |
| 138 |
cdealloc_fptr = rmodel.inputconst( |
| 139 |
lltype.typeOf(dealloc_fptr), dealloc_fptr) |
| 140 |
llops.genop("direct_call", [self.decref_ptr, v_adr, cdealloc_fptr]) |
| 141 |
|
| 142 |
def gct_fv_gc_malloc(self, hop, flags, TYPE, c_size): |
| 143 |
v_raw = hop.genop("direct_call", [self.malloc_fixedsize_ptr, c_size], |
| 144 |
resulttype=llmemory.Address) |
| 145 |
return v_raw |
| 146 |
|
| 147 |
def gct_fv_gc_malloc_varsize(self, hop, flags, TYPE, v_length, c_const_size, c_item_size, |
| 148 |
c_offset_to_length): |
| 149 |
if c_offset_to_length is None: |
| 150 |
v_raw = hop.genop("direct_call", |
| 151 |
[self.malloc_varsize_no_length_ptr, v_length, |
| 152 |
c_const_size, c_item_size], |
| 153 |
resulttype=llmemory.Address) |
| 154 |
else: |
| 155 |
v_raw = hop.genop("direct_call", |
| 156 |
[self.malloc_varsize_ptr, v_length, |
| 157 |
c_const_size, c_item_size, c_offset_to_length], |
| 158 |
resulttype=llmemory.Address) |
| 159 |
return v_raw |
| 160 |
|
| 161 |
def gct_gc_deallocate(self, hop): |
| 162 |
TYPE = hop.spaceop.args[0].value |
| 163 |
v_addr = hop.spaceop.args[1] |
| 164 |
dealloc_fptr = self.dynamic_deallocation_funcptr_for_type(TYPE) |
| 165 |
cdealloc_fptr = rmodel.inputconst( |
| 166 |
lltype.typeOf(dealloc_fptr), dealloc_fptr) |
| 167 |
hop.genop("direct_call", [cdealloc_fptr, v_addr]) |
| 168 |
|
| 169 |
def consider_constant(self, TYPE, value): |
| 170 |
if value is not lltype.top_container(value): |
| 171 |
return |
| 172 |
if isinstance(TYPE, (lltype.GcStruct, lltype.GcArray)): |
| 173 |
p = value._as_ptr() |
| 174 |
if not self.gcheaderbuilder.get_header(p): |
| 175 |
hdr = self.gcheaderbuilder.new_header(p) |
| 176 |
hdr.refcount = sys.maxint // 2 |
| 177 |
|
| 178 |
def static_deallocation_funcptr_for_type(self, TYPE): |
| 179 |
if TYPE in self.static_deallocator_funcptrs: |
| 180 |
return self.static_deallocator_funcptrs[TYPE] |
| 181 |
|
| 182 |
|
| 183 |
rtti = get_rtti(TYPE) |
| 184 |
if rtti is not None and hasattr(rtti._obj, 'destructor_funcptr'): |
| 185 |
destrptr = rtti._obj.destructor_funcptr |
| 186 |
DESTR_ARG = lltype.typeOf(destrptr).TO.ARGS[0] |
| 187 |
else: |
| 188 |
destrptr = None |
| 189 |
DESTR_ARG = None |
| 190 |
|
| 191 |
if destrptr is None and not find_gc_ptrs_in_type(TYPE): |
| 192 |
p = self.no_pointer_dealloc_ptr.value |
| 193 |
self.static_deallocator_funcptrs[TYPE] = p |
| 194 |
return p |
| 195 |
|
| 196 |
if destrptr is not None: |
| 197 |
body = '\n'.join(_static_deallocator_body_for_type('v', TYPE, 3)) |
| 198 |
src = """ |
| 199 |
def ll_deallocator(addr): |
| 200 |
exc_instance = llop.gc_fetch_exception(EXC_INSTANCE_TYPE) |
| 201 |
try: |
| 202 |
v = cast_adr_to_ptr(addr, PTR_TYPE) |
| 203 |
gcheader = cast_adr_to_ptr(addr - gc_header_offset, HDRPTR) |
| 204 |
# refcount is at zero, temporarily bump it to 1: |
| 205 |
gcheader.refcount = 1 |
| 206 |
destr_v = cast_pointer(DESTR_ARG, v) |
| 207 |
ll_call_destructor(destrptr, destr_v, %r) |
| 208 |
refcount = gcheader.refcount - 1 |
| 209 |
gcheader.refcount = refcount |
| 210 |
if refcount == 0: |
| 211 |
%s |
| 212 |
llop.%s_free(lltype.Void, addr) |
| 213 |
except: |
| 214 |
pass |
| 215 |
llop.gc_restore_exception(lltype.Void, exc_instance) |
| 216 |
pop_alive(exc_instance) |
| 217 |
# XXX layering of exceptiontransform versus gcpolicy |
| 218 |
|
| 219 |
""" % (TYPE.__name__, body, TYPE._gckind) |
| 220 |
else: |
| 221 |
call_del = None |
| 222 |
body = '\n'.join(_static_deallocator_body_for_type('v', TYPE)) |
| 223 |
src = ('def ll_deallocator(addr):\n v = cast_adr_to_ptr(addr, PTR_TYPE)\n' + |
| 224 |
body + '\n llop.%s_free(lltype.Void, addr)\n' % (TYPE._gckind,)) |
| 225 |
d = {'pop_alive': LLTransformerOp(self.pop_alive), |
| 226 |
'llop': llop, |
| 227 |
'lltype': lltype, |
| 228 |
'destrptr': destrptr, |
| 229 |
'gc_header_offset': self.gcheaderbuilder.size_gc_header, |
| 230 |
'cast_adr_to_ptr': llmemory.cast_adr_to_ptr, |
| 231 |
'cast_pointer': lltype.cast_pointer, |
| 232 |
'PTR_TYPE': lltype.Ptr(TYPE), |
| 233 |
'DESTR_ARG': DESTR_ARG, |
| 234 |
'EXC_INSTANCE_TYPE': self.translator.rtyper.exceptiondata.lltype_of_exception_value, |
| 235 |
'll_call_destructor': ll_call_destructor, |
| 236 |
'HDRPTR':lltype.Ptr(self.HDR)} |
| 237 |
exec(src, d) |
| 238 |
this = d['ll_deallocator'] |
| 239 |
fptr = self.annotate_finalizer(this, [llmemory.Address], lltype.Void) |
| 240 |
self.static_deallocator_funcptrs[TYPE] = fptr |
| 241 |
for p in find_gc_ptrs_in_type(TYPE): |
| 242 |
self.static_deallocation_funcptr_for_type(p.TO) |
| 243 |
return fptr |
| 244 |
|
| 245 |
def dynamic_deallocation_funcptr_for_type(self, TYPE): |
| 246 |
if TYPE in self.dynamic_deallocator_funcptrs: |
| 247 |
return self.dynamic_deallocator_funcptrs[TYPE] |
| 248 |
|
| 249 |
|
| 250 |
rtti = get_rtti(TYPE) |
| 251 |
if rtti is None: |
| 252 |
p = self.static_deallocation_funcptr_for_type(TYPE) |
| 253 |
self.dynamic_deallocator_funcptrs[TYPE] = p |
| 254 |
return p |
| 255 |
|
| 256 |
queryptr = rtti._obj.query_funcptr |
| 257 |
if queryptr._obj in self.queryptr2dynamic_deallocator_funcptr: |
| 258 |
return self.queryptr2dynamic_deallocator_funcptr[queryptr._obj] |
| 259 |
|
| 260 |
RTTI_PTR = lltype.Ptr(lltype.RuntimeTypeInfo) |
| 261 |
QUERY_ARG_TYPE = lltype.typeOf(queryptr).TO.ARGS[0] |
| 262 |
gc_header_offset = self.gcheaderbuilder.size_gc_header |
| 263 |
HDRPTR = lltype.Ptr(self.HDR) |
| 264 |
def ll_dealloc(addr): |
| 265 |
|
| 266 |
gcheader = llmemory.cast_adr_to_ptr(addr - gc_header_offset, HDRPTR) |
| 267 |
gcheader.refcount = 1 |
| 268 |
v = llmemory.cast_adr_to_ptr(addr, QUERY_ARG_TYPE) |
| 269 |
rtti = queryptr(v) |
| 270 |
gcheader.refcount = 0 |
| 271 |
llop.gc_call_rtti_destructor(lltype.Void, rtti, addr) |
| 272 |
fptr = self.annotate_helper(ll_dealloc, [llmemory.Address], lltype.Void) |
| 273 |
self.dynamic_deallocator_funcptrs[TYPE] = fptr |
| 274 |
self.queryptr2dynamic_deallocator_funcptr[queryptr._obj] = fptr |
| 275 |
return fptr |
| 276 |
|
| 277 |
def gct_gc_identityhash(self, hop): |
| 278 |
v_obj = hop.spaceop.args[0] |
| 279 |
v_adr = hop.genop("cast_ptr_to_adr", [v_obj], |
| 280 |
resulttype=llmemory.Address) |
| 281 |
hop.genop("direct_call", [self.identityhash_ptr, v_adr], |
| 282 |
resultvar=hop.spaceop.result) |
| 283 |
|
| 284 |
def gcheader_initdata(self, obj): |
| 285 |
top = lltype.top_container(obj) |
| 286 |
return self.gcheaderbuilder.header_of_object(top)._obj |