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.
        Nr   )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   $

 %
 






