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" |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 12 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/LLVMContext.h" |
Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MemoryBuffer.h" |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 16 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 17 | #include <cstring> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include <string> |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 22 | /* Builds a module from the bitcode in the specified memory buffer, returning a |
| 23 | reference to the module via the OutModule parameter. Returns 0 on success. |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 24 | Optionally returns a human-readable error message via OutMessage. */ |
| 25 | LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, |
| 26 | LLVMModuleRef *OutModule, char **OutMessage) { |
Daniel Dunbar | e44fc85 | 2010-02-15 21:08:22 +0000 | [diff] [blame] | 27 | return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, |
| 28 | OutMessage); |
Owen Anderson | c8897d9 | 2009-07-02 07:17:57 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | d686c8e | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 31 | LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, |
| 32 | LLVMMemoryBufferRef MemBuf, |
| 33 | LLVMModuleRef *OutModule, |
| 34 | char **OutMessage) { |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 35 | MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); |
| 36 | LLVMContext &Ctx = *unwrap(ContextRef); |
| 37 | |
| 38 | std::string Message; |
| 39 | raw_string_ostream Stream(Message); |
| 40 | DiagnosticPrinterRawOStream DP(Stream); |
| 41 | |
| 42 | ErrorOr<Module *> ModuleOrErr = parseBitcodeFile( |
| 43 | Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); |
| 44 | if (ModuleOrErr.getError()) { |
| 45 | if (OutMessage) { |
| 46 | Stream.flush(); |
| 47 | *OutMessage = strdup(Message.c_str()); |
| 48 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 49 | *OutModule = wrap((Module*)nullptr); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 50 | return 1; |
| 51 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 52 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 53 | *OutModule = wrap(ModuleOrErr.get()); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 57 | /* Reads a module from the specified path, returning via the OutModule parameter |
| 58 | a module provider which performs lazy deserialization. Returns 0 on success. |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 59 | Optionally returns a human-readable error message via OutMessage. */ |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 60 | LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, |
| 61 | LLVMMemoryBufferRef MemBuf, |
| 62 | LLVMModuleRef *OutM, |
| 63 | char **OutMessage) { |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 64 | std::string Message; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 65 | std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); |
| 66 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 67 | ErrorOr<Module *> ModuleOrErr = |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 68 | getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef)); |
| 69 | Owner.release(); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 70 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 71 | if (std::error_code EC = ModuleOrErr.getError()) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 72 | *OutM = wrap((Module *)nullptr); |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 73 | if (OutMessage) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 74 | *OutMessage = strdup(EC.message().c_str()); |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 75 | return 1; |
| 76 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 77 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 78 | *OutM = wrap(ModuleOrErr.get()); |
| 79 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 80 | return 0; |
Erick Tryzelaar | df7df07 | 2010-03-02 23:58:54 +0000 | [diff] [blame] | 81 | |
| 82 | } |
| 83 | |
| 84 | LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 85 | char **OutMessage) { |
| 86 | return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, |
| 87 | OutMessage); |
| 88 | } |
| 89 | |
| 90 | /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ |
| 91 | LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, |
| 92 | LLVMMemoryBufferRef MemBuf, |
| 93 | LLVMModuleProviderRef *OutMP, |
| 94 | char **OutMessage) { |
| 95 | return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, |
| 96 | reinterpret_cast<LLVMModuleRef*>(OutMP), |
| 97 | OutMessage); |
| 98 | } |
| 99 | |
| 100 | /* Deprecated: Use LLVMGetBitcodeModule instead. */ |
| 101 | LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, |
| 102 | LLVMModuleProviderRef *OutMP, |
| 103 | char **OutMessage) { |
| 104 | return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, |
| 105 | OutMP, OutMessage); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 106 | } |