1 //===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===// 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 #include "MCTargetDesc/X86BaseInfo.h" 10 #include "MCTargetDesc/X86IntelInstPrinter.h" 11 #include "MCTargetDesc/X86MCExpr.h" 12 #include "MCTargetDesc/X86TargetStreamer.h" 13 #include "TargetInfo/X86TargetInfo.h" 14 #include "X86AsmParserCommon.h" 15 #include "X86Operand.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/StringSwitch.h" 20 #include "llvm/ADT/Twine.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCInst.h" 24 #include "llvm/MC/MCInstrInfo.h" 25 #include "llvm/MC/MCParser/MCAsmLexer.h" 26 #include "llvm/MC/MCParser/MCAsmParser.h" 27 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 28 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 29 #include "llvm/MC/MCRegisterInfo.h" 30 #include "llvm/MC/MCSection.h" 31 #include "llvm/MC/MCStreamer.h" 32 #include "llvm/MC/MCSubtargetInfo.h" 33 #include "llvm/MC/MCSymbol.h" 34 #include "llvm/Support/SourceMgr.h" 35 #include "llvm/Support/TargetRegistry.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include <algorithm> 38 #include <memory> 39 40 using namespace llvm; 41 42 static bool checkScale(unsigned Scale, StringRef &ErrMsg) { 43 if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) { 44 ErrMsg = "scale factor in address must be 1, 2, 4 or 8"; 45 return true; 46 } 47 return false; 48 } 49 50 namespace { 51 52 static const char OpPrecedence[] = { 53 0, // IC_OR 54 1, // IC_XOR 55 2, // IC_AND 56 3, // IC_LSHIFT 57 3, // IC_RSHIFT 58 4, // IC_PLUS 59 4, // IC_MINUS 60 5, // IC_MULTIPLY 61 5, // IC_DIVIDE 62 5, // IC_MOD 63 6, // IC_NOT 64 7, // IC_NEG 65 8, // IC_RPAREN 66 9, // IC_LPAREN 67 0, // IC_IMM 68 0 // IC_REGISTER 69 }; 70 71 class X86AsmParser : public MCTargetAsmParser { 72 ParseInstructionInfo *InstInfo; 73 bool Code16GCC; 74 75 enum VEXEncoding { 76 VEXEncoding_Default, 77 VEXEncoding_VEX2, 78 VEXEncoding_VEX3, 79 VEXEncoding_EVEX, 80 }; 81 82 VEXEncoding ForcedVEXEncoding = VEXEncoding_Default; 83 84 private: 85 SMLoc consumeToken() { 86 MCAsmParser &Parser = getParser(); 87 SMLoc Result = Parser.getTok().getLoc(); 88 Parser.Lex(); 89 return Result; 90 } 91 92 X86TargetStreamer &getTargetStreamer() { 93 assert(getParser().getStreamer().getTargetStreamer() && 94 "do not have a target streamer"); 95 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer(); 96 return static_cast<X86TargetStreamer &>(TS); 97 } 98 99 unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst, 100 uint64_t &ErrorInfo, FeatureBitset &MissingFeatures, 101 bool matchingInlineAsm, unsigned VariantID = 0) { 102 // In Code16GCC mode, match as 32-bit. 103 if (Code16GCC) 104 SwitchMode(X86::Mode32Bit); 105 unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo, 106 MissingFeatures, matchingInlineAsm, 107 VariantID); 108 if (Code16GCC) 109 SwitchMode(X86::Mode16Bit); 110 return rv; 111 } 112 113 enum InfixCalculatorTok { 114 IC_OR = 0, 115 IC_XOR, 116 IC_AND, 117 IC_LSHIFT, 118 IC_RSHIFT, 119 IC_PLUS, 120 IC_MINUS, 121 IC_MULTIPLY, 122 IC_DIVIDE, 123 IC_MOD, 124 IC_NOT, 125 IC_NEG, 126 IC_RPAREN, 127 IC_LPAREN, 128 IC_IMM, 129 IC_REGISTER 130 }; 131 132 enum IntelOperatorKind { 133 IOK_INVALID = 0, 134 IOK_LENGTH, 135 IOK_SIZE, 136 IOK_TYPE, 137 }; 138 139 class InfixCalculator { 140 typedef std::pair< InfixCalculatorTok, int64_t > ICToken; 141 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack; 142 SmallVector<ICToken, 4> PostfixStack; 143 144 bool isUnaryOperator(const InfixCalculatorTok Op) { 145 return Op == IC_NEG || Op == IC_NOT; 146 } 147 148 public: 149 int64_t popOperand() { 150 assert (!PostfixStack.empty() && "Poped an empty stack!"); 151 ICToken Op = PostfixStack.pop_back_val(); 152 if (!(Op.first == IC_IMM || Op.first == IC_REGISTER)) 153 return -1; // The invalid Scale value will be caught later by checkScale 154 return Op.second; 155 } 156 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) { 157 assert ((Op == IC_IMM || Op == IC_REGISTER) && 158 "Unexpected operand!"); 159 PostfixStack.push_back(std::make_pair(Op, Val)); 160 } 161 162 void popOperator() { InfixOperatorStack.pop_back(); } 163 void pushOperator(InfixCalculatorTok Op) { 164 // Push the new operator if the stack is empty. 165 if (InfixOperatorStack.empty()) { 166 InfixOperatorStack.push_back(Op); 167 return; 168 } 169 170 // Push the new operator if it has a higher precedence than the operator 171 // on the top of the stack or the operator on the top of the stack is a 172 // left parentheses. 173 unsigned Idx = InfixOperatorStack.size() - 1; 174 InfixCalculatorTok StackOp = InfixOperatorStack[Idx]; 175 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) { 176 InfixOperatorStack.push_back(Op); 177 return; 178 } 179 180 // The operator on the top of the stack has higher precedence than the 181 // new operator. 182 unsigned ParenCount = 0; 183 while (1) { 184 // Nothing to process. 185 if (InfixOperatorStack.empty()) 186 break; 187 188 Idx = InfixOperatorStack.size() - 1; 189 StackOp = InfixOperatorStack[Idx]; 190 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount)) 191 break; 192 193 // If we have an even parentheses count and we see a left parentheses, 194 // then stop processing. 195 if (!ParenCount && StackOp == IC_LPAREN) 196 break; 197 198 if (StackOp == IC_RPAREN) { 199 ++ParenCount; 200 InfixOperatorStack.pop_back(); 201 } else if (StackOp == IC_LPAREN) { 202 --ParenCount; 203 InfixOperatorStack.pop_back(); 204 } else { 205 InfixOperatorStack.pop_back(); 206 PostfixStack.push_back(std::make_pair(StackOp, 0)); 207 } 208 } 209 // Push the new operator. 210 InfixOperatorStack.push_back(Op); 211 } 212 213 int64_t execute() { 214 // Push any remaining operators onto the postfix stack. 215 while (!InfixOperatorStack.empty()) { 216 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val(); 217 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN) 218 PostfixStack.push_back(std::make_pair(StackOp, 0)); 219 } 220 221 if (PostfixStack.empty()) 222 return 0; 223 224 SmallVector<ICToken, 16> OperandStack; 225 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) { 226 ICToken Op = PostfixStack[i]; 227 if (Op.first == IC_IMM || Op.first == IC_REGISTER) { 228 OperandStack.push_back(Op); 229 } else if (isUnaryOperator(Op.first)) { 230 assert (OperandStack.size() > 0 && "Too few operands."); 231 ICToken Operand = OperandStack.pop_back_val(); 232 assert (Operand.first == IC_IMM && 233 "Unary operation with a register!"); 234 switch (Op.first) { 235 default: 236 report_fatal_error("Unexpected operator!"); 237 break; 238 case IC_NEG: 239 OperandStack.push_back(std::make_pair(IC_IMM, -Operand.second)); 240 break; 241 case IC_NOT: 242 OperandStack.push_back(std::make_pair(IC_IMM, ~Operand.second)); 243 break; 244 } 245 } else { 246 assert (OperandStack.size() > 1 && "Too few operands."); 247 int64_t Val; 248 ICToken Op2 = OperandStack.pop_back_val(); 249 ICToken Op1 = OperandStack.pop_back_val(); 250 switch (Op.first) { 251 default: 252 report_fatal_error("Unexpected operator!"); 253 break; 254 case IC_PLUS: 255 Val = Op1.second + Op2.second; 256 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 257 break; 258 case IC_MINUS: 259 Val = Op1.second - Op2.second; 260 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 261 break; 262 case IC_MULTIPLY: 263 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 264 "Multiply operation with an immediate and a register!"); 265 Val = Op1.second * Op2.second; 266 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 267 break; 268 case IC_DIVIDE: 269 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 270 "Divide operation with an immediate and a register!"); 271 assert (Op2.second != 0 && "Division by zero!"); 272 Val = Op1.second / Op2.second; 273 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 274 break; 275 case IC_MOD: 276 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 277 "Modulo operation with an immediate and a register!"); 278 Val = Op1.second % Op2.second; 279 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 280 break; 281 case IC_OR: 282 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 283 "Or operation with an immediate and a register!"); 284 Val = Op1.second | Op2.second; 285 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 286 break; 287 case IC_XOR: 288 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 289 "Xor operation with an immediate and a register!"); 290 Val = Op1.second ^ Op2.second; 291 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 292 break; 293 case IC_AND: 294 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 295 "And operation with an immediate and a register!"); 296 Val = Op1.second & Op2.second; 297 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 298 break; 299 case IC_LSHIFT: 300 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 301 "Left shift operation with an immediate and a register!"); 302 Val = Op1.second << Op2.second; 303 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 304 break; 305 case IC_RSHIFT: 306 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 307 "Right shift operation with an immediate and a register!"); 308 Val = Op1.second >> Op2.second; 309 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 310 break; 311 } 312 } 313 } 314 assert (OperandStack.size() == 1 && "Expected a single result."); 315 return OperandStack.pop_back_val().second; 316 } 317 }; 318 319 enum IntelExprState { 320 IES_INIT, 321 IES_OR, 322 IES_XOR, 323 IES_AND, 324 IES_LSHIFT, 325 IES_RSHIFT, 326 IES_PLUS, 327 IES_MINUS, 328 IES_OFFSET, 329 IES_NOT, 330 IES_MULTIPLY, 331 IES_DIVIDE, 332 IES_MOD, 333 IES_LBRAC, 334 IES_RBRAC, 335 IES_LPAREN, 336 IES_RPAREN, 337 IES_REGISTER, 338 IES_INTEGER, 339 IES_IDENTIFIER, 340 IES_ERROR 341 }; 342 343 class IntelExprStateMachine { 344 IntelExprState State, PrevState; 345 unsigned BaseReg, IndexReg, TmpReg, Scale; 346 int64_t Imm; 347 const MCExpr *Sym; 348 StringRef SymName; 349 InfixCalculator IC; 350 InlineAsmIdentifierInfo Info; 351 short BracCount; 352 bool MemExpr; 353 bool OffsetOperator; 354 SMLoc OffsetOperatorLoc; 355 356 bool setSymRef(const MCExpr *Val, StringRef ID, StringRef &ErrMsg) { 357 if (Sym) { 358 ErrMsg = "cannot use more than one symbol in memory operand"; 359 return true; 360 } 361 Sym = Val; 362 SymName = ID; 363 return false; 364 } 365 366 public: 367 IntelExprStateMachine() 368 : State(IES_INIT), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), 369 TmpReg(0), Scale(0), Imm(0), Sym(nullptr), BracCount(0), 370 MemExpr(false), OffsetOperator(false) {} 371 372 void addImm(int64_t imm) { Imm += imm; } 373 short getBracCount() { return BracCount; } 374 bool isMemExpr() { return MemExpr; } 375 bool isOffsetOperator() { return OffsetOperator; } 376 SMLoc getOffsetLoc() { return OffsetOperatorLoc; } 377 unsigned getBaseReg() { return BaseReg; } 378 unsigned getIndexReg() { return IndexReg; } 379 unsigned getScale() { return Scale; } 380 const MCExpr *getSym() { return Sym; } 381 StringRef getSymName() { return SymName; } 382 int64_t getImm() { return Imm + IC.execute(); } 383 bool isValidEndState() { 384 return State == IES_RBRAC || State == IES_INTEGER; 385 } 386 bool hadError() { return State == IES_ERROR; } 387 InlineAsmIdentifierInfo &getIdentifierInfo() { return Info; } 388 389 void onOr() { 390 IntelExprState CurrState = State; 391 switch (State) { 392 default: 393 State = IES_ERROR; 394 break; 395 case IES_INTEGER: 396 case IES_RPAREN: 397 case IES_REGISTER: 398 State = IES_OR; 399 IC.pushOperator(IC_OR); 400 break; 401 } 402 PrevState = CurrState; 403 } 404 void onXor() { 405 IntelExprState CurrState = State; 406 switch (State) { 407 default: 408 State = IES_ERROR; 409 break; 410 case IES_INTEGER: 411 case IES_RPAREN: 412 case IES_REGISTER: 413 State = IES_XOR; 414 IC.pushOperator(IC_XOR); 415 break; 416 } 417 PrevState = CurrState; 418 } 419 void onAnd() { 420 IntelExprState CurrState = State; 421 switch (State) { 422 default: 423 State = IES_ERROR; 424 break; 425 case IES_INTEGER: 426 case IES_RPAREN: 427 case IES_REGISTER: 428 State = IES_AND; 429 IC.pushOperator(IC_AND); 430 break; 431 } 432 PrevState = CurrState; 433 } 434 void onLShift() { 435 IntelExprState CurrState = State; 436 switch (State) { 437 default: 438 State = IES_ERROR; 439 break; 440 case IES_INTEGER: 441 case IES_RPAREN: 442 case IES_REGISTER: 443 State = IES_LSHIFT; 444 IC.pushOperator(IC_LSHIFT); 445 break; 446 } 447 PrevState = CurrState; 448 } 449 void onRShift() { 450 IntelExprState CurrState = State; 451 switch (State) { 452 default: 453 State = IES_ERROR; 454 break; 455 case IES_INTEGER: 456 case IES_RPAREN: 457 case IES_REGISTER: 458 State = IES_RSHIFT; 459 IC.pushOperator(IC_RSHIFT); 460 break; 461 } 462 PrevState = CurrState; 463 } 464 bool onPlus(StringRef &ErrMsg) { 465 IntelExprState CurrState = State; 466 switch (State) { 467 default: 468 State = IES_ERROR; 469 break; 470 case IES_INTEGER: 471 case IES_RPAREN: 472 case IES_REGISTER: 473 case IES_OFFSET: 474 State = IES_PLUS; 475 IC.pushOperator(IC_PLUS); 476 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 477 // If we already have a BaseReg, then assume this is the IndexReg with 478 // no explicit scale. 479 if (!BaseReg) { 480 BaseReg = TmpReg; 481 } else { 482 if (IndexReg) { 483 ErrMsg = "BaseReg/IndexReg already set!"; 484 return true; 485 } 486 IndexReg = TmpReg; 487 Scale = 0; 488 } 489 } 490 break; 491 } 492 PrevState = CurrState; 493 return false; 494 } 495 bool onMinus(StringRef &ErrMsg) { 496 IntelExprState CurrState = State; 497 switch (State) { 498 default: 499 State = IES_ERROR; 500 break; 501 case IES_OR: 502 case IES_XOR: 503 case IES_AND: 504 case IES_LSHIFT: 505 case IES_RSHIFT: 506 case IES_PLUS: 507 case IES_NOT: 508 case IES_MULTIPLY: 509 case IES_DIVIDE: 510 case IES_MOD: 511 case IES_LPAREN: 512 case IES_RPAREN: 513 case IES_LBRAC: 514 case IES_RBRAC: 515 case IES_INTEGER: 516 case IES_REGISTER: 517 case IES_INIT: 518 case IES_OFFSET: 519 State = IES_MINUS; 520 // push minus operator if it is not a negate operator 521 if (CurrState == IES_REGISTER || CurrState == IES_RPAREN || 522 CurrState == IES_INTEGER || CurrState == IES_RBRAC || 523 CurrState == IES_OFFSET) 524 IC.pushOperator(IC_MINUS); 525 else if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) { 526 // We have negate operator for Scale: it's illegal 527 ErrMsg = "Scale can't be negative"; 528 return true; 529 } else 530 IC.pushOperator(IC_NEG); 531 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 532 // If we already have a BaseReg, then assume this is the IndexReg with 533 // no explicit scale. 534 if (!BaseReg) { 535 BaseReg = TmpReg; 536 } else { 537 if (IndexReg) { 538 ErrMsg = "BaseReg/IndexReg already set!"; 539 return true; 540 } 541 IndexReg = TmpReg; 542 Scale = 0; 543 } 544 } 545 break; 546 } 547 PrevState = CurrState; 548 return false; 549 } 550 void onNot() { 551 IntelExprState CurrState = State; 552 switch (State) { 553 default: 554 State = IES_ERROR; 555 break; 556 case IES_OR: 557 case IES_XOR: 558 case IES_AND: 559 case IES_LSHIFT: 560 case IES_RSHIFT: 561 case IES_PLUS: 562 case IES_MINUS: 563 case IES_NOT: 564 case IES_MULTIPLY: 565 case IES_DIVIDE: 566 case IES_MOD: 567 case IES_LPAREN: 568 case IES_LBRAC: 569 case IES_INIT: 570 State = IES_NOT; 571 IC.pushOperator(IC_NOT); 572 break; 573 } 574 PrevState = CurrState; 575 } 576 bool onRegister(unsigned Reg, StringRef &ErrMsg) { 577 IntelExprState CurrState = State; 578 switch (State) { 579 default: 580 State = IES_ERROR; 581 break; 582 case IES_PLUS: 583 case IES_LPAREN: 584 case IES_LBRAC: 585 State = IES_REGISTER; 586 TmpReg = Reg; 587 IC.pushOperand(IC_REGISTER); 588 break; 589 case IES_MULTIPLY: 590 // Index Register - Scale * Register 591 if (PrevState == IES_INTEGER) { 592 if (IndexReg) { 593 ErrMsg = "BaseReg/IndexReg already set!"; 594 return true; 595 } 596 State = IES_REGISTER; 597 IndexReg = Reg; 598 // Get the scale and replace the 'Scale * Register' with '0'. 599 Scale = IC.popOperand(); 600 if (checkScale(Scale, ErrMsg)) 601 return true; 602 IC.pushOperand(IC_IMM); 603 IC.popOperator(); 604 } else { 605 State = IES_ERROR; 606 } 607 break; 608 } 609 PrevState = CurrState; 610 return false; 611 } 612 bool onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName, 613 const InlineAsmIdentifierInfo &IDInfo, 614 bool ParsingInlineAsm, StringRef &ErrMsg) { 615 // InlineAsm: Treat an enum value as an integer 616 if (ParsingInlineAsm) 617 if (IDInfo.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) 618 return onInteger(IDInfo.Enum.EnumVal, ErrMsg); 619 // Treat a symbolic constant like an integer 620 if (auto *CE = dyn_cast<MCConstantExpr>(SymRef)) 621 return onInteger(CE->getValue(), ErrMsg); 622 PrevState = State; 623 switch (State) { 624 default: 625 State = IES_ERROR; 626 break; 627 case IES_PLUS: 628 case IES_MINUS: 629 case IES_NOT: 630 case IES_INIT: 631 case IES_LBRAC: 632 if (setSymRef(SymRef, SymRefName, ErrMsg)) 633 return true; 634 MemExpr = true; 635 State = IES_INTEGER; 636 IC.pushOperand(IC_IMM); 637 if (ParsingInlineAsm) 638 Info = IDInfo; 639 break; 640 } 641 return false; 642 } 643 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) { 644 IntelExprState CurrState = State; 645 switch (State) { 646 default: 647 State = IES_ERROR; 648 break; 649 case IES_PLUS: 650 case IES_MINUS: 651 case IES_NOT: 652 case IES_OR: 653 case IES_XOR: 654 case IES_AND: 655 case IES_LSHIFT: 656 case IES_RSHIFT: 657 case IES_DIVIDE: 658 case IES_MOD: 659 case IES_MULTIPLY: 660 case IES_LPAREN: 661 case IES_INIT: 662 case IES_LBRAC: 663 State = IES_INTEGER; 664 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) { 665 // Index Register - Register * Scale 666 if (IndexReg) { 667 ErrMsg = "BaseReg/IndexReg already set!"; 668 return true; 669 } 670 IndexReg = TmpReg; 671 Scale = TmpInt; 672 if (checkScale(Scale, ErrMsg)) 673 return true; 674 // Get the scale and replace the 'Register * Scale' with '0'. 675 IC.popOperator(); 676 } else { 677 IC.pushOperand(IC_IMM, TmpInt); 678 } 679 break; 680 } 681 PrevState = CurrState; 682 return false; 683 } 684 void onStar() { 685 PrevState = State; 686 switch (State) { 687 default: 688 State = IES_ERROR; 689 break; 690 case IES_INTEGER: 691 case IES_REGISTER: 692 case IES_RPAREN: 693 State = IES_MULTIPLY; 694 IC.pushOperator(IC_MULTIPLY); 695 break; 696 } 697 } 698 void onDivide() { 699 PrevState = State; 700 switch (State) { 701 default: 702 State = IES_ERROR; 703 break; 704 case IES_INTEGER: 705 case IES_RPAREN: 706 State = IES_DIVIDE; 707 IC.pushOperator(IC_DIVIDE); 708 break; 709 } 710 } 711 void onMod() { 712 PrevState = State; 713 switch (State) { 714 default: 715 State = IES_ERROR; 716 break; 717 case IES_INTEGER: 718 case IES_RPAREN: 719 State = IES_MOD; 720 IC.pushOperator(IC_MOD); 721 break; 722 } 723 } 724 bool onLBrac() { 725 if (BracCount) 726 return true; 727 PrevState = State; 728 switch (State) { 729 default: 730 State = IES_ERROR; 731 break; 732 case IES_RBRAC: 733 case IES_INTEGER: 734 case IES_RPAREN: 735 State = IES_PLUS; 736 IC.pushOperator(IC_PLUS); 737 break; 738 case IES_INIT: 739 assert(!BracCount && "BracCount should be zero on parsing's start"); 740 State = IES_LBRAC; 741 break; 742 } 743 MemExpr = true; 744 BracCount++; 745 return false; 746 } 747 bool onRBrac() { 748 IntelExprState CurrState = State; 749 switch (State) { 750 default: 751 State = IES_ERROR; 752 break; 753 case IES_INTEGER: 754 case IES_OFFSET: 755 case IES_REGISTER: 756 case IES_RPAREN: 757 if (BracCount-- != 1) 758 return true; 759 State = IES_RBRAC; 760 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 761 // If we already have a BaseReg, then assume this is the IndexReg with 762 // no explicit scale. 763 if (!BaseReg) { 764 BaseReg = TmpReg; 765 } else { 766 assert (!IndexReg && "BaseReg/IndexReg already set!"); 767 IndexReg = TmpReg; 768 Scale = 0; 769 } 770 } 771 break; 772 } 773 PrevState = CurrState; 774 return false; 775 } 776 void onLParen() { 777 IntelExprState CurrState = State; 778 switch (State) { 779 default: 780 State = IES_ERROR; 781 break; 782 case IES_PLUS: 783 case IES_MINUS: 784 case IES_NOT: 785 case IES_OR: 786 case IES_XOR: 787 case IES_AND: 788 case IES_LSHIFT: 789 case IES_RSHIFT: 790 case IES_MULTIPLY: 791 case IES_DIVIDE: 792 case IES_MOD: 793 case IES_LPAREN: 794 case IES_INIT: 795 case IES_LBRAC: 796 State = IES_LPAREN; 797 IC.pushOperator(IC_LPAREN); 798 break; 799 } 800 PrevState = CurrState; 801 } 802 void onRParen() { 803 PrevState = State; 804 switch (State) { 805 default: 806 State = IES_ERROR; 807 break; 808 case IES_INTEGER: 809 case IES_OFFSET: 810 case IES_REGISTER: 811 case IES_RPAREN: 812 State = IES_RPAREN; 813 IC.pushOperator(IC_RPAREN); 814 break; 815 } 816 } 817 bool onOffset(const MCExpr *Val, SMLoc OffsetLoc, StringRef ID, 818 const InlineAsmIdentifierInfo &IDInfo, bool ParsingInlineAsm, 819 StringRef &ErrMsg) { 820 PrevState = State; 821 switch (State) { 822 default: 823 ErrMsg = "unexpected offset operator expression"; 824 return true; 825 case IES_PLUS: 826 case IES_INIT: 827 case IES_LBRAC: 828 if (setSymRef(Val, ID, ErrMsg)) 829 return true; 830 OffsetOperator = true; 831 OffsetOperatorLoc = OffsetLoc; 832 State = IES_OFFSET; 833 // As we cannot yet resolve the actual value (offset), we retain 834 // the requested semantics by pushing a '0' to the operands stack 835 IC.pushOperand(IC_IMM); 836 if (ParsingInlineAsm) { 837 Info = IDInfo; 838 } 839 break; 840 } 841 return false; 842 } 843 }; 844 845 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None, 846 bool MatchingInlineAsm = false) { 847 MCAsmParser &Parser = getParser(); 848 if (MatchingInlineAsm) { 849 if (!getLexer().isAtStartOfStatement()) 850 Parser.eatToEndOfStatement(); 851 return false; 852 } 853 return Parser.Error(L, Msg, Range); 854 } 855 856 std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg, SMRange R = SMRange()) { 857 Error(Loc, Msg, R); 858 return nullptr; 859 } 860 861 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc); 862 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc); 863 bool IsSIReg(unsigned Reg); 864 unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg); 865 void 866 AddDefaultSrcDestOperands(OperandVector &Operands, 867 std::unique_ptr<llvm::MCParsedAsmOperand> &&Src, 868 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst); 869 bool VerifyAndAdjustOperands(OperandVector &OrigOperands, 870 OperandVector &FinalOperands); 871 std::unique_ptr<X86Operand> ParseOperand(); 872 std::unique_ptr<X86Operand> ParseATTOperand(); 873 std::unique_ptr<X86Operand> ParseIntelOperand(); 874 bool ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID, 875 InlineAsmIdentifierInfo &Info, SMLoc &End); 876 bool ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End); 877 unsigned IdentifyIntelInlineAsmOperator(StringRef Name); 878 unsigned ParseIntelInlineAsmOperator(unsigned OpKind); 879 std::unique_ptr<X86Operand> ParseRoundingModeOp(SMLoc Start); 880 bool ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM, 881 bool &ParseError, SMLoc &End); 882 void RewriteIntelExpression(IntelExprStateMachine &SM, SMLoc Start, 883 SMLoc End); 884 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End); 885 bool ParseIntelInlineAsmIdentifier(const MCExpr *&Val, StringRef &Identifier, 886 InlineAsmIdentifierInfo &Info, 887 bool IsUnevaluatedOperand, SMLoc &End, 888 bool IsParsingOffsetOperator = false); 889 890 std::unique_ptr<X86Operand> ParseMemOperand(unsigned SegReg, 891 const MCExpr *&Disp, 892 const SMLoc &StartLoc, 893 SMLoc &EndLoc); 894 895 X86::CondCode ParseConditionCode(StringRef CCode); 896 897 bool ParseIntelMemoryOperandSize(unsigned &Size); 898 std::unique_ptr<X86Operand> 899 CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, 900 unsigned IndexReg, unsigned Scale, SMLoc Start, 901 SMLoc End, unsigned Size, StringRef Identifier, 902 const InlineAsmIdentifierInfo &Info); 903 904 bool parseDirectiveEven(SMLoc L); 905 bool ParseDirectiveCode(StringRef IDVal, SMLoc L); 906 907 /// CodeView FPO data directives. 908 bool parseDirectiveFPOProc(SMLoc L); 909 bool parseDirectiveFPOSetFrame(SMLoc L); 910 bool parseDirectiveFPOPushReg(SMLoc L); 911 bool parseDirectiveFPOStackAlloc(SMLoc L); 912 bool parseDirectiveFPOStackAlign(SMLoc L); 913 bool parseDirectiveFPOEndPrologue(SMLoc L); 914 bool parseDirectiveFPOEndProc(SMLoc L); 915 bool parseDirectiveFPOData(SMLoc L); 916 917 /// SEH directives. 918 bool parseSEHRegisterNumber(unsigned RegClassID, unsigned &RegNo); 919 bool parseDirectiveSEHPushReg(SMLoc); 920 bool parseDirectiveSEHSetFrame(SMLoc); 921 bool parseDirectiveSEHSaveReg(SMLoc); 922 bool parseDirectiveSEHSaveXMM(SMLoc); 923 bool parseDirectiveSEHPushFrame(SMLoc); 924 925 unsigned checkTargetMatchPredicate(MCInst &Inst) override; 926 927 bool validateInstruction(MCInst &Inst, const OperandVector &Ops); 928 bool processInstruction(MCInst &Inst, const OperandVector &Ops); 929 930 /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds 931 /// instrumentation around Inst. 932 void EmitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out); 933 934 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 935 OperandVector &Operands, MCStreamer &Out, 936 uint64_t &ErrorInfo, 937 bool MatchingInlineAsm) override; 938 939 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands, 940 MCStreamer &Out, bool MatchingInlineAsm); 941 942 bool ErrorMissingFeature(SMLoc IDLoc, const FeatureBitset &MissingFeatures, 943 bool MatchingInlineAsm); 944 945 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode, 946 OperandVector &Operands, MCStreamer &Out, 947 uint64_t &ErrorInfo, 948 bool MatchingInlineAsm); 949 950 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode, 951 OperandVector &Operands, MCStreamer &Out, 952 uint64_t &ErrorInfo, 953 bool MatchingInlineAsm); 954 955 bool OmitRegisterFromClobberLists(unsigned RegNo) override; 956 957 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z}) 958 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required. 959 /// return false if no parsing errors occurred, true otherwise. 960 bool HandleAVX512Operand(OperandVector &Operands, 961 const MCParsedAsmOperand &Op); 962 963 bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc); 964 965 bool is64BitMode() const { 966 // FIXME: Can tablegen auto-generate this? 967 return getSTI().getFeatureBits()[X86::Mode64Bit]; 968 } 969 bool is32BitMode() const { 970 // FIXME: Can tablegen auto-generate this? 971 return getSTI().getFeatureBits()[X86::Mode32Bit]; 972 } 973 bool is16BitMode() const { 974 // FIXME: Can tablegen auto-generate this? 975 return getSTI().getFeatureBits()[X86::Mode16Bit]; 976 } 977 void SwitchMode(unsigned mode) { 978 MCSubtargetInfo &STI = copySTI(); 979 FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit}); 980 FeatureBitset OldMode = STI.getFeatureBits() & AllModes; 981 FeatureBitset FB = ComputeAvailableFeatures( 982 STI.ToggleFeature(OldMode.flip(mode))); 983 setAvailableFeatures(FB); 984 985 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes)); 986 } 987 988 unsigned getPointerWidth() { 989 if (is16BitMode()) return 16; 990 if (is32BitMode()) return 32; 991 if (is64BitMode()) return 64; 992 llvm_unreachable("invalid mode"); 993 } 994 995 bool isParsingIntelSyntax() { 996 return getParser().getAssemblerDialect(); 997 } 998 999 /// @name Auto-generated Matcher Functions 1000 /// { 1001 1002 #define GET_ASSEMBLER_HEADER 1003 #include "X86GenAsmMatcher.inc" 1004 1005 /// } 1006 1007 public: 1008 enum X86MatchResultTy { 1009 Match_Unsupported = FIRST_TARGET_MATCH_RESULT_TY, 1010 #define GET_OPERAND_DIAGNOSTIC_TYPES 1011 #include "X86GenAsmMatcher.inc" 1012 }; 1013 1014 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser, 1015 const MCInstrInfo &mii, const MCTargetOptions &Options) 1016 : MCTargetAsmParser(Options, sti, mii), InstInfo(nullptr), 1017 Code16GCC(false) { 1018 1019 Parser.addAliasForDirective(".word", ".2byte"); 1020 1021 // Initialize the set of available features. 1022 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); 1023 } 1024 1025 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; 1026 1027 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; 1028 1029 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 1030 SMLoc NameLoc, OperandVector &Operands) override; 1031 1032 bool ParseDirective(AsmToken DirectiveID) override; 1033 }; 1034 } // end anonymous namespace 1035 1036 /// @name Auto-generated Match Functions 1037 /// { 1038 1039 static unsigned MatchRegisterName(StringRef Name); 1040 1041 /// } 1042 1043 static bool CheckBaseRegAndIndexRegAndScale(unsigned BaseReg, unsigned IndexReg, 1044 unsigned Scale, bool Is64BitMode, 1045 StringRef &ErrMsg) { 1046 // If we have both a base register and an index register make sure they are 1047 // both 64-bit or 32-bit registers. 1048 // To support VSIB, IndexReg can be 128-bit or 256-bit registers. 1049 1050 if (BaseReg != 0 && 1051 !(BaseReg == X86::RIP || BaseReg == X86::EIP || 1052 X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) || 1053 X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) || 1054 X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg))) { 1055 ErrMsg = "invalid base+index expression"; 1056 return true; 1057 } 1058 1059 if (IndexReg != 0 && 1060 !(IndexReg == X86::EIZ || IndexReg == X86::RIZ || 1061 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1062 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1063 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) || 1064 X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) || 1065 X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) || 1066 X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg))) { 1067 ErrMsg = "invalid base+index expression"; 1068 return true; 1069 } 1070 1071 if (((BaseReg == X86::RIP || BaseReg == X86::EIP) && IndexReg != 0) || 1072 IndexReg == X86::EIP || IndexReg == X86::RIP || 1073 IndexReg == X86::ESP || IndexReg == X86::RSP) { 1074 ErrMsg = "invalid base+index expression"; 1075 return true; 1076 } 1077 1078 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed, 1079 // and then only in non-64-bit modes. 1080 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) && 1081 (Is64BitMode || (BaseReg != X86::BX && BaseReg != X86::BP && 1082 BaseReg != X86::SI && BaseReg != X86::DI))) { 1083 ErrMsg = "invalid 16-bit base register"; 1084 return true; 1085 } 1086 1087 if (BaseReg == 0 && 1088 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) { 1089 ErrMsg = "16-bit memory operand may not include only index register"; 1090 return true; 1091 } 1092 1093 if (BaseReg != 0 && IndexReg != 0) { 1094 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) && 1095 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1096 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1097 IndexReg == X86::EIZ)) { 1098 ErrMsg = "base register is 64-bit, but index register is not"; 1099 return true; 1100 } 1101 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) && 1102 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1103 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) || 1104 IndexReg == X86::RIZ)) { 1105 ErrMsg = "base register is 32-bit, but index register is not"; 1106 return true; 1107 } 1108 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) { 1109 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1110 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) { 1111 ErrMsg = "base register is 16-bit, but index register is not"; 1112 return true; 1113 } 1114 if ((BaseReg != X86::BX && BaseReg != X86::BP) || 1115 (IndexReg != X86::SI && IndexReg != X86::DI)) { 1116 ErrMsg = "invalid 16-bit base/index register combination"; 1117 return true; 1118 } 1119 } 1120 } 1121 1122 // RIP/EIP-relative addressing is only supported in 64-bit mode. 1123 if (!Is64BitMode && BaseReg != 0 && 1124 (BaseReg == X86::RIP || BaseReg == X86::EIP)) { 1125 ErrMsg = "IP-relative addressing requires 64-bit mode"; 1126 return true; 1127 } 1128 1129 return checkScale(Scale, ErrMsg); 1130 } 1131 1132 bool X86AsmParser::ParseRegister(unsigned &RegNo, 1133 SMLoc &StartLoc, SMLoc &EndLoc) { 1134 MCAsmParser &Parser = getParser(); 1135 RegNo = 0; 1136 const AsmToken &PercentTok = Parser.getTok(); 1137 StartLoc = PercentTok.getLoc(); 1138 1139 // If we encounter a %, ignore it. This code handles registers with and 1140 // without the prefix, unprefixed registers can occur in cfi directives. 1141 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent)) 1142 Parser.Lex(); // Eat percent token. 1143 1144 const AsmToken &Tok = Parser.getTok(); 1145 EndLoc = Tok.getEndLoc(); 1146 1147 if (Tok.isNot(AsmToken::Identifier)) { 1148 if (isParsingIntelSyntax()) return true; 1149 return Error(StartLoc, "invalid register name", 1150 SMRange(StartLoc, EndLoc)); 1151 } 1152 1153 RegNo = MatchRegisterName(Tok.getString()); 1154 1155 // If the match failed, try the register name as lowercase. 1156 if (RegNo == 0) 1157 RegNo = MatchRegisterName(Tok.getString().lower()); 1158 1159 // The "flags" and "mxcsr" registers cannot be referenced directly. 1160 // Treat it as an identifier instead. 1161 if (isParsingInlineAsm() && isParsingIntelSyntax() && 1162 (RegNo == X86::EFLAGS || RegNo == X86::MXCSR)) 1163 RegNo = 0; 1164 1165 if (!is64BitMode()) { 1166 // FIXME: This should be done using Requires<Not64BitMode> and 1167 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also 1168 // checked. 1169 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a 1170 // REX prefix. 1171 if (RegNo == X86::RIZ || RegNo == X86::RIP || 1172 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) || 1173 X86II::isX86_64NonExtLowByteReg(RegNo) || 1174 X86II::isX86_64ExtendedReg(RegNo)) { 1175 StringRef RegName = Tok.getString(); 1176 Parser.Lex(); // Eat register name. 1177 return Error(StartLoc, 1178 "register %" + RegName + " is only available in 64-bit mode", 1179 SMRange(StartLoc, EndLoc)); 1180 } 1181 } 1182 1183 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens. 1184 if (RegNo == X86::ST0) { 1185 Parser.Lex(); // Eat 'st' 1186 1187 // Check to see if we have '(4)' after %st. 1188 if (getLexer().isNot(AsmToken::LParen)) 1189 return false; 1190 // Lex the paren. 1191 getParser().Lex(); 1192 1193 const AsmToken &IntTok = Parser.getTok(); 1194 if (IntTok.isNot(AsmToken::Integer)) 1195 return Error(IntTok.getLoc(), "expected stack index"); 1196 switch (IntTok.getIntVal()) { 1197 case 0: RegNo = X86::ST0; break; 1198 case 1: RegNo = X86::ST1; break; 1199 case 2: RegNo = X86::ST2; break; 1200 case 3: RegNo = X86::ST3; break; 1201 case 4: RegNo = X86::ST4; break; 1202 case 5: RegNo = X86::ST5; break; 1203 case 6: RegNo = X86::ST6; break; 1204 case 7: RegNo = X86::ST7; break; 1205 default: return Error(IntTok.getLoc(), "invalid stack index"); 1206 } 1207 1208 if (getParser().Lex().isNot(AsmToken::RParen)) 1209 return Error(Parser.getTok().getLoc(), "expected ')'"); 1210 1211 EndLoc = Parser.getTok().getEndLoc(); 1212 Parser.Lex(); // Eat ')' 1213 return false; 1214 } 1215 1216 EndLoc = Parser.getTok().getEndLoc(); 1217 1218 // If this is "db[0-15]", match it as an alias 1219 // for dr[0-15]. 1220 if (RegNo == 0 && Tok.getString().startswith("db")) { 1221 if (Tok.getString().size() == 3) { 1222 switch (Tok.getString()[2]) { 1223 case '0': RegNo = X86::DR0; break; 1224 case '1': RegNo = X86::DR1; break; 1225 case '2': RegNo = X86::DR2; break; 1226 case '3': RegNo = X86::DR3; break; 1227 case '4': RegNo = X86::DR4; break; 1228 case '5': RegNo = X86::DR5; break; 1229 case '6': RegNo = X86::DR6; break; 1230 case '7': RegNo = X86::DR7; break; 1231 case '8': RegNo = X86::DR8; break; 1232 case '9': RegNo = X86::DR9; break; 1233 } 1234 } else if (Tok.getString().size() == 4 && Tok.getString()[2] == '1') { 1235 switch (Tok.getString()[3]) { 1236 case '0': RegNo = X86::DR10; break; 1237 case '1': RegNo = X86::DR11; break; 1238 case '2': RegNo = X86::DR12; break; 1239 case '3': RegNo = X86::DR13; break; 1240 case '4': RegNo = X86::DR14; break; 1241 case '5': RegNo = X86::DR15; break; 1242 } 1243 } 1244 1245 if (RegNo != 0) { 1246 EndLoc = Parser.getTok().getEndLoc(); 1247 Parser.Lex(); // Eat it. 1248 return false; 1249 } 1250 } 1251 1252 if (RegNo == 0) { 1253 if (isParsingIntelSyntax()) return true; 1254 return Error(StartLoc, "invalid register name", 1255 SMRange(StartLoc, EndLoc)); 1256 } 1257 1258 Parser.Lex(); // Eat identifier token. 1259 return false; 1260 } 1261 1262 std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) { 1263 bool Parse32 = is32BitMode() || Code16GCC; 1264 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI); 1265 const MCExpr *Disp = MCConstantExpr::create(0, getContext()); 1266 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp, 1267 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1, 1268 Loc, Loc, 0); 1269 } 1270 1271 std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) { 1272 bool Parse32 = is32BitMode() || Code16GCC; 1273 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI); 1274 const MCExpr *Disp = MCConstantExpr::create(0, getContext()); 1275 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp, 1276 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1, 1277 Loc, Loc, 0); 1278 } 1279 1280 bool X86AsmParser::IsSIReg(unsigned Reg) { 1281 switch (Reg) { 1282 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!"); 1283 case X86::RSI: 1284 case X86::ESI: 1285 case X86::SI: 1286 return true; 1287 case X86::RDI: 1288 case X86::EDI: 1289 case X86::DI: 1290 return false; 1291 } 1292 } 1293 1294 unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, 1295 bool IsSIReg) { 1296 switch (RegClassID) { 1297 default: llvm_unreachable("Unexpected register class"); 1298 case X86::GR64RegClassID: 1299 return IsSIReg ? X86::RSI : X86::RDI; 1300 case X86::GR32RegClassID: 1301 return IsSIReg ? X86::ESI : X86::EDI; 1302 case X86::GR16RegClassID: 1303 return IsSIReg ? X86::SI : X86::DI; 1304 } 1305 } 1306 1307 void X86AsmParser::AddDefaultSrcDestOperands( 1308 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src, 1309 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) { 1310 if (isParsingIntelSyntax()) { 1311 Operands.push_back(std::move(Dst)); 1312 Operands.push_back(std::move(Src)); 1313 } 1314 else { 1315 Operands.push_back(std::move(Src)); 1316 Operands.push_back(std::move(Dst)); 1317 } 1318 } 1319 1320 bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands, 1321 OperandVector &FinalOperands) { 1322 1323 if (OrigOperands.size() > 1) { 1324 // Check if sizes match, OrigOperands also contains the instruction name 1325 assert(OrigOperands.size() == FinalOperands.size() + 1 && 1326 "Operand size mismatch"); 1327 1328 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings; 1329 // Verify types match 1330 int RegClassID = -1; 1331 for (unsigned int i = 0; i < FinalOperands.size(); ++i) { 1332 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]); 1333 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]); 1334 1335 if (FinalOp.isReg() && 1336 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg())) 1337 // Return false and let a normal complaint about bogus operands happen 1338 return false; 1339 1340 if (FinalOp.isMem()) { 1341 1342 if (!OrigOp.isMem()) 1343 // Return false and let a normal complaint about bogus operands happen 1344 return false; 1345 1346 unsigned OrigReg = OrigOp.Mem.BaseReg; 1347 unsigned FinalReg = FinalOp.Mem.BaseReg; 1348 1349 // If we've already encounterd a register class, make sure all register 1350 // bases are of the same register class 1351 if (RegClassID != -1 && 1352 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) { 1353 return Error(OrigOp.getStartLoc(), 1354 "mismatching source and destination index registers"); 1355 } 1356 1357 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg)) 1358 RegClassID = X86::GR64RegClassID; 1359 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg)) 1360 RegClassID = X86::GR32RegClassID; 1361 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg)) 1362 RegClassID = X86::GR16RegClassID; 1363 else 1364 // Unexpected register class type 1365 // Return false and let a normal complaint about bogus operands happen 1366 return false; 1367 1368 bool IsSI = IsSIReg(FinalReg); 1369 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI); 1370 1371 if (FinalReg != OrigReg) { 1372 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI"; 1373 Warnings.push_back(std::make_pair( 1374 OrigOp.getStartLoc(), 1375 "memory operand is only for determining the size, " + RegName + 1376 " will be used for the location")); 1377 } 1378 1379 FinalOp.Mem.Size = OrigOp.Mem.Size; 1380 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg; 1381 FinalOp.Mem.BaseReg = FinalReg; 1382 } 1383 } 1384 1385 // Produce warnings only if all the operands passed the adjustment - prevent 1386 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings 1387 for (auto &WarningMsg : Warnings) { 1388 Warning(WarningMsg.first, WarningMsg.second); 1389 } 1390 1391 // Remove old operands 1392 for (unsigned int i = 0; i < FinalOperands.size(); ++i) 1393 OrigOperands.pop_back(); 1394 } 1395 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end()); 1396 for (unsigned int i = 0; i < FinalOperands.size(); ++i) 1397 OrigOperands.push_back(std::move(FinalOperands[i])); 1398 1399 return false; 1400 } 1401 1402 std::unique_ptr<X86Operand> X86AsmParser::ParseOperand() { 1403 if (isParsingIntelSyntax()) 1404 return ParseIntelOperand(); 1405 return ParseATTOperand(); 1406 } 1407 1408 std::unique_ptr<X86Operand> X86AsmParser::CreateMemForInlineAsm( 1409 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg, 1410 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier, 1411 const InlineAsmIdentifierInfo &Info) { 1412 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or 1413 // some other label reference. 1414 if (Info.isKind(InlineAsmIdentifierInfo::IK_Label)) { 1415 // Insert an explicit size if the user didn't have one. 1416 if (!Size) { 1417 Size = getPointerWidth(); 1418 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start, 1419 /*Len=*/0, Size); 1420 } 1421 // Create an absolute memory reference in order to match against 1422 // instructions taking a PC relative operand. 1423 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size, 1424 Identifier, Info.Label.Decl); 1425 } 1426 // We either have a direct symbol reference, or an offset from a symbol. The 1427 // parser always puts the symbol on the LHS, so look there for size 1428 // calculation purposes. 1429 unsigned FrontendSize = 0; 1430 void *Decl = nullptr; 1431 bool IsGlobalLV = false; 1432 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 1433 // Size is in terms of bits in this context. 1434 FrontendSize = Info.Var.Type * 8; 1435 Decl = Info.Var.Decl; 1436 IsGlobalLV = Info.Var.IsGlobalLV; 1437 } 1438 // It is widely common for MS InlineAsm to use a global variable and one/two 1439 // registers in a mmory expression, and though unaccessible via rip/eip. 1440 if (IsGlobalLV && (BaseReg || IndexReg)) { 1441 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End); 1442 // Otherwise, we set the base register to a non-zero value 1443 // if we don't know the actual value at this time. This is necessary to 1444 // get the matching correct in some cases. 1445 } else { 1446 BaseReg = BaseReg ? BaseReg : 1; 1447 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg, 1448 IndexReg, Scale, Start, End, Size, Identifier, 1449 Decl, FrontendSize); 1450 } 1451 } 1452 1453 // Some binary bitwise operators have a named synonymous 1454 // Query a candidate string for being such a named operator 1455 // and if so - invoke the appropriate handler 1456 bool X86AsmParser::ParseIntelNamedOperator(StringRef Name, 1457 IntelExprStateMachine &SM, 1458 bool &ParseError, SMLoc &End) { 1459 // A named operator should be either lower or upper case, but not a mix 1460 if (Name.compare(Name.lower()) && Name.compare(Name.upper())) 1461 return false; 1462 if (Name.equals_lower("not")) { 1463 SM.onNot(); 1464 } else if (Name.equals_lower("or")) { 1465 SM.onOr(); 1466 } else if (Name.equals_lower("shl")) { 1467 SM.onLShift(); 1468 } else if (Name.equals_lower("shr")) { 1469 SM.onRShift(); 1470 } else if (Name.equals_lower("xor")) { 1471 SM.onXor(); 1472 } else if (Name.equals_lower("and")) { 1473 SM.onAnd(); 1474 } else if (Name.equals_lower("mod")) { 1475 SM.onMod(); 1476 } else if (Name.equals_lower("offset")) { 1477 SMLoc OffsetLoc = getTok().getLoc(); 1478 const MCExpr *Val = nullptr; 1479 StringRef ID; 1480 InlineAsmIdentifierInfo Info; 1481 ParseError = ParseIntelOffsetOperator(Val, ID, Info, End); 1482 if (ParseError) 1483 return true; 1484 StringRef ErrMsg; 1485 ParseError = 1486 SM.onOffset(Val, OffsetLoc, ID, Info, isParsingInlineAsm(), ErrMsg); 1487 if (ParseError) 1488 return Error(SMLoc::getFromPointer(Name.data()), ErrMsg); 1489 } else { 1490 return false; 1491 } 1492 if (!Name.equals_lower("offset")) 1493 End = consumeToken(); 1494 return true; 1495 } 1496 1497 bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) { 1498 MCAsmParser &Parser = getParser(); 1499 const AsmToken &Tok = Parser.getTok(); 1500 StringRef ErrMsg; 1501 1502 AsmToken::TokenKind PrevTK = AsmToken::Error; 1503 bool Done = false; 1504 while (!Done) { 1505 bool UpdateLocLex = true; 1506 AsmToken::TokenKind TK = getLexer().getKind(); 1507 1508 switch (TK) { 1509 default: 1510 if ((Done = SM.isValidEndState())) 1511 break; 1512 return Error(Tok.getLoc(), "unknown token in expression"); 1513 case AsmToken::EndOfStatement: 1514 Done = true; 1515 break; 1516 case AsmToken::Real: 1517 // DotOperator: [ebx].0 1518 UpdateLocLex = false; 1519 if (ParseIntelDotOperator(SM, End)) 1520 return true; 1521 break; 1522 case AsmToken::At: 1523 case AsmToken::String: 1524 case AsmToken::Identifier: { 1525 SMLoc IdentLoc = Tok.getLoc(); 1526 StringRef Identifier = Tok.getString(); 1527 UpdateLocLex = false; 1528 // Register 1529 unsigned Reg; 1530 if (Tok.is(AsmToken::Identifier) && !ParseRegister(Reg, IdentLoc, End)) { 1531 if (SM.onRegister(Reg, ErrMsg)) 1532 return Error(Tok.getLoc(), ErrMsg); 1533 break; 1534 } 1535 // Operator synonymous ("not", "or" etc.) 1536 bool ParseError = false; 1537 if (ParseIntelNamedOperator(Identifier, SM, ParseError, End)) { 1538 if (ParseError) 1539 return true; 1540 break; 1541 } 1542 // Symbol reference, when parsing assembly content 1543 InlineAsmIdentifierInfo Info; 1544 const MCExpr *Val; 1545 if (!isParsingInlineAsm()) { 1546 if (getParser().parsePrimaryExpr(Val, End)) { 1547 return Error(Tok.getLoc(), "Unexpected identifier!"); 1548 } else if (SM.onIdentifierExpr(Val, Identifier, Info, false, ErrMsg)) { 1549 return Error(IdentLoc, ErrMsg); 1550 } else 1551 break; 1552 } 1553 // MS InlineAsm operators (TYPE/LENGTH/SIZE) 1554 if (unsigned OpKind = IdentifyIntelInlineAsmOperator(Identifier)) { 1555 if (int64_t Val = ParseIntelInlineAsmOperator(OpKind)) { 1556 if (SM.onInteger(Val, ErrMsg)) 1557 return Error(IdentLoc, ErrMsg); 1558 } else 1559 return true; 1560 break; 1561 } 1562 // MS Dot Operator expression 1563 if (Identifier.count('.') && PrevTK == AsmToken::RBrac) { 1564 if (ParseIntelDotOperator(SM, End)) 1565 return true; 1566 break; 1567 } 1568 // MS InlineAsm identifier 1569 // Call parseIdentifier() to combine @ with the identifier behind it. 1570 if (TK == AsmToken::At && Parser.parseIdentifier(Identifier)) 1571 return Error(IdentLoc, "expected identifier"); 1572 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, false, End)) 1573 return true; 1574 else if (SM.onIdentifierExpr(Val, Identifier, Info, true, ErrMsg)) 1575 return Error(IdentLoc, ErrMsg); 1576 break; 1577 } 1578 case AsmToken::Integer: { 1579 // Look for 'b' or 'f' following an Integer as a directional label 1580 SMLoc Loc = getTok().getLoc(); 1581 int64_t IntVal = getTok().getIntVal(); 1582 End = consumeToken(); 1583 UpdateLocLex = false; 1584 if (getLexer().getKind() == AsmToken::Identifier) { 1585 StringRef IDVal = getTok().getString(); 1586 if (IDVal == "f" || IDVal == "b") { 1587 MCSymbol *Sym = 1588 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b"); 1589 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1590 const MCExpr *Val = 1591 MCSymbolRefExpr::create(Sym, Variant, getContext()); 1592 if (IDVal == "b" && Sym->isUndefined()) 1593 return Error(Loc, "invalid reference to undefined symbol"); 1594 StringRef Identifier = Sym->getName(); 1595 InlineAsmIdentifierInfo Info; 1596 if (SM.onIdentifierExpr(Val, Identifier, Info, 1597 isParsingInlineAsm(), ErrMsg)) 1598 return Error(Loc, ErrMsg); 1599 End = consumeToken(); 1600 } else { 1601 if (SM.onInteger(IntVal, ErrMsg)) 1602 return Error(Loc, ErrMsg); 1603 } 1604 } else { 1605 if (SM.onInteger(IntVal, ErrMsg)) 1606 return Error(Loc, ErrMsg); 1607 } 1608 break; 1609 } 1610 case AsmToken::Plus: 1611 if (SM.onPlus(ErrMsg)) 1612 return Error(getTok().getLoc(), ErrMsg); 1613 break; 1614 case AsmToken::Minus: 1615 if (SM.onMinus(ErrMsg)) 1616 return Error(getTok().getLoc(), ErrMsg); 1617 break; 1618 case AsmToken::Tilde: SM.onNot(); break; 1619 case AsmToken::Star: SM.onStar(); break; 1620 case AsmToken::Slash: SM.onDivide(); break; 1621 case AsmToken::Percent: SM.onMod(); break; 1622 case AsmToken::Pipe: SM.onOr(); break; 1623 case AsmToken::Caret: SM.onXor(); break; 1624 case AsmToken::Amp: SM.onAnd(); break; 1625 case AsmToken::LessLess: 1626 SM.onLShift(); break; 1627 case AsmToken::GreaterGreater: 1628 SM.onRShift(); break; 1629 case AsmToken::LBrac: 1630 if (SM.onLBrac()) 1631 return Error(Tok.getLoc(), "unexpected bracket encountered"); 1632 break; 1633 case AsmToken::RBrac: 1634 if (SM.onRBrac()) 1635 return Error(Tok.getLoc(), "unexpected bracket encountered"); 1636 break; 1637 case AsmToken::LParen: SM.onLParen(); break; 1638 case AsmToken::RParen: SM.onRParen(); break; 1639 } 1640 if (SM.hadError()) 1641 return Error(Tok.getLoc(), "unknown token in expression"); 1642 1643 if (!Done && UpdateLocLex) 1644 End = consumeToken(); 1645 1646 PrevTK = TK; 1647 } 1648 return false; 1649 } 1650 1651 void X86AsmParser::RewriteIntelExpression(IntelExprStateMachine &SM, 1652 SMLoc Start, SMLoc End) { 1653 SMLoc Loc = Start; 1654 unsigned ExprLen = End.getPointer() - Start.getPointer(); 1655 // Skip everything before a symbol displacement (if we have one) 1656 if (SM.getSym() && !SM.isOffsetOperator()) { 1657 StringRef SymName = SM.getSymName(); 1658 if (unsigned Len = SymName.data() - Start.getPointer()) 1659 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Start, Len); 1660 Loc = SMLoc::getFromPointer(SymName.data() + SymName.size()); 1661 ExprLen = End.getPointer() - (SymName.data() + SymName.size()); 1662 // If we have only a symbol than there's no need for complex rewrite, 1663 // simply skip everything after it 1664 if (!(SM.getBaseReg() || SM.getIndexReg() || SM.getImm())) { 1665 if (ExprLen) 1666 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Loc, ExprLen); 1667 return; 1668 } 1669 } 1670 // Build an Intel Expression rewrite 1671 StringRef BaseRegStr; 1672 StringRef IndexRegStr; 1673 StringRef OffsetNameStr; 1674 if (SM.getBaseReg()) 1675 BaseRegStr = X86IntelInstPrinter::getRegisterName(SM.getBaseReg()); 1676 if (SM.getIndexReg()) 1677 IndexRegStr = X86IntelInstPrinter::getRegisterName(SM.getIndexReg()); 1678 if (SM.isOffsetOperator()) 1679 OffsetNameStr = SM.getSymName(); 1680 // Emit it 1681 IntelExpr Expr(BaseRegStr, IndexRegStr, SM.getScale(), OffsetNameStr, 1682 SM.getImm(), SM.isMemExpr()); 1683 InstInfo->AsmRewrites->emplace_back(Loc, ExprLen, Expr); 1684 } 1685 1686 // Inline assembly may use variable names with namespace alias qualifiers. 1687 bool X86AsmParser::ParseIntelInlineAsmIdentifier( 1688 const MCExpr *&Val, StringRef &Identifier, InlineAsmIdentifierInfo &Info, 1689 bool IsUnevaluatedOperand, SMLoc &End, bool IsParsingOffsetOperator) { 1690 MCAsmParser &Parser = getParser(); 1691 assert(isParsingInlineAsm() && "Expected to be parsing inline assembly."); 1692 Val = nullptr; 1693 1694 StringRef LineBuf(Identifier.data()); 1695 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand); 1696 1697 const AsmToken &Tok = Parser.getTok(); 1698 SMLoc Loc = Tok.getLoc(); 1699 1700 // Advance the token stream until the end of the current token is 1701 // after the end of what the frontend claimed. 1702 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size(); 1703 do { 1704 End = Tok.getEndLoc(); 1705 getLexer().Lex(); 1706 } while (End.getPointer() < EndPtr); 1707 Identifier = LineBuf; 1708 1709 // The frontend should end parsing on an assembler token boundary, unless it 1710 // failed parsing. 1711 assert((End.getPointer() == EndPtr || 1712 Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) && 1713 "frontend claimed part of a token?"); 1714 1715 // If the identifier lookup was unsuccessful, assume that we are dealing with 1716 // a label. 1717 if (Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) { 1718 StringRef InternalName = 1719 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(), 1720 Loc, false); 1721 assert(InternalName.size() && "We should have an internal name here."); 1722 // Push a rewrite for replacing the identifier name with the internal name, 1723 // unless we are parsing the operand of an offset operator 1724 if (!IsParsingOffsetOperator) 1725 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(), 1726 InternalName); 1727 else 1728 Identifier = InternalName; 1729 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) 1730 return false; 1731 // Create the symbol reference. 1732 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier); 1733 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1734 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext()); 1735 return false; 1736 } 1737 1738 //ParseRoundingModeOp - Parse AVX-512 rounding mode operand 1739 std::unique_ptr<X86Operand> 1740 X86AsmParser::ParseRoundingModeOp(SMLoc Start) { 1741 MCAsmParser &Parser = getParser(); 1742 const AsmToken &Tok = Parser.getTok(); 1743 // Eat "{" and mark the current place. 1744 const SMLoc consumedToken = consumeToken(); 1745 if (Tok.isNot(AsmToken::Identifier)) 1746 return ErrorOperand(Tok.getLoc(), "Expected an identifier after {"); 1747 if (Tok.getIdentifier().startswith("r")){ 1748 int rndMode = StringSwitch<int>(Tok.getIdentifier()) 1749 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT) 1750 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF) 1751 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF) 1752 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO) 1753 .Default(-1); 1754 if (-1 == rndMode) 1755 return ErrorOperand(Tok.getLoc(), "Invalid rounding mode."); 1756 Parser.Lex(); // Eat "r*" of r*-sae 1757 if (!getLexer().is(AsmToken::Minus)) 1758 return ErrorOperand(Tok.getLoc(), "Expected - at this point"); 1759 Parser.Lex(); // Eat "-" 1760 Parser.Lex(); // Eat the sae 1761 if (!getLexer().is(AsmToken::RCurly)) 1762 return ErrorOperand(Tok.getLoc(), "Expected } at this point"); 1763 SMLoc End = Tok.getEndLoc(); 1764 Parser.Lex(); // Eat "}" 1765 const MCExpr *RndModeOp = 1766 MCConstantExpr::create(rndMode, Parser.getContext()); 1767 return X86Operand::CreateImm(RndModeOp, Start, End); 1768 } 1769 if(Tok.getIdentifier().equals("sae")){ 1770 Parser.Lex(); // Eat the sae 1771 if (!getLexer().is(AsmToken::RCurly)) 1772 return ErrorOperand(Tok.getLoc(), "Expected } at this point"); 1773 Parser.Lex(); // Eat "}" 1774 return X86Operand::CreateToken("{sae}", consumedToken); 1775 } 1776 return ErrorOperand(Tok.getLoc(), "unknown token in expression"); 1777 } 1778 1779 /// Parse the '.' operator. 1780 bool X86AsmParser::ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End) { 1781 const AsmToken &Tok = getTok(); 1782 unsigned Offset; 1783 1784 // Drop the optional '.'. 1785 StringRef DotDispStr = Tok.getString(); 1786 if (DotDispStr.startswith(".")) 1787 DotDispStr = DotDispStr.drop_front(1); 1788 1789 // .Imm gets lexed as a real. 1790 if (Tok.is(AsmToken::Real)) { 1791 APInt DotDisp; 1792 DotDispStr.getAsInteger(10, DotDisp); 1793 Offset = DotDisp.getZExtValue(); 1794 } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) { 1795 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.'); 1796 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second, 1797 Offset)) 1798 return Error(Tok.getLoc(), "Unable to lookup field reference!"); 1799 } else 1800 return Error(Tok.getLoc(), "Unexpected token type!"); 1801 1802 // Eat the DotExpression and update End 1803 End = SMLoc::getFromPointer(DotDispStr.data()); 1804 const char *DotExprEndLoc = DotDispStr.data() + DotDispStr.size(); 1805 while (Tok.getLoc().getPointer() < DotExprEndLoc) 1806 Lex(); 1807 SM.addImm(Offset); 1808 return false; 1809 } 1810 1811 /// Parse the 'offset' operator. 1812 /// This operator is used to specify the location of a given operand 1813 bool X86AsmParser::ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID, 1814 InlineAsmIdentifierInfo &Info, 1815 SMLoc &End) { 1816 // Eat offset, mark start of identifier. 1817 SMLoc Start = Lex().getLoc(); 1818 ID = getTok().getString(); 1819 if (!isParsingInlineAsm()) { 1820 if ((getTok().isNot(AsmToken::Identifier) && 1821 getTok().isNot(AsmToken::String)) || 1822 getParser().parsePrimaryExpr(Val, End)) 1823 return Error(Start, "unexpected token!"); 1824 } else if (ParseIntelInlineAsmIdentifier(Val, ID, Info, false, End, true)) { 1825 return Error(Start, "unable to lookup expression"); 1826 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) { 1827 return Error(Start, "offset operator cannot yet handle constants"); 1828 } 1829 return false; 1830 } 1831 1832 // Query a candidate string for being an Intel assembly operator 1833 // Report back its kind, or IOK_INVALID if does not evaluated as a known one 1834 unsigned X86AsmParser::IdentifyIntelInlineAsmOperator(StringRef Name) { 1835 return StringSwitch<unsigned>(Name) 1836 .Cases("TYPE","type",IOK_TYPE) 1837 .Cases("SIZE","size",IOK_SIZE) 1838 .Cases("LENGTH","length",IOK_LENGTH) 1839 .Default(IOK_INVALID); 1840 } 1841 1842 /// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator 1843 /// returns the number of elements in an array. It returns the value 1 for 1844 /// non-array variables. The SIZE operator returns the size of a C or C++ 1845 /// variable. A variable's size is the product of its LENGTH and TYPE. The 1846 /// TYPE operator returns the size of a C or C++ type or variable. If the 1847 /// variable is an array, TYPE returns the size of a single element. 1848 unsigned X86AsmParser::ParseIntelInlineAsmOperator(unsigned OpKind) { 1849 MCAsmParser &Parser = getParser(); 1850 const AsmToken &Tok = Parser.getTok(); 1851 Parser.Lex(); // Eat operator. 1852 1853 const MCExpr *Val = nullptr; 1854 InlineAsmIdentifierInfo Info; 1855 SMLoc Start = Tok.getLoc(), End; 1856 StringRef Identifier = Tok.getString(); 1857 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, 1858 /*Unevaluated=*/true, End)) 1859 return 0; 1860 1861 if (!Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 1862 Error(Start, "unable to lookup expression"); 1863 return 0; 1864 } 1865 1866 unsigned CVal = 0; 1867 switch(OpKind) { 1868 default: llvm_unreachable("Unexpected operand kind!"); 1869 case IOK_LENGTH: CVal = Info.Var.Length; break; 1870 case IOK_SIZE: CVal = Info.Var.Size; break; 1871 case IOK_TYPE: CVal = Info.Var.Type; break; 1872 } 1873 1874 return CVal; 1875 } 1876 1877 bool X86AsmParser::ParseIntelMemoryOperandSize(unsigned &Size) { 1878 Size = StringSwitch<unsigned>(getTok().getString()) 1879 .Cases("BYTE", "byte", 8) 1880 .Cases("WORD", "word", 16) 1881 .Cases("DWORD", "dword", 32) 1882 .Cases("FLOAT", "float", 32) 1883 .Cases("LONG", "long", 32) 1884 .Cases("FWORD", "fword", 48) 1885 .Cases("DOUBLE", "double", 64) 1886 .Cases("QWORD", "qword", 64) 1887 .Cases("MMWORD","mmword", 64) 1888 .Cases("XWORD", "xword", 80) 1889 .Cases("TBYTE", "tbyte", 80) 1890 .Cases("XMMWORD", "xmmword", 128) 1891 .Cases("YMMWORD", "ymmword", 256) 1892 .Cases("ZMMWORD", "zmmword", 512) 1893 .Default(0); 1894 if (Size) { 1895 const AsmToken &Tok = Lex(); // Eat operand size (e.g., byte, word). 1896 if (!(Tok.getString().equals("PTR") || Tok.getString().equals("ptr"))) 1897 return Error(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!"); 1898 Lex(); // Eat ptr. 1899 } 1900 return false; 1901 } 1902 1903 std::unique_ptr<X86Operand> X86AsmParser::ParseIntelOperand() { 1904 MCAsmParser &Parser = getParser(); 1905 const AsmToken &Tok = Parser.getTok(); 1906 SMLoc Start, End; 1907 1908 // Parse optional Size directive. 1909 unsigned Size; 1910 if (ParseIntelMemoryOperandSize(Size)) 1911 return nullptr; 1912 bool PtrInOperand = bool(Size); 1913 1914 Start = Tok.getLoc(); 1915 1916 // Rounding mode operand. 1917 if (getLexer().is(AsmToken::LCurly)) 1918 return ParseRoundingModeOp(Start); 1919 1920 // Register operand. 1921 unsigned RegNo = 0; 1922 if (Tok.is(AsmToken::Identifier) && !ParseRegister(RegNo, Start, End)) { 1923 if (RegNo == X86::RIP) 1924 return ErrorOperand(Start, "rip can only be used as a base register"); 1925 // A Register followed by ':' is considered a segment override 1926 if (Tok.isNot(AsmToken::Colon)) 1927 return !PtrInOperand ? X86Operand::CreateReg(RegNo, Start, End) : 1928 ErrorOperand(Start, "expected memory operand after 'ptr', " 1929 "found register operand instead"); 1930 // An alleged segment override. check if we have a valid segment register 1931 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo)) 1932 return ErrorOperand(Start, "invalid segment register"); 1933 // Eat ':' and update Start location 1934 Start = Lex().getLoc(); 1935 } 1936 1937 // Immediates and Memory 1938 IntelExprStateMachine SM; 1939 if (ParseIntelExpression(SM, End)) 1940 return nullptr; 1941 1942 if (isParsingInlineAsm()) 1943 RewriteIntelExpression(SM, Start, Tok.getLoc()); 1944 1945 int64_t Imm = SM.getImm(); 1946 const MCExpr *Disp = SM.getSym(); 1947 const MCExpr *ImmDisp = MCConstantExpr::create(Imm, getContext()); 1948 if (Disp && Imm) 1949 Disp = MCBinaryExpr::createAdd(Disp, ImmDisp, getContext()); 1950 if (!Disp) 1951 Disp = ImmDisp; 1952 1953 // RegNo != 0 specifies a valid segment register, 1954 // and we are parsing a segment override 1955 if (!SM.isMemExpr() && !RegNo) { 1956 if (isParsingInlineAsm() && SM.isOffsetOperator()) { 1957 const InlineAsmIdentifierInfo Info = SM.getIdentifierInfo(); 1958 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 1959 // Disp includes the address of a variable; make sure this is recorded 1960 // for later handling. 1961 return X86Operand::CreateImm(Disp, Start, End, SM.getSymName(), 1962 Info.Var.Decl, Info.Var.IsGlobalLV); 1963 } 1964 } 1965 1966 return X86Operand::CreateImm(Disp, Start, End); 1967 } 1968 1969 StringRef ErrMsg; 1970 unsigned BaseReg = SM.getBaseReg(); 1971 unsigned IndexReg = SM.getIndexReg(); 1972 unsigned Scale = SM.getScale(); 1973 1974 if (Scale == 0 && BaseReg != X86::ESP && BaseReg != X86::RSP && 1975 (IndexReg == X86::ESP || IndexReg == X86::RSP)) 1976 std::swap(BaseReg, IndexReg); 1977 1978 // If BaseReg is a vector register and IndexReg is not, swap them unless 1979 // Scale was specified in which case it would be an error. 1980 if (Scale == 0 && 1981 !(X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) || 1982 X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) || 1983 X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg)) && 1984 (X86MCRegisterClasses[X86::VR128XRegClassID].contains(BaseReg) || 1985 X86MCRegisterClasses[X86::VR256XRegClassID].contains(BaseReg) || 1986 X86MCRegisterClasses[X86::VR512RegClassID].contains(BaseReg))) 1987 std::swap(BaseReg, IndexReg); 1988 1989 if (Scale != 0 && 1990 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) 1991 return ErrorOperand(Start, "16-bit addresses cannot have a scale"); 1992 1993 // If there was no explicit scale specified, change it to 1. 1994 if (Scale == 0) 1995 Scale = 1; 1996 1997 // If this is a 16-bit addressing mode with the base and index in the wrong 1998 // order, swap them so CheckBaseRegAndIndexRegAndScale doesn't fail. It is 1999 // shared with att syntax where order matters. 2000 if ((BaseReg == X86::SI || BaseReg == X86::DI) && 2001 (IndexReg == X86::BX || IndexReg == X86::BP)) 2002 std::swap(BaseReg, IndexReg); 2003 2004 if ((BaseReg || IndexReg) && 2005 CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(), 2006 ErrMsg)) 2007 return ErrorOperand(Start, ErrMsg); 2008 if (isParsingInlineAsm()) 2009 return CreateMemForInlineAsm(RegNo, Disp, BaseReg, IndexReg, 2010 Scale, Start, End, Size, SM.getSymName(), 2011 SM.getIdentifierInfo()); 2012 if (!(BaseReg || IndexReg || RegNo)) 2013 return X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size); 2014 return X86Operand::CreateMem(getPointerWidth(), RegNo, Disp, 2015 BaseReg, IndexReg, Scale, Start, End, Size); 2016 } 2017 2018 std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand() { 2019 MCAsmParser &Parser = getParser(); 2020 switch (getLexer().getKind()) { 2021 case AsmToken::Dollar: { 2022 // $42 or $ID -> immediate. 2023 SMLoc Start = Parser.getTok().getLoc(), End; 2024 Parser.Lex(); 2025 const MCExpr *Val; 2026 // This is an immediate, so we should not parse a register. Do a precheck 2027 // for '%' to supercede intra-register parse errors. 2028 SMLoc L = Parser.getTok().getLoc(); 2029 if (check(getLexer().is(AsmToken::Percent), L, 2030 "expected immediate expression") || 2031 getParser().parseExpression(Val, End) || 2032 check(isa<X86MCExpr>(Val), L, "expected immediate expression")) 2033 return nullptr; 2034 return X86Operand::CreateImm(Val, Start, End); 2035 } 2036 case AsmToken::LCurly: { 2037 SMLoc Start = Parser.getTok().getLoc(); 2038 return ParseRoundingModeOp(Start); 2039 } 2040 default: { 2041 // This a memory operand or a register. We have some parsing complications 2042 // as a '(' may be part of an immediate expression or the addressing mode 2043 // block. This is complicated by the fact that an assembler-level variable 2044 // may refer either to a register or an immediate expression. 2045 2046 SMLoc Loc = Parser.getTok().getLoc(), EndLoc; 2047 const MCExpr *Expr = nullptr; 2048 unsigned Reg = 0; 2049 if (getLexer().isNot(AsmToken::LParen)) { 2050 // No '(' so this is either a displacement expression or a register. 2051 if (Parser.parseExpression(Expr, EndLoc)) 2052 return nullptr; 2053 if (auto *RE = dyn_cast<X86MCExpr>(Expr)) { 2054 // Segment Register. Reset Expr and copy value to register. 2055 Expr = nullptr; 2056 Reg = RE->getRegNo(); 2057 2058 // Sanity check register. 2059 if (Reg == X86::EIZ || Reg == X86::RIZ) 2060 return ErrorOperand( 2061 Loc, "%eiz and %riz can only be used as index registers", 2062 SMRange(Loc, EndLoc)); 2063 if (Reg == X86::RIP) 2064 return ErrorOperand(Loc, "%rip can only be used as a base register", 2065 SMRange(Loc, EndLoc)); 2066 // Return register that are not segment prefixes immediately. 2067 if (!Parser.parseOptionalToken(AsmToken::Colon)) 2068 return X86Operand::CreateReg(Reg, Loc, EndLoc); 2069 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(Reg)) 2070 return ErrorOperand(Loc, "invalid segment register"); 2071 } 2072 } 2073 // This is a Memory operand. 2074 return ParseMemOperand(Reg, Expr, Loc, EndLoc); 2075 } 2076 } 2077 } 2078 2079 // X86::COND_INVALID if not a recognized condition code or alternate mnemonic, 2080 // otherwise the EFLAGS Condition Code enumerator. 2081 X86::CondCode X86AsmParser::ParseConditionCode(StringRef CC) { 2082 return StringSwitch<X86::CondCode>(CC) 2083 .Case("o", X86::COND_O) // Overflow 2084 .Case("no", X86::COND_NO) // No Overflow 2085 .Cases("b", "nae", X86::COND_B) // Below/Neither Above nor Equal 2086 .Cases("ae", "nb", X86::COND_AE) // Above or Equal/Not Below 2087 .Cases("e", "z", X86::COND_E) // Equal/Zero 2088 .Cases("ne", "nz", X86::COND_NE) // Not Equal/Not Zero 2089 .Cases("be", "na", X86::COND_BE) // Below or Equal/Not Above 2090 .Cases("a", "nbe", X86::COND_A) // Above/Neither Below nor Equal 2091 .Case("s", X86::COND_S) // Sign 2092 .Case("ns", X86::COND_NS) // No Sign 2093 .Cases("p", "pe", X86::COND_P) // Parity/Parity Even 2094 .Cases("np", "po", X86::COND_NP) // No Parity/Parity Odd 2095 .Cases("l", "nge", X86::COND_L) // Less/Neither Greater nor Equal 2096 .Cases("ge", "nl", X86::COND_GE) // Greater or Equal/Not Less 2097 .Cases("le", "ng", X86::COND_LE) // Less or Equal/Not Greater 2098 .Cases("g", "nle", X86::COND_G) // Greater/Neither Less nor Equal 2099 .Default(X86::COND_INVALID); 2100 } 2101 2102 // true on failure, false otherwise 2103 // If no {z} mark was found - Parser doesn't advance 2104 bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z, 2105 const SMLoc &StartLoc) { 2106 MCAsmParser &Parser = getParser(); 2107 // Assuming we are just pass the '{' mark, quering the next token 2108 // Searched for {z}, but none was found. Return false, as no parsing error was 2109 // encountered 2110 if (!(getLexer().is(AsmToken::Identifier) && 2111 (getLexer().getTok().getIdentifier() == "z"))) 2112 return false; 2113 Parser.Lex(); // Eat z 2114 // Query and eat the '}' mark 2115 if (!getLexer().is(AsmToken::RCurly)) 2116 return Error(getLexer().getLoc(), "Expected } at this point"); 2117 Parser.Lex(); // Eat '}' 2118 // Assign Z with the {z} mark opernad 2119 Z = X86Operand::CreateToken("{z}", StartLoc); 2120 return false; 2121 } 2122 2123 // true on failure, false otherwise 2124 bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands, 2125 const MCParsedAsmOperand &Op) { 2126 MCAsmParser &Parser = getParser(); 2127 if (getLexer().is(AsmToken::LCurly)) { 2128 // Eat "{" and mark the current place. 2129 const SMLoc consumedToken = consumeToken(); 2130 // Distinguish {1to<NUM>} from {%k<NUM>}. 2131 if(getLexer().is(AsmToken::Integer)) { 2132 // Parse memory broadcasting ({1to<NUM>}). 2133 if (getLexer().getTok().getIntVal() != 1) 2134 return TokError("Expected 1to<NUM> at this point"); 2135 Parser.Lex(); // Eat "1" of 1to8 2136 if (!getLexer().is(AsmToken::Identifier) || 2137 !getLexer().getTok().getIdentifier().startswith("to")) 2138 return TokError("Expected 1to<NUM> at this point"); 2139 // Recognize only reasonable suffixes. 2140 const char *BroadcastPrimitive = 2141 StringSwitch<const char*>(getLexer().getTok().getIdentifier()) 2142 .Case("to2", "{1to2}") 2143 .Case("to4", "{1to4}") 2144 .Case("to8", "{1to8}") 2145 .Case("to16", "{1to16}") 2146 .Default(nullptr); 2147 if (!BroadcastPrimitive) 2148 return TokError("Invalid memory broadcast primitive."); 2149 Parser.Lex(); // Eat "toN" of 1toN 2150 if (!getLexer().is(AsmToken::RCurly)) 2151 return TokError("Expected } at this point"); 2152 Parser.Lex(); // Eat "}" 2153 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive, 2154 consumedToken)); 2155 // No AVX512 specific primitives can pass 2156 // after memory broadcasting, so return. 2157 return false; 2158 } else { 2159 // Parse either {k}{z}, {z}{k}, {k} or {z} 2160 // last one have no meaning, but GCC accepts it 2161 // Currently, we're just pass a '{' mark 2162 std::unique_ptr<X86Operand> Z; 2163 if (ParseZ(Z, consumedToken)) 2164 return true; 2165 // Reaching here means that parsing of the allegadly '{z}' mark yielded 2166 // no errors. 2167 // Query for the need of further parsing for a {%k<NUM>} mark 2168 if (!Z || getLexer().is(AsmToken::LCurly)) { 2169 SMLoc StartLoc = Z ? consumeToken() : consumedToken; 2170 // Parse an op-mask register mark ({%k<NUM>}), which is now to be 2171 // expected 2172 unsigned RegNo; 2173 SMLoc RegLoc; 2174 if (!ParseRegister(RegNo, RegLoc, StartLoc) && 2175 X86MCRegisterClasses[X86::VK1RegClassID].contains(RegNo)) { 2176 if (RegNo == X86::K0) 2177 return Error(RegLoc, "Register k0 can't be used as write mask"); 2178 if (!getLexer().is(AsmToken::RCurly)) 2179 return Error(getLexer().getLoc(), "Expected } at this point"); 2180 Operands.push_back(X86Operand::CreateToken("{", StartLoc)); 2181 Operands.push_back( 2182 X86Operand::CreateReg(RegNo, StartLoc, StartLoc)); 2183 Operands.push_back(X86Operand::CreateToken("}", consumeToken())); 2184 } else 2185 return Error(getLexer().getLoc(), 2186 "Expected an op-mask register at this point"); 2187 // {%k<NUM>} mark is found, inquire for {z} 2188 if (getLexer().is(AsmToken::LCurly) && !Z) { 2189 // Have we've found a parsing error, or found no (expected) {z} mark 2190 // - report an error 2191 if (ParseZ(Z, consumeToken()) || !Z) 2192 return Error(getLexer().getLoc(), 2193 "Expected a {z} mark at this point"); 2194 2195 } 2196 // '{z}' on its own is meaningless, hence should be ignored. 2197 // on the contrary - have it been accompanied by a K register, 2198 // allow it. 2199 if (Z) 2200 Operands.push_back(std::move(Z)); 2201 } 2202 } 2203 } 2204 return false; 2205 } 2206 2207 /// ParseMemOperand: 'seg : disp(basereg, indexreg, scale)'. The '%ds:' prefix 2208 /// has already been parsed if present. disp may be provided as well. 2209 std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg, 2210 const MCExpr *&Disp, 2211 const SMLoc &StartLoc, 2212 SMLoc &EndLoc) { 2213 MCAsmParser &Parser = getParser(); 2214 SMLoc Loc; 2215 // Based on the initial passed values, we may be in any of these cases, we are 2216 // in one of these cases (with current position (*)): 2217 2218 // 1. seg : * disp (base-index-scale-expr) 2219 // 2. seg : *(disp) (base-index-scale-expr) 2220 // 3. seg : *(base-index-scale-expr) 2221 // 4. disp *(base-index-scale-expr) 2222 // 5. *(disp) (base-index-scale-expr) 2223 // 6. *(base-index-scale-expr) 2224 // 7. disp * 2225 // 8. *(disp) 2226 2227 // If we do not have an displacement yet, check if we're in cases 4 or 6 by 2228 // checking if the first object after the parenthesis is a register (or an 2229 // identifier referring to a register) and parse the displacement or default 2230 // to 0 as appropriate. 2231 auto isAtMemOperand = [this]() { 2232 if (this->getLexer().isNot(AsmToken::LParen)) 2233 return false; 2234 AsmToken Buf[2]; 2235 StringRef Id; 2236 auto TokCount = this->getLexer().peekTokens(Buf, true); 2237 if (TokCount == 0) 2238 return false; 2239 switch (Buf[0].getKind()) { 2240 case AsmToken::Percent: 2241 case AsmToken::Comma: 2242 return true; 2243 // These lower cases are doing a peekIdentifier. 2244 case AsmToken::At: 2245 case AsmToken::Dollar: 2246 if ((TokCount > 1) && 2247 (Buf[1].is(AsmToken::Identifier) || Buf[1].is(AsmToken::String)) && 2248 (Buf[0].getLoc().getPointer() + 1 == Buf[1].getLoc().getPointer())) 2249 Id = StringRef(Buf[0].getLoc().getPointer(), 2250 Buf[1].getIdentifier().size() + 1); 2251 break; 2252 case AsmToken::Identifier: 2253 case AsmToken::String: 2254 Id = Buf[0].getIdentifier(); 2255 break; 2256 default: 2257 return false; 2258 } 2259 // We have an ID. Check if it is bound to a register. 2260 if (!Id.empty()) { 2261 MCSymbol *Sym = this->getContext().getOrCreateSymbol(Id); 2262 if (Sym->isVariable()) { 2263 auto V = Sym->getVariableValue(/*SetUsed*/ false); 2264 return isa<X86MCExpr>(V); 2265 } 2266 } 2267 return false; 2268 }; 2269 2270 if (!Disp) { 2271 // Parse immediate if we're not at a mem operand yet. 2272 if (!isAtMemOperand()) { 2273 if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(Disp, EndLoc)) 2274 return nullptr; 2275 assert(!isa<X86MCExpr>(Disp) && "Expected non-register here."); 2276 } else { 2277 // Disp is implicitly zero if we haven't parsed it yet. 2278 Disp = MCConstantExpr::create(0, Parser.getContext()); 2279 } 2280 } 2281 2282 // We are now either at the end of the operand or at the '(' at the start of a 2283 // base-index-scale-expr. 2284 2285 if (!parseOptionalToken(AsmToken::LParen)) { 2286 if (SegReg == 0) 2287 return X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc); 2288 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 0, 0, 1, 2289 StartLoc, EndLoc); 2290 } 2291 2292 // If we reached here, then eat the '(' and Process 2293 // the rest of the memory operand. 2294 unsigned BaseReg = 0, IndexReg = 0, Scale = 1; 2295 SMLoc BaseLoc = getLexer().getLoc(); 2296 const MCExpr *E; 2297 StringRef ErrMsg; 2298 2299 // Parse BaseReg if one is provided. 2300 if (getLexer().isNot(AsmToken::Comma) && getLexer().isNot(AsmToken::RParen)) { 2301 if (Parser.parseExpression(E, EndLoc) || 2302 check(!isa<X86MCExpr>(E), BaseLoc, "expected register here")) 2303 return nullptr; 2304 2305 // Sanity check register. 2306 BaseReg = cast<X86MCExpr>(E)->getRegNo(); 2307 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) 2308 return ErrorOperand(BaseLoc, 2309 "eiz and riz can only be used as index registers", 2310 SMRange(BaseLoc, EndLoc)); 2311 } 2312 2313 if (parseOptionalToken(AsmToken::Comma)) { 2314 // Following the comma we should have either an index register, or a scale 2315 // value. We don't support the later form, but we want to parse it 2316 // correctly. 2317 // 2318 // Even though it would be completely consistent to support syntax like 2319 // "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this. 2320 if (getLexer().isNot(AsmToken::RParen)) { 2321 if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(E, EndLoc)) 2322 return nullptr; 2323 2324 if (!isa<X86MCExpr>(E)) { 2325 // We've parsed an unexpected Scale Value instead of an index 2326 // register. Interpret it as an absolute. 2327 int64_t ScaleVal; 2328 if (!E->evaluateAsAbsolute(ScaleVal, getStreamer().getAssemblerPtr())) 2329 return ErrorOperand(Loc, "expected absolute expression"); 2330 if (ScaleVal != 1) 2331 Warning(Loc, "scale factor without index register is ignored"); 2332 Scale = 1; 2333 } else { // IndexReg Found. 2334 IndexReg = cast<X86MCExpr>(E)->getRegNo(); 2335 2336 if (BaseReg == X86::RIP) 2337 return ErrorOperand( 2338 Loc, "%rip as base register can not have an index register"); 2339 if (IndexReg == X86::RIP) 2340 return ErrorOperand(Loc, "%rip is not allowed as an index register"); 2341 2342 if (parseOptionalToken(AsmToken::Comma)) { 2343 // Parse the scale amount: 2344 // ::= ',' [scale-expression] 2345 2346 // A scale amount without an index is ignored. 2347 if (getLexer().isNot(AsmToken::RParen)) { 2348 int64_t ScaleVal; 2349 if (Parser.parseTokenLoc(Loc) || 2350 Parser.parseAbsoluteExpression(ScaleVal)) 2351 return ErrorOperand(Loc, "expected scale expression"); 2352 Scale = (unsigned)ScaleVal; 2353 // Validate the scale amount. 2354 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) && 2355 Scale != 1) 2356 return ErrorOperand(Loc, 2357 "scale factor in 16-bit address must be 1"); 2358 if (checkScale(Scale, ErrMsg)) 2359 return ErrorOperand(Loc, ErrMsg); 2360 } 2361 } 2362 } 2363 } 2364 } 2365 2366 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. 2367 if (parseToken(AsmToken::RParen, "unexpected token in memory operand")) 2368 return nullptr; 2369 2370 // This is to support otherwise illegal operand (%dx) found in various 2371 // unofficial manuals examples (e.g. "out[s]?[bwl]? %al, (%dx)") and must now 2372 // be supported. Mark such DX variants separately fix only in special cases. 2373 if (BaseReg == X86::DX && IndexReg == 0 && Scale == 1 && SegReg == 0 && 2374 isa<MCConstantExpr>(Disp) && cast<MCConstantExpr>(Disp)->getValue() == 0) 2375 return X86Operand::CreateDXReg(BaseLoc, BaseLoc); 2376 2377 if (CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(), 2378 ErrMsg)) 2379 return ErrorOperand(BaseLoc, ErrMsg); 2380 2381 if (SegReg || BaseReg || IndexReg) 2382 return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg, 2383 IndexReg, Scale, StartLoc, EndLoc); 2384 return X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc); 2385 } 2386 2387 // Parse either a standard primary expression or a register. 2388 bool X86AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { 2389 MCAsmParser &Parser = getParser(); 2390 // See if this is a register first. 2391 if (getTok().is(AsmToken::Percent) || 2392 (isParsingIntelSyntax() && getTok().is(AsmToken::Identifier) && 2393 MatchRegisterName(Parser.getTok().getString()))) { 2394 SMLoc StartLoc = Parser.getTok().getLoc(); 2395 unsigned RegNo; 2396 if (ParseRegister(RegNo, StartLoc, EndLoc)) 2397 return true; 2398 Res = X86MCExpr::create(RegNo, Parser.getContext()); 2399 return false; 2400 } 2401 return Parser.parsePrimaryExpr(Res, EndLoc); 2402 } 2403 2404 bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 2405 SMLoc NameLoc, OperandVector &Operands) { 2406 MCAsmParser &Parser = getParser(); 2407 InstInfo = &Info; 2408 2409 // Reset the forced VEX encoding. 2410 ForcedVEXEncoding = VEXEncoding_Default; 2411 2412 // Parse pseudo prefixes. 2413 while (1) { 2414 if (Name == "{") { 2415 if (getLexer().isNot(AsmToken::Identifier)) 2416 return Error(Parser.getTok().getLoc(), "Unexpected token after '{'"); 2417 std::string Prefix = Parser.getTok().getString().lower(); 2418 Parser.Lex(); // Eat identifier. 2419 if (getLexer().isNot(AsmToken::RCurly)) 2420 return Error(Parser.getTok().getLoc(), "Expected '}'"); 2421 Parser.Lex(); // Eat curly. 2422 2423 if (Prefix == "vex2") 2424 ForcedVEXEncoding = VEXEncoding_VEX2; 2425 else if (Prefix == "vex3") 2426 ForcedVEXEncoding = VEXEncoding_VEX3; 2427 else if (Prefix == "evex") 2428 ForcedVEXEncoding = VEXEncoding_EVEX; 2429 else 2430 return Error(NameLoc, "unknown prefix"); 2431 2432 NameLoc = Parser.getTok().getLoc(); 2433 if (getLexer().is(AsmToken::LCurly)) { 2434 Parser.Lex(); 2435 Name = "{"; 2436 } else { 2437 if (getLexer().isNot(AsmToken::Identifier)) 2438 return Error(Parser.getTok().getLoc(), "Expected identifier"); 2439 // FIXME: The mnemonic won't match correctly if its not in lower case. 2440 Name = Parser.getTok().getString(); 2441 Parser.Lex(); 2442 } 2443 continue; 2444 } 2445 2446 break; 2447 } 2448 2449 StringRef PatchedName = Name; 2450 2451 // Hack to skip "short" following Jcc. 2452 if (isParsingIntelSyntax() && 2453 (PatchedName == "jmp" || PatchedName == "jc" || PatchedName == "jnc" || 2454 PatchedName == "jcxz" || PatchedName == "jexcz" || 2455 (PatchedName.startswith("j") && 2456 ParseConditionCode(PatchedName.substr(1)) != X86::COND_INVALID))) { 2457 StringRef NextTok = Parser.getTok().getString(); 2458 if (NextTok == "short") { 2459 SMLoc NameEndLoc = 2460 NameLoc.getFromPointer(NameLoc.getPointer() + Name.size()); 2461 // Eat the short keyword. 2462 Parser.Lex(); 2463 // MS and GAS ignore the short keyword; they both determine the jmp type 2464 // based on the distance of the label. (NASM does emit different code with 2465 // and without "short," though.) 2466 InstInfo->AsmRewrites->emplace_back(AOK_Skip, NameEndLoc, 2467 NextTok.size() + 1); 2468 } 2469 } 2470 2471 // FIXME: Hack to recognize setneb as setne. 2472 if (PatchedName.startswith("set") && PatchedName.endswith("b") && 2473 PatchedName != "setb" && PatchedName != "setnb") 2474 PatchedName = PatchedName.substr(0, Name.size()-1); 2475 2476 unsigned ComparisonPredicate = ~0U; 2477 2478 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}. 2479 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) && 2480 (PatchedName.endswith("ss") || PatchedName.endswith("sd") || 2481 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) { 2482 bool IsVCMP = PatchedName[0] == 'v'; 2483 unsigned CCIdx = IsVCMP ? 4 : 3; 2484 unsigned CC = StringSwitch<unsigned>( 2485 PatchedName.slice(CCIdx, PatchedName.size() - 2)) 2486 .Case("eq", 0x00) 2487 .Case("eq_oq", 0x00) 2488 .Case("lt", 0x01) 2489 .Case("lt_os", 0x01) 2490 .Case("le", 0x02) 2491 .Case("le_os", 0x02) 2492 .Case("unord", 0x03) 2493 .Case("unord_q", 0x03) 2494 .Case("neq", 0x04) 2495 .Case("neq_uq", 0x04) 2496 .Case("nlt", 0x05) 2497 .Case("nlt_us", 0x05) 2498 .Case("nle", 0x06) 2499 .Case("nle_us", 0x06) 2500 .Case("ord", 0x07) 2501 .Case("ord_q", 0x07) 2502 /* AVX only from here */ 2503 .Case("eq_uq", 0x08) 2504 .Case("nge", 0x09) 2505 .Case("nge_us", 0x09) 2506 .Case("ngt", 0x0A) 2507 .Case("ngt_us", 0x0A) 2508 .Case("false", 0x0B) 2509 .Case("false_oq", 0x0B) 2510 .Case("neq_oq", 0x0C) 2511 .Case("ge", 0x0D) 2512 .Case("ge_os", 0x0D) 2513 .Case("gt", 0x0E) 2514 .Case("gt_os", 0x0E) 2515 .Case("true", 0x0F) 2516 .Case("true_uq", 0x0F) 2517 .Case("eq_os", 0x10) 2518 .Case("lt_oq", 0x11) 2519 .Case("le_oq", 0x12) 2520 .Case("unord_s", 0x13) 2521 .Case("neq_us", 0x14) 2522 .Case("nlt_uq", 0x15) 2523 .Case("nle_uq", 0x16) 2524 .Case("ord_s", 0x17) 2525 .Case("eq_us", 0x18) 2526 .Case("nge_uq", 0x19) 2527 .Case("ngt_uq", 0x1A) 2528 .Case("false_os", 0x1B) 2529 .Case("neq_os", 0x1C) 2530 .Case("ge_oq", 0x1D) 2531 .Case("gt_oq", 0x1E) 2532 .Case("true_us", 0x1F) 2533 .Default(~0U); 2534 if (CC != ~0U && (IsVCMP || CC < 8)) { 2535 if (PatchedName.endswith("ss")) 2536 PatchedName = IsVCMP ? "vcmpss" : "cmpss"; 2537 else if (PatchedName.endswith("sd")) 2538 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd"; 2539 else if (PatchedName.endswith("ps")) 2540 PatchedName = IsVCMP ? "vcmpps" : "cmpps"; 2541 else if (PatchedName.endswith("pd")) 2542 PatchedName = IsVCMP ? "vcmppd" : "cmppd"; 2543 else 2544 llvm_unreachable("Unexpected suffix!"); 2545 2546 ComparisonPredicate = CC; 2547 } 2548 } 2549 2550 // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}. 2551 if (PatchedName.startswith("vpcmp") && 2552 (PatchedName.back() == 'b' || PatchedName.back() == 'w' || 2553 PatchedName.back() == 'd' || PatchedName.back() == 'q')) { 2554 unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1; 2555 unsigned CC = StringSwitch<unsigned>( 2556 PatchedName.slice(5, PatchedName.size() - SuffixSize)) 2557 .Case("eq", 0x0) // Only allowed on unsigned. Checked below. 2558 .Case("lt", 0x1) 2559 .Case("le", 0x2) 2560 //.Case("false", 0x3) // Not a documented alias. 2561 .Case("neq", 0x4) 2562 .Case("nlt", 0x5) 2563 .Case("nle", 0x6) 2564 //.Case("true", 0x7) // Not a documented alias. 2565 .Default(~0U); 2566 if (CC != ~0U && (CC != 0 || SuffixSize == 2)) { 2567 switch (PatchedName.back()) { 2568 default: llvm_unreachable("Unexpected character!"); 2569 case 'b': PatchedName = SuffixSize == 2 ? "vpcmpub" : "vpcmpb"; break; 2570 case 'w': PatchedName = SuffixSize == 2 ? "vpcmpuw" : "vpcmpw"; break; 2571 case 'd': PatchedName = SuffixSize == 2 ? "vpcmpud" : "vpcmpd"; break; 2572 case 'q': PatchedName = SuffixSize == 2 ? "vpcmpuq" : "vpcmpq"; break; 2573 } 2574 // Set up the immediate to push into the operands later. 2575 ComparisonPredicate = CC; 2576 } 2577 } 2578 2579 // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}. 2580 if (PatchedName.startswith("vpcom") && 2581 (PatchedName.back() == 'b' || PatchedName.back() == 'w' || 2582 PatchedName.back() == 'd' || PatchedName.back() == 'q')) { 2583 unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1; 2584 unsigned CC = StringSwitch<unsigned>( 2585 PatchedName.slice(5, PatchedName.size() - SuffixSize)) 2586 .Case("lt", 0x0) 2587 .Case("le", 0x1) 2588 .Case("gt", 0x2) 2589 .Case("ge", 0x3) 2590 .Case("eq", 0x4) 2591 .Case("neq", 0x5) 2592 .Case("false", 0x6) 2593 .Case("true", 0x7) 2594 .Default(~0U); 2595 if (CC != ~0U) { 2596 switch (PatchedName.back()) { 2597 default: llvm_unreachable("Unexpected character!"); 2598 case 'b': PatchedName = SuffixSize == 2 ? "vpcomub" : "vpcomb"; break; 2599 case 'w': PatchedName = SuffixSize == 2 ? "vpcomuw" : "vpcomw"; break; 2600 case 'd': PatchedName = SuffixSize == 2 ? "vpcomud" : "vpcomd"; break; 2601 case 'q': PatchedName = SuffixSize == 2 ? "vpcomuq" : "vpcomq"; break; 2602 } 2603 // Set up the immediate to push into the operands later. 2604 ComparisonPredicate = CC; 2605 } 2606 } 2607 2608 2609 // Determine whether this is an instruction prefix. 2610 // FIXME: 2611 // Enhance prefixes integrity robustness. for example, following forms 2612 // are currently tolerated: 2613 // repz repnz <insn> ; GAS errors for the use of two similar prefixes 2614 // lock addq %rax, %rbx ; Destination operand must be of memory type 2615 // xacquire <insn> ; xacquire must be accompanied by 'lock' 2616 bool isPrefix = StringSwitch<bool>(Name) 2617 .Cases("rex64", "data32", "data16", true) 2618 .Cases("xacquire", "xrelease", true) 2619 .Cases("acquire", "release", isParsingIntelSyntax()) 2620 .Default(false); 2621 2622 auto isLockRepeatNtPrefix = [](StringRef N) { 2623 return StringSwitch<bool>(N) 2624 .Cases("lock", "rep", "repe", "repz", "repne", "repnz", "notrack", true) 2625 .Default(false); 2626 }; 2627 2628 bool CurlyAsEndOfStatement = false; 2629 2630 unsigned Flags = X86::IP_NO_PREFIX; 2631 while (isLockRepeatNtPrefix(Name.lower())) { 2632 unsigned Prefix = 2633 StringSwitch<unsigned>(Name) 2634 .Cases("lock", "lock", X86::IP_HAS_LOCK) 2635 .Cases("rep", "repe", "repz", X86::IP_HAS_REPEAT) 2636 .Cases("repne", "repnz", X86::IP_HAS_REPEAT_NE) 2637 .Cases("notrack", "notrack", X86::IP_HAS_NOTRACK) 2638 .Default(X86::IP_NO_PREFIX); // Invalid prefix (impossible) 2639 Flags |= Prefix; 2640 if (getLexer().is(AsmToken::EndOfStatement)) { 2641 // We don't have real instr with the given prefix 2642 // let's use the prefix as the instr. 2643 // TODO: there could be several prefixes one after another 2644 Flags = X86::IP_NO_PREFIX; 2645 break; 2646 } 2647 // FIXME: The mnemonic won't match correctly if its not in lower case. 2648 Name = Parser.getTok().getString(); 2649 Parser.Lex(); // eat the prefix 2650 // Hack: we could have something like "rep # some comment" or 2651 // "lock; cmpxchg16b $1" or "lock\0A\09incl" or "lock/incl" 2652 while (Name.startswith(";") || Name.startswith("\n") || 2653 Name.startswith("#") || Name.startswith("\t") || 2654 Name.startswith("/")) { 2655 // FIXME: The mnemonic won't match correctly if its not in lower case. 2656 Name = Parser.getTok().getString(); 2657 Parser.Lex(); // go to next prefix or instr 2658 } 2659 } 2660 2661 if (Flags) 2662 PatchedName = Name; 2663 2664 // Hacks to handle 'data16' and 'data32' 2665 if (PatchedName == "data16" && is16BitMode()) { 2666 return Error(NameLoc, "redundant data16 prefix"); 2667 } 2668 if (PatchedName == "data32") { 2669 if (is32BitMode()) 2670 return Error(NameLoc, "redundant data32 prefix"); 2671 if (is64BitMode()) 2672 return Error(NameLoc, "'data32' is not supported in 64-bit mode"); 2673 // Hack to 'data16' for the table lookup. 2674 PatchedName = "data16"; 2675 } 2676 2677 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc)); 2678 2679 // Push the immediate if we extracted one from the mnemonic. 2680 if (ComparisonPredicate != ~0U && !isParsingIntelSyntax()) { 2681 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate, 2682 getParser().getContext()); 2683 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc)); 2684 } 2685 2686 // This does the actual operand parsing. Don't parse any more if we have a 2687 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we 2688 // just want to parse the "lock" as the first instruction and the "incl" as 2689 // the next one. 2690 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) { 2691 // Parse '*' modifier. 2692 if (getLexer().is(AsmToken::Star)) 2693 Operands.push_back(X86Operand::CreateToken("*", consumeToken())); 2694 2695 // Read the operands. 2696 while(1) { 2697 if (std::unique_ptr<X86Operand> Op = ParseOperand()) { 2698 Operands.push_back(std::move(Op)); 2699 if (HandleAVX512Operand(Operands, *Operands.back())) 2700 return true; 2701 } else { 2702 return true; 2703 } 2704 // check for comma and eat it 2705 if (getLexer().is(AsmToken::Comma)) 2706 Parser.Lex(); 2707 else 2708 break; 2709 } 2710 2711 // In MS inline asm curly braces mark the beginning/end of a block, 2712 // therefore they should be interepreted as end of statement 2713 CurlyAsEndOfStatement = 2714 isParsingIntelSyntax() && isParsingInlineAsm() && 2715 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly)); 2716 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement) 2717 return TokError("unexpected token in argument list"); 2718 } 2719 2720 // Push the immediate if we extracted one from the mnemonic. 2721 if (ComparisonPredicate != ~0U && isParsingIntelSyntax()) { 2722 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate, 2723 getParser().getContext()); 2724 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc)); 2725 } 2726 2727 // Consume the EndOfStatement or the prefix separator Slash 2728 if (getLexer().is(AsmToken::EndOfStatement) || 2729 (isPrefix && getLexer().is(AsmToken::Slash))) 2730 Parser.Lex(); 2731 else if (CurlyAsEndOfStatement) 2732 // Add an actual EndOfStatement before the curly brace 2733 Info.AsmRewrites->emplace_back(AOK_EndOfStatement, 2734 getLexer().getTok().getLoc(), 0); 2735 2736 // This is for gas compatibility and cannot be done in td. 2737 // Adding "p" for some floating point with no argument. 2738 // For example: fsub --> fsubp 2739 bool IsFp = 2740 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr"; 2741 if (IsFp && Operands.size() == 1) { 2742 const char *Repl = StringSwitch<const char *>(Name) 2743 .Case("fsub", "fsubp") 2744 .Case("fdiv", "fdivp") 2745 .Case("fsubr", "fsubrp") 2746 .Case("fdivr", "fdivrp"); 2747 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl); 2748 } 2749 2750 if ((Name == "mov" || Name == "movw" || Name == "movl") && 2751 (Operands.size() == 3)) { 2752 X86Operand &Op1 = (X86Operand &)*Operands[1]; 2753 X86Operand &Op2 = (X86Operand &)*Operands[2]; 2754 SMLoc Loc = Op1.getEndLoc(); 2755 // Moving a 32 or 16 bit value into a segment register has the same 2756 // behavior. Modify such instructions to always take shorter form. 2757 if (Op1.isReg() && Op2.isReg() && 2758 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains( 2759 Op2.getReg()) && 2760 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) || 2761 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) { 2762 // Change instruction name to match new instruction. 2763 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) { 2764 Name = is16BitMode() ? "movw" : "movl"; 2765 Operands[0] = X86Operand::CreateToken(Name, NameLoc); 2766 } 2767 // Select the correct equivalent 16-/32-bit source register. 2768 unsigned Reg = 2769 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32); 2770 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc); 2771 } 2772 } 2773 2774 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" -> 2775 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely 2776 // documented form in various unofficial manuals, so a lot of code uses it. 2777 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" || 2778 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") && 2779 Operands.size() == 3) { 2780 X86Operand &Op = (X86Operand &)*Operands.back(); 2781 if (Op.isDXReg()) 2782 Operands.back() = X86Operand::CreateReg(X86::DX, Op.getStartLoc(), 2783 Op.getEndLoc()); 2784 } 2785 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al". 2786 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" || 2787 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") && 2788 Operands.size() == 3) { 2789 X86Operand &Op = (X86Operand &)*Operands[1]; 2790 if (Op.isDXReg()) 2791 Operands[1] = X86Operand::CreateReg(X86::DX, Op.getStartLoc(), 2792 Op.getEndLoc()); 2793 } 2794 2795 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands; 2796 bool HadVerifyError = false; 2797 2798 // Append default arguments to "ins[bwld]" 2799 if (Name.startswith("ins") && 2800 (Operands.size() == 1 || Operands.size() == 3) && 2801 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" || 2802 Name == "ins")) { 2803 2804 AddDefaultSrcDestOperands(TmpOperands, 2805 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc), 2806 DefaultMemDIOperand(NameLoc)); 2807 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2808 } 2809 2810 // Append default arguments to "outs[bwld]" 2811 if (Name.startswith("outs") && 2812 (Operands.size() == 1 || Operands.size() == 3) && 2813 (Name == "outsb" || Name == "outsw" || Name == "outsl" || 2814 Name == "outsd" || Name == "outs")) { 2815 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc), 2816 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc)); 2817 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2818 } 2819 2820 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate 2821 // values of $SIREG according to the mode. It would be nice if this 2822 // could be achieved with InstAlias in the tables. 2823 if (Name.startswith("lods") && 2824 (Operands.size() == 1 || Operands.size() == 2) && 2825 (Name == "lods" || Name == "lodsb" || Name == "lodsw" || 2826 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) { 2827 TmpOperands.push_back(DefaultMemSIOperand(NameLoc)); 2828 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2829 } 2830 2831 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate 2832 // values of $DIREG according to the mode. It would be nice if this 2833 // could be achieved with InstAlias in the tables. 2834 if (Name.startswith("stos") && 2835 (Operands.size() == 1 || Operands.size() == 2) && 2836 (Name == "stos" || Name == "stosb" || Name == "stosw" || 2837 Name == "stosl" || Name == "stosd" || Name == "stosq")) { 2838 TmpOperands.push_back(DefaultMemDIOperand(NameLoc)); 2839 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2840 } 2841 2842 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate 2843 // values of $DIREG according to the mode. It would be nice if this 2844 // could be achieved with InstAlias in the tables. 2845 if (Name.startswith("scas") && 2846 (Operands.size() == 1 || Operands.size() == 2) && 2847 (Name == "scas" || Name == "scasb" || Name == "scasw" || 2848 Name == "scasl" || Name == "scasd" || Name == "scasq")) { 2849 TmpOperands.push_back(DefaultMemDIOperand(NameLoc)); 2850 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2851 } 2852 2853 // Add default SI and DI operands to "cmps[bwlq]". 2854 if (Name.startswith("cmps") && 2855 (Operands.size() == 1 || Operands.size() == 3) && 2856 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" || 2857 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) { 2858 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc), 2859 DefaultMemSIOperand(NameLoc)); 2860 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2861 } 2862 2863 // Add default SI and DI operands to "movs[bwlq]". 2864 if (((Name.startswith("movs") && 2865 (Name == "movs" || Name == "movsb" || Name == "movsw" || 2866 Name == "movsl" || Name == "movsd" || Name == "movsq")) || 2867 (Name.startswith("smov") && 2868 (Name == "smov" || Name == "smovb" || Name == "smovw" || 2869 Name == "smovl" || Name == "smovd" || Name == "smovq"))) && 2870 (Operands.size() == 1 || Operands.size() == 3)) { 2871 if (Name == "movsd" && Operands.size() == 1 && !isParsingIntelSyntax()) 2872 Operands.back() = X86Operand::CreateToken("movsl", NameLoc); 2873 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc), 2874 DefaultMemDIOperand(NameLoc)); 2875 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 2876 } 2877 2878 // Check if we encountered an error for one the string insturctions 2879 if (HadVerifyError) { 2880 return HadVerifyError; 2881 } 2882 2883 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to 2884 // "shift <op>". 2885 if ((Name.startswith("shr") || Name.startswith("sar") || 2886 Name.startswith("shl") || Name.startswith("sal") || 2887 Name.startswith("rcl") || Name.startswith("rcr") || 2888 Name.startswith("rol") || Name.startswith("ror")) && 2889 Operands.size() == 3) { 2890 if (isParsingIntelSyntax()) { 2891 // Intel syntax 2892 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[2]); 2893 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) && 2894 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1) 2895 Operands.pop_back(); 2896 } else { 2897 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]); 2898 if (Op1.isImm() && isa<MCConstantExpr>(Op1.getImm()) && 2899 cast<MCConstantExpr>(Op1.getImm())->getValue() == 1) 2900 Operands.erase(Operands.begin() + 1); 2901 } 2902 } 2903 2904 // Transforms "int $3" into "int3" as a size optimization. We can't write an 2905 // instalias with an immediate operand yet. 2906 if (Name == "int" && Operands.size() == 2) { 2907 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]); 2908 if (Op1.isImm()) 2909 if (auto *CE = dyn_cast<MCConstantExpr>(Op1.getImm())) 2910 if (CE->getValue() == 3) { 2911 Operands.erase(Operands.begin() + 1); 2912 static_cast<X86Operand &>(*Operands[0]).setTokenValue("int3"); 2913 } 2914 } 2915 2916 // Transforms "xlat mem8" into "xlatb" 2917 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) { 2918 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]); 2919 if (Op1.isMem8()) { 2920 Warning(Op1.getStartLoc(), "memory operand is only for determining the " 2921 "size, (R|E)BX will be used for the location"); 2922 Operands.pop_back(); 2923 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb"); 2924 } 2925 } 2926 2927 if (Flags) 2928 Operands.push_back(X86Operand::CreatePrefix(Flags, NameLoc, NameLoc)); 2929 return false; 2930 } 2931 2932 bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) { 2933 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 2934 2935 switch (Inst.getOpcode()) { 2936 default: return false; 2937 case X86::VMOVZPQILo2PQIrr: 2938 case X86::VMOVAPDrr: 2939 case X86::VMOVAPDYrr: 2940 case X86::VMOVAPSrr: 2941 case X86::VMOVAPSYrr: 2942 case X86::VMOVDQArr: 2943 case X86::VMOVDQAYrr: 2944 case X86::VMOVDQUrr: 2945 case X86::VMOVDQUYrr: 2946 case X86::VMOVUPDrr: 2947 case X86::VMOVUPDYrr: 2948 case X86::VMOVUPSrr: 2949 case X86::VMOVUPSYrr: { 2950 // We can get a smaller encoding by using VEX.R instead of VEX.B if one of 2951 // the registers is extended, but other isn't. 2952 if (ForcedVEXEncoding == VEXEncoding_VEX3 || 2953 MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 || 2954 MRI->getEncodingValue(Inst.getOperand(1).getReg()) < 8) 2955 return false; 2956 2957 unsigned NewOpc; 2958 switch (Inst.getOpcode()) { 2959 default: llvm_unreachable("Invalid opcode"); 2960 case X86::VMOVZPQILo2PQIrr: NewOpc = X86::VMOVPQI2QIrr; break; 2961 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break; 2962 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break; 2963 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break; 2964 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break; 2965 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break; 2966 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break; 2967 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break; 2968 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break; 2969 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break; 2970 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break; 2971 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break; 2972 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break; 2973 } 2974 Inst.setOpcode(NewOpc); 2975 return true; 2976 } 2977 case X86::VMOVSDrr: 2978 case X86::VMOVSSrr: { 2979 // We can get a smaller encoding by using VEX.R instead of VEX.B if one of 2980 // the registers is extended, but other isn't. 2981 if (ForcedVEXEncoding == VEXEncoding_VEX3 || 2982 MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 || 2983 MRI->getEncodingValue(Inst.getOperand(2).getReg()) < 8) 2984 return false; 2985 2986 unsigned NewOpc; 2987 switch (Inst.getOpcode()) { 2988 default: llvm_unreachable("Invalid opcode"); 2989 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break; 2990 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break; 2991 } 2992 Inst.setOpcode(NewOpc); 2993 return true; 2994 } 2995 } 2996 } 2997 2998 bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) { 2999 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 3000 3001 switch (Inst.getOpcode()) { 3002 case X86::VGATHERDPDYrm: 3003 case X86::VGATHERDPDrm: 3004 case X86::VGATHERDPSYrm: 3005 case X86::VGATHERDPSrm: 3006 case X86::VGATHERQPDYrm: 3007 case X86::VGATHERQPDrm: 3008 case X86::VGATHERQPSYrm: 3009 case X86::VGATHERQPSrm: 3010 case X86::VPGATHERDDYrm: 3011 case X86::VPGATHERDDrm: 3012 case X86::VPGATHERDQYrm: 3013 case X86::VPGATHERDQrm: 3014 case X86::VPGATHERQDYrm: 3015 case X86::VPGATHERQDrm: 3016 case X86::VPGATHERQQYrm: 3017 case X86::VPGATHERQQrm: { 3018 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg()); 3019 unsigned Mask = MRI->getEncodingValue(Inst.getOperand(1).getReg()); 3020 unsigned Index = 3021 MRI->getEncodingValue(Inst.getOperand(3 + X86::AddrIndexReg).getReg()); 3022 if (Dest == Mask || Dest == Index || Mask == Index) 3023 return Warning(Ops[0]->getStartLoc(), "mask, index, and destination " 3024 "registers should be distinct"); 3025 break; 3026 } 3027 case X86::VGATHERDPDZ128rm: 3028 case X86::VGATHERDPDZ256rm: 3029 case X86::VGATHERDPDZrm: 3030 case X86::VGATHERDPSZ128rm: 3031 case X86::VGATHERDPSZ256rm: 3032 case X86::VGATHERDPSZrm: 3033 case X86::VGATHERQPDZ128rm: 3034 case X86::VGATHERQPDZ256rm: 3035 case X86::VGATHERQPDZrm: 3036 case X86::VGATHERQPSZ128rm: 3037 case X86::VGATHERQPSZ256rm: 3038 case X86::VGATHERQPSZrm: 3039 case X86::VPGATHERDDZ128rm: 3040 case X86::VPGATHERDDZ256rm: 3041 case X86::VPGATHERDDZrm: 3042 case X86::VPGATHERDQZ128rm: 3043 case X86::VPGATHERDQZ256rm: 3044 case X86::VPGATHERDQZrm: 3045 case X86::VPGATHERQDZ128rm: 3046 case X86::VPGATHERQDZ256rm: 3047 case X86::VPGATHERQDZrm: 3048 case X86::VPGATHERQQZ128rm: 3049 case X86::VPGATHERQQZ256rm: 3050 case X86::VPGATHERQQZrm: { 3051 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg()); 3052 unsigned Index = 3053 MRI->getEncodingValue(Inst.getOperand(4 + X86::AddrIndexReg).getReg()); 3054 if (Dest == Index) 3055 return Warning(Ops[0]->getStartLoc(), "index and destination registers " 3056 "should be distinct"); 3057 break; 3058 } 3059 case X86::V4FMADDPSrm: 3060 case X86::V4FMADDPSrmk: 3061 case X86::V4FMADDPSrmkz: 3062 case X86::V4FMADDSSrm: 3063 case X86::V4FMADDSSrmk: 3064 case X86::V4FMADDSSrmkz: 3065 case X86::V4FNMADDPSrm: 3066 case X86::V4FNMADDPSrmk: 3067 case X86::V4FNMADDPSrmkz: 3068 case X86::V4FNMADDSSrm: 3069 case X86::V4FNMADDSSrmk: 3070 case X86::V4FNMADDSSrmkz: 3071 case X86::VP4DPWSSDSrm: 3072 case X86::VP4DPWSSDSrmk: 3073 case X86::VP4DPWSSDSrmkz: 3074 case X86::VP4DPWSSDrm: 3075 case X86::VP4DPWSSDrmk: 3076 case X86::VP4DPWSSDrmkz: { 3077 unsigned Src2 = Inst.getOperand(Inst.getNumOperands() - 3078 X86::AddrNumOperands - 1).getReg(); 3079 unsigned Src2Enc = MRI->getEncodingValue(Src2); 3080 if (Src2Enc % 4 != 0) { 3081 StringRef RegName = X86IntelInstPrinter::getRegisterName(Src2); 3082 unsigned GroupStart = (Src2Enc / 4) * 4; 3083 unsigned GroupEnd = GroupStart + 3; 3084 return Warning(Ops[0]->getStartLoc(), 3085 "source register '" + RegName + "' implicitly denotes '" + 3086 RegName.take_front(3) + Twine(GroupStart) + "' to '" + 3087 RegName.take_front(3) + Twine(GroupEnd) + 3088 "' source group"); 3089 } 3090 break; 3091 } 3092 } 3093 3094 return false; 3095 } 3096 3097 static const char *getSubtargetFeatureName(uint64_t Val); 3098 3099 void X86AsmParser::EmitInstruction(MCInst &Inst, OperandVector &Operands, 3100 MCStreamer &Out) { 3101 Out.EmitInstruction(Inst, getSTI()); 3102 } 3103 3104 bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 3105 OperandVector &Operands, 3106 MCStreamer &Out, uint64_t &ErrorInfo, 3107 bool MatchingInlineAsm) { 3108 if (isParsingIntelSyntax()) 3109 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo, 3110 MatchingInlineAsm); 3111 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo, 3112 MatchingInlineAsm); 3113 } 3114 3115 void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, 3116 OperandVector &Operands, MCStreamer &Out, 3117 bool MatchingInlineAsm) { 3118 // FIXME: This should be replaced with a real .td file alias mechanism. 3119 // Also, MatchInstructionImpl should actually *do* the EmitInstruction 3120 // call. 3121 const char *Repl = StringSwitch<const char *>(Op.getToken()) 3122 .Case("finit", "fninit") 3123 .Case("fsave", "fnsave") 3124 .Case("fstcw", "fnstcw") 3125 .Case("fstcww", "fnstcw") 3126 .Case("fstenv", "fnstenv") 3127 .Case("fstsw", "fnstsw") 3128 .Case("fstsww", "fnstsw") 3129 .Case("fclex", "fnclex") 3130 .Default(nullptr); 3131 if (Repl) { 3132 MCInst Inst; 3133 Inst.setOpcode(X86::WAIT); 3134 Inst.setLoc(IDLoc); 3135 if (!MatchingInlineAsm) 3136 EmitInstruction(Inst, Operands, Out); 3137 Operands[0] = X86Operand::CreateToken(Repl, IDLoc); 3138 } 3139 } 3140 3141 bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, 3142 const FeatureBitset &MissingFeatures, 3143 bool MatchingInlineAsm) { 3144 assert(MissingFeatures.any() && "Unknown missing feature!"); 3145 SmallString<126> Msg; 3146 raw_svector_ostream OS(Msg); 3147 OS << "instruction requires:"; 3148 for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i) { 3149 if (MissingFeatures[i]) 3150 OS << ' ' << getSubtargetFeatureName(i); 3151 } 3152 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm); 3153 } 3154 3155 static unsigned getPrefixes(OperandVector &Operands) { 3156 unsigned Result = 0; 3157 X86Operand &Prefix = static_cast<X86Operand &>(*Operands.back()); 3158 if (Prefix.isPrefix()) { 3159 Result = Prefix.getPrefix(); 3160 Operands.pop_back(); 3161 } 3162 return Result; 3163 } 3164 3165 unsigned X86AsmParser::checkTargetMatchPredicate(MCInst &Inst) { 3166 unsigned Opc = Inst.getOpcode(); 3167 const MCInstrDesc &MCID = MII.get(Opc); 3168 3169 if (ForcedVEXEncoding == VEXEncoding_EVEX && 3170 (MCID.TSFlags & X86II::EncodingMask) != X86II::EVEX) 3171 return Match_Unsupported; 3172 3173 if ((ForcedVEXEncoding == VEXEncoding_VEX2 || 3174 ForcedVEXEncoding == VEXEncoding_VEX3) && 3175 (MCID.TSFlags & X86II::EncodingMask) != X86II::VEX) 3176 return Match_Unsupported; 3177 3178 // These instructions match ambiguously with their VEX encoded counterparts 3179 // and appear first in the matching table. Reject them unless we're forcing 3180 // EVEX encoding. 3181 // FIXME: We really need a way to break the ambiguity. 3182 switch (Opc) { 3183 case X86::VCVTSD2SIZrm_Int: 3184 case X86::VCVTSD2SI64Zrm_Int: 3185 case X86::VCVTSS2SIZrm_Int: 3186 case X86::VCVTSS2SI64Zrm_Int: 3187 case X86::VCVTTSD2SIZrm: case X86::VCVTTSD2SIZrm_Int: 3188 case X86::VCVTTSD2SI64Zrm: case X86::VCVTTSD2SI64Zrm_Int: 3189 case X86::VCVTTSS2SIZrm: case X86::VCVTTSS2SIZrm_Int: 3190 case X86::VCVTTSS2SI64Zrm: case X86::VCVTTSS2SI64Zrm_Int: 3191 if (ForcedVEXEncoding != VEXEncoding_EVEX) 3192 return Match_Unsupported; 3193 break; 3194 } 3195 3196 return Match_Success; 3197 } 3198 3199 bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode, 3200 OperandVector &Operands, 3201 MCStreamer &Out, 3202 uint64_t &ErrorInfo, 3203 bool MatchingInlineAsm) { 3204 assert(!Operands.empty() && "Unexpect empty operand list!"); 3205 assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!"); 3206 SMRange EmptyRange = None; 3207 3208 // First, handle aliases that expand to multiple instructions. 3209 MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands, 3210 Out, MatchingInlineAsm); 3211 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]); 3212 unsigned Prefixes = getPrefixes(Operands); 3213 3214 MCInst Inst; 3215 3216 // If VEX3 encoding is forced, we need to pass the USE_VEX3 flag to the 3217 // encoder. 3218 if (ForcedVEXEncoding == VEXEncoding_VEX3) 3219 Prefixes |= X86::IP_USE_VEX3; 3220 3221 if (Prefixes) 3222 Inst.setFlags(Prefixes); 3223 3224 // First, try a direct match. 3225 FeatureBitset MissingFeatures; 3226 unsigned OriginalError = MatchInstruction(Operands, Inst, ErrorInfo, 3227 MissingFeatures, MatchingInlineAsm, 3228 isParsingIntelSyntax()); 3229 switch (OriginalError) { 3230 default: llvm_unreachable("Unexpected match result!"); 3231 case Match_Success: 3232 if (!MatchingInlineAsm && validateInstruction(Inst, Operands)) 3233 return true; 3234 // Some instructions need post-processing to, for example, tweak which 3235 // encoding is selected. Loop on it while changes happen so the 3236 // individual transformations can chain off each other. 3237 if (!MatchingInlineAsm) 3238 while (processInstruction(Inst, Operands)) 3239 ; 3240 3241 Inst.setLoc(IDLoc); 3242 if (!MatchingInlineAsm) 3243 EmitInstruction(Inst, Operands, Out); 3244 Opcode = Inst.getOpcode(); 3245 return false; 3246 case Match_InvalidImmUnsignedi4: { 3247 SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc(); 3248 if (ErrorLoc == SMLoc()) 3249 ErrorLoc = IDLoc; 3250 return Error(ErrorLoc, "immediate must be an integer in range [0, 15]", 3251 EmptyRange, MatchingInlineAsm); 3252 } 3253 case Match_MissingFeature: 3254 return ErrorMissingFeature(IDLoc, MissingFeatures, MatchingInlineAsm); 3255 case Match_InvalidOperand: 3256 case Match_MnemonicFail: 3257 case Match_Unsupported: 3258 break; 3259 } 3260 if (Op.getToken().empty()) { 3261 Error(IDLoc, "instruction must have size higher than 0", EmptyRange, 3262 MatchingInlineAsm); 3263 return true; 3264 } 3265 3266 // FIXME: Ideally, we would only attempt suffix matches for things which are 3267 // valid prefixes, and we could just infer the right unambiguous 3268 // type. However, that requires substantially more matcher support than the 3269 // following hack. 3270 3271 // Change the operand to point to a temporary token. 3272 StringRef Base = Op.getToken(); 3273 SmallString<16> Tmp; 3274 Tmp += Base; 3275 Tmp += ' '; 3276 Op.setTokenValue(Tmp); 3277 3278 // If this instruction starts with an 'f', then it is a floating point stack 3279 // instruction. These come in up to three forms for 32-bit, 64-bit, and 3280 // 80-bit floating point, which use the suffixes s,l,t respectively. 3281 // 3282 // Otherwise, we assume that this may be an integer instruction, which comes 3283 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively. 3284 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0"; 3285 3286 // Check for the various suffix matches. 3287 uint64_t ErrorInfoIgnore; 3288 FeatureBitset ErrorInfoMissingFeatures; // Init suppresses compiler warnings. 3289 unsigned Match[4]; 3290 3291 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) { 3292 Tmp.back() = Suffixes[I]; 3293 Match[I] = MatchInstruction(Operands, Inst, ErrorInfoIgnore, 3294 MissingFeatures, MatchingInlineAsm, 3295 isParsingIntelSyntax()); 3296 // If this returned as a missing feature failure, remember that. 3297 if (Match[I] == Match_MissingFeature) 3298 ErrorInfoMissingFeatures = MissingFeatures; 3299 } 3300 3301 // Restore the old token. 3302 Op.setTokenValue(Base); 3303 3304 // If exactly one matched, then we treat that as a successful match (and the 3305 // instruction will already have been filled in correctly, since the failing 3306 // matches won't have modified it). 3307 unsigned NumSuccessfulMatches = 3308 std::count(std::begin(Match), std::end(Match), Match_Success); 3309 if (NumSuccessfulMatches == 1) { 3310 Inst.setLoc(IDLoc); 3311 if (!MatchingInlineAsm) 3312 EmitInstruction(Inst, Operands, Out); 3313 Opcode = Inst.getOpcode(); 3314 return false; 3315 } 3316 3317 // Otherwise, the match failed, try to produce a decent error message. 3318 3319 // If we had multiple suffix matches, then identify this as an ambiguous 3320 // match. 3321 if (NumSuccessfulMatches > 1) { 3322 char MatchChars[4]; 3323 unsigned NumMatches = 0; 3324 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) 3325 if (Match[I] == Match_Success) 3326 MatchChars[NumMatches++] = Suffixes[I]; 3327 3328 SmallString<126> Msg; 3329 raw_svector_ostream OS(Msg); 3330 OS << "ambiguous instructions require an explicit suffix (could be "; 3331 for (unsigned i = 0; i != NumMatches; ++i) { 3332 if (i != 0) 3333 OS << ", "; 3334 if (i + 1 == NumMatches) 3335 OS << "or "; 3336 OS << "'" << Base << MatchChars[i] << "'"; 3337 } 3338 OS << ")"; 3339 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm); 3340 return true; 3341 } 3342 3343 // Okay, we know that none of the variants matched successfully. 3344 3345 // If all of the instructions reported an invalid mnemonic, then the original 3346 // mnemonic was invalid. 3347 if (std::count(std::begin(Match), std::end(Match), Match_MnemonicFail) == 4) { 3348 if (OriginalError == Match_MnemonicFail) 3349 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'", 3350 Op.getLocRange(), MatchingInlineAsm); 3351 3352 if (OriginalError == Match_Unsupported) 3353 return Error(IDLoc, "unsupported instruction", EmptyRange, 3354 MatchingInlineAsm); 3355 3356 assert(OriginalError == Match_InvalidOperand && "Unexpected error"); 3357 // Recover location info for the operand if we know which was the problem. 3358 if (ErrorInfo != ~0ULL) { 3359 if (ErrorInfo >= Operands.size()) 3360 return Error(IDLoc, "too few operands for instruction", EmptyRange, 3361 MatchingInlineAsm); 3362 3363 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo]; 3364 if (Operand.getStartLoc().isValid()) { 3365 SMRange OperandRange = Operand.getLocRange(); 3366 return Error(Operand.getStartLoc(), "invalid operand for instruction", 3367 OperandRange, MatchingInlineAsm); 3368 } 3369 } 3370 3371 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 3372 MatchingInlineAsm); 3373 } 3374 3375 // If one instruction matched as unsupported, report this as unsupported. 3376 if (std::count(std::begin(Match), std::end(Match), 3377 Match_Unsupported) == 1) { 3378 return Error(IDLoc, "unsupported instruction", EmptyRange, 3379 MatchingInlineAsm); 3380 } 3381 3382 // If one instruction matched with a missing feature, report this as a 3383 // missing feature. 3384 if (std::count(std::begin(Match), std::end(Match), 3385 Match_MissingFeature) == 1) { 3386 ErrorInfo = Match_MissingFeature; 3387 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures, 3388 MatchingInlineAsm); 3389 } 3390 3391 // If one instruction matched with an invalid operand, report this as an 3392 // operand failure. 3393 if (std::count(std::begin(Match), std::end(Match), 3394 Match_InvalidOperand) == 1) { 3395 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 3396 MatchingInlineAsm); 3397 } 3398 3399 // If all of these were an outright failure, report it in a useless way. 3400 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix", 3401 EmptyRange, MatchingInlineAsm); 3402 return true; 3403 } 3404 3405 bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode, 3406 OperandVector &Operands, 3407 MCStreamer &Out, 3408 uint64_t &ErrorInfo, 3409 bool MatchingInlineAsm) { 3410 assert(!Operands.empty() && "Unexpect empty operand list!"); 3411 assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!"); 3412 StringRef Mnemonic = (static_cast<X86Operand &>(*Operands[0])).getToken(); 3413 SMRange EmptyRange = None; 3414 StringRef Base = (static_cast<X86Operand &>(*Operands[0])).getToken(); 3415 unsigned Prefixes = getPrefixes(Operands); 3416 3417 // First, handle aliases that expand to multiple instructions. 3418 MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands, Out, MatchingInlineAsm); 3419 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]); 3420 3421 MCInst Inst; 3422 3423 // If VEX3 encoding is forced, we need to pass the USE_VEX3 flag to the 3424 // encoder. 3425 if (ForcedVEXEncoding == VEXEncoding_VEX3) 3426 Prefixes |= X86::IP_USE_VEX3; 3427 3428 if (Prefixes) 3429 Inst.setFlags(Prefixes); 3430 3431 // Find one unsized memory operand, if present. 3432 X86Operand *UnsizedMemOp = nullptr; 3433 for (const auto &Op : Operands) { 3434 X86Operand *X86Op = static_cast<X86Operand *>(Op.get()); 3435 if (X86Op->isMemUnsized()) { 3436 UnsizedMemOp = X86Op; 3437 // Have we found an unqualified memory operand, 3438 // break. IA allows only one memory operand. 3439 break; 3440 } 3441 } 3442 3443 // Allow some instructions to have implicitly pointer-sized operands. This is 3444 // compatible with gas. 3445 if (UnsizedMemOp) { 3446 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"}; 3447 for (const char *Instr : PtrSizedInstrs) { 3448 if (Mnemonic == Instr) { 3449 UnsizedMemOp->Mem.Size = getPointerWidth(); 3450 break; 3451 } 3452 } 3453 } 3454 3455 SmallVector<unsigned, 8> Match; 3456 FeatureBitset ErrorInfoMissingFeatures; 3457 FeatureBitset MissingFeatures; 3458 3459 // If unsized push has immediate operand we should default the default pointer 3460 // size for the size. 3461 if (Mnemonic == "push" && Operands.size() == 2) { 3462 auto *X86Op = static_cast<X86Operand *>(Operands[1].get()); 3463 if (X86Op->isImm()) { 3464 // If it's not a constant fall through and let remainder take care of it. 3465 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm()); 3466 unsigned Size = getPointerWidth(); 3467 if (CE && 3468 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) { 3469 SmallString<16> Tmp; 3470 Tmp += Base; 3471 Tmp += (is64BitMode()) 3472 ? "q" 3473 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " "; 3474 Op.setTokenValue(Tmp); 3475 // Do match in ATT mode to allow explicit suffix usage. 3476 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo, 3477 MissingFeatures, MatchingInlineAsm, 3478 false /*isParsingIntelSyntax()*/)); 3479 Op.setTokenValue(Base); 3480 } 3481 } 3482 } 3483 3484 // If an unsized memory operand is present, try to match with each memory 3485 // operand size. In Intel assembly, the size is not part of the instruction 3486 // mnemonic. 3487 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) { 3488 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512}; 3489 for (unsigned Size : MopSizes) { 3490 UnsizedMemOp->Mem.Size = Size; 3491 uint64_t ErrorInfoIgnore; 3492 unsigned LastOpcode = Inst.getOpcode(); 3493 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore, 3494 MissingFeatures, MatchingInlineAsm, 3495 isParsingIntelSyntax()); 3496 if (Match.empty() || LastOpcode != Inst.getOpcode()) 3497 Match.push_back(M); 3498 3499 // If this returned as a missing feature failure, remember that. 3500 if (Match.back() == Match_MissingFeature) 3501 ErrorInfoMissingFeatures = MissingFeatures; 3502 } 3503 3504 // Restore the size of the unsized memory operand if we modified it. 3505 UnsizedMemOp->Mem.Size = 0; 3506 } 3507 3508 // If we haven't matched anything yet, this is not a basic integer or FPU 3509 // operation. There shouldn't be any ambiguity in our mnemonic table, so try 3510 // matching with the unsized operand. 3511 if (Match.empty()) { 3512 Match.push_back(MatchInstruction( 3513 Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm, 3514 isParsingIntelSyntax())); 3515 // If this returned as a missing feature failure, remember that. 3516 if (Match.back() == Match_MissingFeature) 3517 ErrorInfoMissingFeatures = MissingFeatures; 3518 } 3519 3520 // Restore the size of the unsized memory operand if we modified it. 3521 if (UnsizedMemOp) 3522 UnsizedMemOp->Mem.Size = 0; 3523 3524 // If it's a bad mnemonic, all results will be the same. 3525 if (Match.back() == Match_MnemonicFail) { 3526 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'", 3527 Op.getLocRange(), MatchingInlineAsm); 3528 } 3529 3530 unsigned NumSuccessfulMatches = 3531 std::count(std::begin(Match), std::end(Match), Match_Success); 3532 3533 // If matching was ambiguous and we had size information from the frontend, 3534 // try again with that. This handles cases like "movxz eax, m8/m16". 3535 if (UnsizedMemOp && NumSuccessfulMatches > 1 && 3536 UnsizedMemOp->getMemFrontendSize()) { 3537 UnsizedMemOp->Mem.Size = UnsizedMemOp->getMemFrontendSize(); 3538 unsigned M = MatchInstruction( 3539 Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm, 3540 isParsingIntelSyntax()); 3541 if (M == Match_Success) 3542 NumSuccessfulMatches = 1; 3543 3544 // Add a rewrite that encodes the size information we used from the 3545 // frontend. 3546 InstInfo->AsmRewrites->emplace_back( 3547 AOK_SizeDirective, UnsizedMemOp->getStartLoc(), 3548 /*Len=*/0, UnsizedMemOp->getMemFrontendSize()); 3549 } 3550 3551 // If exactly one matched, then we treat that as a successful match (and the 3552 // instruction will already have been filled in correctly, since the failing 3553 // matches won't have modified it). 3554 if (NumSuccessfulMatches == 1) { 3555 if (!MatchingInlineAsm && validateInstruction(Inst, Operands)) 3556 return true; 3557 // Some instructions need post-processing to, for example, tweak which 3558 // encoding is selected. Loop on it while changes happen so the individual 3559 // transformations can chain off each other. 3560 if (!MatchingInlineAsm) 3561 while (processInstruction(Inst, Operands)) 3562 ; 3563 Inst.setLoc(IDLoc); 3564 if (!MatchingInlineAsm) 3565 EmitInstruction(Inst, Operands, Out); 3566 Opcode = Inst.getOpcode(); 3567 return false; 3568 } else if (NumSuccessfulMatches > 1) { 3569 assert(UnsizedMemOp && 3570 "multiple matches only possible with unsized memory operands"); 3571 return Error(UnsizedMemOp->getStartLoc(), 3572 "ambiguous operand size for instruction '" + Mnemonic + "\'", 3573 UnsizedMemOp->getLocRange()); 3574 } 3575 3576 // If one instruction matched as unsupported, report this as unsupported. 3577 if (std::count(std::begin(Match), std::end(Match), 3578 Match_Unsupported) == 1) { 3579 return Error(IDLoc, "unsupported instruction", EmptyRange, 3580 MatchingInlineAsm); 3581 } 3582 3583 // If one instruction matched with a missing feature, report this as a 3584 // missing feature. 3585 if (std::count(std::begin(Match), std::end(Match), 3586 Match_MissingFeature) == 1) { 3587 ErrorInfo = Match_MissingFeature; 3588 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures, 3589 MatchingInlineAsm); 3590 } 3591 3592 // If one instruction matched with an invalid operand, report this as an 3593 // operand failure. 3594 if (std::count(std::begin(Match), std::end(Match), 3595 Match_InvalidOperand) == 1) { 3596 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 3597 MatchingInlineAsm); 3598 } 3599 3600 if (std::count(std::begin(Match), std::end(Match), 3601 Match_InvalidImmUnsignedi4) == 1) { 3602 SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc(); 3603 if (ErrorLoc == SMLoc()) 3604 ErrorLoc = IDLoc; 3605 return Error(ErrorLoc, "immediate must be an integer in range [0, 15]", 3606 EmptyRange, MatchingInlineAsm); 3607 } 3608 3609 // If all of these were an outright failure, report it in a useless way. 3610 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange, 3611 MatchingInlineAsm); 3612 } 3613 3614 bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) { 3615 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo); 3616 } 3617 3618 bool X86AsmParser::ParseDirective(AsmToken DirectiveID) { 3619 MCAsmParser &Parser = getParser(); 3620 StringRef IDVal = DirectiveID.getIdentifier(); 3621 if (IDVal.startswith(".code")) 3622 return ParseDirectiveCode(IDVal, DirectiveID.getLoc()); 3623 else if (IDVal.startswith(".att_syntax")) { 3624 if (getLexer().isNot(AsmToken::EndOfStatement)) { 3625 if (Parser.getTok().getString() == "prefix") 3626 Parser.Lex(); 3627 else if (Parser.getTok().getString() == "noprefix") 3628 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not " 3629 "supported: registers must have a " 3630 "'%' prefix in .att_syntax"); 3631 } 3632 getParser().setAssemblerDialect(0); 3633 return false; 3634 } else if (IDVal.startswith(".intel_syntax")) { 3635 getParser().setAssemblerDialect(1); 3636 if (getLexer().isNot(AsmToken::EndOfStatement)) { 3637 if (Parser.getTok().getString() == "noprefix") 3638 Parser.Lex(); 3639 else if (Parser.getTok().getString() == "prefix") 3640 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not " 3641 "supported: registers must not have " 3642 "a '%' prefix in .intel_syntax"); 3643 } 3644 return false; 3645 } else if (IDVal == ".even") 3646 return parseDirectiveEven(DirectiveID.getLoc()); 3647 else if (IDVal == ".cv_fpo_proc") 3648 return parseDirectiveFPOProc(DirectiveID.getLoc()); 3649 else if (IDVal == ".cv_fpo_setframe") 3650 return parseDirectiveFPOSetFrame(DirectiveID.getLoc()); 3651 else if (IDVal == ".cv_fpo_pushreg") 3652 return parseDirectiveFPOPushReg(DirectiveID.getLoc()); 3653 else if (IDVal == ".cv_fpo_stackalloc") 3654 return parseDirectiveFPOStackAlloc(DirectiveID.getLoc()); 3655 else if (IDVal == ".cv_fpo_stackalign") 3656 return parseDirectiveFPOStackAlign(DirectiveID.getLoc()); 3657 else if (IDVal == ".cv_fpo_endprologue") 3658 return parseDirectiveFPOEndPrologue(DirectiveID.getLoc()); 3659 else if (IDVal == ".cv_fpo_endproc") 3660 return parseDirectiveFPOEndProc(DirectiveID.getLoc()); 3661 else if (IDVal == ".seh_pushreg") 3662 return parseDirectiveSEHPushReg(DirectiveID.getLoc()); 3663 else if (IDVal == ".seh_setframe") 3664 return parseDirectiveSEHSetFrame(DirectiveID.getLoc()); 3665 else if (IDVal == ".seh_savereg") 3666 return parseDirectiveSEHSaveReg(DirectiveID.getLoc()); 3667 else if (IDVal == ".seh_savexmm") 3668 return parseDirectiveSEHSaveXMM(DirectiveID.getLoc()); 3669 else if (IDVal == ".seh_pushframe") 3670 return parseDirectiveSEHPushFrame(DirectiveID.getLoc()); 3671 3672 return true; 3673 } 3674 3675 /// parseDirectiveEven 3676 /// ::= .even 3677 bool X86AsmParser::parseDirectiveEven(SMLoc L) { 3678 if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive")) 3679 return false; 3680 3681 const MCSection *Section = getStreamer().getCurrentSectionOnly(); 3682 if (!Section) { 3683 getStreamer().InitSections(false); 3684 Section = getStreamer().getCurrentSectionOnly(); 3685 } 3686 if (Section->UseCodeAlign()) 3687 getStreamer().EmitCodeAlignment(2, 0); 3688 else 3689 getStreamer().EmitValueToAlignment(2, 0, 1, 0); 3690 return false; 3691 } 3692 3693 /// ParseDirectiveCode 3694 /// ::= .code16 | .code32 | .code64 3695 bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) { 3696 MCAsmParser &Parser = getParser(); 3697 Code16GCC = false; 3698 if (IDVal == ".code16") { 3699 Parser.Lex(); 3700 if (!is16BitMode()) { 3701 SwitchMode(X86::Mode16Bit); 3702 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16); 3703 } 3704 } else if (IDVal == ".code16gcc") { 3705 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode. 3706 Parser.Lex(); 3707 Code16GCC = true; 3708 if (!is16BitMode()) { 3709 SwitchMode(X86::Mode16Bit); 3710 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16); 3711 } 3712 } else if (IDVal == ".code32") { 3713 Parser.Lex(); 3714 if (!is32BitMode()) { 3715 SwitchMode(X86::Mode32Bit); 3716 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32); 3717 } 3718 } else if (IDVal == ".code64") { 3719 Parser.Lex(); 3720 if (!is64BitMode()) { 3721 SwitchMode(X86::Mode64Bit); 3722 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64); 3723 } 3724 } else { 3725 Error(L, "unknown directive " + IDVal); 3726 return false; 3727 } 3728 3729 return false; 3730 } 3731 3732 // .cv_fpo_proc foo 3733 bool X86AsmParser::parseDirectiveFPOProc(SMLoc L) { 3734 MCAsmParser &Parser = getParser(); 3735 StringRef ProcName; 3736 int64_t ParamsSize; 3737 if (Parser.parseIdentifier(ProcName)) 3738 return Parser.TokError("expected symbol name"); 3739 if (Parser.parseIntToken(ParamsSize, "expected parameter byte count")) 3740 return true; 3741 if (!isUIntN(32, ParamsSize)) 3742 return Parser.TokError("parameters size out of range"); 3743 if (Parser.parseEOL("unexpected tokens")) 3744 return addErrorSuffix(" in '.cv_fpo_proc' directive"); 3745 MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); 3746 return getTargetStreamer().emitFPOProc(ProcSym, ParamsSize, L); 3747 } 3748 3749 // .cv_fpo_setframe ebp 3750 bool X86AsmParser::parseDirectiveFPOSetFrame(SMLoc L) { 3751 MCAsmParser &Parser = getParser(); 3752 unsigned Reg; 3753 SMLoc DummyLoc; 3754 if (ParseRegister(Reg, DummyLoc, DummyLoc) || 3755 Parser.parseEOL("unexpected tokens")) 3756 return addErrorSuffix(" in '.cv_fpo_setframe' directive"); 3757 return getTargetStreamer().emitFPOSetFrame(Reg, L); 3758 } 3759 3760 // .cv_fpo_pushreg ebx 3761 bool X86AsmParser::parseDirectiveFPOPushReg(SMLoc L) { 3762 MCAsmParser &Parser = getParser(); 3763 unsigned Reg; 3764 SMLoc DummyLoc; 3765 if (ParseRegister(Reg, DummyLoc, DummyLoc) || 3766 Parser.parseEOL("unexpected tokens")) 3767 return addErrorSuffix(" in '.cv_fpo_pushreg' directive"); 3768 return getTargetStreamer().emitFPOPushReg(Reg, L); 3769 } 3770 3771 // .cv_fpo_stackalloc 20 3772 bool X86AsmParser::parseDirectiveFPOStackAlloc(SMLoc L) { 3773 MCAsmParser &Parser = getParser(); 3774 int64_t Offset; 3775 if (Parser.parseIntToken(Offset, "expected offset") || 3776 Parser.parseEOL("unexpected tokens")) 3777 return addErrorSuffix(" in '.cv_fpo_stackalloc' directive"); 3778 return getTargetStreamer().emitFPOStackAlloc(Offset, L); 3779 } 3780 3781 // .cv_fpo_stackalign 8 3782 bool X86AsmParser::parseDirectiveFPOStackAlign(SMLoc L) { 3783 MCAsmParser &Parser = getParser(); 3784 int64_t Offset; 3785 if (Parser.parseIntToken(Offset, "expected offset") || 3786 Parser.parseEOL("unexpected tokens")) 3787 return addErrorSuffix(" in '.cv_fpo_stackalign' directive"); 3788 return getTargetStreamer().emitFPOStackAlign(Offset, L); 3789 } 3790 3791 // .cv_fpo_endprologue 3792 bool X86AsmParser::parseDirectiveFPOEndPrologue(SMLoc L) { 3793 MCAsmParser &Parser = getParser(); 3794 if (Parser.parseEOL("unexpected tokens")) 3795 return addErrorSuffix(" in '.cv_fpo_endprologue' directive"); 3796 return getTargetStreamer().emitFPOEndPrologue(L); 3797 } 3798 3799 // .cv_fpo_endproc 3800 bool X86AsmParser::parseDirectiveFPOEndProc(SMLoc L) { 3801 MCAsmParser &Parser = getParser(); 3802 if (Parser.parseEOL("unexpected tokens")) 3803 return addErrorSuffix(" in '.cv_fpo_endproc' directive"); 3804 return getTargetStreamer().emitFPOEndProc(L); 3805 } 3806 3807 bool X86AsmParser::parseSEHRegisterNumber(unsigned RegClassID, 3808 unsigned &RegNo) { 3809 SMLoc startLoc = getLexer().getLoc(); 3810 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 3811 3812 // Try parsing the argument as a register first. 3813 if (getLexer().getTok().isNot(AsmToken::Integer)) { 3814 SMLoc endLoc; 3815 if (ParseRegister(RegNo, startLoc, endLoc)) 3816 return true; 3817 3818 if (!X86MCRegisterClasses[RegClassID].contains(RegNo)) { 3819 return Error(startLoc, 3820 "register is not supported for use with this directive"); 3821 } 3822 } else { 3823 // Otherwise, an integer number matching the encoding of the desired 3824 // register may appear. 3825 int64_t EncodedReg; 3826 if (getParser().parseAbsoluteExpression(EncodedReg)) 3827 return true; 3828 3829 // The SEH register number is the same as the encoding register number. Map 3830 // from the encoding back to the LLVM register number. 3831 RegNo = 0; 3832 for (MCPhysReg Reg : X86MCRegisterClasses[RegClassID]) { 3833 if (MRI->getEncodingValue(Reg) == EncodedReg) { 3834 RegNo = Reg; 3835 break; 3836 } 3837 } 3838 if (RegNo == 0) { 3839 return Error(startLoc, 3840 "incorrect register number for use with this directive"); 3841 } 3842 } 3843 3844 return false; 3845 } 3846 3847 bool X86AsmParser::parseDirectiveSEHPushReg(SMLoc Loc) { 3848 unsigned Reg = 0; 3849 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 3850 return true; 3851 3852 if (getLexer().isNot(AsmToken::EndOfStatement)) 3853 return TokError("unexpected token in directive"); 3854 3855 getParser().Lex(); 3856 getStreamer().EmitWinCFIPushReg(Reg, Loc); 3857 return false; 3858 } 3859 3860 bool X86AsmParser::parseDirectiveSEHSetFrame(SMLoc Loc) { 3861 unsigned Reg = 0; 3862 int64_t Off; 3863 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 3864 return true; 3865 if (getLexer().isNot(AsmToken::Comma)) 3866 return TokError("you must specify a stack pointer offset"); 3867 3868 getParser().Lex(); 3869 if (getParser().parseAbsoluteExpression(Off)) 3870 return true; 3871 3872 if (getLexer().isNot(AsmToken::EndOfStatement)) 3873 return TokError("unexpected token in directive"); 3874 3875 getParser().Lex(); 3876 getStreamer().EmitWinCFISetFrame(Reg, Off, Loc); 3877 return false; 3878 } 3879 3880 bool X86AsmParser::parseDirectiveSEHSaveReg(SMLoc Loc) { 3881 unsigned Reg = 0; 3882 int64_t Off; 3883 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 3884 return true; 3885 if (getLexer().isNot(AsmToken::Comma)) 3886 return TokError("you must specify an offset on the stack"); 3887 3888 getParser().Lex(); 3889 if (getParser().parseAbsoluteExpression(Off)) 3890 return true; 3891 3892 if (getLexer().isNot(AsmToken::EndOfStatement)) 3893 return TokError("unexpected token in directive"); 3894 3895 getParser().Lex(); 3896 getStreamer().EmitWinCFISaveReg(Reg, Off, Loc); 3897 return false; 3898 } 3899 3900 bool X86AsmParser::parseDirectiveSEHSaveXMM(SMLoc Loc) { 3901 unsigned Reg = 0; 3902 int64_t Off; 3903 if (parseSEHRegisterNumber(X86::VR128XRegClassID, Reg)) 3904 return true; 3905 if (getLexer().isNot(AsmToken::Comma)) 3906 return TokError("you must specify an offset on the stack"); 3907 3908 getParser().Lex(); 3909 if (getParser().parseAbsoluteExpression(Off)) 3910 return true; 3911 3912 if (getLexer().isNot(AsmToken::EndOfStatement)) 3913 return TokError("unexpected token in directive"); 3914 3915 getParser().Lex(); 3916 getStreamer().EmitWinCFISaveXMM(Reg, Off, Loc); 3917 return false; 3918 } 3919 3920 bool X86AsmParser::parseDirectiveSEHPushFrame(SMLoc Loc) { 3921 bool Code = false; 3922 StringRef CodeID; 3923 if (getLexer().is(AsmToken::At)) { 3924 SMLoc startLoc = getLexer().getLoc(); 3925 getParser().Lex(); 3926 if (!getParser().parseIdentifier(CodeID)) { 3927 if (CodeID != "code") 3928 return Error(startLoc, "expected @code"); 3929 Code = true; 3930 } 3931 } 3932 3933 if (getLexer().isNot(AsmToken::EndOfStatement)) 3934 return TokError("unexpected token in directive"); 3935 3936 getParser().Lex(); 3937 getStreamer().EmitWinCFIPushFrame(Code, Loc); 3938 return false; 3939 } 3940 3941 // Force static initialization. 3942 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86AsmParser() { 3943 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target()); 3944 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target()); 3945 } 3946 3947 #define GET_REGISTER_MATCHER 3948 #define GET_MATCHER_IMPLEMENTATION 3949 #define GET_SUBTARGET_FEATURE_NAME 3950 #include "X86GenAsmMatcher.inc" 3951