
\K]c           @` s  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l	 m
 Z
 d  d l j j Z d  d l m Z d  d l m Z d d l m Z d  d	 l m Z m Z d  d
 l m Z d  d l m Z d  d l m Z e j e   Z! d g Z" e j# j$ e j%  Z& e j# j' e j( d   Z) e j# j' e j( d  Z* e j# j' e j( d  Z+ d   Z, d e- f d     YZ. d e- f d     YZ/ d e/ f d     YZ0 d e/ f d     YZ1 e re1 n e0 Z2 d S(   i    (   t   print_functiont   divisiont   absolute_importN(   t   ir(   t   Linkage(   t   cgutils(   t   IS_PY3i   (   t
   llvm_types(   t   compile_extrat   Flags(   t   global_compiler_lock(   t
   cpu_target(   t	   nrtdynmodt   Compileri   c          C` s2   d d  l  }  d d  l } | j d t |  d   S(   Ni    s      #include <stdint.h>

    #ifndef HAVE_LONGDOUBLE
        #define HAVE_LONGDOUBLE %d
    #endif

    typedef struct {
        float real;
        float imag;
    } complex64;

    typedef struct {
        double real;
        double imag;
    } complex128;

    #if HAVE_LONGDOUBLE
    typedef struct {
        long double real;
        long double imag;
    } complex256;
    #endif

    typedef float float32;
    typedef double float64;
    #if HAVE_LONGDOUBLE
    typedef long double float128;
    #endif
    t
   complex256(   t   numpyt   textwrapt   dedentt   hasattr(   R   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt
   get_header    s    t   ExportEntryc           B` s    e  Z d  Z d   Z d   Z RS(   s0   
    A simple record for exporting symbols.
    c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   symbolt	   signaturet   function(   t   selfR   R   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   __init__I   s    		c         C` s   d |  j  |  j f S(   Ns   ExportEntry(%r, %r)(   R   R   (   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   __repr__N   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR   D   s   	t   _ModuleCompilerc           B` s   e  Z d  Z e j j e j e j e j	 e j f  Z
 e j j e
  Z e j j e j e j	 f  Z e j j e  Z e d  Z d   Z d   Z e d    Z e d  Z e d  Z d   Z d   Z d	   Z d
   Z d   Z d   Z RS(   s   A base class to compile Python modules to a single shared library or
    extension module.

    :param export_entries: a list of ExportEntry instances.
    :param module_name: the name of the exported module.
    c         K` sa   | |  _  t |  _ g  |  _ | |  _ d  |  _ | |  _ t j	 |  _	 t j
 j |  j  |  |  _ d  S(   N(   t   module_namet   Falset   export_python_wrapt   dll_exportst   export_entriest   Nonet   external_init_functiont   use_nrtR   t   typing_contextt   target_contextt   with_aot_codegent   context(   R   R#   R   R&   t   aot_options(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR   l   s    							c         C` s   d | f S(   Ns   ._pycc_method_%s(    (   R   t	   func_name(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _mangle_method_symbolz   s    c         C` s
   t   d S(   sA   Emit generated Python wrapper and extension module code.
        N(   t   NotImplementedError(   R   t   llvm_module(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _emit_python_wrapper}   s    c         C` s  i  |  _  i  |  _ i  |  _ |  j j   } | j |  j  } t   } | j d  |  j	 sk | j d  n  |  j
 r | j d  t j |  j  \ } } | j |  n  x5|  j D]*} t |  j |  j | j | j j | j j | d i  d | } | j j } | j j |  }	 |  j	 rt j |	 _ | j j }
 | j j |
  } |  j | j  | _ t j  | _ | j! j" j# | j j$ | j j%  } | |  j  | <| j& |  j | <| j j' |  j | <q | j |	 _ |  j( j) | j  q W|  j	 r| j* d  } |  j+ |  | j |  n  | j,   xY | j-   D]K } | j |  j( k r-| j t. j/ t. j0 h k rld | _1 qxd | _1 q-q-W| S(	   s   Read all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        t
   no_compilet   no_cpython_wrappert   nrtt   localst   libraryt   wrappert   defaultt   hidden(2   t   exported_function_typest   function_environmentst   environment_gvsR*   t   codegent   create_libraryR   R	   t   setR!   R&   R   t   create_nrt_modulet   add_ir_moduleR#   R   R'   R   R   t   argst   return_typet   fndesct   llvm_func_nameR5   t   get_functiont   lct   LINKAGE_INTERNALt   linkaget   llvm_cpython_wrapper_nameR-   R   t   namet   LINKAGE_EXTERNALR(   t	   call_convt   get_function_typet   restypet   argtypest   environmentt   env_nameR"   t   appendt   create_ir_moduleR0   t   finalizet   get_defined_functionsR   t   privatet   internalt
   visibility(   R   R<   R5   t   flagst
   nrt_modulet   _t   entryt   cresR,   t	   llvm_funct   wrappernameR6   t   fntyt   wrapper_modulet   fn(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _cull_exports   sZ    									
c         K` sD   | |  _  |  j   } t | d   } | j | j    Wd  QXd  S(   Nt   wb(   R!   Rc   t   opent   writet   emit_bitcode(   R   t   outputt   wrapt   kwsR5   t   fout(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   write_llvm_bitcode   s    	c         K` sD   | |  _  |  j   } t | d   } | j | j    Wd  QXd  S(   NRd   (   R!   Rc   Re   Rf   t   emit_native_object(   R   Rh   Ri   Rj   R5   Rk   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   write_native_object   s    	c         C` s;   t  |  } d | k r7 | j d  r7 | d 7} q7 n  | S(   Nt   intt   8t   16t   32t   64t   _t(   Rp   Rq   Rr   Rs   (   t   strt   endswith(   R   t   tyobjt   ret_val(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt	   emit_type   s
    c   	   
   ` s   t  j j |  \ } } t | d d   } | j t    | j d  xn   j D]c } | j }   j | j	 j
  } d j   f d   | j	 j D  } | j d | | | f  qU WWd  QXd  S(   Ns   .ht   ws   
/* Prototypes */
s   , c         3` s   |  ] }   j  |  Vq d  S(   N(   Ry   (   t   .0t   argtype(   R   (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pys	   <genexpr>   s   s   extern %s %s(%s);
(   t   ost   patht   splitextRe   Rf   R   R#   R   Ry   R   RB   t   joinRA   (	   R   Rh   t   fnamet   extRk   t   export_entryRJ   RN   RA   (    (   R   s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   emit_header   s    	c         C` s2  g  } x |  j  D] } | j } |  j |  } |  j | } | j | d | } |  j j | |  } t j j	 | t j j
 | t j  t t f  }	 | j |	  q Wt j j	 t t t t g  }
 | j |
  t j j |  j |  } | j | j d  } | | _ t j | _ t j j | t t g  } | S(   s   
        Collect exported methods and emit a PyMethodDef array.

        :returns: a pointer to the PyMethodDef array.
        RJ   s   .module_methods(   R#   R   R-   R9   t   add_functionR*   t   insert_const_stringRF   t   Constantt   structt   bitcastt   ltt
   _void_start   METH_VARARGS_AND_KEYWORDSt   NULLRR   t   ZEROt   arrayt   method_def_tyt   add_global_variablet   typet   initializerRG   RH   t   gep(   R   R/   t   method_defsR\   RJ   RD   R`   t   lfunct   method_namet   method_def_constt   sentinelt   method_array_initt   method_arrayt   method_array_ptr(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _emit_method_array   s*    			c   
      C` s   g  } x= |  j  D]2 } |  j | } | j | j  } | j |  q Wt j j |  j |  } |  j	 j
 | d |  }	 |	 j t t g  S(   s   
        Emit an array of env_def_t structures (see modulemixin.c)
        storing the pickled environment constants for each of the
        exported functions.
        s   .module_environments(   R#   R:   t   serialize_uncachedt   constsRR   RF   R   R   t
   env_def_tyR*   t   insert_unique_constR   R   (
   R   R/   t   buildert   pyapit   env_defsR\   t   envt   env_deft   env_defs_initt   gv(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _emit_environment_array   s    	c   
      C` s   g  } xR |  j  D]G } |  j | } |  j j | |  } | j t j  } | j |  q Wt j	 j
 t j |  }	 |  j j | d |	  } | j t t g  S(   sj   
        Emit an array of Environment pointers that needs to be filled at
        initialization.
        s   .module_envgvs(   R#   R;   R*   t   declare_env_globalR   R   R   RR   RF   R   R   R   R   R   (
   R   R/   R   R   t   env_settersR\   t
   envgv_nameR   t   envgvt   env_setters_init(    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _emit_envgvs_array  s    	c   	      C` so   |  j  rg t j t j | j |  j |  j | j g  } | j | |  j   } | j	 | | | | | g  Sd Sd S(   s@   
        Emit call to "external" init function, if any.
        N(   R%   R   t   FunctionTypeR   t   _int32R   t   method_def_ptrt   env_def_ptrR   t   callR$   (	   R   R/   R   t   modobjR   t	   env_arrayt   envgv_arrayR`   Rb   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   _emit_module_init_code#  s    	
(   R   R   R   RF   t   TypeR   R   t
   _int8_starR   R   R   t   pointerR   R   R   R    R   R-   R0   R
   Rc   Rl   Rn   Ry   R   R   R   R   R   (    (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR   R   s(   
		@					t   ModuleCompilerPy2c           B` s/   e  Z e d     Z e d    Z d   Z RS(   c         C` sx   t  j j t j t j |  j t j t j t j f  } d } t j rR | d 7} n  t j	 d k rn | d 7} n  | | f S(   sP   Return the signature and name of the function to initialize the module.
        t   Py_InitModule4t	   TraceRefsi@   t   _64(
   RF   R   R   R   t   _pyobject_head_pR   R   R   t   _trace_refs_t
   _plat_bits(   R   R   RJ   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   module_create_definition5  s    	c         C` s)   t  j j t  j j   d  d |  j f S(   sS   Return the signature and name of the function to initialize the extension.
        t   init(    (   RF   R   R   t   voidR   (   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   module_init_definitionI  s    c      	   C` s3  | j  |  j   } | j d  } t j |  } |  j j |  } | j  |  j   } t j | _	 |  j j
 | |  j  } |  j |  } | j | | | t t j j t j  t j j t j t j  f  }	 |  j | | |  }
 |  j | | |  } |  j | | |	 | |
 |  | j   |  j j | j  d  S(   Nt   Entry(   R   R   t   append_basic_blockRF   t   BuilderR*   t   get_python_apiR   RK   RH   R   R   R   R   R   R   t   nullR   R   Ro   R   t   syst   api_versionR   R   R   t   ret_voidR"   RR   RJ   (   R   R/   t   mod_init_fnR\   R   R   t   create_module_fnt   mod_name_constR   t   modR   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR0   O  s*    	!
(   R   R   t   propertyR   R   R0   (    (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR   3  s   t   ModuleCompilerPy3c        
   B` s  e  Z d    Z e e j e j  Z e e j e j  Z e e j e j e e j  Z	 e e j e j  Z
 e e j  Z e j Z e j j e j e e j e j f  Z e j j e e e e j e j e e	 e e
 f	  Z e d    Z e d    Z d   Z RS(   c         G` s   t  j j t  j j |  |   S(   N(   RF   R   R   R   (   t   retRA   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   <lambda>v  t    c         C` sV   t  j j t j t  j j |  j  t j f  } d } t j rL | d 7} n  | | f S(   sn   
        Return the signature and name of the Python C API function to
        initialize the module.
        t   PyModule_Create2R   (	   RF   R   R   R   R   R   t   module_def_tyR   R   (   R   R   RJ   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR     s    	c         C` s)   t  j j t j d  } | d |  j f S(   sX   
        Return the name and signature of the module's initialization function.
        t   PyInit_(    (   RF   R   R   R   R   R   (   R   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR     s    c         C` s  | j  |  j   } t j | _ |  j j | |  j  } t j j	 t
 j t j j |  j  t j j t
 j  t j j t
 j  f  } | j | j d  } | | _ t j | _ |  j |  } t j j	 | | t j j |  j  t j j t
 j d  | t j j |  j  t j j |  j  t j j |  j  t j j |  j  f	  } | j | j d  } | | _ t j | _ | j  |  j   }	 |	 j d  }
 t j |
  } |  j j |  } | j | | t j j t
 j t  j!  f  } | j" t# j$ | |   $ | j% t& j' |	 j j( j)   Wd  QX|  j* | | |  } |  j+ | | |  } |  j, | | | | | |  } | d  k	 r| j" t# j. | |   ! | j% t j j | j   Wd  QXn  | j% |  |  j/ j0 |	 j1  d  S(   Ns   .module_def_baseis   .module_defR   (2   R   R   RF   RK   RH   R*   R   R   R   R   R   t   _pyobject_head_initR   t	   m_init_tyt   _llvm_py_ssize_tR   R   R   R   RG   R   t
   _char_starRo   t
   inquiry_tyt   traverseproc_tyt   freefunc_tyR   R   R   R   R   R   R   R   t   if_thenR   t   is_nullR   R   R   t   pointeeRB   R   R   R   R$   t   is_not_nullR"   RR   RJ   (   R   R/   R   R   t   mod_def_base_initt   mod_def_baseR   t   mod_def_initt   mod_defR   R\   R   R   R   R   R   R   (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR0     s\    							!%%(   R   R   t   _ptr_funR   t   _int8R   t   visitproc_tyR   R   R   R   R   R   R   RF   R   R   t   _pyobject_headR   t   module_def_base_tyR   R   R   R   R   R   R0   (    (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyR   t  s:   							i   (3   t
   __future__R    R   R   t   loggingR}   R   t   llvmliteR   t   llvmlite.bindingR   t   llvmlite.llvmpy.coret   llvmpyt   coreRF   t   numbaR   t   numba.utilsR   R   R   R   t   numba.compilerR   R	   t   numba.compiler_lockR
   t   numba.targets.registryR   t   numba.runtimeR   t	   getLoggerR   t   loggert   __all__R   R   R   R   Ro   R   R   t   ONER   R   t   objectR   R   R   R   t   ModuleCompiler(    (    (    s2   lib/python2.7/site-packages/numba/pycc/compiler.pyt   <module>   s2   		$A