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, StringRef Source,
127 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
128 bool parseTypedImmediateOperand(MachineOperand &Dest);
129 bool parseFPImmediateOperand(MachineOperand &Dest);
130 bool parseMBBReference(MachineBasicBlock *&MBB);
131 bool parseMBBOperand(MachineOperand &Dest);
132 bool parseStackFrameIndex(int &FI);
133 bool parseStackObjectOperand(MachineOperand &Dest);
134 bool parseFixedStackFrameIndex(int &FI);
135 bool parseFixedStackObjectOperand(MachineOperand &Dest);
136 bool parseGlobalValue(GlobalValue *&GV);
137 bool parseGlobalAddressOperand(MachineOperand &Dest);
138 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
139 bool parseJumpTableIndexOperand(MachineOperand &Dest);
140 bool parseExternalSymbolOperand(MachineOperand &Dest);
141 bool parseMDNode(MDNode *&Node);
142 bool parseMetadataOperand(MachineOperand &Dest);
143 bool parseCFIOffset(int &Offset);
144 bool parseCFIRegister(unsigned &Reg);
145 bool parseCFIOperand(MachineOperand &Dest);
146 bool parseIRBlock(BasicBlock *&BB, const Function &F);
147 bool parseBlockAddressOperand(MachineOperand &Dest);
148 bool parseTargetIndexOperand(MachineOperand &Dest);
149 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
150 bool parseMachineOperand(MachineOperand &Dest,
151 Optional<unsigned> &TiedDefIdx);
152 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
153 Optional<unsigned> &TiedDefIdx);
154 bool parseOffset(int64_t &Offset);
155 bool parseAlignment(unsigned &Alignment);
156 bool parseOperandsOffset(MachineOperand &Op);
157 bool parseIRValue(const Value *&V);
158 bool parseMemoryOperandFlag(unsigned &Flags);
159 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
160 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
161 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
164 /// Convert the integer literal in the current token into an unsigned integer.
166 /// Return true if an error occurred.
167 bool getUnsigned(unsigned &Result);
169 /// Convert the integer literal in the current token into an uint64.
171 /// Return true if an error occurred.
172 bool getUint64(uint64_t &Result);
174 /// If the current token is of the given kind, consume it and return false.
175 /// Otherwise report an error and return true.
176 bool expectAndConsume(MIToken::TokenKind TokenKind);
178 /// If the current token is of the given kind, consume it and return true.
179 /// Otherwise return false.
180 bool consumeIfPresent(MIToken::TokenKind TokenKind);
182 void initNames2InstrOpCodes();
184 /// Try to convert an instruction name to an opcode. Return true if the
185 /// instruction name is invalid.
186 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
188 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
190 bool assignRegisterTies(MachineInstr &MI,
191 ArrayRef<ParsedMachineOperand> Operands);
193 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
194 const MCInstrDesc &MCID);
196 void initNames2Regs();
198 /// Try to convert a register name to a register number. Return true if the
199 /// register name is invalid.
200 bool getRegisterByName(StringRef RegName, unsigned &Reg);
202 void initNames2RegMasks();
204 /// Check if the given identifier is a name of a register mask.
206 /// Return null if the identifier isn't a register mask.
207 const uint32_t *getRegMask(StringRef Identifier);
209 void initNames2SubRegIndices();
211 /// Check if the given identifier is a name of a subregister index.
213 /// Return 0 if the name isn't a subregister index class.
214 unsigned getSubRegIndex(StringRef Name);
216 const BasicBlock *getIRBlock(unsigned Slot);
217 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
219 const Value *getIRValue(unsigned Slot);
221 void initNames2TargetIndices();
223 /// Try to convert a name of target index to the corresponding target index.
225 /// Return true if the name isn't a name of a target index.
226 bool getTargetIndex(StringRef Name, int &Index);
228 void initNames2DirectTargetFlags();
230 /// Try to convert a name of a direct target flag to the corresponding
233 /// Return true if the name isn't a name of a direct flag.
234 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
236 void initNames2BitmaskTargetFlags();
238 /// Try to convert a name of a bitmask target flag to the corresponding
241 /// Return true if the name isn't a name of a bitmask target flag.
242 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
245 } // end anonymous namespace
247 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
248 StringRef Source, const PerFunctionMIParsingState &PFS,
249 const SlotMapping &IRSlots)
250 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
251 PFS(PFS), IRSlots(IRSlots) {}
253 void MIParser::lex() {
254 CurrentSource = lexMIToken(
255 CurrentSource, Token,
256 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
259 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
261 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
262 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
263 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
264 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
265 // Create an ordinary diagnostic when the source manager's buffer is the
267 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
270 // Create a diagnostic for a YAML string literal.
271 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
272 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
277 static const char *toString(MIToken::TokenKind TokenKind) {
285 case MIToken::lparen:
287 case MIToken::rparen:
290 return "<unknown token>";
294 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
295 if (Token.isNot(TokenKind))
296 return error(Twine("expected ") + toString(TokenKind));
301 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
302 if (Token.isNot(TokenKind))
308 bool MIParser::parseBasicBlockDefinition(
309 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
310 assert(Token.is(MIToken::MachineBasicBlockLabel));
314 auto Loc = Token.location();
315 auto Name = Token.stringValue();
317 bool HasAddressTaken = false;
318 bool IsLandingPad = false;
319 unsigned Alignment = 0;
320 BasicBlock *BB = nullptr;
321 if (consumeIfPresent(MIToken::lparen)) {
323 // TODO: Report an error when multiple same attributes are specified.
324 switch (Token.kind()) {
325 case MIToken::kw_address_taken:
326 HasAddressTaken = true;
329 case MIToken::kw_landing_pad:
333 case MIToken::kw_align:
334 if (parseAlignment(Alignment))
337 case MIToken::IRBlock:
338 // TODO: Report an error when both name and ir block are specified.
339 if (parseIRBlock(BB, *MF.getFunction()))
346 } while (consumeIfPresent(MIToken::comma));
347 if (expectAndConsume(MIToken::rparen))
350 if (expectAndConsume(MIToken::colon))
354 BB = dyn_cast_or_null<BasicBlock>(
355 MF.getFunction()->getValueSymbolTable().lookup(Name));
357 return error(Loc, Twine("basic block '") + Name +
358 "' is not defined in the function '" +
361 auto *MBB = MF.CreateMachineBasicBlock(BB);
362 MF.insert(MF.end(), MBB);
363 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
365 return error(Loc, Twine("redefinition of machine basic block with id #") +
368 MBB->setAlignment(Alignment);
370 MBB->setHasAddressTaken();
371 MBB->setIsLandingPad(IsLandingPad);
375 bool MIParser::parseBasicBlockDefinitions(
376 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
378 // Skip until the first machine basic block.
379 while (Token.is(MIToken::Newline))
381 if (Token.isErrorOrEOF())
382 return Token.isError();
383 if (Token.isNot(MIToken::MachineBasicBlockLabel))
384 return error("expected a basic block definition before instructions");
385 unsigned BraceDepth = 0;
387 if (parseBasicBlockDefinition(MBBSlots))
389 bool IsAfterNewline = false;
390 // Skip until the next machine basic block.
392 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
393 Token.isErrorOrEOF())
395 else if (Token.is(MIToken::MachineBasicBlockLabel))
396 return error("basic block definition should be located at the start of "
398 else if (consumeIfPresent(MIToken::Newline)) {
399 IsAfterNewline = true;
402 IsAfterNewline = false;
403 if (Token.is(MIToken::lbrace))
405 if (Token.is(MIToken::rbrace)) {
407 return error("extraneous closing brace ('}')");
412 // Verify that we closed all of the '{' at the end of a file or a block.
413 if (!Token.isError() && BraceDepth)
414 return error("expected '}'"); // FIXME: Report a note that shows '{'.
415 } while (!Token.isErrorOrEOF());
416 return Token.isError();
419 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
420 assert(Token.is(MIToken::kw_liveins));
422 if (expectAndConsume(MIToken::colon))
424 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
427 if (Token.isNot(MIToken::NamedRegister))
428 return error("expected a named register");
430 if (parseRegister(Reg))
434 } while (consumeIfPresent(MIToken::comma));
438 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
439 assert(Token.is(MIToken::kw_successors));
441 if (expectAndConsume(MIToken::colon))
443 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
446 if (Token.isNot(MIToken::MachineBasicBlock))
447 return error("expected a machine basic block reference");
448 MachineBasicBlock *SuccMBB = nullptr;
449 if (parseMBBReference(SuccMBB))
453 if (consumeIfPresent(MIToken::lparen)) {
454 if (Token.isNot(MIToken::IntegerLiteral))
455 return error("expected an integer literal after '('");
456 if (getUnsigned(Weight))
459 if (expectAndConsume(MIToken::rparen))
462 MBB.addSuccessor(SuccMBB, Weight);
463 } while (consumeIfPresent(MIToken::comma));
467 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
468 // Skip the definition.
469 assert(Token.is(MIToken::MachineBasicBlockLabel));
471 if (consumeIfPresent(MIToken::lparen)) {
472 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
474 consumeIfPresent(MIToken::rparen);
476 consumeIfPresent(MIToken::colon);
478 // Parse the liveins and successors.
479 // N.B: Multiple lists of successors and liveins are allowed and they're
486 // liveins: %edi, %esi
488 if (Token.is(MIToken::kw_successors)) {
489 if (parseBasicBlockSuccessors(MBB))
491 } else if (Token.is(MIToken::kw_liveins)) {
492 if (parseBasicBlockLiveins(MBB))
494 } else if (consumeIfPresent(MIToken::Newline)) {
498 if (!Token.isNewlineOrEOF())
499 return error("expected line break at the end of a list");
503 // Parse the instructions.
504 bool IsInBundle = false;
505 MachineInstr *PrevMI = nullptr;
507 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
509 else if (consumeIfPresent(MIToken::Newline))
511 if (consumeIfPresent(MIToken::rbrace)) {
512 // The first parsing pass should verify that all closing '}' have an
518 MachineInstr *MI = nullptr;
521 MBB.insert(MBB.end(), MI);
523 PrevMI->setFlag(MachineInstr::BundledSucc);
524 MI->setFlag(MachineInstr::BundledPred);
527 if (Token.is(MIToken::lbrace)) {
529 return error("nested instruction bundles are not allowed");
531 // This instruction is the start of the bundle.
532 MI->setFlag(MachineInstr::BundledSucc);
534 if (!Token.is(MIToken::Newline))
535 // The next instruction can be on the same line.
538 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
544 bool MIParser::parseBasicBlocks() {
546 // Skip until the first machine basic block.
547 while (Token.is(MIToken::Newline))
549 if (Token.isErrorOrEOF())
550 return Token.isError();
551 // The first parsing pass should have verified that this token is a MBB label
552 // in the 'parseBasicBlockDefinitions' method.
553 assert(Token.is(MIToken::MachineBasicBlockLabel));
555 MachineBasicBlock *MBB = nullptr;
556 if (parseMBBReference(MBB))
558 if (parseBasicBlock(*MBB))
560 // The method 'parseBasicBlock' should parse the whole block until the next
561 // block or the end of file.
562 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
563 } while (Token.isNot(MIToken::Eof));
567 bool MIParser::parse(MachineInstr *&MI) {
568 // Parse any register operands before '='
569 MachineOperand MO = MachineOperand::CreateImm(0);
570 SmallVector<ParsedMachineOperand, 8> Operands;
571 while (Token.isRegister() || Token.isRegisterFlag()) {
572 auto Loc = Token.location();
573 Optional<unsigned> TiedDefIdx;
574 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
577 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
578 if (Token.isNot(MIToken::comma))
582 if (!Operands.empty() && expectAndConsume(MIToken::equal))
585 unsigned OpCode, Flags = 0;
586 if (Token.isError() || parseInstruction(OpCode, Flags))
589 // Parse the remaining machine operands.
590 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
591 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
592 auto Loc = Token.location();
593 Optional<unsigned> TiedDefIdx;
594 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
597 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
598 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
599 Token.is(MIToken::lbrace))
601 if (Token.isNot(MIToken::comma))
602 return error("expected ',' before the next machine operand");
606 DebugLoc DebugLocation;
607 if (Token.is(MIToken::kw_debug_location)) {
609 if (Token.isNot(MIToken::exclaim))
610 return error("expected a metadata node after 'debug-location'");
611 MDNode *Node = nullptr;
612 if (parseMDNode(Node))
614 DebugLocation = DebugLoc(Node);
617 // Parse the machine memory operands.
618 SmallVector<MachineMemOperand *, 2> MemOperands;
619 if (Token.is(MIToken::coloncolon)) {
621 while (!Token.isNewlineOrEOF()) {
622 MachineMemOperand *MemOp = nullptr;
623 if (parseMachineMemoryOperand(MemOp))
625 MemOperands.push_back(MemOp);
626 if (Token.isNewlineOrEOF())
628 if (Token.isNot(MIToken::comma))
629 return error("expected ',' before the next machine memory operand");
634 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
635 if (!MCID.isVariadic()) {
636 // FIXME: Move the implicit operand verification to the machine verifier.
637 if (verifyImplicitOperands(Operands, MCID))
641 // TODO: Check for extraneous machine operands.
642 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
644 for (const auto &Operand : Operands)
645 MI->addOperand(MF, Operand.Operand);
646 if (assignRegisterTies(*MI, Operands))
648 if (MemOperands.empty())
650 MachineInstr::mmo_iterator MemRefs =
651 MF.allocateMemRefsArray(MemOperands.size());
652 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
653 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
657 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
659 if (Token.isNot(MIToken::MachineBasicBlock))
660 return error("expected a machine basic block reference");
661 if (parseMBBReference(MBB))
664 if (Token.isNot(MIToken::Eof))
666 "expected end of string after the machine basic block reference");
670 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
672 if (Token.isNot(MIToken::NamedRegister))
673 return error("expected a named register");
674 if (parseRegister(Reg))
677 if (Token.isNot(MIToken::Eof))
678 return error("expected end of string after the register reference");
682 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
684 if (Token.isNot(MIToken::VirtualRegister))
685 return error("expected a virtual register");
686 if (parseRegister(Reg))
689 if (Token.isNot(MIToken::Eof))
690 return error("expected end of string after the register reference");
694 bool MIParser::parseStandaloneStackObject(int &FI) {
696 if (Token.isNot(MIToken::StackObject))
697 return error("expected a stack object");
698 if (parseStackFrameIndex(FI))
700 if (Token.isNot(MIToken::Eof))
701 return error("expected end of string after the stack object reference");
705 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
707 if (Token.isNot(MIToken::exclaim))
708 return error("expected a metadata node");
709 if (parseMDNode(Node))
711 if (Token.isNot(MIToken::Eof))
712 return error("expected end of string after the metadata node");
716 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
717 assert(MO.isImplicit());
718 return MO.isDef() ? "implicit-def" : "implicit";
721 static std::string getRegisterName(const TargetRegisterInfo *TRI,
723 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
724 return StringRef(TRI->getName(Reg)).lower();
727 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
728 const MCInstrDesc &MCID) {
730 // We can't verify call instructions as they can contain arbitrary implicit
731 // register and register mask operands.
734 // Gather all the expected implicit operands.
735 SmallVector<MachineOperand, 4> ImplicitOperands;
736 if (MCID.ImplicitDefs)
737 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
738 ImplicitOperands.push_back(
739 MachineOperand::CreateReg(*ImpDefs, true, true));
740 if (MCID.ImplicitUses)
741 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
742 ImplicitOperands.push_back(
743 MachineOperand::CreateReg(*ImpUses, false, true));
745 const auto *TRI = MF.getSubtarget().getRegisterInfo();
746 assert(TRI && "Expected target register info");
747 size_t I = ImplicitOperands.size(), J = Operands.size();
752 const auto &ImplicitOperand = ImplicitOperands[I];
753 const auto &Operand = Operands[J].Operand;
754 if (ImplicitOperand.isIdenticalTo(Operand))
756 if (Operand.isReg() && Operand.isImplicit()) {
757 // Check if this implicit register is a subregister of an explicit
759 bool IsImplicitSubRegister = false;
760 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
761 const auto &Op = Operands[K].Operand;
762 if (Op.isReg() && !Op.isImplicit() &&
763 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
764 IsImplicitSubRegister = true;
768 if (IsImplicitSubRegister)
770 return error(Operands[J].Begin,
771 Twine("expected an implicit register operand '") +
772 printImplicitRegisterFlag(ImplicitOperand) + " %" +
773 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
776 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
777 // insead of reporting an error at this location:
780 // report the error at the following location:
783 return error(J < Operands.size() ? Operands[J].End : Token.location(),
784 Twine("missing implicit register operand '") +
785 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
786 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
791 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
792 if (Token.is(MIToken::kw_frame_setup)) {
793 Flags |= MachineInstr::FrameSetup;
796 if (Token.isNot(MIToken::Identifier))
797 return error("expected a machine instruction");
798 StringRef InstrName = Token.stringValue();
799 if (parseInstrName(InstrName, OpCode))
800 return error(Twine("unknown machine instruction name '") + InstrName + "'");
805 bool MIParser::parseRegister(unsigned &Reg) {
806 switch (Token.kind()) {
807 case MIToken::underscore:
810 case MIToken::NamedRegister: {
811 StringRef Name = Token.stringValue();
812 if (getRegisterByName(Name, Reg))
813 return error(Twine("unknown register name '") + Name + "'");
816 case MIToken::VirtualRegister: {
820 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
821 if (RegInfo == PFS.VirtualRegisterSlots.end())
822 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
824 Reg = RegInfo->second;
827 // TODO: Parse other register kinds.
829 llvm_unreachable("The current token should be a register");
834 bool MIParser::parseRegisterFlag(unsigned &Flags) {
835 const unsigned OldFlags = Flags;
836 switch (Token.kind()) {
837 case MIToken::kw_implicit:
838 Flags |= RegState::Implicit;
840 case MIToken::kw_implicit_define:
841 Flags |= RegState::ImplicitDefine;
843 case MIToken::kw_def:
844 Flags |= RegState::Define;
846 case MIToken::kw_dead:
847 Flags |= RegState::Dead;
849 case MIToken::kw_killed:
850 Flags |= RegState::Kill;
852 case MIToken::kw_undef:
853 Flags |= RegState::Undef;
855 case MIToken::kw_internal:
856 Flags |= RegState::InternalRead;
858 case MIToken::kw_early_clobber:
859 Flags |= RegState::EarlyClobber;
861 case MIToken::kw_debug_use:
862 Flags |= RegState::Debug;
865 llvm_unreachable("The current token should be a register flag");
867 if (OldFlags == Flags)
868 // We know that the same flag is specified more than once when the flags
870 return error("duplicate '" + Token.stringValue() + "' register flag");
875 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
876 assert(Token.is(MIToken::colon));
878 if (Token.isNot(MIToken::Identifier))
879 return error("expected a subregister index after ':'");
880 auto Name = Token.stringValue();
881 SubReg = getSubRegIndex(Name);
883 return error(Twine("use of unknown subregister index '") + Name + "'");
888 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
889 if (!consumeIfPresent(MIToken::kw_tied_def))
890 return error("expected 'tied-def' after '('");
891 if (Token.isNot(MIToken::IntegerLiteral))
892 return error("expected an integer literal after 'tied-def'");
893 if (getUnsigned(TiedDefIdx))
896 if (expectAndConsume(MIToken::rparen))
901 bool MIParser::assignRegisterTies(MachineInstr &MI,
902 ArrayRef<ParsedMachineOperand> Operands) {
903 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
904 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
905 if (!Operands[I].TiedDefIdx)
907 // The parser ensures that this operand is a register use, so we just have
908 // to check the tied-def operand.
909 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
911 return error(Operands[I].Begin,
912 Twine("use of invalid tied-def operand index '" +
913 Twine(DefIdx) + "'; instruction has only ") +
914 Twine(E) + " operands");
915 const auto &DefOperand = Operands[DefIdx].Operand;
916 if (!DefOperand.isReg() || !DefOperand.isDef())
917 // FIXME: add note with the def operand.
918 return error(Operands[I].Begin,
919 Twine("use of invalid tied-def operand index '") +
920 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
921 " isn't a defined register");
922 // Check that the tied-def operand wasn't tied elsewhere.
923 for (const auto &TiedPair : TiedRegisterPairs) {
924 if (TiedPair.first == DefIdx)
925 return error(Operands[I].Begin,
926 Twine("the tied-def operand #") + Twine(DefIdx) +
927 " is already tied with another register operand");
929 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
931 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
932 // indices must be less than tied max.
933 for (const auto &TiedPair : TiedRegisterPairs)
934 MI.tieOperands(TiedPair.first, TiedPair.second);
938 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
939 Optional<unsigned> &TiedDefIdx,
942 unsigned Flags = IsDef ? RegState::Define : 0;
943 while (Token.isRegisterFlag()) {
944 if (parseRegisterFlag(Flags))
947 if (!Token.isRegister())
948 return error("expected a register after register flags");
949 if (parseRegister(Reg))
953 if (Token.is(MIToken::colon)) {
954 if (parseSubRegisterIndex(SubReg))
957 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
959 if (parseRegisterTiedDefIndex(Idx))
963 Dest = MachineOperand::CreateReg(
964 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
965 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
966 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
967 Flags & RegState::InternalRead);
971 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
972 assert(Token.is(MIToken::IntegerLiteral));
973 const APSInt &Int = Token.integerValue();
974 if (Int.getMinSignedBits() > 64)
975 return error("integer literal is too large to be an immediate operand");
976 Dest = MachineOperand::CreateImm(Int.getExtValue());
981 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
982 const Constant *&C) {
983 auto Source = StringValue.str(); // The source has to be null terminated.
985 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
988 return error(Loc + Err.getColumnNo(), Err.getMessage());
992 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
993 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
999 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1000 assert(Token.is(MIToken::IntegerType));
1001 auto Loc = Token.location();
1003 if (Token.isNot(MIToken::IntegerLiteral))
1004 return error("expected an integer literal");
1005 const Constant *C = nullptr;
1006 if (parseIRConstant(Loc, C))
1008 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1012 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1013 auto Loc = Token.location();
1015 if (Token.isNot(MIToken::FloatingPointLiteral))
1016 return error("expected a floating point literal");
1017 const Constant *C = nullptr;
1018 if (parseIRConstant(Loc, C))
1020 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1024 bool MIParser::getUnsigned(unsigned &Result) {
1025 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1026 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1027 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1029 return error("expected 32-bit integer (too large)");
1034 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1035 assert(Token.is(MIToken::MachineBasicBlock) ||
1036 Token.is(MIToken::MachineBasicBlockLabel));
1038 if (getUnsigned(Number))
1040 auto MBBInfo = PFS.MBBSlots.find(Number);
1041 if (MBBInfo == PFS.MBBSlots.end())
1042 return error(Twine("use of undefined machine basic block #") +
1044 MBB = MBBInfo->second;
1045 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1046 return error(Twine("the name of machine basic block #") + Twine(Number) +
1047 " isn't '" + Token.stringValue() + "'");
1051 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1052 MachineBasicBlock *MBB;
1053 if (parseMBBReference(MBB))
1055 Dest = MachineOperand::CreateMBB(MBB);
1060 bool MIParser::parseStackFrameIndex(int &FI) {
1061 assert(Token.is(MIToken::StackObject));
1063 if (getUnsigned(ID))
1065 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1066 if (ObjectInfo == PFS.StackObjectSlots.end())
1067 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1070 if (const auto *Alloca =
1071 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1072 Name = Alloca->getName();
1073 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1074 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1075 "' isn't '" + Token.stringValue() + "'");
1077 FI = ObjectInfo->second;
1081 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1083 if (parseStackFrameIndex(FI))
1085 Dest = MachineOperand::CreateFI(FI);
1089 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1090 assert(Token.is(MIToken::FixedStackObject));
1092 if (getUnsigned(ID))
1094 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1095 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1096 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1099 FI = ObjectInfo->second;
1103 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1105 if (parseFixedStackFrameIndex(FI))
1107 Dest = MachineOperand::CreateFI(FI);
1111 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1112 switch (Token.kind()) {
1113 case MIToken::NamedGlobalValue: {
1114 const Module *M = MF.getFunction()->getParent();
1115 GV = M->getNamedValue(Token.stringValue());
1117 return error(Twine("use of undefined global value '") + Token.range() +
1121 case MIToken::GlobalValue: {
1123 if (getUnsigned(GVIdx))
1125 if (GVIdx >= IRSlots.GlobalValues.size())
1126 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1128 GV = IRSlots.GlobalValues[GVIdx];
1132 llvm_unreachable("The current token should be a global value");
1137 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1138 GlobalValue *GV = nullptr;
1139 if (parseGlobalValue(GV))
1142 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1143 if (parseOperandsOffset(Dest))
1148 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1149 assert(Token.is(MIToken::ConstantPoolItem));
1151 if (getUnsigned(ID))
1153 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1154 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1155 return error("use of undefined constant '%const." + Twine(ID) + "'");
1157 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1158 if (parseOperandsOffset(Dest))
1163 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1164 assert(Token.is(MIToken::JumpTableIndex));
1166 if (getUnsigned(ID))
1168 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1169 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1170 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1172 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1176 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1177 assert(Token.is(MIToken::ExternalSymbol));
1178 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1180 Dest = MachineOperand::CreateES(Symbol);
1181 if (parseOperandsOffset(Dest))
1186 bool MIParser::parseMDNode(MDNode *&Node) {
1187 assert(Token.is(MIToken::exclaim));
1188 auto Loc = Token.location();
1190 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1191 return error("expected metadata id after '!'");
1193 if (getUnsigned(ID))
1195 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1196 if (NodeInfo == IRSlots.MetadataNodes.end())
1197 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1199 Node = NodeInfo->second.get();
1203 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1204 MDNode *Node = nullptr;
1205 if (parseMDNode(Node))
1207 Dest = MachineOperand::CreateMetadata(Node);
1211 bool MIParser::parseCFIOffset(int &Offset) {
1212 if (Token.isNot(MIToken::IntegerLiteral))
1213 return error("expected a cfi offset");
1214 if (Token.integerValue().getMinSignedBits() > 32)
1215 return error("expected a 32 bit integer (the cfi offset is too large)");
1216 Offset = (int)Token.integerValue().getExtValue();
1221 bool MIParser::parseCFIRegister(unsigned &Reg) {
1222 if (Token.isNot(MIToken::NamedRegister))
1223 return error("expected a cfi register");
1225 if (parseRegister(LLVMReg))
1227 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1228 assert(TRI && "Expected target register info");
1229 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1231 return error("invalid DWARF register");
1232 Reg = (unsigned)DwarfReg;
1237 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1238 auto Kind = Token.kind();
1240 auto &MMI = MF.getMMI();
1245 case MIToken::kw_cfi_same_value:
1246 if (parseCFIRegister(Reg))
1249 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1251 case MIToken::kw_cfi_offset:
1252 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1253 parseCFIOffset(Offset))
1256 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1258 case MIToken::kw_cfi_def_cfa_register:
1259 if (parseCFIRegister(Reg))
1262 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1264 case MIToken::kw_cfi_def_cfa_offset:
1265 if (parseCFIOffset(Offset))
1267 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1268 CFIIndex = MMI.addFrameInst(
1269 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1271 case MIToken::kw_cfi_def_cfa:
1272 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1273 parseCFIOffset(Offset))
1275 // NB: MCCFIInstruction::createDefCfa negates the offset.
1277 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1280 // TODO: Parse the other CFI operands.
1281 llvm_unreachable("The current token should be a cfi operand");
1283 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1287 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1288 switch (Token.kind()) {
1289 case MIToken::NamedIRBlock: {
1290 BB = dyn_cast_or_null<BasicBlock>(
1291 F.getValueSymbolTable().lookup(Token.stringValue()));
1293 return error(Twine("use of undefined IR block '") + Token.range() + "'");
1296 case MIToken::IRBlock: {
1297 unsigned SlotNumber = 0;
1298 if (getUnsigned(SlotNumber))
1300 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1302 return error(Twine("use of undefined IR block '%ir-block.") +
1303 Twine(SlotNumber) + "'");
1307 llvm_unreachable("The current token should be an IR block reference");
1312 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1313 assert(Token.is(MIToken::kw_blockaddress));
1315 if (expectAndConsume(MIToken::lparen))
1317 if (Token.isNot(MIToken::GlobalValue) &&
1318 Token.isNot(MIToken::NamedGlobalValue))
1319 return error("expected a global value");
1320 GlobalValue *GV = nullptr;
1321 if (parseGlobalValue(GV))
1323 auto *F = dyn_cast<Function>(GV);
1325 return error("expected an IR function reference");
1327 if (expectAndConsume(MIToken::comma))
1329 BasicBlock *BB = nullptr;
1330 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1331 return error("expected an IR block reference");
1332 if (parseIRBlock(BB, *F))
1335 if (expectAndConsume(MIToken::rparen))
1337 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1338 if (parseOperandsOffset(Dest))
1343 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1344 assert(Token.is(MIToken::kw_target_index));
1346 if (expectAndConsume(MIToken::lparen))
1348 if (Token.isNot(MIToken::Identifier))
1349 return error("expected the name of the target index");
1351 if (getTargetIndex(Token.stringValue(), Index))
1352 return error("use of undefined target index '" + Token.stringValue() + "'");
1354 if (expectAndConsume(MIToken::rparen))
1356 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1357 if (parseOperandsOffset(Dest))
1362 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1363 assert(Token.is(MIToken::kw_liveout));
1364 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1365 assert(TRI && "Expected target register info");
1366 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1368 if (expectAndConsume(MIToken::lparen))
1371 if (Token.isNot(MIToken::NamedRegister))
1372 return error("expected a named register");
1374 if (parseRegister(Reg))
1377 Mask[Reg / 32] |= 1U << (Reg % 32);
1378 // TODO: Report an error if the same register is used more than once.
1379 if (Token.isNot(MIToken::comma))
1383 if (expectAndConsume(MIToken::rparen))
1385 Dest = MachineOperand::CreateRegLiveOut(Mask);
1389 bool MIParser::parseMachineOperand(MachineOperand &Dest,
1390 Optional<unsigned> &TiedDefIdx) {
1391 switch (Token.kind()) {
1392 case MIToken::kw_implicit:
1393 case MIToken::kw_implicit_define:
1394 case MIToken::kw_def:
1395 case MIToken::kw_dead:
1396 case MIToken::kw_killed:
1397 case MIToken::kw_undef:
1398 case MIToken::kw_internal:
1399 case MIToken::kw_early_clobber:
1400 case MIToken::kw_debug_use:
1401 case MIToken::underscore:
1402 case MIToken::NamedRegister:
1403 case MIToken::VirtualRegister:
1404 return parseRegisterOperand(Dest, TiedDefIdx);
1405 case MIToken::IntegerLiteral:
1406 return parseImmediateOperand(Dest);
1407 case MIToken::IntegerType:
1408 return parseTypedImmediateOperand(Dest);
1409 case MIToken::kw_half:
1410 case MIToken::kw_float:
1411 case MIToken::kw_double:
1412 case MIToken::kw_x86_fp80:
1413 case MIToken::kw_fp128:
1414 case MIToken::kw_ppc_fp128:
1415 return parseFPImmediateOperand(Dest);
1416 case MIToken::MachineBasicBlock:
1417 return parseMBBOperand(Dest);
1418 case MIToken::StackObject:
1419 return parseStackObjectOperand(Dest);
1420 case MIToken::FixedStackObject:
1421 return parseFixedStackObjectOperand(Dest);
1422 case MIToken::GlobalValue:
1423 case MIToken::NamedGlobalValue:
1424 return parseGlobalAddressOperand(Dest);
1425 case MIToken::ConstantPoolItem:
1426 return parseConstantPoolIndexOperand(Dest);
1427 case MIToken::JumpTableIndex:
1428 return parseJumpTableIndexOperand(Dest);
1429 case MIToken::ExternalSymbol:
1430 return parseExternalSymbolOperand(Dest);
1431 case MIToken::exclaim:
1432 return parseMetadataOperand(Dest);
1433 case MIToken::kw_cfi_same_value:
1434 case MIToken::kw_cfi_offset:
1435 case MIToken::kw_cfi_def_cfa_register:
1436 case MIToken::kw_cfi_def_cfa_offset:
1437 case MIToken::kw_cfi_def_cfa:
1438 return parseCFIOperand(Dest);
1439 case MIToken::kw_blockaddress:
1440 return parseBlockAddressOperand(Dest);
1441 case MIToken::kw_target_index:
1442 return parseTargetIndexOperand(Dest);
1443 case MIToken::kw_liveout:
1444 return parseLiveoutRegisterMaskOperand(Dest);
1445 case MIToken::Error:
1447 case MIToken::Identifier:
1448 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1449 Dest = MachineOperand::CreateRegMask(RegMask);
1455 // FIXME: Parse the MCSymbol machine operand.
1456 return error("expected a machine operand");
1461 bool MIParser::parseMachineOperandAndTargetFlags(
1462 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1464 bool HasTargetFlags = false;
1465 if (Token.is(MIToken::kw_target_flags)) {
1466 HasTargetFlags = true;
1468 if (expectAndConsume(MIToken::lparen))
1470 if (Token.isNot(MIToken::Identifier))
1471 return error("expected the name of the target flag");
1472 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1473 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1474 return error("use of undefined target flag '" + Token.stringValue() +
1478 while (Token.is(MIToken::comma)) {
1480 if (Token.isNot(MIToken::Identifier))
1481 return error("expected the name of the target flag");
1482 unsigned BitFlag = 0;
1483 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1484 return error("use of undefined target flag '" + Token.stringValue() +
1486 // TODO: Report an error when using a duplicate bit target flag.
1490 if (expectAndConsume(MIToken::rparen))
1493 auto Loc = Token.location();
1494 if (parseMachineOperand(Dest, TiedDefIdx))
1496 if (!HasTargetFlags)
1499 return error(Loc, "register operands can't have target flags");
1500 Dest.setTargetFlags(TF);
1504 bool MIParser::parseOffset(int64_t &Offset) {
1505 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1507 StringRef Sign = Token.range();
1508 bool IsNegative = Token.is(MIToken::minus);
1510 if (Token.isNot(MIToken::IntegerLiteral))
1511 return error("expected an integer literal after '" + Sign + "'");
1512 if (Token.integerValue().getMinSignedBits() > 64)
1513 return error("expected 64-bit integer (too large)");
1514 Offset = Token.integerValue().getExtValue();
1521 bool MIParser::parseAlignment(unsigned &Alignment) {
1522 assert(Token.is(MIToken::kw_align));
1524 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1525 return error("expected an integer literal after 'align'");
1526 if (getUnsigned(Alignment))
1532 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1534 if (parseOffset(Offset))
1536 Op.setOffset(Offset);
1540 bool MIParser::parseIRValue(const Value *&V) {
1541 switch (Token.kind()) {
1542 case MIToken::NamedIRValue: {
1543 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1546 case MIToken::IRValue: {
1547 unsigned SlotNumber = 0;
1548 if (getUnsigned(SlotNumber))
1550 V = getIRValue(SlotNumber);
1553 case MIToken::NamedGlobalValue:
1554 case MIToken::GlobalValue: {
1555 GlobalValue *GV = nullptr;
1556 if (parseGlobalValue(GV))
1561 case MIToken::QuotedIRValue: {
1562 const Constant *C = nullptr;
1563 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1569 llvm_unreachable("The current token should be an IR block reference");
1572 return error(Twine("use of undefined IR value '") + Token.range() + "'");
1576 bool MIParser::getUint64(uint64_t &Result) {
1577 assert(Token.hasIntegerValue());
1578 if (Token.integerValue().getActiveBits() > 64)
1579 return error("expected 64-bit integer (too large)");
1580 Result = Token.integerValue().getZExtValue();
1584 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1585 const unsigned OldFlags = Flags;
1586 switch (Token.kind()) {
1587 case MIToken::kw_volatile:
1588 Flags |= MachineMemOperand::MOVolatile;
1590 case MIToken::kw_non_temporal:
1591 Flags |= MachineMemOperand::MONonTemporal;
1593 case MIToken::kw_invariant:
1594 Flags |= MachineMemOperand::MOInvariant;
1596 // TODO: parse the target specific memory operand flags.
1598 llvm_unreachable("The current token should be a memory operand flag");
1600 if (OldFlags == Flags)
1601 // We know that the same flag is specified more than once when the flags
1602 // weren't modified.
1603 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1608 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1609 switch (Token.kind()) {
1610 case MIToken::kw_stack:
1611 PSV = MF.getPSVManager().getStack();
1613 case MIToken::kw_got:
1614 PSV = MF.getPSVManager().getGOT();
1616 case MIToken::kw_jump_table:
1617 PSV = MF.getPSVManager().getJumpTable();
1619 case MIToken::kw_constant_pool:
1620 PSV = MF.getPSVManager().getConstantPool();
1622 case MIToken::FixedStackObject: {
1624 if (parseFixedStackFrameIndex(FI))
1626 PSV = MF.getPSVManager().getFixedStack(FI);
1627 // The token was already consumed, so use return here instead of break.
1630 case MIToken::kw_call_entry: {
1632 switch (Token.kind()) {
1633 case MIToken::GlobalValue:
1634 case MIToken::NamedGlobalValue: {
1635 GlobalValue *GV = nullptr;
1636 if (parseGlobalValue(GV))
1638 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1641 case MIToken::ExternalSymbol:
1642 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1643 MF.createExternalSymbolName(Token.stringValue()));
1647 "expected a global value or an external symbol after 'call-entry'");
1652 llvm_unreachable("The current token should be pseudo source value");
1658 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1659 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1660 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1661 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
1662 const PseudoSourceValue *PSV = nullptr;
1663 if (parseMemoryPseudoSourceValue(PSV))
1666 if (parseOffset(Offset))
1668 Dest = MachinePointerInfo(PSV, Offset);
1671 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1672 Token.isNot(MIToken::GlobalValue) &&
1673 Token.isNot(MIToken::NamedGlobalValue) &&
1674 Token.isNot(MIToken::QuotedIRValue))
1675 return error("expected an IR value reference");
1676 const Value *V = nullptr;
1677 if (parseIRValue(V))
1679 if (!V->getType()->isPointerTy())
1680 return error("expected a pointer IR value");
1683 if (parseOffset(Offset))
1685 Dest = MachinePointerInfo(V, Offset);
1689 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1690 if (expectAndConsume(MIToken::lparen))
1693 while (Token.isMemoryOperandFlag()) {
1694 if (parseMemoryOperandFlag(Flags))
1697 if (Token.isNot(MIToken::Identifier) ||
1698 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1699 return error("expected 'load' or 'store' memory operation");
1700 if (Token.stringValue() == "load")
1701 Flags |= MachineMemOperand::MOLoad;
1703 Flags |= MachineMemOperand::MOStore;
1706 if (Token.isNot(MIToken::IntegerLiteral))
1707 return error("expected the size integer literal after memory operation");
1709 if (getUint64(Size))
1713 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1714 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1715 return error(Twine("expected '") + Word + "'");
1718 MachinePointerInfo Ptr = MachinePointerInfo();
1719 if (parseMachinePointerInfo(Ptr))
1721 unsigned BaseAlignment = Size;
1723 MDNode *Range = nullptr;
1724 while (consumeIfPresent(MIToken::comma)) {
1725 switch (Token.kind()) {
1726 case MIToken::kw_align:
1727 if (parseAlignment(BaseAlignment))
1730 case MIToken::md_tbaa:
1732 if (parseMDNode(AAInfo.TBAA))
1735 case MIToken::md_alias_scope:
1737 if (parseMDNode(AAInfo.Scope))
1740 case MIToken::md_noalias:
1742 if (parseMDNode(AAInfo.NoAlias))
1745 case MIToken::md_range:
1747 if (parseMDNode(Range))
1750 // TODO: Report an error on duplicate metadata nodes.
1752 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1753 "'!noalias' or '!range'");
1756 if (expectAndConsume(MIToken::rparen))
1759 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1763 void MIParser::initNames2InstrOpCodes() {
1764 if (!Names2InstrOpCodes.empty())
1766 const auto *TII = MF.getSubtarget().getInstrInfo();
1767 assert(TII && "Expected target instruction info");
1768 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1769 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1772 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1773 initNames2InstrOpCodes();
1774 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1775 if (InstrInfo == Names2InstrOpCodes.end())
1777 OpCode = InstrInfo->getValue();
1781 void MIParser::initNames2Regs() {
1782 if (!Names2Regs.empty())
1784 // The '%noreg' register is the register 0.
1785 Names2Regs.insert(std::make_pair("noreg", 0));
1786 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1787 assert(TRI && "Expected target register info");
1788 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1790 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1793 assert(WasInserted && "Expected registers to be unique case-insensitively");
1797 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1799 auto RegInfo = Names2Regs.find(RegName);
1800 if (RegInfo == Names2Regs.end())
1802 Reg = RegInfo->getValue();
1806 void MIParser::initNames2RegMasks() {
1807 if (!Names2RegMasks.empty())
1809 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1810 assert(TRI && "Expected target register info");
1811 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1812 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1813 assert(RegMasks.size() == RegMaskNames.size());
1814 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1815 Names2RegMasks.insert(
1816 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1819 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1820 initNames2RegMasks();
1821 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1822 if (RegMaskInfo == Names2RegMasks.end())
1824 return RegMaskInfo->getValue();
1827 void MIParser::initNames2SubRegIndices() {
1828 if (!Names2SubRegIndices.empty())
1830 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1831 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1832 Names2SubRegIndices.insert(
1833 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1836 unsigned MIParser::getSubRegIndex(StringRef Name) {
1837 initNames2SubRegIndices();
1838 auto SubRegInfo = Names2SubRegIndices.find(Name);
1839 if (SubRegInfo == Names2SubRegIndices.end())
1841 return SubRegInfo->getValue();
1844 static void initSlots2BasicBlocks(
1846 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1847 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1848 MST.incorporateFunction(F);
1849 for (auto &BB : F) {
1852 int Slot = MST.getLocalSlot(&BB);
1855 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1859 static const BasicBlock *getIRBlockFromSlot(
1861 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1862 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1863 if (BlockInfo == Slots2BasicBlocks.end())
1865 return BlockInfo->second;
1868 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1869 if (Slots2BasicBlocks.empty())
1870 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1871 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1874 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1875 if (&F == MF.getFunction())
1876 return getIRBlock(Slot);
1877 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1878 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1879 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1882 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1883 DenseMap<unsigned, const Value *> &Slots2Values) {
1884 int Slot = MST.getLocalSlot(V);
1887 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1890 /// Creates the mapping from slot numbers to function's unnamed IR values.
1891 static void initSlots2Values(const Function &F,
1892 DenseMap<unsigned, const Value *> &Slots2Values) {
1893 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1894 MST.incorporateFunction(F);
1895 for (const auto &Arg : F.args())
1896 mapValueToSlot(&Arg, MST, Slots2Values);
1897 for (const auto &BB : F) {
1898 mapValueToSlot(&BB, MST, Slots2Values);
1899 for (const auto &I : BB)
1900 mapValueToSlot(&I, MST, Slots2Values);
1904 const Value *MIParser::getIRValue(unsigned Slot) {
1905 if (Slots2Values.empty())
1906 initSlots2Values(*MF.getFunction(), Slots2Values);
1907 auto ValueInfo = Slots2Values.find(Slot);
1908 if (ValueInfo == Slots2Values.end())
1910 return ValueInfo->second;
1913 void MIParser::initNames2TargetIndices() {
1914 if (!Names2TargetIndices.empty())
1916 const auto *TII = MF.getSubtarget().getInstrInfo();
1917 assert(TII && "Expected target instruction info");
1918 auto Indices = TII->getSerializableTargetIndices();
1919 for (const auto &I : Indices)
1920 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1923 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1924 initNames2TargetIndices();
1925 auto IndexInfo = Names2TargetIndices.find(Name);
1926 if (IndexInfo == Names2TargetIndices.end())
1928 Index = IndexInfo->second;
1932 void MIParser::initNames2DirectTargetFlags() {
1933 if (!Names2DirectTargetFlags.empty())
1935 const auto *TII = MF.getSubtarget().getInstrInfo();
1936 assert(TII && "Expected target instruction info");
1937 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1938 for (const auto &I : Flags)
1939 Names2DirectTargetFlags.insert(
1940 std::make_pair(StringRef(I.second), I.first));
1943 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1944 initNames2DirectTargetFlags();
1945 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1946 if (FlagInfo == Names2DirectTargetFlags.end())
1948 Flag = FlagInfo->second;
1952 void MIParser::initNames2BitmaskTargetFlags() {
1953 if (!Names2BitmaskTargetFlags.empty())
1955 const auto *TII = MF.getSubtarget().getInstrInfo();
1956 assert(TII && "Expected target instruction info");
1957 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1958 for (const auto &I : Flags)
1959 Names2BitmaskTargetFlags.insert(
1960 std::make_pair(StringRef(I.second), I.first));
1963 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1964 initNames2BitmaskTargetFlags();
1965 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1966 if (FlagInfo == Names2BitmaskTargetFlags.end())
1968 Flag = FlagInfo->second;
1972 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1973 PerFunctionMIParsingState &PFS,
1974 const SlotMapping &IRSlots,
1975 SMDiagnostic &Error) {
1977 SM.AddNewSourceBuffer(
1978 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1980 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1981 .parseBasicBlockDefinitions(PFS.MBBSlots);
1984 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1985 const PerFunctionMIParsingState &PFS,
1986 const SlotMapping &IRSlots,
1987 SMDiagnostic &Error) {
1989 SM.AddNewSourceBuffer(
1990 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1992 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
1995 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1996 MachineFunction &MF, StringRef Src,
1997 const PerFunctionMIParsingState &PFS,
1998 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1999 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
2002 bool llvm::parseNamedRegisterReference(unsigned &Reg, 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 .parseStandaloneNamedRegister(Reg);
2011 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
2012 MachineFunction &MF, StringRef Src,
2013 const PerFunctionMIParsingState &PFS,
2014 const SlotMapping &IRSlots,
2015 SMDiagnostic &Error) {
2016 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2017 .parseStandaloneVirtualRegister(Reg);
2020 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
2021 MachineFunction &MF, StringRef Src,
2022 const PerFunctionMIParsingState &PFS,
2023 const SlotMapping &IRSlots,
2024 SMDiagnostic &Error) {
2025 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2026 .parseStandaloneStackObject(FI);
2029 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2030 StringRef Src, const PerFunctionMIParsingState &PFS,
2031 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2032 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);