Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 1 | //===- MIRParser.cpp - MIR serialization format parser implementation -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the class that parses the optional LLVM IR and machine |
| 11 | // functions that are stored in MIR files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 16 | #include "MIParser.h" |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/AsmParser/Parser.h" |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 22 | #include "llvm/AsmParser/SlotMapping.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 27 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DiagnosticInfo.h" |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Module.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 32 | #include "llvm/IR/ValueSymbolTable.h" |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/LineIterator.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SMLoc.h" |
| 35 | #include "llvm/Support/SourceMgr.h" |
| 36 | #include "llvm/Support/MemoryBuffer.h" |
| 37 | #include "llvm/Support/YAMLTraits.h" |
| 38 | #include <memory> |
| 39 | |
| 40 | using namespace llvm; |
| 41 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 42 | namespace llvm { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 43 | |
| 44 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 45 | /// file. |
| 46 | class MIRParserImpl { |
| 47 | SourceMgr SM; |
| 48 | StringRef Filename; |
| 49 | LLVMContext &Context; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 50 | StringMap<std::unique_ptr<yaml::MachineFunction>> Functions; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 51 | SlotMapping IRSlots; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 52 | /// Maps from register class names to register classes. |
| 53 | StringMap<const TargetRegisterClass *> Names2RegClasses; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 57 | LLVMContext &Context); |
| 58 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 59 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 60 | |
| 61 | /// Report an error with the given message at unknown location. |
| 62 | /// |
| 63 | /// Always returns true. |
| 64 | bool error(const Twine &Message); |
| 65 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 66 | /// Report an error with the given message at the given location. |
| 67 | /// |
| 68 | /// Always returns true. |
| 69 | bool error(SMLoc Loc, const Twine &Message); |
| 70 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 71 | /// Report a given error with the location translated from the location in an |
| 72 | /// embedded string literal to a location in the MIR file. |
| 73 | /// |
| 74 | /// Always returns true. |
| 75 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 76 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 77 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 78 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 79 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 80 | /// Return null if an error occurred. |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 81 | std::unique_ptr<Module> parse(); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 82 | |
| 83 | /// Parse the machine function in the current YAML document. |
| 84 | /// |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 85 | /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR. |
| 86 | /// A dummy IR function is created and inserted into the given module when |
| 87 | /// this parameter is true. |
| 88 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 89 | /// Return true if an error occurred. |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 90 | bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 91 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 92 | /// Initialize the machine function to the state that's described in the MIR |
| 93 | /// file. |
| 94 | /// |
| 95 | /// Return true if error occurred. |
| 96 | bool initializeMachineFunction(MachineFunction &MF); |
| 97 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 98 | /// Initialize the machine basic block using it's YAML representation. |
| 99 | /// |
| 100 | /// Return true if an error occurred. |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 101 | bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB, |
| 102 | const yaml::MachineBasicBlock &YamlMBB, |
| 103 | const PerFunctionMIParsingState &PFS); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 104 | |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame^] | 105 | bool |
| 106 | initializeRegisterInfo(const MachineFunction &MF, |
| 107 | MachineRegisterInfo &RegInfo, |
| 108 | const yaml::MachineFunction &YamlMF, |
| 109 | DenseMap<unsigned, unsigned> &VirtualRegisterSlots); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 110 | |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 111 | bool initializeFrameInfo(MachineFrameInfo &MFI, |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 112 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 113 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 114 | private: |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 115 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 116 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 117 | SMRange SourceRange); |
| 118 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 119 | /// Return a MIR diagnostic converted from an LLVM assembly diagnostic. |
| 120 | SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 121 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 122 | |
| 123 | /// Create an empty function with the given name. |
| 124 | void createDummyFunction(StringRef Name, Module &M); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 125 | |
| 126 | void initNames2RegClasses(const MachineFunction &MF); |
| 127 | |
| 128 | /// Check if the given identifier is a name of a register class. |
| 129 | /// |
| 130 | /// Return null if the name isn't a register class. |
| 131 | const TargetRegisterClass *getRegClass(const MachineFunction &MF, |
| 132 | StringRef Name); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 135 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 136 | |
| 137 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 138 | StringRef Filename, LLVMContext &Context) |
| 139 | : SM(), Filename(Filename), Context(Context) { |
| 140 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 141 | } |
| 142 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 143 | bool MIRParserImpl::error(const Twine &Message) { |
| 144 | Context.diagnose(DiagnosticInfoMIRParser( |
| 145 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 146 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 147 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 148 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 149 | bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { |
| 150 | Context.diagnose(DiagnosticInfoMIRParser( |
| 151 | DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message))); |
| 152 | return true; |
| 153 | } |
| 154 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 155 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 156 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); |
| 157 | reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); |
| 158 | return true; |
| 159 | } |
| 160 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 161 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 162 | DiagnosticSeverity Kind; |
| 163 | switch (Diag.getKind()) { |
| 164 | case SourceMgr::DK_Error: |
| 165 | Kind = DS_Error; |
| 166 | break; |
| 167 | case SourceMgr::DK_Warning: |
| 168 | Kind = DS_Warning; |
| 169 | break; |
| 170 | case SourceMgr::DK_Note: |
| 171 | Kind = DS_Note; |
| 172 | break; |
| 173 | } |
| 174 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 175 | } |
| 176 | |
| 177 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 178 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 179 | } |
| 180 | |
| 181 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 182 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 183 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 184 | In.setContext(&In); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 185 | |
| 186 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 187 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 188 | return nullptr; |
| 189 | // Create an empty module when the MIR file is empty. |
| 190 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 193 | std::unique_ptr<Module> M; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 194 | bool NoLLVMIR = false; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 195 | // Parse the block scalar manually so that we can return unique pointer |
| 196 | // without having to go trough YAML traits. |
| 197 | if (const auto *BSN = |
| 198 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 199 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 200 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 201 | Context, &IRSlots); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 202 | if (!M) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 203 | reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange())); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 204 | return M; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 205 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 206 | In.nextDocument(); |
| 207 | if (!In.setCurrentDocument()) |
| 208 | return M; |
| 209 | } else { |
| 210 | // Create an new, empty module. |
| 211 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 212 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Parse the machine functions. |
| 216 | do { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 217 | if (parseMachineFunction(In, *M, NoLLVMIR)) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 218 | return nullptr; |
| 219 | In.nextDocument(); |
| 220 | } while (In.setCurrentDocument()); |
| 221 | |
| 222 | return M; |
| 223 | } |
| 224 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 225 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, |
| 226 | bool NoLLVMIR) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 227 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 228 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 229 | if (In.error()) |
| 230 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 231 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame] | 232 | if (Functions.find(FunctionName) != Functions.end()) |
| 233 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 234 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 235 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 236 | if (NoLLVMIR) |
| 237 | createDummyFunction(FunctionName, M); |
Alex Lorenz | 5ef16b8 | 2015-06-16 17:06:29 +0000 | [diff] [blame] | 238 | else if (!M.getFunction(FunctionName)) |
| 239 | return error(Twine("function '") + FunctionName + |
| 240 | "' isn't defined in the provided LLVM IR"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 244 | void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 245 | auto &Context = M.getContext(); |
| 246 | Function *F = cast<Function>(M.getOrInsertFunction( |
| 247 | Name, FunctionType::get(Type::getVoidTy(Context), false))); |
| 248 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 249 | new UnreachableInst(Context, BB); |
| 250 | } |
| 251 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 252 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 253 | auto It = Functions.find(MF.getName()); |
| 254 | if (It == Functions.end()) |
| 255 | return error(Twine("no machine function information for function '") + |
| 256 | MF.getName() + "' in the MIR file"); |
| 257 | // TODO: Recreate the machine function. |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 258 | const yaml::MachineFunction &YamlMF = *It->getValue(); |
| 259 | if (YamlMF.Alignment) |
| 260 | MF.setAlignment(YamlMF.Alignment); |
| 261 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 262 | MF.setHasInlineAsm(YamlMF.HasInlineAsm); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame^] | 263 | PerFunctionMIParsingState PFS; |
| 264 | if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF, |
| 265 | PFS.VirtualRegisterSlots)) |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 266 | return true; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 267 | if (initializeFrameInfo(*MF.getFrameInfo(), YamlMF)) |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 268 | return true; |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 269 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 270 | const auto &F = *MF.getFunction(); |
| 271 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 272 | const BasicBlock *BB = nullptr; |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 273 | const yaml::StringValue &Name = YamlMBB.Name; |
| 274 | if (!Name.Value.empty()) { |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 275 | BB = dyn_cast_or_null<BasicBlock>( |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 276 | F.getValueSymbolTable().lookup(Name.Value)); |
Alex Lorenz | 00302df | 2015-06-19 20:12:03 +0000 | [diff] [blame] | 277 | if (!BB) |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 278 | return error(Name.SourceRange.Start, |
| 279 | Twine("basic block '") + Name.Value + |
| 280 | "' is not defined in the function '" + MF.getName() + |
| 281 | "'"); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 282 | } |
| 283 | auto *MBB = MF.CreateMachineBasicBlock(BB); |
| 284 | MF.insert(MF.end(), MBB); |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 285 | bool WasInserted = |
| 286 | PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 287 | if (!WasInserted) |
| 288 | return error(Twine("redefinition of machine basic block with id #") + |
| 289 | Twine(YamlMBB.ID)); |
| 290 | } |
| 291 | |
Alex Lorenz | c8704b0 | 2015-07-09 21:21:33 +0000 | [diff] [blame] | 292 | if (YamlMF.BasicBlocks.empty()) |
| 293 | return error(Twine("machine function '") + Twine(MF.getName()) + |
| 294 | "' requires at least one machine basic block in its body"); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 295 | // Initialize the machine basic blocks after creating them all so that the |
| 296 | // machine instructions parser can resolve the MBB references. |
| 297 | unsigned I = 0; |
| 298 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 299 | if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 300 | PFS)) |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool MIRParserImpl::initializeMachineBasicBlock( |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 307 | MachineFunction &MF, MachineBasicBlock &MBB, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 308 | const yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 309 | const PerFunctionMIParsingState &PFS) { |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 310 | MBB.setAlignment(YamlMBB.Alignment); |
| 311 | if (YamlMBB.AddressTaken) |
| 312 | MBB.setHasAddressTaken(); |
| 313 | MBB.setIsLandingPad(YamlMBB.IsLandingPad); |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 314 | SMDiagnostic Error; |
| 315 | // Parse the successors. |
| 316 | for (const auto &MBBSource : YamlMBB.Successors) { |
| 317 | MachineBasicBlock *SuccMBB = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 318 | if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots, |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 319 | Error)) |
| 320 | return error(Error, MBBSource.SourceRange); |
| 321 | // TODO: Report an error when adding the same successor more than once. |
| 322 | MBB.addSuccessor(SuccMBB); |
| 323 | } |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 324 | // Parse the instructions. |
| 325 | for (const auto &MISource : YamlMBB.Instructions) { |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 326 | MachineInstr *MI = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 327 | if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error)) |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 328 | return error(Error, MISource.SourceRange); |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 329 | MBB.insert(MBB.end(), MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 330 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 331 | return false; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 334 | bool MIRParserImpl::initializeRegisterInfo( |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 335 | const MachineFunction &MF, MachineRegisterInfo &RegInfo, |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame^] | 336 | const yaml::MachineFunction &YamlMF, |
| 337 | DenseMap<unsigned, unsigned> &VirtualRegisterSlots) { |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 338 | assert(RegInfo.isSSA()); |
| 339 | if (!YamlMF.IsSSA) |
| 340 | RegInfo.leaveSSA(); |
| 341 | assert(RegInfo.tracksLiveness()); |
| 342 | if (!YamlMF.TracksRegLiveness) |
| 343 | RegInfo.invalidateLiveness(); |
| 344 | RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 345 | |
| 346 | // Parse the virtual register information. |
| 347 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
| 348 | const auto *RC = getRegClass(MF, VReg.Class.Value); |
| 349 | if (!RC) |
| 350 | return error(VReg.Class.SourceRange.Start, |
| 351 | Twine("use of undefined register class '") + |
| 352 | VReg.Class.Value + "'"); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame^] | 353 | unsigned Reg = RegInfo.createVirtualRegister(RC); |
| 354 | // TODO: Report an error when the same virtual register with the same ID is |
| 355 | // redefined. |
| 356 | VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg)); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 357 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 358 | return false; |
| 359 | } |
| 360 | |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 361 | bool MIRParserImpl::initializeFrameInfo(MachineFrameInfo &MFI, |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 362 | const yaml::MachineFunction &YamlMF) { |
| 363 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 364 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 365 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 366 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 367 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 368 | MFI.setStackSize(YamlMFI.StackSize); |
| 369 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 370 | if (YamlMFI.MaxAlignment) |
| 371 | MFI.ensureMaxAlignment(YamlMFI.MaxAlignment); |
| 372 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 373 | MFI.setHasCalls(YamlMFI.HasCalls); |
| 374 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
| 375 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 376 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 377 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 378 | |
| 379 | // Initialize the frame objects. |
| 380 | for (const auto &Object : YamlMF.StackObjects) { |
| 381 | int ObjectIdx = MFI.CreateStackObject( |
| 382 | Object.Size, Object.Alignment, |
| 383 | Object.Type == yaml::MachineStackObject::SpillSlot); |
| 384 | MFI.setObjectOffset(ObjectIdx, Object.Offset); |
| 385 | // TODO: Store the mapping between object IDs and object indices to parse |
| 386 | // stack object references correctly. |
| 387 | } |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 391 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 392 | SMRange SourceRange) { |
| 393 | assert(SourceRange.isValid() && "Invalid source range"); |
| 394 | SMLoc Loc = SourceRange.Start; |
| 395 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 396 | *Loc.getPointer() == '\''; |
| 397 | // Translate the location of the error from the location in the MI string to |
| 398 | // the corresponding location in the MIR file. |
| 399 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 400 | (HasQuote ? 1 : 0)); |
| 401 | |
| 402 | // TODO: Translate any source ranges as well. |
| 403 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 404 | Error.getFixIts()); |
| 405 | } |
| 406 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 407 | SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 408 | SMRange SourceRange) { |
| 409 | assert(SourceRange.isValid()); |
| 410 | |
| 411 | // Translate the location of the error from the location in the llvm IR string |
| 412 | // to the corresponding location in the MIR file. |
| 413 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 414 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 415 | unsigned Column = Error.getColumnNo(); |
| 416 | StringRef LineStr = Error.getLineContents(); |
| 417 | SMLoc Loc = Error.getLoc(); |
| 418 | |
| 419 | // Get the full line and adjust the column number by taking the indentation of |
| 420 | // LLVM IR into account. |
| 421 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 422 | L != E; ++L) { |
| 423 | if (L.line_number() == Line) { |
| 424 | LineStr = *L; |
| 425 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 426 | auto Indent = LineStr.find(Error.getLineContents()); |
| 427 | if (Indent != StringRef::npos) |
| 428 | Column += Indent; |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 434 | Error.getMessage(), LineStr, Error.getRanges(), |
| 435 | Error.getFixIts()); |
| 436 | } |
| 437 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 438 | void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) { |
| 439 | if (!Names2RegClasses.empty()) |
| 440 | return; |
| 441 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 442 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 443 | const auto *RC = TRI->getRegClass(I); |
| 444 | Names2RegClasses.insert( |
| 445 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF, |
| 450 | StringRef Name) { |
| 451 | initNames2RegClasses(MF); |
| 452 | auto RegClassInfo = Names2RegClasses.find(Name); |
| 453 | if (RegClassInfo == Names2RegClasses.end()) |
| 454 | return nullptr; |
| 455 | return RegClassInfo->getValue(); |
| 456 | } |
| 457 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 458 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 459 | : Impl(std::move(Impl)) {} |
| 460 | |
| 461 | MIRParser::~MIRParser() {} |
| 462 | |
| 463 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 464 | |
| 465 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 466 | return Impl->initializeMachineFunction(MF); |
| 467 | } |
| 468 | |
| 469 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 470 | SMDiagnostic &Error, |
| 471 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 472 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 473 | if (std::error_code EC = FileOrErr.getError()) { |
| 474 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 475 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 476 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 477 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 478 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 481 | std::unique_ptr<MIRParser> |
| 482 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 483 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 484 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 485 | return llvm::make_unique<MIRParser>( |
| 486 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 487 | } |