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