vmcore.ml revision d2b4aff4bca23ff5dadf180281a74902a18fcf73
1(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %s -o %t 2 * RUN: ./%t %t.bc 3 * RUN: llvm-dis < %t.bc > %t.ll 4 *) 5 6(* Note: It takes several seconds for ocamlopt to link an executable with 7 libLLVMCore.a, so it's better to write a big test than a bunch of 8 little ones. *) 9 10open Llvm 11open Llvm_bitwriter 12 13 14(* Tiny unit test framework - really just to help find which line is busted *) 15let exit_status = ref 0 16let suite_name = ref "" 17let group_name = ref "" 18let case_num = ref 0 19let print_checkpoints = false 20let context = global_context () 21let i1_type = Llvm.i1_type context 22let i8_type = Llvm.i8_type context 23let i16_type = Llvm.i16_type context 24let i32_type = Llvm.i32_type context 25let i64_type = Llvm.i64_type context 26let void_type = Llvm.void_type context 27let float_type = Llvm.float_type context 28let double_type = Llvm.double_type context 29let fp128_type = Llvm.fp128_type context 30 31let group name = 32 group_name := !suite_name ^ "/" ^ name; 33 case_num := 0; 34 if print_checkpoints then 35 prerr_endline (" " ^ name ^ "...") 36 37let insist cond = 38 incr case_num; 39 if not cond then 40 exit_status := 10; 41 match print_checkpoints, cond with 42 | false, true -> () 43 | false, false -> 44 prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num)) 45 | true, true -> 46 prerr_endline (" " ^ (string_of_int !case_num)) 47 | true, false -> 48 prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL") 49 50let suite name f = 51 suite_name := name; 52 if print_checkpoints then 53 prerr_endline (name ^ ":"); 54 f () 55 56 57(*===-- Fixture -----------------------------------------------------------===*) 58 59let filename = Sys.argv.(1) 60let m = create_module context filename 61let mp = ModuleProvider.create m 62 63 64(*===-- Target ------------------------------------------------------------===*) 65 66let test_target () = 67 begin group "triple"; 68 (* RUN: grep "i686-apple-darwin8" < %t.ll 69 *) 70 let trip = "i686-apple-darwin8" in 71 set_target_triple trip m; 72 insist (trip = target_triple m) 73 end; 74 75 begin group "layout"; 76 (* RUN: grep "bogus" < %t.ll 77 *) 78 let layout = "bogus" in 79 set_data_layout layout m; 80 insist (layout = data_layout m) 81 end 82 83(*===-- Types -------------------------------------------------------------===*) 84 85let test_types () = 86 (* RUN: grep {Ty01.*void} < %t.ll 87 *) 88 group "void"; 89 insist (define_type_name "Ty01" void_type m); 90 insist (TypeKind.Void == classify_type void_type); 91 92 (* RUN: grep {Ty02.*i1} < %t.ll 93 *) 94 group "i1"; 95 insist (define_type_name "Ty02" i1_type m); 96 insist (TypeKind.Integer == classify_type i1_type); 97 98 (* RUN: grep {Ty03.*i32} < %t.ll 99 *) 100 group "i32"; 101 insist (define_type_name "Ty03" i32_type m); 102 103 (* RUN: grep {Ty04.*i42} < %t.ll 104 *) 105 group "i42"; 106 let ty = integer_type context 42 in 107 insist (define_type_name "Ty04" ty m); 108 109 (* RUN: grep {Ty05.*float} < %t.ll 110 *) 111 group "float"; 112 insist (define_type_name "Ty05" float_type m); 113 insist (TypeKind.Float == classify_type float_type); 114 115 (* RUN: grep {Ty06.*double} < %t.ll 116 *) 117 group "double"; 118 insist (define_type_name "Ty06" double_type m); 119 insist (TypeKind.Double == classify_type double_type); 120 121 (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll 122 *) 123 group "function"; 124 let ty = function_type i32_type [| i1_type; double_type |] in 125 insist (define_type_name "Ty07" ty m); 126 insist (TypeKind.Function = classify_type ty); 127 insist (not (is_var_arg ty)); 128 insist (i32_type == return_type ty); 129 insist (double_type == (param_types ty).(1)); 130 131 (* RUN: grep {Ty08.*\.\.\.} < %t.ll 132 *) 133 group "var arg function"; 134 let ty = var_arg_function_type void_type [| i32_type |] in 135 insist (define_type_name "Ty08" ty m); 136 insist (is_var_arg ty); 137 138 (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll 139 *) 140 group "array"; 141 let ty = array_type i8_type 7 in 142 insist (define_type_name "Ty09" ty m); 143 insist (7 = array_length ty); 144 insist (i8_type == element_type ty); 145 insist (TypeKind.Array == classify_type ty); 146 147 begin group "pointer"; 148 (* RUN: grep {UnqualPtrTy.*float\*} < %t.ll 149 *) 150 let ty = pointer_type float_type in 151 insist (define_type_name "UnqualPtrTy" ty m); 152 insist (float_type == element_type ty); 153 insist (0 == address_space ty); 154 insist (TypeKind.Pointer == classify_type ty) 155 end; 156 157 begin group "qualified_pointer"; 158 (* RUN: grep {QualPtrTy.*i8.*3.*\*} < %t.ll 159 *) 160 let ty = qualified_pointer_type i8_type 3 in 161 insist (define_type_name "QualPtrTy" ty m); 162 insist (i8_type == element_type ty); 163 insist (3 == address_space ty) 164 end; 165 166 (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll 167 *) 168 group "vector"; 169 let ty = vector_type i16_type 4 in 170 insist (define_type_name "Ty11" ty m); 171 insist (i16_type == element_type ty); 172 insist (4 = vector_size ty); 173 174 (* RUN: grep {Ty12.*opaque} < %t.ll 175 *) 176 group "opaque"; 177 let ty = opaque_type context in 178 insist (define_type_name "Ty12" ty m); 179 insist (ty == ty); 180 insist (ty <> opaque_type context); 181 182 (* RUN: grep -v {Ty13} < %t.ll 183 *) 184 group "delete"; 185 let ty = opaque_type context in 186 insist (define_type_name "Ty13" ty m); 187 delete_type_name "Ty13" m; 188 189 (* RUN: grep -v {RecursiveTy.*RecursiveTy} < %t.ll 190 *) 191 group "recursive"; 192 let ty = opaque_type context in 193 let th = handle_to_type ty in 194 refine_type ty (pointer_type ty); 195 let ty = type_of_handle th in 196 insist (define_type_name "RecursiveTy" ty m); 197 insist (ty == element_type ty) 198 199 200(*===-- Constants ---------------------------------------------------------===*) 201 202let test_constants () = 203 (* RUN: grep {Const01.*i32.*-1} < %t.ll 204 *) 205 group "int"; 206 let c = const_int i32_type (-1) in 207 ignore (define_global "Const01" c m); 208 insist (i32_type = type_of c); 209 insist (is_constant c); 210 211 (* RUN: grep {Const02.*i64.*-1} < %t.ll 212 *) 213 group "sext int"; 214 let c = const_int i64_type (-1) in 215 ignore (define_global "Const02" c m); 216 insist (i64_type = type_of c); 217 218 (* RUN: grep {Const03.*i64.*4294967295} < %t.ll 219 *) 220 group "zext int64"; 221 let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in 222 ignore (define_global "Const03" c m); 223 insist (i64_type = type_of c); 224 225 (* RUN: grep {ConstIntString.*i32.*-1} < %t.ll 226 *) 227 group "int string"; 228 let c = const_int_of_string i32_type "-1" 10 in 229 ignore (define_global "ConstIntString" c m); 230 insist (i32_type = type_of c); 231 232 (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll 233 *) 234 group "string"; 235 let c = const_string context "cruel\000world" in 236 ignore (define_global "Const04" c m); 237 insist ((array_type i8_type 11) = type_of c); 238 239 (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll 240 *) 241 group "stringz"; 242 let c = const_stringz context "hi\000again" in 243 ignore (define_global "Const05" c m); 244 insist ((array_type i8_type 9) = type_of c); 245 246 (* RUN: grep {ConstSingle.*2.75} < %t.ll 247 * RUN: grep {ConstDouble.*3.1459} < %t.ll 248 * RUN: grep {ConstDoubleString.*1.25} < %t.ll 249 *) 250 begin group "real"; 251 let cs = const_float float_type 2.75 in 252 ignore (define_global "ConstSingle" cs m); 253 insist (float_type = type_of cs); 254 255 let cd = const_float double_type 3.1459 in 256 ignore (define_global "ConstDouble" cd m); 257 insist (double_type = type_of cd); 258 259 let cd = const_float_of_string double_type "1.25" in 260 ignore (define_global "ConstDoubleString" cd m); 261 insist (double_type = type_of cd) 262 end; 263 264 let one = const_int i16_type 1 in 265 let two = const_int i16_type 2 in 266 let three = const_int i32_type 3 in 267 let four = const_int i32_type 4 in 268 269 (* RUN: grep {Const07.*\\\[i32 3, i32 4\\\]} < %t.ll 270 *) 271 group "array"; 272 let c = const_array i32_type [| three; four |] in 273 ignore (define_global "Const07" c m); 274 insist ((array_type i32_type 2) = (type_of c)); 275 276 (* RUN: grep {Const08.*<i16 1, i16 2.*>} < %t.ll 277 *) 278 group "vector"; 279 let c = const_vector [| one; two; one; two; 280 one; two; one; two |] in 281 ignore (define_global "Const08" c m); 282 insist ((vector_type i16_type 8) = (type_of c)); 283 284 (* RUN: grep {Const09.*.i16 1, i16 2, i32 3, i32 4} < %t.ll 285 *) 286 group "structure"; 287 let c = const_struct context [| one; two; three; four |] in 288 ignore (define_global "Const09" c m); 289 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |]) 290 = (type_of c)); 291 292 (* RUN: grep {Const10.*zeroinit} < %t.ll 293 *) 294 group "null"; 295 let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type; 296 double_type |]) in 297 ignore (define_global "Const10" c m); 298 299 (* RUN: grep {Const11.*-1} < %t.ll 300 *) 301 group "all ones"; 302 let c = const_all_ones i64_type in 303 ignore (define_global "Const11" c m); 304 305 (* RUN: grep {Const12.*undef} < %t.ll 306 *) 307 group "undef"; 308 let c = undef i1_type in 309 ignore (define_global "Const12" c m); 310 insist (i1_type = type_of c); 311 insist (is_undef c); 312 313 group "constant arithmetic"; 314 (* RUN: grep {ConstNeg.*sub} < %t.ll 315 * RUN: grep {ConstNot.*xor} < %t.ll 316 * RUN: grep {ConstAdd.*add} < %t.ll 317 * RUN: grep {ConstSub.*sub} < %t.ll 318 * RUN: grep {ConstMul.*mul} < %t.ll 319 * RUN: grep {ConstUDiv.*udiv} < %t.ll 320 * RUN: grep {ConstSDiv.*sdiv} < %t.ll 321 * RUN: grep {ConstFDiv.*fdiv} < %t.ll 322 * RUN: grep {ConstURem.*urem} < %t.ll 323 * RUN: grep {ConstSRem.*srem} < %t.ll 324 * RUN: grep {ConstFRem.*frem} < %t.ll 325 * RUN: grep {ConstAnd.*and} < %t.ll 326 * RUN: grep {ConstOr.*or} < %t.ll 327 * RUN: grep {ConstXor.*xor} < %t.ll 328 * RUN: grep {ConstICmp.*icmp} < %t.ll 329 * RUN: grep {ConstFCmp.*fcmp} < %t.ll 330 *) 331 let void_ptr = pointer_type i8_type in 332 let five = const_int i64_type 5 in 333 let ffive = const_uitofp five double_type in 334 let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in 335 let foldbomb = const_ptrtoint foldbomb_gv i64_type in 336 let ffoldbomb = const_uitofp foldbomb double_type in 337 ignore (define_global "ConstNeg" (const_neg foldbomb) m); 338 ignore (define_global "ConstNot" (const_not foldbomb) m); 339 ignore (define_global "ConstAdd" (const_add foldbomb five) m); 340 ignore (define_global "ConstSub" (const_sub foldbomb five) m); 341 ignore (define_global "ConstMul" (const_mul foldbomb five) m); 342 ignore (define_global "ConstUDiv" (const_udiv foldbomb five) m); 343 ignore (define_global "ConstSDiv" (const_sdiv foldbomb five) m); 344 ignore (define_global "ConstFDiv" (const_fdiv ffoldbomb ffive) m); 345 ignore (define_global "ConstURem" (const_urem foldbomb five) m); 346 ignore (define_global "ConstSRem" (const_srem foldbomb five) m); 347 ignore (define_global "ConstFRem" (const_frem ffoldbomb ffive) m); 348 ignore (define_global "ConstAnd" (const_and foldbomb five) m); 349 ignore (define_global "ConstOr" (const_or foldbomb five) m); 350 ignore (define_global "ConstXor" (const_xor foldbomb five) m); 351 ignore (define_global "ConstICmp" (const_icmp Icmp.Sle foldbomb five) m); 352 ignore (define_global "ConstFCmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m); 353 354 group "constant casts"; 355 (* RUN: grep {ConstTrunc.*trunc} < %t.ll 356 * RUN: grep {ConstSExt.*sext} < %t.ll 357 * RUN: grep {ConstZExt.*zext} < %t.ll 358 * RUN: grep {ConstFPTrunc.*fptrunc} < %t.ll 359 * RUN: grep {ConstFPExt.*fpext} < %t.ll 360 * RUN: grep {ConstUIToFP.*uitofp} < %t.ll 361 * RUN: grep {ConstSIToFP.*sitofp} < %t.ll 362 * RUN: grep {ConstFPToUI.*fptoui} < %t.ll 363 * RUN: grep {ConstFPToSI.*fptosi} < %t.ll 364 * RUN: grep {ConstPtrToInt.*ptrtoint} < %t.ll 365 * RUN: grep {ConstIntToPtr.*inttoptr} < %t.ll 366 * RUN: grep {ConstBitCast.*bitcast} < %t.ll 367 *) 368 let i128_type = integer_type context 128 in 369 ignore (define_global "ConstTrunc" (const_trunc (const_add foldbomb five) 370 i8_type) m); 371 ignore (define_global "ConstSExt" (const_sext foldbomb i128_type) m); 372 ignore (define_global "ConstZExt" (const_zext foldbomb i128_type) m); 373 ignore (define_global "ConstFPTrunc" (const_fptrunc ffoldbomb float_type) m); 374 ignore (define_global "ConstFPExt" (const_fpext ffoldbomb fp128_type) m); 375 ignore (define_global "ConstUIToFP" (const_uitofp foldbomb double_type) m); 376 ignore (define_global "ConstSIToFP" (const_sitofp foldbomb double_type) m); 377 ignore (define_global "ConstFPToUI" (const_fptoui ffoldbomb i32_type) m); 378 ignore (define_global "ConstFPToSI" (const_fptosi ffoldbomb i32_type) m); 379 ignore (define_global "ConstPtrToInt" (const_ptrtoint 380 (const_gep (const_null (pointer_type i8_type)) 381 [| const_int i32_type 1 |]) 382 i32_type) m); 383 ignore (define_global "ConstIntToPtr" (const_inttoptr (const_add foldbomb five) 384 void_ptr) m); 385 ignore (define_global "ConstBitCast" (const_bitcast ffoldbomb i64_type) m); 386 387 group "misc constants"; 388 (* RUN: grep {ConstSizeOf.*getelementptr.*null} < %t.ll 389 * RUN: grep {ConstGEP.*getelementptr} < %t.ll 390 * RUN: grep {ConstSelect.*select} < %t.ll 391 * RUN: grep {ConstExtractElement.*extractelement} < %t.ll 392 * RUN: grep {ConstInsertElement.*insertelement} < %t.ll 393 * RUN: grep {ConstShuffleVector.*shufflevector} < %t.ll 394 *) 395 ignore (define_global "ConstSizeOf" (size_of (pointer_type i8_type)) m); 396 ignore (define_global "ConstGEP" (const_gep foldbomb_gv [| five |]) m); 397 ignore (define_global "ConstSelect" (const_select 398 (const_icmp Icmp.Sle foldbomb five) 399 (const_int i8_type (-1)) 400 (const_int i8_type 0)) m); 401 let zero = const_int i32_type 0 in 402 let one = const_int i32_type 1 in 403 ignore (define_global "ConstExtractElement" (const_extractelement 404 (const_vector [| zero; one; zero; one |]) 405 (const_trunc foldbomb i32_type)) m); 406 ignore (define_global "ConstInsertElement" (const_insertelement 407 (const_vector [| zero; one; zero; one |]) 408 zero (const_trunc foldbomb i32_type)) m); 409 ignore (define_global "ConstShuffleVector" (const_shufflevector 410 (const_vector [| zero; one |]) 411 (const_vector [| one; zero |]) 412 (const_bitcast foldbomb (vector_type i32_type 2))) m) 413 414 415(*===-- Global Values -----------------------------------------------------===*) 416 417let test_global_values () = 418 let (++) x f = f x; x in 419 let zero32 = const_null i32_type in 420 421 (* RUN: grep {GVal01} < %t.ll 422 *) 423 group "naming"; 424 let g = define_global "TEMPORARY" zero32 m in 425 insist ("TEMPORARY" = value_name g); 426 set_value_name "GVal01" g; 427 insist ("GVal01" = value_name g); 428 429 (* RUN: grep {GVal02.*linkonce} < %t.ll 430 *) 431 group "linkage"; 432 let g = define_global "GVal02" zero32 m ++ 433 set_linkage Linkage.Link_once in 434 insist (Linkage.Link_once = linkage g); 435 436 (* RUN: grep {GVal03.*Hanalei} < %t.ll 437 *) 438 group "section"; 439 let g = define_global "GVal03" zero32 m ++ 440 set_section "Hanalei" in 441 insist ("Hanalei" = section g); 442 443 (* RUN: grep {GVal04.*hidden} < %t.ll 444 *) 445 group "visibility"; 446 let g = define_global "GVal04" zero32 m ++ 447 set_visibility Visibility.Hidden in 448 insist (Visibility.Hidden = visibility g); 449 450 (* RUN: grep {GVal05.*align 128} < %t.ll 451 *) 452 group "alignment"; 453 let g = define_global "GVal05" zero32 m ++ 454 set_alignment 128 in 455 insist (128 = alignment g) 456 457 458(*===-- Global Variables --------------------------------------------------===*) 459 460let test_global_variables () = 461 let (++) x f = f x; x in 462 let fourty_two32 = const_int i32_type 42 in 463 464 (* RUN: grep {GVar01.*external} < %t.ll 465 *) 466 group "declarations"; 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 (* RUN: grep {GVar02.*42} < %t.ll 477 * RUN: grep {GVar03.*42} < %t.ll 478 *) 479 group "definitions"; 480 let g = define_global "GVar02" fourty_two32 m in 481 let g2 = declare_global i32_type "GVar03" m ++ 482 set_initializer fourty_two32 in 483 insist (not (is_declaration g)); 484 insist (not (is_declaration g2)); 485 insist ((global_initializer g) == (global_initializer g2)); 486 487 (* RUN: grep {GVar04.*thread_local} < %t.ll 488 *) 489 group "threadlocal"; 490 let g = define_global "GVar04" fourty_two32 m ++ 491 set_thread_local true in 492 insist (is_thread_local g); 493 494 (* RUN: grep -v {GVar05} < %t.ll 495 *) 496 group "delete"; 497 let g = define_global "GVar05" fourty_two32 m in 498 delete_global g; 499 500 (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll 501 *) 502 group "constant"; 503 let g = define_global "ConstGlobalVar" fourty_two32 m in 504 insist (not (is_global_constant g)); 505 set_global_constant true g; 506 insist (is_global_constant g); 507 508 begin group "iteration"; 509 let m = create_module context "temp" in 510 511 insist (At_end m = global_begin m); 512 insist (At_start m = global_end m); 513 514 let g1 = declare_global i32_type "One" m in 515 let g2 = declare_global i32_type "Two" m in 516 517 insist (Before g1 = global_begin m); 518 insist (Before g2 = global_succ g1); 519 insist (At_end m = global_succ g2); 520 521 insist (After g2 = global_end m); 522 insist (After g1 = global_pred g2); 523 insist (At_start m = global_pred g1); 524 525 let lf s x = s ^ "->" ^ value_name x in 526 insist ("->One->Two" = fold_left_globals lf "" m); 527 528 let rf x s = value_name x ^ "<-" ^ s in 529 insist ("One<-Two<-" = fold_right_globals rf m ""); 530 531 dispose_module m 532 end 533 534 535(*===-- Functions ---------------------------------------------------------===*) 536 537let test_functions () = 538 let ty = function_type i32_type [| i32_type; i64_type |] in 539 let ty2 = function_type i8_type [| i8_type; i64_type |] in 540 541 (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll 542 *) 543 begin group "declare"; 544 insist (None = lookup_function "Fn1" m); 545 let fn = declare_function "Fn1" ty m in 546 insist (pointer_type ty = type_of fn); 547 insist (is_declaration fn); 548 insist (0 = Array.length (basic_blocks fn)); 549 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m)); 550 insist (fn == declare_function "Fn1" ty m); 551 insist (None <> lookup_function "Fn1" m); 552 insist (match lookup_function "Fn1" m with Some x -> x = fn 553 | None -> false); 554 insist (m == global_parent fn) 555 end; 556 557 (* RUN: grep -v {Fn2} < %t.ll 558 *) 559 group "delete"; 560 let fn = declare_function "Fn2" ty m in 561 delete_function fn; 562 563 (* RUN: grep {define.*Fn3} < %t.ll 564 *) 565 group "define"; 566 let fn = define_function "Fn3" ty m in 567 insist (not (is_declaration fn)); 568 insist (1 = Array.length (basic_blocks fn)); 569 ignore (build_unreachable (builder_at_end context (entry_block fn))); 570 571 (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll 572 *) 573 group "params"; 574 let fn = define_function "Fn4" ty m in 575 let params = params fn in 576 insist (2 = Array.length params); 577 insist (params.(0) = param fn 0); 578 insist (params.(1) = param fn 1); 579 insist (i32_type = type_of params.(0)); 580 insist (i64_type = type_of params.(1)); 581 set_value_name "Param1" params.(0); 582 set_value_name "Param2" params.(1); 583 ignore (build_unreachable (builder_at_end context (entry_block fn))); 584 585 (* RUN: grep {fastcc.*Fn5} < %t.ll 586 *) 587 group "callconv"; 588 let fn = define_function "Fn5" ty m in 589 insist (CallConv.c = function_call_conv fn); 590 set_function_call_conv CallConv.fast fn; 591 insist (CallConv.fast = function_call_conv fn); 592 ignore (build_unreachable (builder_at_end context (entry_block fn))); 593 594 begin group "gc"; 595 (* RUN: grep {Fn6.*gc.*shadowstack} < %t.ll 596 *) 597 let fn = define_function "Fn6" ty m in 598 insist (None = gc fn); 599 set_gc (Some "ocaml") fn; 600 insist (Some "ocaml" = gc fn); 601 set_gc None fn; 602 insist (None = gc fn); 603 set_gc (Some "shadowstack") fn; 604 ignore (build_unreachable (builder_at_end context (entry_block fn))); 605 end; 606 607 begin group "iteration"; 608 let m = create_module context "temp" in 609 610 insist (At_end m = function_begin m); 611 insist (At_start m = function_end m); 612 613 let f1 = define_function "One" ty m in 614 let f2 = define_function "Two" ty m in 615 616 insist (Before f1 = function_begin m); 617 insist (Before f2 = function_succ f1); 618 insist (At_end m = function_succ f2); 619 620 insist (After f2 = function_end m); 621 insist (After f1 = function_pred f2); 622 insist (At_start m = function_pred f1); 623 624 let lf s x = s ^ "->" ^ value_name x in 625 insist ("->One->Two" = fold_left_functions lf "" m); 626 627 let rf x s = value_name x ^ "<-" ^ s in 628 insist ("One<-Two<-" = fold_right_functions rf m ""); 629 630 dispose_module m 631 end 632 633 634(*===-- Params ------------------------------------------------------------===*) 635 636let test_params () = 637 begin group "iteration"; 638 let m = create_module context "temp" in 639 640 let vf = define_function "void" (function_type void_type [| |]) m in 641 642 insist (At_end vf = param_begin vf); 643 insist (At_start vf = param_end vf); 644 645 let ty = function_type void_type [| i32_type; i32_type |] in 646 let f = define_function "f" ty m in 647 let p1 = param f 0 in 648 let p2 = param f 1 in 649 set_value_name "One" p1; 650 set_value_name "Two" p2; 651 add_param_attr p1 Attribute.Sext; 652 add_param_attr p2 Attribute.Noalias; 653 remove_param_attr p2 Attribute.Noalias; 654 add_function_attr f Attribute.Nounwind; 655 add_function_attr f Attribute.Noreturn; 656 remove_function_attr f Attribute.Noreturn; 657 658 insist (Before p1 = param_begin f); 659 insist (Before p2 = param_succ p1); 660 insist (At_end f = param_succ p2); 661 662 insist (After p2 = param_end f); 663 insist (After p1 = param_pred p2); 664 insist (At_start f = param_pred p1); 665 666 let lf s x = s ^ "->" ^ value_name x in 667 insist ("->One->Two" = fold_left_params lf "" f); 668 669 let rf x s = value_name x ^ "<-" ^ s in 670 insist ("One<-Two<-" = fold_right_params rf f ""); 671 672 dispose_module m 673 end 674 675 676(*===-- Basic Blocks ------------------------------------------------------===*) 677 678let test_basic_blocks () = 679 let ty = function_type void_type [| |] in 680 681 (* RUN: grep {Bb1} < %t.ll 682 *) 683 group "entry"; 684 let fn = declare_function "X" ty m in 685 let bb = append_block context "Bb1" fn in 686 insist (bb = entry_block fn); 687 ignore (build_unreachable (builder_at_end context bb)); 688 689 (* RUN: grep -v Bb2 < %t.ll 690 *) 691 group "delete"; 692 let fn = declare_function "X2" ty m in 693 let bb = append_block context "Bb2" fn in 694 delete_block bb; 695 696 group "insert"; 697 let fn = declare_function "X3" ty m in 698 let bbb = append_block context "b" fn in 699 let bba = insert_block context "a" bbb in 700 insist ([| bba; bbb |] = basic_blocks fn); 701 ignore (build_unreachable (builder_at_end context bba)); 702 ignore (build_unreachable (builder_at_end context bbb)); 703 704 (* RUN: grep Bb3 < %t.ll 705 *) 706 group "name/value"; 707 let fn = define_function "X4" ty m in 708 let bb = entry_block fn in 709 ignore (build_unreachable (builder_at_end context bb)); 710 let bbv = value_of_block bb in 711 set_value_name "Bb3" bbv; 712 insist ("Bb3" = value_name bbv); 713 714 group "casts"; 715 let fn = define_function "X5" ty m in 716 let bb = entry_block fn in 717 ignore (build_unreachable (builder_at_end context bb)); 718 insist (bb = block_of_value (value_of_block bb)); 719 insist (value_is_block (value_of_block bb)); 720 insist (not (value_is_block (const_null i32_type))); 721 722 begin group "iteration"; 723 let m = create_module context "temp" in 724 let f = declare_function "Temp" (function_type i32_type [| |]) m in 725 726 insist (At_end f = block_begin f); 727 insist (At_start f = block_end f); 728 729 let b1 = append_block context "One" f in 730 let b2 = append_block context "Two" f in 731 732 insist (Before b1 = block_begin f); 733 insist (Before b2 = block_succ b1); 734 insist (At_end f = block_succ b2); 735 736 insist (After b2 = block_end f); 737 insist (After b1 = block_pred b2); 738 insist (At_start f = block_pred b1); 739 740 let lf s x = s ^ "->" ^ value_name (value_of_block x) in 741 insist ("->One->Two" = fold_left_blocks lf "" f); 742 743 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in 744 insist ("One<-Two<-" = fold_right_blocks rf f ""); 745 746 dispose_module m 747 end 748 749 750(*===-- Instructions ------------------------------------------------------===*) 751 752let test_instructions () = 753 begin group "iteration"; 754 let m = create_module context "temp" in 755 let fty = function_type void_type [| i32_type; i32_type |] in 756 let f = define_function "f" fty m in 757 let bb = entry_block f in 758 let b = builder_at context (At_end bb) in 759 760 insist (At_end bb = instr_begin bb); 761 insist (At_start bb = instr_end bb); 762 763 let i1 = build_add (param f 0) (param f 1) "One" b in 764 let i2 = build_sub (param f 0) (param f 1) "Two" b in 765 766 insist (Before i1 = instr_begin bb); 767 insist (Before i2 = instr_succ i1); 768 insist (At_end bb = instr_succ i2); 769 770 insist (After i2 = instr_end bb); 771 insist (After i1 = instr_pred i2); 772 insist (At_start bb = instr_pred i1); 773 774 let lf s x = s ^ "->" ^ value_name x in 775 insist ("->One->Two" = fold_left_instrs lf "" bb); 776 777 let rf x s = value_name x ^ "<-" ^ s in 778 insist ("One<-Two<-" = fold_right_instrs rf bb ""); 779 780 dispose_module m 781 end 782 783 784(*===-- Builder -----------------------------------------------------------===*) 785 786let test_builder () = 787 let (++) x f = f x; x in 788 789 begin group "parent"; 790 insist (try 791 ignore (insertion_block (builder context)); 792 false 793 with Not_found -> 794 true); 795 796 let fty = function_type void_type [| i32_type |] in 797 let fn = define_function "BuilderParent" fty m in 798 let bb = entry_block fn in 799 let b = builder_at_end context bb in 800 let p = param fn 0 in 801 let sum = build_add p p "sum" b in 802 ignore (build_ret_void b); 803 804 insist (fn = block_parent bb); 805 insist (fn = param_parent p); 806 insist (bb = instr_parent sum); 807 insist (bb = insertion_block b) 808 end; 809 810 group "ret void"; 811 begin 812 (* RUN: grep {ret void} < %t.ll 813 *) 814 let fty = function_type void_type [| |] in 815 let fn = declare_function "X6" fty m in 816 let b = builder_at_end context (append_block context "Bb01" fn) in 817 ignore (build_ret_void b) 818 end; 819 820 (* The rest of the tests will use one big function. *) 821 let fty = function_type i32_type [| i32_type; i32_type |] in 822 let fn = define_function "X7" fty m in 823 let atentry = builder_at_end context (entry_block fn) in 824 let p1 = param fn 0 ++ set_value_name "P1" in 825 let p2 = param fn 1 ++ set_value_name "P2" in 826 let f1 = build_uitofp p1 float_type "F1" atentry in 827 let f2 = build_uitofp p2 float_type "F2" atentry in 828 829 let bb00 = append_block context "Bb00" fn in 830 ignore (build_unreachable (builder_at_end context bb00)); 831 832 group "ret"; begin 833 (* RUN: grep {ret.*P1} < %t.ll 834 *) 835 let ret = build_ret p1 atentry in 836 position_before ret atentry 837 end; 838 839 group "br"; begin 840 (* RUN: grep {br.*Bb02} < %t.ll 841 *) 842 let bb02 = append_block context "Bb02" fn in 843 let b = builder_at_end context bb02 in 844 ignore (build_br bb02 b) 845 end; 846 847 group "cond_br"; begin 848 (* RUN: grep {br.*Inst01.*Bb03.*Bb00} < %t.ll 849 *) 850 let bb03 = append_block context "Bb03" fn in 851 let b = builder_at_end context bb03 in 852 let cond = build_trunc p1 i1_type "Inst01" b in 853 ignore (build_cond_br cond bb03 bb00 b) 854 end; 855 856 group "switch"; begin 857 (* RUN: grep {switch.*P1.*SwiBlock3} < %t.ll 858 * RUN: grep {2,.*SwiBlock2} < %t.ll 859 *) 860 let bb1 = append_block context "SwiBlock1" fn in 861 let bb2 = append_block context "SwiBlock2" fn in 862 ignore (build_unreachable (builder_at_end context bb2)); 863 let bb3 = append_block context "SwiBlock3" fn in 864 ignore (build_unreachable (builder_at_end context bb3)); 865 let si = build_switch p1 bb3 1 (builder_at_end context bb1) in 866 ignore (add_case si (const_int i32_type 2) bb2) 867 end; 868 869 group "invoke"; begin 870 (* RUN: grep {Inst02.*invoke.*P1.*P2} < %t.ll 871 * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll 872 *) 873 let bb04 = append_block context "Bb04" fn in 874 let b = builder_at_end context bb04 in 875 ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "Inst02" b) 876 end; 877 878 group "unwind"; begin 879 (* RUN: grep {unwind} < %t.ll 880 *) 881 let bb05 = append_block context "Bb05" fn in 882 let b = builder_at_end context bb05 in 883 ignore (build_unwind b) 884 end; 885 886 group "unreachable"; begin 887 (* RUN: grep {unreachable} < %t.ll 888 *) 889 let bb06 = append_block context "Bb06" fn in 890 let b = builder_at_end context bb06 in 891 ignore (build_unreachable b) 892 end; 893 894 group "arithmetic"; begin 895 let bb07 = append_block context "Bb07" fn in 896 let b = builder_at_end context bb07 in 897 898 (* RUN: grep {Inst03.*add.*P1.*P2} < %t.ll 899 * RUN: grep {Inst04.*sub.*P1.*Inst03} < %t.ll 900 * RUN: grep {Inst05.*mul.*P1.*Inst04} < %t.ll 901 * RUN: grep {Inst06.*udiv.*P1.*Inst05} < %t.ll 902 * RUN: grep {Inst07.*sdiv.*P1.*Inst06} < %t.ll 903 * RUN: grep {Inst08.*fdiv.*F1.*F2} < %t.ll 904 * RUN: grep {Inst09.*urem.*P1.*Inst07} < %t.ll 905 * RUN: grep {Inst10.*srem.*P1.*Inst09} < %t.ll 906 * RUN: grep {Inst11.*frem.*F1.*Inst08} < %t.ll 907 * RUN: grep {Inst12.*shl.*P1.*Inst10} < %t.ll 908 * RUN: grep {Inst13.*lshr.*P1.*Inst12} < %t.ll 909 * RUN: grep {Inst14.*ashr.*P1.*Inst13} < %t.ll 910 * RUN: grep {Inst15.*and.*P1.*Inst14} < %t.ll 911 * RUN: grep {Inst16.*or.*P1.*Inst15} < %t.ll 912 * RUN: grep {Inst17.*xor.*P1.*Inst16} < %t.ll 913 * RUN: grep {Inst18.*sub.*0.*Inst17} < %t.ll 914 * RUN: grep {Inst19.*xor.*Inst18.*-1} < %t.ll 915 *) 916 let inst03 = build_add p1 p2 "Inst03" b in 917 let inst04 = build_sub p1 inst03 "Inst04" b in 918 let inst05 = build_mul p1 inst04 "Inst05" b in 919 let inst06 = build_udiv p1 inst05 "Inst06" b in 920 let inst07 = build_sdiv p1 inst06 "Inst07" b in 921 let inst08 = build_fdiv f1 f2 "Inst08" b in 922 let inst09 = build_urem p1 inst07 "Inst09" b in 923 let inst10 = build_srem p1 inst09 "Inst10" b in 924 ignore(build_frem f1 inst08 "Inst11" b); 925 let inst12 = build_shl p1 inst10 "Inst12" b in 926 let inst13 = build_lshr p1 inst12 "Inst13" b in 927 let inst14 = build_ashr p1 inst13 "Inst14" b in 928 let inst15 = build_and p1 inst14 "Inst15" b in 929 let inst16 = build_or p1 inst15 "Inst16" b in 930 let inst17 = build_xor p1 inst16 "Inst17" b in 931 let inst18 = build_neg inst17 "Inst18" b in 932 ignore (build_not inst18 "Inst19" b); 933 ignore (build_unreachable b) 934 end; 935 936 group "memory"; begin 937 let bb08 = append_block context "Bb08" fn in 938 let b = builder_at_end context bb08 in 939 940 (* RUN: grep {Inst22.*alloca.*i32 } < %t.ll 941 * RUN: grep {Inst23.*alloca.*i32.*P2} < %t.ll 942 * RUN: grep {Inst25.*load.*Inst23} < %t.ll 943 * RUN: grep {store.*P2.*Inst22} < %t.ll 944 * RUN: grep {Inst27.*getelementptr.*Inst23.*P2} < %t.ll 945 *) 946 let inst22 = build_alloca i32_type "Inst22" b in 947 let inst23 = build_array_alloca i32_type p2 "Inst23" b in 948 ignore(build_load inst23 "Inst25" b); 949 ignore(build_store p2 inst22 b); 950 ignore(build_gep inst23 [| p2 |] "Inst27" b); 951 ignore(build_unreachable b) 952 end; 953 954 group "casts"; begin 955 let void_ptr = pointer_type i8_type in 956 957 (* RUN: grep {Inst28.*trunc.*P1.*i8} < %t.ll 958 * RUN: grep {Inst29.*zext.*Inst28.*i32} < %t.ll 959 * RUN: grep {Inst30.*sext.*Inst29.*i64} < %t.ll 960 * RUN: grep {Inst31.*uitofp.*Inst30.*float} < %t.ll 961 * RUN: grep {Inst32.*sitofp.*Inst29.*double} < %t.ll 962 * RUN: grep {Inst33.*fptoui.*Inst31.*i32} < %t.ll 963 * RUN: grep {Inst34.*fptosi.*Inst32.*i64} < %t.ll 964 * RUN: grep {Inst35.*fptrunc.*Inst32.*float} < %t.ll 965 * RUN: grep {Inst36.*fpext.*Inst35.*double} < %t.ll 966 * RUN: grep {Inst37.*inttoptr.*P1.*i8\*} < %t.ll 967 * RUN: grep {Inst38.*ptrtoint.*Inst37.*i64} < %t.ll 968 * RUN: grep {Inst39.*bitcast.*Inst38.*double} < %t.ll 969 *) 970 let inst28 = build_trunc p1 i8_type "Inst28" atentry in 971 let inst29 = build_zext inst28 i32_type "Inst29" atentry in 972 let inst30 = build_sext inst29 i64_type "Inst30" atentry in 973 let inst31 = build_uitofp inst30 float_type "Inst31" atentry in 974 let inst32 = build_sitofp inst29 double_type "Inst32" atentry in 975 ignore(build_fptoui inst31 i32_type "Inst33" atentry); 976 ignore(build_fptosi inst32 i64_type "Inst34" atentry); 977 let inst35 = build_fptrunc inst32 float_type "Inst35" atentry in 978 ignore(build_fpext inst35 double_type "Inst36" atentry); 979 let inst37 = build_inttoptr p1 void_ptr "Inst37" atentry in 980 let inst38 = build_ptrtoint inst37 i64_type "Inst38" atentry in 981 ignore(build_bitcast inst38 double_type "Inst39" atentry) 982 end; 983 984 group "comparisons"; begin 985 (* RUN: grep {Inst40.*icmp.*ne.*P1.*P2} < %t.ll 986 * RUN: grep {Inst41.*icmp.*sle.*P2.*P1} < %t.ll 987 * RUN: grep {Inst42.*fcmp.*false.*F1.*F2} < %t.ll 988 * RUN: grep {Inst43.*fcmp.*true.*F2.*F1} < %t.ll 989 *) 990 ignore (build_icmp Icmp.Ne p1 p2 "Inst40" atentry); 991 ignore (build_icmp Icmp.Sle p2 p1 "Inst41" atentry); 992 ignore (build_fcmp Fcmp.False f1 f2 "Inst42" atentry); 993 ignore (build_fcmp Fcmp.True f2 f1 "Inst43" atentry) 994 end; 995 996 group "miscellaneous"; begin 997 (* RUN: grep {CallInst.*call.*P2.*P1} < %t.ll 998 * RUN: grep {CallInst.*cc63} < %t.ll 999 * RUN: grep {Inst47.*select.*Inst46.*P1.*P2} < %t.ll 1000 * RUN: grep {Inst48.*va_arg.*null.*i32} < %t.ll 1001 * RUN: grep {Inst49.*extractelement.*Vec1.*P2} < %t.ll 1002 * RUN: grep {Inst50.*insertelement.*Vec1.*P1.*P2} < %t.ll 1003 * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*1.*1.*0.*0} < %t.ll 1004 * RUN: grep {CallInst.*tail call} < %t.ll 1005 *) 1006 let ci = build_call fn [| p2; p1 |] "CallInst" atentry in 1007 insist (CallConv.c = instruction_call_conv ci); 1008 set_instruction_call_conv 63 ci; 1009 insist (63 = instruction_call_conv ci); 1010 insist (not (is_tail_call ci)); 1011 set_tail_call true ci; 1012 insist (is_tail_call ci); 1013 add_instruction_param_attr ci 1 Attribute.Sext; 1014 add_instruction_param_attr ci 2 Attribute.Noalias; 1015 remove_instruction_param_attr ci 2 Attribute.Noalias; 1016 1017 let inst46 = build_icmp Icmp.Eq p1 p2 "Inst46" atentry in 1018 ignore (build_select inst46 p1 p2 "Inst47" atentry); 1019 ignore (build_va_arg 1020 (const_null (pointer_type (pointer_type i8_type))) 1021 i32_type "Inst48" atentry); 1022 1023 (* Set up some vector vregs. *) 1024 let one = const_int i32_type 1 in 1025 let zero = const_int i32_type 0 in 1026 let t1 = const_vector [| one; zero; one; zero |] in 1027 let t2 = const_vector [| zero; one; zero; one |] in 1028 let t3 = const_vector [| one; one; zero; zero |] in 1029 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in 1030 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in 1031 1032 ignore (build_extractelement vec1 p2 "Inst49" atentry); 1033 ignore (build_insertelement vec1 p1 p2 "Inst50" atentry); 1034 ignore (build_shufflevector vec1 vec2 t3 "Inst51" atentry); 1035 end; 1036 1037 group "phi"; begin 1038 (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll 1039 *) 1040 let b1 = append_block context "PhiBlock1" fn in 1041 let b2 = append_block context "PhiBlock2" fn in 1042 1043 let jb = append_block context "PhiJoinBlock" fn in 1044 ignore (build_br jb (builder_at_end context b1)); 1045 ignore (build_br jb (builder_at_end context b2)); 1046 let at_jb = builder_at_end context jb in 1047 1048 let phi = build_phi [(p1, b1)] "PhiNode" at_jb in 1049 insist ([(p1, b1)] = incoming phi); 1050 1051 add_incoming (p2, b2) phi; 1052 insist ([(p1, b1); (p2, b2)] = incoming phi); 1053 1054 ignore (build_unreachable at_jb); 1055 end 1056 1057 1058(*===-- Module Provider ---------------------------------------------------===*) 1059 1060let test_module_provider () = 1061 let m = create_module context "test" in 1062 let mp = ModuleProvider.create m in 1063 ModuleProvider.dispose mp 1064 1065 1066(*===-- Pass Managers -----------------------------------------------------===*) 1067 1068let test_pass_manager () = 1069 let (++) x f = ignore (f x); x in 1070 1071 begin group "module pass manager"; 1072 ignore (PassManager.create () 1073 ++ PassManager.run_module m 1074 ++ PassManager.dispose) 1075 end; 1076 1077 begin group "function pass manager"; 1078 let fty = function_type void_type [| |] in 1079 let fn = define_function "FunctionPassManager" fty m in 1080 ignore (build_ret_void (builder_at_end context (entry_block fn))); 1081 1082 ignore (PassManager.create_function mp 1083 ++ PassManager.initialize 1084 ++ PassManager.run_function fn 1085 ++ PassManager.finalize 1086 ++ PassManager.dispose) 1087 end 1088 1089 1090(*===-- Writer ------------------------------------------------------------===*) 1091 1092let test_writer () = 1093 group "valid"; 1094 insist (match Llvm_analysis.verify_module m with 1095 | None -> true 1096 | Some msg -> prerr_string msg; false); 1097 1098 group "writer"; 1099 insist (write_bitcode_file m filename); 1100 1101 ModuleProvider.dispose mp 1102 1103 1104(*===-- Driver ------------------------------------------------------------===*) 1105 1106let _ = 1107 suite "target" test_target; 1108 suite "types" test_types; 1109 suite "constants" test_constants; 1110 suite "global values" test_global_values; 1111 suite "global variables" test_global_variables; 1112 suite "functions" test_functions; 1113 suite "params" test_params; 1114 suite "basic blocks" test_basic_blocks; 1115 suite "instructions" test_instructions; 1116 suite "builder" test_builder; 1117 suite "module provider" test_module_provider; 1118 suite "pass manager" test_pass_manager; 1119 suite "writer" test_writer; (* Keep this last; it disposes m. *) 1120 exit !exit_status 1121