B
      ›\aO  ã               @   sV  d dl mZmZmZ 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 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 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/ƒZ0G dd„ de/ƒZ1erNe1ne0Z2dS )é    )Úprint_functionÚdivisionÚabsolute_importN)Úir)ÚLinkage)Úcgutils)ÚIS_PY3é   )Ú
llvm_types)Úcompile_extraÚFlags)Úglobal_compiler_lock)Ú
cpu_target)Ú	nrtdynmodZ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   ú2lib/python3.7/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__I   s    zExportEntry.__init__c             C   s   d| j | jf S )NzExportEntry(%r, %r))r   r   )r   r   r   r   Ú__repr__N   s    zExportEntry.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   D   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
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   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   l   s    z_ModuleCompiler.__init__c             C   s
   d|f S )Nz._pycc_method_%sr   )r   Ú	func_namer   r   r   Ú_mangle_method_symbolz   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	sH| d¡ | j
rr| d¡ t | j¡\}}| |¡ xÖ| jD ]Ì}t| j| j|j|jj|jj|i |d}|jj}|j |¡}	| j	r0tj|	_|jj}
|j |
¡}|  |j¡|_tj |_|j!j" #|jj$|jj%¡}|| j |< |j&| j|< |jj'| j|< qz|j|	_| j( )|j¡ qzW | j	rp| *d¡}|  +|¡ | |¡ | ,¡  xB| -¡ D ]6}|j| j(kr‚|jt.j/t.j0hkr°d|_1nd|_1q‚W |S )z€Read all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        Z
no_compileZno_cpython_wrapperZnrt)ÚlocalsÚlibraryÚwrapperÚdefaultZhidden)2Úexported_function_typesÚfunction_environmentsÚenvironment_gvsr,   ÚcodegenZcreate_libraryr$   r   Úsetr%   r)   r   Zcreate_nrt_moduleZadd_ir_moduler'   r   r*   r   r   ÚargsÚreturn_typeZfndescÚllvm_func_namer3   Zget_functionÚlcÚLINKAGE_INTERNALÚlinkageZllvm_cpython_wrapper_namer.   r   ÚnameÚLINKAGE_EXTERNALr+   Z	call_convZget_function_typeÚrestypeZargtypesZenvironmentZenv_namer&   ÚappendZcreate_ir_moduler1   ÚfinalizeZget_defined_functionsr   ZprivateZinternalZ
visibility)r   r9   r3   ÚflagsZ
nrt_moduleÚ_ÚentryZcresr-   Z	llvm_funcZwrappernamer4   ÚfntyZwrapper_moduleÚfnr   r   r   Ú_cull_exports‚   sZ    










z_ModuleCompiler._cull_exportsc          	   K   s6   || _ |  ¡ }t|dƒ}| | ¡ ¡ W d Q R X d S )NÚwb)r%   rK   ÚopenÚwriteZemit_bitcode)r   ÚoutputÚwrapÚkwsr3   Úfoutr   r   r   Úwrite_llvm_bitcodeÂ   s    z"_ModuleCompiler.write_llvm_bitcodec          	   K   s6   || _ |  ¡ }t|dƒ}| | ¡ ¡ W d Q R X d S )NrL   )r%   rK   rM   rN   Zemit_native_object)r   rO   rP   rQ   r3   rR   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ƒp}| tƒ ¡ | d¡ xRˆ jD ]H}|j}ˆ  |j	j
¡}d ‡ fdd„|j	jD ƒ¡}| d|||f ¡ q>W W d Q R X d S )Nz.hÚwz
/* Prototypes */
z, c             3   s   | ]}ˆ   |¡V  qd S )N)rY   )Ú.0Zargtype)r   r   r   ú	<genexpr>Ý   s   z._ModuleCompiler.emit_header.<locals>.<genexpr>zextern %s %s(%s);
)ÚosÚpathÚsplitextrM   rN   r   r'   r   rY   r   r<   Újoinr;   )	r   rO   ÚfnameZextrR   Zexport_entryrA   rC   r;   r   )r   r   Úemit_headerÕ   s    
z_ModuleCompiler.emit_headerc             C   sÔ   g }xn| j D ]d}|j}|  |¡}| j| }|j||d}| j ||¡}tj 	|tj 
|tj¡ttf¡}	| |	¡ qW tj 	ttttg¡}
| |
¡ tj | j|¡}| |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.
        )rA   z.module_methods)r'   r   r.   r6   Úadd_functionr,   Úinsert_const_stringr>   ÚConstantÚstructÚbitcastÚltÚ
_void_starÚMETH_VARARGS_AND_KEYWORDSÚNULLrD   ÚZEROÚarrayÚmethod_def_tyÚadd_global_variableÚtypeÚinitializerr?   r@   Úgep)r   r0   Zmethod_defsrH   rA   r=   rI   ZlfuncZmethod_nameZmethod_def_constÚsentinelZmethod_array_initÚmethod_arrayZmethod_array_ptrr   r   r   Ú_emit_method_arrayá   s*    


z"_ModuleCompiler._emit_method_arrayc       
      C   sb   g }x.| j D ]$}| j| }| |j¡}| |¡ qW 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'   r7   Zserialize_uncachedZconstsrD   r>   re   rm   Ú
env_def_tyr,   Úinsert_unique_constrr   rl   )
r   r0   ÚbuilderÚpyapiZenv_defsrH   ÚenvZenv_defZenv_defs_initÚgvr   r   r   Ú_emit_environment_arrayÿ   s    
z'_ModuleCompiler._emit_environment_arrayc       
      C   sp   g }x<| j D ]2}| j| }| j ||¡}| tj¡}| |¡ qW 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'   r8   r,   Zdeclare_env_globalrg   rh   ri   rD   r>   re   rm   rw   rr   rl   )
r   r0   rx   ry   Zenv_settersrH   Z
envgv_namer{   ZenvgvZenv_setters_initr   r   r   Ú_emit_envgvs_array  s    
z"_ModuleCompiler._emit_envgvs_arrayc       	      C   sN   | j rFt tj|j| j| j|jg¡}| || j ¡}| 	|||||g¡S dS dS )z@
        Emit call to "external" init function, if any.
        N)
r(   r   ÚFunctionTyperh   Ú_int32rp   Úmethod_def_ptrÚenv_def_ptrrc   Úcall)	r   r0   rx   Zmodobjrt   Ú	env_arrayÚenvgv_arrayrI   rJ   r   r   r   Ú_emit_module_init_code#  s    z&_ModuleCompiler._emit_module_init_codeN)F)F)F)r   r    r!   r"   r>   ÚTyperf   rh   Ú
_int8_starri   r   rn   Úpointerr€   rv   r   r   r.   r1   r   rK   rS   rT   rY   rb   ru   r|   r}   r…   r   r   r   r   r#   R   s(   



@

r#   c               @   s,   e Zd Zedd„ ƒZedd„ ƒZdd„ ZdS )ÚModuleCompilerPy2c             C   sP   t j tjtj| jtjtjtjf¡}d}tjr6|d7 }tj	dkrH|d7 }||fS )zPReturn the signature and name of the function to initialize the module.
        ZPy_InitModule4Ú	TraceRefsé@   Z_64)
r>   r†   r   rh   Ú_pyobject_head_pr‡   r€   r   Ú_trace_refs_Z
_plat_bits)r   r   rA   r   r   r   Úmodule_create_definition5  s    


z*ModuleCompilerPy2.module_create_definitionc             C   s   t j t j ¡ d¡d| j fS )zSReturn the signature and name of the function to initialize the extension.
        r   Zinit)r>   r†   r   Zvoidr$   )r   r   r   r   Úmodule_init_definitionI  s    z(ModuleCompilerPy2.module_init_definitionc             C   sÒ   |j | jŽ }| d¡}t |¡}| j |¡}|j | jŽ }tj|_	| j 
|| j¡}|  |¡}| |||ttj tj¡tj tjtj¡f¡}	|  |||¡}
|  |||¡}|  |||	||
|¡ | ¡  | j |j¡ d S )NÚEntry)rc   r   Úappend_basic_blockr>   ÚBuilderr,   Úget_python_apirŽ   rB   r@   rd   r$   ru   r‚   rk   re   Únullrh   rŒ   rU   r   ÚsysÚapi_versionr|   r}   r…   Zret_voidr&   rD   rA   )r   r0   Úmod_init_fnrH   rx   ry   Úcreate_module_fnÚmod_name_constrt   Úmodrƒ   r„   r   r   r   r1   O  s*    




z&ModuleCompilerPy2._emit_python_wrapperN)r   r    r!   ÚpropertyrŽ   r   r1   r   r   r   r   r‰   3  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 )
ÚModuleCompilerPy3c             G   s   t j t j | |¡¡S )N)r>   r†   rˆ   r   )Úretr;   r   r   r   Ú<lambda>v  s    zModuleCompilerPy3.<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_Create2rŠ   )	r>   r†   r   rh   rŒ   rˆ   Úmodule_def_tyr   r   )r   r   rA   r   r   r   rŽ   °  s    

z*ModuleCompilerPy3.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   rh   rŒ   r$   )r   r   r   r   r   r   À  s    z(ModuleCompilerPy3.module_init_definitionc             C   sü  |j | jŽ }tj|_| j || j¡}tj 	t
jtj | j¡tj t
j¡tj t
j¡f¡}| |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	¡}| |jd¡}||_tj|_|j | jŽ }	|	 d¡}
t |
¡}| j |¡}| ||tj t
jt j!¡f¡}| "t# $||¡¡ | %t& '|	jj(j)¡¡ W d Q R X |  *|||¡}|  +|||¡}|  ,||||||¡}|d k	rà| "t# -||¡¡ | %tj |j¡¡ W d Q R X | %|¡ | j. /|	j0¡ d S )Nz.module_def_baseéÿÿÿÿz.module_defr   )1rc   rŽ   r>   rB   r@   r,   rd   r$   re   rf   rh   Z_pyobject_head_initr”   Ú	m_init_tyÚ_llvm_py_ssize_trŒ   ro   rp   rq   r?   ru   Ú
_char_starrU   Ú
inquiry_tyÚtraverseproc_tyÚfreefunc_tyr   r‘   r’   r“   r‚   r   r•   r–   Zif_thenr   Zis_nullr   rk   rg   Zpointeer<   r|   r}   r…   Zis_not_nullr&   rD   rA   )r   r0   r˜   r™   Zmod_def_base_initZmod_def_basert   Zmod_def_initZmod_defr—   rH   rx   ry   rš   rƒ   r„   r   r   r   r   r1   É  s\    


 



z&ModuleCompilerPy3._emit_python_wrapperN)r   r    r!   Z_ptr_funrh   Z_int8rŒ   Zvisitproc_tyr¤   ri   r¥   r¦   r¡   r‡   r£   r>   r†   rf   Z_pyobject_headr¢   Zmodule_def_base_tyr#   r€   rŸ   r›   rŽ   r   r1   r   r   r   r   rœ   t  s:   


	rœ   )3Z
__future__r   r   r   Zloggingr]   r•   Zllvmliter   Zllvmlite.bindingr   Zllvmlite.llvmpy.coreZllvmpyZcorer>   Znumbar   Znumba.utilsr   Ú r
   rh   Znumba.compilerr   r   Znumba.compiler_lockr   Znumba.targets.registryr   Znumba.runtimer   Z	getLoggerr   ZloggerÚ__all__re   r”   ri   rk   rU   r   rl   ZONErj   r   Úobjectr   r#   r‰   rœ   ZModuleCompilerr   r   r   r   Ú<module>   s6   
$ bA 