Expose creating constant ints and floats from strings to ocaml.
[oota-llvm.git] / test / Bindings / Ocaml / vmcore.ml
1 (* RUN: %ocamlc -warn-error A llvm.cma llvm_analysis.cma llvm_bitwriter.cma %s -o %t 2> /dev/null
2  * RUN: ./%t %t.bc
3  * RUN: llvm-dis < %t.bc > %t.ll
4  *)
5
6 (* Note: It takes several seconds for ocamlc 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
21 let group name =
22   group_name := !suite_name ^ "/" ^ name;
23   case_num := 0;
24   if print_checkpoints then
25     prerr_endline ("  " ^ name ^ "...")
26
27 let insist cond =
28   incr case_num;
29   if not cond then
30     exit_status := 10;
31   match print_checkpoints, cond with
32   | false, true -> ()
33   | false, false ->
34       prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
35   | true, true ->
36       prerr_endline ("    " ^ (string_of_int !case_num))
37   | true, false ->
38       prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
39
40 let suite name f =
41   suite_name := name;
42   if print_checkpoints then
43     prerr_endline (name ^ ":");
44   f ()
45
46
47 (*===-- Fixture -----------------------------------------------------------===*)
48
49 let filename = Sys.argv.(1)
50 let m = create_module filename
51 let mp = ModuleProvider.create m
52
53
54 (*===-- Target ------------------------------------------------------------===*)
55
56 let test_target () =
57   begin group "triple";
58     (* RUN: grep "i686-apple-darwin8" < %t.ll
59      *)
60     let trip = "i686-apple-darwin8" in
61     set_target_triple trip m;
62     insist (trip = target_triple m)
63   end;
64   
65   begin group "layout";
66     (* RUN: grep "bogus" < %t.ll
67      *)
68     let layout = "bogus" in
69     set_data_layout layout m;
70     insist (layout = data_layout m)
71   end
72
73 (*===-- Types -------------------------------------------------------------===*)
74
75 let test_types () =
76   (* RUN: grep {Ty01.*void} < %t.ll
77    *)
78   group "void";
79   insist (define_type_name "Ty01" void_type m);
80   insist (TypeKind.Void == classify_type void_type);
81
82   (* RUN: grep {Ty02.*i1} < %t.ll
83    *)
84   group "i1";
85   insist (define_type_name "Ty02" i1_type m);
86   insist (TypeKind.Integer == classify_type i1_type);
87
88   (* RUN: grep {Ty03.*i32} < %t.ll
89    *)
90   group "i32";
91   insist (define_type_name "Ty03" i32_type m);
92
93   (* RUN: grep {Ty04.*i42} < %t.ll
94    *)
95   group "i42";
96   let ty = integer_type 42 in
97   insist (define_type_name "Ty04" ty m);
98
99   (* RUN: grep {Ty05.*float} < %t.ll
100    *)
101   group "float";
102   insist (define_type_name "Ty05" float_type m);
103   insist (TypeKind.Float == classify_type float_type);
104
105   (* RUN: grep {Ty06.*double} < %t.ll
106    *)
107   group "double";
108   insist (define_type_name "Ty06" double_type m);
109   insist (TypeKind.Double == classify_type double_type);
110
111   (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
112    *)
113   group "function";
114   let ty = function_type i32_type [| i1_type; double_type |] in
115   insist (define_type_name "Ty07" ty m);
116   insist (TypeKind.Function = classify_type ty);
117   insist (not (is_var_arg ty));
118   insist (i32_type == return_type ty);
119   insist (double_type == (param_types ty).(1));
120   
121   (* RUN: grep {Ty08.*\.\.\.} < %t.ll
122    *)
123   group "var arg function";
124   let ty = var_arg_function_type void_type [| i32_type |] in
125   insist (define_type_name "Ty08" ty m);
126   insist (is_var_arg ty);
127   
128   (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll
129    *)
130   group "array";
131   let ty = array_type i8_type 7 in
132   insist (define_type_name "Ty09" ty m);
133   insist (7 = array_length ty);
134   insist (i8_type == element_type ty);
135   insist (TypeKind.Array == classify_type ty);
136   
137   begin group "pointer";
138     (* RUN: grep {UnqualPtrTy.*float\*} < %t.ll
139      *)
140     let ty = pointer_type float_type in
141     insist (define_type_name "UnqualPtrTy" ty m);
142     insist (float_type == element_type ty);
143     insist (0 == address_space ty);
144     insist (TypeKind.Pointer == classify_type ty)
145   end;
146   
147   begin group "qualified_pointer";
148     (* RUN: grep {QualPtrTy.*i8.*3.*\*} < %t.ll
149      *)
150     let ty = qualified_pointer_type i8_type 3 in
151     insist (define_type_name "QualPtrTy" ty m);
152     insist (i8_type == element_type ty);
153     insist (3 == address_space ty)
154   end;
155   
156   (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll
157    *)
158   group "vector";
159   let ty = vector_type i16_type 4 in
160   insist (define_type_name "Ty11" ty m);
161   insist (i16_type == element_type ty);
162   insist (4 = vector_size ty);
163   
164   (* RUN: grep {Ty12.*opaque} < %t.ll
165    *)
166   group "opaque";
167   let ty = opaque_type () in
168   insist (define_type_name "Ty12" ty m);
169   insist (ty == ty);
170   insist (ty <> opaque_type ());
171   
172   (* RUN: grep -v {Ty13} < %t.ll
173    *)
174   group "delete";
175   let ty = opaque_type () in
176   insist (define_type_name "Ty13" ty m);
177   delete_type_name "Ty13" m;
178   
179   (* RUN: grep -v {RecursiveTy.*RecursiveTy} < %t.ll
180    *)
181   group "recursive";
182   let ty = opaque_type () in
183   let th = handle_to_type ty in
184   refine_type ty (pointer_type ty);
185   let ty = type_of_handle th in
186   insist (define_type_name "RecursiveTy" ty m);
187   insist (ty == element_type ty)
188
189
190 (*===-- Constants ---------------------------------------------------------===*)
191
192 let test_constants () =
193   (* RUN: grep {Const01.*i32.*-1} < %t.ll
194    *)
195   group "int";
196   let c = const_int i32_type (-1) in
197   ignore (define_global "Const01" c m);
198   insist (i32_type = type_of c);
199   insist (is_constant c);
200
201   (* RUN: grep {Const02.*i64.*-1} < %t.ll
202    *)
203   group "sext int";
204   let c = const_int i64_type (-1) in
205   ignore (define_global "Const02" c m);
206   insist (i64_type = type_of c);
207
208   (* RUN: grep {Const03.*i64.*4294967295} < %t.ll
209    *)
210   group "zext int64";
211   let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
212   ignore (define_global "Const03" c m);
213   insist (i64_type = type_of c);
214
215   (* RUN: grep {ConstIntString.*i32.*-1} < %t.ll
216    *)
217   group "int string";
218   let c = const_int_of_string i32_type "-1" 10 in
219   ignore (define_global "ConstIntString" c m);
220   insist (i32_type = type_of c);
221
222   (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll
223    *)
224   group "string";
225   let c = const_string "cruel\000world" in
226   ignore (define_global "Const04" c m);
227   insist ((array_type i8_type 11) = type_of c);
228
229   (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll
230    *)
231   group "stringz";
232   let c = const_stringz "hi\000again" in
233   ignore (define_global "Const05" c m);
234   insist ((array_type i8_type 9) = type_of c);
235
236   (* RUN: grep {ConstSingle.*2.75} < %t.ll
237    * RUN: grep {ConstDouble.*3.1459} < %t.ll
238    * RUN: grep {ConstDoubleString.*1.25} < %t.ll
239    *)
240   begin group "real";
241     let cs = const_float float_type 2.75 in
242     ignore (define_global "ConstSingle" cs m);
243     insist (float_type = type_of cs);
244     
245     let cd = const_float double_type 3.1459 in
246     ignore (define_global "ConstDouble" cd m);
247     insist (double_type = type_of cd);
248
249     let cd = const_float_of_string double_type "1.25" in
250     ignore (define_global "ConstDoubleString" cd m);
251     insist (double_type = type_of cd)
252   end;
253   
254   let one = const_int i16_type 1 in
255   let two = const_int i16_type 2 in
256   let three = const_int i32_type 3 in
257   let four = const_int i32_type 4 in
258   
259   (* RUN: grep {Const07.*\\\[i32 3, i32 4\\\]} < %t.ll
260    *)
261   group "array";
262   let c = const_array i32_type [| three; four |] in
263   ignore (define_global "Const07" c m);
264   insist ((array_type i32_type 2) = (type_of c));
265   
266   (* RUN: grep {Const08.*<i16 1, i16 2.*>} < %t.ll
267    *)
268   group "vector";
269   let c = const_vector [| one; two; one; two;
270                           one; two; one; two |] in
271   ignore (define_global "Const08" c m);
272   insist ((vector_type i16_type 8) = (type_of c));
273   
274   (* RUN: grep {Const09.*.i16 1, i16 2, i32 3, i32 4} < %t.ll
275    *)
276   group "structure";
277   let c = const_struct [| one; two; three; four |] in
278   ignore (define_global "Const09" c m);
279   insist ((struct_type [| i16_type; i16_type; i32_type; i32_type |])
280         = (type_of c));
281   
282   (* RUN: grep {Const10.*zeroinit} < %t.ll
283    *)
284   group "null";
285   let c = const_null (packed_struct_type [| i1_type; i8_type;
286                                             i64_type; double_type |]) in
287   ignore (define_global "Const10" c m);
288   
289   (* RUN: grep {Const11.*-1} < %t.ll
290    *)
291   group "all ones";
292   let c = const_all_ones i64_type in
293   ignore (define_global "Const11" c m);
294   
295   (* RUN: grep {Const12.*undef} < %t.ll
296    *)
297   group "undef";
298   let c = undef i1_type in
299   ignore (define_global "Const12" c m);
300   insist (i1_type = type_of c);
301   insist (is_undef c);
302   
303   group "constant arithmetic";
304   (* RUN: grep {ConstNeg.*sub} < %t.ll
305    * RUN: grep {ConstNot.*xor} < %t.ll
306    * RUN: grep {ConstAdd.*add} < %t.ll
307    * RUN: grep {ConstSub.*sub} < %t.ll
308    * RUN: grep {ConstMul.*mul} < %t.ll
309    * RUN: grep {ConstUDiv.*udiv} < %t.ll
310    * RUN: grep {ConstSDiv.*sdiv} < %t.ll
311    * RUN: grep {ConstFDiv.*fdiv} < %t.ll
312    * RUN: grep {ConstURem.*urem} < %t.ll
313    * RUN: grep {ConstSRem.*srem} < %t.ll
314    * RUN: grep {ConstFRem.*frem} < %t.ll
315    * RUN: grep {ConstAnd.*and} < %t.ll
316    * RUN: grep {ConstOr.*or} < %t.ll
317    * RUN: grep {ConstXor.*xor} < %t.ll
318    * RUN: grep {ConstICmp.*icmp} < %t.ll
319    * RUN: grep {ConstFCmp.*fcmp} < %t.ll
320    *)
321   let void_ptr = pointer_type i8_type in
322   let five = const_int i64_type 5 in
323   let ffive = const_uitofp five double_type in
324   let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
325   let foldbomb = const_ptrtoint foldbomb_gv i64_type in
326   let ffoldbomb = const_uitofp foldbomb double_type in
327   ignore (define_global "ConstNeg" (const_neg foldbomb) m);
328   ignore (define_global "ConstNot" (const_not foldbomb) m);
329   ignore (define_global "ConstAdd" (const_add foldbomb five) m);
330   ignore (define_global "ConstSub" (const_sub foldbomb five) m);
331   ignore (define_global "ConstMul" (const_mul foldbomb five) m);
332   ignore (define_global "ConstUDiv" (const_udiv foldbomb five) m);
333   ignore (define_global "ConstSDiv" (const_sdiv foldbomb five) m);
334   ignore (define_global "ConstFDiv" (const_fdiv ffoldbomb ffive) m);
335   ignore (define_global "ConstURem" (const_urem foldbomb five) m);
336   ignore (define_global "ConstSRem" (const_srem foldbomb five) m);
337   ignore (define_global "ConstFRem" (const_frem ffoldbomb ffive) m);
338   ignore (define_global "ConstAnd" (const_and foldbomb five) m);
339   ignore (define_global "ConstOr" (const_or foldbomb five) m);
340   ignore (define_global "ConstXor" (const_xor foldbomb five) m);
341   ignore (define_global "ConstICmp" (const_icmp Icmp.Sle foldbomb five) m);
342   ignore (define_global "ConstFCmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
343   
344   group "constant casts";
345   (* RUN: grep {ConstTrunc.*trunc} < %t.ll
346    * RUN: grep {ConstSExt.*sext} < %t.ll
347    * RUN: grep {ConstZExt.*zext} < %t.ll
348    * RUN: grep {ConstFPTrunc.*fptrunc} < %t.ll
349    * RUN: grep {ConstFPExt.*fpext} < %t.ll
350    * RUN: grep {ConstUIToFP.*uitofp} < %t.ll
351    * RUN: grep {ConstSIToFP.*sitofp} < %t.ll
352    * RUN: grep {ConstFPToUI.*fptoui} < %t.ll
353    * RUN: grep {ConstFPToSI.*fptosi} < %t.ll
354    * RUN: grep {ConstPtrToInt.*ptrtoint} < %t.ll
355    * RUN: grep {ConstIntToPtr.*inttoptr} < %t.ll
356    * RUN: grep {ConstBitCast.*bitcast} < %t.ll
357    *)
358   let i128_type = integer_type 128 in
359   ignore (define_global "ConstTrunc" (const_trunc (const_add foldbomb five)
360                                                i8_type) m);
361   ignore (define_global "ConstSExt" (const_sext foldbomb i128_type) m);
362   ignore (define_global "ConstZExt" (const_zext foldbomb i128_type) m);
363   ignore (define_global "ConstFPTrunc" (const_fptrunc ffoldbomb float_type) m);
364   ignore (define_global "ConstFPExt" (const_fpext ffoldbomb fp128_type) m);
365   ignore (define_global "ConstUIToFP" (const_uitofp foldbomb double_type) m);
366   ignore (define_global "ConstSIToFP" (const_sitofp foldbomb double_type) m);
367   ignore (define_global "ConstFPToUI" (const_fptoui ffoldbomb i32_type) m);
368   ignore (define_global "ConstFPToSI" (const_fptosi ffoldbomb i32_type) m);
369   ignore (define_global "ConstPtrToInt" (const_ptrtoint 
370     (const_gep (const_null (pointer_type i8_type))
371                [| const_int i32_type 1 |])
372     i32_type) m);
373   ignore (define_global "ConstIntToPtr" (const_inttoptr (const_add foldbomb five)
374                                                   void_ptr) m);
375   ignore (define_global "ConstBitCast" (const_bitcast ffoldbomb i64_type) m);
376   
377   group "misc constants";
378   (* RUN: grep {ConstSizeOf.*getelementptr.*null} < %t.ll
379    * RUN: grep {ConstGEP.*getelementptr} < %t.ll
380    * RUN: grep {ConstSelect.*select} < %t.ll
381    * RUN: grep {ConstExtractElement.*extractelement} < %t.ll
382    * RUN: grep {ConstInsertElement.*insertelement} < %t.ll
383    * RUN: grep {ConstShuffleVector.*shufflevector} < %t.ll
384    *)
385   ignore (define_global "ConstSizeOf" (size_of (pointer_type i8_type)) m);
386   ignore (define_global "ConstGEP" (const_gep foldbomb_gv [| five |]) m);
387   ignore (define_global "ConstSelect" (const_select
388     (const_icmp Icmp.Sle foldbomb five)
389     (const_int i8_type (-1))
390     (const_int i8_type 0)) m);
391   let zero = const_int i32_type 0 in
392   let one  = const_int i32_type 1 in
393   ignore (define_global "ConstExtractElement" (const_extractelement
394     (const_vector [| zero; one; zero; one |])
395     (const_trunc foldbomb i32_type)) m);
396   ignore (define_global "ConstInsertElement" (const_insertelement
397     (const_vector [| zero; one; zero; one |])
398     zero (const_trunc foldbomb i32_type)) m);
399   ignore (define_global "ConstShuffleVector" (const_shufflevector
400     (const_vector [| zero; one |])
401     (const_vector [| one; zero |])
402     (const_bitcast foldbomb (vector_type i32_type 2))) m)
403
404
405 (*===-- Global Values -----------------------------------------------------===*)
406
407 let test_global_values () =
408   let (++) x f = f x; x in
409   let zero32 = const_null i32_type in
410
411   (* RUN: grep {GVal01} < %t.ll
412    *)
413   group "naming";
414   let g = define_global "TEMPORARY" zero32 m in
415   insist ("TEMPORARY" = value_name g);
416   set_value_name "GVal01" g;
417   insist ("GVal01" = value_name g);
418
419   (* RUN: grep {GVal02.*linkonce} < %t.ll
420    *)
421   group "linkage";
422   let g = define_global "GVal02" zero32 m ++
423           set_linkage Linkage.Link_once in
424   insist (Linkage.Link_once = linkage g);
425
426   (* RUN: grep {GVal03.*Hanalei} < %t.ll
427    *)
428   group "section";
429   let g = define_global "GVal03" zero32 m ++
430           set_section "Hanalei" in
431   insist ("Hanalei" = section g);
432   
433   (* RUN: grep {GVal04.*hidden} < %t.ll
434    *)
435   group "visibility";
436   let g = define_global "GVal04" zero32 m ++
437           set_visibility Visibility.Hidden in
438   insist (Visibility.Hidden = visibility g);
439   
440   (* RUN: grep {GVal05.*align 128} < %t.ll
441    *)
442   group "alignment";
443   let g = define_global "GVal05" zero32 m ++
444           set_alignment 128 in
445   insist (128 = alignment g)
446
447
448 (*===-- Global Variables --------------------------------------------------===*)
449
450 let test_global_variables () =
451   let (++) x f = f x; x in
452   let fourty_two32 = const_int i32_type 42 in
453
454   (* RUN: grep {GVar01.*external} < %t.ll
455    *)
456   group "declarations";
457   insist (None == lookup_global "GVar01" m);
458   let g = declare_global i32_type "GVar01" m in
459   insist (is_declaration g);
460   insist (pointer_type float_type ==
461             type_of (declare_global float_type "GVar01" m));
462   insist (g == declare_global i32_type "GVar01" m);
463   insist (match lookup_global "GVar01" m with Some x -> x = g
464                                             | None -> false);
465   
466   (* RUN: grep {GVar02.*42} < %t.ll
467    * RUN: grep {GVar03.*42} < %t.ll
468    *)
469   group "definitions";
470   let g = define_global "GVar02" fourty_two32 m in
471   let g2 = declare_global i32_type "GVar03" m ++
472            set_initializer fourty_two32 in
473   insist (not (is_declaration g));
474   insist (not (is_declaration g2));
475   insist ((global_initializer g) == (global_initializer g2));
476
477   (* RUN: grep {GVar04.*thread_local} < %t.ll
478    *)
479   group "threadlocal";
480   let g = define_global "GVar04" fourty_two32 m ++
481           set_thread_local true in
482   insist (is_thread_local g);
483
484   (* RUN: grep -v {GVar05} < %t.ll
485    *)
486   group "delete";
487   let g = define_global "GVar05" fourty_two32 m in
488   delete_global g;
489
490   (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll
491    *)
492   group "constant";
493   let g = define_global "ConstGlobalVar" fourty_two32 m in
494   insist (not (is_global_constant g));
495   set_global_constant true g;
496   insist (is_global_constant g);
497   
498   begin group "iteration";
499     let m = create_module "temp" in
500     
501     insist (At_end m = global_begin m);
502     insist (At_start m = global_end m);
503     
504     let g1 = declare_global i32_type "One" m in
505     let g2 = declare_global i32_type "Two" m in
506     
507     insist (Before g1 = global_begin m);
508     insist (Before g2 = global_succ g1);
509     insist (At_end m = global_succ g2);
510     
511     insist (After g2 = global_end m);
512     insist (After g1 = global_pred g2);
513     insist (At_start m = global_pred g1);
514     
515     let lf s x = s ^ "->" ^ value_name x in
516     insist ("->One->Two" = fold_left_globals lf "" m);
517     
518     let rf x s = value_name x ^ "<-" ^ s in
519     insist ("One<-Two<-" = fold_right_globals rf m "");
520     
521     dispose_module m
522   end
523
524
525 (*===-- Functions ---------------------------------------------------------===*)
526
527 let test_functions () =
528   let ty = function_type i32_type [| i32_type; i64_type |] in
529   let ty2 = function_type i8_type [| i8_type; i64_type |] in
530   
531   (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
532    *)
533   begin group "declare";
534     insist (None = lookup_function "Fn1" m);
535     let fn = declare_function "Fn1" ty m in
536     insist (pointer_type ty = type_of fn);
537     insist (is_declaration fn);
538     insist (0 = Array.length (basic_blocks fn));
539     insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
540     insist (fn == declare_function "Fn1" ty m);
541     insist (None <> lookup_function "Fn1" m);
542     insist (match lookup_function "Fn1" m with Some x -> x = fn
543                                              | None -> false);
544     insist (m == global_parent fn)
545   end;
546   
547   (* RUN: grep -v {Fn2} < %t.ll
548    *)
549   group "delete";
550   let fn = declare_function "Fn2" ty m in
551   delete_function fn;
552   
553   (* RUN: grep {define.*Fn3} < %t.ll
554    *)
555   group "define";
556   let fn = define_function "Fn3" ty m in
557   insist (not (is_declaration fn));
558   insist (1 = Array.length (basic_blocks fn));
559   ignore (build_unreachable (builder_at_end (entry_block fn)));
560   
561   (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll
562    *)
563   group "params";
564   let fn = define_function "Fn4" ty m in
565   let params = params fn in
566   insist (2 = Array.length params);
567   insist (params.(0) = param fn 0);
568   insist (params.(1) = param fn 1);
569   insist (i32_type = type_of params.(0));
570   insist (i64_type = type_of params.(1));
571   set_value_name "Param1" params.(0);
572   set_value_name "Param2" params.(1);
573   ignore (build_unreachable (builder_at_end (entry_block fn)));
574   
575   (* RUN: grep {fastcc.*Fn5} < %t.ll
576    *)
577   group "callconv";
578   let fn = define_function "Fn5" ty m in
579   insist (CallConv.c = function_call_conv fn);
580   set_function_call_conv CallConv.fast fn;
581   insist (CallConv.fast = function_call_conv fn);
582   ignore (build_unreachable (builder_at_end (entry_block fn)));
583   
584   begin group "gc";
585     (* RUN: grep {Fn6.*gc.*shadowstack} < %t.ll
586      *)
587     let fn = define_function "Fn6" ty m in
588     insist (None = gc fn);
589     set_gc (Some "ocaml") fn;
590     insist (Some "ocaml" = gc fn);
591     set_gc None fn;
592     insist (None = gc fn);
593     set_gc (Some "shadowstack") fn;
594     ignore (build_unreachable (builder_at_end (entry_block fn)));
595   end;
596   
597   begin group "iteration";
598     let m = create_module "temp" in
599     
600     insist (At_end m = function_begin m);
601     insist (At_start m = function_end m);
602     
603     let f1 = define_function "One" ty m in
604     let f2 = define_function "Two" ty m in
605     
606     insist (Before f1 = function_begin m);
607     insist (Before f2 = function_succ f1);
608     insist (At_end m = function_succ f2);
609     
610     insist (After f2 = function_end m);
611     insist (After f1 = function_pred f2);
612     insist (At_start m = function_pred f1);
613     
614     let lf s x = s ^ "->" ^ value_name x in
615     insist ("->One->Two" = fold_left_functions lf "" m);
616     
617     let rf x s = value_name x ^ "<-" ^ s in
618     insist ("One<-Two<-" = fold_right_functions rf m "");
619     
620     dispose_module m
621   end
622
623
624 (*===-- Params ------------------------------------------------------------===*)
625
626 let test_params () =
627   begin group "iteration";
628     let m = create_module "temp" in
629     
630     let vf = define_function "void" (function_type void_type [| |]) m in
631     
632     insist (At_end vf = param_begin vf);
633     insist (At_start vf = param_end vf);
634     
635     let ty = function_type void_type [| i32_type; i32_type |] in
636     let f = define_function "f" ty m in
637     let p1 = param f 0 in
638     let p2 = param f 1 in
639     set_value_name "One" p1;
640     set_value_name "Two" p2;
641     add_param_attr p1 Attribute.Sext;
642     add_param_attr p2 Attribute.Noalias;
643     remove_param_attr p2 Attribute.Noalias;
644     add_function_attr f Attribute.Nounwind;
645     add_function_attr f Attribute.Noreturn;
646     remove_function_attr f Attribute.Noreturn;
647
648     insist (Before p1 = param_begin f);
649     insist (Before p2 = param_succ p1);
650     insist (At_end f = param_succ p2);
651     
652     insist (After p2 = param_end f);
653     insist (After p1 = param_pred p2);
654     insist (At_start f = param_pred p1);
655     
656     let lf s x = s ^ "->" ^ value_name x in
657     insist ("->One->Two" = fold_left_params lf "" f);
658     
659     let rf x s = value_name x ^ "<-" ^ s in
660     insist ("One<-Two<-" = fold_right_params rf f "");
661     
662     dispose_module m
663   end
664
665
666 (*===-- Basic Blocks ------------------------------------------------------===*)
667
668 let test_basic_blocks () =
669   let ty = function_type void_type [| |] in
670   
671   (* RUN: grep {Bb1} < %t.ll
672    *)
673   group "entry";
674   let fn = declare_function "X" ty m in
675   let bb = append_block "Bb1" fn in
676   insist (bb = entry_block fn);
677   ignore (build_unreachable (builder_at_end bb));
678   
679   (* RUN: grep -v Bb2 < %t.ll
680    *)
681   group "delete";
682   let fn = declare_function "X2" ty m in
683   let bb = append_block "Bb2" fn in
684   delete_block bb;
685   
686   group "insert";
687   let fn = declare_function "X3" ty m in
688   let bbb = append_block "b" fn in
689   let bba = insert_block "a" bbb in
690   insist ([| bba; bbb |] = basic_blocks fn);
691   ignore (build_unreachable (builder_at_end bba));
692   ignore (build_unreachable (builder_at_end bbb));
693   
694   (* RUN: grep Bb3 < %t.ll
695    *)
696   group "name/value";
697   let fn = define_function "X4" ty m in
698   let bb = entry_block fn in
699   ignore (build_unreachable (builder_at_end bb));
700   let bbv = value_of_block bb in
701   set_value_name "Bb3" bbv;
702   insist ("Bb3" = value_name bbv);
703   
704   group "casts";
705   let fn = define_function "X5" ty m in
706   let bb = entry_block fn in
707   ignore (build_unreachable (builder_at_end bb));
708   insist (bb = block_of_value (value_of_block bb));
709   insist (value_is_block (value_of_block bb));
710   insist (not (value_is_block (const_null i32_type)));
711   
712   begin group "iteration";
713     let m = create_module "temp" in
714     let f = declare_function "Temp" (function_type i32_type [| |]) m in
715     
716     insist (At_end f = block_begin f);
717     insist (At_start f = block_end f);
718     
719     let b1 = append_block "One" f in
720     let b2 = append_block "Two" f in
721     
722     insist (Before b1 = block_begin f);
723     insist (Before b2 = block_succ b1);
724     insist (At_end f = block_succ b2);
725     
726     insist (After b2 = block_end f);
727     insist (After b1 = block_pred b2);
728     insist (At_start f = block_pred b1);
729     
730     let lf s x = s ^ "->" ^ value_name (value_of_block x) in
731     insist ("->One->Two" = fold_left_blocks lf "" f);
732     
733     let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
734     insist ("One<-Two<-" = fold_right_blocks rf f "");
735     
736     dispose_module m
737   end
738
739
740 (*===-- Instructions ------------------------------------------------------===*)
741
742 let test_instructions () =
743   begin group "iteration";
744     let m = create_module "temp" in
745     let fty = function_type void_type [| i32_type; i32_type |] in
746     let f = define_function "f" fty m in
747     let bb = entry_block f in
748     let b = builder_at (At_end bb) in
749     
750     insist (At_end bb = instr_begin bb);
751     insist (At_start bb = instr_end bb);
752     
753     let i1 = build_add (param f 0) (param f 1) "One" b in
754     let i2 = build_sub (param f 0) (param f 1) "Two" b in
755     
756     insist (Before i1 = instr_begin bb);
757     insist (Before i2 = instr_succ i1);
758     insist (At_end bb = instr_succ i2);
759     
760     insist (After i2 = instr_end bb);
761     insist (After i1 = instr_pred i2);
762     insist (At_start bb = instr_pred i1);
763     
764     let lf s x = s ^ "->" ^ value_name x in
765     insist ("->One->Two" = fold_left_instrs lf "" bb);
766     
767     let rf x s = value_name x ^ "<-" ^ s in
768     insist ("One<-Two<-" = fold_right_instrs rf bb "");
769     
770     dispose_module m
771   end
772
773
774 (*===-- Builder -----------------------------------------------------------===*)
775
776 let test_builder () =
777   let (++) x f = f x; x in
778   
779   begin group "parent";
780     insist (try
781               ignore (insertion_block (builder ()));
782               false
783             with Not_found ->
784               true);
785     
786     let fty = function_type void_type [| i32_type |] in
787     let fn = define_function "BuilderParent" fty m in
788     let bb = entry_block fn in
789     let b = builder_at_end bb in
790     let p = param fn 0 in
791     let sum = build_add p p "sum" b in
792     ignore (build_ret_void b);
793     
794     insist (fn = block_parent bb);
795     insist (fn = param_parent p);
796     insist (bb = instr_parent sum);
797     insist (bb = insertion_block b)
798   end;
799   
800   group "ret void";
801   begin
802     (* RUN: grep {ret void} < %t.ll
803      *)
804     let fty = function_type void_type [| |] in
805     let fn = declare_function "X6" fty m in
806     let b = builder_at_end (append_block "Bb01" fn) in
807     ignore (build_ret_void b)
808   end;
809   
810   (* The rest of the tests will use one big function. *)
811   let fty = function_type i32_type [| i32_type; i32_type |] in
812   let fn = define_function "X7" fty m in
813   let atentry = builder_at_end (entry_block fn) in
814   let p1 = param fn 0 ++ set_value_name "P1" in
815   let p2 = param fn 1 ++ set_value_name "P2" in
816   let f1 = build_uitofp p1 float_type "F1" atentry in
817   let f2 = build_uitofp p2 float_type "F2" atentry in
818   
819   let bb00 = append_block "Bb00" fn in
820   ignore (build_unreachable (builder_at_end bb00));
821   
822   group "ret"; begin
823     (* RUN: grep {ret.*P1} < %t.ll
824      *)
825     let ret = build_ret p1 atentry in
826     position_before ret atentry
827   end;
828   
829   group "br"; begin
830     (* RUN: grep {br.*Bb02} < %t.ll
831      *)
832     let bb02 = append_block "Bb02" fn in
833     let b = builder_at_end bb02 in
834     ignore (build_br bb02 b)
835   end;
836   
837   group "cond_br"; begin
838     (* RUN: grep {br.*Inst01.*Bb03.*Bb00} < %t.ll
839      *)
840     let bb03 = append_block "Bb03" fn in
841     let b = builder_at_end bb03 in
842     let cond = build_trunc p1 i1_type "Inst01" b in
843     ignore (build_cond_br cond bb03 bb00 b)
844   end;
845   
846   group "switch"; begin
847     (* RUN: grep {switch.*P1.*SwiBlock3} < %t.ll
848      * RUN: grep {2,.*SwiBlock2} < %t.ll
849      *)
850     let bb1 = append_block "SwiBlock1" fn in
851     let bb2 = append_block "SwiBlock2" fn in
852     ignore (build_unreachable (builder_at_end bb2));
853     let bb3 = append_block "SwiBlock3" fn in
854     ignore (build_unreachable (builder_at_end bb3));
855     let si = build_switch p1 bb3 1 (builder_at_end bb1) in
856     ignore (add_case si (const_int i32_type 2) bb2)
857   end;
858   
859   group "invoke"; begin
860     (* RUN: grep {Inst02.*invoke.*P1.*P2} < %t.ll
861      * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll
862      *)
863     let bb04 = append_block "Bb04" fn in
864     let b = builder_at_end bb04 in
865     ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "Inst02" b)
866   end;
867   
868   group "unwind"; begin
869     (* RUN: grep {unwind} < %t.ll
870      *)
871     let bb05 = append_block "Bb05" fn in
872     let b = builder_at_end bb05 in
873     ignore (build_unwind b)
874   end;
875   
876   group "unreachable"; begin
877     (* RUN: grep {unreachable} < %t.ll
878      *)
879     let bb06 = append_block "Bb06" fn in
880     let b = builder_at_end bb06 in
881     ignore (build_unreachable b)
882   end;
883   
884   group "arithmetic"; begin
885     let bb07 = append_block "Bb07" fn in
886     let b = builder_at_end bb07 in
887     
888     (* RUN: grep {Inst03.*add.*P1.*P2} < %t.ll
889      * RUN: grep {Inst04.*sub.*P1.*Inst03} < %t.ll
890      * RUN: grep {Inst05.*mul.*P1.*Inst04} < %t.ll
891      * RUN: grep {Inst06.*udiv.*P1.*Inst05} < %t.ll
892      * RUN: grep {Inst07.*sdiv.*P1.*Inst06} < %t.ll
893      * RUN: grep {Inst08.*fdiv.*F1.*F2} < %t.ll
894      * RUN: grep {Inst09.*urem.*P1.*Inst07} < %t.ll
895      * RUN: grep {Inst10.*srem.*P1.*Inst09} < %t.ll
896      * RUN: grep {Inst11.*frem.*F1.*Inst08} < %t.ll
897      * RUN: grep {Inst12.*shl.*P1.*Inst10} < %t.ll
898      * RUN: grep {Inst13.*lshr.*P1.*Inst12} < %t.ll
899      * RUN: grep {Inst14.*ashr.*P1.*Inst13} < %t.ll
900      * RUN: grep {Inst15.*and.*P1.*Inst14} < %t.ll
901      * RUN: grep {Inst16.*or.*P1.*Inst15} < %t.ll
902      * RUN: grep {Inst17.*xor.*P1.*Inst16} < %t.ll
903      * RUN: grep {Inst18.*sub.*0.*Inst17} < %t.ll
904      * RUN: grep {Inst19.*xor.*Inst18.*-1} < %t.ll
905      *)
906     let inst03 = build_add  p1 p2     "Inst03" b in
907     let inst04 = build_sub  p1 inst03 "Inst04" b in
908     let inst05 = build_mul  p1 inst04 "Inst05" b in
909     let inst06 = build_udiv p1 inst05 "Inst06" b in
910     let inst07 = build_sdiv p1 inst06 "Inst07" b in
911     let inst08 = build_fdiv f1 f2     "Inst08" b in
912     let inst09 = build_urem p1 inst07 "Inst09" b in
913     let inst10 = build_srem p1 inst09 "Inst10" b in
914           ignore(build_frem f1 inst08 "Inst11" b);
915     let inst12 = build_shl  p1 inst10 "Inst12" b in
916     let inst13 = build_lshr p1 inst12 "Inst13" b in
917     let inst14 = build_ashr p1 inst13 "Inst14" b in
918     let inst15 = build_and  p1 inst14 "Inst15" b in
919     let inst16 = build_or   p1 inst15 "Inst16" b in
920     let inst17 = build_xor  p1 inst16 "Inst17" b in
921     let inst18 = build_neg  inst17    "Inst18" b in
922          ignore (build_not  inst18    "Inst19" b);
923          ignore (build_unreachable b)
924   end;
925   
926   group "memory"; begin
927     let bb08 = append_block "Bb08" fn in
928     let b = builder_at_end bb08 in
929     
930     (* RUN: grep {Inst20.*malloc.*i8 } < %t.ll
931      * RUN: grep {Inst21.*malloc.*i8.*P1} < %t.ll
932      * RUN: grep {Inst22.*alloca.*i32 } < %t.ll
933      * RUN: grep {Inst23.*alloca.*i32.*P2} < %t.ll
934      * RUN: grep {free.*Inst20} < %t.ll
935      * RUN: grep {Inst25.*load.*Inst21} < %t.ll
936      * RUN: grep {store.*P2.*Inst22} < %t.ll
937      * RUN: grep {Inst27.*getelementptr.*Inst23.*P2} < %t.ll
938      *)
939     let inst20 = build_malloc i8_type "Inst20" b in
940     let inst21 = build_array_malloc i8_type p1 "Inst21" b in
941     let inst22 = build_alloca i32_type "Inst22" b in
942     let inst23 = build_array_alloca i32_type p2 "Inst23" b in
943           ignore(build_free inst20 b);
944           ignore(build_load inst21 "Inst25" b);
945           ignore(build_store p2 inst22 b);
946           ignore(build_gep inst23 [| p2 |] "Inst27" b);
947           ignore(build_unreachable b)
948   end;
949   
950   group "casts"; begin
951     let void_ptr = pointer_type i8_type in
952     
953     (* RUN: grep {Inst28.*trunc.*P1.*i8} < %t.ll
954      * RUN: grep {Inst29.*zext.*Inst28.*i32} < %t.ll
955      * RUN: grep {Inst30.*sext.*Inst29.*i64} < %t.ll
956      * RUN: grep {Inst31.*uitofp.*Inst30.*float} < %t.ll
957      * RUN: grep {Inst32.*sitofp.*Inst29.*double} < %t.ll
958      * RUN: grep {Inst33.*fptoui.*Inst31.*i32} < %t.ll
959      * RUN: grep {Inst34.*fptosi.*Inst32.*i64} < %t.ll
960      * RUN: grep {Inst35.*fptrunc.*Inst32.*float} < %t.ll
961      * RUN: grep {Inst36.*fpext.*Inst35.*double} < %t.ll
962      * RUN: grep {Inst37.*inttoptr.*P1.*i8\*} < %t.ll
963      * RUN: grep {Inst38.*ptrtoint.*Inst37.*i64} < %t.ll
964      * RUN: grep {Inst39.*bitcast.*Inst38.*double} < %t.ll
965      *)
966     let inst28 = build_trunc p1 i8_type "Inst28" atentry in
967     let inst29 = build_zext inst28 i32_type "Inst29" atentry in
968     let inst30 = build_sext inst29 i64_type "Inst30" atentry in
969     let inst31 = build_uitofp inst30 float_type "Inst31" atentry in
970     let inst32 = build_sitofp inst29 double_type "Inst32" atentry in
971           ignore(build_fptoui inst31 i32_type "Inst33" atentry);
972           ignore(build_fptosi inst32 i64_type "Inst34" atentry);
973     let inst35 = build_fptrunc inst32 float_type "Inst35" atentry in
974           ignore(build_fpext inst35 double_type "Inst36" atentry);
975     let inst37 = build_inttoptr p1 void_ptr "Inst37" atentry in
976     let inst38 = build_ptrtoint inst37 i64_type "Inst38" atentry in
977           ignore(build_bitcast inst38 double_type "Inst39" atentry)
978   end;
979   
980   group "comparisons"; begin
981     (* RUN: grep {Inst40.*icmp.*ne.*P1.*P2} < %t.ll
982      * RUN: grep {Inst41.*icmp.*sle.*P2.*P1} < %t.ll
983      * RUN: grep {Inst42.*fcmp.*false.*F1.*F2} < %t.ll
984      * RUN: grep {Inst43.*fcmp.*true.*F2.*F1} < %t.ll
985      *)
986     ignore (build_icmp Icmp.Ne    p1 p2 "Inst40" atentry);
987     ignore (build_icmp Icmp.Sle   p2 p1 "Inst41" atentry);
988     ignore (build_fcmp Fcmp.False f1 f2 "Inst42" atentry);
989     ignore (build_fcmp Fcmp.True  f2 f1 "Inst43" atentry)
990   end;
991   
992   group "miscellaneous"; begin
993     (* RUN: grep {CallInst.*call.*P2.*P1} < %t.ll
994      * RUN: grep {CallInst.*cc63} < %t.ll
995      * RUN: grep {Inst47.*select.*Inst46.*P1.*P2} < %t.ll
996      * RUN: grep {Inst48.*va_arg.*null.*i32} < %t.ll
997      * RUN: grep {Inst49.*extractelement.*Vec1.*P2} < %t.ll
998      * RUN: grep {Inst50.*insertelement.*Vec1.*P1.*P2} < %t.ll
999      * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*1.*1.*0.*0} < %t.ll
1000      * RUN: grep {CallInst.*tail call} < %t.ll
1001      *)
1002     let ci = build_call fn [| p2; p1 |] "CallInst" atentry in
1003     insist (CallConv.c = instruction_call_conv ci);
1004     set_instruction_call_conv 63 ci;
1005     insist (63 = instruction_call_conv ci);
1006     insist (not (is_tail_call ci));
1007     set_tail_call true ci;
1008     insist (is_tail_call ci);
1009     add_instruction_param_attr ci 1 Attribute.Sext;
1010     add_instruction_param_attr ci 2 Attribute.Noalias;
1011     remove_instruction_param_attr ci 2 Attribute.Noalias;
1012     
1013     let inst46 = build_icmp Icmp.Eq p1 p2 "Inst46" atentry in
1014          ignore (build_select inst46 p1 p2 "Inst47" atentry);
1015          ignore (build_va_arg
1016                   (const_null (pointer_type (pointer_type i8_type)))
1017                   i32_type "Inst48" atentry);
1018     
1019     (* Set up some vector vregs. *)
1020     let one  = const_int i32_type 1 in
1021     let zero = const_int i32_type 0 in
1022     let t1 = const_vector [| one; zero; one; zero |] in
1023     let t2 = const_vector [| zero; one; zero; one |] in
1024     let t3 = const_vector [| one; one; zero; zero |] in
1025     let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1026     let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1027     
1028     ignore (build_extractelement vec1 p2 "Inst49" atentry);
1029     ignore (build_insertelement vec1 p1 p2 "Inst50" atentry);
1030     ignore (build_shufflevector vec1 vec2 t3 "Inst51" atentry);
1031   end;
1032   
1033   group "phi"; begin
1034     (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll
1035      *)
1036     let b1 = append_block "PhiBlock1" fn in
1037     let b2 = append_block "PhiBlock2" fn in
1038     
1039     let jb = append_block "PhiJoinBlock" fn in
1040     ignore (build_br jb (builder_at_end b1));
1041     ignore (build_br jb (builder_at_end b2));
1042     let at_jb = builder_at_end jb in
1043     
1044     let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1045     insist ([(p1, b1)] = incoming phi);
1046     
1047     add_incoming (p2, b2) phi;
1048     insist ([(p1, b1); (p2, b2)] = incoming phi);
1049     
1050     ignore (build_unreachable at_jb);
1051   end
1052
1053
1054 (*===-- Module Provider ---------------------------------------------------===*)
1055
1056 let test_module_provider () =
1057   let m = create_module "test" in
1058   let mp = ModuleProvider.create m in
1059   ModuleProvider.dispose mp
1060
1061
1062 (*===-- Pass Managers -----------------------------------------------------===*)
1063
1064 let test_pass_manager () =
1065   let (++) x f = ignore (f x); x in
1066
1067   begin group "module pass manager";
1068     ignore (PassManager.create ()
1069              ++ PassManager.run_module m
1070              ++ PassManager.dispose)
1071   end;
1072   
1073   begin group "function pass manager";
1074     let fty = function_type void_type [| |] in
1075     let fn = define_function "FunctionPassManager" fty m in
1076     ignore (build_ret_void (builder_at_end (entry_block fn)));
1077     
1078     ignore (PassManager.create_function mp
1079              ++ PassManager.initialize
1080              ++ PassManager.run_function fn
1081              ++ PassManager.finalize
1082              ++ PassManager.dispose)
1083   end
1084
1085
1086 (*===-- Writer ------------------------------------------------------------===*)
1087
1088 let test_writer () =
1089   group "valid";
1090   insist (match Llvm_analysis.verify_module m with
1091           | None -> true
1092           | Some msg -> prerr_string msg; false);
1093
1094   group "writer";
1095   insist (write_bitcode_file m filename);
1096   
1097   ModuleProvider.dispose mp
1098
1099
1100 (*===-- Driver ------------------------------------------------------------===*)
1101
1102 let _ =
1103   suite "target"           test_target;
1104   suite "types"            test_types;
1105   suite "constants"        test_constants;
1106   suite "global values"    test_global_values;
1107   suite "global variables" test_global_variables;
1108   suite "functions"        test_functions;
1109   suite "params"           test_params;
1110   suite "basic blocks"     test_basic_blocks;
1111   suite "instructions"     test_instructions;
1112   suite "builder"          test_builder;
1113   suite "module provider"  test_module_provider;
1114   suite "pass manager"     test_pass_manager;
1115   suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1116   exit !exit_status