frompypy.config.pypyoptionimportget_pypy_configfrompypy.jit.metainterp.historyimportLoopToken,ConstInt,History,Statsfrompypy.jit.metainterp.historyimportBoxInt,INTfrompypy.jit.metainterp.compileimportinsert_loop_token,compile_new_loopfrompypy.jit.metainterp.compileimportResumeGuardDescrfrompypy.jit.metainterp.compileimportResumeGuardCountersIntfrompypy.jit.metainterp.compileimportcompile_tmp_callbackfrompypy.jit.metainterpimportjitprof,typesystem,compilefrompypy.jit.metainterp.optimizeopt.test.test_utilimportLLtypeMixinfrompypy.jit.tool.oparserimportparsefrompypy.jit.metainterp.optimizeoptimportALL_OPTS_DICTdeftest_insert_loop_token():# XXX this test is a bit useless now that there are no specnodeslst=[]#tok1=LoopToken()insert_loop_token(lst,tok1)assertlst==[tok1]#tok2=LoopToken()insert_loop_token(lst,tok2)assertlst==[tok1,tok2]#tok3=LoopToken()insert_loop_token(lst,tok3)assertlst==[tok1,tok2,tok3]classFakeCPU(object):ts=typesystem.llhelperdef__init__(self):self.seen=[]defcompile_loop(self,inputargs,operations,token,name=''):self.seen.append((inputargs,operations,token))classFakeLogger(object):deflog_loop(self,inputargs,operations,number=0,type=None,ops_offset=None):passdefrepr_of_resop(self,op):returnrepr(op)classFakeState(object):enable_opts=ALL_OPTS_DICT.copy()enable_opts.pop('unroll')defattach_unoptimized_bridge_from_interp(*args):passdefget_location_str(self,args):return'location'classFakeGlobalData(object):loopnumbering=0classFakeMetaInterpStaticData(object):logger_noopt=FakeLogger()logger_ops=FakeLogger()config=get_pypy_config(translating=True)stats=Stats()profiler=jitprof.EmptyProfiler()warmrunnerdesc=Nonedeflog(self,msg,event_kind=None):passclassFakeMetaInterp:call_pure_results={}classjitdriver_sd:warmstate=FakeState()on_compile=staticmethod(lambda*args:None)on_compile_bridge=staticmethod(lambda*args:None)deftest_compile_new_loop():cpu=FakeCPU()staticdata=FakeMetaInterpStaticData()staticdata.cpu=cpustaticdata.globaldata=FakeGlobalData()staticdata.globaldata.loopnumbering=1#loop=parse(''' [p1] i1 = getfield_gc(p1, descr=valuedescr) i2 = int_add(i1, 1) p2 = new_with_vtable(ConstClass(node_vtable)) setfield_gc(p2, i2, descr=valuedescr) jump(p2) ''',namespace=LLtypeMixin.__dict__.copy())#metainterp=FakeMetaInterp()metainterp.staticdata=staticdatametainterp.cpu=cpumetainterp.history=History()metainterp.history.operations=loop.operations[:]metainterp.history.inputargs=loop.inputargs[:]cpu._all_size_descrs_with_vtable=(LLtypeMixin.cpu._all_size_descrs_with_vtable)#loop_tokens=[]loop_token=compile_new_loop(metainterp,loop_tokens,[],0,None)assertloop_tokens==[loop_token]assertloop_token.number==1assertstaticdata.globaldata.loopnumbering==2#assertlen(cpu.seen)==1assertcpu.seen[0][2]==loop_token#delcpu.seen[:]metainterp=FakeMetaInterp()metainterp.staticdata=staticdatametainterp.cpu=cpumetainterp.history=History()metainterp.history.operations=loop.operations[:]metainterp.history.inputargs=loop.inputargs[:]#loop_token_2=compile_new_loop(metainterp,loop_tokens,[],0,None)assertloop_token_2isloop_tokenassertloop_tokens==[loop_token]assertlen(cpu.seen)==0assertstaticdata.globaldata.loopnumbering==2deftest_resume_guard_counters():rgc=ResumeGuardCountersInt()# fill in the tableforiinrange(5):count=rgc.see_int(100+i)assertcount==1count=rgc.see_int(100+i)assertcount==2assertrgc.counters==[0]*(4-i)+[2]*(1+i)foriinrange(5):count=rgc.see_int(100+i)assertcount==3# make a distribution: [5, 4, 7, 6, 3]assertrgc.counters==[3,3,3,3,3]count=rgc.see_int(101)assertcount==4count=rgc.see_int(101)assertcount==5count=rgc.see_int(101)assertcount==6count=rgc.see_int(102)assertcount==4count=rgc.see_int(102)assertcount==5count=rgc.see_int(102)assertcount==6count=rgc.see_int(102)assertcount==7count=rgc.see_int(103)assertcount==4count=rgc.see_int(104)assertcount==4count=rgc.see_int(104)assertcount==5assertrgc.counters==[5,4,7,6,3]# the next new item should throw away 104, as 5 is the middle countercount=rgc.see_int(190)assertcount==1assertrgc.counters==[1,4,7,6,3]# the next new item should throw away 103, as 4 is the middle countercount=rgc.see_int(191)assertcount==1assertrgc.counters==[1,1,7,6,3]# the next new item should throw away 100, as 3 is the middle countercount=rgc.see_int(192)assertcount==1assertrgc.counters==[1,1,7,6,1]deftest_compile_tmp_callback():frompypy.jit.codewriterimportheaptrackerfrompypy.jit.backend.llgraphimportrunnerfrompypy.rpython.lltypesystemimportlltype,llmemoryfrompypy.rpython.annlowlevelimportllhelperfrompypy.rpython.llinterpimportLLException#cpu=runner.LLtypeCPU(None)FUNC=lltype.FuncType([lltype.Signed]*4,lltype.Signed)defll_portal_runner(g1,g2,r3,r4):assert(g1,g2,r3,r4)==(12,34,-156,-178)ifraiseme:raiseraisemeelse:return54321#classFakeJitDriverSD:portal_runner_ptr=llhelper(lltype.Ptr(FUNC),ll_portal_runner)portal_runner_adr=llmemory.cast_ptr_to_adr(portal_runner_ptr)portal_calldescr=cpu.calldescrof(FUNC,FUNC.ARGS,FUNC.RESULT)portal_finishtoken=compile.DoneWithThisFrameDescrInt()num_red_args=2result_type=INT#loop_token=compile_tmp_callback(cpu,FakeJitDriverSD(),[ConstInt(12),ConstInt(34)],[BoxInt(56),ConstInt(78),BoxInt(90)])#raiseme=Nonecpu.set_future_value_int(0,-156)cpu.set_future_value_int(1,-178)cpu.set_future_value_int(2,-190)# passed in, but droppedfail_descr=cpu.execute_token(loop_token)assertfail_descrisFakeJitDriverSD().portal_finishtoken#EXC=lltype.GcStruct('EXC')llexc=lltype.malloc(EXC)raiseme=LLException("exception class",llexc)cpu.set_future_value_int(0,-156)cpu.set_future_value_int(1,-178)cpu.set_future_value_int(2,-190)fail_descr=cpu.execute_token(loop_token)assertisinstance(fail_descr,compile.PropagateExceptionDescr)got=cpu.grab_exc_value()assertlltype.cast_opaque_ptr(lltype.Ptr(EXC),got)==llexc#classFakeMetaInterpSD:classExitFrameWithExceptionRef(Exception):passFakeMetaInterpSD.cpu=cpucpu.set_future_value_int(0,-156)cpu.set_future_value_int(1,-178)cpu.set_future_value_int(2,-190)fail_descr=cpu.execute_token(loop_token)try:fail_descr.handle_fail(FakeMetaInterpSD(),None)exceptFakeMetaInterpSD.ExitFrameWithExceptionRef,e:assertlltype.cast_opaque_ptr(lltype.Ptr(EXC),e.args[1])==llexcelse:assert0,"should have raised"