def __init__(self, size): assert isinstance(size, r_uint) self._array = [empty_slot] * size self._array_len = size self._length = size self._head = r_uint(0) self._tail = r_uint(0)
def pickSourcePixels(self, nPixels, srcMask, dstMask, srcShiftInc, dstShiftInc): # Pick nPix pixels starting at srcBitIndex from the source, map by the # color map, and justify them according to dstBitIndex in the resulting destWord. sourceWord = r_uint(self.source.w_bits.getword(self.sourceIndex)) destWord = 0 srcShift = self.srcBitShift # put into temp for speed dstShift = self.dstBitShift nPix = nPixels # always > 0 so we can use do { } while(--nPix); if (self.w_cmLookupTable): # a little optimization for (pretty crucial) blits using indexed lookups only for px in range(nPix): sourcePix = self.rshift(r_uint(sourceWord), srcShift) & srcMask destPix = self.w_cmLookupTable.getword(intmask(sourcePix & self.cmMask)) # adjust dest pix index destWord = destWord | ((destPix & dstMask) << dstShift) # adjust source pix index dstShift += dstShiftInc srcShift += srcShiftInc if srcShift & r_uint(0xFFFFFFE0): if (self.source.msb): srcShift += 32 else: srcShift -= 32 self.sourceIndex += 1 sourceWord = self.source.w_bits.getword(self.sourceIndex) else: raise PrimitiveFailedError("Failed to pick source pixels") self.srcBitShift = srcShift # Store back return destWord
def seed(self, space, w_n=None): if w_n is None: w_n = space.newint(int(time.time())) else: if space.isinstance_w(w_n, space.w_int): w_n = space.abs(w_n) elif space.isinstance_w(w_n, space.w_long): w_n = space.abs(w_n) else: n = space.hash_w(w_n) w_n = space.wrap(r_uint(n)) key = [] w_one = space.newint(1) w_two = space.newint(2) w_thirtytwo = space.newint(32) # 0xffffffff w_masklower = space.sub(space.pow(w_two, w_thirtytwo, space.w_None), w_one) while space.is_true(w_n): w_chunk = space.and_(w_n, w_masklower) chunk = space.uint_w(w_chunk) key.append(chunk) w_n = space.rshift(w_n, w_thirtytwo) if not key: key = [r_uint(0)] self._rnd.init_by_array(key)
def test_invert(): def f(x): return ~x res = interpret(f, [3]) assert res == ~3 assert interpret(f, [r_uint(3)]) == ~r_uint(3)
def _hash_float(space, v): if not isfinite(v): if isinf(v): return HASH_INF if v > 0 else -HASH_INF return HASH_NAN m, e = math.frexp(v) sign = 1 if m < 0: sign = -1 m = -m # process 28 bits at a time; this should work well both for binary # and hexadecimal floating point. x = r_uint(0) while m: x = ((x << 28) & HASH_MODULUS) | x >> (HASH_BITS - 28) m *= 268435456.0 # 2**28 e -= 28 y = r_uint(m) # pull out integer part m -= y x += y if x >= HASH_MODULUS: x -= HASH_MODULUS # adjust for the exponent; first reduce it modulo HASH_BITS e = e % HASH_BITS if e >= 0 else HASH_BITS - 1 - ((-1 - e) % HASH_BITS) x = ((x << e) & HASH_MODULUS) | x >> (HASH_BITS - e) x = intmask(intmask(x) * sign) return -2 if x == -1 else x
def conj(self, val): assert self._cnt < r_uint(0xFFFFFFFF) if self._cnt - self.tailoff() < 32: new_tail = self._tail[:] new_tail.append(val) return PersistentVector(self._meta, self._cnt + 1, self._shift, self._root, new_tail) root = self._root assert isinstance(root, Node) tail_node = Node(root._edit, self._tail) new_shift = self._shift if (self._cnt >> 5) > (r_uint(1) << self._shift): root = self._root assert isinstance(root, Node) new_root = Node(root._edit) new_root._array[0] = self._root root = self._root assert isinstance(root, Node) new_root._array[1] = new_path(root._edit, self._shift, tail_node) new_shift += 5 else: new_root = self.push_tail(self._shift, self._root, tail_node) return PersistentVector(self._meta, self._cnt + 1, new_shift, new_root, [val])
def wrap_int(self, val): if isinstance(val, rbigint.rbigint): return self.wrap_rbigint(val) elif isinstance(val, int): return self.wrap_smallint_unsafe(val) elif isinstance(val, r_uint): if val <= r_uint(constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) else: return self.wrap_wordint_direct(val, self.w_LargePositiveInteger) elif IS_64BIT and isinstance(val, r_uint32): return self.wrap_smallint_unsafe(intmask(val)) elif isinstance(val, r_longlong) or isinstance(val, r_int64): # use '&' instead of 'and' in these checks, so we only generate 1 guard instead of two if (constants.MININT <= val) & (val <= constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) elif (0 <= val) & (val <= r_longlong(constants.U_MAXINT)): return self.wrap_wordint_direct(r_uint(val), self.w_LargePositiveInteger) elif (0 > val) & (-r_longlong(constants.U_MAXINT) <= val): return self.wrap_wordint_direct(r_uint(-val), self.w_LargeNegativeInteger) else: return self.wrap_rbigint_direct(rbigint.rbigint.fromrarith_int(val)) elif isinstance(val, r_ulonglong): if val <= r_ulonglong(constants.MAXINT): return self.wrap_smallint_unsafe(intmask(val)) elif val <= constants.U_MAXINT: return self.wrap_wordint_direct(r_uint(val), self.w_LargePositiveInteger) else: return self.wrap_rbigint_direct(rbigint.rbigint.fromrarith_int(val)) else: raise WrappingError
def min_max_acc_method(size, signed): if signed: min = -(2 ** (8*size-1)) max = (2 ** (8*size-1)) - 1 if size <= native_int_size: accept_method = 'accept_int_arg' min = int(min) max = int(max) else: accept_method = 'accept_longlong_arg' min = r_longlong(min) max = r_longlong(max) else: min = 0 max = (2 ** (8*size)) - 1 if size < native_int_size: accept_method = 'accept_int_arg' elif size == native_int_size: accept_method = 'accept_uint_arg' min = r_uint(min) max = r_uint(max) else: accept_method = 'accept_ulonglong_arg' min = r_ulonglong(min) max = r_ulonglong(max) return min, max, accept_method
def short_at0(self, space, index0): byte_index0 = index0 * 2 byte0 = ord(self.getchar(byte_index0)) byte1 = ord(self.getchar(byte_index0 + 1)) << 8 if byte1 & 0x8000 != 0: byte1 = intmask(r_uint(0xffff0000) | r_uint(byte1)) return space.wrap_int(byte1 | byte0)
def test_uintmask(self): assert rbigint.fromint(-1).uintmask() == r_uint(-1) assert rbigint.fromint(0).uintmask() == r_uint(0) assert (rbigint.fromint(sys.maxint).uintmask() == r_uint(sys.maxint)) assert (rbigint.fromlong(sys.maxint+1).uintmask() == r_uint(-sys.maxint-1))
def _int_binary_operations(): minint = -sys.maxint-1 # Test cases. Note that for each operation there should be at least # one case in which the two input arguments are equal. for opnum, testcases in [ (rop.INT_ADD, [(10, -2, 8), (-60, -60, -120)]), (rop.INT_SUB, [(10, -2, 12), (133, 133, 0)]), (rop.INT_MUL, [(-6, -3, 18), (15, 15, 225)]), (rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00), (-111, -111, -111)]), (rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0), (-111, -111, -111)]), (rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0), (-111, -111, 0)]), (rop.INT_LSHIFT, [(10, 4, 10<<4), (-5, 2, -20), (-5, 0, -5), (3, 3, 24)]), (rop.INT_RSHIFT, [(-17, 2, -5), (19, 1, 9), (3, 3, 0)]), (rop.UINT_RSHIFT, [(-1, 4, intmask(r_uint(-1) >> r_uint(4))), ( 1, 4, intmask(r_uint(1) >> r_uint(4))), ( 3, 3, 0)]), (rop.UINT_MUL_HIGH, [(5, 6, 0), (0xffff, 0xffff, 0), (-1, -1, -2), (-1, 123, 122)]), ]: for x, y, z in testcases: yield opnum, [x, y], z
def test_cast_primitive(self): def llf(u): return lltype.cast_primitive(lltype.Signed, u) res = self.interpret(llf, [r_uint(-1)], policy=LowLevelAnnotatorPolicy()) assert res == -1 res = self.interpret(llf, ['x'], policy=LowLevelAnnotatorPolicy()) assert res == ord('x') def llf(v): return lltype.cast_primitive(lltype.Unsigned, v) res = self.interpret(llf, [-1], policy=LowLevelAnnotatorPolicy()) assert res == r_uint(-1) res = self.interpret(llf, [u'x'], policy=LowLevelAnnotatorPolicy()) assert res == ord(u'x') res = self.interpret(llf, [1.0], policy=LowLevelAnnotatorPolicy()) assert res == r_uint(1) def llf(v): return lltype.cast_primitive(lltype.Char, v) res = self.interpret(llf, [ord('x')], policy=LowLevelAnnotatorPolicy()) assert res == 'x' def llf(v): return lltype.cast_primitive(lltype.UniChar, v) res = self.interpret(llf, [ord('x')], policy=LowLevelAnnotatorPolicy()) assert res == u'x' def llf(v): return lltype.cast_primitive(rffi.SHORT, v) res = self.interpret(llf, [123], policy=LowLevelAnnotatorPolicy()) assert res == 123 def llf(v): return lltype.cast_primitive(lltype.Signed, v) res = self.interpret(llf, [rffi.r_short(123)], policy=LowLevelAnnotatorPolicy()) assert res == 123
def test_display_bitmap(): # XXX: Patch SDLDisplay -> get_pixelbuffer() to circumvent # double-free bug def get_pixelbuffer(self): from rpython.rtyper.lltypesystem import lltype, rffi return lltype.malloc(rffi.ULONGP.TO, self.width * self.height * 32, flavor='raw') display.SDLDisplay.get_pixelbuffer = get_pixelbuffer d = display.SDLDisplay("test") d.set_video_mode(32, 10, 1) target = model.W_DisplayBitmap.create(space, space.w_Array, 10, 1, d) target.setword(0, r_uint(0xFF00)) assert bin(target.getword(0)) == bin(0xFF00) target.setword(0, r_uint(0x00FF00FF)) assert bin(target.getword(0)) == bin(0x00FF00FF) target.setword(0, r_uint(0xFF00FF00)) assert bin(target.getword(0)) == bin(0xFF00FF00) for i in xrange(2): assert target.pixelbuffer[i] == 0x01010101 for i in xrange(2, 4): assert target.pixelbuffer[i] == 0x0 for i in xrange(4, 6): assert target.pixelbuffer[i] == 0x01010101 for i in xrange(6, 8): assert target.pixelbuffer[i] == 0x0
def get_len_of_range(space, lo, hi, step): """ Return number of items in range/xrange (lo, hi, step). Raise ValueError if step == 0 and OverflowError if the true value is too large to fit in a signed long. """ # If lo >= hi, the range is empty. # Else if n values are in the range, the last one is # lo + (n-1)*step, which must be <= hi-1. Rearranging, # n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives # the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so # the RHS is non-negative and so truncation is the same as the # floor. Letting M be the largest positive long, the worst case # for the RHS numerator is hi=M, lo=-M-1, and then # hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough # precision to compute the RHS exactly. if step == 0: raise OperationError(space.w_ValueError, space.wrap("step argument must not be zero")) elif step < 0: lo, hi, step = hi, lo, -step if lo < hi: uhi = r_uint(hi) ulo = r_uint(lo) diff = uhi - ulo - 1 n = intmask(diff // r_uint(step) + 1) if n < 0: raise OperationError(space.w_OverflowError, space.wrap("result has too many items")) else: n = 0 return n
def ll_dict_lookup(d, key, hash): entries = d.entries ENTRIES = lltype.typeOf(entries).TO direct_compare = not hasattr(ENTRIES, 'no_direct_compare') mask = len(entries) - 1 i = r_uint(hash & mask) # do the first try before any looping if entries.valid(i): checkingkey = entries[i].key if direct_compare and checkingkey == key: return i # found the entry if d.keyeq is not None and entries.hash(i) == hash: # correct hash, maybe the key is e.g. a different pointer to # an equal object found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): # the compare did major nasty stuff to the dict: start over return ll_dict_lookup(d, key, hash) if found: return i # found the entry freeslot = -1 elif entries.everused(i): freeslot = intmask(i) else: return i | HIGHEST_BIT # pristine entry -- lookup failed # In the loop, a deleted entry (everused and not valid) is by far # (factor of 100s) the least likely outcome, so test for that last. perturb = r_uint(hash) while 1: # compute the next index using unsigned arithmetic i = (i << 2) + i + perturb + 1 i = i & mask # keep 'i' as a signed number here, to consistently pass signed # arguments to the small helper methods. if not entries.everused(i): if freeslot == -1: freeslot = intmask(i) return r_uint(freeslot) | HIGHEST_BIT elif entries.valid(i): checkingkey = entries[i].key if direct_compare and checkingkey == key: return i if d.keyeq is not None and entries.hash(i) == hash: # correct hash, maybe the key is e.g. a different pointer to # an equal object found = d.keyeq(checkingkey, key) if d.paranoia: if (entries != d.entries or not entries.valid(i) or entries[i].key != checkingkey): # the compare did major nasty stuff to the dict: # start over return ll_dict_lookup(d, key, hash) if found: return i # found the entry elif freeslot == -1: freeslot = intmask(i) perturb >>= PERTURB_SHIFT
def _find_map_attr_cache(self, selector): space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() hash_selector = objectmodel.compute_hash(selector) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_selector = cache.selectors[attr_hash] if cached_selector == selector: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(selector) cache.attrs[attr_hash] = self cache.selectors[attr_hash] = selector cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def unwrap_positive_32bit_int(self, w_value): if isinstance(w_value, model.W_SmallInteger): if w_value.value >= 0: return r_uint(w_value.value) elif isinstance(w_value, model.W_LargePositiveInteger1Word): return r_uint(w_value.value) raise UnwrappingError("Wrong types or negative SmallInteger.")
def test_large_positive_integer_1word_at_put(): target = model.W_LargePositiveInteger1Word(0) source = model.W_LargePositiveInteger1Word(-1) for i in range(constants.BYTES_PER_MACHINE_INT): target.atput0(space, i, source.at0(space, i)) assert target.at0(space, i) == source.at0(space, i) assert hex(r_uint(target.value)) == hex(r_uint(source.value))
def test_display_bitmap(): size = 10 space.display().set_video_mode(32, size, 1) target = model_display.W_MappingDisplayBitmap(space, size, 1) for idx in range(size): target.setword(idx, r_uint(0)) target.take_over_display() target.setword(0, r_uint(0xFF00)) assert bin(target.getword(0)) == bin(0xFF00) target.setword(0, r_uint(0x00FF00FF)) assert bin(target.getword(0)) == bin(0x00FF00FF) target.setword(0, r_uint(0xFF00FF00)) assert bin(target.getword(0)) == bin(0xFF00FF00) buf = target.pixelbuffer() for i in xrange(2, 8): assert buf[i] == 0x0 target.force_rectange_to_screen(0, 31, 0, 9) buf = target.pixelbuffer() for i in xrange(2): assert buf[i] == 0x01010101 for i in xrange(2, 4): assert buf[i] == 0x0 for i in xrange(4, 6): assert buf[i] == 0x01010101 for i in xrange(6, 8): assert buf[i] == 0x0
def find_map_attr(self, name, index): # attr cache space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() # unrolled hash computation for 2-tuple c1 = 0x345678 c2 = 1000003 hash_name = objectmodel.compute_hash(name) hash_selector = intmask((c2 * ((c2 * c1) ^ hash_name)) ^ index) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_name = cache.names[attr_hash] cached_index = cache.indexes[attr_hash] if cached_name == name and cached_index == index: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(name, index) cache.attrs[attr_hash] = self cache.names[attr_hash] = name cache.indexes[attr_hash] = index cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def int_signext(value, numbytes): b8 = numbytes * 8 a = r_uint(value) a += r_uint(1 << (b8 - 1)) # a += 128 a &= r_uint((1 << b8) - 1) # a &= 255 a -= r_uint(1 << (b8 - 1)) # a -= 128 return intmask(a)
def test_display_bitmap8(): size = 10 space.display().set_video_mode(32, size, 8) target = W_MappingDisplayBitmap(space, size, 8) for idx in range(size): target.setword(idx, r_uint(0)) target.take_over_display() target.setword(0, r_uint(0xFF00)) assert bin(target.getword(0)) == bin(0xFF00) target.setword(0, r_uint(0x00FF00FF)) assert bin(target.getword(0)) == bin(0x00FF00FF) target.setword(0, r_uint(0xFF00FF00)) assert bin(target.getword(0)) == bin(0xFF00FF00) buf = target._sdl_pixel_buffer # for i in xrange(2, 8): # assert buf[i] == 0xff000000 target.setword(0, r_uint(0xFF01FF01)) target.force_rectange_to_screen(0, 31, 0, 9) # now we have 1 pixels white, 1 black, 1 white, 1 black buf = target._sdl_pixel_buffer assert buf[0] == 0xffffffff assert buf[1] == 0xff000000 assert buf[2] == 0xffffffff assert buf[3] == 0xff000000
def _get_serial_type_of_int_hidden(i, file_format): MAX_6BYTE = (0x00008000 << 32) - 1 if i < 0: # test prevents: u = -(-9223372036854775808) if i < -MAX_6BYTE: return 6 u = rarithmetic.r_uint(-i) else: u = rarithmetic.r_uint(i) if u <= 127: if rffi.cast(lltype.Signed, file_format) > 4: if i == 0: return 8 elif i == 1: return 9 return 1 if u <= 32767: return 2 if u <= 8388607: return 3 if u<=2147483647: return 4 if u <= MAX_6BYTE: return 5 return 6
def test_display_bitmap4(): size = 10 space.display().set_video_mode(32, size, 4) target = W_MappingDisplayBitmap(space, size, 4) for idx in range(size): target.setword(idx, r_uint(0)) target.take_over_display() target.setword(0, r_uint(0xFF00)) assert bin(target.getword(0)) == bin(0xFF00) target.setword(0, r_uint(0x00FF00FF)) assert bin(target.getword(0)) == bin(0x00FF00FF) target.setword(0, r_uint(0xFF00FF00)) assert bin(target.getword(0)) == bin(0xFF00FF00) buf = target._sdl_pixel_buffer # for i in xrange(2, 8): # assert buf[i] == 0xff000000 target.force_rectange_to_screen(0, 31, 0, 9) # now we have 2 pixels white, 2 black, 2 white, 2 black buf = target._sdl_pixel_buffer for i in xrange(2): assert buf[i] == 0xffffffff for i in xrange(3, 4): assert buf[i] == 0xff000000 for i in xrange(5, 6): assert buf[i] == 0xffffffff for i in xrange(7, 8): assert buf[i] == 0xff000000
def read_int_until(self, delim, can_be_negative=True): i = self.consume(2) s = self.s negative = False if not s[i].isdigit(): if not can_be_negative: raise SerializerError("unexpected negative int") if s[i] != '-': raise SerializerError("bad int") negative = True i += 1 if not s[i].isdigit(): raise SerializerError("bad int") value = r_uint(ord(s[i]) - 48) self.pos = i + 1 while True: i = self.consume(1) if not s[i].isdigit(): break value = value * 10 + r_uint(ord(s[i]) - 48) if s[i] != delim: raise SerializerError("bad int") if negative: value = -value return intmask(value)
def test_display_bitmap(): size = 10 space.display().set_video_mode(32, size, 1) target = W_MappingDisplayBitmap(space, size, 1) for idx in range(size): target.setword(idx, r_uint(0)) target.take_over_display() target.setword(0, r_uint(0xFF00)) assert bin(target.getword(0)) == bin(0xFF00) target.setword(0, r_uint(0x00FF00FF)) assert bin(target.getword(0)) == bin(0x00FF00FF) target.setword(0, r_uint(0xFF00FF00)) assert bin(target.getword(0)) == bin(0xFF00FF00) buf = target._sdl_pixel_buffer for i in xrange(2, 8): assert buf[i] == 0xffffffff target.force_rectange_to_screen(0, 31, 0, 9) # now we have 8 pixels black, 8 white, 8 black, 8 white buf = target._sdl_pixel_buffer for i in xrange(8): assert buf[i] == 0xff000000 for i in xrange(9, 16): assert buf[i] == 0xffffffff for i in xrange(17, 24): assert buf[i] == 0xff000000 for i in xrange(25, 32): assert buf[i] == 0xffffffff
def compile_fn(form, ctx): form = rt.next(form) if isinstance(rt.first(form), symbol.Symbol): name = rt.first(form) form = rt.next(form) else: name = symbol.symbol(default_fn_name) if rt._satisfies_QMARK_(rt.ISeq.deref(), rt.first(form)): arities = [] while form is not nil: required_arity, argc = compile_fn_body(name, rt.first(rt.first(form)), rt.next(rt.first(form)), ctx) arities.append(argc if required_arity == -1 else required_arity | 256) form = rt.next(form) ctx.bytecode.append(code.MAKE_MULTI_ARITY) ctx.bytecode.append(r_uint(len(arities))) arities.reverse() for x in arities: ctx.bytecode.append(r_uint(x)) ctx.add_sp(1) # result ctx.sub_sp(len(arities)) else: res = compile_fn_body(name, rt.first(form), rt.next(form), ctx) if rt.meta(name) is not nil: compile_meta(rt.meta(name), ctx)
def _GetContents(self, fp): endrec = _EndRecData(fp) if not endrec: raise BadZipfile, "File is not a zip file" size_cd = endrec.stuff[5] # bytes in central directory offset_cd = endrec.stuff[6] # offset of central directory self.comment = endrec.comment x = endrec.filesize - size_cd concat = x - offset_cd self.start_dir = offset_cd + concat fp.seek(self.start_dir, 0) total = 0 while total < size_cd: centdir = fp.read(46) total = total + 46 if centdir[0:4] != stringCentralDir: raise BadZipfile, "Bad magic number for central directory" centdir = runpack(structCentralDir, centdir) filename = fp.read(centdir[_CD_FILENAME_LENGTH]) # Create ZipInfo instance to store file information x = RZipInfo(filename) x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH]) x.comment = fp.read(centdir[_CD_COMMENT_LENGTH]) total = (total + centdir[_CD_FILENAME_LENGTH] + centdir[_CD_EXTRA_FIELD_LENGTH] + centdir[_CD_COMMENT_LENGTH]) x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat # file_offset must be computed below... (x.create_version, x.create_system, x.extract_version, x.reserved, x.flag_bits, x.compress_type, t, d, crc, x.compress_size, x.file_size) = centdir[1:12] x.CRC = r_uint(crc) & r_uint(0xffffffff) x.dostime = t x.dosdate = d x.volume, x.internal_attr, x.external_attr = centdir[15:18] # Convert date/time code to (year, month, day, hour, min, sec) x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F, t>>11, (t>>5)&0x3F, (t&0x1F) * 2 ) self.filelist.append(x) self.NameToInfo[x.filename] = x for data in self.filelist: fp.seek(data.header_offset, 0) fheader = fp.read(30) if fheader[0:4] != stringFileHeader: raise BadZipfile, "Bad magic number for file header" fheader = runpack(structFileHeader, fheader) # file_offset is computed here, since the extra field for # the central directory and for the local file header # refer to different fields, and they can have different # lengths data.file_offset = (data.header_offset + 30 + fheader[_FH_FILENAME_LENGTH] + fheader[_FH_EXTRA_FIELD_LENGTH]) fname = fp.read(fheader[_FH_FILENAME_LENGTH]) if fname != data.orig_filename: raise BadZipfile, \ 'File name in directory "%s" and header "%s" differ.' % ( data.orig_filename, fname) fp.seek(self.start_dir, 0)
def add_const(self, v): for x in range(len(self.consts)): if self.consts[x] is v: return r_uint(x) idx = len(self.consts) self.consts.append(v) return r_uint(idx)
def test_WordsObject_short_at(): target = model.W_WordsObject(space, None, 2) target.setword(0, r_uint(0x00018000)) target.setword(1, r_uint(0x80010111)) assert target.short_at0(space, 0).value == intmask(0xffff8000) assert target.short_at0(space, 1).value == intmask(0x0001) assert target.short_at0(space, 2).value == intmask(0x0111) assert target.short_at0(space, 3).value == intmask(0xffff8001)
def set_position(self, new_pos): assert new_pos >= 0 self.position_and_flags &= ~FO_POSITION_MASK self.position_and_flags |= r_uint(new_pos << FO_POSITION_SHIFT)
def r_uint_val(self): return r_uint(self._int_val)
def set_state(self, t): self._state_stackptr_pc = (self._get_state_stackptr_pc() & state_mask ) | (r_uint(t.num()) << state_shift) assert self.get_state() == t
def val_at(self, key, not_found): return not_found if self._root is None else self._root.find( r_uint(0), rt.hash(key) & MASK_32, key, not_found)
py_object = object import pixie.vm.object as object from pixie.vm.object import affirm from pixie.vm.primitives import nil, true, false import pixie.vm.stdlib as proto from pixie.vm.code import extend, as_var from rpython.rlib.rarithmetic import r_int, r_uint, intmask import rpython.rlib.jit as jit import pixie.vm.rt as rt MASK_32 = r_uint(0xFFFFFFFF) NOT_FOUND = object.Object() class Box(py_object): def __init__(self): self._val = None class PersistentHashMap(object.Object): _type = object.Type(u"pixie.stdlib.PersistentHashMap") def __init__(self, cnt, root, meta=nil): self._cnt = cnt self._root = root self._meta = meta def meta(self): return self._meta
def fn(x): return r_uint(x)
def f(n, m): return _rotateLeft(r_uint(n), m)
def uint_w(self, space): intval = self.intval if intval < 0: raise oefmt(space.w_ValueError, "cannot convert negative integer to unsigned") return r_uint(intval)
def uint_w(self, w_obj): assert isinstance(w_obj, FakeLong) return rarithmetic.r_uint(w_obj.val.touint())
def crc32(s, crc=r_uint(0)): crc = ~crc & r_uint(0xffffffffL) for c in s: crc = crc_32_tab[(crc ^ r_uint(ord(c))) & 0xffL] ^ (crc >> 8) #/* Note: (crc >> 8) MUST zero fill on left return crc ^ r_uint(0xffffffffL)
def __init__(self, pos): # p is the 32-bit position shifted left by one (might be negative, # but casted to the 32-bit UINT type) p = rffi.cast(rffi.UINT, pos << FO_POSITION_SHIFT) self.position_and_flags = r_uint(p) # zero-extended to a full word
def uint_rshift(space, n, m): n = r_uint(n) x = llop.uint_rshift(lltype.Unsigned, n, m) return space.newint(intmask(x))
def push_arg(self, idx): self.bytecode.append(code.ARG) self.bytecode.append(r_uint(idx)) self.add_sp(1)
def fn(n): return float(r_uint(n)) / 2
def get_jitcounter_hash(self): from rpython.rlib.rarithmetic import r_uint return r_uint(13)
def _list_all_operations(result, operations, omit_finish=True): if omit_finish and operations[-1].getopnum() == rop.FINISH: # xxx obscure return result.extend(operations) for op in operations: if op.is_guard() and op.getdescr(): if hasattr(op.getdescr(), '_debug_suboperations'): ops = op.getdescr()._debug_suboperations _list_all_operations(result, ops, omit_finish) # ____________________________________________________________ FO_REPLACED_WITH_CONST = r_uint(1) FO_POSITION_SHIFT = 1 FO_POSITION_MASK = r_uint(0xFFFFFFFE) class FrontendOp(AbstractResOp): type = 'v' _attrs_ = ('position_and_flags', ) def __init__(self, pos): # p is the 32-bit position shifted left by one (might be negative, # but casted to the 32-bit UINT type) p = rffi.cast(rffi.UINT, pos << FO_POSITION_SHIFT) self.position_and_flags = r_uint(p) # zero-extended to a full word def get_position(self):
class AbstractResumeGuardDescr(ResumeDescr): _attrs_ = ('status', ) status = r_uint(0) ST_BUSY_FLAG = 0x01 # if set, busy tracing from the guard ST_TYPE_MASK = 0x06 # mask for the type (TY_xxx) ST_SHIFT = 3 # in "status >> ST_SHIFT" is stored: # - if TY_NONE, the jitcounter hash directly # - otherwise, the guard_value failarg index ST_SHIFT_MASK = -(1 << ST_SHIFT) TY_NONE = 0x00 TY_INT = 0x02 TY_REF = 0x04 TY_FLOAT = 0x06 def get_resumestorage(self): raise NotImplementedError("abstract base class") def handle_fail(self, deadframe, metainterp_sd, jitdriver_sd): if (self.must_compile(deadframe, metainterp_sd, jitdriver_sd) and not rstack.stack_almost_full()): self.start_compiling() try: self._trace_and_compile_from_bridge(deadframe, metainterp_sd, jitdriver_sd) finally: self.done_compiling() else: from rpython.jit.metainterp.blackhole import resume_in_blackhole if isinstance(self, ResumeGuardCopiedDescr): resume_in_blackhole(metainterp_sd, jitdriver_sd, self.prev, deadframe) else: assert isinstance(self, ResumeGuardDescr) resume_in_blackhole(metainterp_sd, jitdriver_sd, self, deadframe) assert 0, "unreachable" def _trace_and_compile_from_bridge(self, deadframe, metainterp_sd, jitdriver_sd): # 'jitdriver_sd' corresponds to the outermost one, i.e. the one # of the jit_merge_point where we started the loop, even if the # loop itself may contain temporarily recursion into other # jitdrivers. from rpython.jit.metainterp.pyjitpl import MetaInterp metainterp = MetaInterp(metainterp_sd, jitdriver_sd) metainterp.handle_guard_failure(self, deadframe) _trace_and_compile_from_bridge._dont_inline_ = True def get_jitcounter_hash(self): return self.status & self.ST_SHIFT_MASK def must_compile(self, deadframe, metainterp_sd, jitdriver_sd): jitcounter = metainterp_sd.warmrunnerdesc.jitcounter # if self.status & (self.ST_BUSY_FLAG | self.ST_TYPE_MASK) == 0: # common case: this is not a guard_value, and we are not # already busy tracing. The rest of self.status stores a # valid per-guard index in the jitcounter. hash = self.status assert hash == (self.status & self.ST_SHIFT_MASK) # # do we have the BUSY flag? If so, we're tracing right now, e.g. in an # outer invocation of the same function, so don't trace again for now. elif self.status & self.ST_BUSY_FLAG: return False # else: # we have a GUARD_VALUE that fails. from rpython.rlib.objectmodel import current_object_addr_as_int index = intmask(self.status >> self.ST_SHIFT) typetag = intmask(self.status & self.ST_TYPE_MASK) # fetch the actual value of the guard_value, possibly turning # it to an integer if typetag == self.TY_INT: intval = metainterp_sd.cpu.get_value_direct( deadframe, 'i', index) elif typetag == self.TY_REF: refval = metainterp_sd.cpu.get_value_direct( deadframe, 'r', index) intval = lltype.cast_ptr_to_int(refval) elif typetag == self.TY_FLOAT: floatval = metainterp_sd.cpu.get_value_direct( deadframe, 'f', index) intval = longlong.gethash_fast(floatval) else: assert 0, typetag if not we_are_translated(): if isinstance(intval, llmemory.AddressAsInt): intval = llmemory.cast_adr_to_int( llmemory.cast_int_to_adr(intval), "forced") hash = r_uint( current_object_addr_as_int(self) * 777767777 + intval * 1442968193) # increment = jitdriver_sd.warmstate.increment_trace_eagerness return jitcounter.tick(hash, increment) def start_compiling(self): # start tracing and compiling from this guard. self.status |= self.ST_BUSY_FLAG def done_compiling(self): # done tracing and compiling from this guard. Note that if the # bridge has not been successfully compiled, the jitcounter for # it was reset to 0 already by jitcounter.tick() and not # incremented at all as long as ST_BUSY_FLAG was set. self.status &= ~self.ST_BUSY_FLAG def compile_and_attach(self, metainterp, new_loop, orig_inputargs): # We managed to create a bridge. Attach the new operations # to the corresponding guard_op and compile from there assert metainterp.resumekey_original_loop_token is not None new_loop.original_jitcell_token = metainterp.resumekey_original_loop_token inputargs = new_loop.inputargs if not we_are_translated(): self._debug_subinputargs = new_loop.inputargs self._debug_suboperations = new_loop.operations propagate_original_jitcell_token(new_loop) send_bridge_to_backend(metainterp.jitdriver_sd, metainterp.staticdata, self, inputargs, new_loop.operations, new_loop.original_jitcell_token, metainterp.box_names_memo) record_loop_or_bridge(metainterp.staticdata, new_loop) def make_a_counter_per_value(self, guard_value_op, index): assert guard_value_op.getopnum() == rop.GUARD_VALUE box = guard_value_op.getarg(0) if box.type == history.INT: ty = self.TY_INT elif box.type == history.REF: ty = self.TY_REF elif box.type == history.FLOAT: ty = self.TY_FLOAT else: assert 0, box.type self.status = ty | (r_uint(index) << self.ST_SHIFT) def store_hash(self, metainterp_sd): if metainterp_sd.warmrunnerdesc is not None: # for tests jitcounter = metainterp_sd.warmrunnerdesc.jitcounter hash = jitcounter.fetch_next_hash() self.status = hash & self.ST_SHIFT_MASK
def compile_form(form, ctx): if form is nil: ctx.push_const(nil) return if rt.satisfies_QMARK_(rt.ISeq.deref(), form) and form is not nil: form = macroexpand(form) return compile_cons(form, ctx) if isinstance(form, numbers.Integer): ctx.push_const(form) return if isinstance(form, numbers.BigInteger): ctx.push_const(form) return if isinstance(form, numbers.Float): ctx.push_const(form) return if isinstance(form, numbers.Ratio): ctx.push_const(form) return if isinstance(form, symbol.Symbol): name = rt.name(form) loc = resolve_local(ctx, name) if loc is None: var = resolve_var(ctx, form) if var is None: var = NS_VAR.deref().intern_or_make(name) ctx.push_const(var) meta = rt.meta(form) if meta is not nil: ctx.debug_points[len( ctx.bytecode)] = rt.interpreter_code_info(meta) ctx.bytecode.append(code.DEREF_VAR) return loc.emit(ctx) return if isinstance(form, Bool) or form is nil: ctx.push_const(form) return if isinstance(form, Keyword): ctx.push_const(form) return if isinstance(form, PersistentVector): vector_var = rt.vector() size = rt.count(form) #assert rt.count(form).int_val() == 0 ctx.push_const(code.intern_var(u"pixie.stdlib", u"vector")) for x in range(size): compile_form(rt.nth(form, rt.wrap(x)), ctx) ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size + 1)) ctx.sub_sp(size) compile_meta(rt.meta(form), ctx) return if isinstance(form, PersistentHashSet): compile_set_literal(form, ctx) return if rt.satisfies_QMARK_(rt.IMap.deref(), form): compile_map_literal(form, ctx) return if isinstance(form, String): ctx.push_const(form) return if isinstance(form, Character): ctx.push_const(form) return raise Exception("Can't compile ")
def bit_count(i): assert isinstance(i, r_uint) i = i - ((i >> 1) & r_uint(0x55555555)) i = (i & r_uint(0x33333333)) + ((i >> 2) & r_uint(0x33333333)) return (((i + (i >> 4) & r_uint(0xF0F0F0F)) * r_uint(0x1010101)) & r_uint(0xffffffff)) >> 24
def __init__(self, idx): self.idx = r_uint(idx)
def store_pc(self, newpc): assert newpc >= 0, "trying to store pc < 0" assert newpc <= returned_pc, "trying to store pc > returned_pc" self._state_stackptr_pc = (self._get_state_stackptr_pc() & pc_mask) | (r_uint(newpc) << pc_shift) assert self.pc() == newpc
def emit(self, ctx): ctx.bytecode.append(code.DUP_NTH) assert 0 <= ctx.sp() - self.sp < 100000 ctx.bytecode.append(r_uint(ctx.sp() - self.sp)) ctx.add_sp(1)
'_s_fallback', # From block-context '_w_home', '_initialip', '_eargc' ] def __init__(self): self.instances_w = None self._s_fallback = None self._w_home = None self._initialip = 0 self._eargc = 0 state_mask = r_uint(0b00111111111111111111111111111111) stackptr_mask = r_uint(0b11000000001111111111111111111111) pc_mask = r_uint(0b11111111110000000000000000000000) returned_pc = 0b00000000001111111111111111111111 state_shift = 30 stackptr_shift = 22 pc_shift = 0 class ContextPartShadow(AbstractStrategy): """ This Shadow handles the entire object storage on its own, ignoring the _storage field in W_PointersObject. The w_self parameter in fetch/store/size etc. is ignored, and the own_fetch/own_store/own_size methods from ShadowMixin should be used instead. This shadow can exist without a W_PointersObject. In order to integrate well with the RPython toolchain (virtualizables and jit), this
def mark(self, lbl): self.bytecode[lbl] = r_uint(len(self.bytecode)) - lbl
# ~~~ NOTE ~~~ # The exact value returned by a function is NOT DEFINED. The returned value is # IGNORED EVERYWHERE in RPython and the question "was_an_exception_raised()" # is implemented by checking the content of the global exc_data. # # The only case in which the returned error value is significant is for # llhelpers which can be called by 3rd-party C functions, such as e.g. the HPy # API. In that case, the error value is specified by @llhelper_error_value. # # The following table defines the default error values to return, but in # general the returned value is not guaranteed. The only case in which it is # guaranteed is for functions decorated with @llhelper_error_value PrimitiveErrorValue = { lltype.Signed: -1, lltype.Unsigned: r_uint(-1), lltype.SignedLongLong: r_longlong(-1), lltype.UnsignedLongLong: r_ulonglong(-1), lltype.Float: -1.0, lltype.SingleFloat: r_singlefloat(-1.0), lltype.LongFloat: r_longfloat(-1.0), lltype.Char: chr(255), lltype.UniChar: unichr(0xFFFF), # XXX is this always right? lltype.Bool: True, llmemory.Address: llmemory.NULL, lltype.Void: None } for TYPE in rffi.NUMBER_TYPES: PrimitiveErrorValue[TYPE] = lltype.cast_primitive(TYPE, -1) del TYPE
def label(self): lbl = len(self.bytecode) self.bytecode.append(r_uint(99)) return lbl
def debug_bridge(descr_number, rawstart, codeendpos): debug_start("jit-backend-addr") debug_print("bridge out of Guard 0x%x has address 0x%x to 0x%x" % (r_uint(descr_number), r_uint(rawstart), r_uint(rawstart + codeendpos))) debug_stop("jit-backend-addr")
def wrap_uid(space, uid): if uid <= r_uint(sys.maxint): return space.newint(intmask(uid)) else: return space.newint(uid) # an unsigned number
def read_uint_from_env(varname): value, factor = _read_float_and_factor_from_env(varname) return r_uint(value * factor)
def setup_once(self): # the address of the function called by 'new' gc_ll_descr = self.cpu.gc_ll_descr gc_ll_descr.initialize() if hasattr(gc_ll_descr, 'minimal_size_in_nursery'): self.gc_minimal_size_in_nursery = gc_ll_descr.minimal_size_in_nursery else: self.gc_minimal_size_in_nursery = 0 if hasattr(gc_ll_descr, 'gcheaderbuilder'): self.gc_size_of_header = gc_ll_descr.gcheaderbuilder.size_gc_header else: self.gc_size_of_header = WORD # for tests self.memcpy_addr = rffi.cast(lltype.Signed, memcpy_fn) self.memset_addr = rffi.cast(lltype.Signed, memset_fn) self._build_failure_recovery(False, withfloats=False) self._build_failure_recovery(True, withfloats=False) self._build_wb_slowpath(False) self._build_wb_slowpath(True) self._build_wb_slowpath(False, for_frame=True) # only one of those self.build_frame_realloc_slowpath() if self.cpu.supports_floats: self._build_failure_recovery(False, withfloats=True) self._build_failure_recovery(True, withfloats=True) self._build_wb_slowpath(False, withfloats=True) self._build_wb_slowpath(True, withfloats=True) self._build_propagate_exception_path() if gc_ll_descr.get_malloc_slowpath_addr is not None: # generate few slowpaths for various cases self.malloc_slowpath = self._build_malloc_slowpath(kind='fixed') self.malloc_slowpath_varsize = self._build_malloc_slowpath( kind='var') if hasattr(gc_ll_descr, 'malloc_str'): self.malloc_slowpath_str = self._build_malloc_slowpath(kind='str') else: self.malloc_slowpath_str = None if hasattr(gc_ll_descr, 'malloc_unicode'): self.malloc_slowpath_unicode = self._build_malloc_slowpath( kind='unicode') else: self.malloc_slowpath_unicode = None lst = [0, 0, 0, 0] lst[0] = self._build_cond_call_slowpath(False, False) lst[1] = self._build_cond_call_slowpath(False, True) if self.cpu.supports_floats: lst[2] = self._build_cond_call_slowpath(True, False) lst[3] = self._build_cond_call_slowpath(True, True) self.cond_call_slowpath = lst self._build_stack_check_slowpath() self._build_release_gil(gc_ll_descr.gcrootmap) if not self._debug: # if self._debug is already set it means that someone called # set_debug by hand before initializing the assembler. Leave it # as it is self.set_debug(have_debug_prints_for('jit-backend-counts')) # when finishing, we only have one value at [0], the rest dies self.gcmap_for_finish = lltype.malloc(jitframe.GCMAP, 1, flavor='raw', track_allocation=False) self.gcmap_for_finish[0] = r_uint(1)