ó
\K]c           @   sū   d  Z  d d l Z d d l Z d d l m Z d d l j Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z e
 j d k o e
 j o e
 j Z d
   Z d   Z d   Z d e f d     YZ d e f d     YZ d   Z d   Z e   Z d S(   sD   
Register external C functions necessary for Numba code generation.
i’’’’N(   t   ir(   t   utilst   config(   t
   _helperlibi   (   t
   intrinsicsi   i   c         C   s&   t  j |   s" t  j |  |  n  d S(   s1   Add missing symbol into LLVM internal symtab
    N(   t   llt   address_of_symbolt
   add_symbol(   t   symbolt   addr(    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyt   _add_missing_symbol   s    c         C   s>   d d l  m } m } m } t | j |   } | | |  j S(   sr   
    Under Windows, look up a symbol inside the C runtime
    and return the raw pointer value as an integer.
    i’’’’(   t   cdllt   castt   c_void_p(   t   ctypesR   R   R   t   getattrt   msvcrtt   value(   R   R   R   R   t   f(    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyt   _get_msvcrt_symbol   s    c         C   sX  |  j    } | j d  } | j d  } t j d  } t j d  } t j | d  } t j | d  } t j | d  } t j | | | g  }	 t j | |	 d d }
 |
 j \ } } |
 j	   } t j
 |  } | j | |  } | j | |  } | j | j | |  |  } | j | j | |  |  } | j | j | |  | j | |   } | j | |  } | j | |  } | j | | j | j | |  | j | |    } | j | | j | |   } | j | |  } | j | |  } | j | |  } | j | | j | j | |  | j | |    } | j | | j | |   } | j | | j | |   } | j | | j | j | |  | j | |    } | j | | j | |   } | j | | j | |   } | j | |  } | j | | j | j | |  |   } | j |  | j |  | j   | S(   so   
    Compile the multi3() helper function used by LLVM
    for 128-bit multiplication on 32-bit platforms.
    t   multi3i@   i   I’’’’    i    t   name(   t   codegent   create_libraryt   create_ir_moduleR    t   IntTypet   Constantt   FunctionTypet   Functiont   argst   append_basic_blockt	   IRBuildert   trunct   ashrt   mult   and_t   lshrt   addt   shlt   zextt   rett   add_ir_modulet   finalize(   t   contextR   t   libraryt   ir_modt   i64t   i128t
   lower_maskt   _32t   _64t   fn_typet   fnt   at   bt   bbt   buildert   alt   blt   aht   bht   rlt   tt   rht   r(    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyt   compile_multi3&   sP    **
t
   _Installerc           B   s   e  Z e Z d    Z RS(   c         C   s&   |  j  s" |  j |  t |  _  n  d S(   s   
        Install the functions into LLVM.  This only needs to be done once,
        as the mappings are persistent during the process lifetime.
        N(   t
   _installedt   _do_installt   True(   t   selfR+   (    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyt   installs   s    	(   t   __name__t
   __module__t   FalseRC   RG   (    (    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyRB   o   s   t   _ExternalMathFunctionsc           B   s   e  Z d  Z d   Z RS(   sd   
    Map the math functions from the C runtime library into the LLVM
    execution environment.
    c         C   sC  t  j d k } t j } t j j d  rL | rL t d  } t d |  n= t j j d  r | r t d | d  t d | d	  n  | rĢ t	 |  |  _
 |  j
 j d
  } | s¼ t  t d |  n  xU t j D]J } t r| j d  rt j | | d |  qÖ t j | | |  qÖ Wt r?t | |  |  _ n  d  S(   Ni    t   win32t   _ftolt   _ftol2t   linuxt   __fixunsdfdit   fptouit   __fixunssfdit   fptouifR   t   __multi3t   fmodt   fixed_(   R   t   MACHINE_BITSR   t	   c_helperst   syst   platformt
   startswithR   R
   RA   t   _multi3_libt   get_pointer_to_functiont   AssertionErrorR   t	   INTR_MATHt   need_kb982107R   R   t
   set_fnclext   _kb982107_lib(   RF   R+   t   is32bitRX   t   ftolt   ptrt   fname(    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyRD      s&    	(   RH   RI   t   __doc__RD   (    (    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyRK   }   s   c         C   sN   | d } t  j d t  j  |  } t |   } | j d  } | |  | S(   sl   
    Install fnclex before fmod calls.
    Workaround for https://support.microsoft.com/en-us/kb/982107
    Ra   t   fnclexN(   R   t	   CFUNCTYPEt   NoneR   t   compile_fnclexR]   (   R+   RX   t   ptr_set_fnclexR4   R,   t
   fnclex_ptr(    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyRa   ©   s    

c         C   sO   |  j    } | j d  } d } t j   | j t j |   | j   | S(   sm   
    Compile a function that calls fnclex to workround
    https://support.microsoft.com/en-us/kb/982107
    t   kb982107sT   
define void @fnclex() {
  call void asm sideeffect "fnclex", ""()
  ret void
}
    (   R   R   R   t   initialize_native_asmparsert   add_llvm_modulet   parse_assemblyR*   (   R+   R   R,   R-   (    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyRk   ø   s    

(   i   i   (   Rg   RY   R   t   llvmliteR    t   llvmlite.bindingt   bindingR   t   numbaR   R   R   t    R   t	   PYVERSIONt   IS_WIN32t	   IS_32BITSR`   R
   R   RA   t   objectRB   RK   Ra   Rk   t   c_math_functions(    (    (    s6   lib/python2.7/site-packages/numba/targets/externals.pyt   <module>   s$   	
		
	I,		