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