d9f7a50c52d0ee11b9bde68b4510be368bbda549
[oota-llvm.git] / bindings / ocaml / irreader / irreader_ocaml.c
1 /*===-- irreader_ocaml.c - LLVM OCaml Glue ----------------------*- C++ -*-===*\
2 |*                                                                            *|
3 |*                     The LLVM Compiler Infrastructure                       *|
4 |*                                                                            *|
5 |* This file is distributed under the University of Illinois Open Source      *|
6 |* License. See LICENSE.TXT for details.                                      *|
7 |*                                                                            *|
8 |*===----------------------------------------------------------------------===*|
9 |*                                                                            *|
10 |* This file glues LLVM's OCaml interface to its C interface. These functions *|
11 |* are by and large transparent wrappers to the corresponding C functions.    *|
12 |*                                                                            *|
13 \*===----------------------------------------------------------------------===*/
14
15 #include "llvm-c/IRReader.h"
16 #include "caml/alloc.h"
17 #include "caml/fail.h"
18 #include "caml/memory.h"
19 #include "caml/callback.h"
20
21 static void llvm_raise(value Prototype, char *Message) {
22   CAMLparam1(Prototype);
23   CAMLlocal1(CamlMessage);
24
25   CamlMessage = copy_string(Message);
26   LLVMDisposeMessage(Message);
27
28   raise_with_arg(Prototype, CamlMessage);
29   CAMLnoreturn;
30 }
31
32 /*===-- IRReader ----------------------------------------------------------===*/
33
34 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
35 CAMLprim value llvm_parse_ir(LLVMContextRef C,
36                              LLVMMemoryBufferRef MemBuf) {
37   CAMLparam0();
38   CAMLlocal2(Variant, MessageVal);
39   LLVMModuleRef M;
40   char *Message;
41
42   if (LLVMParseIRInContext(C, MemBuf, &M, &Message))
43     llvm_raise(*caml_named_value("Llvm_irreader.Error"), Message);
44
45   CAMLreturn((value) M);
46 }