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" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 16 | #include "MIParser.h" |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/AsmParser/Parser.h" |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 22 | #include "llvm/AsmParser/SlotMapping.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 26 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DiagnosticInfo.h" |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LLVMContext.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 31 | #include "llvm/IR/ValueSymbolTable.h" |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 32 | #include "llvm/Support/LineIterator.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SMLoc.h" |
| 34 | #include "llvm/Support/SourceMgr.h" |
| 35 | #include "llvm/Support/MemoryBuffer.h" |
| 36 | #include "llvm/Support/YAMLTraits.h" |
| 37 | #include <memory> |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 41 | namespace llvm { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 42 | |
| 43 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 44 | /// file. |
| 45 | class MIRParserImpl { |
| 46 | SourceMgr SM; |
| 47 | StringRef Filename; |
| 48 | LLVMContext &Context; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 49 | StringMap<std::unique_ptr<yaml::MachineFunction>> Functions; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 50 | SlotMapping IRSlots; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 51 | |
| 52 | public: |
| 53 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 54 | LLVMContext &Context); |
| 55 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 56 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 57 | |
| 58 | /// Report an error with the given message at unknown location. |
| 59 | /// |
| 60 | /// Always returns true. |
| 61 | bool error(const Twine &Message); |
| 62 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 63 | /// Report a given error with the location translated from the location in an |
| 64 | /// embedded string literal to a location in the MIR file. |
| 65 | /// |
| 66 | /// Always returns true. |
| 67 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 68 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 69 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 70 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 71 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 72 | /// Return null if an error occurred. |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 73 | std::unique_ptr<Module> parse(); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 74 | |
| 75 | /// Parse the machine function in the current YAML document. |
| 76 | /// |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 77 | /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR. |
| 78 | /// A dummy IR function is created and inserted into the given module when |
| 79 | /// this parameter is true. |
| 80 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 81 | /// Return true if an error occurred. |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 82 | bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 83 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 84 | /// Initialize the machine function to the state that's described in the MIR |
| 85 | /// file. |
| 86 | /// |
| 87 | /// Return true if error occurred. |
| 88 | bool initializeMachineFunction(MachineFunction &MF); |
| 89 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 90 | /// Initialize the machine basic block using it's YAML representation. |
| 91 | /// |
| 92 | /// Return true if an error occurred. |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 93 | bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB, |
| 94 | const yaml::MachineBasicBlock &YamlMBB, |
| 95 | const PerFunctionMIParsingState &PFS); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 96 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 97 | bool initializeRegisterInfo(MachineRegisterInfo &RegInfo, |
| 98 | const yaml::MachineFunction &YamlMF); |
| 99 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 100 | private: |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 101 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 102 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 103 | SMRange SourceRange); |
| 104 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 105 | /// Return a MIR diagnostic converted from an LLVM assembly diagnostic. |
| 106 | SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 107 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 108 | |
| 109 | /// Create an empty function with the given name. |
| 110 | void createDummyFunction(StringRef Name, Module &M); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 113 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 114 | |
| 115 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 116 | StringRef Filename, LLVMContext &Context) |
| 117 | : SM(), Filename(Filename), Context(Context) { |
| 118 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 119 | } |
| 120 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 121 | bool MIRParserImpl::error(const Twine &Message) { |
| 122 | Context.diagnose(DiagnosticInfoMIRParser( |
| 123 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 124 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 125 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 126 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 127 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 128 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); |
| 129 | reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); |
| 130 | return true; |
| 131 | } |
| 132 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 133 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 134 | DiagnosticSeverity Kind; |
| 135 | switch (Diag.getKind()) { |
| 136 | case SourceMgr::DK_Error: |
| 137 | Kind = DS_Error; |
| 138 | break; |
| 139 | case SourceMgr::DK_Warning: |
| 140 | Kind = DS_Warning; |
| 141 | break; |
| 142 | case SourceMgr::DK_Note: |
| 143 | Kind = DS_Note; |
| 144 | break; |
| 145 | } |
| 146 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 147 | } |
| 148 | |
| 149 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 150 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 151 | } |
| 152 | |
| 153 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 154 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 155 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 156 | In.setContext(&In); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 157 | |
| 158 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 159 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 160 | return nullptr; |
| 161 | // Create an empty module when the MIR file is empty. |
| 162 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 165 | std::unique_ptr<Module> M; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 166 | bool NoLLVMIR = false; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 167 | // Parse the block scalar manually so that we can return unique pointer |
| 168 | // without having to go trough YAML traits. |
| 169 | if (const auto *BSN = |
| 170 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 171 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 172 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 173 | Context, &IRSlots); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 174 | if (!M) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 175 | reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange())); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 176 | return M; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 177 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 178 | In.nextDocument(); |
| 179 | if (!In.setCurrentDocument()) |
| 180 | return M; |
| 181 | } else { |
| 182 | // Create an new, empty module. |
| 183 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 184 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Parse the machine functions. |
| 188 | do { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 189 | if (parseMachineFunction(In, *M, NoLLVMIR)) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 190 | return nullptr; |
| 191 | In.nextDocument(); |
| 192 | } while (In.setCurrentDocument()); |
| 193 | |
| 194 | return M; |
| 195 | } |
| 196 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 197 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, |
| 198 | bool NoLLVMIR) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 199 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 200 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 201 | if (In.error()) |
| 202 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 203 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame] | 204 | if (Functions.find(FunctionName) != Functions.end()) |
| 205 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 206 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 207 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 208 | if (NoLLVMIR) |
| 209 | createDummyFunction(FunctionName, M); |
Alex Lorenz | 5ef16b8 | 2015-06-16 17:06:29 +0000 | [diff] [blame] | 210 | else if (!M.getFunction(FunctionName)) |
| 211 | return error(Twine("function '") + FunctionName + |
| 212 | "' isn't defined in the provided LLVM IR"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 216 | void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 217 | auto &Context = M.getContext(); |
| 218 | Function *F = cast<Function>(M.getOrInsertFunction( |
| 219 | Name, FunctionType::get(Type::getVoidTy(Context), false))); |
| 220 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 221 | new UnreachableInst(Context, BB); |
| 222 | } |
| 223 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 224 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 225 | auto It = Functions.find(MF.getName()); |
| 226 | if (It == Functions.end()) |
| 227 | return error(Twine("no machine function information for function '") + |
| 228 | MF.getName() + "' in the MIR file"); |
| 229 | // TODO: Recreate the machine function. |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 230 | const yaml::MachineFunction &YamlMF = *It->getValue(); |
| 231 | if (YamlMF.Alignment) |
| 232 | MF.setAlignment(YamlMF.Alignment); |
| 233 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 234 | MF.setHasInlineAsm(YamlMF.HasInlineAsm); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 235 | if (initializeRegisterInfo(MF.getRegInfo(), YamlMF)) |
| 236 | return true; |
| 237 | |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 238 | PerFunctionMIParsingState PFS; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 239 | const auto &F = *MF.getFunction(); |
| 240 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 241 | const BasicBlock *BB = nullptr; |
| 242 | if (!YamlMBB.Name.empty()) { |
| 243 | BB = dyn_cast_or_null<BasicBlock>( |
| 244 | F.getValueSymbolTable().lookup(YamlMBB.Name)); |
Alex Lorenz | 00302df | 2015-06-19 20:12:03 +0000 | [diff] [blame] | 245 | if (!BB) |
| 246 | return error(Twine("basic block '") + YamlMBB.Name + |
| 247 | "' is not defined in the function '" + MF.getName() + "'"); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 248 | } |
| 249 | auto *MBB = MF.CreateMachineBasicBlock(BB); |
| 250 | MF.insert(MF.end(), MBB); |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 251 | bool WasInserted = |
| 252 | PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 253 | if (!WasInserted) |
| 254 | return error(Twine("redefinition of machine basic block with id #") + |
| 255 | Twine(YamlMBB.ID)); |
| 256 | } |
| 257 | |
| 258 | // Initialize the machine basic blocks after creating them all so that the |
| 259 | // machine instructions parser can resolve the MBB references. |
| 260 | unsigned I = 0; |
| 261 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 262 | if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 263 | PFS)) |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 264 | return true; |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | bool MIRParserImpl::initializeMachineBasicBlock( |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 270 | MachineFunction &MF, MachineBasicBlock &MBB, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 271 | const yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 272 | const PerFunctionMIParsingState &PFS) { |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 273 | MBB.setAlignment(YamlMBB.Alignment); |
| 274 | if (YamlMBB.AddressTaken) |
| 275 | MBB.setHasAddressTaken(); |
| 276 | MBB.setIsLandingPad(YamlMBB.IsLandingPad); |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 277 | SMDiagnostic Error; |
| 278 | // Parse the successors. |
| 279 | for (const auto &MBBSource : YamlMBB.Successors) { |
| 280 | MachineBasicBlock *SuccMBB = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 281 | if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots, |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 282 | Error)) |
| 283 | return error(Error, MBBSource.SourceRange); |
| 284 | // TODO: Report an error when adding the same successor more than once. |
| 285 | MBB.addSuccessor(SuccMBB); |
| 286 | } |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 287 | // Parse the instructions. |
| 288 | for (const auto &MISource : YamlMBB.Instructions) { |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 289 | MachineInstr *MI = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame^] | 290 | if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error)) |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 291 | return error(Error, MISource.SourceRange); |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 292 | MBB.insert(MBB.end(), MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 293 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 294 | return false; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 297 | bool MIRParserImpl::initializeRegisterInfo( |
| 298 | MachineRegisterInfo &RegInfo, const yaml::MachineFunction &YamlMF) { |
| 299 | assert(RegInfo.isSSA()); |
| 300 | if (!YamlMF.IsSSA) |
| 301 | RegInfo.leaveSSA(); |
| 302 | assert(RegInfo.tracksLiveness()); |
| 303 | if (!YamlMF.TracksRegLiveness) |
| 304 | RegInfo.invalidateLiveness(); |
| 305 | RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness); |
| 306 | return false; |
| 307 | } |
| 308 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 309 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 310 | SMRange SourceRange) { |
| 311 | assert(SourceRange.isValid() && "Invalid source range"); |
| 312 | SMLoc Loc = SourceRange.Start; |
| 313 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 314 | *Loc.getPointer() == '\''; |
| 315 | // Translate the location of the error from the location in the MI string to |
| 316 | // the corresponding location in the MIR file. |
| 317 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 318 | (HasQuote ? 1 : 0)); |
| 319 | |
| 320 | // TODO: Translate any source ranges as well. |
| 321 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 322 | Error.getFixIts()); |
| 323 | } |
| 324 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 325 | SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 326 | SMRange SourceRange) { |
| 327 | assert(SourceRange.isValid()); |
| 328 | |
| 329 | // Translate the location of the error from the location in the llvm IR string |
| 330 | // to the corresponding location in the MIR file. |
| 331 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 332 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 333 | unsigned Column = Error.getColumnNo(); |
| 334 | StringRef LineStr = Error.getLineContents(); |
| 335 | SMLoc Loc = Error.getLoc(); |
| 336 | |
| 337 | // Get the full line and adjust the column number by taking the indentation of |
| 338 | // LLVM IR into account. |
| 339 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 340 | L != E; ++L) { |
| 341 | if (L.line_number() == Line) { |
| 342 | LineStr = *L; |
| 343 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 344 | auto Indent = LineStr.find(Error.getLineContents()); |
| 345 | if (Indent != StringRef::npos) |
| 346 | Column += Indent; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 352 | Error.getMessage(), LineStr, Error.getRanges(), |
| 353 | Error.getFixIts()); |
| 354 | } |
| 355 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 356 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 357 | : Impl(std::move(Impl)) {} |
| 358 | |
| 359 | MIRParser::~MIRParser() {} |
| 360 | |
| 361 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 362 | |
| 363 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 364 | return Impl->initializeMachineFunction(MF); |
| 365 | } |
| 366 | |
| 367 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 368 | SMDiagnostic &Error, |
| 369 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 370 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 371 | if (std::error_code EC = FileOrErr.getError()) { |
| 372 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 373 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 374 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 375 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 376 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 379 | std::unique_ptr<MIRParser> |
| 380 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 381 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 382 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 383 | return llvm::make_unique<MIRParser>( |
| 384 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 385 | } |