B
    Ꮚ\)                 @   s(  d dl mZmZ 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 j0_"ej*ej j0_$ej#egej j1_"ej2ej j1_$ej#gej j3_"ej4ej j3_$ej4gej j5_"ej4gej j6_"ej*ej j6_$ej#gej j7_"ej8ej j7_$ej#gej j9_"ej:ej j9_$ej8gej j;_"ej:gej j<_"ej8gej j=_"ej*ej j=_$ej:gej j>_"ej2e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 jA_"d	S )    )print_functionabsolute_import)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 d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   6lib/python3.7/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 d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   Z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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%   5   s    zModuleRef.__init__c          	   C   s*   t  }t j| | t|S Q R X d S )N)r   r   r   LLVMPY_PrintModuleToStringstr)r'   Zoutstrr   r   r   __str__9   s    
zModuleRef.__str__c          	   C   s^   t d}td}tj| t|t| |s0tz|jdks@tt	||j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valueAssertionErrorr
   ZLLVMPY_DisposeString)r'   ptrsizer   r   r   
as_bitcode>   s    zModuleRef.as_bitcodec             C   s   | j |  d S )N)_capiLLVMPY_DisposeModule)r'   r   r   r   _disposeM   s    zModuleRef._disposec             C   s*   t j| t|}|st|t|| dS )z~
        Get a ValueRef pointing to the function named *name*.
        NameError is raised if the symbol isn't found.
        )module)r   r   LLVMPY_GetNamedFunctionr   	NameErrorr   )r'   namepr   r   r   get_functionP   s    zModuleRef.get_functionc             C   s*   t j| t|}|st|t|| dS )z
        Get a ValueRef pointing to the global variable named *name*.
        NameError is raised if the symbol isn't found.
        )r7   )r   r   LLVMPY_GetNamedGlobalVariabler   r9   r   )r'   r:   r;   r   r   r   get_global_variableZ   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   r9   r   )r'   r:   r;   r   r   r   get_struct_typed   s    zModuleRef.get_struct_typec          	   C   s2   t   }t j| |r$tt|W d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'   outmsgr   r   r   verifyn   s    
zModuleRef.verifyc             C   s   t tj| S )z*
        The module's identifier.
        )r   r   r   LLVMPY_GetModuleName)r'   r   r   r   r:   v   s    zModuleRef.namec             C   s   t j| t| d S )N)r   r   LLVMPY_SetModuleNamer   )r'   r/   r   r   r   r:   }   s    c          	   C   s.   t jdd}t j| | t|S Q R X dS )zG
        This module's data layout specification, as a string.
        F)ownedN)r   r   r   LLVMPY_GetDataLayoutr*   )r'   rB   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'   strrepr   r   r   rH      s    c          	   C   s.   t jdd}t j| | t|S Q R X dS )zK
        This module's target "triple" specification, as a string.
        F)rF   N)r   r   r   LLVMPY_GetTargetr*   )r'   rB   r   r   r   triple   s    zModuleRef.triplec             C   s   t j| t|d d S )NrI   )r   r   LLVMPY_SetTargetr   rK   )r'   rL   r   r   r   rN      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'   otherZpreserver   r   r   link_in   s    zModuleRef.link_inc             C   s   t j| }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)
        )r7   )r   r   LLVMPY_ModuleGlobalsIter_GlobalsIterator)r'   itr   r   r   global_variables   s    
zModuleRef.global_variablesc             C   s   t j| }t|| dS )z
        Return an iterator over this module's functions.
        The iterator will yield a ValueRef for each function.
        )r7   )r   r   LLVMPY_ModuleFunctionsIter_FunctionsIterator)r'   rU   r   r   r   	functions   s    zModuleRef.functionsc             C   s   t j| }t|| dS )z|
        Return an iterator over the struct types defined in
        the module. The iterator will yield a TypeRef.
        )r7   )r   r   LLVMPY_ModuleTypesIter_TypesIterator)r'   rU   r   r   r   struct_types   s    zModuleRef.struct_typesc             C   s   t tj| | jS )N)r   r   r   LLVMPY_CloneModuler&   )r'   r   r   r   rP      s    zModuleRef.clone)F)__name__
__module____qualname____doc__r%   r+   r3   r6   r<   r>   r@   rC   propertyr:   setterrH   rN   rR   rV   rY   r\   rP   __classcell__r   r   )r(   r   r   0   s(   





			r   c               @   s(   e Zd Zdd Zdd ZeZdd ZdS )	_Iteratorc             C   s   t j| | || _d S )N)r   	ObjectRefr%   _module)r'   r1   r7   r   r   r   r%      s    z_Iterator.__init__c             C   s    |   }|rt|| jS td S )N)_nextr   rg   StopIteration)r'   vpr   r   r   __next__   s    z_Iterator.__next__c             C   s   | S )Nr   )r'   r   r   r   __iter__   s    z_Iterator.__iter__N)r^   r_   r`   r%   rk   nextrl   r   r   r   r   re      s   re   c               @   s   e Zd Zdd Zdd ZdS )rT   c             C   s   | j |  d S )N)r4   LLVMPY_DisposeGlobalsIter)r'   r   r   r   r6      s    z_GlobalsIterator._disposec             C   s   t j| S )N)r   r   LLVMPY_GlobalsIterNext)r'   r   r   r   rh      s    z_GlobalsIterator._nextN)r^   r_   r`   r6   rh   r   r   r   r   rT      s   rT   c               @   s   e Zd Zdd Zdd ZdS )rX   c             C   s   | j |  d S )N)r4   LLVMPY_DisposeFunctionsIter)r'   r   r   r   r6      s    z_FunctionsIterator._disposec             C   s   t j| S )N)r   r   LLVMPY_FunctionsIterNext)r'   r   r   r   rh      s    z_FunctionsIterator._nextN)r^   r_   r`   r6   rh   r   r   r   r   rX      s   rX   c               @   s(   e Zd Zdd Zdd Zdd ZeZdS )r[   c             C   s   | j |  d S )N)r4   LLVMPY_DisposeTypesIter)r'   r   r   r   r6      s    z_TypesIterator._disposec             C   s   |   }|rt|S td S )N)rh   r   ri   )r'   rj   r   r   r   rk      s    z_TypesIterator.__next__c             C   s   t j| S )N)r   r   LLVMPY_TypesIterNext)r'   r   r   r   rh      s    z_TypesIterator._nextN)r^   r_   r`   r6   rk   rh   rm   r   r   r   r   r[      s   r[   )N)N)BZ
__future__r   r   Zctypesr   r   r   r   r   r	   r
    r   Zlinkerr   commonr   r   r/   r   r   r   r   r   r#   rf   r   re   rT   rX   r[   ZLLVMContextRefr   r   ZargtypesZLLVMModuleRefZrestyper!   r5   r)   r-   r8   ZLLVMValueRefrA   rG   rJ   rM   rO   r=   r?   ZLLVMTypeRefrS   ZLLVMGlobalsIteratorrn   ro   rW   ZLLVMFunctionsIteratorrZ   ZLLVMTypesIteratorrp   rr   rq   rs   r]   rD   rE   r   r   r   r   <module>   s|   $

 		

