def f(): s1 = lltype.malloc(S) llop.keepalive(lltype.Void, s1) s2 = lltype.malloc(S) llop.keepalive(lltype.Void, s1) llop.keepalive(lltype.Void, s2) return lltype.cast_ptr_to_int(s1) + lltype.cast_ptr_to_int(s2)
def f(): s1 = lltype.malloc(S) llop.keepalive(lltype.Void, s1) s2 = lltype.malloc(S) llop.keepalive(lltype.Void, s1) llop.keepalive(lltype.Void, s2) return lltype.cast_ptr_to_int(s1) + lltype.cast_ptr_to_int(s2)
def ll_unboxed_isinstance_const(obj, minid, maxid, answer_if_unboxed): if not obj: return False if lltype.cast_ptr_to_int(obj) & 1: return answer_if_unboxed else: return ll_issubclass_const(obj.typeptr, minid, maxid)
def test_instantiate(): def fn1(n): return C(n) res = interpret(fn1, [42]) value = lltype.cast_ptr_to_int(res) assert value == 42 * 2 + 1 # for now
def ll_unboxed_isinstance_const(obj, minid, maxid, answer_if_unboxed): if not obj: return False if lltype.cast_ptr_to_int(obj) & 1: return answer_if_unboxed else: return ll_issubclass_const(obj.typeptr, minid, maxid)
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 ll_str(self, i): if lltype.cast_ptr_to_int(i) & 1: from pypy.rpython.lltypesystem import rstr from pypy.rpython.rint import signed_repr llstr1 = signed_repr.ll_str(ll_unboxed_to_int(i)) return rstr.ll_strconcat(rstr.unboxed_instance_str_prefix, rstr.ll_strconcat(llstr1, rstr.unboxed_instance_str_suffix)) else: return InstanceRepr.ll_str(self, i)
def ll_str(self, i): if lltype.cast_ptr_to_int(i) & 1: from pypy.rpython.lltypesystem import rstr from pypy.rpython.rint import signed_repr llstr1 = signed_repr.ll_str(ll_unboxed_to_int(i)) return rstr.ll_strconcat( rstr.unboxed_instance_str_prefix, rstr.ll_strconcat(llstr1, rstr.unboxed_instance_str_suffix)) else: return InstanceRepr.ll_str(self, i)
def ll_inst_hash(ins): if not ins: return 0 # for None cached = ins.hash_cache if cached == 0: # XXX this should ideally be done in a GC-dependent way: we only # need a hash_cache for moving GCs, and we only need the '~' to # avoid Boehm keeping the object alive if the value is passed # around cached = ins.hash_cache = ~cast_ptr_to_int(ins) return cached
def ll_inst_hash(ins): if not ins: return 0 # for None cached = ins.hash_cache if cached == 0: # XXX this should ideally be done in a GC-dependent way: we only # need a hash_cache for moving GCs, and we only need the '~' to # avoid Boehm keeping the object alive if the value is passed # around cached = ins.hash_cache = ~cast_ptr_to_int(ins) return cached
def tigetstr_llimpl(cap): check_setup_invoked() ll_cap = rffi.str2charp(cap) try: ll_res = c_tigetstr(ll_cap) num = lltype.cast_ptr_to_int(ll_res) if num == 0 or num == -1: raise interp_curses.TermError() res = rffi.charp2str(ll_res) return res finally: rffi.free_charp(ll_cap)
def tigetstr_llimpl(cap): check_setup_invoked() ll_cap = rffi.str2charp(cap) try: ll_res = c_tigetstr(ll_cap) num = lltype.cast_ptr_to_int(ll_res) if num == 0 or num == -1: raise interp_curses.TermError() res = rffi.charp2str(ll_res) return res finally: rffi.free_charp(ll_cap)
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 add_case(block, exitcase): block = from_opaque_object(block) exitcase = from_opaque_object(exitcase) assert isinstance(exitcase, flowmodel.Constant) assert isinstance(block.exitswitch, flowmodel.Variable) case_link = flowmodel.Link([], None) exitvalue = exitcase.value if isinstance(lltype.typeOf(exitvalue), lltype.Ptr): # XXX hack! exitvalue = lltype.cast_ptr_to_int(exitvalue) case_link.exitcase = exitvalue case_link.llexitcase = exitvalue if block.exits and block.exits[-1].exitcase == 'default': exits = block.exits[:-1] + (case_link,) + block.exits[-1:] else: exits = block.exits + (case_link,) block.recloseblock(*exits) return to_opaque_object(case_link)
def add_case(block, exitcase): block = _from_opaque(block) exitcase = _from_opaque(exitcase) assert isinstance(exitcase, flowmodel.Constant) assert isinstance(block.exitswitch, flowmodel.Variable) case_link = flowmodel.Link([], None) exitvalue = exitcase.value if isinstance(lltype.typeOf(exitvalue), lltype.Ptr): # XXX hack! exitvalue = lltype.cast_ptr_to_int(exitvalue) case_link.exitcase = exitvalue case_link.llexitcase = exitvalue if block.exits and block.exits[-1].exitcase == 'default': exits = block.exits[:-1] + (case_link, ) + block.exits[-1:] else: exits = block.exits + (case_link, ) block.recloseblock(*exits) return _to_opaque(case_link)
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 _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 ll_str(self, i): # doesn't work for non-gc classes! from pypy.rpython.lltypesystem import rstr from pypy.rpython.lltypesystem.ll_str import ll_int2hex from pypy.rlib.rarithmetic import r_uint if not i: return rstr.null_str instance = cast_pointer(OBJECTPTR, i) uid = r_uint(cast_ptr_to_int(i)) nameLen = len(instance.typeptr.name) nameString = rstr.mallocstr(nameLen-1) i = 0 while i < nameLen - 1: nameString.chars[i] = instance.typeptr.name[i] i += 1 res = rstr.instance_str_prefix res = rstr.ll_strconcat(res, nameString) res = rstr.ll_strconcat(res, rstr.instance_str_infix) res = rstr.ll_strconcat(res, ll_int2hex(uid, False)) res = rstr.ll_strconcat(res, rstr.instance_str_suffix) return res
def ll_str(self, i): # doesn't work for non-gc classes! from pypy.rpython.lltypesystem import rstr from pypy.rpython.lltypesystem.ll_str import ll_int2hex from pypy.rlib.rarithmetic import r_uint if not i: return rstr.null_str instance = cast_pointer(OBJECTPTR, i) uid = r_uint(cast_ptr_to_int(i)) nameLen = len(instance.typeptr.name) nameString = rstr.mallocstr(nameLen - 1) i = 0 while i < nameLen - 1: nameString.chars[i] = instance.typeptr.name[i] i += 1 res = rstr.instance_str_prefix res = rstr.ll_strconcat(res, nameString) res = rstr.ll_strconcat(res, rstr.instance_str_infix) res = rstr.ll_strconcat(res, ll_int2hex(uid, False)) res = rstr.ll_strconcat(res, rstr.instance_str_suffix) return res
def gc_malloc_fnaddr(): """Returns the address of the Boehm 'malloc' function.""" if we_are_translated(): gc_malloc_ptr = llhelper(GC_MALLOC, gc_malloc) return lltype.cast_ptr_to_int(gc_malloc_ptr) else: # <pedronis> don't do this at home import threading if not isinstance(threading.currentThread(), threading._MainThread): import py py.test.skip("must run in the main thread") try: from ctypes import cast, c_void_p, util path = util.find_library('gc') if path is None: raise ImportError("Boehm (libgc) not found") boehmlib = ctypes.cdll.LoadLibrary(path) except ImportError, e: import py py.test.skip(str(e)) else:
def test_gcref_casts(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert lltype.cast_ptr_to_int(ref0) == 0 assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL NODE = lltype.GcStruct('NODE') assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE) node = lltype.malloc(NODE) ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node) intval = rffi.cast(lltype.Signed, node) intval1 = rffi.cast(lltype.Signed, ref1) assert intval == intval1 ref2 = ctypes2lltype(llmemory.GCREF, intval1) assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
def test_gcref_casts(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert lltype.cast_ptr_to_int(ref0) == 0 assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL NODE = lltype.GcStruct('NODE') assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE) node = lltype.malloc(NODE) ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node) intval = rffi.cast(lltype.Signed, node) intval1 = rffi.cast(lltype.Signed, ref1) assert intval == intval1 ref2 = ctypes2lltype(llmemory.GCREF, intval1) assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
def ll_unboxed_getclass(instance, class_if_unboxed): if lltype.cast_ptr_to_int(instance) & 1: return class_if_unboxed return instance.typeptr
def f(p): n = lltype.cast_ptr_to_int(p) return n
def f(): p = lltype.malloc(GCS1) return lltype.cast_ptr_to_int(p)
def ll_unboxed_to_int(p): return lltype.cast_ptr_to_int(p) >> 1
def g(p): return lltype.cast_ptr_to_int(p)
def test_instantiate(): def fn1(n): return C(n) res = interpret(fn1, [42], taggedpointers=True) value = lltype.cast_ptr_to_int(res) assert value == 42 * 2 + 1 # for now
def f(): p = lltype.malloc(GCS1) return lltype.cast_ptr_to_int(p)
def op_cast_ptr_to_int(self, ptr1): checkptr(ptr1) return lltype.cast_ptr_to_int(ptr1)
def f(p): n = lltype.cast_ptr_to_int(p) return n
def id(self, ptr): return lltype.cast_ptr_to_int(ptr)
def ll_unboxed_to_int(p): return lltype.cast_ptr_to_int(p) >> 1
def cast_gcref_to_int(gcref): if we_are_translated(): return lltype.cast_ptr_to_int(gcref) else: return id(gcref._x)
def cast_gcref_to_int(gcref): if we_are_translated(): return lltype.cast_ptr_to_int(gcref) else: return id(gcref._x)
def ll_unboxed_getclass(instance, class_if_unboxed): if lltype.cast_ptr_to_int(instance) & 1: return class_if_unboxed else: return instance.typeptr
def ll_str(self, p): from pypy.rpython.lltypesystem.rstr import ll_str id = lltype.cast_ptr_to_int(p) return ll_str.ll_int2hex(r_uint(id), True)
def ll_str(self, p): from pypy.rpython.lltypesystem.rstr import ll_str id = lltype.cast_ptr_to_int(p) return ll_str.ll_int2hex(r_uint(id), True)
def op_cast_ptr_to_int(self, ptr1): checkptr(ptr1) return lltype.cast_ptr_to_int(ptr1)
def id(self, ptr): return lltype.cast_ptr_to_int(ptr)
def f(): s1 = lltype.malloc(S) adr = cast_ptr_to_adr(s1) i = cast_adr_to_int(adr) i2 = lltype.cast_ptr_to_int(s1) return i == i2