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