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