vmcore.ml revision f00a9e0f79541538df7e3af2e83de37f40d904cc
13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)(* RUN: rm -rf %t.builddir
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: mkdir -p %t.builddir
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: cp %s %t.builddir
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %t.builddir/vmcore.ml -o %t
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: %t %t.bc
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: llvm-dis < %t.bc > %t.ll
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: FileCheck %s < %t.ll
83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * Do a second pass for things that shouldn't be anywhere.
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * XFAIL: vg_leak
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) *)
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)(* Note: It takes several seconds for ocamlopt to link an executable with
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)         libLLVMCore.a, so it's better to write a big test than a bunch of
153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)         little ones. *)
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)open Llvm
183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)open Llvm_bitwriter
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)(* Tiny unit test framework - really just to help find which line is busted *)
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let exit_status = ref 0
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let suite_name = ref ""
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let group_name = ref ""
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let case_num = ref 0
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let print_checkpoints = false
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let context = global_context ()
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let i1_type = Llvm.i1_type context
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)let 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-NOWHERE-NOT: GVar05
456   *)
457  group "delete";
458  let g = define_global "GVar05" fourty_two32 m in
459  delete_global g;
460
461  (* CHECK: ConstGlobalVar{{.*}}constant
462   *)
463  group "constant";
464  let g = define_global "ConstGlobalVar" fourty_two32 m in
465  insist (not (is_global_constant g));
466  set_global_constant true g;
467  insist (is_global_constant g);
468  
469  begin group "iteration";
470    let m = create_module context "temp" in
471    
472    insist (At_end m = global_begin m);
473    insist (At_start m = global_end m);
474    
475    let g1 = declare_global i32_type "One" m in
476    let g2 = declare_global i32_type "Two" m in
477    
478    insist (Before g1 = global_begin m);
479    insist (Before g2 = global_succ g1);
480    insist (At_end m = global_succ g2);
481    
482    insist (After g2 = global_end m);
483    insist (After g1 = global_pred g2);
484    insist (At_start m = global_pred g1);
485    
486    let lf s x = s ^ "->" ^ value_name x in
487    insist ("->One->Two" = fold_left_globals lf "" m);
488    
489    let rf x s = value_name x ^ "<-" ^ s in
490    insist ("One<-Two<-" = fold_right_globals rf m "");
491    
492    dispose_module m
493  end
494
495(* String globals built below are emitted here.
496 * CHECK: build_global_string{{.*}}stringval
497 *)
498
499
500(*===-- Uses --------------------------------------------------------------===*)
501
502let test_uses () =
503  let ty = function_type i32_type [| i32_type; i32_type |] in
504  let fn = define_function "use_function" ty m in
505  let b = builder_at_end context (entry_block fn) in
506
507  let p1 = param fn 0 in
508  let p2 = param fn 1 in
509  let v1 = build_add p1 p2 "v1" b in
510  let v2 = build_add p1 v1 "v2" b in
511  let _ = build_add v1 v2 "v3" b in
512
513  let lf s u = value_name (user u) ^ "->" ^ s in
514  insist ("v2->v3->" = fold_left_uses lf "" v1);
515  let rf u s = value_name (user u) ^ "<-" ^ s in
516  insist ("v3<-v2<-" = fold_right_uses rf v1 "");
517
518  let lf s u = value_name (used_value u) ^ "->" ^ s in
519  insist ("v1->v1->" = fold_left_uses lf "" v1);
520
521  let rf u s = value_name (used_value u) ^ "<-" ^ s in
522  insist ("v1<-v1<-" = fold_right_uses rf v1 "");
523
524  ignore (build_unreachable b)
525
526
527(*===-- Users -------------------------------------------------------------===*)
528
529let test_users () =
530  let ty = function_type i32_type [| i32_type; i32_type |] in
531  let fn = define_function "user_function" ty m in
532  let b = builder_at_end context (entry_block fn) in
533
534  let p1 = param fn 0 in
535  let p2 = param fn 1 in
536  let a3 = build_alloca i32_type "user_alloca" b in
537  let p3 = build_load a3 "user_load" b in
538  let i = build_add p1 p2 "sum" b in
539
540  insist ((num_operands i) = 2);
541  insist ((operand i 0) = p1);
542  insist ((operand i 1) = p2);
543
544  set_operand i 1 p3;
545  insist ((operand i 1) != p2);
546  insist ((operand i 1) = p3);
547
548  ignore (build_unreachable b)
549
550
551(*===-- Aliases -----------------------------------------------------------===*)
552
553let test_aliases () =
554  (* CHECK: @alias = alias i32* @aliasee
555   *)
556  let v = declare_global i32_type "aliasee" m in
557  ignore (add_alias m (pointer_type i32_type) v "alias")
558
559
560(*===-- Functions ---------------------------------------------------------===*)
561
562let test_functions () =
563  let ty = function_type i32_type [| i32_type; i64_type |] in
564  let ty2 = function_type i8_type [| i8_type; i64_type |] in
565  
566  (* CHECK: declare i32 @Fn1(i32, i64)
567   *)
568  begin group "declare";
569    insist (None = lookup_function "Fn1" m);
570    let fn = declare_function "Fn1" ty m in
571    insist (pointer_type ty = type_of fn);
572    insist (is_declaration fn);
573    insist (0 = Array.length (basic_blocks fn));
574    insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
575    insist (fn == declare_function "Fn1" ty m);
576    insist (None <> lookup_function "Fn1" m);
577    insist (match lookup_function "Fn1" m with Some x -> x = fn
578                                             | None -> false);
579    insist (m == global_parent fn)
580  end;
581  
582  (* CHECK-NOWHERE-NOT: Fn2
583   *)
584  group "delete";
585  let fn = declare_function "Fn2" ty m in
586  delete_function fn;
587  
588  (* CHECK: define{{.*}}Fn3
589   *)
590  group "define";
591  let fn = define_function "Fn3" ty m in
592  insist (not (is_declaration fn));
593  insist (1 = Array.length (basic_blocks fn));
594  ignore (build_unreachable (builder_at_end context (entry_block fn)));
595  
596  (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
597   *)
598  group "params";
599  let fn = define_function "Fn4" ty m in
600  let params = params fn in
601  insist (2 = Array.length params);
602  insist (params.(0) = param fn 0);
603  insist (params.(1) = param fn 1);
604  insist (i32_type = type_of params.(0));
605  insist (i64_type = type_of params.(1));
606  set_value_name "Param1" params.(0);
607  set_value_name "Param2" params.(1);
608  ignore (build_unreachable (builder_at_end context (entry_block fn)));
609  
610  (* CHECK: fastcc{{.*}}Fn5
611   *)
612  group "callconv";
613  let fn = define_function "Fn5" ty m in
614  insist (CallConv.c = function_call_conv fn);
615  set_function_call_conv CallConv.fast fn;
616  insist (CallConv.fast = function_call_conv fn);
617  ignore (build_unreachable (builder_at_end context (entry_block fn)));
618  
619  begin group "gc";
620    (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
621     *)
622    let fn = define_function "Fn6" ty m in
623    insist (None = gc fn);
624    set_gc (Some "ocaml") fn;
625    insist (Some "ocaml" = gc fn);
626    set_gc None fn;
627    insist (None = gc fn);
628    set_gc (Some "shadowstack") fn;
629    ignore (build_unreachable (builder_at_end context (entry_block fn)));
630  end;
631  
632  begin group "iteration";
633    let m = create_module context "temp" in
634    
635    insist (At_end m = function_begin m);
636    insist (At_start m = function_end m);
637    
638    let f1 = define_function "One" ty m in
639    let f2 = define_function "Two" ty m in
640    
641    insist (Before f1 = function_begin m);
642    insist (Before f2 = function_succ f1);
643    insist (At_end m = function_succ f2);
644    
645    insist (After f2 = function_end m);
646    insist (After f1 = function_pred f2);
647    insist (At_start m = function_pred f1);
648    
649    let lf s x = s ^ "->" ^ value_name x in
650    insist ("->One->Two" = fold_left_functions lf "" m);
651    
652    let rf x s = value_name x ^ "<-" ^ s in
653    insist ("One<-Two<-" = fold_right_functions rf m "");
654    
655    dispose_module m
656  end
657
658
659(*===-- Params ------------------------------------------------------------===*)
660
661let test_params () =
662  begin group "iteration";
663    let m = create_module context "temp" in
664    
665    let vf = define_function "void" (function_type void_type [| |]) m in
666    
667    insist (At_end vf = param_begin vf);
668    insist (At_start vf = param_end vf);
669    
670    let ty = function_type void_type [| i32_type; i32_type |] in
671    let f = define_function "f" ty m in
672    let p1 = param f 0 in
673    let p2 = param f 1 in
674    set_value_name "One" p1;
675    set_value_name "Two" p2;
676    add_param_attr p1 Attribute.Sext;
677    add_param_attr p2 Attribute.Noalias;
678    remove_param_attr p2 Attribute.Noalias;
679    add_function_attr f Attribute.Nounwind;
680    add_function_attr f Attribute.Noreturn;
681    remove_function_attr f Attribute.Noreturn;
682
683    insist (Before p1 = param_begin f);
684    insist (Before p2 = param_succ p1);
685    insist (At_end f = param_succ p2);
686    
687    insist (After p2 = param_end f);
688    insist (After p1 = param_pred p2);
689    insist (At_start f = param_pred p1);
690    
691    let lf s x = s ^ "->" ^ value_name x in
692    insist ("->One->Two" = fold_left_params lf "" f);
693    
694    let rf x s = value_name x ^ "<-" ^ s in
695    insist ("One<-Two<-" = fold_right_params rf f "");
696    
697    dispose_module m
698  end
699
700
701(*===-- Basic Blocks ------------------------------------------------------===*)
702
703let test_basic_blocks () =
704  let ty = function_type void_type [| |] in
705  
706  (* CHECK: Bb1
707   *)
708  group "entry";
709  let fn = declare_function "X" ty m in
710  let bb = append_block context "Bb1" fn in
711  insist (bb = entry_block fn);
712  ignore (build_unreachable (builder_at_end context bb));
713  
714  (* CHECK-NOWHERE-NOT: Bb2
715   *)
716  group "delete";
717  let fn = declare_function "X2" ty m in
718  let bb = append_block context "Bb2" fn in
719  delete_block bb;
720  
721  group "insert";
722  let fn = declare_function "X3" ty m in
723  let bbb = append_block context "b" fn in
724  let bba = insert_block context "a" bbb in
725  insist ([| bba; bbb |] = basic_blocks fn);
726  ignore (build_unreachable (builder_at_end context bba));
727  ignore (build_unreachable (builder_at_end context bbb));
728  
729  (* CHECK: Bb3
730   *)
731  group "name/value";
732  let fn = define_function "X4" ty m in
733  let bb = entry_block fn in
734  ignore (build_unreachable (builder_at_end context bb));
735  let bbv = value_of_block bb in
736  set_value_name "Bb3" bbv;
737  insist ("Bb3" = value_name bbv);
738  
739  group "casts";
740  let fn = define_function "X5" ty m in
741  let bb = entry_block fn in
742  ignore (build_unreachable (builder_at_end context bb));
743  insist (bb = block_of_value (value_of_block bb));
744  insist (value_is_block (value_of_block bb));
745  insist (not (value_is_block (const_null i32_type)));
746  
747  begin group "iteration";
748    let m = create_module context "temp" in
749    let f = declare_function "Temp" (function_type i32_type [| |]) m in
750    
751    insist (At_end f = block_begin f);
752    insist (At_start f = block_end f);
753    
754    let b1 = append_block context "One" f in
755    let b2 = append_block context "Two" f in
756    
757    insist (Before b1 = block_begin f);
758    insist (Before b2 = block_succ b1);
759    insist (At_end f = block_succ b2);
760    
761    insist (After b2 = block_end f);
762    insist (After b1 = block_pred b2);
763    insist (At_start f = block_pred b1);
764    
765    let lf s x = s ^ "->" ^ value_name (value_of_block x) in
766    insist ("->One->Two" = fold_left_blocks lf "" f);
767    
768    let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
769    insist ("One<-Two<-" = fold_right_blocks rf f "");
770    
771    dispose_module m
772  end
773
774
775(*===-- Instructions ------------------------------------------------------===*)
776
777let test_instructions () =
778  begin group "iteration";
779    let m = create_module context "temp" in
780    let fty = function_type void_type [| i32_type; i32_type |] in
781    let f = define_function "f" fty m in
782    let bb = entry_block f in
783    let b = builder_at context (At_end bb) in
784    
785    insist (At_end bb = instr_begin bb);
786    insist (At_start bb = instr_end bb);
787    
788    let i1 = build_add (param f 0) (param f 1) "One" b in
789    let i2 = build_sub (param f 0) (param f 1) "Two" b in
790    
791    insist (Before i1 = instr_begin bb);
792    insist (Before i2 = instr_succ i1);
793    insist (At_end bb = instr_succ i2);
794    
795    insist (After i2 = instr_end bb);
796    insist (After i1 = instr_pred i2);
797    insist (At_start bb = instr_pred i1);
798    
799    let lf s x = s ^ "->" ^ value_name x in
800    insist ("->One->Two" = fold_left_instrs lf "" bb);
801    
802    let rf x s = value_name x ^ "<-" ^ s in
803    insist ("One<-Two<-" = fold_right_instrs rf bb "");
804    
805    dispose_module m
806  end
807
808
809(*===-- Builder -----------------------------------------------------------===*)
810
811let test_builder () =
812  let (++) x f = f x; x in
813  
814  begin group "parent";
815    insist (try
816              ignore (insertion_block (builder context));
817              false
818            with Not_found ->
819              true);
820    
821    let fty = function_type void_type [| i32_type |] in
822    let fn = define_function "BuilderParent" fty m in
823    let bb = entry_block fn in
824    let b = builder_at_end context bb in
825    let p = param fn 0 in
826    let sum = build_add p p "sum" b in
827    ignore (build_ret_void b);
828    
829    insist (fn = block_parent bb);
830    insist (fn = param_parent p);
831    insist (bb = instr_parent sum);
832    insist (bb = insertion_block b)
833  end;
834  
835  group "ret void";
836  begin
837    (* CHECK: ret void
838     *)
839    let fty = function_type void_type [| |] in
840    let fn = declare_function "X6" fty m in
841    let b = builder_at_end context (append_block context "Bb01" fn) in
842    ignore (build_ret_void b)
843  end;
844
845  group "ret aggregate";
846  begin
847      (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
848       *)
849      let sty = struct_type context [| i8_type; i64_type |] in
850      let fty = function_type sty [| |] in
851      let fn = declare_function "XA6" fty m in
852      let b = builder_at_end context (append_block context "Bb01" fn) in
853      let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
854      ignore (build_aggregate_ret agg b)
855  end;
856  
857  (* The rest of the tests will use one big function. *)
858  let fty = function_type i32_type [| i32_type; i32_type |] in
859  let fn = define_function "X7" fty m in
860  let atentry = builder_at_end context (entry_block fn) in
861  let p1 = param fn 0 ++ set_value_name "P1" in
862  let p2 = param fn 1 ++ set_value_name "P2" in
863  let f1 = build_uitofp p1 float_type "F1" atentry in
864  let f2 = build_uitofp p2 float_type "F2" atentry in
865  
866  let bb00 = append_block context "Bb00" fn in
867  ignore (build_unreachable (builder_at_end context bb00));
868
869  group "function attribute";
870  begin
871      ignore (add_function_attr fn Attribute.UWTable);
872      (* CHECK: X7{{.*}}#0
873       * #0 is uwtable, defined at EOF.
874       *)
875      insist ([Attribute.UWTable] = function_attr fn);
876  end;
877
878  group "casts"; begin
879    let void_ptr = pointer_type i8_type in
880
881    (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
882     * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
883     * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
884     * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
885     * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
886     * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
887     * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
888     * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
889     * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
890     * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
891     * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
892     * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
893     * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
894     * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
895     * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
896     * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
897     * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8*
898     * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64
899     * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64
900     * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
901     * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
902     * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
903     * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
904     * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16*
905     *)
906    let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
907    let inst29 = build_zext inst28 i32_type "build_zext" atentry in
908    let inst30 = build_sext inst29 i64_type "build_sext" atentry in
909    let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
910    let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
911    ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
912    ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
913    let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
914    ignore(build_fpext inst35 double_type "build_fpext" atentry);
915    let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
916    let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
917    ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
918    ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
919    ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
920    ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
921    ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry);
922
923    ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
924    ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
925    ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
926    ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
927    ignore(build_intcast inst29 i64_type "build_sext3" atentry);
928    ignore(build_intcast p1 i8_type "build_trunc3" atentry);
929    ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
930    ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
931  end;
932
933  group "comparisons"; begin
934    (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
935     * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
936     * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
937     * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
938     * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
939     * CHECK: %build_is_not_null = icmp ne i8* %X1, null
940     * CHECK: %build_ptrdiff
941     *)
942    ignore (build_icmp Icmp.Ne    p1 p2 "build_icmp_ne" atentry);
943    ignore (build_icmp Icmp.Sle   p2 p1 "build_icmp_sle" atentry);
944    ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry);
945    ignore (build_fcmp Fcmp.True  f2 f1 "build_fcmp_true" atentry);
946    let g0 = declare_global (pointer_type i8_type) "g0" m in
947    let g1 = declare_global (pointer_type i8_type) "g1" m in
948    let p0 = build_load g0 "X0" atentry in
949    let p1 = build_load g1 "X1" atentry in
950    ignore (build_is_null p0 "build_is_null" atentry);
951    ignore (build_is_not_null p1 "build_is_not_null" atentry);
952    ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry);
953  end;
954
955  group "miscellaneous"; begin
956    (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1)
957     * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
958     * CHECK: %build_va_arg = va_arg i8** null, i32
959     * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
960     * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
961     * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
962     * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
963     * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
964     *)
965    let ci = build_call fn [| p2; p1 |] "build_call" atentry in
966    insist (CallConv.c = instruction_call_conv ci);
967    set_instruction_call_conv 63 ci;
968    insist (63 = instruction_call_conv ci);
969    insist (not (is_tail_call ci));
970    set_tail_call true ci;
971    insist (is_tail_call ci);
972    add_instruction_param_attr ci 1 Attribute.Sext;
973    add_instruction_param_attr ci 2 Attribute.Noalias;
974    remove_instruction_param_attr ci 2 Attribute.Noalias;
975
976    let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
977    ignore (build_select inst46 p1 p2 "build_select" atentry);
978    ignore (build_va_arg
979      (const_null (pointer_type (pointer_type i8_type)))
980      i32_type "build_va_arg" atentry);
981
982    (* Set up some vector vregs. *)
983    let one  = const_int i32_type 1 in
984    let zero = const_int i32_type 0 in
985    let t1 = const_vector [| one; zero; one; zero |] in
986    let t2 = const_vector [| zero; one; zero; one |] in
987    let t3 = const_vector [| one; one; zero; zero |] in
988    let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
989    let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
990    let sty = struct_type context [| i32_type; i8_type |] in
991
992    ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
993    ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
994    ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
995
996    let p = build_alloca sty "ba" atentry in
997    let agg = build_load p "bl" atentry in
998    let agg0 = build_insertvalue agg (const_int i32_type 1) 0
999                 "build_insertvalue0" atentry in
1000    let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1001                 "build_insertvalue1" atentry in
1002    ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1003  end;
1004
1005  group "metadata"; begin
1006    (* CHECK: %metadata = add i32 %P1, %P2, !test !0
1007     * !0 is metadata emitted at EOF.
1008     *)
1009    let i = build_add p1 p2 "metadata" atentry in
1010    insist ((has_metadata i) = false);
1011
1012    let m1 = const_int i32_type 1 in
1013    let m2 = mdstring context "metadata test" in
1014    let md = mdnode context [| m1; m2 |] in
1015
1016    let kind = mdkind_id context "test" in
1017    set_metadata i kind md;
1018
1019    insist ((has_metadata i) = true);
1020    insist ((metadata i kind) = Some md);
1021
1022    clear_metadata i kind;
1023
1024    insist ((has_metadata i) = false);
1025    insist ((metadata i kind) = None);
1026
1027    set_metadata i kind md
1028  end;
1029
1030  group "dbg"; begin
1031    (* CHECK: %dbg = add i32 %P1, %P2, !dbg !1
1032     * !1 is metadata emitted at EOF.
1033     *)
1034    insist ((current_debug_location atentry) = None);
1035
1036    let m_line = const_int i32_type 2 in
1037    let m_col = const_int i32_type 3 in
1038    let m_scope = mdnode context [| |] in
1039    let m_inlined = mdnode context [| |] in
1040    let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in
1041    set_current_debug_location atentry md;
1042
1043    insist ((current_debug_location atentry) = Some md);
1044
1045    let i = build_add p1 p2 "dbg" atentry in
1046    insist ((has_metadata i) = true);
1047
1048    clear_current_debug_location atentry
1049  end;
1050
1051  group "ret"; begin
1052    (* CHECK: ret{{.*}}P1
1053     *)
1054    let ret = build_ret p1 atentry in
1055    position_before ret atentry
1056  end;
1057
1058  (* see test/Feature/exception.ll *)
1059  let bblpad = append_block context "Bblpad" fn in
1060  let rt = struct_type context [| pointer_type i8_type; i32_type |] in
1061  let ft = var_arg_function_type i32_type  [||] in
1062  let personality = declare_function "__gxx_personality_v0" ft m in
1063  let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in
1064  let ztid = declare_global (pointer_type i8_type) "_ZTId" m in
1065  let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in
1066  begin
1067      set_global_constant true ztic;
1068      set_global_constant true ztid;
1069      set_global_constant true ztipkc;
1070      let lp = build_landingpad rt personality 0 "lpad"
1071       (builder_at_end context bblpad) in begin
1072           set_cleanup lp true;
1073           add_clause lp ztic;
1074           insist((pointer_type (pointer_type i8_type)) = type_of ztid);
1075           let ety = pointer_type (pointer_type i8_type) in
1076           add_clause lp (const_array ety [| ztipkc; ztid |]);
1077           ignore (build_resume lp (builder_at_end context bblpad));
1078      end;
1079      (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0
1080       * CHECK: cleanup
1081       * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc
1082       * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1083       * CHECK: resume
1084       * *)
1085  end;
1086
1087  group "br"; begin
1088    (* CHECK: br{{.*}}Bb02
1089     *)
1090    let bb02 = append_block context "Bb02" fn in
1091    let b = builder_at_end context bb02 in
1092    ignore (build_br bb02 b)
1093  end;
1094  
1095  group "cond_br"; begin
1096    (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1097     *)
1098    let bb03 = append_block context "Bb03" fn in
1099    let b = builder_at_end context bb03 in
1100    let cond = build_trunc p1 i1_type "build_br" b in
1101    ignore (build_cond_br cond bb03 bb00 b)
1102  end;
1103  
1104  group "switch"; begin
1105    (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1106     * CHECK: 2,{{.*}}SwiBlock2
1107     *)
1108    let bb1 = append_block context "SwiBlock1" fn in
1109    let bb2 = append_block context "SwiBlock2" fn in
1110    ignore (build_unreachable (builder_at_end context bb2));
1111    let bb3 = append_block context "SwiBlock3" fn in
1112    ignore (build_unreachable (builder_at_end context bb3));
1113    let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1114        ignore (add_case si (const_int i32_type 2) bb2);
1115        insist (switch_default_dest si = bb3);
1116    end;
1117  end;
1118
1119  group "malloc/free"; begin
1120      (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1121       * CHECK: call{{.*}}@free(i8*
1122       * CHECK: call{{.*}}@malloc(i32 %
1123       *)
1124      let bb1 = append_block context "MallocBlock1" fn in
1125      let m1 = (build_malloc (pointer_type i32_type) "m1"
1126      (builder_at_end context bb1)) in
1127      ignore (build_free m1 (builder_at_end context bb1));
1128      ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1129      ignore (build_unreachable (builder_at_end context bb1));
1130  end;
1131
1132  group "indirectbr"; begin
1133    (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1134     *)
1135    let bb1 = append_block context "IBRBlock1" fn in
1136
1137    let bb2 = append_block context "IBRBlock2" fn in
1138    ignore (build_unreachable (builder_at_end context bb2));
1139
1140    let bb3 = append_block context "IBRBlock3" fn in
1141    ignore (build_unreachable (builder_at_end context bb3));
1142
1143    let addr = block_address fn bb2 in
1144    let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1145    ignore (add_destination ibr bb2);
1146    ignore (add_destination ibr bb3)
1147  end;
1148  
1149  group "invoke"; begin
1150    (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1151     * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1152     *)
1153    let bb04 = append_block context "Bb04" fn in
1154    let b = builder_at_end context bb04 in
1155    ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1156  end;
1157  
1158  group "unreachable"; begin
1159    (* CHECK: unreachable
1160     *)
1161    let bb06 = append_block context "Bb06" fn in
1162    let b = builder_at_end context bb06 in
1163    ignore (build_unreachable b)
1164  end;
1165  
1166  group "arithmetic"; begin
1167    let bb07 = append_block context "Bb07" fn in
1168    let b = builder_at_end context bb07 in
1169    
1170    (* CHECK: %build_add = add i32 %P1, %P2
1171     * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1172     * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1173     * CHECK: %build_fadd = fadd float %F1, %F2
1174     * CHECK: %build_sub = sub i32 %P1, %P2
1175     * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1176     * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1177     * CHECK: %build_fsub = fsub float %F1, %F2
1178     * CHECK: %build_mul = mul i32 %P1, %P2
1179     * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1180     * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1181     * CHECK: %build_fmul = fmul float %F1, %F2
1182     * CHECK: %build_udiv = udiv i32 %P1, %P2
1183     * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1184     * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1185     * CHECK: %build_fdiv = fdiv float %F1, %F2
1186     * CHECK: %build_urem = urem i32 %P1, %P2
1187     * CHECK: %build_srem = srem i32 %P1, %P2
1188     * CHECK: %build_frem = frem float %F1, %F2
1189     * CHECK: %build_shl = shl i32 %P1, %P2
1190     * CHECK: %build_lshl = lshr i32 %P1, %P2
1191     * CHECK: %build_ashl = ashr i32 %P1, %P2
1192     * CHECK: %build_and = and i32 %P1, %P2
1193     * CHECK: %build_or = or i32 %P1, %P2
1194     * CHECK: %build_xor = xor i32 %P1, %P2
1195     * CHECK: %build_neg = sub i32 0, %P1
1196     * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1197     * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1198     * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1
1199     * CHECK: %build_not = xor i32 %P1, -1
1200     *)
1201    ignore (build_add p1 p2 "build_add" b);
1202    ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1203    ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1204    ignore (build_fadd f1 f2 "build_fadd" b);
1205    ignore (build_sub p1 p2 "build_sub" b);
1206    ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1207    ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1208    ignore (build_fsub f1 f2 "build_fsub" b);
1209    ignore (build_mul p1 p2 "build_mul" b);
1210    ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1211    ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1212    ignore (build_fmul f1 f2 "build_fmul" b);
1213    ignore (build_udiv p1 p2 "build_udiv" b);
1214    ignore (build_sdiv p1 p2 "build_sdiv" b);
1215    ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1216    ignore (build_fdiv f1 f2 "build_fdiv" b);
1217    ignore (build_urem p1 p2 "build_urem" b);
1218    ignore (build_srem p1 p2 "build_srem" b);
1219    ignore (build_frem f1 f2 "build_frem" b);
1220    ignore (build_shl p1 p2 "build_shl" b);
1221    ignore (build_lshr p1 p2 "build_lshl" b);
1222    ignore (build_ashr p1 p2 "build_ashl" b);
1223    ignore (build_and p1 p2 "build_and" b);
1224    ignore (build_or p1 p2 "build_or" b);
1225    ignore (build_xor p1 p2 "build_xor" b);
1226    ignore (build_neg p1 "build_neg" b);
1227    ignore (build_nsw_neg p1 "build_nsw_neg" b);
1228    ignore (build_nuw_neg p1 "build_nuw_neg" b);
1229    ignore (build_fneg f1 "build_fneg" b);
1230    ignore (build_not p1 "build_not" b);
1231    ignore (build_unreachable b)
1232  end;
1233  
1234  group "memory"; begin
1235    let bb08 = append_block context "Bb08" fn in
1236    let b = builder_at_end context bb08 in
1237
1238    (* CHECK: %build_alloca = alloca i32
1239     * CHECK: %build_array_alloca = alloca i32, i32 %P2
1240     * CHECK: %build_load = load i32* %build_array_alloca
1241     * CHECK: store i32 %P2, i32* %build_alloca
1242     * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
1243     * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
1244     * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1245     *)
1246    let alloca = build_alloca i32_type "build_alloca" b in
1247    let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1248    ignore(build_load array_alloca "build_load" b);
1249    ignore(build_store p2 alloca b);
1250    ignore(build_gep array_alloca [| p2 |] "build_gep" b);
1251    ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
1252
1253    let sty = struct_type context [| i32_type; i8_type |] in
1254    let alloca2 = build_alloca sty "build_alloca2" b in
1255    ignore(build_struct_gep alloca2 1 "build_struct_gep" b);
1256
1257    ignore(build_unreachable b)
1258  end;
1259
1260  group "string"; begin
1261    let bb09 = append_block context "Bb09" fn in
1262    let b = builder_at_end context bb09 in
1263    let p = build_alloca (pointer_type i8_type) "p" b in
1264    (* build_global_string is emitted above.
1265     * CHECK: store{{.*}}build_global_string1{{.*}}p
1266     * *)
1267    ignore (build_global_string "stringval" "build_global_string" b);
1268    let g = build_global_stringptr "stringval" "build_global_string1" b in
1269    ignore (build_store g p b);
1270    ignore(build_unreachable b);
1271  end;
1272
1273  group "phi"; begin
1274    (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1275     *)
1276    let b1 = append_block context "PhiBlock1" fn in
1277    let b2 = append_block context "PhiBlock2" fn in
1278    
1279    let jb = append_block context "PhiJoinBlock" fn in
1280    ignore (build_br jb (builder_at_end context b1));
1281    ignore (build_br jb (builder_at_end context b2));
1282    let at_jb = builder_at_end context jb in
1283    
1284    let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1285    insist ([(p1, b1)] = incoming phi);
1286    
1287    add_incoming (p2, b2) phi;
1288    insist ([(p1, b1); (p2, b2)] = incoming phi);
1289    
1290    ignore (build_unreachable at_jb);
1291  end
1292
1293(* End-of-file checks for things like metdata and attributes.
1294 * CHECK: attributes #0 = {{.*}}uwtable{{.*}}
1295 * CHECK: !0 = metadata !{i32 1, metadata !"metadata test"}
1296 * CHECK: !1 = metadata !{i32 2, i32 3, metadata !2, metadata !2}
1297 *)
1298
1299(*===-- Pass Managers -----------------------------------------------------===*)
1300
1301let test_pass_manager () =
1302  let (++) x f = ignore (f x); x in
1303
1304  begin group "module pass manager";
1305    ignore (PassManager.create ()
1306             ++ PassManager.run_module m
1307             ++ PassManager.dispose)
1308  end;
1309  
1310  begin group "function pass manager";
1311    let fty = function_type void_type [| |] in
1312    let fn = define_function "FunctionPassManager" fty m in
1313    ignore (build_ret_void (builder_at_end context (entry_block fn)));
1314    
1315    ignore (PassManager.create_function m
1316             ++ PassManager.initialize
1317             ++ PassManager.run_function fn
1318             ++ PassManager.finalize
1319             ++ PassManager.dispose)
1320  end
1321
1322
1323(*===-- Memory Buffer -----------------------------------------------------===*)
1324
1325let test_memory_buffer () =
1326  group "memory buffer";
1327  let buf = MemoryBuffer.of_string "foobar" in
1328  insist ((MemoryBuffer.as_string buf) = "foobar")
1329
1330
1331(*===-- Writer ------------------------------------------------------------===*)
1332
1333let test_writer () =
1334  group "valid";
1335  insist (match Llvm_analysis.verify_module m with
1336          | None -> true
1337          | Some msg -> prerr_string msg; false);
1338
1339  group "writer";
1340  insist (write_bitcode_file m filename);
1341  
1342  dispose_module m
1343
1344
1345(*===-- Driver ------------------------------------------------------------===*)
1346
1347let _ =
1348  suite "target"           test_target;
1349  suite "constants"        test_constants;
1350  suite "global values"    test_global_values;
1351  suite "global variables" test_global_variables;
1352  suite "uses"             test_uses;
1353  suite "users"            test_users;
1354  suite "aliases"          test_aliases;
1355  suite "functions"        test_functions;
1356  suite "params"           test_params;
1357  suite "basic blocks"     test_basic_blocks;
1358  suite "instructions"     test_instructions;
1359  suite "builder"          test_builder;
1360  suite "pass manager"     test_pass_manager;
1361  suite "memory buffer"    test_memory_buffer;
1362  suite "writer"           test_writer; (* Keep this last; it disposes m. *)
1363  exit !exit_status
1364