U
    ,d4                     @   sd  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lm Z  d dl!Zd dl"m#Z#m$Z$m%Z% d dl&m'Z' G dd dej(Z)G dd dej(Z*G dd deZ+edddddddddddddd d!d"d#d$Z,G d%d& d&e,eZ-d'd( Z.d)d* Z/d+d, Z0dS )-    N)ir)_dynfunc)PyCallWrapper)BaseContextPYOBJECT)	utilstypesconfigcgutilscallconvcodegen	externalsfastmathpass
intrinsics)cached_property)TargetOptionsinclude_default_options)rtsys)global_compiler_lock)ParallelOptionsFastMathOptionsInlineOptions)ufunc_dbc                   @   s   e Zd ZdejfgZdS )ClosureBodyenvN__name__
__module____qualname__r   Zpyobject_fields r    r    2/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/cpu.pyr      s   r   c                   @   s    e Zd ZdejfdejfgZdS )EnvBodyglobalsZconstsNr   r    r    r    r!   r"      s   r"   c                       s   e Zd ZdZdZd- fdd	Zdd Zedd	 Zd
d Z	e
dd Zdd Zdd Ze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#d$Zd%d& Zd'd( Zd)d* Zd+d, Z  ZS )/
CPUContextz0
    Changes BaseContext calling convention
    Tcpuc                    s   t  || d S N)super__init__)selfZ	typingctxtarget	__class__r    r!   r(   '   s    zCPUContext.__init__c                 C   s   | j |S r&   )_internal_codegenZ_create_empty_module)r)   namer    r    r!   create_module+   s    zCPUContext.create_modulec                 C   sp   t jdk| _td| _t dkr.t	d t
j|  t|  i | _ddlm} ddlm} || j|< d S )N    z
numba.execarmv7lzlibgcc_s.so.1r   )_lower_parfor_parallel)Parfor)r   ZMACHINE_BITSis32bitr   ZJITCPUCodegenr-   platformmachinellZload_library_permanentlyr   Zc_math_functionsinstallr   Z
initializeZlower_extensionsZnumba.parfors.parfor_loweringr2   Znumba.parfors.parforr3   )r)   r2   r3   r    r    r!   init.   s    

zCPUContext.initc           %      C   s\  ddl m}m}m}m}m}m}m}m}m	}	m
}
m}m}m} ddlm} ddlm}m} ddlm}m}m}m} ddlm}m} ddlm}m} ddlm}m} dd	l m!}m"} dd
lm#} ddlm$} ddl m%}m&} m'}!m(}" ddlm)}# ddl*m+}$ | ,|j- | ,|#j- | ,| j- | ,|j- | ,|!j- | ,|"j- | ,|$j. t/j0j12  d S )Nr   )builtinscharseqenumimplhashingheapq	iteratorslistobjnumbersrangeobjsetobjslicingtupleobjunicode)optional)gdb_hookliteral)linalg
polynomial	arraymatharrayobj)generator_coregenerator_methods)	typeddictdictimpl)	typedlist
listobject)jitclassfunction_type)
npdatetime)npyimpl)	cmathimplmathimpl	printimpl
randomimpl)cffiimpl)ClassBuilder)3numba.cpythonr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   
numba.corerG   Z
numba.miscrH   rI   numba.nprJ   rK   rL   rM   Znumba.np.randomrN   rO   numba.typedrP   rQ   rR   rS   Znumba.experimentalrT   rU   rV   rW   rX   rY   rZ   r[   r\   Z numba.experimental.jitclass.baser]   Zinstall_registryregistryZclass_impl_registrynumbacoreZentrypointsZinit_all)%r)   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   Zjitclassimplr    r    r!   load_additional_registriesD   s*    <z%CPUContext.load_additional_registriesc                 C   s   | j jS r&   )r-   target_datar)   r    r    r!   rf   c   s    zCPUContext.target_datac                 K   s   t j|f|}| j|ddS )NT)r-   Zaot_mode)r   ZAOTCPUCodegenZ	subtarget)r)   r.   Zaot_optionsZaot_codegenr    r    r!   with_aot_codegeng   s    zCPUContext.with_aot_codegenc                 C   s   | j S r&   )r-   rg   r    r    r!   r   l   s    zCPUContext.codegenc                 C   s
   t | S r&   )r   ZCPUCallConvrg   r    r    r!   	call_convo   s    zCPUContext.call_convc                 C   s$   t ||tjd }t| ||ddS )z
        From the given *envptr* (a pointer to a _dynfunc.Environment object),
        get a EnvBody allowing structured access to environment fields.
        Zoffsetof_env_bodyT)refZcast_ref)r
   pointer_addr   
_impl_infor"   )r)   builderZenvptrZbody_ptrr    r    r!   get_env_bodys   s      zCPUContext.get_env_bodyc                 C   sX   |  |j| | j}||}| |}|j|| jjd | ||}|	| j
||S )N)Z	debug_msg)Zdeclare_env_globalmoduleget_env_namefndescloadget_python_apiZemit_environment_sentryenv_namern   get_env_managerenvironment)r)   rm   ZenvgvZenvargpyapiZenv_bodyr    r    r!   ru   |   s    


 zCPUContext.get_env_managerc                 C   s   t j||tjd |dS )z~
        From the given *genptr* (a pointer to a _dynfunc.Generator object),
        get a pointer to its state area.
        Zoffsetof_generator_state)return_type)r
   rk   r   rl   )r)   rm   Zgenptrrx   r    r    r!   get_generator_state   s      zCPUContext.get_generator_statec                 C   s   ddl m} || |||S )zR
        Build a list from the Numba *list_type* and its initial *items*.
        r   )r@   )r^   r@   
build_list)r)   rm   Z	list_typeitemsr@   r    r    r!   rz      s    zCPUContext.build_listc                 C   s   ddl m} || |||S )zP
        Build a set from the Numba *set_type* and its initial *items*.
        r   )rC   )r^   rC   	build_set)r)   rm   set_typer{   rC   r    r    r!   r|      s    zCPUContext.build_setc                 C   s   ddl m} || ||||S )Nr   )
dictobject)ra   r~   	build_map)r)   rm   	dict_typeZ
item_typesr{   r~   r    r    r!   r      s    zCPUContext.build_mapc                 C   s4   | j rt|| j  | jr$t| |tj d S r&   )	fastmathr   Zrewrite_moduler4   r   Z
fix_divmodZadd_linking_libraryr   library)r)   modr   r    r    r!   post_lowering   s
    
zCPUContext.post_loweringFc           
   	   C   sX   |  d}| j|j|j}t|||j}t| ||||||d}	|		  |
| d S )Nwrapper)call_helperrelease_gil)r/   ri   get_function_typerestypeargtypesr   Functionllvm_func_namer   buildadd_ir_module)
r)   r   rq   r   r   r   wrapper_modulefntywrapper_calleerm   r    r    r!   create_cpython_wrapper   s    
  z!CPUContext.create_cpython_wrapperc              	      s*    d} j|j|j}t|||j} fdd|jD } |j}	t	|	|}
t||
|j
}t|d} jj|||j|j|jdd\}}|j|jddb  |}| } j|||  |jt }||}|| || || W 5 Q R X || || d S )	NZcfunc_wrapperc                    s   g | ]}  |qS r    )get_value_type).0tyrg   r    r!   
<listcomp>   s     z3CPUContext.create_cfunc_wrapper.<locals>.<listcomp>entry)Znoinline)attrsF)Zlikely)r/   ri   r   r   r   r   r   r   r   FunctionTypeZllvm_cfunc_wrapper_nameZ	IRBuilderZappend_basic_blockcall_functionargsZif_thenZis_errorrs   Z
gil_ensureZraise_errorZinsert_const_stringro   reprZstring_from_stringZerr_write_unraisableZdecrefZgil_releaseretr   )r)   r   rq   r   r   r   r   r   Zll_argtypesZll_return_typeZwraptyZwrapfnrm   statusoutrw   Z	gil_stateZcstrZstrobjr    rg   r!   create_cfunc_wrapper   s6    
    





zCPUContext.create_cfunc_wrapperc                 C   sb   | |j}| |j}d|jf }t| |jdd ||||f}|j	| 
|| |S )z
        Returns
        -------
        (cfunc, fnptr)

        - cfunc
            callable function (Can be None)
        - fnptr
            callable function address
        - env
            an execution environment (from _dynfunc)
        zcompiled wrapper for %r.)Zget_pointer_to_functionr   Zllvm_cpython_wrapper_namequalnamer   Zmake_functionZlookup_modulesplitr   Zset_envrp   )r)   r   rq   r   ZbaseptrZfnptrdocZcfuncr    r    r!   get_executable   s    
  zCPUContext.get_executablec                 C   s    t t j|d}| | |S )zI
        Calculate the size of an array struct on the CPU target
        A)r   ArrayZint32Zget_abi_sizeofr   )r)   ndimZarytyr    r    r!   calc_array_sizeof   s    zCPUContext.calc_array_sizeofc                 C   s
   t |S r&   )r   get_ufunc_info)r)   Z	ufunc_keyr    r    r!   r      s    zCPUContext.get_ufunc_info)r%   )F)r   r   r   __doc__Zallow_dynamic_globalsr(   r/   r   r9   re   propertyrf   rh   r   r   ri   rn   ru   ry   rz   r|   r   r   r   r   r   r   r   __classcell__r    r    r+   r!   r$   !   s4   


		 
r$   ZnopythonZforceobjZloopliftZ_nrtdebugboundscheckZnogilZno_rewritesZno_cpython_wrapperZno_cfunc_wrapperparallelr   error_modelinlineforceinlinetarget_backendZ_dbg_extend_lifetimesZ_dbg_optnonec                   @   s   e Zd Zdd ZdS )CPUTargetOptionsc                 C   s   | dsd|_| ds d|_|jddd | ds@tj|_| ds`|jrXd|_ntj|_| dsr|j|_	d|_
|d	 |jd
dd |d |d |jrd|_d S )Nenable_pyobjectTenable_loopliftZnrt)default	debuginfodbg_extend_lifetimesr   r   r   pythonr   r   F)is_setr   r   Zinherit_if_not_setr	   ZDEBUGINFO_DEFAULTr   r   ZEXTEND_VARIABLE_LIFETIMESr   Zenable_pyobject_loopliftr   Zoptnone)r)   flagsoptionsr    r    r!   finalize  s(    







zCPUTargetOptions.finalizeN)r   r   r   r   r    r    r    r!   r     s   r   c                 C   s    | j D ]}t| t| qdS )zD
    Remove redundant incref/decref within on a per block basis
    N)Zbasic_blocksremove_null_refct_callremove_refct_pairs)funcbbr    r    r!   remove_refct_callsE  s    
r   c                 C   s   dS )z0
    Remove refct api calls to NULL pointer
    Nr    )r   r    r    r!   r   N  s    r   c                 C   s   d}|rd}i }i }| j D ]N}t|tjr|jj}|dkrN|jd }|||< q|dkr|jd }|||< q| D ](}||krr||   ||   d}qrqdS )z9
    Remove incref decref pairs on the same variable
    TFZ	Py_IncRefr   Z	Py_DecRefN)	Zinstructions
isinstancer   Z	CallInstrZcalled_functionr.   ZoperandskeysZerase_from_parent)r   ZdidsomethingZincrefsZdecrefsinstfnameargvalr    r    r!   r   ^  s&    




r   )1sysr5   Zllvmlite.bindingZbindingr7   Zllvmliter   rc   r   Znumba.core.callwrapperr   Znumba.core.baser   r   r_   r   r   r	   r
   r   r   r   r   r   Znumba.core.utilsr   Znumba.core.optionsr   r   Znumba.core.runtimer   Znumba.core.compiler_lockr   Znumba.core.entrypointsZnumba.core.cpu_optionsr   r   r   r`   r   	Structurer   r"   r$   Z_options_mixinr   r   r   r   r    r    r    r!   <module>   sT   , d+	