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) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 33 | ErrorOr<Module *> ModuleOrErr = |
| 34 | parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef)); |
| 35 | if (error_code EC = ModuleOrErr.getError()) { |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 36 | if (OutMessage) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 37 | *OutMessage = strdup(EC.message().c_str()); |
| 38 | *OutModule = wrap((Module*)0); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 39 | return 1; |
| 40 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 41 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 42 | *OutModule = wrap(ModuleOrErr.get()); |
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; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 54 | ErrorOr<Module *> ModuleOrErr = |
| 55 | getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef)); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 56 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 57 | if (error_code EC = ModuleOrErr.getError()) { |
| 58 | *OutM = wrap((Module *)NULL); |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 59 | if (OutMessage) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 60 | *OutMessage = strdup(EC.message().c_str()); |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 63 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 64 | *OutM = wrap(ModuleOrErr.get()); |
| 65 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 66 | return 0; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 67 | |
| 68 | } |
| 69 | |
| 70 | LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 71 | char **OutMessage) { |
| 72 | return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, |
| 73 | OutMessage); |
| 74 | } |
| 75 | |
| 76 | /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ |
| 77 | LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, |
| 78 | LLVMMemoryBufferRef MemBuf, |
| 79 | LLVMModuleProviderRef *OutMP, |
| 80 | char **OutMessage) { |
| 81 | return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, |
| 82 | reinterpret_cast<LLVMModuleRef*>(OutMP), |
| 83 | OutMessage); |
| 84 | } |
| 85 | |
| 86 | /* Deprecated: Use LLVMGetBitcodeModule instead. */ |
| 87 | LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, |
| 88 | LLVMModuleProviderRef *OutMP, |
| 89 | char **OutMessage) { |
| 90 | return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, |
| 91 | OutMP, OutMessage); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 92 | } |