vmcore.ml revision 3ba5de6a6363d2eb3e80ace2413b03df3cd69801
1(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %s -o %t
2 * RUN: %t %t.bc
3 * RUN: llvm-dis < %t.bc > %t.ll
4 * XFAIL: vg_leak
5 *)
6
7(* Note: It takes several seconds for ocamlopt to link an executable with
8         libLLVMCore.a, so it's better to write a big test than a bunch of
9         little ones. *)
10
11open Llvm
12open Llvm_bitwriter
13
14
15(* Tiny unit test framework - really just to help find which line is busted *)
16let exit_status = ref 0
17let suite_name = ref ""
18let group_name = ref ""
19let case_num = ref 0
20let print_checkpoints = false
21let context = global_context ()
22let i1_type = Llvm.i1_type context
23let i8_type = Llvm.i8_type context
24let i16_type = Llvm.i16_type context
25let i32_type = Llvm.i32_type context
26let i64_type = Llvm.i64_type context
27let void_type = Llvm.void_type context
28let float_type = Llvm.float_type context
29let double_type = Llvm.double_type context
30let fp128_type = Llvm.fp128_type context
31
32let group name =
33  group_name := !suite_name ^ "/" ^ name;
34  case_num := 0;
35  if print_checkpoints then
36    prerr_endline ("  " ^ name ^ "...")
37
38let insist cond =
39  incr case_num;
40  if not cond then
41    exit_status := 10;
42  match print_checkpoints, cond with
43  | false, true -> ()
44  | false, false ->
45      prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
46  | true, true ->
47      prerr_endline ("    " ^ (string_of_int !case_num))
48  | true, false ->
49      prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
50
51let suite name f =
52  suite_name := name;
53  if print_checkpoints then
54    prerr_endline (name ^ ":");
55  f ()
56
57
58(*===-- Fixture -----------------------------------------------------------===*)
59
60let filename = Sys.argv.(1)
61let m = create_module context filename
62
63
64(*===-- Target ------------------------------------------------------------===*)
65
66let test_target () =
67  begin group "triple";
68    (* RUN: grep "i686-apple-darwin8" < %t.ll
69     *)
70    let trip = "i686-apple-darwin8" in
71    set_target_triple trip m;
72    insist (trip = target_triple m)
73  end;
74  
75  begin group "layout";
76    (* RUN: grep "bogus" < %t.ll
77     *)
78    let layout = "bogus" in
79    set_data_layout layout m;
80    insist (layout = data_layout m)
81  end
82
83
84(*===-- Constants ---------------------------------------------------------===*)
85
86let test_constants () =
87  (* RUN: grep "const_int.*i32.*-1" < %t.ll
88   *)
89  group "int";
90  let c = const_int i32_type (-1) in
91  ignore (define_global "const_int" c m);
92  insist (i32_type = type_of c);
93  insist (is_constant c);
94
95  (* RUN: grep "const_sext_int.*i64.*-1" < %t.ll
96   *)
97  group "sext int";
98  let c = const_int i64_type (-1) in
99  ignore (define_global "const_sext_int" c m);
100  insist (i64_type = type_of c);
101
102  (* RUN: grep "const_zext_int64.*i64.*4294967295" < %t.ll
103   *)
104  group "zext int64";
105  let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
106  ignore (define_global "const_zext_int64" c m);
107  insist (i64_type = type_of c);
108
109  (* RUN: grep "const_int_string.*i32.*-1" < %t.ll
110   *)
111  group "int string";
112  let c = const_int_of_string i32_type "-1" 10 in
113  ignore (define_global "const_int_string" c m);
114  insist (i32_type = type_of c);
115
116  (* RUN: grep 'const_string.*"cruel\\00world"' < %t.ll
117   *)
118  group "string";
119  let c = const_string context "cruel\000world" in
120  ignore (define_global "const_string" c m);
121  insist ((array_type i8_type 11) = type_of c);
122
123  (* RUN: grep 'const_stringz.*"hi\\00again\\00"' < %t.ll
124   *)
125  group "stringz";
126  let c = const_stringz context "hi\000again" in
127  ignore (define_global "const_stringz" c m);
128  insist ((array_type i8_type 9) = type_of c);
129
130  (* RUN: grep "const_single.*2.75" < %t.ll
131   * RUN: grep "const_double.*3.1459" < %t.ll
132   * RUN: grep "const_double_string.*1.25" < %t.ll
133   *)
134  begin group "real";
135    let cs = const_float float_type 2.75 in
136    ignore (define_global "const_single" cs m);
137    insist (float_type = type_of cs);
138    
139    let cd = const_float double_type 3.1459 in
140    ignore (define_global "const_double" cd m);
141    insist (double_type = type_of cd);
142
143    let cd = const_float_of_string double_type "1.25" in
144    ignore (define_global "const_double_string" cd m);
145    insist (double_type = type_of cd)
146  end;
147  
148  let one = const_int i16_type 1 in
149  let two = const_int i16_type 2 in
150  let three = const_int i32_type 3 in
151  let four = const_int i32_type 4 in
152  
153  (* RUN: grep "const_array.*[i32 3, i32 4]" < %t.ll
154   *)
155  group "array";
156  let c = const_array i32_type [| three; four |] in
157  ignore (define_global "const_array" c m);
158  insist ((array_type i32_type 2) = (type_of c));
159  
160  (* RUN: grep "const_vector.*<i16 1, i16 2.*>" < %t.ll
161   *)
162  group "vector";
163  let c = const_vector [| one; two; one; two;
164                          one; two; one; two |] in
165  ignore (define_global "const_vector" c m);
166  insist ((vector_type i16_type 8) = (type_of c));
167
168  (* RUN: grep "const_structure.*.i16 1, i16 2, i32 3, i32 4" < %t.ll
169   *)
170  group "structure";
171  let c = const_struct context [| one; two; three; four |] in
172  ignore (define_global "const_structure" c m);
173  insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
174        = (type_of c));
175
176  (* RUN: grep "const_null.*zeroinit" < %t.ll
177   *)
178  group "null";
179  let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
180                                                    double_type |]) in
181  ignore (define_global "const_null" c m);
182  
183  (* RUN: grep "const_all_ones.*-1" < %t.ll
184   *)
185  group "all ones";
186  let c = const_all_ones i64_type in
187  ignore (define_global "const_all_ones" c m);
188
189  group "pointer null"; begin
190    (* RUN: grep "const_pointer_null = global i64\* null" < %t.ll
191     *)
192    let c = const_pointer_null (pointer_type i64_type) in
193    ignore (define_global "const_pointer_null" c m);
194  end;
195  
196  (* RUN: grep "const_undef.*undef" < %t.ll
197   *)
198  group "undef";
199  let c = undef i1_type in
200  ignore (define_global "const_undef" c m);
201  insist (i1_type = type_of c);
202  insist (is_undef c);
203  
204  group "constant arithmetic";
205  (* RUN: grep "@const_neg = global i64 sub" < %t.ll
206   * RUN: grep "@const_nsw_neg = global i64 sub nsw " < %t.ll
207   * RUN: grep "@const_nuw_neg = global i64 sub nuw " < %t.ll
208   * RUN: grep "@const_fneg = global double fsub " < %t.ll
209   * RUN: grep "@const_not = global i64 xor " < %t.ll
210   * RUN: grep "@const_add = global i64 add " < %t.ll
211   * RUN: grep "@const_nsw_add = global i64 add nsw " < %t.ll
212   * RUN: grep "@const_nuw_add = global i64 add nuw " < %t.ll
213   * RUN: grep "@const_fadd = global double fadd " < %t.ll
214   * RUN: grep "@const_sub = global i64 sub " < %t.ll
215   * RUN: grep "@const_nsw_sub = global i64 sub nsw " < %t.ll
216   * RUN: grep "@const_nuw_sub = global i64 sub nuw " < %t.ll
217   * RUN: grep "@const_fsub = global double fsub " < %t.ll
218   * RUN: grep "@const_mul = global i64 mul " < %t.ll
219   * RUN: grep "@const_nsw_mul = global i64 mul nsw " < %t.ll
220   * RUN: grep "@const_nuw_mul = global i64 mul nuw " < %t.ll
221   * RUN: grep "@const_fmul = global double fmul " < %t.ll
222   * RUN: grep "@const_udiv = global i64 udiv " < %t.ll
223   * RUN: grep "@const_sdiv = global i64 sdiv " < %t.ll
224   * RUN: grep "@const_exact_sdiv = global i64 sdiv exact " < %t.ll
225   * RUN: grep "@const_fdiv = global double fdiv " < %t.ll
226   * RUN: grep "@const_urem = global i64 urem " < %t.ll
227   * RUN: grep "@const_srem = global i64 srem " < %t.ll
228   * RUN: grep "@const_frem = global double frem " < %t.ll
229   * RUN: grep "@const_and = global i64 and " < %t.ll
230   * RUN: grep "@const_or = global i64 or " < %t.ll
231   * RUN: grep "@const_xor = global i64 xor " < %t.ll
232   * RUN: grep "@const_icmp = global i1 icmp sle " < %t.ll
233   * RUN: grep "@const_fcmp = global i1 fcmp ole " < %t.ll
234   *)
235  let void_ptr = pointer_type i8_type in
236  let five = const_int i64_type 5 in
237  let ffive = const_uitofp five double_type in
238  let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
239  let foldbomb = const_ptrtoint foldbomb_gv i64_type in
240  let ffoldbomb = const_uitofp foldbomb double_type in
241  ignore (define_global "const_neg" (const_neg foldbomb) m);
242  ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
243  ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
244  ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
245  ignore (define_global "const_not" (const_not foldbomb) m);
246  ignore (define_global "const_add" (const_add foldbomb five) m);
247  ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
248  ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
249  ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
250  ignore (define_global "const_sub" (const_sub foldbomb five) m);
251  ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
252  ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
253  ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
254  ignore (define_global "const_mul" (const_mul foldbomb five) m);
255  ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
256  ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
257  ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
258  ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
259  ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
260  ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
261  ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
262  ignore (define_global "const_urem" (const_urem foldbomb five) m);
263  ignore (define_global "const_srem" (const_srem foldbomb five) m);
264  ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
265  ignore (define_global "const_and" (const_and foldbomb five) m);
266  ignore (define_global "const_or" (const_or foldbomb five) m);
267  ignore (define_global "const_xor" (const_xor foldbomb five) m);
268  ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
269  ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
270  
271  group "constant casts";
272  (* RUN: grep "const_trunc.*trunc" < %t.ll
273   * RUN: grep "const_sext.*sext" < %t.ll
274   * RUN: grep "const_zext.*zext" < %t.ll
275   * RUN: grep "const_fptrunc.*fptrunc" < %t.ll
276   * RUN: grep "const_fpext.*fpext" < %t.ll
277   * RUN: grep "const_uitofp.*uitofp" < %t.ll
278   * RUN: grep "const_sitofp.*sitofp" < %t.ll
279   * RUN: grep "const_fptoui.*fptoui" < %t.ll
280   * RUN: grep "const_fptosi.*fptosi" < %t.ll
281   * RUN: grep "const_ptrtoint.*ptrtoint" < %t.ll
282   * RUN: grep "const_inttoptr.*inttoptr" < %t.ll
283   * RUN: grep "const_bitcast.*bitcast" < %t.ll
284   *)
285  let i128_type = integer_type context 128 in
286  ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
287                                               i8_type) m);
288  ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
289  ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
290  ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
291  ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
292  ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
293  ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
294  ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
295  ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
296  ignore (define_global "const_ptrtoint" (const_ptrtoint 
297    (const_gep (const_null (pointer_type i8_type))
298               [| const_int i32_type 1 |])
299    i32_type) m);
300  ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
301                                                  void_ptr) m);
302  ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
303  
304  group "misc constants";
305  (* RUN: grep "const_size_of.*getelementptr.*null" < %t.ll
306   * RUN: grep "const_gep.*getelementptr" < %t.ll
307   * RUN: grep "const_select.*select" < %t.ll
308   * RUN: grep "const_extractelement.*extractelement" < %t.ll
309   * RUN: grep "const_insertelement.*insertelement" < %t.ll
310   * RUN: grep "const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>" < %t.ll
311   *)
312  ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
313  ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
314  ignore (define_global "const_select" (const_select
315    (const_icmp Icmp.Sle foldbomb five)
316    (const_int i8_type (-1))
317    (const_int i8_type 0)) m);
318  let zero = const_int i32_type 0 in
319  let one  = const_int i32_type 1 in
320  ignore (define_global "const_extractelement" (const_extractelement
321    (const_vector [| zero; one; zero; one |])
322    (const_trunc foldbomb i32_type)) m);
323  ignore (define_global "const_insertelement" (const_insertelement
324    (const_vector [| zero; one; zero; one |])
325    zero (const_trunc foldbomb i32_type)) m);
326  ignore (define_global "const_shufflevector" (const_shufflevector
327    (const_vector [| zero; one |])
328    (const_vector [| one; zero |])
329    (const_vector [| const_int i32_type 0; const_int i32_type 1;
330                     const_int i32_type 2; const_int i32_type 3 |])) m);
331
332  group "asm"; begin
333    let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
334    ignore (const_inline_asm
335      ft
336      ""
337      "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
338      true
339      false)
340  end;
341
342  group "recursive struct"; begin
343      let nsty = named_struct_type context "rec" in
344      let pty = pointer_type nsty in
345      struct_set_body nsty [| i32_type; pty |] false;
346      let elts = [| const_int i32_type 4; const_pointer_null pty |] in
347      let grec_init = const_named_struct nsty elts in
348      ignore (define_global "grec" grec_init m);
349      ignore (string_of_lltype nsty);
350  end
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  group "declarations"; begin
403    (* RUN: grep "GVar01.*external" < %t.ll
404     *)
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    insist (None == lookup_global "QGVar01" m);
415    let g = declare_qualified_global i32_type "QGVar01" 3 m in
416    insist (is_declaration g);
417    insist (qualified_pointer_type float_type 3 ==
418              type_of (declare_qualified_global float_type "QGVar01" 3 m));
419    insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
420    insist (match lookup_global "QGVar01" m with Some x -> x = g
421                                              | None -> false);
422  end;
423  
424  group "definitions"; begin
425    (* RUN: grep "GVar02.*42" < %t.ll
426     * RUN: grep "GVar03.*42" < %t.ll
427     *)
428    let g = define_global "GVar02" fourty_two32 m in
429    let g2 = declare_global i32_type "GVar03" m ++
430           set_initializer fourty_two32 in
431    insist (not (is_declaration g));
432    insist (not (is_declaration g2));
433    insist ((global_initializer g) == (global_initializer g2));
434
435    let g = define_qualified_global "QGVar02" fourty_two32 3 m in
436    let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
437           set_initializer fourty_two32 in
438    insist (not (is_declaration g));
439    insist (not (is_declaration g2));
440    insist ((global_initializer g) == (global_initializer g2));
441  end;
442
443  (* RUN: grep "GVar04.*thread_local" < %t.ll
444   *)
445  group "threadlocal";
446  let g = define_global "GVar04" fourty_two32 m ++
447          set_thread_local true in
448  insist (is_thread_local g);
449
450  (* RUN: grep -v "GVar05" < %t.ll
451   *)
452  group "delete";
453  let g = define_global "GVar05" fourty_two32 m in
454  delete_global g;
455
456  (* RUN: grep -v "ConstGlobalVar.*constant" < %t.ll
457   *)
458  group "constant";
459  let g = define_global "ConstGlobalVar" fourty_two32 m in
460  insist (not (is_global_constant g));
461  set_global_constant true g;
462  insist (is_global_constant g);
463  
464  begin group "iteration";
465    let m = create_module context "temp" in
466    
467    insist (At_end m = global_begin m);
468    insist (At_start m = global_end m);
469    
470    let g1 = declare_global i32_type "One" m in
471    let g2 = declare_global i32_type "Two" m in
472    
473    insist (Before g1 = global_begin m);
474    insist (Before g2 = global_succ g1);
475    insist (At_end m = global_succ g2);
476    
477    insist (After g2 = global_end m);
478    insist (After g1 = global_pred g2);
479    insist (At_start m = global_pred g1);
480    
481    let lf s x = s ^ "->" ^ value_name x in
482    insist ("->One->Two" = fold_left_globals lf "" m);
483    
484    let rf x s = value_name x ^ "<-" ^ s in
485    insist ("One<-Two<-" = fold_right_globals rf m "");
486    
487    dispose_module m
488  end
489
490
491(*===-- Uses --------------------------------------------------------------===*)
492
493let test_uses () =
494  let ty = function_type i32_type [| i32_type; i32_type |] in
495  let fn = define_function "use_function" ty m in
496  let b = builder_at_end context (entry_block fn) in
497
498  let p1 = param fn 0 in
499  let p2 = param fn 1 in
500  let v1 = build_add p1 p2 "v1" b in
501  let v2 = build_add p1 v1 "v2" b in
502  let _ = build_add v1 v2 "v3" b in
503
504  let lf s u = value_name (user u) ^ "->" ^ s in
505  insist ("v2->v3->" = fold_left_uses lf "" v1);
506  let rf u s = value_name (user u) ^ "<-" ^ s in
507  insist ("v3<-v2<-" = fold_right_uses rf v1 "");
508
509  let lf s u = value_name (used_value u) ^ "->" ^ s in
510  insist ("v1->v1->" = fold_left_uses lf "" v1);
511
512  let rf u s = value_name (used_value u) ^ "<-" ^ s in
513  insist ("v1<-v1<-" = fold_right_uses rf v1 "");
514
515  ignore (build_unreachable b)
516
517
518(*===-- Users -------------------------------------------------------------===*)
519
520let test_users () =
521  let ty = function_type i32_type [| i32_type; i32_type |] in
522  let fn = define_function "user_function" ty m in
523  let b = builder_at_end context (entry_block fn) in
524
525  let p1 = param fn 0 in
526  let p2 = param fn 1 in
527  let a3 = build_alloca i32_type "user_alloca" b in
528  let p3 = build_load a3 "user_load" b in
529  let i = build_add p1 p2 "sum" b in
530
531  insist ((num_operands i) = 2);
532  insist ((operand i 0) = p1);
533  insist ((operand i 1) = p2);
534
535  set_operand i 1 p3;
536  insist ((operand i 1) != p2);
537  insist ((operand i 1) = p3);
538
539  ignore (build_unreachable b)
540
541
542(*===-- Aliases -----------------------------------------------------------===*)
543
544let test_aliases () =
545  (* RUN: grep "@alias = alias i32\* @aliasee" < %t.ll
546   *)
547  let v = declare_global i32_type "aliasee" m in
548  ignore (add_alias m (pointer_type i32_type) v "alias")
549
550
551(*===-- Functions ---------------------------------------------------------===*)
552
553let test_functions () =
554  let ty = function_type i32_type [| i32_type; i64_type |] in
555  let ty2 = function_type i8_type [| i8_type; i64_type |] in
556  
557  (* RUN: grep 'declare i32 @Fn1(i32, i64)' < %t.ll
558   *)
559  begin group "declare";
560    insist (None = lookup_function "Fn1" m);
561    let fn = declare_function "Fn1" ty m in
562    insist (pointer_type ty = type_of fn);
563    insist (is_declaration fn);
564    insist (0 = Array.length (basic_blocks fn));
565    insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
566    insist (fn == declare_function "Fn1" ty m);
567    insist (None <> lookup_function "Fn1" m);
568    insist (match lookup_function "Fn1" m with Some x -> x = fn
569                                             | None -> false);
570    insist (m == global_parent fn)
571  end;
572  
573  (* RUN: grep -v "Fn2" < %t.ll
574   *)
575  group "delete";
576  let fn = declare_function "Fn2" ty m in
577  delete_function fn;
578  
579  (* RUN: grep "define.*Fn3" < %t.ll
580   *)
581  group "define";
582  let fn = define_function "Fn3" ty m in
583  insist (not (is_declaration fn));
584  insist (1 = Array.length (basic_blocks fn));
585  ignore (build_unreachable (builder_at_end context (entry_block fn)));
586  
587  (* RUN: grep "define.*Fn4.*Param1.*Param2" < %t.ll
588   *)
589  group "params";
590  let fn = define_function "Fn4" ty m in
591  let params = params fn in
592  insist (2 = Array.length params);
593  insist (params.(0) = param fn 0);
594  insist (params.(1) = param fn 1);
595  insist (i32_type = type_of params.(0));
596  insist (i64_type = type_of params.(1));
597  set_value_name "Param1" params.(0);
598  set_value_name "Param2" params.(1);
599  ignore (build_unreachable (builder_at_end context (entry_block fn)));
600  
601  (* RUN: grep "fastcc.*Fn5" < %t.ll
602   *)
603  group "callconv";
604  let fn = define_function "Fn5" ty m in
605  insist (CallConv.c = function_call_conv fn);
606  set_function_call_conv CallConv.fast fn;
607  insist (CallConv.fast = function_call_conv fn);
608  ignore (build_unreachable (builder_at_end context (entry_block fn)));
609  
610  begin group "gc";
611    (* RUN: grep "Fn6.*gc.*shadowstack" < %t.ll
612     *)
613    let fn = define_function "Fn6" ty m in
614    insist (None = gc fn);
615    set_gc (Some "ocaml") fn;
616    insist (Some "ocaml" = gc fn);
617    set_gc None fn;
618    insist (None = gc fn);
619    set_gc (Some "shadowstack") fn;
620    ignore (build_unreachable (builder_at_end context (entry_block fn)));
621  end;
622  
623  begin group "iteration";
624    let m = create_module context "temp" in
625    
626    insist (At_end m = function_begin m);
627    insist (At_start m = function_end m);
628    
629    let f1 = define_function "One" ty m in
630    let f2 = define_function "Two" ty m in
631    
632    insist (Before f1 = function_begin m);
633    insist (Before f2 = function_succ f1);
634    insist (At_end m = function_succ f2);
635    
636    insist (After f2 = function_end m);
637    insist (After f1 = function_pred f2);
638    insist (At_start m = function_pred f1);
639    
640    let lf s x = s ^ "->" ^ value_name x in
641    insist ("->One->Two" = fold_left_functions lf "" m);
642    
643    let rf x s = value_name x ^ "<-" ^ s in
644    insist ("One<-Two<-" = fold_right_functions rf m "");
645    
646    dispose_module m
647  end
648
649
650(*===-- Params ------------------------------------------------------------===*)
651
652let test_params () =
653  begin group "iteration";
654    let m = create_module context "temp" in
655    
656    let vf = define_function "void" (function_type void_type [| |]) m in
657    
658    insist (At_end vf = param_begin vf);
659    insist (At_start vf = param_end vf);
660    
661    let ty = function_type void_type [| i32_type; i32_type |] in
662    let f = define_function "f" ty m in
663    let p1 = param f 0 in
664    let p2 = param f 1 in
665    set_value_name "One" p1;
666    set_value_name "Two" p2;
667    add_param_attr p1 Attribute.Sext;
668    add_param_attr p2 Attribute.Noalias;
669    remove_param_attr p2 Attribute.Noalias;
670    add_function_attr f Attribute.Nounwind;
671    add_function_attr f Attribute.Noreturn;
672    remove_function_attr f Attribute.Noreturn;
673
674    insist (Before p1 = param_begin f);
675    insist (Before p2 = param_succ p1);
676    insist (At_end f = param_succ p2);
677    
678    insist (After p2 = param_end f);
679    insist (After p1 = param_pred p2);
680    insist (At_start f = param_pred p1);
681    
682    let lf s x = s ^ "->" ^ value_name x in
683    insist ("->One->Two" = fold_left_params lf "" f);
684    
685    let rf x s = value_name x ^ "<-" ^ s in
686    insist ("One<-Two<-" = fold_right_params rf f "");
687    
688    dispose_module m
689  end
690
691
692(*===-- Basic Blocks ------------------------------------------------------===*)
693
694let test_basic_blocks () =
695  let ty = function_type void_type [| |] in
696  
697  (* RUN: grep "Bb1" < %t.ll
698   *)
699  group "entry";
700  let fn = declare_function "X" ty m in
701  let bb = append_block context "Bb1" fn in
702  insist (bb = entry_block fn);
703  ignore (build_unreachable (builder_at_end context bb));
704  
705  (* RUN: grep -v Bb2 < %t.ll
706   *)
707  group "delete";
708  let fn = declare_function "X2" ty m in
709  let bb = append_block context "Bb2" fn in
710  delete_block bb;
711  
712  group "insert";
713  let fn = declare_function "X3" ty m in
714  let bbb = append_block context "b" fn in
715  let bba = insert_block context "a" bbb in
716  insist ([| bba; bbb |] = basic_blocks fn);
717  ignore (build_unreachable (builder_at_end context bba));
718  ignore (build_unreachable (builder_at_end context bbb));
719  
720  (* RUN: grep Bb3 < %t.ll
721   *)
722  group "name/value";
723  let fn = define_function "X4" ty m in
724  let bb = entry_block fn in
725  ignore (build_unreachable (builder_at_end context bb));
726  let bbv = value_of_block bb in
727  set_value_name "Bb3" bbv;
728  insist ("Bb3" = value_name bbv);
729  
730  group "casts";
731  let fn = define_function "X5" ty m in
732  let bb = entry_block fn in
733  ignore (build_unreachable (builder_at_end context bb));
734  insist (bb = block_of_value (value_of_block bb));
735  insist (value_is_block (value_of_block bb));
736  insist (not (value_is_block (const_null i32_type)));
737  
738  begin group "iteration";
739    let m = create_module context "temp" in
740    let f = declare_function "Temp" (function_type i32_type [| |]) m in
741    
742    insist (At_end f = block_begin f);
743    insist (At_start f = block_end f);
744    
745    let b1 = append_block context "One" f in
746    let b2 = append_block context "Two" f in
747    
748    insist (Before b1 = block_begin f);
749    insist (Before b2 = block_succ b1);
750    insist (At_end f = block_succ b2);
751    
752    insist (After b2 = block_end f);
753    insist (After b1 = block_pred b2);
754    insist (At_start f = block_pred b1);
755    
756    let lf s x = s ^ "->" ^ value_name (value_of_block x) in
757    insist ("->One->Two" = fold_left_blocks lf "" f);
758    
759    let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
760    insist ("One<-Two<-" = fold_right_blocks rf f "");
761    
762    dispose_module m
763  end
764
765
766(*===-- Instructions ------------------------------------------------------===*)
767
768let test_instructions () =
769  begin group "iteration";
770    let m = create_module context "temp" in
771    let fty = function_type void_type [| i32_type; i32_type |] in
772    let f = define_function "f" fty m in
773    let bb = entry_block f in
774    let b = builder_at context (At_end bb) in
775    
776    insist (At_end bb = instr_begin bb);
777    insist (At_start bb = instr_end bb);
778    
779    let i1 = build_add (param f 0) (param f 1) "One" b in
780    let i2 = build_sub (param f 0) (param f 1) "Two" b in
781    
782    insist (Before i1 = instr_begin bb);
783    insist (Before i2 = instr_succ i1);
784    insist (At_end bb = instr_succ i2);
785    
786    insist (After i2 = instr_end bb);
787    insist (After i1 = instr_pred i2);
788    insist (At_start bb = instr_pred i1);
789    
790    let lf s x = s ^ "->" ^ value_name x in
791    insist ("->One->Two" = fold_left_instrs lf "" bb);
792    
793    let rf x s = value_name x ^ "<-" ^ s in
794    insist ("One<-Two<-" = fold_right_instrs rf bb "");
795    
796    dispose_module m
797  end
798
799
800(*===-- Builder -----------------------------------------------------------===*)
801
802let test_builder () =
803  let (++) x f = f x; x in
804  
805  begin group "parent";
806    insist (try
807              ignore (insertion_block (builder context));
808              false
809            with Not_found ->
810              true);
811    
812    let fty = function_type void_type [| i32_type |] in
813    let fn = define_function "BuilderParent" fty m in
814    let bb = entry_block fn in
815    let b = builder_at_end context bb in
816    let p = param fn 0 in
817    let sum = build_add p p "sum" b in
818    ignore (build_ret_void b);
819    
820    insist (fn = block_parent bb);
821    insist (fn = param_parent p);
822    insist (bb = instr_parent sum);
823    insist (bb = insertion_block b)
824  end;
825  
826  group "ret void";
827  begin
828    (* RUN: grep "ret void" < %t.ll
829     *)
830    let fty = function_type void_type [| |] in
831    let fn = declare_function "X6" fty m in
832    let b = builder_at_end context (append_block context "Bb01" fn) in
833    ignore (build_ret_void b)
834  end;
835
836  group "ret aggregate";
837  begin
838      (* RUN: grep "ret { i8, i64 } { i8 4, i64 5 }" < %t.ll
839       *)
840      let sty = struct_type context [| i8_type; i64_type |] in
841      let fty = function_type sty [| |] in
842      let fn = declare_function "XA6" fty m in
843      let b = builder_at_end context (append_block context "Bb01" fn) in
844      let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
845      ignore (build_aggregate_ret agg b)
846  end;
847  
848  (* The rest of the tests will use one big function. *)
849  let fty = function_type i32_type [| i32_type; i32_type |] in
850  let fn = define_function "X7" fty m in
851  let atentry = builder_at_end context (entry_block fn) in
852  let p1 = param fn 0 ++ set_value_name "P1" in
853  let p2 = param fn 1 ++ set_value_name "P2" in
854  let f1 = build_uitofp p1 float_type "F1" atentry in
855  let f2 = build_uitofp p2 float_type "F2" atentry in
856  
857  let bb00 = append_block context "Bb00" fn in
858  ignore (build_unreachable (builder_at_end context bb00));
859
860  group "function attribute";
861  begin
862      ignore (add_function_attr fn Attribute.UWTable);
863      (* RUN: grep "X7.*uwtable" < %t.ll
864       *)
865      insist ([Attribute.UWTable] = function_attr fn);
866  end;
867
868  (* see test/Feature/exception.ll *)
869  let bblpad = append_block context "Bblpad" fn in
870  let rt = struct_type context [| pointer_type i8_type; i32_type |] in
871  let ft = var_arg_function_type i32_type  [||] in
872  let personality = declare_function "__gxx_personality_v0" ft m in
873  let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
874  let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
875  let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
876  begin
877      set_global_constant true ztic;
878      set_global_constant true ztid;
879      set_global_constant true ztipkc;
880      let lp = build_landingpad rt personality 0 "lpad"
881       (builder_at_end context bblpad) in begin
882           set_cleanup lp true;
883           add_clause lp ztic;
884           insist((pointer_type (pointer_type i8_type)) = type_of ztid);
885           let ety = pointer_type (pointer_type i8_type) in
886           add_clause lp (const_array ety [| ztipkc; ztid |]);
887           ignore (build_resume lp (builder_at_end context bblpad));
888      end;
889      (* RUN: grep "landingpad.*personality.*__gxx_personality_v0" < %t.ll
890       * RUN: grep "cleanup" < %t.ll
891       * RUN: grep "catch.*i8\*\*.*@_ZTIc" < %t.ll
892       * RUN: grep "filter.*@_ZTIPKc.*@_ZTId" < %t.ll
893       * RUN: grep "resume " < %t.ll
894       * *)
895  end;
896
897  group "ret"; begin
898    (* RUN: grep "ret.*P1" < %t.ll
899     *)
900    let ret = build_ret p1 atentry in
901    position_before ret atentry
902  end;
903  
904  group "br"; begin
905    (* RUN: grep "br.*Bb02" < %t.ll
906     *)
907    let bb02 = append_block context "Bb02" fn in
908    let b = builder_at_end context bb02 in
909    ignore (build_br bb02 b)
910  end;
911  
912  group "cond_br"; begin
913    (* RUN: grep "br.*build_br.*Bb03.*Bb00" < %t.ll
914     *)
915    let bb03 = append_block context "Bb03" fn in
916    let b = builder_at_end context bb03 in
917    let cond = build_trunc p1 i1_type "build_br" b in
918    ignore (build_cond_br cond bb03 bb00 b)
919  end;
920  
921  group "switch"; begin
922    (* RUN: grep "switch.*P1.*SwiBlock3" < %t.ll
923     * RUN: grep "2,.*SwiBlock2" < %t.ll
924     *)
925    let bb1 = append_block context "SwiBlock1" fn in
926    let bb2 = append_block context "SwiBlock2" fn in
927    ignore (build_unreachable (builder_at_end context bb2));
928    let bb3 = append_block context "SwiBlock3" fn in
929    ignore (build_unreachable (builder_at_end context bb3));
930    let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
931        ignore (add_case si (const_int i32_type 2) bb2);
932        insist (switch_default_dest si = bb3);
933    end;
934  end;
935
936  group "malloc/free"; begin
937      (* RUN: grep "call.*@malloc(i32 ptrtoint" < %t.ll
938       * RUN: grep "call.*@free(i8\*" < %t.ll
939       * RUN: grep "call.*@malloc(i32 %" < %t.ll
940       *)
941      let bb1 = append_block context "MallocBlock1" fn in
942      let m1 = (build_malloc (pointer_type i32_type) "m1"
943      (builder_at_end context bb1)) in
944      ignore (build_free m1 (builder_at_end context bb1));
945      ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
946      ignore (build_unreachable (builder_at_end context bb1));
947  end;
948
949  group "indirectbr"; begin
950    (* RUN: grep "indirectbr i8\* blockaddress(@X7, %IBRBlock2), \[label %IBRBlock2, label %IBRBlock3\]" < %t.ll
951     *)
952    let bb1 = append_block context "IBRBlock1" fn in
953
954    let bb2 = append_block context "IBRBlock2" fn in
955    ignore (build_unreachable (builder_at_end context bb2));
956
957    let bb3 = append_block context "IBRBlock3" fn in
958    ignore (build_unreachable (builder_at_end context bb3));
959
960    let addr = block_address fn bb2 in
961    let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
962    ignore (add_destination ibr bb2);
963    ignore (add_destination ibr bb3)
964  end;
965  
966  group "invoke"; begin
967    (* RUN: grep "build_invoke.*invoke.*P1.*P2" < %t.ll
968     * RUN: grep "to.*Bb04.*unwind.*Bblpad" < %t.ll
969     *)
970    let bb04 = append_block context "Bb04" fn in
971    let b = builder_at_end context bb04 in
972    ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
973  end;
974  
975  group "unreachable"; begin
976    (* RUN: grep "unreachable" < %t.ll
977     *)
978    let bb06 = append_block context "Bb06" fn in
979    let b = builder_at_end context bb06 in
980    ignore (build_unreachable b)
981  end;
982  
983  group "arithmetic"; begin
984    let bb07 = append_block context "Bb07" fn in
985    let b = builder_at_end context bb07 in
986    
987    (* RUN: grep "%build_add = add i32 %P1, %P2" < %t.ll
988     * RUN: grep "%build_nsw_add = add nsw i32 %P1, %P2" < %t.ll
989     * RUN: grep "%build_nuw_add = add nuw i32 %P1, %P2" < %t.ll
990     * RUN: grep "%build_fadd = fadd float %F1, %F2" < %t.ll
991     * RUN: grep "%build_sub = sub i32 %P1, %P2" < %t.ll
992     * RUN: grep "%build_nsw_sub = sub nsw i32 %P1, %P2" < %t.ll
993     * RUN: grep "%build_nuw_sub = sub nuw i32 %P1, %P2" < %t.ll
994     * RUN: grep "%build_fsub = fsub float %F1, %F2" < %t.ll
995     * RUN: grep "%build_mul = mul i32 %P1, %P2" < %t.ll
996     * RUN: grep "%build_nsw_mul = mul nsw i32 %P1, %P2" < %t.ll
997     * RUN: grep "%build_nuw_mul = mul nuw i32 %P1, %P2" < %t.ll
998     * RUN: grep "%build_fmul = fmul float %F1, %F2" < %t.ll
999     * RUN: grep "%build_udiv = udiv i32 %P1, %P2" < %t.ll
1000     * RUN: grep "%build_sdiv = sdiv i32 %P1, %P2" < %t.ll
1001     * RUN: grep "%build_exact_sdiv = sdiv exact i32 %P1, %P2" < %t.ll
1002     * RUN: grep "%build_fdiv = fdiv float %F1, %F2" < %t.ll
1003     * RUN: grep "%build_urem = urem i32 %P1, %P2" < %t.ll
1004     * RUN: grep "%build_srem = srem i32 %P1, %P2" < %t.ll
1005     * RUN: grep "%build_frem = frem float %F1, %F2" < %t.ll
1006     * RUN: grep "%build_shl = shl i32 %P1, %P2" < %t.ll
1007     * RUN: grep "%build_lshl = lshr i32 %P1, %P2" < %t.ll
1008     * RUN: grep "%build_ashl = ashr i32 %P1, %P2" < %t.ll
1009     * RUN: grep "%build_and = and i32 %P1, %P2" < %t.ll
1010     * RUN: grep "%build_or = or i32 %P1, %P2" < %t.ll
1011     * RUN: grep "%build_xor = xor i32 %P1, %P2" < %t.ll
1012     * RUN: grep "%build_neg = sub i32 0, %P1" < %t.ll
1013     * RUN: grep "%build_nsw_neg = sub nsw i32 0, %P1" < %t.ll
1014     * RUN: grep "%build_nuw_neg = sub nuw i32 0, %P1" < %t.ll
1015     * RUN: grep "%build_fneg = fsub float .*0.*, %F1" < %t.ll
1016     * RUN: grep "%build_not = xor i32 %P1, -1" < %t.ll
1017     *)
1018    ignore (build_add p1 p2 "build_add" b);
1019    ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1020    ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1021    ignore (build_fadd f1 f2 "build_fadd" b);
1022    ignore (build_sub p1 p2 "build_sub" b);
1023    ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1024    ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1025    ignore (build_fsub f1 f2 "build_fsub" b);
1026    ignore (build_mul p1 p2 "build_mul" b);
1027    ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1028    ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1029    ignore (build_fmul f1 f2 "build_fmul" b);
1030    ignore (build_udiv p1 p2 "build_udiv" b);
1031    ignore (build_sdiv p1 p2 "build_sdiv" b);
1032    ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1033    ignore (build_fdiv f1 f2 "build_fdiv" b);
1034    ignore (build_urem p1 p2 "build_urem" b);
1035    ignore (build_srem p1 p2 "build_srem" b);
1036    ignore (build_frem f1 f2 "build_frem" b);
1037    ignore (build_shl p1 p2 "build_shl" b);
1038    ignore (build_lshr p1 p2 "build_lshl" b);
1039    ignore (build_ashr p1 p2 "build_ashl" b);
1040    ignore (build_and p1 p2 "build_and" b);
1041    ignore (build_or p1 p2 "build_or" b);
1042    ignore (build_xor p1 p2 "build_xor" b);
1043    ignore (build_neg p1 "build_neg" b);
1044    ignore (build_nsw_neg p1 "build_nsw_neg" b);
1045    ignore (build_nuw_neg p1 "build_nuw_neg" b);
1046    ignore (build_fneg f1 "build_fneg" b);
1047    ignore (build_not p1 "build_not" b);
1048    ignore (build_unreachable b)
1049  end;
1050  
1051  group "memory"; begin
1052    let bb08 = append_block context "Bb08" fn in
1053    let b = builder_at_end context bb08 in
1054
1055    (* RUN: grep "%build_alloca = alloca i32" < %t.ll
1056     * RUN: grep "%build_array_alloca = alloca i32, i32 %P2" < %t.ll
1057     * RUN: grep "%build_load = load i32\* %build_array_alloca" < %t.ll
1058     * RUN: grep "store i32 %P2, i32\* %build_alloca" < %t.ll
1059     * RUN: grep "%build_gep = getelementptr i32\* %build_array_alloca, i32 %P2" < %t.ll
1060     * RUN: grep "%build_in_bounds_gep = getelementptr inbounds i32\* %build_array_alloca, i32 %P2" < %t.ll
1061     * RUN: grep "%build_struct_gep = getelementptr inbounds.*%build_alloca2, i32 0, i32 1" < %t.ll
1062     *)
1063    let alloca = build_alloca i32_type "build_alloca" b in
1064    let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1065    ignore(build_load array_alloca "build_load" b);
1066    ignore(build_store p2 alloca b);
1067    ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1068    ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1069
1070    let sty = struct_type context [| i32_type; i8_type |] in
1071    let alloca2 = build_alloca sty "build_alloca2" b in
1072    ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1073
1074    ignore(build_unreachable b)
1075  end;
1076
1077  group "string"; begin
1078    let bb09 = append_block context "Bb09" fn in
1079    let b = builder_at_end context bb09 in
1080    let p = build_alloca (pointer_type i8_type) "p" b in
1081    (* RUN: grep "build_global_string.*stringval" < %t.ll
1082     * RUN: grep "store.*build_global_string1.*p" < %t.ll
1083     * *)
1084    ignore (build_global_string "stringval" "build_global_string" b);
1085    let g = build_global_stringptr "stringval" "build_global_string1" b in
1086    ignore (build_store g p b);
1087    ignore(build_unreachable b);
1088  end;
1089
1090  group "casts"; begin
1091    let void_ptr = pointer_type i8_type in
1092    
1093    (* RUN: grep "%build_trunc = trunc i32 %P1 to i8" < %t.ll
1094     * RUN: grep "%build_trunc2 = trunc i32 %P1 to i8" < %t.ll
1095     * RUN: grep "%build_trunc3 = trunc i32 %P1 to i8" < %t.ll
1096     * RUN: grep "%build_zext = zext i8 %build_trunc to i32" < %t.ll
1097     * RUN: grep "%build_zext2 = zext i8 %build_trunc to i32" < %t.ll
1098     * RUN: grep "%build_sext = sext i32 %build_zext to i64" < %t.ll
1099     * RUN: grep "%build_sext2 = sext i32 %build_zext to i64" < %t.ll
1100     * RUN: grep "%build_sext3 = sext i32 %build_zext to i64" < %t.ll
1101     * RUN: grep "%build_uitofp = uitofp i64 %build_sext to float" < %t.ll
1102     * RUN: grep "%build_sitofp = sitofp i32 %build_zext to double" < %t.ll
1103     * RUN: grep "%build_fptoui = fptoui float %build_uitofp to i32" < %t.ll
1104     * RUN: grep "%build_fptosi = fptosi double %build_sitofp to i64" < %t.ll
1105     * RUN: grep "%build_fptrunc = fptrunc double %build_sitofp to float" < %t.ll
1106     * RUN: grep "%build_fptrunc2 = fptrunc double %build_sitofp to float" < %t.ll
1107     * RUN: grep "%build_fpext = fpext float %build_fptrunc to double" < %t.ll
1108     * RUN: grep "%build_fpext2 = fpext float %build_fptrunc to double" < %t.ll
1109     * RUN: grep "%build_inttoptr = inttoptr i32 %P1 to i8\*" < %t.ll
1110     * RUN: grep "%build_ptrtoint = ptrtoint i8\* %build_inttoptr to i64" < %t.ll
1111     * RUN: grep "%build_ptrtoint2 = ptrtoint i8\* %build_inttoptr to i64" < %t.ll
1112     * RUN: grep "%build_bitcast = bitcast i64 %build_ptrtoint to double" < %t.ll
1113     * RUN: grep "%build_bitcast2 = bitcast i64 %build_ptrtoint to double" < %t.ll
1114     * RUN: grep "%build_bitcast3 = bitcast i64 %build_ptrtoint to double" < %t.ll
1115     * RUN: grep "%build_bitcast4 = bitcast i64 %build_ptrtoint to double" < %t.ll
1116     * RUN: grep "%build_pointercast = bitcast i8\* %build_inttoptr to i16*" < %t.ll
1117     *)
1118    let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
1119    let inst29 = build_zext inst28 i32_type "build_zext" atentry in
1120    let inst30 = build_sext inst29 i64_type "build_sext" atentry in
1121    let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1122    let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1123    ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1124    ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1125    let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1126    ignore(build_fpext inst35 double_type "build_fpext" atentry);
1127    let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1128    let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1129    ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
1130    ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
1131    ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
1132    ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
1133    ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
1134
1135    ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
1136    ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
1137    ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
1138    ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
1139    ignore(build_intcast inst29 i64_type "build_sext3" atentry);
1140    ignore(build_intcast p1 i8_type "build_trunc3" atentry);
1141    ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
1142    ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
1143  end;
1144  
1145  group "comparisons"; begin
1146    (* RUN: grep "%build_icmp_ne = icmp ne i32 %P1, %P2" < %t.ll
1147     * RUN: grep "%build_icmp_sle = icmp sle i32 %P2, %P1" < %t.ll
1148     * RUN: grep "%build_fcmp_false = fcmp false float %F1, %F2" < %t.ll
1149     * RUN: grep "%build_fcmp_true = fcmp true float %F2, %F1" < %t.ll
1150     * RUN: grep "%build_is_null.*= icmp eq.*%X0,.*null" < %t.ll
1151     * RUN: grep "%build_is_not_null = icmp ne i8\* %X1, null" < %t.ll
1152     * RUN: grep "%build_ptrdiff" < %t.ll
1153     *)
1154    ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
1155    ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
1156    ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
1157    ignore (build_fcmp Fcmp.True  f2 f1 "build_fcmp_true" atentry);
1158    let g0 = declare_global (pointer_type i8_type) "g0" m in
1159    let g1 = declare_global (pointer_type i8_type) "g1" m in
1160    let p0 = build_load g0 "X0" atentry in
1161    let p1 = build_load g1 "X1" atentry in
1162    ignore (build_is_null p0 "build_is_null" atentry);
1163    ignore (build_is_not_null p1 "build_is_not_null" atentry);
1164    ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
1165  end;
1166  
1167  group "miscellaneous"; begin
1168    (* RUN: grep "%build_call = tail call cc63 i32 @.*(i32 signext %P2, i32 %P1)" < %t.ll
1169     * RUN: grep "%build_select = select i1 %build_icmp, i32 %P1, i32 %P2" < %t.ll
1170     * RUN: grep "%build_va_arg = va_arg i8\*\* null, i32" < %t.ll
1171     * RUN: grep "%build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2" < %t.ll
1172     * RUN: grep "%build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2" < %t.ll
1173     * RUN: grep "%build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>" < %t.ll
1174     * RUN: grep "%build_insertvalue0 = insertvalue.*%bl, i32 1, 0" < %t.ll
1175     * RUN: grep "%build_extractvalue = extractvalue.*%build_insertvalue1, 1" < %t.ll
1176     *)
1177    let ci = build_call fn [| p2; p1 |] "build_call" atentry in
1178    insist (CallConv.c = instruction_call_conv ci);
1179    set_instruction_call_conv 63 ci;
1180    insist (63 = instruction_call_conv ci);
1181    insist (not (is_tail_call ci));
1182    set_tail_call true ci;
1183    insist (is_tail_call ci);
1184    add_instruction_param_attr ci 1 Attribute.Sext;
1185    add_instruction_param_attr ci 2 Attribute.Noalias;
1186    remove_instruction_param_attr ci 2 Attribute.Noalias;
1187    
1188    let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1189    ignore (build_select inst46 p1 p2 "build_select" atentry);
1190    ignore (build_va_arg
1191      (const_null (pointer_type (pointer_type i8_type)))
1192      i32_type "build_va_arg" atentry);
1193    
1194    (* Set up some vector vregs. *)
1195    let one  = const_int i32_type 1 in
1196    let zero = const_int i32_type 0 in
1197    let t1 = const_vector [| one; zero; one; zero |] in
1198    let t2 = const_vector [| zero; one; zero; one |] in
1199    let t3 = const_vector [| one; one; zero; zero |] in
1200    let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1201    let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1202    let sty = struct_type context [| i32_type; i8_type |] in
1203    
1204    ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1205    ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1206    ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1207
1208    let p = build_alloca sty "ba" atentry in
1209    let agg = build_load p "bl" atentry in
1210    let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1211                 "build_insertvalue0" atentry in
1212    let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1213                 "build_insertvalue1" atentry in
1214    ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1215  end;
1216
1217  group "metadata"; begin
1218    (* RUN: grep '%metadata = add i32 %P1, %P2, !test !0' < %t.ll
1219     * RUN: grep '!0 = metadata !{i32 1, metadata !"metadata test"}' < %t.ll
1220     *)
1221    let i = build_add p1 p2 "metadata" atentry in
1222    insist ((has_metadata i) = false);
1223
1224    let m1 = const_int i32_type 1 in
1225    let m2 = mdstring context "metadata test" in
1226    let md = mdnode context [| m1; m2 |] in
1227
1228    let kind = mdkind_id context "test" in
1229    set_metadata i kind md;
1230
1231    insist ((has_metadata i) = true);
1232    insist ((metadata i kind) = Some md);
1233
1234    clear_metadata i kind;
1235
1236    insist ((has_metadata i) = false);
1237    insist ((metadata i kind) = None);
1238
1239    set_metadata i kind md
1240  end;
1241
1242  group "dbg"; begin
1243    (* RUN: grep '%dbg = add i32 %P1, %P2, !dbg !1' < %t.ll
1244     * RUN: grep '!1 = metadata !{i32 2, i32 3, metadata !2, metadata !2}' < %t.ll
1245     *)
1246    insist ((current_debug_location atentry) = None);
1247
1248    let m_line = const_int i32_type 2 in
1249    let m_col = const_int i32_type 3 in
1250    let m_scope = mdnode context [| |] in
1251    let m_inlined = mdnode context [| |] in
1252    let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1253    set_current_debug_location atentry md;
1254
1255    insist ((current_debug_location atentry) = Some md);
1256
1257    let i = build_add p1 p2 "dbg" atentry in
1258    insist ((has_metadata i) = true);
1259
1260    clear_current_debug_location atentry
1261  end;
1262  
1263  group "phi"; begin
1264    (* RUN: grep "PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2" < %t.ll
1265     *)
1266    let b1 = append_block context "PhiBlock1" fn in
1267    let b2 = append_block context "PhiBlock2" fn in
1268    
1269    let jb = append_block context "PhiJoinBlock" fn in
1270    ignore (build_br jb (builder_at_end context b1));
1271    ignore (build_br jb (builder_at_end context b2));
1272    let at_jb = builder_at_end context jb in
1273    
1274    let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1275    insist ([(p1, b1)] = incoming phi);
1276    
1277    add_incoming (p2, b2) phi;
1278    insist ([(p1, b1); (p2, b2)] = incoming phi);
1279    
1280    ignore (build_unreachable at_jb);
1281  end
1282
1283
1284(*===-- Pass Managers -----------------------------------------------------===*)
1285
1286let test_pass_manager () =
1287  let (++) x f = ignore (f x); x in
1288
1289  begin group "module pass manager";
1290    ignore (PassManager.create ()
1291             ++ PassManager.run_module m
1292             ++ PassManager.dispose)
1293  end;
1294  
1295  begin group "function pass manager";
1296    let fty = function_type void_type [| |] in
1297    let fn = define_function "FunctionPassManager" fty m in
1298    ignore (build_ret_void (builder_at_end context (entry_block fn)));
1299    
1300    ignore (PassManager.create_function m
1301             ++ PassManager.initialize
1302             ++ PassManager.run_function fn
1303             ++ PassManager.finalize
1304             ++ PassManager.dispose)
1305  end
1306
1307
1308(*===-- Writer ------------------------------------------------------------===*)
1309
1310let test_writer () =
1311  group "valid";
1312  insist (match Llvm_analysis.verify_module m with
1313          | None -> true
1314          | Some msg -> prerr_string msg; false);
1315
1316  group "writer";
1317  insist (write_bitcode_file m filename);
1318  
1319  dispose_module m
1320
1321
1322(*===-- Driver ------------------------------------------------------------===*)
1323
1324let _ =
1325  suite "target"           test_target;
1326  suite "constants"        test_constants;
1327  suite "global values"    test_global_values;
1328  suite "global variables" test_global_variables;
1329  suite "uses"             test_uses;
1330  suite "users"            test_users;
1331  suite "aliases"          test_aliases;
1332  suite "functions"        test_functions;
1333  suite "params"           test_params;
1334  suite "basic blocks"     test_basic_blocks;
1335  suite "instructions"     test_instructions;
1336  suite "builder"          test_builder;
1337  suite "pass manager"     test_pass_manager;
1338  suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1339  exit !exit_status
1340