U
    +d+                     @   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   ;/tmp/pip-unpacked-wheel-stw2luzp/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)selfZ
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   MemoryErrorZ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.
        globalr9   )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_GetModuleNamer6   r   r   r   r?   u   s    zModuleRef.namec                 C   s   t j| t| d S r"   )r	   r   LLVMPY_SetModuleNamer   )r&   r/   r   r   r   r?   |   s    c                 C   s   t tj| S )z8
        The module's original source file name
        )r   r	   r   LLVMPY_GetModuleSourceFileNamer6   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ZownedN)r	   r   r   LLVMPY_GetDataLayoutr*   rH   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   rP      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.
        FrN   N)r	   r   r   LLVMPY_GetTargetr*   rH   r   r   r   triple   s    zModuleRef.triplec                 C   s   t j| t|d d S rQ   )r	   r   LLVMPY_SetTargetr   rT   rU   r   r   r   rW      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)
        r9   )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.
        r9   )r	   r   LLVMPY_ModuleFunctionsIter_FunctionsIteratorr=   r_   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.
        r9   )r	   r   LLVMPY_ModuleTypesIter_TypesIteratorr=   r_   r   r   r   struct_types   s    zModuleRef.struct_typesc                 C   s   t tj| | jS r"   )r   r	   r   LLVMPY_CloneModuler%   r6   r   r   r   rY      s    zModuleRef.clone)F)__name__
__module____qualname____doc__r$   r+   r3   r7   rA   rD   rF   rI   propertyr?   setterrM   rP   rW   r\   ra   rd   rg   rY   __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kindr0   )r&   r1   parentsr   r   r   r$      s    z_Iterator.__init__c                 C   s$   |   }|rt|| j| jS td S r"   )_nextr   rs   rr   StopIterationr&   Zvpr   r   r   __next__   s    z_Iterator.__next__c                 C   s   | S r"   r   r6   r   r   r   __iter__   s    z_Iterator.__iter__)ri   rj   rk   rs   r$   rx   nextry   r   r   r   r   rp      s
   rp   c                   @   s    e Zd ZdZdd Zdd ZdS )r^   rB   c                 C   s   | j |  d S r"   )r4   LLVMPY_DisposeGlobalsIterr6   r   r   r   r7      s    z_GlobalsIterator._disposec                 C   s   t j| S r"   )r	   r   LLVMPY_GlobalsIterNextr6   r   r   r   ru      s    z_GlobalsIterator._nextNri   rj   rk   rs   r7   ru   r   r   r   r   r^      s   r^   c                   @   s    e Zd ZdZdd Zdd ZdS )rc   r8   c                 C   s   | j |  d S r"   )r4   LLVMPY_DisposeFunctionsIterr6   r   r   r   r7      s    z_FunctionsIterator._disposec                 C   s   t j| S r"   )r	   r   LLVMPY_FunctionsIterNextr6   r   r   r   ru      s    z_FunctionsIterator._nextNr}   r   r   r   r   rc      s   rc   c                   @   s,   e Zd ZdZdd Zdd Zdd ZeZdS )	rf   typec                 C   s   | j |  d S r"   )r4   LLVMPY_DisposeTypesIterr6   r   r   r   r7     s    z_TypesIterator._disposec                 C   s   |   }|rt|S td S r"   )ru   r   rv   rw   r   r   r   rx     s    z_TypesIterator.__next__c                 C   s   t j| S r"   )r	   r   LLVMPY_TypesIterNextr6   r   r   r   ru     s    z_TypesIterator._nextN)ri   rj   rk   rs   r7   rx   ru   rz   r   r   r   r   rf      s
   rf   )N)N)@ctypesr   r   r   r   r   r   r   Zllvmlite.bindingr	   Zllvmlite.binding.linkerr
   Zllvmlite.binding.commonr   r   Zllvmlite.binding.valuer   r   Zllvmlite.binding.contextr   r   r!   rq   r   rp   r^   rc   rf   ZLLVMContextRefr   r   argtypesZLLVMModuleRefrestyper   r5   r)   r-   r;   ZLLVMValueRefrG   rO   rS   rV   rX   rC   rE   ZLLVMTypeRefr]   ZLLVMGlobalsIteratorr{   r|   rb   ZLLVMFunctionsIteratorre   ZLLVMTypesIteratorr~   r   r   r   rh   rJ   rK   rL   r   r   r   r   <module>   s   $

 %
 






