U
    ºmœdÒ9  ã                
   @   sX  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d„ ZG dd„ deƒZd	d
„ Zdd„ Zdd„ ZddddœZd(dd„Zdd„ ZG dd„ de
jƒZeddddgƒZeddddd d!gƒZG d"d#„ d#e
jƒZG d$d%„ d%e
jƒZd&d'„ Z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j%_!ege
jj&_!ee
jj&_#ege
jj'_!e
j(e
jj'_#e
j(eeƒge
jj)_!e
j(ge
jj*_!e
j(e
j+ge
jj,_!ee
jj,_#e
j(e
j+ege
jj-_!ee
jj-_#e
j(e
j+ge
jj._!ee
jj._#e
j(e
j+ge
jj/_!ee
jj/_#eeeƒge
jj0_!e
j1e
jj0_#e
j1ge
jj2_!ee
jj2_#e
j1ge
jj3_!ee
jj3_#e
j1eeeeeeeeeg
e
jj4_!e
j5e
jj4_#e
j5ge
jj6_!e
j5eeƒge
jj7_!e
j5ege
jj8_!e
j5e
j9ge
jj:_!e
j5e
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=ge
jj@_!e
j5ge
jjA_!e
j(e
jjA_#g e
jjB_!ee
jjB_#dS ))é    N)ÚPOINTERÚc_char_pÚ
c_longlongÚc_intÚc_size_tÚc_void_pÚ	string_at)Úffi)Ú_decode_stringÚ_encode_stringc               
   C   s4   t  ¡ �"} t j | ¡ t| ƒW  5 Q R £ S Q R X 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   úQ/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/llvmlite/binding/targets.pyÚget_process_triple	   s    
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()``.

        ú+ú-)TFú,c                 3   s"   | ]\}}d   ˆ | |¡V  qdS )z{0}{1}N)Úformat)Ú.0ÚkÚv©Úflag_mapr   r   Ú	<genexpr>*   s   ÿz%FeatureMap.flatten.<locals>.<genexpr>)ÚsortedÚitemsÚiterÚjoin)ÚselfÚsortÚiteratorr   r   r   Úflatten   s
    
ÿzFeatureMap.flattenN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r   r   r   r   r      s   r   c               
   C   sˆ   t  ¡ �v} tƒ }t j | ¡s,|W  5 Q R £ S dddœ}t| ƒ}|rn| d¡D ] }|rL||d  ||dd… < qL|W  5 Q R £ S Q R X 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.
    TF)r   r   r   r   é   N)r	   r   r   r   ÚLLVMPY_GetHostCPUFeaturesr   Úsplit)r   Zoutdictr   ÚcontentZfeatr   r   r   Úget_host_cpu_features.   s    

r0   c               
   C   s4   t  ¡ �"} t j | ¡ t| ƒW  5 Q R £ S Q R X dS )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_tripleG   s    
r2   c               
   C   s4   t  ¡ �"} t j | ¡ t| ƒW  5 Q R £ S Q R X dS )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_nameP   s    
r4   ÚCOFFÚELFZMachO)r,   é   é   c                 C   s&   | dk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)r2   r	   r   ÚLLVMPY_GetTripleObjectFormatr   Ú_object_formats)ÚtripleÚresr   r   r   Úget_object_formata   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_datam   s    r@   c                   @   s@   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S )r>   z{
    A TargetData provides structured access to a data layout.
    Use :func:`create_target_data` to create instances.
    c              
   C   s@   | j r
dS t ¡ �$}tj | |¡ t|ƒW  5 Q R £ S Q R X d S )Nz<dead TargetData>)Ú_closedr	   r   r   Ú LLVMPY_CopyStringRepOfTargetDatar   ©r$   r   r   r   r   Ú__str__z   s
    
zTargetData.__str__c                 C   s   | j  | ¡ d S ©N)Ú_capiÚLLVMPY_DisposeTargetData©r$   r   r   r   Ú_dispose�   s    zTargetData._disposec                 C   s   t j | |¡S )z1
        Get ABI size of LLVM type *ty*.
        )r	   r   ÚLLVMPY_ABISizeOfType)r$   Útyr   r   r   Úget_abi_size„   s    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   )r$   rK   ÚpositionÚoffsetr   r   r   Úget_element_offsetŠ   s     þzTargetData.get_element_offsetc                 C   s(   t j | |¡}|dkr$td|f ƒ‚|S )zI
        Get ABI size of pointee type of LLVM pointer type *ty*.
        rM   úNot a pointer type: %s)r	   r   ÚLLVMPY_ABISizeOfElementTypeÚRuntimeError©r$   rK   Úsizer   r   r   Úget_pointee_abi_size–   s    zTargetData.get_pointee_abi_sizec                 C   s(   t j | |¡}|dkr$td|f ƒ‚|S )zV
        Get minimum ABI alignment of pointee type of LLVM pointer type *ty*.
        rM   rS   )r	   r   Ú LLVMPY_ABIAlignmentOfElementTyperU   rV   r   r   r   Úget_pointee_abi_alignmentŸ   s    z$TargetData.get_pointee_abi_alignmentN)
r(   r)   r*   r+   rD   rI   rL   rR   rX   rZ   r   r   r   r   r>   t   s   	r>   ÚdefaultZstaticZpicZdynamicnopicÚ
jitdefaultZsmallZkernelZmediumZlargec                   @   s^   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 )ÚTargetÚ c                 C   s   t ƒ }|  |¡S )zB
        Create a Target instance for the default triple.
        )r2   Úfrom_triple)Úclsr;   r   r   r   Úfrom_default_triple´   s    zTarget.from_default_triplec              
   C   sV   t  ¡ �D}t j | d¡|¡}|s.tt|ƒƒ‚| |ƒ}||_|W  5 Q R £ S Q R X dS )zK
        Create a Target instance for the given triple (a string).
        Úutf8N)r	   r   r   ÚLLVMPY_GetTargetFromTripleÚencoderU   r   Ú_triple)r`   r;   ÚouterrÚtargetr   r   r   r_   ¼   s    
ÿzTarget.from_triplec                 C   s   t j | ¡}t|ƒS rE   )r	   r   ÚLLVMPY_GetTargetNamer
   ©r$   Úsr   r   r   ÚnameÊ   s    zTarget.namec                 C   s   t j | ¡}t|ƒS rE   )r	   r   ÚLLVMPY_GetTargetDescriptionr
   ri   r   r   r   ÚdescriptionÏ   s    zTarget.descriptionc                 C   s   | j S rE   )re   rH   r   r   r   r;   Ô   s    zTarget.triplec                 C   s   d  | j| j¡S )Nz<Target {0} ({1})>)r   rk   rm   rH   r   r   r   rD   Ø   s    zTarget.__str__r7   r[   r\   Fc	                 C   s¨   d|  krdksn t ‚|tks&t ‚|tks2t ‚| j}	tjdkrR|dkrR|	d7 }	tj | t	|	ƒt	|ƒt	|ƒ|t	|ƒt	|ƒt
|ƒt
|ƒt	|ƒ¡
}
|
rœt|
ƒS tdƒ‚dS )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   r8   Úntr\   z-elfzCannot create target machineN)ÚAssertionErrorÚRELOCÚ	CODEMODELre   Úosrk   r	   r   ÚLLVMPY_CreateTargetMachiner   ÚintÚTargetMachinerU   )r$   ÚcpuÚfeaturesÚoptZrelocZ	codemodelZprintmcZjitZabinamer;   Útmr   r   r   Úcreate_target_machineÛ   s(    ÷zTarget.create_target_machineN)r^   r^   r7   r[   r\   FFr^   )r(   r)   r*   re   Úclassmethodra   r_   Úpropertyrk   rm   r;   rD   rz   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 )ru   c                 C   s   | j  | ¡ d S rE   )rF   ÚLLVMPY_DisposeTargetMachinerH   r   r   r   rI   	  s    zTargetMachine._disposec                 C   s   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_passes  s    z!TargetMachine.add_analysis_passesc                 C   s   t j | |¡ dS )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_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©r$   Úmoduler   r   r   Úemit_object  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   st   t  ¡ �,}t j | |t|ƒ|¡}|s0tt|ƒƒ‚W 5 Q R X t j |¡}t j |¡}zt
||ƒW ¢S t j 	|¡ X dS )z¨Returns 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_TargetMachineEmitToMemoryrt   rU   r   ÚLLVMPY_GetBufferStartÚLLVMPY_GetBufferSizeÚLLVMPY_DisposeMemoryBufferr   )r$   rˆ   r…   rf   ÚmbZbufptrZbufszr   r   r   r†   (  s    

þzTargetMachine._emit_to_memoryc                 C   s   t tj | ¡ƒS rE   )r>   r	   r   ÚLLVMPY_CreateTargetMachineDatarH   r   r   r   Útarget_data>  s    zTargetMachine.target_datac              
   C   s6   t  ¡ �$}t j | |¡ t|ƒW  5 Q R £ S Q R X d S rE   )r	   r   r   ÚLLVMPY_GetTargetMachineTripler   rC   r   r   r   r;   B  s    
zTargetMachine.tripleN)F)r(   r)   r*   rI   r€   rƒ   r‰   rŠ   r†   r|   r‘   r;   r   r   r   r   ru     s   

ru   c                   C   s   t j ¡ dkrdS dS dS )zG
    Returns True if SVML was enabled at FFI support compile time.
    r   FTN)r	   r   ÚLLVMPY_HasSVMLSupportr   r   r   r   Úhas_svmlI  s    r”   )N)Crr   Úctypesr   r   r   r   r   r   r   Zllvmlite.bindingr	   Zllvmlite.binding.commonr
   r   r   Údictr   r0   r2   r4   r:   r=   r@   Z	ObjectRefr>   Ú	frozensetrp   rq   r]   ru   r”   r   r   Zargtypesr-   Úrestyper1   r3   r9   r?   ZLLVMTargetDataRefrB   rG   ZLLVMTypeRefrJ   rN   rT   rY   rc   ZLLVMTargetRefrh   rl   rs   ZLLVMTargetMachineRefr}   r’   r�   ZLLVMPassManagerRefr~   ZLLVMModuleRefr‹   ZLLVMMemoryBufferRefrŒ   r�   rŽ   r�   r“   r   r   r   r   Ú<module>   sÂ   $	ý
5ÿYB

þ
ÿ
ÿ

þ

ÿ

ÿ



í
ÿ
 ÿ
þ
ü


ÿ

