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