σ
\K]c           @` s+  d  Z  d d l m Z m Z m Z d d l m Z d d l m Z m	 Z	 d d l
 m Z m Z e Z e j e  Z e j e j d   Z e j e e e e e g  Z e j e j   e g  Z e j e e g  Z d   Z d   Z d	   Z d Z d
   Z d   Z d   Z d   Z  d   Z! d S(   s%   
Dynamically generate the NRT module
i    (   t   print_functiont   absolute_importt   division(   t   MACHINE_BITS(   t   cgutilst   types(   t   irt   bindingi   c         C` s   |  j  t d d } t j | j    } | j \ } | j | t j    } | j	 t
 j | | d d   } | j |  d S(   st   
    Implement NRT_MemInfo_data_fast in the module.  This allows LLVM
    to inline lookup of the data pointer.
    t   namet   NRT_MemInfo_data_fasti    i   N(   t   get_or_insert_functiont   meminfo_data_tyR   t	   IRBuildert   append_basic_blockt   argst   bitcastt   _meminfo_struct_typet
   as_pointert   loadR   t   gept   ret(   t   modulet   fnt   buildert   ptrt
   struct_ptrt   data_ptr(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_nrt_meminfo_data   s    	!c         C` sι   |  j  t d d } | j j d  t j | j    } | j \ } | j d | t	 j
 | j   } t	 j | |   | j   Wd QXt r² t	 j | d | j |  |  n  | j | | j | | j d j  g  | j   d S(   s,   
    Implement NRT_incref in the module
    R   t
   NRT_increft   noinlines   ==Ns   *** NRT_Incref %zu [%p]
i    (   R
   t   incref_decref_tyt
   attributest   addR   R   R   R   t   icmp_unsignedR   t   get_null_valuet   typet   if_unlikelyt   ret_voidt   _debug_printt   printfR   t   callR   (   R   t   atomic_incrt	   fn_increfR   R   t   is_null(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_nrt_incref,   s    	!
)c   	      C` s  |  j  t d d } | j j d  |  j t j t j   t g  d d } t j	 | j
    } | j \ } | j d | t j | j   } t j | |   | j   Wd QXt rά t j | d | j |  |  n  | j d  | j | | j | | j d	 j  g  } | j d | t j | j d	   } t j | |  % | j d
  | j | | g  Wd QX| j   d S(   s,   
    Implement NRT_decref in the module
    R   t
   NRT_decrefR   t   NRT_MemInfo_call_dtors   ==Ns   *** NRT_Decref %zu [%p]
t   releasei    t   acquire(   R
   R   R   R    t   add_functionR   t   FunctionTypet   VoidTypet   _pointer_typeR   R   R   R!   R   R"   R#   R$   R%   R&   R'   R   t   fenceR(   R   t   Constant(	   R   t   atomic_decrt	   fn_decreft   calldtorR   R   R+   t   newrefctt
   refct_eq_0(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_nrt_decrefA   s,    	!	!
	"c         C` s  t  j t t j   g  } t  j |  | d d j |  } | j \ } | j   } t  j |  } t  j	 t d  } t
 sΎ | j | | | d | }	 t | |  |	 |  }
 | j |
  nD | j |  }	 t | |  |	 |  } | j | |  | j |	  | S(   sβ   Define a llvm function for atomic increment/decrement to the given module
    Argument ``op`` is the operation "add"/"sub".  Argument ``ordering`` is
    the memory ordering.  The generated function returns the new value.
    R   s   nrt_atomic_{0}i   t   ordering(   R   R2   t
   _word_typeR   t   Functiont   formatR   R   R   R6   t   _disable_atomicityt
   atomic_rmwt   getattrR   R   t   store(   R   t   opR=   t   ftypet	   fn_atomicR   t   bbR   t   ONEt   oldvalt   rest   newval(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_atomic_inc_decm   s    !c         C` sα   t  j t  j d  t j   t t t j   g  } t  j |  | d d } | j \ } } } } | j   } t  j |  }	 |	 j	 | | | d | }
 t
 j |	 |
 d  \ } } |	 j | |  |	 j |	 j | | j   | S(   s¨  Define a llvm function for atomic compare-and-swap.
    The generated function is a direct wrapper of the LLVM cmpxchg with the
    difference that the a int indicate success (1) or failure (0) is returned
    and the last argument is a output pointer for storing the old value.

    Note
    ----
    On failure, the generated function behaves like an atomic load.  The loaded
    value is stored to the last argument.
    i    R   t   nrt_atomic_casR=   i   (   R   R2   t   IntTypeR>   R   R?   R   R   R   t   cmpxchgR   t   unpack_tupleRD   R   t   zextt   return_type(   R   R=   RF   t   fn_casR   t   cmpt   replt   oldptrRH   R   t   outtupt   oldt   ok(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_atomic_cas   s    c         C` sn   |  j  j t j d  } t j | | d d } | j   } t j |  } d } |  j  j | t	 | f  | S(   sΡ   
    Defines an abort function due to unresolved symbol.

    The function takes no args and will always raise an exception.
    It should be safe to call this function with incorrect number of arguments.
    R   t   nrt_unresolved_aborts6   numba jitted function aborted due to unresolved symbol(    (
   t	   call_convt   get_function_typeR   t   noneR   R?   R   R   t   return_user_exct   RuntimeError(   t   ctxR   t   fntyR   RH   R   t   msg(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   _define_nrt_unresolved_abort£   s    c         C` s   |  j    } | j d  } | j d  } t | d d d } t | d d d } t | d d t |  t | |  t | |  t |  |  | | f S(   sl   
    Create an IR module defining the LLVM NRT functions.
    A (IR module, library) tuple is returned.
    t   nrtt
   nrt_moduleR    R=   t	   monotonict   sub(	   t   codegent   create_libraryt   create_ir_moduleRM   R[   R   R,   R<   Re   (   Rb   Rj   t   libraryt   ir_modt
   atomic_inct
   atomic_dec(    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   create_nrt_module³   s    
c         C` s-   t  |   \ } } | j |  | j   | S(   s   
    Compile all LLVM NRT functions and return a library containing them.
    The library is created using the given target context.
    (   Rq   t   add_ir_modulet   finalize(   Rb   Rn   Rm   (    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   compile_nrt_functionsΛ   s    
N("   t   __doc__t
   __future__R    R   R   t   numba.configR   t   numbaR   R   t   llvmliteR   R   t   FalseR&   RO   R>   t   PointerTypeR4   t   LiteralStructTypeR   R2   R3   R   R   R   R,   R<   RA   RM   R[   Re   Rq   Rt   (    (    (    s6   lib/python2.7/site-packages/numba/runtime/nrtdynmod.pyt   <module>   s0   			)				