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