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