1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the parsing of machine instructions.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/AsmParser/Parser.h"
18 #include "llvm/AsmParser/SlotMapping.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineMemOperand.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ModuleSlotTracker.h"
30 #include "llvm/IR/ValueSymbolTable.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
34 #include "llvm/Target/TargetInstrInfo.h"
40 /// A wrapper struct around the 'MachineOperand' struct that includes a source
42 struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
56 StringRef Source, CurrentSource;
58 const PerFunctionMIParsingState &PFS;
59 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
61 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
63 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
65 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
67 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
69 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
71 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
73 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
77 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
78 StringRef Source, const PerFunctionMIParsingState &PFS,
79 const SlotMapping &IRSlots);
83 /// Report an error at the current location with the given message.
85 /// This function always return true.
86 bool error(const Twine &Msg);
88 /// Report an error at the given location with the given message.
90 /// This function always return true.
91 bool error(StringRef::iterator Loc, const Twine &Msg);
93 bool parse(MachineInstr *&MI);
94 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
95 bool parseStandaloneNamedRegister(unsigned &Reg);
96 bool parseStandaloneVirtualRegister(unsigned &Reg);
97 bool parseStandaloneIRBlockReference(const BasicBlock *&BB);
99 bool parseRegister(unsigned &Reg);
100 bool parseRegisterFlag(unsigned &Flags);
101 bool parseSubRegisterIndex(unsigned &SubReg);
102 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
103 bool parseImmediateOperand(MachineOperand &Dest);
104 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
105 bool parseTypedImmediateOperand(MachineOperand &Dest);
106 bool parseFPImmediateOperand(MachineOperand &Dest);
107 bool parseMBBReference(MachineBasicBlock *&MBB);
108 bool parseMBBOperand(MachineOperand &Dest);
109 bool parseStackObjectOperand(MachineOperand &Dest);
110 bool parseFixedStackObjectOperand(MachineOperand &Dest);
111 bool parseGlobalValue(GlobalValue *&GV);
112 bool parseGlobalAddressOperand(MachineOperand &Dest);
113 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
114 bool parseJumpTableIndexOperand(MachineOperand &Dest);
115 bool parseExternalSymbolOperand(MachineOperand &Dest);
116 bool parseMDNode(MDNode *&Node);
117 bool parseMetadataOperand(MachineOperand &Dest);
118 bool parseCFIOffset(int &Offset);
119 bool parseCFIRegister(unsigned &Reg);
120 bool parseCFIOperand(MachineOperand &Dest);
121 bool parseIRBlock(BasicBlock *&BB, const Function &F);
122 bool parseBlockAddressOperand(MachineOperand &Dest);
123 bool parseTargetIndexOperand(MachineOperand &Dest);
124 bool parseMachineOperand(MachineOperand &Dest);
125 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
126 bool parseOperandsOffset(MachineOperand &Op);
127 bool parseIRValue(Value *&V);
128 bool parseMemoryOperandFlag(unsigned &Flags);
129 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
132 /// Convert the integer literal in the current token into an unsigned integer.
134 /// Return true if an error occurred.
135 bool getUnsigned(unsigned &Result);
137 /// Convert the integer literal in the current token into an uint64.
139 /// Return true if an error occurred.
140 bool getUint64(uint64_t &Result);
142 /// If the current token is of the given kind, consume it and return false.
143 /// Otherwise report an error and return true.
144 bool expectAndConsume(MIToken::TokenKind TokenKind);
146 void initNames2InstrOpCodes();
148 /// Try to convert an instruction name to an opcode. Return true if the
149 /// instruction name is invalid.
150 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
152 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
154 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
155 const MCInstrDesc &MCID);
157 void initNames2Regs();
159 /// Try to convert a register name to a register number. Return true if the
160 /// register name is invalid.
161 bool getRegisterByName(StringRef RegName, unsigned &Reg);
163 void initNames2RegMasks();
165 /// Check if the given identifier is a name of a register mask.
167 /// Return null if the identifier isn't a register mask.
168 const uint32_t *getRegMask(StringRef Identifier);
170 void initNames2SubRegIndices();
172 /// Check if the given identifier is a name of a subregister index.
174 /// Return 0 if the name isn't a subregister index class.
175 unsigned getSubRegIndex(StringRef Name);
177 void initSlots2BasicBlocks();
179 const BasicBlock *getIRBlock(unsigned Slot);
181 void initNames2TargetIndices();
183 /// Try to convert a name of target index to the corresponding target index.
185 /// Return true if the name isn't a name of a target index.
186 bool getTargetIndex(StringRef Name, int &Index);
188 void initNames2DirectTargetFlags();
190 /// Try to convert a name of a direct target flag to the corresponding
193 /// Return true if the name isn't a name of a direct flag.
194 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
197 } // end anonymous namespace
199 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
200 StringRef Source, const PerFunctionMIParsingState &PFS,
201 const SlotMapping &IRSlots)
202 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
203 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
205 void MIParser::lex() {
206 CurrentSource = lexMIToken(
207 CurrentSource, Token,
208 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
211 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
213 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
214 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
215 Error = SMDiagnostic(
217 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
218 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
222 static const char *toString(MIToken::TokenKind TokenKind) {
228 case MIToken::lparen:
230 case MIToken::rparen:
233 return "<unknown token>";
237 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
238 if (Token.isNot(TokenKind))
239 return error(Twine("expected ") + toString(TokenKind));
244 bool MIParser::parse(MachineInstr *&MI) {
247 // Parse any register operands before '='
248 MachineOperand MO = MachineOperand::CreateImm(0);
249 SmallVector<MachineOperandWithLocation, 8> Operands;
250 while (Token.isRegister() || Token.isRegisterFlag()) {
251 auto Loc = Token.location();
252 if (parseRegisterOperand(MO, /*IsDef=*/true))
254 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
255 if (Token.isNot(MIToken::comma))
259 if (!Operands.empty() && expectAndConsume(MIToken::equal))
262 unsigned OpCode, Flags = 0;
263 if (Token.isError() || parseInstruction(OpCode, Flags))
266 // TODO: Parse the bundle instruction flags.
268 // Parse the remaining machine operands.
269 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
270 Token.isNot(MIToken::coloncolon)) {
271 auto Loc = Token.location();
272 if (parseMachineOperandAndTargetFlags(MO))
274 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
275 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
277 if (Token.isNot(MIToken::comma))
278 return error("expected ',' before the next machine operand");
282 DebugLoc DebugLocation;
283 if (Token.is(MIToken::kw_debug_location)) {
285 if (Token.isNot(MIToken::exclaim))
286 return error("expected a metadata node after 'debug-location'");
287 MDNode *Node = nullptr;
288 if (parseMDNode(Node))
290 DebugLocation = DebugLoc(Node);
293 // Parse the machine memory operands.
294 SmallVector<MachineMemOperand *, 2> MemOperands;
295 if (Token.is(MIToken::coloncolon)) {
297 while (Token.isNot(MIToken::Eof)) {
298 MachineMemOperand *MemOp = nullptr;
299 if (parseMachineMemoryOperand(MemOp))
301 MemOperands.push_back(MemOp);
302 if (Token.is(MIToken::Eof))
304 if (Token.isNot(MIToken::comma))
305 return error("expected ',' before the next machine memory operand");
310 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
311 if (!MCID.isVariadic()) {
312 // FIXME: Move the implicit operand verification to the machine verifier.
313 if (verifyImplicitOperands(Operands, MCID))
317 // TODO: Check for extraneous machine operands.
318 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
320 for (const auto &Operand : Operands)
321 MI->addOperand(MF, Operand.Operand);
322 if (MemOperands.empty())
324 MachineInstr::mmo_iterator MemRefs =
325 MF.allocateMemRefsArray(MemOperands.size());
326 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
327 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
331 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
333 if (Token.isNot(MIToken::MachineBasicBlock))
334 return error("expected a machine basic block reference");
335 if (parseMBBReference(MBB))
338 if (Token.isNot(MIToken::Eof))
340 "expected end of string after the machine basic block reference");
344 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
346 if (Token.isNot(MIToken::NamedRegister))
347 return error("expected a named register");
348 if (parseRegister(Reg))
351 if (Token.isNot(MIToken::Eof))
352 return error("expected end of string after the register reference");
356 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
358 if (Token.isNot(MIToken::VirtualRegister))
359 return error("expected a virtual register");
360 if (parseRegister(Reg))
363 if (Token.isNot(MIToken::Eof))
364 return error("expected end of string after the register reference");
368 bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
370 if (Token.isNot(MIToken::IRBlock))
371 return error("expected an IR block reference");
372 unsigned SlotNumber = 0;
373 if (getUnsigned(SlotNumber))
375 BB = getIRBlock(SlotNumber);
377 return error(Twine("use of undefined IR block '%ir-block.") +
378 Twine(SlotNumber) + "'");
380 if (Token.isNot(MIToken::Eof))
381 return error("expected end of string after the IR block reference");
385 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
386 assert(MO.isImplicit());
387 return MO.isDef() ? "implicit-def" : "implicit";
390 static std::string getRegisterName(const TargetRegisterInfo *TRI,
392 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
393 return StringRef(TRI->getName(Reg)).lower();
396 bool MIParser::verifyImplicitOperands(
397 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
399 // We can't verify call instructions as they can contain arbitrary implicit
400 // register and register mask operands.
403 // Gather all the expected implicit operands.
404 SmallVector<MachineOperand, 4> ImplicitOperands;
405 if (MCID.ImplicitDefs)
406 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
407 ImplicitOperands.push_back(
408 MachineOperand::CreateReg(*ImpDefs, true, true));
409 if (MCID.ImplicitUses)
410 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
411 ImplicitOperands.push_back(
412 MachineOperand::CreateReg(*ImpUses, false, true));
414 const auto *TRI = MF.getSubtarget().getRegisterInfo();
415 assert(TRI && "Expected target register info");
416 size_t I = ImplicitOperands.size(), J = Operands.size();
421 const auto &ImplicitOperand = ImplicitOperands[I];
422 const auto &Operand = Operands[J].Operand;
423 if (ImplicitOperand.isIdenticalTo(Operand))
425 if (Operand.isReg() && Operand.isImplicit()) {
426 return error(Operands[J].Begin,
427 Twine("expected an implicit register operand '") +
428 printImplicitRegisterFlag(ImplicitOperand) + " %" +
429 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
432 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
433 // insead of reporting an error at this location:
436 // report the error at the following location:
439 return error(J < Operands.size() ? Operands[J].End : Token.location(),
440 Twine("missing implicit register operand '") +
441 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
442 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
447 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
448 if (Token.is(MIToken::kw_frame_setup)) {
449 Flags |= MachineInstr::FrameSetup;
452 if (Token.isNot(MIToken::Identifier))
453 return error("expected a machine instruction");
454 StringRef InstrName = Token.stringValue();
455 if (parseInstrName(InstrName, OpCode))
456 return error(Twine("unknown machine instruction name '") + InstrName + "'");
461 bool MIParser::parseRegister(unsigned &Reg) {
462 switch (Token.kind()) {
463 case MIToken::underscore:
466 case MIToken::NamedRegister: {
467 StringRef Name = Token.stringValue();
468 if (getRegisterByName(Name, Reg))
469 return error(Twine("unknown register name '") + Name + "'");
472 case MIToken::VirtualRegister: {
476 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
477 if (RegInfo == PFS.VirtualRegisterSlots.end())
478 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
480 Reg = RegInfo->second;
483 // TODO: Parse other register kinds.
485 llvm_unreachable("The current token should be a register");
490 bool MIParser::parseRegisterFlag(unsigned &Flags) {
491 const unsigned OldFlags = Flags;
492 switch (Token.kind()) {
493 case MIToken::kw_implicit:
494 Flags |= RegState::Implicit;
496 case MIToken::kw_implicit_define:
497 Flags |= RegState::ImplicitDefine;
499 case MIToken::kw_dead:
500 Flags |= RegState::Dead;
502 case MIToken::kw_killed:
503 Flags |= RegState::Kill;
505 case MIToken::kw_undef:
506 Flags |= RegState::Undef;
508 case MIToken::kw_early_clobber:
509 Flags |= RegState::EarlyClobber;
511 case MIToken::kw_debug_use:
512 Flags |= RegState::Debug;
514 // TODO: parse the other register flags.
516 llvm_unreachable("The current token should be a register flag");
518 if (OldFlags == Flags)
519 // We know that the same flag is specified more than once when the flags
521 return error("duplicate '" + Token.stringValue() + "' register flag");
526 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
527 assert(Token.is(MIToken::colon));
529 if (Token.isNot(MIToken::Identifier))
530 return error("expected a subregister index after ':'");
531 auto Name = Token.stringValue();
532 SubReg = getSubRegIndex(Name);
534 return error(Twine("use of unknown subregister index '") + Name + "'");
539 bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
541 unsigned Flags = IsDef ? RegState::Define : 0;
542 while (Token.isRegisterFlag()) {
543 if (parseRegisterFlag(Flags))
546 if (!Token.isRegister())
547 return error("expected a register after register flags");
548 if (parseRegister(Reg))
552 if (Token.is(MIToken::colon)) {
553 if (parseSubRegisterIndex(SubReg))
556 Dest = MachineOperand::CreateReg(
557 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
558 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
559 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
563 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
564 assert(Token.is(MIToken::IntegerLiteral));
565 const APSInt &Int = Token.integerValue();
566 if (Int.getMinSignedBits() > 64)
567 return error("integer literal is too large to be an immediate operand");
568 Dest = MachineOperand::CreateImm(Int.getExtValue());
573 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
574 auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str();
577 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
579 return error(Loc + Err.getColumnNo(), Err.getMessage());
583 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
584 assert(Token.is(MIToken::IntegerType));
585 auto Loc = Token.location();
587 if (Token.isNot(MIToken::IntegerLiteral))
588 return error("expected an integer literal");
589 const Constant *C = nullptr;
590 if (parseIRConstant(Loc, C))
592 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
596 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
597 auto Loc = Token.location();
599 if (Token.isNot(MIToken::FloatingPointLiteral))
600 return error("expected a floating point literal");
601 const Constant *C = nullptr;
602 if (parseIRConstant(Loc, C))
604 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
608 bool MIParser::getUnsigned(unsigned &Result) {
609 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
610 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
611 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
613 return error("expected 32-bit integer (too large)");
618 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
619 assert(Token.is(MIToken::MachineBasicBlock));
621 if (getUnsigned(Number))
623 auto MBBInfo = PFS.MBBSlots.find(Number);
624 if (MBBInfo == PFS.MBBSlots.end())
625 return error(Twine("use of undefined machine basic block #") +
627 MBB = MBBInfo->second;
628 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
629 return error(Twine("the name of machine basic block #") + Twine(Number) +
630 " isn't '" + Token.stringValue() + "'");
634 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
635 MachineBasicBlock *MBB;
636 if (parseMBBReference(MBB))
638 Dest = MachineOperand::CreateMBB(MBB);
643 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
644 assert(Token.is(MIToken::StackObject));
648 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
649 if (ObjectInfo == PFS.StackObjectSlots.end())
650 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
653 if (const auto *Alloca =
654 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
655 Name = Alloca->getName();
656 if (!Token.stringValue().empty() && Token.stringValue() != Name)
657 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
658 "' isn't '" + Token.stringValue() + "'");
660 Dest = MachineOperand::CreateFI(ObjectInfo->second);
664 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
665 assert(Token.is(MIToken::FixedStackObject));
669 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
670 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
671 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
674 Dest = MachineOperand::CreateFI(ObjectInfo->second);
678 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
679 switch (Token.kind()) {
680 case MIToken::NamedGlobalValue: {
681 const Module *M = MF.getFunction()->getParent();
682 GV = M->getNamedValue(Token.stringValue());
684 return error(Twine("use of undefined global value '@") +
685 Token.rawStringValue() + "'");
688 case MIToken::GlobalValue: {
690 if (getUnsigned(GVIdx))
692 if (GVIdx >= IRSlots.GlobalValues.size())
693 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
695 GV = IRSlots.GlobalValues[GVIdx];
699 llvm_unreachable("The current token should be a global value");
704 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
705 GlobalValue *GV = nullptr;
706 if (parseGlobalValue(GV))
709 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
710 if (parseOperandsOffset(Dest))
715 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
716 assert(Token.is(MIToken::ConstantPoolItem));
720 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
721 if (ConstantInfo == PFS.ConstantPoolSlots.end())
722 return error("use of undefined constant '%const." + Twine(ID) + "'");
724 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
725 if (parseOperandsOffset(Dest))
730 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
731 assert(Token.is(MIToken::JumpTableIndex));
735 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
736 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
737 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
739 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
743 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
744 assert(Token.is(MIToken::ExternalSymbol));
745 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
747 Dest = MachineOperand::CreateES(Symbol);
748 if (parseOperandsOffset(Dest))
753 bool MIParser::parseMDNode(MDNode *&Node) {
754 assert(Token.is(MIToken::exclaim));
755 auto Loc = Token.location();
757 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
758 return error("expected metadata id after '!'");
762 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
763 if (NodeInfo == IRSlots.MetadataNodes.end())
764 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
766 Node = NodeInfo->second.get();
770 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
771 MDNode *Node = nullptr;
772 if (parseMDNode(Node))
774 Dest = MachineOperand::CreateMetadata(Node);
778 bool MIParser::parseCFIOffset(int &Offset) {
779 if (Token.isNot(MIToken::IntegerLiteral))
780 return error("expected a cfi offset");
781 if (Token.integerValue().getMinSignedBits() > 32)
782 return error("expected a 32 bit integer (the cfi offset is too large)");
783 Offset = (int)Token.integerValue().getExtValue();
788 bool MIParser::parseCFIRegister(unsigned &Reg) {
789 if (Token.isNot(MIToken::NamedRegister))
790 return error("expected a cfi register");
792 if (parseRegister(LLVMReg))
794 const auto *TRI = MF.getSubtarget().getRegisterInfo();
795 assert(TRI && "Expected target register info");
796 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
798 return error("invalid DWARF register");
799 Reg = (unsigned)DwarfReg;
804 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
805 auto Kind = Token.kind();
807 auto &MMI = MF.getMMI();
812 case MIToken::kw_cfi_offset:
813 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
814 parseCFIOffset(Offset))
817 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
819 case MIToken::kw_cfi_def_cfa_register:
820 if (parseCFIRegister(Reg))
823 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
825 case MIToken::kw_cfi_def_cfa_offset:
826 if (parseCFIOffset(Offset))
828 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
829 CFIIndex = MMI.addFrameInst(
830 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
832 case MIToken::kw_cfi_def_cfa:
833 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
834 parseCFIOffset(Offset))
836 // NB: MCCFIInstruction::createDefCfa negates the offset.
838 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
841 // TODO: Parse the other CFI operands.
842 llvm_unreachable("The current token should be a cfi operand");
844 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
848 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
849 switch (Token.kind()) {
850 case MIToken::NamedIRBlock: {
851 BB = dyn_cast_or_null<BasicBlock>(
852 F.getValueSymbolTable().lookup(Token.stringValue()));
854 return error(Twine("use of undefined IR block '%ir-block.") +
855 Token.rawStringValue() + "'");
858 case MIToken::IRBlock: {
859 unsigned SlotNumber = 0;
860 if (getUnsigned(SlotNumber))
862 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber));
864 return error(Twine("use of undefined IR block '%ir-block.") +
865 Twine(SlotNumber) + "'");
869 llvm_unreachable("The current token should be an IR block reference");
874 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
875 assert(Token.is(MIToken::kw_blockaddress));
877 if (expectAndConsume(MIToken::lparen))
879 if (Token.isNot(MIToken::GlobalValue) &&
880 Token.isNot(MIToken::NamedGlobalValue))
881 return error("expected a global value");
882 GlobalValue *GV = nullptr;
883 if (parseGlobalValue(GV))
885 auto *F = dyn_cast<Function>(GV);
887 return error("expected an IR function reference");
889 if (expectAndConsume(MIToken::comma))
891 BasicBlock *BB = nullptr;
892 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
893 return error("expected an IR block reference");
894 if (parseIRBlock(BB, *F))
897 if (expectAndConsume(MIToken::rparen))
899 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
900 if (parseOperandsOffset(Dest))
905 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
906 assert(Token.is(MIToken::kw_target_index));
908 if (expectAndConsume(MIToken::lparen))
910 if (Token.isNot(MIToken::Identifier))
911 return error("expected the name of the target index");
913 if (getTargetIndex(Token.stringValue(), Index))
914 return error("use of undefined target index '" + Token.stringValue() + "'");
916 if (expectAndConsume(MIToken::rparen))
918 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
919 if (parseOperandsOffset(Dest))
924 bool MIParser::parseMachineOperand(MachineOperand &Dest) {
925 switch (Token.kind()) {
926 case MIToken::kw_implicit:
927 case MIToken::kw_implicit_define:
928 case MIToken::kw_dead:
929 case MIToken::kw_killed:
930 case MIToken::kw_undef:
931 case MIToken::kw_early_clobber:
932 case MIToken::kw_debug_use:
933 case MIToken::underscore:
934 case MIToken::NamedRegister:
935 case MIToken::VirtualRegister:
936 return parseRegisterOperand(Dest);
937 case MIToken::IntegerLiteral:
938 return parseImmediateOperand(Dest);
939 case MIToken::IntegerType:
940 return parseTypedImmediateOperand(Dest);
941 case MIToken::kw_half:
942 case MIToken::kw_float:
943 case MIToken::kw_double:
944 case MIToken::kw_x86_fp80:
945 case MIToken::kw_fp128:
946 case MIToken::kw_ppc_fp128:
947 return parseFPImmediateOperand(Dest);
948 case MIToken::MachineBasicBlock:
949 return parseMBBOperand(Dest);
950 case MIToken::StackObject:
951 return parseStackObjectOperand(Dest);
952 case MIToken::FixedStackObject:
953 return parseFixedStackObjectOperand(Dest);
954 case MIToken::GlobalValue:
955 case MIToken::NamedGlobalValue:
956 return parseGlobalAddressOperand(Dest);
957 case MIToken::ConstantPoolItem:
958 return parseConstantPoolIndexOperand(Dest);
959 case MIToken::JumpTableIndex:
960 return parseJumpTableIndexOperand(Dest);
961 case MIToken::ExternalSymbol:
962 return parseExternalSymbolOperand(Dest);
963 case MIToken::exclaim:
964 return parseMetadataOperand(Dest);
965 case MIToken::kw_cfi_offset:
966 case MIToken::kw_cfi_def_cfa_register:
967 case MIToken::kw_cfi_def_cfa_offset:
968 case MIToken::kw_cfi_def_cfa:
969 return parseCFIOperand(Dest);
970 case MIToken::kw_blockaddress:
971 return parseBlockAddressOperand(Dest);
972 case MIToken::kw_target_index:
973 return parseTargetIndexOperand(Dest);
976 case MIToken::Identifier:
977 if (const auto *RegMask = getRegMask(Token.stringValue())) {
978 Dest = MachineOperand::CreateRegMask(RegMask);
984 // TODO: parse the other machine operands.
985 return error("expected a machine operand");
990 bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
992 bool HasTargetFlags = false;
993 if (Token.is(MIToken::kw_target_flags)) {
994 HasTargetFlags = true;
996 if (expectAndConsume(MIToken::lparen))
998 if (Token.isNot(MIToken::Identifier))
999 return error("expected the name of the target flag");
1000 if (getDirectTargetFlag(Token.stringValue(), TF))
1001 return error("use of undefined target flag '" + Token.stringValue() +
1004 // TODO: Parse target's bit target flags.
1005 if (expectAndConsume(MIToken::rparen))
1008 auto Loc = Token.location();
1009 if (parseMachineOperand(Dest))
1011 if (!HasTargetFlags)
1014 return error(Loc, "register operands can't have target flags");
1015 Dest.setTargetFlags(TF);
1019 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1020 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1022 StringRef Sign = Token.stringValue();
1023 bool IsNegative = Token.is(MIToken::minus);
1025 if (Token.isNot(MIToken::IntegerLiteral))
1026 return error("expected an integer literal after '" + Sign + "'");
1027 if (Token.integerValue().getMinSignedBits() > 64)
1028 return error("expected 64-bit integer (too large)");
1029 int64_t Offset = Token.integerValue().getExtValue();
1033 Op.setOffset(Offset);
1037 bool MIParser::parseIRValue(Value *&V) {
1038 switch (Token.kind()) {
1039 case MIToken::NamedIRValue: {
1040 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1042 return error(Twine("use of undefined IR value '%ir.") +
1043 Token.rawStringValue() + "'");
1046 // TODO: Parse unnamed IR value references.
1048 llvm_unreachable("The current token should be an IR block reference");
1053 bool MIParser::getUint64(uint64_t &Result) {
1054 assert(Token.hasIntegerValue());
1055 if (Token.integerValue().getActiveBits() > 64)
1056 return error("expected 64-bit integer (too large)");
1057 Result = Token.integerValue().getZExtValue();
1061 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1062 switch (Token.kind()) {
1063 case MIToken::kw_volatile:
1064 Flags |= MachineMemOperand::MOVolatile;
1066 case MIToken::kw_non_temporal:
1067 Flags |= MachineMemOperand::MONonTemporal;
1069 case MIToken::kw_invariant:
1070 Flags |= MachineMemOperand::MOInvariant;
1072 // TODO: report an error when we specify the same flag more than once.
1073 // TODO: parse the target specific memory operand flags.
1075 llvm_unreachable("The current token should be a memory operand flag");
1081 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1082 if (expectAndConsume(MIToken::lparen))
1085 while (Token.isMemoryOperandFlag()) {
1086 if (parseMemoryOperandFlag(Flags))
1089 if (Token.isNot(MIToken::Identifier) ||
1090 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1091 return error("expected 'load' or 'store' memory operation");
1092 if (Token.stringValue() == "load")
1093 Flags |= MachineMemOperand::MOLoad;
1095 Flags |= MachineMemOperand::MOStore;
1098 if (Token.isNot(MIToken::IntegerLiteral))
1099 return error("expected the size integer literal after memory operation");
1101 if (getUint64(Size))
1105 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1106 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1107 return error(Twine("expected '") + Word + "'");
1110 // TODO: Parse pseudo source values.
1111 if (Token.isNot(MIToken::NamedIRValue))
1112 return error("expected an IR value reference");
1114 if (parseIRValue(V))
1116 if (!V->getType()->isPointerTy())
1117 return error("expected a pointer IR value");
1119 // TODO: Parse the base alignment.
1120 // TODO: Parse the attached metadata nodes.
1121 if (expectAndConsume(MIToken::rparen))
1124 Dest = MF.getMachineMemOperand(MachinePointerInfo(V), Flags, Size, Size);
1128 void MIParser::initNames2InstrOpCodes() {
1129 if (!Names2InstrOpCodes.empty())
1131 const auto *TII = MF.getSubtarget().getInstrInfo();
1132 assert(TII && "Expected target instruction info");
1133 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1134 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1137 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1138 initNames2InstrOpCodes();
1139 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1140 if (InstrInfo == Names2InstrOpCodes.end())
1142 OpCode = InstrInfo->getValue();
1146 void MIParser::initNames2Regs() {
1147 if (!Names2Regs.empty())
1149 // The '%noreg' register is the register 0.
1150 Names2Regs.insert(std::make_pair("noreg", 0));
1151 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1152 assert(TRI && "Expected target register info");
1153 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1155 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1158 assert(WasInserted && "Expected registers to be unique case-insensitively");
1162 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1164 auto RegInfo = Names2Regs.find(RegName);
1165 if (RegInfo == Names2Regs.end())
1167 Reg = RegInfo->getValue();
1171 void MIParser::initNames2RegMasks() {
1172 if (!Names2RegMasks.empty())
1174 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1175 assert(TRI && "Expected target register info");
1176 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1177 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1178 assert(RegMasks.size() == RegMaskNames.size());
1179 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1180 Names2RegMasks.insert(
1181 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1184 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1185 initNames2RegMasks();
1186 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1187 if (RegMaskInfo == Names2RegMasks.end())
1189 return RegMaskInfo->getValue();
1192 void MIParser::initNames2SubRegIndices() {
1193 if (!Names2SubRegIndices.empty())
1195 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1196 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1197 Names2SubRegIndices.insert(
1198 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1201 unsigned MIParser::getSubRegIndex(StringRef Name) {
1202 initNames2SubRegIndices();
1203 auto SubRegInfo = Names2SubRegIndices.find(Name);
1204 if (SubRegInfo == Names2SubRegIndices.end())
1206 return SubRegInfo->getValue();
1209 void MIParser::initSlots2BasicBlocks() {
1210 if (!Slots2BasicBlocks.empty())
1212 const auto &F = *MF.getFunction();
1213 ModuleSlotTracker MST(F.getParent());
1214 MST.incorporateFunction(F);
1215 for (auto &BB : F) {
1218 int Slot = MST.getLocalSlot(&BB);
1221 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1225 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1226 initSlots2BasicBlocks();
1227 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1228 if (BlockInfo == Slots2BasicBlocks.end())
1230 return BlockInfo->second;
1233 void MIParser::initNames2TargetIndices() {
1234 if (!Names2TargetIndices.empty())
1236 const auto *TII = MF.getSubtarget().getInstrInfo();
1237 assert(TII && "Expected target instruction info");
1238 auto Indices = TII->getSerializableTargetIndices();
1239 for (const auto &I : Indices)
1240 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1243 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1244 initNames2TargetIndices();
1245 auto IndexInfo = Names2TargetIndices.find(Name);
1246 if (IndexInfo == Names2TargetIndices.end())
1248 Index = IndexInfo->second;
1252 void MIParser::initNames2DirectTargetFlags() {
1253 if (!Names2DirectTargetFlags.empty())
1255 const auto *TII = MF.getSubtarget().getInstrInfo();
1256 assert(TII && "Expected target instruction info");
1257 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1258 for (const auto &I : Flags)
1259 Names2DirectTargetFlags.insert(
1260 std::make_pair(StringRef(I.second), I.first));
1263 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1264 initNames2DirectTargetFlags();
1265 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1266 if (FlagInfo == Names2DirectTargetFlags.end())
1268 Flag = FlagInfo->second;
1272 bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1273 MachineFunction &MF, StringRef Src,
1274 const PerFunctionMIParsingState &PFS,
1275 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1276 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
1279 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1280 MachineFunction &MF, StringRef Src,
1281 const PerFunctionMIParsingState &PFS,
1282 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1283 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
1286 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1287 MachineFunction &MF, StringRef Src,
1288 const PerFunctionMIParsingState &PFS,
1289 const SlotMapping &IRSlots,
1290 SMDiagnostic &Error) {
1291 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1292 .parseStandaloneNamedRegister(Reg);
1295 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1296 MachineFunction &MF, StringRef Src,
1297 const PerFunctionMIParsingState &PFS,
1298 const SlotMapping &IRSlots,
1299 SMDiagnostic &Error) {
1300 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1301 .parseStandaloneVirtualRegister(Reg);
1304 bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1305 MachineFunction &MF, StringRef Src,
1306 const PerFunctionMIParsingState &PFS,
1307 const SlotMapping &IRSlots,
1308 SMDiagnostic &Error) {
1309 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1310 .parseStandaloneIRBlockReference(BB);