vmcore.ml revision 4647569fe7706e76135a08ca0e5f90a447ccc5b4
1(* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %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 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 case_num = ref 0 17 18let group name = 19 case_num := 0; 20 prerr_endline (" " ^ name ^ "...") 21 22let insist cond = 23 incr case_num; 24 let msg = if cond then " pass " else begin 25 exit_status := 10; 26 " FAIL " 27 end in 28 prerr_endline (msg ^ (string_of_int !case_num)) 29 30let suite name f = 31 prerr_endline (name ^ ":"); 32 f () 33 34 35(*===-- Fixture -----------------------------------------------------------===*) 36 37let filename = Sys.argv.(1) 38let m = create_module filename 39 40 41(*===-- Types -------------------------------------------------------------===*) 42 43let test_types () = 44 (* RUN: grep {Ty01.*void} < %t.ll 45 *) 46 group "void"; 47 insist (define_type_name "Ty01" void_type m); 48 insist (Void_type == classify_type void_type); 49 50 (* RUN: grep {Ty02.*i1} < %t.ll 51 *) 52 group "i1"; 53 insist (define_type_name "Ty02" i1_type m); 54 insist (Integer_type == classify_type i1_type); 55 56 (* RUN: grep {Ty03.*i32} < %t.ll 57 *) 58 group "i32"; 59 insist (define_type_name "Ty03" i32_type m); 60 61 (* RUN: grep {Ty04.*i42} < %t.ll 62 *) 63 group "i42"; 64 let ty = make_integer_type 42 in 65 insist (define_type_name "Ty04" ty m); 66 67 (* RUN: grep {Ty05.*float} < %t.ll 68 *) 69 group "float"; 70 insist (define_type_name "Ty05" float_type m); 71 insist (Float_type == classify_type float_type); 72 73 (* RUN: grep {Ty06.*double} < %t.ll 74 *) 75 group "double"; 76 insist (define_type_name "Ty06" double_type m); 77 insist (Double_type == classify_type double_type); 78 79 (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll 80 *) 81 group "function"; 82 let ty = make_function_type i32_type [| i1_type; double_type |] false in 83 insist (define_type_name "Ty07" ty m); 84 insist (Function_type = classify_type ty); 85 insist (not (is_var_arg ty)); 86 insist (i32_type == return_type ty); 87 insist (double_type == (param_types ty).(1)); 88 89 (* RUN: grep {Ty08.*\.\.\.} < %t.ll 90 *) 91 group "vararg"; 92 let ty = make_function_type void_type [| i32_type |] true in 93 insist (define_type_name "Ty08" ty m); 94 insist (is_var_arg ty); 95 96 (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll 97 *) 98 group "array"; 99 let ty = make_array_type i8_type 7 in 100 insist (define_type_name "Ty09" ty m); 101 insist (7 = array_length ty); 102 insist (i8_type == element_type ty); 103 insist (Array_type == classify_type ty); 104 105 (* RUN: grep {Ty10.*float\*} < %t.ll 106 *) 107 group "pointer"; 108 let ty = make_pointer_type float_type in 109 insist (define_type_name "Ty10" ty m); 110 insist (float_type == element_type ty); 111 insist (Pointer_type == classify_type ty); 112 113 (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll 114 *) 115 group "vector"; 116 let ty = make_vector_type i16_type 4 in 117 insist (define_type_name "Ty11" ty m); 118 insist (i16_type == element_type ty); 119 insist (4 = vector_size ty); 120 121 (* RUN: grep {Ty12.*opaque} < %t.ll 122 *) 123 group "opaque"; 124 let ty = make_opaque_type () in 125 insist (define_type_name "Ty12" ty m); 126 insist (ty == ty); 127 insist (ty <> make_opaque_type ()); 128 129 (* RUN: grep -v {Ty13} < %t.ll 130 *) 131 group "delete"; 132 let ty = make_opaque_type () in 133 insist (define_type_name "Ty13" ty m); 134 delete_type_name "Ty13" m 135 136 137(*===-- Constants ---------------------------------------------------------===*) 138 139let test_constants () = 140 (* RUN: grep {Const01.*i32.*-1} < %t.ll 141 *) 142 group "int"; 143 let c = make_int_constant i32_type (-1) true in 144 ignore (define_global "Const01" c m); 145 insist (i32_type = type_of c); 146 insist (is_constant c); 147 148 (* RUN: grep {Const02.*i64.*-1} < %t.ll 149 *) 150 group "sext int"; 151 let c = make_int_constant i64_type (-1) true in 152 ignore (define_global "Const02" c m); 153 insist (i64_type = type_of c); 154 155 (* RUN: grep {Const03.*i64.*4294967295} < %t.ll 156 *) 157 group "zext int64"; 158 let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in 159 ignore (define_global "Const03" c m); 160 insist (i64_type = type_of c); 161 162 (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll 163 *) 164 group "string"; 165 let c = make_string_constant "cruel\000world" false in 166 ignore (define_global "Const04" c m); 167 insist ((make_array_type i8_type 11) = type_of c); 168 169 (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll 170 *) 171 group "string w/ null"; 172 let c = make_string_constant "hi\000again" true in 173 ignore (define_global "Const05" c m); 174 insist ((make_array_type i8_type 9) = type_of c); 175 176 (* RUN: grep {Const06.*3.1459} < %t.ll 177 *) 178 group "real"; 179 let c = make_real_constant double_type 3.1459 in 180 ignore (define_global "Const06" c m); 181 insist (double_type = type_of c); 182 183 let one = make_int_constant i16_type 1 true in 184 let two = make_int_constant i16_type 2 true in 185 let three = make_int_constant i32_type 3 true in 186 let four = make_int_constant i32_type 4 true in 187 188 (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll 189 *) 190 group "array"; 191 let c = make_array_constant i32_type [| three; four |] in 192 ignore (define_global "Const07" c m); 193 insist ((make_array_type i32_type 2) = (type_of c)); 194 195 (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll 196 *) 197 group "vector"; 198 let c = make_vector_constant [| one; two; one; two; 199 one; two; one; two |] in 200 ignore (define_global "Const08" c m); 201 insist ((make_vector_type i16_type 8) = (type_of c)); 202 203 (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll 204 *) 205 group "structure"; 206 let c = make_struct_constant [| one; two; three; four |] false in 207 ignore (define_global "Const09" c m); 208 insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false) 209 = (type_of c)); 210 211 (* RUN: grep {Const10.*zeroinit} < %t.ll 212 *) 213 group "null"; 214 let c = make_null (make_struct_type [| i1_type; i8_type; 215 i64_type; double_type |] true) in 216 ignore (define_global "Const10" c m); 217 218 (* RUN: grep {Const11.*-1} < %t.ll 219 *) 220 group "all ones"; 221 let c = make_all_ones i64_type in 222 ignore (define_global "Const11" c m); 223 224 (* RUN: grep {Const12.*undef} < %t.ll 225 *) 226 group "undef"; 227 let c = make_undef i1_type in 228 ignore (define_global "Const12" c m); 229 insist (i1_type = type_of c); 230 insist (is_undef c); 231 232 group "constant arithmetic"; 233 (* RUN: grep {ConstNeg.*sub} < %t.ll 234 * RUN: grep {ConstNot.*xor} < %t.ll 235 * RUN: grep {ConstAdd.*add} < %t.ll 236 * RUN: grep {ConstSub.*sub} < %t.ll 237 * RUN: grep {ConstMul.*mul} < %t.ll 238 * RUN: grep {ConstUDiv.*udiv} < %t.ll 239 * RUN: grep {ConstSDiv.*sdiv} < %t.ll 240 * RUN: grep {ConstFDiv.*fdiv} < %t.ll 241 * RUN: grep {ConstURem.*urem} < %t.ll 242 * RUN: grep {ConstSRem.*srem} < %t.ll 243 * RUN: grep {ConstFRem.*frem} < %t.ll 244 * RUN: grep {ConstAnd.*and} < %t.ll 245 * RUN: grep {ConstOr.*or} < %t.ll 246 * RUN: grep {ConstXor.*xor} < %t.ll 247 * RUN: grep {ConstICmp.*icmp} < %t.ll 248 * RUN: grep {ConstFCmp.*fcmp} < %t.ll 249 *) 250 let void_ptr = make_pointer_type i8_type in 251 let five = make_int_constant i64_type 5 false in 252 let ffive = const_uitofp five double_type in 253 let foldbomb_gv = define_global "FoldBomb" (make_null i8_type) m in 254 let foldbomb = const_ptrtoint foldbomb_gv i64_type in 255 let ffoldbomb = const_uitofp foldbomb double_type in 256 ignore (define_global "ConstNeg" (const_neg foldbomb) m); 257 ignore (define_global "ConstNot" (const_not foldbomb) m); 258 ignore (define_global "ConstAdd" (const_add foldbomb five) m); 259 ignore (define_global "ConstSub" (const_sub foldbomb five) m); 260 ignore (define_global "ConstMul" (const_mul foldbomb five) m); 261 ignore (define_global "ConstUDiv" (const_udiv foldbomb five) m); 262 ignore (define_global "ConstSDiv" (const_sdiv foldbomb five) m); 263 ignore (define_global "ConstFDiv" (const_fdiv ffoldbomb ffive) m); 264 ignore (define_global "ConstURem" (const_urem foldbomb five) m); 265 ignore (define_global "ConstSRem" (const_srem foldbomb five) m); 266 ignore (define_global "ConstFRem" (const_frem ffoldbomb ffive) m); 267 ignore (define_global "ConstAnd" (const_and foldbomb five) m); 268 ignore (define_global "ConstOr" (const_or foldbomb five) m); 269 ignore (define_global "ConstXor" (const_xor foldbomb five) m); 270 ignore (define_global "ConstICmp" (const_icmp Icmp_sle foldbomb five) m); 271 ignore (define_global "ConstFCmp" (const_fcmp Fcmp_ole ffoldbomb ffive) m); 272 273 group "constant casts"; 274 (* RUN: grep {ConstTrunc.*trunc} < %t.ll 275 * RUN: grep {ConstSExt.*sext} < %t.ll 276 * RUN: grep {ConstZExt.*zext} < %t.ll 277 * RUN: grep {ConstFPTrunc.*fptrunc} < %t.ll 278 * RUN: grep {ConstFPExt.*fpext} < %t.ll 279 * RUN: grep {ConstUIToFP.*uitofp} < %t.ll 280 * RUN: grep {ConstSIToFP.*sitofp} < %t.ll 281 * RUN: grep {ConstFPToUI.*fptoui} < %t.ll 282 * RUN: grep {ConstFPToSI.*fptosi} < %t.ll 283 * RUN: grep {ConstPtrToInt.*ptrtoint} < %t.ll 284 * RUN: grep {ConstIntToPtr.*inttoptr} < %t.ll 285 * RUN: grep {ConstBitCast.*bitcast} < %t.ll 286 *) 287 let i128_type = make_integer_type 128 in 288 ignore (define_global "ConstTrunc" (const_trunc (const_add foldbomb five) 289 i8_type) m); 290 ignore (define_global "ConstSExt" (const_sext foldbomb i128_type) m); 291 ignore (define_global "ConstZExt" (const_zext foldbomb i128_type) m); 292 ignore (define_global "ConstFPTrunc" (const_fptrunc ffoldbomb float_type) m); 293 ignore (define_global "ConstFPExt" (const_fpext ffoldbomb fp128_type) m); 294 ignore (define_global "ConstUIToFP" (const_uitofp foldbomb double_type) m); 295 ignore (define_global "ConstSIToFP" (const_sitofp foldbomb double_type) m); 296 ignore (define_global "ConstFPToUI" (const_fptoui ffoldbomb i32_type) m); 297 ignore (define_global "ConstFPToSI" (const_fptosi ffoldbomb i32_type) m); 298 ignore (define_global "ConstPtrToInt" (const_ptrtoint 299 (const_gep (make_null (make_pointer_type i8_type)) 300 [| make_int_constant i32_type 1 false |]) 301 i32_type) m); 302 ignore (define_global "ConstIntToPtr" (const_inttoptr (const_add foldbomb five) 303 void_ptr) m); 304 ignore (define_global "ConstBitCast" (const_bitcast ffoldbomb i64_type) m); 305 306 group "misc constants"; 307 (* RUN: grep {ConstSizeOf.*getelementptr.*null} < %t.ll 308 * RUN: grep {ConstGEP.*getelementptr} < %t.ll 309 * RUN: grep {ConstSelect.*select} < %t.ll 310 * RUN: grep {ConstExtractElement.*extractelement} < %t.ll 311 * RUN: grep {ConstInsertElement.*insertelement} < %t.ll 312 * RUN: grep {ConstShuffleVector.*shufflevector} < %t.ll 313 *) 314 ignore (define_global "ConstSizeOf" (sizeof (make_pointer_type i8_type)) m); 315 ignore (define_global "ConstGEP" (const_gep foldbomb_gv [| five |]) m); 316 ignore (define_global "ConstSelect" (const_select 317 (const_icmp Icmp_sle foldbomb five) 318 (make_int_constant i8_type (-1) true) 319 (make_int_constant i8_type 0 true)) m); 320 let zero = make_int_constant i32_type 0 false in 321 let one = make_int_constant i32_type 1 false in 322 ignore (define_global "ConstExtractElement" (const_extractelement 323 (make_vector_constant [| zero; one; zero; one |]) 324 (const_trunc foldbomb i32_type)) m); 325 ignore (define_global "ConstInsertElement" (const_insertelement 326 (make_vector_constant [| zero; one; zero; one |]) 327 zero (const_trunc foldbomb i32_type)) m); 328 ignore (define_global "ConstShuffleVector" (const_shufflevector 329 (make_vector_constant [| zero; one |]) 330 (make_vector_constant [| one; zero |]) 331 (const_bitcast foldbomb (make_vector_type i32_type 2))) m) 332 333 334(*===-- Global Values -----------------------------------------------------===*) 335 336let test_global_values () = 337 let (++) x f = f x; x in 338 let zero32 = make_null i32_type in 339 340 (* RUN: grep {GVal01} < %t.ll 341 *) 342 group "naming"; 343 let g = define_global "TEMPORARY" zero32 m in 344 insist ("TEMPORARY" = value_name g); 345 set_value_name "GVal01" g; 346 insist ("GVal01" = value_name g); 347 348 (* RUN: grep {GVal02.*linkonce} < %t.ll 349 *) 350 group "linkage"; 351 let g = define_global "GVal02" zero32 m ++ 352 set_linkage Link_once_linkage in 353 insist (Link_once_linkage = linkage g); 354 355 (* RUN: grep {GVal03.*Hanalei} < %t.ll 356 *) 357 group "section"; 358 let g = define_global "GVal03" zero32 m ++ 359 set_section "Hanalei" in 360 insist ("Hanalei" = section g); 361 362 (* RUN: grep {GVal04.*hidden} < %t.ll 363 *) 364 group "visibility"; 365 let g = define_global "GVal04" zero32 m ++ 366 set_visibility Hidden_visibility in 367 insist (Hidden_visibility = visibility g); 368 369 (* RUN: grep {GVal05.*align 128} < %t.ll 370 *) 371 group "alignment"; 372 let g = define_global "GVal05" zero32 m ++ 373 set_alignment 128 in 374 insist (128 = alignment g) 375 376 377(*===-- Global Variables --------------------------------------------------===*) 378 379let test_global_variables () = 380 let (++) x f = f x; x in 381 let fourty_two32 = make_int_constant i32_type 42 false in 382 383 (* RUN: grep {GVar01.*external} < %t.ll 384 *) 385 group "declarations"; 386 let g = declare_global i32_type "GVar01" m in 387 insist (is_declaration g); 388 389 (* RUN: grep {GVar02.*42} < %t.ll 390 * RUN: grep {GVar03.*42} < %t.ll 391 *) 392 group "definitions"; 393 let g = define_global "GVar02" fourty_two32 m in 394 let g2 = declare_global i32_type "GVar03" m ++ 395 set_initializer fourty_two32 in 396 insist (not (is_declaration g)); 397 insist (not (is_declaration g2)); 398 insist ((global_initializer g) == (global_initializer g2)); 399 400 (* RUN: grep {GVar04.*thread_local} < %t.ll 401 *) 402 group "threadlocal"; 403 let g = define_global "GVar04" fourty_two32 m ++ 404 set_thread_local true in 405 insist (is_thread_local g); 406 407 (* RUN: grep -v {GVar05} < %t.ll 408 *) 409 group "delete"; 410 let g = define_global "GVar05" fourty_two32 m in 411 delete_global g 412 413 414(*===-- Functions ---------------------------------------------------------===*) 415 416let test_functions () = 417 let ty = make_function_type i32_type [| i32_type; i64_type |] false in 418 let pty = make_pointer_type ty in 419 420 (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll 421 *) 422 group "declare"; 423 let fn = declare_function "Fn1" ty m in 424 insist (pty = type_of fn); 425 insist (is_declaration fn); 426 insist (0 = Array.length (basic_blocks fn)); 427 428 (* RUN: grep -v {Fn2} < %t.ll 429 *) 430 group "delete"; 431 let fn = declare_function "Fn2" ty m in 432 delete_function fn; 433 434 (* RUN: grep {define.*Fn3} < %t.ll 435 *) 436 group "define"; 437 let fn = define_function "Fn3" ty m in 438 insist (not (is_declaration fn)); 439 insist (1 = Array.length (basic_blocks fn)); 440 (* this function is not valid because init bb lacks a terminator *) 441 442 (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll 443 *) 444 group "params"; 445 let fn = define_function "Fn4" ty m in 446 let params = params fn in 447 insist (2 = Array.length params); 448 insist (params.(0) = param fn 0); 449 insist (params.(1) = param fn 1); 450 insist (i32_type = type_of params.(0)); 451 insist (i64_type = type_of params.(1)); 452 set_value_name "Param1" params.(0); 453 set_value_name "Param2" params.(1); 454 (* this function is not valid because init bb lacks a terminator *) 455 456 (* RUN: grep {fastcc.*Fn5} < %t.ll 457 *) 458 group "callconv"; 459 let fn = define_function "Fn5" ty m in 460 insist (ccc = function_call_conv fn); 461 set_function_call_conv fastcc fn; 462 insist (fastcc = function_call_conv fn) 463 464 465(*===-- Basic Blocks ------------------------------------------------------===*) 466 467let test_basic_blocks () = 468 let ty = make_function_type void_type [| |] false in 469 470 (* RUN: grep {Bb1} < %t.ll 471 *) 472 group "entry"; 473 let fn = declare_function "X" ty m in 474 let bb = append_block "Bb1" fn in 475 insist (bb = entry_block fn); 476 477 (* RUN: grep -v Bb2 < %t.ll 478 *) 479 group "delete"; 480 let fn = declare_function "X2" ty m in 481 let bb = append_block "Bb2" fn in 482 delete_block bb; 483 484 group "insert"; 485 let fn = declare_function "X3" ty m in 486 let bbb = append_block "" fn in 487 let bba = insert_block "" bbb in 488 insist ([| bba; bbb |] = basic_blocks fn); 489 490 (* RUN: grep Bb3 < %t.ll 491 *) 492 group "name/value"; 493 let fn = define_function "X4" ty m in 494 let bb = entry_block fn in 495 let bbv = value_of_block bb in 496 set_value_name "Bb3" bbv; 497 insist ("Bb3" = value_name bbv); 498 499 group "casts"; 500 let fn = define_function "X5" ty m in 501 let bb = entry_block fn in 502 insist (bb = block_of_value (value_of_block bb)); 503 insist (value_is_block (value_of_block bb)); 504 insist (not (value_is_block (make_null i32_type))) 505 506 507(*===-- Builder -----------------------------------------------------------===*) 508 509let test_builder () = 510 let (++) x f = f x; x in 511 512 group "ret void"; 513 begin 514 (* RUN: grep {ret void} < %t.ll 515 *) 516 let fty = make_function_type void_type [| |] false in 517 let fn = declare_function "X6" fty m in 518 let b = builder_at_end (append_block "Bb01" fn) in 519 ignore (build_ret_void b) 520 end; 521 522 (* The rest of the tests will use one big function. *) 523 let fty = make_function_type i32_type [| i32_type; i32_type |] false in 524 let fn = define_function "X7" fty m in 525 let atentry = builder_at_end (entry_block fn) in 526 let p1 = param fn 0 ++ set_value_name "P1" in 527 let p2 = param fn 1 ++ set_value_name "P2" in 528 let f1 = build_uitofp p1 float_type "F1" atentry in 529 let f2 = build_uitofp p2 float_type "F2" atentry in 530 531 let bb00 = append_block "Bb00" fn in 532 ignore (build_unreachable (builder_at_end bb00)); 533 534 group "ret"; begin 535 (* RUN: grep {ret.*P1} < %t.ll 536 *) 537 let ret = build_ret p1 atentry in 538 position_before ret atentry 539 end; 540 541 group "br"; begin 542 (* RUN: grep {br.*Bb02} < %t.ll 543 *) 544 let bb02 = append_block "Bb02" fn in 545 let b = builder_at_end bb02 in 546 ignore (build_br bb02 b) 547 end; 548 549 group "cond_br"; begin 550 (* RUN: grep {br.*Inst01.*Bb03.*Bb00} < %t.ll 551 *) 552 let bb03 = append_block "Bb03" fn in 553 let b = builder_at_end bb03 in 554 let cond = build_trunc p1 i1_type "Inst01" b in 555 ignore (build_cond_br cond bb03 bb00 b) 556 end; 557 558 (* TODO: Switch *) 559 560 group "invoke"; begin 561 (* RUN: grep {Inst02.*invoke.*P1.*P2} < %t.ll 562 * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll 563 *) 564 let bb04 = append_block "Bb04" fn in 565 let b = builder_at_end bb04 in 566 ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "Inst02" b) 567 end; 568 569 group "unwind"; begin 570 (* RUN: grep {unwind} < %t.ll 571 *) 572 let bb05 = append_block "Bb05" fn in 573 let b = builder_at_end bb05 in 574 ignore (build_unwind b) 575 end; 576 577 group "unreachable"; begin 578 (* RUN: grep {unreachable} < %t.ll 579 *) 580 let bb06 = append_block "Bb06" fn in 581 let b = builder_at_end bb06 in 582 ignore (build_unreachable b) 583 end; 584 585 group "arithmetic"; begin 586 let bb07 = append_block "Bb07" fn in 587 let b = builder_at_end bb07 in 588 589 (* RUN: grep {Inst03.*add.*P1.*P2} < %t.ll 590 * RUN: grep {Inst04.*sub.*P1.*Inst03} < %t.ll 591 * RUN: grep {Inst05.*mul.*P1.*Inst04} < %t.ll 592 * RUN: grep {Inst06.*udiv.*P1.*Inst05} < %t.ll 593 * RUN: grep {Inst07.*sdiv.*P1.*Inst06} < %t.ll 594 * RUN: grep {Inst08.*fdiv.*F1.*F2} < %t.ll 595 * RUN: grep {Inst09.*urem.*P1.*Inst07} < %t.ll 596 * RUN: grep {Inst10.*srem.*P1.*Inst09} < %t.ll 597 * RUN: grep {Inst11.*frem.*F1.*Inst08} < %t.ll 598 * RUN: grep {Inst12.*shl.*P1.*Inst10} < %t.ll 599 * RUN: grep {Inst13.*lshr.*P1.*Inst12} < %t.ll 600 * RUN: grep {Inst14.*ashr.*P1.*Inst13} < %t.ll 601 * RUN: grep {Inst15.*and.*P1.*Inst14} < %t.ll 602 * RUN: grep {Inst16.*or.*P1.*Inst15} < %t.ll 603 * RUN: grep {Inst17.*xor.*P1.*Inst16} < %t.ll 604 * RUN: grep {Inst18.*sub.*0.*Inst17} < %t.ll 605 * RUN: grep {Inst19.*xor.*Inst18.*-1} < %t.ll 606 *) 607 let inst03 = build_add p1 p2 "Inst03" b in 608 let inst04 = build_sub p1 inst03 "Inst04" b in 609 let inst05 = build_mul p1 inst04 "Inst05" b in 610 let inst06 = build_udiv p1 inst05 "Inst06" b in 611 let inst07 = build_sdiv p1 inst06 "Inst07" b in 612 let inst08 = build_fdiv f1 f2 "Inst08" b in 613 let inst09 = build_urem p1 inst07 "Inst09" b in 614 let inst10 = build_srem p1 inst09 "Inst10" b in 615 ignore(build_frem f1 inst08 "Inst11" b); 616 let inst12 = build_shl p1 inst10 "Inst12" b in 617 let inst13 = build_lshr p1 inst12 "Inst13" b in 618 let inst14 = build_ashr p1 inst13 "Inst14" b in 619 let inst15 = build_and p1 inst14 "Inst15" b in 620 let inst16 = build_or p1 inst15 "Inst16" b in 621 let inst17 = build_xor p1 inst16 "Inst17" b in 622 let inst18 = build_neg inst17 "Inst18" b in 623 ignore (build_not inst18 "Inst19" b) 624 end; 625 626 group "memory"; begin 627 let bb08 = append_block "Bb08" fn in 628 let b = builder_at_end bb08 in 629 630 (* RUN: grep {Inst20.*malloc.*i8 } < %t.ll 631 * RUN: grep {Inst21.*malloc.*i8.*P1} < %t.ll 632 * RUN: grep {Inst22.*alloca.*i32 } < %t.ll 633 * RUN: grep {Inst23.*alloca.*i32.*P2} < %t.ll 634 * RUN: grep {free.*Inst20} < %t.ll 635 * RUN: grep {Inst25.*load.*Inst21} < %t.ll 636 * RUN: grep {store.*P2.*Inst22} < %t.ll 637 * RUN: grep {Inst27.*getelementptr.*Inst23.*P2} < %t.ll 638 *) 639 let inst20 = build_malloc i8_type "Inst20" b in 640 let inst21 = build_array_malloc i8_type p1 "Inst21" b in 641 let inst22 = build_alloca i32_type "Inst22" b in 642 let inst23 = build_array_alloca i32_type p2 "Inst23" b in 643 ignore(build_free inst20 b); 644 ignore(build_load inst21 "Inst25" b); 645 ignore(build_store p2 inst22 b); 646 ignore(build_gep inst23 [| p2 |] "Inst27" b) 647 end; 648 649 group "casts"; begin 650 let void_ptr = make_pointer_type i8_type in 651 652 (* RUN: grep {Inst28.*trunc.*P1.*i8} < %t.ll 653 * RUN: grep {Inst29.*zext.*Inst28.*i32} < %t.ll 654 * RUN: grep {Inst30.*sext.*Inst29.*i64} < %t.ll 655 * RUN: grep {Inst31.*uitofp.*Inst30.*float} < %t.ll 656 * RUN: grep {Inst32.*sitofp.*Inst29.*double} < %t.ll 657 * RUN: grep {Inst33.*fptoui.*Inst31.*i32} < %t.ll 658 * RUN: grep {Inst34.*fptosi.*Inst32.*i64} < %t.ll 659 * RUN: grep {Inst35.*fptrunc.*Inst32.*float} < %t.ll 660 * RUN: grep {Inst36.*fpext.*Inst35.*double} < %t.ll 661 * RUN: grep {Inst37.*inttoptr.*P1.*i8\*} < %t.ll 662 * RUN: grep {Inst38.*ptrtoint.*Inst37.*i64} < %t.ll 663 * RUN: grep {Inst39.*bitcast.*Inst38.*double} < %t.ll 664 *) 665 let inst28 = build_trunc p1 i8_type "Inst28" atentry in 666 let inst29 = build_zext inst28 i32_type "Inst29" atentry in 667 let inst30 = build_sext inst29 i64_type "Inst30" atentry in 668 let inst31 = build_uitofp inst30 float_type "Inst31" atentry in 669 let inst32 = build_sitofp inst29 double_type "Inst32" atentry in 670 ignore(build_fptoui inst31 i32_type "Inst33" atentry); 671 ignore(build_fptosi inst32 i64_type "Inst34" atentry); 672 let inst35 = build_fptrunc inst32 float_type "Inst35" atentry in 673 ignore(build_fpext inst35 double_type "Inst36" atentry); 674 let inst37 = build_inttoptr p1 void_ptr "Inst37" atentry in 675 let inst38 = build_ptrtoint inst37 i64_type "Inst38" atentry in 676 ignore(build_bitcast inst38 double_type "Inst39" atentry) 677 end; 678 679 group "comparisons"; begin 680 (* RUN: grep {Inst40.*icmp.*ne.*P1.*P2} < %t.ll 681 * RUN: grep {Inst41.*icmp.*sle.*P2.*P1} < %t.ll 682 * RUN: grep {Inst42.*fcmp.*false.*F1.*F2} < %t.ll 683 * RUN: grep {Inst43.*fcmp.*true.*F2.*F1} < %t.ll 684 *) 685 ignore (build_icmp Icmp_ne p1 p2 "Inst40" atentry); 686 ignore (build_icmp Icmp_sle p2 p1 "Inst41" atentry); 687 ignore (build_fcmp Fcmp_false f1 f2 "Inst42" atentry); 688 ignore (build_fcmp Fcmp_true f2 f1 "Inst43" atentry) 689 end; 690 691 group "miscellaneous"; begin 692 (* RUN: grep {Inst45.*call.*P2.*P1} < %t.ll 693 * RUN: grep {Inst47.*select.*Inst46.*P1.*P2} < %t.ll 694 * RUN: grep {Inst48.*va_arg.*null.*i32} < %t.ll 695 * RUN: grep {Inst49.*extractelement.*Vec1.*P2} < %t.ll 696 * RUN: grep {Inst50.*insertelement.*Vec1.*P1.*P2} < %t.ll 697 * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*Vec3} < %t.ll 698 *) 699 700 (* TODO: %Inst44 = Phi *) 701 702 ignore (build_call fn [| p2; p1 |] "Inst45" atentry); 703 let inst46 = build_icmp Icmp_eq p1 p2 "Inst46" atentry in 704 ignore (build_select inst46 p1 p2 "Inst47" atentry); 705 ignore (build_va_arg 706 (make_null (make_pointer_type (make_pointer_type i8_type))) 707 i32_type "Inst48" atentry); 708 709 (* Set up some vector vregs. *) 710 let one = make_int_constant i32_type (-1) true in 711 let zero = make_int_constant i32_type 1 true in 712 let t1 = make_vector_constant [| one; zero; one; zero |] in 713 let t2 = make_vector_constant [| zero; one; zero; one |] in 714 let t3 = make_vector_constant [| one; one; zero; zero |] in 715 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in 716 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in 717 let vec3 = build_insertelement t3 p1 p2 "Vec3" atentry in 718 719 ignore (build_extractelement vec1 p2 "Inst49" atentry); 720 ignore (build_insertelement vec1 p1 p2 "Inst50" atentry); 721 ignore (build_shufflevector vec1 vec2 vec3 "Inst51" atentry); 722 end 723 724 725(*===-- Writer ------------------------------------------------------------===*) 726 727let test_writer () = 728 group "writer"; 729 insist (write_bitcode_file m filename); 730 731 dispose_module m 732 733 734(*===-- Driver ------------------------------------------------------------===*) 735 736let _ = 737 suite "types" test_types; 738 suite "constants" test_constants; 739 suite "global values" test_global_values; 740 suite "global variables" test_global_variables; 741 suite "functions" test_functions; 742 suite "basic blocks" test_basic_blocks; 743 suite "builder" test_builder; 744 suite "writer" test_writer; 745 exit !exit_status 746