U
    +dx                     @   s  d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZ G dd deeZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deeZG dd deeZG dd deeZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZG d%d& d&eZ G d'd( d(eZ!G d)d* d*eZ"G d+d, d,eZ#G d-d. d.eZ$G d/d0 d0eZ%G d1d2 d2eZ&G d3d4 d4eZ'G d5d6 d6eZ(G d7d8 d8eZ)G d9d: d:eZ*G d;d< d<eZ+G d=d> d>eZ,G d?d@ d@eZ-G dAdB dBeZ.G dCdD dDe/Z0G dEdF dFeZ1G dGdH dHeZ2G dIdJ dJe/Z3G dKdL dLe3Z4G dMdN dNe3Z5G dOdP dPeZ6G dQdR dReZ7dSS )Tz)
Implementation of LLVM IR instructions.
    )types)
BlockFunctionValue
NamedValueConstantMetaDataArgumentMetaDataStringAttributeSet	UndefinedArgumentAttributes)_HasMetadatac                       sN   e Zd Zd fdd	Zedd Zedd Zd	d
 Zdd Zdd Z	  Z
S )Instruction  c                    sV   t t| j|||d t|ts$tt|ttfs6t|| _|| _	t|| _
i | _d S Nname)superr   __init__
isinstancer   AssertionErrortuplelistopnameoperandsflagsmetadata)selfparenttypr   r   r   r   	__class__r   </tmp/pip-unpacked-wheel-stw2luzp/llvmlite/ir/instructions.pyr      s    
zInstruction.__init__c                 C   s   | j jS N)r   functionr   r   r   r#   r%      s    zInstruction.functionc                 C   s
   | j jjS r$   )r   r%   moduler&   r   r   r#   r'      s    zInstruction.modulec                 C   s`   | j }| jrd|g| j }ddd | jD }| j}| jdd}|d|||| d S )N , c                 S   s   g | ]}|  qS r   )get_reference.0opr   r   r#   
<listcomp>"   s     z%Instruction.descr.<locals>.<listcomp>TZleading_comma{0} {1} {2}{3}
)r   r   joinr   type_stringify_metadataappendformat)r   bufr   r   r    r   r   r   r#   descr   s       zInstruction.descrc                 C   sF   || j krBg }| j D ]}|||kr(|n| qt|| _ |   d S r$   )r   r4   r   Z_clear_string_cache)r   oldnewopsr-   r   r   r#   replace_usage(   s    


zInstruction.replace_usagec                 C   s   d| j j| j| j| j| jf S )Nz/<ir.%s %r of type '%s', opname %r, operands %r>)r"   __name__r   r2   r   r   r&   r   r   r#   __repr__0   s       zInstruction.__repr__)r   r   )r<   
__module____qualname__r   propertyr%   r'   r7   r;   r=   __classcell__r   r   r!   r#   r      s   	


r   c                   @   s    e Zd ZeddddddgZdS )CallInstrAttributesZnoreturnZnounwindreadonlyZreadnoneZnoinlineZalwaysinlineNr<   r>   r?   	frozensetZ_knownr   r   r   r#   rB   6   s   
 rB   c                	   @   s$   e Zd ZeddddddddgZd	S )
FastMathFlagsfastnnanninfnszarcpcontractafnreassocNrD   r   r   r   r#   rF   ;   s    rF   c                       sh   e Zd Zd fdd	Zedd Zejd	d Zed
d Zdd Zedd Z	dd Z
dd Z  ZS )	CallInstrr   NFr   c
                    s4  |d krt |tr|jn|| _|| _t|| _t|| _i | _	|	r|	
 D ]>\}
}d|
  krjt|k szn td|
t|| j	|
< qJt|}tt|jjD ]d}|| }|jj| }t |tjr|j|krt|}|j|krdd| ||j}t||||< qtt| j||jjd|gt| |d d S )Nr   zInvalid argument index {}z%Type of #{0} arg mismatch: {1} != {2}   callr   )r   r   Zcalling_conventioncconvtailrF   fastmathrB   
attributesarg_attributesitemslen
ValueErrorr5   r   r   rangefunction_typeargsr   ZMetaDataTyper2   r   	TypeErrorr   rO   r   return_type)r   r   funcr\   r   rR   rS   rT   attrs	arg_attrsidxiargZexpected_typemsgr!   r   r#   r   A   sL    


  
  zCallInstr.__init__c                 C   s
   | j d S Nr   r   r&   r   r   r#   calleeb   s    zCallInstr.calleec                 C   s   || j d< d S rf   rg   )r   Z	newcalleer   r   r#   rh   f   s    c                 C   s   | j dd  S NrP   rg   r&   r   r   r#   r\   j   s    zCallInstr.argsc                 C   s    |j | jj krtd|| _d S )Nz"New function has incompatible type)r[   rh   r]   )r   Znewfuncr   r   r#   replace_calleen   s    zCallInstr.replace_calleec                 C   s   | j S )zAlias for llvmpy)rh   r&   r   r   r#   called_functions   s    zCallInstr.called_functionc                    s   fdd d  fddtjD }jj}|jr>|}n|j}d|j }j	rjdj	|}|
djjr|dnd	j|d	 d
d jD |d	 dd jD |rjddnd	d d S )Nc                    s<   |  j kr$d j |   d }nd}d|j|| S )Nr(   r   
{0} {1}{2})rV   r1   Z_to_listr5   r2   r*   )rc   ar`   r&   r   r#   	descr_argy   s    
z#CallInstr._descr.<locals>.descr_argr)   c                    s   g | ]\}} ||qS r   r   )r,   rc   rm   )rn   r   r#   r.      s     z$CallInstr._descr.<locals>.<listcomp>{0} {1}z2{tail}{op}{fastmath} {callee}({args}){attr}{meta}
ztail r   c                 S   s   g | ]}d | qS r(   r   r,   attrr   r   r#   r.      s     c                 S   s   g | ]}d | qS rp   r   rq   r   r   r#   r.      s     Tr/   )rS   r-   rh   rT   r\   rr   meta)r1   	enumerater\   rh   r[   Zvar_argr^   r5   r*   rR   r4   rS   r   rT   rU   r3   )r   r6   add_metadatar\   ZfntytyZ
callee_refr   )rn   r   r#   _descrx   s(    zCallInstr._descrc                 C   s   | j |dd d S )NTru   )rw   r   r6   r   r   r#   r7      s    zCallInstr.descr)r   NFr   r   N)r<   r>   r?   r   r@   rh   setterr\   rj   rk   rw   r7   rA   r   r   r!   r#   rO   @   s          !



rO   c                       s*   e Zd Zd fdd	Z fddZ  ZS )	InvokeInstrr   Nr   c                    sT   t |tstt |tsttt| j|||||d||	|
d	 d| _|| _|| _d S )NF)rS   rT   r`   ra   Zinvoke)	r   r   r   r   r{   r   r   	normal_to	unwind_to)r   r   r_   r\   r|   r}   r   rR   rT   r`   ra   r!   r   r#   r      s      zInvokeInstr.__init__c                    sB   t t| j|dd |dj| j | j | jddd d S )NFrx   z.      to label {0} unwind label {1}{metadata}
Tr/   )r   )	r   r{   rw   r4   r5   r|   r*   r}   r3   ry   r!   r   r#   r7      s    
zInvokeInstr.descr)r   Nr   r   Nr<   r>   r?   r   r7   rA   r   r   r!   r#   r{      s           r{   c                       s$   e Zd Z fddZdd Z  ZS )
Terminatorc                    s   t t| |t || d S r$   )r   r   r   r   VoidType)r   r   r   r   r!   r   r#   r      s    zTerminator.__init__c                 C   s@   | j }ddd | jD }| jdd}|d||| d S )Nr)   c                 S   s   g | ]}d  |j| qS ro   r5   r2   r*   r+   r   r   r#   r.      s   z$Terminator.descr.<locals>.<listcomp>Tr/   rl   )r   r1   r   r3   r4   r5   )r   r6   r   r   r   r   r   r#   r7      s    
zTerminator.descrr~   r   r   r!   r#   r      s   r   c                   @   s   e Zd Zdd ZdS )PredictableInstrc                 C   sZ   t | jdg}|D ]*}|dk r&td|ttd| q| j|}| d| d S )NZbranch_weightsr   z(branch weight must be a positive integer    Zprof)	r	   r'   rY   r4   r   r   IntTyperu   Zset_metadata)r   weightsr   wZmdr   r   r#   set_weights   s    zPredictableInstr.set_weightsN)r<   r>   r?   r   r   r   r   r#   r      s   r   c                       s2   e Zd Zd fdd	Zedd Zdd Z  ZS )	RetNc                    s*   |d k	r|gng }t t| ||| d S r$   )r   r   r   )r   r   r   return_valuer   r!   r   r#   r      s    zRet.__init__c                 C   s   | j r| j d S d S d S rf   rg   r&   r   r   r#   r      s    
zRet.return_valuec                 C   sR   | j }| jdd}|d k	r:|d| j|j| | n|d| j| d S )NTr/   r0   z{0}{1}
)r   r3   r4   r5   r   r2   r*   )r   r6   r   r   r   r   r#   r7      s     z	Ret.descr)N)r<   r>   r?   r   r@   r   r7   rA   r   r   r!   r#   r      s   
r   c                   @   s   e Zd ZdS )BranchNr<   r>   r?   r   r   r   r#   r      s   r   c                   @   s   e Zd ZdS )ConditionalBranchNr   r   r   r   r#   r      s   r   c                       s8   e Zd Z fddZedd Zdd Zdd Z  ZS )	IndirectBranchc                    s    t t| |||g g | _d S r$   )r   r   r   destinations)r   r   r   addrr!   r   r#   r      s    zIndirectBranch.__init__c                 C   s
   | j d S rf   rg   r&   r   r   r#   address   s    zIndirectBranch.addressc                 C   s   t |tst| j| d S r$   )r   r   r   r   r4   )r   blockr   r   r#   add_destination   s    zIndirectBranch.add_destinationc              
   C   sB   dd | j D }|d| jj| j d|| jdd d S )Nc                 S   s   g | ]}d  | qS )z	label {0}r5   r*   )r,   blkr   r   r#   r.      s   z(IndirectBranch.descr.<locals>.<listcomp>zindirectbr {0} {1}, [{2}]  {3}
r)   Tr/   )r   r4   r5   r   r2   r*   r1   r3   )r   r6   r   r   r   r#   r7      s    
zIndirectBranch.descr)	r<   r>   r?   r   r@   r   r   r7   rA   r   r   r!   r#   r      s
   
r   c                       s8   e Zd Z fddZedd Zdd Zdd Z  ZS )	SwitchInstrc                    s&   t t| |||g || _g | _d S r$   )r   r   r   defaultcases)r   r   r   valr   r!   r   r#   r     s    zSwitchInstr.__init__c                 C   s
   | j d S rf   rg   r&   r   r   r#   value  s    zSwitchInstr.valuec                 C   s:   t |tstt |ts&t| jj|}| j||f d S r$   )	r   r   r   r   r   r   r2   r   r4   )r   r   r   r   r   r#   add_case  s    
zSwitchInstr.add_casec                 C   sJ   dd | j D }|d| jj| j | j d|| jdd d S )Nc                 S   s(   g | ] \}}d  |j| | qS )z{0} {1}, label {2}r   r,   r   r   r   r   r#   r.     s   z%SwitchInstr.descr.<locals>.<listcomp>z%switch {0} {1}, label {2} [{3}]  {4}
r(   Tr/   )	r   r4   r5   r   r2   r*   r   r1   r3   )r   r6   r   r   r   r#   r7     s    
zSwitchInstr.descr)	r<   r>   r?   r   r@   r   r   r7   rA   r   r   r!   r#   r     s
   
r   c                   @   s   e Zd ZdS )ResumeNr   r   r   r   r#   r     s   r   c                       sJ   e Zd Zd fdd	Zedd Zedd Zed	d
 Zdd Z  Z	S )SelectInstrr   r   c                    s8   |j |j ksttt| j||j d|||g||d d S )Nselectr   r   )r2   r   r   r   r   )r   r   condlhsrhsr   r   r!   r   r#   r   $  s     zSelectInstr.__init__c                 C   s
   | j d S rf   rg   r&   r   r   r#   r   *  s    zSelectInstr.condc                 C   s
   | j d S ri   rg   r&   r   r   r#   r   .  s    zSelectInstr.lhsc                 C   s
   | j d S )N   rg   r&   r   r   r#   r   2  s    zSelectInstr.rhsc                 C   sP   | dd| j| jj| j | jj| j | jj| j | j	dd d S )Nz)select {0} {1} {2}, {3} {4}, {5} {6} {7}
r(   Tr/   )
r4   r5   r1   r   r   r2   r*   r   r   r3   ry   r   r   r#   r7   6  s    
   
zSelectInstr.descr)r   r   )
r<   r>   r?   r   r@   r   r   r   r7   rA   r   r   r!   r#   r   #  s   


r   c                       s2   e Zd ZdZi Zdg f fdd	Zdd Z  ZS )CompareInstrzinvalid-comparer   c           
         s   || j krtd|| jf |D ] }|| jkr td|| jf q | j}t|jtjrnttd|jj	}	n
td}	t
t| j||	|||g||d || _d S )Nzinvalid comparison %r for %szinvalid flag %r for %srP   )r   r   )VALID_OPrY   OPNAME
VALID_FLAGr   r2   r   
VectorTyper   countr   r   r   r-   )
r   r   r-   r   r   r   r   flagr   r    r!   r   r#   r   E  s     


  zCompareInstr.__init__c                 C   s\   | dj| jddd | jD | j| jd j| jd  | jd  | j	ddd	 d S )
Nz.{opname}{flags} {op} {ty} {lhs}, {rhs} {meta}
r   c                 s   s   | ]}d | V  qdS )r(   Nr   )r,   itr   r   r#   	<genexpr>X  s     z%CompareInstr.descr.<locals>.<genexpr>r   rP   Tr/   )r   r   r-   rv   r   r   rs   )
r4   r5   r   r1   r   r-   r   r2   r*   r3   ry   r   r   r#   r7   U  s    

zCompareInstr.descr)r<   r>   r?   r   r   r   r7   rA   r   r   r!   r#   r   @  s   r   c                   @   s0   e Zd ZdZdddddddd	d
dd
Ze ZdS )	ICMPInstrZicmpequalz	not equalzunsigned greater thanzunsigned greater or equalzunsigned less thanzunsigned less or equalzsigned greater thanzsigned greater or equalzsigned less thanzsigned less or equal)
eqneugtugeultuleZsgtZsgeZsltZsleN)r<   r>   r?   r   r   setr   r   r   r   r#   r   a  s   r   c                   @   sJ   e Zd ZdZdddddddd	d
ddddddddZddddddddhZdS )	FCMPInstrZfcmpz#no comparison, always returns falsezordered and equalzordered and greater thanz!ordered and greater than or equalzordered and less thanzordered and less than or equalzordered and not equalzordered (no nans)zunordered or equalzunordered or greater thanz"unordered or greater than or equalzunordered or less thanzunordered or less than or equalzunordered or not equalzunordered (either nans)z"no comparison, always returns true)falseZoeqZogtZogeZoltZoleZoneordZueqr   r   r   r   ZuneZunotruerH   rI   rJ   rK   rL   rM   rN   rG   N)r<   r>   r?   r   r   r   r   r   r   r#   r   r  s*   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )	CastInstrr   c                    s    t t| j||||g|d d S r   )r   r   r   )r   r   r-   r   r    r   r!   r   r#   r     s    zCastInstr.__init__c                 C   s:   | d| j| jd j| jd  | j| jdd d S )Nz{0} {1} {2} to {3} {4}
r   Tr/   )r4   r5   r   r   r2   r*   r3   ry   r   r   r#   r7     s    

zCastInstr.descr)r   r~   r   r   r!   r#   r     s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )	LoadInstrr   c                    s*   t t| j||jjd|g|d d | _d S )Nloadr   )r   r   r   r2   pointeealign)r   r   ptrr   r!   r   r#   r     s
     zLoadInstr.__init__c                 C   sP   | j \}| jd k	rd| j }nd}|d|jj|j| || jdd d S )N
, align %dr   zload {0}, {1} {2}{3}{4}
Tr/   )r   r   r4   r5   r2   r   r*   r3   )r   r6   r   r   r   r   r#   r7     s    

zLoadInstr.descr)r   r~   r   r   r!   r#   r     s   r   c                       s$   e Zd Z fddZdd Z  ZS )
StoreInstrc                    s"   t t| |t d||g d S )Nstore)r   r   r   r   r   )r   r   r   r   r!   r   r#   r     s    zStoreInstr.__init__c                 C   sV   | j \}}| jd k	r d| j }nd}|d|j| |j| || jdd d S )Nr   r   zstore {0} {1}, {2} {3}{4}{5}
Tr/   )r   r   r4   r5   r2   r*   r3   )r   r6   r   r   r   r   r   r#   r7     s    


zStoreInstr.descrr~   r   r   r!   r#   r     s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )LoadAtomicInstrr   c                    s0   t t| j||jjd|g|d || _|| _d S )Nzload atomicr   )r   r   r   r2   r   orderingr   )r   r   r   r   r   r   r!   r   r#   r     s      zLoadAtomicInstr.__init__c                 C   s<   | j \}|d|jj|j| | j| j| jdd d S )Nz+load atomic {0}, {1} {2} {3}, align {4}{5}
Tr/   )	r   r4   r5   r2   r   r*   r   r   r3   )r   r6   r   r   r   r#   r7     s    
zLoadAtomicInstr.descr)r   r~   r   r   r!   r#   r     s   r   c                       s$   e Zd Z fddZdd Z  ZS )StoreAtomicInstrc                    s.   t t| |t d||g || _|| _d S )Nzstore atomic)r   r   r   r   r   r   r   )r   r   r   r   r   r   r!   r   r#   r     s     zStoreAtomicInstr.__init__c                 C   sB   | j \}}|d|j| |j| | j| j| jdd d S )Nz0store atomic {0} {1}, {2} {3} {4}, align {5}{6}
Tr/   )r   r4   r5   r2   r*   r   r   r3   )r   r6   r   r   r   r   r#   r7     s    

zStoreAtomicInstr.descrr~   r   r   r!   r#   r     s   r   c                       s$   e Zd Z fddZdd Z  ZS )AllocaInstrc                    s4   |r
|gnd}t t| || d|| d | _d S )Nr   Zalloca)r   r   r   
as_pointerr   )r   r   r    r   r   r   r!   r   r#   r     s     zAllocaInstr.__init__c                 C   sv   | d| j| jj | jr>| j\}| d|j|  | jd k	rZ| d| j | jrr| | j	dd d S )Nro   z	, {0} {1}z, align {0}Tr/   )
r4   r5   r   r2   r   r   r*   r   r   r3   )r   r6   r-   r   r   r#   r7     s    
zAllocaInstr.descrr~   r   r   r!   r#   r     s   r   c                       s$   e Zd Z fddZdd Z  ZS )GEPInstrc           
         s   |j }d }d}|D ]&}	|||	 }}t|tjr|j}qt|tjsXt|tjrX|}n
||}tt| j	||d|gt
| |d || _|| _|| _d S )Nr   getelementptrr   )r2   Zgepr   r   ZPointerTypeZ	addrspacer   r   r   r   r   pointerindicesinbounds)
r   r   r   r   r   r   r    ZlasttypZlastaddrspacerc   r!   r   r#   r     s&    

 zGEPInstr.__init__c                 C   sZ   dd | j D }| jrdnd}|d|| jjj| jj| j d|| j	dd d S )	Nc                 S   s   g | ]}d  |j| qS r   r   r,   rc   r   r   r#   r.     s   z"GEPInstr.descr.<locals>.<listcomp>zgetelementptr inboundsr   z{0} {1}, {2} {3}, {4} {5}
r)   Tr/   )
r   r   r4   r5   r   r2   r   r*   r1   r3   )r   r6   r   r-   r   r   r#   r7     s    
zGEPInstr.descrr~   r   r   r!   r#   r     s   r   c                       s6   e Zd Zd
 fdd	Zdd Zdd Zdd	 Z  ZS )PhiInstrr   c                    s&   t t| j||dd||d g | _d S )Nphir   r   )r   r   r   	incomings)r   r   r    r   r   r!   r   r#   r   "  s    zPhiInstr.__init__c              
   C   sB   d dd | jD }|dd | j| j|| jdd d S )Nr)   c                 s   s&   | ]\}}d  | | V  qdS )z
[{0}, {1}]Nr   )r,   vbr   r   r#   r   (  s   
z!PhiInstr.descr.<locals>.<genexpr>zphi {0} {1} {2} {3}
r(   Tr/   )r1   r   r4   r5   r   r2   r3   )r   r6   Zincsr   r   r#   r7   '  s    


zPhiInstr.descrc                 C   s"   t |tst| j||f d S r$   )r   r   r   r   r4   )r   r   r   r   r   r#   add_incoming2  s    zPhiInstr.add_incomingc                    s    fdd| j D | _ d S )Nc                    s$   g | ]\}}|kr n||fqS r   r   r   r9   r8   r   r#   r.   7  s   z*PhiInstr.replace_usage.<locals>.<listcomp>)r   )r   r8   r9   r   r   r#   r;   6  s    zPhiInstr.replace_usage)r   )r<   r>   r?   r   r7   r   r;   rA   r   r   r!   r#   r   !  s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )ExtractElementr   c                    sV   t |jtjstdt |jtjs,td|jj}tt| j	||d||g|d d S )N!vector needs to be of VectorType.index needs to be of IntType.Zextractelementr   )
r   r2   r   r   r]   r   elementr   r   r   )r   r   vectorindexr   r    r!   r   r#   r   <  s     zExtractElement.__init__c                 C   s0   d dd | jD }|dj| j|d d S )Nr)   c                 s   s    | ]}d  |j| V  qdS ro   Nr   r+   r   r   r#   r   F  s
    z'ExtractElement.descr.<locals>.<genexpr>{opname} {operands}
r   r   r1   r   r4   r5   r   r   r6   r   r   r   r#   r7   E  s    
 zExtractElement.descr)r   r~   r   r   r!   r#   r   ;  s   	r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )InsertElementr   c                    sz   t |jtjstd|j|jjks:td|jj|jt |jtjsPtd|j}tt	| j
||d|||g|d d S )Nr   z$value needs to be of type {} not {}.r   Zinsertelementr   )r   r2   r   r   r]   r   r5   r   r   r   r   )r   r   r   r   r   r   r    r!   r   r#   r   M  s       zInsertElement.__init__c                 C   s0   d dd | jD }|dj| j|d d S )Nr)   c                 s   s    | ]}d  |j| V  qdS r   r   r+   r   r   r#   r   [  s
    z&InsertElement.descr.<locals>.<genexpr>r   r   r   r   r   r   r#   r7   Z  s    
 zInsertElement.descr)r   r~   r   r   r!   r#   r   L  s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )ShuffleVectorr   c                    s   t |jtjstd|tkr2|j|jkr2tdt |trht |jtjrht |jjtjrh|jjj	dksptdt|jj|jj
}t|tkr|jj
n
d|jj
  t fdd|jD std tt| j||d	|||g|d
 d S )Nz"vector1 needs to be of VectorType.z=vector2 needs to be Undefined or of the same type as vector1.r   z'mask needs to be a constant i32 vector.r   c                 3   s   | ]}|j  kV  qd S r$   )constant)r,   iiZindex_ranger   r#   r   r  s     z)ShuffleVector.__init__.<locals>.<genexpr>zmask values need to be in {0}Zshufflevectorr   )r   r2   r   r   r]   r   r   r   r   widthr   rZ   allr   
IndexErrorr5   r   r   r   )r   r   Zvector1Zvector2maskr   r    r!   r   r#   r   b  s6    


 zShuffleVector.__init__c                 C   s0   | dddd | jD | jdd d S )Nzshufflevector {0} {1}
r)   c                 s   s    | ]}d  |j| V  qdS r   r   r+   r   r   r#   r   {  s   z&ShuffleVector.descr.<locals>.<genexpr>Tr/   )r4   r5   r1   r   r3   ry   r   r   r#   r7   y  s    

zShuffleVector.descr)r   r~   r   r   r!   r#   r   a  s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )ExtractValuer   c              	      sz   |j }z|D ]}|j| }qW n. ttfk
rL   tdt||j f Y nX tt| j||d|g|d || _	|| _
d S )NCan't index at %r in %sZextractvaluer   )r2   elementsAttributeErrorr   r]   r   r   r   r   	aggregater   )r   r   aggr   r   r    rc   r!   r   r#   r     s     zExtractValue.__init__c              
   C   sB   dd | j D }|d| jj| j d|| jdd d S )Nc                 S   s   g | ]}t |qS r   strr   r   r   r#   r.     s     z&ExtractValue.descr.<locals>.<listcomp>zextractvalue {0} {1}, {2} {3}
r)   Tr/   )r   r4   r5   r   r2   r*   r1   r3   r   r6   r   r   r   r#   r7     s    
zExtractValue.descr)r   r~   r   r   r!   r#   r     s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )InsertValuer   c              	      s   |j }z|D ]}|j| }qW n. ttfk
rL   tdt||j f Y nX |j |krttd|t||j |j f tt| j||j d||g|d || _	|| _
|| _d S )Nr   z&Can only insert %s at %r in %s: got %sZinsertvaluer   )r2   r   r   r   r]   r   r   r   r   r   r   r   )r   r   r   elemr   r   r    rc   r!   r   r#   r     s&    
 zInsertValue.__init__c                 C   sP   dd | j D }|d| jj| j | jj| j d|| jdd d S )Nc                 S   s   g | ]}t |qS r   r   r   r   r   r#   r.     s     z%InsertValue.descr.<locals>.<listcomp>z&insertvalue {0} {1}, {2} {3}, {4} {5}
r)   Tr/   )	r   r4   r5   r   r2   r*   r   r1   r3   r   r   r   r#   r7     s      
zInsertValue.descr)r   r~   r   r   r!   r#   r     s   r   c                       s$   e Zd Z fddZdd Z  ZS )Unreachablec                    s"   t t| j|t dddd d S )NZunreachabler   r   r   )r   r   r   r   r   )r   r   r!   r   r#   r     s
      zUnreachable.__init__c                 C   s   || j df7 }d S )N
)r   ry   r   r   r#   r7     s    zUnreachable.descrr~   r   r   r!   r#   r     s   r   c                   @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )	InlineAsmFc                 C   s$   |j | _|| _|| _|| _|| _d S r$   )r^   r2   r[   asm
constraintside_effect)r   Zftyper   r   r   r   r   r#   r     s
    zInlineAsm.__init__c                 C   s0   | j r
dnd}d}||j|| j| jd d S )N
sideeffectr   z)asm {sideeffect} "{asm}", "{constraint}"
)r   r   r   )r   r4   r5   r   r   )r   r6   r   fmtr   r   r#   r7     s
    zInlineAsm.descrc                 C   s   g }|  | d|S )Nr   )r7   r1   ry   r   r   r#   r*     s    
zInlineAsm.get_referencec                 C   s   d | j|  S )Nro   r   r&   r   r   r#   __str__  s    zInlineAsm.__str__N)F)r<   r>   r?   r   r7   r*   r   r   r   r   r#   r     s   
r   c                       s$   e Zd Z fddZdd Z  ZS )	AtomicRMWc                    s0   t t| j||jd||f|d || _|| _d S )NZ	atomicrmwr   )r   r   r   r2   	operationr   )r   r   r-   r   r   r   r   r!   r   r#   r     s     zAtomicRMW.__init__c                 C   sH   | j \}}d}||j| j|j| |j| | j| jddd d S )NzBatomicrmw {op} {ptrty} {ptr}, {valty} {val} {ordering} {metadata}
Tr/   )r-   ptrtyr   Zvaltyr   r   r   )r   r4   r5   r   r2   r*   r   r3   )r   r6   r   r   r   r   r   r#   r7     s    
zAtomicRMW.descrr~   r   r   r!   r#   r     s   r   c                       s(   e Zd ZdZ fddZdd Z  ZS )CmpXchgzdThis instruction has changed since llvm3.5.  It is not compatible with
    older llvm versions.
    c           	         sF   t |jt dg}tt| j||d|||f|d || _|| _d S )NrP   Zcmpxchgr   )	r   ZLiteralStructTyper2   r   r   r  r   r   failordering)	r   r   r   cmpr   r   r  r   Zouttyper!   r   r#   r     s     zCmpXchg.__init__c                 C   sP   | j \}}}d}||j|j| |j| | | j| j| jddd d S )NzScmpxchg {ptrty} {ptr}, {ty} {cmp}, {ty} {val} {ordering} {failordering} {metadata}
Tr/   )r   r   rv   r  r   r   r  r   )r   r4   r5   r2   r*   r   r  r3   )r   r6   r   Zcmpvalr   r   r   r   r#   r7     s    zCmpXchg.descr)r<   r>   r?   __doc__r   r7   rA   r   r   r!   r#   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )_LandingPadClausec                 C   s
   || _ d S r$   )r   r   r   r   r   r#   r     s    z_LandingPadClause.__init__c                 C   s   dj | j| jj| j dS )Nz{kind} {type} {value})kindr2   r   )r5   r  r   r2   r*   r&   r   r   r#   r     s
    z_LandingPadClause.__str__N)r<   r>   r?   r   r   r   r   r   r#   r    s   r  c                   @   s   e Zd ZdZdS )CatchClauseZcatchN)r<   r>   r?   r  r   r   r   r#   r    s   r  c                       s    e Zd ZdZ fddZ  ZS )FilterClausefilterc                    s4   t |tstt |jtjs ttt| | d S r$   )	r   r   r   r2   r   Z	ArrayTyper   r	  r   r  r!   r   r#   r     s    zFilterClause.__init__)r<   r>   r?   r  r   rA   r   r   r!   r#   r	    s   r	  c                       s.   e Zd Zd	 fdd	Zdd Zdd Z  ZS )
LandingPadInstrr   Fc                    s*   t t| j||dg |d || _g | _d S )NZ
landingpadr   )r   r  r   cleanupclauses)r   r   r    r   r  r!   r   r#   r   %  s
    zLandingPadInstr.__init__c                 C   s   t |tst| j| d S r$   )r   r  r   r  r4   )r   clauser   r   r#   
add_clause+  s    zLandingPadInstr.add_clausec              	   C   s<   d}| |j| j| jrdndddd | jD d d S )Nz$landingpad {type}{cleanup}{clauses}
z cleanupr   c                 S   s   g | ]}d  |qS )z

      {0})r5   )r,   r  r   r   r#   r.   3  s   z)LandingPadInstr.descr.<locals>.<listcomp>)r2   r  r  )r4   r5   r2   r  r1   r  )r   r6   r   r   r   r#   r7   /  s    
zLandingPadInstr.descr)r   F)r<   r>   r?   r   r  r7   rA   r   r   r!   r#   r  $  s   r  c                       s6   e Zd ZdZddddhZd fdd		Zd
d Z  ZS )Fencez|
    The `fence` instruction.

    As of LLVM 5.0.1:

    fence [syncscope("<target-scope>")] <ordering>  ; yields void
    acquirereleaseZacq_relZseq_cstNr   c                    sT   t t| j|t dd|d || jkrDd}t||d| j|| _	|| _
d S )NZfencer   r   z3Invalid fence ordering "{0}"! Should be one of {1}.r)   )r   r  r   r   r   VALID_FENCE_ORDERINGSrY   r5   r1   r   targetscope)r   r   r   r  r   re   r!   r   r#   r   C  s    

zFence.__init__c                 C   s:   | j d krd}nd| j }d}||j|| jd d S )Nr   zsyncscope("{0}") zfence {syncscope}{ordering}
)	syncscoper   )r  r5   r4   r   )r   r6   r  r   r   r   r#   r7   M  s    

zFence.descr)Nr   )r<   r>   r?   r  r  r   r7   rA   r   r   r!   r#   r  8  s   
r  N)8r  Zllvmlite.irr   Zllvmlite.ir.valuesr   r   r   r   r   r   r	   r
   r   r   Zllvmlite.ir._utilsr   r   rB   rF   rO   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   objectr   r   r  r  r  r	  r  r  r   r   r   r#   <module>   sT   0*[!% 		