Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 1 | //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 11 | #include "llvm-c/Core.h" |
| 12 | #include "llvm-c/IRReader.h" |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 13 | #include "llvm/AsmParser/Parser.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 14 | #include "llvm/Bitcode/ReaderWriter.h" |
Peter Zotov | 285eed6 | 2013-11-06 09:21:15 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
| 16 | #include "llvm/IR/Module.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
| 18 | #include "llvm/Support/SourceMgr.h" |
Eli Bendersky | b35a211 | 2013-04-03 15:33:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Timer.h" |
Peter Zotov | 285eed6 | 2013-11-06 09:21:15 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/Support/system_error.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
Eli Bendersky | b35a211 | 2013-04-03 15:33:45 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | extern bool TimePassesIsEnabled; |
| 27 | } |
| 28 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 29 | static const char *const TimeIRParsingGroupName = "LLVM IR Parsing"; |
| 30 | static const char *const TimeIRParsingName = "Parse IR"; |
Eli Bendersky | b35a211 | 2013-04-03 15:33:45 +0000 | [diff] [blame] | 31 | |
| 32 | |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 33 | Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err, |
Eli Bendersky | e60fc2f | 2013-04-01 19:47:56 +0000 | [diff] [blame] | 34 | LLVMContext &Context) { |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 35 | if (isBitcode((const unsigned char *)Buffer->getBufferStart(), |
| 36 | (const unsigned char *)Buffer->getBufferEnd())) { |
| 37 | std::string ErrMsg; |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 38 | ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); |
| 39 | if (error_code EC = ModuleOrErr.getError()) { |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 40 | Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 41 | EC.message()); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 42 | // ParseBitcodeFile does not take ownership of the Buffer in the |
| 43 | // case of an error. |
| 44 | delete Buffer; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 45 | return nullptr; |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 46 | } |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 47 | return ModuleOrErr.get(); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 50 | return ParseAssembly(Buffer, nullptr, Err, Context); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err, |
Eli Bendersky | e60fc2f | 2013-04-01 19:47:56 +0000 | [diff] [blame] | 54 | LLVMContext &Context) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 55 | std::unique_ptr<MemoryBuffer> File; |
Rafael Espindola | 8c81172 | 2013-06-25 05:28:34 +0000 | [diff] [blame] | 56 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 57 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 58 | "Could not open input file: " + ec.message()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 59 | return nullptr; |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 62 | return getLazyIRModule(File.release(), Err, Context); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, |
| 66 | LLVMContext &Context) { |
Eli Bendersky | b35a211 | 2013-04-03 15:33:45 +0000 | [diff] [blame] | 67 | NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName, |
| 68 | TimePassesIsEnabled); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 69 | if (isBitcode((const unsigned char *)Buffer->getBufferStart(), |
| 70 | (const unsigned char *)Buffer->getBufferEnd())) { |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 71 | ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 72 | Module *M = nullptr; |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 73 | if (error_code EC = ModuleOrErr.getError()) |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 74 | Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, |
Rafael Espindola | 8f31e21 | 2014-01-15 01:08:23 +0000 | [diff] [blame] | 75 | EC.message()); |
| 76 | else |
| 77 | M = ModuleOrErr.get(); |
| 78 | // parseBitcodeFile does not take ownership of the Buffer. |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 79 | delete Buffer; |
| 80 | return M; |
| 81 | } |
| 82 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 83 | return ParseAssembly(Buffer, nullptr, Err, Context); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, |
| 87 | LLVMContext &Context) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 88 | std::unique_ptr<MemoryBuffer> File; |
Rafael Espindola | 8c81172 | 2013-06-25 05:28:34 +0000 | [diff] [blame] | 89 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 90 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 91 | "Could not open input file: " + ec.message()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 92 | return nullptr; |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 95 | return ParseIR(File.release(), Err, Context); |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 96 | } |
Peter Zotov | 285eed6 | 2013-11-06 09:21:15 +0000 | [diff] [blame] | 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | // C API. |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
| 102 | LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef, |
| 103 | LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 104 | char **OutMessage) { |
| 105 | SMDiagnostic Diag; |
| 106 | |
| 107 | *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef))); |
| 108 | |
| 109 | if(!*OutM) { |
| 110 | if (OutMessage) { |
| 111 | std::string buf; |
| 112 | raw_string_ostream os(buf); |
| 113 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame^] | 114 | Diag.print(nullptr, os, false); |
Peter Zotov | 285eed6 | 2013-11-06 09:21:15 +0000 | [diff] [blame] | 115 | os.flush(); |
| 116 | |
| 117 | *OutMessage = strdup(buf.c_str()); |
| 118 | } |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |