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