[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / test / Bindings / OCaml / core.ml
blob1f6ece9532f194389e801880508581b9b5d05512
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
268 * CHECK: @const_fcmp = global i1 fcmp ole
270 let void_ptr = pointer_type context in
271 let five = const_int i64_type 5 in
272 let ffive = const_uitofp five double_type in
273 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
274 let foldbomb = const_ptrtoint foldbomb_gv i64_type in
275 let ffoldbomb = const_uitofp foldbomb double_type in
276 ignore (define_global "const_neg" (const_neg foldbomb) m);
277 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
278 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
279 ignore (define_global "const_not" (const_not foldbomb) m);
280 ignore (define_global "const_add" (const_add foldbomb five) m);
281 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
282 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
283 ignore (define_global "const_sub" (const_sub foldbomb five) m);
284 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
285 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
286 ignore (define_global "const_mul" (const_mul foldbomb five) m);
287 ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
288 ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
289 ignore (define_global "const_xor" (const_xor foldbomb five) m);
290 ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
291 ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
293 group "constant casts";
294 (* CHECK: const_trunc{{.*}}trunc
295 * CHECK: const_sext{{.*}}sext
296 * CHECK: const_zext{{.*}}zext
297 * CHECK: const_fptrunc{{.*}}fptrunc
298 * CHECK: const_fpext{{.*}}fpext
299 * CHECK: const_uitofp{{.*}}uitofp
300 * CHECK: const_sitofp{{.*}}sitofp
301 * CHECK: const_fptoui{{.*}}fptoui
302 * CHECK: const_fptosi{{.*}}fptosi
303 * CHECK: const_ptrtoint{{.*}}ptrtoint
304 * CHECK: const_inttoptr{{.*}}inttoptr
305 * CHECK: const_bitcast{{.*}}bitcast
306 * CHECK: const_intcast{{.*}}zext
308 let i128_type = integer_type context 128 in
309 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
310 i8_type) m);
311 ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
312 ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
313 ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
314 ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
315 ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
316 ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
317 ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
318 ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
319 ignore (define_global "const_ptrtoint" (const_ptrtoint
320 (const_gep i8_type (const_null (pointer_type context))
321 [| const_int i32_type 1 |])
322 i32_type) m);
323 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
324 void_ptr) m);
325 ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
326 ignore (define_global "const_intcast"
327 (const_intcast foldbomb i128_type ~is_signed:false) m);
329 group "misc constants";
330 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
331 * CHECK: const_gep{{.*}}getelementptr
332 * CHECK: const_extractelement{{.*}}extractelement
333 * CHECK: const_insertelement{{.*}}insertelement
334 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
336 ignore (define_global "const_size_of" (size_of (pointer_type context)) m);
337 ignore (define_global "const_gep" (const_gep i8_type foldbomb_gv [| five |])
339 let zero = const_int i32_type 0 in
340 let one = const_int i32_type 1 in
341 ignore (define_global "const_extractelement" (const_extractelement
342 (const_vector [| zero; one; zero; one |])
343 (const_trunc foldbomb i32_type)) m);
344 ignore (define_global "const_insertelement" (const_insertelement
345 (const_vector [| zero; one; zero; one |])
346 zero (const_trunc foldbomb i32_type)) m);
347 ignore (define_global "const_shufflevector" (const_shufflevector
348 (const_vector [| zero; one |])
349 (const_vector [| one; zero |])
350 (const_vector [| const_int i32_type 0; const_int i32_type 1;
351 const_int i32_type 2; const_int i32_type 3 |])) m);
353 group "asm"; begin
354 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
355 ignore (const_inline_asm
358 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
359 true
360 false)
361 end;
363 group "recursive struct"; begin
364 let nsty = named_struct_type context "rec" in
365 let pty = pointer_type context in
366 struct_set_body nsty [| i32_type; pty |] false;
367 let elts = [| const_int i32_type 4; const_pointer_null pty |] in
368 let grec_init = const_named_struct nsty elts in
369 ignore (define_global "grec" grec_init m);
370 ignore (string_of_lltype nsty);
374 (*===-- Attributes --------------------------------------------------------===*)
376 let test_attributes () =
377 group "enum attrs";
378 let nonnull_kind = enum_attr_kind "nonnull" in
379 let dereferenceable_kind = enum_attr_kind "dereferenceable" in
380 insist (nonnull_kind = (enum_attr_kind "nonnull"));
381 insist (nonnull_kind <> dereferenceable_kind);
383 let nonnull =
384 create_enum_attr context "nonnull" 0L in
385 let dereferenceable_4 =
386 create_enum_attr context "dereferenceable" 4L in
387 let dereferenceable_8 =
388 create_enum_attr context "dereferenceable" 8L in
389 insist (nonnull <> dereferenceable_4);
390 insist (dereferenceable_4 <> dereferenceable_8);
391 insist (nonnull = (create_enum_attr context "nonnull" 0L));
392 insist ((repr_of_attr nonnull) =
393 AttrRepr.Enum(nonnull_kind, 0L));
394 insist ((repr_of_attr dereferenceable_4) =
395 AttrRepr.Enum(dereferenceable_kind, 4L));
396 insist ((attr_of_repr context (repr_of_attr nonnull)) =
397 nonnull);
398 insist ((attr_of_repr context (repr_of_attr dereferenceable_4)) =
399 dereferenceable_4);
401 group "string attrs";
402 let foo_bar = create_string_attr context "foo" "bar" in
403 let foo_baz = create_string_attr context "foo" "baz" in
404 insist (foo_bar <> foo_baz);
405 insist (foo_bar = (create_string_attr context "foo" "bar"));
406 insist ((repr_of_attr foo_bar) = AttrRepr.String("foo", "bar"));
407 insist ((attr_of_repr context (repr_of_attr foo_bar)) = foo_bar);
410 (*===-- Global Values -----------------------------------------------------===*)
412 let test_global_values () =
413 let (++) x f = f x; x in
414 let zero32 = const_null i32_type in
416 (* CHECK: GVal01
418 group "naming";
419 let g = define_global "TEMPORARY" zero32 m in
420 insist ("TEMPORARY" = value_name g);
421 set_value_name "GVal01" g;
422 insist ("GVal01" = value_name g);
424 (* CHECK: GVal02{{.*}}linkonce
426 group "linkage";
427 let g = define_global "GVal02" zero32 m ++
428 set_linkage Linkage.Link_once in
429 insist (Linkage.Link_once = linkage g);
431 (* CHECK: GVal03{{.*}}Hanalei
433 group "section";
434 let g = define_global "GVal03" zero32 m ++
435 set_section "Hanalei" in
436 insist ("Hanalei" = section g);
438 (* CHECK: GVal04{{.*}}hidden
440 group "visibility";
441 let g = define_global "GVal04" zero32 m ++
442 set_visibility Visibility.Hidden in
443 insist (Visibility.Hidden = visibility g);
445 (* CHECK: GVal05{{.*}}align 128
447 group "alignment";
448 let g = define_global "GVal05" zero32 m ++
449 set_alignment 128 in
450 insist (128 = alignment g);
452 (* CHECK: GVal06{{.*}}dllexport
454 group "dll_storage_class";
455 let g = define_global "GVal06" zero32 m ++
456 set_dll_storage_class DLLStorageClass.DLLExport in
457 insist (DLLStorageClass.DLLExport = dll_storage_class g)
460 (*===-- Global Variables --------------------------------------------------===*)
462 let test_global_variables () =
463 let (++) x f = f x; x in
464 let forty_two32 = const_int i32_type 42 in
466 group "declarations"; begin
467 (* CHECK: @GVar01 = external global i32
468 * CHECK: @QGVar01 = external addrspace(3) global i32
470 insist (None == lookup_global "GVar01" m);
471 let g = declare_global i32_type "GVar01" m in
472 insist (is_declaration g);
473 insist (pointer_type context ==
474 type_of (declare_global float_type "GVar01" m));
475 insist (g == declare_global i32_type "GVar01" m);
476 insist (match lookup_global "GVar01" m with Some x -> x = g
477 | None -> false);
479 insist (None == lookup_global "QGVar01" m);
480 let g = declare_qualified_global i32_type "QGVar01" 3 m in
481 insist (is_declaration g);
482 insist (qualified_pointer_type context 3 ==
483 type_of (declare_qualified_global float_type "QGVar01" 3 m));
484 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
485 insist (match lookup_global "QGVar01" m with Some x -> x = g
486 | None -> false);
487 end;
489 group "definitions"; begin
490 (* CHECK: @GVar02 = global i32 42
491 * CHECK: @GVar03 = global i32 42
492 * CHECK: @QGVar02 = addrspace(3) global i32 42
493 * CHECK: @QGVar03 = addrspace(3) global i32 42
495 let g = define_global "GVar02" forty_two32 m in
496 let g2 = declare_global i32_type "GVar03" m ++
497 set_initializer forty_two32 in
498 insist (not (is_declaration g));
499 insist (not (is_declaration g2));
500 insist ((global_initializer g) = (global_initializer g2));
502 let g = define_qualified_global "QGVar02" forty_two32 3 m in
503 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
504 set_initializer forty_two32 in
505 insist (not (is_declaration g));
506 insist (not (is_declaration g2));
507 insist ((global_initializer g) = (global_initializer g2));
508 end;
510 (* CHECK: GVar04{{.*}}thread_local
512 group "threadlocal";
513 let g = define_global "GVar04" forty_two32 m ++
514 set_thread_local true in
515 insist (is_thread_local g);
517 (* CHECK: GVar05{{.*}}thread_local(initialexec)
519 group "threadlocal_mode";
520 let g = define_global "GVar05" forty_two32 m ++
521 set_thread_local_mode ThreadLocalMode.InitialExec in
522 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
524 (* CHECK: GVar06{{.*}}externally_initialized
526 group "externally_initialized";
527 let g = define_global "GVar06" forty_two32 m ++
528 set_externally_initialized true in
529 insist (is_externally_initialized g);
531 (* CHECK-NOWHERE-NOT: GVar07
533 group "delete";
534 let g = define_global "GVar07" forty_two32 m in
535 delete_global g;
537 (* CHECK: ConstGlobalVar{{.*}}constant
539 group "constant";
540 let g = define_global "ConstGlobalVar" forty_two32 m in
541 insist (not (is_global_constant g));
542 set_global_constant true g;
543 insist (is_global_constant g);
545 begin group "iteration";
546 let m = create_module context "temp" in
548 insist (get_module_identifier m = "temp");
549 set_module_identifer m "temp2";
550 insist (get_module_identifier m = "temp2");
552 insist (At_end m = global_begin m);
553 insist (At_start m = global_end m);
555 let g1 = declare_global i32_type "One" m in
556 let g2 = declare_global i32_type "Two" m in
558 insist (Before g1 = global_begin m);
559 insist (Before g2 = global_succ g1);
560 insist (At_end m = global_succ g2);
562 insist (After g2 = global_end m);
563 insist (After g1 = global_pred g2);
564 insist (At_start m = global_pred g1);
566 let lf s x = s ^ "->" ^ value_name x in
567 insist ("->One->Two" = fold_left_globals lf "" m);
569 let rf x s = value_name x ^ "<-" ^ s in
570 insist ("One<-Two<-" = fold_right_globals rf m "");
572 dispose_module m
575 (* String globals built below are emitted here.
576 * CHECK: build_global_string{{.*}}stringval
580 (*===-- Uses --------------------------------------------------------------===*)
582 let test_uses () =
583 let ty = function_type i32_type [| i32_type; i32_type |] in
584 let fn = define_function "use_function" ty m in
585 let b = builder_at_end context (entry_block fn) in
587 let p1 = param fn 0 in
588 let p2 = param fn 1 in
589 let v1 = build_add p1 p2 "v1" b in
590 let v2 = build_add p1 v1 "v2" b in
591 let _ = build_add v1 v2 "v3" b in
593 let lf s u = value_name (user u) ^ "->" ^ s in
594 insist ("v2->v3->" = fold_left_uses lf "" v1);
595 let rf u s = value_name (user u) ^ "<-" ^ s in
596 insist ("v3<-v2<-" = fold_right_uses rf v1 "");
598 let lf s u = value_name (used_value u) ^ "->" ^ s in
599 insist ("v1->v1->" = fold_left_uses lf "" v1);
601 let rf u s = value_name (used_value u) ^ "<-" ^ s in
602 insist ("v1<-v1<-" = fold_right_uses rf v1 "");
604 ignore (build_unreachable b)
607 (*===-- Users -------------------------------------------------------------===*)
609 let test_users () =
610 let ty = function_type i32_type [| i32_type; i32_type |] in
611 let fn = define_function "user_function" ty m in
612 let b = builder_at_end context (entry_block fn) in
614 let p1 = param fn 0 in
615 let p2 = param fn 1 in
616 let a3 = build_alloca i32_type "user_alloca" b in
617 let p3 = build_load i32_type a3 "user_load" b in
618 let i = build_add p1 p2 "sum" b in
620 insist ((num_operands i) = 2);
621 insist ((operand i 0) = p1);
622 insist ((operand i 1) = p2);
624 set_operand i 1 p3;
625 insist ((operand i 1) != p2);
626 insist ((operand i 1) = p3);
628 ignore (build_unreachable b)
631 (*===-- Aliases -----------------------------------------------------------===*)
633 let test_aliases () =
634 (* CHECK: @alias = alias i32, ptr @aliasee
636 let forty_two32 = const_int i32_type 42 in
637 let v = define_global "aliasee" forty_two32 m in
638 ignore (add_alias m i32_type 0 v "alias")
641 (*===-- Functions ---------------------------------------------------------===*)
643 let test_functions () =
644 let ty = function_type i32_type [| i32_type; i64_type |] in
645 let ty2 = function_type i8_type [| i8_type; i64_type |] in
647 (* CHECK: declare i32 @Fn1(i32, i64)
649 begin group "declare";
650 insist (None = lookup_function "Fn1" m);
651 let fn = declare_function "Fn1" ty m in
652 insist (pointer_type context = type_of fn);
653 insist (is_declaration fn);
654 insist (0 = Array.length (basic_blocks fn));
655 insist (pointer_type context == type_of (declare_function "Fn1" ty2 m));
656 insist (fn == declare_function "Fn1" ty m);
657 insist (None <> lookup_function "Fn1" m);
658 insist (match lookup_function "Fn1" m with Some x -> x = fn
659 | None -> false);
660 insist (m == global_parent fn)
661 end;
663 (* CHECK-NOWHERE-NOT: Fn2
665 group "delete";
666 let fn = declare_function "Fn2" ty m in
667 delete_function fn;
669 (* CHECK: define{{.*}}Fn3
671 group "define";
672 let fn = define_function "Fn3" ty m in
673 insist (not (is_declaration fn));
674 insist (1 = Array.length (basic_blocks fn));
675 ignore (build_unreachable (builder_at_end context (entry_block fn)));
677 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
679 group "params";
680 let fn = define_function "Fn4" ty m in
681 let params = params fn in
682 insist (2 = Array.length params);
683 insist (params.(0) = param fn 0);
684 insist (params.(1) = param fn 1);
685 insist (i32_type = type_of params.(0));
686 insist (i64_type = type_of params.(1));
687 set_value_name "Param1" params.(0);
688 set_value_name "Param2" params.(1);
689 ignore (build_unreachable (builder_at_end context (entry_block fn)));
691 (* CHECK: fastcc{{.*}}Fn5
693 group "callconv";
694 let fn = define_function "Fn5" ty m in
695 insist (CallConv.c = function_call_conv fn);
696 set_function_call_conv CallConv.fast fn;
697 insist (CallConv.fast = function_call_conv fn);
698 ignore (build_unreachable (builder_at_end context (entry_block fn)));
700 begin group "gc";
701 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
703 let fn = define_function "Fn6" ty m in
704 insist (None = gc fn);
705 set_gc (Some "ocaml") fn;
706 insist (Some "ocaml" = gc fn);
707 set_gc None fn;
708 insist (None = gc fn);
709 set_gc (Some "shadowstack") fn;
710 ignore (build_unreachable (builder_at_end context (entry_block fn)));
711 end;
713 begin group "iteration";
714 let m = create_module context "temp" in
716 insist (At_end m = function_begin m);
717 insist (At_start m = function_end m);
719 let f1 = define_function "One" ty m in
720 let f2 = define_function "Two" ty m in
722 insist (Before f1 = function_begin m);
723 insist (Before f2 = function_succ f1);
724 insist (At_end m = function_succ f2);
726 insist (After f2 = function_end m);
727 insist (After f1 = function_pred f2);
728 insist (At_start m = function_pred f1);
730 let lf s x = s ^ "->" ^ value_name x in
731 insist ("->One->Two" = fold_left_functions lf "" m);
733 let rf x s = value_name x ^ "<-" ^ s in
734 insist ("One<-Two<-" = fold_right_functions rf m "");
736 dispose_module m
740 (*===-- Params ------------------------------------------------------------===*)
742 let test_params () =
743 begin group "iteration";
744 let m = create_module context "temp" in
746 let vf = define_function "void" (function_type void_type [| |]) m in
748 insist (At_end vf = param_begin vf);
749 insist (At_start vf = param_end vf);
751 let ty = function_type void_type [| i32_type; i32_type |] in
752 let f = define_function "f" ty m in
753 let p1 = param f 0 in
754 let p2 = param f 1 in
755 set_value_name "One" p1;
756 set_value_name "Two" p2;
758 insist (Before p1 = param_begin f);
759 insist (Before p2 = param_succ p1);
760 insist (At_end f = param_succ p2);
762 insist (After p2 = param_end f);
763 insist (After p1 = param_pred p2);
764 insist (At_start f = param_pred p1);
766 let lf s x = s ^ "->" ^ value_name x in
767 insist ("->One->Two" = fold_left_params lf "" f);
769 let rf x s = value_name x ^ "<-" ^ s in
770 insist ("One<-Two<-" = fold_right_params rf f "");
772 dispose_module m
776 (*===-- Basic Blocks ------------------------------------------------------===*)
778 let test_basic_blocks () =
779 let ty = function_type void_type [| |] in
781 (* CHECK: Bb1
783 group "entry";
784 let fn = declare_function "X" ty m in
785 let bb = append_block context "Bb1" fn in
786 insist (bb = entry_block fn);
787 ignore (build_unreachable (builder_at_end context bb));
789 (* CHECK-NOWHERE-NOT: Bb2
791 group "delete";
792 let fn = declare_function "X2" ty m in
793 let bb = append_block context "Bb2" fn in
794 delete_block bb;
796 group "insert";
797 let fn = declare_function "X3" ty m in
798 let bbb = append_block context "b" fn in
799 let bba = insert_block context "a" bbb in
800 insist ([| bba; bbb |] = basic_blocks fn);
801 ignore (build_unreachable (builder_at_end context bba));
802 ignore (build_unreachable (builder_at_end context bbb));
804 (* CHECK: Bb3
806 group "name/value";
807 let fn = define_function "X4" ty m in
808 let bb = entry_block fn in
809 ignore (build_unreachable (builder_at_end context bb));
810 let bbv = value_of_block bb in
811 set_value_name "Bb3" bbv;
812 insist ("Bb3" = value_name bbv);
814 group "casts";
815 let fn = define_function "X5" ty m in
816 let bb = entry_block fn in
817 ignore (build_unreachable (builder_at_end context bb));
818 insist (bb = block_of_value (value_of_block bb));
819 insist (value_is_block (value_of_block bb));
820 insist (not (value_is_block (const_null i32_type)));
822 begin group "iteration";
823 let m = create_module context "temp" in
824 let f = declare_function "Temp" (function_type i32_type [| |]) m in
826 insist (At_end f = block_begin f);
827 insist (At_start f = block_end f);
829 let b1 = append_block context "One" f in
830 let b2 = append_block context "Two" f in
832 insist (Before b1 = block_begin f);
833 insist (Before b2 = block_succ b1);
834 insist (At_end f = block_succ b2);
836 insist (After b2 = block_end f);
837 insist (After b1 = block_pred b2);
838 insist (At_start f = block_pred b1);
840 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
841 insist ("->One->Two" = fold_left_blocks lf "" f);
843 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
844 insist ("One<-Two<-" = fold_right_blocks rf f "");
846 dispose_module m
850 (*===-- Instructions ------------------------------------------------------===*)
852 let test_instructions () =
853 begin group "iteration";
854 let m = create_module context "temp" in
855 let fty = function_type void_type [| i32_type; i32_type |] in
856 let f = define_function "f" fty m in
857 let bb = entry_block f in
858 let b = builder_at context (At_end bb) in
860 insist (At_end bb = instr_begin bb);
861 insist (At_start bb = instr_end bb);
863 let i1 = build_add (param f 0) (param f 1) "One" b in
864 let i2 = build_sub (param f 0) (param f 1) "Two" b in
866 insist (Before i1 = instr_begin bb);
867 insist (Before i2 = instr_succ i1);
868 insist (At_end bb = instr_succ i2);
870 insist (After i2 = instr_end bb);
871 insist (After i1 = instr_pred i2);
872 insist (At_start bb = instr_pred i1);
874 let lf s x = s ^ "->" ^ value_name x in
875 insist ("->One->Two" = fold_left_instrs lf "" bb);
877 let rf x s = value_name x ^ "<-" ^ s in
878 insist ("One<-Two<-" = fold_right_instrs rf bb "");
880 dispose_module m
881 end;
883 group "clone instr";
884 begin
885 (* CHECK: %clone = add i32 %0, 2
887 let fty = function_type void_type [| i32_type |] in
888 let fn = define_function "BuilderParent" fty m in
889 let bb = entry_block fn in
890 let b = builder_at_end context bb in
891 let p = param fn 0 in
892 let sum = build_add p p "sum" b in
893 let y = const_int i32_type 2 in
894 let clone = instr_clone sum in
895 set_operand clone 0 p;
896 set_operand clone 1 y;
897 insert_into_builder clone "clone" b;
898 ignore (build_ret_void b)
902 (*===-- Builder -----------------------------------------------------------===*)
904 let test_builder () =
905 let (++) x f = f x; x in
907 begin group "parent";
908 insist (try
909 ignore (insertion_block (builder context));
910 false
911 with Not_found ->
912 true);
914 let fty = function_type void_type [| i32_type |] in
915 let fn = define_function "BuilderParent" fty m in
916 let bb = entry_block fn in
917 let b = builder_at_end context bb in
918 let p = param fn 0 in
919 let sum = build_add p p "sum" b in
920 ignore (build_ret_void b);
922 insist (fn = block_parent bb);
923 insist (fn = param_parent p);
924 insist (bb = instr_parent sum);
925 insist (bb = insertion_block b)
926 end;
928 group "ret void";
929 begin
930 (* CHECK: ret void
932 let fty = function_type void_type [| |] in
933 let fn = declare_function "X6" fty m in
934 let b = builder_at_end context (append_block context "Bb01" fn) in
935 ignore (build_ret_void b)
936 end;
938 group "ret aggregate";
939 begin
940 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }
942 let sty = struct_type context [| i8_type; i64_type |] in
943 let fty = function_type sty [| |] in
944 let fn = declare_function "XA6" fty m in
945 let b = builder_at_end context (append_block context "Bb01" fn) in
946 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in
947 ignore (build_aggregate_ret agg b)
948 end;
950 (* The rest of the tests will use one big function. *)
951 let fty = function_type i32_type [| i32_type; i32_type |] in
952 let fn = define_function "X7" fty m in
953 let atentry = builder_at_end context (entry_block fn) in
954 let p1 = param fn 0 ++ set_value_name "P1" in
955 let p2 = param fn 1 ++ set_value_name "P2" in
956 let f1 = build_uitofp p1 float_type "F1" atentry in
957 let f2 = build_uitofp p2 float_type "F2" atentry in
959 let bb00 = append_block context "Bb00" fn in
960 ignore (build_unreachable (builder_at_end context bb00));
962 group "function attribute";
963 begin
964 let signext = create_enum_attr context "signext" 0L in
965 let zeroext = create_enum_attr context "zeroext" 0L in
966 let noalias = create_enum_attr context "noalias" 0L in
967 let nounwind = create_enum_attr context "nounwind" 0L in
968 let no_sse = create_string_attr context "no-sse" "" in
970 add_function_attr fn signext (AttrIndex.Param 0);
971 add_function_attr fn noalias (AttrIndex.Param 1);
972 insist ((function_attrs fn (AttrIndex.Param 1)) = [|noalias|]);
973 remove_enum_function_attr fn (enum_attr_kind "noalias") (AttrIndex.Param 1);
974 add_function_attr fn no_sse (AttrIndex.Param 1);
975 insist ((function_attrs fn (AttrIndex.Param 1)) = [|no_sse|]);
976 remove_string_function_attr fn "no-sse" (AttrIndex.Param 1);
977 insist ((function_attrs fn (AttrIndex.Param 1)) = [||]);
978 add_function_attr fn nounwind AttrIndex.Function;
979 add_function_attr fn zeroext AttrIndex.Return;
981 (* CHECK: define zeroext i32 @X7(i32 signext %P1, i32 %P2)
983 end;
985 group "casts"; begin
986 let void_ptr = pointer_type context in
988 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8
989 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8
990 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8
991 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32
992 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32
993 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64
994 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64
995 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64
996 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float
997 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double
998 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32
999 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64
1000 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float
1001 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float
1002 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double
1003 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double
1004 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to ptr
1005 * CHECK-DAG: %build_ptrtoint = ptrtoint ptr %build_inttoptr to i64
1006 * CHECK-DAG: %build_ptrtoint2 = ptrtoint ptr %build_inttoptr to i64
1007 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double
1008 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double
1009 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double
1010 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double
1012 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in
1013 let inst29 = build_zext inst28 i32_type "build_zext" atentry in
1014 let inst30 = build_sext inst29 i64_type "build_sext" atentry in
1015 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in
1016 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in
1017 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);
1018 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);
1019 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in
1020 ignore(build_fpext inst35 double_type "build_fpext" atentry);
1021 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in
1022 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in
1023 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);
1024 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);
1025 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);
1026 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);
1027 ignore(build_pointercast inst37 (pointer_type context) "build_pointercast" atentry);
1029 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);
1030 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);
1031 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);
1032 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);
1033 ignore(build_intcast inst29 i64_type "build_sext3" atentry);
1034 ignore(build_intcast p1 i8_type "build_trunc3" atentry);
1035 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);
1036 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);
1037 end;
1039 group "comparisons"; begin
1040 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2
1041 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1
1042 * CHECK: %build_fcmp_false = fcmp false float %F1, %F2
1043 * CHECK: %build_fcmp_true = fcmp true float %F2, %F1
1044 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null
1045 * CHECK: %build_is_not_null = icmp ne ptr %X1, null
1046 * CHECK: %build_ptrdiff
1048 let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in
1049 insist (Some Icmp.Ne = icmp_predicate c);
1050 insist (None = fcmp_predicate c);
1052 let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in
1053 insist (Some Icmp.Sle = icmp_predicate c);
1054 insist (None = fcmp_predicate c);
1056 let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in
1057 (* insist (Some Fcmp.False = fcmp_predicate c); *)
1058 insist (None = icmp_predicate c);
1060 let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in
1061 (* insist (Some Fcmp.True = fcmp_predicate c); *)
1062 insist (None = icmp_predicate c);
1064 let g0 = declare_global (pointer_type context) "g0" m in
1065 let g1 = declare_global (pointer_type context) "g1" m in
1066 let p0 = build_load (pointer_type context) g0 "X0" atentry in
1067 let p1 = build_load (pointer_type context) g1 "X1" atentry in
1068 ignore (build_is_null p0 "build_is_null" atentry);
1069 ignore (build_is_not_null p1 "build_is_not_null" atentry);
1070 ignore (build_ptrdiff i8_type p1 p0 "build_ptrdiff" atentry);
1071 end;
1073 group "miscellaneous"; begin
1074 (* CHECK: %build_call = tail call cc63 zeroext i32 @{{.*}}(i32 signext %P2, i32 %P1)
1075 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2
1076 * CHECK: %build_va_arg = va_arg ptr null, i32
1077 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2
1078 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2
1079 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>
1080 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0
1081 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1
1083 let ci = build_call fty fn [| p2; p1 |] "build_call" atentry in
1084 insist (CallConv.c = instruction_call_conv ci);
1085 set_instruction_call_conv 63 ci;
1086 insist (63 = instruction_call_conv ci);
1087 insist (not (is_tail_call ci));
1088 set_tail_call true ci;
1089 insist (is_tail_call ci);
1091 let signext = create_enum_attr context "signext" 0L in
1092 let zeroext = create_enum_attr context "zeroext" 0L in
1093 let noalias = create_enum_attr context "noalias" 0L in
1094 let noreturn = create_enum_attr context "noreturn" 0L in
1095 let no_sse = create_string_attr context "no-sse" "" in
1097 add_call_site_attr ci signext (AttrIndex.Param 0);
1098 add_call_site_attr ci noalias (AttrIndex.Param 1);
1099 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|noalias|]);
1100 remove_enum_call_site_attr ci (enum_attr_kind "noalias") (AttrIndex.Param 1);
1101 add_call_site_attr ci no_sse (AttrIndex.Param 1);
1102 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|no_sse|]);
1103 remove_string_call_site_attr ci "no-sse" (AttrIndex.Param 1);
1104 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [||]);
1105 add_call_site_attr ci noreturn AttrIndex.Function;
1106 add_call_site_attr ci zeroext AttrIndex.Return;
1108 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in
1109 ignore (build_select inst46 p1 p2 "build_select" atentry);
1110 ignore (build_va_arg
1111 (const_null (pointer_type context))
1112 i32_type "build_va_arg" atentry);
1114 (* Set up some vector vregs. *)
1115 let one = const_int i32_type 1 in
1116 let zero = const_int i32_type 0 in
1117 let t1 = const_vector [| one; zero; one; zero |] in
1118 let t2 = const_vector [| zero; one; zero; one |] in
1119 let t3 = const_vector [| one; one; zero; zero |] in
1120 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
1121 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
1122 let sty = struct_type context [| i32_type; i8_type |] in
1124 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);
1125 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);
1126 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);
1128 let p = build_alloca sty "ba" atentry in
1129 let agg = build_load sty p "bl" atentry in
1130 let agg0 = build_insertvalue agg (const_int i32_type 1) 0
1131 "build_insertvalue0" atentry in
1132 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1
1133 "build_insertvalue1" atentry in
1134 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)
1135 end;
1137 group "metadata"; begin
1138 (* CHECK: %metadata = add i32 %P1, %P2, !test !1
1139 * !1 is metadata emitted at EOF.
1141 let i = build_add p1 p2 "metadata" atentry in
1142 insist ((has_metadata i) = false);
1144 let m1 = const_int i32_type 1 in
1145 let m2 = mdstring context "metadata test" in
1146 let md = mdnode context [| m1; m2 |] in
1148 let kind = mdkind_id context "test" in
1149 set_metadata i kind md;
1151 insist ((has_metadata i) = true);
1152 insist ((metadata i kind) = Some md);
1153 insist ((get_mdnode_operands md) = [| m1; m2 |]);
1155 clear_metadata i kind;
1157 insist ((has_metadata i) = false);
1158 insist ((metadata i kind) = None);
1160 set_metadata i kind md
1161 end;
1163 group "named metadata"; begin
1164 (* !llvm.module.flags is emitted at EOF. *)
1165 let n1 = const_int i32_type 1 in
1166 let n2 = mdstring context "Debug Info Version" in
1167 let n3 = const_int i32_type 3 in
1168 let md = mdnode context [| n1; n2; n3 |] in
1169 add_named_metadata_operand m "llvm.module.flags" md;
1171 insist ((get_named_metadata m "llvm.module.flags") = [| md |])
1172 end;
1174 group "ret"; begin
1175 (* CHECK: ret{{.*}}P1
1177 let ret = build_ret p1 atentry in
1178 position_before ret atentry
1179 end;
1181 (* see test/Feature/exception.ll *)
1182 let bblpad = append_block context "Bblpad" fn in
1183 let rt = struct_type context [| pointer_type context; i32_type |] in
1184 let ft = var_arg_function_type i32_type [||] in
1185 let personality = declare_function "__gxx_personality_v0" ft m in
1186 let ztic = declare_global (pointer_type context) "_ZTIc" m in
1187 let ztid = declare_global (pointer_type context) "_ZTId" m in
1188 let ztipkc = declare_global (pointer_type context) "_ZTIPKc" m in
1189 begin
1190 set_global_constant true ztic;
1191 set_global_constant true ztid;
1192 set_global_constant true ztipkc;
1193 let lp = build_landingpad rt personality 0 "lpad"
1194 (builder_at_end context bblpad) in begin
1195 set_cleanup lp true;
1196 add_clause lp ztic;
1197 insist((pointer_type context) = type_of ztid);
1198 let ety = pointer_type context in
1199 add_clause lp (const_array ety [| ztipkc; ztid |]);
1200 ignore (build_resume lp (builder_at_end context bblpad));
1201 end;
1202 (* CHECK: landingpad
1203 * CHECK: cleanup
1204 * CHECK: catch{{.*}}ptr{{.*}}@_ZTIc
1205 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId
1206 * CHECK: resume
1207 * *)
1208 end;
1210 group "br"; begin
1211 (* CHECK: br{{.*}}Bb02
1213 let bb02 = append_block context "Bb02" fn in
1214 let b = builder_at_end context bb02 in
1215 let br = build_br bb02 b in
1216 insist (successors br = [| bb02 |]) ;
1217 insist (is_conditional br = false) ;
1218 insist (get_branch br = Some (`Unconditional bb02)) ;
1219 end;
1221 group "cond_br"; begin
1222 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00
1224 let bb03 = append_block context "Bb03" fn in
1225 let b = builder_at_end context bb03 in
1226 let cond = build_trunc p1 i1_type "build_br" b in
1227 let br = build_cond_br cond bb03 bb00 b in
1228 insist (num_successors br = 2) ;
1229 insist (successor br 0 = bb03) ;
1230 insist (successor br 1 = bb00) ;
1231 insist (is_conditional br = true) ;
1232 insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ;
1233 end;
1235 group "switch"; begin
1236 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3
1237 * CHECK: 2,{{.*}}SwiBlock2
1239 let bb1 = append_block context "SwiBlock1" fn in
1240 let bb2 = append_block context "SwiBlock2" fn in
1241 ignore (build_unreachable (builder_at_end context bb2));
1242 let bb3 = append_block context "SwiBlock3" fn in
1243 ignore (build_unreachable (builder_at_end context bb3));
1244 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin
1245 ignore (add_case si (const_int i32_type 2) bb2);
1246 insist (switch_default_dest si = bb3);
1247 end;
1248 insist (num_successors si = 2) ;
1249 insist (get_branch si = None) ;
1250 end;
1252 group "malloc/free"; begin
1253 (* CHECK: call{{.*}}@malloc(i32 ptrtoint
1254 * CHECK: call{{.*}}@free(ptr
1255 * CHECK: call{{.*}}@malloc(i32 %
1257 let bb1 = append_block context "MallocBlock1" fn in
1258 let m1 = (build_malloc (pointer_type context) "m1"
1259 (builder_at_end context bb1)) in
1260 ignore (build_free m1 (builder_at_end context bb1));
1261 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));
1262 ignore (build_unreachable (builder_at_end context bb1));
1263 end;
1265 group "indirectbr"; begin
1266 (* CHECK: indirectbr ptr blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]
1268 let bb1 = append_block context "IBRBlock1" fn in
1270 let bb2 = append_block context "IBRBlock2" fn in
1271 ignore (build_unreachable (builder_at_end context bb2));
1273 let bb3 = append_block context "IBRBlock3" fn in
1274 ignore (build_unreachable (builder_at_end context bb3));
1276 let addr = block_address fn bb2 in
1277 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in
1278 ignore (add_destination ibr bb2);
1279 ignore (add_destination ibr bb3)
1280 end;
1282 group "invoke"; begin
1283 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2
1284 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad
1286 let bb04 = append_block context "Bb04" fn in
1287 let b = builder_at_end context bb04 in
1288 ignore (build_invoke fty fn [| p1; p2 |] bb04 bblpad "build_invoke" b)
1289 end;
1291 group "unreachable"; begin
1292 (* CHECK: unreachable
1294 let bb06 = append_block context "Bb06" fn in
1295 let b = builder_at_end context bb06 in
1296 ignore (build_unreachable b)
1297 end;
1299 group "arithmetic"; begin
1300 let bb07 = append_block context "Bb07" fn in
1301 let b = builder_at_end context bb07 in
1303 (* CHECK: %build_add = add i32 %P1, %P2
1304 * CHECK: %build_nsw_add = add nsw i32 %P1, %P2
1305 * CHECK: %build_nuw_add = add nuw i32 %P1, %P2
1306 * CHECK: %build_fadd = fadd float %F1, %F2
1307 * CHECK: %build_sub = sub i32 %P1, %P2
1308 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2
1309 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2
1310 * CHECK: %build_fsub = fsub float %F1, %F2
1311 * CHECK: %build_mul = mul i32 %P1, %P2
1312 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2
1313 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2
1314 * CHECK: %build_fmul = fmul float %F1, %F2
1315 * CHECK: %build_udiv = udiv i32 %P1, %P2
1316 * CHECK: %build_sdiv = sdiv i32 %P1, %P2
1317 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2
1318 * CHECK: %build_fdiv = fdiv float %F1, %F2
1319 * CHECK: %build_urem = urem i32 %P1, %P2
1320 * CHECK: %build_srem = srem i32 %P1, %P2
1321 * CHECK: %build_frem = frem float %F1, %F2
1322 * CHECK: %build_shl = shl i32 %P1, %P2
1323 * CHECK: %build_lshl = lshr i32 %P1, %P2
1324 * CHECK: %build_ashl = ashr i32 %P1, %P2
1325 * CHECK: %build_and = and i32 %P1, %P2
1326 * CHECK: %build_or = or i32 %P1, %P2
1327 * CHECK: %build_xor = xor i32 %P1, %P2
1328 * CHECK: %build_neg = sub i32 0, %P1
1329 * CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1330 * CHECK: %build_nuw_neg = sub nuw i32 0, %P1
1331 * CHECK: %build_fneg = fneg float %F1
1332 * CHECK: %build_not = xor i32 %P1, -1
1333 * CHECK: %build_freeze = freeze i32 %P1
1335 ignore (build_add p1 p2 "build_add" b);
1336 ignore (build_nsw_add p1 p2 "build_nsw_add" b);
1337 ignore (build_nuw_add p1 p2 "build_nuw_add" b);
1338 ignore (build_fadd f1 f2 "build_fadd" b);
1339 ignore (build_sub p1 p2 "build_sub" b);
1340 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);
1341 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);
1342 ignore (build_fsub f1 f2 "build_fsub" b);
1343 ignore (build_mul p1 p2 "build_mul" b);
1344 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);
1345 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);
1346 ignore (build_fmul f1 f2 "build_fmul" b);
1347 ignore (build_udiv p1 p2 "build_udiv" b);
1348 ignore (build_sdiv p1 p2 "build_sdiv" b);
1349 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);
1350 ignore (build_fdiv f1 f2 "build_fdiv" b);
1351 ignore (build_urem p1 p2 "build_urem" b);
1352 ignore (build_srem p1 p2 "build_srem" b);
1353 ignore (build_frem f1 f2 "build_frem" b);
1354 ignore (build_shl p1 p2 "build_shl" b);
1355 ignore (build_lshr p1 p2 "build_lshl" b);
1356 ignore (build_ashr p1 p2 "build_ashl" b);
1357 ignore (build_and p1 p2 "build_and" b);
1358 ignore (build_or p1 p2 "build_or" b);
1359 ignore (build_xor p1 p2 "build_xor" b);
1360 ignore (build_neg p1 "build_neg" b);
1361 ignore (build_nsw_neg p1 "build_nsw_neg" b);
1362 ignore (build_nuw_neg p1 "build_nuw_neg" b);
1363 ignore (build_fneg f1 "build_fneg" b);
1364 ignore (build_not p1 "build_not" b);
1365 ignore (build_freeze p1 "build_freeze" b);
1366 ignore (build_unreachable b)
1367 end;
1369 group "memory"; begin
1370 let bb08 = append_block context "Bb08" fn in
1371 let b = builder_at_end context bb08 in
1373 (* CHECK: %build_alloca = alloca i32
1374 * CHECK: %build_array_alloca = alloca i32, i32 %P2
1375 * CHECK: %build_load = load volatile i32, ptr %build_array_alloca, align 4
1376 * CHECK: store volatile i32 %P2, ptr %build_alloca, align 4
1377 * CHECK: %build_gep = getelementptr i32, ptr %build_array_alloca, i32 %P2
1378 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32, ptr %build_array_alloca, i32 %P2
1379 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
1380 * CHECK: %build_atomicrmw = atomicrmw xchg ptr %p, i8 42 seq_cst
1382 let alloca = build_alloca i32_type "build_alloca" b in
1383 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
1385 let load = build_load i32_type array_alloca "build_load" b in
1386 ignore(set_alignment 4 load);
1387 ignore(set_volatile true load);
1388 insist(true = is_volatile load);
1389 insist(4 = alignment load);
1391 let store = build_store p2 alloca b in
1392 ignore(set_volatile true store);
1393 ignore(set_alignment 4 store);
1394 insist(true = is_volatile store);
1395 insist(4 = alignment store);
1396 ignore(build_gep i32_type array_alloca [| p2 |] "build_gep" b);
1397 ignore(build_in_bounds_gep i32_type array_alloca [| p2 |]
1398 "build_in_bounds_gep" b);
1400 let sty = struct_type context [| i32_type; i8_type |] in
1401 let alloca2 = build_alloca sty "build_alloca2" b in
1402 ignore(build_struct_gep sty alloca2 1 "build_struct_gep" b);
1404 let p = build_alloca i8_type "p" b in
1405 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)
1406 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"
1409 ignore(build_unreachable b)
1410 end;
1412 group "string"; begin
1413 let bb09 = append_block context "Bb09" fn in
1414 let b = builder_at_end context bb09 in
1415 let p = build_alloca (pointer_type context) "p" b in
1416 (* build_global_string is emitted above.
1417 * CHECK: store{{.*}}build_global_string1{{.*}}p
1418 * *)
1419 ignore (build_global_string "stringval" "build_global_string" b);
1420 let g = build_global_stringptr "stringval" "build_global_string1" b in
1421 ignore (build_store g p b);
1422 ignore(build_unreachable b);
1423 end;
1425 group "phi"; begin
1426 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2
1428 let b1 = append_block context "PhiBlock1" fn in
1429 let b2 = append_block context "PhiBlock2" fn in
1431 let jb = append_block context "PhiJoinBlock" fn in
1432 ignore (build_br jb (builder_at_end context b1));
1433 ignore (build_br jb (builder_at_end context b2));
1434 let at_jb = builder_at_end context jb in
1436 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
1437 insist ([(p1, b1)] = incoming phi);
1439 add_incoming (p2, b2) phi;
1440 insist ([(p1, b1); (p2, b2)] = incoming phi);
1442 (* CHECK: %PhiEmptyNode = phi i8
1444 let phi_empty = build_empty_phi i8_type "PhiEmptyNode" at_jb in
1445 insist ([] = incoming phi_empty);
1447 (* can't emit an empty phi to bitcode *)
1448 add_incoming (const_int i8_type 1, b1) phi_empty;
1449 add_incoming (const_int i8_type 2, b2) phi_empty;
1451 ignore (build_unreachable at_jb);
1454 (* End-of-file checks for things like metdata and attributes.
1455 * CHECK: !llvm.module.flags = !{!0}
1456 * CHECK: !0 = !{i32 1, !"Debug Info Version", i32 3}
1457 * CHECK: !1 = !{i32 1, !"metadata test"}
1461 (*===-- Memory Buffer -----------------------------------------------------===*)
1463 let test_memory_buffer () =
1464 group "memory buffer";
1465 let buf = MemoryBuffer.of_string "foobar" in
1466 insist ((MemoryBuffer.as_string buf) = "foobar")
1469 (*===-- Writer ------------------------------------------------------------===*)
1471 let test_writer () =
1472 group "valid";
1473 insist (match Llvm_analysis.verify_module m with
1474 | None -> true
1475 | Some msg -> prerr_string msg; false);
1477 group "writer";
1478 insist (write_bitcode_file m filename);
1480 dispose_module m
1483 (*===-- Driver ------------------------------------------------------------===*)
1485 let _ =
1486 suite "modules" test_modules;
1487 suite "contained types" test_contained_types;
1488 suite "pointer types" test_pointer_types;
1489 suite "conversion" test_conversion;
1490 suite "target" test_target;
1491 suite "constants" test_constants;
1492 suite "attributes" test_attributes;
1493 suite "global values" test_global_values;
1494 suite "global variables" test_global_variables;
1495 suite "uses" test_uses;
1496 suite "users" test_users;
1497 suite "aliases" test_aliases;
1498 suite "functions" test_functions;
1499 suite "params" test_params;
1500 suite "basic blocks" test_basic_blocks;
1501 suite "instructions" test_instructions;
1502 suite "builder" test_builder;
1503 suite "memory buffer" test_memory_buffer;
1504 suite "writer" test_writer; (* Keep this last; it disposes m. *)
1505 exit !exit_status