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