def copy_str_content(optimizer, srcbox, targetbox, srcoffsetbox, offsetbox, lengthbox, mode): if isinstance(srcbox, ConstPtr) and isinstance(srcoffsetbox, Const): M = 5 else: M = 2 if isinstance(lengthbox, ConstInt) and lengthbox.value <= M: # up to M characters are done "inline", i.e. with STRGETITEM/STRSETITEM # instead of just a COPYSTRCONTENT. for i in range(lengthbox.value): charbox = _strgetitem(optimizer, srcbox, srcoffsetbox, mode) srcoffsetbox = _int_add(optimizer, srcoffsetbox, CONST_1) optimizer.emit_operation(ResOperation(mode.STRSETITEM, [targetbox, offsetbox, charbox], None)) offsetbox = _int_add(optimizer, offsetbox, CONST_1) else: nextoffsetbox = _int_add(optimizer, offsetbox, lengthbox) op = ResOperation(mode.COPYSTRCONTENT, [srcbox, targetbox, srcoffsetbox, offsetbox, lengthbox], None) optimizer.emit_operation(op) offsetbox = nextoffsetbox return offsetbox
def _int_sub(optimizer, box1, box2): if isinstance(box2, ConstInt): if box2.value == 0: return box1 if isinstance(box1, ConstInt): return ConstInt(box1.value - box2.value) resbox = BoxInt() optimizer.emit_operation(ResOperation(rop.INT_SUB, [box1, box2], resbox)) return resbox
def string_copy_parts(self, optimizer, targetbox, offsetbox, mode): for i in range(len(self._chars)): charbox = self._chars[i].force_box() optimizer.emit_operation(ResOperation(mode.STRSETITEM, [targetbox, offsetbox, charbox], None)) offsetbox = _int_add(optimizer, offsetbox, CONST_1) return offsetbox
def string_copy_parts(self, optimizer, targetbox, offsetbox, mode): for i in range(len(self._chars)): charbox = self._chars[i].force_box() if not (isinstance(charbox, Const) and charbox.same_constant(CONST_0)): optimizer.emit_operation(ResOperation(mode.STRSETITEM, [targetbox, offsetbox, charbox], None)) offsetbox = _int_add(optimizer, offsetbox, CONST_1) return offsetbox
def _strgetitem(optimizer, strbox, indexbox, mode): if isinstance(strbox, ConstPtr) and isinstance(indexbox, ConstInt): if mode is mode_string: s = strbox.getref(lltype.Ptr(rstr.STR)) return ConstInt(ord(s.chars[indexbox.getint()])) else: s = strbox.getref(lltype.Ptr(rstr.UNICODE)) return ConstInt(ord(s.chars[indexbox.getint()])) resbox = BoxInt() optimizer.emit_operation(ResOperation(mode.STRGETITEM, [strbox, indexbox], resbox)) return resbox
def _int_add(optimizer, box1, box2): if isinstance(box1, ConstInt): if box1.value == 0: return box2 if isinstance(box2, ConstInt): return ConstInt(box1.value + box2.value) elif isinstance(box2, ConstInt) and box2.value == 0: return box1 if optimizer is None: return None resbox = BoxInt() optimizer.emit_operation(ResOperation(rop.INT_ADD, [box1, box2], resbox)) return resbox