o
    "&i=                  
   @   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dddddddddd 	Zd-d!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(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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
j?e
j)j>_.e
j?ge
j)j@_+e
j?eege
j)jA_+e
j?ege
j)jB_+e
j?e
jCge
j)jD_+e
j?e
jEeeege
j)jF_+e
jGe
j)jF_.e
jGge
j)jH_+ee
j)jH_.e
jGge
j)jI_+ee
j)jI_.e
jGge
j)jJ_+e
j?ge
j)jK_+e
j3e
j)jK_.dS ).    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)	_has_svmlTriple)ZArchZSubArchZVendorZOSZEnvZ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   CC:\wamp64\www\opt\env\Lib\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envZsubarch_strr   r   r   get_triple_parts   s2   

hP r)   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   r1   r   flatten7   s
   
zFeatureMap.flattenN)T)__name__
__module____qualname____doc__r;   r   r   r   r   r*   1   s    r*   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)r+   r   r,   r      )r	   r   r*   r   LLVMPY_GetHostCPUFeaturesr   r    )r   Zoutdictr2   contentZfeatr   r   r   get_host_cpu_featuresJ   s   

$rC   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   
$rE   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   
$rG   UnknownCOFFZDXContainerELFZGOFFZMachOZSPIRVZWasmZXCOFF)	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)rE   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   )Zlayoutr   r   r   create_target_data   s   rW   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )rU   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   r8   r   r   r   r   __str__   s   
$zTargetData.__str__c                 C      | j |  d S N)_capiLLVMPY_DisposeTargetDatar8   r   r   r   _dispose      zTargetData._disposec                 C      t j| |S )z1
        Get ABI size of LLVM type *ty*.
        )r	   r   LLVMPY_ABISizeOfTyper8   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
ValueErrorr-   r   )r8   rf   positionoffsetr   r   r   get_element_offset   s   zTargetData.get_element_offsetc                 C   rc   )z>
        Get minimum ABI alignment of LLVM type *ty*.
        )r	   r   LLVMPY_ABIAlignmentOfTypere   r   r   r   get_abi_alignment   rh   zTargetData.get_abi_alignmentN)	r<   r=   r>   r?   r[   ra   rg   rn   rp   r   r   r   r   rU      s    rU   )defaultZstaticZpicZdynamicnopic)rq   
jitdefaultZsmallZkernelZmediumZ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.
        )rE   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   RuntimeErrorr   _triple)ru   r   outerrtargetr   r   r   rt      s   
$zTarget.from_triplec                 C      t j| }t|S r]   )r	   r   LLVMPY_GetTargetNamer   r8   sr   r   r   name      zTarget.namec                 C   r|   r]   )r	   r   LLVMPY_GetTargetDescriptionr   r~   r   r   r   description   r   zTarget.descriptionc                 C   s   | j S r]   )ry   r`   r   r   r   r      s   zTarget.triplec                 C   s   d | j| jS )Nz<Target {0} ({1})>)r-   r   r   r`   r   r   r   r[      rb   zTarget.__str__rK   rq   rr   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   rL   ntrr   z-elfzCannot create target machine)RELOC	CODEMODELry   r&   r   r	   r   LLVMPY_CreateTargetMachiner   intTargetMachinerx   )r8   cpufeaturesoptZrelocZ	codemodelZprintmcZjitZabinamer   tmr   r   r   create_target_machine   s(   zTarget.create_target_machineN)r   r   rK   rq   rr   FFr   )r<   r=   r>   ry   classmethodrv   rt   propertyr   r   r   r[   r   r   r   r   r   rs      s"    




rs   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   r\   r]   )r^   LLVMPY_DisposeTargetMachiner`   r   r   r   ra   #  rb   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)r8   pmr   r   r   add_analysis_passes&  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)r8   verboser   r   r   set_asm_verbosity,  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r8   moduler   r   r   emit_object3  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   rx   r   LLVMPY_GetBufferStartLLVMPY_GetBufferSizer   LLVMPY_DisposeMemoryBuffer)r8   r   r   rz   mbZbufptrZbufszr   r   r   r   B  s   


zTargetMachine._emit_to_memoryc                 C   s   t tj| S r]   )rU   r	   r   LLVMPY_CreateTargetMachineDatar`   r   r   r   target_dataX  s   zTargetMachine.target_datac                 C   sB   t  }t j| | t|W  d    S 1 sw   Y  d S r]   )r	   r   r   LLVMPY_GetTargetMachineTripler   rZ   r   r   r   r   \  s   
$zTargetMachine.tripleN)F)r<   r=   r>   ra   r   r   r   r   r   r   r   r   r   r   r   r   r   !  s    

r   r]   )Lr&   ctypesr   r   r   r   r   r   r   Zllvmlite.bindingr	   Zllvmlite.binding.initfinir
   Zllvmlite.binding.commonr   r   collectionsr   Zllvmlite.binding.configr   Zhas_svmlr   r   r   r)   dictr*   rC   rE   rG   Zllvm_version_majorrS   r#   rW   Z	ObjectRefrU   	frozensetr   r   rs   r   r   r   argtypesr   rA   restyperD   rF   rR   rV   ZLLVMTargetDataRefrY   r_   ZLLVMTypeRefrd   rj   ro   rw   ZLLVMTargetRefr}   r   r   ZLLVMTargetMachineRefr   r   r   ZLLVMPassManagerRefr   ZLLVMModuleRefr   ZLLVMMemoryBufferRefr   r   r   r   r   r   r   r   <module>   s    $	
)YE










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