From 5f28729c61232e7a941e320f9e6985112d5bf6bd Mon Sep 17 00:00:00 2001 From: Peter Zotov Date: Wed, 29 Oct 2014 08:16:01 +0000 Subject: [PATCH] [OCaml] Expose Llvm_bitwriter.write_bitcode_to_memory_buffer. llvm-svn: 220844 --- .../ocaml/bitwriter/bitwriter_ocaml.c | 5 +++++ .../ocaml/bitwriter/llvm_bitwriter.ml | 17 ++++++++++------- .../ocaml/bitwriter/llvm_bitwriter.mli | 19 +++++++++++++------ llvm/test/Bindings/Ocaml/bitwriter.ml | 3 ++- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c b/llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c index 1045c2344a70..f20fd59fd31a 100644 --- a/llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c +++ b/llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c @@ -43,3 +43,8 @@ CAMLprim value llvm_write_bitcode_to_fd(value U, LLVMModuleRef M, value FD) { Result = LLVMWriteBitcodeToFD(M, Int_val(FD), 0, Unbuffered); return Val_bool(Result == 0); } + +/* Llvm.llmodule -> Llvm.llmemorybuffer */ +CAMLprim LLVMMemoryBufferRef llvm_write_bitcode_to_memory_buffer(LLVMModuleRef M) { + return LLVMWriteBitcodeToMemoryBuffer(M); +} diff --git a/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.ml b/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.ml index fac85538dbab..fca6efa4a75b 100644 --- a/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.ml +++ b/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.ml @@ -1,4 +1,4 @@ -(*===-- llvm_bitwriter.ml - LLVM OCaml Interface ----------------*- C++ -*-===* +(*===-- llvm_bitwriter.ml - LLVM OCaml Interface --------------*- OCaml -*-===* * * The LLVM Compiler Infrastructure * @@ -12,14 +12,17 @@ * *===----------------------------------------------------------------------===*) +external write_bitcode_file + : Llvm.llmodule -> string -> bool + = "llvm_write_bitcode_file" -(* 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" -external write_bitcode_to_fd : ?unbuffered:bool -> Llvm.llmodule - -> Unix.file_descr -> bool - = "llvm_write_bitcode_to_fd" +external write_bitcode_to_memory_buffer + : Llvm.llmodule -> Llvm.llmemorybuffer + = "llvm_write_bitcode_to_memory_buffer" let output_bitcode ?unbuffered channel m = write_bitcode_to_fd ?unbuffered m (Unix.descr_of_out_channel channel) diff --git a/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.mli b/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.mli index bb3e3b894560..3d0f78082257 100644 --- a/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.mli +++ b/llvm/bindings/ocaml/bitwriter/llvm_bitwriter.mli @@ -1,4 +1,4 @@ -(*===-- llvm_bitwriter.mli - LLVM OCaml Interface ---------------*- C++ -*-===* +(*===-- llvm_bitwriter.mli - LLVM OCaml Interface -------------*- OCaml -*-===* * * The LLVM Compiler Infrastructure * @@ -14,15 +14,22 @@ (** [write_bitcode_file m path] writes the bitcode for module [m] to the file at [path]. Returns [true] if successful, [false] otherwise. *) -external write_bitcode_file : Llvm.llmodule -> string -> bool - = "llvm_write_bitcode_file" +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" +external write_bitcode_to_fd + : ?unbuffered:bool -> Llvm.llmodule -> Unix.file_descr -> bool + = "llvm_write_bitcode_to_fd" + +(** [write_bitcode_to_memory_buffer m] returns a memory buffer containing + the bitcode for module [m]. *) +external write_bitcode_to_memory_buffer + : Llvm.llmodule -> Llvm.llmemorybuffer + = "llvm_write_bitcode_to_memory_buffer" (** [output_bitcode ~unbuffered c m] writes the bitcode for module [m] to the channel [c]. If [unbuffered] is [true], after every write the fd diff --git a/llvm/test/Bindings/Ocaml/bitwriter.ml b/llvm/test/Bindings/Ocaml/bitwriter.ml index 8ede44b597ca..98c04172a26e 100644 --- a/llvm/test/Bindings/Ocaml/bitwriter.ml +++ b/llvm/test/Bindings/Ocaml/bitwriter.ml @@ -45,4 +45,5 @@ let _ = test (file_buf = temp_bitcode m); test (file_buf = temp_bitcode ~unbuffered:false m); - test (file_buf = temp_bitcode ~unbuffered:true m) + test (file_buf = temp_bitcode ~unbuffered:true m); + test (file_buf = Llvm.MemoryBuffer.as_string (Llvm_bitwriter.write_bitcode_to_memory_buffer m))