Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 1 | //===-- BitReader.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm-c/BitReader.h" |
| 11 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 3ee23a9 | 2015-02-03 00:49:57 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/LLVMContext.h" |
Filip Pizlo | dec20e4 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 3ee23a9 | 2015-02-03 00:49:57 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 17 | #include <cstring> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include <string> |
Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
Gordon Henriksen | 34eb6d8 | 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 | 25963c6 | 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 | 754946c | 2010-02-15 21:08:22 +0000 | [diff] [blame] | 27 | return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, |
| 28 | OutMessage); |
Owen Anderson | 31d44e4 | 2009-07-02 07:17:57 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 25963c6 | 2010-01-09 22:27:07 +0000 | [diff] [blame] | 31 | LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, |
| 32 | LLVMMemoryBufferRef MemBuf, |
| 33 | LLVMModuleRef *OutModule, |
| 34 | char **OutMessage) { |
Rafael Espindola | 3ee23a9 | 2015-02-03 00:49:57 +0000 | [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 | |
Rafael Espindola | dcd1dca | 2015-06-16 22:27:55 +0000 | [diff] [blame] | 42 | ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile( |
Rafael Espindola | 3ee23a9 | 2015-02-03 00:49:57 +0000 | [diff] [blame] | 43 | Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); |
Rafael Espindola | dcfd6ed | 2015-02-03 01:53:03 +0000 | [diff] [blame] | 44 | if (ModuleOrErr.getError()) { |
| 45 | if (OutMessage) { |
| 46 | Stream.flush(); |
| 47 | *OutMessage = strdup(Message.c_str()); |
| 48 | } |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 49 | *OutModule = wrap((Module*)nullptr); |
Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 50 | return 1; |
| 51 | } |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | dcd1dca | 2015-06-16 22:27:55 +0000 | [diff] [blame] | 53 | *OutModule = wrap(ModuleOrErr.get().release()); |
Gordon Henriksen | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Gordon Henriksen | 34eb6d8 | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 59 | Optionally returns a human-readable error message via OutMessage. */ |
Erick Tryzelaar | ad0e0cb | 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 | 34eb6d8 | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 64 | std::string Message; |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 65 | std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); |
| 66 | |
Rafael Espindola | dcd1dca | 2015-06-16 22:27:55 +0000 | [diff] [blame] | 67 | ErrorOr<std::unique_ptr<Module>> ModuleOrErr = |
Rafael Espindola | 6881215 | 2014-09-03 17:31:46 +0000 | [diff] [blame] | 68 | getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef)); |
Rafael Espindola | e2c1d77 | 2014-08-26 22:00:09 +0000 | [diff] [blame] | 69 | Owner.release(); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 70 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 71 | if (std::error_code EC = ModuleOrErr.getError()) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 72 | *OutM = wrap((Module *)nullptr); |
Gordon Henriksen | 34eb6d8 | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 73 | if (OutMessage) |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 74 | *OutMessage = strdup(EC.message().c_str()); |
Gordon Henriksen | 34eb6d8 | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 75 | return 1; |
| 76 | } |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 77 | |
Rafael Espindola | dcd1dca | 2015-06-16 22:27:55 +0000 | [diff] [blame] | 78 | *OutM = wrap(ModuleOrErr.get().release()); |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 79 | |
Gordon Henriksen | 34eb6d8 | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 80 | return 0; |
Erick Tryzelaar | ad0e0cb | 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 | 2b0eed2 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 106 | } |