1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
import sys
from rpython.flowspace.model import Constant
from rpython.rtyper.lltypesystem.lltype import (RttiStruct,
RuntimeTypeInfo)
from rpython.translator.c.node import ContainerNode
from rpython.translator.c.support import cdecl
from rpython.translator.tool.cbuild import ExternalCompilationInfo
class BasicGcPolicy(object):
def __init__(self, db, thread_enabled=False):
self.db = db
self.thread_enabled = thread_enabled
def common_gcheader_definition(self, defnode):
if defnode.db.gctransformer is not None:
return defnode.db.gctransformer.HDR
return None
def struct_gcheader_definition(self, defnode):
return self.common_gcheader_definition(defnode)
def array_gcheader_definition(self, defnode):
return self.common_gcheader_definition(defnode)
def compilation_info(self):
if not self.db:
return ExternalCompilationInfo()
gct = self.db.gctransformer
return ExternalCompilationInfo(
pre_include_bits=['/* using %s */' % (gct.__class__.__name__,),
'#define MALLOC_ZERO_FILLED %d' % (gct.malloc_zero_filled,),
]
)
def need_no_typeptr(self):
return False
def gc_startup_code(self):
return []
def struct_setup(self, structdefnode, rtti):
return None
def array_setup(self, arraydefnode):
return None
def rtti_type(self):
return ''
def OP_GC_SET_MAX_HEAP_SIZE(self, funcgen, op):
return ''
def OP_GC_THREAD_PREPARE(self, funcgen, op):
return ''
def OP_GC_THREAD_RUN(self, funcgen, op):
# The gc transformer leaves this operation in the graphs
# in all cases except with framework+shadowstack. In that
# case the operation is removed because redundant with
# rthread.get_or_make_ident().
return 'RPY_THREADLOCALREF_ENSURE();'
def OP_GC_THREAD_START(self, funcgen, op):
return ''
def OP_GC_THREAD_DIE(self, funcgen, op):
# The gc transformer leaves this operation in the graphs
# (but may insert a call to a gcrootfinder-specific
# function just before).
return 'RPython_ThreadLocals_ThreadDie();'
def OP_GC_THREAD_BEFORE_FORK(self, funcgen, op):
return '%s = NULL;' % funcgen.expr(op.result)
def OP_GC_THREAD_AFTER_FORK(self, funcgen, op):
return ''
def OP_GC_WRITEBARRIER(self, funcgen, op):
return ''
def OP_GC_STACK_BOTTOM(self, funcgen, op):
return ''
def OP_GC_GCFLAG_EXTRA(self, funcgen, op):
return '%s = 0; /* gc_gcflag_extra%r */' % (
funcgen.expr(op.result),
op.args[0])
class RefcountingInfo:
static_deallocator = None
class RefcountingGcPolicy(BasicGcPolicy):
def gettransformer(self, translator, gchooks):
from rpython.memory.gctransform import refcounting
return refcounting.RefcountingGCTransformer(translator)
# for structs
def struct_setup(self, structdefnode, rtti):
if rtti is not None:
transformer = structdefnode.db.gctransformer
fptr = transformer.static_deallocation_funcptr_for_type(
structdefnode.STRUCT)
structdefnode.gcinfo = RefcountingInfo()
structdefnode.gcinfo.static_deallocator = structdefnode.db.get(fptr)
# for arrays
def array_setup(self, arraydefnode):
pass
# for rtti node
def rtti_type(self):
return 'void (@)(void *)' # void dealloc_xx(struct xx *)
def rtti_node_factory(self):
return RefcountingRuntimeTypeInfo_OpaqueNode
# zero malloc impl
def OP_GC_CALL_RTTI_DESTRUCTOR(self, funcgen, op):
args = [funcgen.expr(v) for v in op.args]
line = '%s(%s);' % (args[0], ', '.join(args[1:]))
return line
def OP_GC_FREE(self, funcgen, op):
args = [funcgen.expr(v) for v in op.args]
return 'OP_FREE(%s);' % (args[0], )
def OP_GC__COLLECT(self, funcgen, op):
return ''
def OP_GC__DISABLE_FINALIZERS(self, funcgen, op):
return ''
def OP_GC__ENABLE_FINALIZERS(self, funcgen, op):
return ''
class RefcountingRuntimeTypeInfo_OpaqueNode(ContainerNode):
nodekind = 'refcnt rtti'
globalcontainer = True
typename = 'void (@)(void *)'
_funccodegen_owner = None
def __init__(self, db, T, obj):
assert T == RuntimeTypeInfo
assert isinstance(obj.about, RttiStruct)
self.db = db
self.T = T
self.obj = obj
defnode = db.gettypedefnode(obj.about)
self.implementationtypename = 'void (@)(void *)'
self.name = defnode.gcinfo.static_deallocator
def getptrname(self):
return '((void (*)(void *)) %s)' % (self.name,)
def enum_dependencies(self):
return []
def implementation(self):
return []
class BoehmInfo:
finalizer = None
class BoehmGcPolicy(BasicGcPolicy):
def gettransformer(self, translator, gchooks):
from rpython.memory.gctransform import boehm
return boehm.BoehmGCTransformer(translator)
def array_setup(self, arraydefnode):
pass
def struct_setup(self, structdefnode, rtti):
pass
def rtti_type(self):
return BoehmGcRuntimeTypeInfo_OpaqueNode.typename
def rtti_node_factory(self):
return BoehmGcRuntimeTypeInfo_OpaqueNode
def compilation_info(self):
eci = BasicGcPolicy.compilation_info(self)
from rpython.rtyper.tool.rffi_platform import configure_boehm
eci = eci.merge(configure_boehm())
pre_include_bits = []
if sys.platform.startswith('linux'):
pre_include_bits += ["#define _REENTRANT 1",
"#define GC_LINUX_THREADS 1"]
if sys.platform != "win32" and not sys.platform.startswith("openbsd"):
# GC_REDIRECT_TO_LOCAL is not supported on Win32 by gc6.8
pre_include_bits += ["#define GC_REDIRECT_TO_LOCAL 1"]
hdr_flag = ''
if not getattr(self.db.gctransformer, 'NO_HEADER', False):
hdr_flag = '-DPYPY_BOEHM_WITH_HEADER'
eci = eci.merge(ExternalCompilationInfo(
pre_include_bits=pre_include_bits,
# The following define is required by the thread module,
# See module/thread/test/test_rthread.py
compile_extra=['-DPYPY_USING_BOEHM_GC', hdr_flag],
))
gct = self.db.gctransformer
gct.finalizer_triggers = tuple(gct.finalizer_triggers) # stop changing
sourcelines = ['']
for trig in gct.finalizer_triggers:
sourcelines.append('RPY_EXTERN void %s(void);' % (
self.db.get(trig),))
sourcelines.append('')
sourcelines.append('void (*boehm_fq_trigger[])(void) = {')
for trig in gct.finalizer_triggers:
sourcelines.append('\t%s,' % (self.db.get(trig),))
sourcelines.append('\tNULL')
sourcelines.append('};')
sourcelines.append('struct boehm_fq_s *boehm_fq_queues[%d];' % (
len(gct.finalizer_triggers) or 1,))
sourcelines.append('')
eci = eci.merge(ExternalCompilationInfo(
separate_module_sources=['\n'.join(sourcelines)]))
return eci
def gc_startup_code(self):
if sys.platform == 'win32':
pass # yield 'assert(GC_all_interior_pointers == 0);'
else:
yield 'GC_all_interior_pointers = 0;'
yield 'boehm_gc_startup_code();'
def get_real_weakref_type(self):
return self.db.gctransformer.WEAKLINK
def convert_weakref_to(self, ptarget):
return self.db.gctransformer.convert_weakref_to(ptarget)
def OP_GC__COLLECT(self, funcgen, op):
return 'GC_gcollect();'
def OP_GC_SET_MAX_HEAP_SIZE(self, funcgen, op):
nbytes = funcgen.expr(op.args[0])
return 'GC_set_max_heap_size(%s);' % (nbytes,)
def GC_KEEPALIVE(self, funcgen, v):
return 'pypy_asm_keepalive(%s);' % funcgen.expr(v)
class BoehmGcRuntimeTypeInfo_OpaqueNode(ContainerNode):
nodekind = 'boehm rtti'
globalcontainer = True
typename = 'char @'
_funccodegen_owner = None
def __init__(self, db, T, obj):
assert T == RuntimeTypeInfo
assert isinstance(obj.about, RttiStruct)
self.db = db
self.T = T
self.obj = obj
defnode = db.gettypedefnode(obj.about)
self.implementationtypename = self.typename
self.name = self.db.namespace.uniquename('g_rtti_v_'+ defnode.barename)
def getptrname(self):
return '(&%s)' % (self.name,)
def enum_dependencies(self):
return []
def implementation(self):
yield 'char %s /* uninitialized */;' % self.name
class FrameworkGcRuntimeTypeInfo_OpaqueNode(BoehmGcRuntimeTypeInfo_OpaqueNode):
nodekind = 'framework rtti'
# to get an idea how it looks like with no refcount/gc at all
class NoneGcPolicy(BoehmGcPolicy):
gc_startup_code = RefcountingGcPolicy.gc_startup_code.im_func
def compilation_info(self):
eci = BasicGcPolicy.compilation_info(self)
eci = eci.merge(ExternalCompilationInfo(
post_include_bits=['#define PYPY_USING_NO_GC_AT_ALL'],
))
return eci
class BasicFrameworkGcPolicy(BasicGcPolicy):
def gettransformer(self, translator, gchooks):
if hasattr(self, 'transformerclass'): # for rpython/memory tests
return self.transformerclass(translator, gchooks=gchooks)
raise NotImplementedError
def struct_setup(self, structdefnode, rtti):
if rtti is not None and hasattr(rtti._obj, 'destructor_funcptr'):
gctransf = self.db.gctransformer
TYPE = structdefnode.STRUCT
fptrs = gctransf.special_funcptr_for_type(TYPE)
# make sure this is seen by the database early, i.e. before
# finish_helpers() on the gctransformer
destrptr = rtti._obj.destructor_funcptr
self.db.get(destrptr)
# the following, on the other hand, will only discover ll_finalizer
# helpers. The get() sees and records a delayed pointer. It is
# still important to see it so that it can be followed as soon as
# the mixlevelannotator resolves it.
for fptr in fptrs.values():
self.db.get(fptr)
def array_setup(self, arraydefnode):
pass
def rtti_type(self):
return FrameworkGcRuntimeTypeInfo_OpaqueNode.typename
def rtti_node_factory(self):
return FrameworkGcRuntimeTypeInfo_OpaqueNode
def gc_startup_code(self):
fnptr = self.db.gctransformer.frameworkgc_setup_ptr.value
yield '%s();' % (self.db.get(fnptr),)
def get_real_weakref_type(self):
from rpython.memory.gctypelayout import WEAKREF
return WEAKREF
def convert_weakref_to(self, ptarget):
from rpython.memory.gctypelayout import convert_weakref_to
return convert_weakref_to(ptarget)
def OP_GC_RELOAD_POSSIBLY_MOVED(self, funcgen, op):
if isinstance(op.args[1], Constant):
return '/* %s */' % (op,)
else:
args = [funcgen.expr(v) for v in op.args]
return '%s = %s; /* for moving GCs */' % (args[1], args[0])
def need_no_typeptr(self):
config = self.db.translator.config
return config.translation.gcremovetypeptr
def header_type(self, extra='*'):
# Fish out the C name of the 'struct pypy_header0'
HDR = self.db.gctransformer.HDR
return self.db.gettype(HDR).replace('@', extra)
def tid_fieldname(self, tid_field='tid'):
# Fish out the C name of the tid field.
HDR = self.db.gctransformer.HDR
hdr_node = self.db.gettypedefnode(HDR)
return hdr_node.c_struct_field_name(tid_field)
def OP_GC_GETTYPEPTR_GROUP(self, funcgen, op):
# expands to a number of steps, as per rpython/lltypesystem/opimpl.py,
# all implemented by a single call to a C macro.
[v_obj, c_grpptr, c_skipoffset, c_vtableinfo] = op.args
tid_field = c_vtableinfo.value[2]
typename = funcgen.db.gettype(op.result.concretetype)
return (
'%s = (%s)_OP_GET_NEXT_GROUP_MEMBER(%s, (pypy_halfword_t)%s->'
'_gcheader.%s, %s);'
% (funcgen.expr(op.result),
cdecl(typename, ''),
funcgen.expr(c_grpptr),
funcgen.expr(v_obj),
self.tid_fieldname(tid_field),
funcgen.expr(c_skipoffset)))
def OP_GC_WRITEBARRIER(self, funcgen, op):
raise Exception("the FramewokGCTransformer should handle this")
def OP_GC_GCFLAG_EXTRA(self, funcgen, op):
gcflag_extra = self.db.gctransformer.gcdata.gc.gcflag_extra
if gcflag_extra == 0:
return BasicGcPolicy.OP_GC_GCFLAG_EXTRA(self, funcgen, op)
subopnum = op.args[0].value
if subopnum == 1:
return '%s = 1; /* has_gcflag_extra */' % (
funcgen.expr(op.result),)
hdrfield = '((%s)%s)->%s' % (self.header_type(),
funcgen.expr(op.args[1]),
self.tid_fieldname())
parts = ['%s = (%s & %dL) != 0;' % (funcgen.expr(op.result),
hdrfield,
gcflag_extra)]
if subopnum == 2: # get_gcflag_extra
parts.append('/* get_gcflag_extra */')
elif subopnum == 3: # toggle_gcflag_extra
parts.insert(0, '%s ^= %dL;' % (hdrfield,
gcflag_extra))
parts.append('/* toggle_gcflag_extra */')
else:
raise AssertionError(subopnum)
return ' '.join(parts)
def OP_GC_BIT(self, funcgen, op):
# This is a two-arguments operation (x, y) where x is a
# pointer and y is a constant power of two. It returns 0 if
# "(*(Signed*)x) & y == 0", and non-zero if it is "== y".
#
# On x86-64, emitting this is better than emitting a load
# followed by an INT_AND for the case where y doesn't fit in
# 32 bits. I've seen situations where a register was wasted
# to contain the constant 2**32 throughout a complete messy
# function; the goal of this GC_BIT is to avoid that.
#
# Don't abuse, though. If you need to check several bits in
# sequence, then it's likely better to load the whole Signed
# first; using GC_BIT would result in multiple accesses to
# memory.
#
bitmask = op.args[1].value
assert bitmask > 0 and (bitmask & (bitmask - 1)) == 0
offset = 0
while bitmask >= 0x100:
offset += 1
bitmask >>= 8
if sys.byteorder == 'big':
offset = 'sizeof(Signed)-%s' % (offset+1)
return '%s = ((char *)%s)[%s] & %d;' % (funcgen.expr(op.result),
funcgen.expr(op.args[0]),
offset, bitmask)
class ShadowStackFrameworkGcPolicy(BasicFrameworkGcPolicy):
def gettransformer(self, translator, gchooks):
from rpython.memory.gctransform import shadowstack
return shadowstack.ShadowStackFrameworkGCTransformer(translator, gchooks)
def enter_roots_frame(self, funcgen, (c_gcdata, c_numcolors)):
numcolors = c_numcolors.value
# XXX hard-code the field name here
gcpol_ss = '%s->gcd_inst_root_stack_top' % funcgen.expr(c_gcdata)
#
yield ('typedef struct { void %s; } pypy_ss_t;'
% ', '.join(['*s%d' % i for i in range(numcolors)]))
yield 'pypy_ss_t *ss;'
funcgen.gcpol_ss = gcpol_ss
def OP_GC_PUSH_ROOTS(self, funcgen, op):
raise Exception("gc_push_roots should be removed by postprocess_graph")
def OP_GC_POP_ROOTS(self, funcgen, op):
raise Exception("gc_pop_roots should be removed by postprocess_graph")
def OP_GC_ENTER_ROOTS_FRAME(self, funcgen, op):
return 'ss = (pypy_ss_t *)%s; %s = (void *)(ss+1);' % (
funcgen.gcpol_ss, funcgen.gcpol_ss)
def OP_GC_LEAVE_ROOTS_FRAME(self, funcgen, op):
return '%s = (void *)ss;' % funcgen.gcpol_ss
def OP_GC_SAVE_ROOT(self, funcgen, op):
num = op.args[0].value
exprvalue = funcgen.expr(op.args[1])
return 'ss->s%d = (void *)%s;\t/* gc_save_root */' % (num, exprvalue)
def OP_GC_RESTORE_ROOT(self, funcgen, op):
num = op.args[0].value
exprvalue = funcgen.expr(op.args[1])
typename = funcgen.db.gettype(op.args[1].concretetype)
result = '%s = (%s)ss->s%d;' % (exprvalue, cdecl(typename, ''), num)
if isinstance(op.args[1], Constant):
return '/* %s\t* gc_restore_root */' % result
else:
return '%s\t/* gc_restore_root */' % result
class AsmGcRootFrameworkGcPolicy(BasicFrameworkGcPolicy):
def gettransformer(self, translator, gchooks):
from rpython.memory.gctransform import asmgcroot
return asmgcroot.AsmGcRootFrameworkGCTransformer(translator, gchooks)
def GC_KEEPALIVE(self, funcgen, v):
return 'pypy_asm_keepalive(%s);' % funcgen.expr(v)
def OP_GC_STACK_BOTTOM(self, funcgen, op):
return 'pypy_asm_stack_bottom();'
name_to_gcpolicy = {
'boehm': BoehmGcPolicy,
'ref': RefcountingGcPolicy,
'none': NoneGcPolicy,
'framework+shadowstack': ShadowStackFrameworkGcPolicy,
'framework+asmgcc': AsmGcRootFrameworkGcPolicy
}
|