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
41 /// range and other attributes.
42 struct ParsedMachineOperand {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
46 Optional<unsigned> TiedDefIdx;
48 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
49 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
50 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
52 assert(Operand.isReg() && Operand.isUse() &&
53 "Only used register operands can be tied");
61 StringRef Source, CurrentSource;
63 const PerFunctionMIParsingState &PFS;
64 /// Maps from indices to unnamed global values and metadata nodes.
65 const SlotMapping &IRSlots;
66 /// Maps from instruction names to op codes.
67 StringMap<unsigned> Names2InstrOpCodes;
68 /// Maps from register names to registers.
69 StringMap<unsigned> Names2Regs;
70 /// Maps from register mask names to register masks.
71 StringMap<const uint32_t *> Names2RegMasks;
72 /// Maps from subregister names to subregister indices.
73 StringMap<unsigned> Names2SubRegIndices;
74 /// Maps from slot numbers to function's unnamed basic blocks.
75 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
76 /// Maps from slot numbers to function's unnamed values.
77 DenseMap<unsigned, const Value *> Slots2Values;
78 /// Maps from target index names to target indices.
79 StringMap<int> Names2TargetIndices;
80 /// Maps from direct target flag names to the direct target flag values.
81 StringMap<unsigned> Names2DirectTargetFlags;
82 /// Maps from direct target flag names to the bitmask target flag values.
83 StringMap<unsigned> Names2BitmaskTargetFlags;
86 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
87 StringRef Source, const PerFunctionMIParsingState &PFS,
88 const SlotMapping &IRSlots);
92 /// Report an error at the current location with the given message.
94 /// This function always return true.
95 bool error(const Twine &Msg);
97 /// Report an error at the given location with the given message.
99 /// This function always return true.
100 bool error(StringRef::iterator Loc, const Twine &Msg);
103 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
104 bool parseBasicBlocks();
105 bool parse(MachineInstr *&MI);
106 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
107 bool parseStandaloneNamedRegister(unsigned &Reg);
108 bool parseStandaloneVirtualRegister(unsigned &Reg);
109 bool parseStandaloneStackObject(int &FI);
110 bool parseStandaloneMDNode(MDNode *&Node);
113 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
114 bool parseBasicBlock(MachineBasicBlock &MBB);
115 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
116 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
118 bool parseRegister(unsigned &Reg);
119 bool parseRegisterFlag(unsigned &Flags);
120 bool parseSubRegisterIndex(unsigned &SubReg);
121 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
122 bool parseRegisterOperand(MachineOperand &Dest,
123 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
124 bool parseImmediateOperand(MachineOperand &Dest);
125 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
126 bool parseTypedImmediateOperand(MachineOperand &Dest);
127 bool parseFPImmediateOperand(MachineOperand &Dest);
128 bool parseMBBReference(MachineBasicBlock *&MBB);
129 bool parseMBBOperand(MachineOperand &Dest);
130 bool parseStackFrameIndex(int &FI);
131 bool parseStackObjectOperand(MachineOperand &Dest);
132 bool parseFixedStackFrameIndex(int &FI);
133 bool parseFixedStackObjectOperand(MachineOperand &Dest);
134 bool parseGlobalValue(GlobalValue *&GV);
135 bool parseGlobalAddressOperand(MachineOperand &Dest);
136 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
137 bool parseJumpTableIndexOperand(MachineOperand &Dest);
138 bool parseExternalSymbolOperand(MachineOperand &Dest);
139 bool parseMDNode(MDNode *&Node);
140 bool parseMetadataOperand(MachineOperand &Dest);
141 bool parseCFIOffset(int &Offset);
142 bool parseCFIRegister(unsigned &Reg);
143 bool parseCFIOperand(MachineOperand &Dest);
144 bool parseIRBlock(BasicBlock *&BB, const Function &F);
145 bool parseBlockAddressOperand(MachineOperand &Dest);
146 bool parseTargetIndexOperand(MachineOperand &Dest);
147 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
148 bool parseMachineOperand(MachineOperand &Dest,
149 Optional<unsigned> &TiedDefIdx);
150 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
151 Optional<unsigned> &TiedDefIdx);
152 bool parseOffset(int64_t &Offset);
153 bool parseAlignment(unsigned &Alignment);
154 bool parseOperandsOffset(MachineOperand &Op);
155 bool parseIRValue(const Value *&V);
156 bool parseMemoryOperandFlag(unsigned &Flags);
157 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
158 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
159 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
162 /// Convert the integer literal in the current token into an unsigned integer.
164 /// Return true if an error occurred.
165 bool getUnsigned(unsigned &Result);
167 /// Convert the integer literal in the current token into an uint64.
169 /// Return true if an error occurred.
170 bool getUint64(uint64_t &Result);
172 /// If the current token is of the given kind, consume it and return false.
173 /// Otherwise report an error and return true.
174 bool expectAndConsume(MIToken::TokenKind TokenKind);
176 /// If the current token is of the given kind, consume it and return true.
177 /// Otherwise return false.
178 bool consumeIfPresent(MIToken::TokenKind TokenKind);
180 void initNames2InstrOpCodes();
182 /// Try to convert an instruction name to an opcode. Return true if the
183 /// instruction name is invalid.
184 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
186 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
188 bool assignRegisterTies(MachineInstr &MI,
189 ArrayRef<ParsedMachineOperand> Operands);
191 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
192 const MCInstrDesc &MCID);
194 void initNames2Regs();
196 /// Try to convert a register name to a register number. Return true if the
197 /// register name is invalid.
198 bool getRegisterByName(StringRef RegName, unsigned &Reg);
200 void initNames2RegMasks();
202 /// Check if the given identifier is a name of a register mask.
204 /// Return null if the identifier isn't a register mask.
205 const uint32_t *getRegMask(StringRef Identifier);
207 void initNames2SubRegIndices();
209 /// Check if the given identifier is a name of a subregister index.
211 /// Return 0 if the name isn't a subregister index class.
212 unsigned getSubRegIndex(StringRef Name);
214 const BasicBlock *getIRBlock(unsigned Slot);
215 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
217 const Value *getIRValue(unsigned Slot);
219 void initNames2TargetIndices();
221 /// Try to convert a name of target index to the corresponding target index.
223 /// Return true if the name isn't a name of a target index.
224 bool getTargetIndex(StringRef Name, int &Index);
226 void initNames2DirectTargetFlags();
228 /// Try to convert a name of a direct target flag to the corresponding
231 /// Return true if the name isn't a name of a direct flag.
232 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
234 void initNames2BitmaskTargetFlags();
236 /// Try to convert a name of a bitmask target flag to the corresponding
239 /// Return true if the name isn't a name of a bitmask target flag.
240 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
243 } // end anonymous namespace
245 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
246 StringRef Source, const PerFunctionMIParsingState &PFS,
247 const SlotMapping &IRSlots)
248 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
249 PFS(PFS), IRSlots(IRSlots) {}
251 void MIParser::lex() {
252 CurrentSource = lexMIToken(
253 CurrentSource, Token,
254 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
257 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
259 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
260 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
261 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
262 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
263 // Create an ordinary diagnostic when the source manager's buffer is the
265 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
268 // Create a diagnostic for a YAML string literal.
269 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
270 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
275 static const char *toString(MIToken::TokenKind TokenKind) {
283 case MIToken::lparen:
285 case MIToken::rparen:
288 return "<unknown token>";
292 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
293 if (Token.isNot(TokenKind))
294 return error(Twine("expected ") + toString(TokenKind));
299 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
300 if (Token.isNot(TokenKind))
306 bool MIParser::parseBasicBlockDefinition(
307 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
308 assert(Token.is(MIToken::MachineBasicBlockLabel));
312 auto Loc = Token.location();
313 auto Name = Token.stringValue();
315 bool HasAddressTaken = false;
316 bool IsLandingPad = false;
317 unsigned Alignment = 0;
318 BasicBlock *BB = nullptr;
319 if (consumeIfPresent(MIToken::lparen)) {
321 // TODO: Report an error when multiple same attributes are specified.
322 switch (Token.kind()) {
323 case MIToken::kw_address_taken:
324 HasAddressTaken = true;
327 case MIToken::kw_landing_pad:
331 case MIToken::kw_align:
332 if (parseAlignment(Alignment))
335 case MIToken::IRBlock:
336 // TODO: Report an error when both name and ir block are specified.
337 if (parseIRBlock(BB, *MF.getFunction()))
344 } while (consumeIfPresent(MIToken::comma));
345 if (expectAndConsume(MIToken::rparen))
348 if (expectAndConsume(MIToken::colon))
352 BB = dyn_cast_or_null<BasicBlock>(
353 MF.getFunction()->getValueSymbolTable().lookup(Name));
355 return error(Loc, Twine("basic block '") + Name +
356 "' is not defined in the function '" +
359 auto *MBB = MF.CreateMachineBasicBlock(BB);
360 MF.insert(MF.end(), MBB);
361 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
363 return error(Loc, Twine("redefinition of machine basic block with id #") +
366 MBB->setAlignment(Alignment);
368 MBB->setHasAddressTaken();
369 MBB->setIsLandingPad(IsLandingPad);
373 bool MIParser::parseBasicBlockDefinitions(
374 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
376 // Skip until the first machine basic block.
377 while (Token.is(MIToken::Newline))
379 if (Token.isErrorOrEOF())
380 return Token.isError();
381 if (Token.isNot(MIToken::MachineBasicBlockLabel))
382 return error("expected a basic block definition before instructions");
383 unsigned BraceDepth = 0;
385 if (parseBasicBlockDefinition(MBBSlots))
387 bool IsAfterNewline = false;
388 // Skip until the next machine basic block.
390 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
391 Token.isErrorOrEOF())
393 else if (Token.is(MIToken::MachineBasicBlockLabel))
394 return error("basic block definition should be located at the start of "
396 else if (consumeIfPresent(MIToken::Newline)) {
397 IsAfterNewline = true;
400 IsAfterNewline = false;
401 if (Token.is(MIToken::lbrace))
403 if (Token.is(MIToken::rbrace)) {
405 return error("extraneous closing brace ('}')");
410 // Verify that we closed all of the '{' at the end of a file or a block.
411 if (!Token.isError() && BraceDepth)
412 return error("expected '}'"); // FIXME: Report a note that shows '{'.
413 } while (!Token.isErrorOrEOF());
414 return Token.isError();
417 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
418 assert(Token.is(MIToken::kw_liveins));
420 if (expectAndConsume(MIToken::colon))
422 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
425 if (Token.isNot(MIToken::NamedRegister))
426 return error("expected a named register");
428 if (parseRegister(Reg))
432 } while (consumeIfPresent(MIToken::comma));
436 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
437 assert(Token.is(MIToken::kw_successors));
439 if (expectAndConsume(MIToken::colon))
441 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
444 if (Token.isNot(MIToken::MachineBasicBlock))
445 return error("expected a machine basic block reference");
446 MachineBasicBlock *SuccMBB = nullptr;
447 if (parseMBBReference(SuccMBB))
451 if (consumeIfPresent(MIToken::lparen)) {
452 if (Token.isNot(MIToken::IntegerLiteral))
453 return error("expected an integer literal after '('");
454 if (getUnsigned(Weight))
457 if (expectAndConsume(MIToken::rparen))
460 MBB.addSuccessor(SuccMBB, Weight);
461 } while (consumeIfPresent(MIToken::comma));
465 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
466 // Skip the definition.
467 assert(Token.is(MIToken::MachineBasicBlockLabel));
469 if (consumeIfPresent(MIToken::lparen)) {
470 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
472 consumeIfPresent(MIToken::rparen);
474 consumeIfPresent(MIToken::colon);
476 // Parse the liveins and successors.
477 // N.B: Multiple lists of successors and liveins are allowed and they're
484 // liveins: %edi, %esi
486 if (Token.is(MIToken::kw_successors)) {
487 if (parseBasicBlockSuccessors(MBB))
489 } else if (Token.is(MIToken::kw_liveins)) {
490 if (parseBasicBlockLiveins(MBB))
492 } else if (consumeIfPresent(MIToken::Newline)) {
496 if (!Token.isNewlineOrEOF())
497 return error("expected line break at the end of a list");
501 // Parse the instructions.
502 bool IsInBundle = false;
503 MachineInstr *PrevMI = nullptr;
505 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
507 else if (consumeIfPresent(MIToken::Newline))
509 if (consumeIfPresent(MIToken::rbrace)) {
510 // The first parsing pass should verify that all closing '}' have an
516 MachineInstr *MI = nullptr;
519 MBB.insert(MBB.end(), MI);
521 PrevMI->setFlag(MachineInstr::BundledSucc);
522 MI->setFlag(MachineInstr::BundledPred);
525 if (Token.is(MIToken::lbrace)) {
527 return error("nested instruction bundles are not allowed");
529 // This instruction is the start of the bundle.
530 MI->setFlag(MachineInstr::BundledSucc);
532 if (!Token.is(MIToken::Newline))
533 // The next instruction can be on the same line.
536 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
542 bool MIParser::parseBasicBlocks() {
544 // Skip until the first machine basic block.
545 while (Token.is(MIToken::Newline))
547 if (Token.isErrorOrEOF())
548 return Token.isError();
549 // The first parsing pass should have verified that this token is a MBB label
550 // in the 'parseBasicBlockDefinitions' method.
551 assert(Token.is(MIToken::MachineBasicBlockLabel));
553 MachineBasicBlock *MBB = nullptr;
554 if (parseMBBReference(MBB))
556 if (parseBasicBlock(*MBB))
558 // The method 'parseBasicBlock' should parse the whole block until the next
559 // block or the end of file.
560 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
561 } while (Token.isNot(MIToken::Eof));
565 bool MIParser::parse(MachineInstr *&MI) {
566 // Parse any register operands before '='
567 MachineOperand MO = MachineOperand::CreateImm(0);
568 SmallVector<ParsedMachineOperand, 8> Operands;
569 while (Token.isRegister() || Token.isRegisterFlag()) {
570 auto Loc = Token.location();
571 Optional<unsigned> TiedDefIdx;
572 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
575 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
576 if (Token.isNot(MIToken::comma))
580 if (!Operands.empty() && expectAndConsume(MIToken::equal))
583 unsigned OpCode, Flags = 0;
584 if (Token.isError() || parseInstruction(OpCode, Flags))
587 // Parse the remaining machine operands.
588 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
589 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
590 auto Loc = Token.location();
591 Optional<unsigned> TiedDefIdx;
592 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
595 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
596 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
597 Token.is(MIToken::lbrace))
599 if (Token.isNot(MIToken::comma))
600 return error("expected ',' before the next machine operand");
604 DebugLoc DebugLocation;
605 if (Token.is(MIToken::kw_debug_location)) {
607 if (Token.isNot(MIToken::exclaim))
608 return error("expected a metadata node after 'debug-location'");
609 MDNode *Node = nullptr;
610 if (parseMDNode(Node))
612 DebugLocation = DebugLoc(Node);
615 // Parse the machine memory operands.
616 SmallVector<MachineMemOperand *, 2> MemOperands;
617 if (Token.is(MIToken::coloncolon)) {
619 while (!Token.isNewlineOrEOF()) {
620 MachineMemOperand *MemOp = nullptr;
621 if (parseMachineMemoryOperand(MemOp))
623 MemOperands.push_back(MemOp);
624 if (Token.isNewlineOrEOF())
626 if (Token.isNot(MIToken::comma))
627 return error("expected ',' before the next machine memory operand");
632 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
633 if (!MCID.isVariadic()) {
634 // FIXME: Move the implicit operand verification to the machine verifier.
635 if (verifyImplicitOperands(Operands, MCID))
639 // TODO: Check for extraneous machine operands.
640 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
642 for (const auto &Operand : Operands)
643 MI->addOperand(MF, Operand.Operand);
644 if (assignRegisterTies(*MI, Operands))
646 if (MemOperands.empty())
648 MachineInstr::mmo_iterator MemRefs =
649 MF.allocateMemRefsArray(MemOperands.size());
650 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
651 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
655 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
657 if (Token.isNot(MIToken::MachineBasicBlock))
658 return error("expected a machine basic block reference");
659 if (parseMBBReference(MBB))
662 if (Token.isNot(MIToken::Eof))
664 "expected end of string after the machine basic block reference");
668 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
670 if (Token.isNot(MIToken::NamedRegister))
671 return error("expected a named register");
672 if (parseRegister(Reg))
675 if (Token.isNot(MIToken::Eof))
676 return error("expected end of string after the register reference");
680 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
682 if (Token.isNot(MIToken::VirtualRegister))
683 return error("expected a virtual register");
684 if (parseRegister(Reg))
687 if (Token.isNot(MIToken::Eof))
688 return error("expected end of string after the register reference");
692 bool MIParser::parseStandaloneStackObject(int &FI) {
694 if (Token.isNot(MIToken::StackObject))
695 return error("expected a stack object");
696 if (parseStackFrameIndex(FI))
698 if (Token.isNot(MIToken::Eof))
699 return error("expected end of string after the stack object reference");
703 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
705 if (Token.isNot(MIToken::exclaim))
706 return error("expected a metadata node");
707 if (parseMDNode(Node))
709 if (Token.isNot(MIToken::Eof))
710 return error("expected end of string after the metadata node");
714 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
715 assert(MO.isImplicit());
716 return MO.isDef() ? "implicit-def" : "implicit";
719 static std::string getRegisterName(const TargetRegisterInfo *TRI,
721 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
722 return StringRef(TRI->getName(Reg)).lower();
725 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
726 const MCInstrDesc &MCID) {
728 // We can't verify call instructions as they can contain arbitrary implicit
729 // register and register mask operands.
732 // Gather all the expected implicit operands.
733 SmallVector<MachineOperand, 4> ImplicitOperands;
734 if (MCID.ImplicitDefs)
735 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
736 ImplicitOperands.push_back(
737 MachineOperand::CreateReg(*ImpDefs, true, true));
738 if (MCID.ImplicitUses)
739 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
740 ImplicitOperands.push_back(
741 MachineOperand::CreateReg(*ImpUses, false, true));
743 const auto *TRI = MF.getSubtarget().getRegisterInfo();
744 assert(TRI && "Expected target register info");
745 size_t I = ImplicitOperands.size(), J = Operands.size();
750 const auto &ImplicitOperand = ImplicitOperands[I];
751 const auto &Operand = Operands[J].Operand;
752 if (ImplicitOperand.isIdenticalTo(Operand))
754 if (Operand.isReg() && Operand.isImplicit()) {
755 // Check if this implicit register is a subregister of an explicit
757 bool IsImplicitSubRegister = false;
758 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
759 const auto &Op = Operands[K].Operand;
760 if (Op.isReg() && !Op.isImplicit() &&
761 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
762 IsImplicitSubRegister = true;
766 if (IsImplicitSubRegister)
768 return error(Operands[J].Begin,
769 Twine("expected an implicit register operand '") +
770 printImplicitRegisterFlag(ImplicitOperand) + " %" +
771 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
774 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
775 // insead of reporting an error at this location:
778 // report the error at the following location:
781 return error(J < Operands.size() ? Operands[J].End : Token.location(),
782 Twine("missing implicit register operand '") +
783 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
784 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
789 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
790 if (Token.is(MIToken::kw_frame_setup)) {
791 Flags |= MachineInstr::FrameSetup;
794 if (Token.isNot(MIToken::Identifier))
795 return error("expected a machine instruction");
796 StringRef InstrName = Token.stringValue();
797 if (parseInstrName(InstrName, OpCode))
798 return error(Twine("unknown machine instruction name '") + InstrName + "'");
803 bool MIParser::parseRegister(unsigned &Reg) {
804 switch (Token.kind()) {
805 case MIToken::underscore:
808 case MIToken::NamedRegister: {
809 StringRef Name = Token.stringValue();
810 if (getRegisterByName(Name, Reg))
811 return error(Twine("unknown register name '") + Name + "'");
814 case MIToken::VirtualRegister: {
818 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
819 if (RegInfo == PFS.VirtualRegisterSlots.end())
820 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
822 Reg = RegInfo->second;
825 // TODO: Parse other register kinds.
827 llvm_unreachable("The current token should be a register");
832 bool MIParser::parseRegisterFlag(unsigned &Flags) {
833 const unsigned OldFlags = Flags;
834 switch (Token.kind()) {
835 case MIToken::kw_implicit:
836 Flags |= RegState::Implicit;
838 case MIToken::kw_implicit_define:
839 Flags |= RegState::ImplicitDefine;
841 case MIToken::kw_def:
842 Flags |= RegState::Define;
844 case MIToken::kw_dead:
845 Flags |= RegState::Dead;
847 case MIToken::kw_killed:
848 Flags |= RegState::Kill;
850 case MIToken::kw_undef:
851 Flags |= RegState::Undef;
853 case MIToken::kw_internal:
854 Flags |= RegState::InternalRead;
856 case MIToken::kw_early_clobber:
857 Flags |= RegState::EarlyClobber;
859 case MIToken::kw_debug_use:
860 Flags |= RegState::Debug;
863 llvm_unreachable("The current token should be a register flag");
865 if (OldFlags == Flags)
866 // We know that the same flag is specified more than once when the flags
868 return error("duplicate '" + Token.stringValue() + "' register flag");
873 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
874 assert(Token.is(MIToken::colon));
876 if (Token.isNot(MIToken::Identifier))
877 return error("expected a subregister index after ':'");
878 auto Name = Token.stringValue();
879 SubReg = getSubRegIndex(Name);
881 return error(Twine("use of unknown subregister index '") + Name + "'");
886 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
887 if (!consumeIfPresent(MIToken::kw_tied_def))
888 return error("expected 'tied-def' after '('");
889 if (Token.isNot(MIToken::IntegerLiteral))
890 return error("expected an integer literal after 'tied-def'");
891 if (getUnsigned(TiedDefIdx))
894 if (expectAndConsume(MIToken::rparen))
899 bool MIParser::assignRegisterTies(MachineInstr &MI,
900 ArrayRef<ParsedMachineOperand> Operands) {
901 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
902 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
903 if (!Operands[I].TiedDefIdx)
905 // The parser ensures that this operand is a register use, so we just have
906 // to check the tied-def operand.
907 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
909 return error(Operands[I].Begin,
910 Twine("use of invalid tied-def operand index '" +
911 Twine(DefIdx) + "'; instruction has only ") +
912 Twine(E) + " operands");
913 const auto &DefOperand = Operands[DefIdx].Operand;
914 if (!DefOperand.isReg() || !DefOperand.isDef())
915 // FIXME: add note with the def operand.
916 return error(Operands[I].Begin,
917 Twine("use of invalid tied-def operand index '") +
918 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
919 " isn't a defined register");
920 // Check that the tied-def operand wasn't tied elsewhere.
921 for (const auto &TiedPair : TiedRegisterPairs) {
922 if (TiedPair.first == DefIdx)
923 return error(Operands[I].Begin,
924 Twine("the tied-def operand #") + Twine(DefIdx) +
925 " is already tied with another register operand");
927 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
929 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
930 // indices must be less than tied max.
931 for (const auto &TiedPair : TiedRegisterPairs)
932 MI.tieOperands(TiedPair.first, TiedPair.second);
936 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
937 Optional<unsigned> &TiedDefIdx,
940 unsigned Flags = IsDef ? RegState::Define : 0;
941 while (Token.isRegisterFlag()) {
942 if (parseRegisterFlag(Flags))
945 if (!Token.isRegister())
946 return error("expected a register after register flags");
947 if (parseRegister(Reg))
951 if (Token.is(MIToken::colon)) {
952 if (parseSubRegisterIndex(SubReg))
955 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
957 if (parseRegisterTiedDefIndex(Idx))
961 Dest = MachineOperand::CreateReg(
962 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
963 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
964 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
965 Flags & RegState::InternalRead);
969 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
970 assert(Token.is(MIToken::IntegerLiteral));
971 const APSInt &Int = Token.integerValue();
972 if (Int.getMinSignedBits() > 64)
973 return error("integer literal is too large to be an immediate operand");
974 Dest = MachineOperand::CreateImm(Int.getExtValue());
979 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
980 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
983 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
985 return error(Loc + Err.getColumnNo(), Err.getMessage());
989 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
990 assert(Token.is(MIToken::IntegerType));
991 auto Loc = Token.location();
993 if (Token.isNot(MIToken::IntegerLiteral))
994 return error("expected an integer literal");
995 const Constant *C = nullptr;
996 if (parseIRConstant(Loc, C))
998 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1002 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1003 auto Loc = Token.location();
1005 if (Token.isNot(MIToken::FloatingPointLiteral))
1006 return error("expected a floating point literal");
1007 const Constant *C = nullptr;
1008 if (parseIRConstant(Loc, C))
1010 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1014 bool MIParser::getUnsigned(unsigned &Result) {
1015 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1016 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1017 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1019 return error("expected 32-bit integer (too large)");
1024 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1025 assert(Token.is(MIToken::MachineBasicBlock) ||
1026 Token.is(MIToken::MachineBasicBlockLabel));
1028 if (getUnsigned(Number))
1030 auto MBBInfo = PFS.MBBSlots.find(Number);
1031 if (MBBInfo == PFS.MBBSlots.end())
1032 return error(Twine("use of undefined machine basic block #") +
1034 MBB = MBBInfo->second;
1035 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1036 return error(Twine("the name of machine basic block #") + Twine(Number) +
1037 " isn't '" + Token.stringValue() + "'");
1041 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1042 MachineBasicBlock *MBB;
1043 if (parseMBBReference(MBB))
1045 Dest = MachineOperand::CreateMBB(MBB);
1050 bool MIParser::parseStackFrameIndex(int &FI) {
1051 assert(Token.is(MIToken::StackObject));
1053 if (getUnsigned(ID))
1055 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1056 if (ObjectInfo == PFS.StackObjectSlots.end())
1057 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1060 if (const auto *Alloca =
1061 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1062 Name = Alloca->getName();
1063 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1064 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1065 "' isn't '" + Token.stringValue() + "'");
1067 FI = ObjectInfo->second;
1071 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1073 if (parseStackFrameIndex(FI))
1075 Dest = MachineOperand::CreateFI(FI);
1079 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1080 assert(Token.is(MIToken::FixedStackObject));
1082 if (getUnsigned(ID))
1084 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1085 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1086 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1089 FI = ObjectInfo->second;
1093 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1095 if (parseFixedStackFrameIndex(FI))
1097 Dest = MachineOperand::CreateFI(FI);
1101 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1102 switch (Token.kind()) {
1103 case MIToken::NamedGlobalValue: {
1104 const Module *M = MF.getFunction()->getParent();
1105 GV = M->getNamedValue(Token.stringValue());
1107 return error(Twine("use of undefined global value '") + Token.range() +
1111 case MIToken::GlobalValue: {
1113 if (getUnsigned(GVIdx))
1115 if (GVIdx >= IRSlots.GlobalValues.size())
1116 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1118 GV = IRSlots.GlobalValues[GVIdx];
1122 llvm_unreachable("The current token should be a global value");
1127 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1128 GlobalValue *GV = nullptr;
1129 if (parseGlobalValue(GV))
1132 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1133 if (parseOperandsOffset(Dest))
1138 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1139 assert(Token.is(MIToken::ConstantPoolItem));
1141 if (getUnsigned(ID))
1143 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1144 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1145 return error("use of undefined constant '%const." + Twine(ID) + "'");
1147 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1148 if (parseOperandsOffset(Dest))
1153 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1154 assert(Token.is(MIToken::JumpTableIndex));
1156 if (getUnsigned(ID))
1158 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1159 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1160 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1162 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1166 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1167 assert(Token.is(MIToken::ExternalSymbol));
1168 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1170 Dest = MachineOperand::CreateES(Symbol);
1171 if (parseOperandsOffset(Dest))
1176 bool MIParser::parseMDNode(MDNode *&Node) {
1177 assert(Token.is(MIToken::exclaim));
1178 auto Loc = Token.location();
1180 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1181 return error("expected metadata id after '!'");
1183 if (getUnsigned(ID))
1185 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1186 if (NodeInfo == IRSlots.MetadataNodes.end())
1187 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1189 Node = NodeInfo->second.get();
1193 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1194 MDNode *Node = nullptr;
1195 if (parseMDNode(Node))
1197 Dest = MachineOperand::CreateMetadata(Node);
1201 bool MIParser::parseCFIOffset(int &Offset) {
1202 if (Token.isNot(MIToken::IntegerLiteral))
1203 return error("expected a cfi offset");
1204 if (Token.integerValue().getMinSignedBits() > 32)
1205 return error("expected a 32 bit integer (the cfi offset is too large)");
1206 Offset = (int)Token.integerValue().getExtValue();
1211 bool MIParser::parseCFIRegister(unsigned &Reg) {
1212 if (Token.isNot(MIToken::NamedRegister))
1213 return error("expected a cfi register");
1215 if (parseRegister(LLVMReg))
1217 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1218 assert(TRI && "Expected target register info");
1219 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1221 return error("invalid DWARF register");
1222 Reg = (unsigned)DwarfReg;
1227 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1228 auto Kind = Token.kind();
1230 auto &MMI = MF.getMMI();
1235 case MIToken::kw_cfi_same_value:
1236 if (parseCFIRegister(Reg))
1239 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1241 case MIToken::kw_cfi_offset:
1242 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1243 parseCFIOffset(Offset))
1246 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1248 case MIToken::kw_cfi_def_cfa_register:
1249 if (parseCFIRegister(Reg))
1252 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1254 case MIToken::kw_cfi_def_cfa_offset:
1255 if (parseCFIOffset(Offset))
1257 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1258 CFIIndex = MMI.addFrameInst(
1259 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1261 case MIToken::kw_cfi_def_cfa:
1262 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1263 parseCFIOffset(Offset))
1265 // NB: MCCFIInstruction::createDefCfa negates the offset.
1267 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1270 // TODO: Parse the other CFI operands.
1271 llvm_unreachable("The current token should be a cfi operand");
1273 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1277 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1278 switch (Token.kind()) {
1279 case MIToken::NamedIRBlock: {
1280 BB = dyn_cast_or_null<BasicBlock>(
1281 F.getValueSymbolTable().lookup(Token.stringValue()));
1283 return error(Twine("use of undefined IR block '") + Token.range() + "'");
1286 case MIToken::IRBlock: {
1287 unsigned SlotNumber = 0;
1288 if (getUnsigned(SlotNumber))
1290 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1292 return error(Twine("use of undefined IR block '%ir-block.") +
1293 Twine(SlotNumber) + "'");
1297 llvm_unreachable("The current token should be an IR block reference");
1302 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1303 assert(Token.is(MIToken::kw_blockaddress));
1305 if (expectAndConsume(MIToken::lparen))
1307 if (Token.isNot(MIToken::GlobalValue) &&
1308 Token.isNot(MIToken::NamedGlobalValue))
1309 return error("expected a global value");
1310 GlobalValue *GV = nullptr;
1311 if (parseGlobalValue(GV))
1313 auto *F = dyn_cast<Function>(GV);
1315 return error("expected an IR function reference");
1317 if (expectAndConsume(MIToken::comma))
1319 BasicBlock *BB = nullptr;
1320 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1321 return error("expected an IR block reference");
1322 if (parseIRBlock(BB, *F))
1325 if (expectAndConsume(MIToken::rparen))
1327 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1328 if (parseOperandsOffset(Dest))
1333 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1334 assert(Token.is(MIToken::kw_target_index));
1336 if (expectAndConsume(MIToken::lparen))
1338 if (Token.isNot(MIToken::Identifier))
1339 return error("expected the name of the target index");
1341 if (getTargetIndex(Token.stringValue(), Index))
1342 return error("use of undefined target index '" + Token.stringValue() + "'");
1344 if (expectAndConsume(MIToken::rparen))
1346 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1347 if (parseOperandsOffset(Dest))
1352 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1353 assert(Token.is(MIToken::kw_liveout));
1354 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1355 assert(TRI && "Expected target register info");
1356 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1358 if (expectAndConsume(MIToken::lparen))
1361 if (Token.isNot(MIToken::NamedRegister))
1362 return error("expected a named register");
1364 if (parseRegister(Reg))
1367 Mask[Reg / 32] |= 1U << (Reg % 32);
1368 // TODO: Report an error if the same register is used more than once.
1369 if (Token.isNot(MIToken::comma))
1373 if (expectAndConsume(MIToken::rparen))
1375 Dest = MachineOperand::CreateRegLiveOut(Mask);
1379 bool MIParser::parseMachineOperand(MachineOperand &Dest,
1380 Optional<unsigned> &TiedDefIdx) {
1381 switch (Token.kind()) {
1382 case MIToken::kw_implicit:
1383 case MIToken::kw_implicit_define:
1384 case MIToken::kw_def:
1385 case MIToken::kw_dead:
1386 case MIToken::kw_killed:
1387 case MIToken::kw_undef:
1388 case MIToken::kw_internal:
1389 case MIToken::kw_early_clobber:
1390 case MIToken::kw_debug_use:
1391 case MIToken::underscore:
1392 case MIToken::NamedRegister:
1393 case MIToken::VirtualRegister:
1394 return parseRegisterOperand(Dest, TiedDefIdx);
1395 case MIToken::IntegerLiteral:
1396 return parseImmediateOperand(Dest);
1397 case MIToken::IntegerType:
1398 return parseTypedImmediateOperand(Dest);
1399 case MIToken::kw_half:
1400 case MIToken::kw_float:
1401 case MIToken::kw_double:
1402 case MIToken::kw_x86_fp80:
1403 case MIToken::kw_fp128:
1404 case MIToken::kw_ppc_fp128:
1405 return parseFPImmediateOperand(Dest);
1406 case MIToken::MachineBasicBlock:
1407 return parseMBBOperand(Dest);
1408 case MIToken::StackObject:
1409 return parseStackObjectOperand(Dest);
1410 case MIToken::FixedStackObject:
1411 return parseFixedStackObjectOperand(Dest);
1412 case MIToken::GlobalValue:
1413 case MIToken::NamedGlobalValue:
1414 return parseGlobalAddressOperand(Dest);
1415 case MIToken::ConstantPoolItem:
1416 return parseConstantPoolIndexOperand(Dest);
1417 case MIToken::JumpTableIndex:
1418 return parseJumpTableIndexOperand(Dest);
1419 case MIToken::ExternalSymbol:
1420 return parseExternalSymbolOperand(Dest);
1421 case MIToken::exclaim:
1422 return parseMetadataOperand(Dest);
1423 case MIToken::kw_cfi_same_value:
1424 case MIToken::kw_cfi_offset:
1425 case MIToken::kw_cfi_def_cfa_register:
1426 case MIToken::kw_cfi_def_cfa_offset:
1427 case MIToken::kw_cfi_def_cfa:
1428 return parseCFIOperand(Dest);
1429 case MIToken::kw_blockaddress:
1430 return parseBlockAddressOperand(Dest);
1431 case MIToken::kw_target_index:
1432 return parseTargetIndexOperand(Dest);
1433 case MIToken::kw_liveout:
1434 return parseLiveoutRegisterMaskOperand(Dest);
1435 case MIToken::Error:
1437 case MIToken::Identifier:
1438 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1439 Dest = MachineOperand::CreateRegMask(RegMask);
1445 // FIXME: Parse the MCSymbol machine operand.
1446 return error("expected a machine operand");
1451 bool MIParser::parseMachineOperandAndTargetFlags(
1452 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1454 bool HasTargetFlags = false;
1455 if (Token.is(MIToken::kw_target_flags)) {
1456 HasTargetFlags = true;
1458 if (expectAndConsume(MIToken::lparen))
1460 if (Token.isNot(MIToken::Identifier))
1461 return error("expected the name of the target flag");
1462 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1463 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1464 return error("use of undefined target flag '" + Token.stringValue() +
1468 while (Token.is(MIToken::comma)) {
1470 if (Token.isNot(MIToken::Identifier))
1471 return error("expected the name of the target flag");
1472 unsigned BitFlag = 0;
1473 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1474 return error("use of undefined target flag '" + Token.stringValue() +
1476 // TODO: Report an error when using a duplicate bit target flag.
1480 if (expectAndConsume(MIToken::rparen))
1483 auto Loc = Token.location();
1484 if (parseMachineOperand(Dest, TiedDefIdx))
1486 if (!HasTargetFlags)
1489 return error(Loc, "register operands can't have target flags");
1490 Dest.setTargetFlags(TF);
1494 bool MIParser::parseOffset(int64_t &Offset) {
1495 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1497 StringRef Sign = Token.range();
1498 bool IsNegative = Token.is(MIToken::minus);
1500 if (Token.isNot(MIToken::IntegerLiteral))
1501 return error("expected an integer literal after '" + Sign + "'");
1502 if (Token.integerValue().getMinSignedBits() > 64)
1503 return error("expected 64-bit integer (too large)");
1504 Offset = Token.integerValue().getExtValue();
1511 bool MIParser::parseAlignment(unsigned &Alignment) {
1512 assert(Token.is(MIToken::kw_align));
1514 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1515 return error("expected an integer literal after 'align'");
1516 if (getUnsigned(Alignment))
1522 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1524 if (parseOffset(Offset))
1526 Op.setOffset(Offset);
1530 bool MIParser::parseIRValue(const Value *&V) {
1531 switch (Token.kind()) {
1532 case MIToken::NamedIRValue: {
1533 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1536 case MIToken::IRValue: {
1537 unsigned SlotNumber = 0;
1538 if (getUnsigned(SlotNumber))
1540 V = getIRValue(SlotNumber);
1543 case MIToken::NamedGlobalValue:
1544 case MIToken::GlobalValue: {
1545 GlobalValue *GV = nullptr;
1546 if (parseGlobalValue(GV))
1552 llvm_unreachable("The current token should be an IR block reference");
1555 return error(Twine("use of undefined IR value '") + Token.range() + "'");
1559 bool MIParser::getUint64(uint64_t &Result) {
1560 assert(Token.hasIntegerValue());
1561 if (Token.integerValue().getActiveBits() > 64)
1562 return error("expected 64-bit integer (too large)");
1563 Result = Token.integerValue().getZExtValue();
1567 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1568 const unsigned OldFlags = Flags;
1569 switch (Token.kind()) {
1570 case MIToken::kw_volatile:
1571 Flags |= MachineMemOperand::MOVolatile;
1573 case MIToken::kw_non_temporal:
1574 Flags |= MachineMemOperand::MONonTemporal;
1576 case MIToken::kw_invariant:
1577 Flags |= MachineMemOperand::MOInvariant;
1579 // TODO: parse the target specific memory operand flags.
1581 llvm_unreachable("The current token should be a memory operand flag");
1583 if (OldFlags == Flags)
1584 // We know that the same flag is specified more than once when the flags
1585 // weren't modified.
1586 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1591 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1592 switch (Token.kind()) {
1593 case MIToken::kw_stack:
1594 PSV = MF.getPSVManager().getStack();
1596 case MIToken::kw_got:
1597 PSV = MF.getPSVManager().getGOT();
1599 case MIToken::kw_jump_table:
1600 PSV = MF.getPSVManager().getJumpTable();
1602 case MIToken::kw_constant_pool:
1603 PSV = MF.getPSVManager().getConstantPool();
1605 case MIToken::FixedStackObject: {
1607 if (parseFixedStackFrameIndex(FI))
1609 PSV = MF.getPSVManager().getFixedStack(FI);
1610 // The token was already consumed, so use return here instead of break.
1613 case MIToken::kw_call_entry: {
1615 switch (Token.kind()) {
1616 case MIToken::GlobalValue:
1617 case MIToken::NamedGlobalValue: {
1618 GlobalValue *GV = nullptr;
1619 if (parseGlobalValue(GV))
1621 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1624 case MIToken::ExternalSymbol:
1625 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1626 MF.createExternalSymbolName(Token.stringValue()));
1630 "expected a global value or an external symbol after 'call-entry'");
1635 llvm_unreachable("The current token should be pseudo source value");
1641 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1642 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1643 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1644 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
1645 const PseudoSourceValue *PSV = nullptr;
1646 if (parseMemoryPseudoSourceValue(PSV))
1649 if (parseOffset(Offset))
1651 Dest = MachinePointerInfo(PSV, Offset);
1654 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1655 Token.isNot(MIToken::GlobalValue) &&
1656 Token.isNot(MIToken::NamedGlobalValue))
1657 return error("expected an IR value reference");
1658 const Value *V = nullptr;
1659 if (parseIRValue(V))
1661 if (!V->getType()->isPointerTy())
1662 return error("expected a pointer IR value");
1665 if (parseOffset(Offset))
1667 Dest = MachinePointerInfo(V, Offset);
1671 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1672 if (expectAndConsume(MIToken::lparen))
1675 while (Token.isMemoryOperandFlag()) {
1676 if (parseMemoryOperandFlag(Flags))
1679 if (Token.isNot(MIToken::Identifier) ||
1680 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1681 return error("expected 'load' or 'store' memory operation");
1682 if (Token.stringValue() == "load")
1683 Flags |= MachineMemOperand::MOLoad;
1685 Flags |= MachineMemOperand::MOStore;
1688 if (Token.isNot(MIToken::IntegerLiteral))
1689 return error("expected the size integer literal after memory operation");
1691 if (getUint64(Size))
1695 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1696 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1697 return error(Twine("expected '") + Word + "'");
1700 MachinePointerInfo Ptr = MachinePointerInfo();
1701 if (parseMachinePointerInfo(Ptr))
1703 unsigned BaseAlignment = Size;
1705 MDNode *Range = nullptr;
1706 while (consumeIfPresent(MIToken::comma)) {
1707 switch (Token.kind()) {
1708 case MIToken::kw_align:
1709 if (parseAlignment(BaseAlignment))
1712 case MIToken::md_tbaa:
1714 if (parseMDNode(AAInfo.TBAA))
1717 case MIToken::md_alias_scope:
1719 if (parseMDNode(AAInfo.Scope))
1722 case MIToken::md_noalias:
1724 if (parseMDNode(AAInfo.NoAlias))
1727 case MIToken::md_range:
1729 if (parseMDNode(Range))
1732 // TODO: Report an error on duplicate metadata nodes.
1734 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1735 "'!noalias' or '!range'");
1738 if (expectAndConsume(MIToken::rparen))
1741 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1745 void MIParser::initNames2InstrOpCodes() {
1746 if (!Names2InstrOpCodes.empty())
1748 const auto *TII = MF.getSubtarget().getInstrInfo();
1749 assert(TII && "Expected target instruction info");
1750 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1751 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1754 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1755 initNames2InstrOpCodes();
1756 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1757 if (InstrInfo == Names2InstrOpCodes.end())
1759 OpCode = InstrInfo->getValue();
1763 void MIParser::initNames2Regs() {
1764 if (!Names2Regs.empty())
1766 // The '%noreg' register is the register 0.
1767 Names2Regs.insert(std::make_pair("noreg", 0));
1768 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1769 assert(TRI && "Expected target register info");
1770 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1772 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1775 assert(WasInserted && "Expected registers to be unique case-insensitively");
1779 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1781 auto RegInfo = Names2Regs.find(RegName);
1782 if (RegInfo == Names2Regs.end())
1784 Reg = RegInfo->getValue();
1788 void MIParser::initNames2RegMasks() {
1789 if (!Names2RegMasks.empty())
1791 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1792 assert(TRI && "Expected target register info");
1793 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1794 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1795 assert(RegMasks.size() == RegMaskNames.size());
1796 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1797 Names2RegMasks.insert(
1798 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1801 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1802 initNames2RegMasks();
1803 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1804 if (RegMaskInfo == Names2RegMasks.end())
1806 return RegMaskInfo->getValue();
1809 void MIParser::initNames2SubRegIndices() {
1810 if (!Names2SubRegIndices.empty())
1812 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1813 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1814 Names2SubRegIndices.insert(
1815 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1818 unsigned MIParser::getSubRegIndex(StringRef Name) {
1819 initNames2SubRegIndices();
1820 auto SubRegInfo = Names2SubRegIndices.find(Name);
1821 if (SubRegInfo == Names2SubRegIndices.end())
1823 return SubRegInfo->getValue();
1826 static void initSlots2BasicBlocks(
1828 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1829 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1830 MST.incorporateFunction(F);
1831 for (auto &BB : F) {
1834 int Slot = MST.getLocalSlot(&BB);
1837 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1841 static const BasicBlock *getIRBlockFromSlot(
1843 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1844 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1845 if (BlockInfo == Slots2BasicBlocks.end())
1847 return BlockInfo->second;
1850 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1851 if (Slots2BasicBlocks.empty())
1852 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1853 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1856 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1857 if (&F == MF.getFunction())
1858 return getIRBlock(Slot);
1859 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1860 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1861 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1864 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1865 DenseMap<unsigned, const Value *> &Slots2Values) {
1866 int Slot = MST.getLocalSlot(V);
1869 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1872 /// Creates the mapping from slot numbers to function's unnamed IR values.
1873 static void initSlots2Values(const Function &F,
1874 DenseMap<unsigned, const Value *> &Slots2Values) {
1875 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1876 MST.incorporateFunction(F);
1877 for (const auto &Arg : F.args())
1878 mapValueToSlot(&Arg, MST, Slots2Values);
1879 for (const auto &BB : F) {
1880 mapValueToSlot(&BB, MST, Slots2Values);
1881 for (const auto &I : BB)
1882 mapValueToSlot(&I, MST, Slots2Values);
1886 const Value *MIParser::getIRValue(unsigned Slot) {
1887 if (Slots2Values.empty())
1888 initSlots2Values(*MF.getFunction(), Slots2Values);
1889 auto ValueInfo = Slots2Values.find(Slot);
1890 if (ValueInfo == Slots2Values.end())
1892 return ValueInfo->second;
1895 void MIParser::initNames2TargetIndices() {
1896 if (!Names2TargetIndices.empty())
1898 const auto *TII = MF.getSubtarget().getInstrInfo();
1899 assert(TII && "Expected target instruction info");
1900 auto Indices = TII->getSerializableTargetIndices();
1901 for (const auto &I : Indices)
1902 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1905 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1906 initNames2TargetIndices();
1907 auto IndexInfo = Names2TargetIndices.find(Name);
1908 if (IndexInfo == Names2TargetIndices.end())
1910 Index = IndexInfo->second;
1914 void MIParser::initNames2DirectTargetFlags() {
1915 if (!Names2DirectTargetFlags.empty())
1917 const auto *TII = MF.getSubtarget().getInstrInfo();
1918 assert(TII && "Expected target instruction info");
1919 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1920 for (const auto &I : Flags)
1921 Names2DirectTargetFlags.insert(
1922 std::make_pair(StringRef(I.second), I.first));
1925 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1926 initNames2DirectTargetFlags();
1927 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1928 if (FlagInfo == Names2DirectTargetFlags.end())
1930 Flag = FlagInfo->second;
1934 void MIParser::initNames2BitmaskTargetFlags() {
1935 if (!Names2BitmaskTargetFlags.empty())
1937 const auto *TII = MF.getSubtarget().getInstrInfo();
1938 assert(TII && "Expected target instruction info");
1939 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1940 for (const auto &I : Flags)
1941 Names2BitmaskTargetFlags.insert(
1942 std::make_pair(StringRef(I.second), I.first));
1945 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1946 initNames2BitmaskTargetFlags();
1947 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1948 if (FlagInfo == Names2BitmaskTargetFlags.end())
1950 Flag = FlagInfo->second;
1954 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1955 PerFunctionMIParsingState &PFS,
1956 const SlotMapping &IRSlots,
1957 SMDiagnostic &Error) {
1959 SM.AddNewSourceBuffer(
1960 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1962 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1963 .parseBasicBlockDefinitions(PFS.MBBSlots);
1966 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1967 const PerFunctionMIParsingState &PFS,
1968 const SlotMapping &IRSlots,
1969 SMDiagnostic &Error) {
1971 SM.AddNewSourceBuffer(
1972 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1974 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
1977 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1978 MachineFunction &MF, StringRef Src,
1979 const PerFunctionMIParsingState &PFS,
1980 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1981 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
1984 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1985 MachineFunction &MF, StringRef Src,
1986 const PerFunctionMIParsingState &PFS,
1987 const SlotMapping &IRSlots,
1988 SMDiagnostic &Error) {
1989 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1990 .parseStandaloneNamedRegister(Reg);
1993 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1994 MachineFunction &MF, StringRef Src,
1995 const PerFunctionMIParsingState &PFS,
1996 const SlotMapping &IRSlots,
1997 SMDiagnostic &Error) {
1998 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1999 .parseStandaloneVirtualRegister(Reg);
2002 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
2003 MachineFunction &MF, StringRef Src,
2004 const PerFunctionMIParsingState &PFS,
2005 const SlotMapping &IRSlots,
2006 SMDiagnostic &Error) {
2007 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2008 .parseStandaloneStackObject(FI);
2011 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2012 StringRef Src, const PerFunctionMIParsingState &PFS,
2013 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2014 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);