Incorporating review feedback for GC verifier patch.
[oota-llvm.git] / test / Bindings / Ocaml / vmcore.ml
1 (* RUN: %ocamlc llvm.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   let msg = if cond then "    pass " else begin
25     exit_status := 10;
26     "    FAIL "
27   end in
28   prerr_endline (msg ^ (string_of_int !case_num))
29
30 let suite name f =
31   prerr_endline (name ^ ":");
32   f ()
33
34
35 (*===-- Fixture -----------------------------------------------------------===*)
36
37 let filename = Sys.argv.(1)
38 let m = create_module filename
39
40
41 (*===-- Types -------------------------------------------------------------===*)
42
43 let test_types () =
44   (* RUN: grep {Ty01.*void} < %t.ll
45    *)
46   group "void";
47   insist (add_type_name "Ty01" void_type m);
48   insist (Void_type == classify_type void_type);
49
50   (* RUN: grep {Ty02.*i1} < %t.ll
51    *)
52   group "i1";
53   insist (add_type_name "Ty02" i1_type m);
54   insist (Integer_type == classify_type i1_type);
55
56   (* RUN: grep {Ty03.*i32} < %t.ll
57    *)
58   group "i32";
59   insist (add_type_name "Ty03" i32_type m);
60
61   (* RUN: grep {Ty04.*i42} < %t.ll
62    *)
63   group "i42";
64   let ty = make_integer_type 42 in
65   insist (add_type_name "Ty04" ty m);
66
67   (* RUN: grep {Ty05.*float} < %t.ll
68    *)
69   group "float";
70   insist (add_type_name "Ty05" float_type m);
71   insist (Float_type == classify_type float_type);
72
73   (* RUN: grep {Ty06.*double} < %t.ll
74    *)
75   group "double";
76   insist (add_type_name "Ty06" double_type m);
77   insist (Double_type == classify_type double_type);
78
79   (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
80    *)
81   group "function";
82   let ty = make_function_type i32_type [| i1_type; double_type |] false in
83   insist (add_type_name "Ty07" ty m);
84   insist (Function_type = classify_type ty);
85   insist (not (is_var_arg ty));
86   insist (i32_type == return_type ty);
87   insist (double_type == (param_types ty).(1));
88   
89   (* RUN: grep {Ty08.*\.\.\.} < %t.ll
90    *)
91   group "vararg";
92   let ty = make_function_type void_type [| i32_type |] true in
93   insist (add_type_name "Ty08" ty m);
94   insist (is_var_arg ty);
95   
96   (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll
97    *)
98   group "array";
99   let ty = make_array_type i8_type 7 in
100   insist (add_type_name "Ty09" ty m);
101   insist (7 = array_length ty);
102   insist (i8_type == element_type ty);
103   insist (Array_type == classify_type ty);
104   
105   (* RUN: grep {Ty10.*float\*} < %t.ll
106    *)
107   group "pointer";
108   let ty = make_pointer_type float_type in
109   insist (add_type_name "Ty10" ty m);
110   insist (float_type == element_type ty);
111   insist (Pointer_type == classify_type ty);
112   
113   (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll
114    *)
115   group "vector";
116   let ty = make_vector_type i16_type 4 in
117   insist (add_type_name "Ty11" ty m);
118   insist (i16_type == element_type ty);
119   insist (4 = vector_size ty);
120   
121   (* RUN: grep {Ty12.*opaque} < %t.ll
122    *)
123   group "opaque";
124   let ty = make_opaque_type () in
125   insist (add_type_name "Ty12" ty m);
126   insist (ty == ty);
127   insist (ty <> make_opaque_type ())
128
129
130 (*===-- Constants ---------------------------------------------------------===*)
131
132 let test_constants () =
133   (* RUN: grep {Const01.*i32.*-1} < %t.ll
134    *)
135   group "int";
136   let c = make_int_constant i32_type (-1) true in
137   ignore (define_global "Const01" c m);
138   insist (i32_type = type_of c);
139   insist (is_constant c);
140
141   (* RUN: grep {Const02.*i64.*-1} < %t.ll
142    *)
143   group "sext int";
144   let c = make_int_constant i64_type (-1) true in
145   ignore (define_global "Const02" c m);
146   insist (i64_type = type_of c);
147
148   (* RUN: grep {Const03.*i64.*4294967295} < %t.ll
149    *)
150   group "zext int64";
151   let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in
152   ignore (define_global "Const03" c m);
153   insist (i64_type = type_of c);
154
155   (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll
156    *)
157   group "string";
158   let c = make_string_constant "cruel\000world" false in
159   ignore (define_global "Const04" c m);
160   insist ((make_array_type i8_type 11) = type_of c);
161
162   (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll
163    *)
164   group "string w/ null";
165   let c = make_string_constant "hi\000again" true in
166   prerr_string "====> ";
167   prerr_int (array_length (type_of c));
168   prerr_endline " <====";
169   ignore (define_global "Const05" c m);
170   insist ((make_array_type i8_type 9) = type_of c);
171
172   (* RUN: grep {Const06.*3.1459} < %t.ll
173    *)
174   group "real";
175   let c = make_real_constant double_type 3.1459 in
176   ignore (define_global "Const06" c m);
177   insist (double_type = type_of c);
178   
179   let one = make_int_constant i16_type 1 true in
180   let two = make_int_constant i16_type 2 true in
181   let three = make_int_constant i32_type 3 true in
182   let four = make_int_constant i32_type 4 true in
183   
184   (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll
185    *)
186   group "array";
187   let c = make_array_constant i32_type [| three; four |] in
188   ignore (define_global "Const07" c m);
189   insist ((make_array_type i32_type 2) = (type_of c));
190   
191   (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll
192    *)
193   group "vector";
194   let c = make_vector_constant [| one; two; one; two;
195                                   one; two; one; two |] in
196   ignore (define_global "Const08" c m);
197   insist ((make_vector_type i16_type 8) = (type_of c));
198   
199   (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll
200    *)
201   group "structure";
202   let c = make_struct_constant [| one; two; three; four |] false in
203   ignore (define_global "Const09" c m);
204   insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false)
205         = (type_of c));
206   
207   (* RUN: grep {Const10.*zeroinit} < %t.ll
208    *)
209   group "null";
210   let c = make_null (make_struct_type [| i1_type; i8_type;
211                                          i64_type; double_type |] true) in
212   ignore (define_global "Const10" c m);
213   
214   (* RUN: grep {Const11.*-1} < %t.ll
215    *)
216   group "all ones";
217   let c = make_all_ones i64_type in
218   ignore (define_global "Const11" c m);
219   
220   (* RUN: grep {Const12.*undef} < %t.ll
221    *)
222   group "undef";
223   let c = make_undef i1_type in
224   ignore (define_global "Const12" c m);
225   insist (i1_type = type_of c);
226   insist (is_undef c)
227
228
229 (*===-- Global Values -----------------------------------------------------===*)
230
231 let test_global_values () =
232   let (++) x f = f x; x in
233   let zero32 = make_null i32_type in
234
235   (* RUN: grep {GVal01} < %t.ll
236    *)
237   group "naming";
238   let g = define_global "TEMPORARY" zero32 m in
239   insist ("TEMPORARY" = value_name g);
240   set_value_name "GVal01" g;
241   insist ("GVal01" = value_name g);
242
243   (* RUN: grep {GVal02.*linkonce} < %t.ll
244    *)
245   group "linkage";
246   let g = define_global "GVal02" zero32 m ++
247           set_linkage Link_once_linkage in
248   insist (Link_once_linkage = linkage g);
249
250   (* RUN: grep {GVal03.*Hanalei} < %t.ll
251    *)
252   group "section";
253   let g = define_global "GVal03" zero32 m ++
254           set_section "Hanalei" in
255   insist ("Hanalei" = section g);
256   
257   (* RUN: grep {GVal04.*hidden} < %t.ll
258    *)
259   group "visibility";
260   let g = define_global "GVal04" zero32 m ++
261           set_visibility Hidden_visibility in
262   insist (Hidden_visibility = visibility g);
263   
264   (* RUN: grep {GVal05.*align 128} < %t.ll
265    *)
266   group "alignment";
267   let g = define_global "GVal05" zero32 m ++
268           set_alignment 128 in
269   insist (128 = alignment g)
270
271
272 (*===-- Global Variables --------------------------------------------------===*)
273
274 let test_global_variables () =
275   let (++) x f = f x; x in
276   let fourty_two32 = make_int_constant i32_type 42 false in
277
278   (* RUN: grep {GVar01.*external} < %t.ll
279    *)
280   group "declarations";
281   let g = declare_global i32_type "GVar01" m in
282   insist (is_declaration g);
283   
284   (* RUN: grep {GVar02.*42} < %t.ll
285    * RUN: grep {GVar03.*42} < %t.ll
286    *)
287   group "definitions";
288   let g = define_global "GVar02" fourty_two32 m in
289   let g2 = declare_global i32_type "GVar03" m ++
290            set_initializer fourty_two32 in
291   insist (not (is_declaration g));
292   insist (not (is_declaration g2));
293   insist ((global_initializer g) == (global_initializer g2));
294
295   (* RUN: grep {GVar04.*thread_local} < %t.ll
296    *)
297   group "threadlocal";
298   let g = define_global "GVar04" fourty_two32 m ++
299           set_thread_local true in
300   insist (is_thread_local g);
301
302   (* RUN: grep -v {GVar05} < %t.ll
303    *)
304   group "delete";
305   let g = define_global "GVar05" fourty_two32 m in
306   delete_global g
307
308
309 (*===-- Writer ------------------------------------------------------------===*)
310
311 let test_writer () =
312   group "writer";
313   insist (write_bitcode_file m filename);
314   
315   dispose_module m
316
317
318 (*===-- Driver ------------------------------------------------------------===*)
319
320 let _ =
321   suite "types"            test_types;
322   suite "constants"        test_constants;
323   suite "global values"    test_global_values;
324   suite "global variables" test_global_variables;
325   suite "writer"           test_writer;
326   exit !exit_status