U
    ,d}\                     @   sJ  d Z ddlm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	d
gZeddZedZeZdd ZedZedZedZedZdZeeZG dd deZG dd deZG dd deZeeeegZe eZ!G dd deZ"G dd deZ#G dd de#Z$G d d! d!e#Z%e$e%d"Z&d#d$ Z'dS )%z3
Calling conventions for Numba-compiled functions.
    )
namedtuple)IterableN)ir)typescgutils)PYOBJECTGENERIC_POINTER	TryStatusin_tryexcinfoStatus)codeis_okis_noneis_erroris_stop_iterationis_python_excis_user_exc
excinfoptr    c                 C   s   t t| S N)r   Constant	errcode_t)r    r   7/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/callconv.py
_const_int'   s    r      c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )BaseCallConvc                 C   s
   || _ d S r   )context)selfr!   r   r   r   __init__9   s    zBaseCallConv.__init__c              	   C   s   |t jkr| | n||kr| jj|||d}t||j}||& | j	||j
|j}| || W 5 Q R X | | n\t|t js||j
kr| jj||||j
d}| j	||j
|}| || ntd||d S )N)value)ZfromtyZtotyzreturning {0} for {1})r   nonereturn_native_noner!   Zmake_helperr   Zas_bool_bitZvalidif_thenZget_return_valuetypedatareturn_value
isinstanceOptionalcastNotImplementedErrorformat)r"   builderrettyZvaltyr$   optvalZvalidbitretvalr   r   r   return_optional_value<   s*    

z"BaseCallConv.return_optional_valuec                 C   s   |  |t d S r   )_return_errcode_rawRETCODE_NONEr"   r0   r   r   r   r&   Y   s    zBaseCallConv.return_native_nonec                 C   s   | j |tdd d S NTmark_exc)r5   RETCODE_EXCr7   r   r   r   
return_exc\   s    zBaseCallConv.return_excc                 C   s   |  |t d S r   )r5   RETCODE_STOPITr7   r   r   r   return_stop_iteration_   s    z"BaseCallConv.return_stop_iterationc                 C   s   | j j|  }| S )zQ
        Get the actual type of the return argument for Numba type *ty*.
        )r!   data_model_managerget_return_typeZ
as_pointer)r"   tyrestyper   r   r   r@   b   s    zBaseCallConv.get_return_typec                 C   s   |  |}||_|S )zS
        Initialize and return a call helper object for the given builder.
        )_make_call_helper_BaseCallConv__call_helper)r"   r0   chr   r   r   init_call_helperi   s    
zBaseCallConv.init_call_helperc                 C   s   |j S r   )rD   r7   r   r   r   _get_call_helperq   s    zBaseCallConv._get_call_helperc              
   C   s   |j  }||jN |  ||j}t|t	|| |
| W 5 Q R X || W 5 Q R X ||j |d || W 5 Q R X ||j || W 5 Q R X |dd || || dS )zT
        Given a non-ok *status*, raise the corresponding Python exception.
        ZPyExc_StopIterationZPyExc_SystemErrorz*unknown error when calling native functionN)functionZappend_basic_blockr'   r   Z	err_clearZunserializer   r   Z	if_likelyZis_not_nullZraise_objectbranchr   Zerr_set_noner   Zerr_set_stringZposition_at_end)r"   r0   apistatusZbbendexcr   r   r   raise_errort   s&    



zBaseCallConv.raise_errorc                 C   s    |  |}| |}|||S )z
        Get the decoded (unpacked) Python arguments with *argtypes*
        from LLVM function *func*.  A tuple of LLVM values is returned.
        )get_arguments_get_arg_packerZfrom_arguments)r"   r0   argtypesfuncZraw_argsarginfor   r   r   decode_arguments   s    

zBaseCallConv.decode_argumentsc                 C   s   | j |S )zF
        Get an argument packer for the given argument types.
        )r!   Zget_arg_packer)r"   rP   r   r   r   rO      s    zBaseCallConv._get_arg_packerN)__name__
__module____qualname__r#   r4   r&   r<   r>   r@   rF   rG   rM   rS   rO   r   r   r   r   r    7   s   	r    c                   @   sf   e Zd ZdZdd Zdd ZdddZd	d
 ZdddZdd Z	dd Z
dddZdd Zdd ZdS )MinimalCallConva  
    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 r   )_MinimalCallHelperr7   r   r   r   rC      s    z!MinimalCallConv._make_call_helperc                 C   sP   |j jd }|j|jjks4tt|jt|jjf||| | |t d S Nr   )	rH   argsr(   pointeeAssertionErrorstrstorer5   
RETCODE_OKr"   r0   r3   retptrr   r   r   r*      s    zMinimalCallConv.return_valueNc           
      C   s   |d k	r t |ts td|f |d k	r@t|ts@td|f |d k	rx| }|d kr\|}||j|jf}d |kr|d }nd }| |}|	|||}	| j
|t|	dd d S )N-exc should be None or exception class, got %r(exc_args should be None or tuple, got %rTr9   )
issubclassBaseException	TypeErrorr+   tuple_raw_function_namefilenamelinerG   _add_exceptionr5   r   )
r"   r0   rL   exc_argsloc	func_namefnamelocinfoZcall_helperexc_idr   r   r   return_user_exc   s&    
zMinimalCallConv.return_user_excc                 C   s   |  ||j d S r   )r5   r   )r"   r0   rK   r   r   r   return_status_propagate   s    z'MinimalCallConv.return_status_propagateFc                 C   s    t |trt|}|| d S r   )r+   intr   ret)r"   r0   r   r:   r   r   r   r5      s    
z#MinimalCallConv._return_errcode_rawc              
   C   sx   | d|t}| d|t}|||}||}| d|t}| d|t}| d|t}	t||||||	|dd}
|
S )z?
        Given a return *code*, get a Status instance.
        ==>=Nr   r   r   r   r   r   r   r   )	icmp_signedr_   r6   or_not_r;   r=   RETCODE_USEREXCr   )r"   r0   r   normr%   okerrrL   r   r   rK   r   r   r   _get_return_status   s"    
z"MinimalCallConv._get_return_statusc                 C   s4   |  |}t|j}| |}tt|g| }|S zQ
        Get the implemented Function type for *restype* and *argtypes*.
        )rO   listargument_typesr@   r   FunctionTyper   r"   rB   rP   rR   ZresptrZfntyr   r   r   get_function_type   s
    


z!MinimalCallConv.get_function_typec                 C   s>   |rt | |}|| |dd |D  d|jd _|S )zA
        Set names and attributes of function arguments.
        c                 S   s   g | ]}d | qS zarg.r   .0ar   r   r   
<listcomp>  s     z5MinimalCallConv.decorate_function.<locals>.<listcomp>z.retr   )r\   rO   assign_namesrN   rZ   name)r"   fnrZ   fe_argtypesnoaliasrR   r   r   r   decorate_function   s    
z!MinimalCallConv.decorate_functionc                 C   s   |j dd S )@
        Get the Python-level arguments of LLVM *func*.
        r   NrZ   r"   rQ   r   r   r   rN     s    zMinimalCallConv.get_argumentsc                 C   s   |j d jj}t||}|t|| | |}|||}|gt	| }	|
||	}
| ||
}||}| j|||}||fS )z3
        Call the Numba-compiled *callee*.
        r   )rZ   r(   r[   r   alloca_oncer^   get_null_valuerO   as_argumentsr   callr   loadr!   get_returned_value)r"   r0   calleerestyargtysrZ   r1   	retvaltmprR   realargsr   rK   r3   outr   r   r   call_function  s    

zMinimalCallConv.call_function)NNN)F)F)rT   rU   rV   __doc__rC   r*   rr   rs   r5   r   r   r   rN   r   r   r   r   r   rW      s     



rW   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rX   z
    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 r   )
exceptionsr"   r   r   r   r#   '  s    z_MinimalCallHelper.__init__c                 C   s"   t | jt }|||f| j|< |S )aV  
        Add a new user exception to this helper. Returns an integer that can be
        used to refer to the added exception in future.

        Parameters
        ----------
        exc :
            exception type
        exc_args : None or tuple
            exception args
        locinfo : tuple
            location information
        )lenr   FIRST_USEREXC)r"   rL   rl   rp   rq   r   r   r   rk   *  s    z!_MinimalCallHelper._add_exceptionc                 C   sF   z| j | W S  tk
r@   d| }t}|f}d}|||f Y S X dS )z
        Get information about a user exception. Returns a tuple of
        (exception type, exception args, location information).

        Parameters
        ----------
        id : integer
            The ID of the exception to look up
        z#unknown error %d in native functionN)r   KeyErrorSystemError)r"   rq   msgrL   rl   rp   r   r   r   get_exception<  s    
z _MinimalCallHelper.get_exceptionN)rT   rU   rV   r   r#   rk   r   r   r   r   r   rX      s   rX   c                   @   s   e Zd ZdZedZdd Zdd Zd'dd	Z	d(d
dZ
dd Zdd Zdd Zdd Zdd Zd)ddZdd Zdd Zd*ddZdd  Zd!d" Zd#d$ Zd+d%d&ZdS ),CPUCallConva  
    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).
    r   c                 C   s   d S r   r   r7   r   r   r   rC   f  s    zCPUCallConv._make_call_helperc                 C   sP   |  |j}|j|jjks4tt|jt|jjf||| | |t d S r   )	_get_return_argumentrH   r(   r[   r\   r]   r^   r5   r_   r`   r   r   r   r*   i  s    zCPUCallConv.return_valueNc                 C   s   |d k	r t |ts td|f |d k	r@t|ts@td|f |d krNt }| j|}|d k	r| }|d krv|}||j|j	f}d |krd }nd }|||f}|
|}	| |j}
||	|
 d S )Nrb   rc   )rd   re   rf   r+   rg   r!   Zget_python_apirh   ri   rj   Zserialize_object_get_excinfo_argumentrH   r^   )r"   r0   rL   rl   rm   rn   Zpyapiro   rp   Z	struct_gvexcptrr   r   r   set_static_user_excp  s.    

zCPUCallConv.set_static_user_excc                 C   sR   t |dd}| j|||||d | |}|r>||d  n| j|tdd d S )NZ_in_try_blockF)rl   rm   rn   targetTr9   )getattrr   check_try_statusrI   r5   r|   )r"   r0   rL   rl   rm   rn   Ztry_info	trystatusr   r   r   rr     s    
 
zCPUCallConv.return_user_excc              	   C   s@   z|j W S  tk
r:   tj|tjddd}||_ | Y S X d S )NZ	try_stateT)r   zfill)Z_CPUCallConv__eh_try_stateAttributeErrorr   r   Zintp_t)r"   r0   ptrr   r   r   _get_try_state  s       zCPUCallConv._get_try_statec                 C   sJ   |  |}||}|d||d}| |j}||}t||dS )N>r   )r
   r   )r   r   Zicmp_unsignedr(   r   rH   r	   )r"   r0   try_state_ptrZ	try_depthr
   r   r   r   r   r   r     s    


zCPUCallConv.check_try_statusc                 C   s6   |  |}||}|||d}||| d S Nr   )r   r   addr(   r^   )r"   r0   r   oldnewr   r   r   set_try_status  s    

zCPUCallConv.set_try_statusc                 C   s\   |  |}||}|||d}||| | |j}t|jj	}||| d S r   )
r   r   subr(   r^   r   rH   r   r   r[   )r"   r0   r   r   r   r   nullr   r   r   unset_try_status  s    

zCPUCallConv.unset_try_statusc              	   C   sX   |  |}| |j}||j| |||j | j||j	dd W 5 Q R X d S r8   )
r   r   rH   r^   r   r'   r{   r
   r5   r   )r"   r0   rK   r   r   r   r   r   rs     s
    
z#CPUCallConv.return_status_propagateFc                 C   s6   | |}|r2|jtddg}|d| d S )Nr   Zret_is_raise)ru   moduleadd_metadatar   IntTypeZset_metadata)r"   r0   r   r:   ru   Zmdr   r   r   r5     s    
zCPUCallConv._return_errcode_rawc              
   C   s   | d|t}| d|t}| d|t}| d|t}|||}||}	| d|t}
||
|t	
tt	j}t|||	|||
||d}|S )zP
        Given a return *code* and *excinfoptr*, get a Status instance.
        rv   rw   rx   )ry   r_   r6   r;   r=   rz   r{   r|   selectr   r   excinfo_ptr_t	Undefinedr   )r"   r0   r   r   r}   r%   rL   r   r~   r   r   rK   r   r   r   r     s(    
zCPUCallConv._get_return_statusc                 C   s<   |  |}t|j}| |}tt|ttg| }|S r   )	rO   r   r   r@   r   r   r   PointerTyper   r   r   r   r   r     s    


zCPUCallConv.get_function_typec                    s     |}| |dd |D   |}d|_|d |d  |}d|_|d |d |r |}|D ]&}t|jt	j
r|d |d q fdd}	tt|	|}
|
r|jd	}||j|g |S )
zU
        Set names of function arguments, and add useful attributes to them.
        c                 S   s   g | ]}d | qS r   r   r   r   r   r   r     s     z1CPUCallConv.decorate_function.<locals>.<listcomp>ra   Z	nocapturer   r   c                    s(   t | tjs$ jj}||   r$dS dS NTF)r+   r   Arrayr!   r?   Zcontains_nrt_meminfo)rA   Zdmmr   r   r   type_may_always_need_nrt  s
    z?CPUCallConv.decorate_function.<locals>.type_may_always_need_nrtZnumba_args_may_always_need_nrt)rO   r   rN   r   r   Zadd_attributer   r+   r(   r   r   anymapr   Zadd_named_metadatar   r   )r"   r   rZ   r   r   rR   ZretargZexcargr   r   Zargs_may_always_need_nrtZnmdr   r   r   r     s8    








zCPUCallConv.decorate_functionc                 C   s   |j dd S )r      Nr   r   r   r   r   rN   &  s    zCPUCallConv.get_argumentsc                 C   s
   |j d S rY   r   r   r   r   r   r   ,  s    z CPUCallConv._get_return_argumentc                 C   s
   |j d S r   r   r   r   r   r   r   /  s    z!CPUCallConv._get_excinfo_argumentc                 C   s   |  |jj}t||}|t|| tj|tt	dd}	| 
|}
t|
||}||	g| }|dkrvd}n&t|trt|tst|}ntd|j|||d}| ||||	}||}| j|||}||fS )aU  
        Call the Numba-compiled *callee*.
        Parameters:
        -----------
        attrs: LLVM style string or iterable of individual attributes, default
               is None which specifies no attributes. Examples:
               LLVM style string: "noinline fast"
               Equivalent iterable: ("noinline", "fast")
        r   )r   Nr   z,attrs must be an iterable of strings or None)attrs)r   Zfunction_typer[   r   r   r^   r   r   r   	excinfo_trO   r   r   r+   r   r]   rg   rf   r   r   r   r!   r   )r"   r0   r   r   r   rZ   r   r1   r   r   rR   r   _attrsr   rK   r3   r   r   r   r   r   2  s*    


zCPUCallConv.call_function)NNN)NNN)F)F)N)rT   rU   rV   r   	itertoolscountZ_status_idsrC   r*   r   rr   r   r   r   r   rs   r5   r   r   r   rN   r   r   r   r   r   r   r   r   U  s0   
  
   



- r   c                   @   s   e Zd Zdd ZdddZdS )
ErrorModelc                 C   s
   || _ d S r   )	call_conv)r"   r   r   r   r   r#   ^  s    zErrorModel.__init__Nc                 C   s$   | j r| j|t|| dS dS d S r   )raise_on_fp_zero_divisionr   rr   ZeroDivisionError)r"   r0   rl   rm   r   r   r   fp_zero_divisiona  s    zErrorModel.fp_zero_division)NN)rT   rU   rV   r#   r   r   r   r   r   r   \  s   r   c                   @   s   e Zd ZdZdZdS )PythonErrorModelzL
    The Python error model.  Any invalid FP input raises an exception.
    TNrT   rU   rV   r   r   r   r   r   r   r   j  s   r   c                   @   s   e Zd ZdZdZdS )NumpyErrorModela6  
    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
    FNr   r   r   r   r   r   q  s   r   )pythonZnumpyc                 C   s   t |  |jS )zF
    Create an error model instance for the given target context.
    )error_modelsr   )Z
model_namer!   r   r   r   create_error_model  s    r   )(r   collectionsr   collections.abcr   r   Zllvmliter   Z
numba.corer   r   Znumba.core.baser   r   r	   r   r   Zint32_tr   r   r_   r;   r6   r=   r   r|   objectr    rW   rX   ZLiteralStructTyper   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sD   
k~1
  	