vmcore.ml revision b4f6095bae5edfe9d6cabfe4802e6f6187e073f2
1caee0dccffb77a003681345ab3281bcf8684526cChris Lattner(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %s -o %t
2caee0dccffb77a003681345ab3281bcf8684526cChris Lattner * RUN: %t %t.bc
3caee0dccffb77a003681345ab3281bcf8684526cChris Lattner * RUN: llvm-dis < %t.bc > %t.ll
4caee0dccffb77a003681345ab3281bcf8684526cChris Lattner * XFAIL: vg_leak
5caee0dccffb77a003681345ab3281bcf8684526cChris Lattner *)
6caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
7caee0dccffb77a003681345ab3281bcf8684526cChris Lattner(* Note: It takes several seconds for ocamlopt to link an executable with
8caee0dccffb77a003681345ab3281bcf8684526cChris Lattner         libLLVMCore.a, so it's better to write a big test than a bunch of
9caee0dccffb77a003681345ab3281bcf8684526cChris Lattner         little ones. *)
10caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
11caee0dccffb77a003681345ab3281bcf8684526cChris Lattneropen Llvm
12caee0dccffb77a003681345ab3281bcf8684526cChris Lattneropen Llvm_bitwriter
13caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
14caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
15caee0dccffb77a003681345ab3281bcf8684526cChris Lattner(* Tiny unit test framework - really just to help find which line is busted *)
16caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerlet exit_status = ref 0
17caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerlet suite_name = ref ""
18522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet group_name = ref ""
19522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet case_num = ref 0
2048f848716e80d01619b239111db48bfac77baad1Chris Lattnerlet print_checkpoints = false
2147f96bf24687b5068aec7166cb8b3ac33ae964aeChris Lattnerlet context = global_context ()
2248f848716e80d01619b239111db48bfac77baad1Chris Lattnerlet i1_type = Llvm.i1_type context
23caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerlet i8_type = Llvm.i8_type context
24caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerlet i16_type = Llvm.i16_type context
25caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerlet i32_type = Llvm.i32_type context
26c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattnerlet i64_type = Llvm.i64_type context
2748c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattnerlet void_type = Llvm.void_type context
28522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet float_type = Llvm.float_type context
29522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet double_type = Llvm.double_type context
30522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet fp128_type = Llvm.fp128_type context
31522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner
32522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet group name =
33522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  group_name := !suite_name ^ "/" ^ name;
34522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  case_num := 0;
35522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  if print_checkpoints then
36522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    prerr_endline ("  " ^ name ^ "...")
37522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner
38522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerlet insist cond =
39522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  incr case_num;
40522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  if not cond then
41522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    exit_status := 10;
42522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  match print_checkpoints, cond with
43522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  | false, true -> ()
44522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  | false, false ->
45522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner      prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
46522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  | true, true ->
47198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattner      prerr_endline ("    " ^ (string_of_int !case_num))
48980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  | true, false ->
49198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattner      prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
50198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattner
51198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattnerlet suite name f =
52522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  suite_name := name;
53522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  if print_checkpoints then
54522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    prerr_endline (name ^ ":");
55a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner  f ()
56a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
57a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
58a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner(*===-- Fixture -----------------------------------------------------------===*)
59a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
60a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattnerlet filename = Sys.argv.(1)
61a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattnerlet m = create_module context filename
62a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
63a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
64a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner(*===-- Target ------------------------------------------------------------===*)
65a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner
66a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattnerlet test_target () =
67a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner  begin group "triple";
68a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner    (* RUN: grep "i686-apple-darwin8" < %t.ll
69a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner     *)
70a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner    let trip = "i686-apple-darwin8" in
71522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    set_target_triple trip m;
72522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    insist (trip = target_triple m)
73522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  end;
74522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  
75522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  begin group "layout";
76522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner    (* RUN: grep "bogus" < %t.ll
77caee0dccffb77a003681345ab3281bcf8684526cChris Lattner     *)
78caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    let layout = "bogus" in
79c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner    set_data_layout layout m;
8048f848716e80d01619b239111db48bfac77baad1Chris Lattner    insist (layout = data_layout m)
8148f848716e80d01619b239111db48bfac77baad1Chris Lattner  end
82caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
83caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
84caee0dccffb77a003681345ab3281bcf8684526cChris Lattner(*===-- Constants ---------------------------------------------------------===*)
85522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner
86e16504eb4ef8f09611cdf6e9a0be9eb886b4ed89Chris Lattnerlet test_constants () =
8707d98b4afbdcbb4eed048400d9116de1ec83e866Chris Lattner  (* RUN: grep {const_int.*i32.*-1} < %t.ll
8848f848716e80d01619b239111db48bfac77baad1Chris Lattner   *)
8948c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  group "int";
9048c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  let c = const_int i32_type (-1) in
9148c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  ignore (define_global "const_int" c m);
9248c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  insist (i32_type = type_of c);
9348c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  insist (is_constant c);
94980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner
95980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  (* RUN: grep {const_sext_int.*i64.*-1} < %t.ll
96980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner   *)
97980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  group "sext int";
9848f848716e80d01619b239111db48bfac77baad1Chris Lattner  let c = const_int i64_type (-1) in
9948f848716e80d01619b239111db48bfac77baad1Chris Lattner  ignore (define_global "const_sext_int" c m);
10048f848716e80d01619b239111db48bfac77baad1Chris Lattner  insist (i64_type = type_of c);
10148f848716e80d01619b239111db48bfac77baad1Chris Lattner
10248f848716e80d01619b239111db48bfac77baad1Chris Lattner  (* RUN: grep {const_zext_int64.*i64.*4294967295} < %t.ll
10348f848716e80d01619b239111db48bfac77baad1Chris Lattner   *)
10448f848716e80d01619b239111db48bfac77baad1Chris Lattner  group "zext int64";
10548f848716e80d01619b239111db48bfac77baad1Chris Lattner  let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
10648f848716e80d01619b239111db48bfac77baad1Chris Lattner  ignore (define_global "const_zext_int64" c m);
10748f848716e80d01619b239111db48bfac77baad1Chris Lattner  insist (i64_type = type_of c);
10848f848716e80d01619b239111db48bfac77baad1Chris Lattner
10948f848716e80d01619b239111db48bfac77baad1Chris Lattner  (* RUN: grep {const_int_string.*i32.*-1} < %t.ll
110caee0dccffb77a003681345ab3281bcf8684526cChris Lattner   *)
11148f848716e80d01619b239111db48bfac77baad1Chris Lattner  group "int string";
11248f848716e80d01619b239111db48bfac77baad1Chris Lattner  let c = const_int_of_string i32_type "-1" 10 in
11348f848716e80d01619b239111db48bfac77baad1Chris Lattner  ignore (define_global "const_int_string" c m);
114c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  insist (i32_type = type_of c);
115caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
116c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  (* RUN: grep {const_string.*"cruel\\\\00world"} < %t.ll
117c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner   *)
118c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  group "string";
119c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  let c = const_string context "cruel\000world" in
120c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  ignore (define_global "const_string" c m);
121c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  insist ((array_type i8_type 11) = type_of c);
122c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner
123caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  (* RUN: grep {const_stringz.*"hi\\\\00again\\\\00"} < %t.ll
12448f848716e80d01619b239111db48bfac77baad1Chris Lattner   *)
125980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  group "stringz";
126d67c632d968157e228cf42b588f8759059730ec0Chris Lattner  let c = const_stringz context "hi\000again" in
127d67c632d968157e228cf42b588f8759059730ec0Chris Lattner  ignore (define_global "const_stringz" c m);
128caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  insist ((array_type i8_type 9) = type_of c);
129caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
130caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  (* RUN: grep {const_single.*2.75} < %t.ll
131caee0dccffb77a003681345ab3281bcf8684526cChris Lattner   * RUN: grep {const_double.*3.1459} < %t.ll
132caee0dccffb77a003681345ab3281bcf8684526cChris Lattner   * RUN: grep {const_double_string.*1.25} < %t.ll
133caee0dccffb77a003681345ab3281bcf8684526cChris Lattner   *)
134caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  begin group "real";
135caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    let cs = const_float float_type 2.75 in
136c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner    ignore (define_global "const_single" cs m);
137caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    insist (float_type = type_of cs);
138caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    
139a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner    let cd = const_float double_type 3.1459 in
140a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner    ignore (define_global "const_double" cd m);
141a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner    insist (double_type = type_of cd);
142f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner
143f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner    let cd = const_float_of_string double_type "1.25" in
144f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner    ignore (define_global "const_double_string" cd m);
145f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner    insist (double_type = type_of cd)
14648c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  end;
14748c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  
14848c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  let one = const_int i16_type 1 in
14948c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  let two = const_int i16_type 2 in
15048c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  let three = const_int i32_type 3 in
1517337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  let four = const_int i32_type 4 in
1527337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  
1537337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  (* RUN: grep {const_array.*\\\[i32 3, i32 4\\\]} < %t.ll
1547337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner   *)
1557337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  group "array";
1567337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  let c = const_array i32_type [| three; four |] in
1577337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  ignore (define_global "const_array" c m);
158650c938c41adc95e0935c75d7ed4349c46d280d1Jeff Cohen  insist ((array_type i32_type 2) = (type_of c));
1597337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  
1607337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  (* RUN: grep {const_vector.*<i16 1, i16 2.*>} < %t.ll
1617337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner   *)
1627337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  group "vector";
1637337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  let c = const_vector [| one; two; one; two;
1647337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner                          one; two; one; two |] in
1657337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  ignore (define_global "const_vector" c m);
1667337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  insist ((vector_type i16_type 8) = (type_of c));
1677337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner
168650c938c41adc95e0935c75d7ed4349c46d280d1Jeff Cohen  (* RUN: grep {const_structure.*.i16 1, i16 2, i32 3, i32 4} < %t.ll
1697337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner   *)
1707337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  group "structure";
1717337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  let c = const_struct context [| one; two; three; four |] in
1727337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  ignore (define_global "const_structure" c m);
1737337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
1747337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner        = (type_of c));
175650c938c41adc95e0935c75d7ed4349c46d280d1Jeff Cohen
1767337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  (* RUN: grep {const_null.*zeroinit} < %t.ll
1777337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner   *)
1787337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  group "null";
17948c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
180caee0dccffb77a003681345ab3281bcf8684526cChris Lattner                                                    double_type |]) in
181866971474ced63f725bd3dbf10fdb9e3badb7d9eChris Lattner  ignore (define_global "const_null" c m);
18248c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  
183866971474ced63f725bd3dbf10fdb9e3badb7d9eChris Lattner  (* RUN: grep {const_all_ones.*-1} < %t.ll
184866971474ced63f725bd3dbf10fdb9e3badb7d9eChris Lattner   *)
185866971474ced63f725bd3dbf10fdb9e3badb7d9eChris Lattner  group "all ones";
186866971474ced63f725bd3dbf10fdb9e3badb7d9eChris Lattner  let c = const_all_ones i64_type in
187980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  ignore (define_global "const_all_ones" c m);
188980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner
18907d98b4afbdcbb4eed048400d9116de1ec83e866Chris Lattner  group "pointer null"; begin
190caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    (* RUN: grep {const_pointer_null = global i64\\* null} < %t.ll
191caee0dccffb77a003681345ab3281bcf8684526cChris Lattner     *)
192caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    let c = const_pointer_null (pointer_type i64_type) in
193caee0dccffb77a003681345ab3281bcf8684526cChris Lattner    ignore (define_global "const_pointer_null" c m);
194caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  end;
195  
196  (* RUN: grep {const_undef.*undef} < %t.ll
197   *)
198  group "undef";
199  let c = undef i1_type in
200  ignore (define_global "const_undef" c m);
201  insist (i1_type = type_of c);
202  insist (is_undef c);
203  
204  group "constant arithmetic";
205  (* RUN: grep {@const_neg = global i64 sub} < %t.ll
206   * RUN: grep {@const_nsw_neg = global i64 sub nsw } < %t.ll
207   * RUN: grep {@const_nuw_neg = global i64 sub nuw } < %t.ll
208   * RUN: grep {@const_fneg = global double fsub } < %t.ll
209   * RUN: grep {@const_not = global i64 xor } < %t.ll
210   * RUN: grep {@const_add = global i64 add } < %t.ll
211   * RUN: grep {@const_nsw_add = global i64 add nsw } < %t.ll
212   * RUN: grep {@const_nuw_add = global i64 add nuw } < %t.ll
213   * RUN: grep {@const_fadd = global double fadd } < %t.ll
214   * RUN: grep {@const_sub = global i64 sub } < %t.ll
215   * RUN: grep {@const_nsw_sub = global i64 sub nsw } < %t.ll
216   * RUN: grep {@const_nuw_sub = global i64 sub nuw } < %t.ll
217   * RUN: grep {@const_fsub = global double fsub } < %t.ll
218   * RUN: grep {@const_mul = global i64 mul } < %t.ll
219   * RUN: grep {@const_nsw_mul = global i64 mul nsw } < %t.ll
220   * RUN: grep {@const_nuw_mul = global i64 mul nuw } < %t.ll
221   * RUN: grep {@const_fmul = global double fmul } < %t.ll
222   * RUN: grep {@const_udiv = global i64 udiv } < %t.ll
223   * RUN: grep {@const_sdiv = global i64 sdiv } < %t.ll
224   * RUN: grep {@const_exact_sdiv = global i64 sdiv exact } < %t.ll
225   * RUN: grep {@const_fdiv = global double fdiv } < %t.ll
226   * RUN: grep {@const_urem = global i64 urem } < %t.ll
227   * RUN: grep {@const_srem = global i64 srem } < %t.ll
228   * RUN: grep {@const_frem = global double frem } < %t.ll
229   * RUN: grep {@const_and = global i64 and } < %t.ll
230   * RUN: grep {@const_or = global i64 or } < %t.ll
231   * RUN: grep {@const_xor = global i64 xor } < %t.ll
232   * RUN: grep {@const_icmp = global i1 icmp sle } < %t.ll
233   * RUN: grep {@const_fcmp = global i1 fcmp ole } < %t.ll
234   *)
235  let void_ptr = pointer_type i8_type in
236  let five = const_int i64_type 5 in
237  let ffive = const_uitofp five double_type in
238  let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
239  let foldbomb = const_ptrtoint foldbomb_gv i64_type in
240  let ffoldbomb = const_uitofp foldbomb double_type in
241  ignore (define_global "const_neg" (const_neg foldbomb) m);
242  ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
243  ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
244  ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
245  ignore (define_global "const_not" (const_not foldbomb) m);
246  ignore (define_global "const_add" (const_add foldbomb five) m);
247  ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
248  ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
249  ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
250  ignore (define_global "const_sub" (const_sub foldbomb five) m);
251  ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
252  ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
253  ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
254  ignore (define_global "const_mul" (const_mul foldbomb five) m);
255  ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
256  ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
257  ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
258  ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
259  ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
260  ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
261  ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
262  ignore (define_global "const_urem" (const_urem foldbomb five) m);
263  ignore (define_global "const_srem" (const_srem foldbomb five) m);
264  ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
265  ignore (define_global "const_and" (const_and foldbomb five) m);
266  ignore (define_global "const_or" (const_or foldbomb five) m);
267  ignore (define_global "const_xor" (const_xor foldbomb five) m);
268  ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
269  ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
270  
271  group "constant casts";
272  (* RUN: grep {const_trunc.*trunc} < %t.ll
273   * RUN: grep {const_sext.*sext} < %t.ll
274   * RUN: grep {const_zext.*zext} < %t.ll
275   * RUN: grep {const_fptrunc.*fptrunc} < %t.ll
276   * RUN: grep {const_fpext.*fpext} < %t.ll
277   * RUN: grep {const_uitofp.*uitofp} < %t.ll
278   * RUN: grep {const_sitofp.*sitofp} < %t.ll
279   * RUN: grep {const_fptoui.*fptoui} < %t.ll
280   * RUN: grep {const_fptosi.*fptosi} < %t.ll
281   * RUN: grep {const_ptrtoint.*ptrtoint} < %t.ll
282   * RUN: grep {const_inttoptr.*inttoptr} < %t.ll
283   * RUN: grep {const_bitcast.*bitcast} < %t.ll
284   *)
285  let i128_type = integer_type context 128 in
286  ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
287                                               i8_type) m);
288  ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
289  ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
290  ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
291  ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
292  ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
293  ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
294  ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
295  ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
296  ignore (define_global "const_ptrtoint" (const_ptrtoint 
297    (const_gep (const_null (pointer_type i8_type))
298               [| const_int i32_type 1 |])
299    i32_type) m);
300  ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
301                                                  void_ptr) m);
302  ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
303  
304  group "misc constants";
305  (* RUN: grep {const_size_of.*getelementptr.*null} < %t.ll
306   * RUN: grep {const_gep.*getelementptr} < %t.ll
307   * RUN: grep {const_select.*select} < %t.ll
308   * RUN: grep {const_extractelement.*extractelement} < %t.ll
309   * RUN: grep {const_insertelement.*insertelement} < %t.ll
310   * RUN: grep {const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>} < %t.ll
311   *)
312  ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
313  ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
314  ignore (define_global "const_select" (const_select
315    (const_icmp Icmp.Sle foldbomb five)
316    (const_int i8_type (-1))
317    (const_int i8_type 0)) m);
318  let zero = const_int i32_type 0 in
319  let one  = const_int i32_type 1 in
320  ignore (define_global "const_extractelement" (const_extractelement
321    (const_vector [| zero; one; zero; one |])
322    (const_trunc foldbomb i32_type)) m);
323  ignore (define_global "const_insertelement" (const_insertelement
324    (const_vector [| zero; one; zero; one |])
325    zero (const_trunc foldbomb i32_type)) m);
326  ignore (define_global "const_shufflevector" (const_shufflevector
327    (const_vector [| zero; one |])
328    (const_vector [| one; zero |])
329    (const_vector [| const_int i32_type 0; const_int i32_type 1;
330                     const_int i32_type 2; const_int i32_type 3 |])) m);
331
332  group "asm"; begin
333    let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
334    ignore (const_inline_asm
335      ft
336      ""
337      "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
338      true
339      false)
340  end
341
342
343(*===-- Global Values -----------------------------------------------------===*)
344
345let test_global_values () =
346  let (++) x f = f x; x in
347  let zero32 = const_null i32_type in
348
349  (* RUN: grep {GVal01} < %t.ll
350   *)
351  group "naming";
352  let g = define_global "TEMPORARY" zero32 m in
353  insist ("TEMPORARY" = value_name g);
354  set_value_name "GVal01" g;
355  insist ("GVal01" = value_name g);
356
357  (* RUN: grep {GVal02.*linkonce} < %t.ll
358   *)
359  group "linkage";
360  let g = define_global "GVal02" zero32 m ++
361          set_linkage Linkage.Link_once in
362  insist (Linkage.Link_once = linkage g);
363
364  (* RUN: grep {GVal03.*Hanalei} < %t.ll
365   *)
366  group "section";
367  let g = define_global "GVal03" zero32 m ++
368          set_section "Hanalei" in
369  insist ("Hanalei" = section g);
370  
371  (* RUN: grep {GVal04.*hidden} < %t.ll
372   *)
373  group "visibility";
374  let g = define_global "GVal04" zero32 m ++
375          set_visibility Visibility.Hidden in
376  insist (Visibility.Hidden = visibility g);
377  
378  (* RUN: grep {GVal05.*align 128} < %t.ll
379   *)
380  group "alignment";
381  let g = define_global "GVal05" zero32 m ++
382          set_alignment 128 in
383  insist (128 = alignment g)
384
385
386(*===-- Global Variables --------------------------------------------------===*)
387
388let test_global_variables () =
389  let (++) x f = f x; x in
390  let fourty_two32 = const_int i32_type 42 in
391
392  group "declarations"; begin
393    (* RUN: grep {GVar01.*external} < %t.ll
394     *)
395    insist (None == lookup_global "GVar01" m);
396    let g = declare_global i32_type "GVar01" m in
397    insist (is_declaration g);
398    insist (pointer_type float_type ==
399              type_of (declare_global float_type "GVar01" m));
400    insist (g == declare_global i32_type "GVar01" m);
401    insist (match lookup_global "GVar01" m with Some x -> x = g
402                                              | None -> false);
403
404    insist (None == lookup_global "QGVar01" m);
405    let g = declare_qualified_global i32_type "QGVar01" 3 m in
406    insist (is_declaration g);
407    insist (qualified_pointer_type float_type 3 ==
408              type_of (declare_qualified_global float_type "QGVar01" 3 m));
409    insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
410    insist (match lookup_global "QGVar01" m with Some x -> x = g
411                                              | None -> false);
412  end;
413  
414  group "definitions"; begin
415    (* RUN: grep {GVar02.*42} < %t.ll
416     * RUN: grep {GVar03.*42} < %t.ll
417     *)
418    let g = define_global "GVar02" fourty_two32 m in
419    let g2 = declare_global i32_type "GVar03" m ++
420           set_initializer fourty_two32 in
421    insist (not (is_declaration g));
422    insist (not (is_declaration g2));
423    insist ((global_initializer g) == (global_initializer g2));
424
425    let g = define_qualified_global "QGVar02" fourty_two32 3 m in
426    let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
427           set_initializer fourty_two32 in
428    insist (not (is_declaration g));
429    insist (not (is_declaration g2));
430    insist ((global_initializer g) == (global_initializer g2));
431  end;
432
433  (* RUN: grep {GVar04.*thread_local} < %t.ll
434   *)
435  group "threadlocal";
436  let g = define_global "GVar04" fourty_two32 m ++
437          set_thread_local true in
438  insist (is_thread_local g);
439
440  (* RUN: grep -v {GVar05} < %t.ll
441   *)
442  group "delete";
443  let g = define_global "GVar05" fourty_two32 m in
444  delete_global g;
445
446  (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll
447   *)
448  group "constant";
449  let g = define_global "ConstGlobalVar" fourty_two32 m in
450  insist (not (is_global_constant g));
451  set_global_constant true g;
452  insist (is_global_constant g);
453  
454  begin group "iteration";
455    let m = create_module context "temp" in
456    
457    insist (At_end m = global_begin m);
458    insist (At_start m = global_end m);
459    
460    let g1 = declare_global i32_type "One" m in
461    let g2 = declare_global i32_type "Two" m in
462    
463    insist (Before g1 = global_begin m);
464    insist (Before g2 = global_succ g1);
465    insist (At_end m = global_succ g2);
466    
467    insist (After g2 = global_end m);
468    insist (After g1 = global_pred g2);
469    insist (At_start m = global_pred g1);
470    
471    let lf s x = s ^ "->" ^ value_name x in
472    insist ("->One->Two" = fold_left_globals lf "" m);
473    
474    let rf x s = value_name x ^ "<-" ^ s in
475    insist ("One<-Two<-" = fold_right_globals rf m "");
476    
477    dispose_module m
478  end
479
480
481(*===-- Uses --------------------------------------------------------------===*)
482
483let test_uses () =
484  let ty = function_type i32_type [| i32_type; i32_type |] in
485  let fn = define_function "use_function" ty m in
486  let b = builder_at_end context (entry_block fn) in
487
488  let p1 = param fn 0 in
489  let p2 = param fn 1 in
490  let v1 = build_add p1 p2 "v1" b in
491  let v2 = build_add p1 v1 "v2" b in
492  let _ = build_add v1 v2 "v3" b in
493
494  let lf s u = value_name (user u) ^ "->" ^ s in
495  insist ("v2->v3->" = fold_left_uses lf "" v1);
496  let rf u s = value_name (user u) ^ "<-" ^ s in
497  insist ("v3<-v2<-" = fold_right_uses rf v1 "");
498
499  let lf s u = value_name (used_value u) ^ "->" ^ s in
500  insist ("v1->v1->" = fold_left_uses lf "" v1);
501
502  let rf u s = value_name (used_value u) ^ "<-" ^ s in
503  insist ("v1<-v1<-" = fold_right_uses rf v1 "");
504
505  ignore (build_unreachable b)
506
507
508(*===-- Users -------------------------------------------------------------===*)
509
510let test_users () =
511  let ty = function_type i32_type [| i32_type; i32_type |] in
512  let fn = define_function "user_function" ty m in
513  let b = builder_at_end context (entry_block fn) in
514
515  let p1 = param fn 0 in
516  let p2 = param fn 1 in
517  let a3 = build_alloca i32_type "user_alloca" b in
518  let p3 = build_load a3 "user_load" b in
519  let i = build_add p1 p2 "sum" b in
520
521  insist ((num_operands i) = 2);
522  insist ((operand i 0) = p1);
523  insist ((operand i 1) = p2);
524
525  set_operand i 1 p3;
526  insist ((operand i 1) != p2);
527  insist ((operand i 1) = p3);
528
529  ignore (build_unreachable b)
530
531
532(*===-- Aliases -----------------------------------------------------------===*)
533
534let test_aliases () =
535  (* RUN: grep {@alias = alias i32\\* @aliasee} < %t.ll
536   *)
537  let v = declare_global i32_type "aliasee" m in
538  ignore (add_alias m (pointer_type i32_type) v "alias")
539
540
541(*===-- Functions ---------------------------------------------------------===*)
542
543let test_functions () =
544  let ty = function_type i32_type [| i32_type; i64_type |] in
545  let ty2 = function_type i8_type [| i8_type; i64_type |] in
546  
547  (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
548   *)
549  begin group "declare";
550    insist (None = lookup_function "Fn1" m);
551    let fn = declare_function "Fn1" ty m in
552    insist (pointer_type ty = type_of fn);
553    insist (is_declaration fn);
554    insist (0 = Array.length (basic_blocks fn));
555    insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
556    insist (fn == declare_function "Fn1" ty m);
557    insist (None <> lookup_function "Fn1" m);
558    insist (match lookup_function "Fn1" m with Some x -> x = fn
559                                             | None -> false);
560    insist (m == global_parent fn)
561  end;
562  
563  (* RUN: grep -v {Fn2} < %t.ll
564   *)
565  group "delete";
566  let fn = declare_function "Fn2" ty m in
567  delete_function fn;
568  
569  (* RUN: grep {define.*Fn3} < %t.ll
570   *)
571  group "define";
572  let fn = define_function "Fn3" ty m in
573  insist (not (is_declaration fn));
574  insist (1 = Array.length (basic_blocks fn));
575  ignore (build_unreachable (builder_at_end context (entry_block fn)));
576  
577  (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll
578   *)
579  group "params";
580  let fn = define_function "Fn4" ty m in
581  let params = params fn in
582  insist (2 = Array.length params);
583  insist (params.(0) = param fn 0);
584  insist (params.(1) = param fn 1);
585  insist (i32_type = type_of params.(0));
586  insist (i64_type = type_of params.(1));
587  set_value_name "Param1" params.(0);
588  set_value_name "Param2" params.(1);
589  ignore (build_unreachable (builder_at_end context (entry_block fn)));
590  
591  (* RUN: grep {fastcc.*Fn5} < %t.ll
592   *)
593  group "callconv";
594  let fn = define_function "Fn5" ty m in
595  insist (CallConv.c = function_call_conv fn);
596  set_function_call_conv CallConv.fast fn;
597  insist (CallConv.fast = function_call_conv fn);
598  ignore (build_unreachable (builder_at_end context (entry_block fn)));
599  
600  begin group "gc";
601    (* RUN: grep {Fn6.*gc.*shadowstack} < %t.ll
602     *)
603    let fn = define_function "Fn6" ty m in
604    insist (None = gc fn);
605    set_gc (Some "ocaml") fn;
606    insist (Some "ocaml" = gc fn);
607    set_gc None fn;
608    insist (None = gc fn);
609    set_gc (Some "shadowstack") fn;
610    ignore (build_unreachable (builder_at_end context (entry_block fn)));
611  end;
612  
613  begin group "iteration";
614    let m = create_module context "temp" in
615    
616    insist (At_end m = function_begin m);
617    insist (At_start m = function_end m);
618    
619    let f1 = define_function "One" ty m in
620    let f2 = define_function "Two" ty m in
621    
622    insist (Before f1 = function_begin m);
623    insist (Before f2 = function_succ f1);
624    insist (At_end m = function_succ f2);
625    
626    insist (After f2 = function_end m);
627    insist (After f1 = function_pred f2);
628    insist (At_start m = function_pred f1);
629    
630    let lf s x = s ^ "->" ^ value_name x in
631    insist ("->One->Two" = fold_left_functions lf "" m);
632    
633    let rf x s = value_name x ^ "<-" ^ s in
634    insist ("One<-Two<-" = fold_right_functions rf m "");
635    
636    dispose_module m
637  end
638
639
640(*===-- Params ------------------------------------------------------------===*)
641
642let test_params () =
643  begin group "iteration";
644    let m = create_module context "temp" in
645    
646    let vf = define_function "void" (function_type void_type [| |]) m in
647    
648    insist (At_end vf = param_begin vf);
649    insist (At_start vf = param_end vf);
650    
651    let ty = function_type void_type [| i32_type; i32_type |] in
652    let f = define_function "f" ty m in
653    let p1 = param f 0 in
654    let p2 = param f 1 in
655    set_value_name "One" p1;
656    set_value_name "Two" p2;
657    add_param_attr p1 Attribute.Sext;
658    add_param_attr p2 Attribute.Noalias;
659    remove_param_attr p2 Attribute.Noalias;
660    add_function_attr f Attribute.Nounwind;
661    add_function_attr f Attribute.Noreturn;
662    remove_function_attr f Attribute.Noreturn;
663
664    insist (Before p1 = param_begin f);
665    insist (Before p2 = param_succ p1);
666    insist (At_end f = param_succ p2);
667    
668    insist (After p2 = param_end f);
669    insist (After p1 = param_pred p2);
670    insist (At_start f = param_pred p1);
671    
672    let lf s x = s ^ "->" ^ value_name x in
673    insist ("->One->Two" = fold_left_params lf "" f);
674    
675    let rf x s = value_name x ^ "<-" ^ s in
676    insist ("One<-Two<-" = fold_right_params rf f "");
677    
678    dispose_module m
679  end
680
681
682(*===-- Basic Blocks ------------------------------------------------------===*)
683
684let test_basic_blocks () =
685  let ty = function_type void_type [| |] in
686  
687  (* RUN: grep {Bb1} < %t.ll
688   *)
689  group "entry";
690  let fn = declare_function "X" ty m in
691  let bb = append_block context "Bb1" fn in
692  insist (bb = entry_block fn);
693  ignore (build_unreachable (builder_at_end context bb));
694  
695  (* RUN: grep -v Bb2 < %t.ll
696   *)
697  group "delete";
698  let fn = declare_function "X2" ty m in
699  let bb = append_block context "Bb2" fn in
700  delete_block bb;
701  
702  group "insert";
703  let fn = declare_function "X3" ty m in
704  let bbb = append_block context "b" fn in
705  let bba = insert_block context "a" bbb in
706  insist ([| bba; bbb |] = basic_blocks fn);
707  ignore (build_unreachable (builder_at_end context bba));
708  ignore (build_unreachable (builder_at_end context bbb));
709  
710  (* RUN: grep Bb3 < %t.ll
711   *)
712  group "name/value";
713  let fn = define_function "X4" ty m in
714  let bb = entry_block fn in
715  ignore (build_unreachable (builder_at_end context bb));
716  let bbv = value_of_block bb in
717  set_value_name "Bb3" bbv;
718  insist ("Bb3" = value_name bbv);
719  
720  group "casts";
721  let fn = define_function "X5" ty m in
722  let bb = entry_block fn in
723  ignore (build_unreachable (builder_at_end context bb));
724  insist (bb = block_of_value (value_of_block bb));
725  insist (value_is_block (value_of_block bb));
726  insist (not (value_is_block (const_null i32_type)));
727  
728  begin group "iteration";
729    let m = create_module context "temp" in
730    let f = declare_function "Temp" (function_type i32_type [| |]) m in
731    
732    insist (At_end f = block_begin f);
733    insist (At_start f = block_end f);
734    
735    let b1 = append_block context "One" f in
736    let b2 = append_block context "Two" f in
737    
738    insist (Before b1 = block_begin f);
739    insist (Before b2 = block_succ b1);
740    insist (At_end f = block_succ b2);
741    
742    insist (After b2 = block_end f);
743    insist (After b1 = block_pred b2);
744    insist (At_start f = block_pred b1);
745    
746    let lf s x = s ^ "->" ^ value_name (value_of_block x) in
747    insist ("->One->Two" = fold_left_blocks lf "" f);
748    
749    let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
750    insist ("One<-Two<-" = fold_right_blocks rf f "");
751    
752    dispose_module m
753  end
754
755
756(*===-- Instructions ------------------------------------------------------===*)
757
758let test_instructions () =
759  begin group "iteration";
760    let m = create_module context "temp" in
761    let fty = function_type void_type [| i32_type; i32_type |] in
762    let f = define_function "f" fty m in
763    let bb = entry_block f in
764    let b = builder_at context (At_end bb) in
765    
766    insist (At_end bb = instr_begin bb);
767    insist (At_start bb = instr_end bb);
768    
769    let i1 = build_add (param f 0) (param f 1) "One" b in
770    let i2 = build_sub (param f 0) (param f 1) "Two" b in
771    
772    insist (Before i1 = instr_begin bb);
773    insist (Before i2 = instr_succ i1);
774    insist (At_end bb = instr_succ i2);
775    
776    insist (After i2 = instr_end bb);
777    insist (After i1 = instr_pred i2);
778    insist (At_start bb = instr_pred i1);
779    
780    let lf s x = s ^ "->" ^ value_name x in
781    insist ("->One->Two" = fold_left_instrs lf "" bb);
782    
783    let rf x s = value_name x ^ "<-" ^ s in
784    insist ("One<-Two<-" = fold_right_instrs rf bb "");
785    
786    dispose_module m
787  end
788
789
790(*===-- Builder -----------------------------------------------------------===*)
791
792let test_builder () =
793  let (++) x f = f x; x in
794  
795  begin group "parent";
796    insist (try
797              ignore (insertion_block (builder context));
798              false
799            with Not_found ->
800              true);
801    
802    let fty = function_type void_type [| i32_type |] in
803    let fn = define_function "BuilderParent" fty m in
804    let bb = entry_block fn in
805    let b = builder_at_end context bb in
806    let p = param fn 0 in
807    let sum = build_add p p "sum" b in
808    ignore (build_ret_void b);
809    
810    insist (fn = block_parent bb);
811    insist (fn = param_parent p);
812    insist (bb = instr_parent sum);
813    insist (bb = insertion_block b)
814  end;
815  
816  group "ret void";
817  begin
818    (* RUN: grep {ret void} < %t.ll
819     *)
820    let fty = function_type void_type [| |] in
821    let fn = declare_function "X6" fty m in
822    let b = builder_at_end context (append_block context "Bb01" fn) in
823    ignore (build_ret_void b)
824  end;
825  
826  (* The rest of the tests will use one big function. *)
827  let fty = function_type i32_type [| i32_type; i32_type |] in
828  let fn = define_function "X7" fty m in
829  let atentry = builder_at_end context (entry_block fn) in
830  let p1 = param fn 0 ++ set_value_name "P1" in
831  let p2 = param fn 1 ++ set_value_name "P2" in
832  let f1 = build_uitofp p1 float_type "F1" atentry in
833  let f2 = build_uitofp p2 float_type "F2" atentry in
834  
835  let bb00 = append_block context "Bb00" fn in
836  ignore (build_unreachable (builder_at_end context bb00));
837  
838  group "ret"; begin
839    (* RUN: grep {ret.*P1} < %t.ll
840     *)
841    let ret = build_ret p1 atentry in
842    position_before ret atentry
843  end;
844  
845  group "br"; begin
846    (* RUN: grep {br.*Bb02} < %t.ll
847     *)
848    let bb02 = append_block context "Bb02" fn in
849    let b = builder_at_end context bb02 in
850    ignore (build_br bb02 b)
851  end;
852  
853  group "cond_br"; begin
854    (* RUN: grep {br.*build_br.*Bb03.*Bb00} < %t.ll
855     *)
856    let bb03 = append_block context "Bb03" fn in
857    let b = builder_at_end context bb03 in
858    let cond = build_trunc p1 i1_type "build_br" b in
859    ignore (build_cond_br cond bb03 bb00 b)
860  end;
861  
862  group "switch"; begin
863    (* RUN: grep {switch.*P1.*SwiBlock3} < %t.ll
864     * RUN: grep {2,.*SwiBlock2} < %t.ll
865     *)
866    let bb1 = append_block context "SwiBlock1" fn in
867    let bb2 = append_block context "SwiBlock2" fn in
868    ignore (build_unreachable (builder_at_end context bb2));
869    let bb3 = append_block context "SwiBlock3" fn in
870    ignore (build_unreachable (builder_at_end context bb3));
871    let si = build_switch p1 bb3 1 (builder_at_end context bb1) in
872    ignore (add_case si (const_int i32_type 2) bb2)
873  end;
874
875  group "indirectbr"; begin
876    (* RUN: grep {indirectbr i8\\* blockaddress(@X7, %IBRBlock2), \\\[label %IBRBlock2, label %IBRBlock3\\\]} < %t.ll
877     *)
878    let bb1 = append_block context "IBRBlock1" fn in
879
880    let bb2 = append_block context "IBRBlock2" fn in
881    ignore (build_unreachable (builder_at_end context bb2));
882
883    let bb3 = append_block context "IBRBlock3" fn in
884    ignore (build_unreachable (builder_at_end context bb3));
885
886    let addr = block_address fn bb2 in
887    let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
888    ignore (add_destination ibr bb2);
889    ignore (add_destination ibr bb3)
890  end;
891  
892  group "invoke"; begin
893    (* RUN: grep {build_invoke.*invoke.*P1.*P2} < %t.ll
894     * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll
895     *)
896    let bb04 = append_block context "Bb04" fn in
897    let b = builder_at_end context bb04 in
898    ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "build_invoke" b)
899  end;
900  
901  group "unwind"; begin
902    (* RUN: grep {unwind} < %t.ll
903     *)
904    let bb05 = append_block context "Bb05" fn in
905    let b = builder_at_end context bb05 in
906    ignore (build_unwind b)
907  end;
908  
909  group "unreachable"; begin
910    (* RUN: grep {unreachable} < %t.ll
911     *)
912    let bb06 = append_block context "Bb06" fn in
913    let b = builder_at_end context bb06 in
914    ignore (build_unreachable b)
915  end;
916  
917  group "arithmetic"; begin
918    let bb07 = append_block context "Bb07" fn in
919    let b = builder_at_end context bb07 in
920    
921    (* RUN: grep {%build_add = add i32 %P1, %P2} < %t.ll
922     * RUN: grep {%build_nsw_add = add nsw i32 %P1, %P2} < %t.ll
923     * RUN: grep {%build_nuw_add = add nuw i32 %P1, %P2} < %t.ll
924     * RUN: grep {%build_fadd = fadd float %F1, %F2} < %t.ll
925     * RUN: grep {%build_sub = sub i32 %P1, %P2} < %t.ll
926     * RUN: grep {%build_nsw_sub = sub nsw i32 %P1, %P2} < %t.ll
927     * RUN: grep {%build_nuw_sub = sub nuw i32 %P1, %P2} < %t.ll
928     * RUN: grep {%build_fsub = fsub float %F1, %F2} < %t.ll
929     * RUN: grep {%build_mul = mul i32 %P1, %P2} < %t.ll
930     * RUN: grep {%build_nsw_mul = mul nsw i32 %P1, %P2} < %t.ll
931     * RUN: grep {%build_nuw_mul = mul nuw i32 %P1, %P2} < %t.ll
932     * RUN: grep {%build_fmul = fmul float %F1, %F2} < %t.ll
933     * RUN: grep {%build_udiv = udiv i32 %P1, %P2} < %t.ll
934     * RUN: grep {%build_sdiv = sdiv i32 %P1, %P2} < %t.ll
935     * RUN: grep {%build_exact_sdiv = sdiv exact i32 %P1, %P2} < %t.ll
936     * RUN: grep {%build_fdiv = fdiv float %F1, %F2} < %t.ll
937     * RUN: grep {%build_urem = urem i32 %P1, %P2} < %t.ll
938     * RUN: grep {%build_srem = srem i32 %P1, %P2} < %t.ll
939     * RUN: grep {%build_frem = frem float %F1, %F2} < %t.ll
940     * RUN: grep {%build_shl = shl i32 %P1, %P2} < %t.ll
941     * RUN: grep {%build_lshl = lshr i32 %P1, %P2} < %t.ll
942     * RUN: grep {%build_ashl = ashr i32 %P1, %P2} < %t.ll
943     * RUN: grep {%build_and = and i32 %P1, %P2} < %t.ll
944     * RUN: grep {%build_or = or i32 %P1, %P2} < %t.ll
945     * RUN: grep {%build_xor = xor i32 %P1, %P2} < %t.ll
946     * RUN: grep {%build_neg = sub i32 0, %P1} < %t.ll
947     * RUN: grep {%build_nsw_neg = sub nsw i32 0, %P1} < %t.ll
948     * RUN: grep {%build_nuw_neg = sub nuw i32 0, %P1} < %t.ll
949     * RUN: grep {%build_fneg = fsub float .*0.*, %F1} < %t.ll
950     * RUN: grep {%build_not = xor i32 %P1, -1} < %t.ll
951     *)
952    ignore (build_add p1 p2 "build_add" b);
953    ignore (build_nsw_add p1 p2 "build_nsw_add" b);
954    ignore (build_nuw_add p1 p2 "build_nuw_add" b);
955    ignore (build_fadd f1 f2 "build_fadd" b);
956    ignore (build_sub p1 p2 "build_sub" b);
957    ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
958    ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
959    ignore (build_fsub f1 f2 "build_fsub" b);
960    ignore (build_mul p1 p2 "build_mul" b);
961    ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
962    ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
963    ignore (build_fmul f1 f2 "build_fmul" b);
964    ignore (build_udiv p1 p2 "build_udiv" b);
965    ignore (build_sdiv p1 p2 "build_sdiv" b);
966    ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
967    ignore (build_fdiv f1 f2 "build_fdiv" b);
968    ignore (build_urem p1 p2 "build_urem" b);
969    ignore (build_srem p1 p2 "build_srem" b);
970    ignore (build_frem f1 f2 "build_frem" b);
971    ignore (build_shl p1 p2 "build_shl" b);
972    ignore (build_lshr p1 p2 "build_lshl" b);
973    ignore (build_ashr p1 p2 "build_ashl" b);
974    ignore (build_and p1 p2 "build_and" b);
975    ignore (build_or p1 p2 "build_or" b);
976    ignore (build_xor p1 p2 "build_xor" b);
977    ignore (build_neg p1 "build_neg" b);
978    ignore (build_nsw_neg p1 "build_nsw_neg" b);
979    ignore (build_nuw_neg p1 "build_nuw_neg" b);
980    ignore (build_fneg f1 "build_fneg" b);
981    ignore (build_not p1 "build_not" b);
982    ignore (build_unreachable b)
983  end;
984  
985  group "memory"; begin
986    let bb08 = append_block context "Bb08" fn in
987    let b = builder_at_end context bb08 in
988
989    (* RUN: grep {%build_alloca = alloca i32} < %t.ll
990     * RUN: grep {%build_array_alloca = alloca i32, i32 %P2} < %t.ll
991     * RUN: grep {%build_load = load i32\\* %build_array_alloca} < %t.ll
992     * RUN: grep {store i32 %P2, i32\\* %build_alloca} < %t.ll
993     * RUN: grep {%build_gep = getelementptr i32\\* %build_array_alloca, i32 %P2} < %t.ll
994     *)
995    let alloca = build_alloca i32_type "build_alloca" b in
996    let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
997    ignore(build_load array_alloca "build_load" b);
998    ignore(build_store p2 alloca b);
999    ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1000    ignore(build_unreachable b)
1001  end;
1002  
1003  group "casts"; begin
1004    let void_ptr = pointer_type i8_type in
1005    
1006    (* RUN: grep {%build_trunc = trunc i32 %P1 to i8} < %t.ll
1007     * RUN: grep {%build_zext = zext i8 %build_trunc to i32} < %t.ll
1008     * RUN: grep {%build_sext = sext i32 %build_zext to i64} < %t.ll
1009     * RUN: grep {%build_uitofp = uitofp i64 %build_sext to float} < %t.ll
1010     * RUN: grep {%build_sitofp = sitofp i32 %build_zext to double} < %t.ll
1011     * RUN: grep {%build_fptoui = fptoui float %build_uitofp to i32} < %t.ll
1012     * RUN: grep {%build_fptosi = fptosi double %build_sitofp to i64} < %t.ll
1013     * RUN: grep {%build_fptrunc = fptrunc double %build_sitofp to float} < %t.ll
1014     * RUN: grep {%build_fpext = fpext float %build_fptrunc to double} < %t.ll
1015     * RUN: grep {%build_inttoptr = inttoptr i32 %P1 to i8\\*} < %t.ll
1016     * RUN: grep {%build_ptrtoint = ptrtoint i8\\* %build_inttoptr to i64} < %t.ll
1017     * RUN: grep {%build_bitcast = bitcast i64 %build_ptrtoint to double} < %t.ll
1018     *)
1019    let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
1020    let inst29 = build_zext inst28 i32_type "build_zext" atentry in
1021    let inst30 = build_sext inst29 i64_type "build_sext" atentry in
1022    let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1023    let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1024    ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1025    ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1026    let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1027    ignore(build_fpext inst35 double_type "build_fpext" atentry);
1028    let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1029    let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1030    ignore(build_bitcast inst38 double_type "build_bitcast" atentry)
1031  end;
1032  
1033  group "comparisons"; begin
1034    (* RUN: grep {%build_icmp_ne = icmp ne i32 %P1, %P2} < %t.ll
1035     * RUN: grep {%build_icmp_sle = icmp sle i32 %P2, %P1} < %t.ll
1036     * RUN: grep {%build_fcmp_false = fcmp false float %F1, %F2} < %t.ll
1037     * RUN: grep {%build_fcmp_true = fcmp true float %F2, %F1} < %t.ll
1038     *)
1039    ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
1040    ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
1041    ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
1042    ignore (build_fcmp Fcmp.True  f2 f1 "build_fcmp_true" atentry)
1043  end;
1044  
1045  group "miscellaneous"; begin
1046    (* RUN: grep {%build_call = tail call cc63 i32 @.*(i32 signext %P2, i32 %P1)} < %t.ll
1047     * RUN: grep {%build_select = select i1 %build_icmp, i32 %P1, i32 %P2} < %t.ll
1048     * RUN: grep {%build_va_arg = va_arg i8\\*\\* null, i32} < %t.ll
1049     * RUN: grep {%build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2} < %t.ll
1050     * RUN: grep {%build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2} < %t.ll
1051     * RUN: grep {%build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>} < %t.ll
1052     *)
1053    let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1054    insist (CallConv.c = instruction_call_conv ci);
1055    set_instruction_call_conv 63 ci;
1056    insist (63 = instruction_call_conv ci);
1057    insist (not (is_tail_call ci));
1058    set_tail_call true ci;
1059    insist (is_tail_call ci);
1060    add_instruction_param_attr ci 1 Attribute.Sext;
1061    add_instruction_param_attr ci 2 Attribute.Noalias;
1062    remove_instruction_param_attr ci 2 Attribute.Noalias;
1063    
1064    let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1065    ignore (build_select inst46 p1 p2 "build_select" atentry);
1066    ignore (build_va_arg
1067      (const_null (pointer_type (pointer_type i8_type)))
1068      i32_type "build_va_arg" atentry);
1069    
1070    (* Set up some vector vregs. *)
1071    let one  = const_int i32_type 1 in
1072    let zero = const_int i32_type 0 in
1073    let t1 = const_vector [| one; zero; one; zero |] in
1074    let t2 = const_vector [| zero; one; zero; one |] in
1075    let t3 = const_vector [| one; one; zero; zero |] in
1076    let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1077    let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1078    
1079    ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1080    ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1081    ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1082  end;
1083
1084  group "metadata"; begin
1085    (* RUN: grep {%metadata = add i32 %P1, %P2, !test !0} < %t.ll
1086     * RUN: grep {!0 = metadata !\{i32 1, metadata !"metadata test"\}} < %t.ll
1087     *)
1088    let i = build_add p1 p2 "metadata" atentry in
1089    insist ((has_metadata i) = false);
1090
1091    let m1 = const_int i32_type 1 in
1092    let m2 = mdstring context "metadata test" in
1093    let md = mdnode context [| m1; m2 |] in
1094
1095    let kind = mdkind_id context "test" in
1096    set_metadata i kind md;
1097
1098    insist ((has_metadata i) = true);
1099    insist ((metadata i kind) = Some md);
1100
1101    clear_metadata i kind;
1102
1103    insist ((has_metadata i) = false);
1104    insist ((metadata i kind) = None);
1105
1106    set_metadata i kind md
1107  end;
1108
1109  group "dbg"; begin
1110    (* RUN: grep {%dbg = add i32 %P1, %P2, !dbg !1} < %t.ll
1111     * RUN: grep {!1 = metadata !\{i32 2, i32 3, metadata !2, metadata !2\}} < %t.ll
1112     *)
1113    insist ((current_debug_location atentry) = None);
1114
1115    let m_line = const_int i32_type 2 in
1116    let m_col = const_int i32_type 3 in
1117    let m_scope = mdnode context [| |] in
1118    let m_inlined = mdnode context [| |] in
1119    let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1120    set_current_debug_location atentry md;
1121
1122    insist ((current_debug_location atentry) = Some md);
1123
1124    let i = build_add p1 p2 "dbg" atentry in
1125    insist ((has_metadata i) = true);
1126
1127    clear_current_debug_location atentry
1128  end;
1129  
1130  group "phi"; begin
1131    (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll
1132     *)
1133    let b1 = append_block context "PhiBlock1" fn in
1134    let b2 = append_block context "PhiBlock2" fn in
1135    
1136    let jb = append_block context "PhiJoinBlock" fn in
1137    ignore (build_br jb (builder_at_end context b1));
1138    ignore (build_br jb (builder_at_end context b2));
1139    let at_jb = builder_at_end context jb in
1140    
1141    let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1142    insist ([(p1, b1)] = incoming phi);
1143    
1144    add_incoming (p2, b2) phi;
1145    insist ([(p1, b1); (p2, b2)] = incoming phi);
1146    
1147    ignore (build_unreachable at_jb);
1148  end
1149
1150
1151(*===-- Pass Managers -----------------------------------------------------===*)
1152
1153let test_pass_manager () =
1154  let (++) x f = ignore (f x); x in
1155
1156  begin group "module pass manager";
1157    ignore (PassManager.create ()
1158             ++ PassManager.run_module m
1159             ++ PassManager.dispose)
1160  end;
1161  
1162  begin group "function pass manager";
1163    let fty = function_type void_type [| |] in
1164    let fn = define_function "FunctionPassManager" fty m in
1165    ignore (build_ret_void (builder_at_end context (entry_block fn)));
1166    
1167    ignore (PassManager.create_function m
1168             ++ PassManager.initialize
1169             ++ PassManager.run_function fn
1170             ++ PassManager.finalize
1171             ++ PassManager.dispose)
1172  end
1173
1174
1175(*===-- Writer ------------------------------------------------------------===*)
1176
1177let test_writer () =
1178  group "valid";
1179  insist (match Llvm_analysis.verify_module m with
1180          | None -> true
1181          | Some msg -> prerr_string msg; false);
1182
1183  group "writer";
1184  insist (write_bitcode_file m filename);
1185  
1186  dispose_module m
1187
1188
1189(*===-- Driver ------------------------------------------------------------===*)
1190
1191let _ =
1192  suite "target"           test_target;
1193  suite "constants"        test_constants;
1194  suite "global values"    test_global_values;
1195  suite "global variables" test_global_variables;
1196  suite "uses"             test_uses;
1197  suite "users"            test_users;
1198  suite "aliases"          test_aliases;
1199  suite "functions"        test_functions;
1200  suite "params"           test_params;
1201  suite "basic blocks"     test_basic_blocks;
1202  suite "instructions"     test_instructions;
1203  suite "builder"          test_builder;
1204  suite "pass manager"     test_pass_manager;
1205  suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1206  exit !exit_status
1207