166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman(* RUN: %ocamlopt -warn-error A unix.cmxa llvm.cmxa llvm_bitwriter.cmxa %s -o %t
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman * RUN: %t %t.bc
366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman * RUN: llvm-dis < %t.bc
466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman * XFAIL: vg_leak
566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman *)
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman(* Note that this takes a moment to link, so it's best to keep the number of
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman   individual tests low. *)
966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanlet context = Llvm.global_context ()
1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanlet test x = if not x then exit 1 else ()
1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanlet read_file name =
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let ic = open_in_bin name in
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let len = in_channel_length ic in
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let buf = String.create len in
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test ((input ic buf 0 len) = len);
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  close_in ic;
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  buf
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanlet temp_bitcode ?unbuffered m =
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let temp_name, temp_oc = Filename.open_temp_file ~mode:[Open_binary] "" "" in
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test (Llvm_bitwriter.output_bitcode ?unbuffered temp_oc m);
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  flush temp_oc;
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let temp_buf = read_file temp_name in
3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  close_out temp_oc;
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  temp_buf
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanlet _ =
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let m = Llvm.create_module context "ocaml_test_module" in
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  
4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1));
4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  let file_buf = read_file Sys.argv.(1) in
4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test (file_buf = temp_bitcode m);
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test (file_buf = temp_bitcode ~unbuffered:false m);
4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  test (file_buf = temp_bitcode ~unbuffered:true m)
46