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, cpu.ts.NULLREF, mutatefielddescr) 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, cpu.ts.NULLREF, mutatefielddescr) qmut_ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, qmut_ref) qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) qmut.invalidate()
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 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) == OBJECTPTR y = annlowlevel.cast_base_ptr_to_instance(X, ptr) 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 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 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) == OBJECTPTR y = annlowlevel.cast_base_ptr_to_instance(X, ptr) assert y is x
def get(): if we_are_translated(): from rpython.rtyper import rclass from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance _threadlocalref_seeme(self) ptr = llop.threadlocalref_get(rclass.OBJECTPTR, offset) return cast_base_ptr_to_instance(Cls, ptr) else: return getattr(self.local, 'value', None)
def get(): if we_are_translated(): from rpython.rtyper import rclass from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance _threadlocalref_seeme(self) ptr = llop.threadlocalref_get(rclass.OBJECTPTR, offset) return cast_base_ptr_to_instance(Cls, ptr) else: return getattr(self.local, 'value', None)
def method_from_greenkey(space, jitdriver, greenkey): if greenkey is None: return space.w_nil jitdriver_name = jitdriver.name if jitdriver_name == jit_driver_name: ll_method = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), greenkey[2].getref_base()) w_method = cast_base_ptr_to_instance(W_CompiledMethod, ll_method) return w_method else: return space.w_nil
def method_from_greenkey(space, jitdriver, greenkey): if greenkey is None: return space.w_nil jitdriver_name = jitdriver.name if jitdriver_name == jit_driver_name: ll_method = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), greenkey[2].getref_base()) w_method = cast_base_ptr_to_instance(W_CompiledMethod, ll_method) return w_method else: return space.w_nil
def next_dead(self): if we_are_translated(): from rpython.rtyper.lltypesystem.lloperation import llop from rpython.rtyper.rclass import OBJECTPTR from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance tag = FinalizerQueue._get_tag(self) ptr = llop.gc_fq_next_dead(OBJECTPTR, tag) return cast_base_ptr_to_instance(self.Class, ptr) try: return self._queue.popleft() except (AttributeError, IndexError): return None
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 next_dead(self): if we_are_translated(): from rpython.rtyper.lltypesystem.lloperation import llop from rpython.rtyper.rclass import OBJECTPTR from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance tag = FinalizerQueue._get_tag(self) ptr = llop.gc_fq_next_dead(OBJECTPTR, tag) return cast_base_ptr_to_instance(self.Class, ptr) try: return self._queue.popleft() except (AttributeError, IndexError): return None
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([pycode, space.newint(next_instr), space.newbool(bool(is_being_profiled))]) else: return space.newtext(greenkey_repr)
def handle_jitexception(e): # XXX there are too many exceptions all around... while True: if isinstance(e, EnterJitAssembler): try: return e.execute() except jitexc.JitException as e: continue # if isinstance(e, jitexc.ContinueRunningNormally): args = () for ARGTYPE, attrname, count in portalfunc_ARGS: x = getattr(e, attrname)[count] x = specialize_value(ARGTYPE, x) args = args + (x,) try: result = support.maybe_on_top_of_llinterp(rtyper, portal_ptr)(*args) except jitexc.JitException as e: continue if result_kind != 'void': result = unspecialize_value(result) return result # if result_kind == 'void': if isinstance(e, jitexc.DoneWithThisFrameVoid): return None if result_kind == 'int': if isinstance(e, jitexc.DoneWithThisFrameInt): return e.result if result_kind == 'ref': if isinstance(e, jitexc.DoneWithThisFrameRef): return e.result if result_kind == 'float': if isinstance(e, jitexc.DoneWithThisFrameFloat): return e.result # if isinstance(e, jitexc.ExitFrameWithExceptionRef): 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) assert value is not None raise value # raise AssertionError("all cases should have been handled")
def handle_jitexception(e): # XXX there are too many exceptions all around... while True: if isinstance(e, EnterJitAssembler): try: return e.execute() except jitexc.JitException as e: continue # if isinstance(e, jitexc.ContinueRunningNormally): args = () for ARGTYPE, attrname, count in portalfunc_ARGS: x = getattr(e, attrname)[count] x = specialize_value(ARGTYPE, x) args = args + (x, ) try: result = support.maybe_on_top_of_llinterp( rtyper, portal_ptr)(*args) except jitexc.JitException as e: continue if result_kind != 'void': result = unspecialize_value(result) return result # if result_kind == 'void': if isinstance(e, jitexc.DoneWithThisFrameVoid): return None if result_kind == 'int': if isinstance(e, jitexc.DoneWithThisFrameInt): return e.result if result_kind == 'ref': if isinstance(e, jitexc.DoneWithThisFrameRef): return e.result if result_kind == 'float': if isinstance(e, jitexc.DoneWithThisFrameFloat): return e.result # if isinstance(e, jitexc.ExitFrameWithExceptionRef): 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) assert value is not None raise value # raise AssertionError("all cases should have been handled")
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 rpython.rtyper.rclass import OBJECTPTR, ll_isinstance from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance if _is_rpy_instance(gcref): objptr = lltype.cast_opaque_ptr(OBJECTPTR, gcref) if objptr.typeptr: # may be NULL, e.g. in rdict's dummykeyobj clsptr = _get_llcls_from_cls(Class) if 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 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 rpython.rtyper.rclass import OBJECTPTR, ll_isinstance from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance if _is_rpy_instance(gcref): objptr = lltype.cast_opaque_ptr(OBJECTPTR, gcref) if objptr.typeptr: # may be NULL, e.g. in rdict's dummykeyobj clsptr = _get_llcls_from_cls(Class) if 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 execute_frame(frame, w_inputvalue=None, operr=None): # go through the asm trampoline ONLY if we are translated but not being JITted. # # If we are not translated, we obviously don't want to go through the # trampoline because there is no C function it can call. # # If we are being JITted, we want to skip the trampoline, else the JIT # cannot see throug it if we_are_translated() and not jit.we_are_jitted(): # if we are translated, call the trampoline gc_frame = cast_instance_to_gcref(frame) gc_inputvalue = cast_instance_to_gcref(w_inputvalue) gc_operr = cast_instance_to_gcref(operr) unique_id = frame.pycode._unique_id gc_result = pypy_execute_frame_trampoline(gc_frame, gc_inputvalue, gc_operr, unique_id) return cast_base_ptr_to_instance(W_Root, gc_result) else: return original_execute_frame(frame, w_inputvalue, operr)
def execute_frame(frame, w_inputvalue=None, operr=None): # go through the asm trampoline ONLY if we are translated but not being JITted. # # If we are not translated, we obviously don't want to go through the # trampoline because there is no C function it can call. # # If we are being JITted, we want to skip the trampoline, else the JIT # cannot see throug it if we_are_translated() and not jit.we_are_jitted(): # if we are translated, call the trampoline gc_frame = cast_instance_to_gcref(frame) gc_inputvalue = cast_instance_to_gcref(w_inputvalue) gc_operr = cast_instance_to_gcref(operr) unique_id = frame.pycode._unique_id gc_result = pypy_execute_frame_trampoline(gc_frame, gc_inputvalue, gc_operr, unique_id) return cast_base_ptr_to_instance(W_Root, gc_result) else: return original_execute_frame(frame, w_inputvalue, operr)
def decorated_function(*args): # go through the asm trampoline ONLY if we are translated but not # being JITted. # # If we are not translated, we obviously don't want to go through # the trampoline because there is no C function it can call. # # If we are being JITted, we want to skip the trampoline, else the # JIT cannot see through it. # if we_are_translated() and not jit.we_are_jitted(): # if we are translated, call the trampoline unique_id = get_code_fn(*args)._vmprof_unique_id ll_args, token = lower(*args) ll_trampoline = get_ll_trampoline(token) ll_result = ll_trampoline(*ll_args + (unique_id, )) if result_class is not None: return cast_base_ptr_to_instance(result_class, ll_result) else: return ll_result else: return func(*args)
def decorated_function(*args): # go through the asm trampoline ONLY if we are translated but not # being JITted. # # If we are not translated, we obviously don't want to go through # the trampoline because there is no C function it can call. # # If we are being JITted, we want to skip the trampoline, else the # JIT cannot see through it. # if we_are_translated() and not jit.we_are_jitted(): # if we are translated, call the trampoline unique_id = get_code_fn(*args)._vmprof_unique_id ll_args, token = lower(*args) ll_trampoline = get_ll_trampoline(token) ll_result = ll_trampoline(*ll_args + (unique_id,)) if result_class is not None: return cast_base_ptr_to_instance(result_class, ll_result) else: return ll_result else: return func(*args)
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) 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 _cast_to_resop(llref): from rpython.jit.metainterp.resoperation import AbstractResOp ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractResOp, ptr)
def show(cpu, descr_gcref): from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance descr_ptr = cpu.ts.cast_to_baseclass(descr_gcref) return cast_base_ptr_to_instance(AbstractDescr, descr_ptr)
def show(cpu, qmut_gcref): qmut_ptr = cpu.ts.cast_to_baseclass(qmut_gcref) return cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
def _cast_to_box(llref): from rpython.jit.metainterp.history import AbstractValue ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractValue, 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 _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(descr_repr)
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 get_exception_obj(self, evaluebox): # only works when translated obj = evaluebox.getref(lltype.Ptr(rclass.OBJECT)) return cast_base_ptr_to_instance(Exception, obj)
return except jitexc.DoneWithThisFrameInt, e: assert result_kind == 'int' return specialize_value(RESULT, e.result) except jitexc.DoneWithThisFrameRef, e: assert result_kind == 'ref' return specialize_value(RESULT, e.result) except jitexc.DoneWithThisFrameFloat, e: assert result_kind == 'float' return specialize_value(RESULT, e.result) except jitexc.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 def handle_jitexception(e): # XXX the bulk of this function is mostly a copy-paste from above try: raise e except jitexc.ContinueRunningNormally, e: args = () for ARGTYPE, attrname, count in portalfunc_ARGS: x = getattr(e, attrname)[count] x = specialize_value(ARGTYPE, x) args = args + (x, ) result = ll_portal_runner(*args) if result_kind != 'void': result = unspecialize_value(result)
return except jitexc.DoneWithThisFrameInt, e: assert result_kind == 'int' return specialize_value(RESULT, e.result) except jitexc.DoneWithThisFrameRef, e: assert result_kind == 'ref' return specialize_value(RESULT, e.result) except jitexc.DoneWithThisFrameFloat, e: assert result_kind == 'float' return specialize_value(RESULT, e.result) except jitexc.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 def handle_jitexception(e): # XXX the bulk of this function is mostly a copy-paste from above try: raise e except jitexc.ContinueRunningNormally, e: args = () for ARGTYPE, attrname, count in portalfunc_ARGS: x = getattr(e, attrname)[count] x = specialize_value(ARGTYPE, x) args = args + (x,) result = ll_portal_runner(*args) if result_kind != 'void': result = unspecialize_value(result)
def show(cpu, gcref): from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance ptr = cpu.ts.cast_to_baseclass(gcref) return cast_base_ptr_to_instance(AllVirtuals, ptr)
def show(cpu, qmut_gcref): qmut_ptr = cpu.ts.cast_to_baseclass(qmut_gcref) return cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
def _cast_to_resop(llref): from rpython.jit.metainterp.resoperation import AbstractResOp ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractResOp, ptr)
def show(cpu, gcref): from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance ptr = cpu.ts.cast_to_baseclass(gcref) return cast_base_ptr_to_instance(AllVirtuals, ptr)
def show(cpu, descr_gcref): from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance descr_ptr = cpu.ts.cast_to_baseclass(descr_gcref) return cast_base_ptr_to_instance(AbstractDescr, descr_ptr)
def _cast_to_box(llref): from rpython.jit.metainterp.history import AbstractValue ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, llref) return cast_base_ptr_to_instance(AbstractValue, ptr)
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