def gettype_from_unboxed(self, llops, vinst, can_be_none=False): unboxedclass_repr = getclassrepr(self.rtyper, self.unboxedclassdef) cunboxedcls = inputconst(CLASSTYPE, unboxedclass_repr.getvtable()) if self.is_parent: # If the lltype of vinst shows that it cannot be a tagged value, # we can directly read the typeptr. Otherwise, call a helper that # checks if the tag bit is set in the pointer. unboxedinstance_repr = getinstancerepr(self.rtyper, self.unboxedclassdef) try: lltype.castable(unboxedinstance_repr.lowleveltype, vinst.concretetype) except lltype.InvalidCast: can_be_tagged = False else: can_be_tagged = True vinst = llops.genop('cast_pointer', [vinst], resulttype=self.common_repr()) if can_be_tagged: if can_be_none: func = ll_unboxed_getclass_canbenone else: func = ll_unboxed_getclass return llops.gendirectcall(func, vinst, cunboxedcls) elif can_be_none: return llops.gendirectcall(ll_inst_type, vinst) else: ctypeptr = inputconst(lltype.Void, 'typeptr') return llops.genop('getfield', [vinst, ctypeptr], resulttype = CLASSTYPE) else: return cunboxedcls
def gettype_from_unboxed(self, llops, vinst): unboxedclass_repr = getclassrepr(self.rtyper, self.unboxedclassdef) cunboxedcls = inputconst(CLASSTYPE, unboxedclass_repr.getvtable()) if self.is_parent: # If the lltype of vinst shows that it cannot be a tagged value, # we can directly read the typeptr. Otherwise, call a helper that # checks if the tag bit is set in the pointer. unboxedinstance_repr = getinstancerepr(self.rtyper, self.unboxedclassdef) try: lltype.castable(unboxedinstance_repr.lowleveltype, vinst.concretetype) except lltype.InvalidCast: can_be_tagged = False else: can_be_tagged = True vinst = llops.genop('cast_pointer', [vinst], resulttype=self.common_repr()) if can_be_tagged: return llops.gendirectcall(ll_unboxed_getclass, vinst, cunboxedcls) else: ctypeptr = inputconst(lltype.Void, 'typeptr') return llops.genop('getfield', [vinst, ctypeptr], resulttype=CLASSTYPE) else: return cunboxedcls
def needs_conservative_livevar_calculation(block): from pypy.rpython.lltypesystem import rclass vars = block.getvariables() assert len(block.exits) == 1 exitingvars = block.exits[0].args for var in vars: TYPE = getattr(var, "concretetype", lltype.Ptr(lltype.PyObject)) if isinstance(TYPE, lltype.Ptr) and not var_needsgc(var): if isinstance(TYPE.TO, lltype.FuncType): continue try: lltype.castable(TYPE, rclass.CLASSTYPE) except lltype.InvalidCast: if var in exitingvars: return True else: return False
def test_rtype_2(): def f(): x1 = X() e = eraseX(x1) #assert not is_integer(e) x2 = uneraseX(e) return x2 x = interpret(f, []) assert lltype.castable(OBJECTPTR, lltype.typeOf(x)) > 0
def test_rtype_2(): def f(): x1 = X() vref = virtual_ref(x1) x2 = vref() virtual_ref_finish(x2) return x2 x = interpret(f, []) assert lltype.castable(OBJECTPTR, lltype.typeOf(x)) > 0
def cast(self, TO): down_or_up = lltype.castable(TO, lltype.Ptr(self.T)) # the following works because if a structure is virtual, then # all its parent and inlined substructures are also virtual vstruct = self if down_or_up >= 0: for n in range(down_or_up): vstruct = vstruct.read_field(vstruct.T._names[0]).contentdef else: for n in range(-down_or_up): vstruct = vstruct.vparent assert vstruct return vstruct
def handle_op_cast_pointer(self, op): node = self.getnode(op.args[0]) if isinstance(node, VirtualSpecNode): node = self.getnode(op.args[0]) SOURCEPTR = lltype.Ptr(node.typedesc.MALLOCTYPE) TARGETPTR = op.result.concretetype try: if lltype.castable(TARGETPTR, SOURCEPTR) < 0: raise lltype.InvalidCast except lltype.InvalidCast: return self.handle_unreachable(op) self.setnode(op.result, node) return [] else: return self.handle_default(op)
def fromtypeptr(self, vcls, llops): """Return the type pointer cast to self's vtable type.""" self.setup() castable(self.lowleveltype, vcls.concretetype) # sanity check return llops.genop('cast_pointer', [vcls], resulttype=self.lowleveltype)
def castable(self, TO, var): return lltype.castable(TO, lltype.typeOf(var)) > 0
def _cast_to(self, PTRTYPE): CURTYPE = self._TYPE down_or_up = lltype.castable(PTRTYPE, CURTYPE) if down_or_up == 0: return self return simulatorptr(PTRTYPE, self._address)