Home | History | Annotate | Download | only in bitreader
      1 /*===-- bitwriter_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/BitReader.h"
     16 #include "caml/alloc.h"
     17 #include "caml/fail.h"
     18 #include "caml/memory.h"
     19 
     20 
     21 /* Can't use the recommended caml_named_value mechanism for backwards
     22    compatibility reasons. This is largely equivalent. */
     23 static value llvm_bitreader_error_exn;
     24 
     25 CAMLprim value llvm_register_bitreader_exns(value Error) {
     26   llvm_bitreader_error_exn = Field(Error, 0);
     27   register_global_root(&llvm_bitreader_error_exn);
     28   return Val_unit;
     29 }
     30 
     31 static void llvm_raise(value Prototype, char *Message) {
     32   CAMLparam1(Prototype);
     33   CAMLlocal1(CamlMessage);
     34 
     35   CamlMessage = copy_string(Message);
     36   LLVMDisposeMessage(Message);
     37 
     38   raise_with_arg(Prototype, CamlMessage);
     39   abort(); /* NOTREACHED */
     40 #ifdef CAMLnoreturn
     41   CAMLnoreturn; /* Silences warnings, but is missing in some versions. */
     42 #endif
     43 }
     44 
     45 
     46 /*===-- Modules -----------------------------------------------------------===*/
     47 
     48 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
     49 CAMLprim value llvm_get_module(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
     50   CAMLparam0();
     51   CAMLlocal2(Variant, MessageVal);
     52   char *Message;
     53 
     54   LLVMModuleRef M;
     55   if (LLVMGetBitcodeModuleInContext(C, MemBuf, &M, &Message))
     56     llvm_raise(llvm_bitreader_error_exn, Message);
     57 
     58   CAMLreturn((value) M);
     59 }
     60 
     61 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
     62 CAMLprim value llvm_parse_bitcode(LLVMContextRef C,
     63                                   LLVMMemoryBufferRef MemBuf) {
     64   CAMLparam0();
     65   CAMLlocal2(Variant, MessageVal);
     66   LLVMModuleRef M;
     67   char *Message;
     68 
     69   if (LLVMParseBitcodeInContext(C, MemBuf, &M, &Message))
     70     llvm_raise(llvm_bitreader_error_exn, Message);
     71 
     72   CAMLreturn((value) M);
     73 }
     74