aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErick Tryzelaar <idadesub@users.sourceforge.net>2010-03-06 00:30:06 +0000
committerErick Tryzelaar <idadesub@users.sourceforge.net>2010-03-06 00:30:06 +0000
commite623050048e56a512e77c28b69925e7dc1efa6ad (patch)
tree4e7f07dc84a9d5629597884c45591e4089aab801
parentb3195fbc2e19755c581eaa3aa52bfc1640c8495a (diff)
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
-rw-r--r--bindings/ocaml/bitwriter/bitwriter_ocaml.c15
-rw-r--r--bindings/ocaml/bitwriter/llvm_bitwriter.ml7
-rw-r--r--bindings/ocaml/bitwriter/llvm_bitwriter.mli12
-rw-r--r--include/llvm-c/BitWriter.h13
-rw-r--r--lib/Bitcode/Writer/BitWriter.cpp9
-rw-r--r--test/Bindings/Ocaml/bitwriter.ml34
6 files changed, 80 insertions, 10 deletions
diff --git a/bindings/ocaml/bitwriter/bitwriter_ocaml.c b/bindings/ocaml/bitwriter/bitwriter_ocaml.c
index 41aca259ca..53c93cbdfe 100644
--- a/bindings/ocaml/bitwriter/bitwriter_ocaml.c
+++ b/bindings/ocaml/bitwriter/bitwriter_ocaml.c
@@ -28,3 +28,18 @@ CAMLprim value llvm_write_bitcode_file(value M, value Path) {
int res = LLVMWriteBitcodeToFile((LLVMModuleRef) M, String_val(Path));
return Val_bool(res == 0);
}
+
+/* ?unbuffered:bool -> Llvm.llmodule -> Unix.file_descr -> bool */
+CAMLprim value llvm_write_bitcode_to_fd(value U, value M, value FD) {
+ int Unbuffered;
+ int res;
+
+ if (U == Val_int(0)) {
+ Unbuffered = 0;
+ } else {
+ Unbuffered = Bool_val(Field(U,0));
+ }
+
+ res = LLVMWriteBitcodeToFD((LLVMModuleRef) M, Int_val(FD), 0, Unbuffered);
+ return Val_bool(res == 0);
+}
diff --git a/bindings/ocaml/bitwriter/llvm_bitwriter.ml b/bindings/ocaml/bitwriter/llvm_bitwriter.ml
index 7b45c53dbf..3e69a3cc8f 100644
--- a/bindings/ocaml/bitwriter/llvm_bitwriter.ml
+++ b/bindings/ocaml/bitwriter/llvm_bitwriter.ml
@@ -16,3 +16,10 @@
(* Writes the bitcode for module the given path. Returns true if successful. *)
external write_bitcode_file : Llvm.llmodule -> string -> bool
= "llvm_write_bitcode_file"
+
+external write_bitcode_to_fd : ?unbuffered:bool -> Llvm.llmodule
+ -> Unix.file_descr -> bool
+ = "llvm_write_bitcode_to_fd"
+
+let output_bitcode ?unbuffered channel m =
+ write_bitcode_to_fd ?unbuffered m (Unix.descr_of_out_channel channel)
diff --git a/bindings/ocaml/bitwriter/llvm_bitwriter.mli b/bindings/ocaml/bitwriter/llvm_bitwriter.mli
index 2f782a1d9f..ea9a87600a 100644
--- a/bindings/ocaml/bitwriter/llvm_bitwriter.mli
+++ b/bindings/ocaml/bitwriter/llvm_bitwriter.mli
@@ -16,3 +16,15 @@
[path]. Returns [true] if successful, [false] otherwise. *)
external write_bitcode_file : Llvm.llmodule -> string -> bool
= "llvm_write_bitcode_file"
+
+(** [write_bitcode_to_fd ~unbuffered fd m] writes the bitcode for module
+ [m] to the channel [c]. If [unbuffered] is [true], after every write the fd
+ will be flushed. Returns [true] if successful, [false] otherwise. *)
+external write_bitcode_to_fd : ?unbuffered:bool -> Llvm.llmodule
+ -> Unix.file_descr -> bool
+ = "llvm_write_bitcode_to_fd"
+
+(** [output_bitcode ~unbuffered c m] writes the bitcode for module [m]
+ to the channel [c]. If [unbuffered] is [true], after every write the fd
+ will be flushed. Returns [true] if successful, [false] otherwise. *)
+val output_bitcode : ?unbuffered:bool -> out_channel -> Llvm.llmodule -> bool
diff --git a/include/llvm-c/BitWriter.h b/include/llvm-c/BitWriter.h
index 008ff9f2c1..bcbfb11149 100644
--- a/include/llvm-c/BitWriter.h
+++ b/include/llvm-c/BitWriter.h
@@ -28,13 +28,16 @@ extern "C" {
/*===-- Operations on modules ---------------------------------------------===*/
-/* Writes a module to an open file descriptor. Returns 0 on success.
- Closes the Handle. Use dup first if this is not what you want. */
-int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
-
-/* Writes a module to the specified path. Returns 0 on success. */
+/** Writes a module to the specified path. Returns 0 on success. */
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path);
+/** Writes a module to an open file descriptor. Returns 0 on success. */
+int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
+ int Unbuffered);
+
+/** Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
+ descriptor. Returns 0 on success. Closes the Handle. */
+int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
#ifdef __cplusplus
}
diff --git a/lib/Bitcode/Writer/BitWriter.cpp b/lib/Bitcode/Writer/BitWriter.cpp
index 6119c42889..4288422463 100644
--- a/lib/Bitcode/Writer/BitWriter.cpp
+++ b/lib/Bitcode/Writer/BitWriter.cpp
@@ -27,9 +27,14 @@ int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
return 0;
}
-int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
- raw_fd_ostream OS(FileHandle, true);
+int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
+ int Unbuffered) {
+ raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
WriteBitcodeToFile(unwrap(M), OS);
return 0;
}
+
+int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
+ return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
+}
diff --git a/test/Bindings/Ocaml/bitwriter.ml b/test/Bindings/Ocaml/bitwriter.ml
index 57caac7cb9..ef1c9ab722 100644
--- a/test/Bindings/Ocaml/bitwriter.ml
+++ b/test/Bindings/Ocaml/bitwriter.ml
@@ -1,4 +1,4 @@
-(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitwriter.cmxa %s -o %t
+(* RUN: %ocamlopt -warn-error A unix.cmxa llvm.cmxa llvm_bitwriter.cmxa %s -o %t
* RUN: ./%t %t.bc
* RUN: llvm-dis < %t.bc | grep caml_int_ty
*)
@@ -10,9 +10,37 @@ let context = Llvm.global_context ()
let test x = if not x then exit 1 else ()
+let read_file name =
+ let ic = open_in_bin name in
+ let len = in_channel_length ic in
+ let buf = String.create len in
+
+ test ((input ic buf 0 len) = len);
+
+ close_in ic;
+
+ buf
+
+let temp_bitcode ?unbuffered m =
+ let temp_name, temp_oc = Filename.open_temp_file ~mode:[Open_binary] "" "" in
+
+ test (Llvm_bitwriter.output_bitcode ?unbuffered temp_oc m);
+ flush temp_oc;
+
+ let temp_buf = read_file temp_name in
+
+ close_out temp_oc;
+
+ temp_buf
+
let _ =
let m = Llvm.create_module context "ocaml_test_module" in
ignore (Llvm.define_type_name "caml_int_ty" (Llvm.i32_type context) m);
-
- test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1))
+
+ test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1));
+ let file_buf = read_file Sys.argv.(1) in
+
+ test (file_buf = temp_bitcode m);
+ test (file_buf = temp_bitcode ~unbuffered:false m);
+ test (file_buf = temp_bitcode ~unbuffered:true m)