U
    £½|e¦+  ã                   @   s0  d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ d dlmZ ddd	„Zdd
d„ZG dd„ de	jƒZG dd„ de	jƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZe	jeeeƒ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ƒge	jj$_e	j eeƒeeƒge	jj%_e	j ege	jj&_e	j'e	jj&_!e	j eeƒge	jj(_ee	jj(_!e	j eeƒge	jj)_e	j ege	jj*_e	j eeƒge	jj+_e	j ege	jj,_e	j ege	jj-_e	j'e	jj-_!e	j ege	jj._e	j/e	jj._!e	j ge	jj0_e	j1e	jj0_!e	j1ge	jj2_e	j1ge	jj3_e	j'e	jj3_!e	j ge	jj4_e	j5e	jj4_!e	j ge	jj6_e	j7e	jj6_!e	j5ge	jj8_e	j7ge	jj9_e	j5ge	jj:_e	j'e	jj:_!e	j7ge	jj;_e	j/e	jj;_!e	j ge	jj<_e	j e	jj<_!e	j ge	jj=_ee	jj=_!e	j ege	jj>_e	j ge	jj?_ee	jj?_!dS )é    )Úc_char_pÚbyrefÚPOINTERÚc_boolÚcreate_string_bufferÚc_size_tÚ	string_at)Úffi)Úlink_modules)Ú_decode_stringÚ_encode_string)ÚValueRefÚTypeRef)Úget_global_contextNc              	   C   sf   |dkrt ƒ }t| ƒ} t| ƒ}t ¡ �6}ttj |||¡|ƒ}|rX| ¡  t	d 
|¡ƒ‚W 5 Q R X |S )z-
    Create Module from a LLVM IR string
    NzLLVM IR parsing error
{0})r   r   r   r	   ÚOutputStringÚ	ModuleRefÚlibÚLLVMPY_ParseAssemblyÚcloseÚRuntimeErrorÚformat)ZllvmirÚcontextZstrbufÚerrmsgÚmod© r   úT/var/www/website-v5/atlas_env/lib/python3.8/site-packages/llvmlite/binding/module.pyÚparse_assembly   s    
þr   c              	   C   sh   |dkrt ƒ }t| ƒ}t| ƒ}t ¡ �8}ttj ||||¡|ƒ}|rZ| ¡  t	d 
|¡ƒ‚W 5 Q R X |S )z?
    Create Module from a LLVM *bitcode* (a bytes object).
    NzLLVM bitcode parsing error
{0})r   r   Úlenr	   r   r   r   ÚLLVMPY_ParseBitcoder   r   r   )Zbitcoder   ÚbufÚbufsizer   r   r   r   r   Úparse_bitcode   s&    
   ÿÿÿr!   c                       sè   e Zd ZdZ‡ f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ejdd„ ƒZedd„ ƒZedd„ ƒZejdd„ ƒZedd„ ƒZejdd„ ƒZd(dd„Zed d!„ ƒZed"d#„ ƒZed$d%„ ƒZd&d'„ Z‡  ZS ))r   z'
    A reference to a LLVM module.
    c                    s   t t| ƒ |¡ || _d S ©N)Úsuperr   Ú__init__Ú_context)ÚselfÚ
module_ptrr   ©Ú	__class__r   r   r$   4   s    zModuleRef.__init__c              
   C   s6   t  ¡ �$}t j | |¡ t|ƒW  5 Q R £ S Q R X d S r"   )r	   r   r   ÚLLVMPY_PrintModuleToStringÚstr)r&   Zoutstrr   r   r   Ú__str__8   s    
zModuleRef.__str__c              	   C   sb   t dƒ}tdƒ}tj | t|ƒt|ƒ¡ |s0t‚z|jdks@t	‚t
||jƒW ¢S tj |¡ X dS )zF
        Return the module's LLVM bitcode, as a bytes object.
        Néÿÿÿÿr   )r   r   r	   r   ÚLLVMPY_WriteBitcodeToStringr   ÚMemoryErrorÚLLVMPY_DisposeStringÚvalueÚAssertionErrorr   )r&   ÚptrÚsizer   r   r   Ú
as_bitcode=   s    zModuleRef.as_bitcodec                 C   s   | j  | ¡ d S r"   )Ú_capiÚLLVMPY_DisposeModule©r&   r   r   r   Ú_disposeL   s    zModuleRef._disposec                 C   s0   t j | t|ƒ¡}|st|ƒ‚t|dt| d�ƒS )z~
        Get a ValueRef pointing to the function named *name*.
        NameError is raised if the symbol isn't found.
        Úfunction©Úmodule)r	   r   ÚLLVMPY_GetNamedFunctionr   Ú	NameErrorr   Údict©r&   ÚnameÚpr   r   r   Úget_functionO   s    zModuleRef.get_functionc                 C   s0   t j | t|ƒ¡}|st|ƒ‚t|dt| d�ƒS )z…
        Get a ValueRef pointing to the global variable named *name*.
        NameError is raised if the symbol isn't found.
        Úglobalr;   )r	   r   ÚLLVMPY_GetNamedGlobalVariabler   r>   r   r?   r@   r   r   r   Úget_global_variableY   s    zModuleRef.get_global_variablec                 C   s&   t j | t|ƒ¡}|st|ƒ‚t|ƒS )z†
        Get a TypeRef pointing to a structure type named *name*.
        NameError is raised if the struct type isn't found.
        )r	   r   ÚLLVMPY_GetNamedStructTyper   r>   r   r@   r   r   r   Úget_struct_typec   s    zModuleRef.get_struct_typec              	   C   s2   t  ¡ � }t j | |¡r$tt|ƒƒ‚W 5 Q R X dS )zW
        Verify the module IR's correctness.  RuntimeError is raised on error.
        N)r	   r   r   ÚLLVMPY_VerifyModuler   r+   ©r&   Zoutmsgr   r   r   Úverifym   s    
zModuleRef.verifyc                 C   s   t tj | ¡ƒS )z*
        The module's identifier.
        )r   r	   r   ÚLLVMPY_GetModuleNamer8   r   r   r   rA   u   s    zModuleRef.namec                 C   s   t j | t|ƒ¡ d S r"   )r	   r   ÚLLVMPY_SetModuleNamer   )r&   r1   r   r   r   rA   |   s    c                 C   s   t tj | ¡ƒS )z8
        The module's original source file name
        )r   r	   r   ÚLLVMPY_GetModuleSourceFileNamer8   r   r   r   Úsource_file€   s    zModuleRef.source_filec              
   C   s:   t jdd��$}t j | |¡ t|ƒW  5 Q R £ S Q R X dS )zG
        This module's data layout specification, as a string.
        F©ÚownedN)r	   r   r   ÚLLVMPY_GetDataLayoutr+   rJ   r   r   r   Údata_layout‡   s    zModuleRef.data_layoutc                 C   s   t j | t| d¡ƒ¡ d S ©NÚutf8)r	   r   ÚLLVMPY_SetDataLayoutr   Úencode©r&   Zstrrepr   r   r   rS   ‘   s
    ÿÿc              
   C   s:   t jdd��$}t j | |¡ t|ƒW  5 Q R £ S Q R X dS )zK
        This module's target "triple" specification, as a string.
        FrP   N)r	   r   r   ÚLLVMPY_GetTargetr+   rJ   r   r   r   Útriple—   s    zModuleRef.triplec                 C   s   t j | t| d¡ƒ¡ d S rT   )r	   r   ÚLLVMPY_SetTargetr   rW   rX   r   r   r   rZ   ¡   s
    ÿÿFc                 C   s   |r|  ¡ }t| |ƒ dS )z�
        Link the *other* module into this one.  The *other* module will
        be destroyed unless *preserve* is true.
        N)Úcloner
   )r&   ÚotherÚpreserver   r   r   Úlink_in§   s    zModuleRef.link_inc                 C   s   t j | ¡}t|t| d�ƒS )a)  
        Return an iterator over this module's global variables.
        The iterator will yield a ValueRef for each global variable.

        Note that global variables don't include functions
        (a function is a "global value" but not a "global variable" in
         LLVM parlance)
        r;   )r	   r   ÚLLVMPY_ModuleGlobalsIterÚ_GlobalsIteratorr?   ©r&   Úitr   r   r   Úglobal_variables°   s    
zModuleRef.global_variablesc                 C   s   t j | ¡}t|t| d�ƒS )z€
        Return an iterator over this module's functions.
        The iterator will yield a ValueRef for each function.
        r;   )r	   r   ÚLLVMPY_ModuleFunctionsIterÚ_FunctionsIteratorr?   rb   r   r   r   Ú	functions½   s    zModuleRef.functionsc                 C   s   t j | ¡}t|t| d�ƒS )z|
        Return an iterator over the struct types defined in
        the module. The iterator will yield a TypeRef.
        r;   )r	   r   ÚLLVMPY_ModuleTypesIterÚ_TypesIteratorr?   rb   r   r   r   Ústruct_typesÆ   s    zModuleRef.struct_typesc                 C   s   t tj | ¡| jƒS r"   )r   r	   r   ÚLLVMPY_CloneModuler%   r8   r   r   r   r\   Ï   s    zModuleRef.clone)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r,   r5   r9   rC   rF   rH   rK   ÚpropertyrA   ÚsetterrO   rS   rZ   r_   rd   rg   rj   r\   Ú__classcell__r   r   r(   r   r   /   s>   






	

	

	


r   c                   @   s,   e Zd ZdZdd„ Zdd„ ZeZdd„ ZdS )Ú	_IteratorNc                 C   s&   t j | |¡ || _| jd k	s"t‚d S r"   )r	   Ú	ObjectRefr$   Ú_parentsÚkindr2   )r&   r3   Úparentsr   r   r   r$   ×   s    z_Iterator.__init__c                 C   s$   |   ¡ }|rt|| j| jƒS t‚d S r"   )Ú_nextr   rv   ru   ÚStopIteration©r&   Zvpr   r   r   Ú__next__Ü   s    z_Iterator.__next__c                 C   s   | S r"   r   r8   r   r   r   Ú__iter__å   s    z_Iterator.__iter__)rl   rm   rn   rv   r$   r{   Únextr|   r   r   r   r   rs   Ó   s
   rs   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ra   rD   c                 C   s   | j  | ¡ d S r"   )r6   ÚLLVMPY_DisposeGlobalsIterr8   r   r   r   r9   í   s    z_GlobalsIterator._disposec                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_GlobalsIterNextr8   r   r   r   rx   ð   s    z_GlobalsIterator._nextN©rl   rm   rn   rv   r9   rx   r   r   r   r   ra   é   s   ra   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rf   r:   c                 C   s   | j  | ¡ d S r"   )r6   ÚLLVMPY_DisposeFunctionsIterr8   r   r   r   r9   ø   s    z_FunctionsIterator._disposec                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_FunctionsIterNextr8   r   r   r   rx   û   s    z_FunctionsIterator._nextNr€   r   r   r   r   rf   ô   s   rf   c                   @   s,   e Zd ZdZdd„ Zdd„ Zdd„ ZeZdS )	ri   Útypec                 C   s   | j  | ¡ d S r"   )r6   ÚLLVMPY_DisposeTypesIterr8   r   r   r   r9     s    z_TypesIterator._disposec                 C   s   |   ¡ }|rt|ƒS t‚d S r"   )rx   r   ry   rz   r   r   r   r{     s    z_TypesIterator.__next__c                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_TypesIterNextr8   r   r   r   rx     s    z_TypesIterator._nextN)rl   rm   rn   rv   r9   r{   rx   r}   r   r   r   r   ri   ÿ   s
   ri   )N)N)@Úctypesr   r   r   r   r   r   r   Úllvmlite.bindingr	   Zllvmlite.binding.linkerr
   Úllvmlite.binding.commonr   r   Zllvmlite.binding.valuer   r   Zllvmlite.binding.contextr   r   r!   rt   r   rs   ra   rf   ri   ÚLLVMContextRefr   r   ÚargtypesÚLLVMModuleRefÚrestyper   r7   r*   r.   r=   ÚLLVMValueRefrI   rR   rV   rY   r[   rE   rG   ÚLLVMTypeRefr`   ÚLLVMGlobalsIteratorr~   r   re   ÚLLVMFunctionsIteratorrh   ÚLLVMTypesIteratorr�   r„   r‚   r…   rk   rL   rM   rN   r   r   r   r   Ú<module>   sŒ   $

 %þ
 þ
ÿ
þ
ÿ
ÿ


