
    wiK                        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 j        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(dS )    N)ir)Linkage)
llvm_types)create_constant_array)compile_extraFlags)global_compiler_lock)
cpu_target)	nrtdynmod)cgutils)lookup_environmentCompiler      c                  ^    dd l } dd l}|                    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     c/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/numba/pycc/compiler.py
get_headerr      sC    LLLOOO?? : %
&
&;' ( ( (    c                       e Zd ZdZd Zd ZdS )ExportEntryz0
    A simple record for exporting symbols.
    c                 0    || _         || _        || _        d S 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   sA         ! ! !
E E E E 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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                     || _         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!   r2   r/   r4   aot_optionss        r   r"   z_ModuleCompiler.__init__q   sj    &"',&*#(7!0A- - +- -r   c                     d|S )Nz._pycc_method_r+   )r!   	func_names     r   _mangle_method_symbolz%_ModuleCompiler._mangle_method_symbol   s     %.Y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                  }t                      }d|_	        | j
        sd|_        d|_        | j        r8d|_        t          j        | j                  \  }}|                    |           | j        D ]}t'          | j        | j        |j        |j        j        |j        j        |i |          }|j        }|j        D ]#}	|	j                            d          rd|	_        $|j        j        }
|j         !                    |
          }| j
        rd|_        |j        j"        }|j         !                    |          }| #                    |j$                  |_        d|_        |j%        j&        '                    |j        j(        |j        j)                  }|| j         |<   |j*        | j        |<   |j        j+        | j        |<   |j,        D ]^}|j        }|j-        D ]M}	|	j        }|                    d          r/t]          |          }||/                                r
|| j        |<   N_|j$        |_        | j0        1                    |j$                   | j
        r?|2                    d	          }| 3                    |           |                    |           |4                                 |5                                D ]>}|j        | j0        vr.|j        tl          j7        tl          j8        hv rd
|_9        7d|_9        ?|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_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   sj   
 (*$%'"!"$,&&(((()9:: & 	*'+E$%)E"< 	.EI%7EEMJ!!*---( )	6 )	6E !4dl % % 4 % ;U')7	< < <D *F& 0 07%%&=>> 0!/BJ2I11)<<I& 6$.	!"kC,33K@@#99%,GG",*4FFK')=? ?6:,U3484D*51.2k.B$U+ ")!; 	G 	GI ) 7I'8 G G g>>/:: G"4S"9"9C"#&==?? !GCFD$;C$@G	G "'	 ''5555" 	2$55i@@N%%n555!!.111 	//11 	- 	-Bwd...:'/73C!DDD$-BMM$,BMr   c                     || _         |                                 }t          |d          5 }|                    |                                           d d d            d S # 1 swxY w Y   d S Nwb)r0   r   openwriteemit_bitcoder!   outputwrapkwsrC   fouts         r   write_llvm_bitcodez"_ModuleCompiler.write_llvm_bitcode   s    "&$$&&&$ 	/4JJw++--...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/   (A!!A%(A%c                     || _         |                                 }t          |d          5 }|                    |                                           d d d            d S # 1 swxY w Y   d S r   )r0   r   r   r   emit_native_objectr   s         r   write_native_objectz#_ModuleCompiler.write_native_object   s    "&$$&&&$ 	54JJw1133444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5r   c                 `    t          |          }d|v r|                    d          r|dz  }|S )Nint)8163264_t)strendswith)r!   tyobjret_vals      r   	emit_typez_ModuleCompiler.emit_type   s=    e**G 788  4r   c                     t           j                            |          \  }}t          |dz   d          5 }|                    t                                 |                    d            j        D ]r}|j        }                     |j	        j
                  }d                     fd|j	        j        D                       }|                    d|d|d|d	           s	 d d d            d S # 1 swxY w Y   d S )
Nz.hwz
/* Prototypes */
r$   c              3   B   K   | ]}                     |          V  d S r   )r   ).0argtyper!   s     r   	<genexpr>z._ModuleCompiler.emit_header.<locals>.<genexpr>   sK       !M !M%, "&!8!8 !M !M !M !M !M !Mr   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   sx   W%%f--
s%$,$$ 	JJJz||$$$JJ-... $ 3 J J#*..)?)KLLyy !M !M !M !M0<0F0K!M !M !M M M


GGGTTT444HIIIIJ	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	Js   B1C66C:=C:c                    g }| j         D ]}|j        }|                     |          }| j        |         }t	          j        |||          }| j                            ||          }t          j        	                    |t          j        
                    |t          j                  t          t          f          }	|                    |	           t          j        	                    t          t          t           t          g          }
|                    |
           t#          | j        |          }t'          j        ||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_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   sY    ( 	1 	1E<D!77==N/6DKT>BBE,::;MMK!{99$$UBM::*   
 /0000;--tT4.FGG8$$$1$2DkRR2;3D3I3DF F $5 );??<$FFr   c                    g }| j         D ]>}| j        |         }                    |j                  }|                    |           ?|                    f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                 D    g | ]}                     |j                  S r+   )serialize_uncachedconsts)r   r   pyapis     r   
<listcomp>z;_ModuleCompiler._emit_environment_array.<locals>.<listcomp>'  s9     
 
 
 $$SZ00
 
 
r   z.module_environments)r2   rM   r   r   rl   extendrO   valuesr   
env_def_tyr8   insert_unique_constr   r   )
r!   r?   builderr   env_defsru   r   env_defenv_defs_initrx   s
      `      r   _emit_environment_arrayz'_ModuleCompiler._emit_environment_array  s     ( 	% 	%E,U3C..sz::GOOG$$$$ 
 
 
 
.5577
 
 
 	 	 	 .doxHH\--k.D.;= = vvtTl###r   c                      g } j         D ]^} j        |         } j                            |          }|                    t
          j                  }|                    |           _|                     fd j	        D                        |                    t          j        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                     g | ]:}j                             |                              t          j                  ;S r+   )r8   declare_env_globalr   r   r   )r   
envgv_namer?   r!   s     r   r   z6_ModuleCompiler._emit_envgvs_array.<locals>.<listcomp>=  sT     
 
 

 	 L++  gbm$$	
 
 
r   Nz.module_envgvs)r2   rN   r8   r   r   r   r   rl   r   rO   r   r   r   r   )
r!   r?   r   r   env_settersru   r   rx   envgvenv_setters_inits
   ``        r   _emit_envgvs_arrayz"_ModuleCompiler._emit_envgvs_array1  s   
 ( 	& 	&E-e4J00jIIBJJr}--Eu%%%% 
 
 
 
 

 #5
 
 
 	 	 	 	2=..///0LL\--k.>.>@ @ vvtTl###r   c                     | j         rlt          j        t          j        |j        | j        | j        |j        g          }t          j        ||| j                   }|	                    |||||g          S dS )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    
 & 	?29$*K1D$($4k6F$HI ID [$0KLLB<<V\9%0%2 3 3 3 4r   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22N &%r}')y')}')}')y	'2 3 3J
 !".,,K- - - -1 1 1" " "
 Q Q Qf/ / / /5 5 5 5  
J 
J 
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dS )ModuleCompilerc                 P    t          j        t          j        | |                    S r   )r   r   r   )retrX   s     r   <lambda>zModuleCompiler.<lambda>_  s    ".d1K1K"L"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  sY     OB$7%'^D4F%G%G%'Y%01 1	 "? 	 KD$r   c                 X    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  s+    
 OB$7<<	)d&6666r   c                    t          j        |g| j        R  }d|_        | j                            || j                  }t           j                            t          j
        t          j        | j        d           t          j        t          j        d           t          j        t          j        d           f          }t          j        ||j        d          }||_        d|_        |                     |          }t           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        R  }	|	                    d          }
t          j        |
          }| j                            |          }|                    ||t          j        t          j        t:          j                  f          }|                    t          j         ||                    5  |!                    tD          #                    |	j        j$        j%                             d d d            n# 1 swxY w Y   | &                    |||          }| '                    |||          }| (                    ||||||          }|m|                    t          j)        ||                    5  |!                    t          j        |j        d                      d d d            n# 1 swxY w Y   |!                    |           | j*        +                    |	j,                   d S )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  s    ;{ST5RSSS#-  99+:>:JL L K66#[..[,d33[,d33
 
 2;3D3I3EG G $5 )..{;;{11[$//[,b11[$//[-t44[$//[)400	
 
 -k<;L.;= =*$ k+L0KLLL..w77,u%%++G44ll+#K	3?CCEF F __W_Wc::;; 	L 	LKK[%5%=%IJJKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 00guMM	--k7EJJ))+w*6	;P P?!4Wc!B!BCC 9 9BK$778889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 	C 011111s$   #=K,,K03K0).N##N'*N'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   ]  sh       LLH 8BH/1 1L "(-/ /J hrx!2+!}. .O (28=* *K ""IJ .-		
 ( )B(*
	
 M   X 7 7 X7G2 G2 G2 G2 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 4 4 4 4 4 4 4 4 4 4 4 4 9 9 9 9 9 9 * * * * * * ( ( ( ( ( (       5 5 5 5 5 5 
	8	$	$,r{2=$''r{29a  bk")Q'BK	377 !( !( !(HE E E E E& E E EI I I I If I I IXb2 b2 b2 b2 b2_ b2 b2 b2 b2 b2r   