Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame^] | 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 | |
| 20 | /*===-- Modules -----------------------------------------------------------===*/ |
| 21 | |
| 22 | /* string -> bitreader_result |
| 23 | |
| 24 | type bitreader_result = |
| 25 | | Bitreader_success of Llvm.llmodule |
| 26 | | Bitreader_failure of string |
| 27 | */ |
| 28 | CAMLprim value llvm_read_bitcode_file(value Path) { |
| 29 | LLVMModuleRef M; |
| 30 | char *Message; |
| 31 | CAMLparam1(Path); |
| 32 | CAMLlocal2(Variant, MessageVal); |
| 33 | |
| 34 | if (LLVMReadBitcodeFromFile(String_val(Path), &M, &Message)) { |
| 35 | MessageVal = copy_string(Message); |
| 36 | LLVMDisposeBitcodeReaderMessage(Message); |
| 37 | |
| 38 | Variant = alloc(1, 1); |
| 39 | Field(Variant, 0) = MessageVal; |
| 40 | } else { |
| 41 | Variant = alloc(1, 0); |
| 42 | Field(Variant, 0) = Val_op(M); |
| 43 | } |
| 44 | |
| 45 | CAMLreturn(Variant); |
| 46 | } |