<s> from . . import config , type_inference <EOL> from . . ndtypes import type_conv , Type , typeof <EOL> from . . syntax import UntypedFn , ActualArgs <EOL> from . . transforms import pipeline <EOL> from . . import c_backend <EOL> from . . import openmp_backend <EOL> import ast_conversion <EOL> def _typeof ( arg ) : <EOL> try : <EOL> return typeof ( arg ) <EOL> except : <EOL> if hasattr ( arg , '<STR_LIT:type>' ) and isinstance ( arg . type , Type ) : <EOL> return arg . type <EOL> else : <EOL> raise <EOL> def prepare_args ( fn , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( fn , UntypedFn ) : <EOL> fn = ast_conversion . translate_function_value ( fn ) <EOL> nonlocals = tuple ( fn . python_nonlocals ( ) ) <EOL> arg_values = ActualArgs ( nonlocals + tuple ( args ) , kwargs ) <EOL> arg_types = arg_values . transform ( _typeof ) <EOL> return arg_values , arg_types <EOL> def specialize ( untyped , args , kwargs = { } , optimize = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( untyped , UntypedFn ) : <EOL> untyped = ast_conversion . translate_function_value ( untyped ) <EOL> arg_values , arg_types = prepare_args ( untyped , args , kwargs ) <EOL> linear_args = untyped . args . linearize_without_defaults ( arg_values ) <EOL> typed_fn = type_inference . specialize ( untyped , arg_types ) <EOL> if optimize : <EOL> from . . transforms . pipeline import normalize <EOL> typed_fn = normalize . apply ( typed_fn ) <EOL> return typed_fn , linear_args <EOL> def run_typed_fn ( fn , args , backend = None ) : <EOL> actual_types = tuple ( type_conv . typeof ( arg ) for arg in args ) <EOL> expected_types = fn . input_types <EOL> assert actual_types == expected_types , "<STR_LIT>" % ( expected_types , actual_types ) <EOL> if backend is None : <EOL> backend = config . backend <EOL> if backend == '<STR_LIT:c>' : <EOL> return c_backend . run ( fn , args ) <EOL> elif backend == '<STR_LIT>' : <EOL> return openmp_backend . run ( fn , args ) <EOL> elif backend == '<STR_LIT>' : <EOL> from . . import cuda_backend <EOL> return cuda_backend . run ( fn , args ) <EOL> elif backend == '<STR_LIT>' : <EOL> from . . llvm_backend . llvm_context import global_context <EOL> from . . llvm_backend import generic_value_to_python <EOL> from . . llvm_backend import ctypes_to_generic_value , compile_fn <EOL> lowered_fn = pipeline . lowering . apply ( fn ) <EOL> llvm_fn = compile_fn ( lowered_fn ) . llvm_fn <EOL> ctypes_inputs = [ t . from_python ( v ) <EOL> for ( v , t ) <EOL> in zip ( args , expected_types ) ] <EOL> gv_inputs = [ ctypes_to_generic_value ( cv , t ) <EOL> for ( cv , t ) <EOL> in zip ( ctypes_inputs , expected_types ) ] <EOL> exec_engine = global_context . exec_engine <EOL> gv_return = exec_engine . run_function ( llvm_fn , gv_inputs ) <EOL> return generic_value_to_python ( gv_return , fn . return_type ) <EOL> elif backend == "<STR_LIT>" : <EOL> from . . import interp <EOL> fn = pipeline . loopify ( fn ) <EOL> return interp . eval_fn ( fn , args ) <EOL> else : <EOL> assert False , "<STR_LIT>" % backend <EOL> def run_untyped_fn ( fn , args , kwargs = None , backend = None ) : <EOL> assert isinstance ( fn , UntypedFn ) <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> typed_fn , linear_args = specialize ( fn , args , kwargs ) <EOL> return run_typed_fn ( typed_fn , linear_args , backend ) <EOL> def run_python_ast ( fn_name , fn_args , fn_body , globals_dict , <EOL> arg_values , kwarg_values = None , backend = None ) : <EOL> """<STR_LIT>""" <EOL> untyped = ast_conversion . translate_function_ast ( fn_name , fn_args , fn_body , globals_dict ) <EOL> return run_untyped_fn ( untyped , arg_values , kwarg_values , backend ) <EOL> def run_python_fn ( fn , args , kwargs = None , backend = None ) : <EOL> """<STR_LIT>""" <EOL> untyped = ast_conversion . translate_function_value ( fn ) <EOL> return run_untyped_fn ( untyped , args , kwargs , backend ) </s>
