
    sKg9              
          d dl 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mZ d Z G d de      Zd Zd	 Zd
 ZddddZddZd Z G d de
j.                        Z eg d      Z eg d      Z G d de
j.                        Z G d de
j.                        Zd Z ee      ge
j>                  j@                  _!         ee      ge
j>                  jD                  _!        ee
j>                  jD                  _#         ee      ge
j>                  jH                  _!         ee      ge
j>                  jJ                  _!        ege
j>                  jL                  _!        ee
j>                  jL                  _#        ege
j>                  jN                  _!        e
jP                  e
j>                  jN                  _#        e
jP                   ee      ge
j>                  jR                  _!        e
jP                  ge
j>                  jT                  _!        e
jP                  e
jV                  ge
j>                  jX                  _!        ee
j>                  jX                  _#        e
jP                  e
jV                  ege
j>                  jZ                  _!        ee
j>                  jZ                  _#        e
jP                  e
jV                  ge
j>                  j\                  _!        ee
j>                  j\                  _#        e
jP                  e
jV                  ge
j>                  j^                  _!        ee
j>                  j^                  _#        e ee      ge
j>                  j`                  _!        e
jb                  e
j>                  j`                  _#        e
jb                  ge
j>                  jd                  _!        ee
j>                  jd                  _#        e
jb                  ge
j>                  jf                  _!        ee
j>                  jf                  _#        e
jb                  eeeeeeeeeg
e
j>                  jh                  _!        e
jj                  e
j>                  jh                  _#        e
jj                  ge
j>                  jl                  _!        e
jj                   ee      ge
j>                  jn                  _!        e
jj                  ege
j>                  jp                  _!        e
jj                  e
jr                  ge
j>                  jt                  _!        e
jj                  e
jv                  e ee      ge
j>                  jx                  _!        e
jz                  e
j>                  jx                  _#        e
jz                  ge
j>                  j|                  _!        ee
j>                  j|                  _#        e
jz                  ge
j>                  j~                  _!        ee
j>                  j~                  _#        e
jz                  ge
j>                  j                  _!        e
jj                  ge
j>                  j                  _!        e
jP                  e
j>                  j                  _#        g e
j>                  j                  _!        ee
j>                  j                  _#        y)    N)POINTERc_char_p
c_longlongc_intc_size_tc_void_p	string_at)ffi)_decode_string_encode_stringc                      t        j                         5 } t         j                  j                  |        t	        |       cddd       S # 1 sw Y   yxY w)a  
    Return a target triple suitable for generating code for the current process.
    An example when the default triple from ``get_default_triple()`` is not be
    suitable is when LLVM is compiled for 32-bit but the process is executing
    in 64-bit mode.
    N)r
   OutputStringlibLLVMPY_GetProcessTriplestrouts    \/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/llvmlite/binding/targets.pyget_process_tripler   	   s7     
			s'',3x 
		   *A		Ac                       e Zd ZdZddZy)
FeatureMapz
    Maps feature name to a boolean indicating the availability of the feature.
    Extends ``dict`` to add `.flatten()` method.
    c                     |rt        | j                               nt        | j                               }ddddj                  fd|D              S )ap  
        Args
        ----
        sort: bool
            Optional.  If True, the features are sorted by name; otherwise,
            the ordering is unstable between python session due to hash
            randomization.  Defaults to True.

        Returns a string suitable for use as the ``features`` argument to
        ``Target.create_target_machine()``.

        +-)TF,c              3   N   K   | ]  \  }}d j                  |   |        yw)z{0}{1}N)format).0kvflag_maps      r   	<genexpr>z%FeatureMap.flatten.<locals>.<genexpr>*   s,      .$,DAq !Q7$,s   "%)sorteditemsiterjoin)selfsortiteratorr"   s      @r   flattenzFeatureMap.flatten   sL     ,06$**,'T$**,5Gc*xx .$,. . 	.    N)T)__name__
__module____qualname____doc__r+    r,   r   r   r      s    
.r,   r   c                  4   t        j                         5 } t               }t         j                  j	                  |       s|cddd       S ddd}t        |       }|r'|j                  d      D ]  }|s||d      ||dd <    |cddd       S # 1 sw Y   yxY w)ac  
    Returns a dictionary-like object indicating the CPU features for current
    architecture and whether they are enabled for this CPU.  The key-value pairs
    are the feature name as string and a boolean indicating whether the feature
    is available.  The returned value is an instance of ``FeatureMap`` class,
    which adds a new method ``.flatten()`` for returning a string suitable for
    use as the "features" argument to ``Target.create_target_machine()``.

    If LLVM has not implemented this feature or it fails to get the information,
    this function will raise a RuntimeError exception.
    NTF)r   r   r   r      )r
   r   r   r   LLVMPY_GetHostCPUFeaturesr   split)r   outdictr"   contentfeats        r   get_host_cpu_featuresr9   .   s     
			s,ww005 
	 E*c(c*(0a(9GDH% +  
		s   +B
(B3BBc                      t        j                         5 } t         j                  j                  |        t	        |       cddd       S # 1 sw Y   yxY w)zR
    Return the default target triple LLVM is configured to produce code for.
    N)r
   r   r   LLVMPY_GetDefaultTargetTripler   r   s    r   get_default_tripler<   G   s7     
			s--c23x 
		r   c                      t        j                         5 } t         j                  j                  |        t	        |       cddd       S # 1 sw Y   yxY w)zm
    Get the name of the host's CPU, suitable for using with
    :meth:`Target.create_target_machine()`.
    N)r
   r   r   LLVMPY_GetHostCPUNamer   r   s    r   get_host_cpu_namer?   P   s7    
 
			s%%c*3x 
		r   COFFELFMachO)r3         c                 |    | 
t               } t        j                  j                  t	        |             }t
        |   S )z~
    Get the object format for the given *triple* string (or the default
    triple if omitted).
    A string is returned
    )r<   r
   r   LLVMPY_GetTripleObjectFormatr   _object_formats)tripleress     r   get_object_formatrJ   a   s5     ~#%
''
.
.~f/E
FC3r,   c                 d    t        t        j                  j                  t	        |                   S )zE
    Create a TargetData instance for the given *layout* string.
    )
TargetDatar
   r   LLVMPY_CreateTargetDatar   )layouts    r   create_target_datarO   m   s#     cgg55nV6LMNNr,   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	rL   z{
    A TargetData provides structured access to a data layout.
    Use :func:`create_target_data` to create instances.
    c                     | j                   ryt        j                         5 }t        j                  j	                  | |       t        |      cd d d        S # 1 sw Y   y xY w)Nz<dead TargetData>)_closedr
   r   r    LLVMPY_CopyStringRepOfTargetDatar   r(   r   s     r   __str__zTargetData.__str__z   sA    <<&3GG44T3?s8  s   +AA c                 :    | j                   j                  |        y N)_capiLLVMPY_DisposeTargetDatar(   s    r   _disposezTargetData._dispose   s    

++D1r,   c                 B    t         j                  j                  | |      S )z1
        Get ABI size of LLVM type *ty*.
        )r
   r   LLVMPY_ABISizeOfType)r(   tys     r   get_abi_sizezTargetData.get_abi_size   s     ww++D"55r,   c                     t         j                  j                  | ||      }|dk(  r$t        dj	                  |t        |                  |S )zL
        Get byte offset of type's ty element at the given position
        zQCould not determined offset of {}th element of the type '{}'. Is it a structtype?)r
   r   LLVMPY_OffsetOfElement
ValueErrorr   r   )r(   r^   positionoffsets       r   get_element_offsetzTargetData.get_element_offset   sN    
 //b(CR< %%+VHc"g%>@ @ r,   c                 l    t         j                  j                  | |      }|dk(  rt        d|      |S )zI
        Get ABI size of pointee type of LLVM pointer type *ty*.
        ra   Not a pointer type: )r
   r   LLVMPY_ABISizeOfElementTypeRuntimeErrorr(   r^   sizes      r   get_pointee_abi_sizezTargetData.get_pointee_abi_size   s4     ww224<2:2?@@r,   c                 l    t         j                  j                  | |      }|dk(  rt        d|      |S )zV
        Get minimum ABI alignment of pointee type of LLVM pointer type *ty*.
        ra   rh   )r
   r    LLVMPY_ABIAlignmentOfElementTyperj   rk   s      r   get_pointee_abi_alignmentz$TargetData.get_pointee_abi_alignment   s4     ww77bA2:2?@@r,   N)
r-   r.   r/   r0   rU   r[   r_   rf   rm   rp   r1   r,   r   rL   rL   t   s%    
26
r,   rL   )defaultstaticpicdynamicnopic)rq   
jitdefaultsmallkernelmediumlargec                   t    e Zd ZdZed        Zed        Zed        Zed        Z	ed        Z
d Z	 	 	 d
dZy	)Target c                 8    t               }| j                  |      S )zB
        Create a Target instance for the default triple.
        )r<   from_triple)clsrH   s     r   from_default_triplezTarget.from_default_triple   s    
 $%v&&r,   c                    t        j                         5 }t         j                  j                  |j	                  d      |      }|st        t        |             | |      }||_        |cddd       S # 1 sw Y   yxY w)zK
        Create a Target instance for the given triple (a string).
        utf8N)r
   r   r   LLVMPY_GetTargetFromTripleencoderj   r   _triple)r   rH   outerrtargets       r   r~   zTarget.from_triple   sf    
 6WW77f8M8>@F"3v;//[F#FN  s   AA55A>c                 V    t         j                  j                  |       }t        |      S rW   )r
   r   LLVMPY_GetTargetNamer   r(   ss     r   namezTarget.name   s!    GG((.a  r,   c                 V    t         j                  j                  |       }t        |      S rW   )r
   r   LLVMPY_GetTargetDescriptionr   r   s     r   descriptionzTarget.description   s!    GG//5a  r,   c                     | j                   S rW   )r   rZ   s    r   rH   zTarget.triple   s    ||r,   c                 N    dj                  | j                  | j                        S )Nz<Target {0} ({1})>)r   r   r   rZ   s    r   rU   zTarget.__str__   s    #**499d6F6FGGr,   c	                    d|cxk  rdk  sJ  J |t         v sJ |t        v sJ | j                  }	t        j                  dk(  r
|dk(  r|	dz  }	t
        j                  j                  | t        |	      t        |      t        |      |t        |      t        |      t        |      t        |      t        |      
      }
|
rt        |
      S t        d      )am  
        Create a new TargetMachine for this target and the given options.

        Specifying codemodel='default' will result in the use of the "small"
        code model. Specifying codemodel='jitdefault' will result in the code
        model being picked based on platform bitness (32="small", 64="large").

        The `printmc` option corresponds to llvm's `-print-machineinstrs`.

        The `jit` option should be set when the target-machine is to be used
        in a JIT engine.

        The `abiname` option specifies the ABI. RISC-V targets with hard-float
        needs to pass the ABI name to LLVM.
        r   rD   ntru   z-elfzCannot create target machine)RELOC	CODEMODELr   osr   r
   r   LLVMPY_CreateTargetMachiner   intTargetMachinerj   )r(   cpufeaturesoptreloc	codemodelprintmcjitabinamerH   tms              r   create_target_machinezTarget.create_target_machine   s    $ C}1}}}~~I%%% 77d?yL8fFWW//0>v0F0>s0C0>x0H030>u0E0>y0I03G03C0>w0G
2  $$=>>r,   N)r|   r|   rC   rq   ru   FFr|   )r-   r.   r/   r   classmethodr   r~   propertyr   r   rH   rU   r   r1   r,   r   r{   r{      s    G
 ' '   ! ! ! !  H 68@L@B)?r,   r{   c                   R    e Zd Zd Zd Zd Zd Zd Zd
dZe	d        Z
e	d        Zy	)r   c                 :    | j                   j                  |        y rW   )rX   LLVMPY_DisposeTargetMachinerZ   s    r   r[   zTargetMachine._dispose	  s    

..t4r,   c                 D    t         j                  j                  | |       y)zW
        Register analysis passes for this target machine with a pass manager.
        N)r
   r   LLVMPY_AddAnalysisPasses)r(   pms     r   add_analysis_passesz!TargetMachine.add_analysis_passes  s     	((r2r,   c                 D    t         j                  j                  | |       y)z
        Set whether this target machine will emit assembly with human-readable
        comments describing control flow, debug information, and so on.
        N)r
   r   #LLVMPY_SetTargetMachineAsmVerbosity)r(   verboses     r   set_asm_verbosityzTargetMachine.set_asm_verbosity  s    
 	33D'Br,   c                 (    | j                  |d      S )z
        Represent the module as a code object, suitable for use with
        the platform's linker.  Returns a byte string.
        T
use_object)_emit_to_memoryr(   modules     r   emit_objectzTargetMachine.emit_object  s    
 ##Ft#<<r,   c                 :    t        | j                  |d            S )z
        Return the raw assembler of the module, as a string.

        llvm.initialize_native_asmprinter() must have been called first.
        Fr   )r   r   r   s     r   emit_assemblyzTargetMachine.emit_assembly   s     d226e2LMMr,   c                    t        j                         5 }t         j                  j                  | |t	        |      |      }|st        t        |            	 ddd       t         j                  j                        }t         j                  j                  |      }	 t        ||      t         j                  j                  |       S # 1 sw Y   sxY w# t         j                  j                  |       w xY w)zReturns bytes of object code of the module.

        Args
        ----
        use_object : bool
            Emit object code or (if False) emit assembly code.
        N)r
   r   r    LLVMPY_TargetMachineEmitToMemoryr   rj   r   LLVMPY_GetBufferStartLLVMPY_GetBufferSizer	   LLVMPY_DisposeMemoryBuffer)r(   r   r   r   mbbufptrbufszs          r   r   zTargetMachine._emit_to_memory(  s     699$:=j/:@BB "3v;// 	   ..r2,,R0	3VU+GG..r2   GG..r2s   AC
C 
C!C7c                 R    t        t        j                  j                  |             S rW   )rL   r
   r   LLVMPY_CreateTargetMachineDatarZ   s    r   target_datazTargetMachine.target_data>  s    #''@@FGGr,   c                     t        j                         5 }t         j                  j                  | |       t	        |      cd d d        S # 1 sw Y   y xY wrW   )r
   r   r   LLVMPY_GetTargetMachineTripler   rT   s     r   rH   zTargetMachine.tripleB  s7    3GG11$<s8  s   +A

AN)F)r-   r.   r/   r[   r   r   r   r   r   r   r   rH   r1   r,   r   r   r     sL    53C=N3, H H  r,   r   c                  H    t         j                  j                         dk(  ryy)zG
    Returns True if SVML was enabled at FFI support compile time.
    r   FT)r
   r   LLVMPY_HasSVMLSupportr1   r,   r   has_svmlr   I  s     ww$$&!+r,   rW   )Cr   ctypesr   r   r   r   r   r   r	   llvmlite.bindingr
   llvmlite.binding.commonr   r   r   dictr   r9   r<   r?   rG   rJ   rO   	ObjectRefrL   	frozensetr   r   r{   r   r   r   r   argtypesr4   restyper;   r>   rF   rM   LLVMTargetDataRefrS   rY   LLVMTypeRefr]   rb   ri   ro   r   LLVMTargetRefr   r   r   LLVMTargetMachineRefr   r   r   LLVMPassManagerRefr   LLVMModuleRefr   LLVMMemoryBufferRefr   r   r   r   r   r1   r,   r   <module>r      s2   	) ) ) ! B	. .22 	 O2 2j 	>?   !	V?S]] V?r?CMM ?D -4H,=+>   (.5h.?-@ ! ! *,1 ! ! )29(2C1D % % .*1(*;)<   &19
 $ $ -/4 $ $ ,,4:   (*-*?*?   ' H5 ( ( 1 -     ) *-)>)>),):   %'1   $+.+@+@+.??+0+2   ' *4   &030E0E030A # # ,.8 # # +585J5J58__5F ( ( 13= ( ( 0/79J.K " " +-0->-> " " *),):):(;   %'/   $030A0A/B # # ,.6 # # + 			'/ " " +* .1-E-E " " *030H0H/I # # ,252J2J29(2C2E % % . e8% + + 4 -     ) 	H	5 ( ( 1 473J3J ( ( 0*-*A*A)B   &(0   %),)@)@(A   %'/   $/2/F/F.G " " + 3 & & / 251F1F & & .)+   &(-   %r,   