o
    C2hr#                     @   s4   d dl Z d dlmZmZmZmZ G dd deZdS )    N)contextvaluestypes_utilsc                   @   s   e Zd ZdejfddZdd Zdd Zdd	 Zd+ddZ	d,ddZ
dd Zedd Zedd Zdd Zdd Zd-ddZd.ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdS )/Module c                 C   sB   || _ || _d| _t | _d| _t | _	g | _
i | _i | _d S )Nr   zunknown-unknown-unknown)r   namedata_layoutr   	NameScopescopetriplecollectionsOrderedDictglobalsmetadatanamedmetadata_metadatacache)selfr   r    r   f/var/www/html/alexa/alex_system/speach-to-text/venv/lib/python3.10/site-packages/llvmlite/ir/module.py__init__   s   


zModule.__init__c                 C   sb   g }|D ]*}|d u rt  d }nt|trt| |}nt|ttfr)| |}|	| q|S N)
r   MetaDataType
isinstancestrr   MetaDataStringlisttupleadd_metadataappend)r   operands	fixed_opsopr   r   r   _fix_metadata_operands   s   

zModule._fix_metadata_operandsc                 C   s<   g }|D ]\}}t |ttfr| |}|||f q|S r   )r   r   r   r   r   )r   r    r!   r   r"   r   r   r   _fix_di_operands$   s   
zModule._fix_di_operandsc                 C   sr   t |ttfstd|f | |}t|}|| jvr2t| j}tj	| |t
|d}|| j|< |S | j| }|S )z
        Add an unnamed metadata to the module with the given *operands*
        (a sequence of values) or return a previous equivalent metadata.
        A MDValue instance is returned, it can then be associated to
        e.g. an instruction.
        z3expected a list or tuple of metadata values, got %rr   )r   r   r   	TypeErrorr#   r   lenr   r   MDValuer   )r   r    keynmdr   r   r   r   -   s   




zModule.add_metadataFc                 C   sh   t t| | }|||f}|| jvr-t| j}tj| |||t	|d}|| j|< |S | j| }|S )ak  
        Add debug information metadata to the module with the given
        *operands* (a dict of values with string keys) or return
        a previous equivalent metadata.  *kind* is a string of the
        debug information kind (e.g. "DICompileUnit").

        A DIValue instance is returned, it can then be associated to e.g.
        an instruction.
        r%   )
r   sortedr$   itemsr   r'   r   r   DIValuer   )r   kindr    is_distinctr)   r*   dir   r   r   add_debug_infoA   s   





zModule.add_debug_infoNc                 C   sr   || j v r| j | }n
t|  }| j |< |dur7t|tjs$| |}t|jtjs2t	d|f |
| |S )a  
        Add a named metadata node to the module, if it doesn't exist,
        or return the existing node.
        If *element* is given, it will append a new element to
        the named metadata node.  If *element* is a sequence of values
        (rather than a metadata value), a new unnamed node will first be
        created.

        Example::
            module.add_named_metadata("llvm.ident", ["llvmlite/1.0"])
        Nz'wrong type for metadata element: got %r)r   r   NamedMetaDatar   Valuer   typer   r   r&   add)r   r   elementnmdr   r   r   add_named_metadataU   s   


zModule.add_named_metadatac                 C   
   | j | S )z
        Return the metadata node with the given *name*.  KeyError is raised
        if no such node exists (contrast with add_named_metadata()).
        )r   r   r   r   r   r   get_named_metadatan      
zModule.get_named_metadatac                 C   s   dd | j  D S )zI
        A list of functions declared or defined in this module.
        c                 S   s   g | ]
}t |tjr|qS r   )r   r   Function.0vr   r   r   
<listcomp>z   s    

z$Module.functions.<locals>.<listcomp>r   r   r   r   r   r   	functionsu   s   zModule.functionsc                 C   s
   | j  S )z>
        An iterable of global values in this module.
        rC   rD   r   r   r   global_values}   r=   zModule.global_valuesc                 C   r:   )z-
        Get a global value by name.
        )r   r;   r   r   r   
get_global   s   
zModule.get_globalc                 C   s    |j | jvsJ || j|j < dS )z)
        Add a new global value.
        N)r   r   )r   globalvaluer   r   r   
add_global   s   zModule.add_globalc                 C   s   | j |S )zJ
        Get a unique global name with the following *name* hint.
        )r   deduplicater;   r   r   r   get_unique_name   s   zModule.get_unique_namer   c                    s*   fdd} dv rd j g}ndd D }d g| }|| jv r+| j| S |d ur0ntdkrG dkrGtt td	g}ntd	kr d
kratd d tdg}n dkrptd d }n dkrtd tdg}n dkrttd}n~td }nutdkrՈ dkrd tdd	 td	gtt }nR dv rшd td	gtd }n<|  n8tdkr
 dv rtd	g tt }n dkrd gd td }n|  n|  tj	| ||dS )Nc                      s   t d tf )Nz"unknown intrinsic %r with %d types)NotImplementedErrorr'   r   	intrinsictysr   r   _error   s   
z(Module.declare_intrinsic.<locals>._error>   llvm.fma	llvm.ctlz	llvm.cttzr   c                 S   s   g | ]}|j qS r   )intrinsic_name)r@   tr   r   r   rB      s    z,Module.declare_intrinsic.<locals>.<listcomp>.zllvm.assume   z	llvm.powi    zllvm.pow   zllvm.convert.from.fp16   zllvm.convert.to.fp16zllvm.memset   >   rR   rS      )zllvm.memcpyzllvm.memmoverQ   r%   )
rT   joinr   r'   r   FunctionTypeVoidTypeIntTyper   r>   )r   rN   rO   fntyrP   suffixesr   r   rM   r   declare_intrinsic   sR   

 
zModule.declare_intrinsicc                 C   s   | j jS r   )r   identified_typesrD   r   r   r   get_identified_types   s   zModule.get_identified_typesc                 C   s2   dd |    D }|dd | j D 7 }|S )Nc                 S   s   g | ]}|  qS r   )get_declaration)r@   itr   r   r   rB      s    z*Module._get_body_lines.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r?   r   r   r   rB      s    )re   r   r   r   linesr   r   r   _get_body_lines   s
   
zModule._get_body_linesc              	   C   s\   g }| j  D ]\}}|dj|ddd |jD d q| jD ]	}|t| q"|S )Nz!{name} = !{{ {operands} }}z, c                 s   s    | ]}|  V  qd S r   )get_reference)r@   ir   r   r   	<genexpr>   s    z-Module._get_metadata_lines.<locals>.<genexpr>)r   r    )r   r-   r   formatr]   r    r   r   )r   mdbufkrA   r+   r   r   r   _get_metadata_lines   s   

zModule._get_metadata_linesc                 C      d |  S N
)r]   rj   rD   r   r   r   _stringify_body      zModule._stringify_bodyc                 C   rr   rs   )r]   rq   rD   r   r   r   _stringify_metadata   rv   zModule._stringify_metadatac                 C   sN   g }|d| j f d| jf d| jf dg7 }||  7 }||  7 }d|S )Nz; ModuleID = "%s"ztarget triple = "%s"ztarget datalayout = "%s"r   rt   )r   r   r	   rj   rq   r]   rh   r   r   r   __repr__   s   



zModule.__repr__)Fr   )r   )r   N)__name__
__module____qualname__r   global_contextr   r#   r$   r   r2   r9   r<   propertyrE   rF   rG   rI   rK   rc   re   rj   rq   ru   rw   rx   r   r   r   r   r      s,    	





5
r   )r   llvmlite.irr   r   r   r   objectr   r   r   r   r   <module>   s    