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 const BasicBlock *getIRBlock(unsigned Slot);
178 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
180 void initNames2TargetIndices();
182 /// Try to convert a name of target index to the corresponding target index.
184 /// Return true if the name isn't a name of a target index.
185 bool getTargetIndex(StringRef Name, int &Index);
187 void initNames2DirectTargetFlags();
189 /// Try to convert a name of a direct target flag to the corresponding
192 /// Return true if the name isn't a name of a direct flag.
193 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
196 } // end anonymous namespace
198 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
199 StringRef Source, const PerFunctionMIParsingState &PFS,
200 const SlotMapping &IRSlots)
201 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
202 PFS(PFS), IRSlots(IRSlots) {}
204 void MIParser::lex() {
205 CurrentSource = lexMIToken(
206 CurrentSource, Token,
207 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
210 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
212 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
213 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
214 Error = SMDiagnostic(
216 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
217 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
221 static const char *toString(MIToken::TokenKind TokenKind) {
227 case MIToken::lparen:
229 case MIToken::rparen:
232 return "<unknown token>";
236 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
237 if (Token.isNot(TokenKind))
238 return error(Twine("expected ") + toString(TokenKind));
243 bool MIParser::parse(MachineInstr *&MI) {
246 // Parse any register operands before '='
247 MachineOperand MO = MachineOperand::CreateImm(0);
248 SmallVector<MachineOperandWithLocation, 8> Operands;
249 while (Token.isRegister() || Token.isRegisterFlag()) {
250 auto Loc = Token.location();
251 if (parseRegisterOperand(MO, /*IsDef=*/true))
253 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
254 if (Token.isNot(MIToken::comma))
258 if (!Operands.empty() && expectAndConsume(MIToken::equal))
261 unsigned OpCode, Flags = 0;
262 if (Token.isError() || parseInstruction(OpCode, Flags))
265 // TODO: Parse the bundle instruction flags.
267 // Parse the remaining machine operands.
268 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
269 Token.isNot(MIToken::coloncolon)) {
270 auto Loc = Token.location();
271 if (parseMachineOperandAndTargetFlags(MO))
273 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
274 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
276 if (Token.isNot(MIToken::comma))
277 return error("expected ',' before the next machine operand");
281 DebugLoc DebugLocation;
282 if (Token.is(MIToken::kw_debug_location)) {
284 if (Token.isNot(MIToken::exclaim))
285 return error("expected a metadata node after 'debug-location'");
286 MDNode *Node = nullptr;
287 if (parseMDNode(Node))
289 DebugLocation = DebugLoc(Node);
292 // Parse the machine memory operands.
293 SmallVector<MachineMemOperand *, 2> MemOperands;
294 if (Token.is(MIToken::coloncolon)) {
296 while (Token.isNot(MIToken::Eof)) {
297 MachineMemOperand *MemOp = nullptr;
298 if (parseMachineMemoryOperand(MemOp))
300 MemOperands.push_back(MemOp);
301 if (Token.is(MIToken::Eof))
303 if (Token.isNot(MIToken::comma))
304 return error("expected ',' before the next machine memory operand");
309 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
310 if (!MCID.isVariadic()) {
311 // FIXME: Move the implicit operand verification to the machine verifier.
312 if (verifyImplicitOperands(Operands, MCID))
316 // TODO: Check for extraneous machine operands.
317 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
319 for (const auto &Operand : Operands)
320 MI->addOperand(MF, Operand.Operand);
321 if (MemOperands.empty())
323 MachineInstr::mmo_iterator MemRefs =
324 MF.allocateMemRefsArray(MemOperands.size());
325 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
326 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
330 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
332 if (Token.isNot(MIToken::MachineBasicBlock))
333 return error("expected a machine basic block reference");
334 if (parseMBBReference(MBB))
337 if (Token.isNot(MIToken::Eof))
339 "expected end of string after the machine basic block reference");
343 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
345 if (Token.isNot(MIToken::NamedRegister))
346 return error("expected a named register");
347 if (parseRegister(Reg))
350 if (Token.isNot(MIToken::Eof))
351 return error("expected end of string after the register reference");
355 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
357 if (Token.isNot(MIToken::VirtualRegister))
358 return error("expected a virtual register");
359 if (parseRegister(Reg))
362 if (Token.isNot(MIToken::Eof))
363 return error("expected end of string after the register reference");
367 bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
369 if (Token.isNot(MIToken::IRBlock))
370 return error("expected an IR block reference");
371 unsigned SlotNumber = 0;
372 if (getUnsigned(SlotNumber))
374 BB = getIRBlock(SlotNumber);
376 return error(Twine("use of undefined IR block '%ir-block.") +
377 Twine(SlotNumber) + "'");
379 if (Token.isNot(MIToken::Eof))
380 return error("expected end of string after the IR block reference");
384 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
385 assert(MO.isImplicit());
386 return MO.isDef() ? "implicit-def" : "implicit";
389 static std::string getRegisterName(const TargetRegisterInfo *TRI,
391 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
392 return StringRef(TRI->getName(Reg)).lower();
395 bool MIParser::verifyImplicitOperands(
396 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
398 // We can't verify call instructions as they can contain arbitrary implicit
399 // register and register mask operands.
402 // Gather all the expected implicit operands.
403 SmallVector<MachineOperand, 4> ImplicitOperands;
404 if (MCID.ImplicitDefs)
405 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
406 ImplicitOperands.push_back(
407 MachineOperand::CreateReg(*ImpDefs, true, true));
408 if (MCID.ImplicitUses)
409 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
410 ImplicitOperands.push_back(
411 MachineOperand::CreateReg(*ImpUses, false, true));
413 const auto *TRI = MF.getSubtarget().getRegisterInfo();
414 assert(TRI && "Expected target register info");
415 size_t I = ImplicitOperands.size(), J = Operands.size();
420 const auto &ImplicitOperand = ImplicitOperands[I];
421 const auto &Operand = Operands[J].Operand;
422 if (ImplicitOperand.isIdenticalTo(Operand))
424 if (Operand.isReg() && Operand.isImplicit()) {
425 return error(Operands[J].Begin,
426 Twine("expected an implicit register operand '") +
427 printImplicitRegisterFlag(ImplicitOperand) + " %" +
428 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
431 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
432 // insead of reporting an error at this location:
435 // report the error at the following location:
438 return error(J < Operands.size() ? Operands[J].End : Token.location(),
439 Twine("missing implicit register operand '") +
440 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
441 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
446 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
447 if (Token.is(MIToken::kw_frame_setup)) {
448 Flags |= MachineInstr::FrameSetup;
451 if (Token.isNot(MIToken::Identifier))
452 return error("expected a machine instruction");
453 StringRef InstrName = Token.stringValue();
454 if (parseInstrName(InstrName, OpCode))
455 return error(Twine("unknown machine instruction name '") + InstrName + "'");
460 bool MIParser::parseRegister(unsigned &Reg) {
461 switch (Token.kind()) {
462 case MIToken::underscore:
465 case MIToken::NamedRegister: {
466 StringRef Name = Token.stringValue();
467 if (getRegisterByName(Name, Reg))
468 return error(Twine("unknown register name '") + Name + "'");
471 case MIToken::VirtualRegister: {
475 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
476 if (RegInfo == PFS.VirtualRegisterSlots.end())
477 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
479 Reg = RegInfo->second;
482 // TODO: Parse other register kinds.
484 llvm_unreachable("The current token should be a register");
489 bool MIParser::parseRegisterFlag(unsigned &Flags) {
490 const unsigned OldFlags = Flags;
491 switch (Token.kind()) {
492 case MIToken::kw_implicit:
493 Flags |= RegState::Implicit;
495 case MIToken::kw_implicit_define:
496 Flags |= RegState::ImplicitDefine;
498 case MIToken::kw_dead:
499 Flags |= RegState::Dead;
501 case MIToken::kw_killed:
502 Flags |= RegState::Kill;
504 case MIToken::kw_undef:
505 Flags |= RegState::Undef;
507 case MIToken::kw_early_clobber:
508 Flags |= RegState::EarlyClobber;
510 case MIToken::kw_debug_use:
511 Flags |= RegState::Debug;
513 // TODO: parse the other register flags.
515 llvm_unreachable("The current token should be a register flag");
517 if (OldFlags == Flags)
518 // We know that the same flag is specified more than once when the flags
520 return error("duplicate '" + Token.stringValue() + "' register flag");
525 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
526 assert(Token.is(MIToken::colon));
528 if (Token.isNot(MIToken::Identifier))
529 return error("expected a subregister index after ':'");
530 auto Name = Token.stringValue();
531 SubReg = getSubRegIndex(Name);
533 return error(Twine("use of unknown subregister index '") + Name + "'");
538 bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
540 unsigned Flags = IsDef ? RegState::Define : 0;
541 while (Token.isRegisterFlag()) {
542 if (parseRegisterFlag(Flags))
545 if (!Token.isRegister())
546 return error("expected a register after register flags");
547 if (parseRegister(Reg))
551 if (Token.is(MIToken::colon)) {
552 if (parseSubRegisterIndex(SubReg))
555 Dest = MachineOperand::CreateReg(
556 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
557 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
558 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
562 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
563 assert(Token.is(MIToken::IntegerLiteral));
564 const APSInt &Int = Token.integerValue();
565 if (Int.getMinSignedBits() > 64)
566 return error("integer literal is too large to be an immediate operand");
567 Dest = MachineOperand::CreateImm(Int.getExtValue());
572 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
573 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
576 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
578 return error(Loc + Err.getColumnNo(), Err.getMessage());
582 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
583 assert(Token.is(MIToken::IntegerType));
584 auto Loc = Token.location();
586 if (Token.isNot(MIToken::IntegerLiteral))
587 return error("expected an integer literal");
588 const Constant *C = nullptr;
589 if (parseIRConstant(Loc, C))
591 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
595 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
596 auto Loc = Token.location();
598 if (Token.isNot(MIToken::FloatingPointLiteral))
599 return error("expected a floating point literal");
600 const Constant *C = nullptr;
601 if (parseIRConstant(Loc, C))
603 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
607 bool MIParser::getUnsigned(unsigned &Result) {
608 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
609 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
610 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
612 return error("expected 32-bit integer (too large)");
617 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
618 assert(Token.is(MIToken::MachineBasicBlock));
620 if (getUnsigned(Number))
622 auto MBBInfo = PFS.MBBSlots.find(Number);
623 if (MBBInfo == PFS.MBBSlots.end())
624 return error(Twine("use of undefined machine basic block #") +
626 MBB = MBBInfo->second;
627 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
628 return error(Twine("the name of machine basic block #") + Twine(Number) +
629 " isn't '" + Token.stringValue() + "'");
633 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
634 MachineBasicBlock *MBB;
635 if (parseMBBReference(MBB))
637 Dest = MachineOperand::CreateMBB(MBB);
642 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
643 assert(Token.is(MIToken::StackObject));
647 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
648 if (ObjectInfo == PFS.StackObjectSlots.end())
649 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
652 if (const auto *Alloca =
653 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
654 Name = Alloca->getName();
655 if (!Token.stringValue().empty() && Token.stringValue() != Name)
656 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
657 "' isn't '" + Token.stringValue() + "'");
659 Dest = MachineOperand::CreateFI(ObjectInfo->second);
663 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
664 assert(Token.is(MIToken::FixedStackObject));
668 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
669 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
670 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
673 Dest = MachineOperand::CreateFI(ObjectInfo->second);
677 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
678 switch (Token.kind()) {
679 case MIToken::NamedGlobalValue: {
680 const Module *M = MF.getFunction()->getParent();
681 GV = M->getNamedValue(Token.stringValue());
683 return error(Twine("use of undefined global value '") + Token.range() +
687 case MIToken::GlobalValue: {
689 if (getUnsigned(GVIdx))
691 if (GVIdx >= IRSlots.GlobalValues.size())
692 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
694 GV = IRSlots.GlobalValues[GVIdx];
698 llvm_unreachable("The current token should be a global value");
703 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
704 GlobalValue *GV = nullptr;
705 if (parseGlobalValue(GV))
708 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
709 if (parseOperandsOffset(Dest))
714 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
715 assert(Token.is(MIToken::ConstantPoolItem));
719 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
720 if (ConstantInfo == PFS.ConstantPoolSlots.end())
721 return error("use of undefined constant '%const." + Twine(ID) + "'");
723 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
724 if (parseOperandsOffset(Dest))
729 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
730 assert(Token.is(MIToken::JumpTableIndex));
734 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
735 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
736 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
738 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
742 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
743 assert(Token.is(MIToken::ExternalSymbol));
744 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
746 Dest = MachineOperand::CreateES(Symbol);
747 if (parseOperandsOffset(Dest))
752 bool MIParser::parseMDNode(MDNode *&Node) {
753 assert(Token.is(MIToken::exclaim));
754 auto Loc = Token.location();
756 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
757 return error("expected metadata id after '!'");
761 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
762 if (NodeInfo == IRSlots.MetadataNodes.end())
763 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
765 Node = NodeInfo->second.get();
769 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
770 MDNode *Node = nullptr;
771 if (parseMDNode(Node))
773 Dest = MachineOperand::CreateMetadata(Node);
777 bool MIParser::parseCFIOffset(int &Offset) {
778 if (Token.isNot(MIToken::IntegerLiteral))
779 return error("expected a cfi offset");
780 if (Token.integerValue().getMinSignedBits() > 32)
781 return error("expected a 32 bit integer (the cfi offset is too large)");
782 Offset = (int)Token.integerValue().getExtValue();
787 bool MIParser::parseCFIRegister(unsigned &Reg) {
788 if (Token.isNot(MIToken::NamedRegister))
789 return error("expected a cfi register");
791 if (parseRegister(LLVMReg))
793 const auto *TRI = MF.getSubtarget().getRegisterInfo();
794 assert(TRI && "Expected target register info");
795 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
797 return error("invalid DWARF register");
798 Reg = (unsigned)DwarfReg;
803 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
804 auto Kind = Token.kind();
806 auto &MMI = MF.getMMI();
811 case MIToken::kw_cfi_offset:
812 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
813 parseCFIOffset(Offset))
816 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
818 case MIToken::kw_cfi_def_cfa_register:
819 if (parseCFIRegister(Reg))
822 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
824 case MIToken::kw_cfi_def_cfa_offset:
825 if (parseCFIOffset(Offset))
827 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
828 CFIIndex = MMI.addFrameInst(
829 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
831 case MIToken::kw_cfi_def_cfa:
832 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
833 parseCFIOffset(Offset))
835 // NB: MCCFIInstruction::createDefCfa negates the offset.
837 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
840 // TODO: Parse the other CFI operands.
841 llvm_unreachable("The current token should be a cfi operand");
843 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
847 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
848 switch (Token.kind()) {
849 case MIToken::NamedIRBlock: {
850 BB = dyn_cast_or_null<BasicBlock>(
851 F.getValueSymbolTable().lookup(Token.stringValue()));
853 return error(Twine("use of undefined IR block '") + Token.range() + "'");
856 case MIToken::IRBlock: {
857 unsigned SlotNumber = 0;
858 if (getUnsigned(SlotNumber))
860 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
862 return error(Twine("use of undefined IR block '%ir-block.") +
863 Twine(SlotNumber) + "'");
867 llvm_unreachable("The current token should be an IR block reference");
872 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
873 assert(Token.is(MIToken::kw_blockaddress));
875 if (expectAndConsume(MIToken::lparen))
877 if (Token.isNot(MIToken::GlobalValue) &&
878 Token.isNot(MIToken::NamedGlobalValue))
879 return error("expected a global value");
880 GlobalValue *GV = nullptr;
881 if (parseGlobalValue(GV))
883 auto *F = dyn_cast<Function>(GV);
885 return error("expected an IR function reference");
887 if (expectAndConsume(MIToken::comma))
889 BasicBlock *BB = nullptr;
890 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
891 return error("expected an IR block reference");
892 if (parseIRBlock(BB, *F))
895 if (expectAndConsume(MIToken::rparen))
897 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
898 if (parseOperandsOffset(Dest))
903 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
904 assert(Token.is(MIToken::kw_target_index));
906 if (expectAndConsume(MIToken::lparen))
908 if (Token.isNot(MIToken::Identifier))
909 return error("expected the name of the target index");
911 if (getTargetIndex(Token.stringValue(), Index))
912 return error("use of undefined target index '" + Token.stringValue() + "'");
914 if (expectAndConsume(MIToken::rparen))
916 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
917 if (parseOperandsOffset(Dest))
922 bool MIParser::parseMachineOperand(MachineOperand &Dest) {
923 switch (Token.kind()) {
924 case MIToken::kw_implicit:
925 case MIToken::kw_implicit_define:
926 case MIToken::kw_dead:
927 case MIToken::kw_killed:
928 case MIToken::kw_undef:
929 case MIToken::kw_early_clobber:
930 case MIToken::kw_debug_use:
931 case MIToken::underscore:
932 case MIToken::NamedRegister:
933 case MIToken::VirtualRegister:
934 return parseRegisterOperand(Dest);
935 case MIToken::IntegerLiteral:
936 return parseImmediateOperand(Dest);
937 case MIToken::IntegerType:
938 return parseTypedImmediateOperand(Dest);
939 case MIToken::kw_half:
940 case MIToken::kw_float:
941 case MIToken::kw_double:
942 case MIToken::kw_x86_fp80:
943 case MIToken::kw_fp128:
944 case MIToken::kw_ppc_fp128:
945 return parseFPImmediateOperand(Dest);
946 case MIToken::MachineBasicBlock:
947 return parseMBBOperand(Dest);
948 case MIToken::StackObject:
949 return parseStackObjectOperand(Dest);
950 case MIToken::FixedStackObject:
951 return parseFixedStackObjectOperand(Dest);
952 case MIToken::GlobalValue:
953 case MIToken::NamedGlobalValue:
954 return parseGlobalAddressOperand(Dest);
955 case MIToken::ConstantPoolItem:
956 return parseConstantPoolIndexOperand(Dest);
957 case MIToken::JumpTableIndex:
958 return parseJumpTableIndexOperand(Dest);
959 case MIToken::ExternalSymbol:
960 return parseExternalSymbolOperand(Dest);
961 case MIToken::exclaim:
962 return parseMetadataOperand(Dest);
963 case MIToken::kw_cfi_offset:
964 case MIToken::kw_cfi_def_cfa_register:
965 case MIToken::kw_cfi_def_cfa_offset:
966 case MIToken::kw_cfi_def_cfa:
967 return parseCFIOperand(Dest);
968 case MIToken::kw_blockaddress:
969 return parseBlockAddressOperand(Dest);
970 case MIToken::kw_target_index:
971 return parseTargetIndexOperand(Dest);
974 case MIToken::Identifier:
975 if (const auto *RegMask = getRegMask(Token.stringValue())) {
976 Dest = MachineOperand::CreateRegMask(RegMask);
982 // TODO: parse the other machine operands.
983 return error("expected a machine operand");
988 bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
990 bool HasTargetFlags = false;
991 if (Token.is(MIToken::kw_target_flags)) {
992 HasTargetFlags = true;
994 if (expectAndConsume(MIToken::lparen))
996 if (Token.isNot(MIToken::Identifier))
997 return error("expected the name of the target flag");
998 if (getDirectTargetFlag(Token.stringValue(), TF))
999 return error("use of undefined target flag '" + Token.stringValue() +
1002 // TODO: Parse target's bit target flags.
1003 if (expectAndConsume(MIToken::rparen))
1006 auto Loc = Token.location();
1007 if (parseMachineOperand(Dest))
1009 if (!HasTargetFlags)
1012 return error(Loc, "register operands can't have target flags");
1013 Dest.setTargetFlags(TF);
1017 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1018 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1020 StringRef Sign = Token.range();
1021 bool IsNegative = Token.is(MIToken::minus);
1023 if (Token.isNot(MIToken::IntegerLiteral))
1024 return error("expected an integer literal after '" + Sign + "'");
1025 if (Token.integerValue().getMinSignedBits() > 64)
1026 return error("expected 64-bit integer (too large)");
1027 int64_t Offset = Token.integerValue().getExtValue();
1031 Op.setOffset(Offset);
1035 bool MIParser::parseIRValue(Value *&V) {
1036 switch (Token.kind()) {
1037 case MIToken::NamedIRValue: {
1038 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1040 return error(Twine("use of undefined IR value '") + Token.range() + "'");
1043 // TODO: Parse unnamed IR value references.
1045 llvm_unreachable("The current token should be an IR block reference");
1050 bool MIParser::getUint64(uint64_t &Result) {
1051 assert(Token.hasIntegerValue());
1052 if (Token.integerValue().getActiveBits() > 64)
1053 return error("expected 64-bit integer (too large)");
1054 Result = Token.integerValue().getZExtValue();
1058 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1059 const unsigned OldFlags = Flags;
1060 switch (Token.kind()) {
1061 case MIToken::kw_volatile:
1062 Flags |= MachineMemOperand::MOVolatile;
1064 case MIToken::kw_non_temporal:
1065 Flags |= MachineMemOperand::MONonTemporal;
1067 case MIToken::kw_invariant:
1068 Flags |= MachineMemOperand::MOInvariant;
1070 // TODO: parse the target specific memory operand flags.
1072 llvm_unreachable("The current token should be a memory operand flag");
1074 if (OldFlags == Flags)
1075 // We know that the same flag is specified more than once when the flags
1076 // weren't modified.
1077 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1082 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1083 if (expectAndConsume(MIToken::lparen))
1086 while (Token.isMemoryOperandFlag()) {
1087 if (parseMemoryOperandFlag(Flags))
1090 if (Token.isNot(MIToken::Identifier) ||
1091 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1092 return error("expected 'load' or 'store' memory operation");
1093 if (Token.stringValue() == "load")
1094 Flags |= MachineMemOperand::MOLoad;
1096 Flags |= MachineMemOperand::MOStore;
1099 if (Token.isNot(MIToken::IntegerLiteral))
1100 return error("expected the size integer literal after memory operation");
1102 if (getUint64(Size))
1106 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1107 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1108 return error(Twine("expected '") + Word + "'");
1111 // TODO: Parse pseudo source values.
1112 if (Token.isNot(MIToken::NamedIRValue))
1113 return error("expected an IR value reference");
1115 if (parseIRValue(V))
1117 if (!V->getType()->isPointerTy())
1118 return error("expected a pointer IR value");
1120 // TODO: Parse the base alignment.
1121 // TODO: Parse the attached metadata nodes.
1122 if (expectAndConsume(MIToken::rparen))
1125 Dest = MF.getMachineMemOperand(MachinePointerInfo(V), Flags, Size, Size);
1129 void MIParser::initNames2InstrOpCodes() {
1130 if (!Names2InstrOpCodes.empty())
1132 const auto *TII = MF.getSubtarget().getInstrInfo();
1133 assert(TII && "Expected target instruction info");
1134 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1135 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1138 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1139 initNames2InstrOpCodes();
1140 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1141 if (InstrInfo == Names2InstrOpCodes.end())
1143 OpCode = InstrInfo->getValue();
1147 void MIParser::initNames2Regs() {
1148 if (!Names2Regs.empty())
1150 // The '%noreg' register is the register 0.
1151 Names2Regs.insert(std::make_pair("noreg", 0));
1152 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1153 assert(TRI && "Expected target register info");
1154 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1156 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1159 assert(WasInserted && "Expected registers to be unique case-insensitively");
1163 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1165 auto RegInfo = Names2Regs.find(RegName);
1166 if (RegInfo == Names2Regs.end())
1168 Reg = RegInfo->getValue();
1172 void MIParser::initNames2RegMasks() {
1173 if (!Names2RegMasks.empty())
1175 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1176 assert(TRI && "Expected target register info");
1177 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1178 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1179 assert(RegMasks.size() == RegMaskNames.size());
1180 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1181 Names2RegMasks.insert(
1182 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1185 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1186 initNames2RegMasks();
1187 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1188 if (RegMaskInfo == Names2RegMasks.end())
1190 return RegMaskInfo->getValue();
1193 void MIParser::initNames2SubRegIndices() {
1194 if (!Names2SubRegIndices.empty())
1196 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1197 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1198 Names2SubRegIndices.insert(
1199 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1202 unsigned MIParser::getSubRegIndex(StringRef Name) {
1203 initNames2SubRegIndices();
1204 auto SubRegInfo = Names2SubRegIndices.find(Name);
1205 if (SubRegInfo == Names2SubRegIndices.end())
1207 return SubRegInfo->getValue();
1210 static void initSlots2BasicBlocks(
1212 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1213 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
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 static const BasicBlock *getIRBlockFromSlot(
1227 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1228 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1229 if (BlockInfo == Slots2BasicBlocks.end())
1231 return BlockInfo->second;
1234 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1235 if (Slots2BasicBlocks.empty())
1236 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1237 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1240 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1241 if (&F == MF.getFunction())
1242 return getIRBlock(Slot);
1243 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1244 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1245 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1248 void MIParser::initNames2TargetIndices() {
1249 if (!Names2TargetIndices.empty())
1251 const auto *TII = MF.getSubtarget().getInstrInfo();
1252 assert(TII && "Expected target instruction info");
1253 auto Indices = TII->getSerializableTargetIndices();
1254 for (const auto &I : Indices)
1255 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1258 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1259 initNames2TargetIndices();
1260 auto IndexInfo = Names2TargetIndices.find(Name);
1261 if (IndexInfo == Names2TargetIndices.end())
1263 Index = IndexInfo->second;
1267 void MIParser::initNames2DirectTargetFlags() {
1268 if (!Names2DirectTargetFlags.empty())
1270 const auto *TII = MF.getSubtarget().getInstrInfo();
1271 assert(TII && "Expected target instruction info");
1272 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1273 for (const auto &I : Flags)
1274 Names2DirectTargetFlags.insert(
1275 std::make_pair(StringRef(I.second), I.first));
1278 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1279 initNames2DirectTargetFlags();
1280 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1281 if (FlagInfo == Names2DirectTargetFlags.end())
1283 Flag = FlagInfo->second;
1287 bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1288 MachineFunction &MF, StringRef Src,
1289 const PerFunctionMIParsingState &PFS,
1290 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1291 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
1294 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1295 MachineFunction &MF, StringRef Src,
1296 const PerFunctionMIParsingState &PFS,
1297 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1298 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
1301 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1302 MachineFunction &MF, StringRef Src,
1303 const PerFunctionMIParsingState &PFS,
1304 const SlotMapping &IRSlots,
1305 SMDiagnostic &Error) {
1306 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1307 .parseStandaloneNamedRegister(Reg);
1310 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1311 MachineFunction &MF, StringRef Src,
1312 const PerFunctionMIParsingState &PFS,
1313 const SlotMapping &IRSlots,
1314 SMDiagnostic &Error) {
1315 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1316 .parseStandaloneVirtualRegister(Reg);
1319 bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1320 MachineFunction &MF, StringRef Src,
1321 const PerFunctionMIParsingState &PFS,
1322 const SlotMapping &IRSlots,
1323 SMDiagnostic &Error) {
1324 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1325 .parseStandaloneIRBlockReference(BB);