aboutsummaryrefslogtreecommitdiffstats
path: root/bindings
diff options
context:
space:
mode:
authorPeter Zotov <whitequark@whitequark.org>2013-11-06 09:21:25 +0000
committerPeter Zotov <whitequark@whitequark.org>2013-11-06 09:21:25 +0000
commitec7270c966b4a49840b1801bfbb11977d76cb333 (patch)
tree9cd0ec95ef489055aab4d3f8b3f8da56b6b690d7 /bindings
parentc6099db476ea863d0e897e3c311bfe490293e04f (diff)
downloadexternal_llvm-ec7270c966b4a49840b1801bfbb11977d76cb333.zip
external_llvm-ec7270c966b4a49840b1801bfbb11977d76cb333.tar.gz
external_llvm-ec7270c966b4a49840b1801bfbb11977d76cb333.tar.bz2
[OCaml] Impement Llvm_irreader, bindings to LLVM assembly parser
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194138 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/ocaml/Makefile3
-rw-r--r--bindings/ocaml/irreader/Makefile19
-rw-r--r--bindings/ocaml/irreader/irreader_ocaml.c59
-rw-r--r--bindings/ocaml/irreader/llvm_irreader.ml17
-rw-r--r--bindings/ocaml/irreader/llvm_irreader.mli21
-rw-r--r--bindings/ocaml/llvm/META.llvm.in8
6 files changed, 126 insertions, 1 deletions
diff --git a/bindings/ocaml/Makefile b/bindings/ocaml/Makefile
index 4d80e2ff..19ba302 100644
--- a/bindings/ocaml/Makefile
+++ b/bindings/ocaml/Makefile
@@ -8,7 +8,8 @@
##===----------------------------------------------------------------------===##
LEVEL := ../..
-DIRS = llvm bitreader bitwriter analysis target executionengine transforms linker
+DIRS = llvm bitreader bitwriter irreader analysis target executionengine \
+ transforms linker
ExtraMakefiles = $(PROJ_OBJ_DIR)/Makefile.ocaml
ocamldoc:
diff --git a/bindings/ocaml/irreader/Makefile b/bindings/ocaml/irreader/Makefile
new file mode 100644
index 0000000..7665999
--- /dev/null
+++ b/bindings/ocaml/irreader/Makefile
@@ -0,0 +1,19 @@
+##===- bindings/ocaml/irreader/Makefile --------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+#
+# This is the makefile for the Objective Caml Llvm_irreader interface.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL := ../../..
+LIBRARYNAME := llvm_irreader
+UsedComponents := irreader
+UsedOcamlInterfaces := llvm
+
+include ../Makefile.ocaml
diff --git a/bindings/ocaml/irreader/irreader_ocaml.c b/bindings/ocaml/irreader/irreader_ocaml.c
new file mode 100644
index 0000000..30c10c7
--- /dev/null
+++ b/bindings/ocaml/irreader/irreader_ocaml.c
@@ -0,0 +1,59 @@
+/*===-- irreader_ocaml.c - LLVM OCaml Glue ----------------------*- C++ -*-===*\
+|* *|
+|* The LLVM Compiler Infrastructure *|
+|* *|
+|* This file 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/IRReader.h"
+#include "caml/alloc.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
+
+/* Can't use the recommended caml_named_value mechanism for backwards
+ compatibility reasons. This is largely equivalent. */
+static value llvm_irreader_error_exn;
+
+CAMLprim value llvm_register_irreader_exns(value Error) {
+ llvm_irreader_error_exn = Field(Error, 0);
+ register_global_root(&llvm_irreader_error_exn);
+ return Val_unit;
+}
+
+static void llvm_raise(value Prototype, char *Message) {
+ CAMLparam1(Prototype);
+ CAMLlocal1(CamlMessage);
+
+ CamlMessage = copy_string(Message);
+ LLVMDisposeMessage(Message);
+
+ raise_with_arg(Prototype, CamlMessage);
+ abort(); /* NOTREACHED */
+#ifdef CAMLnoreturn
+ CAMLnoreturn; /* Silences warnings, but is missing in some versions. */
+#endif
+}
+
+
+/*===-- Modules -----------------------------------------------------------===*/
+
+/* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
+CAMLprim value llvm_parse_ir(LLVMContextRef C,
+ LLVMMemoryBufferRef MemBuf) {
+ CAMLparam0();
+ CAMLlocal2(Variant, MessageVal);
+ LLVMModuleRef M;
+ char *Message;
+
+ if (LLVMParseIRInContext(C, MemBuf, &M, &Message))
+ llvm_raise(llvm_irreader_error_exn, Message);
+
+ CAMLreturn((value) M);
+}
diff --git a/bindings/ocaml/irreader/llvm_irreader.ml b/bindings/ocaml/irreader/llvm_irreader.ml
new file mode 100644
index 0000000..455b1fa
--- /dev/null
+++ b/bindings/ocaml/irreader/llvm_irreader.ml
@@ -0,0 +1,17 @@
+(*===-- llvm_irreader.ml - LLVM OCaml Interface ---------------*- OCaml -*-===*
+ *
+ * The LLVM Compiler Infrastructure
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ *===----------------------------------------------------------------------===*)
+
+
+exception Error of string
+
+external register_exns : exn -> unit = "llvm_register_irreader_exns"
+let _ = register_exns (Error "")
+
+external parse_ir : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
+ = "llvm_parse_ir"
diff --git a/bindings/ocaml/irreader/llvm_irreader.mli b/bindings/ocaml/irreader/llvm_irreader.mli
new file mode 100644
index 0000000..2b11473
--- /dev/null
+++ b/bindings/ocaml/irreader/llvm_irreader.mli
@@ -0,0 +1,21 @@
+(*===-- llvm_irreader.mli - LLVM OCaml Interface --------------*- OCaml -*-===*
+ *
+ * The LLVM Compiler Infrastructure
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ *===----------------------------------------------------------------------===*)
+
+(** IR reader.
+
+ This interface provides an OCaml API for the LLVM assembly reader, the
+ classes in the IRReader library. *)
+
+exception Error of string
+
+(** [parse_ir context mb] parses the IR for a new module [m] from the
+ memory buffer [mb] in the context [context]. Returns [m] if successful, or
+ raises [Error msg] otherwise, where [msg] is a description of the error
+ encountered. See the function [llvm::ParseIR]. *)
+val parse_ir : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
diff --git a/bindings/ocaml/llvm/META.llvm.in b/bindings/ocaml/llvm/META.llvm.in
index 08e8d28..c241ea5 100644
--- a/bindings/ocaml/llvm/META.llvm.in
+++ b/bindings/ocaml/llvm/META.llvm.in
@@ -46,6 +46,14 @@ package "ipo" (
archive(native) = "llvm_ipo.cmxa"
)
+package "irreader" (
+ requires = "llvm"
+ version = "@PACKAGE_VERSION@"
+ description = "IR assembly reader for LLVM"
+ archive(byte) = "llvm_irreader.cma"
+ archive(native) = "llvm_irreader.cmxa"
+)
+
package "scalar_opts" (
requires = "llvm"
version = "@PACKAGE_VERSION@"