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