a
    –§»ah*  ã                   @   s  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>_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   sz   |du rt ƒ }t| ƒ} t| ƒ}t ¡ �@}ttj |||¡|ƒ}|rX| ¡  t	d 
|¡ƒ‚W d  ƒ n1 sl0    Y  |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   úQ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/llvmlite/binding/module.pyÚparse_assembly   s    
þ,r   c                 C   s|   |du rt ƒ }t| ƒ}t| ƒ}t ¡ �B}ttj ||||¡|ƒ}|rZ| ¡  t	d 
|¡ƒ‚W d  ƒ n1 sn0    Y  |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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   sB   t  ¡ �&}t j | |¡ t|ƒW  d   ƒ S 1 s40    Y  d S r"   )r	   r   r   ÚLLVMPY_PrintModuleToStringÚstr)r&   Zoutstrr   r   r   Ú__str__8   s    
zModuleRef.__str__c              	   C   sl   t dƒ}tdƒ}tj | t|ƒt|ƒ¡ |s0t‚z(|jdks@J ‚t||jƒW tj 	|¡ S tj 	|¡ 0 dS )zF
        Return the module's LLVM bitcode, as a bytes object.
        Néÿÿÿÿr   )
r   r   r	   r   ÚLLVMPY_WriteBitcodeToStringr   ÚMemoryErrorÚvaluer   ZLLVMPY_DisposeString)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.
        Úglobalr8   )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   sF   t  ¡ �*}t j | |¡r$tt|ƒƒ‚W d  ƒ n1 s80    Y  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_GetModuleNamer5   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   sF   t jdd��&}t j | |¡ t|ƒW  d  ƒ S 1 s80    Y  dS )zG
        This module's data layout specification, as a string.
        F©ZownedN)r	   r   r   ÚLLVMPY_GetDataLayoutr*   rG   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   rM   Š   s
    ÿÿc                 C   sF   t jdd��&}t j | |¡ t|ƒW  d  ƒ S 1 s80    Y  dS )zK
        This module's target "triple" specification, as a string.
        FrK   N)r	   r   r   ÚLLVMPY_GetTargetr*   rG   r   r   r   Útriple�   s    zModuleRef.triplec                 C   s   t j | t| d¡ƒ¡ d S rN   )r	   r   ÚLLVMPY_SetTargetr   rQ   rR   r   r   r   rT   š   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)
        r8   )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.
        r8   )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.
        r8   )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%   r5   r   r   r   rV   È   s    zModuleRef.clone)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r+   r2   r6   r@   rC   rE   rH   Úpropertyr>   ÚsetterrM   rT   rY   r^   ra   rd   rV   Ú__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 us"J ‚d S r"   )r	   Ú	ObjectRefr$   Ú_parentsÚkind)r&   r0   Úparentsr   r   r   r$   Ð   s    z_Iterator.__init__c                 C   s$   |   ¡ }|rt|| j| jƒS t‚d S r"   )Ú_nextr   rp   ro   ÚStopIteration©r&   Zvpr   r   r   Ú__next__Õ   s    z_Iterator.__next__c                 C   s   | S r"   r   r5   r   r   r   Ú__iter__Þ   s    z_Iterator.__iter__)rf   rg   rh   rp   r$   ru   Únextrv   r   r   r   r   rm   Ì   s
   rm   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r[   rA   c                 C   s   | j  | ¡ d S r"   )r3   ÚLLVMPY_DisposeGlobalsIterr5   r   r   r   r6   æ   s    z_GlobalsIterator._disposec                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_GlobalsIterNextr5   r   r   r   rr   é   s    z_GlobalsIterator._nextN©rf   rg   rh   rp   r6   rr   r   r   r   r   r[   â   s   r[   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r`   r7   c                 C   s   | j  | ¡ d S r"   )r3   ÚLLVMPY_DisposeFunctionsIterr5   r   r   r   r6   ñ   s    z_FunctionsIterator._disposec                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_FunctionsIterNextr5   r   r   r   rr   ô   s    z_FunctionsIterator._nextNrz   r   r   r   r   r`   í   s   r`   c                   @   s,   e Zd ZdZdd„ Zdd„ Zdd„ ZeZdS )	rc   Útypec                 C   s   | j  | ¡ d S r"   )r3   ÚLLVMPY_DisposeTypesIterr5   r   r   r   r6   ü   s    z_TypesIterator._disposec                 C   s   |   ¡ }|rt|ƒS t‚d S r"   )rr   r   rs   rt   r   r   r   ru   ÿ   s    z_TypesIterator.__next__c                 C   s   t j | ¡S r"   )r	   r   ÚLLVMPY_TypesIterNextr5   r   r   r   rr     s    z_TypesIterator._nextN)rf   rg   rh   rp   r6   ru   rr   rw   r   r   r   r   rc   ø   s
   rc   )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!   rn   r   rm   r[   r`   rc   ZLLVMContextRefr   r   ÚargtypesZLLVMModuleRefÚrestyper   r4   r)   r-   r:   ZLLVMValueRefrF   rL   rP   rS   rU   rB   rD   ZLLVMTypeRefrZ   ZLLVMGlobalsIteratorrx   ry   r_   ZLLVMFunctionsIteratorrb   ZLLVMTypesIteratorr{   r~   r|   r   re   rI   rJ   r   r   r   r   Ú<module>   s†   $

 þ
þ
ÿ
þ
ÿ
ÿ


