vmcore.ml revision 1940dd10dd6b79e3332f384756c268b85d0ad400
16d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %s -o %t
26d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) * RUN: ./%t %t.bc
36d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) * RUN: llvm-dis < %t.bc > %t.ll
46d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) *)
56d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
66d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(* Note: It takes several seconds for ocamlopt to link an executable with
76d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         libLLVMCore.a, so it's better to write a big test than a bunch of
86d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)         little ones. *)
96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)open Llvm
116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)open Llvm_bitwriter
126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(* Tiny unit test framework - really just to help find which line is busted *)
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let exit_status = ref 0
166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let suite_name = ref ""
176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let group_name = ref ""
186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let case_num = ref 0
196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let print_checkpoints = false
206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let context = global_context ()
216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let i1_type = Llvm.i1_type context
226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let i8_type = Llvm.i8_type context
236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let i16_type = Llvm.i16_type context
246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let i32_type = Llvm.i32_type context
256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let i64_type = Llvm.i64_type context
266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let void_type = Llvm.void_type context
276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let float_type = Llvm.float_type context
286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let double_type = Llvm.double_type context
296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let fp128_type = Llvm.fp128_type context
306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let group name =
326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group_name := !suite_name ^ "/" ^ name;
336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  case_num := 0;
346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if print_checkpoints then
356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    prerr_endline ("  " ^ name ^ "...")
366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let insist cond =
386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  incr case_num;
396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if not cond then
406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    exit_status := 10;
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  match print_checkpoints, cond with
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  | false, true -> ()
436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  | false, false ->
446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  | true, true ->
466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      prerr_endline ("    " ^ (string_of_int !case_num))
476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  | true, false ->
486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let suite name f =
516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  suite_name := name;
526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if print_checkpoints then
536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    prerr_endline (name ^ ":");
546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  f ()
556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(*===-- Fixture -----------------------------------------------------------===*)
586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let filename = Sys.argv.(1)
606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let m = create_module context filename
616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let mp = ModuleProvider.create m
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(*===-- Target ------------------------------------------------------------===*)
656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let test_target () =
676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  begin group "triple";
686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    (* RUN: grep "i686-apple-darwin8" < %t.ll
696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)     *)
706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    let trip = "i686-apple-darwin8" in
716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    set_target_triple trip m;
726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    insist (trip = target_triple m)
736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  end;
746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  
756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  begin group "layout";
766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    (* RUN: grep "bogus" < %t.ll
776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)     *)
786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    let layout = "bogus" in
796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    set_data_layout layout m;
806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    insist (layout = data_layout m)
816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  end
826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)(*===-- Types -------------------------------------------------------------===*)
846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)let test_types () =
866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {void_type.*void} < %t.ll
876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "void";
896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "void_type" void_type m);
906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (TypeKind.Void == classify_type void_type);
916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {i1_type.*i1} < %t.ll
936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "i1";
956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "i1_type" i1_type m);
966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (TypeKind.Integer == classify_type i1_type);
976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {i32_type.*i32} < %t.ll
996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
1006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "i32";
1016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "i32_type" i32_type m);
1026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {i42_type.*i42} < %t.ll
1046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
1056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "i42";
1066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  let ty = integer_type context 42 in
1076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "i42_type" ty m);
1086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {float_type.*float} < %t.ll
1106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
1116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "float";
1126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "float_type" float_type m);
1136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (TypeKind.Float == classify_type float_type);
1146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {double_type.*double} < %t.ll
1166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
1176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "double";
1186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "double_type" double_type m);
1196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (TypeKind.Double == classify_type double_type);
1206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  (* RUN: grep {function_type.*i32.*i1, double} < %t.ll
1226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)   *)
1236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  group "function";
1246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  let ty = function_type i32_type [| i1_type; double_type |] in
1256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (define_type_name "function_type" ty m);
1266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (TypeKind.Function = classify_type ty);
1276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  insist (not (is_var_arg ty));
128  insist (i32_type == return_type ty);
129  insist (double_type == (param_types ty).(1));
130  
131  (* RUN: grep {var_arg_type.*\.\.\.} < %t.ll
132   *)
133  group "var arg function";
134  let ty = var_arg_function_type void_type [| i32_type |] in
135  insist (define_type_name "var_arg_type" ty m);
136  insist (is_var_arg ty);
137  
138  (* RUN: grep {array_type.*\\\[7 x i8\\\]} < %t.ll
139   *)
140  group "array";
141  let ty = array_type i8_type 7 in
142  insist (define_type_name "array_type" ty m);
143  insist (7 = array_length ty);
144  insist (i8_type == element_type ty);
145  insist (TypeKind.Array == classify_type ty);
146  
147  begin group "pointer";
148    (* RUN: grep {pointer_type.*float\*} < %t.ll
149     *)
150    let ty = pointer_type float_type in
151    insist (define_type_name "pointer_type" ty m);
152    insist (float_type == element_type ty);
153    insist (0 == address_space ty);
154    insist (TypeKind.Pointer == classify_type ty)
155  end;
156  
157  begin group "qualified_pointer";
158    (* RUN: grep {qualified_pointer_type.*i8.*3.*\*} < %t.ll
159     *)
160    let ty = qualified_pointer_type i8_type 3 in
161    insist (define_type_name "qualified_pointer_type" ty m);
162    insist (i8_type == element_type ty);
163    insist (3 == address_space ty)
164  end;
165  
166  (* RUN: grep {vector_type.*\<4 x i16\>} < %t.ll
167   *)
168  group "vector";
169  let ty = vector_type i16_type 4 in
170  insist (define_type_name "vector_type" ty m);
171  insist (i16_type == element_type ty);
172  insist (4 = vector_size ty);
173  
174  (* RUN: grep {opaque_type.*opaque} < %t.ll
175   *)
176  group "opaque";
177  let ty = opaque_type context in
178  insist (define_type_name "opaque_type" ty m);
179  insist (ty == ty);
180  insist (ty <> opaque_type context);
181  
182  (* RUN: grep -v {delete_type} < %t.ll
183   *)
184  group "delete";
185  let ty = opaque_type context in
186  insist (define_type_name "delete_type" ty m);
187  delete_type_name "delete_type" m;
188  
189  (* RUN: grep -v {recursive_type.*recursive_type} < %t.ll
190   *)
191  group "recursive";
192  let ty = opaque_type context in
193  let th = handle_to_type ty in
194  refine_type ty (pointer_type ty);
195  let ty = type_of_handle th in
196  insist (define_type_name "recursive_type" ty m);
197  insist (ty == element_type ty)
198
199
200(*===-- Constants ---------------------------------------------------------===*)
201
202let test_constants () =
203  (* RUN: grep {const_int.*i32.*-1} < %t.ll
204   *)
205  group "int";
206  let c = const_int i32_type (-1) in
207  ignore (define_global "const_int" c m);
208  insist (i32_type = type_of c);
209  insist (is_constant c);
210
211  (* RUN: grep {const_sext_int.*i64.*-1} < %t.ll
212   *)
213  group "sext int";
214  let c = const_int i64_type (-1) in
215  ignore (define_global "const_sext_int" c m);
216  insist (i64_type = type_of c);
217
218  (* RUN: grep {const_zext_int64.*i64.*4294967295} < %t.ll
219   *)
220  group "zext int64";
221  let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
222  ignore (define_global "const_zext_int64" c m);
223  insist (i64_type = type_of c);
224
225  (* RUN: grep {const_int_string.*i32.*-1} < %t.ll
226   *)
227  group "int string";
228  let c = const_int_of_string i32_type "-1" 10 in
229  ignore (define_global "const_int_string" c m);
230  insist (i32_type = type_of c);
231
232  (* RUN: grep {const_string.*"cruel\\\\00world"} < %t.ll
233   *)
234  group "string";
235  let c = const_string context "cruel\000world" in
236  ignore (define_global "const_string" c m);
237  insist ((array_type i8_type 11) = type_of c);
238
239  (* RUN: grep {const_stringz.*"hi\\\\00again\\\\00"} < %t.ll
240   *)
241  group "stringz";
242  let c = const_stringz context "hi\000again" in
243  ignore (define_global "const_stringz" c m);
244  insist ((array_type i8_type 9) = type_of c);
245
246  (* RUN: grep {const_single.*2.75} < %t.ll
247   * RUN: grep {const_double.*3.1459} < %t.ll
248   * RUN: grep {const_double_string.*1.25} < %t.ll
249   *)
250  begin group "real";
251    let cs = const_float float_type 2.75 in
252    ignore (define_global "const_single" cs m);
253    insist (float_type = type_of cs);
254    
255    let cd = const_float double_type 3.1459 in
256    ignore (define_global "const_double" cd m);
257    insist (double_type = type_of cd);
258
259    let cd = const_float_of_string double_type "1.25" in
260    ignore (define_global "const_double_string" cd m);
261    insist (double_type = type_of cd)
262  end;
263  
264  let one = const_int i16_type 1 in
265  let two = const_int i16_type 2 in
266  let three = const_int i32_type 3 in
267  let four = const_int i32_type 4 in
268  
269  (* RUN: grep {const_array.*\\\[i32 3, i32 4\\\]} < %t.ll
270   *)
271  group "array";
272  let c = const_array i32_type [| three; four |] in
273  ignore (define_global "const_array" c m);
274  insist ((array_type i32_type 2) = (type_of c));
275  
276  (* RUN: grep {const_vector.*<i16 1, i16 2.*>} < %t.ll
277   *)
278  group "vector";
279  let c = const_vector [| one; two; one; two;
280                          one; two; one; two |] in
281  ignore (define_global "const_vector" c m);
282  insist ((vector_type i16_type 8) = (type_of c));
283
284  (* RUN: grep {const_structure.*.i16 1, i16 2, i32 3, i32 4} < %t.ll
285   *)
286  group "structure";
287  let c = const_struct context [| one; two; three; four |] in
288  ignore (define_global "const_structure" c m);
289  insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
290        = (type_of c));
291
292  group "union";
293  let t = union_type context [| i1_type; i16_type; i64_type; double_type |] in
294  let c = const_union t one in
295  ignore (define_global "Const_union" c m);
296  insist (t = (type_of c));
297  
298  (* RUN: grep {const_null.*zeroinit} < %t.ll
299   *)
300  group "null";
301  let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
302                                                    double_type |]) in
303  ignore (define_global "const_null" c m);
304  
305  (* RUN: grep {const_all_ones.*-1} < %t.ll
306   *)
307  group "all ones";
308  let c = const_all_ones i64_type in
309  ignore (define_global "const_all_ones" c m);
310  
311  (* RUN: grep {const_undef.*undef} < %t.ll
312   *)
313  group "undef";
314  let c = undef i1_type in
315  ignore (define_global "const_undef" c m);
316  insist (i1_type = type_of c);
317  insist (is_undef c);
318  
319  group "constant arithmetic";
320  (* RUN: grep {@const_neg = global i64 sub} < %t.ll
321   * RUN: grep {@const_not = global i64 xor } < %t.ll
322   * RUN: grep {@const_add = global i64 add } < %t.ll
323   * RUN: grep {@const_sub = global i64 sub } < %t.ll
324   * RUN: grep {@const_mul = global i64 mul } < %t.ll
325   * RUN: grep {@const_udiv = global i64 udiv } < %t.ll
326   * RUN: grep {@const_sdiv = global i64 sdiv } < %t.ll
327   * RUN: grep {@const_fdiv = global double fdiv } < %t.ll
328   * RUN: grep {@const_urem = global i64 urem } < %t.ll
329   * RUN: grep {@const_srem = global i64 srem } < %t.ll
330   * RUN: grep {@const_frem = global double frem } < %t.ll
331   * RUN: grep {@const_and = global i64 and } < %t.ll
332   * RUN: grep {@const_or = global i64 or } < %t.ll
333   * RUN: grep {@const_xor = global i64 xor } < %t.ll
334   * RUN: grep {@const_icmp = global i1 icmp sle } < %t.ll
335   * RUN: grep {@const_fcmp = global i1 fcmp ole } < %t.ll
336   *)
337  let void_ptr = pointer_type i8_type in
338  let five = const_int i64_type 5 in
339  let ffive = const_uitofp five double_type in
340  let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
341  let foldbomb = const_ptrtoint foldbomb_gv i64_type in
342  let ffoldbomb = const_uitofp foldbomb double_type in
343  ignore (define_global "const_neg" (const_neg foldbomb) m);
344  ignore (define_global "const_not" (const_not foldbomb) m);
345  ignore (define_global "const_add" (const_add foldbomb five) m);
346  ignore (define_global "const_sub" (const_sub foldbomb five) m);
347  ignore (define_global "const_mul" (const_mul foldbomb five) m);
348  ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
349  ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
350  ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
351  ignore (define_global "const_urem" (const_urem foldbomb five) m);
352  ignore (define_global "const_srem" (const_srem foldbomb five) m);
353  ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
354  ignore (define_global "const_and" (const_and foldbomb five) m);
355  ignore (define_global "const_or" (const_or foldbomb five) m);
356  ignore (define_global "const_xor" (const_xor foldbomb five) m);
357  ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
358  ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
359  
360  group "constant casts";
361  (* RUN: grep {const_trunc.*trunc} < %t.ll
362   * RUN: grep {const_sext.*sext} < %t.ll
363   * RUN: grep {const_zext.*zext} < %t.ll
364   * RUN: grep {const_fptrunc.*fptrunc} < %t.ll
365   * RUN: grep {const_fpext.*fpext} < %t.ll
366   * RUN: grep {const_uitofp.*uitofp} < %t.ll
367   * RUN: grep {const_sitofp.*sitofp} < %t.ll
368   * RUN: grep {const_fptoui.*fptoui} < %t.ll
369   * RUN: grep {const_fptosi.*fptosi} < %t.ll
370   * RUN: grep {const_ptrtoint.*ptrtoint} < %t.ll
371   * RUN: grep {const_inttoptr.*inttoptr} < %t.ll
372   * RUN: grep {const_bitcast.*bitcast} < %t.ll
373   *)
374  let i128_type = integer_type context 128 in
375  ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
376                                               i8_type) m);
377  ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
378  ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
379  ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
380  ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
381  ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
382  ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
383  ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
384  ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
385  ignore (define_global "const_ptrtoint" (const_ptrtoint 
386    (const_gep (const_null (pointer_type i8_type))
387               [| const_int i32_type 1 |])
388    i32_type) m);
389  ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
390                                                  void_ptr) m);
391  ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
392  
393  group "misc constants";
394  (* RUN: grep {const_size_of.*getelementptr.*null} < %t.ll
395   * RUN: grep {const_gep.*getelementptr} < %t.ll
396   * RUN: grep {const_select.*select} < %t.ll
397   * RUN: grep {const_extractelement.*extractelement} < %t.ll
398   * RUN: grep {const_insertelement.*insertelement} < %t.ll
399   * RUN: grep {const_shufflevector.*shufflevector} < %t.ll
400   *)
401  ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
402  ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
403  ignore (define_global "const_select" (const_select
404    (const_icmp Icmp.Sle foldbomb five)
405    (const_int i8_type (-1))
406    (const_int i8_type 0)) m);
407  let zero = const_int i32_type 0 in
408  let one  = const_int i32_type 1 in
409  ignore (define_global "const_extractelement" (const_extractelement
410    (const_vector [| zero; one; zero; one |])
411    (const_trunc foldbomb i32_type)) m);
412  ignore (define_global "const_insertelement" (const_insertelement
413    (const_vector [| zero; one; zero; one |])
414    zero (const_trunc foldbomb i32_type)) m);
415  ignore (define_global "const_shufflevector" (const_shufflevector
416    (const_vector [| zero; one |])
417    (const_vector [| one; zero |])
418    (const_bitcast foldbomb (vector_type i32_type 2))) m)
419
420
421(*===-- Global Values -----------------------------------------------------===*)
422
423let test_global_values () =
424  let (++) x f = f x; x in
425  let zero32 = const_null i32_type in
426
427  (* RUN: grep {GVal01} < %t.ll
428   *)
429  group "naming";
430  let g = define_global "TEMPORARY" zero32 m in
431  insist ("TEMPORARY" = value_name g);
432  set_value_name "GVal01" g;
433  insist ("GVal01" = value_name g);
434
435  (* RUN: grep {GVal02.*linkonce} < %t.ll
436   *)
437  group "linkage";
438  let g = define_global "GVal02" zero32 m ++
439          set_linkage Linkage.Link_once in
440  insist (Linkage.Link_once = linkage g);
441
442  (* RUN: grep {GVal03.*Hanalei} < %t.ll
443   *)
444  group "section";
445  let g = define_global "GVal03" zero32 m ++
446          set_section "Hanalei" in
447  insist ("Hanalei" = section g);
448  
449  (* RUN: grep {GVal04.*hidden} < %t.ll
450   *)
451  group "visibility";
452  let g = define_global "GVal04" zero32 m ++
453          set_visibility Visibility.Hidden in
454  insist (Visibility.Hidden = visibility g);
455  
456  (* RUN: grep {GVal05.*align 128} < %t.ll
457   *)
458  group "alignment";
459  let g = define_global "GVal05" zero32 m ++
460          set_alignment 128 in
461  insist (128 = alignment g)
462
463
464(*===-- Global Variables --------------------------------------------------===*)
465
466let test_global_variables () =
467  let (++) x f = f x; x in
468  let fourty_two32 = const_int i32_type 42 in
469
470  (* RUN: grep {GVar01.*external} < %t.ll
471   *)
472  group "declarations";
473  insist (None == lookup_global "GVar01" m);
474  let g = declare_global i32_type "GVar01" m in
475  insist (is_declaration g);
476  insist (pointer_type float_type ==
477            type_of (declare_global float_type "GVar01" m));
478  insist (g == declare_global i32_type "GVar01" m);
479  insist (match lookup_global "GVar01" m with Some x -> x = g
480                                            | None -> false);
481  
482  (* RUN: grep {GVar02.*42} < %t.ll
483   * RUN: grep {GVar03.*42} < %t.ll
484   *)
485  group "definitions";
486  let g = define_global "GVar02" fourty_two32 m in
487  let g2 = declare_global i32_type "GVar03" m ++
488           set_initializer fourty_two32 in
489  insist (not (is_declaration g));
490  insist (not (is_declaration g2));
491  insist ((global_initializer g) == (global_initializer g2));
492
493  (* RUN: grep {GVar04.*thread_local} < %t.ll
494   *)
495  group "threadlocal";
496  let g = define_global "GVar04" fourty_two32 m ++
497          set_thread_local true in
498  insist (is_thread_local g);
499
500  (* RUN: grep -v {GVar05} < %t.ll
501   *)
502  group "delete";
503  let g = define_global "GVar05" fourty_two32 m in
504  delete_global g;
505
506  (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll
507   *)
508  group "constant";
509  let g = define_global "ConstGlobalVar" fourty_two32 m in
510  insist (not (is_global_constant g));
511  set_global_constant true g;
512  insist (is_global_constant g);
513  
514  begin group "iteration";
515    let m = create_module context "temp" in
516    
517    insist (At_end m = global_begin m);
518    insist (At_start m = global_end m);
519    
520    let g1 = declare_global i32_type "One" m in
521    let g2 = declare_global i32_type "Two" m in
522    
523    insist (Before g1 = global_begin m);
524    insist (Before g2 = global_succ g1);
525    insist (At_end m = global_succ g2);
526    
527    insist (After g2 = global_end m);
528    insist (After g1 = global_pred g2);
529    insist (At_start m = global_pred g1);
530    
531    let lf s x = s ^ "->" ^ value_name x in
532    insist ("->One->Two" = fold_left_globals lf "" m);
533    
534    let rf x s = value_name x ^ "<-" ^ s in
535    insist ("One<-Two<-" = fold_right_globals rf m "");
536    
537    dispose_module m
538  end
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 "invoke"; begin
876    (* RUN: grep {build_invoke.*invoke.*P1.*P2} < %t.ll
877     * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll
878     *)
879    let bb04 = append_block context "Bb04" fn in
880    let b = builder_at_end context bb04 in
881    ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "build_invoke" b)
882  end;
883  
884  group "unwind"; begin
885    (* RUN: grep {unwind} < %t.ll
886     *)
887    let bb05 = append_block context "Bb05" fn in
888    let b = builder_at_end context bb05 in
889    ignore (build_unwind b)
890  end;
891  
892  group "unreachable"; begin
893    (* RUN: grep {unreachable} < %t.ll
894     *)
895    let bb06 = append_block context "Bb06" fn in
896    let b = builder_at_end context bb06 in
897    ignore (build_unreachable b)
898  end;
899  
900  group "arithmetic"; begin
901    let bb07 = append_block context "Bb07" fn in
902    let b = builder_at_end context bb07 in
903    
904    (* RUN: grep {%build_add = add i32 %P1, %P2} < %t.ll
905     * RUN: grep {%build_sub = sub i32 %P1, %P2} < %t.ll
906     * RUN: grep {%build_mul = mul i32 %P1, %P2} < %t.ll
907     * RUN: grep {%build_udiv = udiv i32 %P1, %P2} < %t.ll
908     * RUN: grep {%build_sdiv = sdiv i32 %P1, %P2} < %t.ll
909     * RUN: grep {%build_fdiv = fdiv float %F1, %F2} < %t.ll
910     * RUN: grep {%build_urem = urem i32 %P1, %P2} < %t.ll
911     * RUN: grep {%build_srem = srem i32 %P1, %P2} < %t.ll
912     * RUN: grep {%build_frem = frem float %F1, %F2} < %t.ll
913     * RUN: grep {%build_shl = shl i32 %P1, %P2} < %t.ll
914     * RUN: grep {%build_lshl = lshr i32 %P1, %P2} < %t.ll
915     * RUN: grep {%build_ashl = ashr i32 %P1, %P2} < %t.ll
916     * RUN: grep {%build_and = and i32 %P1, %P2} < %t.ll
917     * RUN: grep {%build_or = or i32 %P1, %P2} < %t.ll
918     * RUN: grep {%build_xor = xor i32 %P1, %P2} < %t.ll
919     * RUN: grep {%build_neg = sub i32 0, %P1} < %t.ll
920     * RUN: grep {%build_not = xor i32 %P1, -1} < %t.ll
921     *)
922    ignore (build_add p1 p2 "build_add" b);
923    ignore (build_sub p1 p2 "build_sub" b);
924    ignore (build_mul p1 p2 "build_mul" b);
925    ignore (build_udiv p1 p2 "build_udiv" b);
926    ignore (build_sdiv p1 p2 "build_sdiv" b);
927    ignore (build_fdiv f1 f2 "build_fdiv" b);
928    ignore (build_urem p1 p2 "build_urem" b);
929    ignore (build_srem p1 p2 "build_srem" b);
930    ignore (build_frem f1 f2 "build_frem" b);
931    ignore (build_shl p1 p2 "build_shl" b);
932    ignore (build_lshr p1 p2 "build_lshl" b);
933    ignore (build_ashr p1 p2 "build_ashl" b);
934    ignore (build_and p1 p2 "build_and" b);
935    ignore (build_or p1 p2 "build_or" b);
936    ignore (build_xor p1 p2 "build_xor" b);
937    ignore (build_neg p1 "build_neg" b);
938    ignore (build_not p1 "build_not" b);
939    ignore (build_unreachable b)
940  end;
941  
942  group "memory"; begin
943    let bb08 = append_block context "Bb08" fn in
944    let b = builder_at_end context bb08 in
945
946    (* RUN: grep {%build_alloca = alloca i32} < %t.ll
947     * RUN: grep {%build_array_alloca = alloca i32, i32 %P2} < %t.ll
948     * RUN: grep {%build_load = load i32\\* %build_array_alloca} < %t.ll
949     * RUN: grep {store i32 %P2, i32\\* %build_alloca} < %t.ll
950     * RUN: grep {%build_gep = getelementptr i32\\* %build_array_alloca, i32 %P2} < %t.ll
951     *)
952    let alloca = build_alloca i32_type "build_alloca" b in
953    let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
954    ignore(build_load array_alloca "build_load" b);
955    ignore(build_store p2 alloca b);
956    ignore(build_gep array_alloca [| p2 |] "build_gep" b);
957    ignore(build_unreachable b)
958  end;
959  
960  group "casts"; begin
961    let void_ptr = pointer_type i8_type in
962    
963    (* RUN: grep {%build_trunc = trunc i32 %P1 to i8} < %t.ll
964     * RUN: grep {%build_zext = zext i8 %build_trunc to i32} < %t.ll
965     * RUN: grep {%build_sext = sext i32 %build_zext to i64} < %t.ll
966     * RUN: grep {%build_uitofp = uitofp i64 %build_sext to float} < %t.ll
967     * RUN: grep {%build_sitofp = sitofp i32 %build_zext to double} < %t.ll
968     * RUN: grep {%build_fptoui = fptoui float %build_uitofp to i32} < %t.ll
969     * RUN: grep {%build_fptosi = fptosi double %build_sitofp to i64} < %t.ll
970     * RUN: grep {%build_fptrunc = fptrunc double %build_sitofp to float} < %t.ll
971     * RUN: grep {%build_fpext = fpext float %build_fptrunc to double} < %t.ll
972     * RUN: grep {%build_inttoptr = inttoptr i32 %P1 to i8\\*} < %t.ll
973     * RUN: grep {%build_ptrtoint = ptrtoint i8\\* %build_inttoptr to i64} < %t.ll
974     * RUN: grep {%build_bitcast = bitcast i64 %build_ptrtoint to double} < %t.ll
975     *)
976    let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
977    let inst29 = build_zext inst28 i32_type "build_zext" atentry in
978    let inst30 = build_sext inst29 i64_type "build_sext" atentry in
979    let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
980    let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
981    ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
982    ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
983    let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
984    ignore(build_fpext inst35 double_type "build_fpext" atentry);
985    let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
986    let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
987    ignore(build_bitcast inst38 double_type "build_bitcast" atentry)
988  end;
989  
990  group "comparisons"; begin
991    (* RUN: grep {%build_icmp_ne = icmp ne i32 %P1, %P2} < %t.ll
992     * RUN: grep {%build_icmp_sle = icmp sle i32 %P2, %P1} < %t.ll
993     * RUN: grep {%build_icmp_false = fcmp false float %F1, %F2} < %t.ll
994     * RUN: grep {%build_icmp_true = fcmp true float %F2, %F1} < %t.ll
995     *)
996    ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
997    ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
998    ignore (build_fcmp Fcmp.False f1 f2 "build_icmp_false" atentry);
999    ignore (build_fcmp Fcmp.True  f2 f1 "build_icmp_true" atentry)
1000  end;
1001  
1002  group "miscellaneous"; begin
1003    (* RUN: grep {%build_call = tail call cc63 i32 @.*(i32 signext %P2, i32 %P1)} < %t.ll
1004     * RUN: grep {%build_select = select i1 %build_icmp, i32 %P1, i32 %P2} < %t.ll
1005     * RUN: grep {%build_va_arg = va_arg i8\\*\\* null, i32} < %t.ll
1006     * RUN: grep {%build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2} < %t.ll
1007     * RUN: grep {%build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2} < %t.ll
1008     * 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
1009     *)
1010    let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1011    insist (CallConv.c = instruction_call_conv ci);
1012    set_instruction_call_conv 63 ci;
1013    insist (63 = instruction_call_conv ci);
1014    insist (not (is_tail_call ci));
1015    set_tail_call true ci;
1016    insist (is_tail_call ci);
1017    add_instruction_param_attr ci 1 Attribute.Sext;
1018    add_instruction_param_attr ci 2 Attribute.Noalias;
1019    remove_instruction_param_attr ci 2 Attribute.Noalias;
1020    
1021    let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1022    ignore (build_select inst46 p1 p2 "build_select" atentry);
1023    ignore (build_va_arg
1024      (const_null (pointer_type (pointer_type i8_type)))
1025      i32_type "build_va_arg" atentry);
1026    
1027    (* Set up some vector vregs. *)
1028    let one  = const_int i32_type 1 in
1029    let zero = const_int i32_type 0 in
1030    let t1 = const_vector [| one; zero; one; zero |] in
1031    let t2 = const_vector [| zero; one; zero; one |] in
1032    let t3 = const_vector [| one; one; zero; zero |] in
1033    let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1034    let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1035    
1036    ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1037    ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1038    ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1039  end;
1040  
1041  group "phi"; begin
1042    (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll
1043     *)
1044    let b1 = append_block context "PhiBlock1" fn in
1045    let b2 = append_block context "PhiBlock2" fn in
1046    
1047    let jb = append_block context "PhiJoinBlock" fn in
1048    ignore (build_br jb (builder_at_end context b1));
1049    ignore (build_br jb (builder_at_end context b2));
1050    let at_jb = builder_at_end context jb in
1051    
1052    let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1053    insist ([(p1, b1)] = incoming phi);
1054    
1055    add_incoming (p2, b2) phi;
1056    insist ([(p1, b1); (p2, b2)] = incoming phi);
1057    
1058    ignore (build_unreachable at_jb);
1059  end
1060
1061
1062(*===-- Module Provider ---------------------------------------------------===*)
1063
1064let test_module_provider () =
1065  let m = create_module context "test" in
1066  let mp = ModuleProvider.create m in
1067  ModuleProvider.dispose mp
1068
1069
1070(*===-- Pass Managers -----------------------------------------------------===*)
1071
1072let test_pass_manager () =
1073  let (++) x f = ignore (f x); x in
1074
1075  begin group "module pass manager";
1076    ignore (PassManager.create ()
1077             ++ PassManager.run_module m
1078             ++ PassManager.dispose)
1079  end;
1080  
1081  begin group "function pass manager";
1082    let fty = function_type void_type [| |] in
1083    let fn = define_function "FunctionPassManager" fty m in
1084    ignore (build_ret_void (builder_at_end context (entry_block fn)));
1085    
1086    ignore (PassManager.create_function mp
1087             ++ PassManager.initialize
1088             ++ PassManager.run_function fn
1089             ++ PassManager.finalize
1090             ++ PassManager.dispose)
1091  end
1092
1093
1094(*===-- Writer ------------------------------------------------------------===*)
1095
1096let test_writer () =
1097  group "valid";
1098  insist (match Llvm_analysis.verify_module m with
1099          | None -> true
1100          | Some msg -> prerr_string msg; false);
1101
1102  group "writer";
1103  insist (write_bitcode_file m filename);
1104  
1105  ModuleProvider.dispose mp
1106
1107
1108(*===-- Driver ------------------------------------------------------------===*)
1109
1110let _ =
1111  suite "target"           test_target;
1112  suite "types"            test_types;
1113  suite "constants"        test_constants;
1114  suite "global values"    test_global_values;
1115  suite "global variables" test_global_variables;
1116  suite "functions"        test_functions;
1117  suite "params"           test_params;
1118  suite "basic blocks"     test_basic_blocks;
1119  suite "instructions"     test_instructions;
1120  suite "builder"          test_builder;
1121  suite "module provider"  test_module_provider;
1122  suite "pass manager"     test_pass_manager;
1123  suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1124  exit !exit_status
1125