o
    ۾i/                     @   s2  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 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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"Z(G d#d$ d$e(eZ)dS )%    N)ir)_dynfunc)PyCallWrapper)BaseContext)	utilstypesconfigcgutilscallconvcodegen	externalsfastmathpass
intrinsics)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   pyobject_fields r   r   B/home/ubuntu/.local/lib/python3.10/site-packages/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globals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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__)self	typingctxtarget	__class__r   r    r(   )   s   zCPUContext.__init__c                 C   s   | j |S r&   )_internal_codegen_create_empty_module)r)   namer   r   r    create_module-   s   zCPUContext.create_modulec                 C   s>   t jdk| _td| _t dkrt	d t
j|  d S )N    z
numba.execarmv7lzlibgcc_s.so.1)r   MACHINE_BITSis32bitr   JITCPUCodegenr.   platformmachinellload_library_permanentlyr   c_math_functionsinstallr)   r   r   r    init0   s
   
zCPUContext.initc           (      C   s  t |  ddlm}m}m}m}m}m}m	}m
}m}	m}
m}m}m} ddlm}m} ddlm}m} ddl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&}m'} ddlm(} ddlm)}  ddlm*}!m+}"m,}#m-}$ ddlm.}% ddl/m0}& | 1|!j2 | 1|%j2 | 1|"j2 | 1| j2 | 1|#j2 | 1|$j2 | 1|&j3 t4j5j67  ddl8m9}' d S )Nr   )builtinscharseqenumimplhashingheapq	iteratorslistobjnumbersrangeobjsetobjslicingtupleobjunicode)optionalinline_closurecall)gdb_hookliteral)linalg	arraymatharrayobj)generator_coregenerator_methods)polynomial_corepolynomial_functions)	typeddictdictimpl)	typedlist
listobject)jitclassfunction_type)
npdatetime)npyimpl)	cmathimplmathimpl	printimpl
randomimpl)cffiimpl)ClassBuilder)ndarray):r   
initializenumba.cpythonr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   
numba.corerL   rM   
numba.miscrN   rO   numba.nprP   rQ   rR   numba.np.randomrS   rT   numba.np.polynomialrU   rV   numba.typedrW   rX   rY   rZ   numba.experimentalr[   r\   r]   r^   r_   r`   ra   rb   rc    numba.experimental.jitclass.baserd   install_registryregistryclass_impl_registrynumbacoreentrypointsinit_allnumba.np.unsafere   )(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\   r]   r^   r_   r`   ra   rb   rc   jitclassimplre   r   r   r    load_additional_registries<   s0   
<z%CPUContext.load_additional_registriesc                 C   s   | j jS r&   )r.   target_datar=   r   r   r    rz   h   s   zCPUContext.target_datac                 K   s    t j|fi |}| j|ddS )NT)r.   aot_mode)r   AOTCPUCodegen	subtarget)r)   r0   aot_optionsaot_codegenr   r   r    with_aot_codegenl   s   zCPUContext.with_aot_codegenc                 C   s   | j S r&   )r.   r=   r   r   r    r   q   s   zCPUContext.codegenc                 C   s
   t | S r&   )r
   CPUCallConvr=   r   r   r    	call_convt   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.
        offsetof_env_bodyT)refcast_ref)r	   pointer_addr   
_impl_infor!   )r)   builderenvptrbody_ptrr   r   r    get_env_bodyx   s   zCPUContext.get_env_bodyFc                 C   sZ   |  |j| | j}||}| |}|j||| jjd | ||}|	| j
||S )N)return_pyobject	debug_msg)declare_env_globalmoduleget_env_namefndescloadget_python_apiemit_environment_sentryenv_namer   get_env_managerenvironment)r)   r   r   envgvenvargpyapienv_bodyr   r   r    r      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.
        offsetof_generator_state)return_type)r	   r   r   r   )r)   r   genptrr   r   r   r    get_generator_state   s   zCPUContext.get_generator_statec                 C      ddl m} || |||S )zR
        Build a list from the Numba *list_type* and its initial *items*.
        r   )rE   )rg   rE   
build_list)r)   r   	list_typeitemsrE   r   r   r    r         zCPUContext.build_listc                 C   r   )zP
        Build a set from the Numba *set_type* and its initial *items*.
        r   )rH   )rg   rH   	build_set)r)   r   set_typer   rH   r   r   r    r      r   zCPUContext.build_setc                 C   s   ddl m} || ||||S )Nr   )
dictobject)rm   r   	build_map)r)   r   	dict_type
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   rewrite_moduler5   r   
fix_divmodadd_linking_libraryr   library)r)   modr   r   r   r    post_lowering   s
   
zCPUContext.post_loweringc           
   	   C   sX   |  d}| j|j|j}t|||j}t| ||||||d}	|		  |
| d S )Nwrapper)call_helperrelease_gil)r1   r   get_function_typerestypeargtypesr   Functionllvm_func_namer   buildadd_ir_module)
r)   r   r   r   r   r   wrapper_modulefntywrapper_calleer   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6  |}| } j|||  |jt }||}|| || || W d    n1 sw   Y  || || d S )	Ncfunc_wrapperc                    s   g | ]}  |qS r   )get_value_type).0tyr=   r   r    
<listcomp>   s    z3CPUContext.create_cfunc_wrapper.<locals>.<listcomp>entry)noinline)attrsF)likely)r1   r   r   r   r   r   r   r   r   FunctionTypellvm_cfunc_wrapper_name	IRBuilderappend_basic_blockcall_functionargsif_thenis_errorr   
gil_ensureraise_errorinsert_const_stringr   reprstring_from_stringerr_write_unraisabledecrefgil_releaseretr   )r)   r   r   r   r   r   r   r   ll_argtypesll_return_typewraptywrapfnr   statusoutr   	gil_statecstrstrobjr   r=   r    create_cfunc_wrapper   s8   






zCPUContext.create_cfunc_wrapperc                 C   sV   | |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.)
get_pointer_to_functionllvm_cpython_wrapper_namequalnamer   make_functionlookup_modulesplitr   set_envr   )r)   r   r   r   fnptrdoc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   Arrayint32get_abi_sizeofr   )r)   ndimarytyr   r   r    calc_array_sizeof   s   zCPUContext.calc_array_sizeofc                 C   s
   t |S r&   )r   get_ufunc_info)r)   	ufunc_keyr   r   r    r     s   
zCPUContext.get_ufunc_info)r%   )F)r   r   r   __doc__allow_dynamic_globalsr(   r1   r   r>   ry   propertyrz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r,   r    r$   #   s6    
,


		
 r$   nopythonforceobjlooplift_nrtdebugboundschecknogilno_rewritesno_cpython_wrapperno_cfunc_wrapperparallelr   error_modelinlineforceinline_dbg_extend_lifetimes_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s0|jr,d|_ntj|_| ds9|j|_	d|_
|d	 |jd
dd |d |jrUd|_d S d S )Nenable_pyobjectTenable_loopliftnrt)default	debuginfodbg_extend_lifetimesr  r   r	  pythonr  F)is_setr  r  inherit_if_not_setr   DEBUGINFO_DEFAULTr  r  EXTEND_VARIABLE_LIFETIMESr  enable_pyobject_loopliftr  dbg_optnone)r)   flagsoptionsr   r   r    finalize#  s(   







zCPUTargetOptions.finalizeN)r   r   r   r  r   r   r   r    r  "  s    r  )*r7   llvmlite.bindingbindingr9   llvmliter   rs   r   numba.core.callwrapperr   numba.core.baser   rh   r   r   r   r	   r
   r   r   r   r   numba.core.optionsr   r   numba.core.runtimer   numba.core.compiler_lockr   numba.core.entrypointsnumba.core.cpu_optionsr   r   r   rj   r   	Structurer   r!   r$   _options_mixinr  r   r   r   r    <module>   sJ    , k