| 1 |
|
| 2 |
from rpython.rlib.objectmodel import specialize, compute_hash |
| 3 |
from rpython.jit.metainterp.resoperation import ( |
| 4 |
AbstractValue, ResOperation, rop, OpHelpers) |
| 5 |
from rpython.jit.metainterp.history import ConstInt, ConstPtr, Const |
| 6 |
from rpython.rtyper.lltypesystem import lltype |
| 7 |
from rpython.jit.metainterp.optimizeopt.rawbuffer import ( |
| 8 |
RawBuffer, InvalidRawOperation) |
| 9 |
from rpython.jit.metainterp.executor import execute |
| 10 |
from rpython.jit.metainterp.optimize import InvalidLoop |
| 11 |
from .util import get_box_replacement |
| 12 |
|
| 13 |
INFO_NULL = 0 |
| 14 |
INFO_NONNULL = 1 |
| 15 |
INFO_UNKNOWN = 2 |
| 16 |
|
| 17 |
class AbstractInfo(AbstractValue): |
| 18 |
_attrs_ = () |
| 19 |
|
| 20 |
is_info_class = True |
| 21 |
|
| 22 |
def force_box(self, op, optforce): |
| 23 |
return op |
| 24 |
|
| 25 |
def is_virtual(self): |
| 26 |
return False |
| 27 |
|
| 28 |
def is_precise(self): |
| 29 |
return False |
| 30 |
|
| 31 |
def getconst(self): |
| 32 |
raise Exception("not a constant") |
| 33 |
|
| 34 |
def _is_immutable_and_filled_with_constants(self, optimizer, memo=None): |
| 35 |
return False |
| 36 |
|
| 37 |
|
| 38 |
class PtrInfo(AbstractInfo): |
| 39 |
_attrs_ = () |
| 40 |
|
| 41 |
def is_nonnull(self): |
| 42 |
return False |
| 43 |
|
| 44 |
def is_about_object(self): |
| 45 |
return False |
| 46 |
|
| 47 |
def get_descr(self): |
| 48 |
return None |
| 49 |
|
| 50 |
def is_null(self): |
| 51 |
return False |
| 52 |
|
| 53 |
def force_at_the_end_of_preamble(self, op, optforce, rec): |
| 54 |
if not self.is_virtual(): |
| 55 |
return get_box_replacement(op) |
| 56 |
return self._force_at_the_end_of_preamble(op, optforce, rec) |
| 57 |
|
| 58 |
def get_known_class(self, cpu): |
| 59 |
return None |
| 60 |
|
| 61 |
def getlenbound(self, mode): |
| 62 |
return None |
| 63 |
|
| 64 |
def getnullness(self): |
| 65 |
if self.is_null(): |
| 66 |
return INFO_NULL |
| 67 |
elif self.is_nonnull(): |
| 68 |
return INFO_NONNULL |
| 69 |
return INFO_UNKNOWN |
| 70 |
|
| 71 |
def same_info(self, other): |
| 72 |
return self is other |
| 73 |
|
| 74 |
def getstrlen(self, op, string_optimizer, mode): |
| 75 |
return None |
| 76 |
|
| 77 |
def getstrhash(self, op, mode): |
| 78 |
return None |
| 79 |
|
| 80 |
def copy_fields_to_const(self, constinfo, optheap): |
| 81 |
pass |
| 82 |
|
| 83 |
def make_guards(self, op, short, optimizer): |
| 84 |
pass |
| 85 |
|
| 86 |
@specialize.arg(2) |
| 87 |
def get_constant_string_spec(self, string_optimizer, mode): |
| 88 |
return None |
| 89 |
|
| 90 |
class NonNullPtrInfo(PtrInfo): |
| 91 |
_attrs_ = ('last_guard_pos',) |
| 92 |
last_guard_pos = -1 |
| 93 |
|
| 94 |
def is_nonnull(self): |
| 95 |
return True |
| 96 |
|
| 97 |
def get_known_class(self, cpu): |
| 98 |
return None |
| 99 |
|
| 100 |
def get_last_guard(self, optimizer): |
| 101 |
if self.last_guard_pos == -1: |
| 102 |
return None |
| 103 |
return optimizer._newoperations[self.last_guard_pos] |
| 104 |
|
| 105 |
def get_last_guard_pos(self): |
| 106 |
return self.last_guard_pos |
| 107 |
|
| 108 |
def reset_last_guard_pos(self): |
| 109 |
self.last_guard_pos = -1 |
| 110 |
|
| 111 |
def mark_last_guard(self, optimizer): |
| 112 |
if (optimizer.getlastop() is None or |
| 113 |
not optimizer.getlastop().is_guard()): |
| 114 |
|
| 115 |
|
| 116 |
return |
| 117 |
self.last_guard_pos = len(optimizer._newoperations) - 1 |
| 118 |
assert self.get_last_guard(optimizer).is_guard() |
| 119 |
|
| 120 |
def make_guards(self, op, short, optimizer): |
| 121 |
op = ResOperation(rop.GUARD_NONNULL, [op]) |
| 122 |
short.append(op) |
| 123 |
|
| 124 |
class AbstractVirtualPtrInfo(NonNullPtrInfo): |
| 125 |
_attrs_ = ('_cached_vinfo', 'descr', '_is_virtual') |
| 126 |
|
| 127 |
|
| 128 |
_cached_vinfo = None |
| 129 |
descr = None |
| 130 |
|
| 131 |
def get_descr(self): |
| 132 |
return self.descr |
| 133 |
|
| 134 |
def is_precise(self): |
| 135 |
return True |
| 136 |
|
| 137 |
def force_box(self, op, optforce): |
| 138 |
if self.is_virtual(): |
| 139 |
|
| 140 |
if self._is_immutable_and_filled_with_constants(optforce.optimizer): |
| 141 |
constptr = optforce.optimizer.constant_fold(op) |
| 142 |
op.set_forwarded(constptr) |
| 143 |
self._is_virtual = False |
| 144 |
self._force_elements_immutable(self.descr, constptr, optforce.optimizer) |
| 145 |
return constptr |
| 146 |
|
| 147 |
op.set_forwarded(None) |
| 148 |
optforce.emit_extra(op) |
| 149 |
newop = optforce.optimizer.getlastop() |
| 150 |
if newop is not op: |
| 151 |
op.set_forwarded(newop) |
| 152 |
newop.set_forwarded(self) |
| 153 |
descr = self.descr |
| 154 |
self._is_virtual = False |
| 155 |
self._force_elements(newop, optforce, descr) |
| 156 |
return newop |
| 157 |
return op |
| 158 |
|
| 159 |
def _force_at_the_end_of_preamble(self, op, optforce, rec): |
| 160 |
return self.force_box(op, optforce) |
| 161 |
|
| 162 |
def is_virtual(self): |
| 163 |
return self._is_virtual |
| 164 |
|
| 165 |
def _visitor_walk_recursive(self, op, visitor): |
| 166 |
raise NotImplementedError("abstract") |
| 167 |
|
| 168 |
def visitor_walk_recursive(self, instbox, visitor): |
| 169 |
instbox = instbox.get_box_replacement() |
| 170 |
if visitor.already_seen_virtual(instbox): |
| 171 |
return |
| 172 |
return self._visitor_walk_recursive(instbox, visitor) |
| 173 |
|
| 174 |
|
| 175 |
class AbstractStructPtrInfo(AbstractVirtualPtrInfo): |
| 176 |
_attrs_ = ('_fields',) |
| 177 |
|
| 178 |
_fields = None |
| 179 |
|
| 180 |
def init_fields(self, descr, index): |
| 181 |
if self._fields is None: |
| 182 |
self.descr = descr |
| 183 |
self._fields = [None] * len(descr.get_all_fielddescrs()) |
| 184 |
if index >= len(self._fields): |
| 185 |
self.descr = descr |
| 186 |
|
| 187 |
extra_len = len(descr.get_all_fielddescrs()) - len(self._fields) |
| 188 |
self._fields = self._fields + [None] * extra_len |
| 189 |
|
| 190 |
def clear_cache(self): |
| 191 |
assert not self.is_virtual() |
| 192 |
self._fields = [None] * len(self._fields) |
| 193 |
|
| 194 |
def copy_fields_to_const(self, constinfo, optheap): |
| 195 |
if self._fields is not None: |
| 196 |
info = constinfo._get_info(self.descr, optheap) |
| 197 |
assert isinstance(info, AbstractStructPtrInfo) |
| 198 |
info._fields = self._fields[:] |
| 199 |
|
| 200 |
def all_items(self): |
| 201 |
return self._fields |
| 202 |
|
| 203 |
def setfield(self, fielddescr, struct, op, optheap=None, cf=None): |
| 204 |
self.init_fields(fielddescr.get_parent_descr(), fielddescr.get_index()) |
| 205 |
assert isinstance(op, AbstractValue) |
| 206 |
self._fields[fielddescr.get_index()] = op |
| 207 |
if cf is not None: |
| 208 |
assert not self.is_virtual() |
| 209 |
assert struct is not None |
| 210 |
cf.register_info(struct, self) |
| 211 |
|
| 212 |
def getfield(self, fielddescr, optheap=None): |
| 213 |
self.init_fields(fielddescr.get_parent_descr(), fielddescr.get_index()) |
| 214 |
return self._fields[fielddescr.get_index()] |
| 215 |
|
| 216 |
def _force_elements(self, op, optforce, descr): |
| 217 |
if self._fields is None: |
| 218 |
return |
| 219 |
for i, fielddescr in enumerate(descr.get_all_fielddescrs()): |
| 220 |
fld = self._fields[i] |
| 221 |
if fld is not None: |
| 222 |
subbox = optforce.optimizer.force_box(fld) |
| 223 |
setfieldop = ResOperation(rop.SETFIELD_GC, [op, subbox], |
| 224 |
descr=fielddescr) |
| 225 |
self._fields[i] = None |
| 226 |
optforce.emit_extra(setfieldop) |
| 227 |
|
| 228 |
def _force_at_the_end_of_preamble(self, op, optforce, rec): |
| 229 |
if self._fields is None: |
| 230 |
return get_box_replacement(op) |
| 231 |
if self in rec: |
| 232 |
return get_box_replacement(op) |
| 233 |
rec[self] = None |
| 234 |
for i, fldbox in enumerate(self._fields): |
| 235 |
if fldbox is not None: |
| 236 |
info = getptrinfo(fldbox) |
| 237 |
if info is not None: |
| 238 |
fldbox = info.force_at_the_end_of_preamble(fldbox, optforce, |
| 239 |
rec) |
| 240 |
self._fields[i] = fldbox |
| 241 |
return op |
| 242 |
|
| 243 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 244 |
lst = self.descr.get_all_fielddescrs() |
| 245 |
assert self.is_virtual() |
| 246 |
visitor.register_virtual_fields( |
| 247 |
instbox, [get_box_replacement(box) for box in self._fields]) |
| 248 |
for i in range(len(lst)): |
| 249 |
op = self._fields[i] |
| 250 |
if op: |
| 251 |
fieldinfo = getptrinfo(op) |
| 252 |
if fieldinfo and fieldinfo.is_virtual(): |
| 253 |
fieldinfo.visitor_walk_recursive(op, visitor) |
| 254 |
|
| 255 |
def produce_short_preamble_ops(self, structbox, fielddescr, index, optimizer, |
| 256 |
shortboxes): |
| 257 |
if self._fields is None: |
| 258 |
return |
| 259 |
if fielddescr.get_index() >= len(self._fields): |
| 260 |
|
| 261 |
return |
| 262 |
op = get_box_replacement(self._fields[fielddescr.get_index()]) |
| 263 |
if op is None: |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
return |
| 269 |
opnum = OpHelpers.getfield_for_descr(fielddescr) |
| 270 |
getfield_op = ResOperation(opnum, [structbox], descr=fielddescr) |
| 271 |
shortboxes.add_heap_op(op, getfield_op) |
| 272 |
|
| 273 |
def _is_immutable_and_filled_with_constants(self, optimizer, memo=None): |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
assert self.is_virtual() |
| 281 |
if not self.descr.is_immutable(): |
| 282 |
return False |
| 283 |
if memo is not None and self in memo: |
| 284 |
return True |
| 285 |
|
| 286 |
for op in self._fields: |
| 287 |
if op is None: |
| 288 |
return False |
| 289 |
op = op.get_box_replacement() |
| 290 |
if op.is_constant(): |
| 291 |
pass |
| 292 |
else: |
| 293 |
fieldinfo = getptrinfo(op) |
| 294 |
if fieldinfo and fieldinfo.is_virtual(): |
| 295 |
|
| 296 |
if memo is None: |
| 297 |
memo = {self: None} |
| 298 |
if not fieldinfo._is_immutable_and_filled_with_constants( |
| 299 |
optimizer, memo): |
| 300 |
return False |
| 301 |
else: |
| 302 |
return False |
| 303 |
return True |
| 304 |
|
| 305 |
def _force_elements_immutable(self, descr, constptr, optimizer): |
| 306 |
for i, fielddescr in enumerate(descr.get_all_fielddescrs()): |
| 307 |
fld = self._fields[i] |
| 308 |
subbox = optimizer.force_box(fld) |
| 309 |
assert isinstance(subbox, Const) |
| 310 |
execute(optimizer.cpu, None, rop.SETFIELD_GC, |
| 311 |
fielddescr, constptr, subbox) |
| 312 |
|
| 313 |
class InstancePtrInfo(AbstractStructPtrInfo): |
| 314 |
_attrs_ = ('_known_class',) |
| 315 |
_fields = None |
| 316 |
|
| 317 |
def __init__(self, descr=None, known_class=None, is_virtual=False): |
| 318 |
self._known_class = known_class |
| 319 |
|
| 320 |
|
| 321 |
self.descr = descr |
| 322 |
self._is_virtual = is_virtual |
| 323 |
|
| 324 |
def get_known_class(self, cpu): |
| 325 |
return self._known_class |
| 326 |
|
| 327 |
def is_about_object(self): |
| 328 |
return True |
| 329 |
|
| 330 |
@specialize.argtype(1) |
| 331 |
def visitor_dispatch_virtual_type(self, visitor): |
| 332 |
fielddescrs = self.descr.get_all_fielddescrs() |
| 333 |
assert self.is_virtual() |
| 334 |
return visitor.visit_virtual(self.descr, fielddescrs) |
| 335 |
|
| 336 |
def make_guards(self, op, short, optimizer): |
| 337 |
if self._known_class is not None: |
| 338 |
if not optimizer.cpu.remove_gctypeptr: |
| 339 |
short.append(ResOperation(rop.GUARD_NONNULL, [op])) |
| 340 |
short.append(ResOperation(rop.GUARD_IS_OBJECT, [op])) |
| 341 |
short.append(ResOperation(rop.GUARD_CLASS, |
| 342 |
[op, self._known_class])) |
| 343 |
else: |
| 344 |
short.append(ResOperation(rop.GUARD_NONNULL_CLASS, |
| 345 |
[op, self._known_class])) |
| 346 |
elif self.descr is not None: |
| 347 |
short.append(ResOperation(rop.GUARD_NONNULL, [op])) |
| 348 |
if not optimizer.cpu.remove_gctypeptr: |
| 349 |
short.append(ResOperation(rop.GUARD_IS_OBJECT, [op])) |
| 350 |
short.append(ResOperation(rop.GUARD_SUBCLASS, [op, |
| 351 |
ConstInt(self.descr.get_vtable())])) |
| 352 |
else: |
| 353 |
AbstractStructPtrInfo.make_guards(self, op, short, optimizer) |
| 354 |
|
| 355 |
class StructPtrInfo(AbstractStructPtrInfo): |
| 356 |
def __init__(self, descr, is_virtual=False): |
| 357 |
self.descr = descr |
| 358 |
self._is_virtual = is_virtual |
| 359 |
|
| 360 |
def make_guards(self, op, short, optimizer): |
| 361 |
if self.descr is not None: |
| 362 |
c_typeid = ConstInt(self.descr.get_type_id()) |
| 363 |
short.extend([ |
| 364 |
ResOperation(rop.GUARD_NONNULL, [op]), |
| 365 |
ResOperation(rop.GUARD_GC_TYPE, [op, c_typeid]) |
| 366 |
]) |
| 367 |
|
| 368 |
@specialize.argtype(1) |
| 369 |
def visitor_dispatch_virtual_type(self, visitor): |
| 370 |
fielddescrs = self.descr.get_all_fielddescrs() |
| 371 |
assert self.is_virtual() |
| 372 |
return visitor.visit_vstruct(self.descr, fielddescrs) |
| 373 |
|
| 374 |
class AbstractRawPtrInfo(AbstractVirtualPtrInfo): |
| 375 |
@specialize.argtype(1) |
| 376 |
def visitor_dispatch_virtual_type(self, visitor): |
| 377 |
raise NotImplementedError("abstract") |
| 378 |
|
| 379 |
def make_guards(self, op, short, optimizer): |
| 380 |
from rpython.jit.metainterp.optimizeopt.optimizer import CONST_0 |
| 381 |
op = ResOperation(rop.INT_EQ, [op, CONST_0]) |
| 382 |
short.append(op) |
| 383 |
op = ResOperation(rop.GUARD_FALSE, [op]) |
| 384 |
short.append(op) |
| 385 |
|
| 386 |
class RawBufferPtrInfo(AbstractRawPtrInfo): |
| 387 |
buffer = None |
| 388 |
|
| 389 |
def __init__(self, cpu, func, size=-1): |
| 390 |
self.func = func |
| 391 |
self.size = size |
| 392 |
if self.size != -1: |
| 393 |
self.buffer = RawBuffer(cpu, None) |
| 394 |
|
| 395 |
def _get_buffer(self): |
| 396 |
buffer = self.buffer |
| 397 |
assert buffer is not None |
| 398 |
return buffer |
| 399 |
|
| 400 |
def all_items(self): |
| 401 |
return [] |
| 402 |
|
| 403 |
def getitem_raw(self, offset, itemsize, descr): |
| 404 |
if not self.is_virtual(): |
| 405 |
raise InvalidRawOperation |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
return self._get_buffer().read_value(offset, itemsize, descr) |
| 411 |
|
| 412 |
def setitem_raw(self, offset, itemsize, descr, itemop): |
| 413 |
if not self.is_virtual(): |
| 414 |
raise InvalidRawOperation |
| 415 |
self._get_buffer().write_value(offset, itemsize, descr, itemop) |
| 416 |
|
| 417 |
def is_virtual(self): |
| 418 |
return self.size != -1 |
| 419 |
|
| 420 |
def _force_elements(self, op, optforce, descr): |
| 421 |
self.size = -1 |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
check_op = ResOperation(rop.CHECK_MEMORY_ERROR, [op]) |
| 426 |
optforce.emit_extra(check_op) |
| 427 |
|
| 428 |
buffer = self._get_buffer() |
| 429 |
for i in range(len(buffer.offsets)): |
| 430 |
|
| 431 |
offset = buffer.offsets[i] |
| 432 |
descr = buffer.descrs[i] |
| 433 |
itembox = buffer.values[i] |
| 434 |
setfield_op = ResOperation(rop.RAW_STORE, |
| 435 |
[op, ConstInt(offset), itembox], descr=descr) |
| 436 |
optforce.emit_extra(setfield_op) |
| 437 |
|
| 438 |
def _visitor_walk_recursive(self, op, visitor): |
| 439 |
itemboxes = [get_box_replacement(box) |
| 440 |
for box in self._get_buffer().values] |
| 441 |
visitor.register_virtual_fields(op, itemboxes) |
| 442 |
|
| 443 |
|
| 444 |
@specialize.argtype(1) |
| 445 |
def visitor_dispatch_virtual_type(self, visitor): |
| 446 |
buffer = self._get_buffer() |
| 447 |
return visitor.visit_vrawbuffer(self.func, |
| 448 |
self.size, |
| 449 |
buffer.offsets[:], |
| 450 |
buffer.descrs[:]) |
| 451 |
|
| 452 |
class RawStructPtrInfo(AbstractRawPtrInfo): |
| 453 |
def __init__(self): |
| 454 |
pass |
| 455 |
|
| 456 |
def is_virtual(self): |
| 457 |
return False |
| 458 |
|
| 459 |
class RawSlicePtrInfo(AbstractRawPtrInfo): |
| 460 |
def __init__(self, offset, parent): |
| 461 |
self.offset = offset |
| 462 |
self.parent = parent |
| 463 |
|
| 464 |
def is_virtual(self): |
| 465 |
return self.parent is not None |
| 466 |
|
| 467 |
def getitem_raw(self, offset, itemsize, descr): |
| 468 |
return self.parent.getitem_raw(self.offset + offset, itemsize, descr) |
| 469 |
|
| 470 |
def setitem_raw(self, offset, itemsize, descr, itemop): |
| 471 |
self.parent.setitem_raw(self.offset + offset, itemsize, descr, itemop) |
| 472 |
|
| 473 |
def _force_elements(self, op, optforce, descr): |
| 474 |
if self.parent.is_virtual(): |
| 475 |
self.parent._force_elements(op, optforce, descr) |
| 476 |
self.parent = None |
| 477 |
|
| 478 |
def _visitor_walk_recursive(self, op, visitor): |
| 479 |
source_op = get_box_replacement(op.getarg(0)) |
| 480 |
visitor.register_virtual_fields(op, [source_op]) |
| 481 |
if self.parent.is_virtual(): |
| 482 |
self.parent.visitor_walk_recursive(source_op, visitor) |
| 483 |
|
| 484 |
@specialize.argtype(1) |
| 485 |
def visitor_dispatch_virtual_type(self, visitor): |
| 486 |
return visitor.visit_vrawslice(self.offset) |
| 487 |
|
| 488 |
|
| 489 |
def reasonable_array_index(index): |
| 490 |
"""Check a given constant array index or array size for sanity. |
| 491 |
In case of invalid loops or very large arrays, we shouldn't try |
| 492 |
to optimize them.""" |
| 493 |
return index >= 0 and index <= 150000 |
| 494 |
|
| 495 |
|
| 496 |
class ArrayPtrInfo(AbstractVirtualPtrInfo): |
| 497 |
_attrs_ = ('length', '_items', 'lenbound', '_clear', 'descr', |
| 498 |
'_is_virtual') |
| 499 |
|
| 500 |
_items = None |
| 501 |
lenbound = None |
| 502 |
length = -1 |
| 503 |
|
| 504 |
def __init__(self, descr, const=None, size=0, clear=False, |
| 505 |
is_virtual=False): |
| 506 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 507 |
assert descr is not None |
| 508 |
self.descr = descr |
| 509 |
self._is_virtual = is_virtual |
| 510 |
if is_virtual: |
| 511 |
self._init_items(const, size, clear) |
| 512 |
self.lenbound = IntBound.from_constant(size) |
| 513 |
self._clear = clear |
| 514 |
|
| 515 |
def getlenbound(self, mode): |
| 516 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 517 |
assert mode is None |
| 518 |
if self.lenbound is None: |
| 519 |
assert self.length == -1 |
| 520 |
self.lenbound = IntBound.nonnegative() |
| 521 |
return self.lenbound |
| 522 |
|
| 523 |
def _init_items(self, const, size, clear): |
| 524 |
self.length = size |
| 525 |
if clear: |
| 526 |
self._items = [const] * size |
| 527 |
else: |
| 528 |
self._items = [None] * size |
| 529 |
|
| 530 |
def all_items(self): |
| 531 |
return self._items |
| 532 |
|
| 533 |
def copy_fields_to_const(self, constinfo, optheap): |
| 534 |
descr = self.descr |
| 535 |
if self._items is not None: |
| 536 |
info = constinfo._get_array_info(descr, optheap) |
| 537 |
assert isinstance(info, ArrayPtrInfo) |
| 538 |
info._items = self._items[:] |
| 539 |
|
| 540 |
def _force_elements(self, op, optforce, descr): |
| 541 |
|
| 542 |
descr = op.getdescr() |
| 543 |
const = optforce.optimizer.new_const_item(self.descr) |
| 544 |
for i in range(self.length): |
| 545 |
item = self._items[i] |
| 546 |
if item is None: |
| 547 |
continue |
| 548 |
if self._clear and const.same_constant(item): |
| 549 |
|
| 550 |
self._items[i] = None |
| 551 |
continue |
| 552 |
subbox = optforce.optimizer.force_box(item) |
| 553 |
setop = ResOperation(rop.SETARRAYITEM_GC, |
| 554 |
[op, ConstInt(i), subbox], |
| 555 |
descr=descr) |
| 556 |
self._items[i] = None |
| 557 |
optforce.emit_extra(setop) |
| 558 |
optforce.pure_from_args(rop.ARRAYLEN_GC, [op], ConstInt(len(self._items))) |
| 559 |
|
| 560 |
def _setitem_index(self, index, op): |
| 561 |
if not reasonable_array_index(index): |
| 562 |
if self._items is None: |
| 563 |
self._items = [] |
| 564 |
return |
| 565 |
if self._items is None: |
| 566 |
self._items = [None] * (index + 1) |
| 567 |
elif index >= len(self._items): |
| 568 |
if self.is_virtual(): |
| 569 |
return |
| 570 |
self._items = self._items + [None] * (index - len(self._items) + 1) |
| 571 |
self._items[index] = op |
| 572 |
|
| 573 |
def setitem(self, descr, index, struct, op, optheap=None, cf=None): |
| 574 |
self._setitem_index(index, op) |
| 575 |
if cf is not None: |
| 576 |
assert not self.is_virtual() |
| 577 |
cf.register_info(struct, self) |
| 578 |
|
| 579 |
def getitem(self, descr, index, optheap=None): |
| 580 |
if self._items is None or index >= len(self._items) or index < 0: |
| 581 |
return None |
| 582 |
return self._items[index] |
| 583 |
|
| 584 |
def getlength(self): |
| 585 |
return self.length |
| 586 |
|
| 587 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 588 |
itemops = [get_box_replacement(item) for item in self._items] |
| 589 |
visitor.register_virtual_fields(instbox, itemops) |
| 590 |
for i in range(self.getlength()): |
| 591 |
itemop = self._items[i] |
| 592 |
if (itemop is not None and not isinstance(itemop, Const)): |
| 593 |
ptrinfo = getptrinfo(itemop) |
| 594 |
if ptrinfo and ptrinfo.is_virtual(): |
| 595 |
ptrinfo.visitor_walk_recursive(itemop, visitor) |
| 596 |
|
| 597 |
@specialize.argtype(1) |
| 598 |
def visitor_dispatch_virtual_type(self, visitor): |
| 599 |
return visitor.visit_varray(self.descr, self._clear) |
| 600 |
|
| 601 |
def produce_short_preamble_ops(self, structbox, descr, index, optimizer, |
| 602 |
shortboxes): |
| 603 |
if self._items is None: |
| 604 |
return |
| 605 |
if index >= len(self._items) or index < 0: |
| 606 |
|
| 607 |
return |
| 608 |
item = self._items[index] |
| 609 |
if item is not None: |
| 610 |
|
| 611 |
op = get_box_replacement(item) |
| 612 |
opnum = OpHelpers.getarrayitem_for_descr(descr) |
| 613 |
getarrayitem_op = ResOperation(opnum, [structbox, ConstInt(index)], |
| 614 |
descr=descr) |
| 615 |
shortboxes.add_heap_op(op, getarrayitem_op) |
| 616 |
|
| 617 |
def _force_at_the_end_of_preamble(self, op, optforce, rec): |
| 618 |
if self._items is None: |
| 619 |
return get_box_replacement(op) |
| 620 |
if self in rec: |
| 621 |
return get_box_replacement(op) |
| 622 |
rec[self] = None |
| 623 |
for i, fldbox in enumerate(self._items): |
| 624 |
if fldbox is not None: |
| 625 |
info = getptrinfo(fldbox) |
| 626 |
if info is not None: |
| 627 |
fldbox = info.force_at_the_end_of_preamble(fldbox, optforce, |
| 628 |
rec) |
| 629 |
self._items[i] = fldbox |
| 630 |
return op |
| 631 |
|
| 632 |
def make_guards(self, op, short, optimizer): |
| 633 |
AbstractVirtualPtrInfo.make_guards(self, op, short, optimizer) |
| 634 |
c_type_id = ConstInt(self.descr.get_type_id()) |
| 635 |
short.append(ResOperation(rop.GUARD_GC_TYPE, [op, c_type_id])) |
| 636 |
if self.lenbound is not None: |
| 637 |
lenop = ResOperation(rop.ARRAYLEN_GC, [op], descr=self.descr) |
| 638 |
short.append(lenop) |
| 639 |
self.lenbound.make_guards(lenop, short, optimizer) |
| 640 |
|
| 641 |
class ArrayStructInfo(ArrayPtrInfo): |
| 642 |
def __init__(self, descr, size, is_virtual=False): |
| 643 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 644 |
self.length = size |
| 645 |
lgt = len(descr.get_all_fielddescrs()) |
| 646 |
self.lenbound = IntBound.from_constant(size) |
| 647 |
self.descr = descr |
| 648 |
self._items = [None] * (size * lgt) |
| 649 |
self._is_virtual = is_virtual |
| 650 |
|
| 651 |
def _compute_index(self, index, fielddescr): |
| 652 |
all_fdescrs = fielddescr.get_arraydescr().get_all_fielddescrs() |
| 653 |
if all_fdescrs is None or index < 0 or index >= self.length: |
| 654 |
return -1 |
| 655 |
one_size = len(all_fdescrs) |
| 656 |
return index * one_size + fielddescr.get_field_descr().get_index() |
| 657 |
|
| 658 |
def setinteriorfield_virtual(self, index, fielddescr, fld): |
| 659 |
index = self._compute_index(index, fielddescr) |
| 660 |
if index >= 0: |
| 661 |
self._items[index] = fld |
| 662 |
|
| 663 |
def getinteriorfield_virtual(self, index, fielddescr): |
| 664 |
index = self._compute_index(index, fielddescr) |
| 665 |
if index >= 0: |
| 666 |
return self._items[index] |
| 667 |
else: |
| 668 |
return None |
| 669 |
|
| 670 |
def _force_elements(self, op, optforce, descr): |
| 671 |
i = 0 |
| 672 |
fielddescrs = op.getdescr().get_all_fielddescrs() |
| 673 |
for index in range(self.length): |
| 674 |
for fielddescr in fielddescrs: |
| 675 |
fld = self._items[i] |
| 676 |
if fld is not None: |
| 677 |
subbox = optforce.optimizer.force_box(fld) |
| 678 |
setfieldop = ResOperation(rop.SETINTERIORFIELD_GC, |
| 679 |
[op, ConstInt(index), subbox], |
| 680 |
descr=fielddescr) |
| 681 |
optforce.emit_extra(setfieldop) |
| 682 |
|
| 683 |
|
| 684 |
i += 1 |
| 685 |
|
| 686 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 687 |
itemops = [get_box_replacement(item) for item in self._items] |
| 688 |
visitor.register_virtual_fields(instbox, itemops) |
| 689 |
fielddescrs = self.descr.get_all_fielddescrs() |
| 690 |
i = 0 |
| 691 |
for index in range(self.getlength()): |
| 692 |
for fielddescr in fielddescrs: |
| 693 |
itemop = self._items[i] |
| 694 |
if (itemop is not None and not isinstance(itemop, Const)): |
| 695 |
ptrinfo = getptrinfo(itemop) |
| 696 |
if ptrinfo and ptrinfo.is_virtual(): |
| 697 |
ptrinfo.visitor_walk_recursive(itemop, visitor) |
| 698 |
i += 1 |
| 699 |
|
| 700 |
@specialize.argtype(1) |
| 701 |
def visitor_dispatch_virtual_type(self, visitor): |
| 702 |
flddescrs = self.descr.get_all_fielddescrs() |
| 703 |
return visitor.visit_varraystruct(self.descr, self.getlength(), |
| 704 |
flddescrs) |
| 705 |
|
| 706 |
class ConstPtrInfo(PtrInfo): |
| 707 |
_attrs_ = ('_const',) |
| 708 |
|
| 709 |
def __init__(self, const): |
| 710 |
self._const = const |
| 711 |
|
| 712 |
def getconst(self): |
| 713 |
return self._const |
| 714 |
|
| 715 |
def make_guards(self, op, short, optimizer): |
| 716 |
short.append(ResOperation(rop.GUARD_VALUE, [op, self._const])) |
| 717 |
|
| 718 |
def _get_info(self, descr, optheap): |
| 719 |
ref = self._const.getref_base() |
| 720 |
if not ref: |
| 721 |
raise InvalidLoop |
| 722 |
info = optheap.const_infos.get(ref, None) |
| 723 |
if info is None: |
| 724 |
info = StructPtrInfo(descr) |
| 725 |
optheap.const_infos[ref] = info |
| 726 |
return info |
| 727 |
|
| 728 |
def _get_array_info(self, descr, optheap): |
| 729 |
ref = self._const.getref_base() |
| 730 |
if not ref: |
| 731 |
raise InvalidLoop |
| 732 |
info = optheap.const_infos.get(ref, None) |
| 733 |
if info is None: |
| 734 |
info = ArrayPtrInfo(descr) |
| 735 |
optheap.const_infos[ref] = info |
| 736 |
return info |
| 737 |
|
| 738 |
def getfield(self, fielddescr, optheap=None): |
| 739 |
info = self._get_info(fielddescr.get_parent_descr(), optheap) |
| 740 |
return info.getfield(fielddescr) |
| 741 |
|
| 742 |
def getitem(self, descr, index, optheap=None): |
| 743 |
info = self._get_array_info(descr, optheap) |
| 744 |
return info.getitem(descr, index) |
| 745 |
|
| 746 |
def setitem(self, descr, index, struct, op, optheap=None, cf=None): |
| 747 |
info = self._get_array_info(descr, optheap) |
| 748 |
info.setitem(descr, index, struct, op, optheap=optheap, cf=cf) |
| 749 |
|
| 750 |
def setfield(self, fielddescr, struct, op, optheap=None, cf=None): |
| 751 |
info = self._get_info(fielddescr.get_parent_descr(), optheap) |
| 752 |
info.setfield(fielddescr, struct, op, optheap=optheap, cf=cf) |
| 753 |
|
| 754 |
def is_null(self): |
| 755 |
return not bool(self._const.getref_base()) |
| 756 |
|
| 757 |
def is_nonnull(self): |
| 758 |
return bool(self._const.getref_base()) |
| 759 |
|
| 760 |
def is_virtual(self): |
| 761 |
return False |
| 762 |
|
| 763 |
def get_known_class(self, cpu): |
| 764 |
if not self._const.nonnull(): |
| 765 |
return None |
| 766 |
if cpu.supports_guard_gc_type: |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
if not cpu.check_is_object(self._const.getref_base()): |
| 771 |
return None |
| 772 |
return cpu.cls_of_box(self._const) |
| 773 |
|
| 774 |
def same_info(self, other): |
| 775 |
if not isinstance(other, ConstPtrInfo): |
| 776 |
return False |
| 777 |
return self._const.same_constant(other._const) |
| 778 |
|
| 779 |
def get_last_guard(self, optimizer): |
| 780 |
return None |
| 781 |
|
| 782 |
def is_constant(self): |
| 783 |
return True |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
@specialize.arg(1) |
| 788 |
def _unpack_str(self, mode): |
| 789 |
return mode.hlstr(lltype.cast_opaque_ptr( |
| 790 |
lltype.Ptr(mode.LLTYPE), self._const.getref_base())) |
| 791 |
|
| 792 |
@specialize.arg(2) |
| 793 |
def get_constant_string_spec(self, optforce, mode): |
| 794 |
return self._unpack_str(mode) |
| 795 |
|
| 796 |
def getlenbound(self, mode): |
| 797 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 798 |
length = self.getstrlen1(mode) |
| 799 |
if length < 0: |
| 800 |
|
| 801 |
return IntBound.nonnegative() |
| 802 |
return IntBound.from_constant(length) |
| 803 |
|
| 804 |
def getstrlen(self, op, string_optimizer, mode): |
| 805 |
length = self.getstrlen1(mode) |
| 806 |
if length < 0: |
| 807 |
return None |
| 808 |
return ConstInt(length) |
| 809 |
|
| 810 |
def getstrlen1(self, mode): |
| 811 |
from rpython.jit.metainterp.optimizeopt import vstring |
| 812 |
|
| 813 |
if mode is vstring.mode_string: |
| 814 |
s = self._unpack_str(vstring.mode_string) |
| 815 |
if s is None: |
| 816 |
return -1 |
| 817 |
return len(s) |
| 818 |
elif mode is vstring.mode_unicode: |
| 819 |
s = self._unpack_str(vstring.mode_unicode) |
| 820 |
if s is None: |
| 821 |
return -1 |
| 822 |
return len(s) |
| 823 |
else: |
| 824 |
return -1 |
| 825 |
|
| 826 |
def getstrhash(self, op, mode): |
| 827 |
from rpython.jit.metainterp.optimizeopt import vstring |
| 828 |
|
| 829 |
if mode is vstring.mode_string: |
| 830 |
s = self._unpack_str(vstring.mode_string) |
| 831 |
if s is None: |
| 832 |
return None |
| 833 |
return ConstInt(compute_hash(s)) |
| 834 |
else: |
| 835 |
s = self._unpack_str(vstring.mode_unicode) |
| 836 |
if s is None: |
| 837 |
return None |
| 838 |
return ConstInt(compute_hash(s)) |
| 839 |
|
| 840 |
def string_copy_parts(self, op, string_optimizer, targetbox, offsetbox, |
| 841 |
mode): |
| 842 |
from rpython.jit.metainterp.optimizeopt import vstring |
| 843 |
from rpython.jit.metainterp.optimizeopt.optimizer import CONST_0 |
| 844 |
|
| 845 |
lgt = self.getstrlen(op, string_optimizer, mode) |
| 846 |
return vstring.copy_str_content(string_optimizer, self._const, |
| 847 |
targetbox, CONST_0, offsetbox, |
| 848 |
lgt, mode) |
| 849 |
|
| 850 |
|
| 851 |
class FloatConstInfo(AbstractInfo): |
| 852 |
def __init__(self, const): |
| 853 |
self._const = const |
| 854 |
|
| 855 |
def is_constant(self): |
| 856 |
return True |
| 857 |
|
| 858 |
def getconst(self): |
| 859 |
return self._const |
| 860 |
|
| 861 |
def make_guards(self, op, short, optimizer): |
| 862 |
short.append(ResOperation(rop.GUARD_VALUE, [op, self._const])) |
| 863 |
|
| 864 |
|
| 865 |
def getrawptrinfo(op): |
| 866 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 867 |
assert op.type == 'i' |
| 868 |
op = op.get_box_replacement() |
| 869 |
assert op.type == 'i' |
| 870 |
if isinstance(op, ConstInt): |
| 871 |
return ConstPtrInfo(op) |
| 872 |
fw = op.get_forwarded() |
| 873 |
if isinstance(fw, IntBound): |
| 874 |
return None |
| 875 |
if fw is not None: |
| 876 |
assert isinstance(fw, AbstractRawPtrInfo) |
| 877 |
return fw |
| 878 |
return None |
| 879 |
|
| 880 |
def getptrinfo(op): |
| 881 |
if op.type == 'i': |
| 882 |
return getrawptrinfo(op) |
| 883 |
elif op.type == 'f': |
| 884 |
return None |
| 885 |
assert op.type == 'r' |
| 886 |
op = get_box_replacement(op) |
| 887 |
assert op.type == 'r' |
| 888 |
if isinstance(op, ConstPtr): |
| 889 |
return ConstPtrInfo(op) |
| 890 |
fw = op.get_forwarded() |
| 891 |
if fw is not None: |
| 892 |
assert isinstance(fw, PtrInfo) |
| 893 |
return fw |
| 894 |
return None |
| 895 |
|