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