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