Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 1 | //===-- BitReader.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm-c/BitReader.h" |
| 11 | #include "llvm/Bitcode/ReaderWriter.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/LLVMContext.h" |
Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame^] | 13 | #include "llvm/IR/Module.h" |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MemoryBuffer.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 15 | #include <cstring> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include <string> |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 20 | /* Builds a module from the bitcode in the specified memory buffer, returning a |
| 21 | reference to the module via the OutModule parameter. Returns 0 on success. |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 22 | Optionally returns a human-readable error message via OutMessage. */ |
| 23 | LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, |
| 24 | LLVMModuleRef *OutModule, char **OutMessage) { |
Daniel Dunbar | e44fc85 | 2010-02-15 21:08:22 +0000 | [diff] [blame] | 25 | return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, |
| 26 | OutMessage); |
Owen Anderson | c8897d9 | 2009-07-02 07:17:57 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 29 | LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, |
| 30 | LLVMMemoryBufferRef MemBuf, |
| 31 | LLVMModuleRef *OutModule, |
| 32 | char **OutMessage) { |
Owen Anderson | c8897d9 | 2009-07-02 07:17:57 +0000 | [diff] [blame] | 33 | std::string Message; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 34 | |
Erick Tryzelaar | ccf9f2b | 2009-08-11 07:46:16 +0000 | [diff] [blame] | 35 | *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef), |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 36 | &Message)); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 37 | if (!*OutModule) { |
| 38 | if (OutMessage) |
| 39 | *OutMessage = strdup(Message.c_str()); |
| 40 | return 1; |
| 41 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 42 | |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 46 | /* Reads a module from the specified path, returning via the OutModule parameter |
| 47 | a module provider which performs lazy deserialization. Returns 0 on success. |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 48 | Optionally returns a human-readable error message via OutMessage. */ |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 49 | LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, |
| 50 | LLVMMemoryBufferRef MemBuf, |
| 51 | LLVMModuleRef *OutM, |
| 52 | char **OutMessage) { |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 53 | std::string Message; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 54 | |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 55 | *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef), |
| 56 | &Message)); |
| 57 | if (!*OutM) { |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 58 | if (OutMessage) |
| 59 | *OutMessage = strdup(Message.c_str()); |
| 60 | return 1; |
| 61 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 62 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 63 | return 0; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 64 | |
| 65 | } |
| 66 | |
| 67 | LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 68 | char **OutMessage) { |
| 69 | return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, |
| 70 | OutMessage); |
| 71 | } |
| 72 | |
| 73 | /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ |
| 74 | LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, |
| 75 | LLVMMemoryBufferRef MemBuf, |
| 76 | LLVMModuleProviderRef *OutMP, |
| 77 | char **OutMessage) { |
| 78 | return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, |
| 79 | reinterpret_cast<LLVMModuleRef*>(OutMP), |
| 80 | OutMessage); |
| 81 | } |
| 82 | |
| 83 | /* Deprecated: Use LLVMGetBitcodeModule instead. */ |
| 84 | LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, |
| 85 | LLVMModuleProviderRef *OutMP, |
| 86 | char **OutMessage) { |
| 87 | return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, |
| 88 | OutMP, OutMessage); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 89 | } |