1 /*===-- irreader_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/IRReader.h" 16 #include "caml/alloc.h" 17 #include "caml/fail.h" 18 #include "caml/memory.h" 19 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; 23 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); 27 return Val_unit; 28 } 29 30 static void llvm_raise(value Prototype, char *Message) { 31 CAMLparam1(Prototype); 32 CAMLlocal1(CamlMessage); 33 34 CamlMessage = copy_string(Message); 35 LLVMDisposeMessage(Message); 36 37 raise_with_arg(Prototype, CamlMessage); 38 abort(); /* NOTREACHED */ 39 #ifdef CAMLnoreturn 40 CAMLnoreturn; /* Silences warnings, but is missing in some versions. */ 41 #endif 42 } 43 44 45 /*===-- Modules -----------------------------------------------------------===*/ 46 47 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */ 48 CAMLprim value llvm_parse_ir(LLVMContextRef C, 49 LLVMMemoryBufferRef MemBuf) { 50 CAMLparam0(); 51 CAMLlocal2(Variant, MessageVal); 52 LLVMModuleRef M; 53 char *Message; 54 55 if (LLVMParseIRInContext(C, MemBuf, &M, &Message)) 56 llvm_raise(llvm_irreader_error_exn, Message); 57 58 CAMLreturn((value) M); 59 } 60