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