
    J/PhP.                        d dl Z d dl mZmZmZmZmZmZ d dlmZm	Z	  G d de j
                  Z G d de j
                  Z G d d	          Z G d
 dej                  Z G d dej                  ZdddddZej        ee ee           ee          gej        j        _        ej        ej        j        _        ej        gej        j        _        ej        ej        j        _        ej        ee ee          gej        j        _        ej        ej        j        _        ej        gej        j        _        ej        e ee          e ee          e ee          e ee          g	ej        j        _        ej        ej        j        _        ej         ee          gej        j        _        eej        j        _        dS )    N)POINTERc_boolc_char_pc_uint8c_uint64c_size_t)ffitargetsc                   $    e Zd ZdefdefdefgZdS )_LinkElementelement_kindvalue	value_lenN)__name__
__module____qualname__r   r   r   _fields_     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/llvmlite/binding/orcjit.pyr   r      s-        )(#h')HHHr   r   c                       e Zd ZdefdefgZdS )_SymbolAddressnameaddressN)r   r   r   r   r   r   r   r   r   r   r      s"        "Y$9:HHHr   r   c                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )JITLibraryBuildera   
    Create a library for linking by OrcJIT

    OrcJIT operates like a linker: a number of compilation units and
    dependencies are collected together and linked into a single dynamic library
    that can export functions to other libraries or to be consumed directly as
    entry points into JITted code. The native OrcJIT has a lot of memory
    management complications so this API is designed to work well with Python's
    garbage collection.

    The creation of a new library is a bit like a linker command line where
    compilation units, mostly as LLVM IR, and previously constructed libraries
    are linked together, then loaded into memory, and the addresses of exported
    symbols are extracted. Any static initializers are run and the exported
    addresses and a resource tracker is produced. As long as the resource
    tracker is referenced somewhere in Python, the exported addresses will be
    valid. Once the resource tracker is garbage collected, the static
    destructors will run and library will be unloaded from memory.
    c                 H    g | _         t                      | _        i | _        d S N)_JITLibraryBuilder__entriesset_JITLibraryBuilder__exports_JITLibraryBuilder__importsselfs    r   __init__zJITLibraryBuilder.__init__%   s    r   c                 ~    | j                             dt          |                              d          f           | S )z
        Adds a compilation unit to the library using LLVM IR as the input
        format.

        This takes a string or an object that can be converted to a string,
        including IRBuilder, that contains LLVM IR.
        r   utf-8r   appendstrencode)r$   llvmirs     r   add_irzJITLibraryBuilder.add_ir*   s8     	q#f++"4"4W"="=>???r   c                 ~    | j                             dt          |                              d          f           | S )a  
        Adds a compilation unit to the library using native assembly as the
        input format.

        This takes a string or an object that can be converted to a string that
        contains native assembly, which will be
        parsed by LLVM.
           r'   r(   )r$   asms     r   add_native_assemblyz%JITLibraryBuilder.add_native_assembly5   s6     	q#c((//'":":;<<<r   c                 X    | j                             dt          |          f           | S )z
        Adds a compilation unit to the library using pre-compiled object code.

        This takes the bytes of the contents of an object artifact which will be
        loaded by LLVM.
           )r   r)   bytes)r$   datas     r   add_object_imgz JITLibraryBuilder.add_object_imgA   s*     	q%++.///r   c                     t          |d          5 }| j                            d|                                f           ddd           n# 1 swxY w Y   | S )z
        Adds a compilation unit to the library using pre-compiled object file.

        This takes a string or path-like object that references an object file
        which will be loaded by LLVM.
        rbr3   N)openr   r)   read)r$   	file_pathfs      r   add_object_filez!JITLibraryBuilder.add_object_fileK   s     )T"" 	1aN!!1affhh-000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   /AAAc                 ~    | j                             dt          |                              d          f           | S )z
        Adds an existing JIT library as prerequisite.

        The name of the library must match the one provided in a previous link
        command.
           r'   r(   r$   r   s     r   add_jit_libraryz!JITLibraryBuilder.add_jit_libraryV   s8     	q#d))"2"27";";<===r   c                 :    | j                             d           | S )z
        Allows the JITted library to access symbols in the current binary.

        That is, it allows exporting the current binary's symbols, including
        loaded libraries, as imports to the JITted
        library.
        )r?   r   )r   r)   r#   s    r   add_current_processz%JITLibraryBuilder.add_current_process`   s     	h'''r   c                 N    t          |          | j        t          |          <   | S )z
        Register the *address* of global symbol *name*.  This will make
        it usable (e.g. callable) from LLVM-compiled functions.
        )r   r"   r*   )r$   r   r   s      r   import_symbolzJITLibraryBuilder.import_symbolk   s#    
 %-W$5$5s4yy!r   c                 T    | j                             t          |                     | S )a/  
        During linking, extract the address of a symbol that was defined in one
        of the compilation units.

        This allows getting symbols, functions or global variables, out of the
        JIT linked library. The addresses will be
        available when the link method is called.
        )r!   addr*   r@   s     r   export_symbolzJITLibraryBuilder.export_symbols   s%     	3t99%%%r   c                 >   |j         r
J d            t          |                              d          }t          |          dk    s
J d            t	          t          | j                  z              }t          | j                  D ]c\  }\  }}t          |          ||         _        t          |          ||         _
        t          t          |                    ||         _        dt          t          | j                  z              t          | j                  D ]%\  }}|                    d          |         _        &t          t          | j                  z              }	t          | j                                                  D ]5\  }\  }}
|                    d          |	|         _        |
|	|         _        6t'          j                    5 }|j                            |j        ||t          | j                  |	t          | j                  t          | j                  |	  	        }|st1          t          |                    	 ddd           n# 1 swxY w Y   t3          ||fdt          | j                  D                       S )a  
        Link all the current compilation units into a JITted library and extract
        the address of exported symbols.

        An instance of the OrcJIT instance must be provided and this will be the
        scope that is used to find other JITted libraries that are dependencies
        and also be the place where this library will be defined.

        After linking, the method will return a resource tracker that keeps the
        library alive. This tracker also knows the addresses of any exported
        symbols that were requested.

        The addresses will be valid as long as the resource tracker is
        referenced.

        When the resource tracker is destroyed, the library will be cleaned up,
        however, the name of the library cannot be reused.
        zCannot add to closed JITr'   r   zLibrary cannot be emptyNc                 2    i | ]\  }}||         j         S r   )r   ).0idxr   exportss      r   
<dictcomp>z*JITLibraryBuilder.link.<locals>.<dictcomp>   s<      L  L  L$-C !%gcl&:  L  L  Lr   )closedr*   r+   lenr   r   	enumerater   r   r   r   r   r   r   r!   r   r"   itemsr   r	   OutputString_capiLLVMPY_LLJIT_Link_ptrRuntimeErrorResourceTracker)r$   lljitlibrary_nameencoded_library_nameelementsrL   kindr   r   importsaddrouterrtrackerrM   s                @r   linkzJITLibraryBuilder.link   s   & <;;!;;;;"<0077@@'((1,,,.G,,, 3t~#6#6699"+DN";"; 	; 	;C$)0HSM&"*5//HSM&.s5zz&:&:HSM##!C$7$77::"4>22 	5 	5IC $G 4 4GCL!C$7$77::!*4>+?+?+A+A!B!B 	( 	(C$ $G 4 4GCL#'GCL   	06k33
$DN##DN##DN##	 	G  0"3v;;///0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 w+ L  L  L  L1:4>1J1J L  L  LM M 	Ms   A<I%%I),I)N)r   r   r   __doc__r%   r-   r1   r6   r=   rA   rC   rE   rH   rb   r   r   r   r   r      s         &  
	 	 	
 
 
  	 	 	  	 	 	  
 
 
4M 4M 4M 4M 4Mr   r   c                   :    e Zd ZdZd Zd Zed             Zd ZdS )rX   a  
    A resource tracker is created for each loaded JIT library and keeps the
    module alive.

    OrcJIT supports unloading libraries that are no longer used. This resource
    tracker should be stored in any object that reference functions or constants
    for a JITted library. When all references to the resource tracker are
    dropped, this will trigger LLVM to unload the library and destroy any
    functions.

    Failure to keep resource trackers while calling a function or accessing a
    symbol can result in crashes or memory corruption.

    LLVM internally tracks references between different libraries, so only
    "leaf" libraries need to be tracked.
    c                 b    || _         || _        t          j                            | |           d S r   )_ResourceTracker__addresses_ResourceTracker__namer	   	ObjectRefr%   )r$   ptrr   	addressess       r   r%   zResourceTracker.__init__   s/    $tS)))))r   c                     | j         |         S )zE
        Get the address of an exported symbol as an integer
        )rf   )r$   items     r   __getitem__zResourceTracker.__getitem__   s     %%r   c                     | j         S r   )rg   r#   s    r   r   zResourceTracker.name   s
    {r   c                     t          j                    5 }| j                            | |          rt	          t          |                    	 d d d            d S # 1 swxY w Y   d S r   )r	   rS   rT   "LLVMPY_LLJIT_Dylib_Tracker_DisposerW   r*   )r$   r`   s     r   _disposezResourceTracker._dispose   s     	06z<<T6JJ 0"3v;;///0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   8AA!AN)	r   r   r   rc   r%   rm   propertyr   rq   r   r   r   rX   rX      sf          * * *
& & &   X0 0 0 0 0r   rX   c                   :    e Zd ZdZd Zd Zed             Zd ZdS )LLJITa  
    A OrcJIT-based LLVM JIT engine that can compile and run LLVM IR as a
    collection of JITted dynamic libraries

    The C++ OrcJIT API has a lot of memory ownership patterns that do not work
    with Python. This API attempts to provide ones that are safe at the expense
    of some features. Each LLJIT instance is a collection of JIT-compiled
    libraries. In the C++ API, there is a "main" library; this API does not
    provide access to the main library. Use the JITLibraryBuilder to create a
    new named library instead.
    c                 T    d | _         t          j                            | |           d S r   )_tdr	   rh   r%   r$   ri   s     r   r%   zLLJIT.__init__   s'    tS)))))r   c           	         | j         r
J d            t          j                    }t          j                    5 }t          j                            | |                    d          |                    d          t          j        |          |          }|st          t          |                    	 ddd           n# 1 swxY w Y   t          ||||j        i          S )ao  
        Find a function in this dynamic library and construct a new tracking
        object for it

        If the library or function do not exist, an exception will occur.

        Parameters
        ----------
        dylib : str or None
           the name of the library containing the symbol
        fn : str
           the name of the function to get
        zCannot lookup in closed JITr'   N)rO   ctypesr   r	   rS   libLLVMPY_LLJITLookupr+   byrefrW   r*   rX   r   )r$   dylibfnr   r`   ra   s         r   lookupzLLJIT.lookup   s    ;== ====/## 	06g0016g1F1F1371C1C17g1F1F17	9 9G
  0"3v;;///0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 wGM/BCCCs   A:B??CCc                     | j         | j         S t          j                            |           }t	          j        |          | _         d| j         _        | j         S )z9
        The TargetData for this LLJIT instance.
        NT)rv   r	   rz   LLVMPY_LLJITGetDataLayoutr
   
TargetData_ownedrw   s     r   target_datazLLJIT.target_data  sJ    
 88Og//55%c**xr   c                 z    | j         | j                                          | j                            |            d S r   )rv   detachrT   LLVMPY_LLJITDisposer#   s    r   rq   zLLJIT._dispose  s8    8HOO
&&t,,,,,r   N)	r   r   r   rc   r%   r   rr   r   rq   r   r   r   rt   rt      si        
 
* * *D D D6 	 	 X	- - - - -r   rt   F)use_jit_linksuppress_errorsc                    t          j                    5 }t           j                            | |||          }|st	          t          |                    	 ddd           n# 1 swxY w Y   t          |          S )z"
    Create an LLJIT instance
    N)r	   rS   rz   LLVMPY_CreateLLJITCompilerrW   r*   rt   )target_machiner   r   r`   rY   s        r   create_lljit_compilerr     s     
			 ,v22>3B3?39; ;  	,s6{{+++	,, , , , , , , , , , , , , , , <<s   AA""A&)A&r   )ry   r   r   r   r   r   r   llvmlite.bindingr	   r
   	Structurer   r   r   rh   rX   rt   r   LLVMOrcLLJITRefrz   r{   argtypesLLVMOrcDylibTrackerRefrestyper   LLVMTargetDataRefLLVMTargetMachineRefr   r   rU   rp   r   r   r   <module>r      s    I I I I I I I I I I I I I I I I ) ) ) ) ) ) ) )) ) ) ) )6# ) ) ); ; ; ; ;V% ; ; ;bM bM bM bM bM bM bM bMJ#0 #0 #0 #0 #0cm #0 #0 #0L:- :- :- :- :-CM :- :- :-z',*/    $ GHGH'  # &)%?  " . ! * -0,A ! ) 

GH	/ " + .1-@ " * (  $ GLGNGNGH
&  " %($>  ! GH7 * 3 6< * 2 2 2r   