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