o
    D2hC                  
   @   s  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 d dlmZmZ d dlmZ d dlmZ edg d	Zd
d ZdefddZG dd deZdd Zdd Zdd Zed  Zedkrqddddddddd d!	Zn
ddddddd d"Zd1d#d$Zd%d& Z G d'd( d(e
j!Z"e#g d)Z$e#g d*Z%G d+d, d,e
j!Z&G d-d. d.e
j!Z'd/d0 Z(eege
j)j*_+eeeeeeeeege
j)j,_+eege
j)j-_+ee
j)j-_.eege
j)j/_+eege
j)j0_+ege
j)j1_+ee
j)j1_.ege
j)j2_+e
j3e
j)j2_.e
j3eege
j)j4_+e
j3ge
j)j5_+e
j3e
j6ge
j)j7_+ee
j)j7_.e
j3e
j6ege
j)j8_+ee
j)j8_.e
j3e
j6ge
j)j9_+ee
j)j9_.e
j3e
j6ge
j)j:_+ee
j)j:_.e
j3e
j6ge
j)j;_+ee
j)j;_.eeege
j)j<_+e
j=e
j)j<_.e
j=ge
j)j>_+ee
j)j>_.e
j=ge
j)j?_+ee
j)j?_.e
j=eeeeeeeeeg
e
j)j@_+e
jAe
j)j@_.e
jAge
j)jB_+e
jAeege
j)jC_+e
jAege
j)jD_+e
jAe
jEge
j)jF_+e
jAe
jGeeege
j)jH_+e
jIe
j)jH_.e
jIge
j)jJ_+ee
j)jJ_.e
jIge
j)jK_+ee
j)jK_.e
jIge
j)jL_+e
jAge
j)jM_+e
j3e
j)jM_.g e
j)jN_+ee
j)jN_.dS )2    N)POINTERc_char_p
c_longlongc_intc_size_tc_void_p	string_at)ffi)llvm_version_info)_decode_string_encode_string)
namedtuple)opaque_pointers_enabledTriple)ArchSubArchVendorOSEnvObjectFormatc                  C   @   t  } t j|  t| W  d   S 1 sw   Y  dS )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out r   l/var/www/html/alexa/alex_system/speach-to-text/venv/lib/python3.10/site-packages/llvmlite/binding/targets.pyget_process_triple   s   
$r   triplec                 C   sV  t  }t  }t  l}t  X}t j| d|||| t|}d}| dD ]}||r=|t|d } nq,t	||t|t|t|t
| W  d   W  d   W  d   W  d   S 1 snw   Y  W d   n1 s}w   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z:
    Return a tuple of the parts of the given triple.
    utf8 -N)r	   r   r   LLVMPY_GetTriplePartsencoder   split
startswithlenr   get_object_format)r    archvendorosenvsubarch_strr   r   r   get_triple_parts   s2   

hP r0   c                   @   s   e Zd ZdZdddZdS )
FeatureMapz
    Maps feature name to a boolean indicating the availability of the feature.
    Extends ``dict`` to add `.flatten()` method.
    Tc                    s>   |rt |  nt|  }ddd d fd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()``.

        +r#   )TF,c                 3   s$    | ]\}}d   | |V  qdS )z{0}{1}N)format).0kvflag_mapr   r   	<genexpr>F   s    z%FeatureMap.flatten.<locals>.<genexpr>)sorteditemsiterjoin)selfsortiteratorr   r8   r   flatten7   s
   
zFeatureMap.flattenN)T)__name__
__module____qualname____doc__rB   r   r   r   r   r1   1   s    r1   c                  C   s   t  =} t }t j| s|W  d   S ddd}t| }|r8|dD ]}|r7||d  ||dd < q'|W  d   S 1 sDw   Y  dS )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)r2   r#   r3   r      )r	   r   r1   r   LLVMPY_GetHostCPUFeaturesr   r&   )r   outdictr9   contentfeatr   r   r   get_host_cpu_featuresJ   s   

$rL   c                  C   r   )zR
    Return the default target triple LLVM is configured to produce code for.
    N)r	   r   r   LLVMPY_GetDefaultTargetTripler   r   r   r   r   get_default_triplec   s   
$rN   c                  C   r   )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   r   r   r   get_host_cpu_namel   s   
$rP      UnknownCOFFDXContainerELFGOFFMachOSPIRVWasmXCOFF)	r   rG                        )r   rG   r[   r\   r]   r^   r_   c                 C   s&   | du rt  } tjt| }t| S )z~
    Get the object format for the given *triple* string (or the default
    triple if omitted).
    A string is returned
    N)rN   r	   r   LLVMPY_GetTripleObjectFormatr   _object_formats)r    resr   r   r   r)      s   r)   c                 C   s   t tjt| S )zE
    Create a TargetData instance for the given *layout* string.
    )
TargetDatar	   r   LLVMPY_CreateTargetDatar   )layoutr   r   r   create_target_data   s   rh   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )re   z{
    A TargetData provides structured access to a data layout.
    Use :func:`create_target_data` to create instances.
    c                 C   sL   | j rdS t }tj| | t|W  d    S 1 sw   Y  d S )Nz<dead TargetData>)_closedr	   r   r    LLVMPY_CopyStringRepOfTargetDatar   r?   r   r   r   r   __str__   s   
$zTargetData.__str__c                 C      | j |  d S N)_capiLLVMPY_DisposeTargetDatar?   r   r   r   _dispose      zTargetData._disposec                 C      t j| |S )z1
        Get ABI size of LLVM type *ty*.
        )r	   r   LLVMPY_ABISizeOfTyper?   tyr   r   r   get_abi_size      zTargetData.get_abi_sizec                 C   s0   t j| ||}|dkrtd|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
ValueErrorr4   r   )r?   rw   positionoffsetr   r   r   get_element_offset   s   zTargetData.get_element_offsetc                 C   rt   )z>
        Get minimum ABI alignment of LLVM type *ty*.
        )r	   r   LLVMPY_ABIAlignmentOfTyperv   r   r   r   get_abi_alignment   ry   zTargetData.get_abi_alignmentc                 C   4   t rtdtj| |}|dkrtd|f |S )zI
        Get ABI size of pointee type of LLVM pointer type *ty*.
        /Cannot get pointee type in opaque pointer mode.rz   Not a pointer type: %s)r   RuntimeErrorr	   r   LLVMPY_ABISizeOfElementTyper?   rw   sizer   r   r   get_pointee_abi_size      zTargetData.get_pointee_abi_sizec                 C   r   )zV
        Get minimum ABI alignment of pointee type of LLVM pointer type *ty*.
        r   rz   r   )r   r   r	   r    LLVMPY_ABIAlignmentOfElementTyper   r   r   r   get_pointee_abi_alignment   r   z$TargetData.get_pointee_abi_alignmentN)rC   rD   rE   rF   rl   rr   rx   r   r   r   r   r   r   r   r   re      s    re   )defaultstaticpicdynamicnopic)r   
jitdefaultsmallkernelmediumlargec                   @   sd   e Zd ZdZedd Zedd Zedd Zedd	 Z	ed
d Z
dd Z			dddZdS )Targetr"   c                 C   s   t  }| |S )zB
        Create a Target instance for the default triple.
        )rN   from_triple)clsr    r   r   r   from_default_triple   s   
zTarget.from_default_triplec                 C   sb   t  #}t j|d|}|stt|| |}||_|W  d   S 1 s*w   Y  dS )zK
        Create a Target instance for the given triple (a string).
        r!   N)r	   r   r   LLVMPY_GetTargetFromTripler%   r   r   _triple)r   r    outerrtargetr   r   r   r      s   
$zTarget.from_triplec                 C      t j| }t|S rn   )r	   r   LLVMPY_GetTargetNamer   r?   sr   r   r   name     zTarget.namec                 C   r   rn   )r	   r   LLVMPY_GetTargetDescriptionr   r   r   r   r   description  r   zTarget.descriptionc                 C   s   | j S rn   )r   rq   r   r   r   r      s   zTarget.triplec                 C   s   d | j| jS )Nz<Target {0} ({1})>)r4   r   r   rq   r   r   r   rl     rs   zTarget.__str__r[   r   r   Fc	                 C   s   d|  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7 }	tj| t|	t|t||t|t|t	|t	|t|
}
|
rOt
|
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   r\   ntr   z-elfzCannot create target machine)RELOC	CODEMODELr   r,   r   r	   r   LLVMPY_CreateTargetMachiner   intTargetMachiner   )r?   cpufeaturesoptreloc	codemodelprintmcjitabinamer    tmr   r   r   create_target_machine  s(   zTarget.create_target_machineN)r"   r"   r[   r   r   FFr"   )rC   rD   rE   r   classmethodr   r   propertyr   r   r    rl   r   r   r   r   r   r      s"    




r   c                   @   sV   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdddZe	dd Z
e	dd ZdS )r   c                 C   rm   rn   )ro   LLVMPY_DisposeTargetMachinerq   r   r   r   rr   F  rs   zTargetMachine._disposec                 C      t j| | dS )zW
        Register analysis passes for this target machine with a pass manager.
        N)r	   r   LLVMPY_AddAnalysisPasses)r?   pmr   r   r   add_analysis_passesI  s   z!TargetMachine.add_analysis_passesc                 C   r   )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?   verboser   r   r   set_asm_verbosityO  s   zTargetMachine.set_asm_verbosityc                 C   s   | j |d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?   moduler   r   r   emit_objectV  s   zTargetMachine.emit_objectc                 C   s   t | j|d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   r   r   r   emit_assembly]  s   zTargetMachine.emit_assemblyFc              	   C   s   t  }t j| |t||}|stt|W d   n1 s"w   Y  t j|}t j|}zt	||W t j
| S t j
| 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   r   r   LLVMPY_GetBufferStartLLVMPY_GetBufferSizer   LLVMPY_DisposeMemoryBuffer)r?   r   r   r   mbbufptrbufszr   r   r   r   e  s   


zTargetMachine._emit_to_memoryc                 C   s   t tj| S rn   )re   r	   r   LLVMPY_CreateTargetMachineDatarq   r   r   r   target_data{  s   zTargetMachine.target_datac                 C   sB   t  }t j| | t|W  d    S 1 sw   Y  d S rn   )r	   r   r   LLVMPY_GetTargetMachineTripler   rk   r   r   r   r      s   
$zTargetMachine.tripleN)F)rC   rD   rE   rr   r   r   r   r   r   r   r   r    r   r   r   r   r   D  s    

r   c                   C   s   t j dkr	dS dS )zG
    Returns True if SVML was enabled at FFI support compile time.
    r   FT)r	   r   LLVMPY_HasSVMLSupportr   r   r   r   has_svml  s   r   rn   )Or,   ctypesr   r   r   r   r   r   r   llvmlite.bindingr	   llvmlite.binding.initfinir
   llvmlite.binding.commonr   r   collectionsr   llvmliter   r   r   r   r0   dictr1   rL   rN   rP   llvm_version_majorrc   r)   rh   	ObjectRefre   	frozensetr   r   r   r   r   r   r   argtypesr$   rH   restyperM   rO   rb   rf   LLVMTargetDataRefrj   rp   LLVMTypeRefru   r{   r   r   r   r   LLVMTargetRefr   r   r   LLVMTargetMachineRefr   r   r   LLVMPassManagerRefr   LLVMModuleRefr   LLVMMemoryBufferRefr   r   r   r   r   r   r   r   r   <module>   s    $	
AYB

























