a
    –§»a0F  ã                   @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlm  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mZ d d	lmZ e  e¡Zd
gZe
j ej¡Z e
j !ej"d ¡Z#e
j !ej"d¡Z$e
j !ej"d¡Z%dd„ Z&G dd„ de'ƒZ(G dd„ de'ƒZ)G dd„ de)ƒZ*dS )é    N)Úir)ÚLinkage)Ú
llvm_types)Úcompile_extraÚFlags)Úglobal_compiler_lock)Ú
cpu_target)Ú	nrtdynmod)ÚcgutilsZCompileré   é   c                  C   s$   dd l } dd l}| dt| dƒ ¡S )Nr   a      #include <stdint.h>

    #ifndef HAVE_LONGDOUBLE
        #define HAVE_LONGDOUBLE %d
    #endif

    typedef struct {
        float real;
        float imag;
    } complex64;

    typedef struct {
        double real;
        double imag;
    } complex128;

    #if HAVE_LONGDOUBLE
    typedef struct {
        long double real;
        long double imag;
    } complex256;
    #endif

    typedef float float32;
    typedef double float64;
    #if HAVE_LONGDOUBLE
    typedef long double float128;
    #endif
    Z
complex256)ÚnumpyÚtextwrapÚdedentÚhasattr)r   r   © r   úM/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/pycc/compiler.pyÚ
get_header   s
    ãr   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚExportEntryz0
    A simple record for exporting symbols.
    c                 C   s   || _ || _|| _d S ©N)ÚsymbolÚ	signatureÚfunction)Úselfr   r   r   r   r   r   Ú__init__G   s    zExportEntry.__init__c                 C   s   d| j | jf S )NzExportEntry(%r, %r))r   r   ©r   r   r   r   Ú__repr__L   s    zExportEntry.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   B   s   r   c                   @   sÆ   e Zd ZdZej ejej	ej
ejf¡Zej e¡Zej ej	ej
ej	f¡Zej e¡Zddd„Zdd„ Zdd„ Ze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„ ZdS )Ú_ModuleCompilerzÕA base class to compile Python modules to a single shared library or
    extension module.

    :param export_entries: a list of ExportEntry instances.
    :param module_name: the name of the exported module.
    Fc                 K   sH   || _ d| _g | _|| _d | _|| _tj| _tjj	| j fi |¤Ž| _
d S )NF)Úmodule_nameÚexport_python_wrapÚdll_exportsÚexport_entriesÚexternal_init_functionÚuse_nrtr   Útyping_contextÚtarget_contextZwith_aot_codegenÚcontext)r   r%   r"   r'   Zaot_optionsr   r   r   r   j   s    ÿÿz_ModuleCompiler.__init__c                 C   s
   d|f S )Nz._pycc_method_%sr   )r   Ú	func_namer   r   r   Ú_mangle_method_symbolx   s    z%_ModuleCompiler._mangle_method_symbolc                 C   s   t ‚dS )zAEmit generated Python wrapper and extension module code.
        N)ÚNotImplementedError)r   Úllvm_moduler   r   r   Ú_emit_python_wrapper{   s    z$_ModuleCompiler._emit_python_wrapperc                 C   s²  i | _ i | _i | _| j ¡ }| | j¡}tƒ }d|_| j	sFd|_
d|_| jrld|_t | j¡\}}| |¡ | jD ]Ì}t| j| j|j|jj|jj|i |d�}|jj}|j |¡}	| j	�r(tj|	_|jj}
|j |
¡}|   |j!¡|_"tj#|_|j$j% &|jj'|jj(¡}|| j |< |j)| j|< |jj*| j|< qr|j!|	_"| j+ ,|j!¡ qr| j	�rf| -d¡}|  .|¡ | |¡ | /¡  | 0¡ D ]6}|j"| j+v�rv|jt1j2t1j3hv �r¤d|_4nd|_4�qv|S )z€Read all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        T)ÚlocalsÚlibraryÚwrapperÚdefaultÚhidden)5Úexported_function_typesÚfunction_environmentsÚenvironment_gvsr*   ÚcodegenZcreate_libraryr"   r   Z
no_compiler#   Zno_cpython_wrapperZno_cfunc_wrapperr'   Znrtr	   Zcreate_nrt_moduleZadd_ir_moduler%   r   r(   r   r   ÚargsÚreturn_typeZfndescÚllvm_func_namer1   Zget_functionÚlcÚLINKAGE_INTERNALÚlinkageZllvm_cpython_wrapper_namer,   r   ÚnameÚLINKAGE_EXTERNALr)   Z	call_convZget_function_typeÚrestypeÚargtypesÚenvironmentÚenv_namer$   ÚappendZcreate_ir_moduler/   ÚfinalizeZget_defined_functionsr   ÚprivateZinternalZ
visibility)r   r8   r1   ÚflagsZ
nrt_moduleÚ_ÚentryZcresr+   Z	llvm_funcZwrappernamer2   ÚfntyZwrapper_moduleÚfnr   r   r   Ú_cull_exports€   s`    



üÿ




z_ModuleCompiler._cull_exportsc                 K   sJ   || _ |  ¡ }t|dƒ�}| | ¡ ¡ W d   ƒ n1 s<0    Y  d S ©NÚwb)r#   rM   ÚopenÚwriteZemit_bitcode©r   ÚoutputÚwrapÚkwsr1   Úfoutr   r   r   Úwrite_llvm_bitcodeÁ   s    z"_ModuleCompiler.write_llvm_bitcodec                 K   sJ   || _ |  ¡ }t|dƒ�}| | ¡ ¡ W d   ƒ n1 s<0    Y  d S rN   )r#   rM   rP   rQ   Zemit_native_objectrR   r   r   r   Úwrite_native_objectÇ   s    z#_ModuleCompiler.write_native_objectc                 C   s&   t |ƒ}d|v r"| d¡r"|d7 }|S )NÚint)Ú8Z16Z32Z64Z_t)ÚstrÚendswith)r   ZtyobjZret_valr   r   r   Ú	emit_typeÍ   s
    
z_ModuleCompiler.emit_typec           	         s¨   t j |¡\}}t|d dƒ�v}| tƒ ¡ | d¡ ˆ jD ]H}|j}ˆ  |j	j
¡}d ‡ fdd„|j	jD ƒ¡}| d|||f ¡ q<W d   ƒ n1 sš0    Y  d S )Nz.hÚwz
/* Prototypes */
z, c                 3   s   | ]}ˆ   |¡V  qd S r   )r]   )Ú.0Zargtyper   r   r   Ú	<genexpr>Ü   s   ÿz._ModuleCompiler.emit_header.<locals>.<genexpr>zextern %s %s(%s);
)ÚosÚpathÚsplitextrP   rQ   r   r%   r   r]   r   r:   Újoinr9   )	r   rS   ÚfnameÚextrV   Zexport_entryr?   rA   r9   r   r   r   Úemit_headerÔ   s    

ÿz_ModuleCompiler.emit_headerc                 C   sÒ   g }| j D ]d}|j}|  |¡}| j| }t |||¡}| j ||¡}tj	 
|tj	 |tj¡ttf¡}	| |	¡ q
tj	 
ttttg¡}
| |
¡ tj	 | j|¡}t ||jd¡}||_tj|_tj	 |ttg¡}|S )z
        Collect exported methods and emit a PyMethodDef array.

        :returns: a pointer to the PyMethodDef array.
        z.module_methods)r%   r   r,   r5   r   ÚFunctionr*   Úinsert_const_stringr<   ÚConstantÚstructÚbitcastÚltÚ
_void_starÚMETH_VARARGS_AND_KEYWORDSÚNULLrE   ÚZEROÚarrayÚmethod_def_tyr
   Úadd_global_variableÚtypeÚinitializerr=   r>   Úgep)r   r.   Zmethod_defsrJ   r?   r;   rK   ZlfuncÚmethod_nameZmethod_def_constÚsentinelZmethod_array_initÚmethod_arrayZmethod_array_ptrr   r   r   Ú_emit_method_arrayà   s0    


ý
þz"_ModuleCompiler._emit_method_arrayc           
      C   s^   g }| j D ]$}| j| }| |j¡}| |¡ q
tj | j|¡}| j	 
|d|¡}	|	 ttg¡S )z©
        Emit an array of env_def_t structures (see modulemixin.c)
        storing the pickled environment constants for each of the
        exported functions.
        z.module_environments)r%   r6   Zserialize_uncachedZconstsrE   r<   rj   rr   Ú
env_def_tyr*   Úinsert_unique_constrw   rq   )
r   r.   ÚbuilderÚpyapiZenv_defsrJ   ÚenvZenv_defZenv_defs_initÚgvr   r   r   Ú_emit_environment_arrayÿ   s    

þz'_ModuleCompiler._emit_environment_arrayc           
      C   sl   g }| j D ]2}| j| }| j ||¡}| tj¡}| |¡ q
tj	 
tj|¡}	| j |d|	¡}| ttg¡S )zj
        Emit an array of Environment pointers that needs to be filled at
        initialization.
        z.module_envgvs)r%   r7   r*   Zdeclare_env_globalrl   rm   rn   rE   r<   rj   rr   r}   rw   rq   )
r   r.   r~   r   Zenv_settersrJ   Z
envgv_namer�   ZenvgvZenv_setters_initr   r   r   Ú_emit_envgvs_array  s    

þz"_ModuleCompiler._emit_envgvs_arrayc           	      C   sP   | j rHt tj|j| j| j|jg¡}t ||| j ¡}| 	|||||g¡S dS dS )z@
        Emit call to "external" init function, if any.
        N)
r&   r   ÚFunctionTyperm   Ú_int32ru   Úmethod_def_ptrÚenv_def_ptrrh   Úcall)	r   r.   r~   Zmodobjrz   Ú	env_arrayÚenvgv_arrayrK   rL   r   r   r   Ú_emit_module_init_code#  s    ÿÿÿz&_ModuleCompiler._emit_module_init_codeN)F)F)F)r   r   r   r    r<   ÚTyperk   rm   Ú
_int8_starrn   r…   rs   Úpointerr†   r|   r‡   r   r,   r/   r   rM   rW   rX   r]   rg   r{   r‚   rƒ   r‹   r   r   r   r   r!   P   s,   
ý

@

r!   c                   @   s¾   e Zd Zdd„ ZeejejƒZeejejƒZeejejeej	ƒZ
eejej	ƒZeejƒZejZej ejeejejf¡Zej eeeejejee
eef	¡Zedd„ ƒZedd„ ƒZdd„ Zd	S )
ÚModuleCompilerc                 G   s   t j t j | |¡¡S r   )r<   rŒ   rŽ   r   )Úretr9   r   r   r   Ú<lambda>5  ó    zModuleCompiler.<lambda>c                 C   s:   t j tjt j | j¡tjf¡}d}tjr2|d7 }||fS )zn
        Return the signature and name of the Python C API function to
        initialize the module.
        ZPyModule_Create2Z	TraceRefs)	r<   rŒ   r   rm   Ú_pyobject_head_prŽ   Úmodule_def_tyr…   Z_trace_refs_)r   r   r?   r   r   r   Úmodule_create_definitiono  s    
ÿÿz'ModuleCompiler.module_create_definitionc                 C   s   t j tjd¡}|d| j fS )zX
        Return the name and signature of the module's initialization function.
        r   ZPyInit_)r<   rŒ   r   rm   r“   r"   )r   r   r   r   r   Úmodule_init_definition  s    z%ModuleCompiler.module_init_definitionc                 C   s<  t j|g| j¢R Ž }tj|_| j || j¡}tj	 
tjtj	 | j¡tj	 tj¡tj	 tj¡f¡}t ||jd¡}||_tj|_|  |¡}tj	 
||tj	 | j¡tj	 tjd¡|tj	 | j¡tj	 | j¡tj	 | j¡tj	 | j¡f	¡}t ||jd¡}||_tj|_t j|g| j¢R Ž }	|	 d¡}
t |
¡}| j |¡}|  ||tj	 tj!t"j#¡f¡}| $t %||¡¡�& | &t' (|	jj)j*¡¡ W d   ƒ n1 �s”0    Y  |  +|||¡}|  ,|||¡}|  -||||||¡}|d u�r | $t .||¡¡�$ | &tj	 |j¡¡ W d   ƒ n1 �s0    Y  | &|¡ | j/ 0|	j1¡ d S )Nz.module_def_baseéÿÿÿÿz.module_defZEntry)2r   rh   r•   r<   r@   r>   r*   ri   r"   rj   rk   rm   Z_pyobject_head_initÚnullÚ	m_init_tyÚ_llvm_py_ssize_tr“   r
   rt   ru   rv   r=   r{   Ú
_char_starrY   Ú
inquiry_tyÚtraverseproc_tyÚfreefunc_tyr–   Zappend_basic_blockZBuilderZget_python_apirˆ   r…   ÚsysÚapi_versionZif_thenZis_nullr�   rp   rl   Zpointeer:   r‚   rƒ   r‹   Zis_not_nullr$   rE   r?   )r   r.   Zcreate_module_fnZmod_name_constZmod_def_base_initZmod_def_baserz   Zmod_def_initZmod_defZmod_init_fnrJ   r~   r   Úmodr‰   rŠ   r�   r   r   r   r/   ˆ  sr    ÿýÿþ
øÿ
ÿ

ÿÿ6
ÿ
4
z#ModuleCompiler._emit_python_wrapperN)r   r   r   Z_ptr_funrm   Z_int8r“   Zvisitproc_tyrœ   rn   r�   rž   r™   r�   r›   r<   rŒ   rk   Z_pyobject_headrš   Zmodule_def_base_tyr!   r†   r”   Úpropertyr•   r–   r/   r   r   r   r   r�   3  sJ   ÿÿýÿ

ýø


r�   )+Úloggingra   rŸ   Zllvmliter   Zllvmlite.bindingr   Zllvmlite.llvmpy.coreZllvmpyÚcorer<   Z
numba.pyccr   rm   Znumba.core.compilerr   r   Znumba.core.compiler_lockr   Znumba.core.registryr   Znumba.core.runtimer	   Z
numba.corer
   Ú	getLoggerr   ÚloggerÚ__all__rj   r˜   rn   rp   rY   r…   rq   ZONEro   r   Úobjectr   r!   r�   r   r   r   r   Ú<module>   s,   
$ d