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