def wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = cpu.cast_adr_to_int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif isinstance(value, float): if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = heaptracker.adr2int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif (isinstance(value, float) or longlong.is_longlong(lltype.typeOf(value))): if isinstance(value, float): value = longlong.getfloatstorage(value) else: value = rffi.cast(lltype.SignedLongLong, value) if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) elif isinstance(value, str) or isinstance(value, unicode): assert len(value) == 1 # must be a character value = ord(value) elif lltype.typeOf(value) is lltype.SingleFloat: value = longlong.singlefloat2int(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def conststr(self, str): oo = oostr(str) return history.ConstObj(ootype.cast_to_object(oo))
def get_exception_box(self, etype): return history.ConstObj(etype)
def cls_of_box(self, cpu, box): obj = box.getref(ootype.ROOT) oocls = ootype.classof(obj) return history.ConstObj(ootype.cast_to_object(oocls))
def new_ConstRef(self, x): obj = ootype.cast_to_object(x) return history.ConstObj(obj)
class OOTypeHelper(TypeSystemHelper): name = 'ootype' functionptr = staticmethod(ootype.static_meth) nullptr = staticmethod(ootype.null) cast_instance_to_base_ref = staticmethod(cast_instance_to_base_obj) BASETYPE = ootype.Object BoxRef = history.BoxObj ConstRef = history.ConstObj ConstAddr = history.ConstObj loops_done_with_this_frame_ref = None # patched by compile.py CONST_NULL = history.ConstObj(history.ConstObj.value) CVAL_NULLREF = None # patched by optimizeopt.py def get_VABLERTI(self): from pypy.rpython.ootypesystem.rvirtualizable2 import VABLERTI return VABLERTI def new_ConstRef(self, x): obj = ootype.cast_to_object(x) return history.ConstObj(obj) def get_typeptr(self, obj): return ootype.classof(obj) def get_FuncType(self, ARGS, RESULT): FUNCTYPE = ootype.StaticMethod(ARGS, RESULT) return FUNCTYPE, FUNCTYPE def get_superclass(self, TYPE): return TYPE._superclass def cast_to_instance_maybe(self, TYPE, instance): return instance cast_to_instance_maybe._annspecialcase_ = 'specialize:arg(1)' def cast_fnptr_to_root(self, fnptr): return ootype.cast_to_object(fnptr) def cls_of_box(self, cpu, box): obj = box.getref(ootype.ROOT) oocls = ootype.classof(obj) return history.ConstObj(ootype.cast_to_object(oocls)) def subclassOf(self, cpu, clsbox1, clsbox2): cls1 = clsbox1.getref(ootype.Class) cls2 = clsbox2.getref(ootype.Class) return ootype.subclassof(cls1, cls2) def get_exception_box(self, etype): return history.ConstObj(etype) def get_exc_value_box(self, evalue): return history.BoxObj(evalue) def get_exception_obj(self, evaluebox): # only works when translated obj = evaluebox.getref(ootype.ROOT) return cast_base_ptr_to_instance(Exception, obj) def cast_to_baseclass(self, value): return ootype.cast_from_object(ootype.ROOT, value) def getlength(self, array): return array.ll_length() def getarrayitem(self, array, i): return array.ll_getitem_fast(i) def setarrayitem(self, array, i, newvalue): array.ll_setitem_fast(i, newvalue) def conststr(self, str): oo = oostr(str) return history.ConstObj(ootype.cast_to_object(oo)) # A dict whose keys are refs (like the .value of BoxObj). # It is a normal dict on ootype. Two copies, to avoid conflicts # with the value type. def new_ref_dict(self): return {} def new_ref_dict_2(self): return {} def cast_vtable_to_hashable(self, cpu, obj): return ootype.cast_to_object(obj) def cast_from_ref(self, TYPE, value): return ootype.cast_from_object(TYPE, value) cast_from_ref._annspecialcase_ = 'specialize:arg(1)' def cast_to_ref(self, value): return ootype.cast_to_object(value) cast_to_ref._annspecialcase_ = 'specialize:ll' def getaddr_for_box(self, cpu, box): return box.getref_base()
def typedescr2classbox(self, descr): assert isinstance(descr, TypeDescr) return history.ConstObj( ootype.cast_to_object(ootype.runtimeClass(descr.TYPE)))