Skip to content

Commit e623050

Browse files
author
Erick Tryzelaar
committed
Add a LLVMWriteBitcodeToFD that exposes the raw_fd_ostream options.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97858 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b3195fb commit e623050

File tree

6 files changed

+80
-10
lines changed

6 files changed

+80
-10
lines changed

bindings/ocaml/bitwriter/bitwriter_ocaml.c

+15
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ CAMLprim value llvm_write_bitcode_file(value M, value Path) {
2828
int res = LLVMWriteBitcodeToFile((LLVMModuleRef) M, String_val(Path));
2929
return Val_bool(res == 0);
3030
}
31+
32+
/* ?unbuffered:bool -> Llvm.llmodule -> Unix.file_descr -> bool */
33+
CAMLprim value llvm_write_bitcode_to_fd(value U, value M, value FD) {
34+
int Unbuffered;
35+
int res;
36+
37+
if (U == Val_int(0)) {
38+
Unbuffered = 0;
39+
} else {
40+
Unbuffered = Bool_val(Field(U,0));
41+
}
42+
43+
res = LLVMWriteBitcodeToFD((LLVMModuleRef) M, Int_val(FD), 0, Unbuffered);
44+
return Val_bool(res == 0);
45+
}

bindings/ocaml/bitwriter/llvm_bitwriter.ml

+7
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@
1616
(* Writes the bitcode for module the given path. Returns true if successful. *)
1717
external write_bitcode_file : Llvm.llmodule -> string -> bool
1818
= "llvm_write_bitcode_file"
19+
20+
external write_bitcode_to_fd : ?unbuffered:bool -> Llvm.llmodule
21+
-> Unix.file_descr -> bool
22+
= "llvm_write_bitcode_to_fd"
23+
24+
let output_bitcode ?unbuffered channel m =
25+
write_bitcode_to_fd ?unbuffered m (Unix.descr_of_out_channel channel)

bindings/ocaml/bitwriter/llvm_bitwriter.mli

+12
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@
1616
[path]. Returns [true] if successful, [false] otherwise. *)
1717
external write_bitcode_file : Llvm.llmodule -> string -> bool
1818
= "llvm_write_bitcode_file"
19+
20+
(** [write_bitcode_to_fd ~unbuffered fd m] writes the bitcode for module
21+
[m] to the channel [c]. If [unbuffered] is [true], after every write the fd
22+
will be flushed. Returns [true] if successful, [false] otherwise. *)
23+
external write_bitcode_to_fd : ?unbuffered:bool -> Llvm.llmodule
24+
-> Unix.file_descr -> bool
25+
= "llvm_write_bitcode_to_fd"
26+
27+
(** [output_bitcode ~unbuffered c m] writes the bitcode for module [m]
28+
to the channel [c]. If [unbuffered] is [true], after every write the fd
29+
will be flushed. Returns [true] if successful, [false] otherwise. *)
30+
val output_bitcode : ?unbuffered:bool -> out_channel -> Llvm.llmodule -> bool

include/llvm-c/BitWriter.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ extern "C" {
2828

2929
/*===-- Operations on modules ---------------------------------------------===*/
3030

31-
/* Writes a module to an open file descriptor. Returns 0 on success.
32-
Closes the Handle. Use dup first if this is not what you want. */
33-
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
34-
35-
/* Writes a module to the specified path. Returns 0 on success. */
31+
/** Writes a module to the specified path. Returns 0 on success. */
3632
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path);
3733

34+
/** Writes a module to an open file descriptor. Returns 0 on success. */
35+
int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
36+
int Unbuffered);
37+
38+
/** Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
39+
descriptor. Returns 0 on success. Closes the Handle. */
40+
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
3841

3942
#ifdef __cplusplus
4043
}

lib/Bitcode/Writer/BitWriter.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
2727
return 0;
2828
}
2929

30-
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
31-
raw_fd_ostream OS(FileHandle, true);
30+
int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
31+
int Unbuffered) {
32+
raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
3233

3334
WriteBitcodeToFile(unwrap(M), OS);
3435
return 0;
3536
}
37+
38+
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
39+
return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
40+
}

test/Bindings/Ocaml/bitwriter.ml

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitwriter.cmxa %s -o %t
1+
(* RUN: %ocamlopt -warn-error A unix.cmxa llvm.cmxa llvm_bitwriter.cmxa %s -o %t
22
* RUN: ./%t %t.bc
33
* RUN: llvm-dis < %t.bc | grep caml_int_ty
44
*)
@@ -10,9 +10,37 @@ let context = Llvm.global_context ()
1010

1111
let test x = if not x then exit 1 else ()
1212

13+
let read_file name =
14+
let ic = open_in_bin name in
15+
let len = in_channel_length ic in
16+
let buf = String.create len in
17+
18+
test ((input ic buf 0 len) = len);
19+
20+
close_in ic;
21+
22+
buf
23+
24+
let temp_bitcode ?unbuffered m =
25+
let temp_name, temp_oc = Filename.open_temp_file ~mode:[Open_binary] "" "" in
26+
27+
test (Llvm_bitwriter.output_bitcode ?unbuffered temp_oc m);
28+
flush temp_oc;
29+
30+
let temp_buf = read_file temp_name in
31+
32+
close_out temp_oc;
33+
34+
temp_buf
35+
1336
let _ =
1437
let m = Llvm.create_module context "ocaml_test_module" in
1538

1639
ignore (Llvm.define_type_name "caml_int_ty" (Llvm.i32_type context) m);
17-
18-
test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1))
40+
41+
test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1));
42+
let file_buf = read_file Sys.argv.(1) in
43+
44+
test (file_buf = temp_bitcode m);
45+
test (file_buf = temp_bitcode ~unbuffered:false m);
46+
test (file_buf = temp_bitcode ~unbuffered:true m)

0 commit comments

Comments
 (0)