| 1 |
from rpython.rlib import rgc |
| 2 |
from rpython.rlib.objectmodel import we_are_translated, always_inline, newlist_hint |
| 3 |
from rpython.rlib.rarithmetic import ovfcheck, highest_bit |
| 4 |
from rpython.rtyper.lltypesystem import llmemory, lltype, rstr |
| 5 |
from rpython.rtyper.annlowlevel import cast_instance_to_gcref |
| 6 |
from rpython.jit.metainterp.history import ( |
| 7 |
ConstInt, ConstPtr, JitCellToken, new_ref_dict) |
| 8 |
from rpython.jit.metainterp.resoperation import ResOperation, rop, OpHelpers |
| 9 |
from rpython.jit.metainterp.support import ptr2int |
| 10 |
from rpython.jit.backend.llsupport.symbolic import (WORD, |
| 11 |
get_field_token, get_array_token) |
| 12 |
from rpython.jit.backend.llsupport.descr import SizeDescr, ArrayDescr |
| 13 |
from rpython.jit.backend.llsupport.descr import (unpack_arraydescr, |
| 14 |
unpack_fielddescr, unpack_interiorfielddescr) |
| 15 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 16 |
|
| 17 |
FLAG_ARRAY = 0 |
| 18 |
FLAG_STR = 1 |
| 19 |
FLAG_UNICODE = 2 |
| 20 |
|
| 21 |
class BridgeExceptionNotFirst(Exception): |
| 22 |
pass |
| 23 |
|
| 24 |
class GcRewriterAssembler(object): |
| 25 |
""" This class performs the following rewrites on the list of operations: |
| 26 |
|
| 27 |
- Turn all NEW_xxx to either a CALL_R/CHECK_MEMORY_ERROR, |
| 28 |
or a CALL_MALLOC_NURSERY, |
| 29 |
followed by SETFIELDs in order to initialize their GC fields. The |
| 30 |
two advantages of CALL_MALLOC_NURSERY is that it inlines the common |
| 31 |
path, and we need only one such operation to allocate several blocks |
| 32 |
of memory at once. |
| 33 |
|
| 34 |
- Add COND_CALLs to the write barrier before SETFIELD_GC and |
| 35 |
SETARRAYITEM_GC operations. |
| 36 |
|
| 37 |
- Rewrites copystrcontent to a call to memcopy |
| 38 |
|
| 39 |
- XXX does more than that, please write it down |
| 40 |
|
| 41 |
'_write_barrier_applied' contains a dictionary of variable -> None. |
| 42 |
If a variable is in the dictionary, next setfields can be called without |
| 43 |
a write barrier. The idea is that an object that was freshly allocated |
| 44 |
or already write_barrier'd don't need another write_barrier if there |
| 45 |
was no potentially collecting resop inbetween. |
| 46 |
""" |
| 47 |
|
| 48 |
_previous_size = -1 |
| 49 |
_op_malloc_nursery = None |
| 50 |
_v_last_malloced_nursery = None |
| 51 |
c_zero = ConstInt(0) |
| 52 |
c_null = ConstPtr(lltype.nullptr(llmemory.GCREF.TO)) |
| 53 |
|
| 54 |
def __init__(self, gc_ll_descr, cpu, num_operations_hint=1): |
| 55 |
self.gc_ll_descr = gc_ll_descr |
| 56 |
self.cpu = cpu |
| 57 |
|
| 58 |
self._newops = newlist_hint(num_operations_hint) |
| 59 |
self._known_lengths = {} |
| 60 |
self._write_barrier_applied = {} |
| 61 |
self._delayed_zero_setfields = {} |
| 62 |
self.last_zero_arrays = [] |
| 63 |
self._setarrayitems_occurred = {} |
| 64 |
self._constant_additions = {} |
| 65 |
|
| 66 |
def remember_known_length(self, op, val): |
| 67 |
self._known_lengths[op] = val |
| 68 |
|
| 69 |
def remember_setarrayitem_occurred(self, op, index): |
| 70 |
op = self.get_box_replacement(op) |
| 71 |
try: |
| 72 |
subs = self._setarrayitems_occurred[op] |
| 73 |
except KeyError: |
| 74 |
subs = {} |
| 75 |
self._setarrayitems_occurred[op] = subs |
| 76 |
subs[index] = None |
| 77 |
|
| 78 |
def setarrayitems_occurred(self, op): |
| 79 |
return self._setarrayitems_occurred[self.get_box_replacement(op)] |
| 80 |
|
| 81 |
def known_length(self, op, default): |
| 82 |
return self._known_lengths.get(op, default) |
| 83 |
|
| 84 |
def delayed_zero_setfields(self, op): |
| 85 |
op = self.get_box_replacement(op) |
| 86 |
try: |
| 87 |
d = self._delayed_zero_setfields[op] |
| 88 |
except KeyError: |
| 89 |
d = {} |
| 90 |
self._delayed_zero_setfields[op] = d |
| 91 |
return d |
| 92 |
|
| 93 |
def get_box_replacement(self, op, allow_none=False): |
| 94 |
if allow_none and op is None: |
| 95 |
return None |
| 96 |
while op.get_forwarded(): |
| 97 |
op = op.get_forwarded() |
| 98 |
return op |
| 99 |
|
| 100 |
def emit_op(self, op): |
| 101 |
op = self.get_box_replacement(op) |
| 102 |
orig_op = op |
| 103 |
replaced = False |
| 104 |
opnum = op.getopnum() |
| 105 |
keep = (opnum == rop.JIT_DEBUG) |
| 106 |
for i in range(op.numargs()): |
| 107 |
orig_arg = op.getarg(i) |
| 108 |
arg = self.get_box_replacement(orig_arg) |
| 109 |
if isinstance(arg, ConstPtr) and bool(arg.value) and not keep: |
| 110 |
arg = self.remove_constptr(arg) |
| 111 |
if orig_arg is not arg: |
| 112 |
if not replaced: |
| 113 |
op = op.copy_and_change(opnum) |
| 114 |
orig_op.set_forwarded(op) |
| 115 |
replaced = True |
| 116 |
op.setarg(i, arg) |
| 117 |
if rop.is_guard(opnum): |
| 118 |
if not replaced: |
| 119 |
op = op.copy_and_change(opnum) |
| 120 |
orig_op.set_forwarded(op) |
| 121 |
op.setfailargs([self.get_box_replacement(a, True) |
| 122 |
for a in op.getfailargs()]) |
| 123 |
if rop.is_guard(opnum) or opnum == rop.FINISH: |
| 124 |
llref = cast_instance_to_gcref(op.getdescr()) |
| 125 |
self.gcrefs_output_list.append(llref) |
| 126 |
self._newops.append(op) |
| 127 |
|
| 128 |
def replace_op_with(self, op, newop): |
| 129 |
assert not op.get_forwarded() |
| 130 |
op.set_forwarded(newop) |
| 131 |
|
| 132 |
def handle_setarrayitem(self, op): |
| 133 |
itemsize, basesize, _ = unpack_arraydescr(op.getdescr()) |
| 134 |
ptr_box = op.getarg(0) |
| 135 |
index_box = op.getarg(1) |
| 136 |
value_box = op.getarg(2) |
| 137 |
self.emit_gc_store_or_indexed(op, ptr_box, index_box, value_box, |
| 138 |
itemsize, itemsize, basesize) |
| 139 |
|
| 140 |
def emit_gc_store_or_indexed(self, op, ptr_box, index_box, value_box, |
| 141 |
itemsize, factor, offset): |
| 142 |
index_box, offset = self._try_use_older_box(index_box, factor, |
| 143 |
offset) |
| 144 |
factor, offset, index_box = \ |
| 145 |
self._emit_mul_if_factor_offset_not_supported(index_box, |
| 146 |
factor, offset) |
| 147 |
|
| 148 |
if index_box is None: |
| 149 |
args = [ptr_box, ConstInt(offset), value_box, ConstInt(itemsize)] |
| 150 |
newload = ResOperation(rop.GC_STORE, args) |
| 151 |
else: |
| 152 |
args = [ptr_box, index_box, value_box, ConstInt(factor), |
| 153 |
ConstInt(offset), ConstInt(itemsize)] |
| 154 |
newload = ResOperation(rop.GC_STORE_INDEXED, args) |
| 155 |
if op is not None: |
| 156 |
self.replace_op_with(op, newload) |
| 157 |
else: |
| 158 |
self.emit_op(newload) |
| 159 |
|
| 160 |
def handle_getarrayitem(self, op): |
| 161 |
itemsize, ofs, sign = unpack_arraydescr(op.getdescr()) |
| 162 |
ptr_box = op.getarg(0) |
| 163 |
index_box = op.getarg(1) |
| 164 |
self.emit_gc_load_or_indexed(op, ptr_box, index_box, itemsize, itemsize, ofs, sign) |
| 165 |
|
| 166 |
def _emit_mul_if_factor_offset_not_supported(self, index_box, |
| 167 |
factor, offset): |
| 168 |
factor, offset, new_index_box, emit = cpu_simplify_scale(self.cpu, index_box, factor, offset) |
| 169 |
if emit: |
| 170 |
self.emit_op(new_index_box) |
| 171 |
return factor, offset, new_index_box |
| 172 |
|
| 173 |
def _try_use_older_box(self, index_box, factor, offset): |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (not isinstance(index_box, ConstInt) and |
| 179 |
index_box in self._constant_additions): |
| 180 |
index_box, extra_offset = self._constant_additions[index_box] |
| 181 |
offset += factor * extra_offset |
| 182 |
return index_box, offset |
| 183 |
|
| 184 |
def emit_gc_load_or_indexed(self, op, ptr_box, index_box, itemsize, |
| 185 |
factor, offset, sign, type='i'): |
| 186 |
index_box, offset = self._try_use_older_box(index_box, factor, |
| 187 |
offset) |
| 188 |
factor, offset, index_box = \ |
| 189 |
self._emit_mul_if_factor_offset_not_supported(index_box, |
| 190 |
factor, offset) |
| 191 |
|
| 192 |
if sign: |
| 193 |
|
| 194 |
itemsize = -itemsize |
| 195 |
|
| 196 |
optype = type |
| 197 |
if op is not None: |
| 198 |
optype = op.type |
| 199 |
if index_box is None: |
| 200 |
args = [ptr_box, ConstInt(offset), ConstInt(itemsize)] |
| 201 |
newload = ResOperation(OpHelpers.get_gc_load(optype), args) |
| 202 |
else: |
| 203 |
args = [ptr_box, index_box, ConstInt(factor), |
| 204 |
ConstInt(offset), ConstInt(itemsize)] |
| 205 |
newload = ResOperation(OpHelpers.get_gc_load_indexed(optype), args) |
| 206 |
if op is None: |
| 207 |
self.emit_op(newload) |
| 208 |
else: |
| 209 |
self.replace_op_with(op, newload) |
| 210 |
return newload |
| 211 |
|
| 212 |
def transform_to_gc_load(self, op): |
| 213 |
NOT_SIGNED = 0 |
| 214 |
CINT_ZERO = ConstInt(0) |
| 215 |
opnum = op.getopnum() |
| 216 |
if rop.is_getarrayitem(opnum) or \ |
| 217 |
opnum in (rop.GETARRAYITEM_RAW_I, |
| 218 |
rop.GETARRAYITEM_RAW_F): |
| 219 |
self.handle_getarrayitem(op) |
| 220 |
elif opnum in (rop.SETARRAYITEM_GC, rop.SETARRAYITEM_RAW): |
| 221 |
self.handle_setarrayitem(op) |
| 222 |
elif opnum == rop.RAW_STORE: |
| 223 |
itemsize, ofs, _ = unpack_arraydescr(op.getdescr()) |
| 224 |
ptr_box = op.getarg(0) |
| 225 |
index_box = op.getarg(1) |
| 226 |
value_box = op.getarg(2) |
| 227 |
self.emit_gc_store_or_indexed(op, ptr_box, index_box, value_box, itemsize, 1, ofs) |
| 228 |
elif opnum in (rop.RAW_LOAD_I, rop.RAW_LOAD_F): |
| 229 |
itemsize, ofs, sign = unpack_arraydescr(op.getdescr()) |
| 230 |
ptr_box = op.getarg(0) |
| 231 |
index_box = op.getarg(1) |
| 232 |
self.emit_gc_load_or_indexed(op, ptr_box, index_box, itemsize, 1, ofs, sign) |
| 233 |
elif opnum in (rop.GETINTERIORFIELD_GC_I, rop.GETINTERIORFIELD_GC_R, |
| 234 |
rop.GETINTERIORFIELD_GC_F): |
| 235 |
ofs, itemsize, fieldsize, sign = unpack_interiorfielddescr(op.getdescr()) |
| 236 |
ptr_box = op.getarg(0) |
| 237 |
index_box = op.getarg(1) |
| 238 |
self.emit_gc_load_or_indexed(op, ptr_box, index_box, fieldsize, itemsize, ofs, sign) |
| 239 |
elif opnum in (rop.SETINTERIORFIELD_RAW, rop.SETINTERIORFIELD_GC): |
| 240 |
ofs, itemsize, fieldsize, sign = unpack_interiorfielddescr(op.getdescr()) |
| 241 |
ptr_box = op.getarg(0) |
| 242 |
index_box = op.getarg(1) |
| 243 |
value_box = op.getarg(2) |
| 244 |
self.emit_gc_store_or_indexed(op, ptr_box, index_box, value_box, |
| 245 |
fieldsize, itemsize, ofs) |
| 246 |
elif opnum in (rop.GETFIELD_GC_I, rop.GETFIELD_GC_F, rop.GETFIELD_GC_R, |
| 247 |
rop.GETFIELD_RAW_I, rop.GETFIELD_RAW_F, rop.GETFIELD_RAW_R): |
| 248 |
ofs, itemsize, sign = unpack_fielddescr(op.getdescr()) |
| 249 |
ptr_box = op.getarg(0) |
| 250 |
if op.getopnum() in (rop.GETFIELD_GC_F, rop.GETFIELD_GC_I, rop.GETFIELD_GC_R): |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
self.emit_pending_zeros() |
| 258 |
self.emit_gc_load_or_indexed(op, ptr_box, ConstInt(0), itemsize, 1, ofs, sign) |
| 259 |
self.emit_op(op) |
| 260 |
return True |
| 261 |
self.emit_gc_load_or_indexed(op, ptr_box, ConstInt(0), itemsize, 1, ofs, sign) |
| 262 |
elif opnum in (rop.SETFIELD_GC, rop.SETFIELD_RAW): |
| 263 |
ofs, itemsize, sign = unpack_fielddescr(op.getdescr()) |
| 264 |
ptr_box = op.getarg(0) |
| 265 |
value_box = op.getarg(1) |
| 266 |
self.emit_gc_store_or_indexed(op, ptr_box, ConstInt(0), value_box, itemsize, 1, ofs) |
| 267 |
elif opnum == rop.ARRAYLEN_GC: |
| 268 |
descr = op.getdescr() |
| 269 |
assert isinstance(descr, ArrayDescr) |
| 270 |
ofs = descr.lendescr.offset |
| 271 |
self.emit_gc_load_or_indexed(op, op.getarg(0), ConstInt(0), |
| 272 |
WORD, 1, ofs, NOT_SIGNED) |
| 273 |
elif opnum == rop.STRLEN: |
| 274 |
basesize, itemsize, ofs_length = get_array_token(rstr.STR, |
| 275 |
self.cpu.translate_support_code) |
| 276 |
self.emit_gc_load_or_indexed(op, op.getarg(0), ConstInt(0), |
| 277 |
WORD, 1, ofs_length, NOT_SIGNED) |
| 278 |
elif opnum == rop.UNICODELEN: |
| 279 |
basesize, itemsize, ofs_length = get_array_token(rstr.UNICODE, |
| 280 |
self.cpu.translate_support_code) |
| 281 |
self.emit_gc_load_or_indexed(op, op.getarg(0), ConstInt(0), |
| 282 |
WORD, 1, ofs_length, NOT_SIGNED) |
| 283 |
elif opnum == rop.STRHASH: |
| 284 |
offset, size = get_field_token(rstr.STR, |
| 285 |
'hash', self.cpu.translate_support_code) |
| 286 |
assert size == WORD |
| 287 |
self.emit_gc_load_or_indexed(op, op.getarg(0), ConstInt(0), |
| 288 |
WORD, 1, offset, sign=True) |
| 289 |
elif opnum == rop.UNICODEHASH: |
| 290 |
offset, size = get_field_token(rstr.UNICODE, |
| 291 |
'hash', self.cpu.translate_support_code) |
| 292 |
assert size == WORD |
| 293 |
self.emit_gc_load_or_indexed(op, op.getarg(0), ConstInt(0), |
| 294 |
WORD, 1, offset, sign=True) |
| 295 |
elif opnum == rop.STRGETITEM: |
| 296 |
basesize, itemsize, ofs_length = get_array_token(rstr.STR, |
| 297 |
self.cpu.translate_support_code) |
| 298 |
assert itemsize == 1 |
| 299 |
basesize -= 1 |
| 300 |
self.emit_gc_load_or_indexed(op, op.getarg(0), op.getarg(1), |
| 301 |
itemsize, itemsize, basesize, NOT_SIGNED) |
| 302 |
elif opnum == rop.UNICODEGETITEM: |
| 303 |
basesize, itemsize, ofs_length = get_array_token(rstr.UNICODE, |
| 304 |
self.cpu.translate_support_code) |
| 305 |
self.emit_gc_load_or_indexed(op, op.getarg(0), op.getarg(1), |
| 306 |
itemsize, itemsize, basesize, NOT_SIGNED) |
| 307 |
elif opnum == rop.STRSETITEM: |
| 308 |
basesize, itemsize, ofs_length = get_array_token(rstr.STR, |
| 309 |
self.cpu.translate_support_code) |
| 310 |
assert itemsize == 1 |
| 311 |
basesize -= 1 |
| 312 |
self.emit_gc_store_or_indexed(op, op.getarg(0), op.getarg(1), op.getarg(2), |
| 313 |
itemsize, itemsize, basesize) |
| 314 |
elif opnum == rop.UNICODESETITEM: |
| 315 |
basesize, itemsize, ofs_length = get_array_token(rstr.UNICODE, |
| 316 |
self.cpu.translate_support_code) |
| 317 |
self.emit_gc_store_or_indexed(op, op.getarg(0), op.getarg(1), op.getarg(2), |
| 318 |
itemsize, itemsize, basesize) |
| 319 |
elif opnum in (rop.GC_LOAD_INDEXED_I, |
| 320 |
rop.GC_LOAD_INDEXED_F, |
| 321 |
rop.GC_LOAD_INDEXED_R): |
| 322 |
scale_box = op.getarg(2) |
| 323 |
offset_box = op.getarg(3) |
| 324 |
size_box = op.getarg(4) |
| 325 |
assert isinstance(scale_box, ConstInt) |
| 326 |
assert isinstance(offset_box, ConstInt) |
| 327 |
assert isinstance(size_box, ConstInt) |
| 328 |
self.emit_gc_load_or_indexed(op, op.getarg(0), op.getarg(1), |
| 329 |
abs(size_box.value), scale_box.value, offset_box.value, |
| 330 |
size_box.value < 0) |
| 331 |
elif opnum == rop.GC_STORE_INDEXED: |
| 332 |
scale_box = op.getarg(3) |
| 333 |
offset_box = op.getarg(4) |
| 334 |
size_box = op.getarg(5) |
| 335 |
assert isinstance(scale_box, ConstInt) |
| 336 |
assert isinstance(offset_box, ConstInt) |
| 337 |
assert isinstance(size_box, ConstInt) |
| 338 |
|
| 339 |
self.emit_gc_store_or_indexed(op, op.getarg(0), op.getarg(1), |
| 340 |
op.getarg(2), |
| 341 |
abs(size_box.value), scale_box.value, offset_box.value) |
| 342 |
return False |
| 343 |
|
| 344 |
|
| 345 |
def rewrite(self, operations, gcrefs_output_list): |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
self.gcrefs_output_list = gcrefs_output_list |
| 353 |
self.gcrefs_map = None |
| 354 |
self.gcrefs_recently_loaded = None |
| 355 |
operations = self.remove_bridge_exception(operations) |
| 356 |
self._changed_op = None |
| 357 |
for i in range(len(operations)): |
| 358 |
op = operations[i] |
| 359 |
if op.get_forwarded(): |
| 360 |
msg = '[rewrite] operations at %d has forwarded info %s\n' % (i, op.repr({})) |
| 361 |
if we_are_translated(): |
| 362 |
llop.debug_print(lltype.Void, msg) |
| 363 |
raise NotImplementedError(msg) |
| 364 |
if op.getopnum() == rop.DEBUG_MERGE_POINT: |
| 365 |
continue |
| 366 |
if op is self._changed_op: |
| 367 |
op = self._changed_op_to |
| 368 |
|
| 369 |
if self.transform_to_gc_load(op): |
| 370 |
continue |
| 371 |
|
| 372 |
opnum = op.getopnum() |
| 373 |
if rop.is_malloc(opnum): |
| 374 |
self.handle_malloc_operation(op) |
| 375 |
continue |
| 376 |
if (rop.is_guard(opnum) or |
| 377 |
self.could_merge_with_next_guard(op, i, operations)): |
| 378 |
self.emit_pending_zeros() |
| 379 |
elif rop.can_malloc(opnum): |
| 380 |
self.emitting_an_operation_that_can_collect() |
| 381 |
elif opnum == rop.LABEL: |
| 382 |
self.emit_label() |
| 383 |
|
| 384 |
if opnum == rop.INT_ADD or opnum == rop.INT_ADD_OVF: |
| 385 |
self.record_int_add_or_sub(op, is_subtraction=False) |
| 386 |
elif opnum == rop.INT_SUB or opnum == rop.INT_SUB_OVF: |
| 387 |
self.record_int_add_or_sub(op, is_subtraction=True) |
| 388 |
|
| 389 |
if opnum == rop.COPYSTRCONTENT or opnum == rop.COPYUNICODECONTENT: |
| 390 |
self.rewrite_copy_str_content(op) |
| 391 |
continue |
| 392 |
|
| 393 |
if self.gc_ll_descr.write_barrier_descr is not None: |
| 394 |
if opnum == rop.SETFIELD_GC: |
| 395 |
self.consider_setfield_gc(op) |
| 396 |
self.handle_write_barrier_setfield(op) |
| 397 |
continue |
| 398 |
if opnum == rop.SETINTERIORFIELD_GC: |
| 399 |
self.handle_write_barrier_setinteriorfield(op) |
| 400 |
continue |
| 401 |
if opnum == rop.SETARRAYITEM_GC: |
| 402 |
self.consider_setarrayitem_gc(op) |
| 403 |
self.handle_write_barrier_setarrayitem(op) |
| 404 |
continue |
| 405 |
else: |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if opnum == rop.SETFIELD_GC: |
| 410 |
self.consider_setfield_gc(op) |
| 411 |
elif opnum == rop.SETARRAYITEM_GC: |
| 412 |
self.consider_setarrayitem_gc(op) |
| 413 |
|
| 414 |
if OpHelpers.is_call_assembler(opnum): |
| 415 |
self.handle_call_assembler(op) |
| 416 |
continue |
| 417 |
if opnum == rop.JUMP or opnum == rop.FINISH: |
| 418 |
self.emit_pending_zeros() |
| 419 |
if opnum == rop.GUARD_ALWAYS_FAILS: |
| 420 |
|
| 421 |
op1 = ResOperation(rop.SAME_AS_I, [ConstInt(0)]) |
| 422 |
self.emit_op(op1) |
| 423 |
newop = op.copy_and_change(rop.GUARD_VALUE, args=[op1, ConstInt(1)]) |
| 424 |
newop.setfailargs(op.getfailargs()) |
| 425 |
self.emit_op(newop) |
| 426 |
continue |
| 427 |
|
| 428 |
self.emit_op(op) |
| 429 |
return self._newops |
| 430 |
|
| 431 |
def could_merge_with_next_guard(self, op, i, operations): |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if not rop.is_comparison(op.opnum): |
| 436 |
return rop.is_ovf(op.opnum) |
| 437 |
if i + 1 >= len(operations): |
| 438 |
return False |
| 439 |
next_op = operations[i + 1] |
| 440 |
opnum = next_op.getopnum() |
| 441 |
if not (opnum == rop.GUARD_TRUE or |
| 442 |
opnum == rop.GUARD_FALSE or |
| 443 |
opnum == rop.COND_CALL): |
| 444 |
return False |
| 445 |
if next_op.getarg(0) is not op: |
| 446 |
return False |
| 447 |
self.remove_tested_failarg(next_op) |
| 448 |
return True |
| 449 |
|
| 450 |
def remove_tested_failarg(self, op): |
| 451 |
opnum = op.getopnum() |
| 452 |
if not (opnum == rop.GUARD_TRUE or opnum == rop.GUARD_FALSE): |
| 453 |
return |
| 454 |
if op.getarg(0).is_vector(): |
| 455 |
return |
| 456 |
try: |
| 457 |
i = op.getfailargs().index(op.getarg(0)) |
| 458 |
except ValueError: |
| 459 |
return |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
value = int(opnum == rop.GUARD_FALSE) |
| 464 |
op1 = ResOperation(rop.SAME_AS_I, [ConstInt(value)]) |
| 465 |
self.emit_op(op1) |
| 466 |
lst = op.getfailargs()[:] |
| 467 |
lst[i] = op1 |
| 468 |
newop = op.copy_and_change(opnum) |
| 469 |
newop.setfailargs(lst) |
| 470 |
self._changed_op = op |
| 471 |
self._changed_op_to = newop |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
def handle_malloc_operation(self, op): |
| 476 |
opnum = op.getopnum() |
| 477 |
if opnum == rop.NEW: |
| 478 |
self.handle_new_fixedsize(op.getdescr(), op) |
| 479 |
elif opnum == rop.NEW_WITH_VTABLE: |
| 480 |
descr = op.getdescr() |
| 481 |
self.handle_new_fixedsize(descr, op) |
| 482 |
if self.gc_ll_descr.fielddescr_vtable is not None: |
| 483 |
self.emit_setfield(op, ConstInt(descr.get_vtable()), |
| 484 |
descr=self.gc_ll_descr.fielddescr_vtable) |
| 485 |
elif opnum == rop.NEW_ARRAY or opnum == rop.NEW_ARRAY_CLEAR: |
| 486 |
descr = op.getdescr() |
| 487 |
assert isinstance(descr, ArrayDescr) |
| 488 |
self.handle_new_array(descr, op) |
| 489 |
elif opnum == rop.NEWSTR: |
| 490 |
self.handle_new_array(self.gc_ll_descr.str_descr, op, |
| 491 |
kind=FLAG_STR) |
| 492 |
elif opnum == rop.NEWUNICODE: |
| 493 |
self.handle_new_array(self.gc_ll_descr.unicode_descr, op, |
| 494 |
kind=FLAG_UNICODE) |
| 495 |
else: |
| 496 |
raise NotImplementedError(op.getopname()) |
| 497 |
|
| 498 |
def clear_gc_fields(self, descr, result): |
| 499 |
if self.gc_ll_descr.malloc_zero_filled: |
| 500 |
return |
| 501 |
d = self.delayed_zero_setfields(result) |
| 502 |
for fielddescr in descr.gc_fielddescrs: |
| 503 |
ofs = self.cpu.unpack_fielddescr(fielddescr) |
| 504 |
d[ofs] = None |
| 505 |
|
| 506 |
def consider_setfield_gc(self, op): |
| 507 |
offset = self.cpu.unpack_fielddescr(op.getdescr()) |
| 508 |
try: |
| 509 |
del self._delayed_zero_setfields[ |
| 510 |
self.get_box_replacement(op.getarg(0))][offset] |
| 511 |
except KeyError: |
| 512 |
pass |
| 513 |
|
| 514 |
def consider_setarrayitem_gc(self, op): |
| 515 |
array_box = op.getarg(0) |
| 516 |
index_box = op.getarg(1) |
| 517 |
if not isinstance(array_box, ConstPtr) and index_box.is_constant(): |
| 518 |
self.remember_setarrayitem_occurred(array_box, index_box.getint()) |
| 519 |
|
| 520 |
def clear_varsize_gc_fields(self, kind, descr, result, v_length, opnum): |
| 521 |
if self.gc_ll_descr.malloc_zero_filled: |
| 522 |
return |
| 523 |
if kind == FLAG_ARRAY: |
| 524 |
if descr.is_array_of_structs() or descr.is_array_of_pointers(): |
| 525 |
assert opnum == rop.NEW_ARRAY_CLEAR |
| 526 |
if opnum == rop.NEW_ARRAY_CLEAR: |
| 527 |
self.handle_clear_array_contents(descr, result, v_length) |
| 528 |
return |
| 529 |
if kind == FLAG_STR: |
| 530 |
hash_descr = self.gc_ll_descr.str_hash_descr |
| 531 |
elif kind == FLAG_UNICODE: |
| 532 |
hash_descr = self.gc_ll_descr.unicode_hash_descr |
| 533 |
else: |
| 534 |
return |
| 535 |
self.emit_setfield(result, self.c_zero, descr=hash_descr) |
| 536 |
|
| 537 |
def handle_new_fixedsize(self, descr, op): |
| 538 |
assert isinstance(descr, SizeDescr) |
| 539 |
size = descr.size |
| 540 |
if self.gen_malloc_nursery(size, op): |
| 541 |
self.gen_initialize_tid(op, descr.tid) |
| 542 |
else: |
| 543 |
self.gen_malloc_fixedsize(size, descr.tid, op) |
| 544 |
self.clear_gc_fields(descr, op) |
| 545 |
|
| 546 |
def handle_new_array(self, arraydescr, op, kind=FLAG_ARRAY): |
| 547 |
v_length = self.get_box_replacement(op.getarg(0)) |
| 548 |
total_size = -1 |
| 549 |
if isinstance(v_length, ConstInt): |
| 550 |
num_elem = v_length.getint() |
| 551 |
self.remember_known_length(op, num_elem) |
| 552 |
try: |
| 553 |
var_size = ovfcheck(arraydescr.itemsize * num_elem) |
| 554 |
total_size = ovfcheck(arraydescr.basesize + var_size) |
| 555 |
except OverflowError: |
| 556 |
pass |
| 557 |
elif arraydescr.itemsize == 0: |
| 558 |
total_size = arraydescr.basesize |
| 559 |
elif (self.gc_ll_descr.can_use_nursery_malloc(1) and |
| 560 |
self.gen_malloc_nursery_varsize(arraydescr.itemsize, |
| 561 |
v_length, op, arraydescr, kind=kind)): |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
self.gen_initialize_len(op, v_length, arraydescr.lendescr) |
| 566 |
self.clear_varsize_gc_fields(kind, op.getdescr(), op, |
| 567 |
v_length, op.getopnum()) |
| 568 |
return |
| 569 |
if (total_size >= 0 and |
| 570 |
self.gen_malloc_nursery(total_size, op)): |
| 571 |
self.gen_initialize_tid(op, arraydescr.tid) |
| 572 |
self.gen_initialize_len(op, v_length, arraydescr.lendescr) |
| 573 |
elif self.gc_ll_descr.kind == 'boehm': |
| 574 |
self.gen_boehm_malloc_array(arraydescr, v_length, op) |
| 575 |
else: |
| 576 |
opnum = op.getopnum() |
| 577 |
if opnum == rop.NEW_ARRAY or opnum == rop.NEW_ARRAY_CLEAR: |
| 578 |
self.gen_malloc_array(arraydescr, v_length, op) |
| 579 |
elif opnum == rop.NEWSTR: |
| 580 |
self.gen_malloc_str(v_length, op) |
| 581 |
elif opnum == rop.NEWUNICODE: |
| 582 |
self.gen_malloc_unicode(v_length, op) |
| 583 |
else: |
| 584 |
raise NotImplementedError(op.getopname()) |
| 585 |
self.clear_varsize_gc_fields(kind, op.getdescr(), op, v_length, |
| 586 |
op.getopnum()) |
| 587 |
|
| 588 |
def handle_clear_array_contents(self, arraydescr, v_arr, v_length): |
| 589 |
assert v_length is not None |
| 590 |
if isinstance(v_length, ConstInt) and v_length.getint() == 0: |
| 591 |
return |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
assert isinstance(arraydescr, ArrayDescr) |
| 598 |
scale = arraydescr.itemsize |
| 599 |
v_length_scaled = v_length |
| 600 |
if not isinstance(v_length, ConstInt): |
| 601 |
scale, offset, v_length_scaled = \ |
| 602 |
self._emit_mul_if_factor_offset_not_supported(v_length, scale, 0) |
| 603 |
v_scale = ConstInt(scale) |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
args = [v_arr, self.c_zero, v_length_scaled, ConstInt(scale), v_scale] |
| 608 |
o = ResOperation(rop.ZERO_ARRAY, args, descr=arraydescr) |
| 609 |
self.emit_op(o) |
| 610 |
if isinstance(v_length, ConstInt): |
| 611 |
self.last_zero_arrays.append(self._newops[-1]) |
| 612 |
|
| 613 |
def gen_malloc_frame(self, frame_info): |
| 614 |
descrs = self.gc_ll_descr.getframedescrs(self.cpu) |
| 615 |
if self.gc_ll_descr.kind == 'boehm': |
| 616 |
ofs, size, sign = unpack_fielddescr(descrs.jfi_frame_depth) |
| 617 |
if sign: |
| 618 |
size = -size |
| 619 |
args = [ConstInt(frame_info), ConstInt(ofs), ConstInt(size)] |
| 620 |
size = ResOperation(rop.GC_LOAD_I, args) |
| 621 |
self.emit_op(size) |
| 622 |
frame = ResOperation(rop.NEW_ARRAY, [size], |
| 623 |
descr=descrs.arraydescr) |
| 624 |
self.handle_new_array(descrs.arraydescr, frame) |
| 625 |
return self.get_box_replacement(frame) |
| 626 |
else: |
| 627 |
|
| 628 |
ofs, size, sign = unpack_fielddescr(descrs.jfi_frame_size) |
| 629 |
if sign: |
| 630 |
size = -size |
| 631 |
args = [ConstInt(frame_info), ConstInt(ofs), ConstInt(size)] |
| 632 |
size = ResOperation(rop.GC_LOAD_I, args) |
| 633 |
self.emit_op(size) |
| 634 |
frame = self.gen_malloc_nursery_varsize_frame(size) |
| 635 |
self.gen_initialize_tid(frame, descrs.arraydescr.tid) |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
length = self.emit_getfield(ConstInt(frame_info), |
| 640 |
descr=descrs.jfi_frame_depth, raw=True) |
| 641 |
self.emit_setfield(frame, self.c_null, |
| 642 |
descr=descrs.jf_savedata) |
| 643 |
self.emit_setfield(frame, self.c_null, |
| 644 |
descr=descrs.jf_force_descr) |
| 645 |
self.emit_setfield(frame, self.c_null, |
| 646 |
descr=descrs.jf_descr) |
| 647 |
self.emit_setfield(frame, self.c_null, |
| 648 |
descr=descrs.jf_guard_exc) |
| 649 |
self.emit_setfield(frame, self.c_null, |
| 650 |
descr=descrs.jf_forward) |
| 651 |
self.gen_initialize_len(frame, length, |
| 652 |
descrs.arraydescr.lendescr) |
| 653 |
return self.get_box_replacement(frame) |
| 654 |
|
| 655 |
def emit_getfield(self, ptr, descr, type='i', raw=False): |
| 656 |
ofs, size, sign = unpack_fielddescr(descr) |
| 657 |
op = self.emit_gc_load_or_indexed(None, ptr, ConstInt(0), size, 1, ofs, sign) |
| 658 |
return op |
| 659 |
|
| 660 |
def emit_setfield(self, ptr, value, descr): |
| 661 |
ofs, size, sign = unpack_fielddescr(descr) |
| 662 |
self.emit_gc_store_or_indexed(None, ptr, ConstInt(0), value, |
| 663 |
size, 1, ofs) |
| 664 |
|
| 665 |
def handle_call_assembler(self, op): |
| 666 |
descrs = self.gc_ll_descr.getframedescrs(self.cpu) |
| 667 |
loop_token = op.getdescr() |
| 668 |
assert isinstance(loop_token, JitCellToken) |
| 669 |
llfi = ptr2int(loop_token.compiled_loop_token.frame_info) |
| 670 |
frame = self.gen_malloc_frame(llfi) |
| 671 |
self.emit_setfield(frame, ConstInt(llfi), descr=descrs.jf_frame_info) |
| 672 |
arglist = op.getarglist() |
| 673 |
index_list = loop_token.compiled_loop_token._ll_initial_locs |
| 674 |
for i, arg in enumerate(arglist): |
| 675 |
descr = self.cpu.getarraydescr_for_frame(arg.type) |
| 676 |
assert self.cpu.JITFRAME_FIXED_SIZE & 1 == 0 |
| 677 |
_, itemsize, _ = self.cpu.unpack_arraydescr_size(descr) |
| 678 |
array_offset = index_list[i] |
| 679 |
|
| 680 |
_, basesize, _ = unpack_arraydescr(descr) |
| 681 |
offset = basesize + array_offset |
| 682 |
args = [frame, ConstInt(offset), arg, ConstInt(itemsize)] |
| 683 |
self.emit_op(ResOperation(rop.GC_STORE, args)) |
| 684 |
|
| 685 |
descr = op.getdescr() |
| 686 |
assert isinstance(descr, JitCellToken) |
| 687 |
jd = descr.outermost_jitdriver_sd |
| 688 |
args = [frame] |
| 689 |
if jd and jd.index_of_virtualizable >= 0: |
| 690 |
args = [frame, arglist[jd.index_of_virtualizable]] |
| 691 |
else: |
| 692 |
args = [frame] |
| 693 |
call_asm = ResOperation(op.getopnum(), args, descr=op.getdescr()) |
| 694 |
self.replace_op_with(self.get_box_replacement(op), call_asm) |
| 695 |
self.emit_op(call_asm) |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
def emitting_an_operation_that_can_collect(self): |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
self._op_malloc_nursery = None |
| 706 |
self._write_barrier_applied.clear() |
| 707 |
self.emit_pending_zeros() |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
self._constant_additions.clear() |
| 712 |
|
| 713 |
def write_barrier_applied(self, op): |
| 714 |
return self.get_box_replacement(op) in self._write_barrier_applied |
| 715 |
|
| 716 |
def remember_write_barrier(self, op): |
| 717 |
self._write_barrier_applied[self.get_box_replacement(op)] = None |
| 718 |
|
| 719 |
def emit_pending_zeros(self): |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
for op in self.last_zero_arrays: |
| 724 |
assert op.getopnum() == rop.ZERO_ARRAY |
| 725 |
descr = op.getdescr() |
| 726 |
assert isinstance(descr, ArrayDescr) |
| 727 |
scale = descr.itemsize |
| 728 |
box = op.getarg(0) |
| 729 |
try: |
| 730 |
intset = self.setarrayitems_occurred(box) |
| 731 |
except KeyError: |
| 732 |
start_box = op.getarg(1) |
| 733 |
length_box = op.getarg(2) |
| 734 |
if isinstance(start_box, ConstInt): |
| 735 |
start = start_box.getint() |
| 736 |
op.setarg(1, ConstInt(start * scale)) |
| 737 |
op.setarg(3, ConstInt(1)) |
| 738 |
if isinstance(length_box, ConstInt): |
| 739 |
stop = length_box.getint() |
| 740 |
scaled_len = stop * scale |
| 741 |
op.setarg(2, ConstInt(scaled_len)) |
| 742 |
op.setarg(4, ConstInt(1)) |
| 743 |
continue |
| 744 |
assert op.getarg(1).getint() == 0 |
| 745 |
start = 0 |
| 746 |
while start in intset: |
| 747 |
start += 1 |
| 748 |
op.setarg(1, ConstInt(start * scale)) |
| 749 |
stop = op.getarg(2).getint() |
| 750 |
assert start <= stop |
| 751 |
while stop > start and (stop - 1) in intset: |
| 752 |
stop -= 1 |
| 753 |
op.setarg(2, ConstInt((stop - start) * scale)) |
| 754 |
|
| 755 |
op.setarg(3, ConstInt(1)) |
| 756 |
op.setarg(4, ConstInt(1)) |
| 757 |
del self.last_zero_arrays[:] |
| 758 |
self._setarrayitems_occurred.clear() |
| 759 |
|
| 760 |
|
| 761 |
for v, d in self._delayed_zero_setfields.iteritems(): |
| 762 |
v = self.get_box_replacement(v) |
| 763 |
for ofs in d.iterkeys(): |
| 764 |
self.emit_gc_store_or_indexed(None, v, ConstInt(ofs), ConstInt(0), |
| 765 |
WORD, 1, 0) |
| 766 |
self._delayed_zero_setfields.clear() |
| 767 |
|
| 768 |
def _gen_call_malloc_gc(self, args, v_result, descr): |
| 769 |
"""Generate a CALL_R/CHECK_MEMORY_ERROR with the given args.""" |
| 770 |
self.emitting_an_operation_that_can_collect() |
| 771 |
op = ResOperation(rop.CALL_R, args, descr=descr) |
| 772 |
self.replace_op_with(v_result, op) |
| 773 |
self.emit_op(op) |
| 774 |
self.emit_op(ResOperation(rop.CHECK_MEMORY_ERROR, [op])) |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
def gen_malloc_fixedsize(self, size, typeid, v_result): |
| 779 |
"""Generate a CALL_R(malloc_fixedsize_fn, ...). |
| 780 |
Used on Boehm, and on the framework GC for large fixed-size |
| 781 |
mallocs. (For all I know this latter case never occurs in |
| 782 |
practice, but better safe than sorry.) |
| 783 |
""" |
| 784 |
if self.gc_ll_descr.fielddescr_tid is not None: |
| 785 |
assert (size & (WORD-1)) == 0, "size not aligned?" |
| 786 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_big_fixedsize') |
| 787 |
args = [ConstInt(addr), ConstInt(size), ConstInt(typeid)] |
| 788 |
descr = self.gc_ll_descr.malloc_big_fixedsize_descr |
| 789 |
else: |
| 790 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_fixedsize') |
| 791 |
args = [ConstInt(addr), ConstInt(size)] |
| 792 |
descr = self.gc_ll_descr.malloc_fixedsize_descr |
| 793 |
self._gen_call_malloc_gc(args, v_result, descr) |
| 794 |
|
| 795 |
|
| 796 |
self.remember_write_barrier(v_result) |
| 797 |
|
| 798 |
def gen_boehm_malloc_array(self, arraydescr, v_num_elem, v_result): |
| 799 |
"""Generate a CALL_R(malloc_array_fn, ...) for Boehm.""" |
| 800 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_array') |
| 801 |
self._gen_call_malloc_gc([ConstInt(addr), |
| 802 |
ConstInt(arraydescr.basesize), |
| 803 |
v_num_elem, |
| 804 |
ConstInt(arraydescr.itemsize), |
| 805 |
ConstInt(arraydescr.lendescr.offset)], |
| 806 |
v_result, |
| 807 |
self.gc_ll_descr.malloc_array_descr) |
| 808 |
|
| 809 |
def gen_malloc_array(self, arraydescr, v_num_elem, v_result): |
| 810 |
"""Generate a CALL_R(malloc_array_fn, ...) going either |
| 811 |
to the standard or the nonstandard version of the function.""" |
| 812 |
|
| 813 |
if (arraydescr.basesize == self.gc_ll_descr.standard_array_basesize |
| 814 |
and arraydescr.lendescr.offset == |
| 815 |
self.gc_ll_descr.standard_array_length_ofs): |
| 816 |
|
| 817 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_array') |
| 818 |
args = [ConstInt(addr), |
| 819 |
ConstInt(arraydescr.itemsize), |
| 820 |
ConstInt(arraydescr.tid), |
| 821 |
v_num_elem] |
| 822 |
calldescr = self.gc_ll_descr.malloc_array_descr |
| 823 |
else: |
| 824 |
|
| 825 |
addr = self.gc_ll_descr.get_malloc_fn_addr( |
| 826 |
'malloc_array_nonstandard') |
| 827 |
args = [ConstInt(addr), |
| 828 |
ConstInt(arraydescr.basesize), |
| 829 |
ConstInt(arraydescr.itemsize), |
| 830 |
ConstInt(arraydescr.lendescr.offset), |
| 831 |
ConstInt(arraydescr.tid), |
| 832 |
v_num_elem] |
| 833 |
calldescr = self.gc_ll_descr.malloc_array_nonstandard_descr |
| 834 |
self._gen_call_malloc_gc(args, v_result, calldescr) |
| 835 |
|
| 836 |
def gen_malloc_str(self, v_num_elem, v_result): |
| 837 |
"""Generate a CALL_R(malloc_str_fn, ...).""" |
| 838 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_str') |
| 839 |
self._gen_call_malloc_gc([ConstInt(addr), v_num_elem], v_result, |
| 840 |
self.gc_ll_descr.malloc_str_descr) |
| 841 |
|
| 842 |
def gen_malloc_unicode(self, v_num_elem, v_result): |
| 843 |
"""Generate a CALL_R(malloc_unicode_fn, ...).""" |
| 844 |
addr = self.gc_ll_descr.get_malloc_fn_addr('malloc_unicode') |
| 845 |
self._gen_call_malloc_gc([ConstInt(addr), v_num_elem], v_result, |
| 846 |
self.gc_ll_descr.malloc_unicode_descr) |
| 847 |
|
| 848 |
def gen_malloc_nursery_varsize(self, itemsize, v_length, v_result, |
| 849 |
arraydescr, kind=FLAG_ARRAY): |
| 850 |
""" itemsize is an int, v_length and v_result are boxes |
| 851 |
""" |
| 852 |
gc_descr = self.gc_ll_descr |
| 853 |
if (kind == FLAG_ARRAY and |
| 854 |
(arraydescr.basesize != gc_descr.standard_array_basesize or |
| 855 |
arraydescr.lendescr.offset != gc_descr.standard_array_length_ofs)): |
| 856 |
return False |
| 857 |
self.emitting_an_operation_that_can_collect() |
| 858 |
op = ResOperation(rop.CALL_MALLOC_NURSERY_VARSIZE, |
| 859 |
[ConstInt(kind), ConstInt(itemsize), v_length], |
| 860 |
descr=arraydescr) |
| 861 |
self.replace_op_with(v_result, op) |
| 862 |
self.emit_op(op) |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
return True |
| 867 |
|
| 868 |
def gen_malloc_nursery_varsize_frame(self, sizebox): |
| 869 |
""" Generate CALL_MALLOC_NURSERY_VARSIZE_FRAME |
| 870 |
""" |
| 871 |
self.emitting_an_operation_that_can_collect() |
| 872 |
op = ResOperation(rop.CALL_MALLOC_NURSERY_VARSIZE_FRAME, |
| 873 |
[sizebox]) |
| 874 |
|
| 875 |
self.emit_op(op) |
| 876 |
self.remember_write_barrier(op) |
| 877 |
return op |
| 878 |
|
| 879 |
def gen_malloc_nursery(self, size, v_result): |
| 880 |
"""Try to generate or update a CALL_MALLOC_NURSERY. |
| 881 |
If that succeeds, return True; you still need to write the tid. |
| 882 |
If that fails, return False. |
| 883 |
""" |
| 884 |
size = self.round_up_for_allocation(size) |
| 885 |
if not self.gc_ll_descr.can_use_nursery_malloc(size): |
| 886 |
return False |
| 887 |
|
| 888 |
op = None |
| 889 |
if self._op_malloc_nursery is not None: |
| 890 |
|
| 891 |
total_size = self._op_malloc_nursery.getarg(0).getint() |
| 892 |
total_size += size |
| 893 |
if self.gc_ll_descr.can_use_nursery_malloc(total_size): |
| 894 |
|
| 895 |
self._op_malloc_nursery.setarg(0, ConstInt(total_size)) |
| 896 |
op = ResOperation(rop.NURSERY_PTR_INCREMENT, |
| 897 |
[self._v_last_malloced_nursery, |
| 898 |
ConstInt(self._previous_size)]) |
| 899 |
self.replace_op_with(v_result, op) |
| 900 |
if op is None: |
| 901 |
|
| 902 |
self.emitting_an_operation_that_can_collect() |
| 903 |
op = ResOperation(rop.CALL_MALLOC_NURSERY, |
| 904 |
[ConstInt(size)]) |
| 905 |
self.replace_op_with(v_result, op) |
| 906 |
self._op_malloc_nursery = op |
| 907 |
|
| 908 |
self.emit_op(op) |
| 909 |
self._previous_size = size |
| 910 |
self._v_last_malloced_nursery = op |
| 911 |
self.remember_write_barrier(op) |
| 912 |
return True |
| 913 |
|
| 914 |
def gen_initialize_tid(self, v_newgcobj, tid): |
| 915 |
if self.gc_ll_descr.fielddescr_tid is not None: |
| 916 |
|
| 917 |
self.emit_setfield(v_newgcobj, ConstInt(tid), |
| 918 |
descr=self.gc_ll_descr.fielddescr_tid) |
| 919 |
|
| 920 |
def gen_initialize_len(self, v_newgcobj, v_length, arraylen_descr): |
| 921 |
|
| 922 |
self.emit_setfield(v_newgcobj, v_length, descr=arraylen_descr) |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
def handle_write_barrier_setfield(self, op): |
| 927 |
val = op.getarg(0) |
| 928 |
if not self.write_barrier_applied(val): |
| 929 |
v = op.getarg(1) |
| 930 |
if (v.type == 'r' and (not isinstance(v, ConstPtr) or |
| 931 |
rgc.needs_write_barrier(v.value))): |
| 932 |
self.gen_write_barrier(val) |
| 933 |
|
| 934 |
self.emit_op(op) |
| 935 |
|
| 936 |
def handle_write_barrier_setarrayitem(self, op): |
| 937 |
val = op.getarg(0) |
| 938 |
if not self.write_barrier_applied(val): |
| 939 |
v = op.getarg(2) |
| 940 |
if (v.type == 'r' and (not isinstance(v, ConstPtr) or |
| 941 |
rgc.needs_write_barrier(v.value))): |
| 942 |
self.gen_write_barrier_array(val, op.getarg(1)) |
| 943 |
|
| 944 |
self.emit_op(op) |
| 945 |
|
| 946 |
handle_write_barrier_setinteriorfield = handle_write_barrier_setarrayitem |
| 947 |
|
| 948 |
def gen_write_barrier(self, v_base): |
| 949 |
write_barrier_descr = self.gc_ll_descr.write_barrier_descr |
| 950 |
args = [v_base] |
| 951 |
self.emit_op(ResOperation(rop.COND_CALL_GC_WB, args, |
| 952 |
descr=write_barrier_descr)) |
| 953 |
self.remember_write_barrier(v_base) |
| 954 |
|
| 955 |
def gen_write_barrier_array(self, v_base, v_index): |
| 956 |
write_barrier_descr = self.gc_ll_descr.write_barrier_descr |
| 957 |
if write_barrier_descr.has_write_barrier_from_array(self.cpu): |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
LARGE = 130 |
| 962 |
length = self.known_length(v_base, LARGE) |
| 963 |
if length >= LARGE: |
| 964 |
|
| 965 |
args = [v_base, v_index] |
| 966 |
self.emit_op( |
| 967 |
ResOperation(rop.COND_CALL_GC_WB_ARRAY, args, |
| 968 |
descr=write_barrier_descr)) |
| 969 |
|
| 970 |
|
| 971 |
return |
| 972 |
|
| 973 |
self.gen_write_barrier(v_base) |
| 974 |
|
| 975 |
def round_up_for_allocation(self, size): |
| 976 |
if not self.gc_ll_descr.round_up: |
| 977 |
return size |
| 978 |
if self.gc_ll_descr.translate_support_code: |
| 979 |
from rpython.rtyper.lltypesystem import llarena |
| 980 |
return llarena.round_up_for_allocation( |
| 981 |
size, self.gc_ll_descr.minimal_size_in_nursery) |
| 982 |
else: |
| 983 |
|
| 984 |
|
| 985 |
size = max(size, 2 * WORD) |
| 986 |
return (size + WORD-1) & ~(WORD-1) |
| 987 |
|
| 988 |
def remove_bridge_exception(self, operations): |
| 989 |
"""Check a common case: 'save_exception' immediately followed by |
| 990 |
'restore_exception' at the start of the bridge.""" |
| 991 |
|
| 992 |
|
| 993 |
start = 0 |
| 994 |
if operations[0].getopnum() == rop.INCREMENT_DEBUG_COUNTER: |
| 995 |
start = 1 |
| 996 |
if len(operations) >= start + 3: |
| 997 |
if (operations[start+0].getopnum() == rop.SAVE_EXC_CLASS and |
| 998 |
operations[start+1].getopnum() == rop.SAVE_EXCEPTION and |
| 999 |
operations[start+2].getopnum() == rop.RESTORE_EXCEPTION): |
| 1000 |
return operations[:start] + operations[start+3:] |
| 1001 |
return operations |
| 1002 |
|
| 1003 |
def emit_label(self): |
| 1004 |
self.emitting_an_operation_that_can_collect() |
| 1005 |
self._known_lengths.clear() |
| 1006 |
self.gcrefs_recently_loaded = None |
| 1007 |
|
| 1008 |
def record_int_add_or_sub(self, op, is_subtraction): |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
v_arg1 = op.getarg(1) |
| 1013 |
if isinstance(v_arg1, ConstInt): |
| 1014 |
constant = v_arg1.getint() |
| 1015 |
if is_subtraction: |
| 1016 |
constant = -constant |
| 1017 |
box = op.getarg(0) |
| 1018 |
else: |
| 1019 |
if is_subtraction: |
| 1020 |
return |
| 1021 |
v_arg0 = op.getarg(0) |
| 1022 |
if not isinstance(v_arg0, ConstInt): |
| 1023 |
return |
| 1024 |
constant = v_arg0.getint() |
| 1025 |
box = v_arg1 |
| 1026 |
|
| 1027 |
|
| 1028 |
if box in self._constant_additions: |
| 1029 |
box, extra_offset = self._constant_additions[box] |
| 1030 |
constant += extra_offset |
| 1031 |
self._constant_additions[op] = (box, constant) |
| 1032 |
|
| 1033 |
def _gcref_index(self, gcref): |
| 1034 |
if self.gcrefs_map is None: |
| 1035 |
self.gcrefs_map = new_ref_dict() |
| 1036 |
try: |
| 1037 |
return self.gcrefs_map[gcref] |
| 1038 |
except KeyError: |
| 1039 |
pass |
| 1040 |
index = len(self.gcrefs_output_list) |
| 1041 |
self.gcrefs_map[gcref] = index |
| 1042 |
self.gcrefs_output_list.append(gcref) |
| 1043 |
return index |
| 1044 |
|
| 1045 |
def rewrite_copy_str_content(self, op): |
| 1046 |
funcaddr = llmemory.cast_ptr_to_adr(self.gc_ll_descr.memcpy_fn) |
| 1047 |
memcpy_fn = self.cpu.cast_adr_to_int(funcaddr) |
| 1048 |
memcpy_descr = self.gc_ll_descr.memcpy_descr |
| 1049 |
if op.getopnum() == rop.COPYSTRCONTENT: |
| 1050 |
basesize = self.gc_ll_descr.str_descr.basesize |
| 1051 |
|
| 1052 |
|
| 1053 |
basesize -= 1 |
| 1054 |
assert self.gc_ll_descr.str_descr.itemsize == 1 |
| 1055 |
itemscale = 0 |
| 1056 |
else: |
| 1057 |
basesize = self.gc_ll_descr.unicode_descr.basesize |
| 1058 |
itemsize = self.gc_ll_descr.unicode_descr.itemsize |
| 1059 |
if itemsize == 2: |
| 1060 |
itemscale = 1 |
| 1061 |
elif itemsize == 4: |
| 1062 |
itemscale = 2 |
| 1063 |
else: |
| 1064 |
assert False, "unknown size of unicode" |
| 1065 |
i1 = self.emit_load_effective_address(op.getarg(0), op.getarg(2), |
| 1066 |
basesize, itemscale) |
| 1067 |
i2 = self.emit_load_effective_address(op.getarg(1), op.getarg(3), |
| 1068 |
basesize, itemscale) |
| 1069 |
if op.getopnum() == rop.COPYSTRCONTENT: |
| 1070 |
arg = op.getarg(4) |
| 1071 |
else: |
| 1072 |
|
| 1073 |
if isinstance(op.getarg(4), ConstInt): |
| 1074 |
arg = ConstInt(op.getarg(4).getint() << itemscale) |
| 1075 |
else: |
| 1076 |
arg = ResOperation(rop.INT_LSHIFT, |
| 1077 |
[op.getarg(4), ConstInt(itemscale)]) |
| 1078 |
self.emit_op(arg) |
| 1079 |
self.emit_op(ResOperation(rop.CALL_N, |
| 1080 |
[ConstInt(memcpy_fn), i2, i1, arg], descr=memcpy_descr)) |
| 1081 |
|
| 1082 |
def emit_load_effective_address(self, v_gcptr, v_index, base, itemscale): |
| 1083 |
if self.cpu.supports_load_effective_address: |
| 1084 |
i1 = ResOperation(rop.LOAD_EFFECTIVE_ADDRESS, |
| 1085 |
[v_gcptr, v_index, ConstInt(base), |
| 1086 |
ConstInt(itemscale)]) |
| 1087 |
self.emit_op(i1) |
| 1088 |
return i1 |
| 1089 |
else: |
| 1090 |
if itemscale > 0: |
| 1091 |
v_index = ResOperation(rop.INT_LSHIFT, |
| 1092 |
[v_index, ConstInt(itemscale)]) |
| 1093 |
self.emit_op(v_index) |
| 1094 |
i1b = ResOperation(rop.INT_ADD, [v_gcptr, v_index]) |
| 1095 |
self.emit_op(i1b) |
| 1096 |
i1 = ResOperation(rop.INT_ADD, [i1b, ConstInt(base)]) |
| 1097 |
self.emit_op(i1) |
| 1098 |
return i1 |
| 1099 |
|
| 1100 |
def remove_constptr(self, c): |
| 1101 |
"""Remove all ConstPtrs, and replace them with load_from_gc_table. |
| 1102 |
""" |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
index = self._gcref_index(c.value) |
| 1107 |
if self.gcrefs_recently_loaded is None: |
| 1108 |
self.gcrefs_recently_loaded = {} |
| 1109 |
try: |
| 1110 |
load_op = self.gcrefs_recently_loaded[index] |
| 1111 |
except KeyError: |
| 1112 |
load_op = ResOperation(rop.LOAD_FROM_GC_TABLE, [ConstInt(index)]) |
| 1113 |
self._newops.append(load_op) |
| 1114 |
self.gcrefs_recently_loaded[index] = load_op |
| 1115 |
return load_op |
| 1116 |
|
| 1117 |
@always_inline |
| 1118 |
def cpu_simplify_scale(cpu, index_box, factor, offset): |
| 1119 |
|
| 1120 |
|
| 1121 |
if isinstance(index_box, ConstInt): |
| 1122 |
return 1, index_box.value * factor + offset, None, False |
| 1123 |
else: |
| 1124 |
if factor != 1 and factor not in cpu.load_supported_factors: |
| 1125 |
|
| 1126 |
|
| 1127 |
assert factor > 0 |
| 1128 |
if (factor & (factor - 1)) == 0: |
| 1129 |
index_box = ResOperation(rop.INT_LSHIFT, |
| 1130 |
[index_box, ConstInt(highest_bit(factor))]) |
| 1131 |
else: |
| 1132 |
index_box = ResOperation(rop.INT_MUL, |
| 1133 |
[index_box, ConstInt(factor)]) |
| 1134 |
return 1, offset, index_box, True |
| 1135 |
return factor, offset, index_box, False |
| 1136 |
|