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