o
    4ήc                    @   sr  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddl	mZ e
dZe
dZe
dZe
d	Ze
d
Ze
 Ze
 Z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 dd Z!G dd deZ"G dd  d eZ#G d!d" d"eZ$e%d#kre&  dS dS )$z
IR Construction Tests
    N   )TestCase)ir)binding          @   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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d  ZdS )#TestBasez!
    Utilities for IR tests.
    c                 C   s6   dd }d t||}tdd|}| || dS )zr
        Assert *pattern* is in *text*, ignoring any whitespace differences
        (including newlines).
        c                 S   s   |   s|  sd|  S | S )N\)isalnumisspace)c r   =/tmp/pip-target-vg8gfxp4/lib/python/llvmlite/tests/test_ir.pyescape&   s   z%TestBase.assertInText.<locals>.escape z\s+z\\s*N)joinmapresubassertRegex)selfpatterntextr   regexr   r   r   assertInText    s   zTestBase.assertInTextc                 C   s&   dd t | D }| || d S )Nc                 S   s   g | ]}|  qS r   )strip).0liner   r   r   
<listcomp>0   s    z+TestBase.assert_ir_line.<locals>.<listcomp>)str
splitlinesassertIn)r   r   modlinesr   r   r   assert_ir_line/   s   zTestBase.assert_ir_linec                 C   s   t t| d S N)llvmparse_assemblyr!   r   r$   r   r   r   assert_valid_ir3   s   zTestBase.assert_valid_irc                 C   s<   t t j|dd}| |j|j | t|t| |S )zAssert that the IR object pickles and unpickles correctly.
        The IR string is equal and that their type is equal
        )protocol)pickleloadsdumpsassertIs	__class__assertEqualr!   )r   irobject	newobjectr   r   r   assert_pickle_correctly6   s   z TestBase.assert_pickle_correctlyc                 C   s   t  S r'   )r   Moduler   r   r   r   module?   s   zTestBase.moduleNmy_funcc              	   C   s4   |p|   }tttttttf}t|||S r'   )r9   r   FunctionTypeint32dblPointerTypeFunction)r   r9   namefntyr   r   r   functionB   s
   zTestBase.functionr   c                 C   s   |p|   }||S r'   )rB   append_basic_block)r   funcr@   r   r   r   blockH   s   
zTestBase.blockc                 C   s   g }| | d|S Nr   )descrr   )r   thingbufr   r   r   rG   L   s   

zTestBase.descrc                 C   s   t |}|dd}|S )Nz
    z
  )textwrapdedentreplace)r   asmr   r   r   _normalize_asmQ   s   
zTestBase._normalize_asmc                 C   s   |  |}| || d S r'   )rN   r3   )r   rG   rM   expectedr   r   r   check_descrW   s   
zTestBase.check_descrc                 C   s   |  | || d S r'   )rP   rG   )r   rE   rM   r   r   r   check_block[      zTestBase.check_blockc                 C   s*   |  |}| }| | |  d S r'   )rN   _stringify_bodyr3   r   r   r9   rM   rO   actualr   r   r   check_module_body^   s   
zTestBase.check_module_bodyc                 C   s*   |  |}| }| | |  dS )z6
        Check module metadata against *asm*.
        N)rN   _stringify_metadatar3   r   rT   r   r   r   check_metadatac   s   
zTestBase.check_metadatac                 C   sD   |  |}| |}|dd dd }| | |  d S )N{   }r   )rN   rG   	partition
rpartitionr3   r   )r   rD   rM   rO   rU   r   r   r   check_func_bodyk   s   

zTestBase.check_func_body)Nr:   rF   )__name__
__module____qualname____doc__r   r&   r+   r6   r9   rB   rE   rG   rN   rP   rQ   rV   rX   r^   r   r   r   r   r
      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d Z
d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 )!TestFunctionz>i32 @"my_func"(i32 %".1", i32 %".2", double %".3", i32* %".4")c                 C   s0   |   }| | }| | d| j  d S )Nz
declare %s)rB   rG   r   r3   protor   rD   rM   r   r   r   test_declarev   s   zTestFunction.test_declarec                 C   s   |   }|jd |jd d|j_tjtddd}t|  |d}||j_	| 
| }| |d| j  | | d S )	Noptsizealwaysinliner   r   Tvar_arg__gxx_personality_v0z\declare %s alwaysinline optsize alignstack(16) personality i8 (...)* @"__gxx_personality_v0")rB   
attributesadd
alignstackr   r;   int8r?   r9   personalityrG   r   r3   rd   r6   )r   rD   tp_perspersrM   r   r   r   test_declare_attributes|   s   z$TestFunction.test_declare_attributesc                 C   s   |   }|jd d d|jd j_d|jd j_d|jd j_|jd d |jd	 | |	 }| 
|d
 | | d S )Nr   zeroext   r   
         nonnullnoaliaszdeclare noalias i32 @"my_func"(i32 zeroext %".1", i32 dereferenceable(5) dereferenceable_or_null(10) %".2", double %".3", i32* nonnull align 4 %".4"))rB   argsadd_attributerl   dereferenceabledereferenceable_or_nullalignreturn_valuerG   r   r3   r6   re   r   r   r   test_function_attributes   s   z%TestFunction.test_function_attributesc                 C   sV   |   }| |}|d|g  | | }| |d| j d | | d S )Ndbgdeclare z !dbg !0)	r9   rB   set_metadataadd_metadatarG   r   r3   rd   r6   r   r9   rD   rM   r   r   r   test_function_metadata   s   
z#TestFunction.test_function_metadatac                 C   s@   |   }d|_| | }| |d| j d | | d S )N	a_sectionr   z section "a_section")rB   sectionrG   r   r3   rd   r6   re   r   r   r   test_function_section   s   z"TestFunction.test_function_sectionc                 C   s\   |   }| |}d|_|d|g  | | }| |d| j d | 	| d S )Nr   r   r   z section "a_section" !dbg !0)
r9   rB   r   r   r   rG   r   r3   rd   r6   r   r   r   r   test_function_section_meta   s   
z'TestFunction.test_function_section_metac                 C   sb   |   }| |}|jd |d|g  | | }| |d| j	 d | 
| d S )Nrh   r   r   z alwaysinline !dbg !0)r9   rB   rl   rm   r   r   rG   r   r3   rd   r6   r   r   r   r   test_function_attr_meta   s   
z$TestFunction.test_function_attr_metac                 C   sL   |   }|jd d|_| | }| |d| j d | | d S )Nrg   r   r   z optsize section "a_section")	rB   rl   rm   r   rG   r   r3   rd   r6   re   r   r   r   test_function_attr_section   s   z'TestFunction.test_function_attr_sectionc                 C   sh   |   }| |}|jd d|_|d|g  | | }| 	|d| j
 d | | d S )Nrh   r   r   r   z) alwaysinline section "a_section" !dbg !0)r9   rB   rl   rm   r   r   r   rG   r   r3   rd   r6   r   r   r   r   test_function_attr_section_meta   s   
z,TestFunction.test_function_attr_section_metac                 C   sT   |   }|jd |d}t|}|  | |}| |dj	| j
d d S )Nrh   my_blockz            define {proto} alwaysinline
            {{
            my_block:
                ret void
            }}
            )rd   )rB   rl   rm   rC   r   	IRBuilderret_voidrG   rP   formatrd   )r   rD   rE   builderrM   r   r   r   test_define   s   


zTestFunction.test_definec                 C   s   |   }t }|dtg}|d|tg}|d||tg}|d}| | | d | | | d | | | d | | | d d S )	N	llvm.powizllvm.memsetzllvm.memcpyzllvm.assumezD            declare double @"llvm.powi.f64"(double %".1", i32 %".2")zZ            declare void @"llvm.memset.p0i8.i32"(i8* %".1", i8 %".2", i32 %".3", i1 %".4")z`            declare void @"llvm.memcpy.p0i8.p0i8.i32"(i8* %".1", i8* %".2", i32 %".3", i1 %".4")z1            declare void @"llvm.assume"(i1 %".1"))	r9   ro   
as_pointerdeclare_intrinsicr=   r<   rP   rG   r   )r   r9   pint8powimemsetmemcpyassumer   r   r   test_declare_intrinsics   s   
z$TestFunction.test_declare_intrinsicsc                 C   s4   |   }|dtg}|dtg}| || d S )Nr   )r9   r   r=   r1   )r   r9   r   powi2r   r   r   test_redeclare_intrinsic     z%TestFunction.test_redeclare_intrinsicc                 C      |   }| | d S r'   )rB   r6   )r   fnr   r   r   test_pickling     zTestFunction.test_picklingc                 C   `   |   }| |}|jd d}| t| |jd W d    d S 1 s)w   Y  d S )Nrh   $Can't have alwaysinline and noinlinenoinliner9   rB   rl   rm   assertRaisesRegex
ValueErrorr   r9   rD   msgr   r   r   %test_alwaysinline_noinline_disallowed     
"z2TestFunction.test_alwaysinline_noinline_disallowedc                 C   r   )Nr   r   rh   r   r   r   r   r   %test_noinline_alwaysinline_disallowed  r   z2TestFunction.test_noinline_alwaysinline_disallowedN)r_   r`   ra   rd   rf   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   r   s"    	rc   c                   @   s   e 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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 ) TestIRc                 C   s8   |   }|tdtdg | d| | | d S )N{   *   z!0 = !{ i32 123, i8 42 })r9   r   r<   ro   r&   r+   r*   r   r   r   test_unnamed_metadata'  s   zTestIR.test_unnamed_metadatac                 C   s   |   }|tddg}|td|g}|td|g}| || ||||g | d| | d| | d| d S )Nr   kernel  z!0 = !{ i32 123, !"kernel" }z!1 = !{ i64 456, !0 }z!2 = !{ !0, !1, !1 })r9   r   r<   int64r1   r&   )r   r$   m0m1m2r   r   r   test_unnamed_metadata_2.  s   zTestIR.test_unnamed_metadata_2c                 C   sX   |   }|tdtdgtdgtdgg | d| | d| | d| d S )Nr   r   i  z!0 = !{ i32 456 }z!1 = !{ i32 789 }z!2 = !{ i32 123, !0, !1, !0 })r9   r   r<   r&   r*   r   r   r   test_unnamed_metadata_3>  s
   (zTestIR.test_unnamed_metadata_3c                 C   s$   |   }|dg | d| d S )Nz"\$z!0 = !{ !"\22\5c$" })r9   r   r&   r*   r   r   r   test_metadata_stringF  s   zTestIR.test_metadata_stringc                 C   s   |   }|tdg}|tdg}|d}|| || || | d| | | | ||	d | 
t |	d W d    d S 1 sRw   Y  d S )Nr   r   fooz!foo = !{ !0, !1, !0 }bar)r9   r   r<   r   add_named_metadatarm   r&   r+   r1   get_named_metadataassertRaisesKeyError)r   r$   r   r   nmdr   r   r   test_named_metadataL  s   




"zTestIR.test_named_metadatac                 C   s   |   }|tdg}|d| |dtdg |ddg |dg  | d| | d| | d| | d	| | d
| | d| | | d S )Nr   r   r   r   r   z!foo = !{ !0, !1, !2 }z!0 = !{ i32 123 }z!1 = !{ i64 456 }z!2 = !{ !"kernel" }z!bar = !{ !3 }z
!3 = !{  })r9   r   r<   r   r   r&   r+   )r   r$   r   r   r   r   test_named_metadata_2\  s   zTestIR.test_named_metadata_2c                 C   sf   |   }|t d g | d| | | |   }|d tdg | d| | | d S )Nz!0 = !{ i32* null }r   z!0 = !{ null, i32 123 })r9   r   r<   r   r&   r+   r*   r   r   r   test_metadata_nulll  s   
zTestIR.test_metadata_nullc              	   C   s   |   }|dddd}|dd|d gi}|jdtd|d	d
dddd}|jdd|d|d|ddd t|}| d| | d| | d| | d| | d| | | d S )NDIFiler   r   filename	directoryDISubroutineTypetypesDICompileUnitDW_LANG_PythonARTIQr   T)languagefileproducerruntimeVersionisOptimizedis_distinctDISubprogramr:      F)r@   r   r   typeisLocalunit/!0 = !DIFile(directory: "bar", filename: "foo")z!1 = !{ null }z!!2 = !DISubroutineType(types: !1)zy!3 = distinct !DICompileUnit(file: !0, isOptimized: true, language: DW_LANG_Python, producer: "ARTIQ", runtimeVersion: 0)zd!4 = distinct !DISubprogram(file: !0, isLocal: false, line: 11, name: "my_func", type: !2, unit: !3))r9   add_debug_infor   r   DITokenr!   r&   r+   )r   r$   di_filedi_func_typedi_compileunitstrmodr   r   r   test_debug_infox  sP   
zTestIR.test_debug_infoc                 C   s   |   }|dddd}|dddd}|dddd}|jdddddd}| || | t||||hd t|}| d| | d	| | d
| | | d S )Nr   r   r   r   Tr   rx   r   z/!1 = !DIFile(directory: "foo", filename: "bar")z8!2 = distinct !DIFile(directory: "bar", filename: "foo"))r9   r   r1   r3   lenr!   r&   r+   )r   r$   di1di2di3di4r   r   r   r   test_debug_info_2  sF   zTestIR.test_debug_info_2c              
   C   s  |   }t|t d}tt d|_|dddtdd}|d|d	i |jd
|j|ddddd}|	d| t
|}| d| | d| | d| | d| | d| tdddtddg}tdddtddg}||}||}|d}|| || |dddd}	|jdtd |	td!||gd"dd}
|d#|
 d S )$Ngvarr   DIBasicTypefloatr   DW_ATE_float)r@   sizeencodingDIGlobalVariableExpressionDIExpressionDIGlobalVariableT)r@   r   isDefinitionr   )exprvarr   zB!0 = !DIBasicType(encoding: DW_ATE_float, name: "float", size: 32)z!1 = !DIExpression()zK!2 = distinct !DIGlobalVariable(isDefinition: true, name: "gvar", type: !0)z3!3 = !DIGlobalVariableExpression(expr: !1, var: !2)z2@"gvar" = global float 0x4045000000000000, !dbg !3rZ   zDwarf Versionrw   zDebug Info Versionrx   zllvm.module.flagsr   r   r   r   r   r   	FullDebug)r   r   emissionKindglobalszllvm.dbg.cu)r9   r   GlobalVariable	FloatTypeConstantinitializerr   r   r@   r   r!   r&   IntTyper   r   rm   )r   r$   r   di_floatdi_gvarr   dverdiverflagsr   di_cur   r   r   test_debug_info_gvar  sj   






zTestIR.test_debug_info_gvarc                 C   sP   |   }|dddi t|}dtdd d }| d| d	| d S )
NDILocalVariabler@   u   a∆r   c                 S   s   d| dS )Nr   02xr   )xr   r   r   <lambda>  s    z7TestIR.test_debug_info_unicode_string.<locals>.<lambda>u   ∆z!0 = !DILocalVariable(name: "az"))r9   r   r!   r   r   encoder&   )r   r$   r   r@   r   r   r   test_debug_info_unicode_string  s
   z%TestIR.test_debug_info_unicode_stringc                 C   s   |   }t|tt g d}t|d}tttg}tj|dddd}|	|tdg |
  d}| |t| | | d S )	Nr   r   
mov $1, $2=r,rTside_effectr   8call i32 asm sideeffect "mov $1, $2", "=r,r" ( i32 123 ))r9   r   r?   r;   VoidTyper   rC   r<   	InlineAsmcallr   r   r!   r+   )r   r$   r   r   asmtyrM   patr   r   r   test_inline_assembly  s   zTestIR.test_inline_assemblyc                 C   s   |   }t|tt g d}t|d}tttg}|j|ddtdgdd |	  d}| 
|t| | | d S )	Nr   r   r  r  r   Tr  r  )r9   r   r?   r;   r  r   rC   r<   rM   r   r   r!   r+   )r   r$   r   r   r  r  r   r   r   test_builder_asm  s   zTestIR.test_builder_asmc                 C   sn   |   }t|tt g d}t|d}|tdd |	  d}| 
|t| | | d S )Nr   r   r	   raxzcall i64 asm "", "={rax}")r9   r   r?   r;   r  r   rC   load_regr   r   r   r!   r+   r   r$   r   r   r  r   r   r   test_builder_load_reg(  s   zTestIR.test_builder_load_regc                 C   st   |   }t|tt g d}t|d}|tdt	dd |
  d}| |t| | | d S )Nr   r   r   r	   r  z0call void asm sideeffect "", "{rax}" ( i64 123 ))r9   r   r?   r;   r  r   rC   	store_regr   r   r   r   r!   r+   r  r   r   r   test_builder_store_reg2  s   zTestIR.test_builder_store_regN)r_   r`   ra   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r   r   r   r   r   %  s     -D	
r   c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestGlobalValuesc                 C   s   |   }t|tt g d}t|tt g d t|t d}| |d| | |d| | 	t
 |d W d    n1 sNw   Y  | t|d d S )Nr   r   
globdoublekkkz2<ir.GlobalVariable 'globdouble' of type 'double*'>)r9   r   r?   r;   r  r   
DoubleTyper3   
get_globalr   r   repr)r   r$   r   r  r   r   r   test_globals_access?  s   
z$TestGlobalValues.test_globals_accessc                 C   s   |   }tt g }t||d}t||d}t|t d}| t|j	t||f | t|j
t|||f dS )zr
        Accessing functions and global values through Module.functions
        and Module.global_values.
        r   r   r  N)r9   r   r;   r  r?   r   r!  r3   set	functionsglobal_values)r   r$   ftyr   r   r  r   r   r   #test_functions_global_values_accessL  s    z4TestGlobalValues.test_functions_global_values_accessc                 C   s  |   }t|td}tj|tddd}t|td}td|_t|td}d|_t|td	}d
|_tj|tddd}d|_t|td}d
|_td|_d|_	t|td}	d
|	_td|	_d|	_
t|td}
d
|
_td|
_d|
_	d|
_
| |d dS )z7
        IR serialization of global variables.
        abr   	addrspacer   r   dTeinternalfr   gr   h	h_sectionir   	i_sectiona              @"a" = external global i8
            @"b" = external addrspace(42) global i8
            @"c" = global i32 123
            @"d" = external constant i32
            @"e" = internal global i32 undef
            @"f" = external unnamed_addr addrspace(456) global i32
            @"g" = internal global i32 123, align 16
            @"h" = internal global i32 123, section "h_section"
            @"i" = internal global i32 456, section "i_section", align 8
            N)r9   r   r   ro   r<   r   global_constantlinkageunnamed_addrr   r   rV   )r   r$   r*  r+  r   r.  r/  r1  r2  r3  r5  r   r   r   test_global_variables_irY  s2   



z)TestGlobalValues.test_global_variables_irc                 C   r   r'   )r9   r6   r*   r   r   r   test_pickle  r   zTestGlobalValues.test_pickleN)r_   r`   ra   r$  r)  r:  r;  r   r   r   r   r  =  s
    +r  c                   @   r  )
	TestBlockc                 C   s4   |   }tj|dd}| |j| | |j d S )Nstartparentr@   )rB   r   Blockr1   r?  assertFalseis_terminatedr   rD   rE   r   r   r   test_attributes  r   zTestBlock.test_attributesc                 C   sD   | j dd}| | |d |jddg | | |d d S )Nr   r@   z
my_block:
r*  r+  zmy_block:
  a
  b
)rE   r3   rG   instructionsextend)r   rE   r   r   r   
test_descr  s   zTestBlock.test_descrc                 C   s   | j dd}t|}|jjd d \}}|||d}|||d}|||d t||j	d||fd}| 
|d	 ||| | 
|d
 d S )Nr   rE  rZ   r   r.  r/  sdivr1  z            my_block:
                %"c" = add i32 %".1", %".2"
                %"d" = sub i32 %".1", %".2"
                %"e" = mul i32 %"d", %".2"
            z            my_block:
                %"c" = add i32 %".1", %".2"
                %"f" = sdiv i32 %"c", %".2"
                %"e" = mul i32 %"f", %".2"
            )rE   r   r   rB   r{   rm   r   mulInstructionr   rQ   rL   )r   rE   r   r*  r+  r   r.  r1  r   r   r   test_replace  s   
zTestBlock.test_replacec                 C   s*   |   }tj|dd}| t|d dS )z4
        Blocks should have a useful repr()
        r=  r>  z"<ir.Block 'start' of type 'label'>N)rB   r   r@  r3   r#  rC  r   r   r   	test_repr  s   zTestBlock.test_reprN)r_   r`   ra   rD  rH  rL  rM  r   r   r   r   r<    s
    r<  c                   @   s  e Zd 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d Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8dkS )lTestBuildInstructionszN
    Test IR generation of LLVM instructions through the IRBuilder class.
    i  c                 C   sX   | j dd}t|}|jjd d \}}|||d}| |d | t|d d S )Nr   rE  rZ   reszP            my_block:
                %"res" = add i32 %".1", %".2"
            z}<ir.Instruction 'res' of type 'i32', opname 'add', operands (<ir.Argument '.1' of type i32>, <ir.Argument '.2' of type i32>)>)	rE   r   r   rB   r{   rm   rQ   r3   r#  )r   rE   r   r*  r+  instr   r   r   test_simple  s   

z!TestBuildInstructions.test_simplec                 C   s  | j dd}t|}|jjd d \}}}|||d |||d |||d |||d |	||d |
||d	 |||d
 |||d |||d |||d |||d |||d |||d |||d |||d |||d |||d |||d | t}||| W d    n1 sw   Y  | t|jd | |j | |d d S )Nr   rE  rx   r   r.  r/  r1  r2  r3  r5  jklmnopqrstz1Operands must be the same type, got (i32, double)aD              my_block:
                %"c" = add i32 %".1", %".2"
                %"d" = fadd i32 %".1", %".2"
                %"e" = sub i32 %".1", %".2"
                %"f" = fsub i32 %".1", %".2"
                %"g" = mul i32 %".1", %".2"
                %"h" = fmul i32 %".1", %".2"
                %"i" = udiv i32 %".1", %".2"
                %"j" = sdiv i32 %".1", %".2"
                %"k" = fdiv i32 %".1", %".2"
                %"l" = urem i32 %".1", %".2"
                %"m" = srem i32 %".1", %".2"
                %"n" = frem i32 %".1", %".2"
                %"o" = or i32 %".1", %".2"
                %"p" = and i32 %".1", %".2"
                %"q" = xor i32 %".1", %".2"
                %"r" = shl i32 %".1", %".2"
                %"s" = ashr i32 %".1", %".2"
                %"t" = lshr i32 %".1", %".2"
            )rE   r   r   rB   r{   rm   faddr   fsubrJ  fmuludivrI  fdivuremsremfremor_and_xorshlashrlshrr   r   r3   r!   	exceptionrA  rB  rQ   )r   rE   r   r*  r+  ffcmr   r   r   test_binops  s:   
z!TestBuildInstructions.test_binopsc                 C   b   | j dd}t|}|jjd d \}}|j||ddd |j||ddd	gd | |d
 d S )Nr   rE  rZ   r   )nuwr  r.  rp  nswz            my_block:
                %"c" = add nuw i32 %".1", %".2"
                %"d" = sub nuw nsw i32 %".1", %".2"
            )rE   r   r   rB   r{   rm   r   rQ   r   rE   r   r*  r+  r   r   r   test_binop_flags     
z&TestBuildInstructions.test_binop_flagsc                 C   ro  )Nr   rE  rZ   r   fastrq  r.  ninfnszz            my_block:
                %"c" = fadd fast i32 %".1", %".2"
                %"d" = fsub ninf nsz i32 %".1", %".2"
            )rE   r   r   rB   r{   r]  r^  rQ   rs  r   r   r   test_binop_fastmath_flags  ru  z/TestBuildInstructions.test_binop_fastmath_flagsc                 C   s   | j dd}t|}|jjd d \}}|||d |||d |||d |||d |	||d |
||d	 | |d
 d S )Nr   rE  rZ   r   r.  r/  r1  r2  r3  a  my_block:
    %"c" = call {i32, i1} @"llvm.sadd.with.overflow.i32"(i32 %".1", i32 %".2")
    %"d" = call {i32, i1} @"llvm.smul.with.overflow.i32"(i32 %".1", i32 %".2")
    %"e" = call {i32, i1} @"llvm.ssub.with.overflow.i32"(i32 %".1", i32 %".2")
    %"f" = call {i32, i1} @"llvm.uadd.with.overflow.i32"(i32 %".1", i32 %".2")
    %"g" = call {i32, i1} @"llvm.umul.with.overflow.i32"(i32 %".1", i32 %".2")
    %"h" = call {i32, i1} @"llvm.usub.with.overflow.i32"(i32 %".1", i32 %".2")
            )rE   r   r   rB   r{   sadd_with_overflowsmul_with_overflowssub_with_overflowuadd_with_overflowumul_with_overflowusub_with_overflowrQ   rs  r   r   r   test_binops_with_overflow  s   
z/TestBuildInstructions.test_binops_with_overflowc                 C   sl   | j dd}t|}|jjd d \}}}||d ||d ||d | |j	 | 
|d d S )Nr   rE  rx   r.  r/  r1  z            my_block:
                %"d" = sub i32 0, %".1"
                %"e" = xor i32 %".2", -1
                %"f" = fneg double %".3"
            )rE   r   r   rB   r{   negnot_fnegrA  rB  rQ   r   rE   r   r*  r+  r   r   r   r   test_unary_ops-  s   
z$TestBuildInstructions.test_unary_opsc                 C   s   | j dd}t|}|jjd d \}}ttdtj}ttdtj}|||d}| 	|d |
|| |
|| | 	|d d S )Nr   rE  rZ   r   r   zN            my_block:
                %"c" = add i32 undef, undef
            zN            my_block:
                %"c" = add i32 %".1", %".2"
            )rE   r   r   rB   r{   r   r   	Undefinedrm   rQ   replace_usage)r   rE   r   r*  r+  undef1undef2r   r   r   r   test_replace_operand<  s   
z*TestBuildInstructions.test_replace_operandc                 C   sz  | j dd}t|}|jjd d \}}|d||d |d||d |d||d	 |d
||d |d||d |d||d |d||d |d||d |d||d |d
||d |d||d |d||d | t |d||d W d    n1 sw   Y  | t |d||d W d    n1 sw   Y  | 	|j
 | |d d S )Nr   rE  rZ   ==r   !=r.  <r/  <=r1  >r2  >=r3  r5  rR  rS  rT  rU  rV  unozzr   aj              my_block:
                %"c" = icmp eq i32 %".1", %".2"
                %"d" = icmp ne i32 %".1", %".2"
                %"e" = icmp ult i32 %".1", %".2"
                %"f" = icmp ule i32 %".1", %".2"
                %"g" = icmp ugt i32 %".1", %".2"
                %"h" = icmp uge i32 %".1", %".2"
                %"i" = icmp eq i32 %".1", %".2"
                %"j" = icmp ne i32 %".1", %".2"
                %"k" = icmp slt i32 %".1", %".2"
                %"l" = icmp sle i32 %".1", %".2"
                %"m" = icmp sgt i32 %".1", %".2"
                %"n" = icmp sge i32 %".1", %".2"
            )rE   r   r   rB   r{   icmp_unsignedicmp_signedr   r   rA  rB  rQ   rs  r   r   r   test_integer_comparisonsN  s.   
z.TestBuildInstructions.test_integer_comparisonsc                 C   s^  | j dd}t|}|jjd d \}}|d||d |d||d |d||d	 |d
||d |d||d |d||d |d||d |d||d |d||d |d
||d |d||d |d||d |d||d |d||d |d||d |d||d |jd||dg dd | |j | 	|d  d S )!Nr   rE  rZ   r  r   r  r.  r  r/  r  r1  r  r2  r  r3  r5  rR  rS  rT  rU  rV  ordur  vwr  olty)nnanrx  ry  arcprw  rq  a{              my_block:
                %"c" = fcmp oeq i32 %".1", %".2"
                %"d" = fcmp one i32 %".1", %".2"
                %"e" = fcmp olt i32 %".1", %".2"
                %"f" = fcmp ole i32 %".1", %".2"
                %"g" = fcmp ogt i32 %".1", %".2"
                %"h" = fcmp oge i32 %".1", %".2"
                %"i" = fcmp ueq i32 %".1", %".2"
                %"j" = fcmp une i32 %".1", %".2"
                %"k" = fcmp ult i32 %".1", %".2"
                %"l" = fcmp ule i32 %".1", %".2"
                %"m" = fcmp ugt i32 %".1", %".2"
                %"n" = fcmp uge i32 %".1", %".2"
                %"u" = fcmp ord i32 %".1", %".2"
                %"v" = fcmp uno i32 %".1", %".2"
                %"w" = fcmp ord i32 %".1", %".2"
                %"x" = fcmp uno i32 %".1", %".2"
                %"y" = fcmp nnan ninf nsz arcp fast olt i32 %".1", %".2"
            )
rE   r   r   rB   r{   fcmp_orderedfcmp_unorderedrA  rB  rQ   rs  r   r   r   test_float_comparisonss  s0   
z,TestBuildInstructions.test_float_comparisonsc                 C   sz   | j dd}ttd}t|}|jjd d \}}|j|||ddd | |j	 |
  | |j	 | |d d S )	Nr   rE  TrZ   r   )r  r  rq  z            my_block:
                %"c" = select arcp nnan i1 true, i32 %".1", i32 %".2"
                unreachable
            )rE   r   r   int1r   rB   r{   selectrA  rB  unreachable
assertTruerQ   )r   rE   r\  r   r*  r+  r   r   r   test_misc_ops  s   
z#TestBuildInstructions.test_misc_opsc                 C   s   | j dd}t|}|jjd d \}}|jd}|jd}|jtddd}||| ||| | 	|j
 | |d	 d S )
Nr   rE  rZ   b2b3my_phirv  rq  zj            my_block:
                %"my_phi" = phi fast i32 [%".1", %"b2"], [%".2", %"b3"]
            )rE   r   r   rB   r{   rC   phir<   add_incomingrA  rB  rQ   )r   rE   r   r*  r+  bb2bb3r  r   r   r   test_phi  s   
zTestBuildInstructions.test_phic                 C   s  | j dd}t|}|jjd d \}}}|jtdd}|jtddd}|jt|dd}d	|_| 	|j
tt |||}	| 	|	j
t  |||}
| 	|
j
t  ||d
}| 	|j
t |j||dd}| 	|j
t  |j|ddd}| 	|j
t |j||ddd}| 	|j
t  |j|dddd}| 	|j
t | t ||| W d    n1 sw   Y  | t || W d    n1 sw   Y  | t}||| W d    n1 sw   Y  | 	t|jd | |d d S )Nr   rE  rx   r   r   r.  )r   r@   r/  r   r2  r   )r   r5  seq_cstrw   )orderingr   rS  )r  r   r@   z.cannot store i32 to double*: mismatching typesa              my_block:
                %"c" = alloca i32
                %"d" = alloca i32, i32 42
                %"e" = alloca double, i32 %".1", align 8
                store double %".3", double* %"e"
                store i32 %".2", i32* %"c"
                %"g" = load i32, i32* %"c"
                store i32 %".2", i32* %"c", align 1
                %"i" = load i32, i32* %"c", align 1
                store atomic i32 %".2", i32* %"c" seq_cst, align 4
                %"k" = load atomic i32, i32* %"c" seq_cst, align 4
            )rE   r   r   rB   r{   allocar<   r=   r   r3   r   r>   storer  loadstore_atomicload_atomicr   	TypeErrorr!   rk  rQ   )r   rE   r   r*  r+  zr   r.  r/  eer1  r2  r3  r5  rR  rS  rm  r   r   r   test_mem_ops  sF   
z"TestBuildInstructions.test_mem_opsc                 C   s~   | j dd}t|}|jjd d \}}|jttdd}|j|t	td|gdd}| 
|jtt | |d d S )Nr   rE  rZ   r   ru   r.  z            my_block:
                %"c" = alloca i32*
                %"d" = getelementptr i32*, i32** %"c", i32 5, i32 %".1"
            )rE   r   r   rB   r{   r  r>   r<   gepr   r3   r   rQ   r   rE   r   r*  r+  r   r.  r   r   r   test_gep  s   
zTestBuildInstructions.test_gepc           	      C   s   | j dd}t|}|jjd d \}}t }tt|||tg}|j	|| dd}|j
|dd dD dd}| |jt| | |d	 d S )
Nr   rE  rZ   r.  c                 S      g | ]}t t|qS r   r   r   r<   r   r  r   r   r   r          z<TestBuildInstructions.test_gep_castinstr.<locals>.<listcomp>r   rx   r/  z            my_block:
                %"d" = bitcast i32 %".1" to {i64, i8*, i8*, i8*, i64}*
                %"e" = getelementptr {i64, i8*, i8*, i8*, i64}, {i64, i8*, i8*, i8*, i64}* %"d", i32 0, i32 3
            )rE   r   r   rB   r{   ro   r   LiteralStructTyper   bitcastr  r3   r   r>   rQ   )	r   rE   r   r*  r+  int8ptrlsr.  r/  r   r   r   test_gep_castinstr  s   
z(TestBuildInstructions.test_gep_castinstrc           
      C   s   | j dd}t|}|jjd d \}}d}t }tt|||tg}|j	||j|ddd}|j
|dd d	D d
d}	| |	jj| | |	jtj||d | |d d S )Nr   rE  rZ   rw   r,  r.  c                 S   r  r   r  r  r   r   r   r      r  zFTestBuildInstructions.test_gep_castinstr_addrspace.<locals>.<listcomp>r  r/  z            my_block:
                %"d" = bitcast i32 %".1" to {i64, i8*, i8*, i8*, i64} addrspace(4)*
                %"e" = getelementptr {i64, i8*, i8*, i8*, i64}, {i64, i8*, i8*, i8*, i64} addrspace(4)* %"d", i32 0, i32 3
            )rE   r   r   rB   r{   ro   r   r  r   r  r  r3   r   r-  r>   rQ   )
r   rE   r   r*  r+  r-  r  r  r.  r/  r   r   r   test_gep_castinstr_addrspace  s   
z2TestBuildInstructions.test_gep_castinstr_addrspacec           	      C   s   | j dd}t|}|jjd d \}}d}|jtjt|ddd}| t	|j
d | |j
jj| |j|ttd|gd	d}| |j
j| |j|ttd
gdd}| |j
j| | |d d S )Nr   rE  rZ   rw   r,  r   zi32 addrspace(4)**ru   r.  rv   r/  z            my_block:
                %"c" = alloca i32 addrspace(4)*
                %"d" = getelementptr i32 addrspace(4)*, i32 addrspace(4)** %"c", i32 5, i32 %".1"
                %"e" = getelementptr i32, i32 addrspace(4)* %"d", i32 10
            )rE   r   r   rB   r{   r  r>   r<   r3   r!   r   pointeer-  r  r   rQ   )	r   rE   r   r*  r+  r-  r   r.  r/  r   r   r   test_gep_addrspace!  s   
z(TestBuildInstructions.test_gep_addrspacec                 C   s  | j dd}t|}|jjd d \}}tttg}tt|g}t	|t	tdt	tdf}|j
|ddd}|j||ddd}	|j|	t	td	d
dd}
| |	j| | |
j| |j|dd}|j|dd}|j
|ddd}|j
|d
dd}|j
|ddd}|j
|ddd}|j||d
dd}|j||ddd}| |jt | |j| | |jt | |jt | |j| | |j| | t |
|d W d    n1 sw   Y  | t |
|d W d    n1 sw   Y  | t |
|d W d    n	1 sw   Y  | t |||d W d    n	1 s3w   Y  | t |||d
 W d    n	1 sPw   Y  | |d d S )Nr   rE  rZ   rw   Tr   r   r.  Fr   r/  ptrrR  rS  rT  )r   r   rU  )r   r   rV  rW  rX  )r   r   ru   a              my_block:
                %"c" = extractvalue {i32, i1} {i32 4, i1 true}, 0
                %"d" = insertvalue {i32, i1} {i32 4, i1 true}, i32 %".1", 0
                %"e" = insertvalue {i32, i1} %"d", i1 false, 1
                %"ptr" = alloca {i8, {i32, i1}}
                %"j" = load {i8, {i32, i1}}, {i8, {i32, i1}}* %"ptr"
                %"k" = extractvalue {i8, {i32, i1}} %"j", 0
                %"l" = extractvalue {i8, {i32, i1}} %"j", 1
                %"m" = extractvalue {i8, {i32, i1}} %"j", 1, 0
                %"n" = extractvalue {i8, {i32, i1}} %"j", 1, 1
                %"o" = insertvalue {i8, {i32, i1}} %"j", {i32, i1} %"l", 1
                %"p" = insertvalue {i8, {i32, i1}} %"j", i32 %".1", 1, 0
            )rE   r   r   rB   r{   r  r<   r  ro   r   extract_valueinsert_valuer3   r   r  r  r   r  rQ   )r   rE   r   r*  r+  tp_innertp_outerc_innerr   r.  r/  p_outerrR  rS  rT  rU  rV  rW  rX  r   r   r   test_extract_insert_value4  sV   

z/TestBuildInstructions.test_extract_insert_valuec                 C   s   | j dd}t|}|jjd d \}}}}|j|tdd}|j|tdd}|j	|tdd}	|
|td}
||
td}||td	}||td
}||td}||td}||td}||ttd}||td}| |j | |d d S )Nr   rE  rw   r   r.  r/  fbfcr2  r3  fdfer5  rR  rS  ah              my_block:
                %"c" = trunc i32 %".1" to i8
                %"d" = zext i8 %"c" to i32
                %"e" = sext i8 %"c" to i32
                %"fb" = fptrunc double %".3" to float
                %"fc" = fpext float %"fb" to double
                %"g" = fptoui double %".3" to i32
                %"h" = fptosi double %".3" to i8
                %"fd" = uitofp i32 %"g" to float
                %"fe" = sitofp i8 %"h" to double
                %"i" = ptrtoint i32* %".4" to i32
                %"j" = inttoptr i32 %"i" to i8*
                %"k" = bitcast i32 %".1" to float
            )rE   r   r   rB   r{   truncro   zextr<   sextfptruncfltfpextr=   fptouifptosiuitofpsitofpptrtointinttoptrr>   r  rA  rB  rQ   )r   rE   r   r*  r+  far  r   r.  r/  r  r  r2  r3  r  r  r5  rR  rS  r   r   r   test_cast_opsq  s"   
z#TestBuildInstructions.test_cast_opsc                 C   sh   | j dd}t|}|jjd d \}}|jtdd}|d||dd}| |j	t | 
|d d S )	Nr   rE  rZ   r   rm   	monotonicr.  z            my_block:
                %"c" = alloca i32
                %"d" = atomicrmw add i32* %"c", i32 %".1" monotonic
            )rE   r   r   rB   r{   r  r<   
atomic_rmwr3   r   rQ   r  r   r   r   test_atomicrmw  s   
z$TestBuildInstructions.test_atomicrmwc                 C   sJ   | j dd}t|}|jjdd}|| | |j | |d d S )Nr   rE  targetzE            my_block:
                br label %"target"
            )	rE   r   r   rB   rC   branchr  rB  rQ   )r   rE   r   	bb_targetr   r   r   test_branch  s   

z!TestBuildInstructions.test_branchc                 C   sd   | j dd}t|}|jjdd}|jjdd}|ttd|| | |j	 | 
|d d S )Nr   rE  b_trueb_falseFza            my_block:
                br i1 false, label %"b_true", label %"b_false"
            )rE   r   r   rB   rC   cbranchr   r  r  rB  rQ   )r   rE   r   bb_truebb_falser   r   r   test_cbranch  s   
z"TestBuildInstructions.test_cbranchc                 C   s   | j dd}t|}|jjdd}|jjdd}|ttd||}|ddg | 	|j
 | |d | |jd	 d S )
Nr   rE  r  r  Fru   r   zk            my_block:
                br i1 false, label %"b_true", label %"b_false", !prof !0
            zC            !0 = !{ !"branch_weights", i32 5, i32 42 }
            )rE   r   r   rB   rC   r  r   r  set_weightsr  rB  rQ   rX   r9   )r   rE   r   r  r  brr   r   r   test_cbranch_weights  s   
z*TestBuildInstructions.test_cbranch_weightsc                 C   sv   | j dd}t|}|jjdd}|jjdd}|t|j|}|| || | |j	 | 
|d d S )Nr   rE  b_1b_2z            my_block:
                indirectbr i8* blockaddress(@"my_func", %"b_1"), [label %"b_1", label %"b_2"]
            )rE   r   r   rB   rC   branch_indirectBlockAddressadd_destinationr  rB  rQ   )r   rE   r   bb_1bb_2
indirectbrr   r   r   test_branch_indirect  s   


z*TestBuildInstructions.test_branch_indirectc                    s    fdd} j dd}t|}|  ||d  j dd}t|}|td ||d  j dd}t|}| }|d	|jd
 ||d  j dd}t|}|td}|d	|jd
 ||d d S )Nc                    s     | j  | | d S r'   )r  rB  rQ   )rE   expected_irr8   r   r   check  s   z1TestBuildInstructions.test_returns.<locals>.checkr   rE  z;            my_block:
                ret void
            other_blockru   z?            other_block:
                ret i32 5
            r   r   zD            my_block:
                ret void, !dbg !0
               zE            my_block:
                ret i32 6, !dbg !0
            )	rE   r   r   r   retr<   r   r9   r   )r   r  rE   r   rP  r   r8   r   test_returns  s&   






z"TestBuildInstructions.test_returnsc           
      C   s   | j dd}t|}|jjd d \}}|jjdd}|jjdd}|jjdd}|jjdd}|||}	|	tt	d| |	tt	d	| |	d| | 
|j | |d
 d S )Nr   rE  rZ   onzeroononeontwo	otherwiser   r   z            my_block:
                switch i32 %".1", label %"otherwise" [i32 0, label %"onzero" i32 1, label %"onone" i32 2, label %"ontwo"]
            )rE   r   r   rB   r{   rC   switchadd_caser   r<   r  rB  rQ   )
r   rE   r   r*  r+  	bb_onzerobb_ononebb_ontwobb_elseswr   r   r   test_switch  s   
z!TestBuildInstructions.test_switchc                 C   sz  | j dd}t|}|jjd d \}}ttttf}tjttfdd}tt	ttf}t
|jj|d}t
|jj|d}	t
|jj|d}
||||fd	 ||	||fd
 ||
||fd |j|||fddd ||||fd}|jd |j|||fddd |j|||fddd |j|||fddd |j|||fddd |j|||fddd | |d d S )Nr   rE  rZ   Tri   r1  r2  r3  res_fres_gres_h
res_f_fastfastcc)cconvres_f_readonlyreadonlyres_fastrw  )fastmathres_nnan_ninf)r  rx  res_noinliner   )attrsres_alwaysinlinerh   res_noinline_ro)r   r  a          my_block:
            %"res_f" = call float @"f"(i32 %".1", i32 %".2")
            %"res_g" = call double (i32, ...) @"g"(i32 %".2", i32 %".1")
            %"res_h" = call half @"h"(i32 %".1", i32 %".2")
            %"res_f_fast" = call fastcc float @"f"(i32 %".1", i32 %".2")
            %"res_f_readonly" = call float @"f"(i32 %".1", i32 %".2") readonly
            %"res_fast" = call fast float @"f"(i32 %".1", i32 %".2")
            %"res_nnan_ninf" = call ninf nnan float @"f"(i32 %".1", i32 %".2")
            %"res_noinline" = call float @"f"(i32 %".1", i32 %".2") noinline
            %"res_alwaysinline" = call float @"f"(i32 %".1", i32 %".2") alwaysinline
            %"res_noinline_ro" = call float @"f"(i32 %".1", i32 %".2") noinline readonly
        )rE   r   r   rB   r{   r;   r  r<   r=   hlfr?   r9   r  rl   rm   rQ   )r   rE   r   r*  r+  tp_ftp_gtp_hr1  r2  r3  r  r   r   r   	test_call  s*   
zTestBuildInstructions.test_callc                 C   s|   | j dd}t|}tt t gd }t|j|d}|jt	dd}|j
d}|||||f | |d dS )	z9
        Function calls with metadata arguments.
        r   rE  rx   zllvm.dbg.declarer*  r   z            my_block:
                %"a" = alloca i32
                call void @"llvm.dbg.declare"(metadata i32* %"a", metadata !0, metadata !0)
            N)rE   r   r   r;   r  MetaDataTyper?   r9   r  r<   r   r  rQ   )r   rE   r   dbg_declare_tydbg_declarer*  r+  r   r   r   test_call_metadata4  s   
z(TestBuildInstructions.test_call_metadatac                 C   s   | j dd}t|}tt t tt f}t|jj	|d}|j
d d |jtdd}|jtdd}|j||ttd|fd	d
dd | |d d S )Nr   rE  funr   sretretvalotherr   r(  rz   rz   r   rZ   	arg_attrsz        my_block:
            %"retval" = alloca i32
            %"other" = alloca i32
            call void @"fun"(i32* noalias sret %"retval", i32 42, i32* noalias %"other")
        )rE   r   r   r;   r  r<   r   r?   rB   r9   r{   r|   r  r  r   rQ   )r   rE   r   fun_tyr'  r)  r*  r   r   r   test_call_attributesH  s"   
z*TestBuildInstructions.test_call_attributesc                 C   sx   |   }t|}tt d}t|jj|d}| t	 |j
|dddid W d    d S 1 s5w   Y  d S )Nr   r'  r   r(  r-  )rE   r   r   r;   r  r?   rB   r9   r   r   r  )r   rE   r   r/  r'  r   r   r   test_invalid_call_attributes`  s   
"z2TestBuildInstructions.test_invalid_call_attributesc           	      C   s   | j dd}t|}|jjd d \}}ttttf}t|jj	|d}|jj
dd}|jj
dd}||||f||d | |d d S )	Nr   rE  rZ   r1  normalunwindr  z            my_block:
                %"res_f" = invoke float @"f"(i32 %".1", i32 %".2")
                    to label %"normal" unwind label %"unwind"
            )rE   r   r   rB   r{   r;   r  r<   r?   r9   rC   invokerQ   )	r   rE   r   r*  r+  r  r1  	bb_normal	bb_unwindr   r   r   test_invokei  s   
z!TestBuildInstructions.test_invokec           	      C   s   | j dd}t|}tt t tt f}t|jj	|d}d|_
|jd d |jtdd}|jtdd}|jjd	d}|jjd
d}|j||ttd|f||ddddddd | |d d S )Nr   rE  r'  r  r   r(  r)  r*  r2  r3  r   rw  r   r+  rz   r,  )r  r  r  r.  a	          my_block:
            %"retval" = alloca i32
            %"other" = alloca i32
            invoke fast fastcc void @"fun"(i32* noalias sret %"retval", i32 42, i32* noalias %"other") noinline
                to label %"normal" unwind label %"unwind"
        )rE   r   r   r;   r  r<   r   r?   rB   r9   calling_conventionr{   r|   r  rC   r4  r   rQ   )	r   rE   r   r/  r'  r)  r*  r5  r6  r   r   r   test_invoke_attributesx  s2   
z,TestBuildInstructions.test_invoke_attributesc              
   C   s   | j dd}t|}|ttt gd}t|j	j
t d}d|_|t| |ttt|jd|g || | |d d S )Nr   rE  lp_ZTIiTr   z            my_block:
                %"lp" = landingpad {i32, i8*}
                    catch i8** @"_ZTIi"
                    filter [1 x i8**] [i8** @"_ZTIi"]
                resume {i32, i8*} %"lp"
            )rE   r   r   
landingpadr  r<   ro   r   r   rB   r9   r7  
add_clauseCatchClauseFilterClauser   	ArrayTyper   resumerQ   )r   rE   r   r:  int_typeinfor   r   r   test_landingpad  s&   



z%TestBuildInstructions.test_landingpadc                 C   sV   | j dd}t|}|jjd d \}}|jd||dd}|| | |d d S )Nr   rE  rZ   r  r   z            my_block:
                %"c" = icmp sgt i32 %".1", %".2"
                call void @"llvm.assume"(i1 %"c")
            )rE   r   r   rB   r{   r  r   rQ   r  r   r   r   test_assume  s   

z!TestBuildInstructions.test_assumec                 C   s  | j dd}t|}|jjd d \}}d|_d|_t|jd}t|tj	}t
d}|j|||ddd}|j|||d	d
d}| |d |d}|| || t|d	dg}|j|||dd | |d |d}|| || |j||dd | |d |d}|| || |j||ddd}	|j||d	dd}
| |d |||	|
 | |j d S )Ninsert_blockrE  rZ   r*  r+  r   r   vec1r   vec2zinsert_block:
    %"vec1" = insertelement <2 x i32> <i32 undef, i32 undef>, i32 %"a", i32 0
    %"vec2" = insertelement <2 x i32> %"vec1", i32 %"b", i32 1
            shuffle_blockshufz            shuffle_block:
                %"shuf" = shufflevector <2 x i32> %"vec2", <2 x i32> %"vec2", <2 x i32> <i32 1, i32 0>
            	add_blocksumz[            add_block:
                %"sum" = add <2 x i32> %"vec2", %"vec2"
            extract_blockex1ex2z            extract_block:
              %"ex1" = extractelement <2 x i32> %"vec2", i32 0
              %"ex2" = extractelement <2 x i32> %"vec2", i32 1
            )rE   r   r   rB   r{   r@   
VectorTyper   r   r  r   insert_elementrQ   rC   r  position_at_endshuffle_vectorrm   extract_elementr  r+   r9   )r   rE   r   r*  r+  vectyvecidxtymaskr   r.  r   r   r   test_vector_ops  s<   










z%TestBuildInstructions.test_vector_opsc                 C   J   | j dd}t|}ttd}|j|dd}|| | |d d S )Nr   rE  ru   r   z}            my_block:
                %"c" = call i64 @"llvm.bitreverse.i64"(i64 5)
                ret i64 %"c"
            )rE   r   r   r   r   
bitreverser  rQ   r   rE   r   r*  r   r   r   r   test_bitreverse     

z%TestBuildInstructions.test_bitreversec                 C   p   | j dd}t|}ttd}| t}|j|dd W d    n1 s(w   Y  | dt	|j
 d S )Nr   rE  ru   r   #expected an integer type, got float)rE   r   r   r   r  r   r  rZ  r#   r!   rk  r   rE   r   r*  raisesr   r   r   test_bitreverse_wrongtype     
z/TestBuildInstructions.test_bitreverse_wrongtypec                 C   s   | j dd}t|}| t}|dd  W d    n1 s!w   Y  | dt|j | t}|d d W d    n1 sEw   Y  | dt|j |dd  |dd |d	d |d
 |	  | 
|d d S )Nr   rE  r  z#Invalid fence ordering "monotonic"!zInvalid fence ordering "None"!acquirereleasesinglethreadacq_relr  z            my_block:
                fence acquire
                fence syncscope("singlethread") release
                fence syncscope("singlethread") acq_rel
                fence seq_cst
                ret void
            )rE   r   r   r   r   fencer#   r!   rk  r   rQ   )r   rE   r   ra  r   r   r   
test_fence
  s,   

z TestBuildInstructions.test_fencec                 C   rY  )Nr   rE  ru   r   zx            my_block:
                %"c" = call i32 @"llvm.bswap.i32"(i32 5)
                ret i32 %"c"
            )rE   r   r   r   r<   bswapr  rQ   r[  r   r   r   
test_bswap%  r]  z TestBuildInstructions.test_bswapc                 C   rY  )Nr   rE  ru   r   zx            my_block:
                %"c" = call i16 @"llvm.ctpop.i16"(i16 5)
                ret i16 %"c"
            )rE   r   r   r   int16ctpopr  rQ   r[  r   r   r   
test_ctpop1  r]  z TestBuildInstructions.test_ctpopc                 C   X   | j dd}t|}ttd}ttd}|j||dd}|| | |d d S )Nr   rE  ru   r   r   z}            my_block:
                %"c" = call i16 @"llvm.ctlz.i16"(i16 5, i1 1)
                ret i16 %"c"
            )	rE   r   r   r   rl  r  ctlzr  rQ   r  r   r   r   	test_ctlz=     

zTestBuildInstructions.test_ctlzc                 C   rY  )Nr   rE  g      @r+  z            my_block:
                %"b" = call i16 @"llvm.convert.to.fp16.f32"(float 0x4014000000000000)
                ret i16 %"b"
            )rE   r   r   r   r  convert_to_fp16r  rQ   rs  r   r   r   test_convert_to_fp16_f32J  r]  z.TestBuildInstructions.test_convert_to_fp16_f32c                 C   r^  )Nr   rE  ru   r+  expected a float type, got i16)rE   r   r   r   rl  r   r  rs  r#   r!   rk  r`  r   r   r   "test_convert_to_fp16_f32_wrongtypeV  rc  z8TestBuildInstructions.test_convert_to_fp16_f32_wrongtypec                 C   sL   | j dd}t|}ttd}|j|dtd}|| | |d d S )Nr   rE  ru   r+  r@   toz            my_block:
                %"b" = call float @"llvm.convert.from.fp16.f32"(i16 5)
                ret float %"b"
            )	rE   r   r   r   rl  convert_from_fp16r  r  rQ   rs  r   r   r   test_convert_from_fp16_f32a  s   

z0TestBuildInstructions.test_convert_from_fp16_f32c                 C   r^  )Nr   rE        @r+  zexpected a float return typerE   r   r   r   r  r   r  ry  r#   r!   rk  r`  r   r   r   !test_convert_from_fp16_f32_notypem  rc  z7TestBuildInstructions.test_convert_from_fp16_f32_notypec                 C   sr   | j dd}t|}ttd}| t}|j|dtd W d    n1 s)w   Y  | dt	|j
 d S )Nr   rE  r{  r+  rw  zexpected an i16 type, got floatr|  r`  r   r   r   $test_convert_from_fp16_f32_wrongtypex     
z:TestBuildInstructions.test_convert_from_fp16_f32_wrongtypec                 C   sr   | j dd}t|}ttd}| t}|j|dtd W d    n1 s)w   Y  | 	dt
|j d S )Nr   rE  r{  r+  rw  ru  )rE   r   r   r   r  r   r  ry  rl  r#   r!   rk  r`  r   r   r   %test_convert_from_fp16_f32_wrongtype2  r  z;TestBuildInstructions.test_convert_from_fp16_f32_wrongtype2c                 C   ro  )Nr   rE  ru   r   r   z}            my_block:
                %"c" = call i64 @"llvm.cttz.i64"(i64 5, i1 1)
                ret i64 %"c"
            )	rE   r   r   r   r   r  cttzr  rQ   r  r   r   r   	test_cttz  rr  zTestBuildInstructions.test_cttzc                 C   ~   | j dd}t|}ttd}ttd}| t}|j||dd W d    n1 s/w   Y  | 	dt
|j d S )Nr   rE  ru   rx   r   zexpected an i1 type, got i32)rE   r   r   r   r   r<   r   r  r  r#   r!   rk  r   rE   r   r*  r+  ra  r   r   r   test_cttz_wrongflag     
z)TestBuildInstructions.test_cttz_wrongflagc                 C   r  )Nr   rE  ru   r   r   r_  )rE   r   r   r   r  r  r   r  r  r#   r!   rk  r  r   r   r   test_cttz_wrongtype  r  z)TestBuildInstructions.test_cttz_wrongtypec                 C   sf   | j dd}t|}ttd}ttd}ttd}|j|||dd}|| | |d d S )Nr   rE  ru   r   rZ   fmaz            my_block:
                %"fma" = call float @"llvm.fma.f32"(float 0x4014000000000000, float 0x3ff0000000000000, float 0x4000000000000000)
                ret float %"fma"
            )rE   r   r   r   r  r  r  rQ   )r   rE   r   r*  r+  r   r  r   r   r   test_fma  s   

zTestBuildInstructions.test_fmac                 C   s   | j dd}t|}ttd}ttd}ttd}| t}|j|||dd W d    n1 s6w   Y  | dt	|j
 d S )Nr   rE  ru   r   rZ   r  z(expected an floating point type, got i32)rE   r   r   r   r<   r   r  r  r#   r!   rk  r   rE   r   r*  r+  r   ra  r   r   r   test_fma_wrongtype     
z(TestBuildInstructions.test_fma_wrongtypec                 C   s   | j dd}t|}ttd}ttd}ttd}| t}|j|||dd W d    n1 s6w   Y  | 	dt
|j d S )Nr   rE  ru   r   rZ   r  z7expected types to be the same, got float, double, float)rE   r   r   r   r  r=   r   r  r  r#   r!   rk  r  r   r   r   test_fma_mixedtypes  r  z)TestBuildInstructions.test_fma_mixedtypesN)9r_   r`   ra   rb   maxDiffrQ  rn  rt  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r&  r0  r1  r7  r9  rC  rD  rX  r\  rb  ri  rk  rn  rq  rt  rv  rz  r}  r~  r  r  r  r  r  r  r  r   r   r   r   rN    sn    1%-1=!($	!:rN  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d Zdd Zdd Zdd ZdS )TestBuilderMiscz=
    Test various other features of the IRBuilder class.
    c                 C   s\   | j dd}t|}| |j|j | |jtj | |j|jj | |jtj	 d S )Nr=  rE  )
rE   r   r   r1   rB   r?  assertIsInstancer?   r9   r7   r   rE   r   r   r   r   rD    s   
zTestBuilderMisc.test_attributesc              	   C   s,  | j dd}t|}|jjd d \}}|||d |jdd}||6 |||d |	  |
||d W d    n1 sEw   Y  |||d || W d    n1 s`w   Y  |||d	 || |||d
 W d    n1 sw   Y  | |d | |d d S )Nr   rE  rZ   r   r   r.  r/  r1  r2  r3  z            my_block:
                %"c" = add i32 %".1", %".2"
                %"e" = sub i32 %".1", %".2"
                %"g" = mul i32 %".1", %".2"
            z            foo:
                %"d" = fadd i32 %".1", %".2"
                %"f" = fsub i32 %".1", %".2"
                %"h" = fmul i32 %".1", %".2"
                br label %"foo"
            )rE   r   r   rB   r{   rm   rC   
goto_blockr]  goto_entry_blockr   r^  r  rJ  r_  rQ   )r   rE   r   r*  r+  bb_newr   r   r   test_goto_block  s&   

zTestBuilderMisc.test_goto_blockc                 C   s   | j dd}t|}ttd}|||d}||}|||d W d    n1 s/w   Y  | |j | |||d}|| |||d || W d    n1 s^w   Y  | 	|j
d d S )	NonerE  r   r*  r+  r   r.  a              one:
                %"a" = add i1 0, 0
                br i1 %"a", label %"one.if", label %"one.endif"
            one.if:
                %"b" = add i1 0, 0
                br label %"one.endif"
            one.endif:
                %"c" = add i1 0, 0
                br i1 %"c", label %"one.endif.if", label %"one.endif.endif"
            one.endif.if:
                %"d" = add i1 0, 0
                br label %"one"
            one.endif.endif:
            )rE   r   r   r   r  rm   if_thenr1   r  r^   rB   )r   rE   r   r  r*  bbendr   r   r   r   test_if_then  s   
zTestBuilderMisc.test_if_thenc              	   C   s   | j dd}t|}ttd}|||d}||+ |||d}|| |||d W d    n1 s<w   Y  W d    n1 sKw   Y  |  | |j	d d S )Nr  rE  r   r*  r+  r   a              one:
                %"a" = add i1 0, 0
                br i1 %"a", label %"one.if", label %"one.endif"
            one.if:
                %"b" = add i1 0, 0
                br i1 %"b", label %"one.if.if", label %"one.if.endif"
            one.endif:
                ret void
            one.if.if:
                %"c" = add i1 0, 0
                br label %"one.if.endif"
            one.if.endif:
                br label %"one.endif"
            )
rE   r   r   r   r  rm   r  r   r^   rB   )r   rE   r   r  r*  r+  r   r   r   test_if_then_nested$  s   
z#TestBuilderMisc.test_if_then_nestedc              	   C   s   d}| j |d}t|}ttd}|||d}||+ |||d}|| |||d W d    n1 s>w   Y  W d    n1 sMw   Y  |  | |j	dj
||d d d	 d
 d S )NPLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongrE  r   r*  r+  r   a              {full_label}:
                %"a" = add i1 0, 0
                br i1 %"a", label %"{label}.if", label %"{label}.endif"
            {label}.if:
                %"b" = add i1 0, 0
                br i1 %"b", label %"{label}.if.if", label %"{label}.if.endif"
            {label}.endif:
                ret void
            {label}.if.if:
                %"c" = add i1 0, 0
                br label %"{label}.if.endif"
            {label}.if.endif:
                br label %"{label}.endif"
               z..)
full_labellabel)rE   r   r   r   r  rm   r  r   r^   rB   r   )r   r  rE   r   r  r*  r+  r   r   r   test_if_then_long_label?  s    

z'TestBuilderMisc.test_if_then_long_labelc                    <    fdd}|d}  |jd |d}  |jd d S )Nc                    s`    j dd}t|}ttd}|j|| d W d    n1 s#w   Y   |d |S )Nr  rE  r   likelyzp                one:
                    br i1 0, label %"one.if", label %"one.endif", !prof !0
                )rE   r   r   r   r  r  rQ   )r  rE   r   r  r8   r   r   r  [  s   
z2TestBuilderMisc.test_if_then_likely.<locals>.checkTC            !0 = !{ !"branch_weights", i32 99, i32 1 }
            FC            !0 = !{ !"branch_weights", i32 1, i32 99 }
            rX   r9   r   r  r   r   r8   r   test_if_then_likelyZ  s
   z#TestBuilderMisc.test_if_then_likelyc              	   C   s\  | j dd}t|}ttd}|||d}||<\}}| |||d W d    n1 s4w   Y  | |||d W d    n1 sMw   Y  W d    n1 s\w   Y  ||7\}}| || W d    n1 s{w   Y  | |  W d    n1 sw   Y  W d    n1 sw   Y  | 	|j
d d S )Nr  rE  r   r*  r+  r   a)              one:
                %"a" = add i1 0, 0
                br i1 %"a", label %"one.if", label %"one.else"
            one.if:
                %"b" = add i1 0, 0
                br label %"one.endif"
            one.else:
                %"c" = add i1 0, 0
                br label %"one.endif"
            one.endif:
                br i1 %"a", label %"one.endif.if", label %"one.endif.else"
            one.endif.if:
                br label %"one"
            one.endif.else:
                ret void
            one.endif.endif:
            )rE   r   r   r   r  rm   if_elser  r   r^   rB   )r   rE   r   r  r*  thenr  r   r   r   test_if_elseo  s.   

zTestBuilderMisc.test_if_elsec                    r  )Nc              	      s    j dd}t|}ttd}|j|| d7\}}| || W d    n1 s-w   Y  | |  W d    n1 sCw   Y  W d    n1 sRw   Y   |j	d |S )Nr  rE  r   r  z                one:
                    br i1 0, label %"one.if", label %"one.else", !prof !0
                one.if:
                    br label %"one"
                one.else:
                    ret void
                one.endif:
                )
rE   r   r   r   r  r  r  r   r^   rB   )r  rE   r   r  r  r  r8   r   r   r    s   

	z2TestBuilderMisc.test_if_else_likely.<locals>.checkTr  Fr  r  r  r   r8   r   test_if_else_likely  s
   z#TestBuilderMisc.test_if_else_likelyc           	      C   s  |   }t }ttd}|jdd}|jdd}|jdd}|| |||d || |||d |||d || |||d	}|||d
 || |||d}|	| |||d |
| |||d | |d | |d | |d dS )zI
        Test IRBuilder.position_{before,after,at_start,at_end}.
        r   r  rE  twothreer*  rU  rV  rW  rX  r+  rY  r   z            one:
                %"a" = add i32 0, 0
                %"c" = add i32 0, 0
                %"b" = add i32 0, 0
            z            two:
                %"o" = add i32 0, 0
                %"q" = add i32 0, 0
                %"p" = add i32 0, 0
                %"m" = add i32 0, 0
                %"n" = add i32 0, 0
            z            three:
            N)rB   r   r   r   r<   rC   position_at_startrm   rQ  position_afterposition_beforerQ   )	r   rD   r   r  bb_onebb_twobb_threerW  r+  r   r   r   test_positioning  s.   





z TestBuilderMisc.test_positioningc           	      C   s   |   }t }|jdd}|| ttd}|||d}| }| 	|j
 || | |j
 |||d}|||d}|| |  | 	|j
 | |d d S )NentryrE  i  r*  r+  r   z            entry:
                %"a" = add i32 1234, 1234
                %"b" = mul i32 %"a", %"a"
                ret void
        )rB   r   r   rC   rQ  r   r<   rm   r   r  rB  removerA  rJ  rQ   )	r   rD   r   blkrS  r*  retvoidr+  r   r   r   r   test_instruction_removal  s    


z(TestBuilderMisc.test_instruction_removalc                 C   sH   | j dd}t|}|jg |_|jttdd | 	|d d S )Nr   rE  r   zN            my_block:
                %"c" = alloca i32*, !dbg !0
            )
rE   r   r   r9   r   debug_metadatar  r>   r<   rQ   r  r   r   r   test_metadata  s
   
zTestBuilderMisc.test_metadataN)r_   r`   ra   rb   rD  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s    $0r  c                   @   sl   e 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d Z
dd Zdd Zdd Zdd ZdS )	TestTypesc                 C   s,   t |tjr|j}t |tjst |tj S r'   )
isinstancer   r>   r  	LabelType)r   tyr   r   r   has_logical_equality  s   zTestTypes.has_logical_equalityc                 C   s   t  }t  t  t tttft ttft jttfddt ttfttttt	t 
tdt 
t	dt 
t	dt ttft ttf|d|dg}|dd |D 7 }|S )	z3
        A bunch of mutually unequal types
        Tri   ru   rw   MyType1MyType2c                 S   s(   g | ]}t |tjtjfst|qS r   )r  r   r  r  r>   )r   tpr   r   r   r      s    z,TestTypes.assorted_types.<locals>.<listcomp>)r   Contextr  r  r;   r  ro   r<   r  r=   r@  r  get_identified_type)r   contextr   r   r   r   assorted_types	  s   
zTestTypes.assorted_typesc                 C   s6   |   }|D ]}| |}| |r| || qd S r'   )r  r6   r  r3   )r   r   r  newtyr   r   r   r      s   

zTestTypes.test_picklingc                 C   s   |   }t||D ]\}}||ur&| ||k||f | ||k||f q
|D ]5}t|}| |rJ| ||k||f | ||k||f q)| ||k||f | ||k||f q)d S r'   )r  	itertoolsproductrA  r  copyr  )r   r   r*  r+  r  r*  r   r   r   test_comparisons'  s   

zTestTypes.test_comparisonsc              
   C   sx  |  ttd |  ttdd |  ttd |  ttd |  tt d |  tttdd |  ttttfd	 |  tttttfd
 |  ttjtdddd |  ttjttfddd |  ttjtttfddd |  tt	t
d |  tt	t	t
d |  tttdd |  ttt	tdd |  tt	ttdd |  tttfd |  ttttfd |  ttt	ttt
tffd |  ttjtfddd |  ttjttfddd t }|d}|  t|d |d}|  t|d |d }|  t|d! d"S )#z:
        Test the string representation of types.
        i1   i29r   doublevoidr   zi1 ()z
i1 (float)zi1 (float, double)Tri   zi1 (...)zi1 (float, ...)zi1 (float, double, ...)zi32*zi32**ru   z[5 x i1]z	[5 x i1*]z	[5 x i1]*z{i1}z{i1, float}z{i1*, {i32, i8}})packedz<{i1}>z<{i1, float}>MyTypez	%"MyType"zMyType\z%"MyType\5c"zMyType"z%"MyType\22"N)r3   r!   r  r   r   r  r=   r  r;   r>   r<   r@  r  ro   r  r  )r   r  mytypemytype1mytype2r   r   r   test_str7  s`   



zTestTypes.test_strc                 C   s4   t | j|  D ]}| t|tt| qd S r'   )filterr  r  r3   hashr  )r   typr   r   r   	test_hashd  s   zTestTypes.test_hashc                    s   fdd} fdd}t t}tdD ]}|||t q|| t td}tdD ]}|||t q,|| t tt ttff}||dt ||dt ttf  t	 |
t td	 W d    n1 sow   Y  || t  }t |d
}|tt ttf ||dt ||dt ttf  t	 |
t td	 W d    n1 sw   Y  || d S )Nc                    s"   |  tt|} || d S r'   )r  r   r   r<   r3   )r  r5  rO   rU   r8   r   r   check_constanti  s   z*TestTypes.test_gep.<locals>.check_constantc                    sF   t td} t | | W d    d S 1 sw   Y  d S )Ng      ?)r   r   r=   r   r  r  )r  indexr8   r   r   check_index_typem  s   "z,TestTypes.test_gep.<locals>.check_index_typeru   rx   r   r   rZ   r  )r   r>   r=   ranger@  r  r  ro   r   
IndexErrorr  r   r<   r  IdentifiedStructTypeset_body)r   r  r  r  r5  r  r   r8   r   r  h  s4   
zTestTypes.test_gepc                    st   t d fdd}|td |td |td |ttdd |ttdd |ttt	t	fd	 d S )
N%e-m:e-i64:64-f80:128-n8:16:32:64-S128c                         | | d S r'   )r3   get_abi_sizer  rO   r   tdr   r   r    rR   z&TestTypes.test_abi_size.<locals>.checkr   rw   r   ru      r   
r(   create_target_dataro   r<   r   r   r@  r  r=   r  r   r  r   r  r   test_abi_size     



zTestTypes.test_abi_sizec                    st   t d fdd}|td |td |td |ttdd |ttdd |ttt	t	fd d S )Nr  c                    r  r'   )r#   get_abi_alignmentr  r  r   r   r    rR   z+TestTypes.test_abi_alignment.<locals>.check)r   rZ   rw   )rw   )r   ru   r  r  r   r  r   test_abi_alignment  r  zTestTypes.test_abi_alignmentc                 C   s   t  }|d}t j|d}| |j | | t|}|t 	dt 	dt 
  | |j | | | |t| d S )Nr  r  r   r	   )r   r  r  r7   r  	is_opaquer+   r!   r  r   r   rA  assertNotEqual)r   r  r  r9   oldstrr   r   r   test_identified_struct  s   


z TestTypes.test_identified_structc                 C   sD   t  }|d}t dg|_td}| |j||dd d S )Nr  r   r  r  rw   )	r   r  r  r   elementsr(   r  r3   r  )r   r  r  r  r   r   r   $test_target_data_non_default_context  s
   

z.TestTypes.test_target_data_non_default_contextc                 C   s&   t t dd}| t|d d S )Nr   r   z	<8 x i32>)r   rO  r   r3   r!   )r   rT  r   r   r   test_vector     zTestTypes.test_vectorN)r_   r`   ra   r  r  r   r  r  r  r  r  r  r  r  r  r   r   r   r   r    s    -$r  c                 C   s   t t| S r'   r  )r5  r   r   r   c32  s   r  c                   @   s   e 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d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=S )>TestConstantc                 C   s   t td}| t|d t td}| t|d t td}| t|d t td}| t|d t td	}| t|d
 t tt j}| t|d t td }| t|d d S )Nr   zi32 42r   zi1 1r   zi1 0Ti1 trueFzi1 falsezi1 undef)r   r   r<   r3   r!   r  r  r   r   r   r   r   test_integers  s   zTestConstant.test_integersc                 C   s   t td}| t|d t td}| t|d t td}| t|d t td}| t|d t tt j}| t|d t td }| t|d d S )	N      ?zfloat 0x3ff8000000000000g      zfloat 0xbff8000000000000zdouble 0x3ff8000000000000zdouble 0xbff8000000000000zdouble undefz
double 0.0)r   r   r  r3   r!   r=   r  r  r   r   r   
test_reals  s   zTestConstant.test_realsc                 C   s  t t tdtdtdtdf}| t|d t t tdtdtt jf}| t|d t jtdtdtt jf}| t|d | 	t
}t jtdt td	f W d    n1 sjw   Y  | t|jd
 t t tdt j}| t|d t t tdd }| t|d t t tdtd}| t|d t t tdtd}| t|d t t tddt jdf}| t|d | 	t t t tdd W d    d S 1 sw   Y  d S )Nrx   ru   r   rw   z[3 x i32] [i32 5, i32 6, i32 4]rZ   z[2 x i32] [i32 5, i32 undef]z#[3 x i32] [i32 5, i32 6, i32 undef]r  z$all elements must have the same typez[2 x i32] undefz[2 x i32] zeroinitializerr   s   foobar_123z[11 x i8] c"foobar_123\80"s    z[4 x i8] c"\00\01\04\ff"z#[3 x i32] [i32 5, i32 undef, i32 6])ru   r   )r   r   r@  r<   r  r3   r!   r  literal_arrayr   r  r  rk  ro   	bytearrayr   )r   r   ra  r   r   r   test_arrays  s2   &" "zTestConstant.test_arraysc                 C   sR   t t dd}g d}t ||}ddtdj|}| t|| d S )Nr   r   )r   rZ   rw   rx   r   r   	      z<8 x i32> <{}>z, zi32 {})	r   rO  r   r   r   r   r   r3   r!   )r   rT  valsrU  vec_reprr   r   r   r    s   zTestConstant.test_vectorc                 C   s$   t t dd j}| |d d S )Nr   r   )r   r   r   constantr3   )r   r  r   r   r   test_non_nullable_int	  s   z"TestConstant.test_non_nullable_intc                 C   s  t ttf}t t|f}t |t t  dt tdf}| t|d t j	t t  dt tdf}| |j
| | t|d t j	t t  dt tt jf}| |j
| | t|d t |t j}| t|d t |d }| t|d t |d}| t|d t |d|f}| t|d	 t |d
}| t|t| | t t |d W d    d S 1 sw   Y  d S )Nr  T/{float, i1} {float 0x3ff8000000000000, i1 true}z0{float, i1} {float 0x3ff8000000000000, i1 undef}z{float, i1} undefz{float, i1} zeroinitializerr  Tr   L{i32, {float, i1}} {i32 42, {float, i1} {float 0x3ff8000000000000, i1 true}}r   r  rw   ru   r   )r   r  r  r  r<   r   r   r3   r!   literal_structr   r  r   r   )r   st1st2r   c1c2c3r   r   r   test_structs
	  sJ   





"zTestConstant.test_structsc                 C   s0   t d}t t ||gt j}| | d S )Nr   )r   r   r   r  r  r6   )r   i8str   r   r   &test_undefined_literal_struct_pickling,	  s   
z3TestConstant.test_undefined_literal_struct_picklingc                 C   s<  t d}| |tj | t|d td}| |tj | t|d ttd}|t	dt	dt	dg}| t|d	 |g d
}| t|d	 |d}| t|d | 
t |g d W d   n1 sqw   Y  tttf}tt|f}|d}| t|d |d}| t|d dS )z@
        Instantiating a type should create a constant.
        r   zi8 42Tr  rx   rw   ru   r   z[3 x i32] [i32 4, i32 5, i32 6]r  Nz[3 x i32] zeroinitializer)rw   ru   r   r  r  r
  r  r  )ro   r  r   r   r3   r!   r  r@  r<   r  r   r   r  r  )r   r   atr  r  r   r   r   test_type_instantiaton1	  s,   z#TestConstant.test_type_instantiatonc                 C   s   t d}| t|d dS )z8
        Constants should have a useful repr().
        r   z!<ir.Constant type='i32' value=42>N)r<   r3   r#  r  r   r   r   rM  O	  s   zTestConstant.test_reprc                 C   sz   t t t ddttd}|  }t ||jd}d|_	||_
tt|}tt|}| t|t| d S )Nr      
myconstantT)r   r   r@  r   r  r  r9   r   r   r7  r   r(   r)   r!   r3   )r   r   rU  gvparsedreparsedr   r   r   test_encoding_problemV	  s   
z"TestConstant.test_encoding_problemc                 C   s   |   }tttf}t||d}|dd dD }| t|d | |j	t
t t|d }| t |ttdg W d    n1 sNw   Y  t| d }|ttdg}| t|d | |j	t
t d S )Nr  c                 S   r  r   r  r  r   r   r   r    i	  r  z)TestConstant.test_gep.<locals>.<listcomp>r   r   zEgetelementptr ({float, i1}, {float, i1}* @"myconstant", i32 0, i32 1)r   z5getelementptr ({float, i1}, {float, i1}* null, i32 0))r9   r   r  r  r  r   r  r3   r!   r   r>   r   r   r  r<   r   )r   rU  r  r  r   const	const_ptrr  r   r   r   r  e	  s$   

zTestConstant.test_gepc                 C   s   |   }tttf}d}tj||d|d}| |j| |dd dD }| |j	j| | t
|d | |j	tjt|d d S )Nrw   r  r,  c                 S   r  r   r  r  r   r   r   r    	  r  z=TestConstant.test_gep_addrspace_globalvar.<locals>.<listcomp>r!  zRgetelementptr ({float, i1}, {float, i1} addrspace(4)* @"myconstant", i32 0, i32 1))r9   r   r  r  r  r   r3   r-  r  r   r!   r>   )r   rU  r  r-  r  r   r   r   r   test_gep_addrspace_globalvarx	  s   
z)TestConstant.test_gep_addrspace_globalvarc                 C   &   t tdt}| t|d d S )Nr   ztrunc (i64 1 to i32))r   r   r   r  r<   r3   r!   r  r   r   r   
test_trunc	  r  zTestConstant.test_truncc                 C   r%  )Nr   zzext (i32 1 to i64))r   r   r<   r  r   r3   r!   r  r   r   r   	test_zext	  r  zTestConstant.test_zextc                 C   r%  )Nr,   zsext (i32 -1 to i64))r   r   r<   r  r   r3   r!   r  r   r   r   	test_sext	  r  zTestConstant.test_sextc                 C   r%  )Nr   z*fptrunc (float 0x3ff0000000000000 to half))r   r   r  r  r  r3   r!   r  r   r   r   test_fptrunc	  r  zTestConstant.test_fptruncc                 C   r%  )Nr   z*fpext (float 0x3ff0000000000000 to double))r   r   r  r  r=   r3   r!   r  r   r   r   
test_fpext	  r  zTestConstant.test_fpextc                 C   s8   |   }t|td}|t }| t|d d S )Nr  z$bitcast (i32* @"myconstant" to i64*))	r9   r   r   r<   r  r   r   r3   r!   )r   rU  r  r   r   r   r   test_bitcast	  s   zTestConstant.test_bitcastc                 C   r%  )Nr   z(fptoui (float 0x3ff0000000000000 to i32))r   r   r  r  r<   r3   r!   r  r   r   r   test_fptoui	  r  zTestConstant.test_fptouic                 C   r%  )Nr   zuitofp (i32 1 to float))r   r   r<   r  r  r3   r!   r  r   r   r   test_uitofp	  r  zTestConstant.test_uitofpc                 C   r%  )Nr   z(fptosi (float 0x3ff0000000000000 to i32))r   r   r  r  r<   r3   r!   r  r   r   r   test_fptosi	  r  zTestConstant.test_fptosic                 C   r%  )Nr   zsitofp (i32 1 to float))r   r   r<   r  r  r3   r!   r  r   r   r   test_sitofp	  r  zTestConstant.test_sitofpc                 C   sZ   t t d }t td}|t}| t|jt | t|jt | 	t
|d d S )Nr   zptrtoint (i64* null to i32))r   r   r   r   r<   r  r   r  r  r3   r!   )r   r  r  r   r   r   r   test_ptrtoint_1	  s   
zTestConstant.test_ptrtoint_1c                 C   sh   |   }t|td}|t}| t|d | t	d|jt
  ttd}| t	d|jt d S )Nr  z$ptrtoint (i32* @"myconstant" to i64)z2can only ptrtoint\(\) to integer type, not 'i64\*'r   z5can only call ptrtoint\(\) on pointer type, not 'i32')r9   r   r   r<   r  r   r3   r!   r   r  r   r   )r   rU  r  r   r  r   r   r   test_ptrtoint_2	  s"   
zTestConstant.test_ptrtoint_2c                 C   s^   t td}t td}|t }| t|jt | t|jt  | 	t
|d d S )Nr   gQ	@zinttoptr (i32 1 to i64*))r   r   r<   r  r  r   r   r   r  r3   r!   )r   r  pir   r   r   r   test_inttoptr	  s   zTestConstant.test_inttoptrc                 C   $   t td}| t| d d S )Nr   zsub (i32 0, i32 1))r   r   r<   r3   r!   r  r   r  r   r   r   test_neg	     zTestConstant.test_negc                 C   r4  )Nr   zxor (i32 1, i32 -1))r   r   r<   r3   r!   r  r5  r   r   r   test_not	  r7  zTestConstant.test_notc                 C   r4  )Nr   zfneg (float 0x3ff0000000000000))r   r   r  r3   r!   r  r5  r   r   r   	test_fneg	  r7  zTestConstant.test_fnegc           
      C   s0  t td}t td}|jd|jd|jd|jd|jd|jd|j	d	|j
d
|jd|jd|jd|jd|jdi}| D ]\}}| t|||d  q9ddddddd}| D ]\}}t|||}d| d }	| ||	 qWddddddd}| D ]\}}t|||}d| d }	| ||	 q}d S )Nr   rZ   rh  rj  ri  rm   r   rJ  r`  rI  rb  rc  orandrg  z (i32 1, i32 2)eqneugtugeultuler  r  r  r  r  r  zicmp sgtsgesltsle)r   r   r<   rh  rj  ri  rm   r   rJ  r`  rI  rb  rc  re  rf  rg  itemsr3   r!   r  r  )
r   r  r  oracler   iropcopcondrU   rO   r   r   r   test_int_binops	  s2   zTestConstant.test_int_binopsc           
   
   C   s  t td}t td}|jd|jd|jd|jd|jdi}| D ]\}}t	||}|d }| 
|| q!d	d
ddddd}| D ]\}}	t	|||}d|	 d }| 
|| qCddddddd}| D ]\}}	t	|||}d|	 d }| 
|| qid S )Nr   rZ   r]  r^  r_  ra  rd  z5 (float 0x3ff0000000000000, float 0x4000000000000000)oeqr  ogtoger  olerB  zfcmp uequner>  r?  r@  rA  )r   r   r  r]  r^  r_  ra  rd  rG  r!   r3   r  r  )
r   r  r  rH  r   rI  rU   rO   rJ  rK  r   r   r   test_flt_binops	  s0   zTestConstant.test_flt_binopsN)!r_   r`   ra   r  r   r  r  r	  r  r  r  rM  r   r  r$  r&  r'  r(  r)  r*  r+  r,  r-  r.  r/  r0  r1  r3  r6  r8  r9  rL  rS  r   r   r   r   r    s>    "		r  c                   @      e Zd Zdd ZdS )TestTransformsc                 C   s   t  }t |t t  dd}t |t t  dd}t  }||  ||d}| 	|j
| t |||}| || | |j
| | 	|j
| d S )Nr   r   r   )r   r7   r?   r;   r  r   rQ  rC   r  r3   calleereplace_all_callsr#   r  )r   r$   r   r   r   r  modifiedr   r   r   test_call_transform
  s   z"TestTransforms.test_call_transformN)r_   r`   ra   rY  r   r   r   r   rU  
      rU  c                   @   rT  )TestSingletonc                 C   sP   |  tjtj  |  tjttj |  tjttj | tj d S r'   )r1   r   r  values
_Undefinedr  deepcopyr6   r8   r   r   r   test_undefined,
  s   zTestSingleton.test_undefinedN)r_   r`   ra   r_  r   r   r   r   r[  +
  rZ  r[  __main__)'rb   r  r  r.   r   rJ   unittestr   r   llvmliter   r   r(   r   r  ro   rl  r<   r   HalfTyper  r   r  r!  r=   r
   rc   r   r  r<  rN  r  r  r  r  rU  r[  r_   mainr   r   r   r   <module>   sb    




W 4  L-        .  ( ;  ^