
    wg>F                        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 j.                  e      ZdgZ ej6                  e	j8                  d      Z ej6                  e	j<                  d       Z ej6                  e	j<                  d      Z  ej6                  e	j<                  d      Z!d Z" G d de#      Z$ G d de#      Z% G d de%      Z&y)    N)ir)Linkage)
llvm_types)create_constant_array)compile_extraFlags)global_compiler_lock)
cpu_target)	nrtdynmod)cgutilsCompiler      c                  N    dd l } dd l}|j                  dt        | d      z        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   s     X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/numba/pycc/compiler.py
get_headerr      s/    ?? : %
&;' ( (    c                       e Zd ZdZd Zd Zy)ExportEntryz0
    A simple record for exporting symbols.
    c                 .    || _         || _        || _        y N)symbol	signaturefunction)selfr   r   r   s       r   __init__zExportEntry.__init__G   s    " r   c                 <    d| j                   d| j                  dS )NzExportEntry(, ))r   r   )r    s    r   __repr__zExportEntry.__repr__L   s    (,T^^DDr   N)__name__
__module____qualname____doc__r!   r%    r   r   r   r   B   s    !
Er   r   c                      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                  ej                  ej                  f      Z ej                  e      ZddZd Zd Zed        ZddZddZd Zd	 Zd
 Zd Zd Zd Zy)_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.
    c                     || _         d| _        g | _        || _        d | _        || _        t        j                  | _        t        j                  j                  | j                   fi || _
        y )NF)module_nameexport_python_wrapdll_exportsexport_entriesexternal_init_functionuse_nrtr
   typing_contexttarget_contextwith_aot_codegencontext)r    r1   r.   r3   aot_optionss        r   r!   z_ModuleCompiler.__init__p   sh    &"',&*#(77!00AA- +-r   c                     d|S )Nz._pycc_method_r*   )r    	func_names     r   _mangle_method_symbolz%_ModuleCompiler._mangle_method_symbol~   s    %.00r   c                     t         )zAEmit generated Python wrapper and extension module code.
        )NotImplementedError)r    llvm_modules     r   _emit_python_wrapperz$_ModuleCompiler._emit_python_wrapper   s
     "!r   c                    i | _         i | _        i | _        | j                  j	                         }|j                  | j                        }t               }d|_        | j                  sd|_
        d|_        | j                  r:d|_        t        j                  | j                        \  }}|j!                  |       | j"                  D ]  }t%        | j&                  | j                  |j(                  |j*                  j,                  |j*                  j.                  |i |      }|j0                  j2                  }|j4                  j7                  |      }	| j                  rd|	_        |j0                  j:                  }
|j4                  j7                  |
      }| j=                  |j>                        |_         d|_        |jB                  jD                  jG                  |j0                  jH                  |j0                  jJ                        }|| j                   |<   |jL                  | j                  |<   |j0                  jN                  | j                  |<   |j>                  |	_         | jP                  jS                  |j>                          | j                  r3|jU                  d      }| jW                  |       |j!                  |       |jY                          |j[                         D ]V  }|j@                  | jP                  vs|j8                  t\        j^                  t\        j`                  hv rd|_1        Pd|_1        X |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_gvsr7   codegencreate_libraryr.   r   
no_compiler/   no_cpython_wrapperno_cfunc_wrapperr3   nrtr   create_nrt_moduleadd_ir_moduler1   r   r4   r   r   argsreturn_typefndescllvm_func_namerB   get_functionlinkagellvm_cpython_wrapper_namer;   r   namer5   	call_convget_function_typerestypeargtypesenvironmentenv_namer0   appendcreate_ir_moduler?   finalizeget_defined_functionsr   privaterC   
visibility)r    rK   rB   flags
nrt_module_entrycresr:   	llvm_funcwrappernamerE   fntywrapper_modulefns                  r   _cull_exportsz_ModuleCompiler._cull_exports   s   
 (*$%'"!,,&&((()9)9: &&'+E$%)E"<<EI%77EMJ!!*-(( 	6E !4!4dll % % 4 4 % ; ;U')7	<D 22I11)<I&&$.	!"kkCC,,33K@#99%,,G",**44FFKK'')=)=?6:,,U3484D4D**51.2kk.B.B$$U+!&	  ''5/	62 ""$55i@N%%n5!!.1 	//1 	-Bwwd...::'//73C3C!DD$-BM$,BM	- r   c                     || _         | j                         }t        |d      5 }|j                  |j	                                d d d        y # 1 sw Y   y xY wNwb)r/   rq   openwriteemit_bitcoder    outputwrapkwsrB   fouts         r   write_llvm_bitcodez"_ModuleCompiler.write_llvm_bitcode   sN    "&$$&&$ 	/4JJw++-.	/ 	/ 	/    AAc                     || _         | j                         }t        |d      5 }|j                  |j	                                d d d        y # 1 sw Y   y xY wrs   )r/   rq   ru   rv   emit_native_objectrx   s         r   write_native_objectz#_ModuleCompiler.write_native_object   sN    "&$$&&$ 	54JJw1134	5 	5 	5r~   c                 P    t        |      }d|v r|j                  d      r|dz  }|S )Nint)8163264_t)strendswith)r    tyobjret_vals      r   	emit_typez_ModuleCompiler.emit_type   s0    e*G 784r   c                     t         j                  j                  |      \  }}t        |dz   d      5 }|j	                  t                      |j	                  d        j                  D ]|  }|j                  } j                  |j                  j                        }dj                   fd|j                  j                  D              }|j	                  d|d|d|d	       ~ 	 d d d        y # 1 sw Y   y xY w)
Nz.hwz
/* Prototypes */
r#   c              3   @   K   | ]  }j                  |        y wr   )r   ).0argtyper    s     r   	<genexpr>z._ModuleCompiler.emit_header.<locals>.<genexpr>   s$      !M%, "&!8 !Ms   zextern  (z);
)ospathsplitextru   rv   r   r1   r   r   r   rT   joinrS   )	r    ry   fnameextr|   export_entryrZ   r]   rS   s	   `        r   emit_headerz_ModuleCompiler.emit_header   s    WW%%f-
s%$,$ 	JJJz|$JJ-. $ 3 3 J#**..)?)?)K)KLyy !M0<0F0F0K0K!M M

GT4HIJ	J 	J 	Js   B6C33C<c                     g }| j                   D ]  }|j                  }| j                  |      }| j                  |   }t	        j
                  |||      }| j                  j                  ||      }t        j                  j                  |t        j                  j                  |t        j                        t        t        f      }	|j                  |	        t        j                  j                  t        t        t         t        g      }
|j                  |
       t#        | j$                  |      }t'        j(                  ||j*                  d      }||_        d|_        t        j                  j1                  |t         t         g      }|S )z
        Collect exported methods and emit a PyMethodDef array.

        :returns: a pointer to the PyMethodDef array.
        z.module_methodsrC   )r1   r   r;   rH   r   Functionr7   insert_const_stringConstantliteral_structbitcastlt
_void_starMETH_VARARGS_AND_KEYWORDSNULLra   ZEROr   method_def_tyr   add_global_variabletypeinitializerrX   gep)r    r>   method_defsrj   rZ   rV   rn   lfuncmethod_namemethod_def_constsentinelmethod_array_initmethod_arraymethod_array_ptrs                 r   _emit_method_arrayz"_ModuleCompiler._emit_method_array   sK    (( 	1E<<D!77=N//6DKKT>BE,,::;MK!{{99$$UBMM:* 
 /0	1 ;;--tT4.FG8$1$2D2DkR22;3D3I3I3DF $5 );;??<$Fr   c                 :   g }| j                   D ]=  }| j                  |   }|j                  |j                        }|j	                  |       ? t        | j                  |      }| j                  j                  |d|      }	|	j                  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)r1   rI   serialize_uncachedconstsra   r   
env_def_tyr7   insert_unique_constr   r   )
r    r>   builderpyapienv_defsrj   envenv_defenv_defs_initgvs
             r   _emit_environment_arrayz'_ModuleCompiler._emit_environment_array  s     (( 	%E,,U3C..szz:GOOG$		%
 .dooxH\\--k.D.;= vvtTl##r   c                    g }| j                   D ]]  }| j                  |   }| j                  j                  ||      }|j	                  t
        j                        }|j                  |       _ t        t
        j                  |      }	| j                  j                  |d|	      }|j                  t        t        g      S )zj
        Emit an array of Environment pointers that needs to be filled at
        initialization.
        z.module_envgvs)r1   rJ   r7   declare_env_globalr   r   r   ra   r   r   r   r   )
r    r>   r   r   env_settersrj   
envgv_namer   envgvenv_setters_inits
             r   _emit_envgvs_arrayz"_ModuleCompiler._emit_envgvs_array  s    
 (( 	&E--e4J00jIBJJr}}-Eu%		& 1L\\--k.>.>@ vvtTl##r   c                 *   | j                   rt        j                  t        j                  |j
                  | j                  | j                  |j
                  g      }t        j                  ||| j                         }|j                  |||||g      S y)z@
        Emit call to "external" init function, if any.
        N)
r2   r   FunctionTyper   _int32r   method_def_ptrenv_def_ptrr   call)	r    r>   r   modobjr   	env_arrayenvgv_arrayrn   rp   s	            r   _emit_module_init_codez&_ModuleCompiler._emit_module_init_code*  s    
 &&??299$*KK1D1D$($4$4k6F6F$HID [$0K0KLB<<V\9%0%2 3 3 r   N)F)r&   r'   r(   r)   r   LiteralStructTyper   
_int8_starr   r   r   PointerTyper   r   r   r!   r;   r?   r	   rq   r}   r   r   r   r   r   r   r   r*   r   r   r,   r,   P   s     )B(("--*,--*,))*,--*9 :M
 $R^^M2N &%%r}}')yy')}}')}}')yy	'2 3J
 !"..,K-1"
 > >@/5
J @$$$$r   r,   c                   :   e Zd Z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        Zed        Zd Zy)ModuleCompilerc                 T    t        j                  t        j                  | |            S r   )r   r   r   )retrS   s     r   <lambda>zModuleCompiler.<lambda><  s    "..d1K"L r   c                     t        j                  t        j                  t        j                  | j
                        t        j                  f      }d}t        j                  r|dz  }||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   rZ   s      r   module_create_definitionz'ModuleCompiler.module_create_definition|  sZ     OOB$7$7%'^^D4F4F%G%'YY%01	 "??KD$r   c                 l    t        j                  t        j                  d      }|d| j                  z   fS )zX
        Return the name and signature of the module's initialization function.
        r*   PyInit_)r   r   r   r   r.   )r    r   s     r   module_init_definitionz%ModuleCompiler.module_init_definition  s0    
 OOB$7$7<	)d&6&6666r   c                 ^   t        j                  |g| j                   }d|_        | j                  j                  || j                        }t         j                  j                  t        j                  t        j                  | j                  d       t        j                  t        j                  d       t        j                  t        j                  d       f      }t        j                  ||j                   d      }||_        d|_        | j%                  |      }t         j                  j                  ||t        j                  | j&                  d       t        j                  t        j                  d      |t        j                  | j(                  d       t        j                  | j*                  d       t        j                  | j(                  d       t        j                  | j,                  d       f	      }t        j                  ||j                   d      }||_        d|_        t        j                  |g| j.                   }	|	j1                  d      }
t        j2                  |
      }| j                  j5                  |      }|j7                  ||t        j                  t        j8                  t:        j<                        f      }|j?                  t        j@                  ||            5  |jC                  tD        jG                  |	j                   jH                  jJ                               d d d        | jM                  |||      }| jO                  |||      }| jQ                  ||||||      }|]|j?                  t        jR                  ||            5  |jC                  t        j                  |j                   d              d d d        |jC                  |       | jT                  jW                  |	jX                         y # 1 sw Y   xY w# 1 sw Y   LxY w)NrD   z.module_def_baserC   z.module_defEntry)-r   r   r   rX   r7   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   pointeerT   r   r   r   is_not_nullr0   ra   rZ   )r    r>   create_module_fnmod_name_constmod_def_base_initmod_def_baser   mod_def_initmod_defmod_init_fnrj   r   r   modr   r   r   s                    r   r?   z#ModuleCompiler._emit_python_wrapper  sB    ;;{ST5R5RS#-  99+:>:J:JL KK66##[[.[[,,d3[[,,d3
 22;3D3I3I3EG $5 )..{;{{11[[$/[[,,b1[[$/[[--t4[[$/[[))40	
 --k<;L;L.;=*$ kk+L0K0KL..w7,,u%++G4ll+#KK		3??CEF __W__Wc:; 	LKK[%5%5%=%=%I%IJK	L 00guM	--k7EJ))+w*6	;P?!4!4Wc!BC 9BKK$789 	C 0 01	L 	L9 9s   :AP(0P#P #P,N)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   r?   r*   r   r   r   r   :  s:   LH BHH//1L "((--/J rxx!22+!}}.O 288==*K "IJ .--		
( )B((**
	
M   7 7G2r   r   )'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   	getLoggerr&   logger__all__r   r   r   r   r   ONEr   r   objectr   r,   r   r*   r   r   <module>r     s     	 
  $ ' 4 4 9 * (  
		8	$,r{{2==$'r{{299a bkk"))Q'BKK		37 !(HE& Egf gTb2_ b2r   