
\K]c        
   @   s  d  Z  d d l m Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 m Z e d d'  Z e j d  Z e Z d   Z e d  Z e d  Z e d  Z e d  Z d Z e e  Z d e f d     YZ d e f d     YZ d e f d     YZ e j e e g  Z e j e  Z d e f d     YZ d e f d     YZ  d  e  f d!     YZ! d" e  f d#     YZ" i e! d$ 6e" d% 6Z# d&   Z$ d S((   s3   
Calling conventions for Numba-compiled functions.
i(   t
   namedtupleN(   t   ir(   t   cgutilst   typesi   (   t   PYOBJECTt   GENERIC_POINTERt   Statust   codet   is_okt   is_nonet   is_errort   is_stop_iterationt   is_python_exct   is_user_exct
   excinfoptri    c         C   s   t  j t |   S(   N(   R   t   Constantt	   errcode_t(   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt
   _const_int#   s    i    iit   BaseCallConvc           B   sk   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   c         C   s   | |  _  d  S(   N(   t   context(   t   selfR   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   __init__5   s    c         C   sA  | t  j k r |  j |  n| | k r |  j j | | d | } t j | | j  } | j |  3 |  j j	 | | j
 | j  } |  j | |  Wd  QX|  j |  n t | t  j  s%| | j
 k r |  j j | | d | d | j
 } n  |  j j	 | | j
 |  } |  j | |  n t d j | |    d  S(   Nt   valuet   fromtyt   totys   returning {0} for {1}(   R   t   nonet   return_native_noneR   t   make_helperR   t   as_bool_bitt   validt   if_thent   get_return_valuet   typet   datat   return_valuet
   isinstancet   Optionalt   castt   NotImplementedErrort   format(   R   t   buildert   rettyt   valtyR   t   optvalt   validbitt   retval(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   return_optional_value8   s$    c         C   s   |  j  | t  d  S(   N(   t   _return_errcode_rawt   RETCODE_NONE(   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR   U   s    c         C   s   |  j  | t  d  S(   N(   R/   t   RETCODE_EXC(   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt
   return_excX   s    c         C   s   |  j  | t  d  S(   N(   R/   t   RETCODE_STOPIT(   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   return_stop_iteration[   s    c         C   s    |  j  j | j   } | j   S(   sQ   
        Get the actual type of the return argument for Numba type *ty*.
        (   R   t   data_model_managert   get_return_typet
   as_pointer(   R   t   tyt   restype(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR6   ^   s    c         C   s   |  j  |  } | | _ | S(   sS   
        Initialize and return a call helper object for the given builder.
        (   t   _make_call_helpert   _BaseCallConv__call_helper(   R   R(   t   ch(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   init_call_helpere   s    	c         C   s   | j  S(   N(   R;   (   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   _get_call_helperm   s    c      
   C   s
  | j  j   } | j | j  ` | j   | j | j  } t j | t j	 | |    | j
 |  Wd QX| j |  Wd QX| j | j   | j d  | j |  Wd QX| j | j   | j |  Wd QX| j d d  | j |  | j |  d S(   sT   
        Given a non-ok *status*, raise the corresponding Python exception.
        Nt   PyExc_StopIterationt   PyExc_SystemErrors*   unknown error when calling native function(   t   functiont   append_basic_blockR   R   t	   err_cleart   unserializeR   R   t	   if_likelyt   is_not_nullt   raise_objectt   branchR   t   err_set_noneR   t   err_set_stringt   position_at_end(   R   R(   t   apit   statust   bbendt   exc(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   raise_errorp   s"    
		c         C   s.   |  j  |  } |  j |  } | j | |  S(   s   
        Get the decoded (unpacked) Python arguments with *argtypes*
        from LLVM function *func*.  A tuple of LLVM values is returned.
        (   t   get_argumentst   _get_arg_packert   from_arguments(   R   R(   t   argtypest   funct   raw_argst   arginfo(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   decode_arguments   s    c         C   s   |  j  j |  S(   sF   
        Get an argument packer for the given argument types.
        (   R   t   get_arg_packer(   R   RT   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRR      s    (   t   __name__t
   __module__R   R.   R   R2   R4   R6   R=   R>   RP   RX   RR   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR   3   s   											t   MinimalCallConvc           B   st   e  Z d  Z d   Z d   Z d d d d  Z d   Z d   Z d   Z	 d   Z
 e d  Z d	   Z d
   Z RS(   s  
    A minimal calling convention, suitable for e.g. GPU targets.
    The implemented function signature is:

        retcode_t (<Python return type>*, ... <Python arguments>)

    The return code will be one of the RETCODE_* constants or a
    function-specific user exception id (>= RETCODE_USEREXC).

    Caller is responsible for allocating a slot for the return value
    (passed as a pointer in the first argument).
    c         C   s   t    S(   N(   t   _MinimalCallHelper(   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR:      s    c         C   sp   | j  j d } | j | j j k sL t t | j  t | j j  f   | j | |  |  j | t  d  S(   Ni    (	   RA   t   argsR    t   pointeet   AssertionErrort   strt   storeR/   t
   RETCODE_OK(   R   R(   R-   t   retptr(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR"      s
    $c   
      C   s  | d  k	 r2 t | t  r2 t d | f   n  | d  k	 rd t | t  rd t d | f   n  | d  k	 r | j   } | d  k r | } n  | | j | j f } d  | k r d  } q n d  } |  j	 |  } | j
 | | |  }	 |  j | t |	   d  S(   Ns-   exc should be None or exception class, got %rs(   exc_args should be None or tuple, got %r(   t   Nonet
   issubclasst   BaseExceptiont	   TypeErrorR#   t   tuplet   _raw_function_namet   filenamet   lineR>   t   _add_exceptionR/   R   (
   R   R(   RO   t   exc_argst   loct	   func_namet   fnamet   locinfot   call_helpert   exc_id(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   return_user_exc   s"    	c         C   s   |  j  | | j  d  S(   N(   R/   R   (   R   R(   RM   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   return_status_propagate   s    c         C   s/   t  | t  r t |  } n  | j |  d  S(   N(   R#   t   intR   t   ret(   R   R(   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR/      s    c         C   s   | j  d | t  } | j  d | t  } | j | |  } | j |  } | j  d | t  } | j  d | t  } | j  d | t  }	 t d | d | d | d | d | d |	 d	 | d
 d  }
 |
 S(   s?   
        Given a return *code*, get a Status instance.
        s   ==s   >=R   R   R
   R   R	   R   R   R   N(
   t   icmp_signedRc   R0   t   or_t   not_R1   R3   t   RETCODE_USEREXCR   Re   (   R   R(   R   t   normR   t   okt   errRO   R   R   RM   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   _get_return_status   s     	c         C   sJ   |  j  |  } t | j  } |  j |  } t j t | g |  } | S(   sQ   
        Get the implemented Function type for *restype* and *argtypes*.
        (   RR   t   listt   argument_typesR6   R   t   FunctionTypeR   (   R   R9   RT   RW   t   resptrt   fnty(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   get_function_type   s
    c         C   s`   | s t   |  j |  } | j |  j |  g  | D] } d | ^ q5  d | j d _ | S(   sA   
        Set names and attributes of function arguments.
        s   arg.s   .reti    (   R`   RR   t   assign_namesRQ   R^   t   name(   R   t   fnR^   t   fe_argtypest   noaliasRW   t   a(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   decorate_function   s    c         C   s   | j  d S(   s@   
        Get the Python-level arguments of LLVM *func*.
        i   (   R^   (   R   RU   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRQ     s    c         C   s   | j  d j j } t j | |  } | j t j |  |  |  j |  } | j | |  } | g t	 |  }	 | j
 | |	  }
 |  j | |
  } | j |  } |  j j | | |  } | | f S(   s3   
        Call the Numba-compiled *callee*.
        i    (   R^   R    R_   R   t   alloca_onceRb   t   get_null_valueRR   t   as_argumentsR   t   callR   t   loadR   t   get_returned_value(   R   R(   t   calleet   restyt   argtysR^   R)   t	   retvaltmpRW   t   realargsR   RM   R-   t   out(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   call_function	  s    N(   RZ   R[   t   __doc__R:   R"   Re   Ru   Rv   R/   R   R   t   FalseR   RQ   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR\      s   						
	R]   c           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s   
    A call helper object for the "minimal" calling convention.
    User exceptions are represented as integer codes and stored in
    a mapping for retrieval from the caller.
    c         C   s   i  |  _  d  S(   N(   t
   exceptions(   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR   #  s    c         C   s-   t  |  j  t } | | | f |  j | <| S(   s   
        Parameters
        ----------
        exc :
            exception type
        exc_args : None or tuple
            exception args
        locinfo : tuple
            location information
        (   t   lenR   t   FIRST_USEREXC(   R   RO   Rn   Rr   Rt   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRm   &  s    c         C   s;   y |  j  | SWn% t k
 r6 d | } t | f f SXd  S(   Ns#   unknown error %d in native function(   R   t   KeyErrort   SystemError(   R   Rt   t   msg(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   get_exception5  s
    
(   RZ   R[   R   R   Rm   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR]     s   		t   CPUCallConvc           B   s   e  Z d  Z e j d  Z d   Z d   Z d d d d  Z	 d   Z
 d   Z d   Z d   Z e d	  Z d
   Z d   Z d   Z d   Z RS(   s  
    The calling convention for CPU targets.
    The implemented function signature is:

        retcode_t (<Python return type>*, excinfo **, ... <Python arguments>)

    The return code will be one of the RETCODE_* constants.
    If RETCODE_USEREXC, the exception info pointer will be filled with
    a pointer to a constant struct describing the raised exception.

    Caller is responsible for allocating slots for the return value
    and the exception info pointer (passed as first and second arguments,
    respectively).
    i   c         C   s   d  S(   N(   Re   (   R   R(   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR:   R  s    c         C   sr   |  j  | j  } | j | j j k sN t t | j  t | j j  f   | j | |  |  j | t  d  S(   N(	   t   _get_return_argumentRA   R    R_   R`   Ra   Rb   R/   Rc   (   R   R(   R-   Rd   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR"   U  s
    $c         C   sB  | d  k	 r2 t | t  r2 t d | f   n  | d  k	 rd t | t  rd t d | f   n  | d  k r| t   } n  |  j j |  } | d  k	 r | j   } | d  k r | } n  | | j	 | j
 f } d  | k r d  } q n d  } | | | f } | j |  }	 |  j | j  }
 | j |	 |
  |  j | t  d  S(   Ns-   exc should be None or exception class, got %rs(   exc_args should be None or tuple, got %r(   Re   Rf   Rg   Rh   R#   Ri   R   t   get_python_apiRj   Rk   Rl   t   serialize_objectt   _get_excinfo_argumentRA   Rb   R/   R|   (   R   R(   RO   Rn   Ro   Rp   t   pyapiRq   Rr   t	   struct_gvt   excptr(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRu   \  s,    	c         C   s<   |  j  | j  } | j | j |  |  j | | j  d  S(   N(   R   RA   Rb   R   R/   R   (   R   R(   RM   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRv   }  s    c         C   s   | j  |  d  S(   N(   Rx   (   R   R(   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR/     s    c         C   s   | j  d | t  } | j  d | t  } | j  d | t  } | j  d | t  } | j | |  } | j |  }	 | j  d | t  }
 | j |
 | t	 j
 t t	 j   } t d | d | d |	 d | d | d |
 d	 | d
 |  } | S(   sP   
        Given a return *code* and *excinfoptr*, get a Status instance.
        s   ==s   >=R   R   R
   R   R	   R   R   R   (   Ry   Rc   R0   R1   R3   Rz   R{   R|   t   selectR   R   t   excinfo_ptr_tt	   UndefinedR   (   R   R(   R   R   R}   R   RO   R   R~   R   R   RM   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s$    	c         C   sV   |  j  |  } t | j  } |  j |  } t j t | t j t  g |  } | S(   sQ   
        Get the implemented Function type for *restype* and *argtypes*.
        (	   RR   R   R   R6   R   R   R   t   PointerTypeR   (   R   R9   RT   RW   R   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s    	
c   	      C   s  |  j  |  } | j |  j |  g  | D] } d | ^ q(  |  j |  } d | _ | j d  | j d  |  j |  } d | _ | j d  | j d  | r |  j |  } xC | D]8 } t | j t	 j
  r | j d  | j d  q q Wn  | S(   sU   
        Set names of function arguments, and add useful attributes to them.
        s   arg.Rd   t	   nocaptureR   t   excinfo(   RR   R   RQ   R   R   t   add_attributeR   R#   R    R   R   (	   R   R   R^   R   R   RW   R   t   retargt   excarg(    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s$    		c         C   s   | j  d S(   s@   
        Get the Python-level arguments of LLVM *func*.
        i   (   R^   (   R   RU   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyRQ     s    c         C   s   | j  d S(   Ni    (   R^   (   R   RU   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s    c         C   s   | j  d S(   Ni   (   R^   (   R   RU   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s    c         C   s   |  j  | j  j } t j | |  } | j t j |  |  t j | t j t	  d d } |  j
 |  }	 t |	 j | |   } | | g | }
 | j | |
  } |  j | | | j |   } | j |  } |  j j | | |  } | | f S(   s3   
        Call the Numba-compiled *callee*.
        R   R   (   R   t   function_typeR_   R   R   Rb   R   R   R   t	   excinfo_tRR   R   R   R   R   R   R   R   (   R   R(   R   R   R   R^   R)   R   R   RW   R   R   RM   R-   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s    	N(   RZ   R[   R   t	   itertoolst   countt   _status_idsR:   R"   Re   Ru   Rv   R/   R   R   R   R   RQ   R   R   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR   A  s   		 							t
   ErrorModelc           B   s    e  Z d    Z d d d  Z RS(   c         C   s   | |  _  d  S(   N(   t	   call_conv(   R   R   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s    c         C   s.   |  j  r& |  j j | t | |  t St Sd  S(   N(   t   raise_on_fp_zero_divisionR   Ru   t   ZeroDivisionErrort   TrueR   (   R   R(   Rn   Ro   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   fp_zero_division  s
    	N(   RZ   R[   R   Re   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s   	t   PythonErrorModelc           B   s   e  Z d  Z e Z RS(   sL   
    The Python error model.  Any invalid FP input raises an exception.
    (   RZ   R[   R   R   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s   t   NumpyErrorModelc           B   s   e  Z d  Z e Z RS(   s6  
    In the Numpy error model, floating-point errors don't raise an
    exception.  The FPU exception state is inspected by Numpy at the
    end of a ufunc's execution and a warning is raised if appropriate.

    Note there's no easy way to set the FPU exception state from LLVM.
    Instructions known to set an FP exception can be optimized away:
        https://llvm.org/bugs/show_bug.cgi?id=6050
        http://lists.llvm.org/pipermail/llvm-dev/2014-September/076918.html
        http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140929/237997.html
    (   RZ   R[   R   R   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyR     s   t   pythont   numpyc         C   s   t  |  | j  S(   sF   
    Create an error model instance for the given target context.
    (   t   error_modelsR   (   t
   model_nameR   (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   create_error_model  s    (   R   R   R	   R
   R   R   R   R   (%   R   t   collectionsR    R   t   llvmliteR   t   numbaR   R   t   baseR   R   R   t   IntTypet   int32_tR   R   Rc   R1   R0   R3   R   R|   t   objectR   R\   R]   t   LiteralStructTypeR   R   R   R   R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/numba/targets/callconv.pyt   <module>   sH          		k~!
