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