Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 1 | //===- MIRParser.cpp - MIR serialization format parser implementation -----===// |
| 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 | // This file implements the class that parses the optional LLVM IR and machine |
| 11 | // functions that are stored in MIR files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/AsmParser/Parser.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DiagnosticInfo.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/LineIterator.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SMLoc.h" |
| 27 | #include "llvm/Support/SourceMgr.h" |
| 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/YAMLTraits.h" |
| 30 | #include <memory> |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 34 | namespace llvm { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 35 | |
| 36 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 37 | /// file. |
| 38 | class MIRParserImpl { |
| 39 | SourceMgr SM; |
| 40 | StringRef Filename; |
| 41 | LLVMContext &Context; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 42 | StringMap<std::unique_ptr<yaml::MachineFunction>> Functions; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
| 45 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 46 | LLVMContext &Context); |
| 47 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 48 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 49 | |
| 50 | /// Report an error with the given message at unknown location. |
| 51 | /// |
| 52 | /// Always returns true. |
| 53 | bool error(const Twine &Message); |
| 54 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 55 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 56 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 57 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 58 | /// Return null if an error occurred. |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 59 | std::unique_ptr<Module> parse(); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 60 | |
| 61 | /// Parse the machine function in the current YAML document. |
| 62 | /// |
| 63 | /// Return true if an error occurred. |
| 64 | bool parseMachineFunction(yaml::Input &In); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 65 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 66 | /// Initialize the machine function to the state that's described in the MIR |
| 67 | /// file. |
| 68 | /// |
| 69 | /// Return true if error occurred. |
| 70 | bool initializeMachineFunction(MachineFunction &MF); |
| 71 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 72 | private: |
| 73 | /// Return a MIR diagnostic converted from an LLVM assembly diagnostic. |
| 74 | SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 75 | SMRange SourceRange); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 78 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 79 | |
| 80 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 81 | StringRef Filename, LLVMContext &Context) |
| 82 | : SM(), Filename(Filename), Context(Context) { |
| 83 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 84 | } |
| 85 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 86 | bool MIRParserImpl::error(const Twine &Message) { |
| 87 | Context.diagnose(DiagnosticInfoMIRParser( |
| 88 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 89 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 90 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 91 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 92 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 93 | DiagnosticSeverity Kind; |
| 94 | switch (Diag.getKind()) { |
| 95 | case SourceMgr::DK_Error: |
| 96 | Kind = DS_Error; |
| 97 | break; |
| 98 | case SourceMgr::DK_Warning: |
| 99 | Kind = DS_Warning; |
| 100 | break; |
| 101 | case SourceMgr::DK_Note: |
| 102 | Kind = DS_Note; |
| 103 | break; |
| 104 | } |
| 105 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 106 | } |
| 107 | |
| 108 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 109 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 110 | } |
| 111 | |
| 112 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 113 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 114 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 115 | |
| 116 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 117 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 118 | return nullptr; |
| 119 | // Create an empty module when the MIR file is empty. |
| 120 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 123 | std::unique_ptr<Module> M; |
| 124 | // Parse the block scalar manually so that we can return unique pointer |
| 125 | // without having to go trough YAML traits. |
| 126 | if (const auto *BSN = |
| 127 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 128 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 129 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
| 130 | Context); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 131 | if (!M) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 132 | reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange())); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 133 | return M; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 134 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 135 | In.nextDocument(); |
| 136 | if (!In.setCurrentDocument()) |
| 137 | return M; |
| 138 | } else { |
| 139 | // Create an new, empty module. |
| 140 | M = llvm::make_unique<Module>(Filename, Context); |
| 141 | } |
| 142 | |
| 143 | // Parse the machine functions. |
| 144 | do { |
| 145 | if (parseMachineFunction(In)) |
| 146 | return nullptr; |
| 147 | In.nextDocument(); |
| 148 | } while (In.setCurrentDocument()); |
| 149 | |
| 150 | return M; |
| 151 | } |
| 152 | |
| 153 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 154 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 155 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 156 | if (In.error()) |
| 157 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 158 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame^] | 159 | if (Functions.find(FunctionName) != Functions.end()) |
| 160 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 161 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 162 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 167 | auto It = Functions.find(MF.getName()); |
| 168 | if (It == Functions.end()) |
| 169 | return error(Twine("no machine function information for function '") + |
| 170 | MF.getName() + "' in the MIR file"); |
| 171 | // TODO: Recreate the machine function. |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 172 | return false; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 175 | SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 176 | SMRange SourceRange) { |
| 177 | assert(SourceRange.isValid()); |
| 178 | |
| 179 | // Translate the location of the error from the location in the llvm IR string |
| 180 | // to the corresponding location in the MIR file. |
| 181 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 182 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 183 | unsigned Column = Error.getColumnNo(); |
| 184 | StringRef LineStr = Error.getLineContents(); |
| 185 | SMLoc Loc = Error.getLoc(); |
| 186 | |
| 187 | // Get the full line and adjust the column number by taking the indentation of |
| 188 | // LLVM IR into account. |
| 189 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 190 | L != E; ++L) { |
| 191 | if (L.line_number() == Line) { |
| 192 | LineStr = *L; |
| 193 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 194 | auto Indent = LineStr.find(Error.getLineContents()); |
| 195 | if (Indent != StringRef::npos) |
| 196 | Column += Indent; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 202 | Error.getMessage(), LineStr, Error.getRanges(), |
| 203 | Error.getFixIts()); |
| 204 | } |
| 205 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 206 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 207 | : Impl(std::move(Impl)) {} |
| 208 | |
| 209 | MIRParser::~MIRParser() {} |
| 210 | |
| 211 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 212 | |
| 213 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 214 | return Impl->initializeMachineFunction(MF); |
| 215 | } |
| 216 | |
| 217 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 218 | SMDiagnostic &Error, |
| 219 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 220 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 221 | if (std::error_code EC = FileOrErr.getError()) { |
| 222 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 223 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 224 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 225 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 226 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 229 | std::unique_ptr<MIRParser> |
| 230 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 231 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 232 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 233 | return llvm::make_unique<MIRParser>( |
| 234 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 235 | } |