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