def reraise(lle): if we_are_translated(): e = cast_base_ptr_to_instance(Exception, lle) raise e else: etype = rclass.ll_type(lle) raise LLException(etype, lle)
def reraise(lle): if we_are_translated(): e = cast_base_ptr_to_instance(Exception, lle) raise e else: etype = rclass.ll_type(lle) raise LLException(etype, lle)
def do_force_quasi_immutable(cpu, p, mutatefielddescr): qmut_ref = cpu.bh_getfield_gc_r(p, mutatefielddescr) if qmut_ref: cpu.bh_setfield_gc_r(p, mutatefielddescr, cpu.ts.NULLREF) qmut_ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, qmut_ref) qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) qmut.invalidate()
def do_force_quasi_immutable(cpu, p, mutatefielddescr): qmut_ref = cpu.bh_getfield_gc_r(p, mutatefielddescr) if qmut_ref: cpu.bh_setfield_gc_r(p, mutatefielddescr, cpu.ts.NULLREF) qmut_ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, qmut_ref) qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) qmut.invalidate()
def f(): s = T() ls = cast_instance_to_base_ptr(s) as_num = rffi.cast(lltype.Signed, ls) # --- around this point, only 'as_num' is passed t = rffi.cast(rclass.OBJECTPTR, as_num) u = cast_base_ptr_to_instance(S, t) return u.x
def test_cast_instance_to_base_obj(self): class X(object): pass x = X() obj = annlowlevel.cast_instance_to_base_obj(x) assert lltype.typeOf(obj) == annlowlevel.base_obj_ootype() y = annlowlevel.cast_base_ptr_to_instance(X, obj) assert y is x
def f(x, y): if x > 20: a = None else: a = A(x, y) a1 = cast_instance_to_base_ptr(a) b = cast_base_ptr_to_instance(A, a1) return a is b
def set_field_touched(struc, value): T = fielddesc.RESTYPE if fielddesc.canbevirtual and fielddesc.gcref: vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touched_ptr_field(struc.vable_base, j) struc = lltype.cast_pointer(fielddesc.PTRTYPE, struc) setattr(struc, fielddesc.fieldname, value)
def test_cast_instance_to_base_ptr(self): class X(object): pass x = X() ptr = annlowlevel.cast_instance_to_base_ptr(x) assert lltype.typeOf(ptr) == annlowlevel.base_ptr_lltype() y = annlowlevel.cast_base_ptr_to_instance(X, ptr) assert y is x
def debug_merge_point_from_boxes(boxes): mp_no = boxes[0].getint() offset = boxes[2].getint() llcode = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), boxes[4].getref_base()) pycode = cast_base_ptr_to_instance(PyCode, llcode) assert pycode is not None return W_DebugMergePoint(mp_no, offset, pycode)
def f(x, y): if x > 20: a = None else: a = A(x, y) a1 = cast_instance_to_base_ptr(a) b = cast_base_ptr_to_instance(A, a1) return a is b
def f(): s = T() ls = cast_instance_to_base_ptr(s) as_num = rffi.cast(lltype.Signed, ls) # --- around this point, only 'as_num' is passed t = rffi.cast(rclass.OBJECTPTR, as_num) u = cast_base_ptr_to_instance(S, t) return u.x
def set_field_touched(struc, value): T = fielddesc.RESTYPE if fielddesc.canbevirtual and fielddesc.gcref: vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touched_ptr_field(struc.vable_base, j) struc = lltype.cast_pointer(fielddesc.PTRTYPE, struc) setattr(struc, fielddesc.fieldname, value)
def test_cast_instance_to_base_ptr(self): class X(object): pass x = X() ptr = annlowlevel.cast_instance_to_base_ptr(x) assert lltype.typeOf(ptr) == annlowlevel.base_ptr_lltype() y = annlowlevel.cast_base_ptr_to_instance(X, ptr) assert y is x
def test_cast_instance_to_base_obj(self): class X(object): pass x = X() obj = annlowlevel.cast_instance_to_base_obj(x) assert lltype.typeOf(obj) == annlowlevel.base_obj_ootype() y = annlowlevel.cast_base_ptr_to_instance(X, obj) assert y is x
def f(n): s = S() s.x = n ls = cast_instance_to_base_ptr(s) as_num = rffi.cast(lltype.Signed, ls) # --- around this point, only 'as_num' is passed r = rffi.cast(llmemory.GCREF, as_num) t = lltype.cast_opaque_ptr(rclass.OBJECTPTR, r) u = cast_base_ptr_to_instance(S, t) return u.x
def f(n): s = S() s.x = n ls = cast_instance_to_base_ptr(s) as_num = rffi.cast(lltype.Signed, ls) # --- around this point, only 'as_num' is passed r = rffi.cast(llmemory.GCREF, as_num) t = lltype.cast_opaque_ptr(rclass.OBJECTPTR, r) u = cast_base_ptr_to_instance(S, t) return u.x
def get_field_touched(struc): T = fielddesc.RESTYPE tgt = lltype.cast_pointer(fielddesc.PTRTYPE, struc) if fielddesc.canbevirtual and fielddesc.gcref: vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_base = struc.vable_base if vable_rti.is_field_virtual(vable_base, j): # this will force s = vable_rti.read_field(fielddesc, vable_base, j) setattr(tgt, fielddesc.fieldname, s) return s return getattr(tgt, fielddesc.fieldname)
def get_field_touched(struc): T = fielddesc.RESTYPE tgt = lltype.cast_pointer(fielddesc.PTRTYPE, struc) if fielddesc.canbevirtual and fielddesc.gcref: vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_base = struc.vable_base if vable_rti.is_field_virtual(vable_base, j): # this will force s = vable_rti.read_field(fielddesc, vable_base, j) setattr(tgt, fielddesc.fieldname, s) return s return getattr(tgt, fielddesc.fieldname)
def wrap_greenkey(space, jitdriver, greenkey, greenkey_repr): if greenkey is None: return space.w_None jitdriver_name = jitdriver.name if jitdriver_name == 'pypyjit': next_instr = greenkey[0].getint() is_being_profiled = greenkey[1].getint() ll_code = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), greenkey[2].getref_base()) pycode = cast_base_ptr_to_instance(PyCode, ll_code) return space.newtuple([space.wrap(pycode), space.wrap(next_instr), space.newbool(bool(is_being_profiled))]) else: return space.wrap(greenkey_repr)
def _prepare_pendingfields(self, pendingfields): if pendingfields: for i in range(len(pendingfields)): lldescr = pendingfields[i].lldescr num = pendingfields[i].num fieldnum = pendingfields[i].fieldnum itemindex= pendingfields[i].itemindex descr = annlowlevel.cast_base_ptr_to_instance(AbstractDescr, lldescr) struct = self.decode_ref(num) itemindex = rffi.cast(lltype.Signed, itemindex) if itemindex < 0: self.setfield(descr, struct, fieldnum) else: self.setarrayitem(descr, struct, itemindex, fieldnum)
def try_cast_gcref_to_instance(Class, gcref): # Before translation, unwraps the RPython instance contained in a _GcRef. # After translation, it is a type-check performed by the GC. if we_are_translated(): from pypy.rpython.annlowlevel import base_ptr_lltype from pypy.rpython.annlowlevel import cast_base_ptr_to_instance from pypy.rpython.lltypesystem import rclass if _is_rpy_instance(gcref): objptr = lltype.cast_opaque_ptr(base_ptr_lltype(), gcref) if objptr.typeptr: # may be NULL, e.g. in rdict's dummykeyobj clsptr = _get_llcls_from_cls(Class) if rclass.ll_isinstance(objptr, clsptr): return cast_base_ptr_to_instance(Class, objptr) return None else: if isinstance(gcref._x, Class): return gcref._x return None
def touch_update(strucref): struc = lltype.cast_opaque_ptr(TOPPTR, strucref) vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touch(struc.vable_base) vable_base = struc.vable_base j = -1 for fielddesc, _ in redirected_fielddescs: j += 1 if fielddesc.canbevirtual and fielddesc.gcref: if vable_rti.is_field_virtual(vable_base, j): continue v = vable_rti.read_field(fielddesc, vable_base, j) tgt = lltype.cast_pointer(fielddesc.PTRTYPE, struc) setattr(tgt, fielddesc.fieldname, v) ACCESSPTR = TOPPTR.TO.vable_access struc.vable_access = lltype.cast_pointer(ACCESSPTR, access_touched)
def try_cast_gcref_to_instance(Class, gcref): # Before translation, unwraps the RPython instance contained in a _GcRef. # After translation, it is a type-check performed by the GC. if we_are_translated(): from pypy.rpython.annlowlevel import base_ptr_lltype from pypy.rpython.annlowlevel import cast_base_ptr_to_instance from pypy.rpython.lltypesystem import rclass if _is_rpy_instance(gcref): objptr = lltype.cast_opaque_ptr(base_ptr_lltype(), gcref) if objptr.typeptr: # may be NULL, e.g. in rdict's dummykeyobj clsptr = _get_llcls_from_cls(Class) if rclass.ll_isinstance(objptr, clsptr): return cast_base_ptr_to_instance(Class, objptr) return None else: if isinstance(gcref._x, Class): return gcref._x return None
def touch_update(strucref): struc = lltype.cast_opaque_ptr(TOPPTR, strucref) vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touch(struc.vable_base) vable_base = struc.vable_base j = -1 for fielddesc, _ in redirected_fielddescs: j += 1 if fielddesc.canbevirtual and fielddesc.gcref: if vable_rti.is_field_virtual(vable_base, j): continue v = vable_rti.read_field(fielddesc, vable_base, j) tgt = lltype.cast_pointer(fielddesc.PTRTYPE, struc) setattr(tgt, fielddesc.fieldname, v) ACCESSPTR = TOPPTR.TO.vable_access struc.vable_access = lltype.cast_pointer(ACCESSPTR, access_touched)
def get_jitcell(build, *greenargs): fn = support.maybe_on_top_of_llinterp(rtyper, get_jitcell_at_ptr) cellref = fn(*greenargs) # <hacks> if we_are_translated(): BASEJITCELL = lltype.typeOf(cellref) cell = cast_base_ptr_to_instance(JitCell, cellref) else: if isinstance(cellref, (BaseJitCell, type(None))): BASEJITCELL = None cell = cellref else: BASEJITCELL = lltype.typeOf(cellref) if cellref: cell = lltohlhack[rtyper.type_system.deref(cellref)] else: cell = None if not build: return cell if cell is None: cell = JitCell() # <hacks> if we_are_translated(): cellref = cast_object_to_ptr(BASEJITCELL, cell) else: if BASEJITCELL is None: cellref = cell else: if isinstance(BASEJITCELL, lltype.Ptr): cellref = lltype.malloc(BASEJITCELL.TO) elif isinstance(BASEJITCELL, ootype.Instance): cellref = ootype.new(BASEJITCELL) else: assert False, "no clue" lltohlhack[rtyper.type_system.deref(cellref)] = cell # </hacks> fn = support.maybe_on_top_of_llinterp(rtyper, set_jitcell_at_ptr) fn(cellref, *greenargs) return cell
def get_jitcell(build, *greenargs): fn = support.maybe_on_top_of_llinterp(rtyper, get_jitcell_at_ptr) cellref = fn(*greenargs) # <hacks> if we_are_translated(): BASEJITCELL = lltype.typeOf(cellref) cell = cast_base_ptr_to_instance(JitCell, cellref) else: if isinstance(cellref, (BaseJitCell, type(None))): BASEJITCELL = None cell = cellref else: BASEJITCELL = lltype.typeOf(cellref) if cellref: cell = lltohlhack[rtyper.type_system.deref(cellref)] else: cell = None if not build: return cell if cell is None: cell = JitCell() # <hacks> if we_are_translated(): cellref = cast_object_to_ptr(BASEJITCELL, cell) else: if BASEJITCELL is None: cellref = cell else: if isinstance(BASEJITCELL, lltype.Ptr): cellref = lltype.malloc(BASEJITCELL.TO) elif isinstance(BASEJITCELL, ootype.Instance): cellref = ootype.new(BASEJITCELL) else: assert False, "no clue" lltohlhack[rtyper.type_system.deref(cellref)] = cell # </hacks> fn = support.maybe_on_top_of_llinterp(rtyper, set_jitcell_at_ptr) fn(cellref, *greenargs) return cell
def _get_signature(self, funcval): """ given the funcval, return a tuple (argtypes, restype, flags), where the actuall types are libffi.types.* The implementation is tricky because we have three possible cases: - translated: the easiest case, we can just cast back the pointer to the original Func instance and read .argtypes, .restype and .flags - completely untranslated: this is what we get from test_optimizeopt tests. funcval contains a FakeLLObject whose _fake_class is Func, and we can just get .argtypes, .restype and .flags - partially translated: this happens when running metainterp tests: funcval contains the low-level equivalent of a Func, and thus we have to fish inst_argtypes and inst_restype by hand. Note that inst_argtypes is actually a low-level array, but we can use it directly since the only thing we do with it is to read its items """ llfunc = funcval.box.getref_base() if we_are_translated(): func = cast_base_ptr_to_instance(Func, llfunc) return func.argtypes, func.restype, func.flags elif getattr(llfunc, '_fake_class', None) is Func: # untranslated return llfunc.argtypes, llfunc.restype, llfunc.flags else: # partially translated # llfunc contains an opaque pointer to something like the following: # <GcStruct pypy.rlib.libffi.Func { super, inst_argtypes, inst_funcptr, # inst_funcsym, inst_restype }> # # Unfortunately, we cannot use the proper lltype.cast_opaque_ptr, # because we don't have the exact TYPE to cast to. Instead, we # just fish it manually :-( f = llfunc._obj.container return f.inst_argtypes, f.inst_restype, f.inst_flags
def _get_signature(self, funcval): """ given the funcval, return a tuple (argtypes, restype), where the actuall types are libffi.types.* The implementation is tricky because we have three possible cases: - translated: the easiest case, we can just cast back the pointer to the original Func instance and read .argtypes and .restype - completely untranslated: this is what we get from test_optimizeopt tests. funcval contains a FakeLLObject whose _fake_class is Func, and we can just get .argtypes and .restype - partially translated: this happens when running metainterp tests: funcval contains the low-level equivalent of a Func, and thus we have to fish inst_argtypes and inst_restype by hand. Note that inst_argtypes is actually a low-level array, but we can use it directly since the only thing we do with it is to read its items """ llfunc = funcval.box.getref_base() if we_are_translated(): func = cast_base_ptr_to_instance(Func, llfunc) return func.argtypes, func.restype elif getattr(llfunc, '_fake_class', None) is Func: # untranslated return llfunc.argtypes, llfunc.restype else: # partially translated # llfunc contains an opaque pointer to something like the following: # <GcStruct pypy.rlib.libffi.Func { super, inst_argtypes, inst_funcptr, # inst_funcsym, inst_restype }> # # Unfortunately, we cannot use the proper lltype.cast_opaque_ptr, # because we don't have the exact TYPE to cast to. Instead, we # just fish it manually :-( f = llfunc._obj.container return f.inst_argtypes, f.inst_restype
def on_compile(self, logger, looptoken, operations, type, next_instr, is_being_profiled, ll_pycode): from pypy.rpython.annlowlevel import cast_base_ptr_to_instance space = self.space cache = space.fromcache(Cache) if cache.in_recursion: return if space.is_true(cache.w_compile_hook): logops = logger._make_log_operations() list_w = wrap_oplist(space, logops, operations) pycode = cast_base_ptr_to_instance(PyCode, ll_pycode) cache.in_recursion = True try: space.call_function( cache.w_compile_hook, space.wrap("main"), space.wrap(type), space.newtuple([pycode, space.wrap(next_instr), space.wrap(is_being_profiled)]), space.newlist(list_w), ) except OperationError, e: e.write_unraisable(space, "jit hook ", cache.w_compile_hook) cache.in_recursion = False
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_resop(llref): from pypy.jit.metainterp.resoperation import AbstractResOp ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractResOp, ptr)
def _invalidate_now(p): qmut_ptr = getattr(p, mutatefieldname) setattr(p, mutatefieldname, lltype.nullptr(rclass.OBJECT)) qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) qmut.invalidate()
def get_location_str(boxes): ll_code = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), boxes[2].getref_base()) pycode = cast_base_ptr_to_instance(PyCode, ll_code) return pycode.co_name
def func(llfunc): from pypy.rlib.libffi import Func return cast_base_ptr_to_instance(Func, llfunc)
def show(cpu, qmut_gcref): qmut_ptr = cpu.ts.cast_to_baseclass(qmut_gcref) return cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
def force_now(self, virtualizable): rti = virtualizable.vable_rti virtualizable.vable_rti = self.null_vable_rti if we_are_translated(): rti = cast_base_ptr_to_instance(AbstractVableRti, rti) rti.force_now(virtualizable)
def _invalidate_now(p): qmut_ptr = getattr(p, mutatefieldname) setattr(p, mutatefieldname, lltype.nullptr(rclass.OBJECT)) qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) qmut.invalidate()
def _cast_to_box(llref): from pypy.jit.metainterp.history import AbstractValue ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractValue, ptr)
def show(gcref): ptr = lltype.cast_opaque_ptr(lltype.Ptr(rclass.OBJECT), gcref) return cast_base_ptr_to_instance(Opaque, ptr)
return except self.DoneWithThisFrameInt, e: assert result_kind == 'int' return lltype.cast_primitive(RESULT, e.result) except self.DoneWithThisFrameRef, e: assert result_kind == 'ref' return ts.cast_from_ref(RESULT, e.result) except self.DoneWithThisFrameFloat, e: assert result_kind == 'float' return e.result except self.ExitFrameWithExceptionRef, e: value = ts.cast_to_baseclass(e.value) if not we_are_translated(): raise LLException(ts.get_typeptr(value), value) else: value = cast_base_ptr_to_instance(Exception, value) raise Exception, value self.ll_portal_runner = ll_portal_runner # for debugging self.portal_runner_ptr = self.helper_func(self.PTR_PORTAL_FUNCTYPE, ll_portal_runner) self.cpu.portal_calldescr = self.cpu.calldescrof( self.PTR_PORTAL_FUNCTYPE.TO, self.PTR_PORTAL_FUNCTYPE.TO.ARGS, self.PTR_PORTAL_FUNCTYPE.TO.RESULT) vinfo = self.metainterp_sd.virtualizable_info def assembler_call_helper(failindex, virtualizableref): fail_descr = self.cpu.get_fail_descr_from_number(failindex) while True:
def set_field_untouched(struc, value): vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touch_update(lltype.cast_opaque_ptr(llmemory.GCREF, struc)) set_field_touched(struc, value)
def get_field_untouched(struc): vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) return vable_rti.read_field(fielddesc, struc.vable_base, j)
def force_now(self, virtualizable): rti = virtualizable.vable_rti virtualizable.vable_rti = self.null_vable_rti if we_are_translated(): rti = cast_base_ptr_to_instance(AbstractVableRti, rti) rti.force_now(virtualizable)
def get_location_str(boxes): ll_code = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), boxes[2].getref_base()) pycode = cast_base_ptr_to_instance(PyCode, ll_code) return pycode.co_name
def func(llfunc): from pypy.rlib.libffi import Func return cast_base_ptr_to_instance(Func, llfunc)
def get_exception_obj(self, evaluebox): # only works when translated obj = evaluebox.getref(lltype.Ptr(rclass.OBJECT)) return cast_base_ptr_to_instance(Exception, obj)
def _cast_to_box(llref): from pypy.jit.metainterp.history import AbstractValue ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractValue, ptr)
def get_field_untouched(struc): vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) return vable_rti.read_field(fielddesc, struc.vable_base, j)
def _cast_to_resop(llref): from pypy.jit.metainterp.resoperation import AbstractResOp ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractResOp, ptr)
def set_field_untouched(struc, value): vable_rti = struc.vable_rti vable_rti = cast_base_ptr_to_instance(VirtualizableRTI, vable_rti) vable_rti.touch_update(lltype.cast_opaque_ptr(llmemory.GCREF, struc)) set_field_touched(struc, value)
def show(cpu, qmut_gcref): qmut_ptr = cpu.ts.cast_to_baseclass(qmut_gcref) return cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)