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