
    sKg+                     f   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Zdd	Z G d
 de	j,                        Z G d de	j,                        Z G d de      Z G d de      Z G d de      Ze	j8                  e ee      ge	j:                  j<                  _        e	j@                  e	j:                  j<                  _!        e	j8                  ee ee      ge	j:                  jD                  _        e	j@                  e	j:                  jD                  _!        e	j@                  ge	j:                  jF                  _        e	j@                   ee      ge	j:                  jH                  _        e	j@                   ee       ee      ge	j:                  jJ                  _        e	j@                  ege	j:                  jL                  _        e	jN                  e	j:                  jL                  _!        e	j@                   ee      ge	j:                  jP                  _        ee	j:                  jP                  _!        e	j@                   ee      ge	j:                  jR                  _        e	j@                  ege	j:                  jT                  _        e	j@                   ee      ge	j:                  jV                  _        e	j@                  ege	j:                  jX                  _        e	j@                  ege	j:                  jZ                  _        e	jN                  e	j:                  jZ                  _!        e	j@                  ege	j:                  j\                  _        e	j^                  e	j:                  j\                  _!        e	j@                  ge	j:                  j`                  _        e	jb                  e	j:                  j`                  _!        e	jb                  ge	j:                  jd                  _        e	jb                  ge	j:                  jf                  _        e	jN                  e	j:                  jf                  _!        e	j@                  ge	j:                  jh                  _        e	jj                  e	j:                  jh                  _!        e	j@                  ge	j:                  jl                  _        e	jn                  e	j:                  jl                  _!        e	jj                  ge	j:                  jp                  _        e	jn                  ge	j:                  jr                  _        e	jj                  ge	j:                  jt                  _        e	jN                  e	j:                  jt                  _!        e	jn                  ge	j:                  jv                  _        e	j^                  e	j:                  jv                  _!        e	j@                  ge	j:                  jx                  _        e	j@                  e	j:                  jx                  _!        e	j@                  ge	j:                  jz                  _        ee	j:                  jz                  _!        e	j@                  ege	j:                  j|                  _        e	j@                  ge	j:                  j~                  _        ee	j:                  j~                  _!        y)    )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                 N   |
t               }t        |       } t        |       }t        j                         5 }t        t        j                  j                  |||      |      }|r*|j                          t        dj                  |            	 ddd       |S # 1 sw Y   S xY w)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)llvmircontextstrbuferrmsgmods        [/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/llvmlite/binding/module.pyparse_assemblyr      s     $&F#FfF				vGG((&&A IIK;BB6JKK 	 
 J 
 Js   ABB$c           	      P   |
t               }t        |       }t        |       }t        j                         5 }t        t        j                  j                  ||||      |      }|r*|j                          t        dj                  |            	 ddd       |S # 1 sw Y   S xY w)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   )bitcoder   bufbufsizer   r   s         r   parse_bitcoder&      s     $&
7
C'lG				v33S'6+,35IIK188@B B  
 J 
 Js   ABB%c                   6    e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zed
        Zej                  d        Zed        Zed        Zej                  d        Zed        Zej                  d        ZddZed        Zed        Zed        Zd Z xZS )r   z'
    A reference to a LLVM module.
    c                 :    t         t        |   |       || _        y N)superr   __init___context)self
module_ptrr   	__class__s      r   r+   zModuleRef.__init__4   s    i'
3    c                     t        j                         5 }t         j                  j                  | |       t	        |      cd d d        S # 1 sw Y   y xY wr)   )r
   r   r   LLVMPY_PrintModuleToStringstr)r-   outstrs     r   __str__zModuleRef.__str__8   s7    6GG..tV<v;  s   +A

Ac                 z   t        d      }t        d      }t        j                  j	                  | t        |      t        |             |st        	 |j                  dk\  sJ t        ||j                        t        j                  j                  |       S # t        j                  j                  |       w xY w)zF
        Return the module's LLVM bitcode, as a bytes object.
        Nr   )
r   r   r
   r   LLVMPY_WriteBitcodeToStringr   MemoryErrorvaluer	   LLVMPY_DisposeString)r-   ptrsizes      r   
as_bitcodezModuleRef.as_bitcode=   s     tn|++D%*eDkJ	.::?"?S$**-GG((-CGG((-s   &B !B:c                 :    | j                   j                  |        y r)   )_capiLLVMPY_DisposeModuler-   s    r   _disposezModuleRef._disposeL   s    

''-r0   c                     t         j                  j                  | t        |            }|st	        |      t        |dt        |             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ps      r   get_functionzModuleRef.get_functionO   s@    
 GG++D.2FGD/!:t4'899r0   c                     t         j                  j                  | t        |            }|st	        |      t        |dt        |             S )z
        Get a ValueRef pointing to the global variable named *name*.
        NameError is raised if the symbol isn't found.
        globalrF   )r
   r   LLVMPY_GetNamedGlobalVariabler   rI   r   rJ   rK   s      r   get_global_variablezModuleRef.get_global_variableY   s@    
 GG11$t8LMD/!8T%677r0   c                     t         j                  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   rI   r   rK   s      r   get_struct_typezModuleRef.get_struct_typec   s6    
 GG--dN44HID/!qzr0   c                     t        j                         5 }t         j                  j                  | |      rt	        t        |            	 ddd       y# 1 sw Y   yxY w)zW
        Verify the module IR's correctness.  RuntimeError is raised on error.
        N)r
   r   r   LLVMPY_VerifyModuler   r3   r-   outmsgs     r   verifyzModuleRef.verifym   sF     6ww**48"3v;// 9  s   5AAc                 R    t        t        j                  j                  |             S )z*
        The module's identifier.
        )r   r
   r   LLVMPY_GetModuleNamerB   s    r   rL   zModuleRef.nameu   s    
 cgg::4@AAr0   c                 V    t         j                  j                  | t        |             y r)   )r
   r   LLVMPY_SetModuleNamer   )r-   r:   s     r   rL   zModuleRef.name|   s    $$T>%+@Ar0   c                 R    t        t        j                  j                  |             S )z8
        The module's original source file name
        )r   r
   r   LLVMPY_GetModuleSourceFileNamerB   s    r   source_filezModuleRef.source_file   s    
 cggDDTJKKr0   c                     t        j                  d      5 }t         j                  j                  | |       t	        |      cddd       S # 1 sw Y   yxY w)zG
        This module's data layout specification, as a string.
        FownedN)r
   r   r   LLVMPY_GetDataLayoutr3   rX   s     r   data_layoutzModuleRef.data_layout   s;     E*fGG((v6v; +**   +AAc                 t    t         j                  j                  | t        |j	                  d                   y Nutf8)r
   r   LLVMPY_SetDataLayoutr   encoder-   strreps     r   rf   zModuleRef.data_layout   s,    $$T%9)/v)>&@	Ar0   c                     t        j                  d      5 }t         j                  j                  | |       t	        |      cddd       S # 1 sw Y   yxY w)zK
        This module's target "triple" specification, as a string.
        Frc   N)r
   r   r   LLVMPY_GetTargetr3   rX   s     r   triplezModuleRef.triple   s;     E*fGG$$T62v; +**rg   c                 t    t         j                  j                  | t        |j	                  d                   y ri   )r
   r   LLVMPY_SetTargetr   rl   rm   s     r   rq   zModuleRef.triple   s*      !5%+]]6%:"<	=r0   c                 @    |r|j                         }t        | |       y)z
        Link the *other* module into this one.  The *other* module will
        be destroyed unless *preserve* is true.
        N)cloner   )r-   otherpreserves      r   link_inzModuleRef.link_in   s    
 KKMET5!r0   c                 l    t         j                  j                  |       }t        |t	        |             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)
        rF   )r
   r   LLVMPY_ModuleGlobalsIter_GlobalsIteratorrJ   r-   its     r   global_variableszModuleRef.global_variables   s*     WW--d3D$566r0   c                 l    t         j                  j                  |       }t        |t	        |             S )z
        Return an iterator over this module's functions.
        The iterator will yield a ValueRef for each function.
        rF   )r
   r   LLVMPY_ModuleFunctionsIter_FunctionsIteratorrJ   r|   s     r   	functionszModuleRef.functions   s*     WW//5!"d$&788r0   c                 l    t         j                  j                  |       }t        |t	        |             S )z|
        Return an iterator over the struct types defined in
        the module. The iterator will yield a TypeRef.
        rF   )r
   r   LLVMPY_ModuleTypesIter_TypesIteratorrJ   r|   s     r   struct_typeszModuleRef.struct_types   s*     WW++D1b$d"344r0   c                 h    t        t        j                  j                  |       | j                        S r)   )r   r
   r   LLVMPY_CloneModuler,   rB   s    r   ru   zModuleRef.clone   s"    33D94==IIr0   )F)__name__
__module____qualname____doc__r+   r5   r>   rC   rN   rR   rU   rZ   propertyrL   setterra   rf   rq   rx   r~   r   r   ru   __classcell__)r/   s   @r   r   r   /   s    
..:80 B B 
[[B B L L   A A
   ]]= =
" 
7 
7 9 9 5 5Jr0   r   c                   &    e Zd ZdZd Zd ZeZd Zy)	_IteratorNc                 n    t         j                  j                  | |       || _        | j                  J y r)   )r
   	ObjectRefr+   _parentskind)r-   r<   parentss      r   r+   z_Iterator.__init__   s-    tS)yy$$$r0   c                 t    | j                         }|r!t        || j                  | j                        S t        r)   )_nextr   r   r   StopIterationr-   vps     r   __next__z_Iterator.__next__   s-    ZZ\B		4==99r0   c                     | S r)    rB   s    r   __iter__z_Iterator.__iter__   s    r0   )r   r   r   r   r+   r   nextr   r   r0   r   r   r      s    D%
  Dr0   r   c                       e Zd ZdZd Zd Zy)r{   rP   c                 :    | j                   j                  |        y r)   )r@   LLVMPY_DisposeGlobalsIterrB   s    r   rC   z_GlobalsIterator._dispose   s    

,,T2r0   c                 @    t         j                  j                  |       S r)   )r
   r   LLVMPY_GlobalsIterNextrB   s    r   r   z_GlobalsIterator._next   s    ww--d33r0   Nr   r   r   r   rC   r   r   r0   r   r{   r{      s    D34r0   r{   c                       e Zd ZdZd Zd Zy)r   rE   c                 :    | j                   j                  |        y r)   )r@   LLVMPY_DisposeFunctionsIterrB   s    r   rC   z_FunctionsIterator._dispose   s    

..t4r0   c                 @    t         j                  j                  |       S r)   )r
   r   LLVMPY_FunctionsIterNextrB   s    r   r   z_FunctionsIterator._next   s    ww//55r0   Nr   r   r0   r   r   r      s    D56r0   r   c                   &    e Zd ZdZd Zd Zd ZeZy)r   typec                 :    | j                   j                  |        y r)   )r@   LLVMPY_DisposeTypesIterrB   s    r   rC   z_TypesIterator._dispose  s    

**40r0   c                 H    | j                         }|rt        |      S t        r)   )r   r   r   r   s     r   r   z_TypesIterator.__next__  s     ZZ\2;r0   c                 @    t         j                  j                  |       S r)   )r
   r   LLVMPY_TypesIterNextrB   s    r   r   z_TypesIterator._next  s    ww++D11r0   N)r   r   r   r   rC   r   r   r   r   r0   r   r   r      s    D1 2 Dr0   r   r)   )@ctypesr   r   r   r   r   r   r	   llvmlite.bindingr
   llvmlite.binding.linkerr   llvmlite.binding.commonr   r   llvmlite.binding.valuer   r   llvmlite.binding.contextr   r   r&   r   r   r   r{   r   r   LLVMContextRefr   r   argtypesLLVMModuleRefrestyper"   rA   r2   r8   rH   LLVMValueRefrW   re   rk   rp   rs   rQ   rT   LLVMTypeRefrz   LLVMGlobalsIteratorr   r   r   LLVMFunctionsIteratorr   LLVMTypesIteratorr   r   r   r   r   r\   r^   r`   r   r0   r   <module>r      s  ) ) ) ! 0 B 4 7$$aJ aJH ,4y 46 6Y . *-););)1)0):)<   % (+'8'8   $(+(:(:(0((/(9(;   $ '*&7&7   #),):):(;   %/2/@/@/6x/@/B " " +030A0A070A070A0C # # , -0,=,=,4,6   (*-*:*:   '(+(9(9(/(9(;   $&,   #),):):GH<M(N   %),):):H(E   %%(%6%68I$J   !%(%6%6$A   !252C2CX1N % % .030@0@ % % -.1.?.?-J ! ! *,/OO ! ! )-0->->,?     )+.+B+B     (.1.E.E-F ! ! *+.+B+B*C   '),)9)9   &/2/@/@.A " " +-0-F-F " " *+.+<+<*=   '),)>)>   &030I0I/J # # ,,/,A,A+B   (-0-F-F,G     )+.+;+;     (),)>)>(?   %'*   $'*'8'8&9   #%(%6%6   "),):):(;   %'/   $),):):H(E   %363D3D2E & & /19 & & .r0   