bitreader.ml revision da1435f86ebc9886dd7704294e01d192d79e069c
1bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen(* RUN: %ocamlc llvm.cma llvm_bitreader.cma llvm_bitwriter.cma %s -o %t 2bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen * RUN: ./%t %t.bc 3bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen * RUN: llvm-dis < %t.bc | grep caml_int_ty 4bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen *) 5bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 6bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen(* Note that this takes a moment to link, so it's best to keep the number of 7bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen individual tests low. *) 8bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 9bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksenlet test x = if not x then exit 1 else () 10bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 11bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksenlet _ = 12bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen let fn = Sys.argv.(1) in 13bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen let m = Llvm.create_module "ocaml_test_module" in 14bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 15bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen ignore (Llvm.define_type_name "caml_int_ty" Llvm.i32_type m); 16bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 17bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen test (Llvm_bitwriter.write_bitcode_file m fn); 18bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 19bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen Llvm.dispose_module m; 20bbc6597f02d108f32b5bdc7711cc67386196017bGordon Henriksen 21da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen (* parse_bitcode *) 22da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen begin 23da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mb = Llvm.MemoryBuffer.of_file fn in 24da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen begin try 25da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let m = Llvm_bitreader.parse_bitcode mb in 26da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.dispose_module m 27da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen with x -> 28da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.MemoryBuffer.dispose; 29da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen raise x 30da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end 31da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end; 32da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen 33da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen (* MemoryBuffer.of_file *) 34da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen test begin try 35da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in 36da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.MemoryBuffer.dispose mb; 37da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen false 38da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen with Llvm.IoError _ -> 39da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen true 40da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end; 41da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen 42da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen (* get_module_provider *) 43da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen begin 44da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mb = Llvm.MemoryBuffer.of_file fn in 45da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mp = begin try 46da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm_bitreader.get_module_provider mb 47da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen with x -> 48da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.MemoryBuffer.dispose mb; 49da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen raise x 50da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end in 51da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.ModuleProvider.dispose mp 52da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end; 53da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen 54da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen (* corrupt the bitcode *) 55da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let fn = fn ^ ".txt" in 56da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen begin let oc = open_out fn in 57da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen output_string oc "not a bitcode file\n"; 58da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen close_out oc 59da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end; 60da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen 61da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen (* test get_module_provider exceptions *) 62da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen test begin 63da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen try 64da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mb = Llvm.MemoryBuffer.of_file fn in 65da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen let mp = begin try 66da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm_bitreader.get_module_provider mb 67da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen with x -> 68da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.MemoryBuffer.dispose mb; 69da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen raise x 70da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end in 71da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen Llvm.ModuleProvider.dispose mp; 72da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen false 73da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen with Llvm_bitreader.Error _ -> 74da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen true 75da1435f86ebc9886dd7704294e01d192d79e069cGordon Henriksen end 76