[llvm] r193953 - [OCaml] Implement Llvm.MemoryBuffer.{of_string, as_string}
Peter Zotov
whitequark at whitequark.org
Sun Nov 3 01:27:45 PDT 2013
Author: whitequark
Date: Sun Nov 3 02:27:45 2013
New Revision: 193953
URL: http://llvm.org/viewvc/llvm-project?rev=193953&view=rev
Log:
[OCaml] Implement Llvm.MemoryBuffer.{of_string,as_string}
Modified:
llvm/trunk/bindings/ocaml/llvm/llvm.ml
llvm/trunk/bindings/ocaml/llvm/llvm.mli
llvm/trunk/bindings/ocaml/llvm/llvm_ocaml.c
llvm/trunk/test/Bindings/Ocaml/vmcore.ml
Modified: llvm/trunk/bindings/ocaml/llvm/llvm.ml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/ocaml/llvm/llvm.ml?rev=193953&r1=193952&r2=193953&view=diff
==============================================================================
--- llvm/trunk/bindings/ocaml/llvm/llvm.ml (original)
+++ llvm/trunk/bindings/ocaml/llvm/llvm.ml Sun Nov 3 02:27:45 2013
@@ -1167,6 +1167,8 @@ external build_ptrdiff : llvalue -> llva
module MemoryBuffer = struct
external of_file : string -> llmemorybuffer = "llvm_memorybuffer_of_file"
external of_stdin : unit -> llmemorybuffer = "llvm_memorybuffer_of_stdin"
+ external of_string : ?name:string -> string -> llmemorybuffer = "llvm_memorybuffer_of_string"
+ external as_string : llmemorybuffer -> string = "llvm_memorybuffer_as_string"
external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
end
Modified: llvm/trunk/bindings/ocaml/llvm/llvm.mli
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/ocaml/llvm/llvm.mli?rev=193953&r1=193952&r2=193953&view=diff
==============================================================================
--- llvm/trunk/bindings/ocaml/llvm/llvm.mli (original)
+++ llvm/trunk/bindings/ocaml/llvm/llvm.mli Sun Nov 3 02:27:45 2013
@@ -2345,6 +2345,13 @@ module MemoryBuffer : sig
(** [of_stdin ()] is the memory buffer containing the contents of standard input.
If standard input is empty, then [IoError msg] is raised. *)
val of_stdin : unit -> llmemorybuffer
+
+ (** [of_string ~name s] is the memory buffer containing the contents of string [s].
+ The name of memory buffer is set to [name] if it is provided. *)
+ val of_string : ?name:string -> string -> llmemorybuffer
+
+ (** [as_string mb] is the string containing the contents of memory buffer [mb]. *)
+ val as_string : llmemorybuffer -> string
(** Disposes of a memory buffer. *)
val dispose : llmemorybuffer -> unit
Modified: llvm/trunk/bindings/ocaml/llvm/llvm_ocaml.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/ocaml/llvm/llvm_ocaml.c?rev=193953&r1=193952&r2=193953&view=diff
==============================================================================
--- llvm/trunk/bindings/ocaml/llvm/llvm_ocaml.c (original)
+++ llvm/trunk/bindings/ocaml/llvm/llvm_ocaml.c Sun Nov 3 02:27:45 2013
@@ -1996,6 +1996,30 @@ CAMLprim LLVMMemoryBufferRef llvm_memory
return MemBuf;
}
+/* ?name:string -> string -> llmemorybuffer */
+CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_string(value Name, value String) {
+ const char *NameCStr;
+ if(Name == Val_int(0))
+ NameCStr = "";
+ else
+ NameCStr = String_val(Field(Name, 0));
+
+ LLVMMemoryBufferRef MemBuf;
+ MemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(
+ String_val(String), caml_string_length(String), NameCStr);
+
+ return MemBuf;
+}
+
+/* llmemorybuffer -> string */
+CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) {
+ value String = caml_alloc_string(LLVMGetBufferSize(MemBuf));
+ memcpy(String_val(String), LLVMGetBufferStart(MemBuf),
+ LLVMGetBufferSize(MemBuf));
+
+ return String;
+}
+
/* llmemorybuffer -> unit */
CAMLprim value llvm_memorybuffer_dispose(LLVMMemoryBufferRef MemBuf) {
LLVMDisposeMemoryBuffer(MemBuf);
Modified: llvm/trunk/test/Bindings/Ocaml/vmcore.ml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/Ocaml/vmcore.ml?rev=193953&r1=193952&r2=193953&view=diff
==============================================================================
--- llvm/trunk/test/Bindings/Ocaml/vmcore.ml (original)
+++ llvm/trunk/test/Bindings/Ocaml/vmcore.ml Sun Nov 3 02:27:45 2013
@@ -1320,6 +1320,14 @@ let test_pass_manager () =
end
+(*===-- Memory Buffer -----------------------------------------------------===*)
+
+let test_memory_buffer () =
+ group "memory buffer";
+ let buf = MemoryBuffer.of_string "foobar" in
+ insist ((MemoryBuffer.as_string buf) = "foobar")
+
+
(*===-- Writer ------------------------------------------------------------===*)
let test_writer () =
@@ -1350,5 +1358,6 @@ let _ =
suite "instructions" test_instructions;
suite "builder" test_builder;
suite "pass manager" test_pass_manager;
+ suite "memory buffer" test_memory_buffer;
suite "writer" test_writer; (* Keep this last; it disposes m. *)
exit !exit_status
More information about the llvm-commits
mailing list