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