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;
46 Optional<unsigned> TiedDefIdx;
48 MachineOperandWithLocation(const MachineOperand &Operand,
49 StringRef::iterator Begin, StringRef::iterator End,
50 Optional<unsigned> &TiedDefIdx)
51 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
53 assert(Operand.isReg() && Operand.isUse() &&
54 "Only used register operands can be tied");
62 StringRef Source, CurrentSource;
64 const PerFunctionMIParsingState &PFS;
65 /// Maps from indices to unnamed global values and metadata nodes.
66 const SlotMapping &IRSlots;
67 /// Maps from instruction names to op codes.
68 StringMap<unsigned> Names2InstrOpCodes;
69 /// Maps from register names to registers.
70 StringMap<unsigned> Names2Regs;
71 /// Maps from register mask names to register masks.
72 StringMap<const uint32_t *> Names2RegMasks;
73 /// Maps from subregister names to subregister indices.
74 StringMap<unsigned> Names2SubRegIndices;
75 /// Maps from slot numbers to function's unnamed basic blocks.
76 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
77 /// Maps from target index names to target indices.
78 StringMap<int> Names2TargetIndices;
79 /// Maps from direct target flag names to the direct target flag values.
80 StringMap<unsigned> Names2DirectTargetFlags;
81 /// Maps from direct target flag names to the bitmask target flag values.
82 StringMap<unsigned> Names2BitmaskTargetFlags;
85 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
86 StringRef Source, const PerFunctionMIParsingState &PFS,
87 const SlotMapping &IRSlots);
91 /// Report an error at the current location with the given message.
93 /// This function always return true.
94 bool error(const Twine &Msg);
96 /// Report an error at the given location with the given message.
98 /// This function always return true.
99 bool error(StringRef::iterator Loc, const Twine &Msg);
102 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
103 bool parseBasicBlocks();
104 bool parse(MachineInstr *&MI);
105 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
106 bool parseStandaloneNamedRegister(unsigned &Reg);
107 bool parseStandaloneVirtualRegister(unsigned &Reg);
108 bool parseStandaloneStackObject(int &FI);
109 bool parseStandaloneMDNode(MDNode *&Node);
112 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
113 bool parseBasicBlock(MachineBasicBlock &MBB);
114 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
115 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
117 bool parseRegister(unsigned &Reg);
118 bool parseRegisterFlag(unsigned &Flags);
119 bool parseSubRegisterIndex(unsigned &SubReg);
120 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
121 bool parseRegisterOperand(MachineOperand &Dest,
122 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
123 bool parseImmediateOperand(MachineOperand &Dest);
124 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
125 bool parseTypedImmediateOperand(MachineOperand &Dest);
126 bool parseFPImmediateOperand(MachineOperand &Dest);
127 bool parseMBBReference(MachineBasicBlock *&MBB);
128 bool parseMBBOperand(MachineOperand &Dest);
129 bool parseStackFrameIndex(int &FI);
130 bool parseStackObjectOperand(MachineOperand &Dest);
131 bool parseFixedStackFrameIndex(int &FI);
132 bool parseFixedStackObjectOperand(MachineOperand &Dest);
133 bool parseGlobalValue(GlobalValue *&GV);
134 bool parseGlobalAddressOperand(MachineOperand &Dest);
135 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
136 bool parseJumpTableIndexOperand(MachineOperand &Dest);
137 bool parseExternalSymbolOperand(MachineOperand &Dest);
138 bool parseMDNode(MDNode *&Node);
139 bool parseMetadataOperand(MachineOperand &Dest);
140 bool parseCFIOffset(int &Offset);
141 bool parseCFIRegister(unsigned &Reg);
142 bool parseCFIOperand(MachineOperand &Dest);
143 bool parseIRBlock(BasicBlock *&BB, const Function &F);
144 bool parseBlockAddressOperand(MachineOperand &Dest);
145 bool parseTargetIndexOperand(MachineOperand &Dest);
146 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
147 bool parseMachineOperand(MachineOperand &Dest,
148 Optional<unsigned> &TiedDefIdx);
149 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
150 Optional<unsigned> &TiedDefIdx);
151 bool parseOffset(int64_t &Offset);
152 bool parseAlignment(unsigned &Alignment);
153 bool parseOperandsOffset(MachineOperand &Op);
154 bool parseIRValue(Value *&V);
155 bool parseMemoryOperandFlag(unsigned &Flags);
156 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
157 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
158 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
161 /// Convert the integer literal in the current token into an unsigned integer.
163 /// Return true if an error occurred.
164 bool getUnsigned(unsigned &Result);
166 /// Convert the integer literal in the current token into an uint64.
168 /// Return true if an error occurred.
169 bool getUint64(uint64_t &Result);
171 /// If the current token is of the given kind, consume it and return false.
172 /// Otherwise report an error and return true.
173 bool expectAndConsume(MIToken::TokenKind TokenKind);
175 /// If the current token is of the given kind, consume it and return true.
176 /// Otherwise return false.
177 bool consumeIfPresent(MIToken::TokenKind TokenKind);
179 void initNames2InstrOpCodes();
181 /// Try to convert an instruction name to an opcode. Return true if the
182 /// instruction name is invalid.
183 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
185 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
187 bool assignRegisterTies(MachineInstr &MI,
188 ArrayRef<MachineOperandWithLocation> Operands);
190 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
191 const MCInstrDesc &MCID);
193 void initNames2Regs();
195 /// Try to convert a register name to a register number. Return true if the
196 /// register name is invalid.
197 bool getRegisterByName(StringRef RegName, unsigned &Reg);
199 void initNames2RegMasks();
201 /// Check if the given identifier is a name of a register mask.
203 /// Return null if the identifier isn't a register mask.
204 const uint32_t *getRegMask(StringRef Identifier);
206 void initNames2SubRegIndices();
208 /// Check if the given identifier is a name of a subregister index.
210 /// Return 0 if the name isn't a subregister index class.
211 unsigned getSubRegIndex(StringRef Name);
213 const BasicBlock *getIRBlock(unsigned Slot);
214 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
216 void initNames2TargetIndices();
218 /// Try to convert a name of target index to the corresponding target index.
220 /// Return true if the name isn't a name of a target index.
221 bool getTargetIndex(StringRef Name, int &Index);
223 void initNames2DirectTargetFlags();
225 /// Try to convert a name of a direct target flag to the corresponding
228 /// Return true if the name isn't a name of a direct flag.
229 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
231 void initNames2BitmaskTargetFlags();
233 /// Try to convert a name of a bitmask target flag to the corresponding
236 /// Return true if the name isn't a name of a bitmask target flag.
237 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
240 } // end anonymous namespace
242 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
243 StringRef Source, const PerFunctionMIParsingState &PFS,
244 const SlotMapping &IRSlots)
245 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
246 PFS(PFS), IRSlots(IRSlots) {}
248 void MIParser::lex() {
249 CurrentSource = lexMIToken(
250 CurrentSource, Token,
251 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
254 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
256 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
257 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
258 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
259 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
260 // Create an ordinary diagnostic when the source manager's buffer is the
262 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
265 // Create a diagnostic for a YAML string literal.
266 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
267 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
272 static const char *toString(MIToken::TokenKind TokenKind) {
280 case MIToken::lparen:
282 case MIToken::rparen:
285 return "<unknown token>";
289 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
290 if (Token.isNot(TokenKind))
291 return error(Twine("expected ") + toString(TokenKind));
296 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
297 if (Token.isNot(TokenKind))
303 bool MIParser::parseBasicBlockDefinition(
304 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
305 assert(Token.is(MIToken::MachineBasicBlockLabel));
309 auto Loc = Token.location();
310 auto Name = Token.stringValue();
312 bool HasAddressTaken = false;
313 bool IsLandingPad = false;
314 unsigned Alignment = 0;
315 BasicBlock *BB = nullptr;
316 if (consumeIfPresent(MIToken::lparen)) {
318 // TODO: Report an error when multiple same attributes are specified.
319 switch (Token.kind()) {
320 case MIToken::kw_address_taken:
321 HasAddressTaken = true;
324 case MIToken::kw_landing_pad:
328 case MIToken::kw_align:
329 if (parseAlignment(Alignment))
332 case MIToken::IRBlock:
333 // TODO: Report an error when both name and ir block are specified.
334 if (parseIRBlock(BB, *MF.getFunction()))
341 } while (consumeIfPresent(MIToken::comma));
342 if (expectAndConsume(MIToken::rparen))
345 if (expectAndConsume(MIToken::colon))
349 BB = dyn_cast_or_null<BasicBlock>(
350 MF.getFunction()->getValueSymbolTable().lookup(Name));
352 return error(Loc, Twine("basic block '") + Name +
353 "' is not defined in the function '" +
356 auto *MBB = MF.CreateMachineBasicBlock(BB);
357 MF.insert(MF.end(), MBB);
358 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
360 return error(Loc, Twine("redefinition of machine basic block with id #") +
363 MBB->setAlignment(Alignment);
365 MBB->setHasAddressTaken();
366 MBB->setIsLandingPad(IsLandingPad);
370 bool MIParser::parseBasicBlockDefinitions(
371 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
373 // Skip until the first machine basic block.
374 while (Token.is(MIToken::Newline))
376 if (Token.isErrorOrEOF())
377 return Token.isError();
378 if (Token.isNot(MIToken::MachineBasicBlockLabel))
379 return error("expected a basic block definition before instructions");
380 unsigned BraceDepth = 0;
382 if (parseBasicBlockDefinition(MBBSlots))
384 bool IsAfterNewline = false;
385 // Skip until the next machine basic block.
387 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
388 Token.isErrorOrEOF())
390 else if (Token.is(MIToken::MachineBasicBlockLabel))
391 return error("basic block definition should be located at the start of "
393 else if (consumeIfPresent(MIToken::Newline)) {
394 IsAfterNewline = true;
397 IsAfterNewline = false;
398 if (Token.is(MIToken::lbrace))
400 if (Token.is(MIToken::rbrace)) {
402 return error("extraneous closing brace ('}')");
407 // Verify that we closed all of the '{' at the end of a file or a block.
408 if (!Token.isError() && BraceDepth)
409 return error("expected '}'"); // FIXME: Report a note that shows '{'.
410 } while (!Token.isErrorOrEOF());
411 return Token.isError();
414 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
415 assert(Token.is(MIToken::kw_liveins));
417 if (expectAndConsume(MIToken::colon))
419 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
422 if (Token.isNot(MIToken::NamedRegister))
423 return error("expected a named register");
425 if (parseRegister(Reg))
429 } while (consumeIfPresent(MIToken::comma));
433 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
434 assert(Token.is(MIToken::kw_successors));
436 if (expectAndConsume(MIToken::colon))
438 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
441 if (Token.isNot(MIToken::MachineBasicBlock))
442 return error("expected a machine basic block reference");
443 MachineBasicBlock *SuccMBB = nullptr;
444 if (parseMBBReference(SuccMBB))
448 if (consumeIfPresent(MIToken::lparen)) {
449 if (Token.isNot(MIToken::IntegerLiteral))
450 return error("expected an integer literal after '('");
451 if (getUnsigned(Weight))
454 if (expectAndConsume(MIToken::rparen))
457 MBB.addSuccessor(SuccMBB, Weight);
458 } while (consumeIfPresent(MIToken::comma));
462 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
463 // Skip the definition.
464 assert(Token.is(MIToken::MachineBasicBlockLabel));
466 if (consumeIfPresent(MIToken::lparen)) {
467 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
469 consumeIfPresent(MIToken::rparen);
471 consumeIfPresent(MIToken::colon);
473 // Parse the liveins and successors.
474 // N.B: Multiple lists of successors and liveins are allowed and they're
481 // liveins: %edi, %esi
483 if (Token.is(MIToken::kw_successors)) {
484 if (parseBasicBlockSuccessors(MBB))
486 } else if (Token.is(MIToken::kw_liveins)) {
487 if (parseBasicBlockLiveins(MBB))
489 } else if (consumeIfPresent(MIToken::Newline)) {
493 if (!Token.isNewlineOrEOF())
494 return error("expected line break at the end of a list");
498 // Parse the instructions.
499 bool IsInBundle = false;
500 MachineInstr *PrevMI = nullptr;
502 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
504 else if (consumeIfPresent(MIToken::Newline))
506 if (consumeIfPresent(MIToken::rbrace)) {
507 // The first parsing pass should verify that all closing '}' have an
513 MachineInstr *MI = nullptr;
516 MBB.insert(MBB.end(), MI);
518 PrevMI->setFlag(MachineInstr::BundledSucc);
519 MI->setFlag(MachineInstr::BundledPred);
522 if (Token.is(MIToken::lbrace)) {
524 return error("nested instruction bundles are not allowed");
526 // This instruction is the start of the bundle.
527 MI->setFlag(MachineInstr::BundledSucc);
529 if (!Token.is(MIToken::Newline))
530 // The next instruction can be on the same line.
533 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
539 bool MIParser::parseBasicBlocks() {
541 // Skip until the first machine basic block.
542 while (Token.is(MIToken::Newline))
544 if (Token.isErrorOrEOF())
545 return Token.isError();
546 // The first parsing pass should have verified that this token is a MBB label
547 // in the 'parseBasicBlockDefinitions' method.
548 assert(Token.is(MIToken::MachineBasicBlockLabel));
550 MachineBasicBlock *MBB = nullptr;
551 if (parseMBBReference(MBB))
553 if (parseBasicBlock(*MBB))
555 // The method 'parseBasicBlock' should parse the whole block until the next
556 // block or the end of file.
557 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
558 } while (Token.isNot(MIToken::Eof));
562 bool MIParser::parse(MachineInstr *&MI) {
563 // Parse any register operands before '='
564 MachineOperand MO = MachineOperand::CreateImm(0);
565 SmallVector<MachineOperandWithLocation, 8> Operands;
566 while (Token.isRegister() || Token.isRegisterFlag()) {
567 auto Loc = Token.location();
568 Optional<unsigned> TiedDefIdx;
569 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
572 MachineOperandWithLocation(MO, Loc, Token.location(), TiedDefIdx));
573 if (Token.isNot(MIToken::comma))
577 if (!Operands.empty() && expectAndConsume(MIToken::equal))
580 unsigned OpCode, Flags = 0;
581 if (Token.isError() || parseInstruction(OpCode, Flags))
584 // Parse the remaining machine operands.
585 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
586 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
587 auto Loc = Token.location();
588 Optional<unsigned> TiedDefIdx;
589 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
592 MachineOperandWithLocation(MO, Loc, Token.location(), TiedDefIdx));
593 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
594 Token.is(MIToken::lbrace))
596 if (Token.isNot(MIToken::comma))
597 return error("expected ',' before the next machine operand");
601 DebugLoc DebugLocation;
602 if (Token.is(MIToken::kw_debug_location)) {
604 if (Token.isNot(MIToken::exclaim))
605 return error("expected a metadata node after 'debug-location'");
606 MDNode *Node = nullptr;
607 if (parseMDNode(Node))
609 DebugLocation = DebugLoc(Node);
612 // Parse the machine memory operands.
613 SmallVector<MachineMemOperand *, 2> MemOperands;
614 if (Token.is(MIToken::coloncolon)) {
616 while (!Token.isNewlineOrEOF()) {
617 MachineMemOperand *MemOp = nullptr;
618 if (parseMachineMemoryOperand(MemOp))
620 MemOperands.push_back(MemOp);
621 if (Token.isNewlineOrEOF())
623 if (Token.isNot(MIToken::comma))
624 return error("expected ',' before the next machine memory operand");
629 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
630 if (!MCID.isVariadic()) {
631 // FIXME: Move the implicit operand verification to the machine verifier.
632 if (verifyImplicitOperands(Operands, MCID))
636 // TODO: Check for extraneous machine operands.
637 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
639 for (const auto &Operand : Operands)
640 MI->addOperand(MF, Operand.Operand);
641 if (assignRegisterTies(*MI, Operands))
643 if (MemOperands.empty())
645 MachineInstr::mmo_iterator MemRefs =
646 MF.allocateMemRefsArray(MemOperands.size());
647 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
648 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
652 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
654 if (Token.isNot(MIToken::MachineBasicBlock))
655 return error("expected a machine basic block reference");
656 if (parseMBBReference(MBB))
659 if (Token.isNot(MIToken::Eof))
661 "expected end of string after the machine basic block reference");
665 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
667 if (Token.isNot(MIToken::NamedRegister))
668 return error("expected a named register");
669 if (parseRegister(Reg))
672 if (Token.isNot(MIToken::Eof))
673 return error("expected end of string after the register reference");
677 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
679 if (Token.isNot(MIToken::VirtualRegister))
680 return error("expected a virtual register");
681 if (parseRegister(Reg))
684 if (Token.isNot(MIToken::Eof))
685 return error("expected end of string after the register reference");
689 bool MIParser::parseStandaloneStackObject(int &FI) {
691 if (Token.isNot(MIToken::StackObject))
692 return error("expected a stack object");
693 if (parseStackFrameIndex(FI))
695 if (Token.isNot(MIToken::Eof))
696 return error("expected end of string after the stack object reference");
700 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
702 if (Token.isNot(MIToken::exclaim))
703 return error("expected a metadata node");
704 if (parseMDNode(Node))
706 if (Token.isNot(MIToken::Eof))
707 return error("expected end of string after the metadata node");
711 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
712 assert(MO.isImplicit());
713 return MO.isDef() ? "implicit-def" : "implicit";
716 static std::string getRegisterName(const TargetRegisterInfo *TRI,
718 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
719 return StringRef(TRI->getName(Reg)).lower();
722 bool MIParser::verifyImplicitOperands(
723 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
725 // We can't verify call instructions as they can contain arbitrary implicit
726 // register and register mask operands.
729 // Gather all the expected implicit operands.
730 SmallVector<MachineOperand, 4> ImplicitOperands;
731 if (MCID.ImplicitDefs)
732 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
733 ImplicitOperands.push_back(
734 MachineOperand::CreateReg(*ImpDefs, true, true));
735 if (MCID.ImplicitUses)
736 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
737 ImplicitOperands.push_back(
738 MachineOperand::CreateReg(*ImpUses, false, true));
740 const auto *TRI = MF.getSubtarget().getRegisterInfo();
741 assert(TRI && "Expected target register info");
742 size_t I = ImplicitOperands.size(), J = Operands.size();
747 const auto &ImplicitOperand = ImplicitOperands[I];
748 const auto &Operand = Operands[J].Operand;
749 if (ImplicitOperand.isIdenticalTo(Operand))
751 if (Operand.isReg() && Operand.isImplicit()) {
752 // Check if this implicit register is a subregister of an explicit
754 bool IsImplicitSubRegister = false;
755 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
756 const auto &Op = Operands[K].Operand;
757 if (Op.isReg() && !Op.isImplicit() &&
758 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
759 IsImplicitSubRegister = true;
763 if (IsImplicitSubRegister)
765 return error(Operands[J].Begin,
766 Twine("expected an implicit register operand '") +
767 printImplicitRegisterFlag(ImplicitOperand) + " %" +
768 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
771 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
772 // insead of reporting an error at this location:
775 // report the error at the following location:
778 return error(J < Operands.size() ? Operands[J].End : Token.location(),
779 Twine("missing implicit register operand '") +
780 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
781 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
786 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
787 if (Token.is(MIToken::kw_frame_setup)) {
788 Flags |= MachineInstr::FrameSetup;
791 if (Token.isNot(MIToken::Identifier))
792 return error("expected a machine instruction");
793 StringRef InstrName = Token.stringValue();
794 if (parseInstrName(InstrName, OpCode))
795 return error(Twine("unknown machine instruction name '") + InstrName + "'");
800 bool MIParser::parseRegister(unsigned &Reg) {
801 switch (Token.kind()) {
802 case MIToken::underscore:
805 case MIToken::NamedRegister: {
806 StringRef Name = Token.stringValue();
807 if (getRegisterByName(Name, Reg))
808 return error(Twine("unknown register name '") + Name + "'");
811 case MIToken::VirtualRegister: {
815 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
816 if (RegInfo == PFS.VirtualRegisterSlots.end())
817 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
819 Reg = RegInfo->second;
822 // TODO: Parse other register kinds.
824 llvm_unreachable("The current token should be a register");
829 bool MIParser::parseRegisterFlag(unsigned &Flags) {
830 const unsigned OldFlags = Flags;
831 switch (Token.kind()) {
832 case MIToken::kw_implicit:
833 Flags |= RegState::Implicit;
835 case MIToken::kw_implicit_define:
836 Flags |= RegState::ImplicitDefine;
838 case MIToken::kw_def:
839 Flags |= RegState::Define;
841 case MIToken::kw_dead:
842 Flags |= RegState::Dead;
844 case MIToken::kw_killed:
845 Flags |= RegState::Kill;
847 case MIToken::kw_undef:
848 Flags |= RegState::Undef;
850 case MIToken::kw_internal:
851 Flags |= RegState::InternalRead;
853 case MIToken::kw_early_clobber:
854 Flags |= RegState::EarlyClobber;
856 case MIToken::kw_debug_use:
857 Flags |= RegState::Debug;
860 llvm_unreachable("The current token should be a register flag");
862 if (OldFlags == Flags)
863 // We know that the same flag is specified more than once when the flags
865 return error("duplicate '" + Token.stringValue() + "' register flag");
870 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
871 assert(Token.is(MIToken::colon));
873 if (Token.isNot(MIToken::Identifier))
874 return error("expected a subregister index after ':'");
875 auto Name = Token.stringValue();
876 SubReg = getSubRegIndex(Name);
878 return error(Twine("use of unknown subregister index '") + Name + "'");
883 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
884 if (!consumeIfPresent(MIToken::kw_tied_def))
885 return error("expected 'tied-def' after '('");
886 if (Token.isNot(MIToken::IntegerLiteral))
887 return error("expected an integer literal after 'tied-def'");
888 if (getUnsigned(TiedDefIdx))
891 if (expectAndConsume(MIToken::rparen))
896 bool MIParser::assignRegisterTies(
897 MachineInstr &MI, ArrayRef<MachineOperandWithLocation> Operands) {
898 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
899 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
900 if (!Operands[I].TiedDefIdx)
902 // The parser ensures that this operand is a register use, so we just have
903 // to check the tied-def operand.
904 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
906 return error(Operands[I].Begin,
907 Twine("use of invalid tied-def operand index '" +
908 Twine(DefIdx) + "'; instruction has only ") +
909 Twine(E) + " operands");
910 const auto &DefOperand = Operands[DefIdx].Operand;
911 if (!DefOperand.isReg() || !DefOperand.isDef())
912 // FIXME: add note with the def operand.
913 return error(Operands[I].Begin,
914 Twine("use of invalid tied-def operand index '") +
915 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
916 " isn't a defined register");
917 // Check that the tied-def operand wasn't tied elsewhere.
918 for (const auto &TiedPair : TiedRegisterPairs) {
919 if (TiedPair.first == DefIdx)
920 return error(Operands[I].Begin,
921 Twine("the tied-def operand #") + Twine(DefIdx) +
922 " is already tied with another register operand");
924 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
926 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
927 // indices must be less than tied max.
928 for (const auto &TiedPair : TiedRegisterPairs)
929 MI.tieOperands(TiedPair.first, TiedPair.second);
933 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
934 Optional<unsigned> &TiedDefIdx,
937 unsigned Flags = IsDef ? RegState::Define : 0;
938 while (Token.isRegisterFlag()) {
939 if (parseRegisterFlag(Flags))
942 if (!Token.isRegister())
943 return error("expected a register after register flags");
944 if (parseRegister(Reg))
948 if (Token.is(MIToken::colon)) {
949 if (parseSubRegisterIndex(SubReg))
952 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
954 if (parseRegisterTiedDefIndex(Idx))
958 Dest = MachineOperand::CreateReg(
959 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
960 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
961 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
962 Flags & RegState::InternalRead);
966 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
967 assert(Token.is(MIToken::IntegerLiteral));
968 const APSInt &Int = Token.integerValue();
969 if (Int.getMinSignedBits() > 64)
970 return error("integer literal is too large to be an immediate operand");
971 Dest = MachineOperand::CreateImm(Int.getExtValue());
976 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
977 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
980 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
982 return error(Loc + Err.getColumnNo(), Err.getMessage());
986 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
987 assert(Token.is(MIToken::IntegerType));
988 auto Loc = Token.location();
990 if (Token.isNot(MIToken::IntegerLiteral))
991 return error("expected an integer literal");
992 const Constant *C = nullptr;
993 if (parseIRConstant(Loc, C))
995 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
999 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1000 auto Loc = Token.location();
1002 if (Token.isNot(MIToken::FloatingPointLiteral))
1003 return error("expected a floating point literal");
1004 const Constant *C = nullptr;
1005 if (parseIRConstant(Loc, C))
1007 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1011 bool MIParser::getUnsigned(unsigned &Result) {
1012 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1013 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1014 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1016 return error("expected 32-bit integer (too large)");
1021 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1022 assert(Token.is(MIToken::MachineBasicBlock) ||
1023 Token.is(MIToken::MachineBasicBlockLabel));
1025 if (getUnsigned(Number))
1027 auto MBBInfo = PFS.MBBSlots.find(Number);
1028 if (MBBInfo == PFS.MBBSlots.end())
1029 return error(Twine("use of undefined machine basic block #") +
1031 MBB = MBBInfo->second;
1032 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1033 return error(Twine("the name of machine basic block #") + Twine(Number) +
1034 " isn't '" + Token.stringValue() + "'");
1038 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1039 MachineBasicBlock *MBB;
1040 if (parseMBBReference(MBB))
1042 Dest = MachineOperand::CreateMBB(MBB);
1047 bool MIParser::parseStackFrameIndex(int &FI) {
1048 assert(Token.is(MIToken::StackObject));
1050 if (getUnsigned(ID))
1052 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1053 if (ObjectInfo == PFS.StackObjectSlots.end())
1054 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1057 if (const auto *Alloca =
1058 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1059 Name = Alloca->getName();
1060 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1061 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1062 "' isn't '" + Token.stringValue() + "'");
1064 FI = ObjectInfo->second;
1068 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1070 if (parseStackFrameIndex(FI))
1072 Dest = MachineOperand::CreateFI(FI);
1076 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1077 assert(Token.is(MIToken::FixedStackObject));
1079 if (getUnsigned(ID))
1081 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1082 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1083 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1086 FI = ObjectInfo->second;
1090 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1092 if (parseFixedStackFrameIndex(FI))
1094 Dest = MachineOperand::CreateFI(FI);
1098 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1099 switch (Token.kind()) {
1100 case MIToken::NamedGlobalValue: {
1101 const Module *M = MF.getFunction()->getParent();
1102 GV = M->getNamedValue(Token.stringValue());
1104 return error(Twine("use of undefined global value '") + Token.range() +
1108 case MIToken::GlobalValue: {
1110 if (getUnsigned(GVIdx))
1112 if (GVIdx >= IRSlots.GlobalValues.size())
1113 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1115 GV = IRSlots.GlobalValues[GVIdx];
1119 llvm_unreachable("The current token should be a global value");
1124 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1125 GlobalValue *GV = nullptr;
1126 if (parseGlobalValue(GV))
1129 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1130 if (parseOperandsOffset(Dest))
1135 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1136 assert(Token.is(MIToken::ConstantPoolItem));
1138 if (getUnsigned(ID))
1140 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1141 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1142 return error("use of undefined constant '%const." + Twine(ID) + "'");
1144 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1145 if (parseOperandsOffset(Dest))
1150 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1151 assert(Token.is(MIToken::JumpTableIndex));
1153 if (getUnsigned(ID))
1155 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1156 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1157 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1159 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1163 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1164 assert(Token.is(MIToken::ExternalSymbol));
1165 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1167 Dest = MachineOperand::CreateES(Symbol);
1168 if (parseOperandsOffset(Dest))
1173 bool MIParser::parseMDNode(MDNode *&Node) {
1174 assert(Token.is(MIToken::exclaim));
1175 auto Loc = Token.location();
1177 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1178 return error("expected metadata id after '!'");
1180 if (getUnsigned(ID))
1182 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1183 if (NodeInfo == IRSlots.MetadataNodes.end())
1184 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1186 Node = NodeInfo->second.get();
1190 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1191 MDNode *Node = nullptr;
1192 if (parseMDNode(Node))
1194 Dest = MachineOperand::CreateMetadata(Node);
1198 bool MIParser::parseCFIOffset(int &Offset) {
1199 if (Token.isNot(MIToken::IntegerLiteral))
1200 return error("expected a cfi offset");
1201 if (Token.integerValue().getMinSignedBits() > 32)
1202 return error("expected a 32 bit integer (the cfi offset is too large)");
1203 Offset = (int)Token.integerValue().getExtValue();
1208 bool MIParser::parseCFIRegister(unsigned &Reg) {
1209 if (Token.isNot(MIToken::NamedRegister))
1210 return error("expected a cfi register");
1212 if (parseRegister(LLVMReg))
1214 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1215 assert(TRI && "Expected target register info");
1216 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1218 return error("invalid DWARF register");
1219 Reg = (unsigned)DwarfReg;
1224 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1225 auto Kind = Token.kind();
1227 auto &MMI = MF.getMMI();
1232 case MIToken::kw_cfi_same_value:
1233 if (parseCFIRegister(Reg))
1236 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1238 case MIToken::kw_cfi_offset:
1239 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1240 parseCFIOffset(Offset))
1243 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1245 case MIToken::kw_cfi_def_cfa_register:
1246 if (parseCFIRegister(Reg))
1249 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1251 case MIToken::kw_cfi_def_cfa_offset:
1252 if (parseCFIOffset(Offset))
1254 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1255 CFIIndex = MMI.addFrameInst(
1256 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1258 case MIToken::kw_cfi_def_cfa:
1259 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1260 parseCFIOffset(Offset))
1262 // NB: MCCFIInstruction::createDefCfa negates the offset.
1264 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1267 // TODO: Parse the other CFI operands.
1268 llvm_unreachable("The current token should be a cfi operand");
1270 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1274 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1275 switch (Token.kind()) {
1276 case MIToken::NamedIRBlock: {
1277 BB = dyn_cast_or_null<BasicBlock>(
1278 F.getValueSymbolTable().lookup(Token.stringValue()));
1280 return error(Twine("use of undefined IR block '") + Token.range() + "'");
1283 case MIToken::IRBlock: {
1284 unsigned SlotNumber = 0;
1285 if (getUnsigned(SlotNumber))
1287 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1289 return error(Twine("use of undefined IR block '%ir-block.") +
1290 Twine(SlotNumber) + "'");
1294 llvm_unreachable("The current token should be an IR block reference");
1299 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1300 assert(Token.is(MIToken::kw_blockaddress));
1302 if (expectAndConsume(MIToken::lparen))
1304 if (Token.isNot(MIToken::GlobalValue) &&
1305 Token.isNot(MIToken::NamedGlobalValue))
1306 return error("expected a global value");
1307 GlobalValue *GV = nullptr;
1308 if (parseGlobalValue(GV))
1310 auto *F = dyn_cast<Function>(GV);
1312 return error("expected an IR function reference");
1314 if (expectAndConsume(MIToken::comma))
1316 BasicBlock *BB = nullptr;
1317 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1318 return error("expected an IR block reference");
1319 if (parseIRBlock(BB, *F))
1322 if (expectAndConsume(MIToken::rparen))
1324 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1325 if (parseOperandsOffset(Dest))
1330 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1331 assert(Token.is(MIToken::kw_target_index));
1333 if (expectAndConsume(MIToken::lparen))
1335 if (Token.isNot(MIToken::Identifier))
1336 return error("expected the name of the target index");
1338 if (getTargetIndex(Token.stringValue(), Index))
1339 return error("use of undefined target index '" + Token.stringValue() + "'");
1341 if (expectAndConsume(MIToken::rparen))
1343 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1344 if (parseOperandsOffset(Dest))
1349 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1350 assert(Token.is(MIToken::kw_liveout));
1351 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1352 assert(TRI && "Expected target register info");
1353 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1355 if (expectAndConsume(MIToken::lparen))
1358 if (Token.isNot(MIToken::NamedRegister))
1359 return error("expected a named register");
1361 if (parseRegister(Reg))
1364 Mask[Reg / 32] |= 1U << (Reg % 32);
1365 // TODO: Report an error if the same register is used more than once.
1366 if (Token.isNot(MIToken::comma))
1370 if (expectAndConsume(MIToken::rparen))
1372 Dest = MachineOperand::CreateRegLiveOut(Mask);
1376 bool MIParser::parseMachineOperand(MachineOperand &Dest,
1377 Optional<unsigned> &TiedDefIdx) {
1378 switch (Token.kind()) {
1379 case MIToken::kw_implicit:
1380 case MIToken::kw_implicit_define:
1381 case MIToken::kw_def:
1382 case MIToken::kw_dead:
1383 case MIToken::kw_killed:
1384 case MIToken::kw_undef:
1385 case MIToken::kw_internal:
1386 case MIToken::kw_early_clobber:
1387 case MIToken::kw_debug_use:
1388 case MIToken::underscore:
1389 case MIToken::NamedRegister:
1390 case MIToken::VirtualRegister:
1391 return parseRegisterOperand(Dest, TiedDefIdx);
1392 case MIToken::IntegerLiteral:
1393 return parseImmediateOperand(Dest);
1394 case MIToken::IntegerType:
1395 return parseTypedImmediateOperand(Dest);
1396 case MIToken::kw_half:
1397 case MIToken::kw_float:
1398 case MIToken::kw_double:
1399 case MIToken::kw_x86_fp80:
1400 case MIToken::kw_fp128:
1401 case MIToken::kw_ppc_fp128:
1402 return parseFPImmediateOperand(Dest);
1403 case MIToken::MachineBasicBlock:
1404 return parseMBBOperand(Dest);
1405 case MIToken::StackObject:
1406 return parseStackObjectOperand(Dest);
1407 case MIToken::FixedStackObject:
1408 return parseFixedStackObjectOperand(Dest);
1409 case MIToken::GlobalValue:
1410 case MIToken::NamedGlobalValue:
1411 return parseGlobalAddressOperand(Dest);
1412 case MIToken::ConstantPoolItem:
1413 return parseConstantPoolIndexOperand(Dest);
1414 case MIToken::JumpTableIndex:
1415 return parseJumpTableIndexOperand(Dest);
1416 case MIToken::ExternalSymbol:
1417 return parseExternalSymbolOperand(Dest);
1418 case MIToken::exclaim:
1419 return parseMetadataOperand(Dest);
1420 case MIToken::kw_cfi_same_value:
1421 case MIToken::kw_cfi_offset:
1422 case MIToken::kw_cfi_def_cfa_register:
1423 case MIToken::kw_cfi_def_cfa_offset:
1424 case MIToken::kw_cfi_def_cfa:
1425 return parseCFIOperand(Dest);
1426 case MIToken::kw_blockaddress:
1427 return parseBlockAddressOperand(Dest);
1428 case MIToken::kw_target_index:
1429 return parseTargetIndexOperand(Dest);
1430 case MIToken::kw_liveout:
1431 return parseLiveoutRegisterMaskOperand(Dest);
1432 case MIToken::Error:
1434 case MIToken::Identifier:
1435 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1436 Dest = MachineOperand::CreateRegMask(RegMask);
1442 // TODO: parse the other machine operands.
1443 return error("expected a machine operand");
1448 bool MIParser::parseMachineOperandAndTargetFlags(
1449 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1451 bool HasTargetFlags = false;
1452 if (Token.is(MIToken::kw_target_flags)) {
1453 HasTargetFlags = true;
1455 if (expectAndConsume(MIToken::lparen))
1457 if (Token.isNot(MIToken::Identifier))
1458 return error("expected the name of the target flag");
1459 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1460 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1461 return error("use of undefined target flag '" + Token.stringValue() +
1465 while (Token.is(MIToken::comma)) {
1467 if (Token.isNot(MIToken::Identifier))
1468 return error("expected the name of the target flag");
1469 unsigned BitFlag = 0;
1470 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1471 return error("use of undefined target flag '" + Token.stringValue() +
1473 // TODO: Report an error when using a duplicate bit target flag.
1477 if (expectAndConsume(MIToken::rparen))
1480 auto Loc = Token.location();
1481 if (parseMachineOperand(Dest, TiedDefIdx))
1483 if (!HasTargetFlags)
1486 return error(Loc, "register operands can't have target flags");
1487 Dest.setTargetFlags(TF);
1491 bool MIParser::parseOffset(int64_t &Offset) {
1492 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1494 StringRef Sign = Token.range();
1495 bool IsNegative = Token.is(MIToken::minus);
1497 if (Token.isNot(MIToken::IntegerLiteral))
1498 return error("expected an integer literal after '" + Sign + "'");
1499 if (Token.integerValue().getMinSignedBits() > 64)
1500 return error("expected 64-bit integer (too large)");
1501 Offset = Token.integerValue().getExtValue();
1508 bool MIParser::parseAlignment(unsigned &Alignment) {
1509 assert(Token.is(MIToken::kw_align));
1511 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1512 return error("expected an integer literal after 'align'");
1513 if (getUnsigned(Alignment))
1519 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1521 if (parseOffset(Offset))
1523 Op.setOffset(Offset);
1527 bool MIParser::parseIRValue(Value *&V) {
1528 switch (Token.kind()) {
1529 case MIToken::NamedIRValue: {
1530 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1532 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1533 Token.stringValue());
1535 return error(Twine("use of undefined IR value '") + Token.range() + "'");
1538 // TODO: Parse unnamed IR value references.
1540 llvm_unreachable("The current token should be an IR block reference");
1545 bool MIParser::getUint64(uint64_t &Result) {
1546 assert(Token.hasIntegerValue());
1547 if (Token.integerValue().getActiveBits() > 64)
1548 return error("expected 64-bit integer (too large)");
1549 Result = Token.integerValue().getZExtValue();
1553 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1554 const unsigned OldFlags = Flags;
1555 switch (Token.kind()) {
1556 case MIToken::kw_volatile:
1557 Flags |= MachineMemOperand::MOVolatile;
1559 case MIToken::kw_non_temporal:
1560 Flags |= MachineMemOperand::MONonTemporal;
1562 case MIToken::kw_invariant:
1563 Flags |= MachineMemOperand::MOInvariant;
1565 // TODO: parse the target specific memory operand flags.
1567 llvm_unreachable("The current token should be a memory operand flag");
1569 if (OldFlags == Flags)
1570 // We know that the same flag is specified more than once when the flags
1571 // weren't modified.
1572 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1577 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1578 switch (Token.kind()) {
1579 case MIToken::kw_stack:
1580 PSV = MF.getPSVManager().getStack();
1582 case MIToken::kw_got:
1583 PSV = MF.getPSVManager().getGOT();
1585 case MIToken::kw_jump_table:
1586 PSV = MF.getPSVManager().getJumpTable();
1588 case MIToken::kw_constant_pool:
1589 PSV = MF.getPSVManager().getConstantPool();
1591 case MIToken::FixedStackObject: {
1593 if (parseFixedStackFrameIndex(FI))
1595 PSV = MF.getPSVManager().getFixedStack(FI);
1596 // The token was already consumed, so use return here instead of break.
1599 case MIToken::GlobalValue:
1600 case MIToken::NamedGlobalValue: {
1601 GlobalValue *GV = nullptr;
1602 if (parseGlobalValue(GV))
1604 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1607 case MIToken::ExternalSymbol:
1608 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1609 MF.createExternalSymbolName(Token.stringValue()));
1612 llvm_unreachable("The current token should be pseudo source value");
1618 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1619 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1620 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1621 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::GlobalValue) ||
1622 Token.is(MIToken::NamedGlobalValue) ||
1623 Token.is(MIToken::ExternalSymbol)) {
1624 const PseudoSourceValue *PSV = nullptr;
1625 if (parseMemoryPseudoSourceValue(PSV))
1628 if (parseOffset(Offset))
1630 Dest = MachinePointerInfo(PSV, Offset);
1633 if (Token.isNot(MIToken::NamedIRValue))
1634 return error("expected an IR value reference");
1636 if (parseIRValue(V))
1638 if (!V->getType()->isPointerTy())
1639 return error("expected a pointer IR value");
1642 if (parseOffset(Offset))
1644 Dest = MachinePointerInfo(V, Offset);
1648 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1649 if (expectAndConsume(MIToken::lparen))
1652 while (Token.isMemoryOperandFlag()) {
1653 if (parseMemoryOperandFlag(Flags))
1656 if (Token.isNot(MIToken::Identifier) ||
1657 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1658 return error("expected 'load' or 'store' memory operation");
1659 if (Token.stringValue() == "load")
1660 Flags |= MachineMemOperand::MOLoad;
1662 Flags |= MachineMemOperand::MOStore;
1665 if (Token.isNot(MIToken::IntegerLiteral))
1666 return error("expected the size integer literal after memory operation");
1668 if (getUint64(Size))
1672 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1673 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1674 return error(Twine("expected '") + Word + "'");
1677 MachinePointerInfo Ptr = MachinePointerInfo();
1678 if (parseMachinePointerInfo(Ptr))
1680 unsigned BaseAlignment = Size;
1682 MDNode *Range = nullptr;
1683 while (consumeIfPresent(MIToken::comma)) {
1684 switch (Token.kind()) {
1685 case MIToken::kw_align:
1686 if (parseAlignment(BaseAlignment))
1689 case MIToken::md_tbaa:
1691 if (parseMDNode(AAInfo.TBAA))
1694 case MIToken::md_alias_scope:
1696 if (parseMDNode(AAInfo.Scope))
1699 case MIToken::md_noalias:
1701 if (parseMDNode(AAInfo.NoAlias))
1704 case MIToken::md_range:
1706 if (parseMDNode(Range))
1709 // TODO: Report an error on duplicate metadata nodes.
1711 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1712 "'!noalias' or '!range'");
1715 if (expectAndConsume(MIToken::rparen))
1718 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1722 void MIParser::initNames2InstrOpCodes() {
1723 if (!Names2InstrOpCodes.empty())
1725 const auto *TII = MF.getSubtarget().getInstrInfo();
1726 assert(TII && "Expected target instruction info");
1727 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1728 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1731 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1732 initNames2InstrOpCodes();
1733 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1734 if (InstrInfo == Names2InstrOpCodes.end())
1736 OpCode = InstrInfo->getValue();
1740 void MIParser::initNames2Regs() {
1741 if (!Names2Regs.empty())
1743 // The '%noreg' register is the register 0.
1744 Names2Regs.insert(std::make_pair("noreg", 0));
1745 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1746 assert(TRI && "Expected target register info");
1747 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1749 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1752 assert(WasInserted && "Expected registers to be unique case-insensitively");
1756 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1758 auto RegInfo = Names2Regs.find(RegName);
1759 if (RegInfo == Names2Regs.end())
1761 Reg = RegInfo->getValue();
1765 void MIParser::initNames2RegMasks() {
1766 if (!Names2RegMasks.empty())
1768 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1769 assert(TRI && "Expected target register info");
1770 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1771 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1772 assert(RegMasks.size() == RegMaskNames.size());
1773 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1774 Names2RegMasks.insert(
1775 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1778 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1779 initNames2RegMasks();
1780 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1781 if (RegMaskInfo == Names2RegMasks.end())
1783 return RegMaskInfo->getValue();
1786 void MIParser::initNames2SubRegIndices() {
1787 if (!Names2SubRegIndices.empty())
1789 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1790 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1791 Names2SubRegIndices.insert(
1792 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1795 unsigned MIParser::getSubRegIndex(StringRef Name) {
1796 initNames2SubRegIndices();
1797 auto SubRegInfo = Names2SubRegIndices.find(Name);
1798 if (SubRegInfo == Names2SubRegIndices.end())
1800 return SubRegInfo->getValue();
1803 static void initSlots2BasicBlocks(
1805 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1806 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1807 MST.incorporateFunction(F);
1808 for (auto &BB : F) {
1811 int Slot = MST.getLocalSlot(&BB);
1814 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1818 static const BasicBlock *getIRBlockFromSlot(
1820 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1821 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1822 if (BlockInfo == Slots2BasicBlocks.end())
1824 return BlockInfo->second;
1827 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1828 if (Slots2BasicBlocks.empty())
1829 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1830 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1833 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1834 if (&F == MF.getFunction())
1835 return getIRBlock(Slot);
1836 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1837 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1838 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1841 void MIParser::initNames2TargetIndices() {
1842 if (!Names2TargetIndices.empty())
1844 const auto *TII = MF.getSubtarget().getInstrInfo();
1845 assert(TII && "Expected target instruction info");
1846 auto Indices = TII->getSerializableTargetIndices();
1847 for (const auto &I : Indices)
1848 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1851 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1852 initNames2TargetIndices();
1853 auto IndexInfo = Names2TargetIndices.find(Name);
1854 if (IndexInfo == Names2TargetIndices.end())
1856 Index = IndexInfo->second;
1860 void MIParser::initNames2DirectTargetFlags() {
1861 if (!Names2DirectTargetFlags.empty())
1863 const auto *TII = MF.getSubtarget().getInstrInfo();
1864 assert(TII && "Expected target instruction info");
1865 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1866 for (const auto &I : Flags)
1867 Names2DirectTargetFlags.insert(
1868 std::make_pair(StringRef(I.second), I.first));
1871 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1872 initNames2DirectTargetFlags();
1873 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1874 if (FlagInfo == Names2DirectTargetFlags.end())
1876 Flag = FlagInfo->second;
1880 void MIParser::initNames2BitmaskTargetFlags() {
1881 if (!Names2BitmaskTargetFlags.empty())
1883 const auto *TII = MF.getSubtarget().getInstrInfo();
1884 assert(TII && "Expected target instruction info");
1885 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1886 for (const auto &I : Flags)
1887 Names2BitmaskTargetFlags.insert(
1888 std::make_pair(StringRef(I.second), I.first));
1891 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1892 initNames2BitmaskTargetFlags();
1893 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1894 if (FlagInfo == Names2BitmaskTargetFlags.end())
1896 Flag = FlagInfo->second;
1900 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1901 PerFunctionMIParsingState &PFS,
1902 const SlotMapping &IRSlots,
1903 SMDiagnostic &Error) {
1905 SM.AddNewSourceBuffer(
1906 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1908 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1909 .parseBasicBlockDefinitions(PFS.MBBSlots);
1912 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1913 const PerFunctionMIParsingState &PFS,
1914 const SlotMapping &IRSlots,
1915 SMDiagnostic &Error) {
1917 SM.AddNewSourceBuffer(
1918 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1920 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
1923 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1924 MachineFunction &MF, StringRef Src,
1925 const PerFunctionMIParsingState &PFS,
1926 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1927 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
1930 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1931 MachineFunction &MF, StringRef Src,
1932 const PerFunctionMIParsingState &PFS,
1933 const SlotMapping &IRSlots,
1934 SMDiagnostic &Error) {
1935 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1936 .parseStandaloneNamedRegister(Reg);
1939 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1940 MachineFunction &MF, StringRef Src,
1941 const PerFunctionMIParsingState &PFS,
1942 const SlotMapping &IRSlots,
1943 SMDiagnostic &Error) {
1944 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1945 .parseStandaloneVirtualRegister(Reg);
1948 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
1949 MachineFunction &MF, StringRef Src,
1950 const PerFunctionMIParsingState &PFS,
1951 const SlotMapping &IRSlots,
1952 SMDiagnostic &Error) {
1953 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1954 .parseStandaloneStackObject(FI);
1957 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
1958 StringRef Src, const PerFunctionMIParsingState &PFS,
1959 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1960 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);