o
    iL                     @   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 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)cgutils)lookup_environmentModuleCompiler      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
    
complex256)numpytextwrapdedenthasattr)r   r    r   Q/home/ubuntu/transcripts/venv/lib/python3.10/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__H   s   
zExportEntry.__init__c                 C   s   d| j | jf S )NzExportEntry(%r, %r))r   r   r   r   r   r   __repr__M   s   zExportEntry.__repr__N)__name__
__module____qualname____doc__r   r    r   r   r   r   r   C   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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   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_contextwith_aot_codegencontext)r   r)   r&   r+   aot_optionsr   r   r   r   q   s   z_ModuleCompiler.__init__c                 C   s
   d|f S )Nz._pycc_method_%sr   )r   	func_namer   r   r   _mangle_method_symbol   s   
z%_ModuleCompiler._mangle_method_symbolc                 C   s   t )zAEmit generated Python wrapper and extension module code.
        )NotImplementedError)r   llvm_moduler   r   r   _emit_python_wrapper   s   z$_ModuleCompiler._emit_python_wrapperc                 C   s"  i | _ i | _i | _i | _| j }|| j}t }d|_	| j
s&d|_d|_| jr9d|_t| j\}}|| | jD ]}t| j| j|j|jj|jj|i |d}|j}|jD ]}	|	jdrbd|	_qW|jj}
|j !|
}| j
rd|_|jj"}|j !|}| #|j$|_d|_|j%j&'|jj(|jj)}|| j |< |j*| j|< |jj+| j|< |j,D ]$}|j}|j-D ]}	|	j}|drt.|}|dur|/ r|| j|< qqq<|j$|_| j01|j$ q<| j
r|2d	}| 3| || |4  |5 D ]}|j| j0vr|jt6j7t6j8hv rd
|_9qd|_9q|S )zRead all the exported functions/modules in the translator
        environment, and join them into a single LLVM module.
        T)localslibrary__excinfo_unwrap_argslinkonce_odrinternalexternal_ZN08NumbaEnvNwrapperdefaulthidden):exported_function_typesfunction_environmentsenvironment_gvsextra_environmentsr/   codegencreate_libraryr&   r   
no_compiler'   no_cpython_wrapperno_cfunc_wrapperr+   nrtr
   create_nrt_moduleadd_ir_moduler)   r   r,   r   r   argsreturn_type_final_module	functionsname
startswithlinkagefndescllvm_func_namer7   get_functionllvm_cpython_wrapper_namer2   r   r-   	call_convget_function_typerestypeargtypesenvironmentenv_name_linking_librariesglobal_variablesr   	can_cacher(   appendcreate_ir_moduler5   finalizeget_defined_functionsr   privater:   
visibility)r   rD   r7   flags
nrt_module_entrycresmodulegvr1   	llvm_funcwrappernamer=   fnty	linkedlib	linkedmodgvnenvwrapper_modulefnr   r   r   _cull_exports   s   












z_ModuleCompiler._cull_exportsc                 K   L   || _ |  }t|d}||  W d    d S 1 sw   Y  d S Nwb)r'   rv   openwriteemit_bitcoder   outputwrapkwsr7   foutr   r   r   write_llvm_bitcode   
   "z"_ModuleCompiler.write_llvm_bitcodec                 K   rw   rx   )r'   rv   rz   r{   emit_native_objectr}   r   r   r   write_native_object   r   z#_ModuleCompiler.write_native_objectc                 C   s&   t |}d|v r|dr|d7 }|S )Nint)8163264_t)strendswith)r   tyobjret_valr   r   r   	emit_type   s
   
z_ModuleCompiler.emit_typec           	         s   t j|\}}t|d d<}|t  |d  jD ]$}|j} |j	j
}d fdd|j	jD }|d|||f  qW d    d S 1 sNw   Y  d S )Nz.hwz
/* Prototypes */
z, c                 3   s    | ]}  |V  qd S r   )r   ).0argtyper   r   r   	<genexpr>   s    z._ModuleCompiler.emit_header.<locals>.<genexpr>zextern %s %s(%s);
)ospathsplitextrz   r{   r   r)   r   r   r   rM   joinrL   )	r   r~   fnameextr   export_entryrP   rY   rL   r   r   r   emit_header   s   

"z_ModuleCompiler.emit_headerc                 C   s   g }| j D ]2}|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_methodsr:   )r)   r   r2   r@   r   Functionr/   insert_const_stringConstantliteral_structbitcastlt
_void_starMETH_VARARGS_AND_KEYWORDSNULLr`   ZEROr   method_def_tyr   add_global_variabletypeinitializerrR   gep)r   r4   method_defsri   rP   rT   ro   lfuncmethod_namemethod_def_constsentinelmethod_array_initmethod_arraymethod_array_ptrr   r   r   _emit_method_array   s4   



z"_ModuleCompiler._emit_method_arrayc           
         sx   g }| j D ]}| j| } |j}|| q| fdd| j D  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.
        c                    s   g | ]}  |jqS r   )serialize_uncachedconsts)r   rs   pyapir   r   
<listcomp>'  s    
z;_ModuleCompiler._emit_environment_array.<locals>.<listcomp>z.module_environments)r)   rA   r   r   r`   extendrC   valuesr   
env_def_tyr/   insert_unique_constr   r   )
r   r4   builderr   env_defsri   rs   env_defenv_defs_initrl   r   r   r   _emit_environment_array  s   

z'_ModuleCompiler._emit_environment_arrayc           
         s   g }j D ]}j| }j |}|tj}|| q| fddj	D  |td t
tj|}	j d|	}|ttgS )zj
        Emit an array of Environment pointers that needs to be filled at
        initialization.
        c                    s"   g | ]}j  |tjqS r   )r/   declare_env_globalr   r   r   )r   
envgv_namer4   r   r   r   r   =  s    z6_ModuleCompiler._emit_envgvs_array.<locals>.<listcomp>Nz.module_envgvs)r)   rB   r/   r   r   r   r   r`   r   rC   r   r   r   r   )
r   r4   r   r   env_settersri   r   rl   envgvenv_setters_initr   r   r   _emit_envgvs_array1  s    

z"_ModuleCompiler._emit_envgvs_arrayc           	      C   sL   | j r$ttj|j| j| j|jg}t||| j }|	|||||gS dS )z@
        Emit call to "external" init function, if any.
        N)
r*   r   FunctionTyper   _int32r   method_def_ptrenv_def_ptrr   call)	r   r4   r   modobjr   	env_arrayenvgv_arrayro   ru   r   r   r   _emit_module_init_codeM  s   z&_ModuleCompiler._emit_module_init_codeN)F)r!   r"   r#   r$   r   LiteralStructTyper   
_int8_starr   r   r   PointerTyper   r   r   r   r2   r5   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r%   Q   s8    




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 )
r   c                 G   s   t t | |S r   )r   r   r   )retrL   r   r   r   <lambda>_  s    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.
        PyModule_Create2	TraceRefs)r   r   r   _pyobject_head_pr   module_def_tyr   _trace_refs_)r   r   rP   r   r   r   module_create_definition  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   PyInit_)r   r   r   r   r&   )r   r   r   r   r   module_init_definition  s   z%ModuleCompiler.module_init_definitionc                 C   s0  t j|g| jR  }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|g| jR  }	|	d}
t |
}| j|}|||t t	jtjf}|t || |!t"#|	jj$j% W d    n1 sw   Y  | &|||}| '|||}| (||||||}|d ur
|t)|| |!t |jd  W d    n	1 sw   Y  |!| | j*+|	j, d S )Nr;   z.module_def_baser:   z.module_defEntry)-r   r   r   rR   r/   r   r&   r   r   r   _pyobject_head_init	m_init_ty_llvm_py_ssize_tr   r   r   r   r   r   
_char_star
inquiry_tytraverseproc_tyfreefunc_tyr   append_basic_block	IRBuilderget_python_apir   r   sysapi_versionif_thenis_nullr   r   r   pointeerM   r   r   r   is_not_nullr(   r`   rP   )r   r4   create_module_fnmod_name_constmod_def_base_initmod_def_baser   mod_def_initmod_defmod_init_fnri   r   r   modr   r   r   r   r   r   r5     sv   






z#ModuleCompiler._emit_python_wrapperN)r!   r"   r#   _ptr_funr   _int8r   visitproc_tyr   r   r   r   r   r   r   r   r   _pyobject_headr   module_def_base_tyr%   r   r   propertyr   r   r5   r   r   r   r   r   ]  sT    


))loggingr   r   llvmliter   llvmlite.bindingr   
numba.pyccr   r   numba.core.cgutilsr   numba.core.compilerr   r   numba.core.compiler_lockr   numba.core.registryr	   numba.core.runtimer
   
numba.corer   numba.core.environmentr   	getLoggerr!   logger__all__r   r   r   r   r   ONEr   r   objectr   r%   r   r   r   r   r   <module>   s2   
$  