<s> import py <EOL> from pypy . translator . backendopt . all import backend_optimizations <EOL> from pypy . translator . backendopt . all import INLINE_THRESHOLD_FOR_TEST <EOL> from pypy . translator . backendopt . support import md5digest <EOL> from pypy . translator . backendopt . test . test_malloc import TestLLTypeMallocRemoval as LLTypeMallocRemovalTest <EOL> from pypy . translator . backendopt . test . test_malloc import TestOOTypeMallocRemoval as OOTypeMallocRemovalTest <EOL> from pypy . translator . translator import TranslationContext , graphof <EOL> from pypy . objspace . flow . model import Constant , summary <EOL> from pypy . annotation import model as annmodel <EOL> from pypy . rpython . llinterp import LLInterpreter <EOL> from pypy . rlib . rarithmetic import intmask <EOL> from pypy import conftest <EOL> class A : <EOL> def __init__ ( self , x , y ) : <EOL> self . bounds = ( x , y ) <EOL> def mean ( self , percentage = <NUM_LIT:50> ) : <EOL> x , y = self . bounds <EOL> total = x * percentage + y * ( <NUM_LIT:100> - percentage ) <EOL> return total // <NUM_LIT:100> <EOL> def condition ( n ) : <EOL> return n >= <NUM_LIT:100> <EOL> def firstthat ( function , condition ) : <EOL> for n in range ( <NUM_LIT> ) : <EOL> if condition ( function ( n ) ) : <EOL> return n <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def myfunction ( n ) : <EOL> a = A ( <NUM_LIT> , n ) <EOL> return a . mean ( ) <EOL> def big ( ) : <EOL> """<STR_LIT>""" <EOL> return firstthat ( myfunction , condition ) <EOL> LARGE_THRESHOLD = <NUM_LIT:10> * INLINE_THRESHOLD_FOR_TEST <EOL> HUGE_THRESHOLD = <NUM_LIT:100> * INLINE_THRESHOLD_FOR_TEST <EOL> class BaseTester ( object ) : <EOL> type_system = None <EOL> def translateopt ( self , func , sig , ** optflags ) : <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( func , sig ) <EOL> t . buildrtyper ( type_system = self . type_system ) . specialize ( ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> backend_optimizations ( t , ** optflags ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> return t <EOL> def test_big ( self ) : <EOL> assert big ( ) == <NUM_LIT> <EOL> t = self . translateopt ( big , [ ] , inline_threshold = HUGE_THRESHOLD , <EOL> mallocs = True ) <EOL> big_graph = graphof ( t , big ) <EOL> self . check_malloc_removed ( big_graph ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( big_graph , [ ] ) <EOL> assert res == <NUM_LIT> <EOL> def test_for_loop ( self ) : <EOL> def f ( n ) : <EOL> total = <NUM_LIT:0> <EOL> for i in range ( n ) : <EOL> total += i <EOL> return total <EOL> t = self . translateopt ( f , [ int ] , mallocs = True ) <EOL> f_graph = graph = graphof ( t , f ) <EOL> self . check_malloc_removed ( f_graph ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( f_graph , [ <NUM_LIT:11> ] ) <EOL> assert res == <NUM_LIT> <EOL> def test_premature_death ( self ) : <EOL> import os <EOL> from pypy . annotation . listdef import s_list_of_strings <EOL> inputtypes = [ s_list_of_strings ] <EOL> def debug ( msg ) : <EOL> os . write ( <NUM_LIT:2> , "<STR_LIT>" + msg + '<STR_LIT:\n>' ) <EOL> def entry_point ( argv ) : <EOL> for arg in argv : <EOL> r = arg . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> a = r . lower ( ) <EOL> return <NUM_LIT:0> <EOL> t = self . translateopt ( entry_point , inputtypes , mallocs = True ) <EOL> entry_point_graph = graphof ( t , entry_point ) <EOL> argv = t . rtyper . getrepr ( inputtypes [ <NUM_LIT:0> ] ) . convert_const ( [ '<STR_LIT>' ] ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> interp . eval_graph ( entry_point_graph , [ argv ] ) <EOL> def test_idempotent ( self ) : <EOL> def s ( x ) : <EOL> res = <NUM_LIT:0> <EOL> i = <NUM_LIT:1> <EOL> while i <= x : <EOL> res += i <EOL> i += <NUM_LIT:1> <EOL> return res <EOL> def g ( x ) : <EOL> return s ( <NUM_LIT:100> ) + s ( <NUM_LIT:1> ) + x <EOL> def idempotent ( n1 , n2 ) : <EOL> c = [ i for i in range ( n2 ) ] <EOL> return <NUM_LIT> + big ( ) + g ( <NUM_LIT:10> ) <EOL> t = self . translateopt ( idempotent , [ int , int ] , <EOL> raisingop2direct_call = True , <EOL> constfold = False ) <EOL> digest1 = md5digest ( t ) <EOL> digest2 = md5digest ( t ) <EOL> def compare ( digest1 , digest2 ) : <EOL> diffs = [ ] <EOL> assert digest1 . keys ( ) == digest2 . keys ( ) <EOL> for name in digest1 : <EOL> if digest1 [ name ] != digest2 [ name ] : <EOL> diffs . append ( name ) <EOL> assert not diffs <EOL> compare ( digest1 , digest2 ) <EOL> backend_optimizations ( t , raisingop2direct_call = True , <EOL> inline_threshold = <NUM_LIT:0> , constfold = False ) <EOL> digest3 = md5digest ( t ) <EOL> compare ( digest1 , digest3 ) <EOL> def test_bug_inlined_if ( self ) : <EOL> def f ( x , flag ) : <EOL> if flag : <EOL> y = x <EOL> else : <EOL> y = x + <NUM_LIT:1> <EOL> return y * <NUM_LIT:5> <EOL> def myfunc ( x ) : <EOL> return f ( x , False ) - f ( x , True ) <EOL> assert myfunc ( <NUM_LIT:10> ) == <NUM_LIT:5> <EOL> t = self . translateopt ( myfunc , [ int ] , inline_threshold = HUGE_THRESHOLD ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( graphof ( t , myfunc ) , [ <NUM_LIT:10> ] ) <EOL> assert res == <NUM_LIT:5> <EOL> def test_range_iter ( self ) : <EOL> def fn ( start , stop , step ) : <EOL> res = <NUM_LIT:0> <EOL> if step == <NUM_LIT:0> : <EOL> if stop >= start : <EOL> r = range ( start , stop , <NUM_LIT:1> ) <EOL> else : <EOL> r = range ( start , stop , - <NUM_LIT:1> ) <EOL> else : <EOL> r = range ( start , stop , step ) <EOL> for i in r : <EOL> res = res * <NUM_LIT> + i <EOL> return res <EOL> t = self . translateopt ( fn , [ int , int , int ] , merge_if_blocks = True ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> for args in [ <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:0> ] , [ <NUM_LIT:7> , <NUM_LIT:2> , <NUM_LIT:0> ] , [ <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT:7> ] , [ <NUM_LIT:50> , - <NUM_LIT:10> , - <NUM_LIT:3> ] : <EOL> assert interp . eval_graph ( graphof ( t , fn ) , args ) == intmask ( fn ( * args ) ) <EOL> def test_constant_diffuse ( self ) : <EOL> def g ( x , y ) : <EOL> if x < <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return x + y <EOL> def f ( x ) : <EOL> return g ( x , <NUM_LIT:7> ) + g ( x , <NUM_LIT:11> ) <EOL> t = self . translateopt ( f , [ int ] ) <EOL> fgraph = graphof ( t , f ) <EOL> for link in fgraph . iterlinks ( ) : <EOL> assert Constant ( <NUM_LIT:7> ) not in link . args <EOL> assert Constant ( <NUM_LIT:11> ) not in link . args <EOL> def test_isinstance ( self ) : <EOL> class A : <EOL> pass <EOL> class B ( A ) : <EOL> pass <EOL> def g ( n ) : <EOL> if n > <NUM_LIT:10> : <EOL> return A ( ) <EOL> else : <EOL> b = B ( ) <EOL> b . value = <NUM_LIT> <EOL> return b <EOL> def fn ( n ) : <EOL> x = g ( n ) <EOL> assert isinstance ( x , B ) <EOL> return x . value <EOL> t = self . translateopt ( fn , [ int ] , really_remove_asserts = True , <EOL> remove_asserts = True ) <EOL> graph = graphof ( t , fn ) <EOL> assert "<STR_LIT>" not in summary ( graph ) <EOL> class TestLLType ( BaseTester ) : <EOL> type_system = '<STR_LIT>' <EOL> check_malloc_removed = LLTypeMallocRemovalTest . check_malloc_removed <EOL> def test_list_comp ( self ) : <EOL> def f ( n1 , n2 ) : <EOL> c = [ i for i in range ( n2 ) ] <EOL> return <NUM_LIT> <EOL> t = self . translateopt ( f , [ int , int ] , inline_threshold = LARGE_THRESHOLD , <EOL> mallocs = True ) <EOL> f_graph = graphof ( t , f ) <EOL> self . check_malloc_removed ( f_graph ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( f_graph , [ <NUM_LIT:11> , <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> def test_secondary_backendopt ( self ) : <EOL> def common ( n ) : <EOL> if n > <NUM_LIT:5> : <EOL> raise ValueError <EOL> def main ( n ) : <EOL> common ( n ) <EOL> def later ( n ) : <EOL> try : <EOL> common ( n ) <EOL> return <NUM_LIT:0> <EOL> except ValueError : <EOL> return <NUM_LIT:1> <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( main , [ int ] ) <EOL> t . buildrtyper ( type_system = '<STR_LIT>' ) . specialize ( ) <EOL> exctransformer = t . getexceptiontransformer ( ) <EOL> exctransformer . create_exception_handling ( graphof ( t , common ) ) <EOL> from pypy . annotation import model as annmodel <EOL> from pypy . rpython . annlowlevel import MixLevelHelperAnnotator <EOL> annhelper = MixLevelHelperAnnotator ( t . rtyper ) <EOL> later_graph = annhelper . getgraph ( later , [ annmodel . SomeInteger ( ) ] , <EOL> annmodel . SomeInteger ( ) ) <EOL> annhelper . finish ( ) <EOL> annhelper . backend_optimize ( ) <EOL> if conftest . option . view : <EOL> later_graph . show ( ) <EOL> common_graph = graphof ( t , common ) <EOL> found = False <EOL> for block in later_graph . iterblocks ( ) : <EOL> for op in block . operations : <EOL> if ( op . opname == '<STR_LIT>' and <EOL> op . args [ <NUM_LIT:0> ] . value . _obj . graph is common_graph ) : <EOL> found = True <EOL> assert found , "<STR_LIT>" <EOL> from pypy . rpython . llinterp import LLInterpreter <EOL> llinterp = LLInterpreter ( t . rtyper ) <EOL> res = llinterp . eval_graph ( later_graph , [ <NUM_LIT:10> ] ) <EOL> assert res == <NUM_LIT:1> <EOL> class TestOOType ( BaseTester ) : <EOL> type_system = '<STR_LIT>' <EOL> check_malloc_removed = OOTypeMallocRemovalTest . check_malloc_removed </s>
