1 //===- MIParser.cpp - Machine instructions parser implementation ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the parsing of machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MIParser.h" 15 #include "MILexer.h" 16 #include "llvm/ADT/StringMap.h" 17 #include "llvm/AsmParser/Parser.h" 18 #include "llvm/AsmParser/SlotMapping.h" 19 #include "llvm/CodeGen/MachineBasicBlock.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstr.h" 23 #include "llvm/CodeGen/MachineInstrBuilder.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineModuleInfo.h" 26 #include "llvm/CodeGen/MachineRegisterInfo.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/Instructions.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/IR/ModuleSlotTracker.h" 32 #include "llvm/IR/ValueSymbolTable.h" 33 #include "llvm/Support/SourceMgr.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Target/TargetInstrInfo.h" 36 #include "llvm/Target/TargetIntrinsicInfo.h" 37 #include "llvm/Target/TargetSubtargetInfo.h" 38 39 using namespace llvm; 40 41 PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF, 42 SourceMgr &SM, const SlotMapping &IRSlots) 43 : MF(MF), SM(&SM), IRSlots(IRSlots) { 44 } 45 46 namespace { 47 48 /// A wrapper struct around the 'MachineOperand' struct that includes a source 49 /// range and other attributes. 50 struct ParsedMachineOperand { 51 MachineOperand Operand; 52 StringRef::iterator Begin; 53 StringRef::iterator End; 54 Optional<unsigned> TiedDefIdx; 55 56 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin, 57 StringRef::iterator End, Optional<unsigned> &TiedDefIdx) 58 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) { 59 if (TiedDefIdx) 60 assert(Operand.isReg() && Operand.isUse() && 61 "Only used register operands can be tied"); 62 } 63 }; 64 65 class MIParser { 66 MachineFunction &MF; 67 SMDiagnostic &Error; 68 StringRef Source, CurrentSource; 69 MIToken Token; 70 const PerFunctionMIParsingState &PFS; 71 /// Maps from instruction names to op codes. 72 StringMap<unsigned> Names2InstrOpCodes; 73 /// Maps from register names to registers. 74 StringMap<unsigned> Names2Regs; 75 /// Maps from register mask names to register masks. 76 StringMap<const uint32_t *> Names2RegMasks; 77 /// Maps from subregister names to subregister indices. 78 StringMap<unsigned> Names2SubRegIndices; 79 /// Maps from slot numbers to function's unnamed basic blocks. 80 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks; 81 /// Maps from slot numbers to function's unnamed values. 82 DenseMap<unsigned, const Value *> Slots2Values; 83 /// Maps from target index names to target indices. 84 StringMap<int> Names2TargetIndices; 85 /// Maps from direct target flag names to the direct target flag values. 86 StringMap<unsigned> Names2DirectTargetFlags; 87 /// Maps from direct target flag names to the bitmask target flag values. 88 StringMap<unsigned> Names2BitmaskTargetFlags; 89 90 public: 91 MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error, 92 StringRef Source); 93 94 /// \p SkipChar gives the number of characters to skip before looking 95 /// for the next token. 96 void lex(unsigned SkipChar = 0); 97 98 /// Report an error at the current location with the given message. 99 /// 100 /// This function always return true. 101 bool error(const Twine &Msg); 102 103 /// Report an error at the given location with the given message. 104 /// 105 /// This function always return true. 106 bool error(StringRef::iterator Loc, const Twine &Msg); 107 108 bool 109 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); 110 bool parseBasicBlocks(); 111 bool parse(MachineInstr *&MI); 112 bool parseStandaloneMBB(MachineBasicBlock *&MBB); 113 bool parseStandaloneNamedRegister(unsigned &Reg); 114 bool parseStandaloneVirtualRegister(unsigned &Reg); 115 bool parseStandaloneStackObject(int &FI); 116 bool parseStandaloneMDNode(MDNode *&Node); 117 118 bool 119 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); 120 bool parseBasicBlock(MachineBasicBlock &MBB); 121 bool parseBasicBlockLiveins(MachineBasicBlock &MBB); 122 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB); 123 124 bool parseRegister(unsigned &Reg); 125 bool parseRegisterFlag(unsigned &Flags); 126 bool parseSubRegisterIndex(unsigned &SubReg); 127 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx); 128 bool parseSize(unsigned &Size); 129 bool parseRegisterOperand(MachineOperand &Dest, 130 Optional<unsigned> &TiedDefIdx, bool IsDef = false); 131 bool parseImmediateOperand(MachineOperand &Dest); 132 bool parseIRConstant(StringRef::iterator Loc, StringRef Source, 133 const Constant *&C); 134 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C); 135 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty); 136 bool parseTypedImmediateOperand(MachineOperand &Dest); 137 bool parseFPImmediateOperand(MachineOperand &Dest); 138 bool parseMBBReference(MachineBasicBlock *&MBB); 139 bool parseMBBOperand(MachineOperand &Dest); 140 bool parseStackFrameIndex(int &FI); 141 bool parseStackObjectOperand(MachineOperand &Dest); 142 bool parseFixedStackFrameIndex(int &FI); 143 bool parseFixedStackObjectOperand(MachineOperand &Dest); 144 bool parseGlobalValue(GlobalValue *&GV); 145 bool parseGlobalAddressOperand(MachineOperand &Dest); 146 bool parseConstantPoolIndexOperand(MachineOperand &Dest); 147 bool parseSubRegisterIndexOperand(MachineOperand &Dest); 148 bool parseJumpTableIndexOperand(MachineOperand &Dest); 149 bool parseExternalSymbolOperand(MachineOperand &Dest); 150 bool parseMDNode(MDNode *&Node); 151 bool parseMetadataOperand(MachineOperand &Dest); 152 bool parseCFIOffset(int &Offset); 153 bool parseCFIRegister(unsigned &Reg); 154 bool parseCFIOperand(MachineOperand &Dest); 155 bool parseIRBlock(BasicBlock *&BB, const Function &F); 156 bool parseBlockAddressOperand(MachineOperand &Dest); 157 bool parseIntrinsicOperand(MachineOperand &Dest); 158 bool parseTargetIndexOperand(MachineOperand &Dest); 159 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest); 160 bool parseMachineOperand(MachineOperand &Dest, 161 Optional<unsigned> &TiedDefIdx); 162 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest, 163 Optional<unsigned> &TiedDefIdx); 164 bool parseOffset(int64_t &Offset); 165 bool parseAlignment(unsigned &Alignment); 166 bool parseOperandsOffset(MachineOperand &Op); 167 bool parseIRValue(const Value *&V); 168 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags); 169 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV); 170 bool parseMachinePointerInfo(MachinePointerInfo &Dest); 171 bool parseMachineMemoryOperand(MachineMemOperand *&Dest); 172 173 private: 174 /// Convert the integer literal in the current token into an unsigned integer. 175 /// 176 /// Return true if an error occurred. 177 bool getUnsigned(unsigned &Result); 178 179 /// Convert the integer literal in the current token into an uint64. 180 /// 181 /// Return true if an error occurred. 182 bool getUint64(uint64_t &Result); 183 184 /// If the current token is of the given kind, consume it and return false. 185 /// Otherwise report an error and return true. 186 bool expectAndConsume(MIToken::TokenKind TokenKind); 187 188 /// If the current token is of the given kind, consume it and return true. 189 /// Otherwise return false. 190 bool consumeIfPresent(MIToken::TokenKind TokenKind); 191 192 void initNames2InstrOpCodes(); 193 194 /// Try to convert an instruction name to an opcode. Return true if the 195 /// instruction name is invalid. 196 bool parseInstrName(StringRef InstrName, unsigned &OpCode); 197 198 bool parseInstruction(unsigned &OpCode, unsigned &Flags); 199 200 bool assignRegisterTies(MachineInstr &MI, 201 ArrayRef<ParsedMachineOperand> Operands); 202 203 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, 204 const MCInstrDesc &MCID); 205 206 void initNames2Regs(); 207 208 /// Try to convert a register name to a register number. Return true if the 209 /// register name is invalid. 210 bool getRegisterByName(StringRef RegName, unsigned &Reg); 211 212 void initNames2RegMasks(); 213 214 /// Check if the given identifier is a name of a register mask. 215 /// 216 /// Return null if the identifier isn't a register mask. 217 const uint32_t *getRegMask(StringRef Identifier); 218 219 void initNames2SubRegIndices(); 220 221 /// Check if the given identifier is a name of a subregister index. 222 /// 223 /// Return 0 if the name isn't a subregister index class. 224 unsigned getSubRegIndex(StringRef Name); 225 226 const BasicBlock *getIRBlock(unsigned Slot); 227 const BasicBlock *getIRBlock(unsigned Slot, const Function &F); 228 229 const Value *getIRValue(unsigned Slot); 230 231 void initNames2TargetIndices(); 232 233 /// Try to convert a name of target index to the corresponding target index. 234 /// 235 /// Return true if the name isn't a name of a target index. 236 bool getTargetIndex(StringRef Name, int &Index); 237 238 void initNames2DirectTargetFlags(); 239 240 /// Try to convert a name of a direct target flag to the corresponding 241 /// target flag. 242 /// 243 /// Return true if the name isn't a name of a direct flag. 244 bool getDirectTargetFlag(StringRef Name, unsigned &Flag); 245 246 void initNames2BitmaskTargetFlags(); 247 248 /// Try to convert a name of a bitmask target flag to the corresponding 249 /// target flag. 250 /// 251 /// Return true if the name isn't a name of a bitmask target flag. 252 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag); 253 }; 254 255 } // end anonymous namespace 256 257 MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error, 258 StringRef Source) 259 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS) 260 {} 261 262 void MIParser::lex(unsigned SkipChar) { 263 CurrentSource = lexMIToken( 264 CurrentSource.data() + SkipChar, Token, 265 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); 266 } 267 268 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } 269 270 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { 271 const SourceMgr &SM = *PFS.SM; 272 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); 273 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID()); 274 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) { 275 // Create an ordinary diagnostic when the source manager's buffer is the 276 // source string. 277 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); 278 return true; 279 } 280 // Create a diagnostic for a YAML string literal. 281 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 282 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), 283 Source, None, None); 284 return true; 285 } 286 287 static const char *toString(MIToken::TokenKind TokenKind) { 288 switch (TokenKind) { 289 case MIToken::comma: 290 return "','"; 291 case MIToken::equal: 292 return "'='"; 293 case MIToken::colon: 294 return "':'"; 295 case MIToken::lparen: 296 return "'('"; 297 case MIToken::rparen: 298 return "')'"; 299 default: 300 return "<unknown token>"; 301 } 302 } 303 304 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) { 305 if (Token.isNot(TokenKind)) 306 return error(Twine("expected ") + toString(TokenKind)); 307 lex(); 308 return false; 309 } 310 311 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) { 312 if (Token.isNot(TokenKind)) 313 return false; 314 lex(); 315 return true; 316 } 317 318 bool MIParser::parseBasicBlockDefinition( 319 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { 320 assert(Token.is(MIToken::MachineBasicBlockLabel)); 321 unsigned ID = 0; 322 if (getUnsigned(ID)) 323 return true; 324 auto Loc = Token.location(); 325 auto Name = Token.stringValue(); 326 lex(); 327 bool HasAddressTaken = false; 328 bool IsLandingPad = false; 329 unsigned Alignment = 0; 330 BasicBlock *BB = nullptr; 331 if (consumeIfPresent(MIToken::lparen)) { 332 do { 333 // TODO: Report an error when multiple same attributes are specified. 334 switch (Token.kind()) { 335 case MIToken::kw_address_taken: 336 HasAddressTaken = true; 337 lex(); 338 break; 339 case MIToken::kw_landing_pad: 340 IsLandingPad = true; 341 lex(); 342 break; 343 case MIToken::kw_align: 344 if (parseAlignment(Alignment)) 345 return true; 346 break; 347 case MIToken::IRBlock: 348 // TODO: Report an error when both name and ir block are specified. 349 if (parseIRBlock(BB, *MF.getFunction())) 350 return true; 351 lex(); 352 break; 353 default: 354 break; 355 } 356 } while (consumeIfPresent(MIToken::comma)); 357 if (expectAndConsume(MIToken::rparen)) 358 return true; 359 } 360 if (expectAndConsume(MIToken::colon)) 361 return true; 362 363 if (!Name.empty()) { 364 BB = dyn_cast_or_null<BasicBlock>( 365 MF.getFunction()->getValueSymbolTable().lookup(Name)); 366 if (!BB) 367 return error(Loc, Twine("basic block '") + Name + 368 "' is not defined in the function '" + 369 MF.getName() + "'"); 370 } 371 auto *MBB = MF.CreateMachineBasicBlock(BB); 372 MF.insert(MF.end(), MBB); 373 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second; 374 if (!WasInserted) 375 return error(Loc, Twine("redefinition of machine basic block with id #") + 376 Twine(ID)); 377 if (Alignment) 378 MBB->setAlignment(Alignment); 379 if (HasAddressTaken) 380 MBB->setHasAddressTaken(); 381 MBB->setIsEHPad(IsLandingPad); 382 return false; 383 } 384 385 bool MIParser::parseBasicBlockDefinitions( 386 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { 387 lex(); 388 // Skip until the first machine basic block. 389 while (Token.is(MIToken::Newline)) 390 lex(); 391 if (Token.isErrorOrEOF()) 392 return Token.isError(); 393 if (Token.isNot(MIToken::MachineBasicBlockLabel)) 394 return error("expected a basic block definition before instructions"); 395 unsigned BraceDepth = 0; 396 do { 397 if (parseBasicBlockDefinition(MBBSlots)) 398 return true; 399 bool IsAfterNewline = false; 400 // Skip until the next machine basic block. 401 while (true) { 402 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) || 403 Token.isErrorOrEOF()) 404 break; 405 else if (Token.is(MIToken::MachineBasicBlockLabel)) 406 return error("basic block definition should be located at the start of " 407 "the line"); 408 else if (consumeIfPresent(MIToken::Newline)) { 409 IsAfterNewline = true; 410 continue; 411 } 412 IsAfterNewline = false; 413 if (Token.is(MIToken::lbrace)) 414 ++BraceDepth; 415 if (Token.is(MIToken::rbrace)) { 416 if (!BraceDepth) 417 return error("extraneous closing brace ('}')"); 418 --BraceDepth; 419 } 420 lex(); 421 } 422 // Verify that we closed all of the '{' at the end of a file or a block. 423 if (!Token.isError() && BraceDepth) 424 return error("expected '}'"); // FIXME: Report a note that shows '{'. 425 } while (!Token.isErrorOrEOF()); 426 return Token.isError(); 427 } 428 429 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) { 430 assert(Token.is(MIToken::kw_liveins)); 431 lex(); 432 if (expectAndConsume(MIToken::colon)) 433 return true; 434 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins. 435 return false; 436 do { 437 if (Token.isNot(MIToken::NamedRegister)) 438 return error("expected a named register"); 439 unsigned Reg = 0; 440 if (parseRegister(Reg)) 441 return true; 442 MBB.addLiveIn(Reg); 443 lex(); 444 } while (consumeIfPresent(MIToken::comma)); 445 return false; 446 } 447 448 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) { 449 assert(Token.is(MIToken::kw_successors)); 450 lex(); 451 if (expectAndConsume(MIToken::colon)) 452 return true; 453 if (Token.isNewlineOrEOF()) // Allow an empty list of successors. 454 return false; 455 do { 456 if (Token.isNot(MIToken::MachineBasicBlock)) 457 return error("expected a machine basic block reference"); 458 MachineBasicBlock *SuccMBB = nullptr; 459 if (parseMBBReference(SuccMBB)) 460 return true; 461 lex(); 462 unsigned Weight = 0; 463 if (consumeIfPresent(MIToken::lparen)) { 464 if (Token.isNot(MIToken::IntegerLiteral)) 465 return error("expected an integer literal after '('"); 466 if (getUnsigned(Weight)) 467 return true; 468 lex(); 469 if (expectAndConsume(MIToken::rparen)) 470 return true; 471 } 472 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight)); 473 } while (consumeIfPresent(MIToken::comma)); 474 MBB.normalizeSuccProbs(); 475 return false; 476 } 477 478 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) { 479 // Skip the definition. 480 assert(Token.is(MIToken::MachineBasicBlockLabel)); 481 lex(); 482 if (consumeIfPresent(MIToken::lparen)) { 483 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF()) 484 lex(); 485 consumeIfPresent(MIToken::rparen); 486 } 487 consumeIfPresent(MIToken::colon); 488 489 // Parse the liveins and successors. 490 // N.B: Multiple lists of successors and liveins are allowed and they're 491 // merged into one. 492 // Example: 493 // liveins: %edi 494 // liveins: %esi 495 // 496 // is equivalent to 497 // liveins: %edi, %esi 498 while (true) { 499 if (Token.is(MIToken::kw_successors)) { 500 if (parseBasicBlockSuccessors(MBB)) 501 return true; 502 } else if (Token.is(MIToken::kw_liveins)) { 503 if (parseBasicBlockLiveins(MBB)) 504 return true; 505 } else if (consumeIfPresent(MIToken::Newline)) { 506 continue; 507 } else 508 break; 509 if (!Token.isNewlineOrEOF()) 510 return error("expected line break at the end of a list"); 511 lex(); 512 } 513 514 // Parse the instructions. 515 bool IsInBundle = false; 516 MachineInstr *PrevMI = nullptr; 517 while (true) { 518 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)) 519 return false; 520 else if (consumeIfPresent(MIToken::Newline)) 521 continue; 522 if (consumeIfPresent(MIToken::rbrace)) { 523 // The first parsing pass should verify that all closing '}' have an 524 // opening '{'. 525 assert(IsInBundle); 526 IsInBundle = false; 527 continue; 528 } 529 MachineInstr *MI = nullptr; 530 if (parse(MI)) 531 return true; 532 MBB.insert(MBB.end(), MI); 533 if (IsInBundle) { 534 PrevMI->setFlag(MachineInstr::BundledSucc); 535 MI->setFlag(MachineInstr::BundledPred); 536 } 537 PrevMI = MI; 538 if (Token.is(MIToken::lbrace)) { 539 if (IsInBundle) 540 return error("nested instruction bundles are not allowed"); 541 lex(); 542 // This instruction is the start of the bundle. 543 MI->setFlag(MachineInstr::BundledSucc); 544 IsInBundle = true; 545 if (!Token.is(MIToken::Newline)) 546 // The next instruction can be on the same line. 547 continue; 548 } 549 assert(Token.isNewlineOrEOF() && "MI is not fully parsed"); 550 lex(); 551 } 552 return false; 553 } 554 555 bool MIParser::parseBasicBlocks() { 556 lex(); 557 // Skip until the first machine basic block. 558 while (Token.is(MIToken::Newline)) 559 lex(); 560 if (Token.isErrorOrEOF()) 561 return Token.isError(); 562 // The first parsing pass should have verified that this token is a MBB label 563 // in the 'parseBasicBlockDefinitions' method. 564 assert(Token.is(MIToken::MachineBasicBlockLabel)); 565 do { 566 MachineBasicBlock *MBB = nullptr; 567 if (parseMBBReference(MBB)) 568 return true; 569 if (parseBasicBlock(*MBB)) 570 return true; 571 // The method 'parseBasicBlock' should parse the whole block until the next 572 // block or the end of file. 573 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)); 574 } while (Token.isNot(MIToken::Eof)); 575 return false; 576 } 577 578 bool MIParser::parse(MachineInstr *&MI) { 579 // Parse any register operands before '=' 580 MachineOperand MO = MachineOperand::CreateImm(0); 581 SmallVector<ParsedMachineOperand, 8> Operands; 582 while (Token.isRegister() || Token.isRegisterFlag()) { 583 auto Loc = Token.location(); 584 Optional<unsigned> TiedDefIdx; 585 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true)) 586 return true; 587 Operands.push_back( 588 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); 589 if (Token.isNot(MIToken::comma)) 590 break; 591 lex(); 592 } 593 if (!Operands.empty() && expectAndConsume(MIToken::equal)) 594 return true; 595 596 unsigned OpCode, Flags = 0; 597 if (Token.isError() || parseInstruction(OpCode, Flags)) 598 return true; 599 600 SmallVector<LLT, 1> Tys; 601 if (isPreISelGenericOpcode(OpCode)) { 602 // For generic opcode, at least one type is mandatory. 603 auto Loc = Token.location(); 604 bool ManyTypes = Token.is(MIToken::lbrace); 605 if (ManyTypes) 606 lex(); 607 608 // Now actually parse the type(s). 609 do { 610 Tys.resize(Tys.size() + 1); 611 if (parseLowLevelType(Loc, Tys[Tys.size() - 1])) 612 return true; 613 } while (ManyTypes && consumeIfPresent(MIToken::comma)); 614 615 if (ManyTypes) 616 expectAndConsume(MIToken::rbrace); 617 } 618 619 // Parse the remaining machine operands. 620 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) && 621 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) { 622 auto Loc = Token.location(); 623 Optional<unsigned> TiedDefIdx; 624 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx)) 625 return true; 626 Operands.push_back( 627 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); 628 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) || 629 Token.is(MIToken::lbrace)) 630 break; 631 if (Token.isNot(MIToken::comma)) 632 return error("expected ',' before the next machine operand"); 633 lex(); 634 } 635 636 DebugLoc DebugLocation; 637 if (Token.is(MIToken::kw_debug_location)) { 638 lex(); 639 if (Token.isNot(MIToken::exclaim)) 640 return error("expected a metadata node after 'debug-location'"); 641 MDNode *Node = nullptr; 642 if (parseMDNode(Node)) 643 return true; 644 DebugLocation = DebugLoc(Node); 645 } 646 647 // Parse the machine memory operands. 648 SmallVector<MachineMemOperand *, 2> MemOperands; 649 if (Token.is(MIToken::coloncolon)) { 650 lex(); 651 while (!Token.isNewlineOrEOF()) { 652 MachineMemOperand *MemOp = nullptr; 653 if (parseMachineMemoryOperand(MemOp)) 654 return true; 655 MemOperands.push_back(MemOp); 656 if (Token.isNewlineOrEOF()) 657 break; 658 if (Token.isNot(MIToken::comma)) 659 return error("expected ',' before the next machine memory operand"); 660 lex(); 661 } 662 } 663 664 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode); 665 if (!MCID.isVariadic()) { 666 // FIXME: Move the implicit operand verification to the machine verifier. 667 if (verifyImplicitOperands(Operands, MCID)) 668 return true; 669 } 670 671 // TODO: Check for extraneous machine operands. 672 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true); 673 MI->setFlags(Flags); 674 if (Tys.size() > 0) { 675 for (unsigned i = 0; i < Tys.size(); ++i) 676 MI->setType(Tys[i], i); 677 } 678 for (const auto &Operand : Operands) 679 MI->addOperand(MF, Operand.Operand); 680 if (assignRegisterTies(*MI, Operands)) 681 return true; 682 if (MemOperands.empty()) 683 return false; 684 MachineInstr::mmo_iterator MemRefs = 685 MF.allocateMemRefsArray(MemOperands.size()); 686 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs); 687 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size()); 688 return false; 689 } 690 691 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) { 692 lex(); 693 if (Token.isNot(MIToken::MachineBasicBlock)) 694 return error("expected a machine basic block reference"); 695 if (parseMBBReference(MBB)) 696 return true; 697 lex(); 698 if (Token.isNot(MIToken::Eof)) 699 return error( 700 "expected end of string after the machine basic block reference"); 701 return false; 702 } 703 704 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) { 705 lex(); 706 if (Token.isNot(MIToken::NamedRegister)) 707 return error("expected a named register"); 708 if (parseRegister(Reg)) 709 return true; 710 lex(); 711 if (Token.isNot(MIToken::Eof)) 712 return error("expected end of string after the register reference"); 713 return false; 714 } 715 716 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) { 717 lex(); 718 if (Token.isNot(MIToken::VirtualRegister)) 719 return error("expected a virtual register"); 720 if (parseRegister(Reg)) 721 return true; 722 lex(); 723 if (Token.isNot(MIToken::Eof)) 724 return error("expected end of string after the register reference"); 725 return false; 726 } 727 728 bool MIParser::parseStandaloneStackObject(int &FI) { 729 lex(); 730 if (Token.isNot(MIToken::StackObject)) 731 return error("expected a stack object"); 732 if (parseStackFrameIndex(FI)) 733 return true; 734 if (Token.isNot(MIToken::Eof)) 735 return error("expected end of string after the stack object reference"); 736 return false; 737 } 738 739 bool MIParser::parseStandaloneMDNode(MDNode *&Node) { 740 lex(); 741 if (Token.isNot(MIToken::exclaim)) 742 return error("expected a metadata node"); 743 if (parseMDNode(Node)) 744 return true; 745 if (Token.isNot(MIToken::Eof)) 746 return error("expected end of string after the metadata node"); 747 return false; 748 } 749 750 static const char *printImplicitRegisterFlag(const MachineOperand &MO) { 751 assert(MO.isImplicit()); 752 return MO.isDef() ? "implicit-def" : "implicit"; 753 } 754 755 static std::string getRegisterName(const TargetRegisterInfo *TRI, 756 unsigned Reg) { 757 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg"); 758 return StringRef(TRI->getName(Reg)).lower(); 759 } 760 761 /// Return true if the parsed machine operands contain a given machine operand. 762 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, 763 ArrayRef<ParsedMachineOperand> Operands) { 764 for (const auto &I : Operands) { 765 if (ImplicitOperand.isIdenticalTo(I.Operand)) 766 return true; 767 } 768 return false; 769 } 770 771 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, 772 const MCInstrDesc &MCID) { 773 if (MCID.isCall()) 774 // We can't verify call instructions as they can contain arbitrary implicit 775 // register and register mask operands. 776 return false; 777 778 // Gather all the expected implicit operands. 779 SmallVector<MachineOperand, 4> ImplicitOperands; 780 if (MCID.ImplicitDefs) 781 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs) 782 ImplicitOperands.push_back( 783 MachineOperand::CreateReg(*ImpDefs, true, true)); 784 if (MCID.ImplicitUses) 785 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses) 786 ImplicitOperands.push_back( 787 MachineOperand::CreateReg(*ImpUses, false, true)); 788 789 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 790 assert(TRI && "Expected target register info"); 791 for (const auto &I : ImplicitOperands) { 792 if (isImplicitOperandIn(I, Operands)) 793 continue; 794 return error(Operands.empty() ? Token.location() : Operands.back().End, 795 Twine("missing implicit register operand '") + 796 printImplicitRegisterFlag(I) + " %" + 797 getRegisterName(TRI, I.getReg()) + "'"); 798 } 799 return false; 800 } 801 802 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) { 803 if (Token.is(MIToken::kw_frame_setup)) { 804 Flags |= MachineInstr::FrameSetup; 805 lex(); 806 } 807 if (Token.isNot(MIToken::Identifier)) 808 return error("expected a machine instruction"); 809 StringRef InstrName = Token.stringValue(); 810 if (parseInstrName(InstrName, OpCode)) 811 return error(Twine("unknown machine instruction name '") + InstrName + "'"); 812 lex(); 813 return false; 814 } 815 816 bool MIParser::parseRegister(unsigned &Reg) { 817 switch (Token.kind()) { 818 case MIToken::underscore: 819 Reg = 0; 820 break; 821 case MIToken::NamedRegister: { 822 StringRef Name = Token.stringValue(); 823 if (getRegisterByName(Name, Reg)) 824 return error(Twine("unknown register name '") + Name + "'"); 825 break; 826 } 827 case MIToken::VirtualRegister: { 828 unsigned ID; 829 if (getUnsigned(ID)) 830 return true; 831 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID); 832 if (RegInfo == PFS.VirtualRegisterSlots.end()) 833 return error(Twine("use of undefined virtual register '%") + Twine(ID) + 834 "'"); 835 Reg = RegInfo->second; 836 break; 837 } 838 // TODO: Parse other register kinds. 839 default: 840 llvm_unreachable("The current token should be a register"); 841 } 842 return false; 843 } 844 845 bool MIParser::parseRegisterFlag(unsigned &Flags) { 846 const unsigned OldFlags = Flags; 847 switch (Token.kind()) { 848 case MIToken::kw_implicit: 849 Flags |= RegState::Implicit; 850 break; 851 case MIToken::kw_implicit_define: 852 Flags |= RegState::ImplicitDefine; 853 break; 854 case MIToken::kw_def: 855 Flags |= RegState::Define; 856 break; 857 case MIToken::kw_dead: 858 Flags |= RegState::Dead; 859 break; 860 case MIToken::kw_killed: 861 Flags |= RegState::Kill; 862 break; 863 case MIToken::kw_undef: 864 Flags |= RegState::Undef; 865 break; 866 case MIToken::kw_internal: 867 Flags |= RegState::InternalRead; 868 break; 869 case MIToken::kw_early_clobber: 870 Flags |= RegState::EarlyClobber; 871 break; 872 case MIToken::kw_debug_use: 873 Flags |= RegState::Debug; 874 break; 875 default: 876 llvm_unreachable("The current token should be a register flag"); 877 } 878 if (OldFlags == Flags) 879 // We know that the same flag is specified more than once when the flags 880 // weren't modified. 881 return error("duplicate '" + Token.stringValue() + "' register flag"); 882 lex(); 883 return false; 884 } 885 886 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) { 887 assert(Token.is(MIToken::dot)); 888 lex(); 889 if (Token.isNot(MIToken::Identifier)) 890 return error("expected a subregister index after '.'"); 891 auto Name = Token.stringValue(); 892 SubReg = getSubRegIndex(Name); 893 if (!SubReg) 894 return error(Twine("use of unknown subregister index '") + Name + "'"); 895 lex(); 896 return false; 897 } 898 899 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) { 900 if (!consumeIfPresent(MIToken::kw_tied_def)) 901 return error("expected 'tied-def' after '('"); 902 if (Token.isNot(MIToken::IntegerLiteral)) 903 return error("expected an integer literal after 'tied-def'"); 904 if (getUnsigned(TiedDefIdx)) 905 return true; 906 lex(); 907 if (expectAndConsume(MIToken::rparen)) 908 return true; 909 return false; 910 } 911 912 bool MIParser::parseSize(unsigned &Size) { 913 if (Token.isNot(MIToken::IntegerLiteral)) 914 return error("expected an integer literal for the size"); 915 if (getUnsigned(Size)) 916 return true; 917 lex(); 918 if (expectAndConsume(MIToken::rparen)) 919 return true; 920 return false; 921 } 922 923 bool MIParser::assignRegisterTies(MachineInstr &MI, 924 ArrayRef<ParsedMachineOperand> Operands) { 925 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs; 926 for (unsigned I = 0, E = Operands.size(); I != E; ++I) { 927 if (!Operands[I].TiedDefIdx) 928 continue; 929 // The parser ensures that this operand is a register use, so we just have 930 // to check the tied-def operand. 931 unsigned DefIdx = Operands[I].TiedDefIdx.getValue(); 932 if (DefIdx >= E) 933 return error(Operands[I].Begin, 934 Twine("use of invalid tied-def operand index '" + 935 Twine(DefIdx) + "'; instruction has only ") + 936 Twine(E) + " operands"); 937 const auto &DefOperand = Operands[DefIdx].Operand; 938 if (!DefOperand.isReg() || !DefOperand.isDef()) 939 // FIXME: add note with the def operand. 940 return error(Operands[I].Begin, 941 Twine("use of invalid tied-def operand index '") + 942 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) + 943 " isn't a defined register"); 944 // Check that the tied-def operand wasn't tied elsewhere. 945 for (const auto &TiedPair : TiedRegisterPairs) { 946 if (TiedPair.first == DefIdx) 947 return error(Operands[I].Begin, 948 Twine("the tied-def operand #") + Twine(DefIdx) + 949 " is already tied with another register operand"); 950 } 951 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I)); 952 } 953 // FIXME: Verify that for non INLINEASM instructions, the def and use tied 954 // indices must be less than tied max. 955 for (const auto &TiedPair : TiedRegisterPairs) 956 MI.tieOperands(TiedPair.first, TiedPair.second); 957 return false; 958 } 959 960 bool MIParser::parseRegisterOperand(MachineOperand &Dest, 961 Optional<unsigned> &TiedDefIdx, 962 bool IsDef) { 963 unsigned Reg; 964 unsigned Flags = IsDef ? RegState::Define : 0; 965 while (Token.isRegisterFlag()) { 966 if (parseRegisterFlag(Flags)) 967 return true; 968 } 969 if (!Token.isRegister()) 970 return error("expected a register after register flags"); 971 if (parseRegister(Reg)) 972 return true; 973 lex(); 974 unsigned SubReg = 0; 975 if (Token.is(MIToken::dot)) { 976 if (parseSubRegisterIndex(SubReg)) 977 return true; 978 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 979 return error("subregister index expects a virtual register"); 980 } 981 if ((Flags & RegState::Define) == 0) { 982 if (consumeIfPresent(MIToken::lparen)) { 983 unsigned Idx; 984 if (parseRegisterTiedDefIndex(Idx)) 985 return true; 986 TiedDefIdx = Idx; 987 } 988 } else if (consumeIfPresent(MIToken::lparen)) { 989 MachineRegisterInfo &MRI = MF.getRegInfo(); 990 991 // Virtual registers may have a size with GlobalISel. 992 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 993 return error("unexpected size on physical register"); 994 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>()) 995 return error("unexpected size on non-generic virtual register"); 996 997 unsigned Size; 998 if (parseSize(Size)) 999 return true; 1000 1001 MRI.setSize(Reg, Size); 1002 } else if (PFS.GenericVRegs.count(Reg)) { 1003 // Generic virtual registers must have a size. 1004 // If we end up here this means the size hasn't been specified and 1005 // this is bad! 1006 return error("generic virtual registers must have a size"); 1007 } 1008 Dest = MachineOperand::CreateReg( 1009 Reg, Flags & RegState::Define, Flags & RegState::Implicit, 1010 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef, 1011 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug, 1012 Flags & RegState::InternalRead); 1013 return false; 1014 } 1015 1016 bool MIParser::parseImmediateOperand(MachineOperand &Dest) { 1017 assert(Token.is(MIToken::IntegerLiteral)); 1018 const APSInt &Int = Token.integerValue(); 1019 if (Int.getMinSignedBits() > 64) 1020 return error("integer literal is too large to be an immediate operand"); 1021 Dest = MachineOperand::CreateImm(Int.getExtValue()); 1022 lex(); 1023 return false; 1024 } 1025 1026 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue, 1027 const Constant *&C) { 1028 auto Source = StringValue.str(); // The source has to be null terminated. 1029 SMDiagnostic Err; 1030 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(), 1031 &PFS.IRSlots); 1032 if (!C) 1033 return error(Loc + Err.getColumnNo(), Err.getMessage()); 1034 return false; 1035 } 1036 1037 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) { 1038 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C)) 1039 return true; 1040 lex(); 1041 return false; 1042 } 1043 1044 bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) { 1045 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") { 1046 lex(); 1047 Ty = LLT::unsized(); 1048 return false; 1049 } else if (Token.is(MIToken::ScalarType)) { 1050 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue()); 1051 lex(); 1052 return false; 1053 } else if (Token.is(MIToken::PointerType)) { 1054 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue()); 1055 lex(); 1056 return false; 1057 } 1058 1059 // Now we're looking for a vector. 1060 if (Token.isNot(MIToken::less)) 1061 return error(Loc, 1062 "expected unsized, pN, sN or <N x sM> for GlobalISel type"); 1063 1064 lex(); 1065 1066 if (Token.isNot(MIToken::IntegerLiteral)) 1067 return error(Loc, "expected <N x sM> for vctor type"); 1068 uint64_t NumElements = Token.integerValue().getZExtValue(); 1069 lex(); 1070 1071 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x") 1072 return error(Loc, "expected '<N x sM>' for vector type"); 1073 lex(); 1074 1075 if (Token.isNot(MIToken::ScalarType)) 1076 return error(Loc, "expected '<N x sM>' for vector type"); 1077 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue(); 1078 lex(); 1079 1080 if (Token.isNot(MIToken::greater)) 1081 return error(Loc, "expected '<N x sM>' for vector type"); 1082 lex(); 1083 1084 Ty = LLT::vector(NumElements, ScalarSize); 1085 return false; 1086 } 1087 1088 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) { 1089 assert(Token.is(MIToken::IntegerType)); 1090 auto Loc = Token.location(); 1091 lex(); 1092 if (Token.isNot(MIToken::IntegerLiteral)) 1093 return error("expected an integer literal"); 1094 const Constant *C = nullptr; 1095 if (parseIRConstant(Loc, C)) 1096 return true; 1097 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C)); 1098 return false; 1099 } 1100 1101 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) { 1102 auto Loc = Token.location(); 1103 lex(); 1104 if (Token.isNot(MIToken::FloatingPointLiteral)) 1105 return error("expected a floating point literal"); 1106 const Constant *C = nullptr; 1107 if (parseIRConstant(Loc, C)) 1108 return true; 1109 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C)); 1110 return false; 1111 } 1112 1113 bool MIParser::getUnsigned(unsigned &Result) { 1114 assert(Token.hasIntegerValue() && "Expected a token with an integer value"); 1115 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1; 1116 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit); 1117 if (Val64 == Limit) 1118 return error("expected 32-bit integer (too large)"); 1119 Result = Val64; 1120 return false; 1121 } 1122 1123 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) { 1124 assert(Token.is(MIToken::MachineBasicBlock) || 1125 Token.is(MIToken::MachineBasicBlockLabel)); 1126 unsigned Number; 1127 if (getUnsigned(Number)) 1128 return true; 1129 auto MBBInfo = PFS.MBBSlots.find(Number); 1130 if (MBBInfo == PFS.MBBSlots.end()) 1131 return error(Twine("use of undefined machine basic block #") + 1132 Twine(Number)); 1133 MBB = MBBInfo->second; 1134 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName()) 1135 return error(Twine("the name of machine basic block #") + Twine(Number) + 1136 " isn't '" + Token.stringValue() + "'"); 1137 return false; 1138 } 1139 1140 bool MIParser::parseMBBOperand(MachineOperand &Dest) { 1141 MachineBasicBlock *MBB; 1142 if (parseMBBReference(MBB)) 1143 return true; 1144 Dest = MachineOperand::CreateMBB(MBB); 1145 lex(); 1146 return false; 1147 } 1148 1149 bool MIParser::parseStackFrameIndex(int &FI) { 1150 assert(Token.is(MIToken::StackObject)); 1151 unsigned ID; 1152 if (getUnsigned(ID)) 1153 return true; 1154 auto ObjectInfo = PFS.StackObjectSlots.find(ID); 1155 if (ObjectInfo == PFS.StackObjectSlots.end()) 1156 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) + 1157 "'"); 1158 StringRef Name; 1159 if (const auto *Alloca = 1160 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second)) 1161 Name = Alloca->getName(); 1162 if (!Token.stringValue().empty() && Token.stringValue() != Name) 1163 return error(Twine("the name of the stack object '%stack.") + Twine(ID) + 1164 "' isn't '" + Token.stringValue() + "'"); 1165 lex(); 1166 FI = ObjectInfo->second; 1167 return false; 1168 } 1169 1170 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) { 1171 int FI; 1172 if (parseStackFrameIndex(FI)) 1173 return true; 1174 Dest = MachineOperand::CreateFI(FI); 1175 return false; 1176 } 1177 1178 bool MIParser::parseFixedStackFrameIndex(int &FI) { 1179 assert(Token.is(MIToken::FixedStackObject)); 1180 unsigned ID; 1181 if (getUnsigned(ID)) 1182 return true; 1183 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID); 1184 if (ObjectInfo == PFS.FixedStackObjectSlots.end()) 1185 return error(Twine("use of undefined fixed stack object '%fixed-stack.") + 1186 Twine(ID) + "'"); 1187 lex(); 1188 FI = ObjectInfo->second; 1189 return false; 1190 } 1191 1192 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) { 1193 int FI; 1194 if (parseFixedStackFrameIndex(FI)) 1195 return true; 1196 Dest = MachineOperand::CreateFI(FI); 1197 return false; 1198 } 1199 1200 bool MIParser::parseGlobalValue(GlobalValue *&GV) { 1201 switch (Token.kind()) { 1202 case MIToken::NamedGlobalValue: { 1203 const Module *M = MF.getFunction()->getParent(); 1204 GV = M->getNamedValue(Token.stringValue()); 1205 if (!GV) 1206 return error(Twine("use of undefined global value '") + Token.range() + 1207 "'"); 1208 break; 1209 } 1210 case MIToken::GlobalValue: { 1211 unsigned GVIdx; 1212 if (getUnsigned(GVIdx)) 1213 return true; 1214 if (GVIdx >= PFS.IRSlots.GlobalValues.size()) 1215 return error(Twine("use of undefined global value '@") + Twine(GVIdx) + 1216 "'"); 1217 GV = PFS.IRSlots.GlobalValues[GVIdx]; 1218 break; 1219 } 1220 default: 1221 llvm_unreachable("The current token should be a global value"); 1222 } 1223 return false; 1224 } 1225 1226 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) { 1227 GlobalValue *GV = nullptr; 1228 if (parseGlobalValue(GV)) 1229 return true; 1230 lex(); 1231 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0); 1232 if (parseOperandsOffset(Dest)) 1233 return true; 1234 return false; 1235 } 1236 1237 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) { 1238 assert(Token.is(MIToken::ConstantPoolItem)); 1239 unsigned ID; 1240 if (getUnsigned(ID)) 1241 return true; 1242 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID); 1243 if (ConstantInfo == PFS.ConstantPoolSlots.end()) 1244 return error("use of undefined constant '%const." + Twine(ID) + "'"); 1245 lex(); 1246 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0); 1247 if (parseOperandsOffset(Dest)) 1248 return true; 1249 return false; 1250 } 1251 1252 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) { 1253 assert(Token.is(MIToken::JumpTableIndex)); 1254 unsigned ID; 1255 if (getUnsigned(ID)) 1256 return true; 1257 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID); 1258 if (JumpTableEntryInfo == PFS.JumpTableSlots.end()) 1259 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'"); 1260 lex(); 1261 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second); 1262 return false; 1263 } 1264 1265 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) { 1266 assert(Token.is(MIToken::ExternalSymbol)); 1267 const char *Symbol = MF.createExternalSymbolName(Token.stringValue()); 1268 lex(); 1269 Dest = MachineOperand::CreateES(Symbol); 1270 if (parseOperandsOffset(Dest)) 1271 return true; 1272 return false; 1273 } 1274 1275 bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) { 1276 assert(Token.is(MIToken::SubRegisterIndex)); 1277 StringRef Name = Token.stringValue(); 1278 unsigned SubRegIndex = getSubRegIndex(Token.stringValue()); 1279 if (SubRegIndex == 0) 1280 return error(Twine("unknown subregister index '") + Name + "'"); 1281 lex(); 1282 Dest = MachineOperand::CreateImm(SubRegIndex); 1283 return false; 1284 } 1285 1286 bool MIParser::parseMDNode(MDNode *&Node) { 1287 assert(Token.is(MIToken::exclaim)); 1288 auto Loc = Token.location(); 1289 lex(); 1290 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) 1291 return error("expected metadata id after '!'"); 1292 unsigned ID; 1293 if (getUnsigned(ID)) 1294 return true; 1295 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID); 1296 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) 1297 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'"); 1298 lex(); 1299 Node = NodeInfo->second.get(); 1300 return false; 1301 } 1302 1303 bool MIParser::parseMetadataOperand(MachineOperand &Dest) { 1304 MDNode *Node = nullptr; 1305 if (parseMDNode(Node)) 1306 return true; 1307 Dest = MachineOperand::CreateMetadata(Node); 1308 return false; 1309 } 1310 1311 bool MIParser::parseCFIOffset(int &Offset) { 1312 if (Token.isNot(MIToken::IntegerLiteral)) 1313 return error("expected a cfi offset"); 1314 if (Token.integerValue().getMinSignedBits() > 32) 1315 return error("expected a 32 bit integer (the cfi offset is too large)"); 1316 Offset = (int)Token.integerValue().getExtValue(); 1317 lex(); 1318 return false; 1319 } 1320 1321 bool MIParser::parseCFIRegister(unsigned &Reg) { 1322 if (Token.isNot(MIToken::NamedRegister)) 1323 return error("expected a cfi register"); 1324 unsigned LLVMReg; 1325 if (parseRegister(LLVMReg)) 1326 return true; 1327 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1328 assert(TRI && "Expected target register info"); 1329 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true); 1330 if (DwarfReg < 0) 1331 return error("invalid DWARF register"); 1332 Reg = (unsigned)DwarfReg; 1333 lex(); 1334 return false; 1335 } 1336 1337 bool MIParser::parseCFIOperand(MachineOperand &Dest) { 1338 auto Kind = Token.kind(); 1339 lex(); 1340 auto &MMI = MF.getMMI(); 1341 int Offset; 1342 unsigned Reg; 1343 unsigned CFIIndex; 1344 switch (Kind) { 1345 case MIToken::kw_cfi_same_value: 1346 if (parseCFIRegister(Reg)) 1347 return true; 1348 CFIIndex = 1349 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg)); 1350 break; 1351 case MIToken::kw_cfi_offset: 1352 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || 1353 parseCFIOffset(Offset)) 1354 return true; 1355 CFIIndex = 1356 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset)); 1357 break; 1358 case MIToken::kw_cfi_def_cfa_register: 1359 if (parseCFIRegister(Reg)) 1360 return true; 1361 CFIIndex = 1362 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); 1363 break; 1364 case MIToken::kw_cfi_def_cfa_offset: 1365 if (parseCFIOffset(Offset)) 1366 return true; 1367 // NB: MCCFIInstruction::createDefCfaOffset negates the offset. 1368 CFIIndex = MMI.addFrameInst( 1369 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)); 1370 break; 1371 case MIToken::kw_cfi_def_cfa: 1372 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || 1373 parseCFIOffset(Offset)) 1374 return true; 1375 // NB: MCCFIInstruction::createDefCfa negates the offset. 1376 CFIIndex = 1377 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset)); 1378 break; 1379 default: 1380 // TODO: Parse the other CFI operands. 1381 llvm_unreachable("The current token should be a cfi operand"); 1382 } 1383 Dest = MachineOperand::CreateCFIIndex(CFIIndex); 1384 return false; 1385 } 1386 1387 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) { 1388 switch (Token.kind()) { 1389 case MIToken::NamedIRBlock: { 1390 BB = dyn_cast_or_null<BasicBlock>( 1391 F.getValueSymbolTable().lookup(Token.stringValue())); 1392 if (!BB) 1393 return error(Twine("use of undefined IR block '") + Token.range() + "'"); 1394 break; 1395 } 1396 case MIToken::IRBlock: { 1397 unsigned SlotNumber = 0; 1398 if (getUnsigned(SlotNumber)) 1399 return true; 1400 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F)); 1401 if (!BB) 1402 return error(Twine("use of undefined IR block '%ir-block.") + 1403 Twine(SlotNumber) + "'"); 1404 break; 1405 } 1406 default: 1407 llvm_unreachable("The current token should be an IR block reference"); 1408 } 1409 return false; 1410 } 1411 1412 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) { 1413 assert(Token.is(MIToken::kw_blockaddress)); 1414 lex(); 1415 if (expectAndConsume(MIToken::lparen)) 1416 return true; 1417 if (Token.isNot(MIToken::GlobalValue) && 1418 Token.isNot(MIToken::NamedGlobalValue)) 1419 return error("expected a global value"); 1420 GlobalValue *GV = nullptr; 1421 if (parseGlobalValue(GV)) 1422 return true; 1423 auto *F = dyn_cast<Function>(GV); 1424 if (!F) 1425 return error("expected an IR function reference"); 1426 lex(); 1427 if (expectAndConsume(MIToken::comma)) 1428 return true; 1429 BasicBlock *BB = nullptr; 1430 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock)) 1431 return error("expected an IR block reference"); 1432 if (parseIRBlock(BB, *F)) 1433 return true; 1434 lex(); 1435 if (expectAndConsume(MIToken::rparen)) 1436 return true; 1437 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0); 1438 if (parseOperandsOffset(Dest)) 1439 return true; 1440 return false; 1441 } 1442 1443 bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) { 1444 assert(Token.is(MIToken::kw_intrinsic)); 1445 lex(); 1446 if (expectAndConsume(MIToken::lparen)) 1447 return error("expected syntax intrinsic(@llvm.whatever)"); 1448 1449 if (Token.isNot(MIToken::NamedGlobalValue)) 1450 return error("expected syntax intrinsic(@llvm.whatever)"); 1451 1452 std::string Name = Token.stringValue(); 1453 lex(); 1454 1455 if (expectAndConsume(MIToken::rparen)) 1456 return error("expected ')' to terminate intrinsic name"); 1457 1458 // Find out what intrinsic we're dealing with, first try the global namespace 1459 // and then the target's private intrinsics if that fails. 1460 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo(); 1461 Intrinsic::ID ID = Function::lookupIntrinsicID(Name); 1462 if (ID == Intrinsic::not_intrinsic && TII) 1463 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name)); 1464 1465 if (ID == Intrinsic::not_intrinsic) 1466 return error("unknown intrinsic name"); 1467 Dest = MachineOperand::CreateIntrinsicID(ID); 1468 1469 return false; 1470 } 1471 1472 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) { 1473 assert(Token.is(MIToken::kw_target_index)); 1474 lex(); 1475 if (expectAndConsume(MIToken::lparen)) 1476 return true; 1477 if (Token.isNot(MIToken::Identifier)) 1478 return error("expected the name of the target index"); 1479 int Index = 0; 1480 if (getTargetIndex(Token.stringValue(), Index)) 1481 return error("use of undefined target index '" + Token.stringValue() + "'"); 1482 lex(); 1483 if (expectAndConsume(MIToken::rparen)) 1484 return true; 1485 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0); 1486 if (parseOperandsOffset(Dest)) 1487 return true; 1488 return false; 1489 } 1490 1491 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) { 1492 assert(Token.is(MIToken::kw_liveout)); 1493 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1494 assert(TRI && "Expected target register info"); 1495 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs()); 1496 lex(); 1497 if (expectAndConsume(MIToken::lparen)) 1498 return true; 1499 while (true) { 1500 if (Token.isNot(MIToken::NamedRegister)) 1501 return error("expected a named register"); 1502 unsigned Reg = 0; 1503 if (parseRegister(Reg)) 1504 return true; 1505 lex(); 1506 Mask[Reg / 32] |= 1U << (Reg % 32); 1507 // TODO: Report an error if the same register is used more than once. 1508 if (Token.isNot(MIToken::comma)) 1509 break; 1510 lex(); 1511 } 1512 if (expectAndConsume(MIToken::rparen)) 1513 return true; 1514 Dest = MachineOperand::CreateRegLiveOut(Mask); 1515 return false; 1516 } 1517 1518 bool MIParser::parseMachineOperand(MachineOperand &Dest, 1519 Optional<unsigned> &TiedDefIdx) { 1520 switch (Token.kind()) { 1521 case MIToken::kw_implicit: 1522 case MIToken::kw_implicit_define: 1523 case MIToken::kw_def: 1524 case MIToken::kw_dead: 1525 case MIToken::kw_killed: 1526 case MIToken::kw_undef: 1527 case MIToken::kw_internal: 1528 case MIToken::kw_early_clobber: 1529 case MIToken::kw_debug_use: 1530 case MIToken::underscore: 1531 case MIToken::NamedRegister: 1532 case MIToken::VirtualRegister: 1533 return parseRegisterOperand(Dest, TiedDefIdx); 1534 case MIToken::IntegerLiteral: 1535 return parseImmediateOperand(Dest); 1536 case MIToken::IntegerType: 1537 return parseTypedImmediateOperand(Dest); 1538 case MIToken::kw_half: 1539 case MIToken::kw_float: 1540 case MIToken::kw_double: 1541 case MIToken::kw_x86_fp80: 1542 case MIToken::kw_fp128: 1543 case MIToken::kw_ppc_fp128: 1544 return parseFPImmediateOperand(Dest); 1545 case MIToken::MachineBasicBlock: 1546 return parseMBBOperand(Dest); 1547 case MIToken::StackObject: 1548 return parseStackObjectOperand(Dest); 1549 case MIToken::FixedStackObject: 1550 return parseFixedStackObjectOperand(Dest); 1551 case MIToken::GlobalValue: 1552 case MIToken::NamedGlobalValue: 1553 return parseGlobalAddressOperand(Dest); 1554 case MIToken::ConstantPoolItem: 1555 return parseConstantPoolIndexOperand(Dest); 1556 case MIToken::JumpTableIndex: 1557 return parseJumpTableIndexOperand(Dest); 1558 case MIToken::ExternalSymbol: 1559 return parseExternalSymbolOperand(Dest); 1560 case MIToken::SubRegisterIndex: 1561 return parseSubRegisterIndexOperand(Dest); 1562 case MIToken::exclaim: 1563 return parseMetadataOperand(Dest); 1564 case MIToken::kw_cfi_same_value: 1565 case MIToken::kw_cfi_offset: 1566 case MIToken::kw_cfi_def_cfa_register: 1567 case MIToken::kw_cfi_def_cfa_offset: 1568 case MIToken::kw_cfi_def_cfa: 1569 return parseCFIOperand(Dest); 1570 case MIToken::kw_blockaddress: 1571 return parseBlockAddressOperand(Dest); 1572 case MIToken::kw_intrinsic: 1573 return parseIntrinsicOperand(Dest); 1574 case MIToken::kw_target_index: 1575 return parseTargetIndexOperand(Dest); 1576 case MIToken::kw_liveout: 1577 return parseLiveoutRegisterMaskOperand(Dest); 1578 case MIToken::Error: 1579 return true; 1580 case MIToken::Identifier: 1581 if (const auto *RegMask = getRegMask(Token.stringValue())) { 1582 Dest = MachineOperand::CreateRegMask(RegMask); 1583 lex(); 1584 break; 1585 } 1586 LLVM_FALLTHROUGH; 1587 default: 1588 // FIXME: Parse the MCSymbol machine operand. 1589 return error("expected a machine operand"); 1590 } 1591 return false; 1592 } 1593 1594 bool MIParser::parseMachineOperandAndTargetFlags( 1595 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) { 1596 unsigned TF = 0; 1597 bool HasTargetFlags = false; 1598 if (Token.is(MIToken::kw_target_flags)) { 1599 HasTargetFlags = true; 1600 lex(); 1601 if (expectAndConsume(MIToken::lparen)) 1602 return true; 1603 if (Token.isNot(MIToken::Identifier)) 1604 return error("expected the name of the target flag"); 1605 if (getDirectTargetFlag(Token.stringValue(), TF)) { 1606 if (getBitmaskTargetFlag(Token.stringValue(), TF)) 1607 return error("use of undefined target flag '" + Token.stringValue() + 1608 "'"); 1609 } 1610 lex(); 1611 while (Token.is(MIToken::comma)) { 1612 lex(); 1613 if (Token.isNot(MIToken::Identifier)) 1614 return error("expected the name of the target flag"); 1615 unsigned BitFlag = 0; 1616 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag)) 1617 return error("use of undefined target flag '" + Token.stringValue() + 1618 "'"); 1619 // TODO: Report an error when using a duplicate bit target flag. 1620 TF |= BitFlag; 1621 lex(); 1622 } 1623 if (expectAndConsume(MIToken::rparen)) 1624 return true; 1625 } 1626 auto Loc = Token.location(); 1627 if (parseMachineOperand(Dest, TiedDefIdx)) 1628 return true; 1629 if (!HasTargetFlags) 1630 return false; 1631 if (Dest.isReg()) 1632 return error(Loc, "register operands can't have target flags"); 1633 Dest.setTargetFlags(TF); 1634 return false; 1635 } 1636 1637 bool MIParser::parseOffset(int64_t &Offset) { 1638 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus)) 1639 return false; 1640 StringRef Sign = Token.range(); 1641 bool IsNegative = Token.is(MIToken::minus); 1642 lex(); 1643 if (Token.isNot(MIToken::IntegerLiteral)) 1644 return error("expected an integer literal after '" + Sign + "'"); 1645 if (Token.integerValue().getMinSignedBits() > 64) 1646 return error("expected 64-bit integer (too large)"); 1647 Offset = Token.integerValue().getExtValue(); 1648 if (IsNegative) 1649 Offset = -Offset; 1650 lex(); 1651 return false; 1652 } 1653 1654 bool MIParser::parseAlignment(unsigned &Alignment) { 1655 assert(Token.is(MIToken::kw_align)); 1656 lex(); 1657 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) 1658 return error("expected an integer literal after 'align'"); 1659 if (getUnsigned(Alignment)) 1660 return true; 1661 lex(); 1662 return false; 1663 } 1664 1665 bool MIParser::parseOperandsOffset(MachineOperand &Op) { 1666 int64_t Offset = 0; 1667 if (parseOffset(Offset)) 1668 return true; 1669 Op.setOffset(Offset); 1670 return false; 1671 } 1672 1673 bool MIParser::parseIRValue(const Value *&V) { 1674 switch (Token.kind()) { 1675 case MIToken::NamedIRValue: { 1676 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue()); 1677 break; 1678 } 1679 case MIToken::IRValue: { 1680 unsigned SlotNumber = 0; 1681 if (getUnsigned(SlotNumber)) 1682 return true; 1683 V = getIRValue(SlotNumber); 1684 break; 1685 } 1686 case MIToken::NamedGlobalValue: 1687 case MIToken::GlobalValue: { 1688 GlobalValue *GV = nullptr; 1689 if (parseGlobalValue(GV)) 1690 return true; 1691 V = GV; 1692 break; 1693 } 1694 case MIToken::QuotedIRValue: { 1695 const Constant *C = nullptr; 1696 if (parseIRConstant(Token.location(), Token.stringValue(), C)) 1697 return true; 1698 V = C; 1699 break; 1700 } 1701 default: 1702 llvm_unreachable("The current token should be an IR block reference"); 1703 } 1704 if (!V) 1705 return error(Twine("use of undefined IR value '") + Token.range() + "'"); 1706 return false; 1707 } 1708 1709 bool MIParser::getUint64(uint64_t &Result) { 1710 assert(Token.hasIntegerValue()); 1711 if (Token.integerValue().getActiveBits() > 64) 1712 return error("expected 64-bit integer (too large)"); 1713 Result = Token.integerValue().getZExtValue(); 1714 return false; 1715 } 1716 1717 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) { 1718 const auto OldFlags = Flags; 1719 switch (Token.kind()) { 1720 case MIToken::kw_volatile: 1721 Flags |= MachineMemOperand::MOVolatile; 1722 break; 1723 case MIToken::kw_non_temporal: 1724 Flags |= MachineMemOperand::MONonTemporal; 1725 break; 1726 case MIToken::kw_invariant: 1727 Flags |= MachineMemOperand::MOInvariant; 1728 break; 1729 // TODO: parse the target specific memory operand flags. 1730 default: 1731 llvm_unreachable("The current token should be a memory operand flag"); 1732 } 1733 if (OldFlags == Flags) 1734 // We know that the same flag is specified more than once when the flags 1735 // weren't modified. 1736 return error("duplicate '" + Token.stringValue() + "' memory operand flag"); 1737 lex(); 1738 return false; 1739 } 1740 1741 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) { 1742 switch (Token.kind()) { 1743 case MIToken::kw_stack: 1744 PSV = MF.getPSVManager().getStack(); 1745 break; 1746 case MIToken::kw_got: 1747 PSV = MF.getPSVManager().getGOT(); 1748 break; 1749 case MIToken::kw_jump_table: 1750 PSV = MF.getPSVManager().getJumpTable(); 1751 break; 1752 case MIToken::kw_constant_pool: 1753 PSV = MF.getPSVManager().getConstantPool(); 1754 break; 1755 case MIToken::FixedStackObject: { 1756 int FI; 1757 if (parseFixedStackFrameIndex(FI)) 1758 return true; 1759 PSV = MF.getPSVManager().getFixedStack(FI); 1760 // The token was already consumed, so use return here instead of break. 1761 return false; 1762 } 1763 case MIToken::StackObject: { 1764 int FI; 1765 if (parseStackFrameIndex(FI)) 1766 return true; 1767 PSV = MF.getPSVManager().getFixedStack(FI); 1768 // The token was already consumed, so use return here instead of break. 1769 return false; 1770 } 1771 case MIToken::kw_call_entry: { 1772 lex(); 1773 switch (Token.kind()) { 1774 case MIToken::GlobalValue: 1775 case MIToken::NamedGlobalValue: { 1776 GlobalValue *GV = nullptr; 1777 if (parseGlobalValue(GV)) 1778 return true; 1779 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV); 1780 break; 1781 } 1782 case MIToken::ExternalSymbol: 1783 PSV = MF.getPSVManager().getExternalSymbolCallEntry( 1784 MF.createExternalSymbolName(Token.stringValue())); 1785 break; 1786 default: 1787 return error( 1788 "expected a global value or an external symbol after 'call-entry'"); 1789 } 1790 break; 1791 } 1792 default: 1793 llvm_unreachable("The current token should be pseudo source value"); 1794 } 1795 lex(); 1796 return false; 1797 } 1798 1799 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) { 1800 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) || 1801 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) || 1802 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) || 1803 Token.is(MIToken::kw_call_entry)) { 1804 const PseudoSourceValue *PSV = nullptr; 1805 if (parseMemoryPseudoSourceValue(PSV)) 1806 return true; 1807 int64_t Offset = 0; 1808 if (parseOffset(Offset)) 1809 return true; 1810 Dest = MachinePointerInfo(PSV, Offset); 1811 return false; 1812 } 1813 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) && 1814 Token.isNot(MIToken::GlobalValue) && 1815 Token.isNot(MIToken::NamedGlobalValue) && 1816 Token.isNot(MIToken::QuotedIRValue)) 1817 return error("expected an IR value reference"); 1818 const Value *V = nullptr; 1819 if (parseIRValue(V)) 1820 return true; 1821 if (!V->getType()->isPointerTy()) 1822 return error("expected a pointer IR value"); 1823 lex(); 1824 int64_t Offset = 0; 1825 if (parseOffset(Offset)) 1826 return true; 1827 Dest = MachinePointerInfo(V, Offset); 1828 return false; 1829 } 1830 1831 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) { 1832 if (expectAndConsume(MIToken::lparen)) 1833 return true; 1834 MachineMemOperand::Flags Flags = MachineMemOperand::MONone; 1835 while (Token.isMemoryOperandFlag()) { 1836 if (parseMemoryOperandFlag(Flags)) 1837 return true; 1838 } 1839 if (Token.isNot(MIToken::Identifier) || 1840 (Token.stringValue() != "load" && Token.stringValue() != "store")) 1841 return error("expected 'load' or 'store' memory operation"); 1842 if (Token.stringValue() == "load") 1843 Flags |= MachineMemOperand::MOLoad; 1844 else 1845 Flags |= MachineMemOperand::MOStore; 1846 lex(); 1847 1848 if (Token.isNot(MIToken::IntegerLiteral)) 1849 return error("expected the size integer literal after memory operation"); 1850 uint64_t Size; 1851 if (getUint64(Size)) 1852 return true; 1853 lex(); 1854 1855 MachinePointerInfo Ptr = MachinePointerInfo(); 1856 if (Token.is(MIToken::Identifier)) { 1857 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into"; 1858 if (Token.stringValue() != Word) 1859 return error(Twine("expected '") + Word + "'"); 1860 lex(); 1861 1862 if (parseMachinePointerInfo(Ptr)) 1863 return true; 1864 } 1865 unsigned BaseAlignment = Size; 1866 AAMDNodes AAInfo; 1867 MDNode *Range = nullptr; 1868 while (consumeIfPresent(MIToken::comma)) { 1869 switch (Token.kind()) { 1870 case MIToken::kw_align: 1871 if (parseAlignment(BaseAlignment)) 1872 return true; 1873 break; 1874 case MIToken::md_tbaa: 1875 lex(); 1876 if (parseMDNode(AAInfo.TBAA)) 1877 return true; 1878 break; 1879 case MIToken::md_alias_scope: 1880 lex(); 1881 if (parseMDNode(AAInfo.Scope)) 1882 return true; 1883 break; 1884 case MIToken::md_noalias: 1885 lex(); 1886 if (parseMDNode(AAInfo.NoAlias)) 1887 return true; 1888 break; 1889 case MIToken::md_range: 1890 lex(); 1891 if (parseMDNode(Range)) 1892 return true; 1893 break; 1894 // TODO: Report an error on duplicate metadata nodes. 1895 default: 1896 return error("expected 'align' or '!tbaa' or '!alias.scope' or " 1897 "'!noalias' or '!range'"); 1898 } 1899 } 1900 if (expectAndConsume(MIToken::rparen)) 1901 return true; 1902 Dest = 1903 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range); 1904 return false; 1905 } 1906 1907 void MIParser::initNames2InstrOpCodes() { 1908 if (!Names2InstrOpCodes.empty()) 1909 return; 1910 const auto *TII = MF.getSubtarget().getInstrInfo(); 1911 assert(TII && "Expected target instruction info"); 1912 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) 1913 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I)); 1914 } 1915 1916 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) { 1917 initNames2InstrOpCodes(); 1918 auto InstrInfo = Names2InstrOpCodes.find(InstrName); 1919 if (InstrInfo == Names2InstrOpCodes.end()) 1920 return true; 1921 OpCode = InstrInfo->getValue(); 1922 return false; 1923 } 1924 1925 void MIParser::initNames2Regs() { 1926 if (!Names2Regs.empty()) 1927 return; 1928 // The '%noreg' register is the register 0. 1929 Names2Regs.insert(std::make_pair("noreg", 0)); 1930 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1931 assert(TRI && "Expected target register info"); 1932 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) { 1933 bool WasInserted = 1934 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I)) 1935 .second; 1936 (void)WasInserted; 1937 assert(WasInserted && "Expected registers to be unique case-insensitively"); 1938 } 1939 } 1940 1941 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) { 1942 initNames2Regs(); 1943 auto RegInfo = Names2Regs.find(RegName); 1944 if (RegInfo == Names2Regs.end()) 1945 return true; 1946 Reg = RegInfo->getValue(); 1947 return false; 1948 } 1949 1950 void MIParser::initNames2RegMasks() { 1951 if (!Names2RegMasks.empty()) 1952 return; 1953 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 1954 assert(TRI && "Expected target register info"); 1955 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks(); 1956 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames(); 1957 assert(RegMasks.size() == RegMaskNames.size()); 1958 for (size_t I = 0, E = RegMasks.size(); I < E; ++I) 1959 Names2RegMasks.insert( 1960 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I])); 1961 } 1962 1963 const uint32_t *MIParser::getRegMask(StringRef Identifier) { 1964 initNames2RegMasks(); 1965 auto RegMaskInfo = Names2RegMasks.find(Identifier); 1966 if (RegMaskInfo == Names2RegMasks.end()) 1967 return nullptr; 1968 return RegMaskInfo->getValue(); 1969 } 1970 1971 void MIParser::initNames2SubRegIndices() { 1972 if (!Names2SubRegIndices.empty()) 1973 return; 1974 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1975 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I) 1976 Names2SubRegIndices.insert( 1977 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I)); 1978 } 1979 1980 unsigned MIParser::getSubRegIndex(StringRef Name) { 1981 initNames2SubRegIndices(); 1982 auto SubRegInfo = Names2SubRegIndices.find(Name); 1983 if (SubRegInfo == Names2SubRegIndices.end()) 1984 return 0; 1985 return SubRegInfo->getValue(); 1986 } 1987 1988 static void initSlots2BasicBlocks( 1989 const Function &F, 1990 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { 1991 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); 1992 MST.incorporateFunction(F); 1993 for (auto &BB : F) { 1994 if (BB.hasName()) 1995 continue; 1996 int Slot = MST.getLocalSlot(&BB); 1997 if (Slot == -1) 1998 continue; 1999 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB)); 2000 } 2001 } 2002 2003 static const BasicBlock *getIRBlockFromSlot( 2004 unsigned Slot, 2005 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { 2006 auto BlockInfo = Slots2BasicBlocks.find(Slot); 2007 if (BlockInfo == Slots2BasicBlocks.end()) 2008 return nullptr; 2009 return BlockInfo->second; 2010 } 2011 2012 const BasicBlock *MIParser::getIRBlock(unsigned Slot) { 2013 if (Slots2BasicBlocks.empty()) 2014 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks); 2015 return getIRBlockFromSlot(Slot, Slots2BasicBlocks); 2016 } 2017 2018 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) { 2019 if (&F == MF.getFunction()) 2020 return getIRBlock(Slot); 2021 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks; 2022 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks); 2023 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks); 2024 } 2025 2026 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, 2027 DenseMap<unsigned, const Value *> &Slots2Values) { 2028 int Slot = MST.getLocalSlot(V); 2029 if (Slot == -1) 2030 return; 2031 Slots2Values.insert(std::make_pair(unsigned(Slot), V)); 2032 } 2033 2034 /// Creates the mapping from slot numbers to function's unnamed IR values. 2035 static void initSlots2Values(const Function &F, 2036 DenseMap<unsigned, const Value *> &Slots2Values) { 2037 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); 2038 MST.incorporateFunction(F); 2039 for (const auto &Arg : F.args()) 2040 mapValueToSlot(&Arg, MST, Slots2Values); 2041 for (const auto &BB : F) { 2042 mapValueToSlot(&BB, MST, Slots2Values); 2043 for (const auto &I : BB) 2044 mapValueToSlot(&I, MST, Slots2Values); 2045 } 2046 } 2047 2048 const Value *MIParser::getIRValue(unsigned Slot) { 2049 if (Slots2Values.empty()) 2050 initSlots2Values(*MF.getFunction(), Slots2Values); 2051 auto ValueInfo = Slots2Values.find(Slot); 2052 if (ValueInfo == Slots2Values.end()) 2053 return nullptr; 2054 return ValueInfo->second; 2055 } 2056 2057 void MIParser::initNames2TargetIndices() { 2058 if (!Names2TargetIndices.empty()) 2059 return; 2060 const auto *TII = MF.getSubtarget().getInstrInfo(); 2061 assert(TII && "Expected target instruction info"); 2062 auto Indices = TII->getSerializableTargetIndices(); 2063 for (const auto &I : Indices) 2064 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first)); 2065 } 2066 2067 bool MIParser::getTargetIndex(StringRef Name, int &Index) { 2068 initNames2TargetIndices(); 2069 auto IndexInfo = Names2TargetIndices.find(Name); 2070 if (IndexInfo == Names2TargetIndices.end()) 2071 return true; 2072 Index = IndexInfo->second; 2073 return false; 2074 } 2075 2076 void MIParser::initNames2DirectTargetFlags() { 2077 if (!Names2DirectTargetFlags.empty()) 2078 return; 2079 const auto *TII = MF.getSubtarget().getInstrInfo(); 2080 assert(TII && "Expected target instruction info"); 2081 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); 2082 for (const auto &I : Flags) 2083 Names2DirectTargetFlags.insert( 2084 std::make_pair(StringRef(I.second), I.first)); 2085 } 2086 2087 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) { 2088 initNames2DirectTargetFlags(); 2089 auto FlagInfo = Names2DirectTargetFlags.find(Name); 2090 if (FlagInfo == Names2DirectTargetFlags.end()) 2091 return true; 2092 Flag = FlagInfo->second; 2093 return false; 2094 } 2095 2096 void MIParser::initNames2BitmaskTargetFlags() { 2097 if (!Names2BitmaskTargetFlags.empty()) 2098 return; 2099 const auto *TII = MF.getSubtarget().getInstrInfo(); 2100 assert(TII && "Expected target instruction info"); 2101 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags(); 2102 for (const auto &I : Flags) 2103 Names2BitmaskTargetFlags.insert( 2104 std::make_pair(StringRef(I.second), I.first)); 2105 } 2106 2107 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) { 2108 initNames2BitmaskTargetFlags(); 2109 auto FlagInfo = Names2BitmaskTargetFlags.find(Name); 2110 if (FlagInfo == Names2BitmaskTargetFlags.end()) 2111 return true; 2112 Flag = FlagInfo->second; 2113 return false; 2114 } 2115 2116 bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, 2117 StringRef Src, 2118 SMDiagnostic &Error) { 2119 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots); 2120 } 2121 2122 bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS, 2123 StringRef Src, SMDiagnostic &Error) { 2124 return MIParser(PFS, Error, Src).parseBasicBlocks(); 2125 } 2126 2127 bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS, 2128 MachineBasicBlock *&MBB, StringRef Src, 2129 SMDiagnostic &Error) { 2130 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB); 2131 } 2132 2133 bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS, 2134 unsigned &Reg, StringRef Src, 2135 SMDiagnostic &Error) { 2136 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg); 2137 } 2138 2139 bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS, 2140 unsigned &Reg, StringRef Src, 2141 SMDiagnostic &Error) { 2142 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg); 2143 } 2144 2145 bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS, 2146 int &FI, StringRef Src, 2147 SMDiagnostic &Error) { 2148 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI); 2149 } 2150 2151 bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS, 2152 MDNode *&Node, StringRef Src, SMDiagnostic &Error) { 2153 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node); 2154 } 2155