
\K]c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l Z d d l	 m
 Z
 d d l m Z m Z e
 j d  Z e
 j d  Z e
 j d	  Z e
 j e j  Z e j   Z e d  Z e d  Z e d  Z e d  Z d
   Z d d  Z d   Z i  Z d d  Z i  d  Z  d e! f d     YZ" d e" f d     YZ# d e" f d     YZ$ d e! f d     YZ% d d e& d  Z' d d  Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d    Z. d!   Z/ d"   Z0 d#   Z1 e j2 d$ dP  Z3 e d d d'   Z4 e d e5 d(   Z6 e d)    Z7 e d* d+   Z8 e d,    Z9 d d-  Z: d.   Z; d d/  Z< e& d0  Z= e& d1  Z> d2   Z? d3   Z@ d4   ZA d5   ZB eA ZC e@ ZD d6   ZE d7   ZF d d8  ZG e e& d9   ZH eF ZI d:   ZJ d;   ZK d<   ZL d=   ZM d>   ZN d d?  ZO d@   ZP dA dB  ZQ dC   ZR dD   ZS dE   ZT dF   ZU d dG  ZV d dH  ZW dI   ZX dJ   ZY dK   ZZ dL   Z[ e j\ rdM   Z] n	 dN   Z] dO   Z^ d S(Q   s+   
Generic helpers for LLVM code generation.
i    (   t   print_functiont   divisiont   absolute_importN(   t   contextmanager(   t   iri   (   t   utilst   configi   i    c         C` s   |  j  d | | j d   S(   Ns   !=i    (   t   icmp_unsignedt   type(   t   buildert   value(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   as_bool_bit   s    c         C` sy   | d k r4 t j g  | D] } | j ^ q  } n  | t j  } x/ t |  D]! \ } } |  j | | |  } qP W| S(   sH   
    Create an anonymous struct containing the given LLVM *values*.
    N(   t   NoneR   t   LiteralStructTypeR   t	   Undefinedt	   enumeratet   insert_value(   R	   t   valuest   struct_typet   vt
   struct_valt   i(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   make_anonymous_struct    s    (c         C` s=   t  |   } t |  } t j t j t j d  |  |  S(   s0   
    Make a byte array constant from *buf*.
    i   (   t	   bytearrayt   lenR   t   Constantt	   ArrayTypet   IntType(   t   buft   bt   n(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   make_bytearray,   s    R
   c         C` s   |  | f } t  j |  } | d k r i t d 6t d 6| } | j d t |   } | f } t d |   } t | | |  } | t  | <n  | S(   sK   
    Returns a specialized StructProxy subclass for the given fe_type.
    R
   t   datat   _t   _fe_typeN(	   t   _struct_proxy_cachet   getR   t   ValueStructProxyt   DataStructProxyt   __name__t   strt   dictR   (   t   fe_typet   kindt	   cache_keyt   rest   baset   clsnamet   basest
   clsmembers(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   create_struct_proxy8   s    
	c         C` s   | j    } x? | j j D]1 } | j | t | |   } t |  | |  q Wx* | j   D] \ } } t |  | |  q[ W|  S(   sJ   
    Copy structure from *src* to *dst* with replacement from *repl*.
    (   t   copyt
   _datamodelt   _fieldst   popt   getattrt   setattrt   items(   t   dstt   srct   replt   kR   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   copy_structK   s    t   _StructProxyc           B` s   e  Z d  Z d Z d d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    Creates a `Structure` like interface that is constructed with information
    from DataModel instance.  FE type must have a data model that is a
    subclass of StructModel.
    c         C` sQ  d d l  m } | |  _ |  j j |  j |  _ t |  j | j  s_ t d j	 |  j    n  | |  _
 |  j |  j  |  _ t |  j  s t  |  j |  \ } } | j j |  j k r t d |  j j   | j f   n  | d  k	 r;| j | j j k r%t d | j j | j f   n  |  j
 j | |  n  | |  _ | |  _ d  S(   Ni    (   t	   datamodels   Not a structure model: {0}s!   bad ref type: expected %s, got %ss#   bad value type: expected %s, got %s(   t   numbaR@   t   _contextt   data_model_managerR"   R4   t
   isinstancet   StructModelt	   TypeErrort   formatt   _buildert   _get_be_typet   _be_typet
   is_pointert   AssertionErrort
   _make_refsR   t   pointeet
   as_pointerR   t   storet   _valuet
   _outer_ref(   t   selft   contextR	   R
   t   refR@   t	   outer_ref(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   __init__c   s&    			c         C` s4   | d k r* t |  j |  j d t } n  | | f S(   s   
        Return an (outer ref, value ref) pair.  By default, these are
        the same pointers, but a derived class may override this.
        t   zfillN(   R   t   alloca_onceRH   RJ   t   True(   RS   RU   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRM   |   s    c         C` s
   t   d  S(   N(   t   NotImplementedError(   RS   R@   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRI      s    c         C` s
   t   d  S(   N(   R[   (   RS   t   indext   val(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _cast_member_to_value   s    c         C` s
   t   d  S(   N(   R[   (   RS   R\   R]   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _cast_member_from_value   s    c         C` s   t  |  j |  j d |  S(   Ni    (   t   gep_inboundsRH   RQ   (   RS   R\   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _get_ptr_by_index   s    c         C` s   |  j  j |  } |  j |  S(   N(   R4   t   get_field_positionRa   (   RS   t   attrnameR\   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _get_ptr_by_name   s    c         C` s3   | j  d  s# |  |  j j |  St |   d S(   s;   
        Load the LLVM value of the named *field*.
        R!   N(   t
   startswithR4   Rb   t   AttributeError(   RS   t   field(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   __getattr__   s    c         C` sB   | j  d  r( t t |   j | |  S| |  |  j j |  <d S(   s@   
        Store the LLVM *value* into the named *field*.
        R!   N(   Re   t   superR?   t   __setattr__R4   Rb   (   RS   Rg   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRj      s    c         C` s+   |  j  j |  j |   } |  j | |  S(   s>   
        Load the LLVM value of the field at *index*.
        (   RH   t   loadRa   R^   (   RS   R\   t
   member_val(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   __getitem__   s    c      
   C` s   |  j  |  } |  j | |  } | j | j j k r t | j  r t | j j  r | j j | j j j k r |  j j |  j | | j j j  } q t	 d j
 d | d | d |  d |    n  |  j j | |  d S(   sC   
        Store the LLVM *value* into the field at *index*.
        sj   Invalid store of {value.type} to {ptr.type.pointee} in {self._datamodel} (trying to write member #{index})R
   t   ptrRS   R\   N(   Ra   R_   R   RN   RK   RB   t   addrspacecastRH   t	   addrspaceRF   RG   RP   (   RS   R\   R
   Rn   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   __setitem__   s    !c         C` s
   |  j  j S(   s.   
        Return the number of fields.
        (   R4   t   field_count(   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   __len__   s    c         C` s   |  j  S(   sF   
        Return the LLVM pointer to the underlying structure.
        (   RR   (   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _getpointer   s    c         C` s   |  j  j |  j  S(   sM   
        Load and return the value of the underlying LLVM structure.
        (   RH   Rk   RR   (   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt	   _getvalue   s    c         C` sZ   t  | j  s t  | j |  j k s@ t | j |  j f   |  j j | |  j  d S(   s4   
        Store the value in this structure.
        N(   RK   R   RL   RJ   RH   RP   RQ   (   RS   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt	   _setvalue   s    *N(   R'   t
   __module__t   __doc__R   R"   RW   RM   RI   R^   R_   Ra   Rd   Rh   Rj   Rm   Rq   Rs   Rt   Ru   Rv   (    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR?   Z   s"   															R%   c           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   sl   
    Create a StructProxy suitable for accessing regular values
    (e.g. LLVM values or alloca slots).
    c         C` s
   | j    S(   N(   t   get_value_type(   RS   R@   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRI      s    c         C` s   | S(   N(    (   RS   R\   R]   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR^      s    c         C` s   | S(   N(    (   RS   R\   R]   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR_      s    (   R'   Rw   Rx   RI   R^   R_   (    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR%      s   		R&   c           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   sO   
    Create a StructProxy suitable for accessing data persisted in memory.
    c         C` s
   | j    S(   N(   t   get_data_type(   RS   R@   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRI      s    c         C` s%   |  j  j |  } | j |  j |  S(   N(   R4   t	   get_modelt	   from_dataRH   (   RS   R\   R]   t   model(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR^      s    c         C` s%   |  j  j |  } | j |  j |  S(   N(   R4   R{   t   as_dataRH   (   RS   R\   R]   R}   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR_      s    (   R'   Rw   Rx   RI   R^   R_   (    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR&      s   		t	   Structurec           B` sz   e  Z d  Z d d e d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z RS(   ss   
    A high-level object wrapping a alloca'ed LLVM structure, including
    named fields and attribute access.
    c   
      C` s  | j  |   |  _ | |  _ | |  _ | d  k r t | |  j d t |  _ | d  k	 r;t | j	  sm t
  | j	 |  j k s t
 | j	 |  j f   | j | |  j  q;n | d  k s t
  t | j	  s t
  |  j | j	 j k r2| r| j | |  j j    } q2t d | j	 j |  j f   n  | |  _ i  |  _ g  |  _ g  |  _ t d  } x\ t |  j  D]K \ } \ } }	 | |  j | <|  j j | t |  f  |  j j |	  qrWd  S(   NRX   s-   mismatching pointer type: got %s, expected %si    (   t   get_struct_typet   _typeRB   RH   R   RY   RZ   RQ   RK   R   RL   RP   RN   t   bitcastRO   RF   t   _namemapt   _fdmapt   _typemapt   int32_tR   R5   t   append(
   RS   RT   R	   R
   RU   t   cast_refR.   R   R=   t   tp(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRW     s4    		*				"c         C` s)   |  j  j |  j |  j | d t } | S(   Nt   inbounds(   RH   t   gepRQ   R   RZ   (   RS   R\   Rn   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRa   $  s    %c         C` s   |  j  |  j |  S(   N(   Ra   R   (   RS   Rc   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRd   (  s    c         C` s.   | j  d  s |  |  j | St |   d S(   s;   
        Load the LLVM value of the named *field*.
        R!   N(   Re   R   Rf   (   RS   Rg   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRh   +  s    c         C` s=   | j  d  r( t t |   j | |  S| |  |  j | <d S(   s@   
        Store the LLVM *value* into the named *field*.
        R!   N(   Re   Ri   R   Rj   R   (   RS   Rg   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRj   4  s    c         C` s   |  j  j |  j |   S(   s>   
        Load the LLVM value of the field at *index*.
        (   RH   Rk   Ra   (   RS   R\   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRm   <  s    c         C` sr   |  j  |  } | j j | j k r[ d } t | | t | j j  t | j  f   n  |  j j | |  d S(   sC   
        Store the LLVM *value* into the field at *index*.
        s:   Type mismatch: __setitem__(%d, ...) expected %r but got %rN(   Ra   R   RN   RL   R(   RH   RP   (   RS   R\   R
   Rn   t   fmt(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRq   C  s    	c         C` s   t  |  j  S(   s.   
        Return the number of fields.
        (   R   R   (   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRs   O  s    c         C` s   |  j  S(   sF   
        Return the LLVM pointer to the underlying structure.
        (   RQ   (   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRt   U  s    c         C` s   |  j  j |  j  S(   sM   
        Load and return the value of the underlying LLVM structure.
        (   RH   Rk   RQ   (   RS   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRu   [  s    c         C` sZ   t  | j  s t  | j |  j k s@ t | j |  j f   |  j j | |  j  d S(   s!   Store the value in this structureN(   RK   R   RL   R   RH   RP   RQ   (   RS   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRv   a  s    *N(   R'   Rw   Rx   R   t   FalseRW   Ra   Rd   Rh   Rj   Rm   Rq   Rs   Rt   Ru   Rv   (    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR      s   										t    c      
   C` s   t  | t j  r' t j t |  } n  |  j   6 |  j | d | d | } |  j | d  |  Wd QX| r |  j | d  |  n  | S(   s  Allocate stack memory at the entry block of the current function
    pointed by ``builder`` withe llvm type ``ty``.  The optional ``size`` arg
    set the number of element to allocate.  The default is 1.  The optional
    ``name`` arg set the symbol name inside the llvm IR for debugging.
    If ``zfill`` is set, fill the memory with zeros at the current
    use-site location.  Note that the memory is always zero-filled after the
    ``alloca`` at init-site (the entry block).
    t   sizet   nameN(
   RD   R   t	   INT_TYPESR   R   t   intp_tt   goto_entry_blockt   allocaRP   R   (   R	   t   tyR   R   RX   Rn   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRY   j  s    	c         C` s&   t  |  | j  } |  j | |  | S(   s   
    Like alloca_once(), but passing a *value* instead of a type.  The
    type is inferred and the allocated slot is also initialized with the
    given value.
    (   RY   R   RP   (   R	   R
   R   t   storage(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   alloca_once_value  s    c         C` s9   |  j  | d | } | j j d  | j j d  | S(   s_   
    Insert a pure function (in the functional programming sense) in the
    given module.
    R   t   readonlyt   nounwind(   t   get_or_insert_functiont
   attributest   add(   t   modulet   fntyR   t   fn(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   insert_pure_function  s    c         C` s,   |  j  } | j d  k r( |  j |  n  d  S(   N(   t   basic_blockt
   terminatorR   t   branch(   R	   t   bbendt   bb(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt	   terminate  s    	c         C` s
   |  d   S(   N(   R   (   t   ltype(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   get_null_value  s    c         C` s"   t  | j  } |  j d | |  S(   Ns   ==(   R   R   R   (   R	   R]   t   null(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_null  s    c         C` s"   t  | j  } |  j d | |  S(   Ns   !=(   R   R   R   (   R	   R]   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_not_null  s    c         C` s   |  j  | d t S(   Nt   likely(   t   if_thenR   (   R	   t   pred(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   if_unlikely  s    c         C` s   |  j  | d t S(   NR   (   R   RZ   (   R	   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt	   if_likely  s    c         C` s   |  j  |  j |   S(   N(   R   t   not_(   R	   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   ifnot  s    c         C` s(   | j  d  } |  j | | d d g S(   s#   
    Increment an index *val*.
    i   t   flagst   nsw(   R   R   (   R	   R]   t   one(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   increment_index  s    t   LoopR\   t   do_breakc         #` s[  | d	 k r | j } n  | d	 k r3 | d  } n  | }  j d  }  j d  }  j d       f d   }  j }  j |   j |  B  j | d d }	  j d |	 |  }
  j |
 |    Wd	 QX j |  8 t	 |	 |  V j } t
  |	  } t  |  Wd	 QX|	 j | |  |	 j | |   j    d	 S(
   s  
    Generate LLVM IR for a for-loop in [start, count).
    *start* is equal to 0 by default.

    Yields a Loop namedtuple with the following members:
    - `index` is the loop index's value
    - `do_break` is a no-argument callable to break out of the loop
    i    s   for.conds   for.bodys   for.endc           ` s    j     d  S(   N(   R   (    (   R   R	   (    s,   lib/python2.7/site-packages/numba/cgutils.pyR     s    R   s
   loop.indext   <N(   R   R   t   append_basic_blockR   R   t
   goto_blockt   phit   icmp_signedt   cbranchR   R   R   t   add_incomingt   position_at_end(   R	   t   countt   startt   intpt   stopt   bbcondt   bbbodyR   t   bbstartR\   R   t   incr(    (   R   R	   s,   lib/python2.7/site-packages/numba/cgutils.pyt	   for_range  s.    
		c         c` s  | d	 k r | j } n  |  j d  } |  j d  } |  j d  } |  j }	 |  j |  |  j |  u |  j | d d }
 |  j | d d } | r |  j d |
 |  } n |  j d |
 |  } |  j | | |  Wd	 QX|  j |  G |
 | f V|  j } |  j	 |
 |  } t
 |  |  } t |  |  Wd	 QX|
 j | |	  |
 j | |  | j t j | d
  |	  | j | |  |  j |  d	 S(   sY  
    Generate LLVM IR for a for-loop based on a slice.  Yields a
    (index, count) tuple where `index` is the slice index's value
    inside the loop, and `count` the iteration count.

    Parameters
    -------------
    builder : object
        Builder object
    start : int
        The beginning value of the slice
    stop : int
        The end value of the slice
    step : int
        The step value of the slice
    intp :
        The data type
    inc : boolean, optional
        Signals whether the step is positive (True) or negative (False).

    Returns
    -----------
        None
    s   for.conds   for.bodys   for.endR   s
   loop.indexs
   loop.countR   t   >Ni    (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (   R	   R   R   t   stepR   t   incR   R   R   R   R\   R   R   R   t
   next_count(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   for_range_slice  s2    		c         c` s   | j  } |  j d | t j | d   } t |  | | | | d t } t |  | | | | d t } t d    } |  j | d t * \ }	 }
 | |	 |  | |
 |  f VWd QXd S(   s  
    A helper wrapper for for_range_slice().  This is a context manager which
    yields two for_range_slice()-alike context managers, the first for
    the positive step case, the second for the negative step case.

    Use:
        with for_range_slice_generic(...) as (pos_range, neg_range):
            with pos_range as (idx, count):
                ...
            with neg_range as (idx, count):
                ...
    s   >=i    R   c      	   s` s%   |   |  } | VWd  QXWd  QXd  S(   N(    (   t   condt   inner_cmR
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   cm_cond=  s    	R   N(	   R   R   R   R   R   RZ   R   R   t   if_else(   R	   R   R   R   R   t   is_pos_stept   pos_for_ranget   neg_for_rangeR   t   thent	   otherwise(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   for_range_slice_generic)  s    	!t   Cc         c` s   | d k s t   | s  d Vnk | d k r8 d   } n	 d   } t |  | |  |  0 } t |  t |  k sz t   | |  VWd QXd S(   s  
    Generate a loop nest walking a N-dimensional array.
    Yields a tuple of N indices for use in the inner loop body,
    iterating over the *shape* space.

    If *order* is 'C' (the default), indices are incremented inside-out
    (i.e. (0,0), (0,1), (0,2), (1,0) etc.).
    If *order* is 'F', they are incremented outside-in
    (i.e. (0,0), (1,0), (2,0), (0,1) etc.).
    This has performance implications when walking an array as it impacts
    the spatial locality of memory accesses.
    t   CFt   Fc         S` s   |  d  d  d  S(   Ni(    (   t   x(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   <lambda>[  R   c         S` s   |  S(   N(    (   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR   ]  R   N(    (   RL   t
   _loop_nestR   (   R	   t   shapeR   t   ordert   _swapt   indices(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt	   loop_nestG  s    	c      
   c` st   t  |  | d d | U } t |  d k r_ t |  | d |   } | j f | VWd  QXn | j f VWd  QXd  S(   Ni    R   i   (   R   R   R   R\   (   R	   R   R   t   loopR   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR   c  s
    c         C` sy   t  |  } | d k r( | d j } n  t j | |  t j  } x/ t |  D]! \ } } |  j | | |  } qP W| S(   s   
    Pack a sequence of values in a LLVM array.  *ty* should be given
    if the array may be empty, in which case the type can't be inferred
    from the values.
    i    N(   R   R   R   R   R   R   R   R   (   R	   R   R   R   t   aryR   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt
   pack_arraym  s    c         C` sj   t  j g  | D] } | j ^ q  } | t  j  } x/ t |  D]! \ } } |  j | | |  } qA W| S(   s7   
    Pack a sequence of values into a LLVM struct.
    (   R   R   R   R   R   R   (   R	   R   R   t   structtyt   stR   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   pack_struct|  s
    %c         C` sP   | d k r! t | j j  } n  g  t |  D] } |  j | |  ^ q. } | S(   sH   
    Unpack an array or structure of values, return a Python tuple.
    N(   R   R   R   t   elementst   ranget   extract_value(   R	   t   tupR   R   t   vals(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   unpack_tuple  s
    (c         C` sj   t  |  | j d | j } t  |  | j d | j } t |  d | j d | d | d | j d | d | S(   NR   R    R   t   stridest   layoutt   indst
   wraparound(   R   R   t   ndimR   t   get_item_pointer2R    R   (   R	   t   arytyR   R   R   t   shapesR   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   get_item_pointer  s
    c         C` ss  | r g  } xx t  | |  D]^ \ } }	 |  j d | | j d   }
 |  j |	 |  } |  j |
 | |  } | j |  q Wn | } | s |  j | t d  g  S| d j } | d k rg  } | d k r5x t t	 |   D]J } | d  } x( | | d D] } |  j
 | |  } qW| j |  q Wnx | d k rxi t t	 |   D]F } | d  } x$ | |  D] } |  j
 | |  } qqW| j |  qTWn t d   | d  } xA t  | |  D]0 \ } } |  j
 | |  } |  j | |  } qW|  j | | g  } | Sg  t  | |  D] \ } } |  j
 | |  ^ q&} t j |  j |  } t |  | |  Sd  S(   NR   i    R   R   i   R   t   unreachable(   t   zipR   R   R   t   selectR   R   R   R   R   t   mult	   Exceptiont	   functoolst   reducet   pointer_add(   R	   R    R   R   R   R   R   R   t   indt   dimlent   negativet   wrappedt   selectedR   t   stepsR   t   lastt   jt   loct   st   tmpRn   t   dimoffst   offset(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR     sF    4c         C` s   | j  d  } t | j  t j t j f  r? | | |  } nC t | j  t j  rl |  j | | |  } n t d | j  f   | S(   Ni    s   unexpected value type %s(   R   RD   R   t	   FloatTypet
   DoubleTypeR   R   RF   (   R	   R
   t   fpredt   icondt   nullvalt   isnull(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _scalar_pred_against_zero  s    c         C` s"   t  |  | t j |  j d  d  S(   sK   
    Return a predicate representing whether *value* is equal to zero.
    s   ==(   R  R   t   partialt   fcmp_ordered(   R	   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_scalar_zero  s    c         C` s"   t  |  | t j |  j d  d  S(   s   
    Return a predicate representin whether a *value* is not equal to zero.
    (not exactly "not is_scalar_zero" because of nans)
    s   !=(   R  R   R  t   fcmp_unordered(   R	   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_not_scalar_zero  s    c         C` s"   t  |  | t j |  j d  d  S(   s]   
    Return a predicate representing whether *value* is equal to either zero
    or NaN.
    s   ==(   R  R   R  R  (   R	   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_scalar_zero_or_nan  s    c         C` s"   t  |  | t j |  j d  d  S(   s:   
    Is *value* negative?  Assumes *value* is signed.
    R   (   R  R   R  R  (   R	   R
   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   is_scalar_neg  s    c         C` sY   | j  t | |  d t 5 | d } | d p6 d } |  j j | | |  Wd QXd S(   su   
    Guard against *value* being null or zero.
    *exc_tuple* should be a (exception type, arguments...) tuple.
    R   i    i   N(   R   R  R   R   t	   call_convt   return_user_exc(   RT   R	   R
   t	   exc_tuplet   exct   exc_args(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt
   guard_null  s    
c         C` sx   t  | j t j  s$ t | j   | r3 | f n d } | j t | |  d t  |  j j	 | t
 |  Wd QXd S(   sG   
    Guard against *pointer* being NULL (and raise a MemoryError).
    R   N(    (   RD   R   R   t   PointerTypeRL   R   R   R   R  R  t   MemoryError(   RT   R	   t   pointert   msgR   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   guard_memory_error
  s    $c         c` s.   |  j  t |  |  d | 
 d VWd QXd S(   s>   
    Execute the given block if the scalar value is zero.
    R   N(   R   R  (   R	   R
   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   if_zero  s    c         C` s   t  |  t j  S(   s7   
    Whether the LLVM type *typ* is a struct type.
    (   RD   R   R"  (   t   ltyp(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRK      s    c         C` sD   t  |  | d |  } t | j j  s. t  |  j | | j    S(   Ni    (   R`   RK   R   RN   RL   R   RO   (   R	   t   recordR  t   typt   pval(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   get_record_member'  s    c         C` s   |  j  d | | j d   S(   NR   i    (   R   R   (   R	   R]   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt
   is_neg_int-  s    c         O` s   t  |  | d t | | S(   s8   
    Same as *gep*, but add the `inbounds` keyword.
    R   (   R   RZ   (   R	   Rn   R   t   kws(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR`   1  s    c   	      O` s   | j  d d  } | j  d t  } | s1 t  g  } xB | D]: } t | t j  re t |  } n | } | j |  q> W|  j | | d | d | S(   s   
    Emit a getelementptr instruction for the given pointer and indices.
    The indices can be LLVM values or Python int constants.
    R   R   R   (	   R6   R   RL   RD   R   R   R   R   R   (	   R	   Rn   R   R.  R   R   t   idxR   R  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR   8  s    c         C` s^   |  j  | t  } t | t j  r3 t |  } n  |  j | |  } |  j | | pZ | j  S(   s   
    Add an integral *offset* to pointer *ptr*, and return a pointer
    of *return_type* (or, if omitted, the same type as *ptr*).

    Note the computation is done in bytes, and ignores the width of
    the pointed item type.
    (   t   ptrtointR   RD   R   R   R   t   inttoptrR   (   R	   Rn   R  t   return_typet   intptr(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR   K  s
    c         C` s}   |  j  j d t | j f  } |  j | t  } t | t  rN t |  } n  |  j | | | | t	 d  t
 d  g  d S(   s=   
    Fill *size* bytes starting from *ptr* with *value*.
    s   llvm.memseti    N(   R   t   declare_intrinsict	   voidptr_tR   R   RD   t   intt   int8_tt   callR   t   bool_t(   R	   Rn   R   R
   R   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   memsetZ  s
    t   internalc         C` s[   t  |  t j  r |  } n	 |  j } | j | j d | } | | _ t | _ | | _	 | S(   sO   
    Get or create a (LLVM module-)global constant with *name* or *value*.
    R   (
   RD   R   t   ModuleR   t   add_global_variableR   t   linkageRZ   t   global_constantt   initializer(   t   builder_or_moduleR   R
   R>  R   R    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR?  e  s    					c   
      C` s  | d k s t   | j |  } | j d  } t |  | j  } |  j t |  |    \ } } | ' |  j | |  } |  j | |  Wd QX| E |  j | |  } |  j | |  } |  j |  j | |  |  Wd QXWd QX|  j	 |  } |  j | |  j
 | |   }	 | |	 f S(   s  
    Compute the (quotient, remainder) of *val* divided by the constant
    positive *divisor*.  The semantics reflects those of Python integer
    floor division, rather than C's / LLVM's signed division and modulo.
    The difference lies with a negative *val*.
    i    i   N(   RL   R   RY   R   R-  t   sdivRP   R   t   subRk   R   (
   R	   R]   t   divisorR   t   quott   if_negt   if_post   quot_valt   val_plus_onet   rem_val(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   divmod_by_constantt  s    !(c         C` s3   |  j  d  } |  j | | |  |  j |  | S(   s   
    Branch conditionally or continue.

    Note: a new block is created and builder is moved to the end of the new
          block.
    s	   .continue(   R   R   R   (   R	   R   t   bbtruet   bbcont(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   cbranch_or_continue  s    c         C` s   | j  | j  k s t  t |  | d | j  P } |  j | | j g  } |  j | | j g  } |  j |  j |  |  Wd QXd S(   s   
    Emit a memcpy to the builder.

    Copies each element of dst to src. Unlike the C equivalent, each element
    can be any LLVM type.

    Assumes
    -------
    * dst.type == src.type
    * count is positive
    R   N(   R   RL   R   R   R\   RP   Rk   (   R	   R:   R;   R   R   t   out_ptrt   in_ptr(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   memcpy  s
    c   
      C` s   | j  } t | t j  r0 t j | |  } n  |  j j | t t | g  } t j t j	 d  |  } t
 }	 |  j | |  j | t  |  j | t  |  j | |  | |	 g  d  S(   Ni    (   R   RD   R   R   R   R   R   R4  R5  R   t	   false_bitR8  R   R   (
   R	   t	   func_nameR:   R;   R   t   itemsizet   alignt   size_tRQ  t   is_volatile(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   _raw_memcpy  s    	c         C` s   t  |  d | | | | |  S(   sa   
    Emit a raw memcpy() call for `count` items of size `itemsize`
    from `src` to `dest`.
    s   llvm.memcpy(   RX  (   R	   R:   R;   R   RT  RU  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt
   raw_memcpy  s    c         C` s   t  |  d | | | | |  S(   sb   
    Emit a raw memmove() call for `count` items of size `itemsize`
    from `src` to `dest`.
    s   llvm.memmove(   RX  (   R	   R:   R;   R   RT  RU  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   raw_memmove  s    c   
      C` s   |  j  | |  } |  j | d  } |  j | d  } |  j | |  } |  j | d  } |  j | |  j | d   }	 | |	 f S(   sq   
    Compute (a * b + c) and return a (result, overflow bit) pair.
    The operands must be signed integers.
    i    i   (   t   smul_with_overflowR   t   sadd_with_overflowt   or_(
   R	   t   aR   t   ct   pt   prodt   prod_ovfR
  R-   t   ovf(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   muladd_with_overflow  s    c   
      G` s   t  | t  s t  |  j } t } t | d j d   } t | d |  } t j	 t
 | g d t } y | j d  } Wn) t k
 r t j | | d d } n X|  j | |  }	 |  j | |	 g t |   S(   s  
    Calls printf().
    Argument `format` is expected to be a Python string.
    Values to be printed are listed in `args`.

    Note: There is no checking to ensure there is correct number of values
    in `args` and there type matches the declaration in the format string.
    s    t   asciit   printf_formatt   var_argt   printfR   (   RD   R(   RL   R   R5  R   t   encodeR?  R   t   FunctionTypeR   RZ   t
   get_globalt   KeyErrort   FunctionR   R8  t   list(
   R	   RG   t   argst   modt   cstringt	   fmt_bytest
   global_fmtR   R   t   ptr_fmt(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRh    s    		c         G` s   t  | t  s t  |  j } t } t | d j d   } t | d |  } t j	 t
 | t | g d t }	 d }
 t j r d |
 }
 n  y | j |
  } Wn) t k
 r t j | |	 d |
 } n X|  j | |  } |  j | | | | g t |   S(   s8   Calls libc snprintf(buffer, bufsz, format, ...args)
    s    Re  t   snprintf_formatRg  t   snprintfR!   R   (   RD   R(   RL   R   R5  R   Ri  R?  R   Rj  R   R   RZ   R   t   IS_WIN32Rk  Rl  Rm  R   R8  Rn  (   R	   t   buffert   bufszRG   Ro  Rp  Rq  Rr  Rs  R   t   symbolR   Rt  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyRv    s     		c         G` sw   t  | t  s t  t j t j d  |  } t |  | d t } |  j | t	  } t
 |  | t |  | |  | S(   sw   Similar to `snprintf()` but the buffer is stack allocated to size *bufsz*.

    Returns the buffer pointer as i8*.
    i   RX   (   RD   R6  RL   R   R   R   RY   RZ   R   R5  Rv  R   (   R	   Ry  RG   Ro  t   spacetyt   spaceRx  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   snprintf_stackbuffer  s    c         C` s   |  j  d  j d  S(   sw   
        Normalize the given string to latin1 compatible encoding that is suitable
        for use in LLVM IR.
        t   utf8t   latin1(   Ri  t   decode(   t   text(    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   normalize_ir_text%  s    c         C` s   |  S(   sI   
        No-op for python2. Assume there won't be unicode names.
        (    (   R  (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyR  -  s    c   
      C` s  d } |  j  | t  } t |  d | |  t j d  } |  j | | j    } t |  |   } |  j | j	 t |   } |  j
 d | t d   } |  j |   t |  d  Wd QX|  j | | j	 g  } |  j |  }	 t |  d |	  Wd QXt |  d  d S(	   sI   Debug print the memory region in *ptr* to *ptr + nbytes*
    as hex.
    i   s   hexdump p=%p n=%zui   s   ==i    s   
Ns    %02x(   t   zextR   Rh  R   R   R   RO   R   t   uremR\   R   R   R   Rk   (
   R	   Rn   t   nbytest   bytes_per_linet   byte_tR/  t   div_byt   do_new_lineR  R]   (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   hexdump4  s    	
(   R\   R   (_   Rx   t
   __future__R    R   R   t   collectionst
   contextlibR   R   t   llvmliteR   R   R   R   R   R9  R7  R   t   MACHINE_BITSR   RO   R5  t   true_bitRR  t	   true_bytet
   false_byteR   R   R   R   R#   R2   R>   t   objectR?   R%   R&   R   R   RY   R   R   R   R   R   R   R   R   R   R   t
   namedtupleR   R   RZ   R   R   R   R   R   R   R   R   R   R  R  R  R  t   is_truet   is_falseR  R!  R&  R'  t
   guard_zeroRK   R,  R-  R`   R   R   R:  R?  RK  RN  RQ  RX  RY  RZ  Rd  Rh  Rv  R}  t   PY3R  R  (    (    (    s,   lib/python2.7/site-packages/numba/cgutils.pyt   <module>   s   			l									+9
		2								
																