U
    ,d E                     @   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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e	jdZee	jd Zee	jdZ e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)create_constant_array)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   7/tmp/pip-unpacked-wheel-eu7e0c37/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ejejej	ejfZ
ee
Zeejej	ejfZe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 )_ModuleCompilerzA 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   sD   || _ d| _g | _|| _d | _|| _tj| _tjj	| j f|| _
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$d|	_|jj}
|j|
}| |j|_ d|_|j!j"#|jj$|jj%}|| j |< |j&| j|< |jj'| j|< qr|j|	_ | j()|j qr| j	rb|*d}| +| || |,  |- D ]6}|j | j(krr|jt.j/t.j0hkrd|_1nd|_1qr|S )zRead all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        T)localslibraryinternalexternalwrapperdefaulthidden)2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_namer2   Zget_functionlinkageZllvm_cpython_wrapper_namer-   r   namer*   Z	call_convZget_function_typerestypeargtypesenvironmentenv_namer%   appendZcreate_ir_moduler0   finalizeZget_defined_functionsr   privater3   Z
visibility)r   r;   r2   flagsZ
nrt_module_entryZcresr,   Z	llvm_funcZwrappernamer5   fntyZwrapper_modulefnr   r   r   _cull_exports   sf    



   




z_ModuleCompiler._cull_exportsc              	   K   s6   || _ |  }t|d}||  W 5 Q R X d S Nwb)r$   rM   openwriteZemit_bitcoder   outputwrapkwsr2   foutr   r   r   write_llvm_bitcode   s    z"_ModuleCompiler.write_llvm_bitcodec              	   K   s6   || _ |  }t|d}||  W 5 Q R X 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|k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l}|t  |d  jD ]H}|j} |j	j
}d fdd|j	jD }|d|||f  q<W 5 Q R X 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=   joinr<   )	r   rS   fnameextrV   Zexport_entryr@   rA   r<   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|}t||jd}||_d|_tj|ttg}|S )z
        Collect exported methods and emit a PyMethodDef array.

        :returns: a pointer to the PyMethodDef array.
        z.module_methodsr3   )r&   r   r-   r8   r   Functionr+   insert_const_stringConstantliteral_structbitcastlt
_void_starMETH_VARARGS_AND_KEYWORDSNULLrE   ZEROr   method_def_tyr   add_global_variabletypeinitializerr?   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   s4    



z"_ModuleCompiler._emit_method_arrayc           
      C   sZ   g }| j D ]$}| j| }||j}|| q
t| 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&   r9   Zserialize_uncachedZconstsrE   r   
env_def_tyr+   insert_unique_constrv   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   sh   g }| j D ]2}| j| }| j||}|tj}|| q
t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&   r:   r+   Zdeclare_env_globalrl   rm   rn   rE   r   r|   rv   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   _int32rt   method_def_ptrenv_def_ptrrh   call)	r   r/   r}   Zmodobjry   	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   LiteralStructTyperm   
_int8_starrn   r   rr   PointerTyper   r{   r   r   r-   r0   r   rM   rW   rX   r]   rg   rz   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ejeejejfZe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 t | |S r   )r   r   r   )retr<   r   r   r   <lambda>6      zModuleCompiler.<lambda>c                 C   s6   t tjt | jtjf}d}tjr.|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   rm   _pyobject_head_pr   module_def_tyr   Z_trace_refs_)r   r   r@   r   r   r   module_create_definitionv  s    
z'ModuleCompiler.module_create_definitionc                 C   s   t tjd}|d| j fS )zX
        Return the name and signature of the module's initialization function.
        r   ZPyInit_)r   r   rm   r   r#   )r   r   r   r   r   module_init_definition  s    z%ModuleCompiler.module_init_definitionc                 C   s  t j|f| j }d|_| j|| j}t jt	j
t | jd t t	jd t t	jd f}t||jd}||_d|_| |}t j||t | jd t t	jd|t | jd t | jd t | jd t | jd f	}t||jd}||_d|_t j|f| j }	|	d}
t |
}| j|}|||t t	jtjf}|t || |!t"#|	jj$j% W 5 Q R X | &|||}| '|||}| (||||||}|d k	r|t)|| |!t |jd  W 5 Q R X |!| | j*+|	j, d S )Nr4   z.module_def_baser3   z.module_defZEntry)-r   rh   r   r?   r+   ri   r#   rj   rk   rm   Z_pyobject_head_init	m_init_ty_llvm_py_ssize_tr   r   rs   rt   ru   rz   
_char_star
inquiry_tytraverseproc_tyfreefunc_tyr   Zappend_basic_blockZ	IRBuilderZ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_basery   Zmod_def_initZmod_defZmod_init_fnrJ   r}   r~   modr   r   r   r   r   r   r0     sv    



 
  

z#ModuleCompiler._emit_python_wrapperN)r   r   r    Z_ptr_funrm   Z_int8r   Zvisitproc_tyr   rn   r   r   r   r   r   r   r   Z_pyobject_headr   Zmodule_def_base_tyr"   r   r   propertyr   r   r0   r   r   r   r   r   4  sR   


r   )'loggingra   r   Zllvmliter   Zllvmlite.bindingr   Z
numba.pyccr   rm   Znumba.core.cgutilsr   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   rn   rp   r   rq   ZONEro   r   objectr   r"   r   r   r   r   r   <module>   s,   
$ e