def fn(n): s = lltype.cast_int_to_ptr(PS, n) assert lltype.typeOf(s) == PS assert lltype.cast_ptr_to_int(s) == n t = lltype.cast_pointer(PT, s) assert lltype.typeOf(t) == PT assert lltype.cast_ptr_to_int(t) == n assert s == lltype.cast_pointer(PS, t)
def PyObject_Realloc(space, ptr, size): if not lltype.cast_ptr_to_int(ptr): return lltype.malloc(rffi.VOIDP.TO, size, flavor='raw', add_memory_pressure=True) # XXX FIXME return realloc(ptr, size)
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 test_cast_adr_to_int(): S = lltype.Struct('S') p = lltype.malloc(S, immortal=True) def fn(n): a = llmemory.cast_ptr_to_adr(p) if n == 2: return llmemory.cast_adr_to_int(a, "emulated") elif n == 4: return llmemory.cast_adr_to_int(a, "symbolic") else: return llmemory.cast_adr_to_int(a, "forced") res = interpret(fn, [2]) assert is_valid_int(res) assert res == lltype.cast_ptr_to_int(p) # res = interpret(fn, [4]) assert isinstance(res, llmemory.AddressAsInt) assert llmemory.cast_int_to_adr(res) == llmemory.cast_ptr_to_adr(p) # res = interpret(fn, [6]) assert is_valid_int(res) from rpython.rtyper.lltypesystem import rffi assert res == rffi.cast(lltype.Signed, p)
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 ll_attach(shadowstackref): """After prepare_free_slot(), store a shadowstackref in that slot.""" index = fullstack_cache[0] ll_assert(index > 0, "fullstack attach: no free slot") fullstack_cache[0] = fullstack_cache[index] fullstack_cache[index] = lltype.cast_ptr_to_int(shadowstackref) ll_assert(shadowstackref.fsindex == 0, "fullstack attach: already one?") shadowstackref.fsindex = index # > 0
def cast_gcref_to_int(gcref): # This is meant to be used on cast_instance_to_gcref results. # Don't use this on regular gcrefs obtained e.g. with # lltype.cast_opaque_ptr(). if we_are_translated(): return lltype.cast_ptr_to_int(gcref) else: return id(gcref._x)
def ll_str(self, i): if lltype.cast_ptr_to_int(i) & 1: from rpython.rtyper.lltypesystem import rstr from rpython.rtyper.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_detach(shadowstackref): """Detach a shadowstackref from the array of MAX entries.""" index = shadowstackref.fsindex ll_assert(index > 0, "detach: unattached shadowstackref") ll_assert(fullstack_cache[index] == lltype.cast_ptr_to_int(shadowstackref), "detach: bad fullstack_cache") shadowstackref.fsindex = 0 fullstack_cache[index] = fullstack_cache[0] fullstack_cache[0] = index
def ll_str(self, i): if lltype.cast_ptr_to_int(i) & 1: from rpython.rtyper.lltypesystem import rstr from rpython.rtyper.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 set(value): assert isinstance(value, Cls) or value is None if we_are_translated(): from rpython.rtyper.annlowlevel import cast_instance_to_gcref gcref = cast_instance_to_gcref(value) value = lltype.cast_ptr_to_int(gcref) setraw(value) rgc.register_custom_trace_hook(TRACETLREF, _lambda_trace_tlref) rgc.ll_writebarrier(_tracetlref_obj) else: self.local.value = value
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 customtrace(gc, obj, callback, arg): obj = llmemory.cast_adr_to_ptr(obj, SHADOWSTACKREFPTR) index = obj.fsindex if index > 0: # Haaaaaaack: fullstack_cache[] is just an integer, so it # doesn't follow the SHADOWSTACKREF when it moves. But we # know this customtrace() will be called just after the # move. So we fix the fullstack_cache[] now... :-/ fullstack_cache[index] = lltype.cast_ptr_to_int(obj) addr = obj.top start = obj.base while addr != start: addr -= sizeofaddr gc._trace_callback(callback, arg, addr)
def set(value): assert isinstance(value, Cls) or value is None if we_are_translated(): from rpython.rtyper.annlowlevel import cast_instance_to_gcref from rpython.rlib.rgc import _make_sure_does_not_move from rpython.rlib.objectmodel import running_on_llinterp gcref = cast_instance_to_gcref(value) if not running_on_llinterp: if gcref: _make_sure_does_not_move(gcref) value = lltype.cast_ptr_to_int(gcref) setraw(value) else: self.local.value = value
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 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 ll_str(self, i): # doesn't work for non-gc classes! from rpython.rtyper.lltypesystem import rstr from rpython.rtyper.lltypesystem.ll_str import ll_int2hex from rpython.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_unboxed_to_int(p): return lltype.cast_ptr_to_int(p) >> 1
def op_cast_ptr_to_int(self, ptr1): checkptr(ptr1) return lltype.cast_ptr_to_int(ptr1)
def fn(n): t = lltype.cast_int_to_ptr(PT, n) assert lltype.typeOf(t) == PT assert lltype.cast_ptr_to_int(t) == n o = lltype.cast_opaque_ptr(PQ, t) assert lltype.cast_ptr_to_int(o) == n
def id(self, ptr): return lltype.cast_ptr_to_int(ptr)
def ll_cls_hash(cls): return cast_ptr_to_int(cls)
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(): ptr = lltype.malloc(A, 10) gcref = lltype.cast_opaque_ptr(GCREF, ptr) adr = lltype.cast_ptr_to_int(gcref) assert adr == lltype.cast_ptr_to_int(ptr)
def ll_str(self, p): from rpython.rtyper.lltypesystem.rstr import ll_str id = lltype.cast_ptr_to_int(p) return ll_str.ll_int2hex(r_uint(id), True)
def cast_gcref_to_int(gcref): if we_are_translated(): return lltype.cast_ptr_to_int(gcref) else: return id(gcref._x)