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 | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 111 | bool initializeFrameInfo(const Function &F, 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 | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 114 | bool initializeJumpTableInfo(MachineFunction &MF, |
| 115 | const yaml::MachineJumpTable &YamlJTI, |
| 116 | const PerFunctionMIParsingState &PFS); |
| 117 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 118 | private: |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 119 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 120 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 121 | SMRange SourceRange); |
| 122 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 123 | /// Return a MIR diagnostic converted from an LLVM assembly diagnostic. |
| 124 | SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 125 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 126 | |
| 127 | /// Create an empty function with the given name. |
| 128 | void createDummyFunction(StringRef Name, Module &M); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 129 | |
| 130 | void initNames2RegClasses(const MachineFunction &MF); |
| 131 | |
| 132 | /// Check if the given identifier is a name of a register class. |
| 133 | /// |
| 134 | /// Return null if the name isn't a register class. |
| 135 | const TargetRegisterClass *getRegClass(const MachineFunction &MF, |
| 136 | StringRef Name); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 139 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 140 | |
| 141 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 142 | StringRef Filename, LLVMContext &Context) |
| 143 | : SM(), Filename(Filename), Context(Context) { |
| 144 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 145 | } |
| 146 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 147 | bool MIRParserImpl::error(const Twine &Message) { |
| 148 | Context.diagnose(DiagnosticInfoMIRParser( |
| 149 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 150 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 151 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 152 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 153 | bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { |
| 154 | Context.diagnose(DiagnosticInfoMIRParser( |
| 155 | DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message))); |
| 156 | return true; |
| 157 | } |
| 158 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 159 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 160 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); |
| 161 | reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); |
| 162 | return true; |
| 163 | } |
| 164 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 165 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 166 | DiagnosticSeverity Kind; |
| 167 | switch (Diag.getKind()) { |
| 168 | case SourceMgr::DK_Error: |
| 169 | Kind = DS_Error; |
| 170 | break; |
| 171 | case SourceMgr::DK_Warning: |
| 172 | Kind = DS_Warning; |
| 173 | break; |
| 174 | case SourceMgr::DK_Note: |
| 175 | Kind = DS_Note; |
| 176 | break; |
| 177 | } |
| 178 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 179 | } |
| 180 | |
| 181 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 182 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 183 | } |
| 184 | |
| 185 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 186 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 187 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 188 | In.setContext(&In); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 189 | |
| 190 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 191 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 192 | return nullptr; |
| 193 | // Create an empty module when the MIR file is empty. |
| 194 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 197 | std::unique_ptr<Module> M; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 198 | bool NoLLVMIR = false; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 199 | // Parse the block scalar manually so that we can return unique pointer |
| 200 | // without having to go trough YAML traits. |
| 201 | if (const auto *BSN = |
| 202 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 203 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 204 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 205 | Context, &IRSlots); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 206 | if (!M) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 207 | reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange())); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 208 | return M; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 209 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 210 | In.nextDocument(); |
| 211 | if (!In.setCurrentDocument()) |
| 212 | return M; |
| 213 | } else { |
| 214 | // Create an new, empty module. |
| 215 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 216 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Parse the machine functions. |
| 220 | do { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 221 | if (parseMachineFunction(In, *M, NoLLVMIR)) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 222 | return nullptr; |
| 223 | In.nextDocument(); |
| 224 | } while (In.setCurrentDocument()); |
| 225 | |
| 226 | return M; |
| 227 | } |
| 228 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 229 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, |
| 230 | bool NoLLVMIR) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 231 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 232 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 233 | if (In.error()) |
| 234 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 235 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame] | 236 | if (Functions.find(FunctionName) != Functions.end()) |
| 237 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 238 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 239 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 240 | if (NoLLVMIR) |
| 241 | createDummyFunction(FunctionName, M); |
Alex Lorenz | 5ef16b8 | 2015-06-16 17:06:29 +0000 | [diff] [blame] | 242 | else if (!M.getFunction(FunctionName)) |
| 243 | return error(Twine("function '") + FunctionName + |
| 244 | "' isn't defined in the provided LLVM IR"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 248 | void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 249 | auto &Context = M.getContext(); |
| 250 | Function *F = cast<Function>(M.getOrInsertFunction( |
| 251 | Name, FunctionType::get(Type::getVoidTy(Context), false))); |
| 252 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 253 | new UnreachableInst(Context, BB); |
| 254 | } |
| 255 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 256 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 257 | auto It = Functions.find(MF.getName()); |
| 258 | if (It == Functions.end()) |
| 259 | return error(Twine("no machine function information for function '") + |
| 260 | MF.getName() + "' in the MIR file"); |
| 261 | // TODO: Recreate the machine function. |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 262 | const yaml::MachineFunction &YamlMF = *It->getValue(); |
| 263 | if (YamlMF.Alignment) |
| 264 | MF.setAlignment(YamlMF.Alignment); |
| 265 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 266 | MF.setHasInlineAsm(YamlMF.HasInlineAsm); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 267 | PerFunctionMIParsingState PFS; |
| 268 | if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF, |
| 269 | PFS.VirtualRegisterSlots)) |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 270 | return true; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 271 | if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF)) |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 272 | return true; |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 273 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 274 | const auto &F = *MF.getFunction(); |
| 275 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 276 | const BasicBlock *BB = nullptr; |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 277 | const yaml::StringValue &Name = YamlMBB.Name; |
| 278 | if (!Name.Value.empty()) { |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 279 | BB = dyn_cast_or_null<BasicBlock>( |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 280 | F.getValueSymbolTable().lookup(Name.Value)); |
Alex Lorenz | 00302df | 2015-06-19 20:12:03 +0000 | [diff] [blame] | 281 | if (!BB) |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 282 | return error(Name.SourceRange.Start, |
| 283 | Twine("basic block '") + Name.Value + |
| 284 | "' is not defined in the function '" + MF.getName() + |
| 285 | "'"); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 286 | } |
| 287 | auto *MBB = MF.CreateMachineBasicBlock(BB); |
| 288 | MF.insert(MF.end(), MBB); |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 289 | bool WasInserted = |
| 290 | PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 291 | if (!WasInserted) |
| 292 | return error(Twine("redefinition of machine basic block with id #") + |
| 293 | Twine(YamlMBB.ID)); |
| 294 | } |
| 295 | |
Alex Lorenz | c8704b0 | 2015-07-09 21:21:33 +0000 | [diff] [blame] | 296 | if (YamlMF.BasicBlocks.empty()) |
| 297 | return error(Twine("machine function '") + Twine(MF.getName()) + |
| 298 | "' requires at least one machine basic block in its body"); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 299 | // Initialize the jump table after creating all the MBBs so that the MBB |
| 300 | // references can be resolved. |
| 301 | if (!YamlMF.JumpTableInfo.Entries.empty() && |
| 302 | initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS)) |
| 303 | return true; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 304 | // Initialize the machine basic blocks after creating them all so that the |
| 305 | // machine instructions parser can resolve the MBB references. |
| 306 | unsigned I = 0; |
| 307 | for (const auto &YamlMBB : YamlMF.BasicBlocks) { |
| 308 | if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 309 | PFS)) |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | bool MIRParserImpl::initializeMachineBasicBlock( |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 316 | MachineFunction &MF, MachineBasicBlock &MBB, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 317 | const yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 318 | const PerFunctionMIParsingState &PFS) { |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 319 | MBB.setAlignment(YamlMBB.Alignment); |
| 320 | if (YamlMBB.AddressTaken) |
| 321 | MBB.setHasAddressTaken(); |
| 322 | MBB.setIsLandingPad(YamlMBB.IsLandingPad); |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 323 | SMDiagnostic Error; |
| 324 | // Parse the successors. |
| 325 | for (const auto &MBBSource : YamlMBB.Successors) { |
| 326 | MachineBasicBlock *SuccMBB = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 327 | if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots, |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 328 | Error)) |
| 329 | return error(Error, MBBSource.SourceRange); |
| 330 | // TODO: Report an error when adding the same successor more than once. |
| 331 | MBB.addSuccessor(SuccMBB); |
| 332 | } |
Alex Lorenz | 9fab370 | 2015-07-14 21:24:41 +0000 | [diff] [blame] | 333 | // Parse the liveins. |
| 334 | for (const auto &LiveInSource : YamlMBB.LiveIns) { |
| 335 | unsigned Reg = 0; |
| 336 | if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS, |
| 337 | IRSlots, Error)) |
| 338 | return error(Error, LiveInSource.SourceRange); |
| 339 | MBB.addLiveIn(Reg); |
| 340 | } |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 341 | // Parse the instructions. |
| 342 | for (const auto &MISource : YamlMBB.Instructions) { |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 343 | MachineInstr *MI = nullptr; |
Alex Lorenz | 7a503fa | 2015-07-07 17:46:43 +0000 | [diff] [blame] | 344 | if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error)) |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 345 | return error(Error, MISource.SourceRange); |
Alex Lorenz | 3708a64 | 2015-06-30 17:47:50 +0000 | [diff] [blame] | 346 | MBB.insert(MBB.end(), MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 347 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 348 | return false; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 351 | bool MIRParserImpl::initializeRegisterInfo( |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 352 | const MachineFunction &MF, MachineRegisterInfo &RegInfo, |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 353 | const yaml::MachineFunction &YamlMF, |
| 354 | DenseMap<unsigned, unsigned> &VirtualRegisterSlots) { |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 355 | assert(RegInfo.isSSA()); |
| 356 | if (!YamlMF.IsSSA) |
| 357 | RegInfo.leaveSSA(); |
| 358 | assert(RegInfo.tracksLiveness()); |
| 359 | if (!YamlMF.TracksRegLiveness) |
| 360 | RegInfo.invalidateLiveness(); |
| 361 | RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 362 | |
| 363 | // Parse the virtual register information. |
| 364 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
| 365 | const auto *RC = getRegClass(MF, VReg.Class.Value); |
| 366 | if (!RC) |
| 367 | return error(VReg.Class.SourceRange.Start, |
| 368 | Twine("use of undefined register class '") + |
| 369 | VReg.Class.Value + "'"); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 370 | unsigned Reg = RegInfo.createVirtualRegister(RC); |
| 371 | // TODO: Report an error when the same virtual register with the same ID is |
| 372 | // redefined. |
| 373 | VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg)); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 374 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 378 | bool MIRParserImpl::initializeFrameInfo(const Function &F, |
| 379 | MachineFrameInfo &MFI, |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 380 | const yaml::MachineFunction &YamlMF) { |
| 381 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 382 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 383 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 384 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 385 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 386 | MFI.setStackSize(YamlMFI.StackSize); |
| 387 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 388 | if (YamlMFI.MaxAlignment) |
| 389 | MFI.ensureMaxAlignment(YamlMFI.MaxAlignment); |
| 390 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 391 | MFI.setHasCalls(YamlMFI.HasCalls); |
| 392 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
| 393 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 394 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 395 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 396 | |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 397 | // Initialize the fixed frame objects. |
| 398 | for (const auto &Object : YamlMF.FixedStackObjects) { |
| 399 | int ObjectIdx; |
| 400 | if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) |
| 401 | ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset, |
| 402 | Object.IsImmutable, Object.IsAliased); |
| 403 | else |
| 404 | ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset); |
| 405 | MFI.setObjectAlignment(ObjectIdx, Object.Alignment); |
| 406 | // TODO: Store the mapping between fixed object IDs and object indices to |
| 407 | // parse fixed stack object references correctly. |
| 408 | } |
| 409 | |
| 410 | // Initialize the ordinary frame objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 411 | for (const auto &Object : YamlMF.StackObjects) { |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 412 | int ObjectIdx; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 413 | const AllocaInst *Alloca = nullptr; |
| 414 | const yaml::StringValue &Name = Object.Name; |
| 415 | if (!Name.Value.empty()) { |
| 416 | Alloca = dyn_cast_or_null<AllocaInst>( |
| 417 | F.getValueSymbolTable().lookup(Name.Value)); |
| 418 | if (!Alloca) |
| 419 | return error(Name.SourceRange.Start, |
| 420 | "alloca instruction named '" + Name.Value + |
| 421 | "' isn't defined in the function '" + F.getName() + |
| 422 | "'"); |
| 423 | } |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 424 | if (Object.Type == yaml::MachineStackObject::VariableSized) |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 425 | ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca); |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 426 | else |
| 427 | ObjectIdx = MFI.CreateStackObject( |
| 428 | Object.Size, Object.Alignment, |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 429 | Object.Type == yaml::MachineStackObject::SpillSlot, Alloca); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 430 | MFI.setObjectOffset(ObjectIdx, Object.Offset); |
| 431 | // TODO: Store the mapping between object IDs and object indices to parse |
| 432 | // stack object references correctly. |
| 433 | } |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 437 | bool MIRParserImpl::initializeJumpTableInfo( |
| 438 | MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI, |
| 439 | const PerFunctionMIParsingState &PFS) { |
| 440 | MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind); |
| 441 | SMDiagnostic Error; |
| 442 | for (const auto &Entry : YamlJTI.Entries) { |
| 443 | std::vector<MachineBasicBlock *> Blocks; |
| 444 | for (const auto &MBBSource : Entry.Blocks) { |
| 445 | MachineBasicBlock *MBB = nullptr; |
| 446 | if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error)) |
| 447 | return error(Error, MBBSource.SourceRange); |
| 448 | Blocks.push_back(MBB); |
| 449 | } |
| 450 | JTI->createJumpTableIndex(Blocks); |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 455 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 456 | SMRange SourceRange) { |
| 457 | assert(SourceRange.isValid() && "Invalid source range"); |
| 458 | SMLoc Loc = SourceRange.Start; |
| 459 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 460 | *Loc.getPointer() == '\''; |
| 461 | // Translate the location of the error from the location in the MI string to |
| 462 | // the corresponding location in the MIR file. |
| 463 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 464 | (HasQuote ? 1 : 0)); |
| 465 | |
| 466 | // TODO: Translate any source ranges as well. |
| 467 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 468 | Error.getFixIts()); |
| 469 | } |
| 470 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 471 | SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error, |
| 472 | SMRange SourceRange) { |
| 473 | assert(SourceRange.isValid()); |
| 474 | |
| 475 | // Translate the location of the error from the location in the llvm IR string |
| 476 | // to the corresponding location in the MIR file. |
| 477 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 478 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 479 | unsigned Column = Error.getColumnNo(); |
| 480 | StringRef LineStr = Error.getLineContents(); |
| 481 | SMLoc Loc = Error.getLoc(); |
| 482 | |
| 483 | // Get the full line and adjust the column number by taking the indentation of |
| 484 | // LLVM IR into account. |
| 485 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 486 | L != E; ++L) { |
| 487 | if (L.line_number() == Line) { |
| 488 | LineStr = *L; |
| 489 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 490 | auto Indent = LineStr.find(Error.getLineContents()); |
| 491 | if (Indent != StringRef::npos) |
| 492 | Column += Indent; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 498 | Error.getMessage(), LineStr, Error.getRanges(), |
| 499 | Error.getFixIts()); |
| 500 | } |
| 501 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 502 | void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) { |
| 503 | if (!Names2RegClasses.empty()) |
| 504 | return; |
| 505 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 506 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 507 | const auto *RC = TRI->getRegClass(I); |
| 508 | Names2RegClasses.insert( |
| 509 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF, |
| 514 | StringRef Name) { |
| 515 | initNames2RegClasses(MF); |
| 516 | auto RegClassInfo = Names2RegClasses.find(Name); |
| 517 | if (RegClassInfo == Names2RegClasses.end()) |
| 518 | return nullptr; |
| 519 | return RegClassInfo->getValue(); |
| 520 | } |
| 521 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 522 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 523 | : Impl(std::move(Impl)) {} |
| 524 | |
| 525 | MIRParser::~MIRParser() {} |
| 526 | |
| 527 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 528 | |
| 529 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 530 | return Impl->initializeMachineFunction(MF); |
| 531 | } |
| 532 | |
| 533 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 534 | SMDiagnostic &Error, |
| 535 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 536 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 537 | if (std::error_code EC = FileOrErr.getError()) { |
| 538 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 539 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 540 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 541 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 542 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 545 | std::unique_ptr<MIRParser> |
| 546 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 547 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 548 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 549 | return llvm::make_unique<MIRParser>( |
| 550 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 551 | } |