Add metadata functions to llvm-c and 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 -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
202 let 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_nsw_neg = global i64 sub nsw } < %t.ll
322    * RUN: grep {@const_nuw_neg = global i64 sub nuw } < %t.ll
323    * RUN: grep {@const_fneg = global double fsub } < %t.ll
324    * RUN: grep {@const_not = global i64 xor } < %t.ll
325    * RUN: grep {@const_add = global i64 add } < %t.ll
326    * RUN: grep {@const_nsw_add = global i64 add nsw } < %t.ll
327    * RUN: grep {@const_nuw_add = global i64 add nuw } < %t.ll
328    * RUN: grep {@const_fadd = global double fadd } < %t.ll
329    * RUN: grep {@const_sub = global i64 sub } < %t.ll
330    * RUN: grep {@const_nsw_sub = global i64 sub nsw } < %t.ll
331    * RUN: grep {@const_nuw_sub = global i64 sub nuw } < %t.ll
332    * RUN: grep {@const_fsub = global double fsub } < %t.ll
333    * RUN: grep {@const_mul = global i64 mul } < %t.ll
334    * RUN: grep {@const_nsw_mul = global i64 mul nsw } < %t.ll
335    * RUN: grep {@const_nuw_mul = global i64 mul nuw } < %t.ll
336    * RUN: grep {@const_fmul = global double fmul } < %t.ll
337    * RUN: grep {@const_udiv = global i64 udiv } < %t.ll
338    * RUN: grep {@const_sdiv = global i64 sdiv } < %t.ll
339    * RUN: grep {@const_exact_sdiv = global i64 sdiv exact } < %t.ll
340    * RUN: grep {@const_fdiv = global double fdiv } < %t.ll
341    * RUN: grep {@const_urem = global i64 urem } < %t.ll
342    * RUN: grep {@const_srem = global i64 srem } < %t.ll
343    * RUN: grep {@const_frem = global double frem } < %t.ll
344    * RUN: grep {@const_and = global i64 and } < %t.ll
345    * RUN: grep {@const_or = global i64 or } < %t.ll
346    * RUN: grep {@const_xor = global i64 xor } < %t.ll
347    * RUN: grep {@const_icmp = global i1 icmp sle } < %t.ll
348    * RUN: grep {@const_fcmp = global i1 fcmp ole } < %t.ll
349    *)
350   let void_ptr = pointer_type i8_type in
351   let five = const_int i64_type 5 in
352   let ffive = const_uitofp five double_type in
353   let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
354   let foldbomb = const_ptrtoint foldbomb_gv i64_type in
355   let ffoldbomb = const_uitofp foldbomb double_type in
356   ignore (define_global "const_neg" (const_neg foldbomb) m);
357   ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
358   ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
359   ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
360   ignore (define_global "const_not" (const_not foldbomb) m);
361   ignore (define_global "const_add" (const_add foldbomb five) m);
362   ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
363   ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
364   ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
365   ignore (define_global "const_sub" (const_sub foldbomb five) m);
366   ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
367   ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
368   ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
369   ignore (define_global "const_mul" (const_mul foldbomb five) m);
370   ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
371   ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
372   ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
373   ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
374   ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
375   ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
376   ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
377   ignore (define_global "const_urem" (const_urem foldbomb five) m);
378   ignore (define_global "const_srem" (const_srem foldbomb five) m);
379   ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
380   ignore (define_global "const_and" (const_and foldbomb five) m);
381   ignore (define_global "const_or" (const_or foldbomb five) m);
382   ignore (define_global "const_xor" (const_xor foldbomb five) m);
383   ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
384   ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
385   
386   group "constant casts";
387   (* RUN: grep {const_trunc.*trunc} < %t.ll
388    * RUN: grep {const_sext.*sext} < %t.ll
389    * RUN: grep {const_zext.*zext} < %t.ll
390    * RUN: grep {const_fptrunc.*fptrunc} < %t.ll
391    * RUN: grep {const_fpext.*fpext} < %t.ll
392    * RUN: grep {const_uitofp.*uitofp} < %t.ll
393    * RUN: grep {const_sitofp.*sitofp} < %t.ll
394    * RUN: grep {const_fptoui.*fptoui} < %t.ll
395    * RUN: grep {const_fptosi.*fptosi} < %t.ll
396    * RUN: grep {const_ptrtoint.*ptrtoint} < %t.ll
397    * RUN: grep {const_inttoptr.*inttoptr} < %t.ll
398    * RUN: grep {const_bitcast.*bitcast} < %t.ll
399    *)
400   let i128_type = integer_type context 128 in
401   ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
402                                                i8_type) m);
403   ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
404   ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
405   ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
406   ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
407   ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
408   ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
409   ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
410   ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
411   ignore (define_global "const_ptrtoint" (const_ptrtoint 
412     (const_gep (const_null (pointer_type i8_type))
413                [| const_int i32_type 1 |])
414     i32_type) m);
415   ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
416                                                   void_ptr) m);
417   ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
418   
419   group "misc constants";
420   (* RUN: grep {const_size_of.*getelementptr.*null} < %t.ll
421    * RUN: grep {const_gep.*getelementptr} < %t.ll
422    * RUN: grep {const_select.*select} < %t.ll
423    * RUN: grep {const_extractelement.*extractelement} < %t.ll
424    * RUN: grep {const_insertelement.*insertelement} < %t.ll
425    * RUN: grep {const_shufflevector.*shufflevector} < %t.ll
426    *)
427   ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
428   ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
429   ignore (define_global "const_select" (const_select
430     (const_icmp Icmp.Sle foldbomb five)
431     (const_int i8_type (-1))
432     (const_int i8_type 0)) m);
433   let zero = const_int i32_type 0 in
434   let one  = const_int i32_type 1 in
435   ignore (define_global "const_extractelement" (const_extractelement
436     (const_vector [| zero; one; zero; one |])
437     (const_trunc foldbomb i32_type)) m);
438   ignore (define_global "const_insertelement" (const_insertelement
439     (const_vector [| zero; one; zero; one |])
440     zero (const_trunc foldbomb i32_type)) m);
441   ignore (define_global "const_shufflevector" (const_shufflevector
442     (const_vector [| zero; one |])
443     (const_vector [| one; zero |])
444     (const_bitcast foldbomb (vector_type i32_type 2))) m)
445
446
447 (*===-- Global Values -----------------------------------------------------===*)
448
449 let test_global_values () =
450   let (++) x f = f x; x in
451   let zero32 = const_null i32_type in
452
453   (* RUN: grep {GVal01} < %t.ll
454    *)
455   group "naming";
456   let g = define_global "TEMPORARY" zero32 m in
457   insist ("TEMPORARY" = value_name g);
458   set_value_name "GVal01" g;
459   insist ("GVal01" = value_name g);
460
461   (* RUN: grep {GVal02.*linkonce} < %t.ll
462    *)
463   group "linkage";
464   let g = define_global "GVal02" zero32 m ++
465           set_linkage Linkage.Link_once in
466   insist (Linkage.Link_once = linkage g);
467
468   (* RUN: grep {GVal03.*Hanalei} < %t.ll
469    *)
470   group "section";
471   let g = define_global "GVal03" zero32 m ++
472           set_section "Hanalei" in
473   insist ("Hanalei" = section g);
474   
475   (* RUN: grep {GVal04.*hidden} < %t.ll
476    *)
477   group "visibility";
478   let g = define_global "GVal04" zero32 m ++
479           set_visibility Visibility.Hidden in
480   insist (Visibility.Hidden = visibility g);
481   
482   (* RUN: grep {GVal05.*align 128} < %t.ll
483    *)
484   group "alignment";
485   let g = define_global "GVal05" zero32 m ++
486           set_alignment 128 in
487   insist (128 = alignment g)
488
489
490 (*===-- Global Variables --------------------------------------------------===*)
491
492 let test_global_variables () =
493   let (++) x f = f x; x in
494   let fourty_two32 = const_int i32_type 42 in
495
496   (* RUN: grep {GVar01.*external} < %t.ll
497    *)
498   group "declarations";
499   insist (None == lookup_global "GVar01" m);
500   let g = declare_global i32_type "GVar01" m in
501   insist (is_declaration g);
502   insist (pointer_type float_type ==
503             type_of (declare_global float_type "GVar01" m));
504   insist (g == declare_global i32_type "GVar01" m);
505   insist (match lookup_global "GVar01" m with Some x -> x = g
506                                             | None -> false);
507   
508   (* RUN: grep {GVar02.*42} < %t.ll
509    * RUN: grep {GVar03.*42} < %t.ll
510    *)
511   group "definitions";
512   let g = define_global "GVar02" fourty_two32 m in
513   let g2 = declare_global i32_type "GVar03" m ++
514            set_initializer fourty_two32 in
515   insist (not (is_declaration g));
516   insist (not (is_declaration g2));
517   insist ((global_initializer g) == (global_initializer g2));
518
519   (* RUN: grep {GVar04.*thread_local} < %t.ll
520    *)
521   group "threadlocal";
522   let g = define_global "GVar04" fourty_two32 m ++
523           set_thread_local true in
524   insist (is_thread_local g);
525
526   (* RUN: grep -v {GVar05} < %t.ll
527    *)
528   group "delete";
529   let g = define_global "GVar05" fourty_two32 m in
530   delete_global g;
531
532   (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll
533    *)
534   group "constant";
535   let g = define_global "ConstGlobalVar" fourty_two32 m in
536   insist (not (is_global_constant g));
537   set_global_constant true g;
538   insist (is_global_constant g);
539   
540   begin group "iteration";
541     let m = create_module context "temp" in
542     
543     insist (At_end m = global_begin m);
544     insist (At_start m = global_end m);
545     
546     let g1 = declare_global i32_type "One" m in
547     let g2 = declare_global i32_type "Two" m in
548     
549     insist (Before g1 = global_begin m);
550     insist (Before g2 = global_succ g1);
551     insist (At_end m = global_succ g2);
552     
553     insist (After g2 = global_end m);
554     insist (After g1 = global_pred g2);
555     insist (At_start m = global_pred g1);
556     
557     let lf s x = s ^ "->" ^ value_name x in
558     insist ("->One->Two" = fold_left_globals lf "" m);
559     
560     let rf x s = value_name x ^ "<-" ^ s in
561     insist ("One<-Two<-" = fold_right_globals rf m "");
562     
563     dispose_module m
564   end
565
566
567 (*===-- Functions ---------------------------------------------------------===*)
568
569 let test_functions () =
570   let ty = function_type i32_type [| i32_type; i64_type |] in
571   let ty2 = function_type i8_type [| i8_type; i64_type |] in
572   
573   (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
574    *)
575   begin group "declare";
576     insist (None = lookup_function "Fn1" m);
577     let fn = declare_function "Fn1" ty m in
578     insist (pointer_type ty = type_of fn);
579     insist (is_declaration fn);
580     insist (0 = Array.length (basic_blocks fn));
581     insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
582     insist (fn == declare_function "Fn1" ty m);
583     insist (None <> lookup_function "Fn1" m);
584     insist (match lookup_function "Fn1" m with Some x -> x = fn
585                                              | None -> false);
586     insist (m == global_parent fn)
587   end;
588   
589   (* RUN: grep -v {Fn2} < %t.ll
590    *)
591   group "delete";
592   let fn = declare_function "Fn2" ty m in
593   delete_function fn;
594   
595   (* RUN: grep {define.*Fn3} < %t.ll
596    *)
597   group "define";
598   let fn = define_function "Fn3" ty m in
599   insist (not (is_declaration fn));
600   insist (1 = Array.length (basic_blocks fn));
601   ignore (build_unreachable (builder_at_end context (entry_block fn)));
602   
603   (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll
604    *)
605   group "params";
606   let fn = define_function "Fn4" ty m in
607   let params = params fn in
608   insist (2 = Array.length params);
609   insist (params.(0) = param fn 0);
610   insist (params.(1) = param fn 1);
611   insist (i32_type = type_of params.(0));
612   insist (i64_type = type_of params.(1));
613   set_value_name "Param1" params.(0);
614   set_value_name "Param2" params.(1);
615   ignore (build_unreachable (builder_at_end context (entry_block fn)));
616   
617   (* RUN: grep {fastcc.*Fn5} < %t.ll
618    *)
619   group "callconv";
620   let fn = define_function "Fn5" ty m in
621   insist (CallConv.c = function_call_conv fn);
622   set_function_call_conv CallConv.fast fn;
623   insist (CallConv.fast = function_call_conv fn);
624   ignore (build_unreachable (builder_at_end context (entry_block fn)));
625   
626   begin group "gc";
627     (* RUN: grep {Fn6.*gc.*shadowstack} < %t.ll
628      *)
629     let fn = define_function "Fn6" ty m in
630     insist (None = gc fn);
631     set_gc (Some "ocaml") fn;
632     insist (Some "ocaml" = gc fn);
633     set_gc None fn;
634     insist (None = gc fn);
635     set_gc (Some "shadowstack") fn;
636     ignore (build_unreachable (builder_at_end context (entry_block fn)));
637   end;
638   
639   begin group "iteration";
640     let m = create_module context "temp" in
641     
642     insist (At_end m = function_begin m);
643     insist (At_start m = function_end m);
644     
645     let f1 = define_function "One" ty m in
646     let f2 = define_function "Two" ty m in
647     
648     insist (Before f1 = function_begin m);
649     insist (Before f2 = function_succ f1);
650     insist (At_end m = function_succ f2);
651     
652     insist (After f2 = function_end m);
653     insist (After f1 = function_pred f2);
654     insist (At_start m = function_pred f1);
655     
656     let lf s x = s ^ "->" ^ value_name x in
657     insist ("->One->Two" = fold_left_functions lf "" m);
658     
659     let rf x s = value_name x ^ "<-" ^ s in
660     insist ("One<-Two<-" = fold_right_functions rf m "");
661     
662     dispose_module m
663   end
664
665
666 (*===-- Params ------------------------------------------------------------===*)
667
668 let test_params () =
669   begin group "iteration";
670     let m = create_module context "temp" in
671     
672     let vf = define_function "void" (function_type void_type [| |]) m in
673     
674     insist (At_end vf = param_begin vf);
675     insist (At_start vf = param_end vf);
676     
677     let ty = function_type void_type [| i32_type; i32_type |] in
678     let f = define_function "f" ty m in
679     let p1 = param f 0 in
680     let p2 = param f 1 in
681     set_value_name "One" p1;
682     set_value_name "Two" p2;
683     add_param_attr p1 Attribute.Sext;
684     add_param_attr p2 Attribute.Noalias;
685     remove_param_attr p2 Attribute.Noalias;
686     add_function_attr f Attribute.Nounwind;
687     add_function_attr f Attribute.Noreturn;
688     remove_function_attr f Attribute.Noreturn;
689
690     insist (Before p1 = param_begin f);
691     insist (Before p2 = param_succ p1);
692     insist (At_end f = param_succ p2);
693     
694     insist (After p2 = param_end f);
695     insist (After p1 = param_pred p2);
696     insist (At_start f = param_pred p1);
697     
698     let lf s x = s ^ "->" ^ value_name x in
699     insist ("->One->Two" = fold_left_params lf "" f);
700     
701     let rf x s = value_name x ^ "<-" ^ s in
702     insist ("One<-Two<-" = fold_right_params rf f "");
703     
704     dispose_module m
705   end
706
707
708 (*===-- Basic Blocks ------------------------------------------------------===*)
709
710 let test_basic_blocks () =
711   let ty = function_type void_type [| |] in
712   
713   (* RUN: grep {Bb1} < %t.ll
714    *)
715   group "entry";
716   let fn = declare_function "X" ty m in
717   let bb = append_block context "Bb1" fn in
718   insist (bb = entry_block fn);
719   ignore (build_unreachable (builder_at_end context bb));
720   
721   (* RUN: grep -v Bb2 < %t.ll
722    *)
723   group "delete";
724   let fn = declare_function "X2" ty m in
725   let bb = append_block context "Bb2" fn in
726   delete_block bb;
727   
728   group "insert";
729   let fn = declare_function "X3" ty m in
730   let bbb = append_block context "b" fn in
731   let bba = insert_block context "a" bbb in
732   insist ([| bba; bbb |] = basic_blocks fn);
733   ignore (build_unreachable (builder_at_end context bba));
734   ignore (build_unreachable (builder_at_end context bbb));
735   
736   (* RUN: grep Bb3 < %t.ll
737    *)
738   group "name/value";
739   let fn = define_function "X4" ty m in
740   let bb = entry_block fn in
741   ignore (build_unreachable (builder_at_end context bb));
742   let bbv = value_of_block bb in
743   set_value_name "Bb3" bbv;
744   insist ("Bb3" = value_name bbv);
745   
746   group "casts";
747   let fn = define_function "X5" ty m in
748   let bb = entry_block fn in
749   ignore (build_unreachable (builder_at_end context bb));
750   insist (bb = block_of_value (value_of_block bb));
751   insist (value_is_block (value_of_block bb));
752   insist (not (value_is_block (const_null i32_type)));
753   
754   begin group "iteration";
755     let m = create_module context "temp" in
756     let f = declare_function "Temp" (function_type i32_type [| |]) m in
757     
758     insist (At_end f = block_begin f);
759     insist (At_start f = block_end f);
760     
761     let b1 = append_block context "One" f in
762     let b2 = append_block context "Two" f in
763     
764     insist (Before b1 = block_begin f);
765     insist (Before b2 = block_succ b1);
766     insist (At_end f = block_succ b2);
767     
768     insist (After b2 = block_end f);
769     insist (After b1 = block_pred b2);
770     insist (At_start f = block_pred b1);
771     
772     let lf s x = s ^ "->" ^ value_name (value_of_block x) in
773     insist ("->One->Two" = fold_left_blocks lf "" f);
774     
775     let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
776     insist ("One<-Two<-" = fold_right_blocks rf f "");
777     
778     dispose_module m
779   end
780
781
782 (*===-- Instructions ------------------------------------------------------===*)
783
784 let test_instructions () =
785   begin group "iteration";
786     let m = create_module context "temp" in
787     let fty = function_type void_type [| i32_type; i32_type |] in
788     let f = define_function "f" fty m in
789     let bb = entry_block f in
790     let b = builder_at context (At_end bb) in
791     
792     insist (At_end bb = instr_begin bb);
793     insist (At_start bb = instr_end bb);
794     
795     let i1 = build_add (param f 0) (param f 1) "One" b in
796     let i2 = build_sub (param f 0) (param f 1) "Two" b in
797     
798     insist (Before i1 = instr_begin bb);
799     insist (Before i2 = instr_succ i1);
800     insist (At_end bb = instr_succ i2);
801     
802     insist (After i2 = instr_end bb);
803     insist (After i1 = instr_pred i2);
804     insist (At_start bb = instr_pred i1);
805     
806     let lf s x = s ^ "->" ^ value_name x in
807     insist ("->One->Two" = fold_left_instrs lf "" bb);
808     
809     let rf x s = value_name x ^ "<-" ^ s in
810     insist ("One<-Two<-" = fold_right_instrs rf bb "");
811     
812     dispose_module m
813   end
814
815
816 (*===-- Builder -----------------------------------------------------------===*)
817
818 let test_builder () =
819   let (++) x f = f x; x in
820   
821   begin group "parent";
822     insist (try
823               ignore (insertion_block (builder context));
824               false
825             with Not_found ->
826               true);
827     
828     let fty = function_type void_type [| i32_type |] in
829     let fn = define_function "BuilderParent" fty m in
830     let bb = entry_block fn in
831     let b = builder_at_end context bb in
832     let p = param fn 0 in
833     let sum = build_add p p "sum" b in
834     ignore (build_ret_void b);
835     
836     insist (fn = block_parent bb);
837     insist (fn = param_parent p);
838     insist (bb = instr_parent sum);
839     insist (bb = insertion_block b)
840   end;
841   
842   group "ret void";
843   begin
844     (* RUN: grep {ret void} < %t.ll
845      *)
846     let fty = function_type void_type [| |] in
847     let fn = declare_function "X6" fty m in
848     let b = builder_at_end context (append_block context "Bb01" fn) in
849     ignore (build_ret_void b)
850   end;
851   
852   (* The rest of the tests will use one big function. *)
853   let fty = function_type i32_type [| i32_type; i32_type |] in
854   let fn = define_function "X7" fty m in
855   let atentry = builder_at_end context (entry_block fn) in
856   let p1 = param fn 0 ++ set_value_name "P1" in
857   let p2 = param fn 1 ++ set_value_name "P2" in
858   let f1 = build_uitofp p1 float_type "F1" atentry in
859   let f2 = build_uitofp p2 float_type "F2" atentry in
860   
861   let bb00 = append_block context "Bb00" fn in
862   ignore (build_unreachable (builder_at_end context bb00));
863   
864   group "ret"; begin
865     (* RUN: grep {ret.*P1} < %t.ll
866      *)
867     let ret = build_ret p1 atentry in
868     position_before ret atentry
869   end;
870   
871   group "br"; begin
872     (* RUN: grep {br.*Bb02} < %t.ll
873      *)
874     let bb02 = append_block context "Bb02" fn in
875     let b = builder_at_end context bb02 in
876     ignore (build_br bb02 b)
877   end;
878   
879   group "cond_br"; begin
880     (* RUN: grep {br.*build_br.*Bb03.*Bb00} < %t.ll
881      *)
882     let bb03 = append_block context "Bb03" fn in
883     let b = builder_at_end context bb03 in
884     let cond = build_trunc p1 i1_type "build_br" b in
885     ignore (build_cond_br cond bb03 bb00 b)
886   end;
887   
888   group "switch"; begin
889     (* RUN: grep {switch.*P1.*SwiBlock3} < %t.ll
890      * RUN: grep {2,.*SwiBlock2} < %t.ll
891      *)
892     let bb1 = append_block context "SwiBlock1" fn in
893     let bb2 = append_block context "SwiBlock2" fn in
894     ignore (build_unreachable (builder_at_end context bb2));
895     let bb3 = append_block context "SwiBlock3" fn in
896     ignore (build_unreachable (builder_at_end context bb3));
897     let si = build_switch p1 bb3 1 (builder_at_end context bb1) in
898     ignore (add_case si (const_int i32_type 2) bb2)
899   end;
900   
901   group "invoke"; begin
902     (* RUN: grep {build_invoke.*invoke.*P1.*P2} < %t.ll
903      * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll
904      *)
905     let bb04 = append_block context "Bb04" fn in
906     let b = builder_at_end context bb04 in
907     ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "build_invoke" b)
908   end;
909   
910   group "unwind"; begin
911     (* RUN: grep {unwind} < %t.ll
912      *)
913     let bb05 = append_block context "Bb05" fn in
914     let b = builder_at_end context bb05 in
915     ignore (build_unwind b)
916   end;
917   
918   group "unreachable"; begin
919     (* RUN: grep {unreachable} < %t.ll
920      *)
921     let bb06 = append_block context "Bb06" fn in
922     let b = builder_at_end context bb06 in
923     ignore (build_unreachable b)
924   end;
925   
926   group "arithmetic"; begin
927     let bb07 = append_block context "Bb07" fn in
928     let b = builder_at_end context bb07 in
929     
930     (* RUN: grep {%build_add = add i32 %P1, %P2} < %t.ll
931      * RUN: grep {%build_nsw_add = add nsw i32 %P1, %P2} < %t.ll
932      * RUN: grep {%build_nuw_add = add nuw i32 %P1, %P2} < %t.ll
933      * RUN: grep {%build_fadd = fadd float %F1, %F2} < %t.ll
934      * RUN: grep {%build_sub = sub i32 %P1, %P2} < %t.ll
935      * RUN: grep {%build_nsw_sub = sub nsw i32 %P1, %P2} < %t.ll
936      * RUN: grep {%build_nuw_sub = sub nuw i32 %P1, %P2} < %t.ll
937      * RUN: grep {%build_fsub = fsub float %F1, %F2} < %t.ll
938      * RUN: grep {%build_mul = mul i32 %P1, %P2} < %t.ll
939      * RUN: grep {%build_nsw_mul = mul nsw i32 %P1, %P2} < %t.ll
940      * RUN: grep {%build_nuw_mul = mul nuw i32 %P1, %P2} < %t.ll
941      * RUN: grep {%build_fmul = fmul float %F1, %F2} < %t.ll
942      * RUN: grep {%build_udiv = udiv i32 %P1, %P2} < %t.ll
943      * RUN: grep {%build_sdiv = sdiv i32 %P1, %P2} < %t.ll
944      * RUN: grep {%build_exact_sdiv = sdiv exact i32 %P1, %P2} < %t.ll
945      * RUN: grep {%build_fdiv = fdiv float %F1, %F2} < %t.ll
946      * RUN: grep {%build_urem = urem i32 %P1, %P2} < %t.ll
947      * RUN: grep {%build_srem = srem i32 %P1, %P2} < %t.ll
948      * RUN: grep {%build_frem = frem float %F1, %F2} < %t.ll
949      * RUN: grep {%build_shl = shl i32 %P1, %P2} < %t.ll
950      * RUN: grep {%build_lshl = lshr i32 %P1, %P2} < %t.ll
951      * RUN: grep {%build_ashl = ashr i32 %P1, %P2} < %t.ll
952      * RUN: grep {%build_and = and i32 %P1, %P2} < %t.ll
953      * RUN: grep {%build_or = or i32 %P1, %P2} < %t.ll
954      * RUN: grep {%build_xor = xor i32 %P1, %P2} < %t.ll
955      * RUN: grep {%build_neg = sub i32 0, %P1} < %t.ll
956      * RUN: grep {%build_nsw_neg = sub nsw i32 0, %P1} < %t.ll
957      * RUN: grep {%build_nuw_neg = sub nuw i32 0, %P1} < %t.ll
958      * RUN: grep {%build_fneg = fsub float .*0.*, %F1} < %t.ll
959      * RUN: grep {%build_not = xor i32 %P1, -1} < %t.ll
960      *)
961     ignore (build_add p1 p2 "build_add" b);
962     ignore (build_nsw_add p1 p2 "build_nsw_add" b);
963     ignore (build_nuw_add p1 p2 "build_nuw_add" b);
964     ignore (build_fadd f1 f2 "build_fadd" b);
965     ignore (build_sub p1 p2 "build_sub" b);
966     ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
967     ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
968     ignore (build_fsub f1 f2 "build_fsub" b);
969     ignore (build_mul p1 p2 "build_mul" b);
970     ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
971     ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
972     ignore (build_fmul f1 f2 "build_fmul" b);
973     ignore (build_udiv p1 p2 "build_udiv" b);
974     ignore (build_sdiv p1 p2 "build_sdiv" b);
975     ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
976     ignore (build_fdiv f1 f2 "build_fdiv" b);
977     ignore (build_urem p1 p2 "build_urem" b);
978     ignore (build_srem p1 p2 "build_srem" b);
979     ignore (build_frem f1 f2 "build_frem" b);
980     ignore (build_shl p1 p2 "build_shl" b);
981     ignore (build_lshr p1 p2 "build_lshl" b);
982     ignore (build_ashr p1 p2 "build_ashl" b);
983     ignore (build_and p1 p2 "build_and" b);
984     ignore (build_or p1 p2 "build_or" b);
985     ignore (build_xor p1 p2 "build_xor" b);
986     ignore (build_neg p1 "build_neg" b);
987     ignore (build_nsw_neg p1 "build_nsw_neg" b);
988     ignore (build_nuw_neg p1 "build_nuw_neg" b);
989     ignore (build_fneg f1 "build_fneg" b);
990     ignore (build_not p1 "build_not" b);
991     ignore (build_unreachable b)
992   end;
993   
994   group "memory"; begin
995     let bb08 = append_block context "Bb08" fn in
996     let b = builder_at_end context bb08 in
997
998     (* RUN: grep {%build_alloca = alloca i32} < %t.ll
999      * RUN: grep {%build_array_alloca = alloca i32, i32 %P2} < %t.ll
1000      * RUN: grep {%build_load = load i32\\* %build_array_alloca} < %t.ll
1001      * RUN: grep {store i32 %P2, i32\\* %build_alloca} < %t.ll
1002      * RUN: grep {%build_gep = getelementptr i32\\* %build_array_alloca, i32 %P2} < %t.ll
1003      *)
1004     let alloca = build_alloca i32_type "build_alloca" b in
1005     let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1006     ignore(build_load array_alloca "build_load" b);
1007     ignore(build_store p2 alloca b);
1008     ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1009     ignore(build_unreachable b)
1010   end;
1011   
1012   group "casts"; begin
1013     let void_ptr = pointer_type i8_type in
1014     
1015     (* RUN: grep {%build_trunc = trunc i32 %P1 to i8} < %t.ll
1016      * RUN: grep {%build_zext = zext i8 %build_trunc to i32} < %t.ll
1017      * RUN: grep {%build_sext = sext i32 %build_zext to i64} < %t.ll
1018      * RUN: grep {%build_uitofp = uitofp i64 %build_sext to float} < %t.ll
1019      * RUN: grep {%build_sitofp = sitofp i32 %build_zext to double} < %t.ll
1020      * RUN: grep {%build_fptoui = fptoui float %build_uitofp to i32} < %t.ll
1021      * RUN: grep {%build_fptosi = fptosi double %build_sitofp to i64} < %t.ll
1022      * RUN: grep {%build_fptrunc = fptrunc double %build_sitofp to float} < %t.ll
1023      * RUN: grep {%build_fpext = fpext float %build_fptrunc to double} < %t.ll
1024      * RUN: grep {%build_inttoptr = inttoptr i32 %P1 to i8\\*} < %t.ll
1025      * RUN: grep {%build_ptrtoint = ptrtoint i8\\* %build_inttoptr to i64} < %t.ll
1026      * RUN: grep {%build_bitcast = bitcast i64 %build_ptrtoint to double} < %t.ll
1027      *)
1028     let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
1029     let inst29 = build_zext inst28 i32_type "build_zext" atentry in
1030     let inst30 = build_sext inst29 i64_type "build_sext" atentry in
1031     let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1032     let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1033     ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1034     ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1035     let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1036     ignore(build_fpext inst35 double_type "build_fpext" atentry);
1037     let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1038     let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1039     ignore(build_bitcast inst38 double_type "build_bitcast" atentry)
1040   end;
1041   
1042   group "comparisons"; begin
1043     (* RUN: grep {%build_icmp_ne = icmp ne i32 %P1, %P2} < %t.ll
1044      * RUN: grep {%build_icmp_sle = icmp sle i32 %P2, %P1} < %t.ll
1045      * RUN: grep {%build_icmp_false = fcmp false float %F1, %F2} < %t.ll
1046      * RUN: grep {%build_icmp_true = fcmp true float %F2, %F1} < %t.ll
1047      *)
1048     ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
1049     ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
1050     ignore (build_fcmp Fcmp.False f1 f2 "build_icmp_false" atentry);
1051     ignore (build_fcmp Fcmp.True  f2 f1 "build_icmp_true" atentry)
1052   end;
1053   
1054   group "miscellaneous"; begin
1055     (* RUN: grep {%build_call = tail call cc63 i32 @.*(i32 signext %P2, i32 %P1)} < %t.ll
1056      * RUN: grep {%build_select = select i1 %build_icmp, i32 %P1, i32 %P2} < %t.ll
1057      * RUN: grep {%build_va_arg = va_arg i8\\*\\* null, i32} < %t.ll
1058      * RUN: grep {%build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2} < %t.ll
1059      * RUN: grep {%build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2} < %t.ll
1060      * 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
1061      *)
1062     let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1063     insist (CallConv.c = instruction_call_conv ci);
1064     set_instruction_call_conv 63 ci;
1065     insist (63 = instruction_call_conv ci);
1066     insist (not (is_tail_call ci));
1067     set_tail_call true ci;
1068     insist (is_tail_call ci);
1069     add_instruction_param_attr ci 1 Attribute.Sext;
1070     add_instruction_param_attr ci 2 Attribute.Noalias;
1071     remove_instruction_param_attr ci 2 Attribute.Noalias;
1072     
1073     let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1074     ignore (build_select inst46 p1 p2 "build_select" atentry);
1075     ignore (build_va_arg
1076       (const_null (pointer_type (pointer_type i8_type)))
1077       i32_type "build_va_arg" atentry);
1078     
1079     (* Set up some vector vregs. *)
1080     let one  = const_int i32_type 1 in
1081     let zero = const_int i32_type 0 in
1082     let t1 = const_vector [| one; zero; one; zero |] in
1083     let t2 = const_vector [| zero; one; zero; one |] in
1084     let t3 = const_vector [| one; one; zero; zero |] in
1085     let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1086     let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1087     
1088     ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1089     ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1090     ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1091   end;
1092
1093   group "metadata"; begin
1094     (* RUN: grep {%metadata = add i32 %P1, %P2, !test !0} < %t.ll
1095      * RUN: grep {!0 = metadata !\{i32 1, metadata !"metadata test"\}} < %t.ll
1096      *)
1097     let i = build_add p1 p2 "metadata" atentry in
1098     insist ((has_metadata i) = false);
1099
1100     let m1 = const_int i32_type 1 in
1101     let m2 = mdstring context "metadata test" in
1102     let md = mdnode context [| m1; m2 |] in
1103
1104     let kind = mdkind_id context "test" in
1105     set_metadata i kind md;
1106
1107     insist ((has_metadata i) = true);
1108     insist ((metadata i kind) = Some md);
1109
1110     clear_metadata i kind;
1111
1112     insist ((has_metadata i) = false);
1113     insist ((metadata i kind) = None);
1114
1115     set_metadata i kind md
1116   end;
1117
1118   group "dbg"; begin
1119     (* RUN: grep {%dbg = add i32 %P1, %P2, !dbg !1} < %t.ll
1120      * RUN: grep {!1 = metadata !\{i32 2, metadata !"dbg test"\}} < %t.ll
1121      *)
1122     let m1 = const_int i32_type 2 in
1123     let m2 = mdstring context "dbg test" in
1124     let md = mdnode context [| m1; m2 |] in
1125     set_current_debug_location atentry md;
1126
1127     let i = build_add p1 p2 "dbg" atentry in
1128     insist ((has_metadata i) = true);
1129
1130     clear_current_debug_location atentry
1131   end;
1132   
1133   group "phi"; begin
1134     (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll
1135      *)
1136     let b1 = append_block context "PhiBlock1" fn in
1137     let b2 = append_block context "PhiBlock2" fn in
1138     
1139     let jb = append_block context "PhiJoinBlock" fn in
1140     ignore (build_br jb (builder_at_end context b1));
1141     ignore (build_br jb (builder_at_end context b2));
1142     let at_jb = builder_at_end context jb in
1143     
1144     let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1145     insist ([(p1, b1)] = incoming phi);
1146     
1147     add_incoming (p2, b2) phi;
1148     insist ([(p1, b1); (p2, b2)] = incoming phi);
1149     
1150     ignore (build_unreachable at_jb);
1151   end
1152
1153
1154 (*===-- Module Provider ---------------------------------------------------===*)
1155
1156 let test_module_provider () =
1157   let m = create_module context "test" in
1158   let mp = ModuleProvider.create m in
1159   ModuleProvider.dispose mp
1160
1161
1162 (*===-- Pass Managers -----------------------------------------------------===*)
1163
1164 let test_pass_manager () =
1165   let (++) x f = ignore (f x); x in
1166
1167   begin group "module pass manager";
1168     ignore (PassManager.create ()
1169              ++ PassManager.run_module m
1170              ++ PassManager.dispose)
1171   end;
1172   
1173   begin group "function pass manager";
1174     let fty = function_type void_type [| |] in
1175     let fn = define_function "FunctionPassManager" fty m in
1176     ignore (build_ret_void (builder_at_end context (entry_block fn)));
1177     
1178     ignore (PassManager.create_function mp
1179              ++ PassManager.initialize
1180              ++ PassManager.run_function fn
1181              ++ PassManager.finalize
1182              ++ PassManager.dispose)
1183   end
1184
1185
1186 (*===-- Writer ------------------------------------------------------------===*)
1187
1188 let test_writer () =
1189   group "valid";
1190   insist (match Llvm_analysis.verify_module m with
1191           | None -> true
1192           | Some msg -> prerr_string msg; false);
1193
1194   group "writer";
1195   insist (write_bitcode_file m filename);
1196   
1197   ModuleProvider.dispose mp
1198
1199
1200 (*===-- Driver ------------------------------------------------------------===*)
1201
1202 let _ =
1203   suite "target"           test_target;
1204   suite "types"            test_types;
1205   suite "constants"        test_constants;
1206   suite "global values"    test_global_values;
1207   suite "global variables" test_global_variables;
1208   suite "functions"        test_functions;
1209   suite "params"           test_params;
1210   suite "basic blocks"     test_basic_blocks;
1211   suite "instructions"     test_instructions;
1212   suite "builder"          test_builder;
1213   suite "module provider"  test_module_provider;
1214   suite "pass manager"     test_pass_manager;
1215   suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1216   exit !exit_status