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