diff options
author | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-12-11 00:20:48 +0000 |
---|---|---|
committer | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-12-11 00:20:48 +0000 |
commit | bbc6597f02d108f32b5bdc7711cc67386196017b (patch) | |
tree | c0a9de6590c68d7d5cbd907da5ec03c723201e56 /bindings/ocaml | |
parent | 7eb587779e1c19768b98ba633c6dd0bbd306a323 (diff) | |
download | external_llvm-bbc6597f02d108f32b5bdc7711cc67386196017b.zip external_llvm-bbc6597f02d108f32b5bdc7711cc67386196017b.tar.gz external_llvm-bbc6597f02d108f32b5bdc7711cc67386196017b.tar.bz2 |
Adding Ocaml bindings for the bitreader as requested by Sarah
Thompson. Usage should be something like this:
open Llvm
open Llvm_bitreader
match read_bitcode_file fn with
| Bitreader_failure msg ->
prerr_endline msg
| Bitreader_success m ->
...;
dispose_module m
Compile with: ocamlc llvm.cma llvm_bitreader.cma
ocamlopt llvm.cmxa llvm_bitreader.cmxa
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44824 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings/ocaml')
-rw-r--r-- | bindings/ocaml/Makefile | 2 | ||||
-rw-r--r-- | bindings/ocaml/bitreader/Makefile | 20 | ||||
-rw-r--r-- | bindings/ocaml/bitreader/bitreader_ocaml.c | 46 | ||||
-rw-r--r-- | bindings/ocaml/bitreader/llvm_bitreader.ml | 17 | ||||
-rw-r--r-- | bindings/ocaml/bitreader/llvm_bitreader.mli | 25 |
5 files changed, 109 insertions, 1 deletions
diff --git a/bindings/ocaml/Makefile b/bindings/ocaml/Makefile index a3bbae0..11abec4 100644 --- a/bindings/ocaml/Makefile +++ b/bindings/ocaml/Makefile @@ -8,6 +8,6 @@ ##===----------------------------------------------------------------------===## LEVEL := ../.. -DIRS = llvm bitwriter analysis +DIRS = llvm bitreader bitwriter analysis include $(LEVEL)/Makefile.common diff --git a/bindings/ocaml/bitreader/Makefile b/bindings/ocaml/bitreader/Makefile new file mode 100644 index 0000000..4acc7ee --- /dev/null +++ b/bindings/ocaml/bitreader/Makefile @@ -0,0 +1,20 @@ +##===- bindings/ocaml/bitreader/Makefile -------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file was developed by Gordon Henriksen and is distributed under the +# University of Illinois Open Source License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +# +# This is the makefile for the Objective Caml Llvm_bitreader interface. +# +##===----------------------------------------------------------------------===## + +LEVEL := ../../.. +LIBRARYNAME := llvm_bitreader +DONT_BUILD_RELINKED := 1 +UsedComponents := bitreader +UsedOcamlInterfaces := llvm + +include ../Makefile.ocaml diff --git a/bindings/ocaml/bitreader/bitreader_ocaml.c b/bindings/ocaml/bitreader/bitreader_ocaml.c new file mode 100644 index 0000000..7088fa5 --- /dev/null +++ b/bindings/ocaml/bitreader/bitreader_ocaml.c @@ -0,0 +1,46 @@ +/*===-- bitwriter_ocaml.c - LLVM Ocaml Glue ---------------------*- C++ -*-===*\ +|* *| +|* The LLVM Compiler Infrastructure *| +|* *| +|* This file was developed by Gordon Henriksen and is distributed under the *| +|* University of Illinois Open Source License. See LICENSE.TXT for details. *| +|* *| +|*===----------------------------------------------------------------------===*| +|* *| +|* This file glues LLVM's ocaml interface to its C interface. These functions *| +|* are by and large transparent wrappers to the corresponding C functions. *| +|* *| +\*===----------------------------------------------------------------------===*/ + +#include "llvm-c/BitReader.h" +#include "caml/alloc.h" +#include "caml/mlvalues.h" +#include "caml/memory.h" + +/*===-- Modules -----------------------------------------------------------===*/ + +/* string -> bitreader_result + + type bitreader_result = + | Bitreader_success of Llvm.llmodule + | Bitreader_failure of string + */ +CAMLprim value llvm_read_bitcode_file(value Path) { + LLVMModuleRef M; + char *Message; + CAMLparam1(Path); + CAMLlocal2(Variant, MessageVal); + + if (LLVMReadBitcodeFromFile(String_val(Path), &M, &Message)) { + MessageVal = copy_string(Message); + LLVMDisposeBitcodeReaderMessage(Message); + + Variant = alloc(1, 1); + Field(Variant, 0) = MessageVal; + } else { + Variant = alloc(1, 0); + Field(Variant, 0) = Val_op(M); + } + + CAMLreturn(Variant); +} diff --git a/bindings/ocaml/bitreader/llvm_bitreader.ml b/bindings/ocaml/bitreader/llvm_bitreader.ml new file mode 100644 index 0000000..39d0434 --- /dev/null +++ b/bindings/ocaml/bitreader/llvm_bitreader.ml @@ -0,0 +1,17 @@ +(*===-- llvm_bitreader.ml - LLVM Ocaml Interface ----------------*- C++ -*-===* + * + * The LLVM Compiler Infrastructure + * + * This file was developed by Gordon Henriksen and is distributed under the + * University of Illinois Open Source License. See LICENSE.TXT for details. + * + *===----------------------------------------------------------------------===*) + + +type bitreader_result = +| Bitreader_success of Llvm.llmodule +| Bitreader_failure of string + + +external read_bitcode_file : string -> bitreader_result + = "llvm_read_bitcode_file" diff --git a/bindings/ocaml/bitreader/llvm_bitreader.mli b/bindings/ocaml/bitreader/llvm_bitreader.mli new file mode 100644 index 0000000..37750bc --- /dev/null +++ b/bindings/ocaml/bitreader/llvm_bitreader.mli @@ -0,0 +1,25 @@ +(*===-- llvm_bitreader.mli - LLVM Ocaml Interface ---------------*- C++ -*-===* + * + * The LLVM Compiler Infrastructure + * + * This file was developed by Gordon Henriksen and is distributed under the + * University of Illinois Open Source License. See LICENSE.TXT for details. + * + *===----------------------------------------------------------------------=== + * + * This interface provides an ocaml API for the LLVM bitcode reader, the + * classes in the Bitreader library. + * + *===----------------------------------------------------------------------===*) + + +type bitreader_result = +| Bitreader_success of Llvm.llmodule +| Bitreader_failure of string + + +(** [read_bitcode_file path] reads the bitcode for module [m] from the file at + [path]. Returns [Reader_success m] if successful, and [Reader_failure msg] + otherwise, where [msg] is a description of the error encountered. **) +external read_bitcode_file : string -> bitreader_result + = "llvm_read_bitcode_file" |