
    piK                        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 j2                  e      ZdgZ ej:                  e	j<                  d      Z ej:                  e	j@                  d       Z! ej:                  e	j@                  d      Z" ej:                  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)lookup_environment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     U/opt/services/ai/voice_agent/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__H   s    " r   c                 <    d| j                   d| j                  dS )NzExportEntry(, ))r   r   )r!   s    r   __repr__zExportEntry.__repr__M   s    (,T^^DDr   N)__name__
__module____qualname____doc__r"   r&    r   r   r   r   C   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!   r2   r/   r4   aot_optionss        r   r"   z_ModuleCompiler.__init__q   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 | _        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,                  j0                  |i |      }|j2                  }|j4                  D ]%  }	|	j6                  j9                  d      sd|	_        ' |j<                  j>                  }
|j@                  jC                  |
      }| j                  rtd|_        |j<                  jD                  }|j@                  jC                  |      }| jG                  |jH                        |_        d|_        |jJ                  jL                  jO                  |j<                  jP                  |j<                  jR                        }|| j                   |<   |jT                  | j                  |<   |j<                  jV                  | j                  |<   |jX                  D ]k  }|j2                  }|jZ                  D ]N  }	|	j6                  }|j9                  d      s!t]        |      }|/|j_                         s@|| j                  |<   P m O|jH                  |_        | j`                  jc                  |jH                          | j                  r3|je                  d      }| jg                  |       |j#                  |       |ji                          |jk                         D ]V  }|j6                  | j`                  vs|j:                  tl        jn                  tl        jp                  hv rd	|_9        Pd
|_9        X |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_ZN08NumbaEnvwrapperdefaulthidden):exported_function_typesfunction_environmentsenvironment_gvsextra_environmentsr8   codegencreate_libraryr/   r   
no_compiler0   no_cpython_wrapperno_cfunc_wrapperr4   nrtr   create_nrt_moduleadd_ir_moduler2   r   r5   r    r   argsreturn_type_final_module	functionsname
startswithlinkagefndescllvm_func_namerC   get_functionllvm_cpython_wrapper_namer<   r   r6   	call_convget_function_typerestypeargtypesenvironmentenv_name_linking_librariesglobal_variablesr   	can_cacher1   appendcreate_ir_moduler@   finalizeget_defined_functionsr   privaterF   
visibility)r!   rP   rC   flags
nrt_module_entrycresmodulegvr;   	llvm_funcwrappernamerI   fnty	linkedlib	linkedmodgvnenvwrapper_modulefns                        r   _cull_exportsz_ModuleCompiler._cull_exports   sd   
 (*$%'"!"$,,&&((()9)9: &&'+E$%)E"<<EI%77EMJ!!*-(( )	6E !4!4dll % % 4 4 % ; ;U')7	<D **F&& 077%%&=>!/BJ0 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+ ")!;!; 	GI ) 7 7I'88 G gg>>/:"4S"9C"#&==?CFD$;$;C$@G	G "'	  ''5S)	6V ""$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)r0   r   openwriteemit_bitcoder!   outputwrapkwsrC   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r   )r0   r   r   r   emit_native_objectr   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splitextr   r   r   r2   r   r   r   rY   joinrX   )	r!   r   fnameextr   export_entryr\   re   rX   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_methodsrF   )r2   r   r<   rL   r   Functionr8   insert_const_stringConstantliteral_structbitcastlt
_void_starMETH_VARARGS_AND_KEYWORDSNULLrl   ZEROr   method_def_tyr   add_global_variabletypeinitializerr^   gep)r!   r?   method_defsru   r\   r`   r{   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	                  |       ? |j                  | j                  j                         D cg c]  }|j                  |j                         c}       t        | j                  |      }| j                  j                  |d|      }	|	j                  t        t        g      S c c}w )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)r2   rM   serialize_uncachedconstsrl   extendrO   valuesr   
env_def_tyr8   insert_unique_constr   r   )
r!   r?   builderpyapienv_defsru   r   env_defenv_defs_initrx   s
             r   _emit_environment_arrayz'_ModuleCompiler._emit_environment_array  s     (( 	%E,,U3C..szz:GOOG$		% 	..557
 $$SZZ0
 	 .dooxH\\--k.D.;= vvtTl##
s   6"C-c           	         g }| j                   D ]]  }| j                  |   }| j                  j                  ||      }|j	                  t
        j                        }|j                  |       _ |j                  | j                  D cg c];  }| j                  j                  ||      j	                  t
        j                        = c}       |j                  t        j                  d             t        t
        j                  |      }	| j                  j                  |d|	      }|j                  t        t        g      S c c}w )zj
        Emit an array of Environment pointers that needs to be filled at
        initialization.
        Nz.module_envgvs)r2   rN   r8   declare_env_globalr   r   r   rl   r   rO   r   r   r   r   )
r!   r?   r   r   env_settersru   
envgv_namerx   envgvenv_setters_inits
             r   _emit_envgvs_arrayz"_ModuleCompiler._emit_envgvs_array1  s   
 (( 	&E--e4J00jIBJJr}}-Eu%		& 	
 #55

 	 LL++ gbmm$%
 	 	2==./0L\\--k.>.>@ vvtTl##
s   A E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)
r3   r   FunctionTyper   _int32r   method_def_ptrenv_def_ptrr   call)	r!   r?   r   modobjr   	env_arrayenvgv_arrayr{   r   s	            r   _emit_module_init_codez&_ModuleCompiler._emit_module_init_codeM  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	   r   r   r   r   r   r   r   r   r   r+   r   r   r-   r-   Q   s     )B(("--*,--*,))*,--*9 :M
 $R^^M2N &%%r}}')yy')}}')}}')yy	'2 3J
 !"..,K-1"
 Q Qf/5
J @$.$8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   )retrX   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   r\   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)NrG   z.module_def_baserF   z.module_defEntry)-r   r   r   r^   r8   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   pointeerY   r   r   r   is_not_nullr1   rl   r\   )r!   r?   create_module_fnmod_name_constmod_def_base_initmod_def_baser   mod_def_initmod_defmod_init_fnru   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   numba.core.environmentr   	getLoggerr'   logger__all__r   r   r   r   r   ONEr   r   objectr   r-   r   r+   r   r   <module>r)     s     	 
  $ ' 4 4 9 * (  5 
		8	$,r{{2==$'r{{299a bkk"))Q'BKK		37 !(HE& EIf IXb2_ b2r   