
    pi                       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 ddl	mZ  e
j                  d      Z e
j                  d      Z e
j                  d	      Z e
j                  d
      Z e
j                  d      Z e
j(                         Z e
j,                         Z e
j0                         Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z!d Z" G d de      Z# G d d e      Z$ G d! d"e      Z%e&d#k(  r ejN                          yy)$z
IR Construction Tests
    N   )TestCase)ir)binding)ir_layer_typed_pointers_enabled          @   c                   t    e Zd ZdZd Zd Zd Zd Zd ZddZ	dd	Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestBasez!
    Utilities for IR tests.
    c                     d }dj                  t        ||            }t        j                  dd|      }| j	                  ||       y)zr
        Assert *pattern* is in *text*, ignoring any whitespace differences
        (including newlines).
        c                 P    | j                         s| j                         sd| z   S | S )N\)isalnumisspace)cs    X/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/llvmlite/tests/test_ir.pyescapez%TestBase.assertInText.<locals>.escape'   s!    99;qyy{axH     z\s+z\\s*N)joinmapresubassertRegex)selfpatterntextr   regexs        r   assertInTextzTestBase.assertInText!   s@    	
 ''#fg./vw0u%r   c                     t        |      j                         D cg c]  }|j                          }}| j                  |       y c c}w N)str
splitlinesstripassertIn)r   linemodliness       r   assert_ir_linezTestBase.assert_ir_line0   s<    *-c(*=*=*?@$@@dE" As   Ac                 @    t        j                  t        |             y r#   )llvmparse_assemblyr$   r   r)   s     r   assert_valid_irzTestBase.assert_valid_ir4   s    CH%r   c                     t        j                  t        j                  |d            }| j                  |j                  |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	newobjects      r   assert_pickle_correctlyz TestBase.assert_pickle_correctly7   sU     LLh!DE	h(()*=*=>XI7r   c                 *    t        j                         S r#   )r   Moduler   s    r   modulezTestBase.module@   s    yy{r   Nc           	          |xs | j                         }t        j                  t        t        t        t        t        j
                  t              f      }t        j                  |||      S r#   )r@   r   FunctionTypeint32dblPointerTypeFunction)r   r@   namefntys       r   functionzTestBase.functionC   sL    (4;;=uueS')~~e'<'> ?{{64..r   c                 L    |xs | j                         }|j                  |      S r#   )rI   append_basic_block)r   funcrG   s      r   blockzTestBase.blockI   s#    &t}}&&t,,r   c                 J    g }|j                  |       dj                  |      S Nr   )descrr   )r   thingbufs      r   rP   zTestBase.descrM   s!    Cwws|r   c                 T    t        j                  |      }|j                  dd      }|S )Nz
    z
  )textwrapdedentreplace)r   asms     r   _normalize_asmzTestBase._normalize_asmR   s%    ooc"kk(F+
r   c                 J    | j                  |      }| j                  ||       y r#   )rX   r   r   rP   rW   expecteds       r   check_descr_regexzTestBase.check_descr_regexX   "    &&s+)r   c                 J    | j                  |      }| j                  ||       y r#   )rX   r9   rZ   s       r   check_descrzTestBase.check_descr\   r]   r   c                 F    | j                  | j                  |      |       y r#   )r_   rP   r   rM   rW   s      r   check_blockzTestBase.check_block`   s    E*C0r   c                 F    | j                  | j                  |      |       y r#   )r\   rP   ra   s      r   check_block_regexzTestBase.check_block_regexc   s    tzz%0#6r   c                     | j                  |      }|j                         }| j                  |j                         |j                                y r#   )rX   _stringify_bodyr9   r&   r   r@   rW   r[   actuals        r   check_module_bodyzTestBase.check_module_bodyf   s=    &&s+''))9:r   c                     | j                  |      }|j                         }| j                  |j                         |j                                y)z6
        Check module metadata against *asm*.
        N)rX   _stringify_metadatar9   r&   rg   s        r   check_metadatazTestBase.check_metadatak   s?     &&s+++-)9:r   c                     | j                  |      }| j                  |      }|j                  d      d   j                  d      d   }| j	                  |j                         |j                                y )N{   }r   )rX   rP   	partition
rpartitionr9   r&   )r   rL   rW   r[   rh   s        r   check_func_bodyzTestBase.check_func_bodys   sb    &&s+D!!!#&q)44S9!<)9:r   )Nmy_funcrO   )__name__
__module____qualname____doc__r!   r+   r0   r<   r@   rI   rM   rP   rX   r\   r_   rb   rd   ri   rl   rs    r   r   r   r      sW    &#&/-
**17;
;;r   r   c                   r    e Zd ZesdndZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)TestFunctionz=i32 @"my_func"(i32 %".1", i32 %".2", double %".3", ptr %".4")z>i32 @"my_func"(i32 %".1", i32 %".2", double %".3", i32* %".4")c                     | j                         }| j                  |      j                         }| j                  |j                         d| j                  z         y )Nz
declare %s)rI   rP   r&   r9   protor   rL   rW   s      r   test_declarezTestFunction.test_declare   sC    }}jj$$&lTZZ&?@r   c                 v   | j                         }|j                  j                  d       |j                  j                  d       |j                  j                  d       d|j                  _        t	        j
                  t        dd      }t	        j                  | j                         |d      }||j                  _	        | j                  |      j                         }t        s | j                  |d	| j                  z         n| j                  |d
| j                  z         | j                  |       y )Noptsizealwaysinline
convergentr	   ry   Tvar_arg__gxx_personality_v0zadeclare %s alwaysinline convergent optsize alignstack(16) personality ptr @"__gxx_personality_v0"zgdeclare %s alwaysinline convergent optsize alignstack(16) personality i8 (...)* @"__gxx_personality_v0")rI   
attributesadd
alignstackr   rB   int8rF   r@   personalityrP   r&   r   r9   r}   r<   )r   rL   tp_perspersrW   s        r   test_declare_attributesz$TestFunction.test_declare_attributes   s    }}I&N+L)%'"//$D9{{4;;='3IJ&*#jj$$&.SJ "ZZ() SD "ZZ() 	$$T*r   c                 <   | j                         }|j                  d   j                  d       d|j                  d   j                  _        d|j                  d   j                  _        d|j                  d   j                  _        |j                  d   j                  d       |j                  j                  d	       | j                  |      j                         }t        s| j                  |d
       n| j                  |d       | j                  |       y )Nr   zeroext   r   
         nonnullnoaliaszdeclare noalias i32 @"my_func"(i32 zeroext %".1", i32 dereferenceable(5) dereferenceable_or_null(10) %".2", double %".3", ptr nonnull align 4 %".4")zdeclare noalias i32 @"my_func"(i32 zeroext %".1", i32 dereferenceable(5) dereferenceable_or_null(10) %".2", double %".3", i32* nonnull align 4 %".4"))rI   argsadd_attributer   dereferenceabledereferenceable_or_nullalignreturn_valuerP   r&   r   r9   r<   r~   s      r   test_function_attributesz%TestFunction.test_function_attributes   s    }}		!""9-23		!/:<		!7()		!%		!""9-''	2jj$$&.S x S y 	$$T*r   c                 (   | j                         }| j                  |      }|j                  d|j                  g              | j	                  |      j                         }| j                  |d| j                   d       | j                  |       y )Ndbgdeclare z !dbg !0)	r@   rI   set_metadataadd_metadatarP   r&   r9   r}   r<   r   r@   rL   rW   s       r   test_function_metadataz#TestFunction.test_function_metadata   s}    }}V$%!4!4R!89jj$$&#DJJ<x8	 	$$T*r   c                     | j                         }d|_        | j                  |      j                         }| j	                  |d| j
                   d       | j                  |       y )N	a_sectionr   z section "a_section")rI   sectionrP   r&   r9   r}   r<   r~   s      r   test_function_sectionz"TestFunction.test_function_section   s\    }}"jj$$&#DJJ</CD	 	$$T*r   c                 6   | j                         }| j                  |      }d|_        |j                  d|j	                  g              | j                  |      j                         }| j                  |d| j                   d       | j                  |       y )Nr   r   r   z section "a_section" !dbg !0)
r@   rI   r   r   r   rP   r&   r9   r}   r<   r   s       r   test_function_section_metaz'TestFunction.test_function_section_meta   s    }}V$"%!4!4R!89jj$$&#DJJ</KL	 	$$T*r   c                 ^   | j                         }| j                  |      }|j                  j                  d       |j	                  d|j                  g              | j                  |      j                         }| j                  |d| j                   d       | j                  |       y )Nr   r   r   z alwaysinline !dbg !0)r@   rI   r   r   r   r   rP   r&   r9   r}   r<   r   s       r   test_function_attr_metaz$TestFunction.test_function_attr_meta   s    }}V$N+%!4!4R!89jj$$&#DJJ</DE	 	$$T*r   c                    | j                         }|j                  j                  d       d|_        | j	                  |      j                         }| j                  |d| j                   d       | j                  |       y )Nr   r   r   z optsize section "a_section")	rI   r   r   r   rP   r&   r9   r}   r<   r~   s      r   test_function_attr_sectionz'TestFunction.test_function_attr_section   so    }}I&"jj$$&#DJJ</KL	N 	$$T*r   c                 l   | j                         }| j                  |      }|j                  j                  d       d|_        |j                  d|j                  g              | j                  |      j                         }| j                  |d| j                   d       | j                  |       y )Nr   r   r   r   z) alwaysinline section "a_section" !dbg !0)r@   rI   r   r   r   r   r   rP   r&   r9   r}   r<   r   s       r   test_function_attr_section_metaz,TestFunction.test_function_attr_section_meta   s    }}V$N+"%!4!4R!89jj$$&#DJJ</XY	 	$$T*r   c                 @   | j                         }|j                  j                  d       |j                  d      }t	        j
                  |      }|j                          | j                  |      }| j                  |dj                  | j                               y )Nr   my_blockz            define {proto} alwaysinline
            {{
            my_block:
                ret void
            }}
            )r}   )rI   r   r   rK   r   	IRBuilderret_voidrP   r_   formatr}   )r   rL   rM   builderrW   s        r   test_definezTestFunction.test_define   s~    }}N+''
3,,u%jj  TZZ(	*r   c                 @   | j                         }t        j                         }|j                  dt        g      }|j                  d|t
        g      }|j                  d||t
        g      }|j                  d      }| j                  | j                  |      j                         d       t        s_| j                  | j                  |      j                         d       | j                  | j                  |      j                         d       n^| j                  | j                  |      j                         d       | j                  | j                  |      j                         d	       | j                  | j                  |      j                         d
       y )N	llvm.powizllvm.memsetzllvm.memcpyzllvm.assumezD            declare double @"llvm.powi.f64"(double %".1", i32 %".2")z\                declare void @"llvm.memset.p0.i32"(ptr %".1", i8 %".2", i32 %".3", i1 %".4")z`                declare void @"llvm.memcpy.p0.p0.i32"(ptr %".1", ptr %".2", i32 %".3", i1 %".4")z^                declare void @"llvm.memset.p0i8.i32"(i8* %".1", i8 %".2", i32 %".3", i1 %".4")zd                declare void @"llvm.memcpy.p0i8.p0i8.i32"(i8* %".1", i8* %".2", i32 %".3", i1 %".4")z1            declare void @"llvm.assume"(i1 %".1"))
r@   r   
as_pointerdeclare_intrinsicrD   rC   r_   rP   r&   r   )r   r@   pint8powimemsetmemcpyassumes          r   test_declare_intrinsicsz$TestFunction.test_declare_intrinsics  se   !''cU;))-%H))-%9NO))-8D)//1 4H 	I.TZZ/557 :` aTZZ/557 :d e TZZ/557 :b cTZZ/557 :h iF+113 65 	6r   c                     | j                         }|j                  dt        g      }|j                  dt        g      }| j                  ||       y )Nr   )r@   r   rD   r7   )r   r@   r   powi2s       r   test_redeclare_intrinsicz%TestFunction.test_redeclare_intrinsic'  sC    ''cU;((se<dE"r   c                 F    | j                         }| j                  |       y r#   )rI   r<   )r   fns     r   test_picklingzTestFunction.test_pickling-  s    ]]_$$R(r   c                    | j                         }| j                  |      }|j                  j                  d       d}| j	                  t
        |      5  |j                  j                  d       d d d        y # 1 sw Y   y xY w)Nr   $Can't have alwaysinline and noinlinenoinliner@   rI   r   r   assertRaisesRegex
ValueErrorr   r@   rL   msgs       r   %test_alwaysinline_noinline_disallowedz2TestFunction.test_alwaysinline_noinline_disallowed1  si    }}V$N+4##J4 	,OO
+	, 	, 	,   A::Bc                    | j                         }| j                  |      }|j                  j                  d       d}| j	                  t
        |      5  |j                  j                  d       d d d        y # 1 sw Y   y xY w)Nr   r   r   r   r   s       r   %test_noinline_alwaysinline_disallowedz2TestFunction.test_noinline_alwaysinline_disallowed:  si    }}V$J'4##J4 	0OO/	0 	0 	0r   N)ru   rv   rw   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   r   r{   r{   z   sa     / 	LL 

A+4+*
+	+++	++* 6.#),0r   r{   c                   r    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)TestIRc                     | j                         }|j                  t        d      t        d      g       | j	                  d|       | j                  |       y )N{   *   z!0 = !{ i32 123, i8 42 })r@   r   rC   r   r+   r0   r/   s     r   test_unnamed_metadatazTestIR.test_unnamed_metadataF  sG    kkm%*d2h/06<S!r   c                    | j                         }|j                  t        d      dg      }|j                  t        d      |g      }|j                  t        d      |g      }| j	                  ||       |j                  |||g       | j                  d|       | j                  d|       | j                  d|       y )Nr   kernel  z!0 = !{ i32 123, !"kernel" }z!1 = !{ i64 456, !0 }z!2 = !{ !0, !1, !1 })r@   r   rC   int64r7   r+   )r   r)   m0m1m2s        r   test_unnamed_metadata_2zTestIR.test_unnamed_metadata_2M  s    kkmuSz845uSz2./uSz2./b""b"&:C@3S92C8r   c                    | j                         }|j                  t        d      t        d      gt        d      gt        d      gg       | j                  d|       | j                  d|       | j                  d|       y )Nr   r   i  z!0 = !{ i32 456 }z!1 = !{ i32 789 }z!2 = !{ i32 123, !0, !1, !0 })r@   r   rC   r+   r/   s     r   test_unnamed_metadata_3zTestIR.test_unnamed_metadata_3]  sm    kkm%*uSzlU3ZL5:,OP/5/5;SAr   c                 l    | j                         }|j                  dg       | j                  d|       y )Nz"\$z!0 = !{ !"\22\5c$" })r@   r   r+   r/   s     r   test_metadata_stringzTestIR.test_metadata_stringe  s.    kkm'#4c:r   c                    | j                         }|j                  t        d      g      }|j                  t        d      g      }|j	                  d      }|j                  |       |j                  |       |j                  |       | j                  d|       | j                  |       | j                  ||j                  d             | j                  t              5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr   r   fooz!foo = !{ !0, !1, !0 }bar)r@   r   rC   r   add_named_metadatar   r+   r0   r7   get_named_metadataassertRaisesKeyError)r   r)   r   r   nmds        r   test_named_metadatazTestIR.test_named_metadatak  s    kkmuSzl+uSzl+$$U+4c:S!c311%89x( 	*""5)	* 	* 	*s   $C??Dc                    | j                         }|j                  t        d      g      }|j                  d|       |j                  dt	        d      g       |j                  ddg       |j                  dg        | j                  d|       | j                  d|       | j                  d|       | j                  d	|       | j                  d
|       | j                  d|       | j                  |       y )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 = !{  })r@   r   rC   r   r   r+   r0   )r   r)   r   s      r   test_named_metadata_2zTestIR.test_named_metadata_2{  s    kkmuSzl+ub)uuSzl3uxj1ub)4c:/5/5137,c2L#.S!r   c                    | j                         }|j                  t        j                         d       g       t        s| j                  d|       n| j                  d|       | j                  |       | j                         }|j                  d t        d      g       | j                  d|       | j                  |       y )Nz!0 = !{ ptr null }z!0 = !{ i32* null }r   z!0 = !{ null, i32 123 })r@   r   rC   r   r   r+   r0   r/   s     r   test_metadata_nullzTestIR.test_metadata_null  s    kkm%**,T234. 4c: 5s;S!kkm$c
+,5s;S!r   c           
         | j                         }|j                  dddd      }|j                  dd|j                  d g      i      }|j                  dt        j                  d      |d	d
ddd      }|j                  dd|d|d|dd       t        |      }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  |       y )NDIFiler   r   filename	directoryDISubroutineTypetypesDICompileUnitDW_LANG_PythonARTIQr   T)languagefileproducerruntimeVersionisOptimizedis_distinctDISubprogramrt      F)rG   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))r@   add_debug_infor   r   DITokenr$   r+   r0   )r   r)   di_filedi_func_typedi_compileunitstrmods         r   test_debug_infozTestIR.test_debug_info  sK    kkm$$X0
  ))*<S%%tf-?
  ++O

#34>
  ,  	> ",
  	 	 SM"	$,f5?H D #	$ 	 " #	$ 	S!r   c                    | j                         }|j                  dddd      }|j                  dddd      }|j                  dddd      }|j                  ddddd      }| j                  ||       | j                  t	        ||||h      d       t        |      }| j                  d|       | j                  d	|       | j                  d
|       | j                  |       y )Nr   r   r   r   Tr  r   r	  z/!1 = !DIFile(directory: "foo", filename: "bar")z8!2 = distinct !DIFile(directory: "bar", filename: "foo"))r@   r
  r7   r9   lenr$   r+   r0   )r   r)   di1di2di3di4r  s          r   test_debug_info_2zTestIR.test_debug_info_2  s   kkm  .3/4"$%   .3/4"$%   .3/4"$%   .3/4"$15 ! 7 	c3c3S12A6SM"	$M"	$ %&,	.S!r   c                    | j                         }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  ddd	t        j                  d
      d      }|j                  ddd	t        j                  d
      d      }	|j                  ddd	t        j                  d
      d      }
| j	                  ||       | j	                  ||       | j	                  ||       | j	                  ||       | j	                  ||	       | j	                  ||
       | j                  t        ||||||||	|
h	      d       t        |      }| j                  d|       | j                  d|       | j                  d|       | j                  |       y )NDIBasicTypei8r   DW_ATE_unsignedrG   sizeencodingi32r
   i64r   DW_ATE_signedr   zA!0 = !DIBasicType(encoding: DW_ATE_unsigned, name: "i8", size: 8)zC!1 = !DIBasicType(encoding: DW_ATE_unsigned, name: "i32", size: 32)zA!2 = !DIBasicType(encoding: DW_ATE_signed, name: "i64", size: 64))
r@   r
  r   r  r7   r9   r  r$   r+   r0   )r   r)   r  r  r  r  di5di6di7di8di9r  s               r   test_debug_info_3zTestIR.test_debug_info_3  sk   kkm  

#451
 
   

#451
 
   

#451
 
   

#451
 
   

#451
 
   

#451
 
   

?31
 
   

?31
 
   

?31
 
 	c3c3c3c3c3c3c3S#sCcJKQOS 34:	< 56<	> 56<	>S!r   c                    | j                         }|j                  dddt        j                  d      d      }|j                  dddt        j                  d      d      }|j                  dt        j                  d	      d
|d t        j                  d      d      d       |j                  dt        j                  d	      d|d t        j                  d      d      d       t        |      }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  |       y )Nr  rC   r
   r!  r  r   r   DIDerivedTypeDW_TAG_member	field_i32r   )tagrG   baseTyper  	extraDatafield_i8ro   zC!0 = !DIBasicType(encoding: DW_ATE_signed, name: "int32", size: 32)zA!1 = !DIBasicType(encoding: DW_ATE_signed, name: "int8", size: 8)zd!2 = !DIDerivedType(baseType: !0, extraData: i32 1, name: "field_i32", size: 32, tag: DW_TAG_member)za!3 = !DIDerivedType(baseType: !1, extraData: i8 2, name: "field_i8", size: 8, tag: DW_TAG_member))r@   r
  r   r  IntTyper$   r+   r0   )r   r)   di_base_i32
di_base_i8r  s        r   test_debug_info_4zTestIR.test_debug_info_4'  sU   kkm((

?39
 
 ''

?38
 
 	?::o.#'B*-
 	 	?::o."&Aq)-
 	 S 78>	@ 56<	>?@F	H 	=>D	F 	S!r   c                    | j                         }t        j                  |t        j                         d      }t        j                  t        j                         d      |_        |j                  dddt        j                  d      d      }|j                  d|j                  d	i       |j                  d
|j                  |ddd      d      }|j                  d|       t        |      }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|        t        j                  d      d      d t        j                  d      d      g} t        j                  d      d      d t        j                  d      d      g}|j                  |      }|j                  |      }|j                  d      }|j                  |       |j                  |       |j                  dddd      }	|j                  dt        j                  d       |	t        j                  d!      |j                  |g      d"d      }
|j                  d#|
       y )$Ngvarr   r  floatr
   DW_ATE_floatr  DIGlobalVariableExpressionDIExpressionDIGlobalVariableT)rG   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 !3ro   zDwarf Versionr   zDebug Info Versionr   zllvm.module.flagsr   r   r   r   r   r   	FullDebug)r   r   emissionKindglobalszllvm.dbg.cu)r@   r   GlobalVariable	FloatTypeConstantinitializerr
  r  rG   r   r$   r+   r0  r   r   r   )r   r)   r5  di_floatdi_gvarr  dverdiverflagsr  di_cus              r   test_debug_info_gvarzTestIR.test_debug_info_gvarR  sh    kkm  bllnf=;;r||~r:%%m

>26
 
 $$%A&&~r:%%&8		  $;  	 & !D
  	%) S 78>	@2F; <=C	E '(.	0 &'-	/ 

2q!?NBJJrN14EFB"$8."**R.:KL%  '&&':;		$		%$$X0
  ""?

#34JJ{3''	2	5

  #  	}e4r   c                     | j                         }|j                  dddi       t        |      }dj                  t	        d dj                                     }| j                  d| d|       y )	NDILocalVariablerG   u   a∆r   c                     d| dS )Nr   02xry   )xs    r   <lambda>z7TestIR.test_debug_info_unicode_string.<locals>.<lambda>  s    r!C\ r   u   ∆z!0 = !DILocalVariable(name: "az"))r@   r
  r$   r   r   encoder+   )r   r)   r  rG   s       r   test_debug_info_unicode_stringz%TestIR.test_debug_info_unicode_string  sb    kkm,vv.>?Swws15<<>BC<TF"EvNr   c                 
   | j                         }t        j                  |t        j                  t        j                         g       d      }t        j
                  |j                  d            }t        j                  t        t        g      }t        j                  |ddd      }|j                  |t        d      g       |j                          d}| j                  |t        |             | j                  |       y )	Nr   r   
mov $1, $2=r,rTside_effectr   z7call i32 asm sideeffect "mov $1, $2", "=r,r"( i32 123 ))r@   r   rF   rB   VoidTyper   rK   rC   	InlineAsmcallr   r!   r$   r0   )r   r)   r   r   asmtyrW   pats          r   test_inline_assemblyzTestIR.test_inline_assembly  s    kkmkk#rr{{}bA5I,,s55b9:w/ll5,DIS5:,'G#s3x(S!r   c                    | j                         }t        j                  |t        j                  t        j                         g       d      }t        j
                  |j                  d            }t        j                  t        t        g      }|j                  |ddt        d      gd       |j                          d}| j                  |t        |             | j                  |       y )	Nr   r   rU  rV  r   TrW  z8call i32 asm sideeffect "mov $1, $2", "=r,r" ( i32 123 ))r@   r   rF   rB   rY  r   rK   rC   rW   r   r!   r$   r0   )r   r)   r   r   r\  r]  s         r   test_builder_asmzTestIR.test_builder_asm  s    kkmkk#rr{{}bA5I,,s55b9:w/E<%*4PH#s3x(S!r   c                    | j                         }t        j                  |t        j                  t        j                         g       d      }t        j
                  |j                  d            }|j                  t        j                  d      d       |j                          d}| j                  |t        |             | j                  |       y )Nr   r   r   raxzcall i64 asm "", "={rax}")r@   r   rF   rB   rY  r   rK   load_regr0  r   r!   r$   r0   r   r)   r   r   r]  s        r   test_builder_load_regzTestIR.test_builder_load_reg  s    kkmkk#rr{{}bA5I,,s55b9:B/)#s3x(S!r   c                    | j                         }t        j                  |t        j                  t        j                         g       d      }t        j
                  |j                  d            }|j                  t        d      t        j                  d      d       |j                          d}| j                  |t        |             | j                  |       y )Nr   r   r   r   rb  z0call void asm sideeffect "", "{rax}" ( i64 123 ))r@   r   rF   rB   rY  r   rK   	store_regr   r0  r   r!   r$   r0   rd  s        r   test_builder_store_regzTestIR.test_builder_store_reg  s    kkmkk#rr{{}bA5I,,s55b9:%*bjjne<@#s3x(S!r   N)ru   rv   rw   r   r   r   r   r   r   r   r  r  r'  r3  rK  rS  r^  r`  re  rh  ry   r   r   r   r   D  s^    "9 B;* " "+"Z">?"B)"VB5HO
"	"""r   r   c                   $    e Zd Zd Zd Zd Zd Zy)TestGlobalValuesc                    | j                         }t        j                  |t        j                  t        j                         g       d      }t        j                  |t        j                  t        j                         g       d       t        j
                  |t        j                         d      }| j                  |j                  d      |       | j                  |j                  d      |       | j                  t              5  |j                  d       d d d        t        s| j                  t        |      d       y | j                  t        |      d       y # 1 sw Y   GxY w)Nr   r   
globdoublekkkz.<ir.GlobalVariable 'globdouble' of type 'ptr'>z2<ir.GlobalVariable 'globdouble' of type 'double*'>)r@   r   rF   rB   rY  rA  
DoubleTyper9   
get_globalr   r   r   repr)r   r)   r   rl  s       r   test_globals_accessz$TestGlobalValues.test_globals_access  s   kkmkk#rr{{}bA5I
C;UC&&sBMMO\J
.45zBx( 	"NN5!	" /T*-MO Z DF	" 	"s   E##E,c                    | j                         }t        j                  t        j                         g       }t        j                  ||d      }t        j                  ||d      }t        j
                  |t        j                         d      }| j                  t        |j                        t        ||f             | j                  t        |j                        t        |||f             y)zr
        Accessing functions and global values through Module.functions
        and Module.global_values.
        r   r   rl  N)r@   r   rB   rY  rF   rA  rn  r9   set	functionsglobal_values)r   r)   ftyr   r   rl  s         r   #test_functions_global_values_accessz4TestGlobalValues.test_functions_global_values_access  s    
 kkmoobkkmR0kk#sE*kk#sE*&&sBMMO\J
S]]+S#s_=S../c3
5K1LMr   c                 B   | j                         }t        j                  |t        d      }t        j                  |t        dd      }t        j                  |t        d      }t	        d      |_        t        j                  |t        d      }d|_        t        j                  |t        d	      }d
|_        t        j                  |t        dd      }d|_        t        j                  |t        d      }d
|_        t	        d      |_        d|_	        t        j                  |t        d      }	d
|	_        t	        d      |	_        d|	_
        t        j                  |t        d      }
d
|
_        t	        d      |
_        d|
_	        d|
_
        | j                  |d       y)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)r@   r   rA  r   rC   rD  global_constantlinkageunnamed_addrr   r   ri   )r   r)   ry  rz  r   r}  r~  r  r  r  r  s              r   test_global_variables_irz)TestGlobalValues.test_global_variables_ir  sO    kkmc4-c4;c5#.c
c5#. c5#.	c5#=c5#.	c
c5#.	c
	c5#.	c
	s 
% 
	r   c                 F    | j                         }| j                  |       y r#   )r@   r<   r/   s     r   test_picklezTestGlobalValues.test_pickle  s    kkm$$S)r   N)ru   rv   rw   rq  rw  r  r  ry   r   r   rj  rj    s    F$N)V*r   rj  c                   $    e Zd Zd Zd Zd Zd Zy)	TestBlockc                     | j                         }t        j                  |d      }| j                  |j                  |       | j                  |j                         y )NstartparentrG   )rI   r   Blockr7   r  assertFalseis_terminatedr   rL   rM   s      r   test_attributeszTestBlock.test_attributes  sC    }}73ellD),,-r   c                     | j                  d      }| j                  | j                  |      d       |j                  j	                  ddg       | j                  | j                  |      d       y )Nr   rG   z
my_block:
ry  rz  zmy_block:
  a
  b
)rM   r9   rP   instructionsextend)r   rM   s     r   
test_descrzTestBlock.test_descr$  s]    



+E*M:!!3*-E*,CDr   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||d      }|j                  ||d      }|j                  ||d       t        j                  ||j                  d||fd      }| j                  |d	       |j                  ||       | j                  |d
       y )Nr   r  ro   r   r}  r~  sdivr  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"
            )rM   r   r   rI   r   r   r   mulInstructionr  rb   rV   )r   rM   r   ry  rz  r   r}  r  s           r   test_replacezTestBlock.test_replace*  s    



+,,u%$$Ra(1KK1c"KK1c"Aq#NN5!&&&1a&#> ! 	 	a ! 	r   c                     | j                         }t        j                  |d      }| j                  t	        |      d       y)z4
        Blocks should have a useful repr()
        r  r  z"<ir.Block 'start' of type 'label'>N)rI   r   r  r9   rp  r  s      r   	test_reprzTestBlock.test_repr@  s4     }}73e&JKr   N)ru   rv   rw   r  r  r  r  ry   r   r   r  r    s    .E,Lr   r  c                   ^   e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6 Z8d7 Z9d8 Z:d9 Z;y:);TestBuildInstructionszN
    Test IR generation of LLVM instructions through the IRBuilder class.
    i  c                 
   | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||d      }| j                  |d       | j                  t        |      d       y )Nr   r  ro   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>)>)	rM   r   r   rI   r   r   rb   r9   rp  )r   rM   r   ry  rz  insts         r   test_simplez!TestBuildInstructions.test_simpleP  s~    



+,,u%$$Ra(1{{1a' ! 	
 	d<	=r   c                 T   | j                  d      }t        j                  |      }|j                  j                  d 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       |j%                  ||d       |j'                  ||d       |j)                  ||d       |j+                  ||d       |j-                  ||d       | j/                  t0              5 }|j                  ||       d d d        | j3                  t5        j6                        d       | j9                  |j:                         | j=                  |d       y # 1 sw Y   \xY w)Nr   r  r   r   r}  r~  r  r  r  r  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"
            )rM   r   r   rI   r   r   faddr   fsubr  fmuludivr  fdivuremsremfremor_and_xorshlashrlshrr   r   r9   r$   	exceptionr  r  rb   )r   rM   r   ry  rz  ffcms          r   test_binopsz!TestBuildInstructions.test_binops_  s   



+,,u%##((!,1bAq#Q3Aq#Q3Aq#Q3Q3Q3Q3Q3Q3Q3Aq#Q3Aq#Aq#Q3Q3z* 	bKK2	R\\*L	N,,- ! 		 	s   0HH'c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||dd       |j                  ||ddd	g       | j                  |d
       y )Nr   r  ro   r   )nuwrI  r}  r  nswz            my_block:
                %"c" = add nuw i32 %".1", %".2"
                %"d" = sub nuw nsw i32 %".1", %".2"
            )rM   r   r   rI   r   r   r   rb   r   rM   r   ry  rz  s        r   test_binop_flagsz&TestBuildInstructions.test_binop_flags  s}    



+,,u%$$Ra(1Aq#X.Aq#eU^4 ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||dd       |j                  ||ddd	g       | j                  |d
       y )Nr   r  ro   r   fastr  r}  ninfnszz            my_block:
                %"c" = fadd fast i32 %".1", %".2"
                %"d" = fsub ninf nsz i32 %".1", %".2"
            )rM   r   r   rI   r   r  r  rb   r  s        r   test_binop_fastmath_flagsz/TestBuildInstructions.test_binop_fastmath_flags  s}    



+,,u%$$Ra(1Q3i0Q3vuo6 ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||d       |j                  ||d       |j                  ||d       |j                  ||d       |j                  ||d       |j                  ||d	       | j                  |d
       y )Nr   r  ro   r   r}  r~  r  r  r  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")
            )rM   r   r   rI   r   sadd_with_overflowsmul_with_overflowssub_with_overflowuadd_with_overflowumul_with_overflowusub_with_overflowrb   r  s        r   test_binops_with_overflowz/TestBuildInstructions.test_binops_with_overflow  s    



+,,u%$$Ra(1""1a-""1a-""1a-""1a-""1a-""1a- ! 	r   c                 R   | j                  d      }t        j                  |      }|j                  j                  d d \  }}}|j                  |d       |j                  |d       |j                  |d       | j                  |j                         | j                  |d       y )Nr   r  r   r}  r~  r  z            my_block:
                %"d" = sub i32 0, %".1"
                %"e" = xor i32 %".2", -1
                %"f" = fneg double %".3"
            )rM   r   r   rI   r   negnot_fnegr  r  rb   r   rM   r   ry  rz  r   s         r   test_unary_opsz$TestBuildInstructions.test_unary_ops  s    



+,,u%""''+1aAsQQ,,- ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}t        j
                  t        j                  d      t        j                        }t        j
                  t        j                  d      t        j                        }|j                  ||d      }| j                  |d       |j                  ||       |j                  ||       | j                  |d       y )Nr   r  ro   r
   r   zN            my_block:
                %"c" = add i32 undef, undef
            zN            my_block:
                %"c" = add i32 %".1", %".2"
            )rM   r   r   rI   r   rC  r0  	Undefinedr   rb   replace_usage)r   rM   r   ry  rz  undef1undef2r   s           r   test_replace_operandz*TestBuildInstructions.test_replace_operand  s    



+,,u%$$Ra(1RZZ^R\\:RZZ^R\\:KK, ! 	 	
"	" ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  d||d       |j                  d||d       |j                  d||d	       |j                  d
||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d
||d       |j                  d||d       |j                  d||d       | j                  t              5  |j                  d||d       d d d        | j                  t              5  |j                  d||d       d d d        | j                  |j                         | j                  |d       y # 1 sw Y   ixY w# 1 sw Y   CxY w)Nr   r  ro   ==r   !=r}  <r~  <=r  >r  >=r  r  r  r  r  r  r  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"
            )rM   r   r   rI   r   icmp_unsignedicmp_signedr   r   r  r  rb   r  s        r   test_integer_comparisonsz.TestBuildInstructions.test_integer_comparisons  s   



+,,u%$$Ra(1dAq#.dAq#.c1a-dAq#.c1a-dAq#.D!Q,D!Q,CAs+D!Q,CAs+D!Q,z* 	3q!T2	3z* 	3q!T2	3,,- ! 		3 	3	3 	3s   	G;GGGc                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  d||d       |j                  d||d       |j                  d||d	       |j                  d
||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d
||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||d       |j                  d||dg d       | j                  |j                         | j                  |d        y )!Nr   r  ro   r  r   r  r}  r  r~  r  r  r  r  r  r  r  r  r  r  r  r  ordur  vwrP  olty)nnanr  r  arcpr  r  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"
            )
rM   r   r   rI   r   fcmp_orderedfcmp_unorderedr  r  rb   r  s        r   test_float_comparisonsz,TestBuildInstructions.test_float_comparisons  s   



+,,u%$$Ra(1T1a-T1a-S!Q,T1a-S!Q,T1a-tQ3/tQ3/sAq#.tQ3/sAq#.tQ3/UAq#.UAq#.uaC0uaC0uaC%L 	 	N,,- ! 	r   c                    | j                  d      }t        j                  t        d      }t        j                  |      }|j
                  j                  d d \  }}|j                  |||dd       | j                  |j                         |j                          | j                  |j                         | j                  |d       y )	Nr   r  Tro   r   )r  r  r  z            my_block:
                %"c" = select arcp nnan i1 true, i32 %".1", i32 %".2"
                unreachable
            )rM   r   rC  int1r   rI   r   selectr  r  unreachable
assertTruerb   )r   rM   r  r   ry  rz  s         r   test_misc_opsz#TestBuildInstructions.test_misc_ops3  s    



+KKd#,,u%$$Ra(1q!Q+;<,,-++, ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  j                  d      }|j                  j                  d      }|j                  t        dd      }|j                  ||       |j                  ||       | j                  |j                         | j                  |d	       y )
Nr   r  ro   b2b3my_phir  r  zj            my_block:
                %"my_phi" = phi fast i32 [%".1", %"b2"], [%".2", %"b3"]
            )rM   r   r   rI   r   rK   phirC   add_incomingr  r  rb   )r   rM   r   ry  rz  bb2bb3r	  s           r   test_phizTestBuildInstructions.test_phiB  s    



+,,u%$$Ra(111$711$7kk%k;C C ,,- ! 	r   c                 *   | j                  d      }t        j                  |      }|j                  j                  d d \  }}}|j                  t        d      }|j                  t        dd      }|j                  t        |d      }d	|_        | j                  |j                  t        j                  t                     |j                  ||      }	| j                  |	j                  t        j                                |j                  ||      }
| j                  |
j                  t        j                                |j                  |d
      }| j                  |j                  t               |j                  ||d      }| j                  |j                  t        j                                |j                  |dd      }| j                  |j                  t               |j                  ||dd      }| j                  |j                  t        j                                |j!                  |ddd      }| j                  |j                  t               t"        s)t        j$                  t        j                         d       }n-t        j$                  t        j                  t              d       }|j                  t        j$                  t        d      |       | j'                  t(              5  |j                  ||       d d d        | j'                  t(              5  |j                  |       d d d        | j'                  t(              5 }|j                  ||       d d d        t"        s8| j                  t+        j,                        d       | j/                  |d       y | j                  t+        j,                        d       | j/                  |d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)Nr   r  r   r   r   r}  )r  rG   r~  r   r  r   )r   r  seq_cstr   )orderingr   r  )r  r   rG   r   z*cannot store i32 to ptr: mismatching typesal                  my_block:
                    %"c" = alloca i32
                    %"d" = alloca i32, i32 42
                    %"e" = alloca double, i32 %".1", align 8
                    store double %".3", ptr %"e"
                    store i32 %".2", ptr %"c"
                    %"g" = load i32, ptr %"c"
                    store i32 %".2", ptr %"c", align 1
                    %"i" = load i32, ptr %"c", align 1
                    store atomic i32 %".2", ptr %"c" seq_cst, align 4
                    %"k" = load atomic i32, ptr %"c" seq_cst, align 4
                    store i32 5, ptr null
                z.cannot store i32 to double*: mismatching typesaw                  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
                    store i32 5, i32* null
                )rM   r   r   rI   r   allocarC   rD   r   r9   r  rE   storerY  loadstore_atomicload_atomicr   rC  r   	TypeErrorr$   r  rb   )r   rM   r   ry  rz  zr   r}  r~  eer  r  r  r  r  r  ptrr  s                     r   test_mem_opsz"TestBuildInstructions.test_mem_opsQ  s   



+,,u%""''+1aNN5sN+NN5rN4NN3QSN1!45]]1a "++-0MM!Q/LLC 'MM!QaM(/LLCqL)'  A	 C/IQSI'.++bnn.5C++bnnU3T:Cbkk%+S1y) 	 MM!Q	 y) 	LLO	 y) 	 RMM!Q	  /S.IKU %  S.MOU % 9	  	 	 		  	 s$   O11O= P	1O:=P	Pc                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  t        j                  t              d      }|j                  |t        j                  t        d      |gd      }| j                  |j                  t        j                  t                     t        s| j                  |d       y | j                  |d       y )	Nr   r  ro   r   r   r}  z                my_block:
                    %"c" = alloca ptr
                    %"d" = getelementptr ptr, ptr %"c", i32 5, i32 %".1"
                z                my_block:
                    %"c" = alloca i32*
                    %"d" = getelementptr i32*, i32** %"c", i32 5, i32 %".1"
                )rM   r   r   rI   r   r  rE   rC   geprC  r9   r  r   rb   r   rM   r   ry  rz  r   r}  s          r   test_gepzTestBuildInstructions.test_gep  s    



+,,u%$$Ra(1NN2>>%0sN;KKBKKq115CK@!67.U %  U % r   c           
      l   | j                  d      }t        j                  |      }|j                  j                  d d \  }}t
        j                         }t        j                  t        |||t        g      }|j                  ||j                         d      }|j                  |dD cg c]  }t        j                  t        |       c}d      }	| j                  |	j                  t        j                  |             t         s| j#                  |d       y | j#                  |d       y c c}w )	Nr   r  ro   r}  r   r   r~  z                my_block:
                    %"d" = bitcast i32 %".1" to ptr
                    %"e" = getelementptr {i64, ptr, ptr, ptr, i64}, ptr %"d", i32 0, i32 3
                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
                )rM   r   r   rI   r   r   r   LiteralStructTyper   bitcastr  rC  rC   r9   r  rE   r   rb   )
r   rM   r   ry  rz  int8ptrlsr}  rP  r~  s
             r   test_gep_castinstrz(TestBuildInstructions.test_gep_castinstr  s     



+,,u%$$Ra(1//#!!5'7GU"KLOOAr}}SO9KK6BaBKKq1BKM!89.U %  U %  Cs   ,!D1
c           
         | j                  d      }t        j                  |      }|j                  j                  d d \  }}d}t
        j                         }t        j                  t        |||t        g      }|j                  ||j                  |      d      }|j                  |dD 	cg c]  }	t        j                  t        |	       c}	d      }
| j                  |
j                  j                  |       | j                  |
j                  t        j                   ||             t"        s| j%                  |d	       y | j%                  |d
       y c c}	w )Nr   r  ro   r   r{  r}  r   r~  z                my_block:
                    %"d" = bitcast i32 %".1" to ptr addrspace(4)
                    %"e" = getelementptr {i64, ptr, ptr, ptr, i64}, ptr addrspace(4) %"d", i32 0, i32 3
                a                  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
                )rM   r   r   rI   r   r   r   r!  r   r"  r  rC  rC   r9   r  r|  rE   r   rb   )r   rM   r   ry  rz  r|  r#  r$  r}  rP  r~  s              r   test_gep_castinstr_addrspacez2TestBuildInstructions.test_gep_castinstr_addrspace  s!    



+,,u%$$Ra(1	//#!!5'7GU"KLOOAr}}y}AOLKK6BaBKKq1BKM))959!MN.U %  U %  Cs   0!E
c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}d}|j                  t        j                  t        |      d      }t        s&| j                  t        |j                        d       n%| j                  t        |j                        d       | j                  |j                  j                  j                  |       |j                  |t        j                  t        d	      |gd
      }| j                  |j                  j                  |       |j                  |t        j                  t        d      gd      }| j                  |j                  j                  |       t        s| j!                  |d       y | j!                  |d       y )Nr   r  ro   r   r{  r   r  zi32 addrspace(4)**r   r}  r   r~  z                my_block:
                    %"c" = alloca ptr addrspace(4)
                    %"d" = getelementptr ptr addrspace(4), ptr %"c", i32 5, i32 %".1"
                    %"e" = getelementptr i32, ptr addrspace(4) %"d", i32 10
                a                  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
                )rM   r   r   rI   r   r  rE   rC   r   r9   r$   r  pointeer|  r  rC  rb   )	r   rM   r   ry  rz  r|  r   r}  r~  s	            r   test_gep_addrspacez(TestBuildInstructions.test_gep_addrspace  sY   



+,,u%$$Ra(1	NN2>>%9ECNP.S[%0S[*>?119=KKBKKq115CK@))95KKBKKr23#K>))95.U %  U % r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}t        j
                  t        t        g      }t        j
                  t        |g      }t        j                  |t        j                  t        d      t        j                  t        d      f      }|j                  |dd      }|j                  ||dd      }	|j                  |	t        j                  t        d	      d
d      }
| j                  |	j                  |       | j                  |
j                  |       |j                  |d      }|j                  |d      }|j                  |dd      }|j                  |d
d      }|j                  |dd      }|j                  |dd      }|j                  ||d
d      }|j                  ||dd      }| j                  |j                  t               | j                  |j                  |       | j                  |j                  t               | j                  |j                  t               | j                  |j                  |       | j                  |j                  |       | j!                  t"              5  |j                  |d       d d d        | j!                  t"              5  |j                  |d       d d d        | j!                  t"              5  |j                  |d       d d d        | j!                  t"              5  |j                  ||d       d d d        | j!                  t"              5  |j                  ||d
       d d d        t$        s| j'                  |d       y | j'                  |d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   exY w)Nr   r  ro   r   Tr   r   r}  Fr   r~  r  r  r  r  )r   r   r  )r   r   r  r  r  )r   r   r   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}}, ptr %"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
                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
                )rM   r   r   rI   r   r!  rC   r   r   rC  extract_valueinsert_valuer9   r  r  r  r   r  r   rb   )r   rM   r   ry  rz  tp_innertp_outerc_innerr   r}  r~  p_outerr  r  r  r  r  r  r  s                      r   test_extract_insert_valuez/TestBuildInstructions.test_extract_insert_value   s|   



+,,u%$$Ra(1''6''x(89++hUA)>)+T4)@)B C !!'13!7  !QS 9  BKKe$<ac J**...6LLsL+!!!QS!1!!!QS!1!!!V#!6!!!V#!6  Aqs 3  AvC 8&*'&**y) 	.!!'1-	. y) 	3!!'62	3 y) 	.!!'1-	. y) 	*  Aq)	* y) 	0  !Q/	0 /U %  U % A	. 	.	3 	3	. 	.	* 	*	0 	0s<   =O-OOO*>O6OOO'*O36O?c                 n   | j                  d      }t        j                  |      }|j                  j                  d d \  }}}}|j                  |t        d      }|j                  |t        d      }|j                  |t        d      }	|j                  |t        d      }
|j                  |
t        d      }|j                  |t        d	      }|j                  |t        d
      }|j!                  |t        d      }|j#                  |t        d      }|j%                  |t        d      }|j'                  |t        j(                  t              d      }|j+                  |t        d      }| j-                  |j.                         t0        s| j3                  |d       y | j3                  |d       y )Nr   r  r   r   r}  r~  fbfcr  r  fdfer  r  r  a                  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 ptr %".4" to i32
                    %"j" = inttoptr i32 %"i" to ptr
                    %"k" = bitcast i32 %".1" to float
                a                  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
                )rM   r   r   rI   r   truncr   zextrC   sextfptruncfltfpextrD   fptouifptosiuitofpsitofpptrtointinttoptrrE   r"  r  r  r   rb   )r   rM   r   ry  rz  far  r   r}  r~  r4  r5  r  r  r6  r7  r  r  r  s                      r   test_cast_opsz#TestBuildInstructions.test_cast_opsM  sn   



+,,u%((--bq11b#MM!TM,LLEL,LLEL,__Rd+]]2sD)NN2uc*NN2tS)^^AsD)^^AsD)S%-Qt 4c:OOAsC(,,-.U %   U % r   c                 x   | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  t        d      }|j                  d||dd      }| j                  |j                  t               t        s| j                  |d       y | j                  |d	       y )
Nr   r  ro   r   r   	monotonicr}  z                my_block:
                    %"c" = alloca i32
                    %"d" = atomicrmw add ptr %"c", i32 %".1" monotonic
                z                my_block:
                    %"c" = alloca i32
                    %"d" = atomicrmw add i32* %"c", i32 %".1" monotonic
                )rM   r   r   rI   r   r  rC   
atomic_rmwr9   r  r   rb   r  s          r   test_atomicrmwz$TestBuildInstructions.test_atomicrmw  s    



+,,u%$$Ra(1NN5sN+uaK='.U %  U % r   c                    | j                  d      }t        j                  |      }|j                  j	                  d      }|j                  |       | j                  |j                         | j                  |d       y )Nr   r  targetzE            my_block:
                br label %"target"
            )	rM   r   r   rI   rK   branchr  r  rb   )r   rM   r   	bb_targets       r   test_branchz!TestBuildInstructions.test_branch  sl    



+,,u%$$77X7F	y!++, ! 	r   c                 r   | j                  d      }t        j                  |      }|j                  j	                  d      }|j                  j	                  d      }|j                  t        j                  t        d      ||       | j                  |j                         | j                  |d       y )Nr   r  b_trueb_falseFza            my_block:
                br i1 false, label %"b_true", label %"b_false"
            )rM   r   r   rI   rK   cbranchrC  r   r  r  rb   )r   rM   r   bb_truebb_falses        r   test_cbranchz"TestBuildInstructions.test_cbranch  s    



+,,u%""5585D##66I6FD%0'8D++, ! 	r   c                    | j                  d      }t        j                  |      }|j                  j	                  d      }|j                  j	                  d      }|j                  t        j                  t        d      ||      }|j                  ddg       | j                  |j                         | j                  |d       | j                  |j                  d	       y )
Nr   r  rP  rQ  Fr   r   zk            my_block:
                br i1 false, label %"b_true", label %"b_false", !prof !0
            zC            !0 = !{ !"branch_weights", i32 5, i32 42 }
            )rM   r   r   rI   rK   rR  rC  r   set_weightsr  r  rb   rl   r@   )r   rM   r   rS  rT  brs         r   test_cbranch_weightsz*TestBuildInstructions.test_cbranch_weights  s    



+,,u%""5585D##66I6F__R[[u5wI
2w++, ! 	 	GNN - 	r   c                    | j                  d      }t        j                  |      }|j                  j	                  d      }|j                  j	                  d      }|j                  t        j                  |j                  |            }|j                  |       |j                  |       | j                  |j                         t        s| j                  |d       y | j                  |d       y )Nr   r  b_1b_2z                my_block:
                    indirectbr ptr blockaddress(@"my_func", %"b_1"), [label %"b_1", label %"b_2"]
                z                my_block:
                    indirectbr i8* blockaddress(@"my_func", %"b_1"), [label %"b_1", label %"b_2"]
                )rM   r   r   rI   rK   branch_indirectBlockAddressadd_destinationr  r  r   rb   )r   rM   r   bb_1bb_2
indirectbrs         r   test_branch_indirectz*TestBuildInstructions.test_branch_indirect  s    



+,,u%222>222>,,OOG,,d35
""4(""4(++,.U % 
 U % r   c                      fd} j                  d      }t        j                  |      }|j                           ||d        j                  d      }t        j                  |      }|j	                  t        d              ||d        j                  d      }t        j                  |      }|j                         }|j                  d|j                  j                  d	              ||d
        j                  d      }t        j                  |      }|j	                  t        d            }|j                  d|j                  j                  d	              ||d       y )Nc                 `    j                  | j                         j                  | |       y r#   )r  r  rb   )rM   expected_irr   s     r   checkz1TestBuildInstructions.test_returns.<locals>.check  s%    OOE//0UK0r   r   r  z;            my_block:
                ret void
            other_blockr   z?            other_block:
                ret i32 5
            r   ry   zD            my_block:
                ret void, !dbg !0
               zE            my_block:
                ret i32 6, !dbg !0
            )	rM   r   r   r   retrC   r   r@   r   )r   rg  rM   r   r  s   `    r   test_returnsz"TestBuildInstructions.test_returns  s>   	1 



+,,u%e  	
 


.,,u%E!He  	 



+,,u%!%!:!:2!>?e  	
 



+,,u%{{58$%!:!:2!>?e  	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  ||      }	|	j                  t        j                  t        d      |       |	j                  t        j                  t        d	      |       |	j                  d|       | j                  |j                         | j                  |d
       y )Nr   r  ro   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"]
            )rM   r   r   rI   r   rK   switchadd_caserC  rC   r  r  rb   )
r   rM   r   ry  rz  	bb_onzerobb_ononebb_ontwobb_elsesws
             r   test_switchz!TestBuildInstructions.test_switch  s   



+,,u%$$Ra(1$$77X7F	##66G6D##66G6D""55;5G^^Aw'
BKKq)95
BKKq)84
Ax ++, ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}t        j
                  t        t        t        f      }t        j
                  t        t        fd      }t        j
                  t        t        t        f      }t        j                  |j                  j                  |d      }t        j                  |j                  j                  |d      }	t        j                  |j                  j                  |d      }
|j                  |||fd	       |j                  |	||fd
       |j                  |
||fd       |j                  |||fdd       |j                  |||fd      }|j                  j                  d       |j                  |||fdd       |j                  |||fdd       |j                  |||fdd       |j                  |||fdd       |j                  |||fdd       |j                  |||fdd       | j                  |d       y ) Nr   r  ro   Tr   r  r  r  res_fres_gres_h
res_f_fastfastcc)cconvres_f_readonlyreadonlyres_fastr  )fastmathres_nnan_ninf)r  r  res_noinliner   )attrsres_alwaysinliner   res_noinline_ro)r   r  res_convergentr   aT          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
            %"res_convergent" = call float @"f"(i32 %".1", i32 %".2") convergent
        )rM   r   r   rI   r   rB   r<  rC   rD   hlfrF   r@   r[  r   r   rb   )r   rM   r   ry  rz  tp_ftp_gtp_hr  r  r  r  s               r   	test_callzTestBuildInstructions.test_call
  s   



+,,u%$$Ra(1sUEN3sUHd;sUEN3KK((//s;KK((//s;KK((//s;QA(QA(QA(QAH= a!Q1AB!!%%j1QA
V<QA:JKQAjAQA 2.IQA 1 :F 	GQA 0E ! 	r   c                    | j                  d      }t        j                  |      }t        j                  t        j                         t        j
                         gdz        }t        j                  |j                  |d      }|j                  t        d      }|j                  j                  d      }|j                  ||||f       t        s| j                  |d       y	| j                  |d       y	)
z9
        Function calls with metadata arguments.
        r   r  r   zllvm.dbg.declarery  ry   z                my_block:
                    %"a" = alloca i32
                    call void @"llvm.dbg.declare"(metadata ptr %"a", metadata !0, metadata !0)
                z                my_block:
                    %"a" = alloca i32
                    call void @"llvm.dbg.declare"(metadata i32* %"a", metadata !0, metadata !0)
                N)rM   r   r   rB   rY  MetaDataTyperF   r@   r  rC   r   r[  r   rb   )r   rM   r   dbg_declare_tydbg_declarery  rz  s          r   test_call_metadataz(TestBuildInstructions.test_call_metadata0  s     



+,,u%9J8Ka8OPkkNN  NN5sN+NN''+[1a),.U %  U % r   c                    | j                  d      }t        j                  |      }t        j                  t        j                         t
        j                         t
        t
        j                         f      }t        j                  |j                  j                  |d      }|j                  d   j                  d       |j                  t
        d      }|j                  t
        d      }|j                  ||t        j                  t
        d      |fd	d
d       t        s| j!                  |d       y | j!                  |d       y )Nr   r  funr   sretretvalotherr   r  r   r   r   ro   	arg_attrsz            my_block:
                %"retval" = alloca i32
                %"other" = alloca i32
                call void @"fun"\(ptr noalias sret(\(i32\))? %"retval", i32 42, ptr noalias %"other"\)
            z            my_block:
                %"retval" = alloca i32
                %"other" = alloca i32
                call void @"fun"\(i32\* noalias sret(\(i32\))? %"retval", i32 42, i32\* noalias %"other"\)
            )rM   r   r   rB   rY  rC   r   rF   rI   r@   r   r   r  r[  rC  r   rd   )r   rM   r   fun_tyr  r  r  s          r   test_call_attributesz*TestBuildInstructions.test_call_attributesK  s   



+,,u%KKME,,.u7G7G7IJLkk'**1165A!!&)H5u73R[[+U3& 	 	
 /""5 +  ""5 + r   c                 X   | j                  d      }t        j                  |      }t        j                  t        j                         d      }t        j
                  |j                  j                  |d      }|j                  |d       |j                  |dd       |j                  |dd       |j                  |dd       |j                  |dd	       |j                  |dd
       |j                  |dg        |j                  |dd       | j                  |d       y )Nr   r  ry   my_funF)tailTr  notailmusttailznot a markeraI          my_block:
            call void @"my_fun"()
            call void @"my_fun"()
            tail call void @"my_fun"()
            tail call void @"my_fun"()
            notail call void @"my_fun"()
            musttail call void @"my_fun"()
            call void @"my_fun"()
            tail call void @"my_fun"()
        )
rM   r   r   rB   rY  rF   rI   r@   r[  rb   r   rM   r   r  r  s        r   test_call_tailz$TestBuildInstructions.test_call_tailk  s    



+,,u%3kk'**1168DS"S"5)S"4(S"6*S"8,S":.S"2&S">2 
! 
	r   c                 t   | j                         }t        j                  |      }t        j                  t        j                         d      }t        j
                  |j                  j                  |d      }| j                  t              5  |j                  |dddi       d d d        y # 1 sw Y   y xY w)Nry   r  r   r  r  )rM   r   r   rB   rY  rF   rI   r@   r   r   r[  r  s        r   test_invalid_call_attributesz2TestBuildInstructions.test_invalid_call_attributes  s    

,,u%3kk'**1165Az* 	9LLbQKL8	9 	9 	9s   B..B7c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}t        j
                  t        t        t        f      }t        j                  |j                  j                  |d      }|j                  j                  d      }|j                  j                  d      }|j                  |||f||d       | j                  |d       y )	Nr   r  ro   r  normalunwindrz  z            my_block:
                %"res_f" = invoke float @"f"(i32 %".1", i32 %".2")
                    to label %"normal" unwind label %"unwind"
            )rM   r   r   rI   r   rB   r<  rC   rF   r@   rK   invokerb   )	r   rM   r   ry  rz  r  r  	bb_normal	bb_unwinds	            r   test_invokez!TestBuildInstructions.test_invoke  s    



+,,u%$$Ra(1sUEN3KK((//s;$$77X7F	$$77X7F	q1a&)Y@ ! 	r   c                 (   | j                  d      }t        j                  |      }t        j                  t        j                         t
        j                         t
        t
        j                         f      }t        j                  |j                  j                  |d      }d|_
        |j                  d   j                  d       |j                  t
        d      }|j                  t
        d      }|j                  j                  d	      }|j                  j                  d
      }|j                  ||t        j                   t
        d      |f||dddddd       t"        s| j%                  |d       y | j%                  |d       y )Nr   r  r  r~  r   r  r  r  r  r  r   r  r   r  r   r  )r  r  r  r  a+              my_block:
                %"retval" = alloca i32
                %"other" = alloca i32
                invoke fast fastcc void @"fun"\(ptr noalias sret(\(i32\))? %"retval", i32 42, ptr noalias %"other"\) noinline
                    to label %"normal" unwind label %"unwind"
            a/              my_block:
                %"retval" = alloca i32
                %"other" = alloca i32
                invoke fast fastcc void @"fun"\(i32\* noalias sret(\(i32\))? %"retval", i32 42, i32\* noalias %"other"\) noinline
                    to label %"normal" unwind label %"unwind"
            )rM   r   r   rB   rY  rC   r   rF   rI   r@   calling_conventionr   r   r  rK   r  rC  r   rd   )	r   rM   r   r  r  r  r  r  r  s	            r   test_invoke_attributesz,TestBuildInstructions.test_invoke_attributes  sY   



+,,u%KKME,,.u7G7G7IJLkk'**1165A!)!!&)H5u73$$77X7F	$$77X7F	R[[+U3& 	 	
 /""5 +  ""5 + r   c           
         | j                  d      }t        j                  |      }|j                  t        j                  t
        t        j                         g      d      }t        j                  |j                  j                  t        j                         d      }d|_        |j                  t        j                  |             |j                  t        j                  t        j                  t        j                   |j"                  d      |g                   |j%                  |       t&        s| j)                  |d       y | j)                  |d       y )	Nr   r  lp_ZTIiTr   z                my_block:
                    %"lp" = landingpad {i32, ptr}
                        catch ptr @"_ZTIi"
                        filter [1 x ptr] [ptr @"_ZTIi"]
                    resume {i32, ptr} %"lp"
                z                my_block:
                    %"lp" = landingpad {i32, i8*}
                        catch i8** @"_ZTIi"
                        filter [1 x i8**] [i8** @"_ZTIi"]
                    resume {i32, i8*} %"lp"
                )rM   r   r   
landingpadr!  rC   r   r   rA  rI   r@   r  
add_clauseCatchClauseFilterClauserC  	ArrayTyper  resumer   rb   )r   rM   r   r  int_typeinfos        r   test_landingpadz%TestBuildInstructions.test_landingpad  s   



+,,u% 4 4e6:oo6G6I !JKOQ(()9)9)@)@)-):GE'+$
bnn\23
boobkk",,q3"$0>'3 4 	5r.U %  U % r   c                     | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  d||d      }|j                  |       | j                  |d       y )Nr   r  ro   r  r   z            my_block:
                %"c" = icmp sgt i32 %".1", %".2"
                call void @"llvm.assume"(i1 %"c")
            )rM   r   r   rI   r   r  r   rb   r  s         r   test_assumez!TestBuildInstructions.test_assume  sv    



+,,u%$$Ra(1Q4q ! 	r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}d|_        d|_        t        j                  |j                  d      }t        j                  |t        j                        }t        j                  d      }|j                  || |d      d      }|j                  || |d	      d
      }| j                  |d       |j                  d      }|j                  |       |j                  |       t        j                  |d	dg      }|j!                  |||d       | j                  |d       |j                  d      }|j                  |       |j                  |       |j#                  ||d       | j                  |d       |j                  d      }|j                  |       |j                  |       |j%                  | |d      d      }	|j%                  | |d	      d      }
| j                  |d       |j'                  |j#                  |	|
             | j)                  |j*                         y )Ninsert_blockr  ro   ry  rz  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
            )rM   r   r   rI   r   rG   
VectorTyper  rC  r  r0  insert_elementrb   rK   rL  position_at_endshuffle_vectorr   extract_elementrj  r0   r@   )r   rM   r   ry  rz  vectyvecidxtymaskr   r}  s              r   test_vector_opsz%TestBuildInstructions.test_vector_ops  s/   


/,,u%$$Ra(1affa(kk%.

2$$S!U1XF$C$$S!U1XF$C ! 	 **?;u&{{51a&)sCF; ! 	
 **;7u&C5) ! 	
 **?;u&##Cq#>##Cq#> ! 	 	GKK1%&W^^,r   c                     | j                  d      }t        j                  |      }t        j                  t        d      }|j                  |d      }|j                  |       | j                  |d       y )Nr   r  r   r   z}            my_block:
                %"c" = call i64 @"llvm.bitreverse.i64"(i64 5)
                ret i64 %"c"
            )rM   r   r   rC  r   
bitreverserj  rb   r   rM   r   ry  r   s        r   test_bitreversez%TestBuildInstructions.test_bitreverse+  se    



+,,u%KKq!qs+A ! 	r   c                 J   | j                  d      }t        j                  |      }t        j                  t        d      }| j                  t              5 }|j                  |d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   r   #expected an integer type, got float)rM   r   r   rC  r<  r   r  r  r'   r$   r  r   rM   r   ry  raisess        r   test_bitreverse_wrongtypez/TestBuildInstructions.test_bitreverse_wrongtype7  s    



+,,u%KKQy) 	,Vqs+	,1  !	#	, 	,   BB"c                    | j                  d      }t        j                  |      }| j                  t              5 }|j                  dd        d d d        | j                  dt        j                               | j                  t              5 }|j                  d d       d d d        | j                  dt        |j                               |j                  dd        |j                  dd       |j                  d	d       |j                  d
       |j                          | j                  |d       y # 1 sw Y   xY w# 1 sw Y   xY w)Nr   r  rG  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
            )rM   r   r   r   r   fencer'   r$   r  r   rb   )r   rM   r   r  s       r   
test_fencez TestBuildInstructions.test_fenceB  s   



+,,u%z* 	-fMM+t,	-3  !	# z* 	-fMM$,	-.  !	# 	i&i0i0i  ! 		- 	-
	- 	-s   D<E<EEc                 .   | j                  d      }t        j                  |      }| j                  t              5  |j                  d       d d d        |j                  d       |j                          | j                  |d       y # 1 sw Y   =xY w)Nr   r  zso
many linesz
my commentzX            my_block:
                ; my comment
                ret void
            )rM   r   r   r   AssertionErrorcommentr   rb   r   rM   r   s      r   test_commentz"TestBuildInstructions.test_comment]  s}    



+,,u%~. 	.OO,-	.% ! 			. 	.s   BBc                     | j                  d      }t        j                  |      }t        j                  t        d      }|j                  |d      }|j                  |       | j                  |d       y )Nr   r  r   r   zx            my_block:
                %"c" = call i32 @"llvm.bswap.i32"(i32 5)
                ret i32 %"c"
            )rM   r   r   rC  rC   bswaprj  rb   r  s        r   
test_bswapz TestBuildInstructions.test_bswapj  b    



+,,u%KKq!MM!#M&A ! 	r   c                     | j                  d      }t        j                  |      }t        j                  t        d      }|j                  |d      }|j                  |       | j                  |d       y )Nr   r  r   r   zx            my_block:
                %"c" = call i16 @"llvm.ctpop.i16"(i16 5)
                ret i16 %"c"
            )rM   r   r   rC  int16ctpoprj  rb   r  s        r   
test_ctpopz TestBuildInstructions.test_ctpopv  r  r   c                 (   | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t
        d      }|j                  ||d      }|j                  |       | j                  |d       y )Nr   r  r   r   r   z}            my_block:
                %"c" = call i16 @"llvm.ctlz.i16"(i16 5, i1 1)
                ret i16 %"c"
            )	rM   r   r   rC  r  r   ctlzrj  rb   r  s         r   	test_ctlzzTestBuildInstructions.test_ctlz  t    



+,,u%KKq!KKa LLACL(A ! 	r   c                     | j                  d      }t        j                  |      }t        j                  t        d      }|j                  |d      }|j                  |       | j                  |d       y )Nr   r  g      @rz  z            my_block:
                %"b" = call i16 @"llvm.convert.to.fp16.f32"(float 0x4014000000000000)
                ret i16 %"b"
            )rM   r   r   rC  r<  convert_to_fp16rj  rb   r  s        r   test_convert_to_fp16_f32z.TestBuildInstructions.test_convert_to_fp16_f32  se    



+,,u%KKS!##AC#0A ! 	r   c                 J   | j                  d      }t        j                  |      }t        j                  t        d      }| j                  t              5 }|j                  |d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   rz  expected a float type, got i16)rM   r   r   rC  r  r   r  r  r'   r$   r  r  s        r   "test_convert_to_fp16_f32_wrongtypez8TestBuildInstructions.test_convert_to_fp16_f32_wrongtype  s    



+,,u%KKq!y) 	1V##AC#0	1,  !	#	1 	1r  c                     | j                  d      }t        j                  |      }t        j                  t        d      }|j                  |dt              }|j                  |       | j                  |d       y )Nr   r  r   rz  rG   toz            my_block:
                %"b" = call float @"llvm.convert.from.fp16.f32"(i16 5)
                ret float %"b"
            )	rM   r   r   rC  r  convert_from_fp16r<  rj  rb   r  s        r   test_convert_from_fp16_f32z0TestBuildInstructions.test_convert_from_fp16_f32  sg    



+,,u%KKq!%%acc%:A ! 	r   c                 J   | j                  d      }t        j                  |      }t        j                  t        d      }| j                  t              5 }|j                  |d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r        @rz  zexpected a float return typerM   r   r   rC  r<  r   r  r  r'   r$   r  r  s        r   !test_convert_from_fp16_f32_notypez7TestBuildInstructions.test_convert_from_fp16_f32_notype  s    



+,,u%KKS!y) 	3V%%ac%2	3*  !	#	3 	3r  c                 T   | j                  d      }t        j                  |      }t        j                  t        d      }| j                  t              5 }|j                  |dt               d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r  rz  r  zexpected an i16 type, got floatr  r  s        r   $test_convert_from_fp16_f32_wrongtypez:TestBuildInstructions.test_convert_from_fp16_f32_wrongtype  s    



+,,u%KKS!y) 	;V%%acc%:	;-  !	#	; 	;   BB'c                 T   | j                  d      }t        j                  |      }t        j                  t        d      }| j                  t              5 }|j                  |dt               d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r  rz  r  r  )rM   r   r   rC  r<  r   r  r  r  r'   r$   r  r  s        r   %test_convert_from_fp16_f32_wrongtype2z;TestBuildInstructions.test_convert_from_fp16_f32_wrongtype2  s    



+,,u%KKS!y) 	=V%%ace%<	=,  !	#	= 	=r  c                 (   | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t
        d      }|j                  ||d      }|j                  |       | j                  |d       y )Nr   r  r   r   r   z}            my_block:
                %"c" = call i64 @"llvm.cttz.i64"(i64 5, i1 1)
                ret i64 %"c"
            )	rM   r   r   rC  r   r   cttzrj  rb   r  s         r   	test_cttzzTestBuildInstructions.test_cttz  r  r   c                    | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t
        d      }| j                  t              5 }|j                  ||d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   r   r   zexpected an i1 type, got i32)rM   r   r   rC  r   rC   r   r  r  r'   r$   r  r   rM   r   ry  rz  r  s         r   test_cttz_wrongflagz)TestBuildInstructions.test_cttz_wrongflag  s    



+,,u%KKq!KKq!y) 	)VLLACL(	)*  !	#	) 	)   1B44B=c                    | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t
        d      }| j                  t              5 }|j                  ||d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   r   r   r  )rM   r   r   rC  r<  r   r   r  r  r'   r$   r  r  s         r   test_cttz_wrongtypez)TestBuildInstructions.test_cttz_wrongtype  s    



+,,u%KKQKKa y) 	)VLLACL(	)1  !	#	) 	)r  c                 ^   | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t        d      }t        j                  t        d      }|j                  |||d      }|j                  |       | j                  |d       y )Nr   r  r   r   ro   fmaz            my_block:
                %"fma" = call float @"llvm.fma.f32"(float 0x4014000000000000, float 0x3ff0000000000000, float 0x4000000000000000)
                ret float %"fma"
            )rM   r   r   rC  r<  r  rj  rb   )r   rM   r   ry  rz  r   r  s          r   test_fmazTestBuildInstructions.test_fma  s    



+,,u%KKQKKQKKQkk!Qk.C ! 	r   c                    | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t        d      }t        j                  t        d      }| j                  t              5 }|j                  |||d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   r   ro   r  z(expected an floating point type, got i32)rM   r   r   rC  rC   r   r  r  r'   r$   r  r   rM   r   ry  rz  r   r  s          r   test_fma_wrongtypez(TestBuildInstructions.test_fma_wrongtype  s    



+,,u%KKq!KKq!KKq!y) 	-VKK1aeK,	-6  !	#	- 	-   CCc                    | j                  d      }t        j                  |      }t        j                  t        d      }t        j                  t
        d      }t        j                  t        d      }| j                  t              5 }|j                  |||d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr   r  r   r   ro   r  z7expected types to be the same, got float, double, float)rM   r   r   rC  r<  rD   r   r  r  r'   r$   r  r  s          r   test_fma_mixedtypesz)TestBuildInstructions.test_fma_mixedtypes  s    



+,,u%KKQKKQKKQy) 	-VKK1aeK,	-E  !	#	- 	-r  c                 N    d }dD ]  }t        j                   ||              y )Nc                    t        j                  t        j                  d      t        j                  d      j                         t        j                  d      g      }t        j                         }t        j
                  ||d      }|j                         }|j                         }|j                         }t        j                         }|j                  |       |j                  |       |j                  |       |j                  t        j                  d            }|j                  t        j                  |j                  d      |       |j                  t        j                  d            }	|	j                  t        j                  |	j                  d      |       |j                  d   j                  |        |j!                  |j                  d   |g      }
|j#                  |
      }|j%                  |	|      }|	j                  ||       |j%                  |t        j                  |j                  d            }|j                  ||       |j'                  d||j                  d         }|j)                  |||       |j                  |       |j+                  |       t-        |      S )Nr
   r  r  r   r   r  )r   rB   r0  r   r>   rF   rK   r   r  rL  r	  r
  rC  r  r   r   r  r  r   r  rR  rj  r$   )	attr_namerH   r@   rL   bb_entrybb_loopbb_exitr   indexaccumr  valueaddedindexp1conds                  r   gen_codez;TestBuildInstructions.test_arg_attributes.<locals>.gen_code!  s    ??2::b>BJJrN4M4M4O46JJrN4D EDYY[F;;vt%8D..0H--/G--/GllnG##H-NN7###G,KK

2/Er{{5::q98DKK

2/Er{{5::q98DIIaL&&y1++diilUG4CLL%EKKu-Eug.kk%UZZ)CDGw0((gtyy|DDOOD'73##G,KKv;r   )byrefbyvalelementtypeimmarginallocainregnestr   	nocapturenofreer   noundefpreallocatedreturnedsignext
swiftasync
swifterror	swiftselfr   )r-   r.   )r   r(  r  s      r   test_arg_attributesz)TestBuildInstructions.test_arg_attributes   s.    &	P
 	5I,  34-	5r   N)<ru   rv   rw   rx   maxDiffr  r  r  r  r  r  r  r  r  r  r  r  r  r%  r'  r*  r2  rE  rI  rN  rU  rY  rc  rk  rx  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  r  r  r9  ry   r   r   r  r  I  s.    G=/b($#J+ZHT*04<KZ0d(	
 *&P&$L6@69(T:
8-t
	#6


	#
	#	#	#
#
###?5r   r  c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)TestBuilderMiscz=
    Test various other features of the IRBuilder class.
    c                    | j                  d      }t        j                  |      }| j                  |j                  |j
                         | j                  |j                  t        j                         | j                  |j                  |j
                  j                         | j                  |j                  t        j                         y )Nr  r  )
rM   r   r   r7   rI   r  assertIsInstancerF   r@   r>   r  s      r   r  zTestBuilderMisc.test_attributesh  s    


(,,u%g&&5g..<gnnell&9&9:gnnbii8r   c                    | j                  d      }t        j                  |      }|j                  j                  d d \  }}|j                  ||d       |j                  d      }|j                  |      5  |j                  ||d       |j                         5  |j                  ||d       d d d        |j                  ||d       |j                  |       d d d        |j                  ||d	       |j                  |      5  |j                  ||d
       d d d        | j                  |d       | j                  |d       y # 1 sw Y   xY w# 1 sw Y   zxY w# 1 sw Y   FxY w)Nr   r  ro   r   r   r}  r~  r  r  r  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"
            )rM   r   r   rI   r   r   rK   
goto_blockr  goto_entry_blockr   r  rL  r  r  rb   )r   rM   r   ry  rz  bb_news         r   test_goto_blockzTestBuilderMisc.test_goto_blockp  sP   



+,,u%$$Ra(1Aq#+++7' 	#LLAs#))+ 'Aq#&'LLAs#NN6"	# 	Aq#' 	$LLAs#	$ ! 	 	 " 	' '	# 	#	$ 	$s0   :$EE2,E
E#E	EE #E,c                 H   | j                  d      }t        j                  |      }t        j                  t        d      }|j                  ||d      }|j                  |      5 }|j                  ||d       d d d        | j                  |j                          |j                  ||d      }|j                  |      5  |j                  ||d       |j                  |       d d d        | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   2xY w)	Noner  r   ry  rz  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:
            )rM   r   r   rC  r   r   if_thenr7   rL  rs   rI   )r   rM   r   r  ry  bbendr   s          r   test_if_thenzTestBuilderMisc.test_if_then  s    


&,,u%KKa KK1c"__Q 	#5KK1c"	# 	gmmU+KK1c"__Q 	"KK1c"NN5!	" 	W-- 0 		# 	#
	" 	"s   &D%DDD!c                    | j                  d      }t        j                  |      }t        j                  t        d      }|j                  ||d      }|j                  |      5  |j                  ||d      }|j                  |      5  |j                  ||d       d d d        d d d        |j                          | j                  |j                  d       y # 1 sw Y   >xY w# 1 sw Y   BxY w)NrE  r  r   ry  rz  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"
            )
rM   r   r   rC  r   r   rF  r   rs   rI   )r   rM   r   r  ry  rz  s         r   test_if_then_nestedz#TestBuilderMisc.test_if_then_nested  s    


&,,u%KKa KK1c"__Q 	'Aq#&A# 'Aq#&'	' 	W-- 0 	' '	' 	's$   &%C(CC(C%	!C((C1c           	         d}| j                  |      }t        j                  |      }t        j                  t        d      }|j                  ||d      }|j                  |      5  |j                  ||d      }|j                  |      5  |j                  ||d       d d d        d d d        |j                          | j                  |j                  dj                  ||d d d	z   
             y # 1 sw Y   UxY w# 1 sw Y   YxY w)NPLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongr  r   ry  rz  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)rM   r   r   rC  r   r   rF  r   rs   rI   r   )r   rN  rM   r   r  ry  rz  s          r   test_if_then_long_labelz'TestBuilderMisc.test_if_then_long_label  s     




+,,u%KKa KK1c"__Q 	'Aq#&A# 'Aq#&'	' 	W-- 0 *JsOd4JK	M' '	' 	's$   (%DC5!D5C>	:DD
c                       fd} |d      } j                  |j                  d        |d      } j                  |j                  d       y )Nc                     j                  d      }t        j                  |      }t        j                  t        d      }|j                  ||       5  	 d d d        j                  |d       |S # 1 sw Y   xY w)NrE  r  r   likelyzp                one:
                    br i1 0, label %"one.if", label %"one.endif", !prof !0
                )rM   r   r   rC  r   rF  rb   )rT  rM   r   r  r   s       r   rg  z2TestBuilderMisc.test_if_then_likely.<locals>.check  su    JJEJ*Ell5)GD!$A62 U %  N s   A44A=TC            !0 = !{ !"branch_weights", i32 99, i32 1 }
            FC            !0 = !{ !"branch_weights", i32 1, i32 99 }
            rl   r@   r   rg  r   s   `  r   test_if_then_likelyz#TestBuilderMisc.test_if_then_likely  sP    
	 +GNN - 	 ,GNN - 	r   c                    | j                  d      }t        j                  |      }t        j                  t        d      }|j                  ||d      }|j                  |      5 \  }}|5  |j                  ||d       d d d        |5  |j                  ||d       d d d        d d d        |j                  |      5 \  }}|5  |j                  |       d d d        |5  |j                          d d d        d d d        | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   mxY w# 1 sw Y   ^xY w# 1 sw Y   bxY w)NrE  r  r   ry  rz  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:
            )rM   r   r   rC  r   r   if_elserL  r   rs   rI   )r   rM   r   r  ry  thenrp  s          r   test_if_elsezTestBuilderMisc.test_if_else  sI   


&,,u%KKa KK1c"__Q 	'#4D) 'Aq#&' 'Aq#&'	' __Q 	##4D) &u%& #  "#	# 	W-- 0 	' '' '	' 	'& &# #	# 	#sx   &D7,D 
D7
D+D7?EE
E!E2ED(	$D7+D4	0D77E E	EE	EE$c                       fd} |d      } j                  |j                  d        |d      } j                  |j                  d       y )Nc                    j                  d      }t        j                  |      }t        j                  t        d      }|j                  ||       5 \  }}|5  |j                  |       d d d        |5  |j                          d d d        d d d        j                  |j                  d       |S # 1 sw Y   JxY w# 1 sw Y   ;xY w# 1 sw Y   ?xY w)NrE  r  r   rS  z                one:
                    br i1 0, label %"one.if", label %"one.else", !prof !0
                one.if:
                    br label %"one"
                one.else:
                    ret void
                one.endif:
                )
rM   r   r   rC  r   r[  rL  r   rs   rI   )rT  rM   r   r  r\  rp  r   s         r   rg  z2TestBuilderMisc.test_if_else_likely.<locals>.check	  s    JJEJ*Ell5)GD!$A62 '6GtY *NN5)* '$$&''
   !1!1 4  N* *' '' 's<   CB7.
C8C	C7C 	<CC	CCTrU  FrV  rW  rX  s   `  r   test_if_else_likelyz#TestBuilderMisc.test_if_else_likely	  sP    	& +GNN - 	 ,GNN - 	r   c                 T   | j                         }t        j                         }t        j                  t        d      }|j                  d      }|j                  d      }|j                  d      }|j                  |       |j                  ||d       |j                  |       |j                  ||d       |j                  ||d       |j                  |       |j                  ||d	      }|j                  ||d
       |j                  |       |j                  ||d      }|j                  |       |j                  ||d       |j                  |       |j                  ||d       | j                  |d       | j                  |d       | j                  |d       y)zI
        Test IRBuilder.position_{before,after,at_start,at_end}.
        r   rE  r  twothreery  r  r  r  r  rz  r  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)rI   r   r   rC  rC   rK   position_at_startr   r  position_afterposition_beforerb   )	r   rL   r   r  bb_onebb_twobb_threer  rz  s	            r   test_positioningz TestBuilderMisc.test_positioning7	  s~    }},,.KKq!((e(4((e(4***8!!&)Aq#'Aq#Aq#!!&)KK1c"Aq#'KK1c"q!Aq#"Aq# " 	 	 " 	 	 $ 	r   c                    | j                         }t        j                         }|j                  d      }|j	                  |       t        j
                  t        d      }|j                  ||d      }|j                         }| j                  |j                         |j                  |       | j                  |j                         |j                  ||d      }|j                  ||d      }|j                  |       |j                          | j                  |j                         | j                  |d       y )Nentryr  i  ry  rz  r   z            entry:
                %"a" = add i32 1234, 1234
                %"b" = mul i32 %"a", %"a"
                ret void
        )rI   r   r   rK   r  rC  rC   r   r   r  r  remover  r  rb   )	r   rL   r   blkr  ry  retvoidrz  r   s	            r   test_instruction_removalz(TestBuilderMisc.test_instruction_removalg	  s    }},,.%%7%3$KKt$KK1c"""$))*w**+KK1c"KK1c"q))*  	r   c                 <   | j                  d      }t        j                  |      }|j                  j	                  g       |_        |j                  t        j                  t              d       t        s| j                  |d       y | j                  |d       y )Nr   r  r   zY                my_block:
                    %"c" = alloca ptr, !dbg !0
                zZ                my_block:
                    %"c" = alloca i32*, !dbg !0
                )rM   r   r   r@   r   debug_metadatar  rE   rC   r   rb   r  s      r   test_metadatazTestBuilderMisc.test_metadata~	  s    



+,,u%!(!<!<R!@r~~e,37.U % 
 U % r   N)ru   rv   rw   rx   r  rC  rH  rJ  rP  rY  r]  r`  rj  rp  rs  ry   r   r   r<  r<  c  sA    9:<6M6*"H:.`.r   r<  c                   l    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)	TestTypesc                     t        |t        j                        r'|j                  }t        |t        j                        r't        |t        j                         S r#   )
isinstancer   rE   r)  	LabelType)r   tys     r   has_logical_equalityzTestTypes.has_logical_equality	  s=    R^^,B R^^,b",,///r   c                    t        j                         }t        j                         t        j                         t        j                  t
        t        t        f      t        j                  t
        t        f      t        j                  t
        t        fd      t        j                  t        t        f      t
        t        t        t        t        t        j                  t        d      t        j                  t        d      t        j                  t        d      t        j                  t
        t        f      t        j                  t        t
        f      |j                  d      |j                  d      g}||D cg c]A  }t        |t         j                  t         j                  f      st        j                  |      C c}z  }|S c c}w )z3
        A bunch of mutually unequal types
        Tr   r   r   MyType1MyType2)r   Contextrx  rY  rB   r   r   rC   r<  rD   r  r!  get_identified_typerw  rE   )r   contextr   tps       r   assorted_typeszTestTypes.assorted_types	  s5   
 **,LLNBKKMOOD4,/w1OOOD4'48OOD4'*$sCLLa ",,sA"6S!8L  $.0D0DdFJFL 1M''	2''	2
 	u F#Bbll(CD ..$ F 	F Fs   AGc                     | j                         }|D ]7  }| j                  |      }| j                  |      s&| j                  ||       9 y r#   )r  r<   rz  r9   )r   r   ry  newtys       r   r   zTestTypes.test_pickling	  sM    ##% 	,B004E((,  +	,r   c                    | j                         }t        j                  ||      D ]8  \  }}||us| j                  ||k(  ||f       | j	                  ||k7  ||f       : |D ]  }t        j
                  |      }| j                  |      r/| j	                  ||k(  ||f       | j                  ||k7  ||f       X| j                  ||k(  ||f       | j	                  ||k7  ||f        y r#   )r  	itertoolsproductr  r  copyrz  )r   r   ry  rz  r  r  s         r   test_comparisonszTestTypes.test_comparisons	  s    ##%%%eU3 	0DAqz  a!Q0QA/	0
  	:BIIbME((,eb%[9  ur5k:  ur5k:eb%[9	:r   c           	          t        j                         t        j                  d      t        j                  t              t        j                  t        d      t        j                  t              t        j                  t        d      d} fd} fd}|j	                         D ]D  } j                         D ]&  }t        |t         j                        r |||       (  |||       F  ||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          y )Nr   r{  )op_a0op_a1tp_i1_a0tp_i1_a1tp_i8_a0tp_i8_a1c                     j                  | |k(  | |f       j                  || k(  || f       j                  | |k7  | |f       j                  || k7  || f       y r#   )r  r  ptr1ptr2r   s     r   	assert_eqz1TestTypes.test_ptr_comparisons.<locals>.assert_eq	  s_    OODDL4,7OODDL4,7TT\D$<8TT\D$<8r   c                     j                  | |k(  | |f       j                  || k(  || f       j                  | |k7  | |f       j                  || k7  || f       y r#   )r  r  r  s     r   	assert_nez1TestTypes.test_ptr_comparisons.<locals>.assert_ne	  s_    TT\D$<8TT\D$<8OODDL4,7OODDL4,7r   r  r  r  r  r  r  )r   rE   r   r   valuesr  rw  )r   ptrsr  r  r  r  s   `     r   test_ptr_comparisonszTestTypes.test_ptr_comparisons	  s    ^^%^^a0t,tq9t,tq9
	9	8 ;;= 	 C,,. *!%8c5)* c3	  	$w-g/$w-j!12$w-j!12$w-j!12$w-j!12$w-j!12$w-j!12$w-j!12$w-j!12$z"D$45$z"D$45$z"D$45$z"D$45$z"D$45$z"D$45r   c                    t        j                         }t        j                  d      }| j                  |j                         | j                  |j                         | j	                  t        |      d       | j	                  t        |      d       |j                         }|j                         }|j                  d      }| j                  |j                         | j                  |j                         | j                  |j                         | j	                  t        |      d       | j	                  t        |      d       | j	                  t        |      d       t        j                  d      j                         }t        j                  d      j                  d      }| j                  |j                          | j                  |j                          t        r7| j	                  t        |      d       | j	                  t        |      d	       n6| j	                  t        |      d       | j	                  t        |      d       |j                         }|j                         }	|j                  d      }
| j                  |j                          | j                  |	j                          | j                  |
j                          t        rR| j	                  t        |      d
       | j	                  t        |	      d       | j	                  t        |
      d       y | j	                  t        |      d       | j	                  t        |	      d       | j	                  t        |
      d       y )Nro   r{  r  zptr addrspace(2)r   zptr addrspace(3)r
   i32*zi32 addrspace(2)*i32**zi32 addrspace(2)**zi32 addrspace(2)* addrspace(3)*)	r   rE   r  	is_opaquer9   r$   r   r0  r   )r   r  r  ptr_ptrptr2_ptr	ptr2_ptr3tptrtptr2tptr_ptr	tptr2_ptr
tptr2_ptr3s              r   test_pointerszTestTypes.test_pointers	  s   nn~~*&'S5)T$67 .."??$OOaO0	))***+	++,Wu-X.Y);< zz"~((*

2))A)6DNN*+EOO+,*SY/SZ)<=SY.SZ);< ??$$$&	%%%2
H.../I///0J0001*S]G4S^-ABS_.OPS]E2S^U3S_.@Ar   c                 j   | j                  t        j                         j                  d       | j                  t        j                  d      j                  d       t        sh| j                  t        j                  t
              j                  d       | j                  t        j                  t
        d      j                  d       y | j                  t        j                  t
              j                  d       | j                  t        j                  t
        d      j                  d       y )Np0r   r{  p1p0i1p1i1)r9   r   rE   intrinsic_namer   r   r?   s    r   test_ptr_intrinsic_namez!TestTypes.test_ptr_intrinsic_name7
  s    )88$?!4CCTJ.R^^D1@@$GR^^D!4CCTJR^^D1@@&IR^^D!4CCVLr   c                 0   | j                  t        t              d       | j                  t        t        j                  d            d       | j                  t        t
              d       | j                  t        t              d       | j                  t        t        j                               d       | j                  t        t        j                  t        d            d       | j                  t        t        j                  t        t
        f            d	       | j                  t        t        j                  t        t
        t        f            d
       | j                  t        t        j                  t        dd            d       | j                  t        t        j                  t        t
        fd            d       | j                  t        t        j                  t        t
        t        fd            d       t        sx| j                  t        t        j                  t                    d       | j                  t        t        j                  t        j                  t                          d       nw| j                  t        t        j                  t                    d       | j                  t        t        j                  t        j                  t                          d       | j                  t        t        j                  t        d            d       t        s| j                  t        t        j                  t        j                  t              d            d       | j                  t        t        j                  t        j                  t        d                  d       n| j                  t        t        j                  t        j                  t              d            d       | j                  t        t        j                  t        j                  t        d                  d       | j                  t        t        j                  t        f            d       | j                  t        t        j                  t        t
        f            d       t        se| j                  t        t        j                  t        j                  t              t        j                  t        t        f      f            d       nd| j                  t        t        j                  t        j                  t              t        j                  t        t        f      f            d       | j                  t        t        j                  t        fd            d       | j                  t        t        j                  t        t
        fd            d       t        j                         }|j!                  d      }| j                  t        |      d        |j!                  d!      }| j                  t        |      d"       |j!                  d#      }| j                  t        |      d$       y%)&z:
        Test the string representation of types.
        i1   i29r6  doublevoidry   zi1 ()z
i1 (float)zi1 (float, double)Tr   zi1 (...)zi1 (float, ...)zi1 (float, double, ...)r  r  r  r   z[5 x i1]z	[5 x ptr]z	[5 x i1*]z	[5 x i1]*z{i1}z{i1, float}z{ptr, {i32, i8}}z{i1*, {i32, i8}})packedz<{i1}>z<{i1, float}>MyTypez	%"MyType"zMyType\z%"MyType\5c"zMyType"z%"MyType\22"N)r9   r$   r   r   r0  r<  rD   rY  rB   r   rE   rC   r  r!  r   r~  r  )r   r  mytypemytype1mytype2s        r   test_strzTestTypes.test_strA
  sb    	TD)RZZ^,e4S7+S8,R[[]+V4R__T267AR__TC6:;\JR__TC:>?-	/R__T2tDE#	%R__TC64HI*	,R__TC:tLM2	4.S!67?Su0E!FGOS!67@Su0E!FG$&R\\$23Z@.SbnnT.BA!FG(*ST10E!FGOSbnnT.BA!FG(*ST10E!FG(*R114':;VDR114+>?O.S!5!5t$b&:&:E4=&I7K "L M"$ S!5!5t$b&:&:E4=&I7K "L M"$ 	R114'$GH!	#R114+dKL(	* **,,,X6Vm4--j9W'89--j9W'89r   c           	          t        | j                  | j                               D ]9  }| j                  t	        |      t	        t        j
                  |                   ; y r#   )filterrz  r  r9   hashr  )r   typs     r   	test_hashzTestTypes.test_hash}
  sH    $33T5H5H5JK 	>CT#YTYYs^(<=	>r   c                      fd} fd}t        j                  t              }t        d      D ]  } |||t                 ||       t        j                  t
        d      }t        d      D ]  } |||t
                 ||       t        j                  t        t        j                  t
        t        f      f      } ||dt                ||dt        j                  t
        t        f              j                  t              5  |j                  t        j                  t        d             d d d         ||       t        j                         }t        j                  |d      }|j                  t        t        j                  t
        t        f              ||dt                ||dt        j                  t
        t        f              j                  t              5  |j                  t        j                  t        d             d d d         ||       y # 1 sw Y   xY w# 1 sw Y   xY w)	Nc                 |    | j                  t        j                  t        |            }j	                  ||       y r#   )r  r   rC  rC   r9   )r  r  r[   rh   r   s       r   check_constantz*TestTypes.test_gep.<locals>.check_constant
  s,    VVBKKq12FVX.r   c                     t        j                  t        d      }j                  t              5  | j                  |       d d d        y # 1 sw Y   y xY w)Ng      ?)r   rC  rD   r   r  r  )r  r"  r   s     r   check_index_typez,TestTypes.test_gep.<locals>.check_index_type
  sA    KKS)E""9- u  s   AAr   r   r   r   ro   r  )r   rE   rD   ranger  r   r!  r   r   
IndexErrorr  rC  rC   r~  IdentifiedStructTypeset_body)r   r  r  r  r  r  s   `     r   r  zTestTypes.test_gep
  s   	/	
 ^^C q 	'A2q#&	'\\$"q 	(A2q$'	(!!3(<(<dD\(J"KLr1c"r1b22D$<@Az* 	*FF2;;ua()	***,$$Wh7
C--tTl;<r1c"r1b22D$<@Az* 	*FF2;;ua()	*	* 	*	* 	*s   *H).*H5)H25H>c                 f    t        j                  d       fd} |t        d        |t        d        |t        d        |t        j                  t        d      d        |t        j                  t        d      d        |t        j                  t        t        t        f      d       y )	N%e-m:e-i64:64-f80:128-n8:16:32:64-S128c                 H    j                  | j                        |       y r#   )r9   get_abi_sizer  r[   r   tds     r   rg  z&TestTypes.test_abi_size.<locals>.check
  s    R__R0(;r   r   r   r   r      r	   
r-   create_target_datar   rC   r   r   r  r!  rD   r<  r   rg  r  s   ` @r   test_abi_sizezTestTypes.test_abi_size
  sy    $$%LM	<dAeQeQbll4#Q'bll5!$b)b""Cc?3R8r   c                 f    t        j                  d       fd} |t        d        |t        d        |t        d        |t        j                  t        d      d        |t        j                  t        d      d        |t        j                  t        t        t        f      d       y )Nr  c                 H    j                  | j                        |       y r#   )r'   get_abi_alignmentr  s     r   rg  z+TestTypes.test_abi_alignment.<locals>.check
  s    MM"..r2H=r   )r   ro   r   )r   )r   r   r  r  s   ` @r   test_abi_alignmentzTestTypes.test_abi_alignment
  s|    $$%LM	>dIeTeTbll4#Y/bll5!$d+b""Cc?3T:r   c                    t        j                         }|j                  d      }t        j                  |      }| j	                  |j
                         | j                  |       t        |      }|j                  t        j                  d      t        j                  d      t        j                                | j                  |j
                         | j                  |       | j                  |t        |             y )Nr  r  r
   r   )r   r~  r  r>   r  r  r0   r$   r  r0  rB  r  assertNotEqual)r   r  r  r@   oldstrs        r   test_identified_structz TestTypes.test_identified_struct
  s    **,,,X67+(()V$V

2

2G))*V$FCK0r   c                 ~   t        j                  d      }t        j                         }|j	                  dd      }t        j
                  |      }| j                  |j                         | j                  |       t        |      }|j                  t        j                  d      t        j                  d      t        j                                | j                  |j                  |d|      d       | j                  |j                         | j                  |       | j!                  |t        |             y )	Nr  r  Tr  r	   r   r   ro   )r-   r  r   r~  r  r>   r  r  r0   r$   r  r0  rB  r9   get_element_offsetr  r  )r   r  r  r  r@   r  s         r   test_identified_struct_packedz'TestTypes.test_identified_struct_packed
  s    $$%LM**,,,Xt<7+(()V$V

2

2G222q'BAF))*V$FCK0r   c                     t        j                         }|j                  d      }t        j                  d      g|_        t        j                  d      }| j                  |j                  ||      d       y )Nr  r
   r  r  r   )	r   r~  r  r0  elementsr-   r  r9   r  )r   r  r  r  s       r   $test_target_data_non_default_contextz.TestTypes.test_target_data_non_default_context
  s`    **,,,X6::b>*$$%LM,,R,A1Er   c                     t        j                  t        j                  d      d      }| j                  t	        |      d       y )Nr
   r   z	<8 x i32>)r   r  r0  r9   r$   )r   r  s     r   test_vectorzTestTypes.test_vector
  s.    bjjna0U[1r   N)ru   rv   rw   rz  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  ry   r   r   ru  ru  	  sY    0
.,: 96v6BpM::x>"H
9
;
11F2r   ru  c                 6    t        j                  t        |       S r#   )r   rC  rC   )r  s    r   c32r  
  s    ;;ua  r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z y) TestConstantc                    t        j                  t        d      }| j                  t	        |      d       t        j                  t
        d      }| j                  t	        |      d       t        j                  t
        d      }| j                  t	        |      d       t        j                  t
        d      }| j                  t	        |      d       t        j                  t
        d	      }| j                  t	        |      d
       t        j                  t
        t         j                        }| j                  t	        |      d       t        j                  t
        d       }| j                  t	        |      d       y )Nr   zi32 42r   zi1 1r   zi1 0Ti1 trueFzi1 falsezi1 undef)r   rC  rC   r9   r$   r   r  r   r   s     r   test_integerszTestConstant.test_integers
  s   KKr"Q*KKa Q(KKa Q(KKd#Q+KKe$Q,KKbll+Q,KKd#Q(r   c                    t        j                  t        d      }| j                  t	        |      d       t        j                  t        d      }| j                  t	        |      d       t        j                  t
        d      }| j                  t	        |      d       t        j                  t
        d      }| j                  t	        |      d       t        j                  t
        t         j                        }| j                  t	        |      d       t        j                  t
        d       }| j                  t	        |      d       y )	N      ?zfloat 0x3ff8000000000000g      zfloat 0xbff8000000000000zdouble 0x3ff8000000000000zdouble 0xbff8000000000000zdouble undefz
double 0.0)r   rC  r<  r9   r$   rD   r  r  s     r   
test_realszTestConstant.test_reals
  s    KKS!Q!;<KKT"Q!;<KKS!Q!<=KKT"Q!<=KKR\\*Q0KKT"Q.r   c                    t        j                  t        j                  t        d      t	        d      t	        d      t	        d      f      }| j                  t        |      d       t        j                  t        j                  t        d      t	        d      t	        t         j                        f      }| j                  t        |      d       t         j                  j                  t	        d      t	        d      t	        t         j                        f      }| j                  t        |      d       | j                  t              5 }t         j                  j                  t	        d      t        j                  t        d	      f       d d d        | j                  t        j                        d
       t        j                  t        j                  t        d      t         j                        }| j                  t        |      d       t        j                  t        j                  t        d      d       }| j                  t        |      d       t        j                  t        j                  t        d      t        d            }| j                  t        |      d       t        j                  t        j                  t        d      t        d            }| j                  t        |      d       t        j                  t        j                  t        d      dt         j                  df      }| j                  t        |      d       | j                  t              5  t        j                  t        j                  t        d      d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   ri  r   z[3 x i32] [i32 5, i32 6, i32 4]ro   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])r   ri  )r   rC  r  rC   r  r9   r$   r  literal_arrayr   r  r<  r  r   	bytearrayr   )r   r   r  s      r   test_arrayszTestConstant.test_arrays  s   KKUA.QQQ0HIQ!BCKKUA.QR\\9J0KLQ!?@KK%%s1vs1vs2<<7H&IJQ!FGy) 	GVKK%%s1vr{{3/D&EF	GV--.?	A KKUA.=Q!23KKUA.5Q!<=KKT2.	:K0LMQ!>?KKT1-y9L/MNQ!<=KKUA.BLL!0DEQ!FGz* 	8KKUA.7	8 	8%	G 	G$	8 	8s   AN)1/N6)N36N?c                 (   t        j                  t        j                  d      d      }g d}t        j                  ||      }dj	                  dj                  t        dj                  |                  }| j                  t        |      |       y )Nr
   r   )r   ro   r   r   r   ri  	      z<8 x i32> <{}>z, zi32 {})	r   r  r0  rC  r   r   r   r9   r$   )r   r  valsr  vec_reprs        r   r  zTestConstant.test_vector%  sj    bjjna0'kk%&#**IIc(//4013S8,r   c                     t        j                  t        j                  d      d       j                  }| j	                  |d       y )Nr
   r   )r   rC  r0  constantr9   )r   r  s     r   test_non_nullable_intz"TestConstant.test_non_nullable_int-  s0    ;;rzz"~t4==1%r   c                 j   t        j                  t        t        f      }t        j                  t        |f      }t        j
                  |t        j
                  t        j                         d      t        j
                  t        d      f      }| j                  t        |      d       t         j
                  j                  t        j
                  t        j                         d      t        j
                  t        d      f      }| j                  |j                  |       | j                  t        |      d       t         j
                  j                  t        j
                  t        j                         d      t        j
                  t        t         j                        f      }| j                  |j                  |       | j                  t        |      d       t        j
                  |t         j                        }| j                  t        |      d       t        j
                  |d       }| j                  t        |      d       t        j
                  |d      }| j                  t        |      d       t        j
                  |d|f      }| j                  t        |      d	       t        j
                  |d
      }| j                  t        |      t        |             | j                  t              5  t        j
                  |d       d d d        y # 1 sw Y   y xY w)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  r   r   ri  )r   r!  r<  r   rC   rC  rB  r9   r$   literal_structr  r  r   r   )r   st1st2r   c1c2c3s          r   test_structszTestConstant.test_structs1  sK   ""C;/""E3<0KKbkk",,.#> kk$57 8QJ	LKK&&BLLNC(H(*D$(?(A B%QJ	LKK&&BLLNC(H(*D",,(G(I J%QK	MKKR\\*Q!45KKT"Q!>?[[k*RJ	L[[r2h'R $J 	L[[/0R#b'*z* 	(KKY'	( 	( 	(s   	L))L2c                     t        j                  d      }t        j                  t        j                  ||g      t         j                        }| j                  |       y )Nr   )r   r0  rC  r!  r  r<   )r   r  sts      r   &test_undefined_literal_struct_picklingz3TestConstant.test_undefined_literal_struct_picklingS  s@    ZZ][[--r2h7F$$R(r   c                    t        d      }| j                  |t        j                         | j	                  t        |      d       t        d      }| j                  |t        j                         | j	                  t        |      d       t        j                  t        d      } |t        d      t        d      t        d      g      }| j	                  t        |      d	        |g d
      }| j	                  t        |      d	        |d      }| j	                  t        |      d       | j                  t              5   |g d       ddd       t        j                  t        t        f      }t        j                  t        |f      } |d      }| j	                  t        |      d        |d      }| j	                  t        |      d       y# 1 sw Y   xY w)z@
        Instantiating a type should create a constant.
        r   zi8 42Tr  r   r   r   ri  z[3 x i32] [i32 4, i32 5, i32 6]r  Nz[3 x i32] zeroinitializer)r   r   ri  r  r  r  r   r  )r   r>  r   rC  r9   r$   r   r  rC   r  r   r   r!  r<  )r   r   atr  r  s        r   test_type_instantiatonz#TestConstant.test_type_instantiatonX  s    Ha-Q)Ja-Q+\\%#AAA'(Q!BCyMQ!BCtHQ!<=z* 	|	 ""C;/""E3<0Q #* 	,!"Q #I 	K	 	s   GG"c                 P    t        d      }| j                  t        |      d       y)z8
        Constants should have a useful repr().
        r   z!<ir.Constant type='i32' value=42>N)rC   r9   rp  r  s     r   r  zTestConstant.test_reprv  s"     "Ia"EFr   c                    t        j                  t        j                  t        j                  d      d      t	        t        d                  }| j                         }t        j                  ||j                  d      }d|_	        ||_
        t        j                  t        |            }t        j                  t        |            }| j                  t        |      t        |             y )Nr      
myconstantT)r   rC  r  r0  r  r  r@   rA  r  r  rD  r-   r.   r$   r9   )r   r   r  gvparsedreparseds         r   test_encoding_problemz"TestConstant.test_encoding_problem}  s    KKRZZ]C8!%*-/KKMq!&&,7! $$SV,&&s6{3Vc(m4r   c           	      8   | j                         }t        j                  t        t        f      }t        j
                  ||d      }|j                  dD cg c]  }t        j                  t        |       c}      }t        s| j                  t        |      d       n| j                  t        |      d       | j                  |j                  t        j                  t                     t        j                  |d       }| j                  t              5  |j                  t        j                  t        d      g       d d d        t        j                  |j!                         d       }|j                  t        j                  t        d      g      }t        s| j                  t        |      d       n| j                  t        |      d       | j                  |j                  t        j                  t                     y c c}w # 1 sw Y   xY w)Nr  r   r   z<getelementptr ({float, i1}, ptr @"myconstant", i32 0, i32 1)zEgetelementptr ({float, i1}, {float, i1}* @"myconstant", i32 0, i32 1)r   z,getelementptr ({float, i1}, ptr null, i32 0)z5getelementptr ({float, i1}, {float, i1}* null, i32 0))r@   r   r!  r<  r   rA  r  rC  rC   r   r9   r$   r  rE   r   r  r   )	r   r  r  r  rP  r   const	const_ptrr  s	            r   r  zTestConstant.test_gep  s   KKM!!3+.q"l3FF6:aBKKq):;.SV[] SVdf!56B%y) 	/IIr{{5!,-.	/ KK6	]]BKKq123.SWKM SWTV!56+ ;	/ 	/s   !H+HHc           	      x   | j                         }t        j                  t        t        f      }d}t        j
                  ||d|      }| j                  |j                  |       |j                  dD cg c]  }t        j                  t        |       c}      }| j                  |j                  j                  |       t        s| j                  t        |      d       n| j                  t        |      d       | j                  |j                  t        j                  t        |             y c c}w )Nr   r  r{  r  zIgetelementptr ({float, i1}, ptr addrspace(4) @"myconstant", i32 0, i32 1)zRgetelementptr ({float, i1}, {float, i1} addrspace(4)* @"myconstant", i32 0, i32 1))r@   r   r!  r<  r   rA  r9   r|  r  rC  rC   r  r   r$   rE   )r   r  r  r|  r  rP  r   s          r   test_gep_addrspace_globalvarz)TestConstant.test_gep_addrspace_globalvar  s    KKM!!3+.	q"liHy1FF6:aBKKq):;))95.SVJL SVKM 		!JK ;s   6!D7c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   ztrunc (i64 1 to i32))r   rC  r   r8  rC   r9   r$   r  s     r   
test_trunczTestConstant.test_trunc  s2    KKq!''.Q!78r   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   zzext (i32 1 to i64))r   rC  rC   r9  r   r9   r$   r  s     r   	test_zextzTestConstant.test_zext  s2    KKq!&&u-Q!67r   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr2   zsext (i32 -1 to i64))r   rC  rC   r:  r   r9   r$   r  s     r   	test_sextzTestConstant.test_sext  s2    KKr"''.Q!78r   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   z*fptrunc (float 0x3ff0000000000000 to half))r   rC  r<  r;  r  r9   r$   r  s     r   test_fptrunczTestConstant.test_fptrunc  s2    KKQ'',Q!MNr   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   z*fpext (float 0x3ff0000000000000 to double))r   rC  r<  r=  rD   r9   r$   r  s     r   
test_fpextzTestConstant.test_fpext  s2    KKQ%%c*Q!MNr   c                    | j                         }t        j                  |t        d      }|j	                  t
        j                               }t        s| j                  t        |      d       y | j                  t        |      d       y )Nr  z"bitcast (ptr @"myconstant" to ptr)z$bitcast (i32* @"myconstant" to i64*))
r@   r   rA  rC   r"  r   r   r   r9   r$   )r   r  r  r   s       r   test_bitcastzTestConstant.test_bitcast  se    KKMq%6JJu'')*.SV%IJSV%KLr   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   z(fptoui (float 0x3ff0000000000000 to i32))r   rC  r<  r>  rC   r9   r$   r  s     r   test_fptouizTestConstant.test_fptoui  2    KKQ&&u-Q!KLr   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   zuitofp (i32 1 to float))r   rC  rC   r@  r<  r9   r$   r  s     r   test_uitofpzTestConstant.test_uitofp  2    KKq!((-Q!:;r   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   z(fptosi (float 0x3ff0000000000000 to i32))r   rC  r<  r?  rC   r9   r$   r  s     r   test_fptosizTestConstant.test_fptosi  r+  r   c                     t        j                  t        d      j                  t              }| j                  t        |      d       y )Nr   zsitofp (i32 1 to float))r   rC  rC   rA  r<  r9   r$   r  s     r   test_sitofpzTestConstant.test_sitofp  r.  r   c                    t        j                  t        j                         d       }t        j                  t        d      }|j                  t              }| j                  t        |j
                  t               | j                  t        |j
                  t               t        s| j                  t        |      d       y | j                  t        |      d       y )Nr   zptrtoint (ptr null to i32)zptrtoint (i64* null to i32))r   rC  r   r   rC   rB  r   r  r<  r   r9   r$   )r   r  rE  r   s       r   test_ptrtoint_1zTestConstant.test_ptrtoint_1  s    kk%**,d3kk%#LL)S\\59)S\\37.SV%ABSV%BCr   c                 N   | j                         }t        j                  |t        d      }|j	                  t
              }t        sP| j                  t        |      d       | j                  t        d|j                  t
        j                                nO| j                  t        |      d       | j                  t        d|j                  t
        j                                t        j                  t        d      }| j                  t        d|j                  t
               y )Nr  z#ptrtoint (ptr @"myconstant" to i64)z0can only ptrtoint\(\) to integer type, not 'ptr'z$ptrtoint (i32* @"myconstant" to i64)z2can only ptrtoint\(\) to integer type, not 'i64\*'r   z5can only call ptrtoint\(\) on pointer type, not 'i32')r@   r   rA  rC   rB  r   r   r9   r$   r   r  r   rC  )r   r  r  r   r  s        r   test_ptrtoint_2zTestConstant.test_ptrtoint_2  s    KKMq%6KK.SV%JK""C  "	$ SV%KL""E  "	$ [["DKK		r   c                    t        j                  t        d      }t        j                  t        d      }|j	                  t
        j                               }| j                  t        |j                  t
               | j                  t        |j                  t
        j                                t        s| j                  t        |      d       y | j                  t        |      d       y )Nr   gQ	@zinttoptr (i32 1 to ptr)zinttoptr (i32 1 to i64*))r   rC  rC   r<  rC  r   r   r   r  r   r9   r$   )r   rE  pir   s       r   test_inttoptrzTestConstant.test_inttoptr  s    kk%#[[d#LL))+,)S\\59)R[[%2B2B2DE.SV%>?SV%?@r   c                     t        j                  t        d      }| j                  t	        |j                               d       y )Nr   zsub (i32 0, i32 1))r   rC  rC   r9   r$   r  r   rE  s     r   test_negzTestConstant.test_neg  s-    kk%#SWWY)=>r   c                     t        j                  t        d      }| j                  t	        |j                               d       y )Nr   zxor (i32 1, i32 -1))r   rC  rC   r9   r$   r  r;  s     r   test_notzTestConstant.test_not  s-    kk%#SXXZ*?@r   c                     t        j                  t        d      }| j                  t	        |j                               d       y )Nr   zfneg (float 0x3ff0000000000000))r   rC  r<  r9   r$   r  r;  s     r   	test_fnegzTestConstant.test_fneg#  s-    kk#q!SXXZ*KLr   c                 x   t        j                  t        d      }t        j                  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}|j!                         D ])  \  }}| j#                  t%         ||            |dz          + ddddddd}|j!                         D ]:  \  }}t%        |j'                  ||            }d|z   dz   }	| j#                  ||	       < ddddddd}|j!                         D ]:  \  }}t%        |j)                  ||            }d|z   dz   }	| j#                  ||	       < y )Nr   ro   r  r  r  r   r   r  r  r  r  r  orandr  z (i32 1, i32 2)eqneugtugeultuler  r  r  r  r  r  zicmp sgtsgesltsle)r   rC  rC   r  r  r  r   r   r  r  r  r  r  r  r  r  itemsr9   r$   r  r  )
r   rE  rb  oracler   iropcopr'  rh   r[   s
             r   test_int_binopszTestConstant.test_int_binops'  s   kk%#kk%#''ECHHfchh''ECGGecgg((FCHHfchh((FCGGdchh''E	#
  	EHBSC\42C+CD	E Du5? 	/IC**345F~(99HVX.	/ DEUD 	/ICc23F~(99HVX.	/r   c           
         t        j                  t        d      }t        j                  t        d      }|j                  d|j                  d|j
                  d|j                  d|j                  di}|j                         D ]-  \  }}t         ||            }|dz   }| j                  ||       / d	d
ddddd}|j                         D ]:  \  }}	t        |j                  ||            }d|	z   dz   }| j                  ||       < ddddddd}|j                         D ]:  \  }}	t        |j                  ||            }d|	z   dz   }| j                  ||       < y )Nr   ro   r  r  r  r  r  z5 (float 0x3ff0000000000000, float 0x4000000000000000)oeqrE  ogtoger  olerJ  zfcmp uequnerF  rG  rH  rI  )r   rC  r<  r  r  r  r  r  rO  r$   r9   r  r  )
r   rE  rb  rP  r   rQ  rh   r[   rR  r'  s
             r   test_flt_binopszTestConstant.test_flt_binopsC  sp   kk#q!kk#q!((FCHHfchh((FCHHf6 	/HBC\F  < =HVX.		/ UeE+ 	/IC))#s34F~ *F GHVX.		/ UeE+ 	/IC++C56F~ *F GHVX.		/r   N)!ru   rv   rw   r  r  r  r  r  r  r  r  r  r  r  r  r  r   r"  r$  r&  r(  r*  r-  r0  r2  r4  r6  r9  r<  r>  r@  rS  r[  ry   r   r   r  r  
  s    ) /8:-& (D)
K<G576L&989OOMM<M<
D8
A?AM/8/r   r  c                       e Zd Zd Zy)TestTransformsc                    t        j                         }t        j                  |t        j                  t        j                         d      d      }t        j                  |t        j                  t        j                         d      d      }t        j
                         }|j                  |j                                |j                  |d      }| j                  |j                  |       t        j                  |||      }| j                  ||       | j                  |j                  |       | j                  |j                  |       y )Nry   r   r   )r   r>   rF   rB   rY  r   r  rK   r[  r9   calleereplace_all_callsr'   r  )r   r)   r   r   r   r[  modifieds          r   test_call_transformz"TestTransforms.test_call_transformc  s    iikkk#rr{{}bA5Ikk#rr{{}bA5I,,. 6 6 89||C$c*''S#6dH%DKK-c*r   N)ru   rv   rw   rb  ry   r   r   r]  r]  b  s    +r   r]  c                       e Zd Zd Zy)TestSingletonc                    | j                  t        j                  t        j                  j	                                | j                  t        j                  t        j
                  t        j                               | j                  t        j                  t        j                  t        j                               | j                  t        j                         y r#   )r7   r   r  r  
_Undefinedr  deepcopyr<   r?   s    r   test_undefinedzTestSingleton.test_undefinedr  sq    bllBII$8$8$:;bllDIIbll$;<bllDMM",,$?@$$R\\2r   N)ru   rv   rw   rh  ry   r   r   rd  rd  q  s    3r   rd  __main__)(rx   r  r  r4   r   rT   unittestr   r   llvmliter   r   r-   r   r0  r   r   r  rC   r   HalfTyper  rB  r<  rn  rD   r   r{   r   rj  r  r  r<  ru  r  r  r]  rd  ru   mainry   r   r   <module>rn     s\      	     $ 4 rzz!}rzz!}

2

2

2bkkmbllnbmmo[;x [;|G08 G0TD"X D"NN*x N*b*L *LZV5H V5t(ih iX	Q2 Q2h
!x/8 x/v+X +3H 3 zHMMO r   