Sketch out an implementation of Briggs' copy placement algorithm.
[oota-llvm.git] / bindings / ocaml / bitreader / bitreader_ocaml.c
1 /*===-- bitwriter_ocaml.c - LLVM Ocaml Glue ---------------------*- C++ -*-===*\
2 |*                                                                            *|
3 |*                     The LLVM Compiler Infrastructure                       *|
4 |*                                                                            *|
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.   *|
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/BitReader.h"
16 #include "caml/alloc.h"
17 #include "caml/mlvalues.h"
18 #include "caml/memory.h"
19 #include <stdio.h>
20
21
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;
25
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);
29   return Val_unit;
30 }
31
32 void llvm_raise(value Prototype, char *Message);
33
34
35 /*===-- Modules -----------------------------------------------------------===*/
36
37 /* Llvm.llmemorybuffer -> Llvm.module */
38 CAMLprim value llvm_get_module_provider(LLVMMemoryBufferRef MemBuf) {
39   CAMLparam0();
40   CAMLlocal2(Variant, MessageVal);
41   char *Message;
42   
43   LLVMModuleProviderRef MP;
44   if (LLVMGetBitcodeModuleProvider(MemBuf, &MP, &Message))
45     llvm_raise(llvm_bitreader_error_exn, Message);
46   
47   CAMLreturn((value) MemBuf);
48 }
49
50 /* Llvm.llmemorybuffer -> Llvm.llmodule */
51 CAMLprim value llvm_parse_bitcode(LLVMMemoryBufferRef MemBuf) {
52   CAMLparam0();
53   CAMLlocal2(Variant, MessageVal);
54   LLVMModuleRef M;
55   char *Message;
56   
57   if (LLVMParseBitcode(MemBuf, &M, &Message))
58     llvm_raise(llvm_bitreader_error_exn, Message);
59   
60   CAMLreturn((value) M);
61 }