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