1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the parsing of machine instructions.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/AsmParser/Parser.h"
18 #include "llvm/AsmParser/SlotMapping.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineMemOperand.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ModuleSlotTracker.h"
30 #include "llvm/IR/ValueSymbolTable.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
34 #include "llvm/Target/TargetInstrInfo.h"
40 /// A wrapper struct around the 'MachineOperand' struct that includes a source
42 struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
56 StringRef Source, CurrentSource;
58 const PerFunctionMIParsingState &PFS;
59 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
61 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
63 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
65 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
67 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
69 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
71 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
73 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
75 /// Maps from direct target flag names to the bitmask target flag values.
76 StringMap<unsigned> Names2BitmaskTargetFlags;
79 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
80 StringRef Source, const PerFunctionMIParsingState &PFS,
81 const SlotMapping &IRSlots);
85 /// Report an error at the current location with the given message.
87 /// This function always return true.
88 bool error(const Twine &Msg);
90 /// Report an error at the given location with the given message.
92 /// This function always return true.
93 bool error(StringRef::iterator Loc, const Twine &Msg);
96 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
97 bool parseBasicBlocks();
98 bool parse(MachineInstr *&MI);
99 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
100 bool parseStandaloneNamedRegister(unsigned &Reg);
101 bool parseStandaloneVirtualRegister(unsigned &Reg);
102 bool parseStandaloneStackObject(int &FI);
103 bool parseStandaloneMDNode(MDNode *&Node);
106 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
107 bool parseBasicBlock(MachineBasicBlock &MBB);
108 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
109 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
111 bool parseRegister(unsigned &Reg);
112 bool parseRegisterFlag(unsigned &Flags);
113 bool parseSubRegisterIndex(unsigned &SubReg);
114 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
115 bool parseImmediateOperand(MachineOperand &Dest);
116 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
117 bool parseTypedImmediateOperand(MachineOperand &Dest);
118 bool parseFPImmediateOperand(MachineOperand &Dest);
119 bool parseMBBReference(MachineBasicBlock *&MBB);
120 bool parseMBBOperand(MachineOperand &Dest);
121 bool parseStackFrameIndex(int &FI);
122 bool parseStackObjectOperand(MachineOperand &Dest);
123 bool parseFixedStackFrameIndex(int &FI);
124 bool parseFixedStackObjectOperand(MachineOperand &Dest);
125 bool parseGlobalValue(GlobalValue *&GV);
126 bool parseGlobalAddressOperand(MachineOperand &Dest);
127 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
128 bool parseJumpTableIndexOperand(MachineOperand &Dest);
129 bool parseExternalSymbolOperand(MachineOperand &Dest);
130 bool parseMDNode(MDNode *&Node);
131 bool parseMetadataOperand(MachineOperand &Dest);
132 bool parseCFIOffset(int &Offset);
133 bool parseCFIRegister(unsigned &Reg);
134 bool parseCFIOperand(MachineOperand &Dest);
135 bool parseIRBlock(BasicBlock *&BB, const Function &F);
136 bool parseBlockAddressOperand(MachineOperand &Dest);
137 bool parseTargetIndexOperand(MachineOperand &Dest);
138 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
139 bool parseMachineOperand(MachineOperand &Dest);
140 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
141 bool parseOffset(int64_t &Offset);
142 bool parseAlignment(unsigned &Alignment);
143 bool parseOperandsOffset(MachineOperand &Op);
144 bool parseIRValue(Value *&V);
145 bool parseMemoryOperandFlag(unsigned &Flags);
146 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
147 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
148 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
151 /// Convert the integer literal in the current token into an unsigned integer.
153 /// Return true if an error occurred.
154 bool getUnsigned(unsigned &Result);
156 /// Convert the integer literal in the current token into an uint64.
158 /// Return true if an error occurred.
159 bool getUint64(uint64_t &Result);
161 /// If the current token is of the given kind, consume it and return false.
162 /// Otherwise report an error and return true.
163 bool expectAndConsume(MIToken::TokenKind TokenKind);
165 /// If the current token is of the given kind, consume it and return true.
166 /// Otherwise return false.
167 bool consumeIfPresent(MIToken::TokenKind TokenKind);
169 void initNames2InstrOpCodes();
171 /// Try to convert an instruction name to an opcode. Return true if the
172 /// instruction name is invalid.
173 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
175 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
177 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
178 const MCInstrDesc &MCID);
180 void initNames2Regs();
182 /// Try to convert a register name to a register number. Return true if the
183 /// register name is invalid.
184 bool getRegisterByName(StringRef RegName, unsigned &Reg);
186 void initNames2RegMasks();
188 /// Check if the given identifier is a name of a register mask.
190 /// Return null if the identifier isn't a register mask.
191 const uint32_t *getRegMask(StringRef Identifier);
193 void initNames2SubRegIndices();
195 /// Check if the given identifier is a name of a subregister index.
197 /// Return 0 if the name isn't a subregister index class.
198 unsigned getSubRegIndex(StringRef Name);
200 const BasicBlock *getIRBlock(unsigned Slot);
201 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
203 void initNames2TargetIndices();
205 /// Try to convert a name of target index to the corresponding target index.
207 /// Return true if the name isn't a name of a target index.
208 bool getTargetIndex(StringRef Name, int &Index);
210 void initNames2DirectTargetFlags();
212 /// Try to convert a name of a direct target flag to the corresponding
215 /// Return true if the name isn't a name of a direct flag.
216 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
218 void initNames2BitmaskTargetFlags();
220 /// Try to convert a name of a bitmask target flag to the corresponding
223 /// Return true if the name isn't a name of a bitmask target flag.
224 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
227 } // end anonymous namespace
229 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
230 StringRef Source, const PerFunctionMIParsingState &PFS,
231 const SlotMapping &IRSlots)
232 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
233 PFS(PFS), IRSlots(IRSlots) {}
235 void MIParser::lex() {
236 CurrentSource = lexMIToken(
237 CurrentSource, Token,
238 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
241 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
243 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
244 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
245 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
246 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
247 // Create an ordinary diagnostic when the source manager's buffer is the
249 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
252 // Create a diagnostic for a YAML string literal.
253 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
254 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
259 static const char *toString(MIToken::TokenKind TokenKind) {
267 case MIToken::lparen:
269 case MIToken::rparen:
272 return "<unknown token>";
276 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
277 if (Token.isNot(TokenKind))
278 return error(Twine("expected ") + toString(TokenKind));
283 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
284 if (Token.isNot(TokenKind))
290 bool MIParser::parseBasicBlockDefinition(
291 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
292 assert(Token.is(MIToken::MachineBasicBlockLabel));
296 auto Loc = Token.location();
297 auto Name = Token.stringValue();
299 bool HasAddressTaken = false;
300 bool IsLandingPad = false;
301 unsigned Alignment = 0;
302 BasicBlock *BB = nullptr;
303 if (consumeIfPresent(MIToken::lparen)) {
305 // TODO: Report an error when multiple same attributes are specified.
306 switch (Token.kind()) {
307 case MIToken::kw_address_taken:
308 HasAddressTaken = true;
311 case MIToken::kw_landing_pad:
315 case MIToken::kw_align:
316 if (parseAlignment(Alignment))
319 case MIToken::IRBlock:
320 // TODO: Report an error when both name and ir block are specified.
321 if (parseIRBlock(BB, *MF.getFunction()))
328 } while (consumeIfPresent(MIToken::comma));
329 if (expectAndConsume(MIToken::rparen))
332 if (expectAndConsume(MIToken::colon))
336 BB = dyn_cast_or_null<BasicBlock>(
337 MF.getFunction()->getValueSymbolTable().lookup(Name));
339 return error(Loc, Twine("basic block '") + Name +
340 "' is not defined in the function '" +
343 auto *MBB = MF.CreateMachineBasicBlock(BB);
344 MF.insert(MF.end(), MBB);
345 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
347 return error(Loc, Twine("redefinition of machine basic block with id #") +
350 MBB->setAlignment(Alignment);
352 MBB->setHasAddressTaken();
353 MBB->setIsLandingPad(IsLandingPad);
357 bool MIParser::parseBasicBlockDefinitions(
358 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
360 // Skip until the first machine basic block.
361 while (Token.is(MIToken::Newline))
363 if (Token.isErrorOrEOF())
364 return Token.isError();
365 if (Token.isNot(MIToken::MachineBasicBlockLabel))
366 return error("expected a basic block definition before instructions");
367 unsigned BraceDepth = 0;
369 if (parseBasicBlockDefinition(MBBSlots))
371 bool IsAfterNewline = false;
372 // Skip until the next machine basic block.
374 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
375 Token.isErrorOrEOF())
377 else if (Token.is(MIToken::MachineBasicBlockLabel))
378 return error("basic block definition should be located at the start of "
380 else if (consumeIfPresent(MIToken::Newline)) {
381 IsAfterNewline = true;
384 IsAfterNewline = false;
385 if (Token.is(MIToken::lbrace))
387 if (Token.is(MIToken::rbrace)) {
389 return error("extraneous closing brace ('}')");
394 // Verify that we closed all of the '{' at the end of a file or a block.
395 if (!Token.isError() && BraceDepth)
396 return error("expected '}'"); // FIXME: Report a note that shows '{'.
397 } while (!Token.isErrorOrEOF());
398 return Token.isError();
401 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
402 assert(Token.is(MIToken::kw_liveins));
404 if (expectAndConsume(MIToken::colon))
406 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
409 if (Token.isNot(MIToken::NamedRegister))
410 return error("expected a named register");
412 if (parseRegister(Reg))
416 } while (consumeIfPresent(MIToken::comma));
420 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
421 assert(Token.is(MIToken::kw_successors));
423 if (expectAndConsume(MIToken::colon))
425 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
428 if (Token.isNot(MIToken::MachineBasicBlock))
429 return error("expected a machine basic block reference");
430 MachineBasicBlock *SuccMBB = nullptr;
431 if (parseMBBReference(SuccMBB))
435 if (consumeIfPresent(MIToken::lparen)) {
436 if (Token.isNot(MIToken::IntegerLiteral))
437 return error("expected an integer literal after '('");
438 if (getUnsigned(Weight))
441 if (expectAndConsume(MIToken::rparen))
444 MBB.addSuccessor(SuccMBB, Weight);
445 } while (consumeIfPresent(MIToken::comma));
449 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
450 // Skip the definition.
451 assert(Token.is(MIToken::MachineBasicBlockLabel));
453 if (consumeIfPresent(MIToken::lparen)) {
454 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
456 consumeIfPresent(MIToken::rparen);
458 consumeIfPresent(MIToken::colon);
460 // Parse the liveins and successors.
461 // N.B: Multiple lists of successors and liveins are allowed and they're
468 // liveins: %edi, %esi
470 if (Token.is(MIToken::kw_successors)) {
471 if (parseBasicBlockSuccessors(MBB))
473 } else if (Token.is(MIToken::kw_liveins)) {
474 if (parseBasicBlockLiveins(MBB))
476 } else if (consumeIfPresent(MIToken::Newline)) {
480 if (!Token.isNewlineOrEOF())
481 return error("expected line break at the end of a list");
485 // Parse the instructions.
486 bool IsInBundle = false;
487 MachineInstr *PrevMI = nullptr;
489 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
491 else if (consumeIfPresent(MIToken::Newline))
493 if (consumeIfPresent(MIToken::rbrace)) {
494 // The first parsing pass should verify that all closing '}' have an
500 MachineInstr *MI = nullptr;
503 MBB.insert(MBB.end(), MI);
505 PrevMI->setFlag(MachineInstr::BundledSucc);
506 MI->setFlag(MachineInstr::BundledPred);
509 if (Token.is(MIToken::lbrace)) {
511 return error("nested instruction bundles are not allowed");
513 // This instruction is the start of the bundle.
514 MI->setFlag(MachineInstr::BundledSucc);
516 if (!Token.is(MIToken::Newline))
517 // The next instruction can be on the same line.
520 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
526 bool MIParser::parseBasicBlocks() {
528 // Skip until the first machine basic block.
529 while (Token.is(MIToken::Newline))
531 if (Token.isErrorOrEOF())
532 return Token.isError();
533 // The first parsing pass should have verified that this token is a MBB label
534 // in the 'parseBasicBlockDefinitions' method.
535 assert(Token.is(MIToken::MachineBasicBlockLabel));
537 MachineBasicBlock *MBB = nullptr;
538 if (parseMBBReference(MBB))
540 if (parseBasicBlock(*MBB))
542 // The method 'parseBasicBlock' should parse the whole block until the next
543 // block or the end of file.
544 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
545 } while (Token.isNot(MIToken::Eof));
549 bool MIParser::parse(MachineInstr *&MI) {
550 // Parse any register operands before '='
551 MachineOperand MO = MachineOperand::CreateImm(0);
552 SmallVector<MachineOperandWithLocation, 8> Operands;
553 while (Token.isRegister() || Token.isRegisterFlag()) {
554 auto Loc = Token.location();
555 if (parseRegisterOperand(MO, /*IsDef=*/true))
557 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
558 if (Token.isNot(MIToken::comma))
562 if (!Operands.empty() && expectAndConsume(MIToken::equal))
565 unsigned OpCode, Flags = 0;
566 if (Token.isError() || parseInstruction(OpCode, Flags))
569 // Parse the remaining machine operands.
570 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
571 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
572 auto Loc = Token.location();
573 if (parseMachineOperandAndTargetFlags(MO))
575 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
576 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
577 Token.is(MIToken::lbrace))
579 if (Token.isNot(MIToken::comma))
580 return error("expected ',' before the next machine operand");
584 DebugLoc DebugLocation;
585 if (Token.is(MIToken::kw_debug_location)) {
587 if (Token.isNot(MIToken::exclaim))
588 return error("expected a metadata node after 'debug-location'");
589 MDNode *Node = nullptr;
590 if (parseMDNode(Node))
592 DebugLocation = DebugLoc(Node);
595 // Parse the machine memory operands.
596 SmallVector<MachineMemOperand *, 2> MemOperands;
597 if (Token.is(MIToken::coloncolon)) {
599 while (!Token.isNewlineOrEOF()) {
600 MachineMemOperand *MemOp = nullptr;
601 if (parseMachineMemoryOperand(MemOp))
603 MemOperands.push_back(MemOp);
604 if (Token.isNewlineOrEOF())
606 if (Token.isNot(MIToken::comma))
607 return error("expected ',' before the next machine memory operand");
612 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
613 if (!MCID.isVariadic()) {
614 // FIXME: Move the implicit operand verification to the machine verifier.
615 if (verifyImplicitOperands(Operands, MCID))
619 // TODO: Check for extraneous machine operands.
620 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
622 for (const auto &Operand : Operands)
623 MI->addOperand(MF, Operand.Operand);
624 if (MemOperands.empty())
626 MachineInstr::mmo_iterator MemRefs =
627 MF.allocateMemRefsArray(MemOperands.size());
628 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
629 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
633 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
635 if (Token.isNot(MIToken::MachineBasicBlock))
636 return error("expected a machine basic block reference");
637 if (parseMBBReference(MBB))
640 if (Token.isNot(MIToken::Eof))
642 "expected end of string after the machine basic block reference");
646 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
648 if (Token.isNot(MIToken::NamedRegister))
649 return error("expected a named register");
650 if (parseRegister(Reg))
653 if (Token.isNot(MIToken::Eof))
654 return error("expected end of string after the register reference");
658 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
660 if (Token.isNot(MIToken::VirtualRegister))
661 return error("expected a virtual register");
662 if (parseRegister(Reg))
665 if (Token.isNot(MIToken::Eof))
666 return error("expected end of string after the register reference");
670 bool MIParser::parseStandaloneStackObject(int &FI) {
672 if (Token.isNot(MIToken::StackObject))
673 return error("expected a stack object");
674 if (parseStackFrameIndex(FI))
676 if (Token.isNot(MIToken::Eof))
677 return error("expected end of string after the stack object reference");
681 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
683 if (Token.isNot(MIToken::exclaim))
684 return error("expected a metadata node");
685 if (parseMDNode(Node))
687 if (Token.isNot(MIToken::Eof))
688 return error("expected end of string after the metadata node");
692 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
693 assert(MO.isImplicit());
694 return MO.isDef() ? "implicit-def" : "implicit";
697 static std::string getRegisterName(const TargetRegisterInfo *TRI,
699 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
700 return StringRef(TRI->getName(Reg)).lower();
703 bool MIParser::verifyImplicitOperands(
704 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
706 // We can't verify call instructions as they can contain arbitrary implicit
707 // register and register mask operands.
710 // Gather all the expected implicit operands.
711 SmallVector<MachineOperand, 4> ImplicitOperands;
712 if (MCID.ImplicitDefs)
713 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
714 ImplicitOperands.push_back(
715 MachineOperand::CreateReg(*ImpDefs, true, true));
716 if (MCID.ImplicitUses)
717 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
718 ImplicitOperands.push_back(
719 MachineOperand::CreateReg(*ImpUses, false, true));
721 const auto *TRI = MF.getSubtarget().getRegisterInfo();
722 assert(TRI && "Expected target register info");
723 size_t I = ImplicitOperands.size(), J = Operands.size();
728 const auto &ImplicitOperand = ImplicitOperands[I];
729 const auto &Operand = Operands[J].Operand;
730 if (ImplicitOperand.isIdenticalTo(Operand))
732 if (Operand.isReg() && Operand.isImplicit()) {
733 // Check if this implicit register is a subregister of an explicit
735 bool IsImplicitSubRegister = false;
736 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
737 const auto &Op = Operands[K].Operand;
738 if (Op.isReg() && !Op.isImplicit() &&
739 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
740 IsImplicitSubRegister = true;
744 if (IsImplicitSubRegister)
746 return error(Operands[J].Begin,
747 Twine("expected an implicit register operand '") +
748 printImplicitRegisterFlag(ImplicitOperand) + " %" +
749 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
752 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
753 // insead of reporting an error at this location:
756 // report the error at the following location:
759 return error(J < Operands.size() ? Operands[J].End : Token.location(),
760 Twine("missing implicit register operand '") +
761 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
762 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
767 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
768 if (Token.is(MIToken::kw_frame_setup)) {
769 Flags |= MachineInstr::FrameSetup;
772 if (Token.isNot(MIToken::Identifier))
773 return error("expected a machine instruction");
774 StringRef InstrName = Token.stringValue();
775 if (parseInstrName(InstrName, OpCode))
776 return error(Twine("unknown machine instruction name '") + InstrName + "'");
781 bool MIParser::parseRegister(unsigned &Reg) {
782 switch (Token.kind()) {
783 case MIToken::underscore:
786 case MIToken::NamedRegister: {
787 StringRef Name = Token.stringValue();
788 if (getRegisterByName(Name, Reg))
789 return error(Twine("unknown register name '") + Name + "'");
792 case MIToken::VirtualRegister: {
796 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
797 if (RegInfo == PFS.VirtualRegisterSlots.end())
798 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
800 Reg = RegInfo->second;
803 // TODO: Parse other register kinds.
805 llvm_unreachable("The current token should be a register");
810 bool MIParser::parseRegisterFlag(unsigned &Flags) {
811 const unsigned OldFlags = Flags;
812 switch (Token.kind()) {
813 case MIToken::kw_implicit:
814 Flags |= RegState::Implicit;
816 case MIToken::kw_implicit_define:
817 Flags |= RegState::ImplicitDefine;
819 case MIToken::kw_dead:
820 Flags |= RegState::Dead;
822 case MIToken::kw_killed:
823 Flags |= RegState::Kill;
825 case MIToken::kw_undef:
826 Flags |= RegState::Undef;
828 case MIToken::kw_internal:
829 Flags |= RegState::InternalRead;
831 case MIToken::kw_early_clobber:
832 Flags |= RegState::EarlyClobber;
834 case MIToken::kw_debug_use:
835 Flags |= RegState::Debug;
838 llvm_unreachable("The current token should be a register flag");
840 if (OldFlags == Flags)
841 // We know that the same flag is specified more than once when the flags
843 return error("duplicate '" + Token.stringValue() + "' register flag");
848 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
849 assert(Token.is(MIToken::colon));
851 if (Token.isNot(MIToken::Identifier))
852 return error("expected a subregister index after ':'");
853 auto Name = Token.stringValue();
854 SubReg = getSubRegIndex(Name);
856 return error(Twine("use of unknown subregister index '") + Name + "'");
861 bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
863 unsigned Flags = IsDef ? RegState::Define : 0;
864 while (Token.isRegisterFlag()) {
865 if (parseRegisterFlag(Flags))
868 if (!Token.isRegister())
869 return error("expected a register after register flags");
870 if (parseRegister(Reg))
874 if (Token.is(MIToken::colon)) {
875 if (parseSubRegisterIndex(SubReg))
878 Dest = MachineOperand::CreateReg(
879 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
880 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
881 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
882 Flags & RegState::InternalRead);
886 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
887 assert(Token.is(MIToken::IntegerLiteral));
888 const APSInt &Int = Token.integerValue();
889 if (Int.getMinSignedBits() > 64)
890 return error("integer literal is too large to be an immediate operand");
891 Dest = MachineOperand::CreateImm(Int.getExtValue());
896 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
897 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
900 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
902 return error(Loc + Err.getColumnNo(), Err.getMessage());
906 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
907 assert(Token.is(MIToken::IntegerType));
908 auto Loc = Token.location();
910 if (Token.isNot(MIToken::IntegerLiteral))
911 return error("expected an integer literal");
912 const Constant *C = nullptr;
913 if (parseIRConstant(Loc, C))
915 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
919 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
920 auto Loc = Token.location();
922 if (Token.isNot(MIToken::FloatingPointLiteral))
923 return error("expected a floating point literal");
924 const Constant *C = nullptr;
925 if (parseIRConstant(Loc, C))
927 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
931 bool MIParser::getUnsigned(unsigned &Result) {
932 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
933 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
934 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
936 return error("expected 32-bit integer (too large)");
941 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
942 assert(Token.is(MIToken::MachineBasicBlock) ||
943 Token.is(MIToken::MachineBasicBlockLabel));
945 if (getUnsigned(Number))
947 auto MBBInfo = PFS.MBBSlots.find(Number);
948 if (MBBInfo == PFS.MBBSlots.end())
949 return error(Twine("use of undefined machine basic block #") +
951 MBB = MBBInfo->second;
952 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
953 return error(Twine("the name of machine basic block #") + Twine(Number) +
954 " isn't '" + Token.stringValue() + "'");
958 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
959 MachineBasicBlock *MBB;
960 if (parseMBBReference(MBB))
962 Dest = MachineOperand::CreateMBB(MBB);
967 bool MIParser::parseStackFrameIndex(int &FI) {
968 assert(Token.is(MIToken::StackObject));
972 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
973 if (ObjectInfo == PFS.StackObjectSlots.end())
974 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
977 if (const auto *Alloca =
978 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
979 Name = Alloca->getName();
980 if (!Token.stringValue().empty() && Token.stringValue() != Name)
981 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
982 "' isn't '" + Token.stringValue() + "'");
984 FI = ObjectInfo->second;
988 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
990 if (parseStackFrameIndex(FI))
992 Dest = MachineOperand::CreateFI(FI);
996 bool MIParser::parseFixedStackFrameIndex(int &FI) {
997 assert(Token.is(MIToken::FixedStackObject));
1001 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1002 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1003 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1006 FI = ObjectInfo->second;
1010 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1012 if (parseFixedStackFrameIndex(FI))
1014 Dest = MachineOperand::CreateFI(FI);
1018 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1019 switch (Token.kind()) {
1020 case MIToken::NamedGlobalValue: {
1021 const Module *M = MF.getFunction()->getParent();
1022 GV = M->getNamedValue(Token.stringValue());
1024 return error(Twine("use of undefined global value '") + Token.range() +
1028 case MIToken::GlobalValue: {
1030 if (getUnsigned(GVIdx))
1032 if (GVIdx >= IRSlots.GlobalValues.size())
1033 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1035 GV = IRSlots.GlobalValues[GVIdx];
1039 llvm_unreachable("The current token should be a global value");
1044 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1045 GlobalValue *GV = nullptr;
1046 if (parseGlobalValue(GV))
1049 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1050 if (parseOperandsOffset(Dest))
1055 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1056 assert(Token.is(MIToken::ConstantPoolItem));
1058 if (getUnsigned(ID))
1060 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1061 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1062 return error("use of undefined constant '%const." + Twine(ID) + "'");
1064 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1065 if (parseOperandsOffset(Dest))
1070 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1071 assert(Token.is(MIToken::JumpTableIndex));
1073 if (getUnsigned(ID))
1075 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1076 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1077 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1079 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1083 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1084 assert(Token.is(MIToken::ExternalSymbol));
1085 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1087 Dest = MachineOperand::CreateES(Symbol);
1088 if (parseOperandsOffset(Dest))
1093 bool MIParser::parseMDNode(MDNode *&Node) {
1094 assert(Token.is(MIToken::exclaim));
1095 auto Loc = Token.location();
1097 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1098 return error("expected metadata id after '!'");
1100 if (getUnsigned(ID))
1102 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1103 if (NodeInfo == IRSlots.MetadataNodes.end())
1104 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1106 Node = NodeInfo->second.get();
1110 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1111 MDNode *Node = nullptr;
1112 if (parseMDNode(Node))
1114 Dest = MachineOperand::CreateMetadata(Node);
1118 bool MIParser::parseCFIOffset(int &Offset) {
1119 if (Token.isNot(MIToken::IntegerLiteral))
1120 return error("expected a cfi offset");
1121 if (Token.integerValue().getMinSignedBits() > 32)
1122 return error("expected a 32 bit integer (the cfi offset is too large)");
1123 Offset = (int)Token.integerValue().getExtValue();
1128 bool MIParser::parseCFIRegister(unsigned &Reg) {
1129 if (Token.isNot(MIToken::NamedRegister))
1130 return error("expected a cfi register");
1132 if (parseRegister(LLVMReg))
1134 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1135 assert(TRI && "Expected target register info");
1136 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1138 return error("invalid DWARF register");
1139 Reg = (unsigned)DwarfReg;
1144 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1145 auto Kind = Token.kind();
1147 auto &MMI = MF.getMMI();
1152 case MIToken::kw_cfi_same_value:
1153 if (parseCFIRegister(Reg))
1156 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1158 case MIToken::kw_cfi_offset:
1159 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1160 parseCFIOffset(Offset))
1163 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1165 case MIToken::kw_cfi_def_cfa_register:
1166 if (parseCFIRegister(Reg))
1169 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1171 case MIToken::kw_cfi_def_cfa_offset:
1172 if (parseCFIOffset(Offset))
1174 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1175 CFIIndex = MMI.addFrameInst(
1176 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1178 case MIToken::kw_cfi_def_cfa:
1179 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1180 parseCFIOffset(Offset))
1182 // NB: MCCFIInstruction::createDefCfa negates the offset.
1184 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1187 // TODO: Parse the other CFI operands.
1188 llvm_unreachable("The current token should be a cfi operand");
1190 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1194 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1195 switch (Token.kind()) {
1196 case MIToken::NamedIRBlock: {
1197 BB = dyn_cast_or_null<BasicBlock>(
1198 F.getValueSymbolTable().lookup(Token.stringValue()));
1200 return error(Twine("use of undefined IR block '") + Token.range() + "'");
1203 case MIToken::IRBlock: {
1204 unsigned SlotNumber = 0;
1205 if (getUnsigned(SlotNumber))
1207 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1209 return error(Twine("use of undefined IR block '%ir-block.") +
1210 Twine(SlotNumber) + "'");
1214 llvm_unreachable("The current token should be an IR block reference");
1219 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1220 assert(Token.is(MIToken::kw_blockaddress));
1222 if (expectAndConsume(MIToken::lparen))
1224 if (Token.isNot(MIToken::GlobalValue) &&
1225 Token.isNot(MIToken::NamedGlobalValue))
1226 return error("expected a global value");
1227 GlobalValue *GV = nullptr;
1228 if (parseGlobalValue(GV))
1230 auto *F = dyn_cast<Function>(GV);
1232 return error("expected an IR function reference");
1234 if (expectAndConsume(MIToken::comma))
1236 BasicBlock *BB = nullptr;
1237 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1238 return error("expected an IR block reference");
1239 if (parseIRBlock(BB, *F))
1242 if (expectAndConsume(MIToken::rparen))
1244 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1245 if (parseOperandsOffset(Dest))
1250 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1251 assert(Token.is(MIToken::kw_target_index));
1253 if (expectAndConsume(MIToken::lparen))
1255 if (Token.isNot(MIToken::Identifier))
1256 return error("expected the name of the target index");
1258 if (getTargetIndex(Token.stringValue(), Index))
1259 return error("use of undefined target index '" + Token.stringValue() + "'");
1261 if (expectAndConsume(MIToken::rparen))
1263 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1264 if (parseOperandsOffset(Dest))
1269 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1270 assert(Token.is(MIToken::kw_liveout));
1271 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1272 assert(TRI && "Expected target register info");
1273 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1275 if (expectAndConsume(MIToken::lparen))
1278 if (Token.isNot(MIToken::NamedRegister))
1279 return error("expected a named register");
1281 if (parseRegister(Reg))
1284 Mask[Reg / 32] |= 1U << (Reg % 32);
1285 // TODO: Report an error if the same register is used more than once.
1286 if (Token.isNot(MIToken::comma))
1290 if (expectAndConsume(MIToken::rparen))
1292 Dest = MachineOperand::CreateRegLiveOut(Mask);
1296 bool MIParser::parseMachineOperand(MachineOperand &Dest) {
1297 switch (Token.kind()) {
1298 case MIToken::kw_implicit:
1299 case MIToken::kw_implicit_define:
1300 case MIToken::kw_dead:
1301 case MIToken::kw_killed:
1302 case MIToken::kw_undef:
1303 case MIToken::kw_internal:
1304 case MIToken::kw_early_clobber:
1305 case MIToken::kw_debug_use:
1306 case MIToken::underscore:
1307 case MIToken::NamedRegister:
1308 case MIToken::VirtualRegister:
1309 return parseRegisterOperand(Dest);
1310 case MIToken::IntegerLiteral:
1311 return parseImmediateOperand(Dest);
1312 case MIToken::IntegerType:
1313 return parseTypedImmediateOperand(Dest);
1314 case MIToken::kw_half:
1315 case MIToken::kw_float:
1316 case MIToken::kw_double:
1317 case MIToken::kw_x86_fp80:
1318 case MIToken::kw_fp128:
1319 case MIToken::kw_ppc_fp128:
1320 return parseFPImmediateOperand(Dest);
1321 case MIToken::MachineBasicBlock:
1322 return parseMBBOperand(Dest);
1323 case MIToken::StackObject:
1324 return parseStackObjectOperand(Dest);
1325 case MIToken::FixedStackObject:
1326 return parseFixedStackObjectOperand(Dest);
1327 case MIToken::GlobalValue:
1328 case MIToken::NamedGlobalValue:
1329 return parseGlobalAddressOperand(Dest);
1330 case MIToken::ConstantPoolItem:
1331 return parseConstantPoolIndexOperand(Dest);
1332 case MIToken::JumpTableIndex:
1333 return parseJumpTableIndexOperand(Dest);
1334 case MIToken::ExternalSymbol:
1335 return parseExternalSymbolOperand(Dest);
1336 case MIToken::exclaim:
1337 return parseMetadataOperand(Dest);
1338 case MIToken::kw_cfi_same_value:
1339 case MIToken::kw_cfi_offset:
1340 case MIToken::kw_cfi_def_cfa_register:
1341 case MIToken::kw_cfi_def_cfa_offset:
1342 case MIToken::kw_cfi_def_cfa:
1343 return parseCFIOperand(Dest);
1344 case MIToken::kw_blockaddress:
1345 return parseBlockAddressOperand(Dest);
1346 case MIToken::kw_target_index:
1347 return parseTargetIndexOperand(Dest);
1348 case MIToken::kw_liveout:
1349 return parseLiveoutRegisterMaskOperand(Dest);
1350 case MIToken::Error:
1352 case MIToken::Identifier:
1353 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1354 Dest = MachineOperand::CreateRegMask(RegMask);
1360 // TODO: parse the other machine operands.
1361 return error("expected a machine operand");
1366 bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1368 bool HasTargetFlags = false;
1369 if (Token.is(MIToken::kw_target_flags)) {
1370 HasTargetFlags = true;
1372 if (expectAndConsume(MIToken::lparen))
1374 if (Token.isNot(MIToken::Identifier))
1375 return error("expected the name of the target flag");
1376 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1377 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1378 return error("use of undefined target flag '" + Token.stringValue() +
1382 while (Token.is(MIToken::comma)) {
1384 if (Token.isNot(MIToken::Identifier))
1385 return error("expected the name of the target flag");
1386 unsigned BitFlag = 0;
1387 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1388 return error("use of undefined target flag '" + Token.stringValue() +
1390 // TODO: Report an error when using a duplicate bit target flag.
1394 if (expectAndConsume(MIToken::rparen))
1397 auto Loc = Token.location();
1398 if (parseMachineOperand(Dest))
1400 if (!HasTargetFlags)
1403 return error(Loc, "register operands can't have target flags");
1404 Dest.setTargetFlags(TF);
1408 bool MIParser::parseOffset(int64_t &Offset) {
1409 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1411 StringRef Sign = Token.range();
1412 bool IsNegative = Token.is(MIToken::minus);
1414 if (Token.isNot(MIToken::IntegerLiteral))
1415 return error("expected an integer literal after '" + Sign + "'");
1416 if (Token.integerValue().getMinSignedBits() > 64)
1417 return error("expected 64-bit integer (too large)");
1418 Offset = Token.integerValue().getExtValue();
1425 bool MIParser::parseAlignment(unsigned &Alignment) {
1426 assert(Token.is(MIToken::kw_align));
1428 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1429 return error("expected an integer literal after 'align'");
1430 if (getUnsigned(Alignment))
1436 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1438 if (parseOffset(Offset))
1440 Op.setOffset(Offset);
1444 bool MIParser::parseIRValue(Value *&V) {
1445 switch (Token.kind()) {
1446 case MIToken::NamedIRValue: {
1447 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1449 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1450 Token.stringValue());
1452 return error(Twine("use of undefined IR value '") + Token.range() + "'");
1455 // TODO: Parse unnamed IR value references.
1457 llvm_unreachable("The current token should be an IR block reference");
1462 bool MIParser::getUint64(uint64_t &Result) {
1463 assert(Token.hasIntegerValue());
1464 if (Token.integerValue().getActiveBits() > 64)
1465 return error("expected 64-bit integer (too large)");
1466 Result = Token.integerValue().getZExtValue();
1470 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1471 const unsigned OldFlags = Flags;
1472 switch (Token.kind()) {
1473 case MIToken::kw_volatile:
1474 Flags |= MachineMemOperand::MOVolatile;
1476 case MIToken::kw_non_temporal:
1477 Flags |= MachineMemOperand::MONonTemporal;
1479 case MIToken::kw_invariant:
1480 Flags |= MachineMemOperand::MOInvariant;
1482 // TODO: parse the target specific memory operand flags.
1484 llvm_unreachable("The current token should be a memory operand flag");
1486 if (OldFlags == Flags)
1487 // We know that the same flag is specified more than once when the flags
1488 // weren't modified.
1489 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1494 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1495 switch (Token.kind()) {
1496 case MIToken::kw_stack:
1497 PSV = MF.getPSVManager().getStack();
1499 case MIToken::kw_got:
1500 PSV = MF.getPSVManager().getGOT();
1502 case MIToken::kw_jump_table:
1503 PSV = MF.getPSVManager().getJumpTable();
1505 case MIToken::kw_constant_pool:
1506 PSV = MF.getPSVManager().getConstantPool();
1508 case MIToken::FixedStackObject: {
1510 if (parseFixedStackFrameIndex(FI))
1512 PSV = MF.getPSVManager().getFixedStack(FI);
1513 // The token was already consumed, so use return here instead of break.
1516 case MIToken::GlobalValue:
1517 case MIToken::NamedGlobalValue: {
1518 GlobalValue *GV = nullptr;
1519 if (parseGlobalValue(GV))
1521 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1524 case MIToken::ExternalSymbol:
1525 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1526 MF.createExternalSymbolName(Token.stringValue()));
1529 llvm_unreachable("The current token should be pseudo source value");
1535 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1536 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1537 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1538 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::GlobalValue) ||
1539 Token.is(MIToken::NamedGlobalValue) ||
1540 Token.is(MIToken::ExternalSymbol)) {
1541 const PseudoSourceValue *PSV = nullptr;
1542 if (parseMemoryPseudoSourceValue(PSV))
1545 if (parseOffset(Offset))
1547 Dest = MachinePointerInfo(PSV, Offset);
1550 if (Token.isNot(MIToken::NamedIRValue))
1551 return error("expected an IR value reference");
1553 if (parseIRValue(V))
1555 if (!V->getType()->isPointerTy())
1556 return error("expected a pointer IR value");
1559 if (parseOffset(Offset))
1561 Dest = MachinePointerInfo(V, Offset);
1565 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1566 if (expectAndConsume(MIToken::lparen))
1569 while (Token.isMemoryOperandFlag()) {
1570 if (parseMemoryOperandFlag(Flags))
1573 if (Token.isNot(MIToken::Identifier) ||
1574 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1575 return error("expected 'load' or 'store' memory operation");
1576 if (Token.stringValue() == "load")
1577 Flags |= MachineMemOperand::MOLoad;
1579 Flags |= MachineMemOperand::MOStore;
1582 if (Token.isNot(MIToken::IntegerLiteral))
1583 return error("expected the size integer literal after memory operation");
1585 if (getUint64(Size))
1589 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1590 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1591 return error(Twine("expected '") + Word + "'");
1594 MachinePointerInfo Ptr = MachinePointerInfo();
1595 if (parseMachinePointerInfo(Ptr))
1597 unsigned BaseAlignment = Size;
1599 MDNode *Range = nullptr;
1600 while (consumeIfPresent(MIToken::comma)) {
1601 switch (Token.kind()) {
1602 case MIToken::kw_align:
1603 if (parseAlignment(BaseAlignment))
1606 case MIToken::md_tbaa:
1608 if (parseMDNode(AAInfo.TBAA))
1611 case MIToken::md_alias_scope:
1613 if (parseMDNode(AAInfo.Scope))
1616 case MIToken::md_noalias:
1618 if (parseMDNode(AAInfo.NoAlias))
1621 case MIToken::md_range:
1623 if (parseMDNode(Range))
1626 // TODO: Report an error on duplicate metadata nodes.
1628 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1629 "'!noalias' or '!range'");
1632 if (expectAndConsume(MIToken::rparen))
1635 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1639 void MIParser::initNames2InstrOpCodes() {
1640 if (!Names2InstrOpCodes.empty())
1642 const auto *TII = MF.getSubtarget().getInstrInfo();
1643 assert(TII && "Expected target instruction info");
1644 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1645 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1648 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1649 initNames2InstrOpCodes();
1650 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1651 if (InstrInfo == Names2InstrOpCodes.end())
1653 OpCode = InstrInfo->getValue();
1657 void MIParser::initNames2Regs() {
1658 if (!Names2Regs.empty())
1660 // The '%noreg' register is the register 0.
1661 Names2Regs.insert(std::make_pair("noreg", 0));
1662 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1663 assert(TRI && "Expected target register info");
1664 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1666 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1669 assert(WasInserted && "Expected registers to be unique case-insensitively");
1673 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1675 auto RegInfo = Names2Regs.find(RegName);
1676 if (RegInfo == Names2Regs.end())
1678 Reg = RegInfo->getValue();
1682 void MIParser::initNames2RegMasks() {
1683 if (!Names2RegMasks.empty())
1685 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1686 assert(TRI && "Expected target register info");
1687 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1688 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1689 assert(RegMasks.size() == RegMaskNames.size());
1690 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1691 Names2RegMasks.insert(
1692 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1695 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1696 initNames2RegMasks();
1697 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1698 if (RegMaskInfo == Names2RegMasks.end())
1700 return RegMaskInfo->getValue();
1703 void MIParser::initNames2SubRegIndices() {
1704 if (!Names2SubRegIndices.empty())
1706 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1707 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1708 Names2SubRegIndices.insert(
1709 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1712 unsigned MIParser::getSubRegIndex(StringRef Name) {
1713 initNames2SubRegIndices();
1714 auto SubRegInfo = Names2SubRegIndices.find(Name);
1715 if (SubRegInfo == Names2SubRegIndices.end())
1717 return SubRegInfo->getValue();
1720 static void initSlots2BasicBlocks(
1722 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1723 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1724 MST.incorporateFunction(F);
1725 for (auto &BB : F) {
1728 int Slot = MST.getLocalSlot(&BB);
1731 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1735 static const BasicBlock *getIRBlockFromSlot(
1737 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1738 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1739 if (BlockInfo == Slots2BasicBlocks.end())
1741 return BlockInfo->second;
1744 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1745 if (Slots2BasicBlocks.empty())
1746 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1747 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1750 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1751 if (&F == MF.getFunction())
1752 return getIRBlock(Slot);
1753 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1754 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1755 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1758 void MIParser::initNames2TargetIndices() {
1759 if (!Names2TargetIndices.empty())
1761 const auto *TII = MF.getSubtarget().getInstrInfo();
1762 assert(TII && "Expected target instruction info");
1763 auto Indices = TII->getSerializableTargetIndices();
1764 for (const auto &I : Indices)
1765 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1768 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1769 initNames2TargetIndices();
1770 auto IndexInfo = Names2TargetIndices.find(Name);
1771 if (IndexInfo == Names2TargetIndices.end())
1773 Index = IndexInfo->second;
1777 void MIParser::initNames2DirectTargetFlags() {
1778 if (!Names2DirectTargetFlags.empty())
1780 const auto *TII = MF.getSubtarget().getInstrInfo();
1781 assert(TII && "Expected target instruction info");
1782 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1783 for (const auto &I : Flags)
1784 Names2DirectTargetFlags.insert(
1785 std::make_pair(StringRef(I.second), I.first));
1788 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1789 initNames2DirectTargetFlags();
1790 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1791 if (FlagInfo == Names2DirectTargetFlags.end())
1793 Flag = FlagInfo->second;
1797 void MIParser::initNames2BitmaskTargetFlags() {
1798 if (!Names2BitmaskTargetFlags.empty())
1800 const auto *TII = MF.getSubtarget().getInstrInfo();
1801 assert(TII && "Expected target instruction info");
1802 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1803 for (const auto &I : Flags)
1804 Names2BitmaskTargetFlags.insert(
1805 std::make_pair(StringRef(I.second), I.first));
1808 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1809 initNames2BitmaskTargetFlags();
1810 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1811 if (FlagInfo == Names2BitmaskTargetFlags.end())
1813 Flag = FlagInfo->second;
1817 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1818 PerFunctionMIParsingState &PFS,
1819 const SlotMapping &IRSlots,
1820 SMDiagnostic &Error) {
1822 SM.AddNewSourceBuffer(
1823 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1825 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1826 .parseBasicBlockDefinitions(PFS.MBBSlots);
1829 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1830 const PerFunctionMIParsingState &PFS,
1831 const SlotMapping &IRSlots,
1832 SMDiagnostic &Error) {
1834 SM.AddNewSourceBuffer(
1835 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1837 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
1840 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1841 MachineFunction &MF, StringRef Src,
1842 const PerFunctionMIParsingState &PFS,
1843 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1844 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
1847 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1848 MachineFunction &MF, StringRef Src,
1849 const PerFunctionMIParsingState &PFS,
1850 const SlotMapping &IRSlots,
1851 SMDiagnostic &Error) {
1852 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1853 .parseStandaloneNamedRegister(Reg);
1856 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1857 MachineFunction &MF, StringRef Src,
1858 const PerFunctionMIParsingState &PFS,
1859 const SlotMapping &IRSlots,
1860 SMDiagnostic &Error) {
1861 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1862 .parseStandaloneVirtualRegister(Reg);
1865 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
1866 MachineFunction &MF, StringRef Src,
1867 const PerFunctionMIParsingState &PFS,
1868 const SlotMapping &IRSlots,
1869 SMDiagnostic &Error) {
1870 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1871 .parseStandaloneStackObject(FI);
1874 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
1875 StringRef Src, const PerFunctionMIParsingState &PFS,
1876 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1877 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);