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 | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 28 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DiagnosticInfo.h" |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 31 | #include "llvm/IR/LLVMContext.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 33 | #include "llvm/IR/ValueSymbolTable.h" |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 34 | #include "llvm/Support/LineIterator.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 35 | #include "llvm/Support/SMLoc.h" |
| 36 | #include "llvm/Support/SourceMgr.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
| 38 | #include "llvm/Support/YAMLTraits.h" |
| 39 | #include <memory> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 43 | namespace llvm { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 44 | |
| 45 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 46 | /// file. |
| 47 | class MIRParserImpl { |
| 48 | SourceMgr SM; |
| 49 | StringRef Filename; |
| 50 | LLVMContext &Context; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 51 | StringMap<std::unique_ptr<yaml::MachineFunction>> Functions; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 52 | SlotMapping IRSlots; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 53 | /// Maps from register class names to register classes. |
| 54 | StringMap<const TargetRegisterClass *> Names2RegClasses; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 55 | |
| 56 | public: |
| 57 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 58 | LLVMContext &Context); |
| 59 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 60 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 61 | |
| 62 | /// Report an error with the given message at unknown location. |
| 63 | /// |
| 64 | /// Always returns true. |
| 65 | bool error(const Twine &Message); |
| 66 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 67 | /// Report an error with the given message at the given location. |
| 68 | /// |
| 69 | /// Always returns true. |
| 70 | bool error(SMLoc Loc, const Twine &Message); |
| 71 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 72 | /// Report a given error with the location translated from the location in an |
| 73 | /// embedded string literal to a location in the MIR file. |
| 74 | /// |
| 75 | /// Always returns true. |
| 76 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 77 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 78 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 79 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 80 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 81 | /// Return null if an error occurred. |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 82 | std::unique_ptr<Module> parse(); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 83 | |
| 84 | /// Parse the machine function in the current YAML document. |
| 85 | /// |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 86 | /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR. |
| 87 | /// A dummy IR function is created and inserted into the given module when |
| 88 | /// this parameter is true. |
| 89 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 90 | /// Return true if an error occurred. |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 91 | bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 92 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 93 | /// Initialize the machine function to the state that's described in the MIR |
| 94 | /// file. |
| 95 | /// |
| 96 | /// Return true if error occurred. |
| 97 | bool initializeMachineFunction(MachineFunction &MF); |
| 98 | |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 99 | bool initializeRegisterInfo(MachineFunction &MF, |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 100 | const yaml::MachineFunction &YamlMF, |
| 101 | PerFunctionMIParsingState &PFS); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 102 | |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 103 | void inferRegisterInfo(MachineFunction &MF, |
| 104 | const yaml::MachineFunction &YamlMF); |
| 105 | |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 106 | bool initializeFrameInfo(MachineFunction &MF, |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 107 | const yaml::MachineFunction &YamlMF, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 108 | PerFunctionMIParsingState &PFS); |
| 109 | |
| 110 | bool parseCalleeSavedRegister(MachineFunction &MF, |
| 111 | PerFunctionMIParsingState &PFS, |
| 112 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 113 | const yaml::StringValue &RegisterSource, |
| 114 | int FrameIdx); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 115 | |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 116 | bool initializeConstantPool(MachineConstantPool &ConstantPool, |
| 117 | const yaml::MachineFunction &YamlMF, |
| 118 | const MachineFunction &MF, |
| 119 | DenseMap<unsigned, unsigned> &ConstantPoolSlots); |
| 120 | |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 121 | bool initializeJumpTableInfo(MachineFunction &MF, |
| 122 | const yaml::MachineJumpTable &YamlJTI, |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 123 | PerFunctionMIParsingState &PFS); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 124 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 125 | private: |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 126 | bool parseMBBReference(MachineBasicBlock *&MBB, |
| 127 | const yaml::StringValue &Source, MachineFunction &MF, |
| 128 | const PerFunctionMIParsingState &PFS); |
| 129 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 130 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 131 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 132 | SMRange SourceRange); |
| 133 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 134 | /// Return a MIR diagnostic converted from a diagnostic located in a YAML |
| 135 | /// block scalar string. |
| 136 | SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 137 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 138 | |
| 139 | /// Create an empty function with the given name. |
| 140 | void createDummyFunction(StringRef Name, Module &M); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 141 | |
| 142 | void initNames2RegClasses(const MachineFunction &MF); |
| 143 | |
| 144 | /// Check if the given identifier is a name of a register class. |
| 145 | /// |
| 146 | /// Return null if the name isn't a register class. |
| 147 | const TargetRegisterClass *getRegClass(const MachineFunction &MF, |
| 148 | StringRef Name); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 151 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 152 | |
| 153 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 154 | StringRef Filename, LLVMContext &Context) |
| 155 | : SM(), Filename(Filename), Context(Context) { |
| 156 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 157 | } |
| 158 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 159 | bool MIRParserImpl::error(const Twine &Message) { |
| 160 | Context.diagnose(DiagnosticInfoMIRParser( |
| 161 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 162 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 163 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 164 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 165 | bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { |
| 166 | Context.diagnose(DiagnosticInfoMIRParser( |
| 167 | DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message))); |
| 168 | return true; |
| 169 | } |
| 170 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 171 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 172 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); |
| 173 | reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); |
| 174 | return true; |
| 175 | } |
| 176 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 177 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 178 | DiagnosticSeverity Kind; |
| 179 | switch (Diag.getKind()) { |
| 180 | case SourceMgr::DK_Error: |
| 181 | Kind = DS_Error; |
| 182 | break; |
| 183 | case SourceMgr::DK_Warning: |
| 184 | Kind = DS_Warning; |
| 185 | break; |
| 186 | case SourceMgr::DK_Note: |
| 187 | Kind = DS_Note; |
| 188 | break; |
| 189 | } |
| 190 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 191 | } |
| 192 | |
| 193 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 194 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 195 | } |
| 196 | |
| 197 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 198 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 199 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 200 | In.setContext(&In); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 201 | |
| 202 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 203 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 204 | return nullptr; |
| 205 | // Create an empty module when the MIR file is empty. |
| 206 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 209 | std::unique_ptr<Module> M; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 210 | bool NoLLVMIR = false; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 211 | // Parse the block scalar manually so that we can return unique pointer |
| 212 | // without having to go trough YAML traits. |
| 213 | if (const auto *BSN = |
| 214 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 215 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 216 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 217 | Context, &IRSlots); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 218 | if (!M) { |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 219 | reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange())); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 220 | return M; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 221 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 222 | In.nextDocument(); |
| 223 | if (!In.setCurrentDocument()) |
| 224 | return M; |
| 225 | } else { |
| 226 | // Create an new, empty module. |
| 227 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 228 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Parse the machine functions. |
| 232 | do { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 233 | if (parseMachineFunction(In, *M, NoLLVMIR)) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 234 | return nullptr; |
| 235 | In.nextDocument(); |
| 236 | } while (In.setCurrentDocument()); |
| 237 | |
| 238 | return M; |
| 239 | } |
| 240 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 241 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, |
| 242 | bool NoLLVMIR) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 243 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 244 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 245 | if (In.error()) |
| 246 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 247 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame] | 248 | if (Functions.find(FunctionName) != Functions.end()) |
| 249 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 250 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 251 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 252 | if (NoLLVMIR) |
| 253 | createDummyFunction(FunctionName, M); |
Alex Lorenz | 5ef16b8 | 2015-06-16 17:06:29 +0000 | [diff] [blame] | 254 | else if (!M.getFunction(FunctionName)) |
| 255 | return error(Twine("function '") + FunctionName + |
| 256 | "' isn't defined in the provided LLVM IR"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 260 | void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 261 | auto &Context = M.getContext(); |
| 262 | Function *F = cast<Function>(M.getOrInsertFunction( |
| 263 | Name, FunctionType::get(Type::getVoidTy(Context), false))); |
| 264 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 265 | new UnreachableInst(Context, BB); |
| 266 | } |
| 267 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 268 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 269 | auto It = Functions.find(MF.getName()); |
| 270 | if (It == Functions.end()) |
| 271 | return error(Twine("no machine function information for function '") + |
| 272 | MF.getName() + "' in the MIR file"); |
| 273 | // TODO: Recreate the machine function. |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 274 | const yaml::MachineFunction &YamlMF = *It->getValue(); |
| 275 | if (YamlMF.Alignment) |
| 276 | MF.setAlignment(YamlMF.Alignment); |
| 277 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 278 | MF.setHasInlineAsm(YamlMF.HasInlineAsm); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 279 | PerFunctionMIParsingState PFS; |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 280 | if (initializeRegisterInfo(MF, YamlMF, PFS)) |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 281 | return true; |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 282 | if (!YamlMF.Constants.empty()) { |
| 283 | auto *ConstantPool = MF.getConstantPool(); |
| 284 | assert(ConstantPool && "Constant pool must be created"); |
| 285 | if (initializeConstantPool(*ConstantPool, YamlMF, MF, |
| 286 | PFS.ConstantPoolSlots)) |
| 287 | return true; |
| 288 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 289 | |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 290 | SMDiagnostic Error; |
| 291 | if (parseMachineBasicBlockDefinitions(MF, YamlMF.Body.Value.Value, PFS, |
| 292 | IRSlots, Error)) { |
| 293 | reportDiagnostic( |
| 294 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 295 | return true; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 298 | if (MF.empty()) |
Alex Lorenz | c8704b0 | 2015-07-09 21:21:33 +0000 | [diff] [blame] | 299 | return error(Twine("machine function '") + Twine(MF.getName()) + |
| 300 | "' requires at least one machine basic block in its body"); |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 301 | // Initialize the frame information after creating all the MBBs so that the |
| 302 | // MBB references in the frame information can be resolved. |
| 303 | if (initializeFrameInfo(MF, YamlMF, PFS)) |
| 304 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 305 | // Initialize the jump table after creating all the MBBs so that the MBB |
| 306 | // references can be resolved. |
| 307 | if (!YamlMF.JumpTableInfo.Entries.empty() && |
| 308 | initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS)) |
| 309 | return true; |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 310 | // Parse the machine instructions after creating all of the MBBs so that the |
| 311 | // parser can resolve the MBB references. |
| 312 | if (parseMachineInstructions(MF, YamlMF.Body.Value.Value, PFS, IRSlots, |
| 313 | Error)) { |
| 314 | reportDiagnostic( |
| 315 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 316 | return true; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 317 | } |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 318 | inferRegisterInfo(MF, YamlMF); |
Alex Lorenz | c7bf204 | 2015-07-24 17:44:49 +0000 | [diff] [blame] | 319 | // FIXME: This is a temporary workaround until the reserved registers can be |
| 320 | // serialized. |
| 321 | MF.getRegInfo().freezeReservedRegs(MF); |
| 322 | MF.verify(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 326 | bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF, |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 327 | const yaml::MachineFunction &YamlMF, |
| 328 | PerFunctionMIParsingState &PFS) { |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 329 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 330 | assert(RegInfo.isSSA()); |
| 331 | if (!YamlMF.IsSSA) |
| 332 | RegInfo.leaveSSA(); |
| 333 | assert(RegInfo.tracksLiveness()); |
| 334 | if (!YamlMF.TracksRegLiveness) |
| 335 | RegInfo.invalidateLiveness(); |
| 336 | RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 337 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 338 | SMDiagnostic Error; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 339 | // Parse the virtual register information. |
| 340 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
| 341 | const auto *RC = getRegClass(MF, VReg.Class.Value); |
| 342 | if (!RC) |
| 343 | return error(VReg.Class.SourceRange.Start, |
| 344 | Twine("use of undefined register class '") + |
| 345 | VReg.Class.Value + "'"); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 346 | unsigned Reg = RegInfo.createVirtualRegister(RC); |
Alex Lorenz | a06c0c6 | 2015-07-30 21:54:10 +0000 | [diff] [blame] | 347 | if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg)) |
| 348 | .second) |
| 349 | return error(VReg.ID.SourceRange.Start, |
| 350 | Twine("redefinition of virtual register '%") + |
| 351 | Twine(VReg.ID.Value) + "'"); |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 352 | if (!VReg.PreferredRegister.Value.empty()) { |
| 353 | unsigned PreferredReg = 0; |
| 354 | if (parseNamedRegisterReference(PreferredReg, SM, MF, |
| 355 | VReg.PreferredRegister.Value, PFS, |
| 356 | IRSlots, Error)) |
| 357 | return error(Error, VReg.PreferredRegister.SourceRange); |
| 358 | RegInfo.setSimpleHint(Reg, PreferredReg); |
| 359 | } |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 360 | } |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 361 | |
| 362 | // Parse the liveins. |
| 363 | for (const auto &LiveIn : YamlMF.LiveIns) { |
| 364 | unsigned Reg = 0; |
| 365 | if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS, |
| 366 | IRSlots, Error)) |
| 367 | return error(Error, LiveIn.Register.SourceRange); |
| 368 | unsigned VReg = 0; |
| 369 | if (!LiveIn.VirtualRegister.Value.empty()) { |
| 370 | if (parseVirtualRegisterReference( |
| 371 | VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error)) |
| 372 | return error(Error, LiveIn.VirtualRegister.SourceRange); |
| 373 | } |
| 374 | RegInfo.addLiveIn(Reg, VReg); |
| 375 | } |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 376 | |
| 377 | // Parse the callee saved register mask. |
| 378 | BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size()); |
| 379 | if (!YamlMF.CalleeSavedRegisters) |
| 380 | return false; |
| 381 | for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) { |
| 382 | unsigned Reg = 0; |
| 383 | if (parseNamedRegisterReference(Reg, SM, MF, RegSource.Value, PFS, IRSlots, |
| 384 | Error)) |
| 385 | return error(Error, RegSource.SourceRange); |
| 386 | CalleeSavedRegisterMask[Reg] = true; |
| 387 | } |
| 388 | RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip()); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 392 | void MIRParserImpl::inferRegisterInfo(MachineFunction &MF, |
| 393 | const yaml::MachineFunction &YamlMF) { |
| 394 | if (YamlMF.CalleeSavedRegisters) |
| 395 | return; |
| 396 | for (const MachineBasicBlock &MBB : MF) { |
| 397 | for (const MachineInstr &MI : MBB) { |
| 398 | for (const MachineOperand &MO : MI.operands()) { |
| 399 | if (!MO.isRegMask()) |
| 400 | continue; |
| 401 | MF.getRegInfo().addPhysRegsUsedFromRegMask(MO.getRegMask()); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 407 | bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 408 | const yaml::MachineFunction &YamlMF, |
| 409 | PerFunctionMIParsingState &PFS) { |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 410 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 411 | const Function &F = *MF.getFunction(); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 412 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 413 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 414 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 415 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 416 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 417 | MFI.setStackSize(YamlMFI.StackSize); |
| 418 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 419 | if (YamlMFI.MaxAlignment) |
| 420 | MFI.ensureMaxAlignment(YamlMFI.MaxAlignment); |
| 421 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 422 | MFI.setHasCalls(YamlMFI.HasCalls); |
| 423 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
| 424 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 425 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 426 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 427 | if (!YamlMFI.SavePoint.Value.empty()) { |
| 428 | MachineBasicBlock *MBB = nullptr; |
| 429 | if (parseMBBReference(MBB, YamlMFI.SavePoint, MF, PFS)) |
| 430 | return true; |
| 431 | MFI.setSavePoint(MBB); |
| 432 | } |
| 433 | if (!YamlMFI.RestorePoint.Value.empty()) { |
| 434 | MachineBasicBlock *MBB = nullptr; |
| 435 | if (parseMBBReference(MBB, YamlMFI.RestorePoint, MF, PFS)) |
| 436 | return true; |
| 437 | MFI.setRestorePoint(MBB); |
| 438 | } |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 439 | |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 440 | std::vector<CalleeSavedInfo> CSIInfo; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 441 | // Initialize the fixed frame objects. |
| 442 | for (const auto &Object : YamlMF.FixedStackObjects) { |
| 443 | int ObjectIdx; |
| 444 | if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) |
| 445 | ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset, |
| 446 | Object.IsImmutable, Object.IsAliased); |
| 447 | else |
| 448 | ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset); |
| 449 | MFI.setObjectAlignment(ObjectIdx, Object.Alignment); |
Alex Lorenz | 1d9a303 | 2015-08-10 23:45:02 +0000 | [diff] [blame] | 450 | if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value, |
| 451 | ObjectIdx)) |
| 452 | .second) |
| 453 | return error(Object.ID.SourceRange.Start, |
| 454 | Twine("redefinition of fixed stack object '%fixed-stack.") + |
| 455 | Twine(Object.ID.Value) + "'"); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 456 | if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister, |
| 457 | ObjectIdx)) |
| 458 | return true; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Initialize the ordinary frame objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 462 | for (const auto &Object : YamlMF.StackObjects) { |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 463 | int ObjectIdx; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 464 | const AllocaInst *Alloca = nullptr; |
| 465 | const yaml::StringValue &Name = Object.Name; |
| 466 | if (!Name.Value.empty()) { |
| 467 | Alloca = dyn_cast_or_null<AllocaInst>( |
| 468 | F.getValueSymbolTable().lookup(Name.Value)); |
| 469 | if (!Alloca) |
| 470 | return error(Name.SourceRange.Start, |
| 471 | "alloca instruction named '" + Name.Value + |
| 472 | "' isn't defined in the function '" + F.getName() + |
| 473 | "'"); |
| 474 | } |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 475 | if (Object.Type == yaml::MachineStackObject::VariableSized) |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 476 | ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca); |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 477 | else |
| 478 | ObjectIdx = MFI.CreateStackObject( |
| 479 | Object.Size, Object.Alignment, |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 480 | Object.Type == yaml::MachineStackObject::SpillSlot, Alloca); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 481 | MFI.setObjectOffset(ObjectIdx, Object.Offset); |
Alex Lorenz | c5d35ba | 2015-08-10 23:50:41 +0000 | [diff] [blame] | 482 | if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx)) |
| 483 | .second) |
| 484 | return error(Object.ID.SourceRange.Start, |
| 485 | Twine("redefinition of stack object '%stack.") + |
| 486 | Twine(Object.ID.Value) + "'"); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 487 | if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister, |
| 488 | ObjectIdx)) |
| 489 | return true; |
Alex Lorenz | a56ba6a | 2015-08-17 22:17:42 +0000 | [diff] [blame^] | 490 | if (Object.LocalOffset) |
| 491 | MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue()); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 492 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 493 | MFI.setCalleeSavedInfo(CSIInfo); |
| 494 | if (!CSIInfo.empty()) |
| 495 | MFI.setCalleeSavedInfoValid(true); |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | bool MIRParserImpl::parseCalleeSavedRegister( |
| 500 | MachineFunction &MF, PerFunctionMIParsingState &PFS, |
| 501 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 502 | const yaml::StringValue &RegisterSource, int FrameIdx) { |
| 503 | if (RegisterSource.Value.empty()) |
| 504 | return false; |
| 505 | unsigned Reg = 0; |
| 506 | SMDiagnostic Error; |
| 507 | if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS, |
| 508 | IRSlots, Error)) |
| 509 | return error(Error, RegisterSource.SourceRange); |
| 510 | CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx)); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 514 | bool MIRParserImpl::initializeConstantPool( |
| 515 | MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF, |
| 516 | const MachineFunction &MF, |
| 517 | DenseMap<unsigned, unsigned> &ConstantPoolSlots) { |
| 518 | const auto &M = *MF.getFunction()->getParent(); |
| 519 | SMDiagnostic Error; |
| 520 | for (const auto &YamlConstant : YamlMF.Constants) { |
| 521 | const Constant *Value = dyn_cast_or_null<Constant>( |
| 522 | parseConstantValue(YamlConstant.Value.Value, Error, M)); |
| 523 | if (!Value) |
| 524 | return error(Error, YamlConstant.Value.SourceRange); |
| 525 | unsigned Alignment = |
| 526 | YamlConstant.Alignment |
| 527 | ? YamlConstant.Alignment |
| 528 | : M.getDataLayout().getPrefTypeAlignment(Value->getType()); |
Alex Lorenz | 60bf599 | 2015-07-30 22:00:17 +0000 | [diff] [blame] | 529 | unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment); |
| 530 | if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index)) |
| 531 | .second) |
| 532 | return error(YamlConstant.ID.SourceRange.Start, |
| 533 | Twine("redefinition of constant pool item '%const.") + |
| 534 | Twine(YamlConstant.ID.Value) + "'"); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 535 | } |
| 536 | return false; |
| 537 | } |
| 538 | |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 539 | bool MIRParserImpl::initializeJumpTableInfo( |
| 540 | MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI, |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 541 | PerFunctionMIParsingState &PFS) { |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 542 | MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 543 | for (const auto &Entry : YamlJTI.Entries) { |
| 544 | std::vector<MachineBasicBlock *> Blocks; |
| 545 | for (const auto &MBBSource : Entry.Blocks) { |
| 546 | MachineBasicBlock *MBB = nullptr; |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 547 | if (parseMBBReference(MBB, MBBSource.Value, MF, PFS)) |
| 548 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 549 | Blocks.push_back(MBB); |
| 550 | } |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 551 | unsigned Index = JTI->createJumpTableIndex(Blocks); |
Alex Lorenz | 59ed591 | 2015-07-31 23:13:23 +0000 | [diff] [blame] | 552 | if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index)) |
| 553 | .second) |
| 554 | return error(Entry.ID.SourceRange.Start, |
| 555 | Twine("redefinition of jump table entry '%jump-table.") + |
| 556 | Twine(Entry.ID.Value) + "'"); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 561 | bool MIRParserImpl::parseMBBReference(MachineBasicBlock *&MBB, |
| 562 | const yaml::StringValue &Source, |
| 563 | MachineFunction &MF, |
| 564 | const PerFunctionMIParsingState &PFS) { |
| 565 | SMDiagnostic Error; |
| 566 | if (llvm::parseMBBReference(MBB, SM, MF, Source.Value, PFS, IRSlots, Error)) |
| 567 | return error(Error, Source.SourceRange); |
| 568 | return false; |
| 569 | } |
| 570 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 571 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 572 | SMRange SourceRange) { |
| 573 | assert(SourceRange.isValid() && "Invalid source range"); |
| 574 | SMLoc Loc = SourceRange.Start; |
| 575 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 576 | *Loc.getPointer() == '\''; |
| 577 | // Translate the location of the error from the location in the MI string to |
| 578 | // the corresponding location in the MIR file. |
| 579 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 580 | (HasQuote ? 1 : 0)); |
| 581 | |
| 582 | // TODO: Translate any source ranges as well. |
| 583 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 584 | Error.getFixIts()); |
| 585 | } |
| 586 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 587 | SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 588 | SMRange SourceRange) { |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 589 | assert(SourceRange.isValid()); |
| 590 | |
| 591 | // Translate the location of the error from the location in the llvm IR string |
| 592 | // to the corresponding location in the MIR file. |
| 593 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 594 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 595 | unsigned Column = Error.getColumnNo(); |
| 596 | StringRef LineStr = Error.getLineContents(); |
| 597 | SMLoc Loc = Error.getLoc(); |
| 598 | |
| 599 | // Get the full line and adjust the column number by taking the indentation of |
| 600 | // LLVM IR into account. |
| 601 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 602 | L != E; ++L) { |
| 603 | if (L.line_number() == Line) { |
| 604 | LineStr = *L; |
| 605 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 606 | auto Indent = LineStr.find(Error.getLineContents()); |
| 607 | if (Indent != StringRef::npos) |
| 608 | Column += Indent; |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 614 | Error.getMessage(), LineStr, Error.getRanges(), |
| 615 | Error.getFixIts()); |
| 616 | } |
| 617 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 618 | void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) { |
| 619 | if (!Names2RegClasses.empty()) |
| 620 | return; |
| 621 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 622 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 623 | const auto *RC = TRI->getRegClass(I); |
| 624 | Names2RegClasses.insert( |
| 625 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF, |
| 630 | StringRef Name) { |
| 631 | initNames2RegClasses(MF); |
| 632 | auto RegClassInfo = Names2RegClasses.find(Name); |
| 633 | if (RegClassInfo == Names2RegClasses.end()) |
| 634 | return nullptr; |
| 635 | return RegClassInfo->getValue(); |
| 636 | } |
| 637 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 638 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 639 | : Impl(std::move(Impl)) {} |
| 640 | |
| 641 | MIRParser::~MIRParser() {} |
| 642 | |
| 643 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 644 | |
| 645 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 646 | return Impl->initializeMachineFunction(MF); |
| 647 | } |
| 648 | |
| 649 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 650 | SMDiagnostic &Error, |
| 651 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 652 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 653 | if (std::error_code EC = FileOrErr.getError()) { |
| 654 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 655 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 656 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 657 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 658 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 661 | std::unique_ptr<MIRParser> |
| 662 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 663 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 664 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 665 | return llvm::make_unique<MIRParser>( |
| 666 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 667 | } |