1 /*===-- irreader_ocaml.c - LLVM OCaml Glue ----------------------*- C++ -*-===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
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. *|
13 \*===----------------------------------------------------------------------===*/
15 #include "llvm-c/IRReader.h"
16 #include "caml/alloc.h"
17 #include "caml/fail.h"
18 #include "caml/memory.h"
20 /* Can't use the recommended caml_named_value mechanism for backwards
21 compatibility reasons. This is largely equivalent. */
22 static value llvm_irreader_error_exn
;
24 CAMLprim value
llvm_register_irreader_exns(value Error
) {
25 llvm_irreader_error_exn
= Field(Error
, 0);
26 register_global_root(&llvm_irreader_error_exn
);
30 static void llvm_raise(value Prototype
, char *Message
) {
31 CAMLparam1(Prototype
);
32 CAMLlocal1(CamlMessage
);
34 CamlMessage
= copy_string(Message
);
35 LLVMDisposeMessage(Message
);
37 raise_with_arg(Prototype
, CamlMessage
);
38 abort(); /* NOTREACHED */
40 CAMLnoreturn
; /* Silences warnings, but is missing in some versions. */
45 /*===-- Modules -----------------------------------------------------------===*/
47 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
48 CAMLprim value
llvm_parse_ir(LLVMContextRef C
,
49 LLVMMemoryBufferRef MemBuf
) {
51 CAMLlocal2(Variant
, MessageVal
);
55 if (LLVMParseIRInContext(C
, MemBuf
, &M
, &Message
))
56 llvm_raise(llvm_irreader_error_exn
, Message
);
58 CAMLreturn((value
) M
);