Home | History | Annotate | Download | only in Ocaml
      1 (* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %s -o %t
      2  * RUN: %t %t.bc
      3  * RUN: llvm-dis < %t.bc > %t.ll
      4  * RUN: FileCheck %s < %t.ll
      5  * Do a second pass for things that shouldn't be anywhere.
      6  * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll
      7  * XFAIL: vg_leak
      8  *)
      9 
     10 (* Note: It takes several seconds for ocamlopt to link an executable with
     11          libLLVMCore.a, so it's better to write a big test than a bunch of
     12          little ones. *)
     13 
     14 open Llvm
     15 open Llvm_bitwriter
     16 
     17 
     18 (* Tiny unit test framework - really just to help find which line is busted *)
     19 let exit_status = ref 0
     20 let suite_name = ref ""
     21 let group_name = ref ""
     22 let case_num = ref 0
     23 let print_checkpoints = false
     24 let context = global_context ()
     25 let i1_type = Llvm.i1_type context
     26 let i8_type = Llvm.i8_type context
     27 let i16_type = Llvm.i16_type context
     28 let i32_type = Llvm.i32_type context
     29 let i64_type = Llvm.i64_type context
     30 let void_type = Llvm.void_type context
     31 let float_type = Llvm.float_type context
     32 let double_type = Llvm.double_type context
     33 let fp128_type = Llvm.fp128_type context
     34 
     35 let group name =
     36   group_name := !suite_name ^ "/" ^ name;
     37   case_num := 0;
     38   if print_checkpoints then
     39     prerr_endline ("  " ^ name ^ "...")
     40 
     41 let insist cond =
     42   incr case_num;
     43   if not cond then
     44     exit_status := 10;
     45   match print_checkpoints, cond with
     46   | false, true -> ()
     47   | false, false ->
     48       prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
     49   | true, true ->
     50       prerr_endline ("    " ^ (string_of_int !case_num))
     51   | true, false ->
     52       prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
     53 
     54 let suite name f =
     55   suite_name := name;
     56   if print_checkpoints then
     57     prerr_endline (name ^ ":");
     58   f ()
     59 
     60 
     61 (*===-- Fixture -----------------------------------------------------------===*)
     62 
     63 let filename = Sys.argv.(1)
     64 let m = create_module context filename
     65 
     66 
     67 (*===-- Target ------------------------------------------------------------===*)
     68 
     69 let test_target () =
     70   begin group "triple";
     71     let trip = "i686-apple-darwin8" in
     72     set_target_triple trip m;
     73     insist (trip = target_triple m)
     74   end;
     75   
     76   begin group "layout";
     77     let layout = "bogus" in
     78     set_data_layout layout m;
     79     insist (layout = data_layout m)
     80   end
     81   (* CHECK: target datalayout = "bogus"
     82    * CHECK: target triple = "i686-apple-darwin8"
     83    *)
     84 
     85 
     86 (*===-- Constants ---------------------------------------------------------===*)
     87 
     88 let test_constants () =
     89   (* CHECK: const_int{{.*}}i32{{.*}}-1
     90    *)
     91   group "int";
     92   let c = const_int i32_type (-1) in
     93   ignore (define_global "const_int" c m);
     94   insist (i32_type = type_of c);
     95   insist (is_constant c);
     96 
     97   (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
     98    *)
     99   group "sext int";
    100   let c = const_int i64_type (-1) in
    101   ignore (define_global "const_sext_int" c m);
    102   insist (i64_type = type_of c);
    103 
    104   (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295
    105    *)
    106   group "zext int64";
    107   let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
    108   ignore (define_global "const_zext_int64" c m);
    109   insist (i64_type = type_of c);
    110 
    111   (* CHECK: const_int_string{{.*}}i32{{.*}}-1
    112    *)
    113   group "int string";
    114   let c = const_int_of_string i32_type "-1" 10 in
    115   ignore (define_global "const_int_string" c m);
    116   insist (i32_type = type_of c);
    117 
    118   (* CHECK: @const_string = global {{.*}}c"cruel\00world"
    119    *)
    120   group "string";
    121   let c = const_string context "cruel\000world" in
    122   ignore (define_global "const_string" c m);
    123   insist ((array_type i8_type 11) = type_of c);
    124 
    125   (* CHECK: const_stringz{{.*}}"hi\00again\00"
    126    *)
    127   group "stringz";
    128   let c = const_stringz context "hi\000again" in
    129   ignore (define_global "const_stringz" c m);
    130   insist ((array_type i8_type 9) = type_of c);
    131 
    132   (* CHECK: const_single{{.*}}2.75
    133    * CHECK: const_double{{.*}}3.1459
    134    * CHECK: const_double_string{{.*}}1.25
    135    *)
    136   begin group "real";
    137     let cs = const_float float_type 2.75 in
    138     ignore (define_global "const_single" cs m);
    139     insist (float_type = type_of cs);
    140     
    141     let cd = const_float double_type 3.1459 in
    142     ignore (define_global "const_double" cd m);
    143     insist (double_type = type_of cd);
    144 
    145     let cd = const_float_of_string double_type "1.25" in
    146     ignore (define_global "const_double_string" cd m);
    147     insist (double_type = type_of cd)
    148   end;
    149   
    150   let one = const_int i16_type 1 in
    151   let two = const_int i16_type 2 in
    152   let three = const_int i32_type 3 in
    153   let four = const_int i32_type 4 in
    154   
    155   (* CHECK: const_array{{.*}}[i32 3, i32 4]
    156    *)
    157   group "array";
    158   let c = const_array i32_type [| three; four |] in
    159   ignore (define_global "const_array" c m);
    160   insist ((array_type i32_type 2) = (type_of c));
    161   
    162   (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
    163    *)
    164   group "vector";
    165   let c = const_vector [| one; two; one; two;
    166                           one; two; one; two |] in
    167   ignore (define_global "const_vector" c m);
    168   insist ((vector_type i16_type 8) = (type_of c));
    169 
    170   (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
    171    *)
    172   group "structure";
    173   let c = const_struct context [| one; two; three; four |] in
    174   ignore (define_global "const_structure" c m);
    175   insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
    176         = (type_of c));
    177 
    178   (* CHECK: const_null{{.*}}zeroinit
    179    *)
    180   group "null";
    181   let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
    182                                                     double_type |]) in
    183   ignore (define_global "const_null" c m);
    184   
    185   (* CHECK: const_all_ones{{.*}}-1
    186    *)
    187   group "all ones";
    188   let c = const_all_ones i64_type in
    189   ignore (define_global "const_all_ones" c m);
    190 
    191   group "pointer null"; begin
    192     (* CHECK: const_pointer_null = global i64* null
    193      *)
    194     let c = const_pointer_null (pointer_type i64_type) in
    195     ignore (define_global "const_pointer_null" c m);
    196   end;
    197   
    198   (* CHECK: const_undef{{.*}}undef
    199    *)
    200   group "undef";
    201   let c = undef i1_type in
    202   ignore (define_global "const_undef" c m);
    203   insist (i1_type = type_of c);
    204   insist (is_undef c);
    205   
    206   group "constant arithmetic";
    207   (* CHECK: @const_neg = global i64 sub
    208    * CHECK: @const_nsw_neg = global i64 sub nsw
    209    * CHECK: @const_nuw_neg = global i64 sub nuw
    210    * CHECK: @const_fneg = global double fsub
    211    * CHECK: @const_not = global i64 xor
    212    * CHECK: @const_add = global i64 add
    213    * CHECK: @const_nsw_add = global i64 add nsw
    214    * CHECK: @const_nuw_add = global i64 add nuw
    215    * CHECK: @const_fadd = global double fadd
    216    * CHECK: @const_sub = global i64 sub
    217    * CHECK: @const_nsw_sub = global i64 sub nsw
    218    * CHECK: @const_nuw_sub = global i64 sub nuw
    219    * CHECK: @const_fsub = global double fsub
    220    * CHECK: @const_mul = global i64 mul
    221    * CHECK: @const_nsw_mul = global i64 mul nsw
    222    * CHECK: @const_nuw_mul = global i64 mul nuw
    223    * CHECK: @const_fmul = global double fmul
    224    * CHECK: @const_udiv = global i64 udiv
    225    * CHECK: @const_sdiv = global i64 sdiv
    226    * CHECK: @const_exact_sdiv = global i64 sdiv exact
    227    * CHECK: @const_fdiv = global double fdiv
    228    * CHECK: @const_urem = global i64 urem
    229    * CHECK: @const_srem = global i64 srem
    230    * CHECK: @const_frem = global double frem
    231    * CHECK: @const_and = global i64 and
    232    * CHECK: @const_or = global i64 or
    233    * CHECK: @const_xor = global i64 xor
    234    * CHECK: @const_icmp = global i1 icmp sle
    235    * CHECK: @const_fcmp = global i1 fcmp ole
    236    *)
    237   let void_ptr = pointer_type i8_type in
    238   let five = const_int i64_type 5 in
    239   let ffive = const_uitofp five double_type in
    240   let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
    241   let foldbomb = const_ptrtoint foldbomb_gv i64_type in
    242   let ffoldbomb = const_uitofp foldbomb double_type in
    243   ignore (define_global "const_neg" (const_neg foldbomb) m);
    244   ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
    245   ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
    246   ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
    247   ignore (define_global "const_not" (const_not foldbomb) m);
    248   ignore (define_global "const_add" (const_add foldbomb five) m);
    249   ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
    250   ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
    251   ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
    252   ignore (define_global "const_sub" (const_sub foldbomb five) m);
    253   ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
    254   ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
    255   ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
    256   ignore (define_global "const_mul" (const_mul foldbomb five) m);
    257   ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
    258   ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
    259   ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
    260   ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
    261   ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
    262   ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
    263   ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
    264   ignore (define_global "const_urem" (const_urem foldbomb five) m);
    265   ignore (define_global "const_srem" (const_srem foldbomb five) m);
    266   ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
    267   ignore (define_global "const_and" (const_and foldbomb five) m);
    268   ignore (define_global "const_or" (const_or foldbomb five) m);
    269   ignore (define_global "const_xor" (const_xor foldbomb five) m);
    270   ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
    271   ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
    272   
    273   group "constant casts";
    274   (* CHECK: const_trunc{{.*}}trunc
    275    * CHECK: const_sext{{.*}}sext
    276    * CHECK: const_zext{{.*}}zext
    277    * CHECK: const_fptrunc{{.*}}fptrunc
    278    * CHECK: const_fpext{{.*}}fpext
    279    * CHECK: const_uitofp{{.*}}uitofp
    280    * CHECK: const_sitofp{{.*}}sitofp
    281    * CHECK: const_fptoui{{.*}}fptoui
    282    * CHECK: const_fptosi{{.*}}fptosi
    283    * CHECK: const_ptrtoint{{.*}}ptrtoint
    284    * CHECK: const_inttoptr{{.*}}inttoptr
    285    * CHECK: const_bitcast{{.*}}bitcast
    286    *)
    287   let i128_type = integer_type context 128 in
    288   ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
    289                                                i8_type) m);
    290   ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
    291   ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
    292   ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
    293   ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
    294   ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
    295   ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
    296   ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
    297   ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
    298   ignore (define_global "const_ptrtoint" (const_ptrtoint 
    299     (const_gep (const_null (pointer_type i8_type))
    300                [| const_int i32_type 1 |])
    301     i32_type) m);
    302   ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
    303                                                   void_ptr) m);
    304   ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
    305   
    306   group "misc constants";
    307   (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
    308    * CHECK: const_gep{{.*}}getelementptr
    309    * CHECK: const_select{{.*}}select
    310    * CHECK: const_extractelement{{.*}}extractelement
    311    * CHECK: const_insertelement{{.*}}insertelement
    312    * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
    313    *)
    314   ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
    315   ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
    316   ignore (define_global "const_select" (const_select
    317     (const_icmp Icmp.Sle foldbomb five)
    318     (const_int i8_type (-1))
    319     (const_int i8_type 0)) m);
    320   let zero = const_int i32_type 0 in
    321   let one  = const_int i32_type 1 in
    322   ignore (define_global "const_extractelement" (const_extractelement
    323     (const_vector [| zero; one; zero; one |])
    324     (const_trunc foldbomb i32_type)) m);
    325   ignore (define_global "const_insertelement" (const_insertelement
    326     (const_vector [| zero; one; zero; one |])
    327     zero (const_trunc foldbomb i32_type)) m);
    328   ignore (define_global "const_shufflevector" (const_shufflevector
    329     (const_vector [| zero; one |])
    330     (const_vector [| one; zero |])
    331     (const_vector [| const_int i32_type 0; const_int i32_type 1;
    332                      const_int i32_type 2; const_int i32_type 3 |])) m);
    333 
    334   group "asm"; begin
    335     let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
    336     ignore (const_inline_asm
    337       ft
    338       ""
    339       "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
    340       true
    341       false)
    342   end;
    343 
    344   group "recursive struct"; begin
    345       let nsty = named_struct_type context "rec" in
    346       let pty = pointer_type nsty in
    347       struct_set_body nsty [| i32_type; pty |] false;
    348       let elts = [| const_int i32_type 4; const_pointer_null pty |] in
    349       let grec_init = const_named_struct nsty elts in
    350       ignore (define_global "grec" grec_init m);
    351       ignore (string_of_lltype nsty);
    352   end
    353 
    354 
    355 (*===-- Global Values -----------------------------------------------------===*)
    356 
    357 let test_global_values () =
    358   let (++) x f = f x; x in
    359   let zero32 = const_null i32_type in
    360 
    361   (* CHECK: GVal01
    362    *)
    363   group "naming";
    364   let g = define_global "TEMPORARY" zero32 m in
    365   insist ("TEMPORARY" = value_name g);
    366   set_value_name "GVal01" g;
    367   insist ("GVal01" = value_name g);
    368 
    369   (* CHECK: GVal02{{.*}}linkonce
    370    *)
    371   group "linkage";
    372   let g = define_global "GVal02" zero32 m ++
    373           set_linkage Linkage.Link_once in
    374   insist (Linkage.Link_once = linkage g);
    375 
    376   (* CHECK: GVal03{{.*}}Hanalei
    377    *)
    378   group "section";
    379   let g = define_global "GVal03" zero32 m ++
    380           set_section "Hanalei" in
    381   insist ("Hanalei" = section g);
    382   
    383   (* CHECK: GVal04{{.*}}hidden
    384    *)
    385   group "visibility";
    386   let g = define_global "GVal04" zero32 m ++
    387           set_visibility Visibility.Hidden in
    388   insist (Visibility.Hidden = visibility g);
    389   
    390   (* CHECK: GVal05{{.*}}align 128
    391    *)
    392   group "alignment";
    393   let g = define_global "GVal05" zero32 m ++
    394           set_alignment 128 in
    395   insist (128 = alignment g)
    396 
    397 
    398 (*===-- Global Variables --------------------------------------------------===*)
    399 
    400 let test_global_variables () =
    401   let (++) x f = f x; x in
    402   let fourty_two32 = const_int i32_type 42 in
    403 
    404   group "declarations"; begin
    405     (* CHECK: GVar01{{.*}}external
    406      *)
    407     insist (None == lookup_global "GVar01" m);
    408     let g = declare_global i32_type "GVar01" m in
    409     insist (is_declaration g);
    410     insist (pointer_type float_type ==
    411               type_of (declare_global float_type "GVar01" m));
    412     insist (g == declare_global i32_type "GVar01" m);
    413     insist (match lookup_global "GVar01" m with Some x -> x = g
    414                                               | None -> false);
    415 
    416     insist (None == lookup_global "QGVar01" m);
    417     let g = declare_qualified_global i32_type "QGVar01" 3 m in
    418     insist (is_declaration g);
    419     insist (qualified_pointer_type float_type 3 ==
    420               type_of (declare_qualified_global float_type "QGVar01" 3 m));
    421     insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
    422     insist (match lookup_global "QGVar01" m with Some x -> x = g
    423                                               | None -> false);
    424   end;
    425   
    426   group "definitions"; begin
    427     (* CHECK: GVar02{{.*}}42
    428      * CHECK: GVar03{{.*}}42
    429      *)
    430     let g = define_global "GVar02" fourty_two32 m in
    431     let g2 = declare_global i32_type "GVar03" m ++
    432            set_initializer fourty_two32 in
    433     insist (not (is_declaration g));
    434     insist (not (is_declaration g2));
    435     insist ((global_initializer g) == (global_initializer g2));
    436 
    437     let g = define_qualified_global "QGVar02" fourty_two32 3 m in
    438     let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
    439            set_initializer fourty_two32 in
    440     insist (not (is_declaration g));
    441     insist (not (is_declaration g2));
    442     insist ((global_initializer g) == (global_initializer g2));
    443   end;
    444 
    445   (* CHECK: GVar04{{.*}}thread_local
    446    *)
    447   group "threadlocal";
    448   let g = define_global "GVar04" fourty_two32 m ++
    449           set_thread_local true in
    450   insist (is_thread_local g);
    451 
    452   (* CHECK-NOWHERE-NOT: GVar05
    453    *)
    454   group "delete";
    455   let g = define_global "GVar05" fourty_two32 m in
    456   delete_global g;
    457 
    458   (* CHECK: ConstGlobalVar{{.*}}constant
    459    *)
    460   group "constant";
    461   let g = define_global "ConstGlobalVar" fourty_two32 m in
    462   insist (not (is_global_constant g));
    463   set_global_constant true g;
    464   insist (is_global_constant g);
    465   
    466   begin group "iteration";
    467     let m = create_module context "temp" in
    468     
    469     insist (At_end m = global_begin m);
    470     insist (At_start m = global_end m);
    471     
    472     let g1 = declare_global i32_type "One" m in
    473     let g2 = declare_global i32_type "Two" m in
    474     
    475     insist (Before g1 = global_begin m);
    476     insist (Before g2 = global_succ g1);
    477     insist (At_end m = global_succ g2);
    478     
    479     insist (After g2 = global_end m);
    480     insist (After g1 = global_pred g2);
    481     insist (At_start m = global_pred g1);
    482     
    483     let lf s x = s ^ "->" ^ value_name x in
    484     insist ("->One->Two" = fold_left_globals lf "" m);
    485     
    486     let rf x s = value_name x ^ "<-" ^ s in
    487     insist ("One<-Two<-" = fold_right_globals rf m "");
    488     
    489     dispose_module m
    490   end
    491 
    492 (* String globals built below are emitted here.
    493  * CHECK: build_global_string{{.*}}stringval
    494  *)
    495 
    496 
    497 (*===-- Uses --------------------------------------------------------------===*)
    498 
    499 let test_uses () =
    500   let ty = function_type i32_type [| i32_type; i32_type |] in
    501   let fn = define_function "use_function" ty m in
    502   let b = builder_at_end context (entry_block fn) in
    503 
    504   let p1 = param fn 0 in
    505   let p2 = param fn 1 in
    506   let v1 = build_add p1 p2 "v1" b in
    507   let v2 = build_add p1 v1 "v2" b in
    508   let _ = build_add v1 v2 "v3" b in
    509 
    510   let lf s u = value_name (user u) ^ "->" ^ s in
    511   insist ("v2->v3->" = fold_left_uses lf "" v1);
    512   let rf u s = value_name (user u) ^ "<-" ^ s in
    513   insist ("v3<-v2<-" = fold_right_uses rf v1 "");
    514 
    515   let lf s u = value_name (used_value u) ^ "->" ^ s in
    516   insist ("v1->v1->" = fold_left_uses lf "" v1);
    517 
    518   let rf u s = value_name (used_value u) ^ "<-" ^ s in
    519   insist ("v1<-v1<-" = fold_right_uses rf v1 "");
    520 
    521   ignore (build_unreachable b)
    522 
    523 
    524 (*===-- Users -------------------------------------------------------------===*)
    525 
    526 let test_users () =
    527   let ty = function_type i32_type [| i32_type; i32_type |] in
    528   let fn = define_function "user_function" ty m in
    529   let b = builder_at_end context (entry_block fn) in
    530 
    531   let p1 = param fn 0 in
    532   let p2 = param fn 1 in
    533   let a3 = build_alloca i32_type "user_alloca" b in
    534   let p3 = build_load a3 "user_load" b in
    535   let i = build_add p1 p2 "sum" b in
    536 
    537   insist ((num_operands i) = 2);
    538   insist ((operand i 0) = p1);
    539   insist ((operand i 1) = p2);
    540 
    541   set_operand i 1 p3;
    542   insist ((operand i 1) != p2);
    543   insist ((operand i 1) = p3);
    544 
    545   ignore (build_unreachable b)
    546 
    547 
    548 (*===-- Aliases -----------------------------------------------------------===*)
    549 
    550 let test_aliases () =
    551   (* CHECK: @alias = alias i32* @aliasee
    552    *)
    553   let v = declare_global i32_type "aliasee" m in
    554   ignore (add_alias m (pointer_type i32_type) v "alias")
    555 
    556 
    557 (*===-- Functions ---------------------------------------------------------===*)
    558 
    559 let test_functions () =
    560   let ty = function_type i32_type [| i32_type; i64_type |] in
    561   let ty2 = function_type i8_type [| i8_type; i64_type |] in
    562   
    563   (* CHECK: declare i32 @Fn1(i32, i64)
    564    *)
    565   begin group "declare";
    566     insist (None = lookup_function "Fn1" m);
    567     let fn = declare_function "Fn1" ty m in
    568     insist (pointer_type ty = type_of fn);
    569     insist (is_declaration fn);
    570     insist (0 = Array.length (basic_blocks fn));
    571     insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
    572     insist (fn == declare_function "Fn1" ty m);
    573     insist (None <> lookup_function "Fn1" m);
    574     insist (match lookup_function "Fn1" m with Some x -> x = fn
    575                                              | None -> false);
    576     insist (m == global_parent fn)
    577   end;
    578   
    579   (* CHECK-NOWHERE-NOT: Fn2
    580    *)
    581   group "delete";
    582   let fn = declare_function "Fn2" ty m in
    583   delete_function fn;
    584   
    585   (* CHECK: define{{.*}}Fn3
    586    *)
    587   group "define";
    588   let fn = define_function "Fn3" ty m in
    589   insist (not (is_declaration fn));
    590   insist (1 = Array.length (basic_blocks fn));
    591   ignore (build_unreachable (builder_at_end context (entry_block fn)));
    592   
    593   (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
    594    *)
    595   group "params";
    596   let fn = define_function "Fn4" ty m in
    597   let params = params fn in
    598   insist (2 = Array.length params);
    599   insist (params.(0) = param fn 0);
    600   insist (params.(1) = param fn 1);
    601   insist (i32_type = type_of params.(0));
    602   insist (i64_type = type_of params.(1));
    603   set_value_name "Param1" params.(0);
    604   set_value_name "Param2" params.(1);
    605   ignore (build_unreachable (builder_at_end context (entry_block fn)));
    606   
    607   (* CHECK: fastcc{{.*}}Fn5
    608    *)
    609   group "callconv";
    610   let fn = define_function "Fn5" ty m in
    611   insist (CallConv.c = function_call_conv fn);
    612   set_function_call_conv CallConv.fast fn;
    613   insist (CallConv.fast = function_call_conv fn);
    614   ignore (build_unreachable (builder_at_end context (entry_block fn)));
    615   
    616   begin group "gc";
    617     (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
    618      *)
    619     let fn = define_function "Fn6" ty m in
    620     insist (None = gc fn);
    621     set_gc (Some "ocaml") fn;
    622     insist (Some "ocaml" = gc fn);
    623     set_gc None fn;
    624     insist (None = gc fn);
    625     set_gc (Some "shadowstack") fn;
    626     ignore (build_unreachable (builder_at_end context (entry_block fn)));
    627   end;
    628   
    629   begin group "iteration";
    630     let m = create_module context "temp" in
    631     
    632     insist (At_end m = function_begin m);
    633     insist (At_start m = function_end m);
    634     
    635     let f1 = define_function "One" ty m in
    636     let f2 = define_function "Two" ty m in
    637     
    638     insist (Before f1 = function_begin m);
    639     insist (Before f2 = function_succ f1);
    640     insist (At_end m = function_succ f2);
    641     
    642     insist (After f2 = function_end m);
    643     insist (After f1 = function_pred f2);
    644     insist (At_start m = function_pred f1);
    645     
    646     let lf s x = s ^ "->" ^ value_name x in
    647     insist ("->One->Two" = fold_left_functions lf "" m);
    648     
    649     let rf x s = value_name x ^ "<-" ^ s in
    650     insist ("One<-Two<-" = fold_right_functions rf m "");
    651     
    652     dispose_module m
    653   end
    654 
    655 
    656 (*===-- Params ------------------------------------------------------------===*)
    657 
    658 let test_params () =
    659   begin group "iteration";
    660     let m = create_module context "temp" in
    661     
    662     let vf = define_function "void" (function_type void_type [| |]) m in
    663     
    664     insist (At_end vf = param_begin vf);
    665     insist (At_start vf = param_end vf);
    666     
    667     let ty = function_type void_type [| i32_type; i32_type |] in
    668     let f = define_function "f" ty m in
    669     let p1 = param f 0 in
    670     let p2 = param f 1 in
    671     set_value_name "One" p1;
    672     set_value_name "Two" p2;
    673     add_param_attr p1 Attribute.Sext;
    674     add_param_attr p2 Attribute.Noalias;
    675     remove_param_attr p2 Attribute.Noalias;
    676     add_function_attr f Attribute.Nounwind;
    677     add_function_attr f Attribute.Noreturn;
    678     remove_function_attr f Attribute.Noreturn;
    679 
    680     insist (Before p1 = param_begin f);
    681     insist (Before p2 = param_succ p1);
    682     insist (At_end f = param_succ p2);
    683     
    684     insist (After p2 = param_end f);
    685     insist (After p1 = param_pred p2);
    686     insist (At_start f = param_pred p1);
    687     
    688     let lf s x = s ^ "->" ^ value_name x in
    689     insist ("->One->Two" = fold_left_params lf "" f);
    690     
    691     let rf x s = value_name x ^ "<-" ^ s in
    692     insist ("One<-Two<-" = fold_right_params rf f "");
    693     
    694     dispose_module m
    695   end
    696 
    697 
    698 (*===-- Basic Blocks ------------------------------------------------------===*)
    699 
    700 let test_basic_blocks () =
    701   let ty = function_type void_type [| |] in
    702   
    703   (* CHECK: Bb1
    704    *)
    705   group "entry";
    706   let fn = declare_function "X" ty m in
    707   let bb = append_block context "Bb1" fn in
    708   insist (bb = entry_block fn);
    709   ignore (build_unreachable (builder_at_end context bb));
    710   
    711   (* CHECK-NOWHERE-NOT: Bb2
    712    *)
    713   group "delete";
    714   let fn = declare_function "X2" ty m in
    715   let bb = append_block context "Bb2" fn in
    716   delete_block bb;
    717   
    718   group "insert";
    719   let fn = declare_function "X3" ty m in
    720   let bbb = append_block context "b" fn in
    721   let bba = insert_block context "a" bbb in
    722   insist ([| bba; bbb |] = basic_blocks fn);
    723   ignore (build_unreachable (builder_at_end context bba));
    724   ignore (build_unreachable (builder_at_end context bbb));
    725   
    726   (* CHECK: Bb3
    727    *)
    728   group "name/value";
    729   let fn = define_function "X4" ty m in
    730   let bb = entry_block fn in
    731   ignore (build_unreachable (builder_at_end context bb));
    732   let bbv = value_of_block bb in
    733   set_value_name "Bb3" bbv;
    734   insist ("Bb3" = value_name bbv);
    735   
    736   group "casts";
    737   let fn = define_function "X5" ty m in
    738   let bb = entry_block fn in
    739   ignore (build_unreachable (builder_at_end context bb));
    740   insist (bb = block_of_value (value_of_block bb));
    741   insist (value_is_block (value_of_block bb));
    742   insist (not (value_is_block (const_null i32_type)));
    743   
    744   begin group "iteration";
    745     let m = create_module context "temp" in
    746     let f = declare_function "Temp" (function_type i32_type [| |]) m in
    747     
    748     insist (At_end f = block_begin f);
    749     insist (At_start f = block_end f);
    750     
    751     let b1 = append_block context "One" f in
    752     let b2 = append_block context "Two" f in
    753     
    754     insist (Before b1 = block_begin f);
    755     insist (Before b2 = block_succ b1);
    756     insist (At_end f = block_succ b2);
    757     
    758     insist (After b2 = block_end f);
    759     insist (After b1 = block_pred b2);
    760     insist (At_start f = block_pred b1);
    761     
    762     let lf s x = s ^ "->" ^ value_name (value_of_block x) in
    763     insist ("->One->Two" = fold_left_blocks lf "" f);
    764     
    765     let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
    766     insist ("One<-Two<-" = fold_right_blocks rf f "");
    767     
    768     dispose_module m
    769   end
    770 
    771 
    772 (*===-- Instructions ------------------------------------------------------===*)
    773 
    774 let test_instructions () =
    775   begin group "iteration";
    776     let m = create_module context "temp" in
    777     let fty = function_type void_type [| i32_type; i32_type |] in
    778     let f = define_function "f" fty m in
    779     let bb = entry_block f in
    780     let b = builder_at context (At_end bb) in
    781     
    782     insist (At_end bb = instr_begin bb);
    783     insist (At_start bb = instr_end bb);
    784     
    785     let i1 = build_add (param f 0) (param f 1) "One" b in
    786     let i2 = build_sub (param f 0) (param f 1) "Two" b in
    787     
    788     insist (Before i1 = instr_begin bb);
    789     insist (Before i2 = instr_succ i1);
    790     insist (At_end bb = instr_succ i2);
    791     
    792     insist (After i2 = instr_end bb);
    793     insist (After i1 = instr_pred i2);
    794     insist (At_start bb = instr_pred i1);
    795     
    796     let lf s x = s ^ "->" ^ value_name x in
    797     insist ("->One->Two" = fold_left_instrs lf "" bb);
    798     
    799     let rf x s = value_name x ^ "<-" ^ s in
    800     insist ("One<-Two<-" = fold_right_instrs rf bb "");
    801     
    802     dispose_module m
    803   end
    804 
    805 
    806 (*===-- Builder -----------------------------------------------------------===*)
    807 
    808 let test_builder () =
    809   let (++) x f = f x; x in
    810   
    811   begin group "parent";
    812     insist (try
    813               ignore (insertion_block (builder context));
    814               false
    815             with Not_found ->
    816               true);
    817     
    818     let fty = function_type void_type [| i32_type |] in
    819     let fn = define_function "BuilderParent" fty m in
    820     let bb = entry_block fn in
    821     let b = builder_at_end context bb in
    822     let p = param fn 0 in
    823     let sum = build_add p p "sum" b in
    824     ignore (build_ret_void b);
    825     
    826     insist (fn = block_parent bb);
    827     insist (fn = param_parent p);
    828     insist (bb = instr_parent sum);
    829     insist (bb = insertion_block b)
    830   end;
    831   
    832   group "ret void";
    833   begin
    834     (* CHECK: ret void
    835      *)
    836     let fty = function_type void_type [| |] in
    837     let fn = declare_function "X6" fty m in
    838     let b = builder_at_end context (append_block context "Bb01" fn) in
    839     ignore (build_ret_void b)
    840   end;
    841 
    842   group "ret aggregate";
    843   begin
    844       (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
    845        *)
    846       let sty = struct_type context [| i8_type; i64_type |] in
    847       let fty = function_type sty [| |] in
    848       let fn = declare_function "XA6" fty m in
    849       let b = builder_at_end context (append_block context "Bb01" fn) in
    850       let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
    851       ignore (build_aggregate_ret agg b)
    852   end;
    853   
    854   (* The rest of the tests will use one big function. *)
    855   let fty = function_type i32_type [| i32_type; i32_type |] in
    856   let fn = define_function "X7" fty m in
    857   let atentry = builder_at_end context (entry_block fn) in
    858   let p1 = param fn 0 ++ set_value_name "P1" in
    859   let p2 = param fn 1 ++ set_value_name "P2" in
    860   let f1 = build_uitofp p1 float_type "F1" atentry in
    861   let f2 = build_uitofp p2 float_type "F2" atentry in
    862   
    863   let bb00 = append_block context "Bb00" fn in
    864   ignore (build_unreachable (builder_at_end context bb00));
    865 
    866   group "function attribute";
    867   begin
    868       ignore (add_function_attr fn Attribute.UWTable);
    869       (* CHECK: X7{{.*}}#0
    870        * #0 is uwtable, defined at EOF.
    871        *)
    872       insist ([Attribute.UWTable] = function_attr fn);
    873   end;
    874 
    875   group "casts"; begin
    876     let void_ptr = pointer_type i8_type in
    877 
    878     (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
    879      * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
    880      * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
    881      * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
    882      * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
    883      * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
    884      * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
    885      * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
    886      * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
    887      * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
    888      * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
    889      * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
    890      * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
    891      * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
    892      * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
    893      * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
    894      * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
    895      * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
    896      * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
    897      * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
    898      * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
    899      * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
    900      * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
    901      * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
    902      *)
    903     let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
    904     let inst29 = build_zext inst28 i32_type "build_zext" atentry in
    905     let inst30 = build_sext inst29 i64_type "build_sext" atentry in
    906     let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
    907     let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
    908     ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
    909     ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
    910     let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
    911     ignore(build_fpext inst35 double_type "build_fpext" atentry);
    912     let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
    913     let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
    914     ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
    915     ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
    916     ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
    917     ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
    918     ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
    919 
    920     ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
    921     ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
    922     ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
    923     ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
    924     ignore(build_intcast inst29 i64_type "build_sext3" atentry);
    925     ignore(build_intcast p1 i8_type "build_trunc3" atentry);
    926     ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
    927     ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
    928   end;
    929 
    930   group "comparisons"; begin
    931     (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
    932      * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
    933      * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
    934      * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
    935      * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
    936      * CHECK: %build_is_not_null = icmp ne i8* %X1, null
    937      * CHECK: %build_ptrdiff
    938      *)
    939     ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
    940     ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
    941     ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
    942     ignore (build_fcmp Fcmp.True  f2 f1 "build_fcmp_true" atentry);
    943     let g0 = declare_global (pointer_type i8_type) "g0" m in
    944     let g1 = declare_global (pointer_type i8_type) "g1" m in
    945     let p0 = build_load g0 "X0" atentry in
    946     let p1 = build_load g1 "X1" atentry in
    947     ignore (build_is_null p0 "build_is_null" atentry);
    948     ignore (build_is_not_null p1 "build_is_not_null" atentry);
    949     ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
    950   end;
    951 
    952   group "miscellaneous"; begin
    953     (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1)
    954      * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
    955      * CHECK: %build_va_arg = va_arg i8** null, i32
    956      * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
    957      * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
    958      * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
    959      * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
    960      * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
    961      *)
    962     let ci = build_call fn [| p2; p1 |] "build_call" atentry in
    963     insist (CallConv.c = instruction_call_conv ci);
    964     set_instruction_call_conv 63 ci;
    965     insist (63 = instruction_call_conv ci);
    966     insist (not (is_tail_call ci));
    967     set_tail_call true ci;
    968     insist (is_tail_call ci);
    969     add_instruction_param_attr ci 1 Attribute.Sext;
    970     add_instruction_param_attr ci 2 Attribute.Noalias;
    971     remove_instruction_param_attr ci 2 Attribute.Noalias;
    972 
    973     let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
    974     ignore (build_select inst46 p1 p2 "build_select" atentry);
    975     ignore (build_va_arg
    976       (const_null (pointer_type (pointer_type i8_type)))
    977       i32_type "build_va_arg" atentry);
    978 
    979     (* Set up some vector vregs. *)
    980     let one  = const_int i32_type 1 in
    981     let zero = const_int i32_type 0 in
    982     let t1 = const_vector [| one; zero; one; zero |] in
    983     let t2 = const_vector [| zero; one; zero; one |] in
    984     let t3 = const_vector [| one; one; zero; zero |] in
    985     let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
    986     let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
    987     let sty = struct_type context [| i32_type; i8_type |] in
    988 
    989     ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
    990     ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
    991     ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
    992 
    993     let p = build_alloca sty "ba" atentry in
    994     let agg = build_load p "bl" atentry in
    995     let agg0 = build_insertvalue agg (const_int i32_type 1) 0
    996                  "build_insertvalue0" atentry in
    997     let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
    998                  "build_insertvalue1" atentry in
    999     ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
   1000   end;
   1001 
   1002   group "metadata"; begin
   1003     (* CHECK: %metadata = add i32 %P1, %P2, !test !0
   1004      * !0 is metadata emitted at EOF.
   1005      *)
   1006     let i = build_add p1 p2 "metadata" atentry in
   1007     insist ((has_metadata i) = false);
   1008 
   1009     let m1 = const_int i32_type 1 in
   1010     let m2 = mdstring context "metadata test" in
   1011     let md = mdnode context [| m1; m2 |] in
   1012 
   1013     let kind = mdkind_id context "test" in
   1014     set_metadata i kind md;
   1015 
   1016     insist ((has_metadata i) = true);
   1017     insist ((metadata i kind) = Some md);
   1018 
   1019     clear_metadata i kind;
   1020 
   1021     insist ((has_metadata i) = false);
   1022     insist ((metadata i kind) = None);
   1023 
   1024     set_metadata i kind md
   1025   end;
   1026 
   1027   group "dbg"; begin
   1028     (* CHECK: %dbg = add i32 %P1, %P2, !dbg !1
   1029      * !1 is metadata emitted at EOF.
   1030      *)
   1031     insist ((current_debug_location atentry) = None);
   1032 
   1033     let m_line = const_int i32_type 2 in
   1034     let m_col = const_int i32_type 3 in
   1035     let m_scope = mdnode context [| |] in
   1036     let m_inlined = mdnode context [| |] in
   1037     let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
   1038     set_current_debug_location atentry md;
   1039 
   1040     insist ((current_debug_location atentry) = Some md);
   1041 
   1042     let i = build_add p1 p2 "dbg" atentry in
   1043     insist ((has_metadata i) = true);
   1044 
   1045     clear_current_debug_location atentry
   1046   end;
   1047 
   1048   group "ret"; begin
   1049     (* CHECK: ret{{.*}}P1
   1050      *)
   1051     let ret = build_ret p1 atentry in
   1052     position_before ret atentry
   1053   end;
   1054 
   1055   (* see test/Feature/exception.ll *)
   1056   let bblpad = append_block context "Bblpad" fn in
   1057   let rt = struct_type context [| pointer_type i8_type; i32_type |] in
   1058   let ft = var_arg_function_type i32_type  [||] in
   1059   let personality = declare_function "__gxx_personality_v0" ft m in
   1060   let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
   1061   let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
   1062   let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
   1063   begin
   1064       set_global_constant true ztic;
   1065       set_global_constant true ztid;
   1066       set_global_constant true ztipkc;
   1067       let lp = build_landingpad rt personality 0 "lpad"
   1068        (builder_at_end context bblpad) in begin
   1069            set_cleanup lp true;
   1070            add_clause lp ztic;
   1071            insist((pointer_type (pointer_type i8_type)) = type_of ztid);
   1072            let ety = pointer_type (pointer_type i8_type) in
   1073            add_clause lp (const_array ety [| ztipkc; ztid |]);
   1074            ignore (build_resume lp (builder_at_end context bblpad));
   1075       end;
   1076       (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0
   1077        * CHECK: cleanup
   1078        * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
   1079        * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
   1080        * CHECK: resume
   1081        * *)
   1082   end;
   1083 
   1084   group "br"; begin
   1085     (* CHECK: br{{.*}}Bb02
   1086      *)
   1087     let bb02 = append_block context "Bb02" fn in
   1088     let b = builder_at_end context bb02 in
   1089     ignore (build_br bb02 b)
   1090   end;
   1091   
   1092   group "cond_br"; begin
   1093     (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
   1094      *)
   1095     let bb03 = append_block context "Bb03" fn in
   1096     let b = builder_at_end context bb03 in
   1097     let cond = build_trunc p1 i1_type "build_br" b in
   1098     ignore (build_cond_br cond bb03 bb00 b)
   1099   end;
   1100   
   1101   group "switch"; begin
   1102     (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
   1103      * CHECK: 2,{{.*}}SwiBlock2
   1104      *)
   1105     let bb1 = append_block context "SwiBlock1" fn in
   1106     let bb2 = append_block context "SwiBlock2" fn in
   1107     ignore (build_unreachable (builder_at_end context bb2));
   1108     let bb3 = append_block context "SwiBlock3" fn in
   1109     ignore (build_unreachable (builder_at_end context bb3));
   1110     let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
   1111         ignore (add_case si (const_int i32_type 2) bb2);
   1112         insist (switch_default_dest si = bb3);
   1113     end;
   1114   end;
   1115 
   1116   group "malloc/free"; begin
   1117       (* CHECK: call{{.*}}@malloc(i32 ptrtoint
   1118        * CHECK: call{{.*}}@free(i8*
   1119        * CHECK: call{{.*}}@malloc(i32 %
   1120        *)
   1121       let bb1 = append_block context "MallocBlock1" fn in
   1122       let m1 = (build_malloc (pointer_type i32_type) "m1"
   1123       (builder_at_end context bb1)) in
   1124       ignore (build_free m1 (builder_at_end context bb1));
   1125       ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
   1126       ignore (build_unreachable (builder_at_end context bb1));
   1127   end;
   1128 
   1129   group "indirectbr"; begin
   1130     (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
   1131      *)
   1132     let bb1 = append_block context "IBRBlock1" fn in
   1133 
   1134     let bb2 = append_block context "IBRBlock2" fn in
   1135     ignore (build_unreachable (builder_at_end context bb2));
   1136 
   1137     let bb3 = append_block context "IBRBlock3" fn in
   1138     ignore (build_unreachable (builder_at_end context bb3));
   1139 
   1140     let addr = block_address fn bb2 in
   1141     let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
   1142     ignore (add_destination ibr bb2);
   1143     ignore (add_destination ibr bb3)
   1144   end;
   1145   
   1146   group "invoke"; begin
   1147     (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
   1148      * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
   1149      *)
   1150     let bb04 = append_block context "Bb04" fn in
   1151     let b = builder_at_end context bb04 in
   1152     ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
   1153   end;
   1154   
   1155   group "unreachable"; begin
   1156     (* CHECK: unreachable
   1157      *)
   1158     let bb06 = append_block context "Bb06" fn in
   1159     let b = builder_at_end context bb06 in
   1160     ignore (build_unreachable b)
   1161   end;
   1162   
   1163   group "arithmetic"; begin
   1164     let bb07 = append_block context "Bb07" fn in
   1165     let b = builder_at_end context bb07 in
   1166     
   1167     (* CHECK: %build_add = add i32 %P1, %P2
   1168      * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
   1169      * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
   1170      * CHECK: %build_fadd = fadd float %F1, %F2
   1171      * CHECK: %build_sub = sub i32 %P1, %P2
   1172      * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
   1173      * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
   1174      * CHECK: %build_fsub = fsub float %F1, %F2
   1175      * CHECK: %build_mul = mul i32 %P1, %P2
   1176      * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
   1177      * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
   1178      * CHECK: %build_fmul = fmul float %F1, %F2
   1179      * CHECK: %build_udiv = udiv i32 %P1, %P2
   1180      * CHECK: %build_sdiv = sdiv i32 %P1, %P2
   1181      * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
   1182      * CHECK: %build_fdiv = fdiv float %F1, %F2
   1183      * CHECK: %build_urem = urem i32 %P1, %P2
   1184      * CHECK: %build_srem = srem i32 %P1, %P2
   1185      * CHECK: %build_frem = frem float %F1, %F2
   1186      * CHECK: %build_shl = shl i32 %P1, %P2
   1187      * CHECK: %build_lshl = lshr i32 %P1, %P2
   1188      * CHECK: %build_ashl = ashr i32 %P1, %P2
   1189      * CHECK: %build_and = and i32 %P1, %P2
   1190      * CHECK: %build_or = or i32 %P1, %P2
   1191      * CHECK: %build_xor = xor i32 %P1, %P2
   1192      * CHECK: %build_neg = sub i32 0, %P1
   1193      * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
   1194      * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
   1195      * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1
   1196      * CHECK: %build_not = xor i32 %P1, -1
   1197      *)
   1198     ignore (build_add p1 p2 "build_add" b);
   1199     ignore (build_nsw_add p1 p2 "build_nsw_add" b);
   1200     ignore (build_nuw_add p1 p2 "build_nuw_add" b);
   1201     ignore (build_fadd f1 f2 "build_fadd" b);
   1202     ignore (build_sub p1 p2 "build_sub" b);
   1203     ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
   1204     ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
   1205     ignore (build_fsub f1 f2 "build_fsub" b);
   1206     ignore (build_mul p1 p2 "build_mul" b);
   1207     ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
   1208     ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
   1209     ignore (build_fmul f1 f2 "build_fmul" b);
   1210     ignore (build_udiv p1 p2 "build_udiv" b);
   1211     ignore (build_sdiv p1 p2 "build_sdiv" b);
   1212     ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
   1213     ignore (build_fdiv f1 f2 "build_fdiv" b);
   1214     ignore (build_urem p1 p2 "build_urem" b);
   1215     ignore (build_srem p1 p2 "build_srem" b);
   1216     ignore (build_frem f1 f2 "build_frem" b);
   1217     ignore (build_shl p1 p2 "build_shl" b);
   1218     ignore (build_lshr p1 p2 "build_lshl" b);
   1219     ignore (build_ashr p1 p2 "build_ashl" b);
   1220     ignore (build_and p1 p2 "build_and" b);
   1221     ignore (build_or p1 p2 "build_or" b);
   1222     ignore (build_xor p1 p2 "build_xor" b);
   1223     ignore (build_neg p1 "build_neg" b);
   1224     ignore (build_nsw_neg p1 "build_nsw_neg" b);
   1225     ignore (build_nuw_neg p1 "build_nuw_neg" b);
   1226     ignore (build_fneg f1 "build_fneg" b);
   1227     ignore (build_not p1 "build_not" b);
   1228     ignore (build_unreachable b)
   1229   end;
   1230   
   1231   group "memory"; begin
   1232     let bb08 = append_block context "Bb08" fn in
   1233     let b = builder_at_end context bb08 in
   1234 
   1235     (* CHECK: %build_alloca = alloca i32
   1236      * CHECK: %build_array_alloca = alloca i32, i32 %P2
   1237      * CHECK: %build_load = load i32* %build_array_alloca
   1238      * CHECK: store i32 %P2, i32* %build_alloca
   1239      * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
   1240      * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
   1241      * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
   1242      *)
   1243     let alloca = build_alloca i32_type "build_alloca" b in
   1244     let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
   1245     ignore(build_load array_alloca "build_load" b);
   1246     ignore(build_store p2 alloca b);
   1247     ignore(build_gep array_alloca [| p2 |] "build_gep" b);
   1248     ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
   1249 
   1250     let sty = struct_type context [| i32_type; i8_type |] in
   1251     let alloca2 = build_alloca sty "build_alloca2" b in
   1252     ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
   1253 
   1254     ignore(build_unreachable b)
   1255   end;
   1256 
   1257   group "string"; begin
   1258     let bb09 = append_block context "Bb09" fn in
   1259     let b = builder_at_end context bb09 in
   1260     let p = build_alloca (pointer_type i8_type) "p" b in
   1261     (* build_global_string is emitted above.
   1262      * CHECK: store{{.*}}build_global_string1{{.*}}p
   1263      * *)
   1264     ignore (build_global_string "stringval" "build_global_string" b);
   1265     let g = build_global_stringptr "stringval" "build_global_string1" b in
   1266     ignore (build_store g p b);
   1267     ignore(build_unreachable b);
   1268   end;
   1269 
   1270   group "phi"; begin
   1271     (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
   1272      *)
   1273     let b1 = append_block context "PhiBlock1" fn in
   1274     let b2 = append_block context "PhiBlock2" fn in
   1275     
   1276     let jb = append_block context "PhiJoinBlock" fn in
   1277     ignore (build_br jb (builder_at_end context b1));
   1278     ignore (build_br jb (builder_at_end context b2));
   1279     let at_jb = builder_at_end context jb in
   1280     
   1281     let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
   1282     insist ([(p1, b1)] = incoming phi);
   1283     
   1284     add_incoming (p2, b2) phi;
   1285     insist ([(p1, b1); (p2, b2)] = incoming phi);
   1286     
   1287     ignore (build_unreachable at_jb);
   1288   end
   1289 
   1290 (* End-of-file checks for things like metdata and attributes.
   1291  * CHECK: attributes #0 = {{.*}}uwtable{{.*}}
   1292  * CHECK: !0 = metadata !{i32 1, metadata !"metadata test"}
   1293  * CHECK: !1 = metadata !{i32 2, i32 3, metadata !2, metadata !2}
   1294  *)
   1295 
   1296 (*===-- Pass Managers -----------------------------------------------------===*)
   1297 
   1298 let test_pass_manager () =
   1299   let (++) x f = ignore (f x); x in
   1300 
   1301   begin group "module pass manager";
   1302     ignore (PassManager.create ()
   1303              ++ PassManager.run_module m
   1304              ++ PassManager.dispose)
   1305   end;
   1306   
   1307   begin group "function pass manager";
   1308     let fty = function_type void_type [| |] in
   1309     let fn = define_function "FunctionPassManager" fty m in
   1310     ignore (build_ret_void (builder_at_end context (entry_block fn)));
   1311     
   1312     ignore (PassManager.create_function m
   1313              ++ PassManager.initialize
   1314              ++ PassManager.run_function fn
   1315              ++ PassManager.finalize
   1316              ++ PassManager.dispose)
   1317   end
   1318 
   1319 
   1320 (*===-- Writer ------------------------------------------------------------===*)
   1321 
   1322 let test_writer () =
   1323   group "valid";
   1324   insist (match Llvm_analysis.verify_module m with
   1325           | None -> true
   1326           | Some msg -> prerr_string msg; false);
   1327 
   1328   group "writer";
   1329   insist (write_bitcode_file m filename);
   1330   
   1331   dispose_module m
   1332 
   1333 
   1334 (*===-- Driver ------------------------------------------------------------===*)
   1335 
   1336 let _ =
   1337   suite "target"           test_target;
   1338   suite "constants"        test_constants;
   1339   suite "global values"    test_global_values;
   1340   suite "global variables" test_global_variables;
   1341   suite "uses"             test_uses;
   1342   suite "users"            test_users;
   1343   suite "aliases"          test_aliases;
   1344   suite "functions"        test_functions;
   1345   suite "params"           test_params;
   1346   suite "basic blocks"     test_basic_blocks;
   1347   suite "instructions"     test_instructions;
   1348   suite "builder"          test_builder;
   1349   suite "pass manager"     test_pass_manager;
   1350   suite "writer"           test_writer; (* Keep this last; it disposes m. *)
   1351   exit !exit_status
   1352