def f(x): x = cast_primitive(UnsignedLongLong, x) x <<= 60 x /= 3 x <<= 1 x = cast_primitive(SignedLongLong, x) x >>= 32 return cast_primitive(Signed, x)
def revealconst(self, T): if isinstance(T, lltype.Ptr): return lltype.cast_int_to_ptr(T, self.get_integer_value()) elif T is llmemory.Address: return llmemory.cast_int_to_adr(self.get_integer_value()) else: return lltype.cast_primitive(T, self.get_integer_value())
def boxresult(RESULT, result): if isinstance(RESULT, ootype.OOType): return history.BoxObj(ootype.cast_to_object(result)) elif RESULT is lltype.Float: return history.BoxFloat(result) else: return history.BoxInt(lltype.cast_primitive(ootype.Signed, result))
def convert_const(self, value): if isinstance(value, objectmodel.Symbolic): return value T = typeOf(value) if isinstance(T, Number) or T is Bool: return cast_primitive(self.lowleveltype, value) raise TyperError("not an integer: %r" % (value, ))
def convert_const(self, value): if isinstance(value, objectmodel.Symbolic): return value T = typeOf(value) if isinstance(T, Number) or T is Bool: return cast_primitive(self.lowleveltype, value) raise TyperError("not an integer: %r" % (value,))
def cast_whatever_to_int(T, value): if isinstance(T, lltype.Ptr): return lltype.cast_ptr_to_int(value) elif T is llmemory.Address: return llmemory.cast_adr_to_int(value) else: return lltype.cast_primitive(lltype.Signed, value)
def cast_int_to_whatever(T, value): if isinstance(T, lltype.Ptr): return lltype.cast_int_to_ptr(T, value) elif T is llmemory.Address: return llmemory.cast_int_to_adr(value) else: return lltype.cast_primitive(T, value)
def ll_array_inplace_binop(ITEM, it0, it1, binop): ll_assert(it0.size == it1.size, "it0.size == it1.size") while it0.index < it0.size: it0.dataptr[0] = cast_primitive(ITEM, binop(it0.dataptr[0], it1.dataptr[0])) it0.ll_next() it1.ll_next()
def ll_array_set(ITEM, it0, it1): if it0.size == 0: return # empty LHS.. ll_assert(it0.size == it1.size, "it0.size == it1.size") while it0.index < it0.size: it0.dataptr[0] = cast_primitive(ITEM, it1.dataptr[0]) it0.ll_next() it1.ll_next()
def os_open_lltypeimpl(path, flags, mode): l_path = rffi.str2charp(path) mode = lltype.cast_primitive(mode_t, mode) result = os_open(l_path, flags, mode) rffi.free_charp(l_path) if result == -1: raise OSError(rffi.c_errno, "os_open failed") return result
def ll_str2unicode(str): lgt = len(str.chars) s = mallocunicode(lgt) for i in range(lgt): if ord(str.chars[i]) > 127: raise UnicodeDecodeError s.chars[i] = cast_primitive(UniChar, str.chars[i]) return s
def ll_decode_latin1(self, value): sb = ootype.new(ootype.UnicodeBuilder) length = value.ll_strlen() sb.ll_allocate(length) for i in range(length): c = value.ll_stritem_nonneg(i) sb.ll_append_char(cast_primitive(UniChar, c)) return sb.ll_build()
def ll_encode_latin1(self, s): length = len(s.chars) result = mallocstr(length) for i in range(length): c = s.chars[i] if ord(c) > 255: raise UnicodeEncodeError("character not in latin1 range") result.chars[i] = cast_primitive(Char, c) return result
def ll_encode_latin1(self, value): sb = ootype.new(ootype.StringBuilder) length = value.ll_strlen() sb.ll_allocate(length) for i in range(length): c = value.ll_stritem_nonneg(i) if ord(c) > 255: raise UnicodeEncodeError("%d > 255, not latin-1" % ord(c)) sb.ll_append_char(cast_primitive(Char, c)) return sb.ll_build()
def ll_str(self, value): sb = ootype.new(ootype.StringBuilder) lgt = value.ll_strlen() sb.ll_allocate(lgt) for i in range(lgt): c = value.ll_stritem_nonneg(i) if ord(c) > 127: raise UnicodeEncodeError("%d > 127, not ascii" % ord(c)) sb.ll_append_char(cast_primitive(Char, c)) return sb.ll_build()
def unwrap(TYPE, box): if TYPE is lltype.Void: return None if isinstance(TYPE, lltype.Ptr): return box.getref(TYPE) if isinstance(TYPE, ootype.OOType): return box.getref(TYPE) if TYPE == lltype.Float: return box.getfloat() else: return lltype.cast_primitive(TYPE, box.getint())
def ll_str(self, s): # XXX crazy that this is here, but I don't want to break # rmodel logic lgt = len(s.chars) result = mallocstr(lgt) for i in range(lgt): c = s.chars[i] if ord(c) > 127: raise UnicodeEncodeError("character not in ascii range") result.chars[i] = cast_primitive(Char, c) return result
def set_future_value(cpu, j, value, typecode): if typecode == 'ref': refvalue = cpu.ts.cast_to_ref(value) cpu.set_future_value_ref(j, refvalue) elif typecode == 'int': intvalue = lltype.cast_primitive(lltype.Signed, value) cpu.set_future_value_int(j, intvalue) elif typecode == 'float': assert isinstance(value, float) cpu.set_future_value_float(j, value) else: assert False
def get_string(self, builder, r): current = getattr(builder, self.builder_cache) if current and r.random() < .8: v_string = r.choice(current) string = v_string.getref(self.ptr) else: string = self.alloc(builder.get_index(500, r).getint()) v_string = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, string)) current.append(v_string) for i in range(len(string.chars)): char = r.random_integer() % self.max string.chars[i] = lltype.cast_primitive(self.primitive, char) return v_string
def genconst(self, llvalue): T = lltype.typeOf(llvalue) if T is llmemory.Address: return AddrConst(llvalue) elif isinstance(T, lltype.Primitive): return IntConst(lltype.cast_primitive(lltype.Signed, llvalue)) elif isinstance(T, lltype.Ptr): lladdr = llmemory.cast_ptr_to_adr(llvalue) if T.TO._gckind == 'gc': self.keepalive_gc_refs.append(lltype.cast_opaque_ptr(llmemory.GCREF, llvalue)) return AddrConst(lladdr) else: assert 0, "XXX not implemented"
def _new(x, cpu): "NOT_RPYTHON" kind = getkind(lltype.typeOf(x)) if kind == "int": intval = lltype.cast_primitive(lltype.Signed, x) return BoxInt(intval) elif kind == "ref": # XXX add ootype support? ptrval = lltype.cast_opaque_ptr(llmemory.GCREF, x) return BoxPtr(ptrval) elif kind == "float": return BoxFloat(x) else: raise NotImplementedError(kind)
def unspecialize_value(value): """Casts 'value' to a Signed, a GCREF or a FLOATSTORAGE.""" if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': return lltype.cast_opaque_ptr(llmemory.GCREF, value) else: adr = llmemory.cast_ptr_to_adr(value) return heaptracker.adr2int(adr) elif isinstance(lltype.typeOf(value), ootype.OOType): return ootype.cast_to_object(value) elif isinstance(value, float): return longlong.getfloatstorage(value) else: return lltype.cast_primitive(lltype.Signed, value)
def unwrap(TYPE, box): if TYPE is lltype.Void: return None if isinstance(TYPE, lltype.Ptr): if TYPE.TO._gckind == "gc": return box.getref(TYPE) else: return llmemory.cast_adr_to_ptr(box.getaddr(), TYPE) if isinstance(TYPE, ootype.OOType): return box.getref(TYPE) if TYPE == lltype.Float: return box.getfloat() else: return lltype.cast_primitive(TYPE, box.getint())
def genconst(self, llvalue): T = lltype.typeOf(llvalue) if T is llmemory.Address: return AddrConst(llvalue) elif T is lltype.Bool: return BoolConst(lltype.cast_primitive(lltype.Bool, llvalue)) elif T is lltype.Char: return CharConst(lltype.cast_primitive(lltype.Char, llvalue)) elif T is lltype.Unsigned: return UIntConst(lltype.cast_primitive(lltype.Unsigned, llvalue)) elif T is lltype.Float: return FloatConst(lltype.cast_primitive(lltype.Float, llvalue)) elif isinstance(T, lltype.Primitive): return IntConst(lltype.cast_primitive(lltype.Signed, llvalue)) elif isinstance(T, lltype.Ptr): lladdr = llmemory.cast_ptr_to_adr(llvalue) #if T.TO._gckind == 'gc': # self.keepalive_gc_refs.append(lltype.cast_opaque_ptr(llmemory.GCREF, llvalue)) return AddrConst(lladdr) else: msg = 'XXX not implemented' logger.dump(msg) assert 0, msg
def ll_join_chars(length_dummy, lst, RES): if RES is ootype.String: target = Char buf = ootype.new(ootype.StringBuilder) else: target = UniChar buf = ootype.new(ootype.UnicodeBuilder) length = lst.ll_length() buf.ll_allocate(length) i = 0 while i < length: buf.ll_append_char(cast_primitive(target, lst.ll_getitem_fast(i))) i += 1 return buf.ll_build()
def _generalcast(T, value): if isinstance(T, lltype.Ptr): return lltype.cast_pointer(T, value) elif T == llmemory.Address: return llmemory.cast_ptr_to_adr(value) else: T1 = lltype.typeOf(value) if T1 is llmemory.Address: value = llmemory.cast_adr_to_int(value) elif isinstance(T1, lltype.Ptr): value = lltype.cast_ptr_to_int(value) else: value = value return lltype.cast_primitive(T, value)
def PyMember_GetOne(space, obj, w_member): addr = rffi.cast(ADDR, obj) addr += w_member.c_offset member_type = rffi.cast(lltype.Signed, w_member.c_type) for converter in integer_converters: typ, lltyp, _ = converter if typ == member_type: result = rffi.cast(rffi.CArrayPtr(lltyp), addr) if lltyp is rffi.FLOAT: w_result = space.wrap(lltype.cast_primitive(lltype.Float, result[0])) elif typ == T_BOOL: x = rffi.cast(lltype.Signed, result[0]) w_result = space.wrap(x != 0) else: w_result = space.wrap(result[0]) return w_result if member_type == T_STRING: result = rffi.cast(rffi.CCHARPP, addr) if result[0]: w_result = PyString_FromString(space, result[0]) else: w_result = space.w_None elif member_type == T_STRING_INPLACE: result = rffi.cast(rffi.CCHARP, addr) w_result = PyString_FromString(space, result) elif member_type == T_CHAR: result = rffi.cast(rffi.CCHARP, addr) w_result = space.wrap(result[0]) elif member_type == T_OBJECT: obj_ptr = rffi.cast(PyObjectP, addr) if obj_ptr[0]: w_result = from_ref(space, obj_ptr[0]) else: w_result = space.w_None elif member_type == T_OBJECT_EX: obj_ptr = rffi.cast(PyObjectP, addr) if obj_ptr[0]: w_result = from_ref(space, obj_ptr[0]) else: w_name = space.wrap(rffi.charp2str(w_member.c_name)) raise OperationError(space.w_AttributeError, w_name) else: raise OperationError(space.w_SystemError, space.wrap("bad memberdescr type")) return w_result
def PyMember_GetOne(space, obj, w_member): addr = rffi.cast(ADDR, obj) addr += w_member.c_offset member_type = rffi.cast(lltype.Signed, w_member.c_type) for converter in integer_converters: typ, lltyp, _ = converter if typ == member_type: result = rffi.cast(rffi.CArrayPtr(lltyp), addr) if lltyp is rffi.FLOAT: w_result = space.wrap( lltype.cast_primitive(lltype.Float, result[0])) elif typ == T_BOOL: x = rffi.cast(lltype.Signed, result[0]) w_result = space.wrap(x != 0) else: w_result = space.wrap(result[0]) return w_result if member_type == T_STRING: result = rffi.cast(rffi.CCHARPP, addr) if result[0]: w_result = PyString_FromString(space, result[0]) else: w_result = space.w_None elif member_type == T_STRING_INPLACE: result = rffi.cast(rffi.CCHARP, addr) w_result = PyString_FromString(space, result) elif member_type == T_CHAR: result = rffi.cast(rffi.CCHARP, addr) w_result = space.wrap(result[0]) elif member_type == T_OBJECT: obj_ptr = rffi.cast(PyObjectP, addr) if obj_ptr[0]: w_result = from_ref(space, obj_ptr[0]) else: w_result = space.w_None elif member_type == T_OBJECT_EX: obj_ptr = rffi.cast(PyObjectP, addr) if obj_ptr[0]: w_result = from_ref(space, obj_ptr[0]) else: w_name = space.wrap(rffi.charp2str(w_member.c_name)) raise OperationError(space.w_AttributeError, w_name) else: raise OperationError(space.w_SystemError, space.wrap("bad memberdescr type")) return w_result
def _new(x): "NOT_RPYTHON" T = lltype.typeOf(x) kind = getkind(T) if kind == "int": if isinstance(T, lltype.Ptr): intval = heaptracker.adr2int(llmemory.cast_ptr_to_adr(x)) else: intval = lltype.cast_primitive(lltype.Signed, x) return ConstInt(intval) elif kind == "ref": return cpu.ts.new_ConstRef(x) elif kind == "float": return ConstFloat(longlong.getfloatstorage(x)) else: raise NotImplementedError(kind)
def convert_const(self, value): if value is None: return nullptr(self.lowleveltype.TO) #value = getattr(value, '__self__', value) # for bound string methods if not isinstance(value, self.basetype): raise TyperError("not a str: %r" % (value, )) try: return self.CACHE[value] except KeyError: p = self.malloc(len(value)) for i in range(len(value)): p.chars[i] = cast_primitive(self.base, value[i]) p.hash = 0 self.ll.ll_strhash(p) # precompute the hash self.CACHE[value] = p return p
def specialize_value(TYPE, x): """'x' must be a Signed, a GCREF or a FLOATSTORAGE. This function casts it to a more specialized type, like Char or Ptr(..). """ INPUT = lltype.typeOf(x) if INPUT is lltype.Signed: if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'raw': # non-gc pointer return rffi.cast(TYPE, x) else: return lltype.cast_primitive(TYPE, x) elif INPUT is longlong.FLOATSTORAGE: assert TYPE is lltype.Float return longlong.getrealfloat(x) else: return lltype.cast_opaque_ptr(TYPE, x)
def set_future_value(cpu, j, value, typecode): if typecode == 'ref': refvalue = cpu.ts.cast_to_ref(value) cpu.set_future_value_ref(j, refvalue) elif typecode == 'int': intvalue = lltype.cast_primitive(lltype.Signed, value) cpu.set_future_value_int(j, intvalue) elif typecode == 'float': if lltype.typeOf(value) is lltype.Float: value = longlong.getfloatstorage(value) else: assert longlong.is_longlong(lltype.typeOf(value)) value = rffi.cast(lltype.SignedLongLong, value) cpu.set_future_value_float(j, value) else: assert False
def convert_const(self, value): if value is None: return nullptr(self.lowleveltype.TO) #value = getattr(value, '__self__', value) # for bound string methods if not isinstance(value, self.basetype): raise TyperError("not a str: %r" % (value,)) try: return self.CACHE[value] except KeyError: p = self.malloc(len(value)) for i in range(len(value)): p.chars[i] = cast_primitive(self.base, value[i]) p.hash = 0 self.ll.ll_strhash(p) # precompute the hash self.CACHE[value] = p return p
def emit_const(self, const, kind, allow_short=False): value = const.value if kind == 'int': TYPE = const.concretetype if isinstance(TYPE, lltype.Ptr): assert TYPE.TO._gckind == 'raw' self.see_raw_object(value) value = llmemory.cast_ptr_to_adr(value) TYPE = llmemory.Address if TYPE == llmemory.Address: value = heaptracker.adr2int(value) if TYPE is lltype.SingleFloat: value = longlong.singlefloat2int(value) if not isinstance(value, (llmemory.AddressAsInt, ComputedIntSymbolic)): value = lltype.cast_primitive(lltype.Signed, value) if allow_short: try: short_num = -128 <= value <= 127 except TypeError: # "Symbolics cannot be compared!" short_num = False if short_num: # emit the constant as a small integer self.code.append(chr(value & 0xFF)) return True constants = self.constants_i elif kind == 'ref': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) constants = self.constants_r elif kind == 'float': if const.concretetype == lltype.Float: value = longlong.getfloatstorage(value) else: assert longlong.is_longlong(const.concretetype) value = rffi.cast(lltype.SignedLongLong, value) constants = self.constants_f else: raise AssemblerError('unimplemented %r in %r' % (const, self.ssareprname)) key = (kind, Constant(value)) if key not in self.constants_dict: constants.append(value) self.constants_dict[key] = 256 - len(constants) # emit the constant normally, as one byte that is an index in the # list of constants self.code.append(chr(self.constants_dict[key])) return False
def ll_join_chars(length, chars, RES): # no need to optimize this, will be replaced by string builder # at some point soon num_chars = length if RES is StringRepr.lowleveltype: target = Char malloc = mallocstr else: target = UniChar malloc = mallocunicode result = malloc(num_chars) res_chars = result.chars i = 0 while i < num_chars: res_chars[i] = cast_primitive(target, chars[i]) i += 1 return result
def _generalcast(T, value): if lltype.typeOf(value) == T: return value elif isinstance(T, lltype.Ptr): return lltype.cast_pointer(T, value) elif T == llmemory.Address: return llmemory.cast_ptr_to_adr(value) elif isinstance(T, ootype.StaticMethod): fn = value._obj return ootype._static_meth(T, graph=fn.graph, _callable=fn._callable) else: T1 = lltype.typeOf(value) if T1 is llmemory.Address: value = llmemory.cast_adr_to_int(value) elif isinstance(T1, lltype.Ptr): value = lltype.cast_ptr_to_int(value) else: value = value return lltype.cast_primitive(T, value)
def specialize_value(TYPE, x): """'x' must be a Signed, a GCREF or a FLOATSTORAGE. This function casts it to a more specialized type, like Char or Ptr(..). """ INPUT = lltype.typeOf(x) if INPUT is lltype.Signed: if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'raw': # non-gc pointer return rffi.cast(TYPE, x) elif TYPE is lltype.SingleFloat: return longlong.int2singlefloat(x) else: return lltype.cast_primitive(TYPE, x) elif INPUT is longlong.FLOATSTORAGE: if longlong.is_longlong(TYPE): return rffi.cast(TYPE, x) assert TYPE is lltype.Float return longlong.getrealfloat(x) else: return lltype.cast_opaque_ptr(TYPE, x)