llvm.org GIT mirror llvm / f00a9e0
[OCaml] Implement Llvm.MemoryBuffer.{of_string,as_string} git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193953 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Zotov 7 years ago
4 changed file(s) with 42 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11661166 module MemoryBuffer = struct
11671167 external of_file : string -> llmemorybuffer = "llvm_memorybuffer_of_file"
11681168 external of_stdin : unit -> llmemorybuffer = "llvm_memorybuffer_of_stdin"
1169 external of_string : ?name:string -> string -> llmemorybuffer = "llvm_memorybuffer_of_string"
1170 external as_string : llmemorybuffer -> string = "llvm_memorybuffer_as_string"
11691171 external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
11701172 end
11711173
23442344 (** [of_stdin ()] is the memory buffer containing the contents of standard input.
23452345 If standard input is empty, then [IoError msg] is raised. *)
23462346 val of_stdin : unit -> llmemorybuffer
2347
2348 (** [of_string ~name s] is the memory buffer containing the contents of string [s].
2349 The name of memory buffer is set to [name] if it is provided. *)
2350 val of_string : ?name:string -> string -> llmemorybuffer
2351
2352 (** [as_string mb] is the string containing the contents of memory buffer [mb]. *)
2353 val as_string : llmemorybuffer -> string
23472354
23482355 (** Disposes of a memory buffer. *)
23492356 val dispose : llmemorybuffer -> unit
19951995 return MemBuf;
19961996 }
19971997
1998 /* ?name:string -> string -> llmemorybuffer */
1999 CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_string(value Name, value String) {
2000 const char *NameCStr;
2001 if(Name == Val_int(0))
2002 NameCStr = "";
2003 else
2004 NameCStr = String_val(Field(Name, 0));
2005
2006 LLVMMemoryBufferRef MemBuf;
2007 MemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(
2008 String_val(String), caml_string_length(String), NameCStr);
2009
2010 return MemBuf;
2011 }
2012
2013 /* llmemorybuffer -> string */
2014 CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) {
2015 value String = caml_alloc_string(LLVMGetBufferSize(MemBuf));
2016 memcpy(String_val(String), LLVMGetBufferStart(MemBuf),
2017 LLVMGetBufferSize(MemBuf));
2018
2019 return String;
2020 }
2021
19982022 /* llmemorybuffer -> unit */
19992023 CAMLprim value llvm_memorybuffer_dispose(LLVMMemoryBufferRef MemBuf) {
20002024 LLVMDisposeMemoryBuffer(MemBuf);
13191319 end
13201320
13211321
1322 (*===-- Memory Buffer -----------------------------------------------------===*)
1323
1324 let test_memory_buffer () =
1325 group "memory buffer";
1326 let buf = MemoryBuffer.of_string "foobar" in
1327 insist ((MemoryBuffer.as_string buf) = "foobar")
1328
1329
13221330 (*===-- Writer ------------------------------------------------------------===*)
13231331
13241332 let test_writer () =
13491357 suite "instructions" test_instructions;
13501358 suite "builder" test_builder;
13511359 suite "pass manager" test_pass_manager;
1360 suite "memory buffer" test_memory_buffer;
13521361 suite "writer" test_writer; (* Keep this last; it disposes m. *)
13531362 exit !exit_status