1 /*===-- bitwriter_ocaml.c - LLVM Ocaml Glue ---------------------*- C++ -*-===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file was developed by Gordon Henriksen and is distributed under the *|
6 |* University of Illinois Open Source 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/BitReader.h"
16 #include "caml/alloc.h"
17 #include "caml/mlvalues.h"
18 #include "caml/memory.h"
22 /* Can't use the recommended caml_named_value mechanism for backwards
23 compatibility reasons. This is largely equivalent. */
24 static value llvm_bitreader_error_exn;
26 CAMLprim value llvm_register_bitreader_exns(value Error) {
27 llvm_bitreader_error_exn = Field(Error, 0);
28 register_global_root(&llvm_bitreader_error_exn);
32 void llvm_raise(value Prototype, char *Message);
35 /*===-- Modules -----------------------------------------------------------===*/
37 /* Llvm.llmemorybuffer -> Llvm.module */
38 CAMLprim value llvm_get_module_provider(LLVMMemoryBufferRef MemBuf) {
40 CAMLlocal2(Variant, MessageVal);
43 LLVMModuleProviderRef MP;
44 if (LLVMGetBitcodeModuleProvider(MemBuf, &MP, &Message))
45 llvm_raise(llvm_bitreader_error_exn, Message);
47 CAMLreturn((value) MemBuf);
50 /* Llvm.llmemorybuffer -> Llvm.llmodule */
51 CAMLprim value llvm_parse_bitcode(LLVMMemoryBufferRef MemBuf) {
53 CAMLlocal2(Variant, MessageVal);
57 if (LLVMParseBitcode(MemBuf, &M, &Message))
58 llvm_raise(llvm_bitreader_error_exn, Message);
60 CAMLreturn((value) M);