<s> import sys <EOL> from pypy . rpython . lltypesystem import lltype , llmemory , rstr <EOL> from pypy . rpython . ootypesystem import ootype <EOL> from pypy . rpython . annlowlevel import hlstr , cast_base_ptr_to_instance <EOL> from pypy . rpython . annlowlevel import cast_object_to_ptr <EOL> from pypy . rlib . objectmodel import specialize , we_are_translated , r_dict <EOL> from pypy . rlib . rarithmetic import intmask <EOL> from pypy . rlib . nonconst import NonConstant <EOL> from pypy . rlib . unroll import unrolling_iterable <EOL> from pypy . rlib . jit import PARAMETERS , OPTIMIZER_SIMPLE , OPTIMIZER_FULL <EOL> from pypy . rlib . jit import DEBUG_PROFILE <EOL> from pypy . rlib . jit import BaseJitCell <EOL> from pypy . jit . metainterp import support , history <EOL> @ specialize . arg ( <NUM_LIT:0> ) <EOL> def unwrap ( TYPE , box ) : <EOL> if TYPE is lltype . Void : <EOL> return None <EOL> if isinstance ( TYPE , lltype . Ptr ) : <EOL> return box . getref ( TYPE ) <EOL> if isinstance ( TYPE , ootype . OOType ) : <EOL> return box . getref ( TYPE ) <EOL> if TYPE == lltype . Float : <EOL> return box . getfloat ( ) <EOL> else : <EOL> return lltype . cast_primitive ( TYPE , box . getint ( ) ) <EOL> @ specialize . ll ( ) <EOL> def wrap ( cpu , value , in_const_box = False ) : <EOL> if isinstance ( lltype . typeOf ( value ) , lltype . Ptr ) : <EOL> if lltype . typeOf ( value ) . TO . _gckind == '<STR_LIT>' : <EOL> value = lltype . cast_opaque_ptr ( llmemory . GCREF , value ) <EOL> if in_const_box : <EOL> return history . ConstPtr ( value ) <EOL> else : <EOL> return history . BoxPtr ( value ) <EOL> else : <EOL> adr = llmemory . cast_ptr_to_adr ( value ) <EOL> value = cpu . cast_adr_to_int ( adr ) <EOL> elif isinstance ( lltype . typeOf ( value ) , ootype . OOType ) : <EOL> value = ootype . cast_to_object ( value ) <EOL> if in_const_box : <EOL> return history . ConstObj ( value ) <EOL> else : <EOL> return history . BoxObj ( value ) <EOL> elif isinstance ( value , float ) : <EOL> if in_const_box : <EOL> return history . ConstFloat ( value ) <EOL> else : <EOL> return history . BoxFloat ( value ) <EOL> else : <EOL> value = intmask ( value ) <EOL> if in_const_box : <EOL> return history . ConstInt ( value ) <EOL> else : <EOL> return history . BoxInt ( value ) <EOL> @ specialize . arg ( <NUM_LIT:0> ) <EOL> def equal_whatever ( TYPE , x , y ) : <EOL> if isinstance ( TYPE , lltype . Ptr ) : <EOL> if TYPE . TO is rstr . STR or TYPE . TO is rstr . UNICODE : <EOL> return rstr . LLHelpers . ll_streq ( x , y ) <EOL> if TYPE is ootype . String or TYPE is ootype . Unicode : <EOL> return x . ll_streq ( y ) <EOL> return x == y <EOL> @ specialize . arg ( <NUM_LIT:0> ) <EOL> def hash_whatever ( TYPE , x ) : <EOL> if isinstance ( TYPE , lltype . Ptr ) : <EOL> if TYPE . TO is rstr . STR or TYPE . TO is rstr . UNICODE : <EOL> return rstr . LLHelpers . ll_strhash ( x ) <EOL> else : <EOL> if x : <EOL> return lltype . identityhash ( x ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> elif TYPE is ootype . String or TYPE is ootype . Unicode : <EOL> return x . ll_hash ( ) <EOL> elif isinstance ( TYPE , ootype . OOType ) : <EOL> if x : <EOL> return ootype . identityhash ( x ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return lltype . cast_primitive ( lltype . Signed , x ) <EOL> @ specialize . ll_and_arg ( <NUM_LIT:3> ) <EOL> def set_future_value ( cpu , j , value , typecode ) : <EOL> if typecode == '<STR_LIT>' : <EOL> refvalue = cpu . ts . cast_to_ref ( value ) <EOL> cpu . set_future_value_ref ( j , refvalue ) <EOL> elif typecode == '<STR_LIT:int>' : <EOL> intvalue = lltype . cast_primitive ( lltype . Signed , value ) <EOL> cpu . set_future_value_int ( j , intvalue ) <EOL> elif typecode == '<STR_LIT:float>' : <EOL> assert isinstance ( value , float ) <EOL> cpu . set_future_value_float ( j , value ) <EOL> else : <EOL> assert False <EOL> class WarmEnterState ( object ) : <EOL> THRESHOLD_LIMIT = sys . maxint // <NUM_LIT:2> <EOL> default_jitcell_dict = None <EOL> def __init__ ( self , warmrunnerdesc ) : <EOL> "<STR_LIT>" <EOL> self . warmrunnerdesc = warmrunnerdesc <EOL> try : <EOL> self . profiler = warmrunnerdesc . metainterp_sd . profiler <EOL> except AttributeError : <EOL> self . profiler = None <EOL> for name , default_value in PARAMETERS . items ( ) : <EOL> meth = getattr ( self , '<STR_LIT>' + name ) <EOL> meth ( default_value ) <EOL> def set_param_threshold ( self , threshold ) : <EOL> if threshold < <NUM_LIT:2> : <EOL> threshold = <NUM_LIT:2> <EOL> self . increment_threshold = ( self . THRESHOLD_LIMIT // threshold ) + <NUM_LIT:1> <EOL> def set_param_trace_eagerness ( self , value ) : <EOL> self . trace_eagerness = value <EOL> def set_param_trace_limit ( self , value ) : <EOL> self . trace_limit = value <EOL> def set_param_inlining ( self , value ) : <EOL> self . inlining = value <EOL> def set_param_optimizer ( self , optimizer ) : <EOL> if optimizer == OPTIMIZER_SIMPLE : <EOL> from pypy . jit . metainterp import simple_optimize <EOL> self . optimize_loop = simple_optimize . optimize_loop <EOL> self . optimize_bridge = simple_optimize . optimize_bridge <EOL> elif optimizer == OPTIMIZER_FULL : <EOL> from pypy . jit . metainterp import optimize <EOL> self . optimize_loop = optimize . optimize_loop <EOL> self . optimize_bridge = optimize . optimize_bridge <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def set_param_debug ( self , value ) : <EOL> self . debug_level = value <EOL> if self . profiler is not None : <EOL> self . profiler . set_printing ( value >= DEBUG_PROFILE ) <EOL> def must_compile_from_failure ( self , key ) : <EOL> key . counter += <NUM_LIT:1> <EOL> return key . counter >= self . trace_eagerness <EOL> def reset_counter_from_failure ( self , key ) : <EOL> key . counter = <NUM_LIT:0> <EOL> def attach_unoptimized_bridge_from_interp ( self , greenkey , <EOL> entry_loop_token ) : <EOL> cell = self . jit_cell_at_key ( greenkey ) <EOL> cell . counter = - <NUM_LIT:1> <EOL> cell . entry_loop_token = entry_loop_token <EOL> def make_entry_point ( self ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . maybe_compile_and_run <EOL> metainterp_sd = self . warmrunnerdesc . metainterp_sd <EOL> vinfo = metainterp_sd . virtualizable_info <EOL> ContinueRunningNormally = self . warmrunnerdesc . ContinueRunningNormally <EOL> num_green_args = self . warmrunnerdesc . num_green_args <EOL> get_jitcell = self . make_jitcell_getter ( ) <EOL> set_future_values = self . make_set_future_values ( ) <EOL> self . make_jitdriver_callbacks ( ) <EOL> def maybe_compile_and_run ( * args ) : <EOL> """<STR_LIT>""" <EOL> globaldata = metainterp_sd . globaldata <EOL> if NonConstant ( False ) : <EOL> self . set_param_optimizer ( OPTIMIZER_FULL ) <EOL> if vinfo is not None : <EOL> virtualizable = args [ vinfo . index_of_virtualizable ] <EOL> virtualizable = vinfo . cast_to_vtype ( virtualizable ) <EOL> assert virtualizable != globaldata . blackhole_virtualizable , ( <EOL> "<STR_LIT>" ) <EOL> greenargs = args [ : num_green_args ] <EOL> cell = get_jitcell ( * greenargs ) <EOL> if cell . counter >= <NUM_LIT:0> : <EOL> n = cell . counter + self . increment_threshold <EOL> if n <= self . THRESHOLD_LIMIT : <EOL> cell . counter = n <EOL> return <EOL> from pypy . jit . metainterp . pyjitpl import MetaInterp <EOL> metainterp = MetaInterp ( metainterp_sd ) <EOL> try : <EOL> loop_token = metainterp . compile_and_run_once ( * args ) <EOL> except ContinueRunningNormally : <EOL> cell . counter = <NUM_LIT:0> <EOL> raise <EOL> else : <EOL> set_future_values ( * args [ num_green_args : ] ) <EOL> loop_token = cell . entry_loop_token <EOL> while True : <EOL> metainterp_sd . profiler . start_running ( ) <EOL> fail_index = metainterp_sd . cpu . execute_token ( loop_token ) <EOL> metainterp_sd . profiler . end_running ( ) <EOL> fail_descr = globaldata . get_fail_descr_from_number ( fail_index ) <EOL> loop_token = fail_descr . handle_fail ( metainterp_sd ) <EOL> maybe_compile_and_run . _dont_inline_ = True <EOL> self . maybe_compile_and_run = maybe_compile_and_run <EOL> return maybe_compile_and_run <EOL> def make_unwrap_greenkey ( self ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . unwrap_greenkey <EOL> warmrunnerdesc = self . warmrunnerdesc <EOL> green_args_spec = unrolling_iterable ( warmrunnerdesc . green_args_spec ) <EOL> def unwrap_greenkey ( greenkey ) : <EOL> greenargs = ( ) <EOL> i = <NUM_LIT:0> <EOL> for TYPE in green_args_spec : <EOL> value = unwrap ( TYPE , greenkey [ i ] ) <EOL> greenargs += ( value , ) <EOL> i = i + <NUM_LIT:1> <EOL> return greenargs <EOL> unwrap_greenkey . _always_inline_ = True <EOL> self . unwrap_greenkey = unwrap_greenkey <EOL> return unwrap_greenkey <EOL> def make_jitcell_getter ( self ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . jit_getter <EOL> class JitCell ( BaseJitCell ) : <EOL> counter = <NUM_LIT:0> <EOL> compiled_merge_points = None <EOL> if self . warmrunnerdesc . get_jitcell_at_ptr is None : <EOL> jit_getter = self . _make_jitcell_getter_default ( JitCell ) <EOL> else : <EOL> jit_getter = self . _make_jitcell_getter_custom ( JitCell ) <EOL> unwrap_greenkey = self . make_unwrap_greenkey ( ) <EOL> def jit_cell_at_key ( greenkey ) : <EOL> greenargs = unwrap_greenkey ( greenkey ) <EOL> return jit_getter ( * greenargs ) <EOL> self . jit_cell_at_key = jit_cell_at_key <EOL> self . jit_getter = jit_getter <EOL> return jit_getter <EOL> def _make_jitcell_getter_default ( self , JitCell ) : <EOL> "<STR_LIT>" <EOL> warmrunnerdesc = self . warmrunnerdesc <EOL> green_args_spec = unrolling_iterable ( warmrunnerdesc . green_args_spec ) <EOL> def comparekey ( greenargs1 , greenargs2 ) : <EOL> i = <NUM_LIT:0> <EOL> for TYPE in green_args_spec : <EOL> if not equal_whatever ( TYPE , greenargs1 [ i ] , greenargs2 [ i ] ) : <EOL> return False <EOL> i = i + <NUM_LIT:1> <EOL> return True <EOL> def hashkey ( greenargs ) : <EOL> x = <NUM_LIT> <EOL> i = <NUM_LIT:0> <EOL> for TYPE in green_args_spec : <EOL> item = greenargs [ i ] <EOL> y = hash_whatever ( TYPE , item ) <EOL> x = intmask ( ( <NUM_LIT> * x ) ^ y ) <EOL> i = i + <NUM_LIT:1> <EOL> return x <EOL> jitcell_dict = r_dict ( comparekey , hashkey ) <EOL> def get_jitcell ( * greenargs ) : <EOL> try : <EOL> cell = jitcell_dict [ greenargs ] <EOL> except KeyError : <EOL> cell = JitCell ( ) <EOL> jitcell_dict [ greenargs ] = cell <EOL> return cell <EOL> return get_jitcell <EOL> def _make_jitcell_getter_custom ( self , JitCell ) : <EOL> "<STR_LIT>" <EOL> rtyper = self . warmrunnerdesc . rtyper <EOL> get_jitcell_at_ptr = self . warmrunnerdesc . get_jitcell_at_ptr <EOL> set_jitcell_at_ptr = self . warmrunnerdesc . set_jitcell_at_ptr <EOL> lltohlhack = { } <EOL> def get_jitcell ( * greenargs ) : <EOL> fn = support . maybe_on_top_of_llinterp ( rtyper , get_jitcell_at_ptr ) <EOL> cellref = fn ( * greenargs ) <EOL> if we_are_translated ( ) : <EOL> BASEJITCELL = lltype . typeOf ( cellref ) <EOL> cell = cast_base_ptr_to_instance ( JitCell , cellref ) <EOL> elif isinstance ( cellref , ( BaseJitCell , type ( None ) ) ) : <EOL> BASEJITCELL = None <EOL> cell = cellref <EOL> else : <EOL> BASEJITCELL = lltype . typeOf ( cellref ) <EOL> if cellref : <EOL> cell = lltohlhack [ rtyper . type_system . deref ( cellref ) ] <EOL> else : <EOL> cell = None <EOL> if cell is None : <EOL> cell = JitCell ( ) <EOL> if we_are_translated ( ) : <EOL> cellref = cast_object_to_ptr ( BASEJITCELL , cell ) <EOL> elif BASEJITCELL is None : <EOL> cellref = cell <EOL> else : <EOL> if isinstance ( BASEJITCELL , lltype . Ptr ) : <EOL> cellref = lltype . malloc ( BASEJITCELL . TO ) <EOL> elif isinstance ( BASEJITCELL , ootype . Instance ) : <EOL> cellref = ootype . new ( BASEJITCELL ) <EOL> else : <EOL> assert False , "<STR_LIT>" <EOL> lltohlhack [ rtyper . type_system . deref ( cellref ) ] = cell <EOL> fn = support . maybe_on_top_of_llinterp ( rtyper , <EOL> set_jitcell_at_ptr ) <EOL> fn ( cellref , * greenargs ) <EOL> return cell <EOL> return get_jitcell <EOL> def make_set_future_values ( self ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . set_future_values <EOL> warmrunnerdesc = self . warmrunnerdesc <EOL> cpu = warmrunnerdesc . cpu <EOL> vinfo = warmrunnerdesc . metainterp_sd . virtualizable_info <EOL> red_args_types = unrolling_iterable ( warmrunnerdesc . red_args_types ) <EOL> def set_future_values ( * redargs ) : <EOL> i = <NUM_LIT:0> <EOL> for typecode in red_args_types : <EOL> set_future_value ( cpu , i , redargs [ i ] , typecode ) <EOL> i = i + <NUM_LIT:1> <EOL> if vinfo is not None : <EOL> set_future_values_from_vinfo ( * redargs ) <EOL> if vinfo is not None : <EOL> i0 = len ( warmrunnerdesc . red_args_types ) <EOL> num_green_args = warmrunnerdesc . num_green_args <EOL> vable_static_fields = unrolling_iterable ( <EOL> zip ( vinfo . static_extra_types , vinfo . static_fields ) ) <EOL> vable_array_fields = unrolling_iterable ( <EOL> zip ( vinfo . arrayitem_extra_types , vinfo . array_fields ) ) <EOL> getlength = cpu . ts . getlength <EOL> getarrayitem = cpu . ts . getarrayitem <EOL> def set_future_values_from_vinfo ( * redargs ) : <EOL> i = i0 <EOL> virtualizable = redargs [ vinfo . index_of_virtualizable - <EOL> num_green_args ] <EOL> virtualizable = vinfo . cast_to_vtype ( virtualizable ) <EOL> for typecode , fieldname in vable_static_fields : <EOL> x = getattr ( virtualizable , fieldname ) <EOL> set_future_value ( cpu , i , x , typecode ) <EOL> i = i + <NUM_LIT:1> <EOL> for typecode , fieldname in vable_array_fields : <EOL> lst = getattr ( virtualizable , fieldname ) <EOL> for j in range ( getlength ( lst ) ) : <EOL> x = getarrayitem ( lst , j ) <EOL> set_future_value ( cpu , i , x , typecode ) <EOL> i = i + <NUM_LIT:1> <EOL> else : <EOL> set_future_values_from_vinfo = None <EOL> self . set_future_values = set_future_values <EOL> return set_future_values <EOL> def make_jitdriver_callbacks ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> can_inline_ptr = self . warmrunnerdesc . can_inline_ptr <EOL> if can_inline_ptr is None : <EOL> def can_inline_callable ( greenkey ) : <EOL> return True <EOL> else : <EOL> rtyper = self . warmrunnerdesc . rtyper <EOL> unwrap_greenkey = self . make_unwrap_greenkey ( ) <EOL> def can_inline_callable ( greenkey ) : <EOL> greenargs = unwrap_greenkey ( greenkey ) <EOL> fn = support . maybe_on_top_of_llinterp ( rtyper , can_inline_ptr ) <EOL> return fn ( * greenargs ) <EOL> self . can_inline_callable = can_inline_callable <EOL> get_location_ptr = self . warmrunnerdesc . get_printable_location_ptr <EOL> if get_location_ptr is None : <EOL> def get_location_str ( greenkey ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> rtyper = self . warmrunnerdesc . rtyper <EOL> unwrap_greenkey = self . make_unwrap_greenkey ( ) <EOL> def get_location_str ( greenkey ) : <EOL> greenargs = unwrap_greenkey ( greenkey ) <EOL> fn = support . maybe_on_top_of_llinterp ( rtyper , get_location_ptr ) <EOL> res = fn ( * greenargs ) <EOL> if not we_are_translated ( ) and not isinstance ( res , str ) : <EOL> res = hlstr ( res ) <EOL> return res <EOL> self . get_location_str = get_location_str </s>
