[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / llvm / lib / CodeGen / MIRParser / MIParser.cpp
blobf77c4613ad801b43d4811e7826c02f22e69cbae5
1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the parsing of machine instructions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MIRParser/MIParser.h"
14 #include "MILexer.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/AsmParser/Parser.h"
25 #include "llvm/AsmParser/SlotMapping.h"
26 #include "llvm/CodeGen/MIRFormatter.h"
27 #include "llvm/CodeGen/MIRPrinter.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/PseudoSourceValueManager.h"
37 #include "llvm/CodeGen/RegisterBank.h"
38 #include "llvm/CodeGen/RegisterBankInfo.h"
39 #include "llvm/CodeGen/TargetInstrInfo.h"
40 #include "llvm/CodeGen/TargetRegisterInfo.h"
41 #include "llvm/CodeGen/TargetSubtargetInfo.h"
42 #include "llvm/CodeGenTypes/LowLevelType.h"
43 #include "llvm/IR/BasicBlock.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/DebugInfoMetadata.h"
47 #include "llvm/IR/DebugLoc.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/InstrTypes.h"
50 #include "llvm/IR/Instructions.h"
51 #include "llvm/IR/Intrinsics.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/ModuleSlotTracker.h"
55 #include "llvm/IR/Type.h"
56 #include "llvm/IR/Value.h"
57 #include "llvm/IR/ValueSymbolTable.h"
58 #include "llvm/MC/LaneBitmask.h"
59 #include "llvm/MC/MCContext.h"
60 #include "llvm/MC/MCDwarf.h"
61 #include "llvm/MC/MCInstrDesc.h"
62 #include "llvm/Support/AtomicOrdering.h"
63 #include "llvm/Support/BranchProbability.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/SMLoc.h"
68 #include "llvm/Support/SourceMgr.h"
69 #include "llvm/Target/TargetIntrinsicInfo.h"
70 #include "llvm/Target/TargetMachine.h"
71 #include <cassert>
72 #include <cctype>
73 #include <cstddef>
74 #include <cstdint>
75 #include <limits>
76 #include <string>
77 #include <utility>
79 using namespace llvm;
81 void PerTargetMIParsingState::setTarget(
82 const TargetSubtargetInfo &NewSubtarget) {
84 // If the subtarget changed, over conservatively assume everything is invalid.
85 if (&Subtarget == &NewSubtarget)
86 return;
88 Names2InstrOpCodes.clear();
89 Names2Regs.clear();
90 Names2RegMasks.clear();
91 Names2SubRegIndices.clear();
92 Names2TargetIndices.clear();
93 Names2DirectTargetFlags.clear();
94 Names2BitmaskTargetFlags.clear();
95 Names2MMOTargetFlags.clear();
97 initNames2RegClasses();
98 initNames2RegBanks();
101 void PerTargetMIParsingState::initNames2Regs() {
102 if (!Names2Regs.empty())
103 return;
105 // The '%noreg' register is the register 0.
106 Names2Regs.insert(std::make_pair("noreg", 0));
107 const auto *TRI = Subtarget.getRegisterInfo();
108 assert(TRI && "Expected target register info");
110 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
111 bool WasInserted =
112 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
113 .second;
114 (void)WasInserted;
115 assert(WasInserted && "Expected registers to be unique case-insensitively");
119 bool PerTargetMIParsingState::getRegisterByName(StringRef RegName,
120 Register &Reg) {
121 initNames2Regs();
122 auto RegInfo = Names2Regs.find(RegName);
123 if (RegInfo == Names2Regs.end())
124 return true;
125 Reg = RegInfo->getValue();
126 return false;
129 bool PerTargetMIParsingState::getVRegFlagValue(StringRef FlagName,
130 uint8_t &FlagValue) const {
131 const auto *TRI = Subtarget.getRegisterInfo();
132 std::optional<uint8_t> FV = TRI->getVRegFlagValue(FlagName);
133 if (!FV)
134 return true;
135 FlagValue = *FV;
136 return false;
139 void PerTargetMIParsingState::initNames2InstrOpCodes() {
140 if (!Names2InstrOpCodes.empty())
141 return;
142 const auto *TII = Subtarget.getInstrInfo();
143 assert(TII && "Expected target instruction info");
144 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
145 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
148 bool PerTargetMIParsingState::parseInstrName(StringRef InstrName,
149 unsigned &OpCode) {
150 initNames2InstrOpCodes();
151 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
152 if (InstrInfo == Names2InstrOpCodes.end())
153 return true;
154 OpCode = InstrInfo->getValue();
155 return false;
158 void PerTargetMIParsingState::initNames2RegMasks() {
159 if (!Names2RegMasks.empty())
160 return;
161 const auto *TRI = Subtarget.getRegisterInfo();
162 assert(TRI && "Expected target register info");
163 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
164 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
165 assert(RegMasks.size() == RegMaskNames.size());
166 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
167 Names2RegMasks.insert(
168 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
171 const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier) {
172 initNames2RegMasks();
173 auto RegMaskInfo = Names2RegMasks.find(Identifier);
174 if (RegMaskInfo == Names2RegMasks.end())
175 return nullptr;
176 return RegMaskInfo->getValue();
179 void PerTargetMIParsingState::initNames2SubRegIndices() {
180 if (!Names2SubRegIndices.empty())
181 return;
182 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
183 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
184 Names2SubRegIndices.insert(
185 std::make_pair(TRI->getSubRegIndexName(I), I));
188 unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name) {
189 initNames2SubRegIndices();
190 auto SubRegInfo = Names2SubRegIndices.find(Name);
191 if (SubRegInfo == Names2SubRegIndices.end())
192 return 0;
193 return SubRegInfo->getValue();
196 void PerTargetMIParsingState::initNames2TargetIndices() {
197 if (!Names2TargetIndices.empty())
198 return;
199 const auto *TII = Subtarget.getInstrInfo();
200 assert(TII && "Expected target instruction info");
201 auto Indices = TII->getSerializableTargetIndices();
202 for (const auto &I : Indices)
203 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
206 bool PerTargetMIParsingState::getTargetIndex(StringRef Name, int &Index) {
207 initNames2TargetIndices();
208 auto IndexInfo = Names2TargetIndices.find(Name);
209 if (IndexInfo == Names2TargetIndices.end())
210 return true;
211 Index = IndexInfo->second;
212 return false;
215 void PerTargetMIParsingState::initNames2DirectTargetFlags() {
216 if (!Names2DirectTargetFlags.empty())
217 return;
219 const auto *TII = Subtarget.getInstrInfo();
220 assert(TII && "Expected target instruction info");
221 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
222 for (const auto &I : Flags)
223 Names2DirectTargetFlags.insert(
224 std::make_pair(StringRef(I.second), I.first));
227 bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name,
228 unsigned &Flag) {
229 initNames2DirectTargetFlags();
230 auto FlagInfo = Names2DirectTargetFlags.find(Name);
231 if (FlagInfo == Names2DirectTargetFlags.end())
232 return true;
233 Flag = FlagInfo->second;
234 return false;
237 void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
238 if (!Names2BitmaskTargetFlags.empty())
239 return;
241 const auto *TII = Subtarget.getInstrInfo();
242 assert(TII && "Expected target instruction info");
243 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
244 for (const auto &I : Flags)
245 Names2BitmaskTargetFlags.insert(
246 std::make_pair(StringRef(I.second), I.first));
249 bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name,
250 unsigned &Flag) {
251 initNames2BitmaskTargetFlags();
252 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
253 if (FlagInfo == Names2BitmaskTargetFlags.end())
254 return true;
255 Flag = FlagInfo->second;
256 return false;
259 void PerTargetMIParsingState::initNames2MMOTargetFlags() {
260 if (!Names2MMOTargetFlags.empty())
261 return;
263 const auto *TII = Subtarget.getInstrInfo();
264 assert(TII && "Expected target instruction info");
265 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
266 for (const auto &I : Flags)
267 Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
270 bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name,
271 MachineMemOperand::Flags &Flag) {
272 initNames2MMOTargetFlags();
273 auto FlagInfo = Names2MMOTargetFlags.find(Name);
274 if (FlagInfo == Names2MMOTargetFlags.end())
275 return true;
276 Flag = FlagInfo->second;
277 return false;
280 void PerTargetMIParsingState::initNames2RegClasses() {
281 if (!Names2RegClasses.empty())
282 return;
284 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
285 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
286 const auto *RC = TRI->getRegClass(I);
287 Names2RegClasses.insert(
288 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
292 void PerTargetMIParsingState::initNames2RegBanks() {
293 if (!Names2RegBanks.empty())
294 return;
296 const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
297 // If the target does not support GlobalISel, we may not have a
298 // register bank info.
299 if (!RBI)
300 return;
302 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
303 const auto &RegBank = RBI->getRegBank(I);
304 Names2RegBanks.insert(
305 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
309 const TargetRegisterClass *
310 PerTargetMIParsingState::getRegClass(StringRef Name) {
311 auto RegClassInfo = Names2RegClasses.find(Name);
312 if (RegClassInfo == Names2RegClasses.end())
313 return nullptr;
314 return RegClassInfo->getValue();
317 const RegisterBank *PerTargetMIParsingState::getRegBank(StringRef Name) {
318 auto RegBankInfo = Names2RegBanks.find(Name);
319 if (RegBankInfo == Names2RegBanks.end())
320 return nullptr;
321 return RegBankInfo->getValue();
324 PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
325 SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T)
326 : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) {
329 VRegInfo &PerFunctionMIParsingState::getVRegInfo(Register Num) {
330 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
331 if (I.second) {
332 MachineRegisterInfo &MRI = MF.getRegInfo();
333 VRegInfo *Info = new (Allocator) VRegInfo;
334 Info->VReg = MRI.createIncompleteVirtualRegister();
335 I.first->second = Info;
337 return *I.first->second;
340 VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
341 assert(RegName != "" && "Expected named reg.");
343 auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
344 if (I.second) {
345 VRegInfo *Info = new (Allocator) VRegInfo;
346 Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
347 I.first->second = Info;
349 return *I.first->second;
352 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
353 DenseMap<unsigned, const Value *> &Slots2Values) {
354 int Slot = MST.getLocalSlot(V);
355 if (Slot == -1)
356 return;
357 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
360 /// Creates the mapping from slot numbers to function's unnamed IR values.
361 static void initSlots2Values(const Function &F,
362 DenseMap<unsigned, const Value *> &Slots2Values) {
363 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
364 MST.incorporateFunction(F);
365 for (const auto &Arg : F.args())
366 mapValueToSlot(&Arg, MST, Slots2Values);
367 for (const auto &BB : F) {
368 mapValueToSlot(&BB, MST, Slots2Values);
369 for (const auto &I : BB)
370 mapValueToSlot(&I, MST, Slots2Values);
374 const Value* PerFunctionMIParsingState::getIRValue(unsigned Slot) {
375 if (Slots2Values.empty())
376 initSlots2Values(MF.getFunction(), Slots2Values);
377 return Slots2Values.lookup(Slot);
380 namespace {
382 /// A wrapper struct around the 'MachineOperand' struct that includes a source
383 /// range and other attributes.
384 struct ParsedMachineOperand {
385 MachineOperand Operand;
386 StringRef::iterator Begin;
387 StringRef::iterator End;
388 std::optional<unsigned> TiedDefIdx;
390 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
391 StringRef::iterator End,
392 std::optional<unsigned> &TiedDefIdx)
393 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
394 if (TiedDefIdx)
395 assert(Operand.isReg() && Operand.isUse() &&
396 "Only used register operands can be tied");
400 class MIParser {
401 MachineFunction &MF;
402 SMDiagnostic &Error;
403 StringRef Source, CurrentSource;
404 SMRange SourceRange;
405 MIToken Token;
406 PerFunctionMIParsingState &PFS;
407 /// Maps from slot numbers to function's unnamed basic blocks.
408 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
410 public:
411 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
412 StringRef Source);
413 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
414 StringRef Source, SMRange SourceRange);
416 /// \p SkipChar gives the number of characters to skip before looking
417 /// for the next token.
418 void lex(unsigned SkipChar = 0);
420 /// Report an error at the current location with the given message.
422 /// This function always return true.
423 bool error(const Twine &Msg);
425 /// Report an error at the given location with the given message.
427 /// This function always return true.
428 bool error(StringRef::iterator Loc, const Twine &Msg);
430 bool
431 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
432 bool parseBasicBlocks();
433 bool parse(MachineInstr *&MI);
434 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
435 bool parseStandaloneNamedRegister(Register &Reg);
436 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
437 bool parseStandaloneRegister(Register &Reg);
438 bool parseStandaloneStackObject(int &FI);
439 bool parseStandaloneMDNode(MDNode *&Node);
440 bool parseMachineMetadata();
441 bool parseMDTuple(MDNode *&MD, bool IsDistinct);
442 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
443 bool parseMetadata(Metadata *&MD);
445 bool
446 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
447 bool parseBasicBlock(MachineBasicBlock &MBB,
448 MachineBasicBlock *&AddFalthroughFrom);
449 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
450 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
452 bool parseNamedRegister(Register &Reg);
453 bool parseVirtualRegister(VRegInfo *&Info);
454 bool parseNamedVirtualRegister(VRegInfo *&Info);
455 bool parseRegister(Register &Reg, VRegInfo *&VRegInfo);
456 bool parseRegisterFlag(unsigned &Flags);
457 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
458 bool parseSubRegisterIndex(unsigned &SubReg);
459 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
460 bool parseRegisterOperand(MachineOperand &Dest,
461 std::optional<unsigned> &TiedDefIdx,
462 bool IsDef = false);
463 bool parseImmediateOperand(MachineOperand &Dest);
464 bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
465 const Constant *&C);
466 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
467 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
468 bool parseTypedImmediateOperand(MachineOperand &Dest);
469 bool parseFPImmediateOperand(MachineOperand &Dest);
470 bool parseMBBReference(MachineBasicBlock *&MBB);
471 bool parseMBBOperand(MachineOperand &Dest);
472 bool parseStackFrameIndex(int &FI);
473 bool parseStackObjectOperand(MachineOperand &Dest);
474 bool parseFixedStackFrameIndex(int &FI);
475 bool parseFixedStackObjectOperand(MachineOperand &Dest);
476 bool parseGlobalValue(GlobalValue *&GV);
477 bool parseGlobalAddressOperand(MachineOperand &Dest);
478 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
479 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
480 bool parseJumpTableIndexOperand(MachineOperand &Dest);
481 bool parseExternalSymbolOperand(MachineOperand &Dest);
482 bool parseMCSymbolOperand(MachineOperand &Dest);
483 [[nodiscard]] bool parseMDNode(MDNode *&Node);
484 bool parseDIExpression(MDNode *&Expr);
485 bool parseDILocation(MDNode *&Expr);
486 bool parseMetadataOperand(MachineOperand &Dest);
487 bool parseCFIOffset(int &Offset);
488 bool parseCFIRegister(Register &Reg);
489 bool parseCFIAddressSpace(unsigned &AddressSpace);
490 bool parseCFIEscapeValues(std::string& Values);
491 bool parseCFIOperand(MachineOperand &Dest);
492 bool parseIRBlock(BasicBlock *&BB, const Function &F);
493 bool parseBlockAddressOperand(MachineOperand &Dest);
494 bool parseIntrinsicOperand(MachineOperand &Dest);
495 bool parsePredicateOperand(MachineOperand &Dest);
496 bool parseShuffleMaskOperand(MachineOperand &Dest);
497 bool parseTargetIndexOperand(MachineOperand &Dest);
498 bool parseDbgInstrRefOperand(MachineOperand &Dest);
499 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
500 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
501 bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
502 MachineOperand &Dest,
503 std::optional<unsigned> &TiedDefIdx);
504 bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
505 const unsigned OpIdx,
506 MachineOperand &Dest,
507 std::optional<unsigned> &TiedDefIdx);
508 bool parseOffset(int64_t &Offset);
509 bool parseIRBlockAddressTaken(BasicBlock *&BB);
510 bool parseAlignment(uint64_t &Alignment);
511 bool parseAddrspace(unsigned &Addrspace);
512 bool parseSectionID(std::optional<MBBSectionID> &SID);
513 bool parseBBID(std::optional<UniqueBBID> &BBID);
514 bool parseCallFrameSize(unsigned &CallFrameSize);
515 bool parseOperandsOffset(MachineOperand &Op);
516 bool parseIRValue(const Value *&V);
517 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
518 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
519 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
520 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
521 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
522 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
523 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
524 bool parseHeapAllocMarker(MDNode *&Node);
525 bool parsePCSections(MDNode *&Node);
527 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
528 MachineOperand &Dest, const MIRFormatter &MF);
530 private:
531 /// Convert the integer literal in the current token into an unsigned integer.
533 /// Return true if an error occurred.
534 bool getUnsigned(unsigned &Result);
536 /// Convert the integer literal in the current token into an uint64.
538 /// Return true if an error occurred.
539 bool getUint64(uint64_t &Result);
541 /// Convert the hexadecimal literal in the current token into an unsigned
542 /// APInt with a minimum bitwidth required to represent the value.
544 /// Return true if the literal does not represent an integer value.
545 bool getHexUint(APInt &Result);
547 /// If the current token is of the given kind, consume it and return false.
548 /// Otherwise report an error and return true.
549 bool expectAndConsume(MIToken::TokenKind TokenKind);
551 /// If the current token is of the given kind, consume it and return true.
552 /// Otherwise return false.
553 bool consumeIfPresent(MIToken::TokenKind TokenKind);
555 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
557 bool assignRegisterTies(MachineInstr &MI,
558 ArrayRef<ParsedMachineOperand> Operands);
560 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
561 const MCInstrDesc &MCID);
563 const BasicBlock *getIRBlock(unsigned Slot);
564 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
566 /// Get or create an MCSymbol for a given name.
567 MCSymbol *getOrCreateMCSymbol(StringRef Name);
569 /// parseStringConstant
570 /// ::= StringConstant
571 bool parseStringConstant(std::string &Result);
573 /// Map the location in the MI string to the corresponding location specified
574 /// in `SourceRange`.
575 SMLoc mapSMLoc(StringRef::iterator Loc);
578 } // end anonymous namespace
580 MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
581 StringRef Source)
582 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
585 MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
586 StringRef Source, SMRange SourceRange)
587 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source),
588 SourceRange(SourceRange), PFS(PFS) {}
590 void MIParser::lex(unsigned SkipChar) {
591 CurrentSource = lexMIToken(
592 CurrentSource.substr(SkipChar), Token,
593 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
596 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
598 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
599 const SourceMgr &SM = *PFS.SM;
600 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
601 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
602 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
603 // Create an ordinary diagnostic when the source manager's buffer is the
604 // source string.
605 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
606 return true;
608 // Create a diagnostic for a YAML string literal.
609 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
610 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
611 Source, {}, {});
612 return true;
615 SMLoc MIParser::mapSMLoc(StringRef::iterator Loc) {
616 assert(SourceRange.isValid() && "Invalid source range");
617 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
618 return SMLoc::getFromPointer(SourceRange.Start.getPointer() +
619 (Loc - Source.data()));
622 typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
623 ErrorCallbackType;
625 static const char *toString(MIToken::TokenKind TokenKind) {
626 switch (TokenKind) {
627 case MIToken::comma:
628 return "','";
629 case MIToken::equal:
630 return "'='";
631 case MIToken::colon:
632 return "':'";
633 case MIToken::lparen:
634 return "'('";
635 case MIToken::rparen:
636 return "')'";
637 default:
638 return "<unknown token>";
642 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
643 if (Token.isNot(TokenKind))
644 return error(Twine("expected ") + toString(TokenKind));
645 lex();
646 return false;
649 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
650 if (Token.isNot(TokenKind))
651 return false;
652 lex();
653 return true;
656 // Parse Machine Basic Block Section ID.
657 bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
658 assert(Token.is(MIToken::kw_bbsections));
659 lex();
660 if (Token.is(MIToken::IntegerLiteral)) {
661 unsigned Value = 0;
662 if (getUnsigned(Value))
663 return error("Unknown Section ID");
664 SID = MBBSectionID{Value};
665 } else {
666 const StringRef &S = Token.stringValue();
667 if (S == "Exception")
668 SID = MBBSectionID::ExceptionSectionID;
669 else if (S == "Cold")
670 SID = MBBSectionID::ColdSectionID;
671 else
672 return error("Unknown Section ID");
674 lex();
675 return false;
678 // Parse Machine Basic Block ID.
679 bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) {
680 assert(Token.is(MIToken::kw_bb_id));
681 lex();
682 unsigned BaseID = 0;
683 unsigned CloneID = 0;
684 if (getUnsigned(BaseID))
685 return error("Unknown BB ID");
686 lex();
687 if (Token.is(MIToken::IntegerLiteral)) {
688 if (getUnsigned(CloneID))
689 return error("Unknown Clone ID");
690 lex();
692 BBID = {BaseID, CloneID};
693 return false;
696 // Parse basic block call frame size.
697 bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) {
698 assert(Token.is(MIToken::kw_call_frame_size));
699 lex();
700 unsigned Value = 0;
701 if (getUnsigned(Value))
702 return error("Unknown call frame size");
703 CallFrameSize = Value;
704 lex();
705 return false;
708 bool MIParser::parseBasicBlockDefinition(
709 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
710 assert(Token.is(MIToken::MachineBasicBlockLabel));
711 unsigned ID = 0;
712 if (getUnsigned(ID))
713 return true;
714 auto Loc = Token.location();
715 auto Name = Token.stringValue();
716 lex();
717 bool MachineBlockAddressTaken = false;
718 BasicBlock *AddressTakenIRBlock = nullptr;
719 bool IsLandingPad = false;
720 bool IsInlineAsmBrIndirectTarget = false;
721 bool IsEHFuncletEntry = false;
722 std::optional<MBBSectionID> SectionID;
723 uint64_t Alignment = 0;
724 std::optional<UniqueBBID> BBID;
725 unsigned CallFrameSize = 0;
726 BasicBlock *BB = nullptr;
727 if (consumeIfPresent(MIToken::lparen)) {
728 do {
729 // TODO: Report an error when multiple same attributes are specified.
730 switch (Token.kind()) {
731 case MIToken::kw_machine_block_address_taken:
732 MachineBlockAddressTaken = true;
733 lex();
734 break;
735 case MIToken::kw_ir_block_address_taken:
736 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
737 return true;
738 break;
739 case MIToken::kw_landing_pad:
740 IsLandingPad = true;
741 lex();
742 break;
743 case MIToken::kw_inlineasm_br_indirect_target:
744 IsInlineAsmBrIndirectTarget = true;
745 lex();
746 break;
747 case MIToken::kw_ehfunclet_entry:
748 IsEHFuncletEntry = true;
749 lex();
750 break;
751 case MIToken::kw_align:
752 if (parseAlignment(Alignment))
753 return true;
754 break;
755 case MIToken::IRBlock:
756 case MIToken::NamedIRBlock:
757 // TODO: Report an error when both name and ir block are specified.
758 if (parseIRBlock(BB, MF.getFunction()))
759 return true;
760 lex();
761 break;
762 case MIToken::kw_bbsections:
763 if (parseSectionID(SectionID))
764 return true;
765 break;
766 case MIToken::kw_bb_id:
767 if (parseBBID(BBID))
768 return true;
769 break;
770 case MIToken::kw_call_frame_size:
771 if (parseCallFrameSize(CallFrameSize))
772 return true;
773 break;
774 default:
775 break;
777 } while (consumeIfPresent(MIToken::comma));
778 if (expectAndConsume(MIToken::rparen))
779 return true;
781 if (expectAndConsume(MIToken::colon))
782 return true;
784 if (!Name.empty()) {
785 BB = dyn_cast_or_null<BasicBlock>(
786 MF.getFunction().getValueSymbolTable()->lookup(Name));
787 if (!BB)
788 return error(Loc, Twine("basic block '") + Name +
789 "' is not defined in the function '" +
790 MF.getName() + "'");
792 auto *MBB = MF.CreateMachineBasicBlock(BB, BBID);
793 MF.insert(MF.end(), MBB);
794 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
795 if (!WasInserted)
796 return error(Loc, Twine("redefinition of machine basic block with id #") +
797 Twine(ID));
798 if (Alignment)
799 MBB->setAlignment(Align(Alignment));
800 if (MachineBlockAddressTaken)
801 MBB->setMachineBlockAddressTaken();
802 if (AddressTakenIRBlock)
803 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
804 MBB->setIsEHPad(IsLandingPad);
805 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
806 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
807 if (SectionID) {
808 MBB->setSectionID(*SectionID);
809 MF.setBBSectionsType(BasicBlockSection::List);
811 MBB->setCallFrameSize(CallFrameSize);
812 return false;
815 bool MIParser::parseBasicBlockDefinitions(
816 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
817 lex();
818 // Skip until the first machine basic block.
819 while (Token.is(MIToken::Newline))
820 lex();
821 if (Token.isErrorOrEOF())
822 return Token.isError();
823 if (Token.isNot(MIToken::MachineBasicBlockLabel))
824 return error("expected a basic block definition before instructions");
825 unsigned BraceDepth = 0;
826 do {
827 if (parseBasicBlockDefinition(MBBSlots))
828 return true;
829 bool IsAfterNewline = false;
830 // Skip until the next machine basic block.
831 while (true) {
832 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
833 Token.isErrorOrEOF())
834 break;
835 else if (Token.is(MIToken::MachineBasicBlockLabel))
836 return error("basic block definition should be located at the start of "
837 "the line");
838 else if (consumeIfPresent(MIToken::Newline)) {
839 IsAfterNewline = true;
840 continue;
842 IsAfterNewline = false;
843 if (Token.is(MIToken::lbrace))
844 ++BraceDepth;
845 if (Token.is(MIToken::rbrace)) {
846 if (!BraceDepth)
847 return error("extraneous closing brace ('}')");
848 --BraceDepth;
850 lex();
852 // Verify that we closed all of the '{' at the end of a file or a block.
853 if (!Token.isError() && BraceDepth)
854 return error("expected '}'"); // FIXME: Report a note that shows '{'.
855 } while (!Token.isErrorOrEOF());
856 return Token.isError();
859 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
860 assert(Token.is(MIToken::kw_liveins));
861 lex();
862 if (expectAndConsume(MIToken::colon))
863 return true;
864 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
865 return false;
866 do {
867 if (Token.isNot(MIToken::NamedRegister))
868 return error("expected a named register");
869 Register Reg;
870 if (parseNamedRegister(Reg))
871 return true;
872 lex();
873 LaneBitmask Mask = LaneBitmask::getAll();
874 if (consumeIfPresent(MIToken::colon)) {
875 // Parse lane mask.
876 if (Token.isNot(MIToken::IntegerLiteral) &&
877 Token.isNot(MIToken::HexLiteral))
878 return error("expected a lane mask");
879 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
880 "Use correct get-function for lane mask");
881 LaneBitmask::Type V;
882 if (getUint64(V))
883 return error("invalid lane mask value");
884 Mask = LaneBitmask(V);
885 lex();
887 MBB.addLiveIn(Reg, Mask);
888 } while (consumeIfPresent(MIToken::comma));
889 return false;
892 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
893 assert(Token.is(MIToken::kw_successors));
894 lex();
895 if (expectAndConsume(MIToken::colon))
896 return true;
897 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
898 return false;
899 do {
900 if (Token.isNot(MIToken::MachineBasicBlock))
901 return error("expected a machine basic block reference");
902 MachineBasicBlock *SuccMBB = nullptr;
903 if (parseMBBReference(SuccMBB))
904 return true;
905 lex();
906 unsigned Weight = 0;
907 if (consumeIfPresent(MIToken::lparen)) {
908 if (Token.isNot(MIToken::IntegerLiteral) &&
909 Token.isNot(MIToken::HexLiteral))
910 return error("expected an integer literal after '('");
911 if (getUnsigned(Weight))
912 return true;
913 lex();
914 if (expectAndConsume(MIToken::rparen))
915 return true;
917 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
918 } while (consumeIfPresent(MIToken::comma));
919 MBB.normalizeSuccProbs();
920 return false;
923 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
924 MachineBasicBlock *&AddFalthroughFrom) {
925 // Skip the definition.
926 assert(Token.is(MIToken::MachineBasicBlockLabel));
927 lex();
928 if (consumeIfPresent(MIToken::lparen)) {
929 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
930 lex();
931 consumeIfPresent(MIToken::rparen);
933 consumeIfPresent(MIToken::colon);
935 // Parse the liveins and successors.
936 // N.B: Multiple lists of successors and liveins are allowed and they're
937 // merged into one.
938 // Example:
939 // liveins: $edi
940 // liveins: $esi
942 // is equivalent to
943 // liveins: $edi, $esi
944 bool ExplicitSuccessors = false;
945 while (true) {
946 if (Token.is(MIToken::kw_successors)) {
947 if (parseBasicBlockSuccessors(MBB))
948 return true;
949 ExplicitSuccessors = true;
950 } else if (Token.is(MIToken::kw_liveins)) {
951 if (parseBasicBlockLiveins(MBB))
952 return true;
953 } else if (consumeIfPresent(MIToken::Newline)) {
954 continue;
955 } else
956 break;
957 if (!Token.isNewlineOrEOF())
958 return error("expected line break at the end of a list");
959 lex();
962 // Parse the instructions.
963 bool IsInBundle = false;
964 MachineInstr *PrevMI = nullptr;
965 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
966 !Token.is(MIToken::Eof)) {
967 if (consumeIfPresent(MIToken::Newline))
968 continue;
969 if (consumeIfPresent(MIToken::rbrace)) {
970 // The first parsing pass should verify that all closing '}' have an
971 // opening '{'.
972 assert(IsInBundle);
973 IsInBundle = false;
974 continue;
976 MachineInstr *MI = nullptr;
977 if (parse(MI))
978 return true;
979 MBB.insert(MBB.end(), MI);
980 if (IsInBundle) {
981 PrevMI->setFlag(MachineInstr::BundledSucc);
982 MI->setFlag(MachineInstr::BundledPred);
984 PrevMI = MI;
985 if (Token.is(MIToken::lbrace)) {
986 if (IsInBundle)
987 return error("nested instruction bundles are not allowed");
988 lex();
989 // This instruction is the start of the bundle.
990 MI->setFlag(MachineInstr::BundledSucc);
991 IsInBundle = true;
992 if (!Token.is(MIToken::Newline))
993 // The next instruction can be on the same line.
994 continue;
996 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
997 lex();
1000 // Construct successor list by searching for basic block machine operands.
1001 if (!ExplicitSuccessors) {
1002 SmallVector<MachineBasicBlock*,4> Successors;
1003 bool IsFallthrough;
1004 guessSuccessors(MBB, Successors, IsFallthrough);
1005 for (MachineBasicBlock *Succ : Successors)
1006 MBB.addSuccessor(Succ);
1008 if (IsFallthrough) {
1009 AddFalthroughFrom = &MBB;
1010 } else {
1011 MBB.normalizeSuccProbs();
1015 return false;
1018 bool MIParser::parseBasicBlocks() {
1019 lex();
1020 // Skip until the first machine basic block.
1021 while (Token.is(MIToken::Newline))
1022 lex();
1023 if (Token.isErrorOrEOF())
1024 return Token.isError();
1025 // The first parsing pass should have verified that this token is a MBB label
1026 // in the 'parseBasicBlockDefinitions' method.
1027 assert(Token.is(MIToken::MachineBasicBlockLabel));
1028 MachineBasicBlock *AddFalthroughFrom = nullptr;
1029 do {
1030 MachineBasicBlock *MBB = nullptr;
1031 if (parseMBBReference(MBB))
1032 return true;
1033 if (AddFalthroughFrom) {
1034 if (!AddFalthroughFrom->isSuccessor(MBB))
1035 AddFalthroughFrom->addSuccessor(MBB);
1036 AddFalthroughFrom->normalizeSuccProbs();
1037 AddFalthroughFrom = nullptr;
1039 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1040 return true;
1041 // The method 'parseBasicBlock' should parse the whole block until the next
1042 // block or the end of file.
1043 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1044 } while (Token.isNot(MIToken::Eof));
1045 return false;
1048 bool MIParser::parse(MachineInstr *&MI) {
1049 // Parse any register operands before '='
1050 MachineOperand MO = MachineOperand::CreateImm(0);
1051 SmallVector<ParsedMachineOperand, 8> Operands;
1052 while (Token.isRegister() || Token.isRegisterFlag()) {
1053 auto Loc = Token.location();
1054 std::optional<unsigned> TiedDefIdx;
1055 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1056 return true;
1057 Operands.push_back(
1058 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1059 if (Token.isNot(MIToken::comma))
1060 break;
1061 lex();
1063 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1064 return true;
1066 unsigned OpCode, Flags = 0;
1067 if (Token.isError() || parseInstruction(OpCode, Flags))
1068 return true;
1070 // Parse the remaining machine operands.
1071 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1072 Token.isNot(MIToken::kw_post_instr_symbol) &&
1073 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1074 Token.isNot(MIToken::kw_pcsections) &&
1075 Token.isNot(MIToken::kw_cfi_type) &&
1076 Token.isNot(MIToken::kw_debug_location) &&
1077 Token.isNot(MIToken::kw_debug_instr_number) &&
1078 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1079 auto Loc = Token.location();
1080 std::optional<unsigned> TiedDefIdx;
1081 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1082 return true;
1083 Operands.push_back(
1084 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1085 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1086 Token.is(MIToken::lbrace))
1087 break;
1088 if (Token.isNot(MIToken::comma))
1089 return error("expected ',' before the next machine operand");
1090 lex();
1093 MCSymbol *PreInstrSymbol = nullptr;
1094 if (Token.is(MIToken::kw_pre_instr_symbol))
1095 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1096 return true;
1097 MCSymbol *PostInstrSymbol = nullptr;
1098 if (Token.is(MIToken::kw_post_instr_symbol))
1099 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1100 return true;
1101 MDNode *HeapAllocMarker = nullptr;
1102 if (Token.is(MIToken::kw_heap_alloc_marker))
1103 if (parseHeapAllocMarker(HeapAllocMarker))
1104 return true;
1105 MDNode *PCSections = nullptr;
1106 if (Token.is(MIToken::kw_pcsections))
1107 if (parsePCSections(PCSections))
1108 return true;
1110 unsigned CFIType = 0;
1111 if (Token.is(MIToken::kw_cfi_type)) {
1112 lex();
1113 if (Token.isNot(MIToken::IntegerLiteral))
1114 return error("expected an integer literal after 'cfi-type'");
1115 // getUnsigned is sufficient for 32-bit integers.
1116 if (getUnsigned(CFIType))
1117 return true;
1118 lex();
1119 // Lex past trailing comma if present.
1120 if (Token.is(MIToken::comma))
1121 lex();
1124 unsigned InstrNum = 0;
1125 if (Token.is(MIToken::kw_debug_instr_number)) {
1126 lex();
1127 if (Token.isNot(MIToken::IntegerLiteral))
1128 return error("expected an integer literal after 'debug-instr-number'");
1129 if (getUnsigned(InstrNum))
1130 return true;
1131 lex();
1132 // Lex past trailing comma if present.
1133 if (Token.is(MIToken::comma))
1134 lex();
1137 DebugLoc DebugLocation;
1138 if (Token.is(MIToken::kw_debug_location)) {
1139 lex();
1140 MDNode *Node = nullptr;
1141 if (Token.is(MIToken::exclaim)) {
1142 if (parseMDNode(Node))
1143 return true;
1144 } else if (Token.is(MIToken::md_dilocation)) {
1145 if (parseDILocation(Node))
1146 return true;
1147 } else
1148 return error("expected a metadata node after 'debug-location'");
1149 if (!isa<DILocation>(Node))
1150 return error("referenced metadata is not a DILocation");
1151 DebugLocation = DebugLoc(Node);
1154 // Parse the machine memory operands.
1155 SmallVector<MachineMemOperand *, 2> MemOperands;
1156 if (Token.is(MIToken::coloncolon)) {
1157 lex();
1158 while (!Token.isNewlineOrEOF()) {
1159 MachineMemOperand *MemOp = nullptr;
1160 if (parseMachineMemoryOperand(MemOp))
1161 return true;
1162 MemOperands.push_back(MemOp);
1163 if (Token.isNewlineOrEOF())
1164 break;
1165 if (Token.isNot(MIToken::comma))
1166 return error("expected ',' before the next machine memory operand");
1167 lex();
1171 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1172 if (!MCID.isVariadic()) {
1173 // FIXME: Move the implicit operand verification to the machine verifier.
1174 if (verifyImplicitOperands(Operands, MCID))
1175 return true;
1178 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1179 MI->setFlags(Flags);
1181 // Don't check the operands make sense, let the verifier catch any
1182 // improprieties.
1183 for (const auto &Operand : Operands)
1184 MI->addOperand(MF, Operand.Operand);
1186 if (assignRegisterTies(*MI, Operands))
1187 return true;
1188 if (PreInstrSymbol)
1189 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1190 if (PostInstrSymbol)
1191 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1192 if (HeapAllocMarker)
1193 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1194 if (PCSections)
1195 MI->setPCSections(MF, PCSections);
1196 if (CFIType)
1197 MI->setCFIType(MF, CFIType);
1198 if (!MemOperands.empty())
1199 MI->setMemRefs(MF, MemOperands);
1200 if (InstrNum)
1201 MI->setDebugInstrNum(InstrNum);
1202 return false;
1205 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1206 lex();
1207 if (Token.isNot(MIToken::MachineBasicBlock))
1208 return error("expected a machine basic block reference");
1209 if (parseMBBReference(MBB))
1210 return true;
1211 lex();
1212 if (Token.isNot(MIToken::Eof))
1213 return error(
1214 "expected end of string after the machine basic block reference");
1215 return false;
1218 bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1219 lex();
1220 if (Token.isNot(MIToken::NamedRegister))
1221 return error("expected a named register");
1222 if (parseNamedRegister(Reg))
1223 return true;
1224 lex();
1225 if (Token.isNot(MIToken::Eof))
1226 return error("expected end of string after the register reference");
1227 return false;
1230 bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1231 lex();
1232 if (Token.isNot(MIToken::VirtualRegister))
1233 return error("expected a virtual register");
1234 if (parseVirtualRegister(Info))
1235 return true;
1236 lex();
1237 if (Token.isNot(MIToken::Eof))
1238 return error("expected end of string after the register reference");
1239 return false;
1242 bool MIParser::parseStandaloneRegister(Register &Reg) {
1243 lex();
1244 if (Token.isNot(MIToken::NamedRegister) &&
1245 Token.isNot(MIToken::VirtualRegister))
1246 return error("expected either a named or virtual register");
1248 VRegInfo *Info;
1249 if (parseRegister(Reg, Info))
1250 return true;
1252 lex();
1253 if (Token.isNot(MIToken::Eof))
1254 return error("expected end of string after the register reference");
1255 return false;
1258 bool MIParser::parseStandaloneStackObject(int &FI) {
1259 lex();
1260 if (Token.isNot(MIToken::StackObject))
1261 return error("expected a stack object");
1262 if (parseStackFrameIndex(FI))
1263 return true;
1264 if (Token.isNot(MIToken::Eof))
1265 return error("expected end of string after the stack object reference");
1266 return false;
1269 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1270 lex();
1271 if (Token.is(MIToken::exclaim)) {
1272 if (parseMDNode(Node))
1273 return true;
1274 } else if (Token.is(MIToken::md_diexpr)) {
1275 if (parseDIExpression(Node))
1276 return true;
1277 } else if (Token.is(MIToken::md_dilocation)) {
1278 if (parseDILocation(Node))
1279 return true;
1280 } else
1281 return error("expected a metadata node");
1282 if (Token.isNot(MIToken::Eof))
1283 return error("expected end of string after the metadata node");
1284 return false;
1287 bool MIParser::parseMachineMetadata() {
1288 lex();
1289 if (Token.isNot(MIToken::exclaim))
1290 return error("expected a metadata node");
1292 lex();
1293 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1294 return error("expected metadata id after '!'");
1295 unsigned ID = 0;
1296 if (getUnsigned(ID))
1297 return true;
1298 lex();
1299 if (expectAndConsume(MIToken::equal))
1300 return true;
1301 bool IsDistinct = Token.is(MIToken::kw_distinct);
1302 if (IsDistinct)
1303 lex();
1304 if (Token.isNot(MIToken::exclaim))
1305 return error("expected a metadata node");
1306 lex();
1308 MDNode *MD;
1309 if (parseMDTuple(MD, IsDistinct))
1310 return true;
1312 auto FI = PFS.MachineForwardRefMDNodes.find(ID);
1313 if (FI != PFS.MachineForwardRefMDNodes.end()) {
1314 FI->second.first->replaceAllUsesWith(MD);
1315 PFS.MachineForwardRefMDNodes.erase(FI);
1317 assert(PFS.MachineMetadataNodes[ID] == MD && "Tracking VH didn't work");
1318 } else {
1319 if (PFS.MachineMetadataNodes.count(ID))
1320 return error("Metadata id is already used");
1321 PFS.MachineMetadataNodes[ID].reset(MD);
1324 return false;
1327 bool MIParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
1328 SmallVector<Metadata *, 16> Elts;
1329 if (parseMDNodeVector(Elts))
1330 return true;
1331 MD = (IsDistinct ? MDTuple::getDistinct
1332 : MDTuple::get)(MF.getFunction().getContext(), Elts);
1333 return false;
1336 bool MIParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
1337 if (Token.isNot(MIToken::lbrace))
1338 return error("expected '{' here");
1339 lex();
1341 if (Token.is(MIToken::rbrace)) {
1342 lex();
1343 return false;
1346 do {
1347 Metadata *MD;
1348 if (parseMetadata(MD))
1349 return true;
1351 Elts.push_back(MD);
1353 if (Token.isNot(MIToken::comma))
1354 break;
1355 lex();
1356 } while (true);
1358 if (Token.isNot(MIToken::rbrace))
1359 return error("expected end of metadata node");
1360 lex();
1362 return false;
1365 // ::= !42
1366 // ::= !"string"
1367 bool MIParser::parseMetadata(Metadata *&MD) {
1368 if (Token.isNot(MIToken::exclaim))
1369 return error("expected '!' here");
1370 lex();
1372 if (Token.is(MIToken::StringConstant)) {
1373 std::string Str;
1374 if (parseStringConstant(Str))
1375 return true;
1376 MD = MDString::get(MF.getFunction().getContext(), Str);
1377 return false;
1380 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1381 return error("expected metadata id after '!'");
1383 SMLoc Loc = mapSMLoc(Token.location());
1385 unsigned ID = 0;
1386 if (getUnsigned(ID))
1387 return true;
1388 lex();
1390 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1391 if (NodeInfo != PFS.IRSlots.MetadataNodes.end()) {
1392 MD = NodeInfo->second.get();
1393 return false;
1395 // Check machine metadata.
1396 NodeInfo = PFS.MachineMetadataNodes.find(ID);
1397 if (NodeInfo != PFS.MachineMetadataNodes.end()) {
1398 MD = NodeInfo->second.get();
1399 return false;
1401 // Forward reference.
1402 auto &FwdRef = PFS.MachineForwardRefMDNodes[ID];
1403 FwdRef = std::make_pair(
1404 MDTuple::getTemporary(MF.getFunction().getContext(), {}), Loc);
1405 PFS.MachineMetadataNodes[ID].reset(FwdRef.first.get());
1406 MD = FwdRef.first.get();
1408 return false;
1411 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1412 assert(MO.isImplicit());
1413 return MO.isDef() ? "implicit-def" : "implicit";
1416 static std::string getRegisterName(const TargetRegisterInfo *TRI,
1417 Register Reg) {
1418 assert(Reg.isPhysical() && "expected phys reg");
1419 return StringRef(TRI->getName(Reg)).lower();
1422 /// Return true if the parsed machine operands contain a given machine operand.
1423 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1424 ArrayRef<ParsedMachineOperand> Operands) {
1425 for (const auto &I : Operands) {
1426 if (ImplicitOperand.isIdenticalTo(I.Operand))
1427 return true;
1429 return false;
1432 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1433 const MCInstrDesc &MCID) {
1434 if (MCID.isCall())
1435 // We can't verify call instructions as they can contain arbitrary implicit
1436 // register and register mask operands.
1437 return false;
1439 // Gather all the expected implicit operands.
1440 SmallVector<MachineOperand, 4> ImplicitOperands;
1441 for (MCPhysReg ImpDef : MCID.implicit_defs())
1442 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1443 for (MCPhysReg ImpUse : MCID.implicit_uses())
1444 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1446 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1447 assert(TRI && "Expected target register info");
1448 for (const auto &I : ImplicitOperands) {
1449 if (isImplicitOperandIn(I, Operands))
1450 continue;
1451 return error(Operands.empty() ? Token.location() : Operands.back().End,
1452 Twine("missing implicit register operand '") +
1453 printImplicitRegisterFlag(I) + " $" +
1454 getRegisterName(TRI, I.getReg()) + "'");
1456 return false;
1459 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1460 // Allow frame and fast math flags for OPCODE
1461 // clang-format off
1462 while (Token.is(MIToken::kw_frame_setup) ||
1463 Token.is(MIToken::kw_frame_destroy) ||
1464 Token.is(MIToken::kw_nnan) ||
1465 Token.is(MIToken::kw_ninf) ||
1466 Token.is(MIToken::kw_nsz) ||
1467 Token.is(MIToken::kw_arcp) ||
1468 Token.is(MIToken::kw_contract) ||
1469 Token.is(MIToken::kw_afn) ||
1470 Token.is(MIToken::kw_reassoc) ||
1471 Token.is(MIToken::kw_nuw) ||
1472 Token.is(MIToken::kw_nsw) ||
1473 Token.is(MIToken::kw_exact) ||
1474 Token.is(MIToken::kw_nofpexcept) ||
1475 Token.is(MIToken::kw_noconvergent) ||
1476 Token.is(MIToken::kw_unpredictable) ||
1477 Token.is(MIToken::kw_nneg) ||
1478 Token.is(MIToken::kw_disjoint) ||
1479 Token.is(MIToken::kw_samesign)) {
1480 // clang-format on
1481 // Mine frame and fast math flags
1482 if (Token.is(MIToken::kw_frame_setup))
1483 Flags |= MachineInstr::FrameSetup;
1484 if (Token.is(MIToken::kw_frame_destroy))
1485 Flags |= MachineInstr::FrameDestroy;
1486 if (Token.is(MIToken::kw_nnan))
1487 Flags |= MachineInstr::FmNoNans;
1488 if (Token.is(MIToken::kw_ninf))
1489 Flags |= MachineInstr::FmNoInfs;
1490 if (Token.is(MIToken::kw_nsz))
1491 Flags |= MachineInstr::FmNsz;
1492 if (Token.is(MIToken::kw_arcp))
1493 Flags |= MachineInstr::FmArcp;
1494 if (Token.is(MIToken::kw_contract))
1495 Flags |= MachineInstr::FmContract;
1496 if (Token.is(MIToken::kw_afn))
1497 Flags |= MachineInstr::FmAfn;
1498 if (Token.is(MIToken::kw_reassoc))
1499 Flags |= MachineInstr::FmReassoc;
1500 if (Token.is(MIToken::kw_nuw))
1501 Flags |= MachineInstr::NoUWrap;
1502 if (Token.is(MIToken::kw_nsw))
1503 Flags |= MachineInstr::NoSWrap;
1504 if (Token.is(MIToken::kw_exact))
1505 Flags |= MachineInstr::IsExact;
1506 if (Token.is(MIToken::kw_nofpexcept))
1507 Flags |= MachineInstr::NoFPExcept;
1508 if (Token.is(MIToken::kw_unpredictable))
1509 Flags |= MachineInstr::Unpredictable;
1510 if (Token.is(MIToken::kw_noconvergent))
1511 Flags |= MachineInstr::NoConvergent;
1512 if (Token.is(MIToken::kw_nneg))
1513 Flags |= MachineInstr::NonNeg;
1514 if (Token.is(MIToken::kw_disjoint))
1515 Flags |= MachineInstr::Disjoint;
1516 if (Token.is(MIToken::kw_samesign))
1517 Flags |= MachineInstr::SameSign;
1519 lex();
1521 if (Token.isNot(MIToken::Identifier))
1522 return error("expected a machine instruction");
1523 StringRef InstrName = Token.stringValue();
1524 if (PFS.Target.parseInstrName(InstrName, OpCode))
1525 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1526 lex();
1527 return false;
1530 bool MIParser::parseNamedRegister(Register &Reg) {
1531 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1532 StringRef Name = Token.stringValue();
1533 if (PFS.Target.getRegisterByName(Name, Reg))
1534 return error(Twine("unknown register name '") + Name + "'");
1535 return false;
1538 bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1539 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1540 StringRef Name = Token.stringValue();
1541 // TODO: Check that the VReg name is not the same as a physical register name.
1542 // If it is, then print a warning (when warnings are implemented).
1543 Info = &PFS.getVRegInfoNamed(Name);
1544 return false;
1547 bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1548 if (Token.is(MIToken::NamedVirtualRegister))
1549 return parseNamedVirtualRegister(Info);
1550 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1551 unsigned ID;
1552 if (getUnsigned(ID))
1553 return true;
1554 Info = &PFS.getVRegInfo(ID);
1555 return false;
1558 bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1559 switch (Token.kind()) {
1560 case MIToken::underscore:
1561 Reg = 0;
1562 return false;
1563 case MIToken::NamedRegister:
1564 return parseNamedRegister(Reg);
1565 case MIToken::NamedVirtualRegister:
1566 case MIToken::VirtualRegister:
1567 if (parseVirtualRegister(Info))
1568 return true;
1569 Reg = Info->VReg;
1570 return false;
1571 // TODO: Parse other register kinds.
1572 default:
1573 llvm_unreachable("The current token should be a register");
1577 bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1578 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1579 return error("expected '_', register class, or register bank name");
1580 StringRef::iterator Loc = Token.location();
1581 StringRef Name = Token.stringValue();
1583 // Was it a register class?
1584 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1585 if (RC) {
1586 lex();
1588 switch (RegInfo.Kind) {
1589 case VRegInfo::UNKNOWN:
1590 case VRegInfo::NORMAL:
1591 RegInfo.Kind = VRegInfo::NORMAL;
1592 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1593 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1594 return error(Loc, Twine("conflicting register classes, previously: ") +
1595 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1597 RegInfo.D.RC = RC;
1598 RegInfo.Explicit = true;
1599 return false;
1601 case VRegInfo::GENERIC:
1602 case VRegInfo::REGBANK:
1603 return error(Loc, "register class specification on generic register");
1605 llvm_unreachable("Unexpected register kind");
1608 // Should be a register bank or a generic register.
1609 const RegisterBank *RegBank = nullptr;
1610 if (Name != "_") {
1611 RegBank = PFS.Target.getRegBank(Name);
1612 if (!RegBank)
1613 return error(Loc, "expected '_', register class, or register bank name");
1616 lex();
1618 switch (RegInfo.Kind) {
1619 case VRegInfo::UNKNOWN:
1620 case VRegInfo::GENERIC:
1621 case VRegInfo::REGBANK:
1622 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1623 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1624 return error(Loc, "conflicting generic register banks");
1625 RegInfo.D.RegBank = RegBank;
1626 RegInfo.Explicit = true;
1627 return false;
1629 case VRegInfo::NORMAL:
1630 return error(Loc, "register bank specification on normal register");
1632 llvm_unreachable("Unexpected register kind");
1635 bool MIParser::parseRegisterFlag(unsigned &Flags) {
1636 const unsigned OldFlags = Flags;
1637 switch (Token.kind()) {
1638 case MIToken::kw_implicit:
1639 Flags |= RegState::Implicit;
1640 break;
1641 case MIToken::kw_implicit_define:
1642 Flags |= RegState::ImplicitDefine;
1643 break;
1644 case MIToken::kw_def:
1645 Flags |= RegState::Define;
1646 break;
1647 case MIToken::kw_dead:
1648 Flags |= RegState::Dead;
1649 break;
1650 case MIToken::kw_killed:
1651 Flags |= RegState::Kill;
1652 break;
1653 case MIToken::kw_undef:
1654 Flags |= RegState::Undef;
1655 break;
1656 case MIToken::kw_internal:
1657 Flags |= RegState::InternalRead;
1658 break;
1659 case MIToken::kw_early_clobber:
1660 Flags |= RegState::EarlyClobber;
1661 break;
1662 case MIToken::kw_debug_use:
1663 Flags |= RegState::Debug;
1664 break;
1665 case MIToken::kw_renamable:
1666 Flags |= RegState::Renamable;
1667 break;
1668 default:
1669 llvm_unreachable("The current token should be a register flag");
1671 if (OldFlags == Flags)
1672 // We know that the same flag is specified more than once when the flags
1673 // weren't modified.
1674 return error("duplicate '" + Token.stringValue() + "' register flag");
1675 lex();
1676 return false;
1679 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1680 assert(Token.is(MIToken::dot));
1681 lex();
1682 if (Token.isNot(MIToken::Identifier))
1683 return error("expected a subregister index after '.'");
1684 auto Name = Token.stringValue();
1685 SubReg = PFS.Target.getSubRegIndex(Name);
1686 if (!SubReg)
1687 return error(Twine("use of unknown subregister index '") + Name + "'");
1688 lex();
1689 return false;
1692 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1693 if (!consumeIfPresent(MIToken::kw_tied_def))
1694 return true;
1695 if (Token.isNot(MIToken::IntegerLiteral))
1696 return error("expected an integer literal after 'tied-def'");
1697 if (getUnsigned(TiedDefIdx))
1698 return true;
1699 lex();
1700 if (expectAndConsume(MIToken::rparen))
1701 return true;
1702 return false;
1705 bool MIParser::assignRegisterTies(MachineInstr &MI,
1706 ArrayRef<ParsedMachineOperand> Operands) {
1707 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1708 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1709 if (!Operands[I].TiedDefIdx)
1710 continue;
1711 // The parser ensures that this operand is a register use, so we just have
1712 // to check the tied-def operand.
1713 unsigned DefIdx = *Operands[I].TiedDefIdx;
1714 if (DefIdx >= E)
1715 return error(Operands[I].Begin,
1716 Twine("use of invalid tied-def operand index '" +
1717 Twine(DefIdx) + "'; instruction has only ") +
1718 Twine(E) + " operands");
1719 const auto &DefOperand = Operands[DefIdx].Operand;
1720 if (!DefOperand.isReg() || !DefOperand.isDef())
1721 // FIXME: add note with the def operand.
1722 return error(Operands[I].Begin,
1723 Twine("use of invalid tied-def operand index '") +
1724 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1725 " isn't a defined register");
1726 // Check that the tied-def operand wasn't tied elsewhere.
1727 for (const auto &TiedPair : TiedRegisterPairs) {
1728 if (TiedPair.first == DefIdx)
1729 return error(Operands[I].Begin,
1730 Twine("the tied-def operand #") + Twine(DefIdx) +
1731 " is already tied with another register operand");
1733 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1735 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1736 // indices must be less than tied max.
1737 for (const auto &TiedPair : TiedRegisterPairs)
1738 MI.tieOperands(TiedPair.first, TiedPair.second);
1739 return false;
1742 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1743 std::optional<unsigned> &TiedDefIdx,
1744 bool IsDef) {
1745 unsigned Flags = IsDef ? RegState::Define : 0;
1746 while (Token.isRegisterFlag()) {
1747 if (parseRegisterFlag(Flags))
1748 return true;
1750 if (!Token.isRegister())
1751 return error("expected a register after register flags");
1752 Register Reg;
1753 VRegInfo *RegInfo;
1754 if (parseRegister(Reg, RegInfo))
1755 return true;
1756 lex();
1757 unsigned SubReg = 0;
1758 if (Token.is(MIToken::dot)) {
1759 if (parseSubRegisterIndex(SubReg))
1760 return true;
1761 if (!Reg.isVirtual())
1762 return error("subregister index expects a virtual register");
1764 if (Token.is(MIToken::colon)) {
1765 if (!Reg.isVirtual())
1766 return error("register class specification expects a virtual register");
1767 lex();
1768 if (parseRegisterClassOrBank(*RegInfo))
1769 return true;
1771 MachineRegisterInfo &MRI = MF.getRegInfo();
1772 if ((Flags & RegState::Define) == 0) {
1773 if (consumeIfPresent(MIToken::lparen)) {
1774 unsigned Idx;
1775 if (!parseRegisterTiedDefIndex(Idx))
1776 TiedDefIdx = Idx;
1777 else {
1778 // Try a redundant low-level type.
1779 LLT Ty;
1780 if (parseLowLevelType(Token.location(), Ty))
1781 return error("expected tied-def or low-level type after '('");
1783 if (expectAndConsume(MIToken::rparen))
1784 return true;
1786 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1787 return error("inconsistent type for generic virtual register");
1789 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1790 MRI.setType(Reg, Ty);
1791 MRI.noteNewVirtualRegister(Reg);
1794 } else if (consumeIfPresent(MIToken::lparen)) {
1795 // Virtual registers may have a tpe with GlobalISel.
1796 if (!Reg.isVirtual())
1797 return error("unexpected type on physical register");
1799 LLT Ty;
1800 if (parseLowLevelType(Token.location(), Ty))
1801 return true;
1803 if (expectAndConsume(MIToken::rparen))
1804 return true;
1806 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1807 return error("inconsistent type for generic virtual register");
1809 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1810 MRI.setType(Reg, Ty);
1811 } else if (Reg.isVirtual()) {
1812 // Generic virtual registers must have a type.
1813 // If we end up here this means the type hasn't been specified and
1814 // this is bad!
1815 if (RegInfo->Kind == VRegInfo::GENERIC ||
1816 RegInfo->Kind == VRegInfo::REGBANK)
1817 return error("generic virtual registers must have a type");
1820 if (Flags & RegState::Define) {
1821 if (Flags & RegState::Kill)
1822 return error("cannot have a killed def operand");
1823 } else {
1824 if (Flags & RegState::Dead)
1825 return error("cannot have a dead use operand");
1828 Dest = MachineOperand::CreateReg(
1829 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1830 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1831 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1832 Flags & RegState::InternalRead, Flags & RegState::Renamable);
1834 return false;
1837 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1838 assert(Token.is(MIToken::IntegerLiteral));
1839 const APSInt &Int = Token.integerValue();
1840 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1841 Dest = MachineOperand::CreateImm(*SImm);
1842 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1843 Dest = MachineOperand::CreateImm(*UImm);
1844 else
1845 return error("integer literal is too large to be an immediate operand");
1846 lex();
1847 return false;
1850 bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1851 const unsigned OpIdx,
1852 MachineOperand &Dest,
1853 const MIRFormatter &MF) {
1854 assert(Token.is(MIToken::dot));
1855 auto Loc = Token.location(); // record start position
1856 size_t Len = 1; // for "."
1857 lex();
1859 // Handle the case that mnemonic starts with number.
1860 if (Token.is(MIToken::IntegerLiteral)) {
1861 Len += Token.range().size();
1862 lex();
1865 StringRef Src;
1866 if (Token.is(MIToken::comma))
1867 Src = StringRef(Loc, Len);
1868 else {
1869 assert(Token.is(MIToken::Identifier));
1870 Src = StringRef(Loc, Len + Token.stringValue().size());
1872 int64_t Val;
1873 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1874 [this](StringRef::iterator Loc, const Twine &Msg)
1875 -> bool { return error(Loc, Msg); }))
1876 return true;
1878 Dest = MachineOperand::CreateImm(Val);
1879 if (!Token.is(MIToken::comma))
1880 lex();
1881 return false;
1884 static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1885 PerFunctionMIParsingState &PFS, const Constant *&C,
1886 ErrorCallbackType ErrCB) {
1887 auto Source = StringValue.str(); // The source has to be null terminated.
1888 SMDiagnostic Err;
1889 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1890 &PFS.IRSlots);
1891 if (!C)
1892 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1893 return false;
1896 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1897 const Constant *&C) {
1898 return ::parseIRConstant(
1899 Loc, StringValue, PFS, C,
1900 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1901 return error(Loc, Msg);
1905 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1906 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1907 return true;
1908 lex();
1909 return false;
1912 // See LLT implementation for bit size limits.
1913 static bool verifyScalarSize(uint64_t Size) {
1914 return Size != 0 && isUInt<16>(Size);
1917 static bool verifyVectorElementCount(uint64_t NumElts) {
1918 return NumElts != 0 && isUInt<16>(NumElts);
1921 static bool verifyAddrSpace(uint64_t AddrSpace) {
1922 return isUInt<24>(AddrSpace);
1925 bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1926 if (Token.range().front() == 's' || Token.range().front() == 'p') {
1927 StringRef SizeStr = Token.range().drop_front();
1928 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1929 return error("expected integers after 's'/'p' type character");
1932 if (Token.range().front() == 's') {
1933 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1934 if (ScalarSize) {
1935 if (!verifyScalarSize(ScalarSize))
1936 return error("invalid size for scalar type");
1937 Ty = LLT::scalar(ScalarSize);
1938 } else {
1939 Ty = LLT::token();
1941 lex();
1942 return false;
1943 } else if (Token.range().front() == 'p') {
1944 const DataLayout &DL = MF.getDataLayout();
1945 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
1946 if (!verifyAddrSpace(AS))
1947 return error("invalid address space number");
1949 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1950 lex();
1951 return false;
1954 // Now we're looking for a vector.
1955 if (Token.isNot(MIToken::less))
1956 return error(Loc, "expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, "
1957 "or <vscale x M x pA> for GlobalISel type");
1958 lex();
1960 bool HasVScale =
1961 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
1962 if (HasVScale) {
1963 lex();
1964 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1965 return error("expected <vscale x M x sN> or <vscale x M x pA>");
1966 lex();
1969 auto GetError = [this, &HasVScale, Loc]() {
1970 if (HasVScale)
1971 return error(
1972 Loc, "expected <vscale x M x sN> or <vscale M x pA> for vector type");
1973 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1976 if (Token.isNot(MIToken::IntegerLiteral))
1977 return GetError();
1978 uint64_t NumElements = Token.integerValue().getZExtValue();
1979 if (!verifyVectorElementCount(NumElements))
1980 return error("invalid number of vector elements");
1982 lex();
1984 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1985 return GetError();
1986 lex();
1988 if (Token.range().front() != 's' && Token.range().front() != 'p')
1989 return GetError();
1991 StringRef SizeStr = Token.range().drop_front();
1992 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1993 return error("expected integers after 's'/'p' type character");
1995 if (Token.range().front() == 's') {
1996 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1997 if (!verifyScalarSize(ScalarSize))
1998 return error("invalid size for scalar element in vector");
1999 Ty = LLT::scalar(ScalarSize);
2000 } else if (Token.range().front() == 'p') {
2001 const DataLayout &DL = MF.getDataLayout();
2002 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
2003 if (!verifyAddrSpace(AS))
2004 return error("invalid address space number");
2006 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2007 } else
2008 return GetError();
2009 lex();
2011 if (Token.isNot(MIToken::greater))
2012 return GetError();
2014 lex();
2016 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2017 return false;
2020 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2021 assert(Token.is(MIToken::Identifier));
2022 StringRef TypeStr = Token.range();
2023 if (TypeStr.front() != 'i' && TypeStr.front() != 's' &&
2024 TypeStr.front() != 'p')
2025 return error(
2026 "a typed immediate operand should start with one of 'i', 's', or 'p'");
2027 StringRef SizeStr = Token.range().drop_front();
2028 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
2029 return error("expected integers after 'i'/'s'/'p' type character");
2031 auto Loc = Token.location();
2032 lex();
2033 if (Token.isNot(MIToken::IntegerLiteral)) {
2034 if (Token.isNot(MIToken::Identifier) ||
2035 !(Token.range() == "true" || Token.range() == "false"))
2036 return error("expected an integer literal");
2038 const Constant *C = nullptr;
2039 if (parseIRConstant(Loc, C))
2040 return true;
2041 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
2042 return false;
2045 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2046 auto Loc = Token.location();
2047 lex();
2048 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2049 Token.isNot(MIToken::HexLiteral))
2050 return error("expected a floating point literal");
2051 const Constant *C = nullptr;
2052 if (parseIRConstant(Loc, C))
2053 return true;
2054 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
2055 return false;
2058 static bool getHexUint(const MIToken &Token, APInt &Result) {
2059 assert(Token.is(MIToken::HexLiteral));
2060 StringRef S = Token.range();
2061 assert(S[0] == '0' && tolower(S[1]) == 'x');
2062 // This could be a floating point literal with a special prefix.
2063 if (!isxdigit(S[2]))
2064 return true;
2065 StringRef V = S.substr(2);
2066 APInt A(V.size()*4, V, 16);
2068 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2069 // sure it isn't the case before constructing result.
2070 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2071 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2072 return false;
2075 static bool getUnsigned(const MIToken &Token, unsigned &Result,
2076 ErrorCallbackType ErrCB) {
2077 if (Token.hasIntegerValue()) {
2078 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2079 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
2080 if (Val64 == Limit)
2081 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2082 Result = Val64;
2083 return false;
2085 if (Token.is(MIToken::HexLiteral)) {
2086 APInt A;
2087 if (getHexUint(Token, A))
2088 return true;
2089 if (A.getBitWidth() > 32)
2090 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2091 Result = A.getZExtValue();
2092 return false;
2094 return true;
2097 bool MIParser::getUnsigned(unsigned &Result) {
2098 return ::getUnsigned(
2099 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2100 return error(Loc, Msg);
2104 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2105 assert(Token.is(MIToken::MachineBasicBlock) ||
2106 Token.is(MIToken::MachineBasicBlockLabel));
2107 unsigned Number;
2108 if (getUnsigned(Number))
2109 return true;
2110 auto MBBInfo = PFS.MBBSlots.find(Number);
2111 if (MBBInfo == PFS.MBBSlots.end())
2112 return error(Twine("use of undefined machine basic block #") +
2113 Twine(Number));
2114 MBB = MBBInfo->second;
2115 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2116 // we drop the <irname> from the bb.<id>.<irname> format.
2117 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2118 return error(Twine("the name of machine basic block #") + Twine(Number) +
2119 " isn't '" + Token.stringValue() + "'");
2120 return false;
2123 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2124 MachineBasicBlock *MBB;
2125 if (parseMBBReference(MBB))
2126 return true;
2127 Dest = MachineOperand::CreateMBB(MBB);
2128 lex();
2129 return false;
2132 bool MIParser::parseStackFrameIndex(int &FI) {
2133 assert(Token.is(MIToken::StackObject));
2134 unsigned ID;
2135 if (getUnsigned(ID))
2136 return true;
2137 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2138 if (ObjectInfo == PFS.StackObjectSlots.end())
2139 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2140 "'");
2141 StringRef Name;
2142 if (const auto *Alloca =
2143 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2144 Name = Alloca->getName();
2145 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2146 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2147 "' isn't '" + Token.stringValue() + "'");
2148 lex();
2149 FI = ObjectInfo->second;
2150 return false;
2153 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2154 int FI;
2155 if (parseStackFrameIndex(FI))
2156 return true;
2157 Dest = MachineOperand::CreateFI(FI);
2158 return false;
2161 bool MIParser::parseFixedStackFrameIndex(int &FI) {
2162 assert(Token.is(MIToken::FixedStackObject));
2163 unsigned ID;
2164 if (getUnsigned(ID))
2165 return true;
2166 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2167 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2168 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2169 Twine(ID) + "'");
2170 lex();
2171 FI = ObjectInfo->second;
2172 return false;
2175 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2176 int FI;
2177 if (parseFixedStackFrameIndex(FI))
2178 return true;
2179 Dest = MachineOperand::CreateFI(FI);
2180 return false;
2183 static bool parseGlobalValue(const MIToken &Token,
2184 PerFunctionMIParsingState &PFS, GlobalValue *&GV,
2185 ErrorCallbackType ErrCB) {
2186 switch (Token.kind()) {
2187 case MIToken::NamedGlobalValue: {
2188 const Module *M = PFS.MF.getFunction().getParent();
2189 GV = M->getNamedValue(Token.stringValue());
2190 if (!GV)
2191 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2192 Token.range() + "'");
2193 break;
2195 case MIToken::GlobalValue: {
2196 unsigned GVIdx;
2197 if (getUnsigned(Token, GVIdx, ErrCB))
2198 return true;
2199 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2200 if (!GV)
2201 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2202 Twine(GVIdx) + "'");
2203 break;
2205 default:
2206 llvm_unreachable("The current token should be a global value");
2208 return false;
2211 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2212 return ::parseGlobalValue(
2213 Token, PFS, GV,
2214 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2215 return error(Loc, Msg);
2219 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2220 GlobalValue *GV = nullptr;
2221 if (parseGlobalValue(GV))
2222 return true;
2223 lex();
2224 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2225 if (parseOperandsOffset(Dest))
2226 return true;
2227 return false;
2230 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2231 assert(Token.is(MIToken::ConstantPoolItem));
2232 unsigned ID;
2233 if (getUnsigned(ID))
2234 return true;
2235 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2236 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2237 return error("use of undefined constant '%const." + Twine(ID) + "'");
2238 lex();
2239 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2240 if (parseOperandsOffset(Dest))
2241 return true;
2242 return false;
2245 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2246 assert(Token.is(MIToken::JumpTableIndex));
2247 unsigned ID;
2248 if (getUnsigned(ID))
2249 return true;
2250 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2251 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2252 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2253 lex();
2254 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2255 return false;
2258 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2259 assert(Token.is(MIToken::ExternalSymbol));
2260 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2261 lex();
2262 Dest = MachineOperand::CreateES(Symbol);
2263 if (parseOperandsOffset(Dest))
2264 return true;
2265 return false;
2268 bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2269 assert(Token.is(MIToken::MCSymbol));
2270 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2271 lex();
2272 Dest = MachineOperand::CreateMCSymbol(Symbol);
2273 if (parseOperandsOffset(Dest))
2274 return true;
2275 return false;
2278 bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2279 assert(Token.is(MIToken::SubRegisterIndex));
2280 StringRef Name = Token.stringValue();
2281 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2282 if (SubRegIndex == 0)
2283 return error(Twine("unknown subregister index '") + Name + "'");
2284 lex();
2285 Dest = MachineOperand::CreateImm(SubRegIndex);
2286 return false;
2289 bool MIParser::parseMDNode(MDNode *&Node) {
2290 assert(Token.is(MIToken::exclaim));
2292 auto Loc = Token.location();
2293 lex();
2294 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2295 return error("expected metadata id after '!'");
2296 unsigned ID;
2297 if (getUnsigned(ID))
2298 return true;
2299 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2300 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2301 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2302 if (NodeInfo == PFS.MachineMetadataNodes.end())
2303 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2305 lex();
2306 Node = NodeInfo->second.get();
2307 return false;
2310 bool MIParser::parseDIExpression(MDNode *&Expr) {
2311 unsigned Read;
2312 Expr = llvm::parseDIExpressionBodyAtBeginning(
2313 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2314 &PFS.IRSlots);
2315 CurrentSource = CurrentSource.substr(Read);
2316 lex();
2317 if (!Expr)
2318 return error(Error.getMessage());
2319 return false;
2322 bool MIParser::parseDILocation(MDNode *&Loc) {
2323 assert(Token.is(MIToken::md_dilocation));
2324 lex();
2326 bool HaveLine = false;
2327 unsigned Line = 0;
2328 unsigned Column = 0;
2329 MDNode *Scope = nullptr;
2330 MDNode *InlinedAt = nullptr;
2331 bool ImplicitCode = false;
2333 if (expectAndConsume(MIToken::lparen))
2334 return true;
2336 if (Token.isNot(MIToken::rparen)) {
2337 do {
2338 if (Token.is(MIToken::Identifier)) {
2339 if (Token.stringValue() == "line") {
2340 lex();
2341 if (expectAndConsume(MIToken::colon))
2342 return true;
2343 if (Token.isNot(MIToken::IntegerLiteral) ||
2344 Token.integerValue().isSigned())
2345 return error("expected unsigned integer");
2346 Line = Token.integerValue().getZExtValue();
2347 HaveLine = true;
2348 lex();
2349 continue;
2351 if (Token.stringValue() == "column") {
2352 lex();
2353 if (expectAndConsume(MIToken::colon))
2354 return true;
2355 if (Token.isNot(MIToken::IntegerLiteral) ||
2356 Token.integerValue().isSigned())
2357 return error("expected unsigned integer");
2358 Column = Token.integerValue().getZExtValue();
2359 lex();
2360 continue;
2362 if (Token.stringValue() == "scope") {
2363 lex();
2364 if (expectAndConsume(MIToken::colon))
2365 return true;
2366 if (parseMDNode(Scope))
2367 return error("expected metadata node");
2368 if (!isa<DIScope>(Scope))
2369 return error("expected DIScope node");
2370 continue;
2372 if (Token.stringValue() == "inlinedAt") {
2373 lex();
2374 if (expectAndConsume(MIToken::colon))
2375 return true;
2376 if (Token.is(MIToken::exclaim)) {
2377 if (parseMDNode(InlinedAt))
2378 return true;
2379 } else if (Token.is(MIToken::md_dilocation)) {
2380 if (parseDILocation(InlinedAt))
2381 return true;
2382 } else
2383 return error("expected metadata node");
2384 if (!isa<DILocation>(InlinedAt))
2385 return error("expected DILocation node");
2386 continue;
2388 if (Token.stringValue() == "isImplicitCode") {
2389 lex();
2390 if (expectAndConsume(MIToken::colon))
2391 return true;
2392 if (!Token.is(MIToken::Identifier))
2393 return error("expected true/false");
2394 // As far as I can see, we don't have any existing need for parsing
2395 // true/false in MIR yet. Do it ad-hoc until there's something else
2396 // that needs it.
2397 if (Token.stringValue() == "true")
2398 ImplicitCode = true;
2399 else if (Token.stringValue() == "false")
2400 ImplicitCode = false;
2401 else
2402 return error("expected true/false");
2403 lex();
2404 continue;
2407 return error(Twine("invalid DILocation argument '") +
2408 Token.stringValue() + "'");
2409 } while (consumeIfPresent(MIToken::comma));
2412 if (expectAndConsume(MIToken::rparen))
2413 return true;
2415 if (!HaveLine)
2416 return error("DILocation requires line number");
2417 if (!Scope)
2418 return error("DILocation requires a scope");
2420 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2421 InlinedAt, ImplicitCode);
2422 return false;
2425 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2426 MDNode *Node = nullptr;
2427 if (Token.is(MIToken::exclaim)) {
2428 if (parseMDNode(Node))
2429 return true;
2430 } else if (Token.is(MIToken::md_diexpr)) {
2431 if (parseDIExpression(Node))
2432 return true;
2434 Dest = MachineOperand::CreateMetadata(Node);
2435 return false;
2438 bool MIParser::parseCFIOffset(int &Offset) {
2439 if (Token.isNot(MIToken::IntegerLiteral))
2440 return error("expected a cfi offset");
2441 if (Token.integerValue().getSignificantBits() > 32)
2442 return error("expected a 32 bit integer (the cfi offset is too large)");
2443 Offset = (int)Token.integerValue().getExtValue();
2444 lex();
2445 return false;
2448 bool MIParser::parseCFIRegister(Register &Reg) {
2449 if (Token.isNot(MIToken::NamedRegister))
2450 return error("expected a cfi register");
2451 Register LLVMReg;
2452 if (parseNamedRegister(LLVMReg))
2453 return true;
2454 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2455 assert(TRI && "Expected target register info");
2456 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2457 if (DwarfReg < 0)
2458 return error("invalid DWARF register");
2459 Reg = (unsigned)DwarfReg;
2460 lex();
2461 return false;
2464 bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2465 if (Token.isNot(MIToken::IntegerLiteral))
2466 return error("expected a cfi address space literal");
2467 if (Token.integerValue().isSigned())
2468 return error("expected an unsigned integer (cfi address space)");
2469 AddressSpace = Token.integerValue().getZExtValue();
2470 lex();
2471 return false;
2474 bool MIParser::parseCFIEscapeValues(std::string &Values) {
2475 do {
2476 if (Token.isNot(MIToken::HexLiteral))
2477 return error("expected a hexadecimal literal");
2478 unsigned Value;
2479 if (getUnsigned(Value))
2480 return true;
2481 if (Value > UINT8_MAX)
2482 return error("expected a 8-bit integer (too large)");
2483 Values.push_back(static_cast<uint8_t>(Value));
2484 lex();
2485 } while (consumeIfPresent(MIToken::comma));
2486 return false;
2489 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2490 auto Kind = Token.kind();
2491 lex();
2492 int Offset;
2493 Register Reg;
2494 unsigned AddressSpace;
2495 unsigned CFIIndex;
2496 switch (Kind) {
2497 case MIToken::kw_cfi_same_value:
2498 if (parseCFIRegister(Reg))
2499 return true;
2500 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2501 break;
2502 case MIToken::kw_cfi_offset:
2503 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2504 parseCFIOffset(Offset))
2505 return true;
2506 CFIIndex =
2507 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2508 break;
2509 case MIToken::kw_cfi_rel_offset:
2510 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2511 parseCFIOffset(Offset))
2512 return true;
2513 CFIIndex = MF.addFrameInst(
2514 MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
2515 break;
2516 case MIToken::kw_cfi_def_cfa_register:
2517 if (parseCFIRegister(Reg))
2518 return true;
2519 CFIIndex =
2520 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2521 break;
2522 case MIToken::kw_cfi_def_cfa_offset:
2523 if (parseCFIOffset(Offset))
2524 return true;
2525 CFIIndex =
2526 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2527 break;
2528 case MIToken::kw_cfi_adjust_cfa_offset:
2529 if (parseCFIOffset(Offset))
2530 return true;
2531 CFIIndex = MF.addFrameInst(
2532 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
2533 break;
2534 case MIToken::kw_cfi_def_cfa:
2535 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2536 parseCFIOffset(Offset))
2537 return true;
2538 CFIIndex =
2539 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2540 break;
2541 case MIToken::kw_cfi_llvm_def_aspace_cfa:
2542 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2543 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2544 parseCFIAddressSpace(AddressSpace))
2545 return true;
2546 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2547 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2548 break;
2549 case MIToken::kw_cfi_remember_state:
2550 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2551 break;
2552 case MIToken::kw_cfi_restore:
2553 if (parseCFIRegister(Reg))
2554 return true;
2555 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2556 break;
2557 case MIToken::kw_cfi_restore_state:
2558 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2559 break;
2560 case MIToken::kw_cfi_undefined:
2561 if (parseCFIRegister(Reg))
2562 return true;
2563 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2564 break;
2565 case MIToken::kw_cfi_register: {
2566 Register Reg2;
2567 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2568 parseCFIRegister(Reg2))
2569 return true;
2571 CFIIndex =
2572 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2573 break;
2575 case MIToken::kw_cfi_window_save:
2576 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2577 break;
2578 case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2579 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2580 break;
2581 case MIToken::kw_cfi_aarch64_negate_ra_sign_state_with_pc:
2582 CFIIndex =
2583 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2584 break;
2585 case MIToken::kw_cfi_escape: {
2586 std::string Values;
2587 if (parseCFIEscapeValues(Values))
2588 return true;
2589 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2590 break;
2592 default:
2593 // TODO: Parse the other CFI operands.
2594 llvm_unreachable("The current token should be a cfi operand");
2596 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2597 return false;
2600 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2601 switch (Token.kind()) {
2602 case MIToken::NamedIRBlock: {
2603 BB = dyn_cast_or_null<BasicBlock>(
2604 F.getValueSymbolTable()->lookup(Token.stringValue()));
2605 if (!BB)
2606 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2607 break;
2609 case MIToken::IRBlock: {
2610 unsigned SlotNumber = 0;
2611 if (getUnsigned(SlotNumber))
2612 return true;
2613 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2614 if (!BB)
2615 return error(Twine("use of undefined IR block '%ir-block.") +
2616 Twine(SlotNumber) + "'");
2617 break;
2619 default:
2620 llvm_unreachable("The current token should be an IR block reference");
2622 return false;
2625 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2626 assert(Token.is(MIToken::kw_blockaddress));
2627 lex();
2628 if (expectAndConsume(MIToken::lparen))
2629 return true;
2630 if (Token.isNot(MIToken::GlobalValue) &&
2631 Token.isNot(MIToken::NamedGlobalValue))
2632 return error("expected a global value");
2633 GlobalValue *GV = nullptr;
2634 if (parseGlobalValue(GV))
2635 return true;
2636 auto *F = dyn_cast<Function>(GV);
2637 if (!F)
2638 return error("expected an IR function reference");
2639 lex();
2640 if (expectAndConsume(MIToken::comma))
2641 return true;
2642 BasicBlock *BB = nullptr;
2643 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2644 return error("expected an IR block reference");
2645 if (parseIRBlock(BB, *F))
2646 return true;
2647 lex();
2648 if (expectAndConsume(MIToken::rparen))
2649 return true;
2650 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2651 if (parseOperandsOffset(Dest))
2652 return true;
2653 return false;
2656 bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2657 assert(Token.is(MIToken::kw_intrinsic));
2658 lex();
2659 if (expectAndConsume(MIToken::lparen))
2660 return error("expected syntax intrinsic(@llvm.whatever)");
2662 if (Token.isNot(MIToken::NamedGlobalValue))
2663 return error("expected syntax intrinsic(@llvm.whatever)");
2665 std::string Name = std::string(Token.stringValue());
2666 lex();
2668 if (expectAndConsume(MIToken::rparen))
2669 return error("expected ')' to terminate intrinsic name");
2671 // Find out what intrinsic we're dealing with, first try the global namespace
2672 // and then the target's private intrinsics if that fails.
2673 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
2674 Intrinsic::ID ID = Intrinsic::lookupIntrinsicID(Name);
2675 if (ID == Intrinsic::not_intrinsic && TII)
2676 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
2678 if (ID == Intrinsic::not_intrinsic)
2679 return error("unknown intrinsic name");
2680 Dest = MachineOperand::CreateIntrinsicID(ID);
2682 return false;
2685 bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2686 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2687 bool IsFloat = Token.is(MIToken::kw_floatpred);
2688 lex();
2690 if (expectAndConsume(MIToken::lparen))
2691 return error("expected syntax intpred(whatever) or floatpred(whatever");
2693 if (Token.isNot(MIToken::Identifier))
2694 return error("whatever");
2696 CmpInst::Predicate Pred;
2697 if (IsFloat) {
2698 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2699 .Case("false", CmpInst::FCMP_FALSE)
2700 .Case("oeq", CmpInst::FCMP_OEQ)
2701 .Case("ogt", CmpInst::FCMP_OGT)
2702 .Case("oge", CmpInst::FCMP_OGE)
2703 .Case("olt", CmpInst::FCMP_OLT)
2704 .Case("ole", CmpInst::FCMP_OLE)
2705 .Case("one", CmpInst::FCMP_ONE)
2706 .Case("ord", CmpInst::FCMP_ORD)
2707 .Case("uno", CmpInst::FCMP_UNO)
2708 .Case("ueq", CmpInst::FCMP_UEQ)
2709 .Case("ugt", CmpInst::FCMP_UGT)
2710 .Case("uge", CmpInst::FCMP_UGE)
2711 .Case("ult", CmpInst::FCMP_ULT)
2712 .Case("ule", CmpInst::FCMP_ULE)
2713 .Case("une", CmpInst::FCMP_UNE)
2714 .Case("true", CmpInst::FCMP_TRUE)
2715 .Default(CmpInst::BAD_FCMP_PREDICATE);
2716 if (!CmpInst::isFPPredicate(Pred))
2717 return error("invalid floating-point predicate");
2718 } else {
2719 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2720 .Case("eq", CmpInst::ICMP_EQ)
2721 .Case("ne", CmpInst::ICMP_NE)
2722 .Case("sgt", CmpInst::ICMP_SGT)
2723 .Case("sge", CmpInst::ICMP_SGE)
2724 .Case("slt", CmpInst::ICMP_SLT)
2725 .Case("sle", CmpInst::ICMP_SLE)
2726 .Case("ugt", CmpInst::ICMP_UGT)
2727 .Case("uge", CmpInst::ICMP_UGE)
2728 .Case("ult", CmpInst::ICMP_ULT)
2729 .Case("ule", CmpInst::ICMP_ULE)
2730 .Default(CmpInst::BAD_ICMP_PREDICATE);
2731 if (!CmpInst::isIntPredicate(Pred))
2732 return error("invalid integer predicate");
2735 lex();
2736 Dest = MachineOperand::CreatePredicate(Pred);
2737 if (expectAndConsume(MIToken::rparen))
2738 return error("predicate should be terminated by ')'.");
2740 return false;
2743 bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
2744 assert(Token.is(MIToken::kw_shufflemask));
2746 lex();
2747 if (expectAndConsume(MIToken::lparen))
2748 return error("expected syntax shufflemask(<integer or undef>, ...)");
2750 SmallVector<int, 32> ShufMask;
2751 do {
2752 if (Token.is(MIToken::kw_undef)) {
2753 ShufMask.push_back(-1);
2754 } else if (Token.is(MIToken::IntegerLiteral)) {
2755 const APSInt &Int = Token.integerValue();
2756 ShufMask.push_back(Int.getExtValue());
2757 } else
2758 return error("expected integer constant");
2760 lex();
2761 } while (consumeIfPresent(MIToken::comma));
2763 if (expectAndConsume(MIToken::rparen))
2764 return error("shufflemask should be terminated by ')'.");
2766 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
2767 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
2768 return false;
2771 bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
2772 assert(Token.is(MIToken::kw_dbg_instr_ref));
2774 lex();
2775 if (expectAndConsume(MIToken::lparen))
2776 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2778 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2779 return error("expected unsigned integer for instruction index");
2780 uint64_t InstrIdx = Token.integerValue().getZExtValue();
2781 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
2782 "Instruction reference's instruction index is too large");
2783 lex();
2785 if (expectAndConsume(MIToken::comma))
2786 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2788 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2789 return error("expected unsigned integer for operand index");
2790 uint64_t OpIdx = Token.integerValue().getZExtValue();
2791 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
2792 "Instruction reference's operand index is too large");
2793 lex();
2795 if (expectAndConsume(MIToken::rparen))
2796 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2798 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
2799 return false;
2802 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2803 assert(Token.is(MIToken::kw_target_index));
2804 lex();
2805 if (expectAndConsume(MIToken::lparen))
2806 return true;
2807 if (Token.isNot(MIToken::Identifier))
2808 return error("expected the name of the target index");
2809 int Index = 0;
2810 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2811 return error("use of undefined target index '" + Token.stringValue() + "'");
2812 lex();
2813 if (expectAndConsume(MIToken::rparen))
2814 return true;
2815 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2816 if (parseOperandsOffset(Dest))
2817 return true;
2818 return false;
2821 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2822 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2823 lex();
2824 if (expectAndConsume(MIToken::lparen))
2825 return true;
2827 uint32_t *Mask = MF.allocateRegMask();
2828 do {
2829 if (Token.isNot(MIToken::rparen)) {
2830 if (Token.isNot(MIToken::NamedRegister))
2831 return error("expected a named register");
2832 Register Reg;
2833 if (parseNamedRegister(Reg))
2834 return true;
2835 lex();
2836 Mask[Reg / 32] |= 1U << (Reg % 32);
2839 // TODO: Report an error if the same register is used more than once.
2840 } while (consumeIfPresent(MIToken::comma));
2842 if (expectAndConsume(MIToken::rparen))
2843 return true;
2844 Dest = MachineOperand::CreateRegMask(Mask);
2845 return false;
2848 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2849 assert(Token.is(MIToken::kw_liveout));
2850 uint32_t *Mask = MF.allocateRegMask();
2851 lex();
2852 if (expectAndConsume(MIToken::lparen))
2853 return true;
2854 while (true) {
2855 if (Token.isNot(MIToken::NamedRegister))
2856 return error("expected a named register");
2857 Register Reg;
2858 if (parseNamedRegister(Reg))
2859 return true;
2860 lex();
2861 Mask[Reg / 32] |= 1U << (Reg % 32);
2862 // TODO: Report an error if the same register is used more than once.
2863 if (Token.isNot(MIToken::comma))
2864 break;
2865 lex();
2867 if (expectAndConsume(MIToken::rparen))
2868 return true;
2869 Dest = MachineOperand::CreateRegLiveOut(Mask);
2870 return false;
2873 bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
2874 MachineOperand &Dest,
2875 std::optional<unsigned> &TiedDefIdx) {
2876 switch (Token.kind()) {
2877 case MIToken::kw_implicit:
2878 case MIToken::kw_implicit_define:
2879 case MIToken::kw_def:
2880 case MIToken::kw_dead:
2881 case MIToken::kw_killed:
2882 case MIToken::kw_undef:
2883 case MIToken::kw_internal:
2884 case MIToken::kw_early_clobber:
2885 case MIToken::kw_debug_use:
2886 case MIToken::kw_renamable:
2887 case MIToken::underscore:
2888 case MIToken::NamedRegister:
2889 case MIToken::VirtualRegister:
2890 case MIToken::NamedVirtualRegister:
2891 return parseRegisterOperand(Dest, TiedDefIdx);
2892 case MIToken::IntegerLiteral:
2893 return parseImmediateOperand(Dest);
2894 case MIToken::kw_half:
2895 case MIToken::kw_bfloat:
2896 case MIToken::kw_float:
2897 case MIToken::kw_double:
2898 case MIToken::kw_x86_fp80:
2899 case MIToken::kw_fp128:
2900 case MIToken::kw_ppc_fp128:
2901 return parseFPImmediateOperand(Dest);
2902 case MIToken::MachineBasicBlock:
2903 return parseMBBOperand(Dest);
2904 case MIToken::StackObject:
2905 return parseStackObjectOperand(Dest);
2906 case MIToken::FixedStackObject:
2907 return parseFixedStackObjectOperand(Dest);
2908 case MIToken::GlobalValue:
2909 case MIToken::NamedGlobalValue:
2910 return parseGlobalAddressOperand(Dest);
2911 case MIToken::ConstantPoolItem:
2912 return parseConstantPoolIndexOperand(Dest);
2913 case MIToken::JumpTableIndex:
2914 return parseJumpTableIndexOperand(Dest);
2915 case MIToken::ExternalSymbol:
2916 return parseExternalSymbolOperand(Dest);
2917 case MIToken::MCSymbol:
2918 return parseMCSymbolOperand(Dest);
2919 case MIToken::SubRegisterIndex:
2920 return parseSubRegisterIndexOperand(Dest);
2921 case MIToken::md_diexpr:
2922 case MIToken::exclaim:
2923 return parseMetadataOperand(Dest);
2924 case MIToken::kw_cfi_same_value:
2925 case MIToken::kw_cfi_offset:
2926 case MIToken::kw_cfi_rel_offset:
2927 case MIToken::kw_cfi_def_cfa_register:
2928 case MIToken::kw_cfi_def_cfa_offset:
2929 case MIToken::kw_cfi_adjust_cfa_offset:
2930 case MIToken::kw_cfi_escape:
2931 case MIToken::kw_cfi_def_cfa:
2932 case MIToken::kw_cfi_llvm_def_aspace_cfa:
2933 case MIToken::kw_cfi_register:
2934 case MIToken::kw_cfi_remember_state:
2935 case MIToken::kw_cfi_restore:
2936 case MIToken::kw_cfi_restore_state:
2937 case MIToken::kw_cfi_undefined:
2938 case MIToken::kw_cfi_window_save:
2939 case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2940 case MIToken::kw_cfi_aarch64_negate_ra_sign_state_with_pc:
2941 return parseCFIOperand(Dest);
2942 case MIToken::kw_blockaddress:
2943 return parseBlockAddressOperand(Dest);
2944 case MIToken::kw_intrinsic:
2945 return parseIntrinsicOperand(Dest);
2946 case MIToken::kw_target_index:
2947 return parseTargetIndexOperand(Dest);
2948 case MIToken::kw_liveout:
2949 return parseLiveoutRegisterMaskOperand(Dest);
2950 case MIToken::kw_floatpred:
2951 case MIToken::kw_intpred:
2952 return parsePredicateOperand(Dest);
2953 case MIToken::kw_shufflemask:
2954 return parseShuffleMaskOperand(Dest);
2955 case MIToken::kw_dbg_instr_ref:
2956 return parseDbgInstrRefOperand(Dest);
2957 case MIToken::Error:
2958 return true;
2959 case MIToken::Identifier:
2960 if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
2961 Dest = MachineOperand::CreateRegMask(RegMask);
2962 lex();
2963 break;
2964 } else if (Token.stringValue() == "CustomRegMask") {
2965 return parseCustomRegisterMaskOperand(Dest);
2966 } else
2967 return parseTypedImmediateOperand(Dest);
2968 case MIToken::dot: {
2969 const auto *TII = MF.getSubtarget().getInstrInfo();
2970 if (const auto *Formatter = TII->getMIRFormatter()) {
2971 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
2973 [[fallthrough]];
2975 default:
2976 // FIXME: Parse the MCSymbol machine operand.
2977 return error("expected a machine operand");
2979 return false;
2982 bool MIParser::parseMachineOperandAndTargetFlags(
2983 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
2984 std::optional<unsigned> &TiedDefIdx) {
2985 unsigned TF = 0;
2986 bool HasTargetFlags = false;
2987 if (Token.is(MIToken::kw_target_flags)) {
2988 HasTargetFlags = true;
2989 lex();
2990 if (expectAndConsume(MIToken::lparen))
2991 return true;
2992 if (Token.isNot(MIToken::Identifier))
2993 return error("expected the name of the target flag");
2994 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
2995 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
2996 return error("use of undefined target flag '" + Token.stringValue() +
2997 "'");
2999 lex();
3000 while (Token.is(MIToken::comma)) {
3001 lex();
3002 if (Token.isNot(MIToken::Identifier))
3003 return error("expected the name of the target flag");
3004 unsigned BitFlag = 0;
3005 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3006 return error("use of undefined target flag '" + Token.stringValue() +
3007 "'");
3008 // TODO: Report an error when using a duplicate bit target flag.
3009 TF |= BitFlag;
3010 lex();
3012 if (expectAndConsume(MIToken::rparen))
3013 return true;
3015 auto Loc = Token.location();
3016 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3017 return true;
3018 if (!HasTargetFlags)
3019 return false;
3020 if (Dest.isReg())
3021 return error(Loc, "register operands can't have target flags");
3022 Dest.setTargetFlags(TF);
3023 return false;
3026 bool MIParser::parseOffset(int64_t &Offset) {
3027 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3028 return false;
3029 StringRef Sign = Token.range();
3030 bool IsNegative = Token.is(MIToken::minus);
3031 lex();
3032 if (Token.isNot(MIToken::IntegerLiteral))
3033 return error("expected an integer literal after '" + Sign + "'");
3034 if (Token.integerValue().getSignificantBits() > 64)
3035 return error("expected 64-bit integer (too large)");
3036 Offset = Token.integerValue().getExtValue();
3037 if (IsNegative)
3038 Offset = -Offset;
3039 lex();
3040 return false;
3043 bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3044 assert(Token.is(MIToken::kw_ir_block_address_taken));
3045 lex();
3046 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3047 return error("expected basic block after 'ir_block_address_taken'");
3049 if (parseIRBlock(BB, MF.getFunction()))
3050 return true;
3052 lex();
3053 return false;
3056 bool MIParser::parseAlignment(uint64_t &Alignment) {
3057 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3058 lex();
3059 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3060 return error("expected an integer literal after 'align'");
3061 if (getUint64(Alignment))
3062 return true;
3063 lex();
3065 if (!isPowerOf2_64(Alignment))
3066 return error("expected a power-of-2 literal after 'align'");
3068 return false;
3071 bool MIParser::parseAddrspace(unsigned &Addrspace) {
3072 assert(Token.is(MIToken::kw_addrspace));
3073 lex();
3074 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3075 return error("expected an integer literal after 'addrspace'");
3076 if (getUnsigned(Addrspace))
3077 return true;
3078 lex();
3079 return false;
3082 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3083 int64_t Offset = 0;
3084 if (parseOffset(Offset))
3085 return true;
3086 Op.setOffset(Offset);
3087 return false;
3090 static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3091 const Value *&V, ErrorCallbackType ErrCB) {
3092 switch (Token.kind()) {
3093 case MIToken::NamedIRValue: {
3094 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3095 break;
3097 case MIToken::IRValue: {
3098 unsigned SlotNumber = 0;
3099 if (getUnsigned(Token, SlotNumber, ErrCB))
3100 return true;
3101 V = PFS.getIRValue(SlotNumber);
3102 break;
3104 case MIToken::NamedGlobalValue:
3105 case MIToken::GlobalValue: {
3106 GlobalValue *GV = nullptr;
3107 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3108 return true;
3109 V = GV;
3110 break;
3112 case MIToken::QuotedIRValue: {
3113 const Constant *C = nullptr;
3114 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3115 return true;
3116 V = C;
3117 break;
3119 case MIToken::kw_unknown_address:
3120 V = nullptr;
3121 return false;
3122 default:
3123 llvm_unreachable("The current token should be an IR block reference");
3125 if (!V)
3126 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3127 return false;
3130 bool MIParser::parseIRValue(const Value *&V) {
3131 return ::parseIRValue(
3132 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3133 return error(Loc, Msg);
3137 bool MIParser::getUint64(uint64_t &Result) {
3138 if (Token.hasIntegerValue()) {
3139 if (Token.integerValue().getActiveBits() > 64)
3140 return error("expected 64-bit integer (too large)");
3141 Result = Token.integerValue().getZExtValue();
3142 return false;
3144 if (Token.is(MIToken::HexLiteral)) {
3145 APInt A;
3146 if (getHexUint(A))
3147 return true;
3148 if (A.getBitWidth() > 64)
3149 return error("expected 64-bit integer (too large)");
3150 Result = A.getZExtValue();
3151 return false;
3153 return true;
3156 bool MIParser::getHexUint(APInt &Result) {
3157 return ::getHexUint(Token, Result);
3160 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3161 const auto OldFlags = Flags;
3162 switch (Token.kind()) {
3163 case MIToken::kw_volatile:
3164 Flags |= MachineMemOperand::MOVolatile;
3165 break;
3166 case MIToken::kw_non_temporal:
3167 Flags |= MachineMemOperand::MONonTemporal;
3168 break;
3169 case MIToken::kw_dereferenceable:
3170 Flags |= MachineMemOperand::MODereferenceable;
3171 break;
3172 case MIToken::kw_invariant:
3173 Flags |= MachineMemOperand::MOInvariant;
3174 break;
3175 case MIToken::StringConstant: {
3176 MachineMemOperand::Flags TF;
3177 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3178 return error("use of undefined target MMO flag '" + Token.stringValue() +
3179 "'");
3180 Flags |= TF;
3181 break;
3183 default:
3184 llvm_unreachable("The current token should be a memory operand flag");
3186 if (OldFlags == Flags)
3187 // We know that the same flag is specified more than once when the flags
3188 // weren't modified.
3189 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3190 lex();
3191 return false;
3194 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3195 switch (Token.kind()) {
3196 case MIToken::kw_stack:
3197 PSV = MF.getPSVManager().getStack();
3198 break;
3199 case MIToken::kw_got:
3200 PSV = MF.getPSVManager().getGOT();
3201 break;
3202 case MIToken::kw_jump_table:
3203 PSV = MF.getPSVManager().getJumpTable();
3204 break;
3205 case MIToken::kw_constant_pool:
3206 PSV = MF.getPSVManager().getConstantPool();
3207 break;
3208 case MIToken::FixedStackObject: {
3209 int FI;
3210 if (parseFixedStackFrameIndex(FI))
3211 return true;
3212 PSV = MF.getPSVManager().getFixedStack(FI);
3213 // The token was already consumed, so use return here instead of break.
3214 return false;
3216 case MIToken::StackObject: {
3217 int FI;
3218 if (parseStackFrameIndex(FI))
3219 return true;
3220 PSV = MF.getPSVManager().getFixedStack(FI);
3221 // The token was already consumed, so use return here instead of break.
3222 return false;
3224 case MIToken::kw_call_entry:
3225 lex();
3226 switch (Token.kind()) {
3227 case MIToken::GlobalValue:
3228 case MIToken::NamedGlobalValue: {
3229 GlobalValue *GV = nullptr;
3230 if (parseGlobalValue(GV))
3231 return true;
3232 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3233 break;
3235 case MIToken::ExternalSymbol:
3236 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3237 MF.createExternalSymbolName(Token.stringValue()));
3238 break;
3239 default:
3240 return error(
3241 "expected a global value or an external symbol after 'call-entry'");
3243 break;
3244 case MIToken::kw_custom: {
3245 lex();
3246 const auto *TII = MF.getSubtarget().getInstrInfo();
3247 if (const auto *Formatter = TII->getMIRFormatter()) {
3248 if (Formatter->parseCustomPseudoSourceValue(
3249 Token.stringValue(), MF, PFS, PSV,
3250 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3251 return error(Loc, Msg);
3253 return true;
3254 } else
3255 return error("unable to parse target custom pseudo source value");
3256 break;
3258 default:
3259 llvm_unreachable("The current token should be pseudo source value");
3261 lex();
3262 return false;
3265 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3266 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3267 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3268 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3269 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3270 const PseudoSourceValue *PSV = nullptr;
3271 if (parseMemoryPseudoSourceValue(PSV))
3272 return true;
3273 int64_t Offset = 0;
3274 if (parseOffset(Offset))
3275 return true;
3276 Dest = MachinePointerInfo(PSV, Offset);
3277 return false;
3279 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3280 Token.isNot(MIToken::GlobalValue) &&
3281 Token.isNot(MIToken::NamedGlobalValue) &&
3282 Token.isNot(MIToken::QuotedIRValue) &&
3283 Token.isNot(MIToken::kw_unknown_address))
3284 return error("expected an IR value reference");
3285 const Value *V = nullptr;
3286 if (parseIRValue(V))
3287 return true;
3288 if (V && !V->getType()->isPointerTy())
3289 return error("expected a pointer IR value");
3290 lex();
3291 int64_t Offset = 0;
3292 if (parseOffset(Offset))
3293 return true;
3294 Dest = MachinePointerInfo(V, Offset);
3295 return false;
3298 bool MIParser::parseOptionalScope(LLVMContext &Context,
3299 SyncScope::ID &SSID) {
3300 SSID = SyncScope::System;
3301 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3302 lex();
3303 if (expectAndConsume(MIToken::lparen))
3304 return error("expected '(' in syncscope");
3306 std::string SSN;
3307 if (parseStringConstant(SSN))
3308 return true;
3310 SSID = Context.getOrInsertSyncScopeID(SSN);
3311 if (expectAndConsume(MIToken::rparen))
3312 return error("expected ')' in syncscope");
3315 return false;
3318 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3319 Order = AtomicOrdering::NotAtomic;
3320 if (Token.isNot(MIToken::Identifier))
3321 return false;
3323 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3324 .Case("unordered", AtomicOrdering::Unordered)
3325 .Case("monotonic", AtomicOrdering::Monotonic)
3326 .Case("acquire", AtomicOrdering::Acquire)
3327 .Case("release", AtomicOrdering::Release)
3328 .Case("acq_rel", AtomicOrdering::AcquireRelease)
3329 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
3330 .Default(AtomicOrdering::NotAtomic);
3332 if (Order != AtomicOrdering::NotAtomic) {
3333 lex();
3334 return false;
3337 return error("expected an atomic scope, ordering or a size specification");
3340 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3341 if (expectAndConsume(MIToken::lparen))
3342 return true;
3343 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
3344 while (Token.isMemoryOperandFlag()) {
3345 if (parseMemoryOperandFlag(Flags))
3346 return true;
3348 if (Token.isNot(MIToken::Identifier) ||
3349 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3350 return error("expected 'load' or 'store' memory operation");
3351 if (Token.stringValue() == "load")
3352 Flags |= MachineMemOperand::MOLoad;
3353 else
3354 Flags |= MachineMemOperand::MOStore;
3355 lex();
3357 // Optional 'store' for operands that both load and store.
3358 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3359 Flags |= MachineMemOperand::MOStore;
3360 lex();
3363 // Optional synchronization scope.
3364 SyncScope::ID SSID;
3365 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3366 return true;
3368 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3369 AtomicOrdering Order, FailureOrder;
3370 if (parseOptionalAtomicOrdering(Order))
3371 return true;
3373 if (parseOptionalAtomicOrdering(FailureOrder))
3374 return true;
3376 if (Token.isNot(MIToken::IntegerLiteral) &&
3377 Token.isNot(MIToken::kw_unknown_size) &&
3378 Token.isNot(MIToken::lparen))
3379 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3380 "memory operation");
3382 LLT MemoryType;
3383 if (Token.is(MIToken::IntegerLiteral)) {
3384 uint64_t Size;
3385 if (getUint64(Size))
3386 return true;
3388 // Convert from bytes to bits for storage.
3389 MemoryType = LLT::scalar(8 * Size);
3390 lex();
3391 } else if (Token.is(MIToken::kw_unknown_size)) {
3392 lex();
3393 } else {
3394 if (expectAndConsume(MIToken::lparen))
3395 return true;
3396 if (parseLowLevelType(Token.location(), MemoryType))
3397 return true;
3398 if (expectAndConsume(MIToken::rparen))
3399 return true;
3402 MachinePointerInfo Ptr = MachinePointerInfo();
3403 if (Token.is(MIToken::Identifier)) {
3404 const char *Word =
3405 ((Flags & MachineMemOperand::MOLoad) &&
3406 (Flags & MachineMemOperand::MOStore))
3407 ? "on"
3408 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3409 if (Token.stringValue() != Word)
3410 return error(Twine("expected '") + Word + "'");
3411 lex();
3413 if (parseMachinePointerInfo(Ptr))
3414 return true;
3416 uint64_t BaseAlignment =
3417 MemoryType.isValid()
3418 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3419 : 1;
3420 AAMDNodes AAInfo;
3421 MDNode *Range = nullptr;
3422 while (consumeIfPresent(MIToken::comma)) {
3423 switch (Token.kind()) {
3424 case MIToken::kw_align: {
3425 // align is printed if it is different than size.
3426 uint64_t Alignment;
3427 if (parseAlignment(Alignment))
3428 return true;
3429 if (Ptr.Offset & (Alignment - 1)) {
3430 // MachineMemOperand::getAlign never returns a value greater than the
3431 // alignment of offset, so this just guards against hand-written MIR
3432 // that specifies a large "align" value when it should probably use
3433 // "basealign" instead.
3434 return error("specified alignment is more aligned than offset");
3436 BaseAlignment = Alignment;
3437 break;
3439 case MIToken::kw_basealign:
3440 // basealign is printed if it is different than align.
3441 if (parseAlignment(BaseAlignment))
3442 return true;
3443 break;
3444 case MIToken::kw_addrspace:
3445 if (parseAddrspace(Ptr.AddrSpace))
3446 return true;
3447 break;
3448 case MIToken::md_tbaa:
3449 lex();
3450 if (parseMDNode(AAInfo.TBAA))
3451 return true;
3452 break;
3453 case MIToken::md_alias_scope:
3454 lex();
3455 if (parseMDNode(AAInfo.Scope))
3456 return true;
3457 break;
3458 case MIToken::md_noalias:
3459 lex();
3460 if (parseMDNode(AAInfo.NoAlias))
3461 return true;
3462 break;
3463 case MIToken::md_range:
3464 lex();
3465 if (parseMDNode(Range))
3466 return true;
3467 break;
3468 // TODO: Report an error on duplicate metadata nodes.
3469 default:
3470 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3471 "'!noalias' or '!range'");
3474 if (expectAndConsume(MIToken::rparen))
3475 return true;
3476 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3477 AAInfo, Range, SSID, Order, FailureOrder);
3478 return false;
3481 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3482 assert((Token.is(MIToken::kw_pre_instr_symbol) ||
3483 Token.is(MIToken::kw_post_instr_symbol)) &&
3484 "Invalid token for a pre- post-instruction symbol!");
3485 lex();
3486 if (Token.isNot(MIToken::MCSymbol))
3487 return error("expected a symbol after 'pre-instr-symbol'");
3488 Symbol = getOrCreateMCSymbol(Token.stringValue());
3489 lex();
3490 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3491 Token.is(MIToken::lbrace))
3492 return false;
3493 if (Token.isNot(MIToken::comma))
3494 return error("expected ',' before the next machine operand");
3495 lex();
3496 return false;
3499 bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3500 assert(Token.is(MIToken::kw_heap_alloc_marker) &&
3501 "Invalid token for a heap alloc marker!");
3502 lex();
3503 if (parseMDNode(Node))
3504 return true;
3505 if (!Node)
3506 return error("expected a MDNode after 'heap-alloc-marker'");
3507 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3508 Token.is(MIToken::lbrace))
3509 return false;
3510 if (Token.isNot(MIToken::comma))
3511 return error("expected ',' before the next machine operand");
3512 lex();
3513 return false;
3516 bool MIParser::parsePCSections(MDNode *&Node) {
3517 assert(Token.is(MIToken::kw_pcsections) &&
3518 "Invalid token for a PC sections!");
3519 lex();
3520 if (parseMDNode(Node))
3521 return true;
3522 if (!Node)
3523 return error("expected a MDNode after 'pcsections'");
3524 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3525 Token.is(MIToken::lbrace))
3526 return false;
3527 if (Token.isNot(MIToken::comma))
3528 return error("expected ',' before the next machine operand");
3529 lex();
3530 return false;
3533 static void initSlots2BasicBlocks(
3534 const Function &F,
3535 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3536 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
3537 MST.incorporateFunction(F);
3538 for (const auto &BB : F) {
3539 if (BB.hasName())
3540 continue;
3541 int Slot = MST.getLocalSlot(&BB);
3542 if (Slot == -1)
3543 continue;
3544 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3548 static const BasicBlock *getIRBlockFromSlot(
3549 unsigned Slot,
3550 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3551 return Slots2BasicBlocks.lookup(Slot);
3554 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3555 if (Slots2BasicBlocks.empty())
3556 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3557 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3560 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3561 if (&F == &MF.getFunction())
3562 return getIRBlock(Slot);
3563 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3564 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3565 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3568 MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3569 // FIXME: Currently we can't recognize temporary or local symbols and call all
3570 // of the appropriate forms to create them. However, this handles basic cases
3571 // well as most of the special aspects are recognized by a prefix on their
3572 // name, and the input names should already be unique. For test cases, keeping
3573 // the symbol name out of the symbol table isn't terribly important.
3574 return MF.getContext().getOrCreateSymbol(Name);
3577 bool MIParser::parseStringConstant(std::string &Result) {
3578 if (Token.isNot(MIToken::StringConstant))
3579 return error("expected string constant");
3580 Result = std::string(Token.stringValue());
3581 lex();
3582 return false;
3585 bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
3586 StringRef Src,
3587 SMDiagnostic &Error) {
3588 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3591 bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
3592 StringRef Src, SMDiagnostic &Error) {
3593 return MIParser(PFS, Error, Src).parseBasicBlocks();
3596 bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
3597 MachineBasicBlock *&MBB, StringRef Src,
3598 SMDiagnostic &Error) {
3599 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3602 bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
3603 Register &Reg, StringRef Src,
3604 SMDiagnostic &Error) {
3605 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3608 bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
3609 Register &Reg, StringRef Src,
3610 SMDiagnostic &Error) {
3611 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3614 bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
3615 VRegInfo *&Info, StringRef Src,
3616 SMDiagnostic &Error) {
3617 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3620 bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
3621 int &FI, StringRef Src,
3622 SMDiagnostic &Error) {
3623 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3626 bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
3627 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
3628 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3631 bool llvm::parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src,
3632 SMRange SrcRange, SMDiagnostic &Error) {
3633 return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata();
3636 bool MIRFormatter::parseIRValue(StringRef Src, MachineFunction &MF,
3637 PerFunctionMIParsingState &PFS, const Value *&V,
3638 ErrorCallbackType ErrorCallback) {
3639 MIToken Token;
3640 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3641 ErrorCallback(Loc, Msg);
3643 V = nullptr;
3645 return ::parseIRValue(Token, PFS, V, ErrorCallback);