| 1 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 2 |
from rpython.jit.metainterp.history import (Const, ConstInt, ConstPtr, |
| 3 |
get_const_ptr_for_string, get_const_ptr_for_unicode, REF, INT, |
| 4 |
DONT_CHANGE, CONST_NULL) |
| 5 |
from rpython.jit.metainterp.optimizeopt.optimizer import ( |
| 6 |
CONST_0, CONST_1, REMOVED, Optimization) |
| 7 |
from rpython.jit.metainterp.optimizeopt.util import ( |
| 8 |
make_dispatcher_method, have_dispatcher_method, get_box_replacement) |
| 9 |
from rpython.jit.metainterp.resoperation import rop, ResOperation |
| 10 |
from .info import AbstractVirtualPtrInfo, getptrinfo |
| 11 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 12 |
from rpython.rlib.unroll import unrolling_iterable |
| 13 |
from rpython.rtyper import annlowlevel |
| 14 |
from rpython.rtyper.lltypesystem import lltype, rstr |
| 15 |
from rpython.rlib.rarithmetic import is_valid_int |
| 16 |
|
| 17 |
|
| 18 |
MAX_CONST_LEN = 100 |
| 19 |
|
| 20 |
|
| 21 |
class StrOrUnicode(object): |
| 22 |
def __init__(self, LLTYPE, hlstr, emptystr, chr, |
| 23 |
NEWSTR, STRLEN, STRGETITEM, STRSETITEM, COPYSTRCONTENT, |
| 24 |
OS_offset): |
| 25 |
self.LLTYPE = LLTYPE |
| 26 |
self.hlstr = hlstr |
| 27 |
self.emptystr = emptystr |
| 28 |
self.chr = chr |
| 29 |
self.NEWSTR = NEWSTR |
| 30 |
self.STRLEN = STRLEN |
| 31 |
self.STRGETITEM = STRGETITEM |
| 32 |
self.STRSETITEM = STRSETITEM |
| 33 |
self.COPYSTRCONTENT = COPYSTRCONTENT |
| 34 |
self.OS_offset = OS_offset |
| 35 |
|
| 36 |
def _freeze_(self): |
| 37 |
return True |
| 38 |
|
| 39 |
mode_string = StrOrUnicode(rstr.STR, annlowlevel.hlstr, '', chr, |
| 40 |
rop.NEWSTR, rop.STRLEN, rop.STRGETITEM, |
| 41 |
rop.STRSETITEM, rop.COPYSTRCONTENT, 0) |
| 42 |
mode_unicode = StrOrUnicode(rstr.UNICODE, annlowlevel.hlunicode, u'', unichr, |
| 43 |
rop.NEWUNICODE, rop.UNICODELEN, rop.UNICODEGETITEM, |
| 44 |
rop.UNICODESETITEM, rop.COPYUNICODECONTENT, |
| 45 |
EffectInfo._OS_offset_uni) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class StrPtrInfo(AbstractVirtualPtrInfo): |
| 51 |
|
| 52 |
|
| 53 |
lenbound = None |
| 54 |
lgtop = None |
| 55 |
_cached_vinfo = None |
| 56 |
|
| 57 |
def __init__(self, mode, is_virtual=False, length=-1): |
| 58 |
self.length = length |
| 59 |
self._is_virtual = is_virtual |
| 60 |
self.mode = mode |
| 61 |
|
| 62 |
def getlenbound(self, mode): |
| 63 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 64 |
|
| 65 |
if self.lenbound is None: |
| 66 |
if self.length == -1: |
| 67 |
self.lenbound = IntBound.nonnegative() |
| 68 |
else: |
| 69 |
self.lenbound = IntBound.from_constant(self.length) |
| 70 |
return self.lenbound |
| 71 |
|
| 72 |
@specialize.arg(2) |
| 73 |
def get_constant_string_spec(self, optstring, mode): |
| 74 |
return None |
| 75 |
|
| 76 |
def force_box(self, op, optforce): |
| 77 |
if not self.is_virtual(): |
| 78 |
return op |
| 79 |
if self.mode is mode_string: |
| 80 |
s = self.get_constant_string_spec(optforce, mode_string) |
| 81 |
if s is not None: |
| 82 |
c_s = get_const_ptr_for_string(s) |
| 83 |
get_box_replacement(op).set_forwarded(c_s) |
| 84 |
return c_s |
| 85 |
else: |
| 86 |
s = self.get_constant_string_spec(optforce, mode_unicode) |
| 87 |
if s is not None: |
| 88 |
c_s = get_const_ptr_for_unicode(s) |
| 89 |
get_box_replacement(op).set_forwarded(c_s) |
| 90 |
return c_s |
| 91 |
self._is_virtual = False |
| 92 |
lengthbox = self.getstrlen(op, optforce.optimizer.optstring, self.mode) |
| 93 |
newop = ResOperation(self.mode.NEWSTR, [lengthbox]) |
| 94 |
if not we_are_translated(): |
| 95 |
newop.name = 'FORCE' |
| 96 |
optforce.emit_extra(newop) |
| 97 |
newop = optforce.optimizer.getlastop() |
| 98 |
newop.set_forwarded(self) |
| 99 |
op = get_box_replacement(op) |
| 100 |
op.set_forwarded(newop) |
| 101 |
optstring = optforce.optimizer.optstring |
| 102 |
self.initialize_forced_string(op, optstring, op, CONST_0, self.mode) |
| 103 |
return newop |
| 104 |
|
| 105 |
def initialize_forced_string(self, op, optstring, targetbox, |
| 106 |
offsetbox, mode): |
| 107 |
return self.string_copy_parts(op, optstring, targetbox, |
| 108 |
offsetbox, mode) |
| 109 |
|
| 110 |
def getstrlen(self, op, optstring, mode): |
| 111 |
assert op is not None |
| 112 |
if self.lgtop is not None: |
| 113 |
return self.lgtop |
| 114 |
assert not self.is_virtual() |
| 115 |
lengthop = ResOperation(mode.STRLEN, [op]) |
| 116 |
lengthop.set_forwarded(self.getlenbound(mode)) |
| 117 |
self.lgtop = lengthop |
| 118 |
optstring.emit_extra(lengthop) |
| 119 |
return lengthop |
| 120 |
|
| 121 |
def make_guards(self, op, short, optimizer): |
| 122 |
AbstractVirtualPtrInfo.make_guards(self, op, short, optimizer) |
| 123 |
if self.lenbound and self.lenbound.lower >= 1: |
| 124 |
if self.mode is mode_string: |
| 125 |
lenop = ResOperation(rop.STRLEN, [op]) |
| 126 |
else: |
| 127 |
assert self.mode is mode_unicode |
| 128 |
lenop = ResOperation(rop.UNICODELEN, [op]) |
| 129 |
short.append(lenop) |
| 130 |
self.lenbound.make_guards(lenop, short, optimizer) |
| 131 |
|
| 132 |
def string_copy_parts(self, op, optstring, targetbox, offsetbox, |
| 133 |
mode): |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
lengthbox = self.getstrlen(op, optstring, mode) |
| 138 |
srcbox = self.force_box(op, optstring) |
| 139 |
return copy_str_content(optstring, srcbox, targetbox, |
| 140 |
CONST_0, offsetbox, lengthbox, mode) |
| 141 |
|
| 142 |
class VStringPlainInfo(StrPtrInfo): |
| 143 |
|
| 144 |
|
| 145 |
_chars = None |
| 146 |
|
| 147 |
def __init__(self, mode, is_virtual, length): |
| 148 |
if length != -1: |
| 149 |
self._chars = [None] * length |
| 150 |
StrPtrInfo.__init__(self, mode, is_virtual, length) |
| 151 |
|
| 152 |
def strsetitem(self, index, op, cf=None, optheap=None): |
| 153 |
self._chars[index] = op |
| 154 |
|
| 155 |
def shrink(self, length): |
| 156 |
assert length >= 0 |
| 157 |
self.length = length |
| 158 |
del self._chars[length:] |
| 159 |
|
| 160 |
def setup_slice(self, longerlist, start, stop): |
| 161 |
assert 0 <= start <= stop <= len(longerlist) |
| 162 |
self._chars = longerlist[start:stop] |
| 163 |
|
| 164 |
|
| 165 |
def strgetitem(self, index, optheap=None): |
| 166 |
return self._chars[index] |
| 167 |
|
| 168 |
def is_virtual(self): |
| 169 |
return self._is_virtual |
| 170 |
|
| 171 |
def getstrlen(self, op, optstring, mode): |
| 172 |
assert op is not None |
| 173 |
if self.lgtop is None: |
| 174 |
self.lgtop = ConstInt(len(self._chars)) |
| 175 |
return self.lgtop |
| 176 |
|
| 177 |
@specialize.arg(2) |
| 178 |
def get_constant_string_spec(self, optforce, mode): |
| 179 |
for c in self._chars: |
| 180 |
if c is None or not c.is_constant(): |
| 181 |
return None |
| 182 |
return mode.emptystr.join([mode.chr(c.getint()) |
| 183 |
for c in self._chars]) |
| 184 |
|
| 185 |
def string_copy_parts(self, op, optstring, targetbox, offsetbox, |
| 186 |
mode): |
| 187 |
if not self.is_virtual(): |
| 188 |
return StrPtrInfo.string_copy_parts(self, op, optstring, |
| 189 |
targetbox, offsetbox, mode) |
| 190 |
else: |
| 191 |
return self.initialize_forced_string(op, optstring, |
| 192 |
targetbox, offsetbox, mode) |
| 193 |
|
| 194 |
def initialize_forced_string(self, op, optstring, targetbox, |
| 195 |
offsetbox, mode): |
| 196 |
for i in range(len(self._chars)): |
| 197 |
assert not isinstance(targetbox, Const) |
| 198 |
charbox = self.strgetitem(i) |
| 199 |
if charbox is not None: |
| 200 |
op = ResOperation(mode.STRSETITEM, [targetbox, |
| 201 |
offsetbox, |
| 202 |
charbox]) |
| 203 |
optstring.emit_extra(op) |
| 204 |
offsetbox = _int_add(optstring, offsetbox, CONST_1) |
| 205 |
return offsetbox |
| 206 |
|
| 207 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 208 |
visitor.register_virtual_fields(instbox, self._chars) |
| 209 |
|
| 210 |
@specialize.argtype(1) |
| 211 |
def visitor_dispatch_virtual_type(self, visitor): |
| 212 |
return visitor.visit_vstrplain(self.mode is mode_unicode) |
| 213 |
|
| 214 |
class VStringSliceInfo(StrPtrInfo): |
| 215 |
length = -1 |
| 216 |
start = None |
| 217 |
lgtop = None |
| 218 |
s = None |
| 219 |
|
| 220 |
def __init__(self, s, start, length, mode): |
| 221 |
self.s = s |
| 222 |
self.start = start |
| 223 |
self.lgtop = length |
| 224 |
self.mode = mode |
| 225 |
self._is_virtual = True |
| 226 |
|
| 227 |
def is_virtual(self): |
| 228 |
return self._is_virtual |
| 229 |
|
| 230 |
def string_copy_parts(self, op, optstring, targetbox, offsetbox, |
| 231 |
mode): |
| 232 |
return copy_str_content(optstring, self.s, targetbox, |
| 233 |
self.start, offsetbox, self.lgtop, mode) |
| 234 |
|
| 235 |
@specialize.arg(2) |
| 236 |
def get_constant_string_spec(self, optstring, mode): |
| 237 |
vstart = optstring.getintbound(self.start) |
| 238 |
vlength = optstring.getintbound(self.lgtop) |
| 239 |
if vstart.is_constant() and vlength.is_constant(): |
| 240 |
vstr = getptrinfo(self.s) |
| 241 |
s1 = vstr.get_constant_string_spec(optstring, mode) |
| 242 |
if s1 is None: |
| 243 |
return None |
| 244 |
start = vstart.get_constant_int() |
| 245 |
length = vlength.get_constant_int() |
| 246 |
assert start >= 0 |
| 247 |
assert length >= 0 |
| 248 |
return s1[start: start + length] |
| 249 |
return None |
| 250 |
|
| 251 |
def getstrlen(self, op, optstring, mode): |
| 252 |
assert op is not None |
| 253 |
return self.lgtop |
| 254 |
|
| 255 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 256 |
boxes = [self.s, self.start, self.lgtop] |
| 257 |
visitor.register_virtual_fields(instbox, boxes) |
| 258 |
opinfo = getptrinfo(self.s) |
| 259 |
if opinfo and opinfo.is_virtual(): |
| 260 |
opinfo.visitor_walk_recursive(self.s, visitor) |
| 261 |
|
| 262 |
@specialize.argtype(1) |
| 263 |
def visitor_dispatch_virtual_type(self, visitor): |
| 264 |
return visitor.visit_vstrslice(self.mode is mode_unicode) |
| 265 |
|
| 266 |
class VStringConcatInfo(StrPtrInfo): |
| 267 |
|
| 268 |
|
| 269 |
vleft = None |
| 270 |
vright = None |
| 271 |
_is_virtual = False |
| 272 |
|
| 273 |
def __init__(self, mode, vleft, vright, is_virtual): |
| 274 |
self.vleft = vleft |
| 275 |
self.vright = vright |
| 276 |
StrPtrInfo.__init__(self, mode, is_virtual) |
| 277 |
|
| 278 |
def is_virtual(self): |
| 279 |
return self._is_virtual |
| 280 |
|
| 281 |
def getstrlen(self, op, optstring, mode): |
| 282 |
assert op is not None |
| 283 |
if self.lgtop is not None: |
| 284 |
return self.lgtop |
| 285 |
lefti = getptrinfo(self.vleft) |
| 286 |
len1box = lefti.getstrlen(self.vleft, optstring, mode) |
| 287 |
if len1box is None: |
| 288 |
return None |
| 289 |
righti = getptrinfo(self.vright) |
| 290 |
len2box = righti.getstrlen(self.vright, optstring, mode) |
| 291 |
if len2box is None: |
| 292 |
return None |
| 293 |
self.lgtop = _int_add(optstring, len1box, len2box) |
| 294 |
|
| 295 |
return self.lgtop |
| 296 |
|
| 297 |
@specialize.arg(2) |
| 298 |
def get_constant_string_spec(self, optstring, mode): |
| 299 |
ileft = getptrinfo(self.vleft) |
| 300 |
s1 = ileft.get_constant_string_spec(optstring, mode) |
| 301 |
if s1 is None: |
| 302 |
return None |
| 303 |
iright = getptrinfo(self.vright) |
| 304 |
s2 = iright.get_constant_string_spec(optstring, mode) |
| 305 |
if s2 is None: |
| 306 |
return None |
| 307 |
return s1 + s2 |
| 308 |
|
| 309 |
def string_copy_parts(self, op, optstring, targetbox, offsetbox, |
| 310 |
mode): |
| 311 |
lefti = getptrinfo(self.vleft) |
| 312 |
offsetbox = lefti.string_copy_parts(self.vleft, optstring, |
| 313 |
targetbox, offsetbox, mode) |
| 314 |
righti = getptrinfo(self.vright) |
| 315 |
offsetbox = righti.string_copy_parts(self.vright, optstring, |
| 316 |
targetbox, offsetbox, mode) |
| 317 |
return offsetbox |
| 318 |
|
| 319 |
def _visitor_walk_recursive(self, instbox, visitor): |
| 320 |
|
| 321 |
|
| 322 |
leftbox = self.vleft |
| 323 |
rightbox = self.vright |
| 324 |
visitor.register_virtual_fields(instbox, [leftbox, rightbox]) |
| 325 |
leftinfo = getptrinfo(leftbox) |
| 326 |
rightinfo = getptrinfo(rightbox) |
| 327 |
if leftinfo and leftinfo.is_virtual(): |
| 328 |
leftinfo.visitor_walk_recursive(leftbox, visitor) |
| 329 |
if rightinfo and rightinfo.is_virtual(): |
| 330 |
rightinfo.visitor_walk_recursive(rightbox, visitor) |
| 331 |
|
| 332 |
@specialize.argtype(1) |
| 333 |
def visitor_dispatch_virtual_type(self, visitor): |
| 334 |
return visitor.visit_vstrconcat(self.mode is mode_unicode) |
| 335 |
|
| 336 |
|
| 337 |
def copy_str_content(optstring, srcbox, targetbox, |
| 338 |
srcoffsetbox, offsetbox, lengthbox, mode, |
| 339 |
need_next_offset=True): |
| 340 |
srcbox = get_box_replacement(srcbox) |
| 341 |
srcoffset = optstring.getintbound(srcoffsetbox) |
| 342 |
lgt = optstring.getintbound(lengthbox) |
| 343 |
if isinstance(srcbox, ConstPtr) and srcoffset.is_constant(): |
| 344 |
M = 5 |
| 345 |
else: |
| 346 |
M = 2 |
| 347 |
if lgt.is_constant() and lgt.get_constant_int() <= M: |
| 348 |
|
| 349 |
|
| 350 |
for i in range(lgt.get_constant_int()): |
| 351 |
charbox = optstring.strgetitem(None, srcbox, srcoffsetbox, |
| 352 |
mode) |
| 353 |
srcoffsetbox = _int_add(optstring, srcoffsetbox, CONST_1) |
| 354 |
assert not isinstance(targetbox, Const) |
| 355 |
optstring.emit_extra(ResOperation(mode.STRSETITEM, |
| 356 |
[targetbox, offsetbox, charbox])) |
| 357 |
offsetbox = _int_add(optstring, offsetbox, CONST_1) |
| 358 |
else: |
| 359 |
if need_next_offset: |
| 360 |
nextoffsetbox = _int_add(optstring, offsetbox, lengthbox) |
| 361 |
else: |
| 362 |
nextoffsetbox = None |
| 363 |
assert not isinstance(targetbox, Const) |
| 364 |
op = ResOperation(mode.COPYSTRCONTENT, [srcbox, targetbox, |
| 365 |
srcoffsetbox, offsetbox, |
| 366 |
lengthbox]) |
| 367 |
optstring.emit_extra(op) |
| 368 |
offsetbox = nextoffsetbox |
| 369 |
return offsetbox |
| 370 |
|
| 371 |
def _int_add(optstring, box1, box2): |
| 372 |
if isinstance(box1, ConstInt): |
| 373 |
if box1.value == 0: |
| 374 |
return box2 |
| 375 |
if isinstance(box2, ConstInt): |
| 376 |
return ConstInt(box1.value + box2.value) |
| 377 |
elif isinstance(box2, ConstInt) and box2.value == 0: |
| 378 |
return box1 |
| 379 |
op = ResOperation(rop.INT_ADD, [box1, box2]) |
| 380 |
optstring.optimizer.send_extra_operation(op) |
| 381 |
return op |
| 382 |
|
| 383 |
def _int_sub(optstring, box1, box2): |
| 384 |
if isinstance(box2, ConstInt): |
| 385 |
if box2.value == 0: |
| 386 |
return box1 |
| 387 |
if isinstance(box1, ConstInt): |
| 388 |
return ConstInt(box1.value - box2.value) |
| 389 |
op = ResOperation(rop.INT_SUB, [box1, box2]) |
| 390 |
optstring.optimizer.send_extra_operation(op) |
| 391 |
return op |
| 392 |
|
| 393 |
def _strgetitem(optstring, strbox, indexbox, mode, resbox=None): |
| 394 |
if isinstance(strbox, ConstPtr) and isinstance(indexbox, ConstInt): |
| 395 |
if mode is mode_string: |
| 396 |
s = strbox.getref(lltype.Ptr(rstr.STR)) |
| 397 |
resnewbox = ConstInt(ord(s.chars[indexbox.getint()])) |
| 398 |
else: |
| 399 |
s = strbox.getref(lltype.Ptr(rstr.UNICODE)) |
| 400 |
resnewbox = ConstInt(ord(s.chars[indexbox.getint()])) |
| 401 |
if resbox is not None: |
| 402 |
optstring.make_equal_to(resbox, resnewbox) |
| 403 |
return resnewbox |
| 404 |
if resbox is None: |
| 405 |
resbox = ResOperation(mode.STRGETITEM, [strbox, indexbox]) |
| 406 |
else: |
| 407 |
resbox = optstring.replace_op_with(resbox, mode.STRGETITEM, |
| 408 |
[strbox, indexbox]) |
| 409 |
optstring.emit_extra(resbox) |
| 410 |
return resbox |
| 411 |
|
| 412 |
|
| 413 |
class OptString(Optimization): |
| 414 |
"Handling of strings and unicodes." |
| 415 |
|
| 416 |
def setup(self): |
| 417 |
self.optimizer.optstring = self |
| 418 |
|
| 419 |
def propagate_forward(self, op): |
| 420 |
return dispatch_opt(self, op) |
| 421 |
|
| 422 |
def make_vstring_plain(self, op, mode, length): |
| 423 |
vvalue = VStringPlainInfo(mode, True, length) |
| 424 |
op = self.replace_op_with(op, op.getopnum()) |
| 425 |
op.set_forwarded(vvalue) |
| 426 |
return vvalue |
| 427 |
|
| 428 |
def make_vstring_concat(self, op, mode, vleft, vright): |
| 429 |
vvalue = VStringConcatInfo(mode, vleft, vright, True) |
| 430 |
op = self.replace_op_with(op, op.getopnum()) |
| 431 |
op.set_forwarded(vvalue) |
| 432 |
return vvalue |
| 433 |
|
| 434 |
def make_vstring_slice(self, op, strbox, startbox, mode, lengthbox): |
| 435 |
vvalue = VStringSliceInfo(strbox, startbox, lengthbox, mode) |
| 436 |
op = self.replace_op_with(op, op.getopnum()) |
| 437 |
op.set_forwarded(vvalue) |
| 438 |
return vvalue |
| 439 |
|
| 440 |
def optimize_NEWSTR(self, op): |
| 441 |
return self._optimize_NEWSTR(op, mode_string) |
| 442 |
|
| 443 |
def optimize_NEWUNICODE(self, op): |
| 444 |
return self._optimize_NEWSTR(op, mode_unicode) |
| 445 |
|
| 446 |
def _optimize_NEWSTR(self, op, mode): |
| 447 |
length_box = self.get_constant_box(op.getarg(0)) |
| 448 |
if length_box and length_box.getint() <= MAX_CONST_LEN: |
| 449 |
assert not op.get_forwarded() |
| 450 |
self.make_vstring_plain(op, mode, length_box.getint()) |
| 451 |
else: |
| 452 |
self.make_nonnull_str(op, mode) |
| 453 |
return self.emit(op) |
| 454 |
|
| 455 |
def postprocess_NEWSTR(self, op): |
| 456 |
self.pure_from_args1(mode_string.STRLEN, op, op.getarg(0)) |
| 457 |
|
| 458 |
def postprocess_NEWUNICODE(self, op): |
| 459 |
self.pure_from_args1(mode_unicode.STRLEN, op, op.getarg(0)) |
| 460 |
|
| 461 |
def optimize_STRSETITEM(self, op): |
| 462 |
opinfo = getptrinfo(op.getarg(0)) |
| 463 |
if opinfo: |
| 464 |
assert not opinfo.is_constant() |
| 465 |
|
| 466 |
if opinfo and opinfo.is_virtual(): |
| 467 |
indexbox = self.get_constant_box(op.getarg(1)) |
| 468 |
if indexbox is not None: |
| 469 |
opinfo.strsetitem(indexbox.getint(), |
| 470 |
get_box_replacement(op.getarg(2))) |
| 471 |
return |
| 472 |
self.make_nonnull(op.getarg(0)) |
| 473 |
return self.emit(op) |
| 474 |
|
| 475 |
optimize_UNICODESETITEM = optimize_STRSETITEM |
| 476 |
|
| 477 |
def optimize_STRGETITEM(self, op): |
| 478 |
return self._optimize_STRGETITEM(op, mode_string) |
| 479 |
|
| 480 |
def optimize_UNICODEGETITEM(self, op): |
| 481 |
return self._optimize_STRGETITEM(op, mode_unicode) |
| 482 |
|
| 483 |
def _optimize_STRGETITEM(self, op, mode): |
| 484 |
self.strgetitem(op, op.getarg(0), op.getarg(1), mode) |
| 485 |
|
| 486 |
def strgetitem(self, op, s, index, mode): |
| 487 |
self.make_nonnull_str(s, mode) |
| 488 |
sinfo = getptrinfo(s) |
| 489 |
|
| 490 |
if isinstance(sinfo, VStringSliceInfo) and sinfo.is_virtual(): |
| 491 |
index = _int_add(self.optimizer, sinfo.start, index) |
| 492 |
s = sinfo.s |
| 493 |
sinfo = getptrinfo(sinfo.s) |
| 494 |
|
| 495 |
vindex = self.getintbound(index) |
| 496 |
if isinstance(sinfo, VStringPlainInfo): |
| 497 |
|
| 498 |
if vindex.is_constant(): |
| 499 |
result = sinfo.strgetitem(vindex.get_constant_int()) |
| 500 |
if result is not None: |
| 501 |
if op is not None: |
| 502 |
self.make_equal_to(op, result) |
| 503 |
return result |
| 504 |
|
| 505 |
if isinstance(sinfo, VStringConcatInfo) and vindex.is_constant(): |
| 506 |
leftinfo = getptrinfo(sinfo.vleft) |
| 507 |
len1box = leftinfo.getstrlen(sinfo.vleft, self, mode) |
| 508 |
if isinstance(len1box, ConstInt): |
| 509 |
raw_index = vindex.get_constant_int() |
| 510 |
len1 = len1box.getint() |
| 511 |
if raw_index < len1: |
| 512 |
return self.strgetitem(op, sinfo.vleft, index, mode) |
| 513 |
else: |
| 514 |
index = ConstInt(raw_index - len1) |
| 515 |
return self.strgetitem(op, sinfo.vright, index, mode) |
| 516 |
|
| 517 |
return _strgetitem(self, s, index, mode, op) |
| 518 |
|
| 519 |
def optimize_STRLEN(self, op): |
| 520 |
return self._optimize_STRLEN(op, mode_string) |
| 521 |
|
| 522 |
def optimize_UNICODELEN(self, op): |
| 523 |
return self._optimize_STRLEN(op, mode_unicode) |
| 524 |
|
| 525 |
def _optimize_STRLEN(self, op, mode): |
| 526 |
arg1 = get_box_replacement(op.getarg(0)) |
| 527 |
opinfo = getptrinfo(arg1) |
| 528 |
if opinfo: |
| 529 |
lgtop = opinfo.getstrlen(arg1, self, mode) |
| 530 |
if lgtop is not None: |
| 531 |
self.make_equal_to(op, lgtop) |
| 532 |
return |
| 533 |
return self.emit(op) |
| 534 |
|
| 535 |
def optimize_STRHASH(self, op): |
| 536 |
return self._optimize_STRHASH(op, mode_string) |
| 537 |
|
| 538 |
def optimize_UNICODEHASH(self, op): |
| 539 |
return self._optimize_STRHASH(op, mode_unicode) |
| 540 |
|
| 541 |
def _optimize_STRHASH(self, op, mode): |
| 542 |
opinfo = getptrinfo(op.getarg(0)) |
| 543 |
if opinfo: |
| 544 |
lgtop = opinfo.getstrhash(op, mode) |
| 545 |
if lgtop is not None: |
| 546 |
self.make_equal_to(op, lgtop) |
| 547 |
return |
| 548 |
return self.emit(op) |
| 549 |
|
| 550 |
def optimize_COPYSTRCONTENT(self, op): |
| 551 |
return self._optimize_COPYSTRCONTENT(op, mode_string) |
| 552 |
|
| 553 |
def optimize_COPYUNICODECONTENT(self, op): |
| 554 |
return self._optimize_COPYSTRCONTENT(op, mode_unicode) |
| 555 |
|
| 556 |
def _optimize_COPYSTRCONTENT(self, op, mode): |
| 557 |
|
| 558 |
assert op.getarg(0).type == REF |
| 559 |
assert op.getarg(1).type == REF |
| 560 |
assert op.getarg(2).type == INT |
| 561 |
assert op.getarg(3).type == INT |
| 562 |
assert op.getarg(4).type == INT |
| 563 |
src = getptrinfo(op.getarg(0)) |
| 564 |
dst = getptrinfo(op.getarg(1)) |
| 565 |
srcstart = self.getintbound(op.getarg(2)) |
| 566 |
dststart = self.getintbound(op.getarg(3)) |
| 567 |
length = self.getintbound(op.getarg(4)) |
| 568 |
dst_virtual = (isinstance(dst, VStringPlainInfo) and dst.is_virtual()) |
| 569 |
|
| 570 |
if length.is_constant() and length.get_constant_int() == 0: |
| 571 |
return |
| 572 |
elif ((str and (src.is_virtual() or src.is_constant())) and |
| 573 |
srcstart.is_constant() and dststart.is_constant() and |
| 574 |
length.is_constant() and |
| 575 |
(length.get_constant_int() < 20 or ((src.is_virtual() or src.is_constant()) and dst_virtual))): |
| 576 |
src_start = srcstart.get_constant_int() |
| 577 |
dst_start = dststart.get_constant_int() |
| 578 |
actual_length = length.get_constant_int() |
| 579 |
for index in range(actual_length): |
| 580 |
vresult = self.strgetitem(None, op.getarg(0), |
| 581 |
ConstInt(index + src_start), mode) |
| 582 |
if dst_virtual: |
| 583 |
dst.strsetitem(index + dst_start, vresult) |
| 584 |
else: |
| 585 |
new_op = ResOperation(mode.STRSETITEM, [ |
| 586 |
op.getarg(1), ConstInt(index + dst_start), |
| 587 |
vresult, |
| 588 |
]) |
| 589 |
self.emit_extra(new_op) |
| 590 |
else: |
| 591 |
copy_str_content(self, op.getarg(0), op.getarg(1), op.getarg(2), |
| 592 |
op.getarg(3), op.getarg(4), mode, |
| 593 |
need_next_offset=False) |
| 594 |
|
| 595 |
def optimize_CALL_I(self, op): |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
effectinfo = op.getdescr().get_extra_info() |
| 600 |
oopspecindex = effectinfo.oopspecindex |
| 601 |
if oopspecindex != EffectInfo.OS_NONE: |
| 602 |
for value, meth in opt_call_oopspec_ops: |
| 603 |
if oopspecindex == value: |
| 604 |
handled, newop = meth(self, op, mode_string) |
| 605 |
if handled: |
| 606 |
return newop |
| 607 |
break |
| 608 |
if oopspecindex == value + EffectInfo._OS_offset_uni: |
| 609 |
|
| 610 |
handled, newop = meth(self, op, mode_unicode) |
| 611 |
if handled: |
| 612 |
return newop |
| 613 |
break |
| 614 |
if oopspecindex == EffectInfo.OS_STR2UNICODE: |
| 615 |
if self.opt_call_str_STR2UNICODE(op): |
| 616 |
return |
| 617 |
if oopspecindex == EffectInfo.OS_SHRINK_ARRAY: |
| 618 |
if self.opt_call_SHRINK_ARRAY(op): |
| 619 |
return |
| 620 |
return self.emit(op) |
| 621 |
optimize_CALL_R = optimize_CALL_I |
| 622 |
optimize_CALL_F = optimize_CALL_I |
| 623 |
optimize_CALL_N = optimize_CALL_I |
| 624 |
optimize_CALL_PURE_I = optimize_CALL_I |
| 625 |
optimize_CALL_PURE_R = optimize_CALL_I |
| 626 |
optimize_CALL_PURE_F = optimize_CALL_I |
| 627 |
optimize_CALL_PURE_N = optimize_CALL_I |
| 628 |
|
| 629 |
def optimize_GUARD_NO_EXCEPTION(self, op): |
| 630 |
if self.last_emitted_operation is REMOVED: |
| 631 |
return |
| 632 |
return self.emit(op) |
| 633 |
|
| 634 |
def opt_call_str_STR2UNICODE(self, op): |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
varg = getptrinfo(op.getarg(1)) |
| 640 |
s = None |
| 641 |
if varg: |
| 642 |
s = varg.get_constant_string_spec(self, mode_string) |
| 643 |
if s is None: |
| 644 |
return False |
| 645 |
try: |
| 646 |
u = unicode(s) |
| 647 |
except UnicodeDecodeError: |
| 648 |
return False |
| 649 |
self.make_constant(op, get_const_ptr_for_unicode(u)) |
| 650 |
self.last_emitted_operation = REMOVED |
| 651 |
return True |
| 652 |
|
| 653 |
def opt_call_stroruni_STR_CONCAT(self, op, mode): |
| 654 |
self.make_nonnull_str(op.getarg(1), mode) |
| 655 |
self.make_nonnull_str(op.getarg(2), mode) |
| 656 |
self.make_vstring_concat(op, mode, |
| 657 |
get_box_replacement(op.getarg(1)), |
| 658 |
get_box_replacement(op.getarg(2))) |
| 659 |
self.last_emitted_operation = REMOVED |
| 660 |
return True, None |
| 661 |
|
| 662 |
def opt_call_stroruni_STR_SLICE(self, op, mode): |
| 663 |
self.make_nonnull_str(op.getarg(1), mode) |
| 664 |
vstr = getptrinfo(op.getarg(1)) |
| 665 |
vstart = self.getintbound(op.getarg(2)) |
| 666 |
vstop = self.getintbound(op.getarg(3)) |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
startbox = op.getarg(2) |
| 679 |
strbox = op.getarg(1) |
| 680 |
lengthbox = _int_sub(self.optimizer, op.getarg(3), op.getarg(2)) |
| 681 |
|
| 682 |
if isinstance(vstr, VStringSliceInfo): |
| 683 |
|
| 684 |
strbox = vstr.s |
| 685 |
startbox = _int_add(self.optimizer, vstr.start, startbox) |
| 686 |
|
| 687 |
self.make_vstring_slice(op, strbox, startbox, mode, lengthbox) |
| 688 |
self.last_emitted_operation = REMOVED |
| 689 |
return True, None |
| 690 |
|
| 691 |
@specialize.arg(2) |
| 692 |
def opt_call_stroruni_STR_EQUAL(self, op, mode): |
| 693 |
arg1 = get_box_replacement(op.getarg(1)) |
| 694 |
arg2 = get_box_replacement(op.getarg(2)) |
| 695 |
i1 = getptrinfo(arg1) |
| 696 |
i2 = getptrinfo(arg2) |
| 697 |
|
| 698 |
if i1: |
| 699 |
l1box = i1.getstrlen(arg1, self, mode) |
| 700 |
else: |
| 701 |
l1box = None |
| 702 |
if i2: |
| 703 |
l2box = i2.getstrlen(arg2, self, mode) |
| 704 |
else: |
| 705 |
l2box = None |
| 706 |
if (l1box is not None and l2box is not None and |
| 707 |
isinstance(l1box, ConstInt) and |
| 708 |
isinstance(l2box, ConstInt) and |
| 709 |
l1box.value != l2box.value): |
| 710 |
|
| 711 |
self.make_constant(op, CONST_0) |
| 712 |
return True, None |
| 713 |
|
| 714 |
handled, result = self.handle_str_equal_level1(arg1, arg2, op, mode) |
| 715 |
if handled: |
| 716 |
return True, result |
| 717 |
handled, result = self.handle_str_equal_level1(arg2, arg1, op, mode) |
| 718 |
if handled: |
| 719 |
return True, result |
| 720 |
handled, result = self.handle_str_equal_level2(arg1, arg2, l2box, op, mode) |
| 721 |
if handled: |
| 722 |
return True, result |
| 723 |
handled, result = self.handle_str_equal_level2(arg2, arg1, l1box, op, mode) |
| 724 |
if handled: |
| 725 |
return True, result |
| 726 |
|
| 727 |
if i1 and i1.is_nonnull() and i2 and i2.is_nonnull(): |
| 728 |
if l1box is not None and l2box is not None and l1box.same_box(l2box): |
| 729 |
do = EffectInfo.OS_STREQ_LENGTHOK |
| 730 |
else: |
| 731 |
do = EffectInfo.OS_STREQ_NONNULL |
| 732 |
return True, self.generate_modified_call(do, [arg1, arg2], op, mode) |
| 733 |
return False, None |
| 734 |
|
| 735 |
def handle_str_equal_level1(self, arg1, arg2, resultop, mode): |
| 736 |
i1 = getptrinfo(arg1) |
| 737 |
i2 = getptrinfo(arg2) |
| 738 |
l2box = None |
| 739 |
l1box = None |
| 740 |
if i2: |
| 741 |
l2box = i2.getstrlen(arg2, self, mode) |
| 742 |
if isinstance(l2box, ConstInt): |
| 743 |
if l2box.value == 0: |
| 744 |
if i1 and i1.is_nonnull(): |
| 745 |
self.make_nonnull_str(arg1, mode) |
| 746 |
i1 = getptrinfo(arg1) |
| 747 |
lengthbox = i1.getstrlen(arg1, self, mode) |
| 748 |
else: |
| 749 |
lengthbox = None |
| 750 |
if lengthbox is not None: |
| 751 |
seo = self.optimizer.send_extra_operation |
| 752 |
op = self.replace_op_with(resultop, rop.INT_EQ, |
| 753 |
[lengthbox, CONST_0], |
| 754 |
descr=DONT_CHANGE) |
| 755 |
seo(op) |
| 756 |
return True, None |
| 757 |
if l2box.value == 1: |
| 758 |
if i1: |
| 759 |
l1box = i1.getstrlen(arg1, self, mode) |
| 760 |
if isinstance(l1box, ConstInt) and l1box.value == 1: |
| 761 |
|
| 762 |
vchar1 = self.strgetitem(None, arg1, CONST_0, mode) |
| 763 |
vchar2 = self.strgetitem(None, arg2, CONST_0, mode) |
| 764 |
seo = self.optimizer.send_extra_operation |
| 765 |
op = self.optimizer.replace_op_with(resultop, rop.INT_EQ, |
| 766 |
[vchar1, vchar2], descr=DONT_CHANGE) |
| 767 |
seo(op) |
| 768 |
return True, None |
| 769 |
if isinstance(i1, VStringSliceInfo): |
| 770 |
vchar = self.strgetitem(None, arg2, CONST_0, mode) |
| 771 |
do = EffectInfo.OS_STREQ_SLICE_CHAR |
| 772 |
return True, self.generate_modified_call(do, [i1.s, i1.start, |
| 773 |
i1.lgtop, vchar], |
| 774 |
resultop, mode) |
| 775 |
|
| 776 |
if i2 and i2.is_null(): |
| 777 |
if i1 and i1.is_nonnull(): |
| 778 |
self.make_constant(resultop, CONST_0) |
| 779 |
return True, None |
| 780 |
if i1 and i1.is_null(): |
| 781 |
self.make_constant(resultop, CONST_1) |
| 782 |
return True, None |
| 783 |
op = self.optimizer.replace_op_with( |
| 784 |
resultop, rop.PTR_EQ, [arg1, CONST_NULL], descr=DONT_CHANGE) |
| 785 |
return True, self.emit(op) |
| 786 |
|
| 787 |
return False, None |
| 788 |
|
| 789 |
def handle_str_equal_level2(self, arg1, arg2, l2box, resultbox, mode): |
| 790 |
i1 = getptrinfo(arg1) |
| 791 |
i2 = getptrinfo(arg2) |
| 792 |
if l2box: |
| 793 |
l2info = self.getintbound(l2box) |
| 794 |
if l2info.is_constant(): |
| 795 |
if l2info.get_constant_int() == 1: |
| 796 |
vchar = self.strgetitem(None, arg2, CONST_0, mode) |
| 797 |
if i1 and i1.is_nonnull(): |
| 798 |
do = EffectInfo.OS_STREQ_NONNULL_CHAR |
| 799 |
else: |
| 800 |
do = EffectInfo.OS_STREQ_CHECKNULL_CHAR |
| 801 |
return True, self.generate_modified_call(do, [arg1, vchar], |
| 802 |
resultbox, mode) |
| 803 |
|
| 804 |
if isinstance(i1, VStringSliceInfo) and i1.is_virtual(): |
| 805 |
if i2 and i2.is_nonnull(): |
| 806 |
do = EffectInfo.OS_STREQ_SLICE_NONNULL |
| 807 |
else: |
| 808 |
do = EffectInfo.OS_STREQ_SLICE_CHECKNULL |
| 809 |
return True, self.generate_modified_call(do, [i1.s, i1.start, i1.lgtop, |
| 810 |
arg2], resultbox, mode) |
| 811 |
return False, None |
| 812 |
|
| 813 |
def opt_call_stroruni_STR_CMP(self, op, mode): |
| 814 |
arg1 = get_box_replacement(op.getarg(1)) |
| 815 |
arg2 = get_box_replacement(op.getarg(2)) |
| 816 |
i1 = getptrinfo(arg1) |
| 817 |
i2 = getptrinfo(arg2) |
| 818 |
if not i1 or not i2: |
| 819 |
return False, None |
| 820 |
l1box = i1.getstrlen(arg1, self, mode) |
| 821 |
l2box = i2.getstrlen(arg2, self, mode) |
| 822 |
if (l1box is not None and l2box is not None and |
| 823 |
isinstance(l1box, ConstInt) and |
| 824 |
isinstance(l2box, ConstInt) and |
| 825 |
l1box.getint() == l2box.getint() == 1): |
| 826 |
|
| 827 |
char1 = self.strgetitem(None, op.getarg(1), CONST_0, mode) |
| 828 |
char2 = self.strgetitem(None, op.getarg(2), CONST_0, mode) |
| 829 |
seo = self.optimizer.send_extra_operation |
| 830 |
op = self.replace_op_with(op, rop.INT_SUB, [char1, char2], |
| 831 |
descr=DONT_CHANGE) |
| 832 |
seo(op) |
| 833 |
return True, None |
| 834 |
return False, None |
| 835 |
|
| 836 |
def opt_call_SHRINK_ARRAY(self, op): |
| 837 |
i1 = getptrinfo(op.getarg(1)) |
| 838 |
i2 = self.getintbound(op.getarg(2)) |
| 839 |
|
| 840 |
|
| 841 |
if (i2 and i2.is_constant() and i1 and i1.is_virtual() and |
| 842 |
isinstance(i1, VStringPlainInfo)): |
| 843 |
length = i2.get_constant_int() |
| 844 |
i1.shrink(length) |
| 845 |
self.last_emitted_operation = REMOVED |
| 846 |
self.make_equal_to(op, op.getarg(1)) |
| 847 |
return True |
| 848 |
return False |
| 849 |
|
| 850 |
def generate_modified_call(self, oopspecindex, args, result, mode): |
| 851 |
oopspecindex += mode.OS_offset |
| 852 |
cic = self.optimizer.metainterp_sd.callinfocollection |
| 853 |
calldescr, func = cic.callinfo_for_oopspec(oopspecindex) |
| 854 |
op = self.optimizer.replace_op_with(result, rop.CALL_I, |
| 855 |
[ConstInt(func)] + args, |
| 856 |
descr=calldescr) |
| 857 |
return self.emit(op) |
| 858 |
|
| 859 |
|
| 860 |
dispatch_opt = make_dispatcher_method(OptString, 'optimize_', |
| 861 |
default=OptString.emit) |
| 862 |
OptString.propagate_postprocess = make_dispatcher_method(OptString, 'postprocess_') |
| 863 |
OptString.have_postprocess_op = have_dispatcher_method(OptString, 'postprocess_') |
| 864 |
|
| 865 |
|
| 866 |
def _findall_call_oopspec(): |
| 867 |
prefix = 'opt_call_stroruni_' |
| 868 |
result = [] |
| 869 |
for name in dir(OptString): |
| 870 |
if name.startswith(prefix): |
| 871 |
value = getattr(EffectInfo, 'OS_' + name[len(prefix):]) |
| 872 |
assert is_valid_int(value) and value != 0 |
| 873 |
result.append((value, getattr(OptString, name))) |
| 874 |
return unrolling_iterable(result) |
| 875 |
opt_call_oopspec_ops = _findall_call_oopspec() |