1f4a2713aSLionel Sambuc /*===-- bitwriter_ocaml.c - LLVM OCaml Glue ---------------------*- C++ -*-===*\
2f4a2713aSLionel Sambuc |* *|
3f4a2713aSLionel Sambuc |* The LLVM Compiler Infrastructure *|
4f4a2713aSLionel Sambuc |* *|
5f4a2713aSLionel Sambuc |* This file is distributed under the University of Illinois Open Source *|
6f4a2713aSLionel Sambuc |* License. See LICENSE.TXT for details. *|
7f4a2713aSLionel Sambuc |* *|
8f4a2713aSLionel Sambuc |*===----------------------------------------------------------------------===*|
9f4a2713aSLionel Sambuc |* *|
10f4a2713aSLionel Sambuc |* This file glues LLVM's OCaml interface to its C interface. These functions *|
11f4a2713aSLionel Sambuc |* are by and large transparent wrappers to the corresponding C functions. *|
12f4a2713aSLionel Sambuc |* *|
13f4a2713aSLionel Sambuc \*===----------------------------------------------------------------------===*/
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "llvm-c/BitReader.h"
16f4a2713aSLionel Sambuc #include "caml/alloc.h"
17f4a2713aSLionel Sambuc #include "caml/fail.h"
18f4a2713aSLionel Sambuc #include "caml/memory.h"
19*0a6a1f1dSLionel Sambuc #include "caml/callback.h"
20f4a2713aSLionel Sambuc
21*0a6a1f1dSLionel Sambuc void llvm_raise(value Prototype, char *Message);
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
llvm_get_module(LLVMContextRef C,LLVMMemoryBufferRef MemBuf)24*0a6a1f1dSLionel Sambuc CAMLprim LLVMModuleRef llvm_get_module(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
25*0a6a1f1dSLionel Sambuc LLVMModuleRef M;
26f4a2713aSLionel Sambuc char *Message;
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc if (LLVMGetBitcodeModuleInContext(C, MemBuf, &M, &Message))
29*0a6a1f1dSLionel Sambuc llvm_raise(*caml_named_value("Llvm_bitreader.Error"), Message);
30f4a2713aSLionel Sambuc
31*0a6a1f1dSLionel Sambuc return M;
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
llvm_parse_bitcode(LLVMContextRef C,LLVMMemoryBufferRef MemBuf)35*0a6a1f1dSLionel Sambuc CAMLprim LLVMModuleRef llvm_parse_bitcode(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
36f4a2713aSLionel Sambuc LLVMModuleRef M;
37f4a2713aSLionel Sambuc char *Message;
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc if (LLVMParseBitcodeInContext(C, MemBuf, &M, &Message))
40*0a6a1f1dSLionel Sambuc llvm_raise(*caml_named_value("Llvm_bitreader.Error"), Message);
41f4a2713aSLionel Sambuc
42*0a6a1f1dSLionel Sambuc return M;
43f4a2713aSLionel Sambuc }
44