1499 lines · plain
1(* RUN: rm -rf %t && mkdir -p %t && cp %s %t/core.ml && cp %S/Utils/Testsuite.ml %t/Testsuite.ml2 * RUN: %ocamlc -g -w +A -package llvm.analysis -package llvm.bitwriter -I %t/ -linkpkg %t/Testsuite.ml %t/core.ml -o %t/executable3 * RUN: %t/executable %t/bitcode.bc4 * RUN: %ocamlopt -g -w +A -package llvm.analysis -package llvm.bitwriter -I %t/ -linkpkg %t/Testsuite.ml %t/core.ml -o %t/executable5 * RUN: %t/executable %t/bitcode.bc6 * RUN: llvm-dis < %t/bitcode.bc > %t/dis.ll7 * RUN: FileCheck %s < %t/dis.ll8 * Do a second pass for things that shouldn't be anywhere.9 * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t/dis.ll10 * XFAIL: vg_leak11 *)12 13(* Note: It takes several seconds for ocamlopt to link an executable with14 libLLVMCore.a, so it's better to write a big test than a bunch of15 little ones. *)16 17open Llvm18open Llvm_bitwriter19 20open Testsuite21let context = global_context ()22let i1_type = Llvm.i1_type context23let i8_type = Llvm.i8_type context24let i16_type = Llvm.i16_type context25let i32_type = Llvm.i32_type context26let i64_type = Llvm.i64_type context27let void_type = Llvm.void_type context28let float_type = Llvm.float_type context29let double_type = Llvm.double_type context30let fp128_type = Llvm.fp128_type context31 32(*===-- Fixture -----------------------------------------------------------===*)33 34let filename = Sys.argv.(1)35let m = create_module context filename36 37(*===-- Modules ----------------------------------------------------------===*)38 39let test_modules () =40 insist (module_context m = context)41 42(*===-- Contained types --------------------------------------------------===*)43 44let test_contained_types () =45 let ar = struct_type context [| i32_type; i8_type |] in46 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)49 50(*===-- Pointer types ----------------------------------------------------===*)51 52let test_pointer_types () =53 insist (TypeKind.Pointer = classify_type (pointer_type context));54 insist (0 = address_space (pointer_type context));55 insist (0 = address_space (qualified_pointer_type context 0));56 insist (1 = address_space (qualified_pointer_type context 1))57 58(*===-- Other types ------------------------------------------------------===*)59 60let test_other_types () =61 insist (TypeKind.Void = classify_type void_type);62 insist (TypeKind.Label = classify_type (label_type context));63 insist (TypeKind.X86_amx = classify_type (x86_amx_type context));64 insist (TypeKind.Token = classify_type (token_type context));65 insist (TypeKind.Metadata = classify_type (metadata_type context))66 67(*===-- Conversion --------------------------------------------------------===*)68 69let test_conversion () =70 insist ("i32" = (string_of_lltype i32_type));71 let c = const_int i32_type 42 in72 insist ("i32 42" = (string_of_llvalue c))73 74 75(*===-- Target ------------------------------------------------------------===*)76 77let test_target () =78 begin group "triple";79 let trip = "i686-apple-darwin8" in80 set_target_triple trip m;81 insist (trip = target_triple m)82 end;83 84 begin group "layout";85 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" in86 set_data_layout layout m;87 insist (layout = data_layout m)88 end89 (* 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"90 * CHECK: target triple = "i686-apple-darwin8"91 *)92 93 94(*===-- Constants ---------------------------------------------------------===*)95 96let test_constants () =97 (* CHECK: const_int{{.*}}i32{{.*}}-198 *)99 group "int";100 let c = const_int i32_type (-1) in101 ignore (define_global "const_int" c m);102 insist (i32_type = type_of c);103 insist (is_constant c);104 insist (Some (-1L) = int64_of_const c);105 106 (* CHECK: const_sext_int{{.*}}i64{{.*}}-1107 *)108 group "sext int";109 let c = const_int i64_type (-1) in110 ignore (define_global "const_sext_int" c m);111 insist (i64_type = type_of c);112 insist (Some (-1L) = int64_of_const c);113 114 (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295115 *)116 group "zext int64";117 let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in118 ignore (define_global "const_zext_int64" c m);119 insist (i64_type = type_of c);120 insist (Some 4294967295L = int64_of_const c);121 122 (* CHECK: const_int_string{{.*}}i32{{.*}}-1123 *)124 group "int string";125 let c = const_int_of_string i32_type "-1" 10 in126 ignore (define_global "const_int_string" c m);127 insist (i32_type = type_of c);128 insist (None = (string_of_const c));129 insist (None = float_of_const c);130 insist (Some (-1L) = int64_of_const c);131 132 (* CHECK: const_int64{{.*}}i64{{.*}}9223372036854775807133 *)134 group "max int64";135 let c = const_of_int64 i64_type 9223372036854775807L true in136 ignore (define_global "const_int64" c m) ;137 insist (i64_type = type_of c);138 insist (Some 9223372036854775807L = int64_of_const c);139 140 if Sys.word_size = 64; then begin141 group "long int";142 let c = const_int i64_type (1 lsl 61) in143 insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false)144 end;145 146 (* CHECK: @const_string = global {{.*}}c"cruel\00world"147 *)148 group "string";149 let c = const_string context "cruel\000world" in150 ignore (define_global "const_string" c m);151 insist ((array_type i8_type 11) = type_of c);152 insist ((Some "cruel\000world") = (string_of_const c));153 154 (* CHECK: const_stringz{{.*}}"hi\00again\00"155 *)156 group "stringz";157 let c = const_stringz context "hi\000again" in158 ignore (define_global "const_stringz" c m);159 insist ((array_type i8_type 9) = type_of c);160 161 (* CHECK: const_single{{.*}}2.75162 * CHECK: const_double{{.*}}3.1459163 * CHECK: const_double_string{{.*}}2164 * CHECK: const_fake_fp128{{.*}}0xL00000000000000004000000000000000165 * CHECK: const_fp128_string{{.*}}0xLF3CB1CCF26FBC178452FB4EC7F91973F166 *)167 begin group "real";168 let cs = const_float float_type 2.75 in169 ignore (define_global "const_single" cs m);170 insist (float_type = type_of cs);171 insist (float_of_const cs = Some 2.75);172 173 let cd = const_float double_type 3.1459 in174 ignore (define_global "const_double" cd m);175 insist (double_type = type_of cd);176 insist (float_of_const cd = Some 3.1459);177 178 let cd = const_float_of_string double_type "2" in179 ignore (define_global "const_double_string" cd m);180 insist (double_type = type_of cd);181 insist (float_of_const cd = Some 2.);182 183 let cd = const_float fp128_type 2. in184 ignore (define_global "const_fake_fp128" cd m);185 insist (fp128_type = type_of cd);186 insist (float_of_const cd = Some 2.);187 188 let cd = const_float_of_string fp128_type "1e400" in189 ignore (define_global "const_fp128_string" cd m);190 insist (fp128_type = type_of cd);191 insist (float_of_const cd = None);192 end;193 194 let one = const_int i16_type 1 in195 let two = const_int i16_type 2 in196 let three = const_int i32_type 3 in197 let four = const_int i32_type 4 in198 199 (* CHECK: const_array{{.*}}[i32 3, i32 4]200 *)201 group "array";202 let c = const_array i32_type [| three; four |] in203 ignore (define_global "const_array" c m);204 insist ((array_type i32_type 2) = (type_of c));205 insist (element_type (type_of c) = i32_type);206 insist (Some three = (aggregate_element c 0));207 insist (Some four = (aggregate_element c 1));208 insist (None = (aggregate_element c 2));209 210 (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>211 *)212 group "vector";213 let c = const_vector [| one; two; one; two;214 one; two; one; two |] in215 ignore (define_global "const_vector" c m);216 insist ((vector_type i16_type 8) = (type_of c));217 insist (element_type (type_of c) = i16_type);218 219 (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4220 *)221 group "structure";222 let c = const_struct context [| one; two; three; four |] in223 ignore (define_global "const_structure" c m);224 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])225 = (type_of c));226 227 (* CHECK: const_null{{.*}}zeroinit228 *)229 group "null";230 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;231 double_type |]) in232 ignore (define_global "const_null" c m);233 234 (* CHECK: const_all_ones{{.*}}-1235 *)236 group "all ones";237 let c = const_all_ones i64_type in238 ignore (define_global "const_all_ones" c m);239 240 group "pointer null"; begin241 (* CHECK: const_pointer_null = global ptr null242 *)243 let c = const_pointer_null (pointer_type context) in244 ignore (define_global "const_pointer_null" c m);245 end;246 247 (* CHECK: const_undef{{.*}}undef248 *)249 group "undef";250 let c = undef i1_type in251 ignore (define_global "const_undef" c m);252 insist (i1_type = type_of c);253 insist (is_undef c);254 255 (* CHECK: const_poison{{.*}}poison256 *)257 group "poison";258 let c = poison i1_type in259 ignore (define_global "const_poison" c m);260 insist (i1_type = type_of c);261 insist (is_poison c);262 263 group "constant arithmetic";264 (* CHECK: @const_neg = global i64 sub265 * CHECK: @const_nsw_neg = global i64 sub nsw266 * CHECK: @const_nuw_neg = global i64 sub267 * CHECK: @const_not = global i64 xor268 * CHECK: @const_add = global i64 add269 * CHECK: @const_nsw_add = global i64 add nsw270 * CHECK: @const_nuw_add = global i64 add nuw271 * CHECK: @const_sub = global i64 sub272 * CHECK: @const_nsw_sub = global i64 sub nsw273 * CHECK: @const_nuw_sub = global i64 sub nuw274 * CHECK: @const_xor = global i64 xor275 *)276 let void_ptr = pointer_type context in277 let five = const_int i64_type 5 in278 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in279 let foldbomb = const_ptrtoint foldbomb_gv i64_type in280 ignore (define_global "const_neg" (const_neg foldbomb) m);281 ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);282 ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);283 ignore (define_global "const_not" (const_not foldbomb) m);284 ignore (define_global "const_add" (const_add foldbomb five) m);285 ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);286 ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);287 ignore (define_global "const_sub" (const_sub foldbomb five) m);288 ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);289 ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);290 ignore (define_global "const_xor" (const_xor foldbomb five) m);291 292 group "constant casts";293 (* CHECK: const_trunc{{.*}}trunc294 * CHECK: const_ptrtoint{{.*}}ptrtoint295 * CHECK: const_inttoptr{{.*}}inttoptr296 * CHECK: const_bitcast{{.*}}bitcast297 *)298 let i128_type = integer_type context 128 in299 ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)300 i8_type) m);301 ignore (define_global "const_ptrtoint" (const_ptrtoint302 (const_gep i8_type (const_null (pointer_type context))303 [| const_int i32_type 1 |])304 i32_type) m);305 ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)306 void_ptr) m);307 ignore (define_global "const_bitcast" (const_bitcast foldbomb double_type) m);308 309 group "misc constants";310 (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null311 * CHECK: const_gep{{.*}}getelementptr312 * CHECK: const_extractelement{{.*}}extractelement313 * CHECK: const_insertelement{{.*}}insertelement314 * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>315 *)316 ignore (define_global "const_size_of" (size_of (pointer_type context)) m);317 ignore (define_global "const_gep" (const_gep i8_type foldbomb_gv [| five |])318 m);319 let zero = const_int i32_type 0 in320 let one = const_int i32_type 1 in321 ignore (define_global "const_extractelement" (const_extractelement322 (const_vector [| zero; one; zero; one |])323 (const_trunc foldbomb i32_type)) m);324 ignore (define_global "const_insertelement" (const_insertelement325 (const_vector [| zero; one; zero; one |])326 zero (const_trunc foldbomb i32_type)) m);327 ignore (define_global "const_shufflevector" (const_shufflevector328 (const_vector [| zero; one |])329 (const_vector [| one; zero |])330 (const_vector [| const_int i32_type 0; const_int i32_type 1;331 const_int i32_type 2; const_int i32_type 3 |])) m);332 333 group "asm"; begin334 let ft = function_type void_type [| i32_type; i32_type; i32_type |] in335 ignore (const_inline_asm336 ft337 ""338 "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"339 true340 false)341 end;342 343 group "recursive struct"; begin344 let nsty = named_struct_type context "rec" in345 let pty = pointer_type context in346 struct_set_body nsty [| i32_type; pty |] false;347 let elts = [| const_int i32_type 4; const_pointer_null pty |] in348 let grec_init = const_named_struct nsty elts in349 ignore (define_global "grec" grec_init m);350 ignore (string_of_lltype nsty);351 end352 353 354(*===-- Attributes --------------------------------------------------------===*)355 356let test_attributes () =357 group "enum attrs";358 let nonnull_kind = enum_attr_kind "nonnull" in359 let dereferenceable_kind = enum_attr_kind "dereferenceable" in360 insist (nonnull_kind = (enum_attr_kind "nonnull"));361 insist (nonnull_kind <> dereferenceable_kind);362 363 let nonnull =364 create_enum_attr context "nonnull" 0L in365 let dereferenceable_4 =366 create_enum_attr context "dereferenceable" 4L in367 let dereferenceable_8 =368 create_enum_attr context "dereferenceable" 8L in369 insist (nonnull <> dereferenceable_4);370 insist (dereferenceable_4 <> dereferenceable_8);371 insist (nonnull = (create_enum_attr context "nonnull" 0L));372 insist ((repr_of_attr nonnull) =373 AttrRepr.Enum(nonnull_kind, 0L));374 insist ((repr_of_attr dereferenceable_4) =375 AttrRepr.Enum(dereferenceable_kind, 4L));376 insist ((attr_of_repr context (repr_of_attr nonnull)) =377 nonnull);378 insist ((attr_of_repr context (repr_of_attr dereferenceable_4)) =379 dereferenceable_4);380 381 group "string attrs";382 let foo_bar = create_string_attr context "foo" "bar" in383 let foo_baz = create_string_attr context "foo" "baz" in384 insist (foo_bar <> foo_baz);385 insist (foo_bar = (create_string_attr context "foo" "bar"));386 insist ((repr_of_attr foo_bar) = AttrRepr.String("foo", "bar"));387 insist ((attr_of_repr context (repr_of_attr foo_bar)) = foo_bar);388 ()389 390(*===-- Global Values -----------------------------------------------------===*)391 392let test_global_values () =393 let (++) x f = f x; x in394 let zero32 = const_null i32_type in395 396 (* CHECK: GVal01397 *)398 group "naming";399 let g = define_global "TEMPORARY" zero32 m in400 insist ("TEMPORARY" = value_name g);401 set_value_name "GVal01" g;402 insist ("GVal01" = value_name g);403 404 (* CHECK: GVal02{{.*}}linkonce405 *)406 group "linkage";407 let g = define_global "GVal02" zero32 m ++408 set_linkage Linkage.Link_once in409 insist (Linkage.Link_once = linkage g);410 411 (* CHECK: GVal03{{.*}}Hanalei412 *)413 group "section";414 let g = define_global "GVal03" zero32 m ++415 set_section "Hanalei" in416 insist ("Hanalei" = section g);417 418 (* CHECK: GVal04{{.*}}hidden419 *)420 group "visibility";421 let g = define_global "GVal04" zero32 m ++422 set_visibility Visibility.Hidden in423 insist (Visibility.Hidden = visibility g);424 425 (* CHECK: GVal05{{.*}}align 128426 *)427 group "alignment";428 let g = define_global "GVal05" zero32 m ++429 set_alignment 128 in430 insist (128 = alignment g);431 432 (* CHECK: GVal06{{.*}}dllexport433 *)434 group "dll_storage_class";435 let g = define_global "GVal06" zero32 m ++436 set_dll_storage_class DLLStorageClass.DLLExport in437 insist (DLLStorageClass.DLLExport = dll_storage_class g);438 439 (* CHECK: GVal07{{.*}}!test !0440 * See metadata check at the end of the file.441 *)442 group "metadata";443 let g = define_global "GVal07" zero32 m in444 let md_string = mdstring context "global test metadata" in445 let md_node = mdnode context [| zero32; md_string |] |> value_as_metadata in446 let mdkind_test = mdkind_id context "test" in447 global_set_metadata g mdkind_test md_node;448 let md' = global_copy_all_metadata g in449 insist (md' = [| mdkind_test, md_node |])450 451(*===-- Global Variables --------------------------------------------------===*)452 453let test_global_variables () =454 let (++) x f = f x; x in455 let forty_two32 = const_int i32_type 42 in456 457 group "declarations"; begin458 (* CHECK: @GVar01 = external global i32459 * CHECK: @QGVar01 = external addrspace(3) global i32460 *)461 insist (None == lookup_global "GVar01" m);462 let g = declare_global i32_type "GVar01" m in463 insist (is_declaration g);464 insist (pointer_type context ==465 type_of (declare_global float_type "GVar01" m));466 insist (g == declare_global i32_type "GVar01" m);467 insist (match lookup_global "GVar01" m with Some x -> x = g468 | None -> false);469 470 insist (None == lookup_global "QGVar01" m);471 let g = declare_qualified_global i32_type "QGVar01" 3 m in472 insist (is_declaration g);473 insist (qualified_pointer_type context 3 ==474 type_of (declare_qualified_global float_type "QGVar01" 3 m));475 insist (g == declare_qualified_global i32_type "QGVar01" 3 m);476 insist (match lookup_global "QGVar01" m with Some x -> x = g477 | None -> false);478 end;479 480 group "definitions"; begin481 (* CHECK: @GVar02 = global i32 42482 * CHECK: @GVar03 = global i32 42483 * CHECK: @QGVar02 = addrspace(3) global i32 42484 * CHECK: @QGVar03 = addrspace(3) global i32 42485 *)486 let g = define_global "GVar02" forty_two32 m in487 let g2 = declare_global i32_type "GVar03" m ++488 set_initializer forty_two32 in489 insist (not (is_declaration g));490 insist (not (is_declaration g2));491 insist ((global_initializer g) = (global_initializer g2));492 493 let g = define_qualified_global "QGVar02" forty_two32 3 m in494 let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++495 set_initializer forty_two32 in496 insist (not (is_declaration g));497 insist (not (is_declaration g2));498 insist ((global_initializer g) = (global_initializer g2));499 end;500 501 (* CHECK: GVar04{{.*}}thread_local502 *)503 group "threadlocal";504 let g = define_global "GVar04" forty_two32 m ++505 set_thread_local true in506 insist (is_thread_local g);507 508 (* CHECK: GVar05{{.*}}thread_local(initialexec)509 *)510 group "threadlocal_mode";511 let g = define_global "GVar05" forty_two32 m ++512 set_thread_local_mode ThreadLocalMode.InitialExec in513 insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);514 515 (* CHECK: GVar06{{.*}}externally_initialized516 *)517 group "externally_initialized";518 let g = define_global "GVar06" forty_two32 m ++519 set_externally_initialized true in520 insist (is_externally_initialized g);521 522 (* CHECK-NOWHERE-NOT: GVar07523 *)524 group "delete";525 let g = define_global "GVar07" forty_two32 m in526 delete_global g;527 528 (* CHECK: ConstGlobalVar{{.*}}constant529 *)530 group "constant";531 let g = define_global "ConstGlobalVar" forty_two32 m in532 insist (not (is_global_constant g));533 set_global_constant true g;534 insist (is_global_constant g);535 536 begin group "iteration";537 let m = create_module context "temp" in538 539 insist (get_module_identifier m = "temp");540 set_module_identifer m "temp2";541 insist (get_module_identifier m = "temp2");542 543 insist (At_end m = global_begin m);544 insist (At_start m = global_end m);545 546 let g1 = declare_global i32_type "One" m in547 let g2 = declare_global i32_type "Two" m in548 549 insist (Before g1 = global_begin m);550 insist (Before g2 = global_succ g1);551 insist (At_end m = global_succ g2);552 553 insist (After g2 = global_end m);554 insist (After g1 = global_pred g2);555 insist (At_start m = global_pred g1);556 557 let lf s x = s ^ "->" ^ value_name x in558 insist ("->One->Two" = fold_left_globals lf "" m);559 560 let rf x s = value_name x ^ "<-" ^ s in561 insist ("One<-Two<-" = fold_right_globals rf m "");562 563 dispose_module m564 end565 566(* String globals built below are emitted here.567 * CHECK: build_global_string{{.*}}stringval568 *)569 570 571(*===-- Uses --------------------------------------------------------------===*)572 573let test_uses () =574 let ty = function_type i32_type [| i32_type; i32_type |] in575 let fn = define_function "use_function" ty m in576 let b = builder_at_end context (entry_block fn) in577 578 let p1 = param fn 0 in579 let p2 = param fn 1 in580 let v1 = build_add p1 p2 "v1" b in581 let v2 = build_add p1 v1 "v2" b in582 let _ = build_add v1 v2 "v3" b in583 584 let lf s u = value_name (user u) ^ "->" ^ s in585 insist ("v2->v3->" = fold_left_uses lf "" v1);586 let rf u s = value_name (user u) ^ "<-" ^ s in587 insist ("v3<-v2<-" = fold_right_uses rf v1 "");588 589 let lf s u = value_name (used_value u) ^ "->" ^ s in590 insist ("v1->v1->" = fold_left_uses lf "" v1);591 592 let rf u s = value_name (used_value u) ^ "<-" ^ s in593 insist ("v1<-v1<-" = fold_right_uses rf v1 "");594 595 ignore (build_unreachable b)596 597 598(*===-- Users -------------------------------------------------------------===*)599 600let test_users () =601 let ty = function_type i32_type [| i32_type; i32_type |] in602 let fn = define_function "user_function" ty m in603 let b = builder_at_end context (entry_block fn) in604 605 let p1 = param fn 0 in606 let p2 = param fn 1 in607 let a3 = build_alloca i32_type "user_alloca" b in608 let p3 = build_load i32_type a3 "user_load" b in609 let i = build_add p1 p2 "sum" b in610 611 insist ((num_operands i) = 2);612 insist ((operand i 0) = p1);613 insist ((operand i 1) = p2);614 615 set_operand i 1 p3;616 insist ((operand i 1) != p2);617 insist ((operand i 1) = p3);618 619 ignore (build_unreachable b)620 621 622(*===-- Aliases -----------------------------------------------------------===*)623 624let test_aliases () =625 (* CHECK: @alias = alias i32, ptr @aliasee626 *)627 let forty_two32 = const_int i32_type 42 in628 let v = define_global "aliasee" forty_two32 m in629 ignore (add_alias m i32_type 0 v "alias")630 631 632(*===-- Functions ---------------------------------------------------------===*)633 634let test_functions () =635 let ty = function_type i32_type [| i32_type; i64_type |] in636 let ty2 = function_type i8_type [| i8_type; i64_type |] in637 638 (* CHECK: declare i32 @Fn1(i32, i64)639 *)640 begin group "declare";641 insist (None = lookup_function "Fn1" m);642 let fn = declare_function "Fn1" ty m in643 insist (pointer_type context = type_of fn);644 insist (is_declaration fn);645 insist (0 = Array.length (basic_blocks fn));646 insist (pointer_type context == type_of (declare_function "Fn1" ty2 m));647 insist (fn == declare_function "Fn1" ty m);648 insist (None <> lookup_function "Fn1" m);649 insist (match lookup_function "Fn1" m with Some x -> x = fn650 | None -> false);651 insist (m == global_parent fn)652 end;653 654 (* CHECK-NOWHERE-NOT: Fn2655 *)656 group "delete";657 let fn = declare_function "Fn2" ty m in658 delete_function fn;659 660 (* CHECK: define{{.*}}Fn3661 *)662 group "define";663 let fn = define_function "Fn3" ty m in664 insist (not (is_declaration fn));665 insist (1 = Array.length (basic_blocks fn));666 ignore (build_unreachable (builder_at_end context (entry_block fn)));667 668 (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2669 *)670 group "params";671 let fn = define_function "Fn4" ty m in672 let params = params fn in673 insist (2 = Array.length params);674 insist (params.(0) = param fn 0);675 insist (params.(1) = param fn 1);676 insist (i32_type = type_of params.(0));677 insist (i64_type = type_of params.(1));678 set_value_name "Param1" params.(0);679 set_value_name "Param2" params.(1);680 ignore (build_unreachable (builder_at_end context (entry_block fn)));681 682 (* CHECK: fastcc{{.*}}Fn5683 *)684 group "callconv";685 let fn = define_function "Fn5" ty m in686 insist (CallConv.c = function_call_conv fn);687 set_function_call_conv CallConv.fast fn;688 insist (CallConv.fast = function_call_conv fn);689 ignore (build_unreachable (builder_at_end context (entry_block fn)));690 691 begin group "gc";692 (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack693 *)694 let fn = define_function "Fn6" ty m in695 insist (None = gc fn);696 set_gc (Some "ocaml") fn;697 insist (Some "ocaml" = gc fn);698 set_gc None fn;699 insist (None = gc fn);700 set_gc (Some "shadowstack") fn;701 ignore (build_unreachable (builder_at_end context (entry_block fn)));702 end;703 704 begin group "iteration";705 let m = create_module context "temp" in706 707 insist (At_end m = function_begin m);708 insist (At_start m = function_end m);709 710 let f1 = define_function "One" ty m in711 let f2 = define_function "Two" ty m in712 713 insist (Before f1 = function_begin m);714 insist (Before f2 = function_succ f1);715 insist (At_end m = function_succ f2);716 717 insist (After f2 = function_end m);718 insist (After f1 = function_pred f2);719 insist (At_start m = function_pred f1);720 721 let lf s x = s ^ "->" ^ value_name x in722 insist ("->One->Two" = fold_left_functions lf "" m);723 724 let rf x s = value_name x ^ "<-" ^ s in725 insist ("One<-Two<-" = fold_right_functions rf m "");726 727 dispose_module m728 end729 730 731(*===-- Params ------------------------------------------------------------===*)732 733let test_params () =734 begin group "iteration";735 let m = create_module context "temp" in736 737 let vf = define_function "void" (function_type void_type [| |]) m in738 739 insist (At_end vf = param_begin vf);740 insist (At_start vf = param_end vf);741 742 let ty = function_type void_type [| i32_type; i32_type |] in743 let f = define_function "f" ty m in744 let p1 = param f 0 in745 let p2 = param f 1 in746 set_value_name "One" p1;747 set_value_name "Two" p2;748 749 insist (Before p1 = param_begin f);750 insist (Before p2 = param_succ p1);751 insist (At_end f = param_succ p2);752 753 insist (After p2 = param_end f);754 insist (After p1 = param_pred p2);755 insist (At_start f = param_pred p1);756 757 let lf s x = s ^ "->" ^ value_name x in758 insist ("->One->Two" = fold_left_params lf "" f);759 760 let rf x s = value_name x ^ "<-" ^ s in761 insist ("One<-Two<-" = fold_right_params rf f "");762 763 dispose_module m764 end765 766 767(*===-- Basic Blocks ------------------------------------------------------===*)768 769let test_basic_blocks () =770 let ty = function_type void_type [| |] in771 772 (* CHECK: Bb1773 *)774 group "entry";775 let fn = declare_function "X" ty m in776 let bb = append_block context "Bb1" fn in777 insist (bb = entry_block fn);778 ignore (build_unreachable (builder_at_end context bb));779 780 (* CHECK-NOWHERE-NOT: Bb2781 *)782 group "delete";783 let fn = declare_function "X2" ty m in784 let bb = append_block context "Bb2" fn in785 delete_block bb;786 787 group "insert";788 let fn = declare_function "X3" ty m in789 let bbb = append_block context "b" fn in790 let bba = insert_block context "a" bbb in791 insist ([| bba; bbb |] = basic_blocks fn);792 ignore (build_unreachable (builder_at_end context bba));793 ignore (build_unreachable (builder_at_end context bbb));794 795 (* CHECK: Bb3796 *)797 group "name/value";798 let fn = define_function "X4" ty m in799 let bb = entry_block fn in800 ignore (build_unreachable (builder_at_end context bb));801 let bbv = value_of_block bb in802 set_value_name "Bb3" bbv;803 insist ("Bb3" = value_name bbv);804 805 group "casts";806 let fn = define_function "X5" ty m in807 let bb = entry_block fn in808 ignore (build_unreachable (builder_at_end context bb));809 insist (bb = block_of_value (value_of_block bb));810 insist (value_is_block (value_of_block bb));811 insist (not (value_is_block (const_null i32_type)));812 813 begin group "iteration";814 let m = create_module context "temp" in815 let f = declare_function "Temp" (function_type i32_type [| |]) m in816 817 insist (At_end f = block_begin f);818 insist (At_start f = block_end f);819 820 let b1 = append_block context "One" f in821 let b2 = append_block context "Two" f in822 823 insist (Before b1 = block_begin f);824 insist (Before b2 = block_succ b1);825 insist (At_end f = block_succ b2);826 827 insist (After b2 = block_end f);828 insist (After b1 = block_pred b2);829 insist (At_start f = block_pred b1);830 831 let lf s x = s ^ "->" ^ value_name (value_of_block x) in832 insist ("->One->Two" = fold_left_blocks lf "" f);833 834 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in835 insist ("One<-Two<-" = fold_right_blocks rf f "");836 837 dispose_module m838 end839 840 841(*===-- Instructions ------------------------------------------------------===*)842 843let test_instructions () =844 begin group "iteration";845 let m = create_module context "temp" in846 let fty = function_type void_type [| i32_type; i32_type |] in847 let f = define_function "f" fty m in848 let bb = entry_block f in849 let b = builder_at context (At_end bb) in850 851 insist (At_end bb = instr_begin bb);852 insist (At_start bb = instr_end bb);853 854 let i1 = build_add (param f 0) (param f 1) "One" b in855 let i2 = build_sub (param f 0) (param f 1) "Two" b in856 857 insist (Before i1 = instr_begin bb);858 insist (Before i2 = instr_succ i1);859 insist (At_end bb = instr_succ i2);860 861 insist (After i2 = instr_end bb);862 insist (After i1 = instr_pred i2);863 insist (At_start bb = instr_pred i1);864 865 let lf s x = s ^ "->" ^ value_name x in866 insist ("->One->Two" = fold_left_instrs lf "" bb);867 868 let rf x s = value_name x ^ "<-" ^ s in869 insist ("One<-Two<-" = fold_right_instrs rf bb "");870 871 dispose_module m872 end;873 874 group "clone instr";875 begin876 (* CHECK: %clone = add i32 %0, 2877 *)878 let fty = function_type void_type [| i32_type |] in879 let fn = define_function "BuilderParent" fty m in880 let bb = entry_block fn in881 let b = builder_at_end context bb in882 let p = param fn 0 in883 let sum = build_add p p "sum" b in884 let y = const_int i32_type 2 in885 let clone = instr_clone sum in886 set_operand clone 0 p;887 set_operand clone 1 y;888 insert_into_builder clone "clone" b;889 ignore (build_ret_void b)890 end891 892 893(*===-- Builder -----------------------------------------------------------===*)894 895let test_builder () =896 let (++) x f = f x; x in897 898 begin group "parent";899 insist (try900 ignore (insertion_block (builder context));901 false902 with Not_found ->903 true);904 905 let fty = function_type void_type [| i32_type |] in906 let fn = define_function "BuilderParent" fty m in907 let bb = entry_block fn in908 let b = builder_at_end context bb in909 let p = param fn 0 in910 let sum = build_add p p "sum" b in911 ignore (build_ret_void b);912 913 insist (fn = block_parent bb);914 insist (fn = param_parent p);915 insist (bb = instr_parent sum);916 insist (bb = insertion_block b)917 end;918 919 group "ret void";920 begin921 (* CHECK: ret void922 *)923 let fty = function_type void_type [| |] in924 let fn = declare_function "X6" fty m in925 let b = builder_at_end context (append_block context "Bb01" fn) in926 ignore (build_ret_void b)927 end;928 929 group "ret aggregate";930 begin931 (* CHECK: ret { i8, i64 } { i8 4, i64 5 }932 *)933 let sty = struct_type context [| i8_type; i64_type |] in934 let fty = function_type sty [| |] in935 let fn = declare_function "XA6" fty m in936 let b = builder_at_end context (append_block context "Bb01" fn) in937 let agg = [| const_int i8_type 4; const_int i64_type 5 |] in938 ignore (build_aggregate_ret agg b)939 end;940 941 (* The rest of the tests will use one big function. *)942 let fty = function_type i32_type [| i32_type; i32_type |] in943 let fn = define_function "X7" fty m in944 let atentry = builder_at_end context (entry_block fn) in945 let p1 = param fn 0 ++ set_value_name "P1" in946 let p2 = param fn 1 ++ set_value_name "P2" in947 let f1 = build_uitofp p1 float_type "F1" atentry in948 let f2 = build_uitofp p2 float_type "F2" atentry in949 950 let bb00 = append_block context "Bb00" fn in951 ignore (build_unreachable (builder_at_end context bb00));952 953 group "function attribute";954 begin955 let signext = create_enum_attr context "signext" 0L in956 let zeroext = create_enum_attr context "zeroext" 0L in957 let noalias = create_enum_attr context "noalias" 0L in958 let nounwind = create_enum_attr context "nounwind" 0L in959 let no_sse = create_string_attr context "no-sse" "" in960 961 add_function_attr fn signext (AttrIndex.Param 0);962 add_function_attr fn noalias (AttrIndex.Param 1);963 insist ((function_attrs fn (AttrIndex.Param 1)) = [|noalias|]);964 remove_enum_function_attr fn (enum_attr_kind "noalias") (AttrIndex.Param 1);965 add_function_attr fn no_sse (AttrIndex.Param 1);966 insist ((function_attrs fn (AttrIndex.Param 1)) = [|no_sse|]);967 remove_string_function_attr fn "no-sse" (AttrIndex.Param 1);968 insist ((function_attrs fn (AttrIndex.Param 1)) = [||]);969 add_function_attr fn nounwind AttrIndex.Function;970 add_function_attr fn zeroext AttrIndex.Return;971 972 (* CHECK: define zeroext i32 @X7(i32 signext %P1, i32 %P2)973 *)974 end;975 976 group "casts"; begin977 let void_ptr = pointer_type context in978 979 (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8980 * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8981 * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8982 * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32983 * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32984 * CHECK-DAG: %build_sext = sext i32 %build_zext to i64985 * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64986 * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64987 * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float988 * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double989 * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32990 * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64991 * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float992 * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float993 * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double994 * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double995 * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to ptr996 * CHECK-DAG: %build_ptrtoint = ptrtoint ptr %build_inttoptr to i64997 * CHECK-DAG: %build_ptrtoint2 = ptrtoint ptr %build_inttoptr to i64998 * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double999 * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double1000 * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double1001 * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double1002 *)1003 let inst28 = build_trunc p1 i8_type "build_trunc" atentry in1004 let inst29 = build_zext inst28 i32_type "build_zext" atentry in1005 let inst30 = build_sext inst29 i64_type "build_sext" atentry in1006 let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in1007 let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in1008 ignore(build_fptoui inst31 i32_type "build_fptoui" atentry);1009 ignore(build_fptosi inst32 i64_type "build_fptosi" atentry);1010 let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in1011 ignore(build_fpext inst35 double_type "build_fpext" atentry);1012 let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in1013 let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in1014 ignore(build_bitcast inst38 double_type "build_bitcast" atentry);1015 ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry);1016 ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry);1017 ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry);1018 ignore(build_pointercast inst37 (pointer_type context) "build_pointercast" atentry);1019 1020 ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry);1021 ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry);1022 ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry);1023 ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry);1024 ignore(build_intcast inst29 i64_type "build_sext3" atentry);1025 ignore(build_intcast p1 i8_type "build_trunc3" atentry);1026 ignore(build_fpcast inst35 double_type "build_fpext2" atentry);1027 ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry);1028 end;1029 1030 group "comparisons"; begin1031 (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P21032 * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P11033 * CHECK: %build_fcmp_false = fcmp false float %F1, %F21034 * CHECK: %build_fcmp_true = fcmp true float %F2, %F11035 * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null1036 * CHECK: %build_is_not_null = icmp ne ptr %X1, null1037 * CHECK: %build_ptrdiff1038 *)1039 let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in1040 insist (Some Icmp.Ne = icmp_predicate c);1041 insist (None = fcmp_predicate c);1042 1043 let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in1044 insist (Some Icmp.Sle = icmp_predicate c);1045 insist (None = fcmp_predicate c);1046 1047 let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in1048 (* insist (Some Fcmp.False = fcmp_predicate c); *)1049 insist (None = icmp_predicate c);1050 1051 let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in1052 (* insist (Some Fcmp.True = fcmp_predicate c); *)1053 insist (None = icmp_predicate c);1054 1055 let g0 = declare_global (pointer_type context) "g0" m in1056 let g1 = declare_global (pointer_type context) "g1" m in1057 let p0 = build_load (pointer_type context) g0 "X0" atentry in1058 let p1 = build_load (pointer_type context) g1 "X1" atentry in1059 ignore (build_is_null p0 "build_is_null" atentry);1060 ignore (build_is_not_null p1 "build_is_not_null" atentry);1061 ignore (build_ptrdiff i8_type p1 p0 "build_ptrdiff" atentry);1062 end;1063 1064 group "miscellaneous"; begin1065 (* CHECK: %build_call = tail call cc63 zeroext i32 @{{.*}}(i32 signext %P2, i32 %P1)1066 * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P21067 * CHECK: %build_va_arg = va_arg ptr null, i321068 * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P21069 * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P21070 * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> <i32 1, i32 1, i32 0, i32 0>1071 * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 01072 * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 11073 *)1074 let ci = build_call fty fn [| p2; p1 |] "build_call" atentry in1075 insist (CallConv.c = instruction_call_conv ci);1076 set_instruction_call_conv 63 ci;1077 insist (63 = instruction_call_conv ci);1078 insist (not (is_tail_call ci));1079 set_tail_call true ci;1080 insist (is_tail_call ci);1081 1082 let signext = create_enum_attr context "signext" 0L in1083 let zeroext = create_enum_attr context "zeroext" 0L in1084 let noalias = create_enum_attr context "noalias" 0L in1085 let noreturn = create_enum_attr context "noreturn" 0L in1086 let no_sse = create_string_attr context "no-sse" "" in1087 1088 add_call_site_attr ci signext (AttrIndex.Param 0);1089 add_call_site_attr ci noalias (AttrIndex.Param 1);1090 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|noalias|]);1091 remove_enum_call_site_attr ci (enum_attr_kind "noalias") (AttrIndex.Param 1);1092 add_call_site_attr ci no_sse (AttrIndex.Param 1);1093 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [|no_sse|]);1094 remove_string_call_site_attr ci "no-sse" (AttrIndex.Param 1);1095 insist ((call_site_attrs ci (AttrIndex.Param 1)) = [||]);1096 add_call_site_attr ci noreturn AttrIndex.Function;1097 add_call_site_attr ci zeroext AttrIndex.Return;1098 1099 let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in1100 ignore (build_select inst46 p1 p2 "build_select" atentry);1101 ignore (build_va_arg1102 (const_null (pointer_type context))1103 i32_type "build_va_arg" atentry);1104 1105 (* Set up some vector vregs. *)1106 let one = const_int i32_type 1 in1107 let zero = const_int i32_type 0 in1108 let t1 = const_vector [| one; zero; one; zero |] in1109 let t2 = const_vector [| zero; one; zero; one |] in1110 let t3 = const_vector [| one; one; zero; zero |] in1111 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in1112 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in1113 let sty = struct_type context [| i32_type; i8_type |] in1114 1115 ignore (build_extractelement vec1 p2 "build_extractelement" atentry);1116 ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry);1117 ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry);1118 1119 let p = build_alloca sty "ba" atentry in1120 let agg = build_load sty p "bl" atentry in1121 let agg0 = build_insertvalue agg (const_int i32_type 1) 01122 "build_insertvalue0" atentry in1123 let agg1 = build_insertvalue agg0 (const_int i8_type 2) 11124 "build_insertvalue1" atentry in1125 ignore (build_extractvalue agg1 1 "build_extractvalue" atentry)1126 end;1127 1128 group "metadata"; begin1129 (* CHECK: %metadata = add i32 %P1, %P2, !test !21130 * !2 is metadata emitted at EOF.1131 *)1132 let i = build_add p1 p2 "metadata" atentry in1133 insist ((has_metadata i) = false);1134 1135 let m1 = const_int i32_type 1 in1136 let m2 = mdstring context "metadata test" in1137 let md = mdnode context [| m1; m2 |] in1138 1139 let kind = mdkind_id context "test" in1140 set_metadata i kind md;1141 1142 insist ((has_metadata i) = true);1143 insist ((metadata i kind) = Some md);1144 insist ((get_mdnode_operands md) = [| m1; m2 |]);1145 1146 clear_metadata i kind;1147 1148 insist ((has_metadata i) = false);1149 insist ((metadata i kind) = None);1150 1151 set_metadata i kind md1152 end;1153 1154 group "named metadata"; begin1155 (* !llvm.module.flags is emitted at EOF. *)1156 let n1 = const_int i32_type 1 in1157 let n2 = mdstring context "Debug Info Version" in1158 let n3 = const_int i32_type 3 in1159 let md = mdnode context [| n1; n2; n3 |] in1160 add_named_metadata_operand m "llvm.module.flags" md;1161 1162 insist ((get_named_metadata m "llvm.module.flags") = [| md |])1163 end;1164 1165 group "ret"; begin1166 (* CHECK: ret{{.*}}P11167 *)1168 let ret = build_ret p1 atentry in1169 position_before_dbg_records ret atentry1170 end;1171 1172 (* see test/Feature/exception.ll *)1173 let bblpad = append_block context "Bblpad" fn in1174 let rt = struct_type context [| pointer_type context; i32_type |] in1175 let ft = var_arg_function_type i32_type [||] in1176 let personality = declare_function "__gxx_personality_v0" ft m in1177 let ztic = declare_global (pointer_type context) "_ZTIc" m in1178 let ztid = declare_global (pointer_type context) "_ZTId" m in1179 let ztipkc = declare_global (pointer_type context) "_ZTIPKc" m in1180 begin1181 set_global_constant true ztic;1182 set_global_constant true ztid;1183 set_global_constant true ztipkc;1184 let lp = build_landingpad rt personality 0 "lpad"1185 (builder_at_end context bblpad) in begin1186 set_cleanup lp true;1187 add_clause lp ztic;1188 insist((pointer_type context) = type_of ztid);1189 let ety = pointer_type context in1190 add_clause lp (const_array ety [| ztipkc; ztid |]);1191 ignore (build_resume lp (builder_at_end context bblpad));1192 end;1193 (* CHECK: landingpad1194 * CHECK: cleanup1195 * CHECK: catch{{.*}}ptr{{.*}}@_ZTIc1196 * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId1197 * CHECK: resume1198 * *)1199 end;1200 1201 group "br"; begin1202 (* CHECK: br{{.*}}Bb021203 *)1204 let bb02 = append_block context "Bb02" fn in1205 let b = builder_at_end context bb02 in1206 let br = build_br bb02 b in1207 insist (successors br = [| bb02 |]) ;1208 insist (is_conditional br = false) ;1209 insist (get_branch br = Some (`Unconditional bb02)) ;1210 end;1211 1212 group "cond_br"; begin1213 (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb001214 *)1215 let bb03 = append_block context "Bb03" fn in1216 let b = builder_at_end context bb03 in1217 let cond = build_trunc p1 i1_type "build_br" b in1218 let br = build_cond_br cond bb03 bb00 b in1219 insist (num_successors br = 2) ;1220 insist (successor br 0 = bb03) ;1221 insist (successor br 1 = bb00) ;1222 insist (is_conditional br = true) ;1223 insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ;1224 end;1225 1226 group "switch"; begin1227 (* CHECK: switch{{.*}}P1{{.*}}SwiBlock31228 * CHECK: 2,{{.*}}SwiBlock21229 *)1230 let bb1 = append_block context "SwiBlock1" fn in1231 let bb2 = append_block context "SwiBlock2" fn in1232 ignore (build_unreachable (builder_at_end context bb2));1233 let bb3 = append_block context "SwiBlock3" fn in1234 ignore (build_unreachable (builder_at_end context bb3));1235 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin1236 ignore (add_case si (const_int i32_type 2) bb2);1237 insist (switch_default_dest si = bb3);1238 end;1239 insist (num_successors si = 2) ;1240 insist (get_branch si = None) ;1241 end;1242 1243 group "malloc/free"; begin1244 (* CHECK: call{{.*}}@malloc(i32 ptrtoint1245 * CHECK: call{{.*}}@free(ptr1246 * CHECK: call{{.*}}@malloc(i32 %1247 *)1248 let bb1 = append_block context "MallocBlock1" fn in1249 let m1 = (build_malloc (pointer_type context) "m1"1250 (builder_at_end context bb1)) in1251 ignore (build_free m1 (builder_at_end context bb1));1252 ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1));1253 ignore (build_unreachable (builder_at_end context bb1));1254 end;1255 1256 group "indirectbr"; begin1257 (* CHECK: indirectbr ptr blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3]1258 *)1259 let bb1 = append_block context "IBRBlock1" fn in1260 1261 let bb2 = append_block context "IBRBlock2" fn in1262 ignore (build_unreachable (builder_at_end context bb2));1263 1264 let bb3 = append_block context "IBRBlock3" fn in1265 ignore (build_unreachable (builder_at_end context bb3));1266 1267 let addr = block_address fn bb2 in1268 let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in1269 ignore (add_destination ibr bb2);1270 ignore (add_destination ibr bb3)1271 end;1272 1273 group "invoke"; begin1274 (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P21275 * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad1276 *)1277 let bb04 = append_block context "Bb04" fn in1278 let b = builder_at_end context bb04 in1279 ignore (build_invoke fty fn [| p1; p2 |] bb04 bblpad "build_invoke" b)1280 end;1281 1282 group "unreachable"; begin1283 (* CHECK: unreachable1284 *)1285 let bb06 = append_block context "Bb06" fn in1286 let b = builder_at_end context bb06 in1287 ignore (build_unreachable b)1288 end;1289 1290 group "arithmetic"; begin1291 let bb07 = append_block context "Bb07" fn in1292 let b = builder_at_end context bb07 in1293 1294 (* CHECK: %build_add = add i32 %P1, %P21295 * CHECK: %build_nsw_add = add nsw i32 %P1, %P21296 * CHECK: %build_nuw_add = add nuw i32 %P1, %P21297 * CHECK: %build_fadd = fadd float %F1, %F21298 * CHECK: %build_sub = sub i32 %P1, %P21299 * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P21300 * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P21301 * CHECK: %build_fsub = fsub float %F1, %F21302 * CHECK: %build_mul = mul i32 %P1, %P21303 * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P21304 * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P21305 * CHECK: %build_fmul = fmul float %F1, %F21306 * CHECK: %build_udiv = udiv i32 %P1, %P21307 * CHECK: %build_sdiv = sdiv i32 %P1, %P21308 * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P21309 * CHECK: %build_fdiv = fdiv float %F1, %F21310 * CHECK: %build_urem = urem i32 %P1, %P21311 * CHECK: %build_srem = srem i32 %P1, %P21312 * CHECK: %build_frem = frem float %F1, %F21313 * CHECK: %build_shl = shl i32 %P1, %P21314 * CHECK: %build_lshl = lshr i32 %P1, %P21315 * CHECK: %build_ashl = ashr i32 %P1, %P21316 * CHECK: %build_and = and i32 %P1, %P21317 * CHECK: %build_or = or i32 %P1, %P21318 * CHECK: %build_xor = xor i32 %P1, %P21319 * CHECK: %build_neg = sub i32 0, %P11320 * CHECK: %build_nsw_neg = sub nsw i32 0, %P11321 * CHECK: %build_nuw_neg = sub nuw i32 0, %P11322 * CHECK: %build_fneg = fneg float %F11323 * CHECK: %build_not = xor i32 %P1, -11324 * CHECK: %build_freeze = freeze i32 %P11325 *)1326 ignore (build_add p1 p2 "build_add" b);1327 ignore (build_nsw_add p1 p2 "build_nsw_add" b);1328 ignore (build_nuw_add p1 p2 "build_nuw_add" b);1329 ignore (build_fadd f1 f2 "build_fadd" b);1330 ignore (build_sub p1 p2 "build_sub" b);1331 ignore (build_nsw_sub p1 p2 "build_nsw_sub" b);1332 ignore (build_nuw_sub p1 p2 "build_nuw_sub" b);1333 ignore (build_fsub f1 f2 "build_fsub" b);1334 ignore (build_mul p1 p2 "build_mul" b);1335 ignore (build_nsw_mul p1 p2 "build_nsw_mul" b);1336 ignore (build_nuw_mul p1 p2 "build_nuw_mul" b);1337 ignore (build_fmul f1 f2 "build_fmul" b);1338 ignore (build_udiv p1 p2 "build_udiv" b);1339 ignore (build_sdiv p1 p2 "build_sdiv" b);1340 ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b);1341 ignore (build_fdiv f1 f2 "build_fdiv" b);1342 ignore (build_urem p1 p2 "build_urem" b);1343 ignore (build_srem p1 p2 "build_srem" b);1344 ignore (build_frem f1 f2 "build_frem" b);1345 ignore (build_shl p1 p2 "build_shl" b);1346 ignore (build_lshr p1 p2 "build_lshl" b);1347 ignore (build_ashr p1 p2 "build_ashl" b);1348 ignore (build_and p1 p2 "build_and" b);1349 ignore (build_or p1 p2 "build_or" b);1350 ignore (build_xor p1 p2 "build_xor" b);1351 ignore (build_neg p1 "build_neg" b);1352 ignore (build_nsw_neg p1 "build_nsw_neg" b);1353 ignore (build_nuw_neg p1 "build_nuw_neg" b);1354 ignore (build_fneg f1 "build_fneg" b);1355 ignore (build_not p1 "build_not" b);1356 ignore (build_freeze p1 "build_freeze" b);1357 ignore (build_unreachable b)1358 end;1359 1360 group "memory"; begin1361 let bb08 = append_block context "Bb08" fn in1362 let b = builder_at_end context bb08 in1363 1364 (* CHECK: %build_alloca = alloca i321365 * CHECK: %build_array_alloca = alloca i32, i32 %P21366 * CHECK: %build_load = load volatile i32, ptr %build_array_alloca, align 41367 * CHECK: store volatile i32 %P2, ptr %build_alloca, align 41368 * CHECK: %build_gep = getelementptr i32, ptr %build_array_alloca, i32 %P21369 * CHECK: %build_in_bounds_gep = getelementptr inbounds i32, ptr %build_array_alloca, i32 %P21370 * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 11371 * CHECK: %build_atomicrmw = atomicrmw xchg ptr %p, i8 42 seq_cst1372 *)1373 let alloca = build_alloca i32_type "build_alloca" b in1374 let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in1375 1376 let load = build_load i32_type array_alloca "build_load" b in1377 ignore(set_alignment 4 load);1378 ignore(set_volatile true load);1379 insist(true = is_volatile load);1380 insist(4 = alignment load);1381 1382 let store = build_store p2 alloca b in1383 ignore(set_volatile true store);1384 ignore(set_alignment 4 store);1385 insist(true = is_volatile store);1386 insist(4 = alignment store);1387 ignore(build_gep i32_type array_alloca [| p2 |] "build_gep" b);1388 ignore(build_in_bounds_gep i32_type array_alloca [| p2 |]1389 "build_in_bounds_gep" b);1390 1391 let sty = struct_type context [| i32_type; i8_type |] in1392 let alloca2 = build_alloca sty "build_alloca2" b in1393 ignore(build_struct_gep sty alloca2 1 "build_struct_gep" b);1394 1395 let p = build_alloca i8_type "p" b in1396 ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42)1397 AtomicOrdering.SequentiallyConsistent false "build_atomicrmw"1398 b);1399 1400 ignore(build_unreachable b)1401 end;1402 1403 group "string"; begin1404 let bb09 = append_block context "Bb09" fn in1405 let b = builder_at_end context bb09 in1406 let p = build_alloca (pointer_type context) "p" b in1407 (* build_global_string is emitted above.1408 * CHECK: store{{.*}}build_global_string1{{.*}}p1409 * *)1410 ignore (build_global_string "stringval" "build_global_string" b);1411 let g = build_global_stringptr "stringval" "build_global_string1" b in1412 ignore (build_store g p b);1413 ignore(build_unreachable b);1414 end;1415 1416 group "phi"; begin1417 (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock21418 *)1419 let b1 = append_block context "PhiBlock1" fn in1420 let b2 = append_block context "PhiBlock2" fn in1421 1422 let jb = append_block context "PhiJoinBlock" fn in1423 ignore (build_br jb (builder_at_end context b1));1424 ignore (build_br jb (builder_at_end context b2));1425 let at_jb = builder_at_end context jb in1426 1427 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in1428 insist ([(p1, b1)] = incoming phi);1429 1430 add_incoming (p2, b2) phi;1431 insist ([(p1, b1); (p2, b2)] = incoming phi);1432 1433 (* CHECK: %PhiEmptyNode = phi i81434 *)1435 let phi_empty = build_empty_phi i8_type "PhiEmptyNode" at_jb in1436 insist ([] = incoming phi_empty);1437 1438 (* can't emit an empty phi to bitcode *)1439 add_incoming (const_int i8_type 1, b1) phi_empty;1440 add_incoming (const_int i8_type 2, b2) phi_empty;1441 1442 ignore (build_unreachable at_jb);1443 end1444 1445(* End-of-file checks for things like metdata and attributes.1446 * CHECK: !llvm.module.flags = !{!1}1447 * CHECK: !0 = !{i32 0, !"global test metadata"}1448 * CHECK: !1 = !{i32 1, !"Debug Info Version", i32 3}1449 * CHECK: !2 = !{i32 1, !"metadata test"}1450 *)1451 1452 1453(*===-- Memory Buffer -----------------------------------------------------===*)1454 1455let test_memory_buffer () =1456 group "memory buffer";1457 let buf = MemoryBuffer.of_string "foobar" in1458 insist ((MemoryBuffer.as_string buf) = "foobar")1459 1460 1461(*===-- Writer ------------------------------------------------------------===*)1462 1463let test_writer () =1464 group "valid";1465 insist (match Llvm_analysis.verify_module m with1466 | None -> true1467 | Some msg -> prerr_string msg; false);1468 1469 group "writer";1470 insist (write_bitcode_file m filename);1471 1472 dispose_module m1473 1474 1475(*===-- Driver ------------------------------------------------------------===*)1476 1477let _ =1478 suite "modules" test_modules;1479 suite "contained types" test_contained_types;1480 suite "pointer types" test_pointer_types;1481 suite "other types" test_other_types;1482 suite "conversion" test_conversion;1483 suite "target" test_target;1484 suite "constants" test_constants;1485 suite "attributes" test_attributes;1486 suite "global values" test_global_values;1487 suite "global variables" test_global_variables;1488 suite "uses" test_uses;1489 suite "users" test_users;1490 suite "aliases" test_aliases;1491 suite "functions" test_functions;1492 suite "params" test_params;1493 suite "basic blocks" test_basic_blocks;1494 suite "instructions" test_instructions;1495 suite "builder" test_builder;1496 suite "memory buffer" test_memory_buffer;1497 suite "writer" test_writer; (* Keep this last; it disposes m. *)1498 exit !exit_status1499