blob: c6c0e4516f117c05d6231306afeca1b1083d5800 [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions 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 parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000026#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35
36using namespace llvm;
37
38namespace {
39
Alex Lorenz36962cd2015-07-07 02:08:46 +000040/// A wrapper struct around the 'MachineOperand' struct that includes a source
41/// range.
42struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
46
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
50};
51
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000052class MIParser {
53 SourceMgr &SM;
54 MachineFunction &MF;
55 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000056 StringRef Source, CurrentSource;
57 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000058 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000059 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000063 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000065 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000067 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000069 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzef5c1962015-07-28 23:02:45 +000071 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000073 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075
76public:
77 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000078 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000079 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
Alex Lorenz91370c52015-06-22 20:37:46 +000081 void lex();
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 /// Report an error at the current location with the given message.
84 ///
85 /// This function always return true.
86 bool error(const Twine &Msg);
87
Alex Lorenz91370c52015-06-22 20:37:46 +000088 /// Report an error at the given location with the given message.
89 ///
90 /// This function always return true.
91 bool error(StringRef::iterator Loc, const Twine &Msg);
92
Alex Lorenz5022f6b2015-08-13 23:10:16 +000093 bool
94 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
95 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +000096 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +000097 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
98 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +000099 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000100
101 bool
102 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
103 bool parseBasicBlock(MachineBasicBlock &MBB);
104 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
105 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000107 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000108 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000109 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000110 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000111 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000112 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000113 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000114 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000115 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000116 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000117 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000118 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000119 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000120 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000121 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000122 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000123 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000124 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000125 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000126 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000127 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000128 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000129 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000130 bool parseIRBlock(BasicBlock *&BB, const Function &F);
131 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000132 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000133 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000134 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000135 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000136 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000137 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000138 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000139 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000140 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000141 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
142 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000143 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000144
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000145private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000146 /// Convert the integer literal in the current token into an unsigned integer.
147 ///
148 /// Return true if an error occurred.
149 bool getUnsigned(unsigned &Result);
150
Alex Lorenz4af7e612015-08-03 23:08:19 +0000151 /// Convert the integer literal in the current token into an uint64.
152 ///
153 /// Return true if an error occurred.
154 bool getUint64(uint64_t &Result);
155
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000156 /// If the current token is of the given kind, consume it and return false.
157 /// Otherwise report an error and return true.
158 bool expectAndConsume(MIToken::TokenKind TokenKind);
159
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000160 /// If the current token is of the given kind, consume it and return true.
161 /// Otherwise return false.
162 bool consumeIfPresent(MIToken::TokenKind TokenKind);
163
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000164 void initNames2InstrOpCodes();
165
166 /// Try to convert an instruction name to an opcode. Return true if the
167 /// instruction name is invalid.
168 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000169
Alex Lorenze5a44662015-07-17 00:24:15 +0000170 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171
Alex Lorenz36962cd2015-07-07 02:08:46 +0000172 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
173 const MCInstrDesc &MCID);
174
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000175 void initNames2Regs();
176
177 /// Try to convert a register name to a register number. Return true if the
178 /// register name is invalid.
179 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000180
181 void initNames2RegMasks();
182
183 /// Check if the given identifier is a name of a register mask.
184 ///
185 /// Return null if the identifier isn't a register mask.
186 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000187
188 void initNames2SubRegIndices();
189
190 /// Check if the given identifier is a name of a subregister index.
191 ///
192 /// Return 0 if the name isn't a subregister index class.
193 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000194
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000195 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000196 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000197
198 void initNames2TargetIndices();
199
200 /// Try to convert a name of target index to the corresponding target index.
201 ///
202 /// Return true if the name isn't a name of a target index.
203 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000204
205 void initNames2DirectTargetFlags();
206
207 /// Try to convert a name of a direct target flag to the corresponding
208 /// target flag.
209 ///
210 /// Return true if the name isn't a name of a direct flag.
211 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000212};
213
214} // end anonymous namespace
215
216MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000217 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000218 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000219 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000220 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000221
Alex Lorenz91370c52015-06-22 20:37:46 +0000222void MIParser::lex() {
223 CurrentSource = lexMIToken(
224 CurrentSource, Token,
225 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
226}
227
228bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
229
230bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000231 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000232 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
233 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
234 // Create an ordinary diagnostic when the source manager's buffer is the
235 // source string.
236 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
237 return true;
238 }
239 // Create a diagnostic for a YAML string literal.
240 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
241 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
242 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000243 return true;
244}
245
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000246static const char *toString(MIToken::TokenKind TokenKind) {
247 switch (TokenKind) {
248 case MIToken::comma:
249 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000250 case MIToken::equal:
251 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000252 case MIToken::colon:
253 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000254 case MIToken::lparen:
255 return "'('";
256 case MIToken::rparen:
257 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000258 default:
259 return "<unknown token>";
260 }
261}
262
263bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
264 if (Token.isNot(TokenKind))
265 return error(Twine("expected ") + toString(TokenKind));
266 lex();
267 return false;
268}
269
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000270bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
271 if (Token.isNot(TokenKind))
272 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000273 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000274 return true;
275}
Alex Lorenz91370c52015-06-22 20:37:46 +0000276
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000277bool MIParser::parseBasicBlockDefinition(
278 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
279 assert(Token.is(MIToken::MachineBasicBlockLabel));
280 unsigned ID = 0;
281 if (getUnsigned(ID))
282 return true;
283 auto Loc = Token.location();
284 auto Name = Token.stringValue();
285 lex();
286 bool HasAddressTaken = false;
287 bool IsLandingPad = false;
288 unsigned Alignment = 0;
289 BasicBlock *BB = nullptr;
290 if (consumeIfPresent(MIToken::lparen)) {
291 do {
292 // TODO: Report an error when multiple same attributes are specified.
293 switch (Token.kind()) {
294 case MIToken::kw_address_taken:
295 HasAddressTaken = true;
296 lex();
297 break;
298 case MIToken::kw_landing_pad:
299 IsLandingPad = true;
300 lex();
301 break;
302 case MIToken::kw_align:
303 if (parseAlignment(Alignment))
304 return true;
305 break;
306 case MIToken::IRBlock:
307 // TODO: Report an error when both name and ir block are specified.
308 if (parseIRBlock(BB, *MF.getFunction()))
309 return true;
310 lex();
311 break;
312 default:
313 break;
314 }
315 } while (consumeIfPresent(MIToken::comma));
316 if (expectAndConsume(MIToken::rparen))
317 return true;
318 }
319 if (expectAndConsume(MIToken::colon))
320 return true;
321
322 if (!Name.empty()) {
323 BB = dyn_cast_or_null<BasicBlock>(
324 MF.getFunction()->getValueSymbolTable().lookup(Name));
325 if (!BB)
326 return error(Loc, Twine("basic block '") + Name +
327 "' is not defined in the function '" +
328 MF.getName() + "'");
329 }
330 auto *MBB = MF.CreateMachineBasicBlock(BB);
331 MF.insert(MF.end(), MBB);
332 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
333 if (!WasInserted)
334 return error(Loc, Twine("redefinition of machine basic block with id #") +
335 Twine(ID));
336 if (Alignment)
337 MBB->setAlignment(Alignment);
338 if (HasAddressTaken)
339 MBB->setHasAddressTaken();
340 MBB->setIsLandingPad(IsLandingPad);
341 return false;
342}
343
344bool MIParser::parseBasicBlockDefinitions(
345 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
346 lex();
347 // Skip until the first machine basic block.
348 while (Token.is(MIToken::Newline))
349 lex();
350 if (Token.isErrorOrEOF())
351 return Token.isError();
352 if (Token.isNot(MIToken::MachineBasicBlockLabel))
353 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000354 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000355 do {
356 if (parseBasicBlockDefinition(MBBSlots))
357 return true;
358 bool IsAfterNewline = false;
359 // Skip until the next machine basic block.
360 while (true) {
361 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
362 Token.isErrorOrEOF())
363 break;
364 else if (Token.is(MIToken::MachineBasicBlockLabel))
365 return error("basic block definition should be located at the start of "
366 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000367 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000368 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000369 continue;
370 }
371 IsAfterNewline = false;
372 if (Token.is(MIToken::lbrace))
373 ++BraceDepth;
374 if (Token.is(MIToken::rbrace)) {
375 if (!BraceDepth)
376 return error("extraneous closing brace ('}')");
377 --BraceDepth;
378 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000379 lex();
380 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000381 // Verify that we closed all of the '{' at the end of a file or a block.
382 if (!Token.isError() && BraceDepth)
383 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000384 } while (!Token.isErrorOrEOF());
385 return Token.isError();
386}
387
388bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
389 assert(Token.is(MIToken::kw_liveins));
390 lex();
391 if (expectAndConsume(MIToken::colon))
392 return true;
393 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
394 return false;
395 do {
396 if (Token.isNot(MIToken::NamedRegister))
397 return error("expected a named register");
398 unsigned Reg = 0;
399 if (parseRegister(Reg))
400 return true;
401 MBB.addLiveIn(Reg);
402 lex();
403 } while (consumeIfPresent(MIToken::comma));
404 return false;
405}
406
407bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
408 assert(Token.is(MIToken::kw_successors));
409 lex();
410 if (expectAndConsume(MIToken::colon))
411 return true;
412 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
413 return false;
414 do {
415 if (Token.isNot(MIToken::MachineBasicBlock))
416 return error("expected a machine basic block reference");
417 MachineBasicBlock *SuccMBB = nullptr;
418 if (parseMBBReference(SuccMBB))
419 return true;
420 lex();
421 unsigned Weight = 0;
422 if (consumeIfPresent(MIToken::lparen)) {
423 if (Token.isNot(MIToken::IntegerLiteral))
424 return error("expected an integer literal after '('");
425 if (getUnsigned(Weight))
426 return true;
427 lex();
428 if (expectAndConsume(MIToken::rparen))
429 return true;
430 }
431 MBB.addSuccessor(SuccMBB, Weight);
432 } while (consumeIfPresent(MIToken::comma));
433 return false;
434}
435
436bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
437 // Skip the definition.
438 assert(Token.is(MIToken::MachineBasicBlockLabel));
439 lex();
440 if (consumeIfPresent(MIToken::lparen)) {
441 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
442 lex();
443 consumeIfPresent(MIToken::rparen);
444 }
445 consumeIfPresent(MIToken::colon);
446
447 // Parse the liveins and successors.
448 // N.B: Multiple lists of successors and liveins are allowed and they're
449 // merged into one.
450 // Example:
451 // liveins: %edi
452 // liveins: %esi
453 //
454 // is equivalent to
455 // liveins: %edi, %esi
456 while (true) {
457 if (Token.is(MIToken::kw_successors)) {
458 if (parseBasicBlockSuccessors(MBB))
459 return true;
460 } else if (Token.is(MIToken::kw_liveins)) {
461 if (parseBasicBlockLiveins(MBB))
462 return true;
463 } else if (consumeIfPresent(MIToken::Newline)) {
464 continue;
465 } else
466 break;
467 if (!Token.isNewlineOrEOF())
468 return error("expected line break at the end of a list");
469 lex();
470 }
471
472 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000473 bool IsInBundle = false;
474 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000475 while (true) {
476 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
477 return false;
478 else if (consumeIfPresent(MIToken::Newline))
479 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000480 if (consumeIfPresent(MIToken::rbrace)) {
481 // The first parsing pass should verify that all closing '}' have an
482 // opening '{'.
483 assert(IsInBundle);
484 IsInBundle = false;
485 continue;
486 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000487 MachineInstr *MI = nullptr;
488 if (parse(MI))
489 return true;
490 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000491 if (IsInBundle) {
492 PrevMI->setFlag(MachineInstr::BundledSucc);
493 MI->setFlag(MachineInstr::BundledPred);
494 }
495 PrevMI = MI;
496 if (Token.is(MIToken::lbrace)) {
497 if (IsInBundle)
498 return error("nested instruction bundles are not allowed");
499 lex();
500 // This instruction is the start of the bundle.
501 MI->setFlag(MachineInstr::BundledSucc);
502 IsInBundle = true;
503 if (!Token.is(MIToken::Newline))
504 // The next instruction can be on the same line.
505 continue;
506 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000507 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
508 lex();
509 }
510 return false;
511}
512
513bool MIParser::parseBasicBlocks() {
514 lex();
515 // Skip until the first machine basic block.
516 while (Token.is(MIToken::Newline))
517 lex();
518 if (Token.isErrorOrEOF())
519 return Token.isError();
520 // The first parsing pass should have verified that this token is a MBB label
521 // in the 'parseBasicBlockDefinitions' method.
522 assert(Token.is(MIToken::MachineBasicBlockLabel));
523 do {
524 MachineBasicBlock *MBB = nullptr;
525 if (parseMBBReference(MBB))
526 return true;
527 if (parseBasicBlock(*MBB))
528 return true;
529 // The method 'parseBasicBlock' should parse the whole block until the next
530 // block or the end of file.
531 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
532 } while (Token.isNot(MIToken::Eof));
533 return false;
534}
535
536bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000537 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000538 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000539 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000540 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000541 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000542 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000543 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000544 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000545 if (Token.isNot(MIToken::comma))
546 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000547 lex();
548 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000549 if (!Operands.empty() && expectAndConsume(MIToken::equal))
550 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000551
Alex Lorenze5a44662015-07-17 00:24:15 +0000552 unsigned OpCode, Flags = 0;
553 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000554 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000555
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000556 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000557 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000558 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000559 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000560 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000561 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000562 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000563 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
564 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000565 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000566 if (Token.isNot(MIToken::comma))
567 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000568 lex();
569 }
570
Alex Lorenz46d760d2015-07-22 21:15:11 +0000571 DebugLoc DebugLocation;
572 if (Token.is(MIToken::kw_debug_location)) {
573 lex();
574 if (Token.isNot(MIToken::exclaim))
575 return error("expected a metadata node after 'debug-location'");
576 MDNode *Node = nullptr;
577 if (parseMDNode(Node))
578 return true;
579 DebugLocation = DebugLoc(Node);
580 }
581
Alex Lorenz4af7e612015-08-03 23:08:19 +0000582 // Parse the machine memory operands.
583 SmallVector<MachineMemOperand *, 2> MemOperands;
584 if (Token.is(MIToken::coloncolon)) {
585 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000586 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000587 MachineMemOperand *MemOp = nullptr;
588 if (parseMachineMemoryOperand(MemOp))
589 return true;
590 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000591 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000592 break;
593 if (Token.isNot(MIToken::comma))
594 return error("expected ',' before the next machine memory operand");
595 lex();
596 }
597 }
598
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000599 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000600 if (!MCID.isVariadic()) {
601 // FIXME: Move the implicit operand verification to the machine verifier.
602 if (verifyImplicitOperands(Operands, MCID))
603 return true;
604 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000605
Alex Lorenzcb268d42015-07-06 23:07:26 +0000606 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000607 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000608 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000609 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000610 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000611 if (MemOperands.empty())
612 return false;
613 MachineInstr::mmo_iterator MemRefs =
614 MF.allocateMemRefsArray(MemOperands.size());
615 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
616 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000617 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000618}
619
Alex Lorenz1ea60892015-07-27 20:29:27 +0000620bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000621 lex();
622 if (Token.isNot(MIToken::MachineBasicBlock))
623 return error("expected a machine basic block reference");
624 if (parseMBBReference(MBB))
625 return true;
626 lex();
627 if (Token.isNot(MIToken::Eof))
628 return error(
629 "expected end of string after the machine basic block reference");
630 return false;
631}
632
Alex Lorenz1ea60892015-07-27 20:29:27 +0000633bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000634 lex();
635 if (Token.isNot(MIToken::NamedRegister))
636 return error("expected a named register");
637 if (parseRegister(Reg))
638 return 0;
639 lex();
640 if (Token.isNot(MIToken::Eof))
641 return error("expected end of string after the register reference");
642 return false;
643}
644
Alex Lorenz12045a42015-07-27 17:42:45 +0000645bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
646 lex();
647 if (Token.isNot(MIToken::VirtualRegister))
648 return error("expected a virtual register");
649 if (parseRegister(Reg))
650 return 0;
651 lex();
652 if (Token.isNot(MIToken::Eof))
653 return error("expected end of string after the register reference");
654 return false;
655}
656
Alex Lorenz36962cd2015-07-07 02:08:46 +0000657static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
658 assert(MO.isImplicit());
659 return MO.isDef() ? "implicit-def" : "implicit";
660}
661
662static std::string getRegisterName(const TargetRegisterInfo *TRI,
663 unsigned Reg) {
664 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
665 return StringRef(TRI->getName(Reg)).lower();
666}
667
668bool MIParser::verifyImplicitOperands(
669 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
670 if (MCID.isCall())
671 // We can't verify call instructions as they can contain arbitrary implicit
672 // register and register mask operands.
673 return false;
674
675 // Gather all the expected implicit operands.
676 SmallVector<MachineOperand, 4> ImplicitOperands;
677 if (MCID.ImplicitDefs)
678 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
679 ImplicitOperands.push_back(
680 MachineOperand::CreateReg(*ImpDefs, true, true));
681 if (MCID.ImplicitUses)
682 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
683 ImplicitOperands.push_back(
684 MachineOperand::CreateReg(*ImpUses, false, true));
685
686 const auto *TRI = MF.getSubtarget().getRegisterInfo();
687 assert(TRI && "Expected target register info");
688 size_t I = ImplicitOperands.size(), J = Operands.size();
689 while (I) {
690 --I;
691 if (J) {
692 --J;
693 const auto &ImplicitOperand = ImplicitOperands[I];
694 const auto &Operand = Operands[J].Operand;
695 if (ImplicitOperand.isIdenticalTo(Operand))
696 continue;
697 if (Operand.isReg() && Operand.isImplicit()) {
Alex Lorenzeb7c9be2015-08-18 17:17:13 +0000698 // Check if this implicit register is a subregister of an explicit
699 // register operand.
700 bool IsImplicitSubRegister = false;
701 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
702 const auto &Op = Operands[K].Operand;
703 if (Op.isReg() && !Op.isImplicit() &&
704 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
705 IsImplicitSubRegister = true;
706 break;
707 }
708 }
709 if (IsImplicitSubRegister)
710 continue;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000711 return error(Operands[J].Begin,
712 Twine("expected an implicit register operand '") +
713 printImplicitRegisterFlag(ImplicitOperand) + " %" +
714 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
715 }
716 }
717 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
718 // insead of reporting an error at this location:
719 // %eax = MOV32r0
720 // ^
721 // report the error at the following location:
722 // %eax = MOV32r0
723 // ^
724 return error(J < Operands.size() ? Operands[J].End : Token.location(),
725 Twine("missing implicit register operand '") +
726 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
727 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
728 }
729 return false;
730}
731
Alex Lorenze5a44662015-07-17 00:24:15 +0000732bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
733 if (Token.is(MIToken::kw_frame_setup)) {
734 Flags |= MachineInstr::FrameSetup;
735 lex();
736 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000737 if (Token.isNot(MIToken::Identifier))
738 return error("expected a machine instruction");
739 StringRef InstrName = Token.stringValue();
740 if (parseInstrName(InstrName, OpCode))
741 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000742 lex();
743 return false;
744}
745
746bool MIParser::parseRegister(unsigned &Reg) {
747 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000748 case MIToken::underscore:
749 Reg = 0;
750 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000751 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000752 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000753 if (getRegisterByName(Name, Reg))
754 return error(Twine("unknown register name '") + Name + "'");
755 break;
756 }
Alex Lorenz53464512015-07-10 22:51:20 +0000757 case MIToken::VirtualRegister: {
758 unsigned ID;
759 if (getUnsigned(ID))
760 return true;
761 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
762 if (RegInfo == PFS.VirtualRegisterSlots.end())
763 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
764 "'");
765 Reg = RegInfo->second;
766 break;
767 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000768 // TODO: Parse other register kinds.
769 default:
770 llvm_unreachable("The current token should be a register");
771 }
772 return false;
773}
774
Alex Lorenzcb268d42015-07-06 23:07:26 +0000775bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000776 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000777 switch (Token.kind()) {
778 case MIToken::kw_implicit:
779 Flags |= RegState::Implicit;
780 break;
781 case MIToken::kw_implicit_define:
782 Flags |= RegState::ImplicitDefine;
783 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000784 case MIToken::kw_dead:
785 Flags |= RegState::Dead;
786 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000787 case MIToken::kw_killed:
788 Flags |= RegState::Kill;
789 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000790 case MIToken::kw_undef:
791 Flags |= RegState::Undef;
792 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000793 case MIToken::kw_internal:
794 Flags |= RegState::InternalRead;
795 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000796 case MIToken::kw_early_clobber:
797 Flags |= RegState::EarlyClobber;
798 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000799 case MIToken::kw_debug_use:
800 Flags |= RegState::Debug;
801 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000802 default:
803 llvm_unreachable("The current token should be a register flag");
804 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000805 if (OldFlags == Flags)
806 // We know that the same flag is specified more than once when the flags
807 // weren't modified.
808 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000809 lex();
810 return false;
811}
812
Alex Lorenz2eacca82015-07-13 23:24:34 +0000813bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
814 assert(Token.is(MIToken::colon));
815 lex();
816 if (Token.isNot(MIToken::Identifier))
817 return error("expected a subregister index after ':'");
818 auto Name = Token.stringValue();
819 SubReg = getSubRegIndex(Name);
820 if (!SubReg)
821 return error(Twine("use of unknown subregister index '") + Name + "'");
822 lex();
823 return false;
824}
825
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000826bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
827 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000828 unsigned Flags = IsDef ? RegState::Define : 0;
829 while (Token.isRegisterFlag()) {
830 if (parseRegisterFlag(Flags))
831 return true;
832 }
833 if (!Token.isRegister())
834 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000835 if (parseRegister(Reg))
836 return true;
837 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000838 unsigned SubReg = 0;
839 if (Token.is(MIToken::colon)) {
840 if (parseSubRegisterIndex(SubReg))
841 return true;
842 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000843 Dest = MachineOperand::CreateReg(
844 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000845 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000846 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
847 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000848 return false;
849}
850
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000851bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
852 assert(Token.is(MIToken::IntegerLiteral));
853 const APSInt &Int = Token.integerValue();
854 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000855 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000856 Dest = MachineOperand::CreateImm(Int.getExtValue());
857 lex();
858 return false;
859}
860
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000861bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000862 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000863 lex();
864 SMDiagnostic Err;
865 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
866 if (!C)
867 return error(Loc + Err.getColumnNo(), Err.getMessage());
868 return false;
869}
870
Alex Lorenz05e38822015-08-05 18:52:21 +0000871bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
872 assert(Token.is(MIToken::IntegerType));
873 auto Loc = Token.location();
874 lex();
875 if (Token.isNot(MIToken::IntegerLiteral))
876 return error("expected an integer literal");
877 const Constant *C = nullptr;
878 if (parseIRConstant(Loc, C))
879 return true;
880 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
881 return false;
882}
883
Alex Lorenzad156fb2015-07-31 20:49:21 +0000884bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
885 auto Loc = Token.location();
886 lex();
887 if (Token.isNot(MIToken::FloatingPointLiteral))
888 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000889 const Constant *C = nullptr;
890 if (parseIRConstant(Loc, C))
891 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000892 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
893 return false;
894}
895
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000896bool MIParser::getUnsigned(unsigned &Result) {
897 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
898 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
899 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
900 if (Val64 == Limit)
901 return error("expected 32-bit integer (too large)");
902 Result = Val64;
903 return false;
904}
905
Alex Lorenzf09df002015-06-30 18:16:42 +0000906bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000907 assert(Token.is(MIToken::MachineBasicBlock) ||
908 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000909 unsigned Number;
910 if (getUnsigned(Number))
911 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000912 auto MBBInfo = PFS.MBBSlots.find(Number);
913 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000914 return error(Twine("use of undefined machine basic block #") +
915 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000916 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000917 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
918 return error(Twine("the name of machine basic block #") + Twine(Number) +
919 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000920 return false;
921}
922
923bool MIParser::parseMBBOperand(MachineOperand &Dest) {
924 MachineBasicBlock *MBB;
925 if (parseMBBReference(MBB))
926 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000927 Dest = MachineOperand::CreateMBB(MBB);
928 lex();
929 return false;
930}
931
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000932bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
933 assert(Token.is(MIToken::StackObject));
934 unsigned ID;
935 if (getUnsigned(ID))
936 return true;
937 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
938 if (ObjectInfo == PFS.StackObjectSlots.end())
939 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
940 "'");
941 StringRef Name;
942 if (const auto *Alloca =
943 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
944 Name = Alloca->getName();
945 if (!Token.stringValue().empty() && Token.stringValue() != Name)
946 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
947 "' isn't '" + Token.stringValue() + "'");
948 lex();
949 Dest = MachineOperand::CreateFI(ObjectInfo->second);
950 return false;
951}
952
Alex Lorenzea882122015-08-12 21:17:02 +0000953bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000954 assert(Token.is(MIToken::FixedStackObject));
955 unsigned ID;
956 if (getUnsigned(ID))
957 return true;
958 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
959 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
960 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
961 Twine(ID) + "'");
962 lex();
Alex Lorenzea882122015-08-12 21:17:02 +0000963 FI = ObjectInfo->second;
964 return false;
965}
966
967bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
968 int FI;
969 if (parseFixedStackFrameIndex(FI))
970 return true;
971 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000972 return false;
973}
974
Alex Lorenz41df7d32015-07-28 17:09:52 +0000975bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000976 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000977 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000978 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000979 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000980 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000981 return error(Twine("use of undefined global value '") + Token.range() +
982 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000983 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000984 }
985 case MIToken::GlobalValue: {
986 unsigned GVIdx;
987 if (getUnsigned(GVIdx))
988 return true;
989 if (GVIdx >= IRSlots.GlobalValues.size())
990 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
991 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000992 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000993 break;
994 }
995 default:
996 llvm_unreachable("The current token should be a global value");
997 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000998 return false;
999}
1000
1001bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1002 GlobalValue *GV = nullptr;
1003 if (parseGlobalValue(GV))
1004 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001005 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001006 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001007 if (parseOperandsOffset(Dest))
1008 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001009 return false;
1010}
1011
Alex Lorenzab980492015-07-20 20:51:18 +00001012bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1013 assert(Token.is(MIToken::ConstantPoolItem));
1014 unsigned ID;
1015 if (getUnsigned(ID))
1016 return true;
1017 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1018 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1019 return error("use of undefined constant '%const." + Twine(ID) + "'");
1020 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001021 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001022 if (parseOperandsOffset(Dest))
1023 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001024 return false;
1025}
1026
Alex Lorenz31d70682015-07-15 23:38:35 +00001027bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1028 assert(Token.is(MIToken::JumpTableIndex));
1029 unsigned ID;
1030 if (getUnsigned(ID))
1031 return true;
1032 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1033 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1034 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1035 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001036 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1037 return false;
1038}
1039
Alex Lorenz6ede3742015-07-21 16:59:53 +00001040bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001041 assert(Token.is(MIToken::ExternalSymbol));
1042 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001043 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001044 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001045 if (parseOperandsOffset(Dest))
1046 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001047 return false;
1048}
1049
Alex Lorenz44f29252015-07-22 21:07:04 +00001050bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001051 assert(Token.is(MIToken::exclaim));
1052 auto Loc = Token.location();
1053 lex();
1054 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1055 return error("expected metadata id after '!'");
1056 unsigned ID;
1057 if (getUnsigned(ID))
1058 return true;
1059 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1060 if (NodeInfo == IRSlots.MetadataNodes.end())
1061 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1062 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001063 Node = NodeInfo->second.get();
1064 return false;
1065}
1066
1067bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1068 MDNode *Node = nullptr;
1069 if (parseMDNode(Node))
1070 return true;
1071 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001072 return false;
1073}
1074
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001075bool MIParser::parseCFIOffset(int &Offset) {
1076 if (Token.isNot(MIToken::IntegerLiteral))
1077 return error("expected a cfi offset");
1078 if (Token.integerValue().getMinSignedBits() > 32)
1079 return error("expected a 32 bit integer (the cfi offset is too large)");
1080 Offset = (int)Token.integerValue().getExtValue();
1081 lex();
1082 return false;
1083}
1084
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001085bool MIParser::parseCFIRegister(unsigned &Reg) {
1086 if (Token.isNot(MIToken::NamedRegister))
1087 return error("expected a cfi register");
1088 unsigned LLVMReg;
1089 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001090 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001091 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1092 assert(TRI && "Expected target register info");
1093 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1094 if (DwarfReg < 0)
1095 return error("invalid DWARF register");
1096 Reg = (unsigned)DwarfReg;
1097 lex();
1098 return false;
1099}
1100
1101bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1102 auto Kind = Token.kind();
1103 lex();
1104 auto &MMI = MF.getMMI();
1105 int Offset;
1106 unsigned Reg;
1107 unsigned CFIIndex;
1108 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001109 case MIToken::kw_cfi_same_value:
1110 if (parseCFIRegister(Reg))
1111 return true;
1112 CFIIndex =
1113 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1114 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001115 case MIToken::kw_cfi_offset:
1116 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1117 parseCFIOffset(Offset))
1118 return true;
1119 CFIIndex =
1120 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1121 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001122 case MIToken::kw_cfi_def_cfa_register:
1123 if (parseCFIRegister(Reg))
1124 return true;
1125 CFIIndex =
1126 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1127 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001128 case MIToken::kw_cfi_def_cfa_offset:
1129 if (parseCFIOffset(Offset))
1130 return true;
1131 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1132 CFIIndex = MMI.addFrameInst(
1133 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1134 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001135 case MIToken::kw_cfi_def_cfa:
1136 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1137 parseCFIOffset(Offset))
1138 return true;
1139 // NB: MCCFIInstruction::createDefCfa negates the offset.
1140 CFIIndex =
1141 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1142 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001143 default:
1144 // TODO: Parse the other CFI operands.
1145 llvm_unreachable("The current token should be a cfi operand");
1146 }
1147 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001148 return false;
1149}
1150
Alex Lorenzdeb53492015-07-28 17:28:03 +00001151bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1152 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001153 case MIToken::NamedIRBlock: {
1154 BB = dyn_cast_or_null<BasicBlock>(
1155 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001156 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001157 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001158 break;
1159 }
1160 case MIToken::IRBlock: {
1161 unsigned SlotNumber = 0;
1162 if (getUnsigned(SlotNumber))
1163 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001164 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001165 if (!BB)
1166 return error(Twine("use of undefined IR block '%ir-block.") +
1167 Twine(SlotNumber) + "'");
1168 break;
1169 }
1170 default:
1171 llvm_unreachable("The current token should be an IR block reference");
1172 }
1173 return false;
1174}
1175
1176bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1177 assert(Token.is(MIToken::kw_blockaddress));
1178 lex();
1179 if (expectAndConsume(MIToken::lparen))
1180 return true;
1181 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001182 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001183 return error("expected a global value");
1184 GlobalValue *GV = nullptr;
1185 if (parseGlobalValue(GV))
1186 return true;
1187 auto *F = dyn_cast<Function>(GV);
1188 if (!F)
1189 return error("expected an IR function reference");
1190 lex();
1191 if (expectAndConsume(MIToken::comma))
1192 return true;
1193 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001194 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001195 return error("expected an IR block reference");
1196 if (parseIRBlock(BB, *F))
1197 return true;
1198 lex();
1199 if (expectAndConsume(MIToken::rparen))
1200 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001201 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001202 if (parseOperandsOffset(Dest))
1203 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001204 return false;
1205}
1206
Alex Lorenzef5c1962015-07-28 23:02:45 +00001207bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1208 assert(Token.is(MIToken::kw_target_index));
1209 lex();
1210 if (expectAndConsume(MIToken::lparen))
1211 return true;
1212 if (Token.isNot(MIToken::Identifier))
1213 return error("expected the name of the target index");
1214 int Index = 0;
1215 if (getTargetIndex(Token.stringValue(), Index))
1216 return error("use of undefined target index '" + Token.stringValue() + "'");
1217 lex();
1218 if (expectAndConsume(MIToken::rparen))
1219 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001220 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001221 if (parseOperandsOffset(Dest))
1222 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001223 return false;
1224}
1225
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001226bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1227 assert(Token.is(MIToken::kw_liveout));
1228 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1229 assert(TRI && "Expected target register info");
1230 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1231 lex();
1232 if (expectAndConsume(MIToken::lparen))
1233 return true;
1234 while (true) {
1235 if (Token.isNot(MIToken::NamedRegister))
1236 return error("expected a named register");
1237 unsigned Reg = 0;
1238 if (parseRegister(Reg))
1239 return true;
1240 lex();
1241 Mask[Reg / 32] |= 1U << (Reg % 32);
1242 // TODO: Report an error if the same register is used more than once.
1243 if (Token.isNot(MIToken::comma))
1244 break;
1245 lex();
1246 }
1247 if (expectAndConsume(MIToken::rparen))
1248 return true;
1249 Dest = MachineOperand::CreateRegLiveOut(Mask);
1250 return false;
1251}
1252
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001253bool MIParser::parseMachineOperand(MachineOperand &Dest) {
1254 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001255 case MIToken::kw_implicit:
1256 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001257 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001258 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001259 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001260 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001261 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001262 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001263 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001264 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001265 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001266 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001267 case MIToken::IntegerLiteral:
1268 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001269 case MIToken::IntegerType:
1270 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001271 case MIToken::kw_half:
1272 case MIToken::kw_float:
1273 case MIToken::kw_double:
1274 case MIToken::kw_x86_fp80:
1275 case MIToken::kw_fp128:
1276 case MIToken::kw_ppc_fp128:
1277 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001278 case MIToken::MachineBasicBlock:
1279 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001280 case MIToken::StackObject:
1281 return parseStackObjectOperand(Dest);
1282 case MIToken::FixedStackObject:
1283 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001284 case MIToken::GlobalValue:
1285 case MIToken::NamedGlobalValue:
1286 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001287 case MIToken::ConstantPoolItem:
1288 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001289 case MIToken::JumpTableIndex:
1290 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001291 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001292 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001293 case MIToken::exclaim:
1294 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001295 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001296 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001297 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001298 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001299 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001300 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001301 case MIToken::kw_blockaddress:
1302 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001303 case MIToken::kw_target_index:
1304 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001305 case MIToken::kw_liveout:
1306 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001307 case MIToken::Error:
1308 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001309 case MIToken::Identifier:
1310 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1311 Dest = MachineOperand::CreateRegMask(RegMask);
1312 lex();
1313 break;
1314 }
1315 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001316 default:
1317 // TODO: parse the other machine operands.
1318 return error("expected a machine operand");
1319 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001320 return false;
1321}
1322
Alex Lorenz49873a82015-08-06 00:44:07 +00001323bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1324 unsigned TF = 0;
1325 bool HasTargetFlags = false;
1326 if (Token.is(MIToken::kw_target_flags)) {
1327 HasTargetFlags = true;
1328 lex();
1329 if (expectAndConsume(MIToken::lparen))
1330 return true;
1331 if (Token.isNot(MIToken::Identifier))
1332 return error("expected the name of the target flag");
1333 if (getDirectTargetFlag(Token.stringValue(), TF))
1334 return error("use of undefined target flag '" + Token.stringValue() +
1335 "'");
1336 lex();
1337 // TODO: Parse target's bit target flags.
1338 if (expectAndConsume(MIToken::rparen))
1339 return true;
1340 }
1341 auto Loc = Token.location();
1342 if (parseMachineOperand(Dest))
1343 return true;
1344 if (!HasTargetFlags)
1345 return false;
1346 if (Dest.isReg())
1347 return error(Loc, "register operands can't have target flags");
1348 Dest.setTargetFlags(TF);
1349 return false;
1350}
1351
Alex Lorenzdc24c172015-08-07 20:21:00 +00001352bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001353 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1354 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001355 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001356 bool IsNegative = Token.is(MIToken::minus);
1357 lex();
1358 if (Token.isNot(MIToken::IntegerLiteral))
1359 return error("expected an integer literal after '" + Sign + "'");
1360 if (Token.integerValue().getMinSignedBits() > 64)
1361 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001362 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001363 if (IsNegative)
1364 Offset = -Offset;
1365 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001366 return false;
1367}
1368
Alex Lorenz620f8912015-08-13 20:33:33 +00001369bool MIParser::parseAlignment(unsigned &Alignment) {
1370 assert(Token.is(MIToken::kw_align));
1371 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001372 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001373 return error("expected an integer literal after 'align'");
1374 if (getUnsigned(Alignment))
1375 return true;
1376 lex();
1377 return false;
1378}
1379
Alex Lorenzdc24c172015-08-07 20:21:00 +00001380bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1381 int64_t Offset = 0;
1382 if (parseOffset(Offset))
1383 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001384 Op.setOffset(Offset);
1385 return false;
1386}
1387
Alex Lorenz4af7e612015-08-03 23:08:19 +00001388bool MIParser::parseIRValue(Value *&V) {
1389 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001390 case MIToken::NamedIRValue: {
1391 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001392 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001393 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1394 Token.stringValue());
1395 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001396 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001397 break;
1398 }
1399 // TODO: Parse unnamed IR value references.
1400 default:
1401 llvm_unreachable("The current token should be an IR block reference");
1402 }
1403 return false;
1404}
1405
1406bool MIParser::getUint64(uint64_t &Result) {
1407 assert(Token.hasIntegerValue());
1408 if (Token.integerValue().getActiveBits() > 64)
1409 return error("expected 64-bit integer (too large)");
1410 Result = Token.integerValue().getZExtValue();
1411 return false;
1412}
1413
Alex Lorenza518b792015-08-04 00:24:45 +00001414bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001415 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001416 switch (Token.kind()) {
1417 case MIToken::kw_volatile:
1418 Flags |= MachineMemOperand::MOVolatile;
1419 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001420 case MIToken::kw_non_temporal:
1421 Flags |= MachineMemOperand::MONonTemporal;
1422 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001423 case MIToken::kw_invariant:
1424 Flags |= MachineMemOperand::MOInvariant;
1425 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001426 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001427 default:
1428 llvm_unreachable("The current token should be a memory operand flag");
1429 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001430 if (OldFlags == Flags)
1431 // We know that the same flag is specified more than once when the flags
1432 // weren't modified.
1433 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001434 lex();
1435 return false;
1436}
1437
Alex Lorenz91097a32015-08-12 20:33:26 +00001438bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1439 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001440 case MIToken::kw_stack:
1441 PSV = MF.getPSVManager().getStack();
1442 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001443 case MIToken::kw_got:
1444 PSV = MF.getPSVManager().getGOT();
1445 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001446 case MIToken::kw_jump_table:
1447 PSV = MF.getPSVManager().getJumpTable();
1448 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001449 case MIToken::kw_constant_pool:
1450 PSV = MF.getPSVManager().getConstantPool();
1451 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001452 case MIToken::FixedStackObject: {
1453 int FI;
1454 if (parseFixedStackFrameIndex(FI))
1455 return true;
1456 PSV = MF.getPSVManager().getFixedStack(FI);
1457 // The token was already consumed, so use return here instead of break.
1458 return false;
1459 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001460 case MIToken::GlobalValue:
1461 case MIToken::NamedGlobalValue: {
1462 GlobalValue *GV = nullptr;
1463 if (parseGlobalValue(GV))
1464 return true;
1465 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1466 break;
1467 }
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001468 case MIToken::ExternalSymbol:
1469 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1470 MF.createExternalSymbolName(Token.stringValue()));
1471 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001472 default:
1473 llvm_unreachable("The current token should be pseudo source value");
1474 }
1475 lex();
1476 return false;
1477}
1478
1479bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001480 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001481 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz50b826f2015-08-14 21:08:30 +00001482 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::GlobalValue) ||
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001483 Token.is(MIToken::NamedGlobalValue) ||
1484 Token.is(MIToken::ExternalSymbol)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001485 const PseudoSourceValue *PSV = nullptr;
1486 if (parseMemoryPseudoSourceValue(PSV))
1487 return true;
1488 int64_t Offset = 0;
1489 if (parseOffset(Offset))
1490 return true;
1491 Dest = MachinePointerInfo(PSV, Offset);
1492 return false;
1493 }
1494 if (Token.isNot(MIToken::NamedIRValue))
1495 return error("expected an IR value reference");
1496 Value *V = nullptr;
1497 if (parseIRValue(V))
1498 return true;
1499 if (!V->getType()->isPointerTy())
1500 return error("expected a pointer IR value");
1501 lex();
1502 int64_t Offset = 0;
1503 if (parseOffset(Offset))
1504 return true;
1505 Dest = MachinePointerInfo(V, Offset);
1506 return false;
1507}
1508
Alex Lorenz4af7e612015-08-03 23:08:19 +00001509bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1510 if (expectAndConsume(MIToken::lparen))
1511 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001512 unsigned Flags = 0;
1513 while (Token.isMemoryOperandFlag()) {
1514 if (parseMemoryOperandFlag(Flags))
1515 return true;
1516 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001517 if (Token.isNot(MIToken::Identifier) ||
1518 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1519 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001520 if (Token.stringValue() == "load")
1521 Flags |= MachineMemOperand::MOLoad;
1522 else
1523 Flags |= MachineMemOperand::MOStore;
1524 lex();
1525
1526 if (Token.isNot(MIToken::IntegerLiteral))
1527 return error("expected the size integer literal after memory operation");
1528 uint64_t Size;
1529 if (getUint64(Size))
1530 return true;
1531 lex();
1532
1533 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1534 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1535 return error(Twine("expected '") + Word + "'");
1536 lex();
1537
Alex Lorenz91097a32015-08-12 20:33:26 +00001538 MachinePointerInfo Ptr = MachinePointerInfo();
1539 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001540 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001541 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001542 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001543 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001544 while (consumeIfPresent(MIToken::comma)) {
1545 switch (Token.kind()) {
1546 case MIToken::kw_align:
1547 if (parseAlignment(BaseAlignment))
1548 return true;
1549 break;
1550 case MIToken::md_tbaa:
1551 lex();
1552 if (parseMDNode(AAInfo.TBAA))
1553 return true;
1554 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001555 case MIToken::md_alias_scope:
1556 lex();
1557 if (parseMDNode(AAInfo.Scope))
1558 return true;
1559 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001560 case MIToken::md_noalias:
1561 lex();
1562 if (parseMDNode(AAInfo.NoAlias))
1563 return true;
1564 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001565 case MIToken::md_range:
1566 lex();
1567 if (parseMDNode(Range))
1568 return true;
1569 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001570 // TODO: Report an error on duplicate metadata nodes.
1571 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001572 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1573 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001574 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001575 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001576 if (expectAndConsume(MIToken::rparen))
1577 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001578 Dest =
1579 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001580 return false;
1581}
1582
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001583void MIParser::initNames2InstrOpCodes() {
1584 if (!Names2InstrOpCodes.empty())
1585 return;
1586 const auto *TII = MF.getSubtarget().getInstrInfo();
1587 assert(TII && "Expected target instruction info");
1588 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1589 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1590}
1591
1592bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1593 initNames2InstrOpCodes();
1594 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1595 if (InstrInfo == Names2InstrOpCodes.end())
1596 return true;
1597 OpCode = InstrInfo->getValue();
1598 return false;
1599}
1600
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001601void MIParser::initNames2Regs() {
1602 if (!Names2Regs.empty())
1603 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001604 // The '%noreg' register is the register 0.
1605 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001606 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1607 assert(TRI && "Expected target register info");
1608 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1609 bool WasInserted =
1610 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1611 .second;
1612 (void)WasInserted;
1613 assert(WasInserted && "Expected registers to be unique case-insensitively");
1614 }
1615}
1616
1617bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1618 initNames2Regs();
1619 auto RegInfo = Names2Regs.find(RegName);
1620 if (RegInfo == Names2Regs.end())
1621 return true;
1622 Reg = RegInfo->getValue();
1623 return false;
1624}
1625
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001626void MIParser::initNames2RegMasks() {
1627 if (!Names2RegMasks.empty())
1628 return;
1629 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1630 assert(TRI && "Expected target register info");
1631 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1632 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1633 assert(RegMasks.size() == RegMaskNames.size());
1634 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1635 Names2RegMasks.insert(
1636 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1637}
1638
1639const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1640 initNames2RegMasks();
1641 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1642 if (RegMaskInfo == Names2RegMasks.end())
1643 return nullptr;
1644 return RegMaskInfo->getValue();
1645}
1646
Alex Lorenz2eacca82015-07-13 23:24:34 +00001647void MIParser::initNames2SubRegIndices() {
1648 if (!Names2SubRegIndices.empty())
1649 return;
1650 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1651 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1652 Names2SubRegIndices.insert(
1653 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1654}
1655
1656unsigned MIParser::getSubRegIndex(StringRef Name) {
1657 initNames2SubRegIndices();
1658 auto SubRegInfo = Names2SubRegIndices.find(Name);
1659 if (SubRegInfo == Names2SubRegIndices.end())
1660 return 0;
1661 return SubRegInfo->getValue();
1662}
1663
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001664static void initSlots2BasicBlocks(
1665 const Function &F,
1666 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1667 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001668 MST.incorporateFunction(F);
1669 for (auto &BB : F) {
1670 if (BB.hasName())
1671 continue;
1672 int Slot = MST.getLocalSlot(&BB);
1673 if (Slot == -1)
1674 continue;
1675 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1676 }
1677}
1678
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001679static const BasicBlock *getIRBlockFromSlot(
1680 unsigned Slot,
1681 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001682 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1683 if (BlockInfo == Slots2BasicBlocks.end())
1684 return nullptr;
1685 return BlockInfo->second;
1686}
1687
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001688const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1689 if (Slots2BasicBlocks.empty())
1690 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1691 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1692}
1693
1694const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1695 if (&F == MF.getFunction())
1696 return getIRBlock(Slot);
1697 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1698 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1699 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1700}
1701
Alex Lorenzef5c1962015-07-28 23:02:45 +00001702void MIParser::initNames2TargetIndices() {
1703 if (!Names2TargetIndices.empty())
1704 return;
1705 const auto *TII = MF.getSubtarget().getInstrInfo();
1706 assert(TII && "Expected target instruction info");
1707 auto Indices = TII->getSerializableTargetIndices();
1708 for (const auto &I : Indices)
1709 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1710}
1711
1712bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1713 initNames2TargetIndices();
1714 auto IndexInfo = Names2TargetIndices.find(Name);
1715 if (IndexInfo == Names2TargetIndices.end())
1716 return true;
1717 Index = IndexInfo->second;
1718 return false;
1719}
1720
Alex Lorenz49873a82015-08-06 00:44:07 +00001721void MIParser::initNames2DirectTargetFlags() {
1722 if (!Names2DirectTargetFlags.empty())
1723 return;
1724 const auto *TII = MF.getSubtarget().getInstrInfo();
1725 assert(TII && "Expected target instruction info");
1726 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1727 for (const auto &I : Flags)
1728 Names2DirectTargetFlags.insert(
1729 std::make_pair(StringRef(I.second), I.first));
1730}
1731
1732bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1733 initNames2DirectTargetFlags();
1734 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1735 if (FlagInfo == Names2DirectTargetFlags.end())
1736 return true;
1737 Flag = FlagInfo->second;
1738 return false;
1739}
1740
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001741bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1742 PerFunctionMIParsingState &PFS,
1743 const SlotMapping &IRSlots,
1744 SMDiagnostic &Error) {
1745 SourceMgr SM;
1746 SM.AddNewSourceBuffer(
1747 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1748 SMLoc());
1749 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1750 .parseBasicBlockDefinitions(PFS.MBBSlots);
1751}
1752
1753bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1754 const PerFunctionMIParsingState &PFS,
1755 const SlotMapping &IRSlots,
1756 SMDiagnostic &Error) {
1757 SourceMgr SM;
1758 SM.AddNewSourceBuffer(
1759 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1760 SMLoc());
1761 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001762}
Alex Lorenzf09df002015-06-30 18:16:42 +00001763
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001764bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1765 MachineFunction &MF, StringRef Src,
1766 const PerFunctionMIParsingState &PFS,
1767 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001768 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001769}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001770
1771bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1772 MachineFunction &MF, StringRef Src,
1773 const PerFunctionMIParsingState &PFS,
1774 const SlotMapping &IRSlots,
1775 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001776 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1777 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001778}
Alex Lorenz12045a42015-07-27 17:42:45 +00001779
1780bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1781 MachineFunction &MF, StringRef Src,
1782 const PerFunctionMIParsingState &PFS,
1783 const SlotMapping &IRSlots,
1784 SMDiagnostic &Error) {
1785 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1786 .parseStandaloneVirtualRegister(Reg);
1787}