o
    D2h+                     @   s  d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZmZmZ ejj dddZdd ZG dd	 d	ejZG d
d dejZejejeeegejj_ejejj_ejejeegejj_eejj_ejejgejj _ejej!egejj"_ejgejj#_ejgejj$_ej%ejj$_g ejj&_eejj&_ejegejj'_eejj'_ejegejj(_eejj(_ejej)gejj*_G dd deZ+e	deee+Z,e	deee+Z-e,ej.Z/e-ej0Z1e,e-egejj2_ej3ejj2_ej3gejj4_ejej3gejj5_eejj6_eegejj6_dS )    N)POINTERc_char_pc_boolc_void_pc_intc_uint64c_size_t	CFUNCTYPE	string_atcast	py_object	Structure)ffitargetsobject_filec                 C   sp   |du r
t  dv }t }tj| |||}|s tt|W d   n1 s*w   Y  d|_t	|| dS )a  
    Create a MCJIT ExecutionEngine from the given *module* and
    *target_machine*.

    *lmm* controls whether the llvmlite memory manager is used. If not supplied,
    the default choice for the platform will be used (``True`` on 64-bit ARM
    systems, ``False`` otherwise).
    N)arm64aarch64T)module)
platformmachiner   OutputStringlibLLVMPY_CreateMCJITCompilerRuntimeErrorstr_ownedExecutionEngine)r   target_machineuse_lmmouterrengine r!   t/var/www/html/alexa/alex_system/speach-to-text/venv/lib/python3.10/site-packages/llvmlite/binding/executionengine.pycreate_mcjit_compiler   s   	
r#   c                  C   s    t j } | dkrt| ddS )zp
    Check the system allows execution of in-memory JITted functions.
    An exception is raised otherwise.
    r   zcannot allocate executable memory. This may be due to security restrictions on your system, such as SELinux or similar mechanisms.N)r   r   "LLVMPY_TryAllocateExecutableMemoryOSError)errnor!   r!   r"   check_jit_execution#   s   
r'   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zedd Zdd Zdd Zdd Zd%ddZdd  Zd!d" Zd#d$ ZdS )&r   zAn ExecutionEngine owns all Modules associated with it.
    Deleting the engine will remove all associated modules.
    It is an error to delete the associated modules.
    Nc                 C   s*   t |g| _d| _d|_tj| | dS )z;
        Module ownership is transferred to the EE
        NT)set_modules_tdr   r   	ObjectRef__init__)selfptrr   r!   r!   r"   r,   8   s   zExecutionEngine.__init__c                 C      t j| |dS )z
        Return the address of the function named *name* as an integer.

        It's a fatal error in LLVM if the symbol of *name* doesn't exist.
        ascii)r   r   LLVMPY_GetFunctionAddressencoder-   namer!   r!   r"   get_function_addressA      z$ExecutionEngine.get_function_addressc                 C   r/   )z
        Return the address of the global value named *name* as an integer.

        It's a fatal error in LLVM if the symbol of *name* doesn't exist.
        r0   )r   r   LLVMPY_GetGlobalValueAddressr2   r3   r!   r!   r"   get_global_value_addressI   r6   z(ExecutionEngine.get_global_value_addressc                 C   s   t j| || d S N)r   r   LLVMPY_AddGlobalMapping)r-   gvaddrr!   r!   r"   add_global_mappingQ   s   z"ExecutionEngine.add_global_mappingc                 C   s6   || j v r	tdtj| | d|_| j | dS )zL
        Ownership of module is transferred to the execution engine
        z#module already added to this engineTN)r)   KeyErrorr   r   LLVMPY_AddModuler   add)r-   r   r!   r!   r"   
add_moduleT   s
   
zExecutionEngine.add_modulec                 C      t j|  dS )z}
        Make sure all modules owned by the execution engine are fully processed
        and "usable" for execution.
        N)r   r   LLVMPY_FinalizeObjectr-   r!   r!   r"   finalize_object^      zExecutionEngine.finalize_objectc                 C   rB   )zW
        Run static constructors which initialize module-level static objects.
        N)r   r   LLVMPY_RunStaticConstructorsrD   r!   r!   r"   run_static_constructorse   s   z'ExecutionEngine.run_static_constructorsc                 C   rB   )zh
        Run static destructors which perform module-level cleanup of static
        resources.
        N)r   r   LLVMPY_RunStaticDestructorsrD   r!   r!   r"   run_static_destructorsk   rF   z&ExecutionEngine.run_static_destructorsc                 C   sZ   t  }t j| ||rtt|W d   n1 sw   Y  | j| d|_dS )z1
        Ownership of module is returned
        NF)	r   r   r   LLVMPY_RemoveModuler   r   r)   remover   )r-   r   r   r!   r!   r"   remove_moduler   s   

zExecutionEngine.remove_modulec                 C   s6   | j dur| j S tj| }t|| _ d| j _| j S )z;
        The TargetData for this execution engine.
        NT)r*   r   r   #LLVMPY_GetExecutionEngineTargetDatar   
TargetDatar   )r-   r.   r!   r!   r"   target_data|   s   
zExecutionEngine.target_datac                 C   s   t j| }|S )z
        Enable JIT events for profiling of generated code.
        Return value indicates whether connection to profiling tool
        was successful.
        )r   r   LLVMPY_EnableJITEvents)r-   retr!   r!   r"   enable_jit_events   s   z!ExecutionEngine.enable_jit_eventsc                 C   s6   t |tj}| jD ]}t |jtj|kr|  S q	dS )zH
        Find the ModuleRef corresponding to the given pointer.
        N)r   r   valuer)   _ptr)r-   
module_ptrr.   r   r!   r!   r"   _find_module_ptr   s   
z ExecutionEngine._find_module_ptrc                 C   s(   t |trtj|}tj| | dS )z
        Add object file to the jit. object_file can be instance of
        :class:ObjectFile or a string representing file system path
        N)
isinstancer   r   ObjectFileRef	from_pathr   r   LLVMPY_MCJITAddObjectFile)r-   obj_filer!   r!   r"   add_object_file   s   
zExecutionEngine.add_object_filec                 C   s*   || _ || _t| | _tj| | j dS )z~
        Set the object cache "notifyObjectCompiled" and "getBuffer"
        callbacks to the given Python functions.
        N)_object_cache_notify_object_cache_getbuffer_ObjectCacheRef_object_cacher   r   LLVMPY_SetObjectCache)r-   notify_funcgetbuffer_funcr!   r!   r"   set_object_cache   s   
z ExecutionEngine.set_object_cachec                 C   s`   | j du rdS |jj}|jj}|jj}t||}| |}|du r(td|f |  || dS )z(
        Low-level notify hook.
        N5object compilation notification for unknown module %s)r^   contentsrV   buf_ptrbuf_lenr
   rW   r   )r-   datarV   rh   ri   bufr   r!   r!   r"   _raw_object_cache_notify   s   


z(ExecutionEngine._raw_object_cache_notifyc                 C   sv   | j du rdS |jj}| |}|du rtd|f |  |}|dur9tj|t||d _	t||d _
dS dS )z+
        Low-level getbuffer hook.
        Nrf   r   )r_   rg   rV   rW   r   r   r   LLVMPY_CreateByteStringlenrh   ri   )r-   rj   rV   r   rk   r!   r!   r"   _raw_object_cache_getbuffer   s   


z+ExecutionEngine._raw_object_cache_getbufferc                 C   sH   | j D ]}|  q| jd ur| j  | j   d | _| j|  d S r9   )r)   detachr*   clearra   _capiLLVMPY_DisposeExecutionEngine)r-   modr!   r!   r"   _dispose   s   




zExecutionEngine._dispose)NN)__name__
__module____qualname____doc__ra   r,   r5   r8   r=   rA   rE   rH   rJ   rM   propertyrP   rS   rW   r]   re   rl   ro   ru   r!   r!   r!   r"   r   1   s*    	


	


r   c                   @   s    e Zd ZdZdd Zdd ZdS )r`   zN
    Internal: an ObjectCache instance for use within an ExecutionEngine.
    c                 C   s"   t jtt|}t j| | d S r9   )r   r   LLVMPY_CreateObjectCache_notify_c_hook_getbuffer_c_hookr+   r,   )r-   objr.   r!   r!   r"   r,      s
   z_ObjectCacheRef.__init__c                 C   s   | j |  d S r9   )rr   LLVMPY_DisposeObjectCacherD   r!   r!   r"   ru      s   z_ObjectCacheRef._disposeN)rv   rw   rx   ry   r,   ru   r!   r!   r!   r"   r`      s    r`   c                   @   s$   e Zd ZdejfdefdefgZdS )_ObjectCacheDatarV   rh   ri   N)rv   rw   rx   r   LLVMModuleRefr   r   _fields_r!   r!   r!   r"   r   *  s
    r   r9   )7r   ctypesr   r   r   r   r   r   r   r	   r
   r   r   r   llvmlite.bindingr   r   r   r   LLVMPY_LinkInMCJITr#   r'   r+   r   r`   r   LLVMTargetMachineRefr   argtypesLLVMExecutionEngineRefrestyperK   r?   LLVMValueRefr:   rC   rN   LLVMTargetDataRefr$   r1   r7   LLVMObjectFileRefr[   r   _ObjectCacheNotifyFunc_ObjectCacheGetBufferFuncrl   r|   ro   r}   r{   LLVMObjectCacheRefr   rb   rm   r!   r!   r!   r"   <module>   s    8
 3















