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