<s> """<STR_LIT>""" <EOL> import py <EOL> from pypy . jit . metainterp . history import ResOperation , BoxInt , ConstInt , BoxPtr , ConstPtr , LoopToken , BasicFailDescr <EOL> from pypy . jit . metainterp . resoperation import rop , ResOperation <EOL> from pypy . jit . backend . llsupport . descr import GcCache <EOL> from pypy . jit . backend . x86 . runner import CPU <EOL> from pypy . jit . backend . x86 . regalloc import RegAlloc , WORD , X86RegisterManager , BASE_CONSTANT_SIZE <EOL> from pypy . jit . metainterp . test . oparser import parse <EOL> from pypy . rpython . lltypesystem import lltype , llmemory , rffi <EOL> from pypy . rpython . annlowlevel import llhelper <EOL> from pypy . rpython . lltypesystem import rclass , rstr <EOL> from pypy . jit . backend . x86 . ri386 import * <EOL> class MockGcDescr ( GcCache ) : <EOL> def get_funcptr_for_new ( self ) : <EOL> return <NUM_LIT> <EOL> get_funcptr_for_newarray = get_funcptr_for_new <EOL> get_funcptr_for_newstr = get_funcptr_for_new <EOL> get_funcptr_for_newunicode = get_funcptr_for_new <EOL> def rewrite_assembler ( self , cpu , operations ) : <EOL> pass <EOL> class MockAssembler ( object ) : <EOL> gcrefs = None <EOL> def __init__ ( self , cpu = None , gc_ll_descr = None ) : <EOL> self . movs = [ ] <EOL> self . performs = [ ] <EOL> self . lea = [ ] <EOL> self . cpu = cpu or CPU ( None , None ) <EOL> if gc_ll_descr is None : <EOL> gc_ll_descr = MockGcDescr ( False ) <EOL> self . cpu . gc_ll_descr = gc_ll_descr <EOL> def dump ( self , * args ) : <EOL> pass <EOL> def regalloc_mov ( self , from_loc , to_loc ) : <EOL> self . movs . append ( ( from_loc , to_loc ) ) <EOL> def regalloc_perform ( self , op , arglocs , resloc ) : <EOL> self . performs . append ( ( op , arglocs , resloc ) ) <EOL> def regalloc_perform_discard ( self , op , arglocs ) : <EOL> self . performs . append ( ( op , arglocs ) ) <EOL> def load_effective_addr ( self , * args ) : <EOL> self . lea . append ( args ) <EOL> def fill_regs ( regalloc , cls = BoxInt ) : <EOL> allboxes = [ ] <EOL> for reg in X86RegisterManager . all_regs : <EOL> box = cls ( ) <EOL> allboxes . append ( box ) <EOL> regalloc . rm . try_allocate_reg ( ) <EOL> return allboxes <EOL> class RegAllocForTests ( RegAlloc ) : <EOL> position = <NUM_LIT:0> <EOL> def _compute_next_usage ( self , v , _ ) : <EOL> return - <NUM_LIT:1> <EOL> class BaseTestRegalloc ( object ) : <EOL> cpu = CPU ( None , None ) <EOL> def raising_func ( i ) : <EOL> if i : <EOL> raise LLException ( zero_division_error , <EOL> zero_division_value ) <EOL> FPTR = lltype . Ptr ( lltype . FuncType ( [ lltype . Signed ] , lltype . Void ) ) <EOL> raising_fptr = llhelper ( FPTR , raising_func ) <EOL> zero_division_tp , zero_division_value = cpu . get_zero_division_error ( ) <EOL> zd_addr = cpu . cast_int_to_adr ( zero_division_tp ) <EOL> zero_division_error = llmemory . cast_adr_to_ptr ( zd_addr , <EOL> lltype . Ptr ( rclass . OBJECT_VTABLE ) ) <EOL> raising_calldescr = cpu . calldescrof ( FPTR . TO , FPTR . TO . ARGS , FPTR . TO . RESULT ) <EOL> fdescr1 = BasicFailDescr ( <NUM_LIT:1> ) <EOL> fdescr2 = BasicFailDescr ( <NUM_LIT:2> ) <EOL> fdescr3 = BasicFailDescr ( <NUM_LIT:3> ) <EOL> namespace = locals ( ) . copy ( ) <EOL> type_system = '<STR_LIT>' <EOL> def parse ( self , s , boxkinds = None ) : <EOL> return parse ( s , self . cpu , self . namespace , <EOL> type_system = self . type_system , <EOL> boxkinds = boxkinds ) <EOL> def interpret ( self , ops , args , run = True ) : <EOL> loop = self . parse ( ops ) <EOL> self . cpu . compile_loop ( loop . inputargs , loop . operations , loop . token ) <EOL> for i , arg in enumerate ( args ) : <EOL> if isinstance ( arg , int ) : <EOL> self . cpu . set_future_value_int ( i , arg ) <EOL> elif isinstance ( arg , float ) : <EOL> self . cpu . set_future_value_float ( i , arg ) <EOL> else : <EOL> assert isinstance ( lltype . typeOf ( arg ) , lltype . Ptr ) <EOL> llgcref = lltype . cast_opaque_ptr ( llmemory . GCREF , arg ) <EOL> self . cpu . set_future_value_ref ( i , llgcref ) <EOL> if run : <EOL> self . cpu . execute_token ( loop . token ) <EOL> return loop <EOL> def getint ( self , index ) : <EOL> return self . cpu . get_latest_value_int ( index ) <EOL> def getfloat ( self , index ) : <EOL> return self . cpu . get_latest_value_float ( index ) <EOL> def getints ( self , end ) : <EOL> return [ self . cpu . get_latest_value_int ( index ) for <EOL> index in range ( <NUM_LIT:0> , end ) ] <EOL> def getfloats ( self , end ) : <EOL> return [ self . cpu . get_latest_value_float ( index ) for <EOL> index in range ( <NUM_LIT:0> , end ) ] <EOL> def getptr ( self , index , T ) : <EOL> gcref = self . cpu . get_latest_value_ref ( index ) <EOL> return lltype . cast_opaque_ptr ( T , gcref ) <EOL> def attach_bridge ( self , ops , loop , guard_op_index , looptoken = None , ** kwds ) : <EOL> if looptoken is not None : <EOL> self . namespace = self . namespace . copy ( ) <EOL> self . namespace [ '<STR_LIT>' ] = looptoken <EOL> guard_op = loop . operations [ guard_op_index ] <EOL> assert guard_op . is_guard ( ) <EOL> bridge = self . parse ( ops , ** kwds ) <EOL> assert ( [ box . type for box in bridge . inputargs ] == <EOL> [ box . type for box in guard_op . fail_args ] ) <EOL> faildescr = guard_op . descr <EOL> self . cpu . compile_bridge ( faildescr , bridge . inputargs , bridge . operations ) <EOL> return bridge <EOL> def run ( self , loop ) : <EOL> return self . cpu . execute_token ( loop . token ) <EOL> class TestRegallocSimple ( BaseTestRegalloc ) : <EOL> def test_simple_loop ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:20> <EOL> def test_two_loops_and_a_bridge ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> ops2 = '''<STR_LIT>''' <EOL> loop2 = self . interpret ( ops2 , [ <NUM_LIT:0> ] ) <EOL> bridge_ops = '''<STR_LIT>''' <EOL> bridge = self . attach_bridge ( bridge_ops , loop2 , <NUM_LIT:4> , looptoken = loop . token ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . run ( loop2 ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT> <EOL> assert self . getint ( <NUM_LIT:1> ) == <NUM_LIT:30> <EOL> assert self . getint ( <NUM_LIT:2> ) == <NUM_LIT:30> <EOL> assert self . getint ( <NUM_LIT:3> ) == <NUM_LIT:30> <EOL> def test_pointer_arg ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> S = lltype . GcStruct ( '<STR_LIT:S>' ) <EOL> ptr = lltype . malloc ( S ) <EOL> self . interpret ( ops , [ <NUM_LIT:0> , ptr ] ) <EOL> assert self . getptr ( <NUM_LIT:0> , lltype . Ptr ( S ) ) == ptr <EOL> assert not self . cpu . assembler . fail_boxes_ptr . getitem ( <NUM_LIT:0> ) <EOL> assert not self . cpu . assembler . fail_boxes_ptr . getitem ( <NUM_LIT:1> ) <EOL> def test_exception_bridge_no_exception ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> bridge_ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:1> <EOL> bridge = self . attach_bridge ( bridge_ops , loop , <NUM_LIT:2> ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . run ( loop ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:1> <EOL> def test_inputarg_unused ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> ] ) <EOL> def test_nested_guards ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> bridge_ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:10> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> assert self . getint ( <NUM_LIT:1> ) == <NUM_LIT:10> <EOL> bridge = self . attach_bridge ( bridge_ops , loop , <NUM_LIT:0> ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> self . run ( loop ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> assert self . getint ( <NUM_LIT:1> ) == <NUM_LIT:10> <EOL> def test_nested_unused_arg ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> bridge_ops = '''<STR_LIT>''' <EOL> self . attach_bridge ( bridge_ops , loop , <NUM_LIT:0> ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . cpu . set_future_value_int ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . run ( loop ) <EOL> def test_spill_for_constant ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getints ( <NUM_LIT:4> ) == [ <NUM_LIT:1> , <NUM_LIT:30> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> def test_spill_for_constant_lshift ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getints ( <NUM_LIT:4> ) == [ <NUM_LIT:1> << <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getints ( <NUM_LIT:4> ) == [ <NUM_LIT:1> << <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getints ( <NUM_LIT:4> ) == [ <NUM_LIT:1> << <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> def test_result_selected_reg_via_neg ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> assert self . getints ( <NUM_LIT:3> ) == [ <NUM_LIT:1> , - <NUM_LIT:3> , <NUM_LIT:10> ] <EOL> def test_compare_memory_result_survives ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:1> <EOL> def test_jump_different_args ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_op_result_unused ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> def test_guard_value_two_boxes ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> def test_bug_wrong_stack_adj ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> bridge_ops = '''<STR_LIT>''' <EOL> self . attach_bridge ( bridge_ops , loop , <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> self . cpu . set_future_value_int ( i , i ) <EOL> self . run ( loop ) <EOL> assert self . getints ( <NUM_LIT:9> ) == range ( <NUM_LIT:9> ) <EOL> class TestRegallocCompOps ( BaseTestRegalloc ) : <EOL> def test_cmp_op_0 ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> class TestRegallocMoreRegisters ( BaseTestRegalloc ) : <EOL> cpu = BaseTestRegalloc . cpu <EOL> S = lltype . GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT>' , lltype . Char ) ) <EOL> fielddescr = cpu . fielddescrof ( S , '<STR_LIT>' ) <EOL> A = lltype . GcArray ( lltype . Char ) <EOL> arraydescr = cpu . arraydescrof ( A ) <EOL> namespace = locals ( ) . copy ( ) <EOL> def test_int_is_true ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> assert self . getints ( <NUM_LIT:8> ) == [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> def test_comparison_ops ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> assert self . getints ( <NUM_LIT:6> ) == [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> def test_nullity ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> assert self . getints ( <NUM_LIT:6> ) == [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def test_strsetitem ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> llstr = rstr . mallocstr ( <NUM_LIT:10> ) <EOL> self . interpret ( ops , [ llstr , ord ( '<STR_LIT:a>' ) ] ) <EOL> assert llstr . chars [ <NUM_LIT:1> ] == '<STR_LIT:a>' <EOL> def test_setfield_char ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> s = lltype . malloc ( self . S ) <EOL> self . interpret ( ops , [ s , ord ( '<STR_LIT:a>' ) ] ) <EOL> assert s . field == '<STR_LIT:a>' <EOL> def test_setarrayitem_gc ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> s = lltype . malloc ( self . A , <NUM_LIT:3> ) <EOL> self . interpret ( ops , [ s , ord ( '<STR_LIT:a>' ) ] ) <EOL> assert s [ <NUM_LIT:1> ] == '<STR_LIT:a>' <EOL> class TestRegallocFloats ( BaseTestRegalloc ) : <EOL> def test_float_add ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert self . getfloats ( <NUM_LIT:3> ) == [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def test_float_adds_stack ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert self . getfloats ( <NUM_LIT:9> ) == [ <NUM_LIT> + <NUM_LIT> , <NUM_LIT> + <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def test_float_overflow_const_list ( self ) : <EOL> ops = [ '<STR_LIT>' ] <EOL> for i in range ( BASE_CONSTANT_SIZE * <NUM_LIT:2> ) : <EOL> ops . append ( '<STR_LIT>' % ( i + <NUM_LIT:1> , i ) ) <EOL> ops . append ( '<STR_LIT>' % ( BASE_CONSTANT_SIZE * <NUM_LIT:2> ) ) <EOL> ops = "<STR_LIT:\n>" . join ( ops ) <EOL> self . interpret ( ops , [ <NUM_LIT:0.1> ] ) <EOL> assert abs ( self . getfloat ( <NUM_LIT:0> ) - ( BASE_CONSTANT_SIZE * <NUM_LIT:2> ) * <NUM_LIT> - <NUM_LIT:0.1> ) < <NUM_LIT> <EOL> def test_lt_const ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> self . interpret ( ops , [ <NUM_LIT:0.1> ] ) <EOL> assert self . getint ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> def test_bug_float_is_true_stack ( self ) : <EOL> ops = '''<STR_LIT>''' <EOL> loop = self . interpret ( ops , [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert self . getints ( <NUM_LIT:9> ) == [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] </s>
