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