Example #1
0
def compile2(env, func, restype=None, argtypes=None, ctypes=False,
             compile_only=False, func_ast=None, **kwds):
    """
    Compile a numba annotated function.

        - decompile function into a Python ast
        - run type inference using the given input types
        - compile the function to LLVM
    """
    # Let the pipeline create a module for the function it is compiling
    # and the user will link that in.
    assert 'llvm_module' not in kwds
    kwds['llvm_module'] = lc.Module.new(module_name(func))
    logger.debug(kwds)
    if func_ast is None:
        func_ast = functions._get_ast(func)
    else:
        func_ast = copy.deepcopy(func_ast)
    func_signature = typesystem.function(restype, argtypes)
    #pipeline, (func_signature, symtab, ast) = _infer_types2(
    #            env, func, restype, argtypes, codegen=True, **kwds)
    with env.TranslationContext(env, func, func_ast, func_signature,
                                need_lfunc_wrapper=not compile_only,
                                **kwds) as func_env:
        pipeline = env.get_pipeline(kwds.get('pipeline_name', None))
        func_ast.pipeline = pipeline
        post_ast = pipeline(func_ast, env)
        func_signature = func_env.func_signature
        symtab = func_env.symtab
        t = func_env.translator
    return func_env
Example #2
0
def infer(func, arg_types):
    sig = minitypes.FunctionType(return_type=None, args=arg_types)
    ast = functions._get_ast(func)

    sig, symtab, ast = ast_type_inference._infer_types(
                                decorators.context, func, ast, sig)
    return sig, symtab
Example #3
0
 def _iexport(func):
     if backend == 'bytecode':
         raise NotImplementedError(
            'Bytecode translation has been removed for exported functions.')
     else:
         name = function_signature.name
         llvm_module = _lc.Module.new('export_%s' % name)
         if not hasattr(func, 'live_objects'):
             func.live_objects = []
         func._is_numba_func = True
         func_ast = functions._get_ast(func)
         # FIXME: Hacked "mangled_name" into the translation
         # environment.  Should do something else.  See comment in
         # codegen.translate.LLVMCodeGenerator.__init__().
         with environment.TranslationContext(
                 env, func, func_ast, function_signature,
                 name=name, llvm_module=llvm_module,
                 mangled_name=name,
                 link=False, wrap=False,
                 is_pycc=True) as func_env:
             pipeline = env.get_pipeline()
             func_ast.pipeline = pipeline
             pipeline(func_ast, env)
             exports_env = env.exports
             exports_env.function_signature_map[name] = function_signature
             exports_env.function_module_map[name] = llvm_module
             if not exports_env.wrap_exports:
                 exports_env.function_wrapper_map[name] = None
             else:
                 wrapper_tup = llvmwrapper.build_wrapper_module(env)
                 exports_env.function_wrapper_map[name] = wrapper_tup
     return func
Example #4
0
def infer(func, arg_types):
    sig = minitypes.FunctionType(return_type=None, args=arg_types)
    ast = functions._get_ast(func)

    sig, symtab, ast = ast_type_inference._infer_types(decorators.context,
                                                       func, ast, sig)
    return sig, symtab
Example #5
0
def infer(func, signature=functype(), warn=True, **kwargs):
    ast = functions._get_ast(func)
    pipe, (signature, symtab, ast) = pipeline.run_pipeline(
                        decorators.context, func, ast, signature,
                        order=order, warn=warn, **kwargs)

    last_block = ast.flow.blocks[-2]
    symbols = {}
    #for block in ast.flow.blocks: print block.symtab
    for var_name, var in symtab.iteritems():
        if not var.parent_var and not var.is_constant:
            var = lookup(last_block, var_name)
            if var:
                symbols[var_name] = var

    return signature, symbols
Example #6
0
def infer(func, signature=functype(), warn=True, **kwargs):
    func_ast = functions._get_ast(func)
    env = environment.NumbaEnvironment.get_environment(kwargs.get('env', None))
    infer_pipe = env.get_or_add_pipeline('infer', construct_infer_pipeline)
    kwargs.update(warn=warn, pipeline_name='infer')
    pipe, (signature, symtab, func_ast) = pipeline.run_pipeline2(
        env, func, func_ast, signature, **kwargs)
    last_block = func_ast.flow.blocks[-2]
    symbols = {}
    #for block in ast.flow.blocks: print block.symtab
    for var_name, var in symtab.iteritems():
        if not var.parent_var and not var.is_constant:
            var = lookup(last_block, var_name)
            if var:
                symbols[var_name] = var

    return signature, symbols
def infer(func, signature=functype(), warn=True, **kwargs):
    func_ast = functions._get_ast(func)
    env = environment.NumbaEnvironment.get_environment(kwargs.get('env', None))
    infer_pipe = env.get_or_add_pipeline('infer', construct_infer_pipeline)
    kwargs.update(warn=warn, pipeline_name='infer')
    pipe, (signature, symtab, func_ast) = pipeline.run_pipeline2(
        env, func, func_ast, signature, **kwargs)
    last_block = func_ast.flow.blocks[-2]
    symbols = {}
    #for block in ast.flow.blocks: print block.symtab
    for var_name, var in symtab.iteritems():
        if not var.parent_var and not var.is_constant:
            var = lookup(last_block, var_name)
            if var:
                symbols[var_name] = var

    return signature, symbols
Example #8
0
def translate(func):
    # TODO use meta package
    wrapper = functools.wraps(func)
    caller_frame = inspect.currentframe().f_back
    tree = _get_ast(func)
    tree = ast.Module(body=tree.body)
    tree = ExpandControlFlow().visit(tree)
    fix_ast_lineno(tree)

    # prepare locals for execution
    local_dict = locals()
    local_dict.update(caller_frame.f_locals)
    local_dict.update(caller_frame.f_globals)

    try:
        compiled = compile(tree, '<string>', 'exec')
        return eval(compiled)
    except Exception, e:
        logger.debug(ast.dump(tree))
        from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
        # http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
        logger.debug(codegen.to_source(tree))
        raise
Example #9
0
def translate(func):
    # TODO use meta package
    wrapper = functools.wraps(func)
    caller_frame = inspect.currentframe().f_back
    tree = _get_ast(func)
    tree = ast.Module(body=tree.body)
    tree = ExpandControlFlow().visit(tree)
    fix_ast_lineno(tree)

    # prepare locals for execution
    local_dict = locals()
    local_dict.update(caller_frame.f_locals)
    local_dict.update(caller_frame.f_globals)

    try:
        compiled = compile(tree, '<string>', 'exec')
        return eval(compiled)
    except Exception as e:
        logger.debug(ast.dump(tree))
        from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
        # http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
        logger.debug(codegen.to_source(tree))
        raise
Example #10
0
 def _iexport(func):
     if backend == 'bytecode':
         raise NotImplementedError(
             'Bytecode translation has been removed for exported functions.'
         )
     else:
         name = function_signature.name
         llvm_module = _lc.Module.new('export_%s' % name)
         if not hasattr(func, 'live_objects'):
             func.live_objects = []
         func._is_numba_func = True
         func_ast = functions._get_ast(func)
         # FIXME: Hacked "mangled_name" into the translation
         # environment.  Should do something else.  See comment in
         # codegen.translate.LLVMCodeGenerator.__init__().
         with environment.TranslationContext(env,
                                             func,
                                             func_ast,
                                             function_signature,
                                             name=name,
                                             llvm_module=llvm_module,
                                             mangled_name=name,
                                             link=False,
                                             wrap=False,
                                             is_pycc=True) as func_env:
             pipeline = env.get_pipeline()
             func_ast.pipeline = pipeline
             pipeline(func_ast, env)
             exports_env = env.exports
             exports_env.function_signature_map[name] = function_signature
             exports_env.function_module_map[name] = llvm_module
             if not exports_env.wrap_exports:
                 exports_env.function_wrapper_map[name] = None
             else:
                 wrapper_tup = llvmwrapper.build_wrapper_module(env)
                 exports_env.function_wrapper_map[name] = wrapper_tup
     return func
Example #11
0
 def __init__(self, env, py_func, nopython, flags, template_signature):
     super(FunctionCompiler,self).__init__(env, py_func, nopython, flags, template_signature)
     self.ast = functions._get_ast(py_func)
Example #12
0
def _infer_types2(env, func, restype=None, argtypes=None, **kwargs):
    ast = functions._get_ast(func)
    func_signature = typesystem.function(restype, argtypes)
    return run_pipeline2(env, func, ast, func_signature, **kwargs)
Example #13
0
def _infer_types(context, func, restype=None, argtypes=None, **kwargs):
    ast = functions._get_ast(func)
    func_signature = minitypes.FunctionType(return_type=restype,
                                            args=argtypes)
    return run_pipeline(context, func, ast, func_signature, **kwargs)
Example #14
0
def _infer_types(context, func, restype=None, argtypes=None, **kwargs):
    ast = functions._get_ast(func)
    func_signature = minitypes.FunctionType(return_type=restype, args=argtypes)
    return run_pipeline(context, func, ast, func_signature, **kwargs)
Example #15
0
 def __init__(self, env, py_func, nopython, flags, template_signature):
     super(FunctionCompiler, self).__init__(env, py_func, nopython, flags,
                                            template_signature)
     self.ast = functions._get_ast(py_func)