U
    ¤½|eÙ)  ã                   @   s  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„ Zdd„ ZG dd„ dejƒZG d	d
„ d
ejƒZejej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j/ƒZ0e+e,egejj1_ej2ejj1_ej2gejj3_ejej2gejj4_eejj5_eegejj5_dS )é    )Ú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   sF   t  ¡ �&}t j | ||¡}|s*tt|ƒƒ‚W 5 Q R X d|_t|| d�S )zZ
    Create a MCJIT ExecutionEngine from the given *module* and
    *target_machine*.
    T)Úmodule)r   ÚOutputStringÚlibÚLLVMPY_CreateMCJITCompilerÚRuntimeErrorÚstrÚ_ownedÚExecutionEngine)r   Ztarget_machineÚouterrÚengine© r   ú]/var/www/website-v5/atlas_env/lib/python3.8/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   z‚cannot 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   z­An 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&   0   s    zExecutionEngine.__init__c                 C   s   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_address9   s    z$ExecutionEngine.get_function_addressc                 C   s   t j | | d¡¡S )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.
        r)   )r   r   ÚLLVMPY_GetGlobalValueAddressr+   r,   r   r   r   Úget_global_value_addressA   s    z(ExecutionEngine.get_global_value_addressc                 C   s   t j | ||¡ d S ©N)r   r   ÚLLVMPY_AddGlobalMapping)r'   ÚgvÚaddrr   r   r   Úadd_global_mappingI   s    z"ExecutionEngine.add_global_mappingc                 C   s6   || j k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_moduleM   s
    
zExecutionEngine.add_modulec                 C   s   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_objectW   s    zExecutionEngine.finalize_objectc                 C   s   t j | ¡ dS )zW
        Run static constructors which initialize module-level static objects.
        N)r   r   ZLLVMPY_RunStaticConstructorsr;   r   r   r   Úrun_static_constructors^   s    z'ExecutionEngine.run_static_constructorsc                 C   s   t j | ¡ dS )zh
        Run static destructors which perform module-level cleanup of static
        resources.
        N)r   r   ZLLVMPY_RunStaticDestructorsr;   r   r   r   Úrun_static_destructorsd   s    z&ExecutionEngine.run_static_destructorsc              	   C   sF   t  ¡ �"}t j | ||¡r&tt|ƒƒ‚W 5 Q R X | j |¡ d|_dS )z1
        Ownership of module is returned
        FN)	r   r   r   ÚLLVMPY_RemoveModuler   r   r#   Úremover   )r'   r   r   r   r   r   Úremove_modulek   s
    
zExecutionEngine.remove_modulec                 C   s6   | j dk	r| j S tj | ¡}t |¡| _ d| j _| j S )z;
        The TargetData for this execution engine.
        NT)r$   r   r   Ú#LLVMPY_GetExecutionEngineTargetDatar   Z
TargetDatar   )r'   r(   r   r   r   Útarget_datau   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   Z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   ZObjectFileRefZ	from_pathr   r   ÚLLVMPY_MCJITAddObjectFile)r'   Z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'   Znotify_funcZgetbuffer_funcr   r   r   Úset_object_cachež   s    
z ExecutionEngine.set_object_cachec                 C   s`   | j dkrdS |jj}|jj}|jj}t||ƒ}|  |¡}|dkrPtd|f ƒ‚|   ||¡ dS )z(
        Low-level notify hook.
        Nú5object compilation notification for unknown module %s)rM   ÚcontentsrH   Úbuf_ptrÚbuf_lenr
   rI   r   )r'   ÚdatarH   rU   rV   Úbufr   r   r   r   Ú_raw_object_cache_notify«   s    


ÿz(ExecutionEngine._raw_object_cache_notifyc                 C   sr   | j dkrdS |jj}|  |¡}|dkr6td|f ƒ‚|   |¡}|dk	rntj |t|ƒ¡|d _	t|ƒ|d _
dS )z+
        Low-level getbuffer hook.
        NrS   r   )rN   rT   rH   rI   r   r   r   ÚLLVMPY_CreateByteStringÚlenrU   rV   )r'   rW   rH   r   rX   r   r   r   Ú_raw_object_cache_getbuffer½   s    

ÿ
z+ExecutionEngine._raw_object_cache_getbufferc                 C   sH   | j D ]}| ¡  q| jd k	r(| j ¡  | j  ¡  d | _| j | ¡ d S r1   )r#   Údetachr$   ÚclearrP   Ú_capiZLLVMPY_DisposeExecutionEngine)r'   Úmodr   r   r   Ú_disposeÑ   s    




zExecutionEngine._dispose)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rP   r&   r.   r0   r5   r9   r<   r=   r>   rA   ÚpropertyrC   rE   rI   rL   rR   rY   r\   ra   r   r   r   r   r   )   s(   	


	


r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rO   zN
    Internal: an ObjectCache instance for use within an ExecutionEngine.
    c                 C   s"   t j tt|¡}t j | |¡ d S r1   )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 r1   )r_   ÚLLVMPY_DisposeObjectCacher;   r   r   r   ra   ç   s    z_ObjectCacheRef._disposeN)rb   rc   rd   re   r&   ra   r   r   r   r   rO   Ü   s   rO   c                   @   s$   e Zd ZdejfdefdefgZdS )Ú_ObjectCacheDatarH   rU   rV   N)rb   rc   rd   r   ÚLLVMModuleRefr   r   Ú_fields_r   r   r   r   rl   "  s   ýrl   N)6Úctypesr   r   r   r   r   r   r   r	   r
   r   r   r   Úllvmlite.bindingr   r   r   r   ZLLVMPY_LinkInMCJITr   r!   r%   r   rO   rm   ÚLLVMTargetMachineRefr   ÚargtypesÚLLVMExecutionEngineRefÚrestyper?   r7   ÚLLVMValueRefr2   r:   rB   ÚLLVMTargetDataRefr   r*   r/   ÚLLVMObjectFileRefrK   rl   Z_ObjectCacheNotifyFuncZ_ObjectCacheGetBufferFuncrY   rh   r\   ri   rg   ÚLLVMObjectCacheRefrk   rQ   rZ   r   r   r   r   Ú<module>   s‚   8 4ý
ý

þ
þ
ÿ


þ

þ

þ
ÿÿÿÿþ
ÿ

