o
    ߐi+                     @   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   s|   |du rt  }t| } t| }t !}ttj||||}|r,|  t	d
|W d   |S 1 s7w   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   f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/llvmlite/binding/module.pyparse_assembly   s"   

r   c                 C   s~   |du rt  }t| }t| }t "}ttj|||||}|r-|  t	d
|W d   |S 1 s8w   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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 sw   Y  d S r"   )r	   r   r   LLVMPY_PrintModuleToStringstr)r&   Zoutstrr   r   r   __str__8   s   
$zModuleRef.__str__c              	   C   sh   t d}td}tj| t|t| |stz|jdks J t||jW tj	| S tj	| w )zF
        Return the module's LLVM bitcode, as a bytes object.
        Nr   )
r   r   r	   r   LLVMPY_WriteBitcodeToStringr   MemoryErrorvaluer   ZLLVMPY_DisposeString)r&   ptrsizer   r   r   
as_bitcode=   s   zModuleRef.as_bitcodec                 C      | j |  d S r"   )_capiLLVMPY_DisposeModuler&   r   r   r   _disposeL      zModuleRef._disposec                 C   0   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      zModuleRef.get_functionc                 C   r9   )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   rD   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   sH   t  }t j| |rtt|W d   dS 1 sw   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      t tj| S )z*
        The module's identifier.
        )r   r	   r   LLVMPY_GetModuleNamer6   r   r   r   rA   u      zModuleRef.namec                 C   s   t j| t| d S r"   )r	   r   LLVMPY_SetModuleNamer   )r&   r/   r   r   r   rA   |   s   c                 C   rM   )z8
        The module's original source file name
        )r   r	   r   LLVMPY_GetModuleSourceFileNamer6   r   r   r   source_file   rO   zModuleRef.source_filec                 C   F   t jdd}t j| | t|W  d   S 1 sw   Y  dS )zG
        This module's data layout specification, as a string.
        FZownedN)r	   r   r   LLVMPY_GetDataLayoutr*   rK   r   r   r   data_layout      $zModuleRef.data_layoutc                 C      t j| t|d d S Nutf8)r	   r   LLVMPY_SetDataLayoutr   encoder&   Zstrrepr   r   r   rV      
   c                 C   rS   )zK
        This module's target "triple" specification, as a string.
        FrT   N)r	   r   r   LLVMPY_GetTargetr*   rK   r   r   r   triple   rW   zModuleRef.triplec                 C   rX   rY   )r	   r   LLVMPY_SetTargetr   r\   r]   r   r   r   r`      r^   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      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   re   )z
        Return an iterator over this module's functions.
        The iterator will yield a ValueRef for each function.
        r;   )r	   r   LLVMPY_ModuleFunctionsIter_FunctionsIteratorr?   rh   r   r   r   	functions      zModuleRef.functionsc                 C   re   )z|
        Return an iterator over the struct types defined in
        the module. The iterator will yield a TypeRef.
        r;   )r	   r   LLVMPY_ModuleTypesIter_TypesIteratorr?   rh   r   r   r   struct_types   rn   zModuleRef.struct_typesc                 C   s   t tj| | jS r"   )r   r	   r   LLVMPY_CloneModuler%   r6   r   r   r   rb      s   zModuleRef.clone)F)__name__
__module____qualname____doc__r$   r+   r2   r7   rC   rG   rI   rL   propertyrA   setterrR   rV   r`   rd   rj   rm   rq   rb   __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r"   )_nextr   r}   r|   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__)rs   rt   ru   r}   r$   r   nextr   r   r   r   r   rz      s    rz   c                   @       e Zd ZdZdd Zdd ZdS )rg   rE   c                 C   r3   r"   )r4   LLVMPY_DisposeGlobalsIterr6   r   r   r   r7      r8   z_GlobalsIterator._disposec                 C      t j| S r"   )r	   r   LLVMPY_GlobalsIterNextr6   r   r   r   r         z_GlobalsIterator._nextNrs   rt   ru   r}   r7   r   r   r   r   r   rg          rg   c                   @   r   )rl   r:   c                 C   r3   r"   )r4   LLVMPY_DisposeFunctionsIterr6   r   r   r   r7      r8   z_FunctionsIterator._disposec                 C   r   r"   )r	   r   LLVMPY_FunctionsIterNextr6   r   r   r   r      r   z_FunctionsIterator._nextNr   r   r   r   r   rl      r   rl   c                   @   s,   e Zd ZdZdd Zdd Zdd ZeZdS )	rp   typec                 C   r3   r"   )r4   LLVMPY_DisposeTypesIterr6   r   r   r   r7     r8   z_TypesIterator._disposec                 C   s   |   }|r
t|S tr"   )r   r   r   r   r   r   r   r     s   z_TypesIterator.__next__c                 C   r   r"   )r	   r   LLVMPY_TypesIterNextr6   r   r   r   r     r   z_TypesIterator._nextN)rs   rt   ru   r}   r7   r   r   r   r   r   r   r   rp      s    rp   r"   )@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!   r{   r   rz   rg   rl   rp   ZLLVMContextRefr   r   argtypesZLLVMModuleRefrestyper   r5   r)   r-   r=   ZLLVMValueRefrJ   rU   r[   r_   ra   rF   rH   ZLLVMTypeRefrf   ZLLVMGlobalsIteratorr   r   rk   ZLLVMFunctionsIteratorro   ZLLVMTypesIteratorr   r   r   r   rr   rN   rP   rQ   r   r   r   r   <module>   s   $ 

 %







