def test_runtime_type_info(): S = lltype.GcStruct('s', ('x', lltype.Signed), rtti=True) def ll_example(p): return (lltype.runtime_type_info(p), lltype.runtime_type_info(p) == lltype.getRuntimeTypeInfo(S)) assert ll_example(lltype.malloc(S)) == (lltype.getRuntimeTypeInfo(S), True) s, t = ll_rtype(ll_example, [SomePtr(lltype.Ptr(S))]) assert s == annmodel.SomeTuple( [SomePtr(lltype.Ptr(lltype.RuntimeTypeInfo)), annmodel.SomeBool()])
def test_charp2str_exact_result(self): from rpython.annotator.annrpython import RPythonAnnotator from rpython.rtyper.llannotation import SomePtr a = RPythonAnnotator() s = a.build_types(charpsize2str, [SomePtr(CCHARP), int]) assert s.knowntype == str assert s.can_be_None is False assert s.no_nul is False # a = RPythonAnnotator() s = a.build_types(charp2str, [SomePtr(CCHARP)]) assert s.knowntype == str assert s.can_be_None is False assert s.no_nul is True
def test_runtime_type_info(self): S = GcStruct('s', ('x', Signed), rtti=True) def llf(p): return runtime_type_info(p) s = self.annotate(llf, [SomePtr(Ptr(S))]) assert isinstance(s, SomePtr) assert s.ll_ptrtype == Ptr(RuntimeTypeInfo)
def test_funcptr(self): F = FuncType((Signed,), Signed) PF = Ptr(F) def llf(p): return p(0) s = self.annotate(llf, [SomePtr(PF)]) assert s.knowntype == int
def _setup_repr_final(self): self._setup_immutable_field_list() self._check_for_immutable_conflicts() if self.gcflavor == 'gc': if (self.classdef is not None and self.classdef.classdesc.lookup('__del__') is not None): s_func = self.classdef.classdesc.s_read_attribute('__del__') source_desc = self.classdef.classdesc.lookup('__del__') source_classdef = source_desc.getclassdef(None) source_repr = getinstancerepr(self.rtyper, source_classdef) assert len(s_func.descriptions) == 1 funcdesc, = s_func.descriptions graph = funcdesc.getuniquegraph() self.check_graph_of_del_does_not_call_too_much( self.rtyper, graph) FUNCTYPE = FuncType([Ptr(source_repr.object_type)], Void) destrptr = functionptr(FUNCTYPE, graph.name, graph=graph, _callable=graph.func) else: destrptr = None self.rtyper.call_all_setups() # compute ForwardReferences now args_s = [SomePtr(Ptr(OBJECT))] graph = self.rtyper.annotate_helper(ll_runtime_type_info, args_s) s = self.rtyper.annotation(graph.getreturnvar()) if (not isinstance(s, SomePtr) or s.ll_ptrtype != Ptr(RuntimeTypeInfo)): raise TyperError("runtime type info function returns %r, " "expected Ptr(RuntimeTypeInfo)" % (s)) funcptr = self.rtyper.getcallable(graph) attachRuntimeTypeInfo(self.object_type, funcptr, destrptr) vtable = self.rclass.getvtable() self.rtyper.set_type_for_typeptr(vtable, self.lowleveltype.TO)
def test_annotation_to_lltype(): s_i = SomeInteger() s_pos = SomeInteger(nonneg=True) s_1 = SomeInteger(nonneg=True) s_1.const = 1 s_m1 = SomeInteger(nonneg=False) s_m1.const = -1 s_u = SomeInteger(nonneg=True, unsigned=True) s_u1 = SomeInteger(nonneg=True, unsigned=True) s_u1.const = r_uint(1) assert annotation_to_lltype(s_i) == lltype.Signed assert annotation_to_lltype(s_pos) == lltype.Signed assert annotation_to_lltype(s_1) == lltype.Signed assert annotation_to_lltype(s_m1) == lltype.Signed assert annotation_to_lltype(s_u) == lltype.Unsigned assert annotation_to_lltype(s_u1) == lltype.Unsigned assert annotation_to_lltype(SomeBool()) == lltype.Bool assert annotation_to_lltype(SomeChar()) == lltype.Char PS = lltype.Ptr(lltype.GcStruct('s')) s_p = SomePtr(ll_ptrtype=PS) assert annotation_to_lltype(s_p) == PS si0 = SomeInstance(DummyClassDef(), True) with py.test.raises(ValueError): annotation_to_lltype(si0) s_singlefloat = SomeSingleFloat() s_singlefloat.const = r_singlefloat(0.0) assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
def test_cast_pointer(): S = lltype.GcStruct('s', ('x', lltype.Signed)) S1 = lltype.GcStruct('s1', ('sub', S)) S2 = lltype.GcStruct('s2', ('sub', S1)) PS = lltype.Ptr(S) PS2 = lltype.Ptr(S2) def lldown(p): return lltype.cast_pointer(PS, p) s, t = ll_rtype(lldown, [SomePtr(PS2)]) assert s.ll_ptrtype == PS def llup(p): return lltype.cast_pointer(PS2, p) s, t = ll_rtype(llup, [SomePtr(PS)]) assert s.ll_ptrtype == PS2
def specialize_call(self, hop): from rpython.rtyper.llannotation import SomePtr TP = hop.args_s[0].const lambda_func = hop.args_s[1].const ll_func = lambda_func() args_s = [SomePtr(lltype.Ptr(TP))] funcptr = hop.rtyper.annotate_helper_fn(ll_func, args_s) hop.exception_cannot_occur() lltype.attachRuntimeTypeInfo(TP, destrptr=funcptr)
def s_list_of_gcrefs(): global _cache_s_list_of_gcrefs if _cache_s_list_of_gcrefs is None: from rpython.annotator import model as annmodel from rpython.rtyper.llannotation import SomePtr from rpython.annotator.listdef import ListDef s_gcref = SomePtr(llmemory.GCREF) _cache_s_list_of_gcrefs = annmodel.SomeList( ListDef(None, s_gcref, mutated=True, resized=False)) return _cache_s_list_of_gcrefs
def compute_result_annotation(self, s_F, s_callable): assert s_F.is_constant() assert s_callable.is_constant() F = s_F.const FUNC = F.TO args_s = [lltype_to_annotation(T) for T in FUNC.ARGS] key = (llhelper, s_callable.const) s_res = self.bookkeeper.emulate_pbc_call(key, s_callable, args_s) assert lltype_to_annotation(FUNC.RESULT).contains(s_res) return SomePtr(F)
def test_cast_simple_widening(self): S2 = Struct("s2", ('a', Signed)) S1 = Struct("s1", ('sub1', S2), ('sub2', S2)) PS1 = Ptr(S1) PS2 = Ptr(S2) def llf(p1): p2 = p1.sub1 p3 = cast_pointer(PS1, p2) return p3 s = self.annotate(llf, [SomePtr(PS1)]) assert isinstance(s, SomePtr) assert s.ll_ptrtype == PS1
def need_stacklet_support(self, gctransformer, getfn): shadow_stack_pool = self.shadow_stack_pool SHADOWSTACKREF = get_shadowstackref(self, gctransformer) def gc_shadowstackref_new(): ssref = shadow_stack_pool.allocate(SHADOWSTACKREF) return lltype.cast_opaque_ptr(llmemory.GCREF, ssref) def gc_shadowstackref_context(gcref): ssref = lltype.cast_opaque_ptr(lltype.Ptr(SHADOWSTACKREF), gcref) return ssref.context def gc_save_current_state_away(gcref, ncontext): ssref = lltype.cast_opaque_ptr(lltype.Ptr(SHADOWSTACKREF), gcref) shadow_stack_pool.save_current_state_away(ssref, ncontext) def gc_forget_current_state(): shadow_stack_pool.forget_current_state() def gc_restore_state_from(gcref): ssref = lltype.cast_opaque_ptr(lltype.Ptr(SHADOWSTACKREF), gcref) shadow_stack_pool.restore_state_from(ssref) def gc_start_fresh_new_state(): shadow_stack_pool.start_fresh_new_state() s_gcref = SomePtr(llmemory.GCREF) s_addr = SomeAddress() self.gc_shadowstackref_new_ptr = getfn(gc_shadowstackref_new, [], s_gcref, minimal_transform=False) self.gc_shadowstackref_context_ptr = getfn(gc_shadowstackref_context, [s_gcref], s_addr, inline=True) self.gc_save_current_state_away_ptr = getfn(gc_save_current_state_away, [s_gcref, s_addr], annmodel.s_None, inline=True) self.gc_forget_current_state_ptr = getfn(gc_forget_current_state, [], annmodel.s_None, inline=True) self.gc_restore_state_from_ptr = getfn(gc_restore_state_from, [s_gcref], annmodel.s_None, inline=True) self.gc_start_fresh_new_state_ptr = getfn(gc_start_fresh_new_state, [], annmodel.s_None, inline=True)
def compute_result_annotation(self, s_F, s_callable): from rpython.annotator.description import FunctionDesc assert s_F.is_constant() assert isinstance(s_callable, annmodel.SomePBC) F = s_F.const FUNC = F.TO args_s = [lltype_to_annotation(T) for T in FUNC.ARGS] for desc in s_callable.descriptions: assert isinstance(desc, FunctionDesc) assert desc.pyobj is not None if s_callable.is_constant(): assert s_callable.const is desc.pyobj key = (llhelper, desc.pyobj) s_res = self.bookkeeper.emulate_pbc_call(key, s_callable, args_s) assert lltype_to_annotation(FUNC.RESULT).contains(s_res) return SomePtr(F)
def attachRuntimeTypeInfoFunc(self, GCSTRUCT, func, ARG_GCSTRUCT=None, destrptr=None): self.call_all_setups() # compute ForwardReferences now if ARG_GCSTRUCT is None: ARG_GCSTRUCT = GCSTRUCT args_s = [SomePtr(Ptr(ARG_GCSTRUCT))] graph = self.annotate_helper(func, args_s) s = self.annotation(graph.getreturnvar()) if (not isinstance(s, SomePtr) or s.ll_ptrtype != Ptr(RuntimeTypeInfo)): raise TyperError("runtime type info function %r returns %r, " "excepted Ptr(RuntimeTypeInfo)" % (func, s)) funcptr = self.getcallable(graph) attachRuntimeTypeInfo(GCSTRUCT, funcptr, destrptr)
def replace_force_virtualizable(self, rtyper, graphs): from rpython.annotator import model as annmodel from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator graph = graphs[0] for block, op in graph.iterblockops(): if op.opname == 'jit_force_virtualizable': v_inst_ll_type = op.args[0].concretetype break def mycall(vinst_ll): if vinst_ll.vable_token: raise ValueError annhelper = MixLevelHelperAnnotator(rtyper) s_vinst = SomePtr(v_inst_ll_type) funcptr = annhelper.delayedfunction(mycall, [s_vinst], annmodel.s_None) annhelper.finish() replace_force_virtualizable_with_call(graphs, v_inst_ll_type, funcptr) return funcptr
def compute_result_annotation(self, s_gcref): from rpython.rtyper.llannotation import SomePtr assert SomePtr(llmemory.GCREF).contains(s_gcref) return s_list_of_gcrefs()
def compute_result_annotation(self, s_Class): from rpython.rtyper.llannotation import SomePtr from rpython.rtyper.rclass import CLASSTYPE assert s_Class.is_constant() return SomePtr(CLASSTYPE)
def compute_result_annotation(self): from rpython.rtyper.llannotation import SomePtr from rpython.memory.gc.base import ARRAY_TYPEID_MAP return SomePtr(lltype.Ptr(ARRAY_TYPEID_MAP))
def make_exception_matcher(self, rtyper): # ll_exception_matcher(real_exception_vtable, match_exception_vtable) s_typeptr = SomePtr(self.lltype_of_exception_type) helper_fn = rtyper.annotate_helper_fn(ll_issubclass, [s_typeptr, s_typeptr]) return helper_fn
def make_type_of_exc_inst(self, rtyper): # ll_type_of_exc_inst(exception_instance) -> exception_vtable s_excinst = SomePtr(self.lltype_of_exception_value) helper_fn = rtyper.annotate_helper_fn(ll_type, [s_excinst]) return helper_fn
def compute_result_annotation(self, s_list): from rpython.rtyper.lltypesystem import lltype, rffi from rpython.rtyper.llannotation import SomePtr _check_resizable_list_of_chars(s_list) return SomePtr(rffi.CCHARP)
def setup_class(cls): cls.marker = lltype.malloc(rffi.CArray(lltype.Signed), 1, flavor='raw', zero=True) funcs0 = [] funcs2 = [] cleanups = [] name_to_func = {} mixlevelstuff = [] for fullname in dir(cls): if not fullname.startswith('define'): continue definefunc = getattr(cls, fullname) _, name = fullname.split('_', 1) func_fixup = definefunc.im_func(cls) cleanup = None if isinstance(func_fixup, tuple): func, cleanup, fixup = func_fixup mixlevelstuff.append(fixup) else: func = func_fixup func.func_name = "f_%s" % name if cleanup: cleanup.func_name = "clean_%s" % name nargs = len(inspect.getargspec(func)[0]) name_to_func[name] = len(funcs0) if nargs == 2: funcs2.append(func) funcs0.append(None) elif nargs == 0: funcs0.append(func) funcs2.append(None) else: raise NotImplementedError( "defined test functions should have 0/2 arguments") # used to let test cleanup static root pointing to runtime # allocated stuff cleanups.append(cleanup) def entrypoint(args): num = args[0] func = funcs0[num] if func: res = func() else: func = funcs2[num] res = func(args[1], args[2]) cleanup = cleanups[num] if cleanup: cleanup() return res from rpython.translator.c.genc import CStandaloneBuilder s_args = SomePtr(lltype.Ptr(ARGS)) t = rtype(entrypoint, [s_args], gcname=cls.gcname, taggedpointers=cls.taggedpointers) for fixup in mixlevelstuff: if fixup: fixup(t) cbuild = CStandaloneBuilder(t, entrypoint, config=t.config, gcpolicy=cls.gcpolicy) db = cbuild.generate_graphs_for_llinterp() entrypointptr = cbuild.getentrypointptr() entrygraph = entrypointptr._obj.graph if option.view: t.viewcg() cls.name_to_func = name_to_func cls.entrygraph = entrygraph cls.rtyper = t.rtyper cls.db = db
def ptr(ll_type): from rpython.rtyper.lltypesystem.lltype import Ptr from rpython.rtyper.llannotation import SomePtr return SomePtr(Ptr(ll_type))
def compute_result_annotation(self, s_PTR, s_object): assert s_PTR.is_constant() if isinstance(s_PTR.const, lltype.Ptr): return SomePtr(s_PTR.const) else: assert False
def compute_result_annotation(self, s_type, **s_fields): TP = s_type.const if not isinstance(TP, lltype.Struct): raise TypeError("make called with %s instead of Struct as first argument" % TP) return SomePtr(lltype.Ptr(TP))
def compute_result_annotation(self): from rpython.rtyper.llannotation import SomePtr return SomePtr(lltype.Ptr(ARRAY_OF_CHAR))
def compute_result_annotation(self): from rpython.rtyper.llannotation import SomePtr from rpython.rtyper.lltypesystem import llgroup return SomePtr(lltype.Ptr(lltype.Array(llgroup.HALFWORD)))
def test_ll_union(): PS1 = lltype.Ptr(lltype.GcStruct('s')) PS2 = lltype.Ptr(lltype.GcStruct('s')) PS3 = lltype.Ptr(lltype.GcStruct('s3')) PA1 = lltype.Ptr(lltype.GcArray()) PA2 = lltype.Ptr(lltype.GcArray()) assert union(SomePtr(PS1), SomePtr(PS1)) == SomePtr(PS1) assert union(SomePtr(PS1), SomePtr(PS2)) == SomePtr(PS2) assert union(SomePtr(PS1), SomePtr(PS2)) == SomePtr(PS1) assert union(SomePtr(PA1), SomePtr(PA1)) == SomePtr(PA1) assert union(SomePtr(PA1), SomePtr(PA2)) == SomePtr(PA2) assert union(SomePtr(PA1), SomePtr(PA2)) == SomePtr(PA1) assert union(SomePtr(PS1), SomeImpossibleValue()) == SomePtr(PS1) assert union(SomeImpossibleValue(), SomePtr(PS1)) == SomePtr(PS1) with py.test.raises(UnionError): union(SomePtr(PA1), SomePtr(PS1)) with py.test.raises(UnionError): union(SomePtr(PS1), SomePtr(PS3)) with py.test.raises(UnionError): union(SomePtr(PS1), SomeInteger()) with py.test.raises(UnionError): union(SomeInteger(), SomePtr(PS1))
return cast_gcref_to_instance(AbstractValue, llref) def _cast_to_resop(llref): from rpython.jit.metainterp.resoperation import AbstractResOp return cast_gcref_to_instance(AbstractResOp, llref) _cast_to_gcref = cast_instance_to_gcref def emptyval(): return lltype.nullptr(llmemory.GCREF.TO) @register_helper(SomePtr(llmemory.GCREF)) def resop_new(no, llargs, llres): from rpython.jit.metainterp.history import ResOperation args = [_cast_to_box(llargs[i]) for i in range(len(llargs))] if llres: res = _cast_to_box(llres) else: res = None return _cast_to_gcref(ResOperation(no, args, res)) @register_helper(annmodel.SomeInteger()) def resop_getopnum(llop): return _cast_to_resop(llop).getopnum()
def compute_result_annotation(self, s_arg): from rpython.rtyper.llannotation import SomePtr from rpython.rtyper.lltypesystem import llmemory return SomePtr(llmemory.GCREF)