vmcore.ml revision 344be5fbecec9908bab611eafeae0549ba3be6d7
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 (add_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 (add_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 (add_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 (add_type_name "Ty04" ty m); 66 67 (* RUN: grep {Ty05.*float} < %t.ll 68 *) 69 group "float"; 70 insist (add_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 (add_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 (add_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 (add_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 (add_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 (add_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 (add_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 (add_type_name "Ty12" ty m); 126 insist (ty == ty); 127 insist (ty <> make_opaque_type ()) 128 129 130(*===-- Constants ---------------------------------------------------------===*) 131 132let test_constants () = 133 (* RUN: grep {Const01.*i32.*-1} < %t.ll 134 *) 135 group "int"; 136 let c = make_int_constant i32_type (-1) true in 137 ignore (define_global "Const01" c m); 138 insist (i32_type = type_of c); 139 insist (is_constant c); 140 141 (* RUN: grep {Const02.*i64.*-1} < %t.ll 142 *) 143 group "sext int"; 144 let c = make_int_constant i64_type (-1) true in 145 ignore (define_global "Const02" c m); 146 insist (i64_type = type_of c); 147 148 (* RUN: grep {Const03.*i64.*4294967295} < %t.ll 149 *) 150 group "zext int64"; 151 let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in 152 ignore (define_global "Const03" c m); 153 insist (i64_type = type_of c); 154 155 (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll 156 *) 157 group "string"; 158 let c = make_string_constant "cruel\x00world" false in 159 ignore (define_global "Const04" c m); 160 insist ((make_array_type i8_type 11) = type_of c); 161 162 (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll 163 *) 164 group "string w/ null"; 165 let c = make_string_constant "hi\x00again" true in 166 ignore (define_global "Const05" c m); 167 insist ((make_array_type i8_type 9) = type_of c); 168 169 (* RUN: grep {Const06.*3.1459} < %t.ll 170 *) 171 group "real"; 172 let c = make_real_constant double_type 3.1459 in 173 ignore (define_global "Const06" c m); 174 insist (double_type = type_of c); 175 176 let one = make_int_constant i16_type 1 true in 177 let two = make_int_constant i16_type 2 true in 178 let three = make_int_constant i32_type 3 true in 179 let four = make_int_constant i32_type 4 true in 180 181 (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll 182 *) 183 group "array"; 184 let c = make_array_constant i32_type [| three; four |] in 185 ignore (define_global "Const07" c m); 186 insist ((make_array_type i32_type 2) = (type_of c)); 187 188 (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll 189 *) 190 group "vector"; 191 let c = make_vector_constant [| one; two; one; two; 192 one; two; one; two |] in 193 ignore (define_global "Const08" c m); 194 insist ((make_vector_type i16_type 8) = (type_of c)); 195 196 (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll 197 *) 198 group "structure"; 199 let c = make_struct_constant [| one; two; three; four |] false in 200 ignore (define_global "Const09" c m); 201 insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false) 202 = (type_of c)); 203 204 (* RUN: grep {Const10.*zeroinit} < %t.ll 205 *) 206 group "null"; 207 let c = make_null (make_struct_type [| i1_type; i8_type; 208 i64_type; double_type |] true) in 209 ignore (define_global "Const10" c m); 210 211 (* RUN: grep {Const11.*-1} < %t.ll 212 *) 213 group "all ones"; 214 let c = make_all_ones i64_type in 215 ignore (define_global "Const11" c m); 216 217 (* RUN: grep {Const12.*undef} < %t.ll 218 *) 219 group "undef"; 220 let c = make_undef i1_type in 221 ignore (define_global "Const12" c m); 222 insist (i1_type = type_of c); 223 insist (is_undef c) 224 225 226(*===-- Global Values -----------------------------------------------------===*) 227 228let test_global_values () = 229 let (++) x f = f x; x in 230 let zero32 = make_null i32_type in 231 232 (* RUN: grep {GVal01} < %t.ll 233 *) 234 group "naming"; 235 let g = define_global "TEMPORARY" zero32 m in 236 insist ("TEMPORARY" = value_name g); 237 set_value_name "GVal01" g; 238 insist ("GVal01" = value_name g); 239 240 (* RUN: grep {GVal02.*linkonce} < %t.ll 241 *) 242 group "linkage"; 243 let g = define_global "GVal02" zero32 m ++ 244 set_linkage Link_once_linkage in 245 insist (Link_once_linkage = linkage g); 246 247 (* RUN: grep {GVal03.*Hanalei} < %t.ll 248 *) 249 group "section"; 250 let g = define_global "GVal03" zero32 m ++ 251 set_section "Hanalei" in 252 insist ("Hanalei" = section g); 253 254 (* RUN: grep {GVal04.*hidden} < %t.ll 255 *) 256 group "visibility"; 257 let g = define_global "GVal04" zero32 m ++ 258 set_visibility Hidden_visibility in 259 insist (Hidden_visibility = visibility g); 260 261 (* RUN: grep {GVal05.*align 128} < %t.ll 262 *) 263 group "alignment"; 264 let g = define_global "GVal05" zero32 m ++ 265 set_alignment 128 in 266 insist (128 = alignment g) 267 268 269(*===-- Global Variables --------------------------------------------------===*) 270 271let test_global_variables () = 272 let (++) x f = f x; x in 273 let fourty_two32 = make_int_constant i32_type 42 false in 274 275 (* RUN: grep {GVar01.*external} < %t.ll 276 *) 277 group "declarations"; 278 let g = declare_global i32_type "GVar01" m in 279 insist (is_declaration g); 280 281 (* RUN: grep {GVar02.*42} < %t.ll 282 * RUN: grep {GVar03.*42} < %t.ll 283 *) 284 group "definitions"; 285 let g = define_global "GVar02" fourty_two32 m in 286 let g2 = declare_global i32_type "GVar03" m ++ 287 set_initializer fourty_two32 in 288 insist (not (is_declaration g)); 289 insist (not (is_declaration g2)); 290 insist ((global_initializer g) == (global_initializer g2)); 291 292 (* RUN: grep {GVar04.*thread_local} < %t.ll 293 *) 294 group "threadlocal"; 295 let g = define_global "GVar04" fourty_two32 m ++ 296 set_thread_local true in 297 insist (is_thread_local g); 298 299 (* RUN: grep -v {GVar05} < %t.ll 300 *) 301 group "delete"; 302 let g = define_global "GVar05" fourty_two32 m in 303 delete_global g 304 305 306(*===-- Writer ------------------------------------------------------------===*) 307 308let test_writer () = 309 group "writer"; 310 insist (write_bitcode_file m filename); 311 312 dispose_module m 313 314 315(*===-- Driver ------------------------------------------------------------===*) 316 317let _ = 318 suite "types" test_types; 319 suite "constants" test_constants; 320 suite "global values" test_global_values; 321 suite "global variables" test_global_variables; 322 suite "writer" test_writer; 323 exit !exit_status 324