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/MC/TargetRegistry.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Compiler.h" 37 #include "llvm/Support/SourceMgr.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include <algorithm> 40 #include <memory> 41 42 using namespace llvm; 43 44 static cl::opt<bool> LVIInlineAsmHardening( 45 "x86-experimental-lvi-inline-asm-hardening", 46 cl::desc("Harden inline assembly code that may be vulnerable to Load Value" 47 " Injection (LVI). This feature is experimental."), cl::Hidden); 48 49 static bool checkScale(unsigned Scale, StringRef &ErrMsg) { 50 if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) { 51 ErrMsg = "scale factor in address must be 1, 2, 4 or 8"; 52 return true; 53 } 54 return false; 55 } 56 57 namespace { 58 59 static const char OpPrecedence[] = { 60 0, // IC_OR 61 1, // IC_XOR 62 2, // IC_AND 63 4, // IC_LSHIFT 64 4, // IC_RSHIFT 65 5, // IC_PLUS 66 5, // IC_MINUS 67 6, // IC_MULTIPLY 68 6, // IC_DIVIDE 69 6, // IC_MOD 70 7, // IC_NOT 71 8, // IC_NEG 72 9, // IC_RPAREN 73 10, // IC_LPAREN 74 0, // IC_IMM 75 0, // IC_REGISTER 76 3, // IC_EQ 77 3, // IC_NE 78 3, // IC_LT 79 3, // IC_LE 80 3, // IC_GT 81 3 // IC_GE 82 }; 83 84 class X86AsmParser : public MCTargetAsmParser { 85 ParseInstructionInfo *InstInfo; 86 bool Code16GCC; 87 unsigned ForcedDataPrefix = 0; 88 89 enum VEXEncoding { 90 VEXEncoding_Default, 91 VEXEncoding_VEX, 92 VEXEncoding_VEX2, 93 VEXEncoding_VEX3, 94 VEXEncoding_EVEX, 95 }; 96 97 VEXEncoding ForcedVEXEncoding = VEXEncoding_Default; 98 99 enum DispEncoding { 100 DispEncoding_Default, 101 DispEncoding_Disp8, 102 DispEncoding_Disp32, 103 }; 104 105 DispEncoding ForcedDispEncoding = DispEncoding_Default; 106 107 private: 108 SMLoc consumeToken() { 109 MCAsmParser &Parser = getParser(); 110 SMLoc Result = Parser.getTok().getLoc(); 111 Parser.Lex(); 112 return Result; 113 } 114 115 X86TargetStreamer &getTargetStreamer() { 116 assert(getParser().getStreamer().getTargetStreamer() && 117 "do not have a target streamer"); 118 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer(); 119 return static_cast<X86TargetStreamer &>(TS); 120 } 121 122 unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst, 123 uint64_t &ErrorInfo, FeatureBitset &MissingFeatures, 124 bool matchingInlineAsm, unsigned VariantID = 0) { 125 // In Code16GCC mode, match as 32-bit. 126 if (Code16GCC) 127 SwitchMode(X86::Is32Bit); 128 unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo, 129 MissingFeatures, matchingInlineAsm, 130 VariantID); 131 if (Code16GCC) 132 SwitchMode(X86::Is16Bit); 133 return rv; 134 } 135 136 enum InfixCalculatorTok { 137 IC_OR = 0, 138 IC_XOR, 139 IC_AND, 140 IC_LSHIFT, 141 IC_RSHIFT, 142 IC_PLUS, 143 IC_MINUS, 144 IC_MULTIPLY, 145 IC_DIVIDE, 146 IC_MOD, 147 IC_NOT, 148 IC_NEG, 149 IC_RPAREN, 150 IC_LPAREN, 151 IC_IMM, 152 IC_REGISTER, 153 IC_EQ, 154 IC_NE, 155 IC_LT, 156 IC_LE, 157 IC_GT, 158 IC_GE 159 }; 160 161 enum IntelOperatorKind { 162 IOK_INVALID = 0, 163 IOK_LENGTH, 164 IOK_SIZE, 165 IOK_TYPE, 166 }; 167 168 enum MasmOperatorKind { 169 MOK_INVALID = 0, 170 MOK_LENGTHOF, 171 MOK_SIZEOF, 172 MOK_TYPE, 173 }; 174 175 class InfixCalculator { 176 typedef std::pair< InfixCalculatorTok, int64_t > ICToken; 177 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack; 178 SmallVector<ICToken, 4> PostfixStack; 179 180 bool isUnaryOperator(InfixCalculatorTok Op) const { 181 return Op == IC_NEG || Op == IC_NOT; 182 } 183 184 public: 185 int64_t popOperand() { 186 assert (!PostfixStack.empty() && "Poped an empty stack!"); 187 ICToken Op = PostfixStack.pop_back_val(); 188 if (!(Op.first == IC_IMM || Op.first == IC_REGISTER)) 189 return -1; // The invalid Scale value will be caught later by checkScale 190 return Op.second; 191 } 192 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) { 193 assert ((Op == IC_IMM || Op == IC_REGISTER) && 194 "Unexpected operand!"); 195 PostfixStack.push_back(std::make_pair(Op, Val)); 196 } 197 198 void popOperator() { InfixOperatorStack.pop_back(); } 199 void pushOperator(InfixCalculatorTok Op) { 200 // Push the new operator if the stack is empty. 201 if (InfixOperatorStack.empty()) { 202 InfixOperatorStack.push_back(Op); 203 return; 204 } 205 206 // Push the new operator if it has a higher precedence than the operator 207 // on the top of the stack or the operator on the top of the stack is a 208 // left parentheses. 209 unsigned Idx = InfixOperatorStack.size() - 1; 210 InfixCalculatorTok StackOp = InfixOperatorStack[Idx]; 211 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) { 212 InfixOperatorStack.push_back(Op); 213 return; 214 } 215 216 // The operator on the top of the stack has higher precedence than the 217 // new operator. 218 unsigned ParenCount = 0; 219 while (true) { 220 // Nothing to process. 221 if (InfixOperatorStack.empty()) 222 break; 223 224 Idx = InfixOperatorStack.size() - 1; 225 StackOp = InfixOperatorStack[Idx]; 226 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount)) 227 break; 228 229 // If we have an even parentheses count and we see a left parentheses, 230 // then stop processing. 231 if (!ParenCount && StackOp == IC_LPAREN) 232 break; 233 234 if (StackOp == IC_RPAREN) { 235 ++ParenCount; 236 InfixOperatorStack.pop_back(); 237 } else if (StackOp == IC_LPAREN) { 238 --ParenCount; 239 InfixOperatorStack.pop_back(); 240 } else { 241 InfixOperatorStack.pop_back(); 242 PostfixStack.push_back(std::make_pair(StackOp, 0)); 243 } 244 } 245 // Push the new operator. 246 InfixOperatorStack.push_back(Op); 247 } 248 249 int64_t execute() { 250 // Push any remaining operators onto the postfix stack. 251 while (!InfixOperatorStack.empty()) { 252 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val(); 253 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN) 254 PostfixStack.push_back(std::make_pair(StackOp, 0)); 255 } 256 257 if (PostfixStack.empty()) 258 return 0; 259 260 SmallVector<ICToken, 16> OperandStack; 261 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) { 262 ICToken Op = PostfixStack[i]; 263 if (Op.first == IC_IMM || Op.first == IC_REGISTER) { 264 OperandStack.push_back(Op); 265 } else if (isUnaryOperator(Op.first)) { 266 assert (OperandStack.size() > 0 && "Too few operands."); 267 ICToken Operand = OperandStack.pop_back_val(); 268 assert (Operand.first == IC_IMM && 269 "Unary operation with a register!"); 270 switch (Op.first) { 271 default: 272 report_fatal_error("Unexpected operator!"); 273 break; 274 case IC_NEG: 275 OperandStack.push_back(std::make_pair(IC_IMM, -Operand.second)); 276 break; 277 case IC_NOT: 278 OperandStack.push_back(std::make_pair(IC_IMM, ~Operand.second)); 279 break; 280 } 281 } else { 282 assert (OperandStack.size() > 1 && "Too few operands."); 283 int64_t Val; 284 ICToken Op2 = OperandStack.pop_back_val(); 285 ICToken Op1 = OperandStack.pop_back_val(); 286 switch (Op.first) { 287 default: 288 report_fatal_error("Unexpected operator!"); 289 break; 290 case IC_PLUS: 291 Val = Op1.second + Op2.second; 292 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 293 break; 294 case IC_MINUS: 295 Val = Op1.second - Op2.second; 296 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 297 break; 298 case IC_MULTIPLY: 299 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 300 "Multiply operation with an immediate and a register!"); 301 Val = Op1.second * Op2.second; 302 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 303 break; 304 case IC_DIVIDE: 305 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 306 "Divide operation with an immediate and a register!"); 307 assert (Op2.second != 0 && "Division by zero!"); 308 Val = Op1.second / Op2.second; 309 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 310 break; 311 case IC_MOD: 312 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 313 "Modulo operation with an immediate and a register!"); 314 Val = Op1.second % Op2.second; 315 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 316 break; 317 case IC_OR: 318 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 319 "Or operation with an immediate and a register!"); 320 Val = Op1.second | Op2.second; 321 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 322 break; 323 case IC_XOR: 324 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 325 "Xor operation with an immediate and a register!"); 326 Val = Op1.second ^ Op2.second; 327 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 328 break; 329 case IC_AND: 330 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 331 "And operation with an immediate and a register!"); 332 Val = Op1.second & Op2.second; 333 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 334 break; 335 case IC_LSHIFT: 336 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 337 "Left shift operation with an immediate and a register!"); 338 Val = Op1.second << Op2.second; 339 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 340 break; 341 case IC_RSHIFT: 342 assert (Op1.first == IC_IMM && Op2.first == IC_IMM && 343 "Right shift operation with an immediate and a register!"); 344 Val = Op1.second >> Op2.second; 345 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 346 break; 347 case IC_EQ: 348 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 349 "Equals operation with an immediate and a register!"); 350 Val = (Op1.second == Op2.second) ? -1 : 0; 351 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 352 break; 353 case IC_NE: 354 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 355 "Not-equals operation with an immediate and a register!"); 356 Val = (Op1.second != Op2.second) ? -1 : 0; 357 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 358 break; 359 case IC_LT: 360 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 361 "Less-than operation with an immediate and a register!"); 362 Val = (Op1.second < Op2.second) ? -1 : 0; 363 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 364 break; 365 case IC_LE: 366 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 367 "Less-than-or-equal operation with an immediate and a " 368 "register!"); 369 Val = (Op1.second <= Op2.second) ? -1 : 0; 370 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 371 break; 372 case IC_GT: 373 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 374 "Greater-than operation with an immediate and a register!"); 375 Val = (Op1.second > Op2.second) ? -1 : 0; 376 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 377 break; 378 case IC_GE: 379 assert(Op1.first == IC_IMM && Op2.first == IC_IMM && 380 "Greater-than-or-equal operation with an immediate and a " 381 "register!"); 382 Val = (Op1.second >= Op2.second) ? -1 : 0; 383 OperandStack.push_back(std::make_pair(IC_IMM, Val)); 384 break; 385 } 386 } 387 } 388 assert (OperandStack.size() == 1 && "Expected a single result."); 389 return OperandStack.pop_back_val().second; 390 } 391 }; 392 393 enum IntelExprState { 394 IES_INIT, 395 IES_OR, 396 IES_XOR, 397 IES_AND, 398 IES_EQ, 399 IES_NE, 400 IES_LT, 401 IES_LE, 402 IES_GT, 403 IES_GE, 404 IES_LSHIFT, 405 IES_RSHIFT, 406 IES_PLUS, 407 IES_MINUS, 408 IES_OFFSET, 409 IES_CAST, 410 IES_NOT, 411 IES_MULTIPLY, 412 IES_DIVIDE, 413 IES_MOD, 414 IES_LBRAC, 415 IES_RBRAC, 416 IES_LPAREN, 417 IES_RPAREN, 418 IES_REGISTER, 419 IES_INTEGER, 420 IES_IDENTIFIER, 421 IES_ERROR 422 }; 423 424 class IntelExprStateMachine { 425 IntelExprState State, PrevState; 426 unsigned BaseReg, IndexReg, TmpReg, Scale; 427 int64_t Imm; 428 const MCExpr *Sym; 429 StringRef SymName; 430 InfixCalculator IC; 431 InlineAsmIdentifierInfo Info; 432 short BracCount; 433 bool MemExpr; 434 bool OffsetOperator; 435 SMLoc OffsetOperatorLoc; 436 AsmTypeInfo CurType; 437 438 bool setSymRef(const MCExpr *Val, StringRef ID, StringRef &ErrMsg) { 439 if (Sym) { 440 ErrMsg = "cannot use more than one symbol in memory operand"; 441 return true; 442 } 443 Sym = Val; 444 SymName = ID; 445 return false; 446 } 447 448 public: 449 IntelExprStateMachine() 450 : State(IES_INIT), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), 451 TmpReg(0), Scale(0), Imm(0), Sym(nullptr), BracCount(0), 452 MemExpr(false), OffsetOperator(false) {} 453 454 void addImm(int64_t imm) { Imm += imm; } 455 short getBracCount() const { return BracCount; } 456 bool isMemExpr() const { return MemExpr; } 457 bool isOffsetOperator() const { return OffsetOperator; } 458 SMLoc getOffsetLoc() const { return OffsetOperatorLoc; } 459 unsigned getBaseReg() const { return BaseReg; } 460 unsigned getIndexReg() const { return IndexReg; } 461 unsigned getScale() const { return Scale; } 462 const MCExpr *getSym() const { return Sym; } 463 StringRef getSymName() const { return SymName; } 464 StringRef getType() const { return CurType.Name; } 465 unsigned getSize() const { return CurType.Size; } 466 unsigned getElementSize() const { return CurType.ElementSize; } 467 unsigned getLength() const { return CurType.Length; } 468 int64_t getImm() { return Imm + IC.execute(); } 469 bool isValidEndState() const { 470 return State == IES_RBRAC || State == IES_INTEGER; 471 } 472 bool hadError() const { return State == IES_ERROR; } 473 const InlineAsmIdentifierInfo &getIdentifierInfo() const { return Info; } 474 475 void onOr() { 476 IntelExprState CurrState = State; 477 switch (State) { 478 default: 479 State = IES_ERROR; 480 break; 481 case IES_INTEGER: 482 case IES_RPAREN: 483 case IES_REGISTER: 484 State = IES_OR; 485 IC.pushOperator(IC_OR); 486 break; 487 } 488 PrevState = CurrState; 489 } 490 void onXor() { 491 IntelExprState CurrState = State; 492 switch (State) { 493 default: 494 State = IES_ERROR; 495 break; 496 case IES_INTEGER: 497 case IES_RPAREN: 498 case IES_REGISTER: 499 State = IES_XOR; 500 IC.pushOperator(IC_XOR); 501 break; 502 } 503 PrevState = CurrState; 504 } 505 void onAnd() { 506 IntelExprState CurrState = State; 507 switch (State) { 508 default: 509 State = IES_ERROR; 510 break; 511 case IES_INTEGER: 512 case IES_RPAREN: 513 case IES_REGISTER: 514 State = IES_AND; 515 IC.pushOperator(IC_AND); 516 break; 517 } 518 PrevState = CurrState; 519 } 520 void onEq() { 521 IntelExprState CurrState = State; 522 switch (State) { 523 default: 524 State = IES_ERROR; 525 break; 526 case IES_INTEGER: 527 case IES_RPAREN: 528 case IES_REGISTER: 529 State = IES_EQ; 530 IC.pushOperator(IC_EQ); 531 break; 532 } 533 PrevState = CurrState; 534 } 535 void onNE() { 536 IntelExprState CurrState = State; 537 switch (State) { 538 default: 539 State = IES_ERROR; 540 break; 541 case IES_INTEGER: 542 case IES_RPAREN: 543 case IES_REGISTER: 544 State = IES_NE; 545 IC.pushOperator(IC_NE); 546 break; 547 } 548 PrevState = CurrState; 549 } 550 void onLT() { 551 IntelExprState CurrState = State; 552 switch (State) { 553 default: 554 State = IES_ERROR; 555 break; 556 case IES_INTEGER: 557 case IES_RPAREN: 558 case IES_REGISTER: 559 State = IES_LT; 560 IC.pushOperator(IC_LT); 561 break; 562 } 563 PrevState = CurrState; 564 } 565 void onLE() { 566 IntelExprState CurrState = State; 567 switch (State) { 568 default: 569 State = IES_ERROR; 570 break; 571 case IES_INTEGER: 572 case IES_RPAREN: 573 case IES_REGISTER: 574 State = IES_LE; 575 IC.pushOperator(IC_LE); 576 break; 577 } 578 PrevState = CurrState; 579 } 580 void onGT() { 581 IntelExprState CurrState = State; 582 switch (State) { 583 default: 584 State = IES_ERROR; 585 break; 586 case IES_INTEGER: 587 case IES_RPAREN: 588 case IES_REGISTER: 589 State = IES_GT; 590 IC.pushOperator(IC_GT); 591 break; 592 } 593 PrevState = CurrState; 594 } 595 void onGE() { 596 IntelExprState CurrState = State; 597 switch (State) { 598 default: 599 State = IES_ERROR; 600 break; 601 case IES_INTEGER: 602 case IES_RPAREN: 603 case IES_REGISTER: 604 State = IES_GE; 605 IC.pushOperator(IC_GE); 606 break; 607 } 608 PrevState = CurrState; 609 } 610 void onLShift() { 611 IntelExprState CurrState = State; 612 switch (State) { 613 default: 614 State = IES_ERROR; 615 break; 616 case IES_INTEGER: 617 case IES_RPAREN: 618 case IES_REGISTER: 619 State = IES_LSHIFT; 620 IC.pushOperator(IC_LSHIFT); 621 break; 622 } 623 PrevState = CurrState; 624 } 625 void onRShift() { 626 IntelExprState CurrState = State; 627 switch (State) { 628 default: 629 State = IES_ERROR; 630 break; 631 case IES_INTEGER: 632 case IES_RPAREN: 633 case IES_REGISTER: 634 State = IES_RSHIFT; 635 IC.pushOperator(IC_RSHIFT); 636 break; 637 } 638 PrevState = CurrState; 639 } 640 bool onPlus(StringRef &ErrMsg) { 641 IntelExprState CurrState = State; 642 switch (State) { 643 default: 644 State = IES_ERROR; 645 break; 646 case IES_INTEGER: 647 case IES_RPAREN: 648 case IES_REGISTER: 649 case IES_OFFSET: 650 State = IES_PLUS; 651 IC.pushOperator(IC_PLUS); 652 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 653 // If we already have a BaseReg, then assume this is the IndexReg with 654 // no explicit scale. 655 if (!BaseReg) { 656 BaseReg = TmpReg; 657 } else { 658 if (IndexReg) { 659 ErrMsg = "BaseReg/IndexReg already set!"; 660 return true; 661 } 662 IndexReg = TmpReg; 663 Scale = 0; 664 } 665 } 666 break; 667 } 668 PrevState = CurrState; 669 return false; 670 } 671 bool onMinus(StringRef &ErrMsg) { 672 IntelExprState CurrState = State; 673 switch (State) { 674 default: 675 State = IES_ERROR; 676 break; 677 case IES_OR: 678 case IES_XOR: 679 case IES_AND: 680 case IES_EQ: 681 case IES_NE: 682 case IES_LT: 683 case IES_LE: 684 case IES_GT: 685 case IES_GE: 686 case IES_LSHIFT: 687 case IES_RSHIFT: 688 case IES_PLUS: 689 case IES_NOT: 690 case IES_MULTIPLY: 691 case IES_DIVIDE: 692 case IES_MOD: 693 case IES_LPAREN: 694 case IES_RPAREN: 695 case IES_LBRAC: 696 case IES_RBRAC: 697 case IES_INTEGER: 698 case IES_REGISTER: 699 case IES_INIT: 700 case IES_OFFSET: 701 State = IES_MINUS; 702 // push minus operator if it is not a negate operator 703 if (CurrState == IES_REGISTER || CurrState == IES_RPAREN || 704 CurrState == IES_INTEGER || CurrState == IES_RBRAC || 705 CurrState == IES_OFFSET) 706 IC.pushOperator(IC_MINUS); 707 else if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) { 708 // We have negate operator for Scale: it's illegal 709 ErrMsg = "Scale can't be negative"; 710 return true; 711 } else 712 IC.pushOperator(IC_NEG); 713 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 714 // If we already have a BaseReg, then assume this is the IndexReg with 715 // no explicit scale. 716 if (!BaseReg) { 717 BaseReg = TmpReg; 718 } else { 719 if (IndexReg) { 720 ErrMsg = "BaseReg/IndexReg already set!"; 721 return true; 722 } 723 IndexReg = TmpReg; 724 Scale = 0; 725 } 726 } 727 break; 728 } 729 PrevState = CurrState; 730 return false; 731 } 732 void onNot() { 733 IntelExprState CurrState = State; 734 switch (State) { 735 default: 736 State = IES_ERROR; 737 break; 738 case IES_OR: 739 case IES_XOR: 740 case IES_AND: 741 case IES_EQ: 742 case IES_NE: 743 case IES_LT: 744 case IES_LE: 745 case IES_GT: 746 case IES_GE: 747 case IES_LSHIFT: 748 case IES_RSHIFT: 749 case IES_PLUS: 750 case IES_MINUS: 751 case IES_NOT: 752 case IES_MULTIPLY: 753 case IES_DIVIDE: 754 case IES_MOD: 755 case IES_LPAREN: 756 case IES_LBRAC: 757 case IES_INIT: 758 State = IES_NOT; 759 IC.pushOperator(IC_NOT); 760 break; 761 } 762 PrevState = CurrState; 763 } 764 bool onRegister(unsigned Reg, StringRef &ErrMsg) { 765 IntelExprState CurrState = State; 766 switch (State) { 767 default: 768 State = IES_ERROR; 769 break; 770 case IES_PLUS: 771 case IES_LPAREN: 772 case IES_LBRAC: 773 State = IES_REGISTER; 774 TmpReg = Reg; 775 IC.pushOperand(IC_REGISTER); 776 break; 777 case IES_MULTIPLY: 778 // Index Register - Scale * Register 779 if (PrevState == IES_INTEGER) { 780 if (IndexReg) { 781 ErrMsg = "BaseReg/IndexReg already set!"; 782 return true; 783 } 784 State = IES_REGISTER; 785 IndexReg = Reg; 786 // Get the scale and replace the 'Scale * Register' with '0'. 787 Scale = IC.popOperand(); 788 if (checkScale(Scale, ErrMsg)) 789 return true; 790 IC.pushOperand(IC_IMM); 791 IC.popOperator(); 792 } else { 793 State = IES_ERROR; 794 } 795 break; 796 } 797 PrevState = CurrState; 798 return false; 799 } 800 bool onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName, 801 const InlineAsmIdentifierInfo &IDInfo, 802 const AsmTypeInfo &Type, bool ParsingMSInlineAsm, 803 StringRef &ErrMsg) { 804 // InlineAsm: Treat an enum value as an integer 805 if (ParsingMSInlineAsm) 806 if (IDInfo.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) 807 return onInteger(IDInfo.Enum.EnumVal, ErrMsg); 808 // Treat a symbolic constant like an integer 809 if (auto *CE = dyn_cast<MCConstantExpr>(SymRef)) 810 return onInteger(CE->getValue(), ErrMsg); 811 PrevState = State; 812 switch (State) { 813 default: 814 State = IES_ERROR; 815 break; 816 case IES_CAST: 817 case IES_PLUS: 818 case IES_MINUS: 819 case IES_NOT: 820 case IES_INIT: 821 case IES_LBRAC: 822 case IES_LPAREN: 823 if (setSymRef(SymRef, SymRefName, ErrMsg)) 824 return true; 825 MemExpr = true; 826 State = IES_INTEGER; 827 IC.pushOperand(IC_IMM); 828 if (ParsingMSInlineAsm) 829 Info = IDInfo; 830 setTypeInfo(Type); 831 break; 832 } 833 return false; 834 } 835 bool onInteger(int64_t TmpInt, StringRef &ErrMsg) { 836 IntelExprState CurrState = State; 837 switch (State) { 838 default: 839 State = IES_ERROR; 840 break; 841 case IES_PLUS: 842 case IES_MINUS: 843 case IES_NOT: 844 case IES_OR: 845 case IES_XOR: 846 case IES_AND: 847 case IES_EQ: 848 case IES_NE: 849 case IES_LT: 850 case IES_LE: 851 case IES_GT: 852 case IES_GE: 853 case IES_LSHIFT: 854 case IES_RSHIFT: 855 case IES_DIVIDE: 856 case IES_MOD: 857 case IES_MULTIPLY: 858 case IES_LPAREN: 859 case IES_INIT: 860 case IES_LBRAC: 861 State = IES_INTEGER; 862 if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) { 863 // Index Register - Register * Scale 864 if (IndexReg) { 865 ErrMsg = "BaseReg/IndexReg already set!"; 866 return true; 867 } 868 IndexReg = TmpReg; 869 Scale = TmpInt; 870 if (checkScale(Scale, ErrMsg)) 871 return true; 872 // Get the scale and replace the 'Register * Scale' with '0'. 873 IC.popOperator(); 874 } else { 875 IC.pushOperand(IC_IMM, TmpInt); 876 } 877 break; 878 } 879 PrevState = CurrState; 880 return false; 881 } 882 void onStar() { 883 PrevState = State; 884 switch (State) { 885 default: 886 State = IES_ERROR; 887 break; 888 case IES_INTEGER: 889 case IES_REGISTER: 890 case IES_RPAREN: 891 State = IES_MULTIPLY; 892 IC.pushOperator(IC_MULTIPLY); 893 break; 894 } 895 } 896 void onDivide() { 897 PrevState = State; 898 switch (State) { 899 default: 900 State = IES_ERROR; 901 break; 902 case IES_INTEGER: 903 case IES_RPAREN: 904 State = IES_DIVIDE; 905 IC.pushOperator(IC_DIVIDE); 906 break; 907 } 908 } 909 void onMod() { 910 PrevState = State; 911 switch (State) { 912 default: 913 State = IES_ERROR; 914 break; 915 case IES_INTEGER: 916 case IES_RPAREN: 917 State = IES_MOD; 918 IC.pushOperator(IC_MOD); 919 break; 920 } 921 } 922 bool onLBrac() { 923 if (BracCount) 924 return true; 925 PrevState = State; 926 switch (State) { 927 default: 928 State = IES_ERROR; 929 break; 930 case IES_RBRAC: 931 case IES_INTEGER: 932 case IES_RPAREN: 933 State = IES_PLUS; 934 IC.pushOperator(IC_PLUS); 935 CurType.Length = 1; 936 CurType.Size = CurType.ElementSize; 937 break; 938 case IES_INIT: 939 case IES_CAST: 940 assert(!BracCount && "BracCount should be zero on parsing's start"); 941 State = IES_LBRAC; 942 break; 943 } 944 MemExpr = true; 945 BracCount++; 946 return false; 947 } 948 bool onRBrac() { 949 IntelExprState CurrState = State; 950 switch (State) { 951 default: 952 State = IES_ERROR; 953 break; 954 case IES_INTEGER: 955 case IES_OFFSET: 956 case IES_REGISTER: 957 case IES_RPAREN: 958 if (BracCount-- != 1) 959 return true; 960 State = IES_RBRAC; 961 if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) { 962 // If we already have a BaseReg, then assume this is the IndexReg with 963 // no explicit scale. 964 if (!BaseReg) { 965 BaseReg = TmpReg; 966 } else { 967 assert (!IndexReg && "BaseReg/IndexReg already set!"); 968 IndexReg = TmpReg; 969 Scale = 0; 970 } 971 } 972 break; 973 } 974 PrevState = CurrState; 975 return false; 976 } 977 void onLParen() { 978 IntelExprState CurrState = State; 979 switch (State) { 980 default: 981 State = IES_ERROR; 982 break; 983 case IES_PLUS: 984 case IES_MINUS: 985 case IES_NOT: 986 case IES_OR: 987 case IES_XOR: 988 case IES_AND: 989 case IES_EQ: 990 case IES_NE: 991 case IES_LT: 992 case IES_LE: 993 case IES_GT: 994 case IES_GE: 995 case IES_LSHIFT: 996 case IES_RSHIFT: 997 case IES_MULTIPLY: 998 case IES_DIVIDE: 999 case IES_MOD: 1000 case IES_LPAREN: 1001 case IES_INIT: 1002 case IES_LBRAC: 1003 State = IES_LPAREN; 1004 IC.pushOperator(IC_LPAREN); 1005 break; 1006 } 1007 PrevState = CurrState; 1008 } 1009 void onRParen() { 1010 PrevState = State; 1011 switch (State) { 1012 default: 1013 State = IES_ERROR; 1014 break; 1015 case IES_INTEGER: 1016 case IES_OFFSET: 1017 case IES_REGISTER: 1018 case IES_RBRAC: 1019 case IES_RPAREN: 1020 State = IES_RPAREN; 1021 IC.pushOperator(IC_RPAREN); 1022 break; 1023 } 1024 } 1025 bool onOffset(const MCExpr *Val, SMLoc OffsetLoc, StringRef ID, 1026 const InlineAsmIdentifierInfo &IDInfo, 1027 bool ParsingMSInlineAsm, StringRef &ErrMsg) { 1028 PrevState = State; 1029 switch (State) { 1030 default: 1031 ErrMsg = "unexpected offset operator expression"; 1032 return true; 1033 case IES_PLUS: 1034 case IES_INIT: 1035 case IES_LBRAC: 1036 if (setSymRef(Val, ID, ErrMsg)) 1037 return true; 1038 OffsetOperator = true; 1039 OffsetOperatorLoc = OffsetLoc; 1040 State = IES_OFFSET; 1041 // As we cannot yet resolve the actual value (offset), we retain 1042 // the requested semantics by pushing a '0' to the operands stack 1043 IC.pushOperand(IC_IMM); 1044 if (ParsingMSInlineAsm) { 1045 Info = IDInfo; 1046 } 1047 break; 1048 } 1049 return false; 1050 } 1051 void onCast(AsmTypeInfo Info) { 1052 PrevState = State; 1053 switch (State) { 1054 default: 1055 State = IES_ERROR; 1056 break; 1057 case IES_LPAREN: 1058 setTypeInfo(Info); 1059 State = IES_CAST; 1060 break; 1061 } 1062 } 1063 void setTypeInfo(AsmTypeInfo Type) { CurType = Type; } 1064 }; 1065 1066 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None, 1067 bool MatchingInlineAsm = false) { 1068 MCAsmParser &Parser = getParser(); 1069 if (MatchingInlineAsm) { 1070 if (!getLexer().isAtStartOfStatement()) 1071 Parser.eatToEndOfStatement(); 1072 return false; 1073 } 1074 return Parser.Error(L, Msg, Range); 1075 } 1076 1077 bool MatchRegisterByName(unsigned &RegNo, StringRef RegName, SMLoc StartLoc, 1078 SMLoc EndLoc); 1079 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc, 1080 bool RestoreOnFailure); 1081 1082 std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc); 1083 std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc); 1084 bool IsSIReg(unsigned Reg); 1085 unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg); 1086 void 1087 AddDefaultSrcDestOperands(OperandVector &Operands, 1088 std::unique_ptr<llvm::MCParsedAsmOperand> &&Src, 1089 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst); 1090 bool VerifyAndAdjustOperands(OperandVector &OrigOperands, 1091 OperandVector &FinalOperands); 1092 bool ParseOperand(OperandVector &Operands); 1093 bool ParseATTOperand(OperandVector &Operands); 1094 bool ParseIntelOperand(OperandVector &Operands); 1095 bool ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID, 1096 InlineAsmIdentifierInfo &Info, SMLoc &End); 1097 bool ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End); 1098 unsigned IdentifyIntelInlineAsmOperator(StringRef Name); 1099 unsigned ParseIntelInlineAsmOperator(unsigned OpKind); 1100 unsigned IdentifyMasmOperator(StringRef Name); 1101 bool ParseMasmOperator(unsigned OpKind, int64_t &Val); 1102 bool ParseRoundingModeOp(SMLoc Start, OperandVector &Operands); 1103 bool ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM, 1104 bool &ParseError, SMLoc &End); 1105 bool ParseMasmNamedOperator(StringRef Name, IntelExprStateMachine &SM, 1106 bool &ParseError, SMLoc &End); 1107 void RewriteIntelExpression(IntelExprStateMachine &SM, SMLoc Start, 1108 SMLoc End); 1109 bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End); 1110 bool ParseIntelInlineAsmIdentifier(const MCExpr *&Val, StringRef &Identifier, 1111 InlineAsmIdentifierInfo &Info, 1112 bool IsUnevaluatedOperand, SMLoc &End, 1113 bool IsParsingOffsetOperator = false); 1114 1115 bool ParseMemOperand(unsigned SegReg, const MCExpr *Disp, SMLoc StartLoc, 1116 SMLoc EndLoc, OperandVector &Operands); 1117 1118 X86::CondCode ParseConditionCode(StringRef CCode); 1119 1120 bool ParseIntelMemoryOperandSize(unsigned &Size); 1121 bool CreateMemForMSInlineAsm(unsigned SegReg, const MCExpr *Disp, 1122 unsigned BaseReg, unsigned IndexReg, 1123 unsigned Scale, SMLoc Start, SMLoc End, 1124 unsigned Size, StringRef Identifier, 1125 const InlineAsmIdentifierInfo &Info, 1126 OperandVector &Operands); 1127 1128 bool parseDirectiveArch(); 1129 bool parseDirectiveNops(SMLoc L); 1130 bool parseDirectiveEven(SMLoc L); 1131 bool ParseDirectiveCode(StringRef IDVal, SMLoc L); 1132 1133 /// CodeView FPO data directives. 1134 bool parseDirectiveFPOProc(SMLoc L); 1135 bool parseDirectiveFPOSetFrame(SMLoc L); 1136 bool parseDirectiveFPOPushReg(SMLoc L); 1137 bool parseDirectiveFPOStackAlloc(SMLoc L); 1138 bool parseDirectiveFPOStackAlign(SMLoc L); 1139 bool parseDirectiveFPOEndPrologue(SMLoc L); 1140 bool parseDirectiveFPOEndProc(SMLoc L); 1141 1142 /// SEH directives. 1143 bool parseSEHRegisterNumber(unsigned RegClassID, unsigned &RegNo); 1144 bool parseDirectiveSEHPushReg(SMLoc); 1145 bool parseDirectiveSEHSetFrame(SMLoc); 1146 bool parseDirectiveSEHSaveReg(SMLoc); 1147 bool parseDirectiveSEHSaveXMM(SMLoc); 1148 bool parseDirectiveSEHPushFrame(SMLoc); 1149 1150 unsigned checkTargetMatchPredicate(MCInst &Inst) override; 1151 1152 bool validateInstruction(MCInst &Inst, const OperandVector &Ops); 1153 bool processInstruction(MCInst &Inst, const OperandVector &Ops); 1154 1155 // Load Value Injection (LVI) Mitigations for machine code 1156 void emitWarningForSpecialLVIInstruction(SMLoc Loc); 1157 void applyLVICFIMitigation(MCInst &Inst, MCStreamer &Out); 1158 void applyLVILoadHardeningMitigation(MCInst &Inst, MCStreamer &Out); 1159 1160 /// Wrapper around MCStreamer::emitInstruction(). Possibly adds 1161 /// instrumentation around Inst. 1162 void emitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out); 1163 1164 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 1165 OperandVector &Operands, MCStreamer &Out, 1166 uint64_t &ErrorInfo, 1167 bool MatchingInlineAsm) override; 1168 1169 void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands, 1170 MCStreamer &Out, bool MatchingInlineAsm); 1171 1172 bool ErrorMissingFeature(SMLoc IDLoc, const FeatureBitset &MissingFeatures, 1173 bool MatchingInlineAsm); 1174 1175 bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode, 1176 OperandVector &Operands, MCStreamer &Out, 1177 uint64_t &ErrorInfo, 1178 bool MatchingInlineAsm); 1179 1180 bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode, 1181 OperandVector &Operands, MCStreamer &Out, 1182 uint64_t &ErrorInfo, 1183 bool MatchingInlineAsm); 1184 1185 bool OmitRegisterFromClobberLists(unsigned RegNo) override; 1186 1187 /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z}) 1188 /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required. 1189 /// return false if no parsing errors occurred, true otherwise. 1190 bool HandleAVX512Operand(OperandVector &Operands); 1191 1192 bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc); 1193 1194 bool is64BitMode() const { 1195 // FIXME: Can tablegen auto-generate this? 1196 return getSTI().getFeatureBits()[X86::Is64Bit]; 1197 } 1198 bool is32BitMode() const { 1199 // FIXME: Can tablegen auto-generate this? 1200 return getSTI().getFeatureBits()[X86::Is32Bit]; 1201 } 1202 bool is16BitMode() const { 1203 // FIXME: Can tablegen auto-generate this? 1204 return getSTI().getFeatureBits()[X86::Is16Bit]; 1205 } 1206 void SwitchMode(unsigned mode) { 1207 MCSubtargetInfo &STI = copySTI(); 1208 FeatureBitset AllModes({X86::Is64Bit, X86::Is32Bit, X86::Is16Bit}); 1209 FeatureBitset OldMode = STI.getFeatureBits() & AllModes; 1210 FeatureBitset FB = ComputeAvailableFeatures( 1211 STI.ToggleFeature(OldMode.flip(mode))); 1212 setAvailableFeatures(FB); 1213 1214 assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes)); 1215 } 1216 1217 unsigned getPointerWidth() { 1218 if (is16BitMode()) return 16; 1219 if (is32BitMode()) return 32; 1220 if (is64BitMode()) return 64; 1221 llvm_unreachable("invalid mode"); 1222 } 1223 1224 bool isParsingIntelSyntax() { 1225 return getParser().getAssemblerDialect(); 1226 } 1227 1228 /// @name Auto-generated Matcher Functions 1229 /// { 1230 1231 #define GET_ASSEMBLER_HEADER 1232 #include "X86GenAsmMatcher.inc" 1233 1234 /// } 1235 1236 public: 1237 enum X86MatchResultTy { 1238 Match_Unsupported = FIRST_TARGET_MATCH_RESULT_TY, 1239 #define GET_OPERAND_DIAGNOSTIC_TYPES 1240 #include "X86GenAsmMatcher.inc" 1241 }; 1242 1243 X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser, 1244 const MCInstrInfo &mii, const MCTargetOptions &Options) 1245 : MCTargetAsmParser(Options, sti, mii), InstInfo(nullptr), 1246 Code16GCC(false) { 1247 1248 Parser.addAliasForDirective(".word", ".2byte"); 1249 1250 // Initialize the set of available features. 1251 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); 1252 } 1253 1254 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; 1255 OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc, 1256 SMLoc &EndLoc) override; 1257 1258 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; 1259 1260 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 1261 SMLoc NameLoc, OperandVector &Operands) override; 1262 1263 bool ParseDirective(AsmToken DirectiveID) override; 1264 }; 1265 } // end anonymous namespace 1266 1267 /// @name Auto-generated Match Functions 1268 /// { 1269 1270 static unsigned MatchRegisterName(StringRef Name); 1271 1272 /// } 1273 1274 static bool CheckBaseRegAndIndexRegAndScale(unsigned BaseReg, unsigned IndexReg, 1275 unsigned Scale, bool Is64BitMode, 1276 StringRef &ErrMsg) { 1277 // If we have both a base register and an index register make sure they are 1278 // both 64-bit or 32-bit registers. 1279 // To support VSIB, IndexReg can be 128-bit or 256-bit registers. 1280 1281 if (BaseReg != 0 && 1282 !(BaseReg == X86::RIP || BaseReg == X86::EIP || 1283 X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) || 1284 X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) || 1285 X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg))) { 1286 ErrMsg = "invalid base+index expression"; 1287 return true; 1288 } 1289 1290 if (IndexReg != 0 && 1291 !(IndexReg == X86::EIZ || IndexReg == X86::RIZ || 1292 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1293 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1294 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) || 1295 X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) || 1296 X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) || 1297 X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg))) { 1298 ErrMsg = "invalid base+index expression"; 1299 return true; 1300 } 1301 1302 if (((BaseReg == X86::RIP || BaseReg == X86::EIP) && IndexReg != 0) || 1303 IndexReg == X86::EIP || IndexReg == X86::RIP || 1304 IndexReg == X86::ESP || IndexReg == X86::RSP) { 1305 ErrMsg = "invalid base+index expression"; 1306 return true; 1307 } 1308 1309 // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed, 1310 // and then only in non-64-bit modes. 1311 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) && 1312 (Is64BitMode || (BaseReg != X86::BX && BaseReg != X86::BP && 1313 BaseReg != X86::SI && BaseReg != X86::DI))) { 1314 ErrMsg = "invalid 16-bit base register"; 1315 return true; 1316 } 1317 1318 if (BaseReg == 0 && 1319 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) { 1320 ErrMsg = "16-bit memory operand may not include only index register"; 1321 return true; 1322 } 1323 1324 if (BaseReg != 0 && IndexReg != 0) { 1325 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) && 1326 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1327 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1328 IndexReg == X86::EIZ)) { 1329 ErrMsg = "base register is 64-bit, but index register is not"; 1330 return true; 1331 } 1332 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) && 1333 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) || 1334 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) || 1335 IndexReg == X86::RIZ)) { 1336 ErrMsg = "base register is 32-bit, but index register is not"; 1337 return true; 1338 } 1339 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) { 1340 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) || 1341 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) { 1342 ErrMsg = "base register is 16-bit, but index register is not"; 1343 return true; 1344 } 1345 if ((BaseReg != X86::BX && BaseReg != X86::BP) || 1346 (IndexReg != X86::SI && IndexReg != X86::DI)) { 1347 ErrMsg = "invalid 16-bit base/index register combination"; 1348 return true; 1349 } 1350 } 1351 } 1352 1353 // RIP/EIP-relative addressing is only supported in 64-bit mode. 1354 if (!Is64BitMode && BaseReg != 0 && 1355 (BaseReg == X86::RIP || BaseReg == X86::EIP)) { 1356 ErrMsg = "IP-relative addressing requires 64-bit mode"; 1357 return true; 1358 } 1359 1360 return checkScale(Scale, ErrMsg); 1361 } 1362 1363 bool X86AsmParser::MatchRegisterByName(unsigned &RegNo, StringRef RegName, 1364 SMLoc StartLoc, SMLoc EndLoc) { 1365 // If we encounter a %, ignore it. This code handles registers with and 1366 // without the prefix, unprefixed registers can occur in cfi directives. 1367 RegName.consume_front("%"); 1368 1369 RegNo = MatchRegisterName(RegName); 1370 1371 // If the match failed, try the register name as lowercase. 1372 if (RegNo == 0) 1373 RegNo = MatchRegisterName(RegName.lower()); 1374 1375 // The "flags" and "mxcsr" registers cannot be referenced directly. 1376 // Treat it as an identifier instead. 1377 if (isParsingMSInlineAsm() && isParsingIntelSyntax() && 1378 (RegNo == X86::EFLAGS || RegNo == X86::MXCSR)) 1379 RegNo = 0; 1380 1381 if (!is64BitMode()) { 1382 // FIXME: This should be done using Requires<Not64BitMode> and 1383 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also 1384 // checked. 1385 if (RegNo == X86::RIZ || RegNo == X86::RIP || 1386 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) || 1387 X86II::isX86_64NonExtLowByteReg(RegNo) || 1388 X86II::isX86_64ExtendedReg(RegNo)) { 1389 return Error(StartLoc, 1390 "register %" + RegName + " is only available in 64-bit mode", 1391 SMRange(StartLoc, EndLoc)); 1392 } 1393 } 1394 1395 // If this is "db[0-15]", match it as an alias 1396 // for dr[0-15]. 1397 if (RegNo == 0 && RegName.startswith("db")) { 1398 if (RegName.size() == 3) { 1399 switch (RegName[2]) { 1400 case '0': 1401 RegNo = X86::DR0; 1402 break; 1403 case '1': 1404 RegNo = X86::DR1; 1405 break; 1406 case '2': 1407 RegNo = X86::DR2; 1408 break; 1409 case '3': 1410 RegNo = X86::DR3; 1411 break; 1412 case '4': 1413 RegNo = X86::DR4; 1414 break; 1415 case '5': 1416 RegNo = X86::DR5; 1417 break; 1418 case '6': 1419 RegNo = X86::DR6; 1420 break; 1421 case '7': 1422 RegNo = X86::DR7; 1423 break; 1424 case '8': 1425 RegNo = X86::DR8; 1426 break; 1427 case '9': 1428 RegNo = X86::DR9; 1429 break; 1430 } 1431 } else if (RegName.size() == 4 && RegName[2] == '1') { 1432 switch (RegName[3]) { 1433 case '0': 1434 RegNo = X86::DR10; 1435 break; 1436 case '1': 1437 RegNo = X86::DR11; 1438 break; 1439 case '2': 1440 RegNo = X86::DR12; 1441 break; 1442 case '3': 1443 RegNo = X86::DR13; 1444 break; 1445 case '4': 1446 RegNo = X86::DR14; 1447 break; 1448 case '5': 1449 RegNo = X86::DR15; 1450 break; 1451 } 1452 } 1453 } 1454 1455 if (RegNo == 0) { 1456 if (isParsingIntelSyntax()) 1457 return true; 1458 return Error(StartLoc, "invalid register name", SMRange(StartLoc, EndLoc)); 1459 } 1460 return false; 1461 } 1462 1463 bool X86AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, 1464 SMLoc &EndLoc, bool RestoreOnFailure) { 1465 MCAsmParser &Parser = getParser(); 1466 MCAsmLexer &Lexer = getLexer(); 1467 RegNo = 0; 1468 1469 SmallVector<AsmToken, 5> Tokens; 1470 auto OnFailure = [RestoreOnFailure, &Lexer, &Tokens]() { 1471 if (RestoreOnFailure) { 1472 while (!Tokens.empty()) { 1473 Lexer.UnLex(Tokens.pop_back_val()); 1474 } 1475 } 1476 }; 1477 1478 const AsmToken &PercentTok = Parser.getTok(); 1479 StartLoc = PercentTok.getLoc(); 1480 1481 // If we encounter a %, ignore it. This code handles registers with and 1482 // without the prefix, unprefixed registers can occur in cfi directives. 1483 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent)) { 1484 Tokens.push_back(PercentTok); 1485 Parser.Lex(); // Eat percent token. 1486 } 1487 1488 const AsmToken &Tok = Parser.getTok(); 1489 EndLoc = Tok.getEndLoc(); 1490 1491 if (Tok.isNot(AsmToken::Identifier)) { 1492 OnFailure(); 1493 if (isParsingIntelSyntax()) return true; 1494 return Error(StartLoc, "invalid register name", 1495 SMRange(StartLoc, EndLoc)); 1496 } 1497 1498 if (MatchRegisterByName(RegNo, Tok.getString(), StartLoc, EndLoc)) { 1499 OnFailure(); 1500 return true; 1501 } 1502 1503 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens. 1504 if (RegNo == X86::ST0) { 1505 Tokens.push_back(Tok); 1506 Parser.Lex(); // Eat 'st' 1507 1508 // Check to see if we have '(4)' after %st. 1509 if (Lexer.isNot(AsmToken::LParen)) 1510 return false; 1511 // Lex the paren. 1512 Tokens.push_back(Parser.getTok()); 1513 Parser.Lex(); 1514 1515 const AsmToken &IntTok = Parser.getTok(); 1516 if (IntTok.isNot(AsmToken::Integer)) { 1517 OnFailure(); 1518 return Error(IntTok.getLoc(), "expected stack index"); 1519 } 1520 switch (IntTok.getIntVal()) { 1521 case 0: RegNo = X86::ST0; break; 1522 case 1: RegNo = X86::ST1; break; 1523 case 2: RegNo = X86::ST2; break; 1524 case 3: RegNo = X86::ST3; break; 1525 case 4: RegNo = X86::ST4; break; 1526 case 5: RegNo = X86::ST5; break; 1527 case 6: RegNo = X86::ST6; break; 1528 case 7: RegNo = X86::ST7; break; 1529 default: 1530 OnFailure(); 1531 return Error(IntTok.getLoc(), "invalid stack index"); 1532 } 1533 1534 // Lex IntTok 1535 Tokens.push_back(IntTok); 1536 Parser.Lex(); 1537 if (Lexer.isNot(AsmToken::RParen)) { 1538 OnFailure(); 1539 return Error(Parser.getTok().getLoc(), "expected ')'"); 1540 } 1541 1542 EndLoc = Parser.getTok().getEndLoc(); 1543 Parser.Lex(); // Eat ')' 1544 return false; 1545 } 1546 1547 EndLoc = Parser.getTok().getEndLoc(); 1548 1549 if (RegNo == 0) { 1550 OnFailure(); 1551 if (isParsingIntelSyntax()) return true; 1552 return Error(StartLoc, "invalid register name", 1553 SMRange(StartLoc, EndLoc)); 1554 } 1555 1556 Parser.Lex(); // Eat identifier token. 1557 return false; 1558 } 1559 1560 bool X86AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, 1561 SMLoc &EndLoc) { 1562 return ParseRegister(RegNo, StartLoc, EndLoc, /*RestoreOnFailure=*/false); 1563 } 1564 1565 OperandMatchResultTy X86AsmParser::tryParseRegister(unsigned &RegNo, 1566 SMLoc &StartLoc, 1567 SMLoc &EndLoc) { 1568 bool Result = 1569 ParseRegister(RegNo, StartLoc, EndLoc, /*RestoreOnFailure=*/true); 1570 bool PendingErrors = getParser().hasPendingError(); 1571 getParser().clearPendingErrors(); 1572 if (PendingErrors) 1573 return MatchOperand_ParseFail; 1574 if (Result) 1575 return MatchOperand_NoMatch; 1576 return MatchOperand_Success; 1577 } 1578 1579 std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) { 1580 bool Parse32 = is32BitMode() || Code16GCC; 1581 unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI); 1582 const MCExpr *Disp = MCConstantExpr::create(0, getContext()); 1583 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp, 1584 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1, 1585 Loc, Loc, 0); 1586 } 1587 1588 std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) { 1589 bool Parse32 = is32BitMode() || Code16GCC; 1590 unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI); 1591 const MCExpr *Disp = MCConstantExpr::create(0, getContext()); 1592 return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp, 1593 /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1, 1594 Loc, Loc, 0); 1595 } 1596 1597 bool X86AsmParser::IsSIReg(unsigned Reg) { 1598 switch (Reg) { 1599 default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!"); 1600 case X86::RSI: 1601 case X86::ESI: 1602 case X86::SI: 1603 return true; 1604 case X86::RDI: 1605 case X86::EDI: 1606 case X86::DI: 1607 return false; 1608 } 1609 } 1610 1611 unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, 1612 bool IsSIReg) { 1613 switch (RegClassID) { 1614 default: llvm_unreachable("Unexpected register class"); 1615 case X86::GR64RegClassID: 1616 return IsSIReg ? X86::RSI : X86::RDI; 1617 case X86::GR32RegClassID: 1618 return IsSIReg ? X86::ESI : X86::EDI; 1619 case X86::GR16RegClassID: 1620 return IsSIReg ? X86::SI : X86::DI; 1621 } 1622 } 1623 1624 void X86AsmParser::AddDefaultSrcDestOperands( 1625 OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src, 1626 std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) { 1627 if (isParsingIntelSyntax()) { 1628 Operands.push_back(std::move(Dst)); 1629 Operands.push_back(std::move(Src)); 1630 } 1631 else { 1632 Operands.push_back(std::move(Src)); 1633 Operands.push_back(std::move(Dst)); 1634 } 1635 } 1636 1637 bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands, 1638 OperandVector &FinalOperands) { 1639 1640 if (OrigOperands.size() > 1) { 1641 // Check if sizes match, OrigOperands also contains the instruction name 1642 assert(OrigOperands.size() == FinalOperands.size() + 1 && 1643 "Operand size mismatch"); 1644 1645 SmallVector<std::pair<SMLoc, std::string>, 2> Warnings; 1646 // Verify types match 1647 int RegClassID = -1; 1648 for (unsigned int i = 0; i < FinalOperands.size(); ++i) { 1649 X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]); 1650 X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]); 1651 1652 if (FinalOp.isReg() && 1653 (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg())) 1654 // Return false and let a normal complaint about bogus operands happen 1655 return false; 1656 1657 if (FinalOp.isMem()) { 1658 1659 if (!OrigOp.isMem()) 1660 // Return false and let a normal complaint about bogus operands happen 1661 return false; 1662 1663 unsigned OrigReg = OrigOp.Mem.BaseReg; 1664 unsigned FinalReg = FinalOp.Mem.BaseReg; 1665 1666 // If we've already encounterd a register class, make sure all register 1667 // bases are of the same register class 1668 if (RegClassID != -1 && 1669 !X86MCRegisterClasses[RegClassID].contains(OrigReg)) { 1670 return Error(OrigOp.getStartLoc(), 1671 "mismatching source and destination index registers"); 1672 } 1673 1674 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg)) 1675 RegClassID = X86::GR64RegClassID; 1676 else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg)) 1677 RegClassID = X86::GR32RegClassID; 1678 else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg)) 1679 RegClassID = X86::GR16RegClassID; 1680 else 1681 // Unexpected register class type 1682 // Return false and let a normal complaint about bogus operands happen 1683 return false; 1684 1685 bool IsSI = IsSIReg(FinalReg); 1686 FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI); 1687 1688 if (FinalReg != OrigReg) { 1689 std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI"; 1690 Warnings.push_back(std::make_pair( 1691 OrigOp.getStartLoc(), 1692 "memory operand is only for determining the size, " + RegName + 1693 " will be used for the location")); 1694 } 1695 1696 FinalOp.Mem.Size = OrigOp.Mem.Size; 1697 FinalOp.Mem.SegReg = OrigOp.Mem.SegReg; 1698 FinalOp.Mem.BaseReg = FinalReg; 1699 } 1700 } 1701 1702 // Produce warnings only if all the operands passed the adjustment - prevent 1703 // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings 1704 for (auto &WarningMsg : Warnings) { 1705 Warning(WarningMsg.first, WarningMsg.second); 1706 } 1707 1708 // Remove old operands 1709 for (unsigned int i = 0; i < FinalOperands.size(); ++i) 1710 OrigOperands.pop_back(); 1711 } 1712 // OrigOperands.append(FinalOperands.begin(), FinalOperands.end()); 1713 for (unsigned int i = 0; i < FinalOperands.size(); ++i) 1714 OrigOperands.push_back(std::move(FinalOperands[i])); 1715 1716 return false; 1717 } 1718 1719 bool X86AsmParser::ParseOperand(OperandVector &Operands) { 1720 if (isParsingIntelSyntax()) 1721 return ParseIntelOperand(Operands); 1722 1723 return ParseATTOperand(Operands); 1724 } 1725 1726 bool X86AsmParser::CreateMemForMSInlineAsm( 1727 unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg, 1728 unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier, 1729 const InlineAsmIdentifierInfo &Info, OperandVector &Operands) { 1730 // If we found a decl other than a VarDecl, then assume it is a FuncDecl or 1731 // some other label reference. 1732 if (Info.isKind(InlineAsmIdentifierInfo::IK_Label)) { 1733 // Insert an explicit size if the user didn't have one. 1734 if (!Size) { 1735 Size = getPointerWidth(); 1736 InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start, 1737 /*Len=*/0, Size); 1738 } 1739 // Create an absolute memory reference in order to match against 1740 // instructions taking a PC relative operand. 1741 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), Disp, Start, 1742 End, Size, Identifier, 1743 Info.Label.Decl)); 1744 return false; 1745 } 1746 // We either have a direct symbol reference, or an offset from a symbol. The 1747 // parser always puts the symbol on the LHS, so look there for size 1748 // calculation purposes. 1749 unsigned FrontendSize = 0; 1750 void *Decl = nullptr; 1751 bool IsGlobalLV = false; 1752 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 1753 // Size is in terms of bits in this context. 1754 FrontendSize = Info.Var.Type * 8; 1755 Decl = Info.Var.Decl; 1756 IsGlobalLV = Info.Var.IsGlobalLV; 1757 } 1758 // It is widely common for MS InlineAsm to use a global variable and one/two 1759 // registers in a mmory expression, and though unaccessible via rip/eip. 1760 if (IsGlobalLV && (BaseReg || IndexReg)) { 1761 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), Disp, Start, 1762 End, Size, Identifier, Decl, 0, 1763 BaseReg && IndexReg)); 1764 return false; 1765 } 1766 // Otherwise, we set the base register to a non-zero value 1767 // if we don't know the actual value at this time. This is necessary to 1768 // get the matching correct in some cases. 1769 BaseReg = BaseReg ? BaseReg : 1; 1770 Operands.push_back(X86Operand::CreateMem( 1771 getPointerWidth(), SegReg, Disp, BaseReg, IndexReg, Scale, Start, End, 1772 Size, 1773 /*DefaultBaseReg=*/X86::RIP, Identifier, Decl, FrontendSize)); 1774 return false; 1775 } 1776 1777 // Some binary bitwise operators have a named synonymous 1778 // Query a candidate string for being such a named operator 1779 // and if so - invoke the appropriate handler 1780 bool X86AsmParser::ParseIntelNamedOperator(StringRef Name, 1781 IntelExprStateMachine &SM, 1782 bool &ParseError, SMLoc &End) { 1783 // A named operator should be either lower or upper case, but not a mix... 1784 // except in MASM, which uses full case-insensitivity. 1785 if (Name.compare(Name.lower()) && Name.compare(Name.upper()) && 1786 !getParser().isParsingMasm()) 1787 return false; 1788 if (Name.equals_insensitive("not")) { 1789 SM.onNot(); 1790 } else if (Name.equals_insensitive("or")) { 1791 SM.onOr(); 1792 } else if (Name.equals_insensitive("shl")) { 1793 SM.onLShift(); 1794 } else if (Name.equals_insensitive("shr")) { 1795 SM.onRShift(); 1796 } else if (Name.equals_insensitive("xor")) { 1797 SM.onXor(); 1798 } else if (Name.equals_insensitive("and")) { 1799 SM.onAnd(); 1800 } else if (Name.equals_insensitive("mod")) { 1801 SM.onMod(); 1802 } else if (Name.equals_insensitive("offset")) { 1803 SMLoc OffsetLoc = getTok().getLoc(); 1804 const MCExpr *Val = nullptr; 1805 StringRef ID; 1806 InlineAsmIdentifierInfo Info; 1807 ParseError = ParseIntelOffsetOperator(Val, ID, Info, End); 1808 if (ParseError) 1809 return true; 1810 StringRef ErrMsg; 1811 ParseError = 1812 SM.onOffset(Val, OffsetLoc, ID, Info, isParsingMSInlineAsm(), ErrMsg); 1813 if (ParseError) 1814 return Error(SMLoc::getFromPointer(Name.data()), ErrMsg); 1815 } else { 1816 return false; 1817 } 1818 if (!Name.equals_insensitive("offset")) 1819 End = consumeToken(); 1820 return true; 1821 } 1822 bool X86AsmParser::ParseMasmNamedOperator(StringRef Name, 1823 IntelExprStateMachine &SM, 1824 bool &ParseError, SMLoc &End) { 1825 if (Name.equals_insensitive("eq")) { 1826 SM.onEq(); 1827 } else if (Name.equals_insensitive("ne")) { 1828 SM.onNE(); 1829 } else if (Name.equals_insensitive("lt")) { 1830 SM.onLT(); 1831 } else if (Name.equals_insensitive("le")) { 1832 SM.onLE(); 1833 } else if (Name.equals_insensitive("gt")) { 1834 SM.onGT(); 1835 } else if (Name.equals_insensitive("ge")) { 1836 SM.onGE(); 1837 } else { 1838 return false; 1839 } 1840 End = consumeToken(); 1841 return true; 1842 } 1843 1844 bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) { 1845 MCAsmParser &Parser = getParser(); 1846 StringRef ErrMsg; 1847 1848 AsmToken::TokenKind PrevTK = AsmToken::Error; 1849 bool Done = false; 1850 while (!Done) { 1851 // Get a fresh reference on each loop iteration in case the previous 1852 // iteration moved the token storage during UnLex(). 1853 const AsmToken &Tok = Parser.getTok(); 1854 1855 bool UpdateLocLex = true; 1856 AsmToken::TokenKind TK = getLexer().getKind(); 1857 1858 switch (TK) { 1859 default: 1860 if ((Done = SM.isValidEndState())) 1861 break; 1862 return Error(Tok.getLoc(), "unknown token in expression"); 1863 case AsmToken::Error: 1864 return Error(getLexer().getErrLoc(), getLexer().getErr()); 1865 break; 1866 case AsmToken::EndOfStatement: 1867 Done = true; 1868 break; 1869 case AsmToken::Real: 1870 // DotOperator: [ebx].0 1871 UpdateLocLex = false; 1872 if (ParseIntelDotOperator(SM, End)) 1873 return true; 1874 break; 1875 case AsmToken::Dot: 1876 if (!Parser.isParsingMasm()) { 1877 if ((Done = SM.isValidEndState())) 1878 break; 1879 return Error(Tok.getLoc(), "unknown token in expression"); 1880 } 1881 // MASM allows spaces around the dot operator (e.g., "var . x") 1882 Lex(); 1883 UpdateLocLex = false; 1884 if (ParseIntelDotOperator(SM, End)) 1885 return true; 1886 break; 1887 case AsmToken::Dollar: 1888 if (!Parser.isParsingMasm()) { 1889 if ((Done = SM.isValidEndState())) 1890 break; 1891 return Error(Tok.getLoc(), "unknown token in expression"); 1892 } 1893 LLVM_FALLTHROUGH; 1894 case AsmToken::String: { 1895 if (Parser.isParsingMasm()) { 1896 // MASM parsers handle strings in expressions as constants. 1897 SMLoc ValueLoc = Tok.getLoc(); 1898 int64_t Res; 1899 const MCExpr *Val; 1900 if (Parser.parsePrimaryExpr(Val, End, nullptr)) 1901 return true; 1902 UpdateLocLex = false; 1903 if (!Val->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) 1904 return Error(ValueLoc, "expected absolute value"); 1905 if (SM.onInteger(Res, ErrMsg)) 1906 return Error(ValueLoc, ErrMsg); 1907 break; 1908 } 1909 LLVM_FALLTHROUGH; 1910 } 1911 case AsmToken::At: 1912 case AsmToken::Identifier: { 1913 SMLoc IdentLoc = Tok.getLoc(); 1914 StringRef Identifier = Tok.getString(); 1915 UpdateLocLex = false; 1916 if (Parser.isParsingMasm()) { 1917 size_t DotOffset = Identifier.find_first_of('.'); 1918 if (DotOffset != StringRef::npos) { 1919 consumeToken(); 1920 StringRef LHS = Identifier.slice(0, DotOffset); 1921 StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1); 1922 StringRef RHS = Identifier.slice(DotOffset + 1, StringRef::npos); 1923 if (!RHS.empty()) { 1924 getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS)); 1925 } 1926 getLexer().UnLex(AsmToken(AsmToken::Dot, Dot)); 1927 if (!LHS.empty()) { 1928 getLexer().UnLex(AsmToken(AsmToken::Identifier, LHS)); 1929 } 1930 break; 1931 } 1932 } 1933 // (MASM only) <TYPE> PTR operator 1934 if (Parser.isParsingMasm()) { 1935 const AsmToken &NextTok = getLexer().peekTok(); 1936 if (NextTok.is(AsmToken::Identifier) && 1937 NextTok.getIdentifier().equals_insensitive("ptr")) { 1938 AsmTypeInfo Info; 1939 if (Parser.lookUpType(Identifier, Info)) 1940 return Error(Tok.getLoc(), "unknown type"); 1941 SM.onCast(Info); 1942 // Eat type and PTR. 1943 consumeToken(); 1944 End = consumeToken(); 1945 break; 1946 } 1947 } 1948 // Register, or (MASM only) <register>.<field> 1949 unsigned Reg; 1950 if (Tok.is(AsmToken::Identifier)) { 1951 if (!ParseRegister(Reg, IdentLoc, End, /*RestoreOnFailure=*/true)) { 1952 if (SM.onRegister(Reg, ErrMsg)) 1953 return Error(IdentLoc, ErrMsg); 1954 break; 1955 } 1956 if (Parser.isParsingMasm()) { 1957 const std::pair<StringRef, StringRef> IDField = 1958 Tok.getString().split('.'); 1959 const StringRef ID = IDField.first, Field = IDField.second; 1960 SMLoc IDEndLoc = SMLoc::getFromPointer(ID.data() + ID.size()); 1961 if (!Field.empty() && 1962 !MatchRegisterByName(Reg, ID, IdentLoc, IDEndLoc)) { 1963 if (SM.onRegister(Reg, ErrMsg)) 1964 return Error(IdentLoc, ErrMsg); 1965 1966 AsmFieldInfo Info; 1967 SMLoc FieldStartLoc = SMLoc::getFromPointer(Field.data()); 1968 if (Parser.lookUpField(Field, Info)) 1969 return Error(FieldStartLoc, "unknown offset"); 1970 else if (SM.onPlus(ErrMsg)) 1971 return Error(getTok().getLoc(), ErrMsg); 1972 else if (SM.onInteger(Info.Offset, ErrMsg)) 1973 return Error(IdentLoc, ErrMsg); 1974 SM.setTypeInfo(Info.Type); 1975 1976 End = consumeToken(); 1977 break; 1978 } 1979 } 1980 } 1981 // Operator synonymous ("not", "or" etc.) 1982 bool ParseError = false; 1983 if (ParseIntelNamedOperator(Identifier, SM, ParseError, End)) { 1984 if (ParseError) 1985 return true; 1986 break; 1987 } 1988 if (Parser.isParsingMasm() && 1989 ParseMasmNamedOperator(Identifier, SM, ParseError, End)) { 1990 if (ParseError) 1991 return true; 1992 break; 1993 } 1994 // Symbol reference, when parsing assembly content 1995 InlineAsmIdentifierInfo Info; 1996 AsmFieldInfo FieldInfo; 1997 const MCExpr *Val; 1998 if (isParsingMSInlineAsm() || Parser.isParsingMasm()) { 1999 // MS Dot Operator expression 2000 if (Identifier.count('.') && 2001 (PrevTK == AsmToken::RBrac || PrevTK == AsmToken::RParen)) { 2002 if (ParseIntelDotOperator(SM, End)) 2003 return true; 2004 break; 2005 } 2006 } 2007 if (isParsingMSInlineAsm()) { 2008 // MS InlineAsm operators (TYPE/LENGTH/SIZE) 2009 if (unsigned OpKind = IdentifyIntelInlineAsmOperator(Identifier)) { 2010 if (int64_t Val = ParseIntelInlineAsmOperator(OpKind)) { 2011 if (SM.onInteger(Val, ErrMsg)) 2012 return Error(IdentLoc, ErrMsg); 2013 } else { 2014 return true; 2015 } 2016 break; 2017 } 2018 // MS InlineAsm identifier 2019 // Call parseIdentifier() to combine @ with the identifier behind it. 2020 if (TK == AsmToken::At && Parser.parseIdentifier(Identifier)) 2021 return Error(IdentLoc, "expected identifier"); 2022 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, false, End)) 2023 return true; 2024 else if (SM.onIdentifierExpr(Val, Identifier, Info, FieldInfo.Type, 2025 true, ErrMsg)) 2026 return Error(IdentLoc, ErrMsg); 2027 break; 2028 } 2029 if (Parser.isParsingMasm()) { 2030 if (unsigned OpKind = IdentifyMasmOperator(Identifier)) { 2031 int64_t Val; 2032 if (ParseMasmOperator(OpKind, Val)) 2033 return true; 2034 if (SM.onInteger(Val, ErrMsg)) 2035 return Error(IdentLoc, ErrMsg); 2036 break; 2037 } 2038 if (!getParser().lookUpType(Identifier, FieldInfo.Type)) { 2039 // Field offset immediate; <TYPE>.<field specification> 2040 Lex(); // eat type 2041 bool EndDot = parseOptionalToken(AsmToken::Dot); 2042 while (EndDot || (getTok().is(AsmToken::Identifier) && 2043 getTok().getString().startswith("."))) { 2044 getParser().parseIdentifier(Identifier); 2045 if (!EndDot) 2046 Identifier.consume_front("."); 2047 EndDot = Identifier.consume_back("."); 2048 if (getParser().lookUpField(FieldInfo.Type.Name, Identifier, 2049 FieldInfo)) { 2050 SMLoc IDEnd = 2051 SMLoc::getFromPointer(Identifier.data() + Identifier.size()); 2052 return Error(IdentLoc, "Unable to lookup field reference!", 2053 SMRange(IdentLoc, IDEnd)); 2054 } 2055 if (!EndDot) 2056 EndDot = parseOptionalToken(AsmToken::Dot); 2057 } 2058 if (SM.onInteger(FieldInfo.Offset, ErrMsg)) 2059 return Error(IdentLoc, ErrMsg); 2060 break; 2061 } 2062 } 2063 if (getParser().parsePrimaryExpr(Val, End, &FieldInfo.Type)) { 2064 return Error(Tok.getLoc(), "Unexpected identifier!"); 2065 } else if (SM.onIdentifierExpr(Val, Identifier, Info, FieldInfo.Type, 2066 false, ErrMsg)) { 2067 return Error(IdentLoc, ErrMsg); 2068 } 2069 break; 2070 } 2071 case AsmToken::Integer: { 2072 // Look for 'b' or 'f' following an Integer as a directional label 2073 SMLoc Loc = getTok().getLoc(); 2074 int64_t IntVal = getTok().getIntVal(); 2075 End = consumeToken(); 2076 UpdateLocLex = false; 2077 if (getLexer().getKind() == AsmToken::Identifier) { 2078 StringRef IDVal = getTok().getString(); 2079 if (IDVal == "f" || IDVal == "b") { 2080 MCSymbol *Sym = 2081 getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b"); 2082 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 2083 const MCExpr *Val = 2084 MCSymbolRefExpr::create(Sym, Variant, getContext()); 2085 if (IDVal == "b" && Sym->isUndefined()) 2086 return Error(Loc, "invalid reference to undefined symbol"); 2087 StringRef Identifier = Sym->getName(); 2088 InlineAsmIdentifierInfo Info; 2089 AsmTypeInfo Type; 2090 if (SM.onIdentifierExpr(Val, Identifier, Info, Type, 2091 isParsingMSInlineAsm(), ErrMsg)) 2092 return Error(Loc, ErrMsg); 2093 End = consumeToken(); 2094 } else { 2095 if (SM.onInteger(IntVal, ErrMsg)) 2096 return Error(Loc, ErrMsg); 2097 } 2098 } else { 2099 if (SM.onInteger(IntVal, ErrMsg)) 2100 return Error(Loc, ErrMsg); 2101 } 2102 break; 2103 } 2104 case AsmToken::Plus: 2105 if (SM.onPlus(ErrMsg)) 2106 return Error(getTok().getLoc(), ErrMsg); 2107 break; 2108 case AsmToken::Minus: 2109 if (SM.onMinus(ErrMsg)) 2110 return Error(getTok().getLoc(), ErrMsg); 2111 break; 2112 case AsmToken::Tilde: SM.onNot(); break; 2113 case AsmToken::Star: SM.onStar(); break; 2114 case AsmToken::Slash: SM.onDivide(); break; 2115 case AsmToken::Percent: SM.onMod(); break; 2116 case AsmToken::Pipe: SM.onOr(); break; 2117 case AsmToken::Caret: SM.onXor(); break; 2118 case AsmToken::Amp: SM.onAnd(); break; 2119 case AsmToken::LessLess: 2120 SM.onLShift(); break; 2121 case AsmToken::GreaterGreater: 2122 SM.onRShift(); break; 2123 case AsmToken::LBrac: 2124 if (SM.onLBrac()) 2125 return Error(Tok.getLoc(), "unexpected bracket encountered"); 2126 break; 2127 case AsmToken::RBrac: 2128 if (SM.onRBrac()) 2129 return Error(Tok.getLoc(), "unexpected bracket encountered"); 2130 break; 2131 case AsmToken::LParen: SM.onLParen(); break; 2132 case AsmToken::RParen: SM.onRParen(); break; 2133 } 2134 if (SM.hadError()) 2135 return Error(Tok.getLoc(), "unknown token in expression"); 2136 2137 if (!Done && UpdateLocLex) 2138 End = consumeToken(); 2139 2140 PrevTK = TK; 2141 } 2142 return false; 2143 } 2144 2145 void X86AsmParser::RewriteIntelExpression(IntelExprStateMachine &SM, 2146 SMLoc Start, SMLoc End) { 2147 SMLoc Loc = Start; 2148 unsigned ExprLen = End.getPointer() - Start.getPointer(); 2149 // Skip everything before a symbol displacement (if we have one) 2150 if (SM.getSym() && !SM.isOffsetOperator()) { 2151 StringRef SymName = SM.getSymName(); 2152 if (unsigned Len = SymName.data() - Start.getPointer()) 2153 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Start, Len); 2154 Loc = SMLoc::getFromPointer(SymName.data() + SymName.size()); 2155 ExprLen = End.getPointer() - (SymName.data() + SymName.size()); 2156 // If we have only a symbol than there's no need for complex rewrite, 2157 // simply skip everything after it 2158 if (!(SM.getBaseReg() || SM.getIndexReg() || SM.getImm())) { 2159 if (ExprLen) 2160 InstInfo->AsmRewrites->emplace_back(AOK_Skip, Loc, ExprLen); 2161 return; 2162 } 2163 } 2164 // Build an Intel Expression rewrite 2165 StringRef BaseRegStr; 2166 StringRef IndexRegStr; 2167 StringRef OffsetNameStr; 2168 if (SM.getBaseReg()) 2169 BaseRegStr = X86IntelInstPrinter::getRegisterName(SM.getBaseReg()); 2170 if (SM.getIndexReg()) 2171 IndexRegStr = X86IntelInstPrinter::getRegisterName(SM.getIndexReg()); 2172 if (SM.isOffsetOperator()) 2173 OffsetNameStr = SM.getSymName(); 2174 // Emit it 2175 IntelExpr Expr(BaseRegStr, IndexRegStr, SM.getScale(), OffsetNameStr, 2176 SM.getImm(), SM.isMemExpr()); 2177 InstInfo->AsmRewrites->emplace_back(Loc, ExprLen, Expr); 2178 } 2179 2180 // Inline assembly may use variable names with namespace alias qualifiers. 2181 bool X86AsmParser::ParseIntelInlineAsmIdentifier( 2182 const MCExpr *&Val, StringRef &Identifier, InlineAsmIdentifierInfo &Info, 2183 bool IsUnevaluatedOperand, SMLoc &End, bool IsParsingOffsetOperator) { 2184 MCAsmParser &Parser = getParser(); 2185 assert(isParsingMSInlineAsm() && "Expected to be parsing inline assembly."); 2186 Val = nullptr; 2187 2188 StringRef LineBuf(Identifier.data()); 2189 SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand); 2190 2191 const AsmToken &Tok = Parser.getTok(); 2192 SMLoc Loc = Tok.getLoc(); 2193 2194 // Advance the token stream until the end of the current token is 2195 // after the end of what the frontend claimed. 2196 const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size(); 2197 do { 2198 End = Tok.getEndLoc(); 2199 getLexer().Lex(); 2200 } while (End.getPointer() < EndPtr); 2201 Identifier = LineBuf; 2202 2203 // The frontend should end parsing on an assembler token boundary, unless it 2204 // failed parsing. 2205 assert((End.getPointer() == EndPtr || 2206 Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) && 2207 "frontend claimed part of a token?"); 2208 2209 // If the identifier lookup was unsuccessful, assume that we are dealing with 2210 // a label. 2211 if (Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) { 2212 StringRef InternalName = 2213 SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(), 2214 Loc, false); 2215 assert(InternalName.size() && "We should have an internal name here."); 2216 // Push a rewrite for replacing the identifier name with the internal name, 2217 // unless we are parsing the operand of an offset operator 2218 if (!IsParsingOffsetOperator) 2219 InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(), 2220 InternalName); 2221 else 2222 Identifier = InternalName; 2223 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) 2224 return false; 2225 // Create the symbol reference. 2226 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier); 2227 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 2228 Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext()); 2229 return false; 2230 } 2231 2232 //ParseRoundingModeOp - Parse AVX-512 rounding mode operand 2233 bool X86AsmParser::ParseRoundingModeOp(SMLoc Start, OperandVector &Operands) { 2234 MCAsmParser &Parser = getParser(); 2235 const AsmToken &Tok = Parser.getTok(); 2236 // Eat "{" and mark the current place. 2237 const SMLoc consumedToken = consumeToken(); 2238 if (Tok.isNot(AsmToken::Identifier)) 2239 return Error(Tok.getLoc(), "Expected an identifier after {"); 2240 if (Tok.getIdentifier().startswith("r")){ 2241 int rndMode = StringSwitch<int>(Tok.getIdentifier()) 2242 .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT) 2243 .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF) 2244 .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF) 2245 .Case("rz", X86::STATIC_ROUNDING::TO_ZERO) 2246 .Default(-1); 2247 if (-1 == rndMode) 2248 return Error(Tok.getLoc(), "Invalid rounding mode."); 2249 Parser.Lex(); // Eat "r*" of r*-sae 2250 if (!getLexer().is(AsmToken::Minus)) 2251 return Error(Tok.getLoc(), "Expected - at this point"); 2252 Parser.Lex(); // Eat "-" 2253 Parser.Lex(); // Eat the sae 2254 if (!getLexer().is(AsmToken::RCurly)) 2255 return Error(Tok.getLoc(), "Expected } at this point"); 2256 SMLoc End = Tok.getEndLoc(); 2257 Parser.Lex(); // Eat "}" 2258 const MCExpr *RndModeOp = 2259 MCConstantExpr::create(rndMode, Parser.getContext()); 2260 Operands.push_back(X86Operand::CreateImm(RndModeOp, Start, End)); 2261 return false; 2262 } 2263 if(Tok.getIdentifier().equals("sae")){ 2264 Parser.Lex(); // Eat the sae 2265 if (!getLexer().is(AsmToken::RCurly)) 2266 return Error(Tok.getLoc(), "Expected } at this point"); 2267 Parser.Lex(); // Eat "}" 2268 Operands.push_back(X86Operand::CreateToken("{sae}", consumedToken)); 2269 return false; 2270 } 2271 return Error(Tok.getLoc(), "unknown token in expression"); 2272 } 2273 2274 /// Parse the '.' operator. 2275 bool X86AsmParser::ParseIntelDotOperator(IntelExprStateMachine &SM, 2276 SMLoc &End) { 2277 const AsmToken &Tok = getTok(); 2278 AsmFieldInfo Info; 2279 2280 // Drop the optional '.'. 2281 StringRef DotDispStr = Tok.getString(); 2282 if (DotDispStr.startswith(".")) 2283 DotDispStr = DotDispStr.drop_front(1); 2284 StringRef TrailingDot; 2285 2286 // .Imm gets lexed as a real. 2287 if (Tok.is(AsmToken::Real)) { 2288 APInt DotDisp; 2289 DotDispStr.getAsInteger(10, DotDisp); 2290 Info.Offset = DotDisp.getZExtValue(); 2291 } else if ((isParsingMSInlineAsm() || getParser().isParsingMasm()) && 2292 Tok.is(AsmToken::Identifier)) { 2293 if (DotDispStr.endswith(".")) { 2294 TrailingDot = DotDispStr.substr(DotDispStr.size() - 1); 2295 DotDispStr = DotDispStr.drop_back(1); 2296 } 2297 const std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.'); 2298 const StringRef Base = BaseMember.first, Member = BaseMember.second; 2299 if (getParser().lookUpField(SM.getType(), DotDispStr, Info) && 2300 getParser().lookUpField(SM.getSymName(), DotDispStr, Info) && 2301 getParser().lookUpField(DotDispStr, Info) && 2302 (!SemaCallback || 2303 SemaCallback->LookupInlineAsmField(Base, Member, Info.Offset))) 2304 return Error(Tok.getLoc(), "Unable to lookup field reference!"); 2305 } else { 2306 return Error(Tok.getLoc(), "Unexpected token type!"); 2307 } 2308 2309 // Eat the DotExpression and update End 2310 End = SMLoc::getFromPointer(DotDispStr.data()); 2311 const char *DotExprEndLoc = DotDispStr.data() + DotDispStr.size(); 2312 while (Tok.getLoc().getPointer() < DotExprEndLoc) 2313 Lex(); 2314 if (!TrailingDot.empty()) 2315 getLexer().UnLex(AsmToken(AsmToken::Dot, TrailingDot)); 2316 SM.addImm(Info.Offset); 2317 SM.setTypeInfo(Info.Type); 2318 return false; 2319 } 2320 2321 /// Parse the 'offset' operator. 2322 /// This operator is used to specify the location of a given operand 2323 bool X86AsmParser::ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID, 2324 InlineAsmIdentifierInfo &Info, 2325 SMLoc &End) { 2326 // Eat offset, mark start of identifier. 2327 SMLoc Start = Lex().getLoc(); 2328 ID = getTok().getString(); 2329 if (!isParsingMSInlineAsm()) { 2330 if ((getTok().isNot(AsmToken::Identifier) && 2331 getTok().isNot(AsmToken::String)) || 2332 getParser().parsePrimaryExpr(Val, End, nullptr)) 2333 return Error(Start, "unexpected token!"); 2334 } else if (ParseIntelInlineAsmIdentifier(Val, ID, Info, false, End, true)) { 2335 return Error(Start, "unable to lookup expression"); 2336 } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) { 2337 return Error(Start, "offset operator cannot yet handle constants"); 2338 } 2339 return false; 2340 } 2341 2342 // Query a candidate string for being an Intel assembly operator 2343 // Report back its kind, or IOK_INVALID if does not evaluated as a known one 2344 unsigned X86AsmParser::IdentifyIntelInlineAsmOperator(StringRef Name) { 2345 return StringSwitch<unsigned>(Name) 2346 .Cases("TYPE","type",IOK_TYPE) 2347 .Cases("SIZE","size",IOK_SIZE) 2348 .Cases("LENGTH","length",IOK_LENGTH) 2349 .Default(IOK_INVALID); 2350 } 2351 2352 /// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator 2353 /// returns the number of elements in an array. It returns the value 1 for 2354 /// non-array variables. The SIZE operator returns the size of a C or C++ 2355 /// variable. A variable's size is the product of its LENGTH and TYPE. The 2356 /// TYPE operator returns the size of a C or C++ type or variable. If the 2357 /// variable is an array, TYPE returns the size of a single element. 2358 unsigned X86AsmParser::ParseIntelInlineAsmOperator(unsigned OpKind) { 2359 MCAsmParser &Parser = getParser(); 2360 const AsmToken &Tok = Parser.getTok(); 2361 Parser.Lex(); // Eat operator. 2362 2363 const MCExpr *Val = nullptr; 2364 InlineAsmIdentifierInfo Info; 2365 SMLoc Start = Tok.getLoc(), End; 2366 StringRef Identifier = Tok.getString(); 2367 if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, 2368 /*IsUnevaluatedOperand=*/true, End)) 2369 return 0; 2370 2371 if (!Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 2372 Error(Start, "unable to lookup expression"); 2373 return 0; 2374 } 2375 2376 unsigned CVal = 0; 2377 switch(OpKind) { 2378 default: llvm_unreachable("Unexpected operand kind!"); 2379 case IOK_LENGTH: CVal = Info.Var.Length; break; 2380 case IOK_SIZE: CVal = Info.Var.Size; break; 2381 case IOK_TYPE: CVal = Info.Var.Type; break; 2382 } 2383 2384 return CVal; 2385 } 2386 2387 // Query a candidate string for being an Intel assembly operator 2388 // Report back its kind, or IOK_INVALID if does not evaluated as a known one 2389 unsigned X86AsmParser::IdentifyMasmOperator(StringRef Name) { 2390 return StringSwitch<unsigned>(Name.lower()) 2391 .Case("type", MOK_TYPE) 2392 .Cases("size", "sizeof", MOK_SIZEOF) 2393 .Cases("length", "lengthof", MOK_LENGTHOF) 2394 .Default(MOK_INVALID); 2395 } 2396 2397 /// Parse the 'LENGTHOF', 'SIZEOF', and 'TYPE' operators. The LENGTHOF operator 2398 /// returns the number of elements in an array. It returns the value 1 for 2399 /// non-array variables. The SIZEOF operator returns the size of a type or 2400 /// variable in bytes. A variable's size is the product of its LENGTH and TYPE. 2401 /// The TYPE operator returns the size of a variable. If the variable is an 2402 /// array, TYPE returns the size of a single element. 2403 bool X86AsmParser::ParseMasmOperator(unsigned OpKind, int64_t &Val) { 2404 MCAsmParser &Parser = getParser(); 2405 SMLoc OpLoc = Parser.getTok().getLoc(); 2406 Parser.Lex(); // Eat operator. 2407 2408 Val = 0; 2409 if (OpKind == MOK_SIZEOF || OpKind == MOK_TYPE) { 2410 // Check for SIZEOF(<type>) and TYPE(<type>). 2411 bool InParens = Parser.getTok().is(AsmToken::LParen); 2412 const AsmToken &IDTok = InParens ? getLexer().peekTok() : Parser.getTok(); 2413 AsmTypeInfo Type; 2414 if (IDTok.is(AsmToken::Identifier) && 2415 !Parser.lookUpType(IDTok.getIdentifier(), Type)) { 2416 Val = Type.Size; 2417 2418 // Eat tokens. 2419 if (InParens) 2420 parseToken(AsmToken::LParen); 2421 parseToken(AsmToken::Identifier); 2422 if (InParens) 2423 parseToken(AsmToken::RParen); 2424 } 2425 } 2426 2427 if (!Val) { 2428 IntelExprStateMachine SM; 2429 SMLoc End, Start = Parser.getTok().getLoc(); 2430 if (ParseIntelExpression(SM, End)) 2431 return true; 2432 2433 switch (OpKind) { 2434 default: 2435 llvm_unreachable("Unexpected operand kind!"); 2436 case MOK_SIZEOF: 2437 Val = SM.getSize(); 2438 break; 2439 case MOK_LENGTHOF: 2440 Val = SM.getLength(); 2441 break; 2442 case MOK_TYPE: 2443 Val = SM.getElementSize(); 2444 break; 2445 } 2446 2447 if (!Val) 2448 return Error(OpLoc, "expression has unknown type", SMRange(Start, End)); 2449 } 2450 2451 return false; 2452 } 2453 2454 bool X86AsmParser::ParseIntelMemoryOperandSize(unsigned &Size) { 2455 Size = StringSwitch<unsigned>(getTok().getString()) 2456 .Cases("BYTE", "byte", 8) 2457 .Cases("WORD", "word", 16) 2458 .Cases("DWORD", "dword", 32) 2459 .Cases("FLOAT", "float", 32) 2460 .Cases("LONG", "long", 32) 2461 .Cases("FWORD", "fword", 48) 2462 .Cases("DOUBLE", "double", 64) 2463 .Cases("QWORD", "qword", 64) 2464 .Cases("MMWORD","mmword", 64) 2465 .Cases("XWORD", "xword", 80) 2466 .Cases("TBYTE", "tbyte", 80) 2467 .Cases("XMMWORD", "xmmword", 128) 2468 .Cases("YMMWORD", "ymmword", 256) 2469 .Cases("ZMMWORD", "zmmword", 512) 2470 .Default(0); 2471 if (Size) { 2472 const AsmToken &Tok = Lex(); // Eat operand size (e.g., byte, word). 2473 if (!(Tok.getString().equals("PTR") || Tok.getString().equals("ptr"))) 2474 return Error(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!"); 2475 Lex(); // Eat ptr. 2476 } 2477 return false; 2478 } 2479 2480 bool X86AsmParser::ParseIntelOperand(OperandVector &Operands) { 2481 MCAsmParser &Parser = getParser(); 2482 const AsmToken &Tok = Parser.getTok(); 2483 SMLoc Start, End; 2484 2485 // Parse optional Size directive. 2486 unsigned Size; 2487 if (ParseIntelMemoryOperandSize(Size)) 2488 return true; 2489 bool PtrInOperand = bool(Size); 2490 2491 Start = Tok.getLoc(); 2492 2493 // Rounding mode operand. 2494 if (getLexer().is(AsmToken::LCurly)) 2495 return ParseRoundingModeOp(Start, Operands); 2496 2497 // Register operand. 2498 unsigned RegNo = 0; 2499 if (Tok.is(AsmToken::Identifier) && !ParseRegister(RegNo, Start, End)) { 2500 if (RegNo == X86::RIP) 2501 return Error(Start, "rip can only be used as a base register"); 2502 // A Register followed by ':' is considered a segment override 2503 if (Tok.isNot(AsmToken::Colon)) { 2504 if (PtrInOperand) 2505 return Error(Start, "expected memory operand after 'ptr', " 2506 "found register operand instead"); 2507 Operands.push_back(X86Operand::CreateReg(RegNo, Start, End)); 2508 return false; 2509 } 2510 // An alleged segment override. check if we have a valid segment register 2511 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo)) 2512 return Error(Start, "invalid segment register"); 2513 // Eat ':' and update Start location 2514 Start = Lex().getLoc(); 2515 } 2516 2517 // Immediates and Memory 2518 IntelExprStateMachine SM; 2519 if (ParseIntelExpression(SM, End)) 2520 return true; 2521 2522 if (isParsingMSInlineAsm()) 2523 RewriteIntelExpression(SM, Start, Tok.getLoc()); 2524 2525 int64_t Imm = SM.getImm(); 2526 const MCExpr *Disp = SM.getSym(); 2527 const MCExpr *ImmDisp = MCConstantExpr::create(Imm, getContext()); 2528 if (Disp && Imm) 2529 Disp = MCBinaryExpr::createAdd(Disp, ImmDisp, getContext()); 2530 if (!Disp) 2531 Disp = ImmDisp; 2532 2533 // RegNo != 0 specifies a valid segment register, 2534 // and we are parsing a segment override 2535 if (!SM.isMemExpr() && !RegNo) { 2536 if (isParsingMSInlineAsm() && SM.isOffsetOperator()) { 2537 const InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo(); 2538 if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) { 2539 // Disp includes the address of a variable; make sure this is recorded 2540 // for later handling. 2541 Operands.push_back(X86Operand::CreateImm(Disp, Start, End, 2542 SM.getSymName(), Info.Var.Decl, 2543 Info.Var.IsGlobalLV)); 2544 return false; 2545 } 2546 } 2547 2548 Operands.push_back(X86Operand::CreateImm(Disp, Start, End)); 2549 return false; 2550 } 2551 2552 StringRef ErrMsg; 2553 unsigned BaseReg = SM.getBaseReg(); 2554 unsigned IndexReg = SM.getIndexReg(); 2555 if (IndexReg && BaseReg == X86::RIP) 2556 BaseReg = 0; 2557 unsigned Scale = SM.getScale(); 2558 if (!PtrInOperand) 2559 Size = SM.getElementSize() << 3; 2560 2561 if (Scale == 0 && BaseReg != X86::ESP && BaseReg != X86::RSP && 2562 (IndexReg == X86::ESP || IndexReg == X86::RSP)) 2563 std::swap(BaseReg, IndexReg); 2564 2565 // If BaseReg is a vector register and IndexReg is not, swap them unless 2566 // Scale was specified in which case it would be an error. 2567 if (Scale == 0 && 2568 !(X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) || 2569 X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) || 2570 X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg)) && 2571 (X86MCRegisterClasses[X86::VR128XRegClassID].contains(BaseReg) || 2572 X86MCRegisterClasses[X86::VR256XRegClassID].contains(BaseReg) || 2573 X86MCRegisterClasses[X86::VR512RegClassID].contains(BaseReg))) 2574 std::swap(BaseReg, IndexReg); 2575 2576 if (Scale != 0 && 2577 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) 2578 return Error(Start, "16-bit addresses cannot have a scale"); 2579 2580 // If there was no explicit scale specified, change it to 1. 2581 if (Scale == 0) 2582 Scale = 1; 2583 2584 // If this is a 16-bit addressing mode with the base and index in the wrong 2585 // order, swap them so CheckBaseRegAndIndexRegAndScale doesn't fail. It is 2586 // shared with att syntax where order matters. 2587 if ((BaseReg == X86::SI || BaseReg == X86::DI) && 2588 (IndexReg == X86::BX || IndexReg == X86::BP)) 2589 std::swap(BaseReg, IndexReg); 2590 2591 if ((BaseReg || IndexReg) && 2592 CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(), 2593 ErrMsg)) 2594 return Error(Start, ErrMsg); 2595 if (isParsingMSInlineAsm()) 2596 return CreateMemForMSInlineAsm(RegNo, Disp, BaseReg, IndexReg, Scale, Start, 2597 End, Size, SM.getSymName(), 2598 SM.getIdentifierInfo(), Operands); 2599 2600 // When parsing x64 MS-style assembly, all non-absolute references to a named 2601 // variable default to RIP-relative. 2602 if (Parser.isParsingMasm() && is64BitMode() && SM.getElementSize() > 0) { 2603 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), RegNo, Disp, 2604 BaseReg, IndexReg, Scale, Start, 2605 End, Size, 2606 /*DefaultBaseReg=*/X86::RIP)); 2607 return false; 2608 } 2609 2610 if ((BaseReg || IndexReg || RegNo)) 2611 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), RegNo, Disp, 2612 BaseReg, IndexReg, Scale, Start, 2613 End, Size)); 2614 else 2615 Operands.push_back( 2616 X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size)); 2617 return false; 2618 } 2619 2620 bool X86AsmParser::ParseATTOperand(OperandVector &Operands) { 2621 MCAsmParser &Parser = getParser(); 2622 switch (getLexer().getKind()) { 2623 case AsmToken::Dollar: { 2624 // $42 or $ID -> immediate. 2625 SMLoc Start = Parser.getTok().getLoc(), End; 2626 Parser.Lex(); 2627 const MCExpr *Val; 2628 // This is an immediate, so we should not parse a register. Do a precheck 2629 // for '%' to supercede intra-register parse errors. 2630 SMLoc L = Parser.getTok().getLoc(); 2631 if (check(getLexer().is(AsmToken::Percent), L, 2632 "expected immediate expression") || 2633 getParser().parseExpression(Val, End) || 2634 check(isa<X86MCExpr>(Val), L, "expected immediate expression")) 2635 return true; 2636 Operands.push_back(X86Operand::CreateImm(Val, Start, End)); 2637 return false; 2638 } 2639 case AsmToken::LCurly: { 2640 SMLoc Start = Parser.getTok().getLoc(); 2641 return ParseRoundingModeOp(Start, Operands); 2642 } 2643 default: { 2644 // This a memory operand or a register. We have some parsing complications 2645 // as a '(' may be part of an immediate expression or the addressing mode 2646 // block. This is complicated by the fact that an assembler-level variable 2647 // may refer either to a register or an immediate expression. 2648 2649 SMLoc Loc = Parser.getTok().getLoc(), EndLoc; 2650 const MCExpr *Expr = nullptr; 2651 unsigned Reg = 0; 2652 if (getLexer().isNot(AsmToken::LParen)) { 2653 // No '(' so this is either a displacement expression or a register. 2654 if (Parser.parseExpression(Expr, EndLoc)) 2655 return true; 2656 if (auto *RE = dyn_cast<X86MCExpr>(Expr)) { 2657 // Segment Register. Reset Expr and copy value to register. 2658 Expr = nullptr; 2659 Reg = RE->getRegNo(); 2660 2661 // Check the register. 2662 if (Reg == X86::EIZ || Reg == X86::RIZ) 2663 return Error( 2664 Loc, "%eiz and %riz can only be used as index registers", 2665 SMRange(Loc, EndLoc)); 2666 if (Reg == X86::RIP) 2667 return Error(Loc, "%rip can only be used as a base register", 2668 SMRange(Loc, EndLoc)); 2669 // Return register that are not segment prefixes immediately. 2670 if (!Parser.parseOptionalToken(AsmToken::Colon)) { 2671 Operands.push_back(X86Operand::CreateReg(Reg, Loc, EndLoc)); 2672 return false; 2673 } 2674 if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(Reg)) 2675 return Error(Loc, "invalid segment register"); 2676 // Accept a '*' absolute memory reference after the segment. Place it 2677 // before the full memory operand. 2678 if (getLexer().is(AsmToken::Star)) 2679 Operands.push_back(X86Operand::CreateToken("*", consumeToken())); 2680 } 2681 } 2682 // This is a Memory operand. 2683 return ParseMemOperand(Reg, Expr, Loc, EndLoc, Operands); 2684 } 2685 } 2686 } 2687 2688 // X86::COND_INVALID if not a recognized condition code or alternate mnemonic, 2689 // otherwise the EFLAGS Condition Code enumerator. 2690 X86::CondCode X86AsmParser::ParseConditionCode(StringRef CC) { 2691 return StringSwitch<X86::CondCode>(CC) 2692 .Case("o", X86::COND_O) // Overflow 2693 .Case("no", X86::COND_NO) // No Overflow 2694 .Cases("b", "nae", X86::COND_B) // Below/Neither Above nor Equal 2695 .Cases("ae", "nb", X86::COND_AE) // Above or Equal/Not Below 2696 .Cases("e", "z", X86::COND_E) // Equal/Zero 2697 .Cases("ne", "nz", X86::COND_NE) // Not Equal/Not Zero 2698 .Cases("be", "na", X86::COND_BE) // Below or Equal/Not Above 2699 .Cases("a", "nbe", X86::COND_A) // Above/Neither Below nor Equal 2700 .Case("s", X86::COND_S) // Sign 2701 .Case("ns", X86::COND_NS) // No Sign 2702 .Cases("p", "pe", X86::COND_P) // Parity/Parity Even 2703 .Cases("np", "po", X86::COND_NP) // No Parity/Parity Odd 2704 .Cases("l", "nge", X86::COND_L) // Less/Neither Greater nor Equal 2705 .Cases("ge", "nl", X86::COND_GE) // Greater or Equal/Not Less 2706 .Cases("le", "ng", X86::COND_LE) // Less or Equal/Not Greater 2707 .Cases("g", "nle", X86::COND_G) // Greater/Neither Less nor Equal 2708 .Default(X86::COND_INVALID); 2709 } 2710 2711 // true on failure, false otherwise 2712 // If no {z} mark was found - Parser doesn't advance 2713 bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z, 2714 const SMLoc &StartLoc) { 2715 MCAsmParser &Parser = getParser(); 2716 // Assuming we are just pass the '{' mark, quering the next token 2717 // Searched for {z}, but none was found. Return false, as no parsing error was 2718 // encountered 2719 if (!(getLexer().is(AsmToken::Identifier) && 2720 (getLexer().getTok().getIdentifier() == "z"))) 2721 return false; 2722 Parser.Lex(); // Eat z 2723 // Query and eat the '}' mark 2724 if (!getLexer().is(AsmToken::RCurly)) 2725 return Error(getLexer().getLoc(), "Expected } at this point"); 2726 Parser.Lex(); // Eat '}' 2727 // Assign Z with the {z} mark operand 2728 Z = X86Operand::CreateToken("{z}", StartLoc); 2729 return false; 2730 } 2731 2732 // true on failure, false otherwise 2733 bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands) { 2734 MCAsmParser &Parser = getParser(); 2735 if (getLexer().is(AsmToken::LCurly)) { 2736 // Eat "{" and mark the current place. 2737 const SMLoc consumedToken = consumeToken(); 2738 // Distinguish {1to<NUM>} from {%k<NUM>}. 2739 if(getLexer().is(AsmToken::Integer)) { 2740 // Parse memory broadcasting ({1to<NUM>}). 2741 if (getLexer().getTok().getIntVal() != 1) 2742 return TokError("Expected 1to<NUM> at this point"); 2743 StringRef Prefix = getLexer().getTok().getString(); 2744 Parser.Lex(); // Eat first token of 1to8 2745 if (!getLexer().is(AsmToken::Identifier)) 2746 return TokError("Expected 1to<NUM> at this point"); 2747 // Recognize only reasonable suffixes. 2748 SmallVector<char, 5> BroadcastVector; 2749 StringRef BroadcastString = (Prefix + getLexer().getTok().getIdentifier()) 2750 .toStringRef(BroadcastVector); 2751 if (!BroadcastString.startswith("1to")) 2752 return TokError("Expected 1to<NUM> at this point"); 2753 const char *BroadcastPrimitive = 2754 StringSwitch<const char *>(BroadcastString) 2755 .Case("1to2", "{1to2}") 2756 .Case("1to4", "{1to4}") 2757 .Case("1to8", "{1to8}") 2758 .Case("1to16", "{1to16}") 2759 .Case("1to32", "{1to32}") 2760 .Default(nullptr); 2761 if (!BroadcastPrimitive) 2762 return TokError("Invalid memory broadcast primitive."); 2763 Parser.Lex(); // Eat trailing token of 1toN 2764 if (!getLexer().is(AsmToken::RCurly)) 2765 return TokError("Expected } at this point"); 2766 Parser.Lex(); // Eat "}" 2767 Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive, 2768 consumedToken)); 2769 // No AVX512 specific primitives can pass 2770 // after memory broadcasting, so return. 2771 return false; 2772 } else { 2773 // Parse either {k}{z}, {z}{k}, {k} or {z} 2774 // last one have no meaning, but GCC accepts it 2775 // Currently, we're just pass a '{' mark 2776 std::unique_ptr<X86Operand> Z; 2777 if (ParseZ(Z, consumedToken)) 2778 return true; 2779 // Reaching here means that parsing of the allegadly '{z}' mark yielded 2780 // no errors. 2781 // Query for the need of further parsing for a {%k<NUM>} mark 2782 if (!Z || getLexer().is(AsmToken::LCurly)) { 2783 SMLoc StartLoc = Z ? consumeToken() : consumedToken; 2784 // Parse an op-mask register mark ({%k<NUM>}), which is now to be 2785 // expected 2786 unsigned RegNo; 2787 SMLoc RegLoc; 2788 if (!ParseRegister(RegNo, RegLoc, StartLoc) && 2789 X86MCRegisterClasses[X86::VK1RegClassID].contains(RegNo)) { 2790 if (RegNo == X86::K0) 2791 return Error(RegLoc, "Register k0 can't be used as write mask"); 2792 if (!getLexer().is(AsmToken::RCurly)) 2793 return Error(getLexer().getLoc(), "Expected } at this point"); 2794 Operands.push_back(X86Operand::CreateToken("{", StartLoc)); 2795 Operands.push_back( 2796 X86Operand::CreateReg(RegNo, StartLoc, StartLoc)); 2797 Operands.push_back(X86Operand::CreateToken("}", consumeToken())); 2798 } else 2799 return Error(getLexer().getLoc(), 2800 "Expected an op-mask register at this point"); 2801 // {%k<NUM>} mark is found, inquire for {z} 2802 if (getLexer().is(AsmToken::LCurly) && !Z) { 2803 // Have we've found a parsing error, or found no (expected) {z} mark 2804 // - report an error 2805 if (ParseZ(Z, consumeToken()) || !Z) 2806 return Error(getLexer().getLoc(), 2807 "Expected a {z} mark at this point"); 2808 2809 } 2810 // '{z}' on its own is meaningless, hence should be ignored. 2811 // on the contrary - have it been accompanied by a K register, 2812 // allow it. 2813 if (Z) 2814 Operands.push_back(std::move(Z)); 2815 } 2816 } 2817 } 2818 return false; 2819 } 2820 2821 /// ParseMemOperand: 'seg : disp(basereg, indexreg, scale)'. The '%ds:' prefix 2822 /// has already been parsed if present. disp may be provided as well. 2823 bool X86AsmParser::ParseMemOperand(unsigned SegReg, const MCExpr *Disp, 2824 SMLoc StartLoc, SMLoc EndLoc, 2825 OperandVector &Operands) { 2826 MCAsmParser &Parser = getParser(); 2827 SMLoc Loc; 2828 // Based on the initial passed values, we may be in any of these cases, we are 2829 // in one of these cases (with current position (*)): 2830 2831 // 1. seg : * disp (base-index-scale-expr) 2832 // 2. seg : *(disp) (base-index-scale-expr) 2833 // 3. seg : *(base-index-scale-expr) 2834 // 4. disp *(base-index-scale-expr) 2835 // 5. *(disp) (base-index-scale-expr) 2836 // 6. *(base-index-scale-expr) 2837 // 7. disp * 2838 // 8. *(disp) 2839 2840 // If we do not have an displacement yet, check if we're in cases 4 or 6 by 2841 // checking if the first object after the parenthesis is a register (or an 2842 // identifier referring to a register) and parse the displacement or default 2843 // to 0 as appropriate. 2844 auto isAtMemOperand = [this]() { 2845 if (this->getLexer().isNot(AsmToken::LParen)) 2846 return false; 2847 AsmToken Buf[2]; 2848 StringRef Id; 2849 auto TokCount = this->getLexer().peekTokens(Buf, true); 2850 if (TokCount == 0) 2851 return false; 2852 switch (Buf[0].getKind()) { 2853 case AsmToken::Percent: 2854 case AsmToken::Comma: 2855 return true; 2856 // These lower cases are doing a peekIdentifier. 2857 case AsmToken::At: 2858 case AsmToken::Dollar: 2859 if ((TokCount > 1) && 2860 (Buf[1].is(AsmToken::Identifier) || Buf[1].is(AsmToken::String)) && 2861 (Buf[0].getLoc().getPointer() + 1 == Buf[1].getLoc().getPointer())) 2862 Id = StringRef(Buf[0].getLoc().getPointer(), 2863 Buf[1].getIdentifier().size() + 1); 2864 break; 2865 case AsmToken::Identifier: 2866 case AsmToken::String: 2867 Id = Buf[0].getIdentifier(); 2868 break; 2869 default: 2870 return false; 2871 } 2872 // We have an ID. Check if it is bound to a register. 2873 if (!Id.empty()) { 2874 MCSymbol *Sym = this->getContext().getOrCreateSymbol(Id); 2875 if (Sym->isVariable()) { 2876 auto V = Sym->getVariableValue(/*SetUsed*/ false); 2877 return isa<X86MCExpr>(V); 2878 } 2879 } 2880 return false; 2881 }; 2882 2883 if (!Disp) { 2884 // Parse immediate if we're not at a mem operand yet. 2885 if (!isAtMemOperand()) { 2886 if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(Disp, EndLoc)) 2887 return true; 2888 assert(!isa<X86MCExpr>(Disp) && "Expected non-register here."); 2889 } else { 2890 // Disp is implicitly zero if we haven't parsed it yet. 2891 Disp = MCConstantExpr::create(0, Parser.getContext()); 2892 } 2893 } 2894 2895 // We are now either at the end of the operand or at the '(' at the start of a 2896 // base-index-scale-expr. 2897 2898 if (!parseOptionalToken(AsmToken::LParen)) { 2899 if (SegReg == 0) 2900 Operands.push_back( 2901 X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc)); 2902 else 2903 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 2904 0, 0, 1, StartLoc, EndLoc)); 2905 return false; 2906 } 2907 2908 // If we reached here, then eat the '(' and Process 2909 // the rest of the memory operand. 2910 unsigned BaseReg = 0, IndexReg = 0, Scale = 1; 2911 SMLoc BaseLoc = getLexer().getLoc(); 2912 const MCExpr *E; 2913 StringRef ErrMsg; 2914 2915 // Parse BaseReg if one is provided. 2916 if (getLexer().isNot(AsmToken::Comma) && getLexer().isNot(AsmToken::RParen)) { 2917 if (Parser.parseExpression(E, EndLoc) || 2918 check(!isa<X86MCExpr>(E), BaseLoc, "expected register here")) 2919 return true; 2920 2921 // Check the register. 2922 BaseReg = cast<X86MCExpr>(E)->getRegNo(); 2923 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) 2924 return Error(BaseLoc, "eiz and riz can only be used as index registers", 2925 SMRange(BaseLoc, EndLoc)); 2926 } 2927 2928 if (parseOptionalToken(AsmToken::Comma)) { 2929 // Following the comma we should have either an index register, or a scale 2930 // value. We don't support the later form, but we want to parse it 2931 // correctly. 2932 // 2933 // Even though it would be completely consistent to support syntax like 2934 // "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this. 2935 if (getLexer().isNot(AsmToken::RParen)) { 2936 if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(E, EndLoc)) 2937 return true; 2938 2939 if (!isa<X86MCExpr>(E)) { 2940 // We've parsed an unexpected Scale Value instead of an index 2941 // register. Interpret it as an absolute. 2942 int64_t ScaleVal; 2943 if (!E->evaluateAsAbsolute(ScaleVal, getStreamer().getAssemblerPtr())) 2944 return Error(Loc, "expected absolute expression"); 2945 if (ScaleVal != 1) 2946 Warning(Loc, "scale factor without index register is ignored"); 2947 Scale = 1; 2948 } else { // IndexReg Found. 2949 IndexReg = cast<X86MCExpr>(E)->getRegNo(); 2950 2951 if (BaseReg == X86::RIP) 2952 return Error(Loc, 2953 "%rip as base register can not have an index register"); 2954 if (IndexReg == X86::RIP) 2955 return Error(Loc, "%rip is not allowed as an index register"); 2956 2957 if (parseOptionalToken(AsmToken::Comma)) { 2958 // Parse the scale amount: 2959 // ::= ',' [scale-expression] 2960 2961 // A scale amount without an index is ignored. 2962 if (getLexer().isNot(AsmToken::RParen)) { 2963 int64_t ScaleVal; 2964 if (Parser.parseTokenLoc(Loc) || 2965 Parser.parseAbsoluteExpression(ScaleVal)) 2966 return Error(Loc, "expected scale expression"); 2967 Scale = (unsigned)ScaleVal; 2968 // Validate the scale amount. 2969 if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) && 2970 Scale != 1) 2971 return Error(Loc, "scale factor in 16-bit address must be 1"); 2972 if (checkScale(Scale, ErrMsg)) 2973 return Error(Loc, ErrMsg); 2974 } 2975 } 2976 } 2977 } 2978 } 2979 2980 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. 2981 if (parseToken(AsmToken::RParen, "unexpected token in memory operand")) 2982 return true; 2983 2984 // This is to support otherwise illegal operand (%dx) found in various 2985 // unofficial manuals examples (e.g. "out[s]?[bwl]? %al, (%dx)") and must now 2986 // be supported. Mark such DX variants separately fix only in special cases. 2987 if (BaseReg == X86::DX && IndexReg == 0 && Scale == 1 && SegReg == 0 && 2988 isa<MCConstantExpr>(Disp) && 2989 cast<MCConstantExpr>(Disp)->getValue() == 0) { 2990 Operands.push_back(X86Operand::CreateDXReg(BaseLoc, BaseLoc)); 2991 return false; 2992 } 2993 2994 if (CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(), 2995 ErrMsg)) 2996 return Error(BaseLoc, ErrMsg); 2997 2998 if (SegReg || BaseReg || IndexReg) 2999 Operands.push_back(X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, 3000 BaseReg, IndexReg, Scale, StartLoc, 3001 EndLoc)); 3002 else 3003 Operands.push_back( 3004 X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc)); 3005 return false; 3006 } 3007 3008 // Parse either a standard primary expression or a register. 3009 bool X86AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { 3010 MCAsmParser &Parser = getParser(); 3011 // See if this is a register first. 3012 if (getTok().is(AsmToken::Percent) || 3013 (isParsingIntelSyntax() && getTok().is(AsmToken::Identifier) && 3014 MatchRegisterName(Parser.getTok().getString()))) { 3015 SMLoc StartLoc = Parser.getTok().getLoc(); 3016 unsigned RegNo; 3017 if (ParseRegister(RegNo, StartLoc, EndLoc)) 3018 return true; 3019 Res = X86MCExpr::create(RegNo, Parser.getContext()); 3020 return false; 3021 } 3022 return Parser.parsePrimaryExpr(Res, EndLoc, nullptr); 3023 } 3024 3025 bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 3026 SMLoc NameLoc, OperandVector &Operands) { 3027 MCAsmParser &Parser = getParser(); 3028 InstInfo = &Info; 3029 3030 // Reset the forced VEX encoding. 3031 ForcedVEXEncoding = VEXEncoding_Default; 3032 ForcedDispEncoding = DispEncoding_Default; 3033 3034 // Parse pseudo prefixes. 3035 while (true) { 3036 if (Name == "{") { 3037 if (getLexer().isNot(AsmToken::Identifier)) 3038 return Error(Parser.getTok().getLoc(), "Unexpected token after '{'"); 3039 std::string Prefix = Parser.getTok().getString().lower(); 3040 Parser.Lex(); // Eat identifier. 3041 if (getLexer().isNot(AsmToken::RCurly)) 3042 return Error(Parser.getTok().getLoc(), "Expected '}'"); 3043 Parser.Lex(); // Eat curly. 3044 3045 if (Prefix == "vex") 3046 ForcedVEXEncoding = VEXEncoding_VEX; 3047 else if (Prefix == "vex2") 3048 ForcedVEXEncoding = VEXEncoding_VEX2; 3049 else if (Prefix == "vex3") 3050 ForcedVEXEncoding = VEXEncoding_VEX3; 3051 else if (Prefix == "evex") 3052 ForcedVEXEncoding = VEXEncoding_EVEX; 3053 else if (Prefix == "disp8") 3054 ForcedDispEncoding = DispEncoding_Disp8; 3055 else if (Prefix == "disp32") 3056 ForcedDispEncoding = DispEncoding_Disp32; 3057 else 3058 return Error(NameLoc, "unknown prefix"); 3059 3060 NameLoc = Parser.getTok().getLoc(); 3061 if (getLexer().is(AsmToken::LCurly)) { 3062 Parser.Lex(); 3063 Name = "{"; 3064 } else { 3065 if (getLexer().isNot(AsmToken::Identifier)) 3066 return Error(Parser.getTok().getLoc(), "Expected identifier"); 3067 // FIXME: The mnemonic won't match correctly if its not in lower case. 3068 Name = Parser.getTok().getString(); 3069 Parser.Lex(); 3070 } 3071 continue; 3072 } 3073 // Parse MASM style pseudo prefixes. 3074 if (isParsingMSInlineAsm()) { 3075 if (Name.equals_insensitive("vex")) 3076 ForcedVEXEncoding = VEXEncoding_VEX; 3077 else if (Name.equals_insensitive("vex2")) 3078 ForcedVEXEncoding = VEXEncoding_VEX2; 3079 else if (Name.equals_insensitive("vex3")) 3080 ForcedVEXEncoding = VEXEncoding_VEX3; 3081 else if (Name.equals_insensitive("evex")) 3082 ForcedVEXEncoding = VEXEncoding_EVEX; 3083 3084 if (ForcedVEXEncoding != VEXEncoding_Default) { 3085 if (getLexer().isNot(AsmToken::Identifier)) 3086 return Error(Parser.getTok().getLoc(), "Expected identifier"); 3087 // FIXME: The mnemonic won't match correctly if its not in lower case. 3088 Name = Parser.getTok().getString(); 3089 NameLoc = Parser.getTok().getLoc(); 3090 Parser.Lex(); 3091 } 3092 } 3093 break; 3094 } 3095 3096 // Support the suffix syntax for overriding displacement size as well. 3097 if (Name.consume_back(".d32")) { 3098 ForcedDispEncoding = DispEncoding_Disp32; 3099 } else if (Name.consume_back(".d8")) { 3100 ForcedDispEncoding = DispEncoding_Disp8; 3101 } 3102 3103 StringRef PatchedName = Name; 3104 3105 // Hack to skip "short" following Jcc. 3106 if (isParsingIntelSyntax() && 3107 (PatchedName == "jmp" || PatchedName == "jc" || PatchedName == "jnc" || 3108 PatchedName == "jcxz" || PatchedName == "jecxz" || 3109 (PatchedName.startswith("j") && 3110 ParseConditionCode(PatchedName.substr(1)) != X86::COND_INVALID))) { 3111 StringRef NextTok = Parser.getTok().getString(); 3112 if (Parser.isParsingMasm() ? NextTok.equals_insensitive("short") 3113 : NextTok == "short") { 3114 SMLoc NameEndLoc = 3115 NameLoc.getFromPointer(NameLoc.getPointer() + Name.size()); 3116 // Eat the short keyword. 3117 Parser.Lex(); 3118 // MS and GAS ignore the short keyword; they both determine the jmp type 3119 // based on the distance of the label. (NASM does emit different code with 3120 // and without "short," though.) 3121 InstInfo->AsmRewrites->emplace_back(AOK_Skip, NameEndLoc, 3122 NextTok.size() + 1); 3123 } 3124 } 3125 3126 // FIXME: Hack to recognize setneb as setne. 3127 if (PatchedName.startswith("set") && PatchedName.endswith("b") && 3128 PatchedName != "setb" && PatchedName != "setnb") 3129 PatchedName = PatchedName.substr(0, Name.size()-1); 3130 3131 unsigned ComparisonPredicate = ~0U; 3132 3133 // FIXME: Hack to recognize cmp<comparison code>{sh,ss,sd,ph,ps,pd}. 3134 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) && 3135 (PatchedName.endswith("ss") || PatchedName.endswith("sd") || 3136 PatchedName.endswith("sh") || PatchedName.endswith("ph") || 3137 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) { 3138 bool IsVCMP = PatchedName[0] == 'v'; 3139 unsigned CCIdx = IsVCMP ? 4 : 3; 3140 unsigned CC = StringSwitch<unsigned>( 3141 PatchedName.slice(CCIdx, PatchedName.size() - 2)) 3142 .Case("eq", 0x00) 3143 .Case("eq_oq", 0x00) 3144 .Case("lt", 0x01) 3145 .Case("lt_os", 0x01) 3146 .Case("le", 0x02) 3147 .Case("le_os", 0x02) 3148 .Case("unord", 0x03) 3149 .Case("unord_q", 0x03) 3150 .Case("neq", 0x04) 3151 .Case("neq_uq", 0x04) 3152 .Case("nlt", 0x05) 3153 .Case("nlt_us", 0x05) 3154 .Case("nle", 0x06) 3155 .Case("nle_us", 0x06) 3156 .Case("ord", 0x07) 3157 .Case("ord_q", 0x07) 3158 /* AVX only from here */ 3159 .Case("eq_uq", 0x08) 3160 .Case("nge", 0x09) 3161 .Case("nge_us", 0x09) 3162 .Case("ngt", 0x0A) 3163 .Case("ngt_us", 0x0A) 3164 .Case("false", 0x0B) 3165 .Case("false_oq", 0x0B) 3166 .Case("neq_oq", 0x0C) 3167 .Case("ge", 0x0D) 3168 .Case("ge_os", 0x0D) 3169 .Case("gt", 0x0E) 3170 .Case("gt_os", 0x0E) 3171 .Case("true", 0x0F) 3172 .Case("true_uq", 0x0F) 3173 .Case("eq_os", 0x10) 3174 .Case("lt_oq", 0x11) 3175 .Case("le_oq", 0x12) 3176 .Case("unord_s", 0x13) 3177 .Case("neq_us", 0x14) 3178 .Case("nlt_uq", 0x15) 3179 .Case("nle_uq", 0x16) 3180 .Case("ord_s", 0x17) 3181 .Case("eq_us", 0x18) 3182 .Case("nge_uq", 0x19) 3183 .Case("ngt_uq", 0x1A) 3184 .Case("false_os", 0x1B) 3185 .Case("neq_os", 0x1C) 3186 .Case("ge_oq", 0x1D) 3187 .Case("gt_oq", 0x1E) 3188 .Case("true_us", 0x1F) 3189 .Default(~0U); 3190 if (CC != ~0U && (IsVCMP || CC < 8) && 3191 (IsVCMP || PatchedName.back() != 'h')) { 3192 if (PatchedName.endswith("ss")) 3193 PatchedName = IsVCMP ? "vcmpss" : "cmpss"; 3194 else if (PatchedName.endswith("sd")) 3195 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd"; 3196 else if (PatchedName.endswith("ps")) 3197 PatchedName = IsVCMP ? "vcmpps" : "cmpps"; 3198 else if (PatchedName.endswith("pd")) 3199 PatchedName = IsVCMP ? "vcmppd" : "cmppd"; 3200 else if (PatchedName.endswith("sh")) 3201 PatchedName = "vcmpsh"; 3202 else if (PatchedName.endswith("ph")) 3203 PatchedName = "vcmpph"; 3204 else 3205 llvm_unreachable("Unexpected suffix!"); 3206 3207 ComparisonPredicate = CC; 3208 } 3209 } 3210 3211 // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}. 3212 if (PatchedName.startswith("vpcmp") && 3213 (PatchedName.back() == 'b' || PatchedName.back() == 'w' || 3214 PatchedName.back() == 'd' || PatchedName.back() == 'q')) { 3215 unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1; 3216 unsigned CC = StringSwitch<unsigned>( 3217 PatchedName.slice(5, PatchedName.size() - SuffixSize)) 3218 .Case("eq", 0x0) // Only allowed on unsigned. Checked below. 3219 .Case("lt", 0x1) 3220 .Case("le", 0x2) 3221 //.Case("false", 0x3) // Not a documented alias. 3222 .Case("neq", 0x4) 3223 .Case("nlt", 0x5) 3224 .Case("nle", 0x6) 3225 //.Case("true", 0x7) // Not a documented alias. 3226 .Default(~0U); 3227 if (CC != ~0U && (CC != 0 || SuffixSize == 2)) { 3228 switch (PatchedName.back()) { 3229 default: llvm_unreachable("Unexpected character!"); 3230 case 'b': PatchedName = SuffixSize == 2 ? "vpcmpub" : "vpcmpb"; break; 3231 case 'w': PatchedName = SuffixSize == 2 ? "vpcmpuw" : "vpcmpw"; break; 3232 case 'd': PatchedName = SuffixSize == 2 ? "vpcmpud" : "vpcmpd"; break; 3233 case 'q': PatchedName = SuffixSize == 2 ? "vpcmpuq" : "vpcmpq"; break; 3234 } 3235 // Set up the immediate to push into the operands later. 3236 ComparisonPredicate = CC; 3237 } 3238 } 3239 3240 // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}. 3241 if (PatchedName.startswith("vpcom") && 3242 (PatchedName.back() == 'b' || PatchedName.back() == 'w' || 3243 PatchedName.back() == 'd' || PatchedName.back() == 'q')) { 3244 unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1; 3245 unsigned CC = StringSwitch<unsigned>( 3246 PatchedName.slice(5, PatchedName.size() - SuffixSize)) 3247 .Case("lt", 0x0) 3248 .Case("le", 0x1) 3249 .Case("gt", 0x2) 3250 .Case("ge", 0x3) 3251 .Case("eq", 0x4) 3252 .Case("neq", 0x5) 3253 .Case("false", 0x6) 3254 .Case("true", 0x7) 3255 .Default(~0U); 3256 if (CC != ~0U) { 3257 switch (PatchedName.back()) { 3258 default: llvm_unreachable("Unexpected character!"); 3259 case 'b': PatchedName = SuffixSize == 2 ? "vpcomub" : "vpcomb"; break; 3260 case 'w': PatchedName = SuffixSize == 2 ? "vpcomuw" : "vpcomw"; break; 3261 case 'd': PatchedName = SuffixSize == 2 ? "vpcomud" : "vpcomd"; break; 3262 case 'q': PatchedName = SuffixSize == 2 ? "vpcomuq" : "vpcomq"; break; 3263 } 3264 // Set up the immediate to push into the operands later. 3265 ComparisonPredicate = CC; 3266 } 3267 } 3268 3269 3270 // Determine whether this is an instruction prefix. 3271 // FIXME: 3272 // Enhance prefixes integrity robustness. for example, following forms 3273 // are currently tolerated: 3274 // repz repnz <insn> ; GAS errors for the use of two similar prefixes 3275 // lock addq %rax, %rbx ; Destination operand must be of memory type 3276 // xacquire <insn> ; xacquire must be accompanied by 'lock' 3277 bool IsPrefix = 3278 StringSwitch<bool>(Name) 3279 .Cases("cs", "ds", "es", "fs", "gs", "ss", true) 3280 .Cases("rex64", "data32", "data16", "addr32", "addr16", true) 3281 .Cases("xacquire", "xrelease", true) 3282 .Cases("acquire", "release", isParsingIntelSyntax()) 3283 .Default(false); 3284 3285 auto isLockRepeatNtPrefix = [](StringRef N) { 3286 return StringSwitch<bool>(N) 3287 .Cases("lock", "rep", "repe", "repz", "repne", "repnz", "notrack", true) 3288 .Default(false); 3289 }; 3290 3291 bool CurlyAsEndOfStatement = false; 3292 3293 unsigned Flags = X86::IP_NO_PREFIX; 3294 while (isLockRepeatNtPrefix(Name.lower())) { 3295 unsigned Prefix = 3296 StringSwitch<unsigned>(Name) 3297 .Cases("lock", "lock", X86::IP_HAS_LOCK) 3298 .Cases("rep", "repe", "repz", X86::IP_HAS_REPEAT) 3299 .Cases("repne", "repnz", X86::IP_HAS_REPEAT_NE) 3300 .Cases("notrack", "notrack", X86::IP_HAS_NOTRACK) 3301 .Default(X86::IP_NO_PREFIX); // Invalid prefix (impossible) 3302 Flags |= Prefix; 3303 if (getLexer().is(AsmToken::EndOfStatement)) { 3304 // We don't have real instr with the given prefix 3305 // let's use the prefix as the instr. 3306 // TODO: there could be several prefixes one after another 3307 Flags = X86::IP_NO_PREFIX; 3308 break; 3309 } 3310 // FIXME: The mnemonic won't match correctly if its not in lower case. 3311 Name = Parser.getTok().getString(); 3312 Parser.Lex(); // eat the prefix 3313 // Hack: we could have something like "rep # some comment" or 3314 // "lock; cmpxchg16b $1" or "lock\0A\09incl" or "lock/incl" 3315 while (Name.startswith(";") || Name.startswith("\n") || 3316 Name.startswith("#") || Name.startswith("\t") || 3317 Name.startswith("/")) { 3318 // FIXME: The mnemonic won't match correctly if its not in lower case. 3319 Name = Parser.getTok().getString(); 3320 Parser.Lex(); // go to next prefix or instr 3321 } 3322 } 3323 3324 if (Flags) 3325 PatchedName = Name; 3326 3327 // Hacks to handle 'data16' and 'data32' 3328 if (PatchedName == "data16" && is16BitMode()) { 3329 return Error(NameLoc, "redundant data16 prefix"); 3330 } 3331 if (PatchedName == "data32") { 3332 if (is32BitMode()) 3333 return Error(NameLoc, "redundant data32 prefix"); 3334 if (is64BitMode()) 3335 return Error(NameLoc, "'data32' is not supported in 64-bit mode"); 3336 // Hack to 'data16' for the table lookup. 3337 PatchedName = "data16"; 3338 3339 if (getLexer().isNot(AsmToken::EndOfStatement)) { 3340 StringRef Next = Parser.getTok().getString(); 3341 getLexer().Lex(); 3342 // data32 effectively changes the instruction suffix. 3343 // TODO Generalize. 3344 if (Next == "callw") 3345 Next = "calll"; 3346 if (Next == "ljmpw") 3347 Next = "ljmpl"; 3348 3349 Name = Next; 3350 PatchedName = Name; 3351 ForcedDataPrefix = X86::Is32Bit; 3352 IsPrefix = false; 3353 } 3354 } 3355 3356 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc)); 3357 3358 // Push the immediate if we extracted one from the mnemonic. 3359 if (ComparisonPredicate != ~0U && !isParsingIntelSyntax()) { 3360 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate, 3361 getParser().getContext()); 3362 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc)); 3363 } 3364 3365 // This does the actual operand parsing. Don't parse any more if we have a 3366 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we 3367 // just want to parse the "lock" as the first instruction and the "incl" as 3368 // the next one. 3369 if (getLexer().isNot(AsmToken::EndOfStatement) && !IsPrefix) { 3370 // Parse '*' modifier. 3371 if (getLexer().is(AsmToken::Star)) 3372 Operands.push_back(X86Operand::CreateToken("*", consumeToken())); 3373 3374 // Read the operands. 3375 while (true) { 3376 if (ParseOperand(Operands)) 3377 return true; 3378 if (HandleAVX512Operand(Operands)) 3379 return true; 3380 3381 // check for comma and eat it 3382 if (getLexer().is(AsmToken::Comma)) 3383 Parser.Lex(); 3384 else 3385 break; 3386 } 3387 3388 // In MS inline asm curly braces mark the beginning/end of a block, 3389 // therefore they should be interepreted as end of statement 3390 CurlyAsEndOfStatement = 3391 isParsingIntelSyntax() && isParsingMSInlineAsm() && 3392 (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly)); 3393 if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement) 3394 return TokError("unexpected token in argument list"); 3395 } 3396 3397 // Push the immediate if we extracted one from the mnemonic. 3398 if (ComparisonPredicate != ~0U && isParsingIntelSyntax()) { 3399 const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate, 3400 getParser().getContext()); 3401 Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc)); 3402 } 3403 3404 // Consume the EndOfStatement or the prefix separator Slash 3405 if (getLexer().is(AsmToken::EndOfStatement) || 3406 (IsPrefix && getLexer().is(AsmToken::Slash))) 3407 Parser.Lex(); 3408 else if (CurlyAsEndOfStatement) 3409 // Add an actual EndOfStatement before the curly brace 3410 Info.AsmRewrites->emplace_back(AOK_EndOfStatement, 3411 getLexer().getTok().getLoc(), 0); 3412 3413 // This is for gas compatibility and cannot be done in td. 3414 // Adding "p" for some floating point with no argument. 3415 // For example: fsub --> fsubp 3416 bool IsFp = 3417 Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr"; 3418 if (IsFp && Operands.size() == 1) { 3419 const char *Repl = StringSwitch<const char *>(Name) 3420 .Case("fsub", "fsubp") 3421 .Case("fdiv", "fdivp") 3422 .Case("fsubr", "fsubrp") 3423 .Case("fdivr", "fdivrp"); 3424 static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl); 3425 } 3426 3427 if ((Name == "mov" || Name == "movw" || Name == "movl") && 3428 (Operands.size() == 3)) { 3429 X86Operand &Op1 = (X86Operand &)*Operands[1]; 3430 X86Operand &Op2 = (X86Operand &)*Operands[2]; 3431 SMLoc Loc = Op1.getEndLoc(); 3432 // Moving a 32 or 16 bit value into a segment register has the same 3433 // behavior. Modify such instructions to always take shorter form. 3434 if (Op1.isReg() && Op2.isReg() && 3435 X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains( 3436 Op2.getReg()) && 3437 (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) || 3438 X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) { 3439 // Change instruction name to match new instruction. 3440 if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) { 3441 Name = is16BitMode() ? "movw" : "movl"; 3442 Operands[0] = X86Operand::CreateToken(Name, NameLoc); 3443 } 3444 // Select the correct equivalent 16-/32-bit source register. 3445 unsigned Reg = 3446 getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32); 3447 Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc); 3448 } 3449 } 3450 3451 // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" -> 3452 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely 3453 // documented form in various unofficial manuals, so a lot of code uses it. 3454 if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" || 3455 Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") && 3456 Operands.size() == 3) { 3457 X86Operand &Op = (X86Operand &)*Operands.back(); 3458 if (Op.isDXReg()) 3459 Operands.back() = X86Operand::CreateReg(X86::DX, Op.getStartLoc(), 3460 Op.getEndLoc()); 3461 } 3462 // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al". 3463 if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" || 3464 Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") && 3465 Operands.size() == 3) { 3466 X86Operand &Op = (X86Operand &)*Operands[1]; 3467 if (Op.isDXReg()) 3468 Operands[1] = X86Operand::CreateReg(X86::DX, Op.getStartLoc(), 3469 Op.getEndLoc()); 3470 } 3471 3472 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands; 3473 bool HadVerifyError = false; 3474 3475 // Append default arguments to "ins[bwld]" 3476 if (Name.startswith("ins") && 3477 (Operands.size() == 1 || Operands.size() == 3) && 3478 (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" || 3479 Name == "ins")) { 3480 3481 AddDefaultSrcDestOperands(TmpOperands, 3482 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc), 3483 DefaultMemDIOperand(NameLoc)); 3484 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3485 } 3486 3487 // Append default arguments to "outs[bwld]" 3488 if (Name.startswith("outs") && 3489 (Operands.size() == 1 || Operands.size() == 3) && 3490 (Name == "outsb" || Name == "outsw" || Name == "outsl" || 3491 Name == "outsd" || Name == "outs")) { 3492 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc), 3493 X86Operand::CreateReg(X86::DX, NameLoc, NameLoc)); 3494 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3495 } 3496 3497 // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate 3498 // values of $SIREG according to the mode. It would be nice if this 3499 // could be achieved with InstAlias in the tables. 3500 if (Name.startswith("lods") && 3501 (Operands.size() == 1 || Operands.size() == 2) && 3502 (Name == "lods" || Name == "lodsb" || Name == "lodsw" || 3503 Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) { 3504 TmpOperands.push_back(DefaultMemSIOperand(NameLoc)); 3505 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3506 } 3507 3508 // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate 3509 // values of $DIREG according to the mode. It would be nice if this 3510 // could be achieved with InstAlias in the tables. 3511 if (Name.startswith("stos") && 3512 (Operands.size() == 1 || Operands.size() == 2) && 3513 (Name == "stos" || Name == "stosb" || Name == "stosw" || 3514 Name == "stosl" || Name == "stosd" || Name == "stosq")) { 3515 TmpOperands.push_back(DefaultMemDIOperand(NameLoc)); 3516 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3517 } 3518 3519 // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate 3520 // values of $DIREG according to the mode. It would be nice if this 3521 // could be achieved with InstAlias in the tables. 3522 if (Name.startswith("scas") && 3523 (Operands.size() == 1 || Operands.size() == 2) && 3524 (Name == "scas" || Name == "scasb" || Name == "scasw" || 3525 Name == "scasl" || Name == "scasd" || Name == "scasq")) { 3526 TmpOperands.push_back(DefaultMemDIOperand(NameLoc)); 3527 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3528 } 3529 3530 // Add default SI and DI operands to "cmps[bwlq]". 3531 if (Name.startswith("cmps") && 3532 (Operands.size() == 1 || Operands.size() == 3) && 3533 (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" || 3534 Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) { 3535 AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc), 3536 DefaultMemSIOperand(NameLoc)); 3537 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3538 } 3539 3540 // Add default SI and DI operands to "movs[bwlq]". 3541 if (((Name.startswith("movs") && 3542 (Name == "movs" || Name == "movsb" || Name == "movsw" || 3543 Name == "movsl" || Name == "movsd" || Name == "movsq")) || 3544 (Name.startswith("smov") && 3545 (Name == "smov" || Name == "smovb" || Name == "smovw" || 3546 Name == "smovl" || Name == "smovd" || Name == "smovq"))) && 3547 (Operands.size() == 1 || Operands.size() == 3)) { 3548 if (Name == "movsd" && Operands.size() == 1 && !isParsingIntelSyntax()) 3549 Operands.back() = X86Operand::CreateToken("movsl", NameLoc); 3550 AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc), 3551 DefaultMemDIOperand(NameLoc)); 3552 HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands); 3553 } 3554 3555 // Check if we encountered an error for one the string insturctions 3556 if (HadVerifyError) { 3557 return HadVerifyError; 3558 } 3559 3560 // Transforms "xlat mem8" into "xlatb" 3561 if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) { 3562 X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]); 3563 if (Op1.isMem8()) { 3564 Warning(Op1.getStartLoc(), "memory operand is only for determining the " 3565 "size, (R|E)BX will be used for the location"); 3566 Operands.pop_back(); 3567 static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb"); 3568 } 3569 } 3570 3571 if (Flags) 3572 Operands.push_back(X86Operand::CreatePrefix(Flags, NameLoc, NameLoc)); 3573 return false; 3574 } 3575 3576 bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) { 3577 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 3578 3579 switch (Inst.getOpcode()) { 3580 default: return false; 3581 case X86::JMP_1: 3582 // {disp32} forces a larger displacement as if the instruction was relaxed. 3583 // NOTE: 16-bit mode uses 16-bit displacement even though it says {disp32}. 3584 // This matches GNU assembler. 3585 if (ForcedDispEncoding == DispEncoding_Disp32) { 3586 Inst.setOpcode(is16BitMode() ? X86::JMP_2 : X86::JMP_4); 3587 return true; 3588 } 3589 3590 return false; 3591 case X86::JCC_1: 3592 // {disp32} forces a larger displacement as if the instruction was relaxed. 3593 // NOTE: 16-bit mode uses 16-bit displacement even though it says {disp32}. 3594 // This matches GNU assembler. 3595 if (ForcedDispEncoding == DispEncoding_Disp32) { 3596 Inst.setOpcode(is16BitMode() ? X86::JCC_2 : X86::JCC_4); 3597 return true; 3598 } 3599 3600 return false; 3601 case X86::VMOVZPQILo2PQIrr: 3602 case X86::VMOVAPDrr: 3603 case X86::VMOVAPDYrr: 3604 case X86::VMOVAPSrr: 3605 case X86::VMOVAPSYrr: 3606 case X86::VMOVDQArr: 3607 case X86::VMOVDQAYrr: 3608 case X86::VMOVDQUrr: 3609 case X86::VMOVDQUYrr: 3610 case X86::VMOVUPDrr: 3611 case X86::VMOVUPDYrr: 3612 case X86::VMOVUPSrr: 3613 case X86::VMOVUPSYrr: { 3614 // We can get a smaller encoding by using VEX.R instead of VEX.B if one of 3615 // the registers is extended, but other isn't. 3616 if (ForcedVEXEncoding == VEXEncoding_VEX3 || 3617 MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 || 3618 MRI->getEncodingValue(Inst.getOperand(1).getReg()) < 8) 3619 return false; 3620 3621 unsigned NewOpc; 3622 switch (Inst.getOpcode()) { 3623 default: llvm_unreachable("Invalid opcode"); 3624 case X86::VMOVZPQILo2PQIrr: NewOpc = X86::VMOVPQI2QIrr; break; 3625 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break; 3626 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break; 3627 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break; 3628 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break; 3629 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break; 3630 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break; 3631 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break; 3632 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break; 3633 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break; 3634 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break; 3635 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break; 3636 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break; 3637 } 3638 Inst.setOpcode(NewOpc); 3639 return true; 3640 } 3641 case X86::VMOVSDrr: 3642 case X86::VMOVSSrr: { 3643 // We can get a smaller encoding by using VEX.R instead of VEX.B if one of 3644 // the registers is extended, but other isn't. 3645 if (ForcedVEXEncoding == VEXEncoding_VEX3 || 3646 MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 || 3647 MRI->getEncodingValue(Inst.getOperand(2).getReg()) < 8) 3648 return false; 3649 3650 unsigned NewOpc; 3651 switch (Inst.getOpcode()) { 3652 default: llvm_unreachable("Invalid opcode"); 3653 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break; 3654 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break; 3655 } 3656 Inst.setOpcode(NewOpc); 3657 return true; 3658 } 3659 case X86::RCR8ri: case X86::RCR16ri: case X86::RCR32ri: case X86::RCR64ri: 3660 case X86::RCL8ri: case X86::RCL16ri: case X86::RCL32ri: case X86::RCL64ri: 3661 case X86::ROR8ri: case X86::ROR16ri: case X86::ROR32ri: case X86::ROR64ri: 3662 case X86::ROL8ri: case X86::ROL16ri: case X86::ROL32ri: case X86::ROL64ri: 3663 case X86::SAR8ri: case X86::SAR16ri: case X86::SAR32ri: case X86::SAR64ri: 3664 case X86::SHR8ri: case X86::SHR16ri: case X86::SHR32ri: case X86::SHR64ri: 3665 case X86::SHL8ri: case X86::SHL16ri: case X86::SHL32ri: case X86::SHL64ri: { 3666 // Optimize s{hr,ar,hl} $1, <op> to "shift <op>". Similar for rotate. 3667 // FIXME: It would be great if we could just do this with an InstAlias. 3668 if (!Inst.getOperand(2).isImm() || Inst.getOperand(2).getImm() != 1) 3669 return false; 3670 3671 unsigned NewOpc; 3672 switch (Inst.getOpcode()) { 3673 default: llvm_unreachable("Invalid opcode"); 3674 case X86::RCR8ri: NewOpc = X86::RCR8r1; break; 3675 case X86::RCR16ri: NewOpc = X86::RCR16r1; break; 3676 case X86::RCR32ri: NewOpc = X86::RCR32r1; break; 3677 case X86::RCR64ri: NewOpc = X86::RCR64r1; break; 3678 case X86::RCL8ri: NewOpc = X86::RCL8r1; break; 3679 case X86::RCL16ri: NewOpc = X86::RCL16r1; break; 3680 case X86::RCL32ri: NewOpc = X86::RCL32r1; break; 3681 case X86::RCL64ri: NewOpc = X86::RCL64r1; break; 3682 case X86::ROR8ri: NewOpc = X86::ROR8r1; break; 3683 case X86::ROR16ri: NewOpc = X86::ROR16r1; break; 3684 case X86::ROR32ri: NewOpc = X86::ROR32r1; break; 3685 case X86::ROR64ri: NewOpc = X86::ROR64r1; break; 3686 case X86::ROL8ri: NewOpc = X86::ROL8r1; break; 3687 case X86::ROL16ri: NewOpc = X86::ROL16r1; break; 3688 case X86::ROL32ri: NewOpc = X86::ROL32r1; break; 3689 case X86::ROL64ri: NewOpc = X86::ROL64r1; break; 3690 case X86::SAR8ri: NewOpc = X86::SAR8r1; break; 3691 case X86::SAR16ri: NewOpc = X86::SAR16r1; break; 3692 case X86::SAR32ri: NewOpc = X86::SAR32r1; break; 3693 case X86::SAR64ri: NewOpc = X86::SAR64r1; break; 3694 case X86::SHR8ri: NewOpc = X86::SHR8r1; break; 3695 case X86::SHR16ri: NewOpc = X86::SHR16r1; break; 3696 case X86::SHR32ri: NewOpc = X86::SHR32r1; break; 3697 case X86::SHR64ri: NewOpc = X86::SHR64r1; break; 3698 case X86::SHL8ri: NewOpc = X86::SHL8r1; break; 3699 case X86::SHL16ri: NewOpc = X86::SHL16r1; break; 3700 case X86::SHL32ri: NewOpc = X86::SHL32r1; break; 3701 case X86::SHL64ri: NewOpc = X86::SHL64r1; break; 3702 } 3703 3704 MCInst TmpInst; 3705 TmpInst.setOpcode(NewOpc); 3706 TmpInst.addOperand(Inst.getOperand(0)); 3707 TmpInst.addOperand(Inst.getOperand(1)); 3708 Inst = TmpInst; 3709 return true; 3710 } 3711 case X86::RCR8mi: case X86::RCR16mi: case X86::RCR32mi: case X86::RCR64mi: 3712 case X86::RCL8mi: case X86::RCL16mi: case X86::RCL32mi: case X86::RCL64mi: 3713 case X86::ROR8mi: case X86::ROR16mi: case X86::ROR32mi: case X86::ROR64mi: 3714 case X86::ROL8mi: case X86::ROL16mi: case X86::ROL32mi: case X86::ROL64mi: 3715 case X86::SAR8mi: case X86::SAR16mi: case X86::SAR32mi: case X86::SAR64mi: 3716 case X86::SHR8mi: case X86::SHR16mi: case X86::SHR32mi: case X86::SHR64mi: 3717 case X86::SHL8mi: case X86::SHL16mi: case X86::SHL32mi: case X86::SHL64mi: { 3718 // Optimize s{hr,ar,hl} $1, <op> to "shift <op>". Similar for rotate. 3719 // FIXME: It would be great if we could just do this with an InstAlias. 3720 if (!Inst.getOperand(X86::AddrNumOperands).isImm() || 3721 Inst.getOperand(X86::AddrNumOperands).getImm() != 1) 3722 return false; 3723 3724 unsigned NewOpc; 3725 switch (Inst.getOpcode()) { 3726 default: llvm_unreachable("Invalid opcode"); 3727 case X86::RCR8mi: NewOpc = X86::RCR8m1; break; 3728 case X86::RCR16mi: NewOpc = X86::RCR16m1; break; 3729 case X86::RCR32mi: NewOpc = X86::RCR32m1; break; 3730 case X86::RCR64mi: NewOpc = X86::RCR64m1; break; 3731 case X86::RCL8mi: NewOpc = X86::RCL8m1; break; 3732 case X86::RCL16mi: NewOpc = X86::RCL16m1; break; 3733 case X86::RCL32mi: NewOpc = X86::RCL32m1; break; 3734 case X86::RCL64mi: NewOpc = X86::RCL64m1; break; 3735 case X86::ROR8mi: NewOpc = X86::ROR8m1; break; 3736 case X86::ROR16mi: NewOpc = X86::ROR16m1; break; 3737 case X86::ROR32mi: NewOpc = X86::ROR32m1; break; 3738 case X86::ROR64mi: NewOpc = X86::ROR64m1; break; 3739 case X86::ROL8mi: NewOpc = X86::ROL8m1; break; 3740 case X86::ROL16mi: NewOpc = X86::ROL16m1; break; 3741 case X86::ROL32mi: NewOpc = X86::ROL32m1; break; 3742 case X86::ROL64mi: NewOpc = X86::ROL64m1; break; 3743 case X86::SAR8mi: NewOpc = X86::SAR8m1; break; 3744 case X86::SAR16mi: NewOpc = X86::SAR16m1; break; 3745 case X86::SAR32mi: NewOpc = X86::SAR32m1; break; 3746 case X86::SAR64mi: NewOpc = X86::SAR64m1; break; 3747 case X86::SHR8mi: NewOpc = X86::SHR8m1; break; 3748 case X86::SHR16mi: NewOpc = X86::SHR16m1; break; 3749 case X86::SHR32mi: NewOpc = X86::SHR32m1; break; 3750 case X86::SHR64mi: NewOpc = X86::SHR64m1; break; 3751 case X86::SHL8mi: NewOpc = X86::SHL8m1; break; 3752 case X86::SHL16mi: NewOpc = X86::SHL16m1; break; 3753 case X86::SHL32mi: NewOpc = X86::SHL32m1; break; 3754 case X86::SHL64mi: NewOpc = X86::SHL64m1; break; 3755 } 3756 3757 MCInst TmpInst; 3758 TmpInst.setOpcode(NewOpc); 3759 for (int i = 0; i != X86::AddrNumOperands; ++i) 3760 TmpInst.addOperand(Inst.getOperand(i)); 3761 Inst = TmpInst; 3762 return true; 3763 } 3764 case X86::INT: { 3765 // Transforms "int $3" into "int3" as a size optimization. We can't write an 3766 // instalias with an immediate operand yet. 3767 if (!Inst.getOperand(0).isImm() || Inst.getOperand(0).getImm() != 3) 3768 return false; 3769 3770 MCInst TmpInst; 3771 TmpInst.setOpcode(X86::INT3); 3772 Inst = TmpInst; 3773 return true; 3774 } 3775 } 3776 } 3777 3778 bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) { 3779 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 3780 3781 switch (Inst.getOpcode()) { 3782 case X86::VGATHERDPDYrm: 3783 case X86::VGATHERDPDrm: 3784 case X86::VGATHERDPSYrm: 3785 case X86::VGATHERDPSrm: 3786 case X86::VGATHERQPDYrm: 3787 case X86::VGATHERQPDrm: 3788 case X86::VGATHERQPSYrm: 3789 case X86::VGATHERQPSrm: 3790 case X86::VPGATHERDDYrm: 3791 case X86::VPGATHERDDrm: 3792 case X86::VPGATHERDQYrm: 3793 case X86::VPGATHERDQrm: 3794 case X86::VPGATHERQDYrm: 3795 case X86::VPGATHERQDrm: 3796 case X86::VPGATHERQQYrm: 3797 case X86::VPGATHERQQrm: { 3798 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg()); 3799 unsigned Mask = MRI->getEncodingValue(Inst.getOperand(1).getReg()); 3800 unsigned Index = 3801 MRI->getEncodingValue(Inst.getOperand(3 + X86::AddrIndexReg).getReg()); 3802 if (Dest == Mask || Dest == Index || Mask == Index) 3803 return Warning(Ops[0]->getStartLoc(), "mask, index, and destination " 3804 "registers should be distinct"); 3805 break; 3806 } 3807 case X86::VGATHERDPDZ128rm: 3808 case X86::VGATHERDPDZ256rm: 3809 case X86::VGATHERDPDZrm: 3810 case X86::VGATHERDPSZ128rm: 3811 case X86::VGATHERDPSZ256rm: 3812 case X86::VGATHERDPSZrm: 3813 case X86::VGATHERQPDZ128rm: 3814 case X86::VGATHERQPDZ256rm: 3815 case X86::VGATHERQPDZrm: 3816 case X86::VGATHERQPSZ128rm: 3817 case X86::VGATHERQPSZ256rm: 3818 case X86::VGATHERQPSZrm: 3819 case X86::VPGATHERDDZ128rm: 3820 case X86::VPGATHERDDZ256rm: 3821 case X86::VPGATHERDDZrm: 3822 case X86::VPGATHERDQZ128rm: 3823 case X86::VPGATHERDQZ256rm: 3824 case X86::VPGATHERDQZrm: 3825 case X86::VPGATHERQDZ128rm: 3826 case X86::VPGATHERQDZ256rm: 3827 case X86::VPGATHERQDZrm: 3828 case X86::VPGATHERQQZ128rm: 3829 case X86::VPGATHERQQZ256rm: 3830 case X86::VPGATHERQQZrm: { 3831 unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg()); 3832 unsigned Index = 3833 MRI->getEncodingValue(Inst.getOperand(4 + X86::AddrIndexReg).getReg()); 3834 if (Dest == Index) 3835 return Warning(Ops[0]->getStartLoc(), "index and destination registers " 3836 "should be distinct"); 3837 break; 3838 } 3839 case X86::V4FMADDPSrm: 3840 case X86::V4FMADDPSrmk: 3841 case X86::V4FMADDPSrmkz: 3842 case X86::V4FMADDSSrm: 3843 case X86::V4FMADDSSrmk: 3844 case X86::V4FMADDSSrmkz: 3845 case X86::V4FNMADDPSrm: 3846 case X86::V4FNMADDPSrmk: 3847 case X86::V4FNMADDPSrmkz: 3848 case X86::V4FNMADDSSrm: 3849 case X86::V4FNMADDSSrmk: 3850 case X86::V4FNMADDSSrmkz: 3851 case X86::VP4DPWSSDSrm: 3852 case X86::VP4DPWSSDSrmk: 3853 case X86::VP4DPWSSDSrmkz: 3854 case X86::VP4DPWSSDrm: 3855 case X86::VP4DPWSSDrmk: 3856 case X86::VP4DPWSSDrmkz: { 3857 unsigned Src2 = Inst.getOperand(Inst.getNumOperands() - 3858 X86::AddrNumOperands - 1).getReg(); 3859 unsigned Src2Enc = MRI->getEncodingValue(Src2); 3860 if (Src2Enc % 4 != 0) { 3861 StringRef RegName = X86IntelInstPrinter::getRegisterName(Src2); 3862 unsigned GroupStart = (Src2Enc / 4) * 4; 3863 unsigned GroupEnd = GroupStart + 3; 3864 return Warning(Ops[0]->getStartLoc(), 3865 "source register '" + RegName + "' implicitly denotes '" + 3866 RegName.take_front(3) + Twine(GroupStart) + "' to '" + 3867 RegName.take_front(3) + Twine(GroupEnd) + 3868 "' source group"); 3869 } 3870 break; 3871 } 3872 case X86::VFCMADDCPHZ128m: 3873 case X86::VFCMADDCPHZ256m: 3874 case X86::VFCMADDCPHZm: 3875 case X86::VFCMADDCPHZ128mb: 3876 case X86::VFCMADDCPHZ256mb: 3877 case X86::VFCMADDCPHZmb: 3878 case X86::VFCMADDCPHZ128mbk: 3879 case X86::VFCMADDCPHZ256mbk: 3880 case X86::VFCMADDCPHZmbk: 3881 case X86::VFCMADDCPHZ128mbkz: 3882 case X86::VFCMADDCPHZ256mbkz: 3883 case X86::VFCMADDCPHZmbkz: 3884 case X86::VFCMADDCPHZ128mk: 3885 case X86::VFCMADDCPHZ256mk: 3886 case X86::VFCMADDCPHZmk: 3887 case X86::VFCMADDCPHZ128mkz: 3888 case X86::VFCMADDCPHZ256mkz: 3889 case X86::VFCMADDCPHZmkz: 3890 case X86::VFCMADDCPHZ128r: 3891 case X86::VFCMADDCPHZ256r: 3892 case X86::VFCMADDCPHZr: 3893 case X86::VFCMADDCPHZ128rk: 3894 case X86::VFCMADDCPHZ256rk: 3895 case X86::VFCMADDCPHZrk: 3896 case X86::VFCMADDCPHZ128rkz: 3897 case X86::VFCMADDCPHZ256rkz: 3898 case X86::VFCMADDCPHZrkz: 3899 case X86::VFCMADDCPHZrb: 3900 case X86::VFCMADDCPHZrbk: 3901 case X86::VFCMADDCPHZrbkz: 3902 case X86::VFCMADDCSHZm: 3903 case X86::VFCMADDCSHZmk: 3904 case X86::VFCMADDCSHZmkz: 3905 case X86::VFCMADDCSHZr: 3906 case X86::VFCMADDCSHZrb: 3907 case X86::VFCMADDCSHZrbk: 3908 case X86::VFCMADDCSHZrbkz: 3909 case X86::VFCMADDCSHZrk: 3910 case X86::VFCMADDCSHZrkz: 3911 case X86::VFMADDCPHZ128m: 3912 case X86::VFMADDCPHZ256m: 3913 case X86::VFMADDCPHZm: 3914 case X86::VFMADDCPHZ128mb: 3915 case X86::VFMADDCPHZ256mb: 3916 case X86::VFMADDCPHZmb: 3917 case X86::VFMADDCPHZ128mbk: 3918 case X86::VFMADDCPHZ256mbk: 3919 case X86::VFMADDCPHZmbk: 3920 case X86::VFMADDCPHZ128mbkz: 3921 case X86::VFMADDCPHZ256mbkz: 3922 case X86::VFMADDCPHZmbkz: 3923 case X86::VFMADDCPHZ128mk: 3924 case X86::VFMADDCPHZ256mk: 3925 case X86::VFMADDCPHZmk: 3926 case X86::VFMADDCPHZ128mkz: 3927 case X86::VFMADDCPHZ256mkz: 3928 case X86::VFMADDCPHZmkz: 3929 case X86::VFMADDCPHZ128r: 3930 case X86::VFMADDCPHZ256r: 3931 case X86::VFMADDCPHZr: 3932 case X86::VFMADDCPHZ128rk: 3933 case X86::VFMADDCPHZ256rk: 3934 case X86::VFMADDCPHZrk: 3935 case X86::VFMADDCPHZ128rkz: 3936 case X86::VFMADDCPHZ256rkz: 3937 case X86::VFMADDCPHZrkz: 3938 case X86::VFMADDCPHZrb: 3939 case X86::VFMADDCPHZrbk: 3940 case X86::VFMADDCPHZrbkz: 3941 case X86::VFMADDCSHZm: 3942 case X86::VFMADDCSHZmk: 3943 case X86::VFMADDCSHZmkz: 3944 case X86::VFMADDCSHZr: 3945 case X86::VFMADDCSHZrb: 3946 case X86::VFMADDCSHZrbk: 3947 case X86::VFMADDCSHZrbkz: 3948 case X86::VFMADDCSHZrk: 3949 case X86::VFMADDCSHZrkz: { 3950 unsigned Dest = Inst.getOperand(0).getReg(); 3951 for (unsigned i = 2; i < Inst.getNumOperands(); i++) 3952 if (Inst.getOperand(i).isReg() && Dest == Inst.getOperand(i).getReg()) 3953 return Warning(Ops[0]->getStartLoc(), "Destination register should be " 3954 "distinct from source registers"); 3955 break; 3956 } 3957 case X86::VFCMULCPHZ128rm: 3958 case X86::VFCMULCPHZ256rm: 3959 case X86::VFCMULCPHZrm: 3960 case X86::VFCMULCPHZ128rmb: 3961 case X86::VFCMULCPHZ256rmb: 3962 case X86::VFCMULCPHZrmb: 3963 case X86::VFCMULCPHZ128rmbk: 3964 case X86::VFCMULCPHZ256rmbk: 3965 case X86::VFCMULCPHZrmbk: 3966 case X86::VFCMULCPHZ128rmbkz: 3967 case X86::VFCMULCPHZ256rmbkz: 3968 case X86::VFCMULCPHZrmbkz: 3969 case X86::VFCMULCPHZ128rmk: 3970 case X86::VFCMULCPHZ256rmk: 3971 case X86::VFCMULCPHZrmk: 3972 case X86::VFCMULCPHZ128rmkz: 3973 case X86::VFCMULCPHZ256rmkz: 3974 case X86::VFCMULCPHZrmkz: 3975 case X86::VFCMULCPHZ128rr: 3976 case X86::VFCMULCPHZ256rr: 3977 case X86::VFCMULCPHZrr: 3978 case X86::VFCMULCPHZ128rrk: 3979 case X86::VFCMULCPHZ256rrk: 3980 case X86::VFCMULCPHZrrk: 3981 case X86::VFCMULCPHZ128rrkz: 3982 case X86::VFCMULCPHZ256rrkz: 3983 case X86::VFCMULCPHZrrkz: 3984 case X86::VFCMULCPHZrrb: 3985 case X86::VFCMULCPHZrrbk: 3986 case X86::VFCMULCPHZrrbkz: 3987 case X86::VFCMULCSHZrm: 3988 case X86::VFCMULCSHZrmk: 3989 case X86::VFCMULCSHZrmkz: 3990 case X86::VFCMULCSHZrr: 3991 case X86::VFCMULCSHZrrb: 3992 case X86::VFCMULCSHZrrbk: 3993 case X86::VFCMULCSHZrrbkz: 3994 case X86::VFCMULCSHZrrk: 3995 case X86::VFCMULCSHZrrkz: 3996 case X86::VFMULCPHZ128rm: 3997 case X86::VFMULCPHZ256rm: 3998 case X86::VFMULCPHZrm: 3999 case X86::VFMULCPHZ128rmb: 4000 case X86::VFMULCPHZ256rmb: 4001 case X86::VFMULCPHZrmb: 4002 case X86::VFMULCPHZ128rmbk: 4003 case X86::VFMULCPHZ256rmbk: 4004 case X86::VFMULCPHZrmbk: 4005 case X86::VFMULCPHZ128rmbkz: 4006 case X86::VFMULCPHZ256rmbkz: 4007 case X86::VFMULCPHZrmbkz: 4008 case X86::VFMULCPHZ128rmk: 4009 case X86::VFMULCPHZ256rmk: 4010 case X86::VFMULCPHZrmk: 4011 case X86::VFMULCPHZ128rmkz: 4012 case X86::VFMULCPHZ256rmkz: 4013 case X86::VFMULCPHZrmkz: 4014 case X86::VFMULCPHZ128rr: 4015 case X86::VFMULCPHZ256rr: 4016 case X86::VFMULCPHZrr: 4017 case X86::VFMULCPHZ128rrk: 4018 case X86::VFMULCPHZ256rrk: 4019 case X86::VFMULCPHZrrk: 4020 case X86::VFMULCPHZ128rrkz: 4021 case X86::VFMULCPHZ256rrkz: 4022 case X86::VFMULCPHZrrkz: 4023 case X86::VFMULCPHZrrb: 4024 case X86::VFMULCPHZrrbk: 4025 case X86::VFMULCPHZrrbkz: 4026 case X86::VFMULCSHZrm: 4027 case X86::VFMULCSHZrmk: 4028 case X86::VFMULCSHZrmkz: 4029 case X86::VFMULCSHZrr: 4030 case X86::VFMULCSHZrrb: 4031 case X86::VFMULCSHZrrbk: 4032 case X86::VFMULCSHZrrbkz: 4033 case X86::VFMULCSHZrrk: 4034 case X86::VFMULCSHZrrkz: { 4035 unsigned Dest = Inst.getOperand(0).getReg(); 4036 for (unsigned i = 1; i < Inst.getNumOperands(); i++) 4037 if (Inst.getOperand(i).isReg() && Dest == Inst.getOperand(i).getReg()) 4038 return Warning(Ops[0]->getStartLoc(), "Destination register should be " 4039 "distinct from source registers"); 4040 break; 4041 } 4042 } 4043 4044 const MCInstrDesc &MCID = MII.get(Inst.getOpcode()); 4045 // Check that we aren't mixing AH/BH/CH/DH with REX prefix. We only need to 4046 // check this with the legacy encoding, VEX/EVEX/XOP don't use REX. 4047 if ((MCID.TSFlags & X86II::EncodingMask) == 0) { 4048 MCPhysReg HReg = X86::NoRegister; 4049 bool UsesRex = MCID.TSFlags & X86II::REX_W; 4050 unsigned NumOps = Inst.getNumOperands(); 4051 for (unsigned i = 0; i != NumOps; ++i) { 4052 const MCOperand &MO = Inst.getOperand(i); 4053 if (!MO.isReg()) 4054 continue; 4055 unsigned Reg = MO.getReg(); 4056 if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH) 4057 HReg = Reg; 4058 if (X86II::isX86_64NonExtLowByteReg(Reg) || 4059 X86II::isX86_64ExtendedReg(Reg)) 4060 UsesRex = true; 4061 } 4062 4063 if (UsesRex && HReg != X86::NoRegister) { 4064 StringRef RegName = X86IntelInstPrinter::getRegisterName(HReg); 4065 return Error(Ops[0]->getStartLoc(), 4066 "can't encode '" + RegName + "' in an instruction requiring " 4067 "REX prefix"); 4068 } 4069 } 4070 4071 return false; 4072 } 4073 4074 static const char *getSubtargetFeatureName(uint64_t Val); 4075 4076 void X86AsmParser::emitWarningForSpecialLVIInstruction(SMLoc Loc) { 4077 Warning(Loc, "Instruction may be vulnerable to LVI and " 4078 "requires manual mitigation"); 4079 Note(SMLoc(), "See https://software.intel.com/" 4080 "security-software-guidance/insights/" 4081 "deep-dive-load-value-injection#specialinstructions" 4082 " for more information"); 4083 } 4084 4085 /// RET instructions and also instructions that indirect calls/jumps from memory 4086 /// combine a load and a branch within a single instruction. To mitigate these 4087 /// instructions against LVI, they must be decomposed into separate load and 4088 /// branch instructions, with an LFENCE in between. For more details, see: 4089 /// - X86LoadValueInjectionRetHardening.cpp 4090 /// - X86LoadValueInjectionIndirectThunks.cpp 4091 /// - https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection 4092 /// 4093 /// Returns `true` if a mitigation was applied or warning was emitted. 4094 void X86AsmParser::applyLVICFIMitigation(MCInst &Inst, MCStreamer &Out) { 4095 // Information on control-flow instructions that require manual mitigation can 4096 // be found here: 4097 // https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions 4098 switch (Inst.getOpcode()) { 4099 case X86::RET16: 4100 case X86::RET32: 4101 case X86::RET64: 4102 case X86::RETI16: 4103 case X86::RETI32: 4104 case X86::RETI64: { 4105 MCInst ShlInst, FenceInst; 4106 bool Parse32 = is32BitMode() || Code16GCC; 4107 unsigned Basereg = 4108 is64BitMode() ? X86::RSP : (Parse32 ? X86::ESP : X86::SP); 4109 const MCExpr *Disp = MCConstantExpr::create(0, getContext()); 4110 auto ShlMemOp = X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp, 4111 /*BaseReg=*/Basereg, /*IndexReg=*/0, 4112 /*Scale=*/1, SMLoc{}, SMLoc{}, 0); 4113 ShlInst.setOpcode(X86::SHL64mi); 4114 ShlMemOp->addMemOperands(ShlInst, 5); 4115 ShlInst.addOperand(MCOperand::createImm(0)); 4116 FenceInst.setOpcode(X86::LFENCE); 4117 Out.emitInstruction(ShlInst, getSTI()); 4118 Out.emitInstruction(FenceInst, getSTI()); 4119 return; 4120 } 4121 case X86::JMP16m: 4122 case X86::JMP32m: 4123 case X86::JMP64m: 4124 case X86::CALL16m: 4125 case X86::CALL32m: 4126 case X86::CALL64m: 4127 emitWarningForSpecialLVIInstruction(Inst.getLoc()); 4128 return; 4129 } 4130 } 4131 4132 /// To mitigate LVI, every instruction that performs a load can be followed by 4133 /// an LFENCE instruction to squash any potential mis-speculation. There are 4134 /// some instructions that require additional considerations, and may requre 4135 /// manual mitigation. For more details, see: 4136 /// https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection 4137 /// 4138 /// Returns `true` if a mitigation was applied or warning was emitted. 4139 void X86AsmParser::applyLVILoadHardeningMitigation(MCInst &Inst, 4140 MCStreamer &Out) { 4141 auto Opcode = Inst.getOpcode(); 4142 auto Flags = Inst.getFlags(); 4143 if ((Flags & X86::IP_HAS_REPEAT) || (Flags & X86::IP_HAS_REPEAT_NE)) { 4144 // Information on REP string instructions that require manual mitigation can 4145 // be found here: 4146 // https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions 4147 switch (Opcode) { 4148 case X86::CMPSB: 4149 case X86::CMPSW: 4150 case X86::CMPSL: 4151 case X86::CMPSQ: 4152 case X86::SCASB: 4153 case X86::SCASW: 4154 case X86::SCASL: 4155 case X86::SCASQ: 4156 emitWarningForSpecialLVIInstruction(Inst.getLoc()); 4157 return; 4158 } 4159 } else if (Opcode == X86::REP_PREFIX || Opcode == X86::REPNE_PREFIX) { 4160 // If a REP instruction is found on its own line, it may or may not be 4161 // followed by a vulnerable instruction. Emit a warning just in case. 4162 emitWarningForSpecialLVIInstruction(Inst.getLoc()); 4163 return; 4164 } 4165 4166 const MCInstrDesc &MCID = MII.get(Inst.getOpcode()); 4167 4168 // Can't mitigate after terminators or calls. A control flow change may have 4169 // already occurred. 4170 if (MCID.isTerminator() || MCID.isCall()) 4171 return; 4172 4173 // LFENCE has the mayLoad property, don't double fence. 4174 if (MCID.mayLoad() && Inst.getOpcode() != X86::LFENCE) { 4175 MCInst FenceInst; 4176 FenceInst.setOpcode(X86::LFENCE); 4177 Out.emitInstruction(FenceInst, getSTI()); 4178 } 4179 } 4180 4181 void X86AsmParser::emitInstruction(MCInst &Inst, OperandVector &Operands, 4182 MCStreamer &Out) { 4183 if (LVIInlineAsmHardening && 4184 getSTI().getFeatureBits()[X86::FeatureLVIControlFlowIntegrity]) 4185 applyLVICFIMitigation(Inst, Out); 4186 4187 Out.emitInstruction(Inst, getSTI()); 4188 4189 if (LVIInlineAsmHardening && 4190 getSTI().getFeatureBits()[X86::FeatureLVILoadHardening]) 4191 applyLVILoadHardeningMitigation(Inst, Out); 4192 } 4193 4194 bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 4195 OperandVector &Operands, 4196 MCStreamer &Out, uint64_t &ErrorInfo, 4197 bool MatchingInlineAsm) { 4198 if (isParsingIntelSyntax()) 4199 return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo, 4200 MatchingInlineAsm); 4201 return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo, 4202 MatchingInlineAsm); 4203 } 4204 4205 void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, 4206 OperandVector &Operands, MCStreamer &Out, 4207 bool MatchingInlineAsm) { 4208 // FIXME: This should be replaced with a real .td file alias mechanism. 4209 // Also, MatchInstructionImpl should actually *do* the EmitInstruction 4210 // call. 4211 const char *Repl = StringSwitch<const char *>(Op.getToken()) 4212 .Case("finit", "fninit") 4213 .Case("fsave", "fnsave") 4214 .Case("fstcw", "fnstcw") 4215 .Case("fstcww", "fnstcw") 4216 .Case("fstenv", "fnstenv") 4217 .Case("fstsw", "fnstsw") 4218 .Case("fstsww", "fnstsw") 4219 .Case("fclex", "fnclex") 4220 .Default(nullptr); 4221 if (Repl) { 4222 MCInst Inst; 4223 Inst.setOpcode(X86::WAIT); 4224 Inst.setLoc(IDLoc); 4225 if (!MatchingInlineAsm) 4226 emitInstruction(Inst, Operands, Out); 4227 Operands[0] = X86Operand::CreateToken(Repl, IDLoc); 4228 } 4229 } 4230 4231 bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, 4232 const FeatureBitset &MissingFeatures, 4233 bool MatchingInlineAsm) { 4234 assert(MissingFeatures.any() && "Unknown missing feature!"); 4235 SmallString<126> Msg; 4236 raw_svector_ostream OS(Msg); 4237 OS << "instruction requires:"; 4238 for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i) { 4239 if (MissingFeatures[i]) 4240 OS << ' ' << getSubtargetFeatureName(i); 4241 } 4242 return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm); 4243 } 4244 4245 static unsigned getPrefixes(OperandVector &Operands) { 4246 unsigned Result = 0; 4247 X86Operand &Prefix = static_cast<X86Operand &>(*Operands.back()); 4248 if (Prefix.isPrefix()) { 4249 Result = Prefix.getPrefix(); 4250 Operands.pop_back(); 4251 } 4252 return Result; 4253 } 4254 4255 unsigned X86AsmParser::checkTargetMatchPredicate(MCInst &Inst) { 4256 unsigned Opc = Inst.getOpcode(); 4257 const MCInstrDesc &MCID = MII.get(Opc); 4258 4259 if (ForcedVEXEncoding == VEXEncoding_EVEX && 4260 (MCID.TSFlags & X86II::EncodingMask) != X86II::EVEX) 4261 return Match_Unsupported; 4262 4263 if ((ForcedVEXEncoding == VEXEncoding_VEX || 4264 ForcedVEXEncoding == VEXEncoding_VEX2 || 4265 ForcedVEXEncoding == VEXEncoding_VEX3) && 4266 (MCID.TSFlags & X86II::EncodingMask) != X86II::VEX) 4267 return Match_Unsupported; 4268 4269 // These instructions are only available with {vex}, {vex2} or {vex3} prefix 4270 if (MCID.TSFlags & X86II::ExplicitVEXPrefix && 4271 (ForcedVEXEncoding != VEXEncoding_VEX && 4272 ForcedVEXEncoding != VEXEncoding_VEX2 && 4273 ForcedVEXEncoding != VEXEncoding_VEX3)) 4274 return Match_Unsupported; 4275 4276 return Match_Success; 4277 } 4278 4279 bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode, 4280 OperandVector &Operands, 4281 MCStreamer &Out, 4282 uint64_t &ErrorInfo, 4283 bool MatchingInlineAsm) { 4284 assert(!Operands.empty() && "Unexpect empty operand list!"); 4285 assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!"); 4286 SMRange EmptyRange = None; 4287 4288 // First, handle aliases that expand to multiple instructions. 4289 MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands, 4290 Out, MatchingInlineAsm); 4291 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]); 4292 unsigned Prefixes = getPrefixes(Operands); 4293 4294 MCInst Inst; 4295 4296 // If VEX/EVEX encoding is forced, we need to pass the USE_* flag to the 4297 // encoder and printer. 4298 if (ForcedVEXEncoding == VEXEncoding_VEX) 4299 Prefixes |= X86::IP_USE_VEX; 4300 else if (ForcedVEXEncoding == VEXEncoding_VEX2) 4301 Prefixes |= X86::IP_USE_VEX2; 4302 else if (ForcedVEXEncoding == VEXEncoding_VEX3) 4303 Prefixes |= X86::IP_USE_VEX3; 4304 else if (ForcedVEXEncoding == VEXEncoding_EVEX) 4305 Prefixes |= X86::IP_USE_EVEX; 4306 4307 // Set encoded flags for {disp8} and {disp32}. 4308 if (ForcedDispEncoding == DispEncoding_Disp8) 4309 Prefixes |= X86::IP_USE_DISP8; 4310 else if (ForcedDispEncoding == DispEncoding_Disp32) 4311 Prefixes |= X86::IP_USE_DISP32; 4312 4313 if (Prefixes) 4314 Inst.setFlags(Prefixes); 4315 4316 // In 16-bit mode, if data32 is specified, temporarily switch to 32-bit mode 4317 // when matching the instruction. 4318 if (ForcedDataPrefix == X86::Is32Bit) 4319 SwitchMode(X86::Is32Bit); 4320 // First, try a direct match. 4321 FeatureBitset MissingFeatures; 4322 unsigned OriginalError = MatchInstruction(Operands, Inst, ErrorInfo, 4323 MissingFeatures, MatchingInlineAsm, 4324 isParsingIntelSyntax()); 4325 if (ForcedDataPrefix == X86::Is32Bit) { 4326 SwitchMode(X86::Is16Bit); 4327 ForcedDataPrefix = 0; 4328 } 4329 switch (OriginalError) { 4330 default: llvm_unreachable("Unexpected match result!"); 4331 case Match_Success: 4332 if (!MatchingInlineAsm && validateInstruction(Inst, Operands)) 4333 return true; 4334 // Some instructions need post-processing to, for example, tweak which 4335 // encoding is selected. Loop on it while changes happen so the 4336 // individual transformations can chain off each other. 4337 if (!MatchingInlineAsm) 4338 while (processInstruction(Inst, Operands)) 4339 ; 4340 4341 Inst.setLoc(IDLoc); 4342 if (!MatchingInlineAsm) 4343 emitInstruction(Inst, Operands, Out); 4344 Opcode = Inst.getOpcode(); 4345 return false; 4346 case Match_InvalidImmUnsignedi4: { 4347 SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc(); 4348 if (ErrorLoc == SMLoc()) 4349 ErrorLoc = IDLoc; 4350 return Error(ErrorLoc, "immediate must be an integer in range [0, 15]", 4351 EmptyRange, MatchingInlineAsm); 4352 } 4353 case Match_MissingFeature: 4354 return ErrorMissingFeature(IDLoc, MissingFeatures, MatchingInlineAsm); 4355 case Match_InvalidOperand: 4356 case Match_MnemonicFail: 4357 case Match_Unsupported: 4358 break; 4359 } 4360 if (Op.getToken().empty()) { 4361 Error(IDLoc, "instruction must have size higher than 0", EmptyRange, 4362 MatchingInlineAsm); 4363 return true; 4364 } 4365 4366 // FIXME: Ideally, we would only attempt suffix matches for things which are 4367 // valid prefixes, and we could just infer the right unambiguous 4368 // type. However, that requires substantially more matcher support than the 4369 // following hack. 4370 4371 // Change the operand to point to a temporary token. 4372 StringRef Base = Op.getToken(); 4373 SmallString<16> Tmp; 4374 Tmp += Base; 4375 Tmp += ' '; 4376 Op.setTokenValue(Tmp); 4377 4378 // If this instruction starts with an 'f', then it is a floating point stack 4379 // instruction. These come in up to three forms for 32-bit, 64-bit, and 4380 // 80-bit floating point, which use the suffixes s,l,t respectively. 4381 // 4382 // Otherwise, we assume that this may be an integer instruction, which comes 4383 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively. 4384 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0"; 4385 // MemSize corresponding to Suffixes. { 8, 16, 32, 64 } { 32, 64, 80, 0 } 4386 const char *MemSize = Base[0] != 'f' ? "\x08\x10\x20\x40" : "\x20\x40\x50\0"; 4387 4388 // Check for the various suffix matches. 4389 uint64_t ErrorInfoIgnore; 4390 FeatureBitset ErrorInfoMissingFeatures; // Init suppresses compiler warnings. 4391 unsigned Match[4]; 4392 4393 // Some instruction like VPMULDQ is NOT the variant of VPMULD but a new one. 4394 // So we should make sure the suffix matcher only works for memory variant 4395 // that has the same size with the suffix. 4396 // FIXME: This flag is a workaround for legacy instructions that didn't 4397 // declare non suffix variant assembly. 4398 bool HasVectorReg = false; 4399 X86Operand *MemOp = nullptr; 4400 for (const auto &Op : Operands) { 4401 X86Operand *X86Op = static_cast<X86Operand *>(Op.get()); 4402 if (X86Op->isVectorReg()) 4403 HasVectorReg = true; 4404 else if (X86Op->isMem()) { 4405 MemOp = X86Op; 4406 assert(MemOp->Mem.Size == 0 && "Memory size always 0 under ATT syntax"); 4407 // Have we found an unqualified memory operand, 4408 // break. IA allows only one memory operand. 4409 break; 4410 } 4411 } 4412 4413 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) { 4414 Tmp.back() = Suffixes[I]; 4415 if (MemOp && HasVectorReg) 4416 MemOp->Mem.Size = MemSize[I]; 4417 Match[I] = Match_MnemonicFail; 4418 if (MemOp || !HasVectorReg) { 4419 Match[I] = 4420 MatchInstruction(Operands, Inst, ErrorInfoIgnore, MissingFeatures, 4421 MatchingInlineAsm, isParsingIntelSyntax()); 4422 // If this returned as a missing feature failure, remember that. 4423 if (Match[I] == Match_MissingFeature) 4424 ErrorInfoMissingFeatures = MissingFeatures; 4425 } 4426 } 4427 4428 // Restore the old token. 4429 Op.setTokenValue(Base); 4430 4431 // If exactly one matched, then we treat that as a successful match (and the 4432 // instruction will already have been filled in correctly, since the failing 4433 // matches won't have modified it). 4434 unsigned NumSuccessfulMatches = llvm::count(Match, Match_Success); 4435 if (NumSuccessfulMatches == 1) { 4436 if (!MatchingInlineAsm && validateInstruction(Inst, Operands)) 4437 return true; 4438 // Some instructions need post-processing to, for example, tweak which 4439 // encoding is selected. Loop on it while changes happen so the 4440 // individual transformations can chain off each other. 4441 if (!MatchingInlineAsm) 4442 while (processInstruction(Inst, Operands)) 4443 ; 4444 4445 Inst.setLoc(IDLoc); 4446 if (!MatchingInlineAsm) 4447 emitInstruction(Inst, Operands, Out); 4448 Opcode = Inst.getOpcode(); 4449 return false; 4450 } 4451 4452 // Otherwise, the match failed, try to produce a decent error message. 4453 4454 // If we had multiple suffix matches, then identify this as an ambiguous 4455 // match. 4456 if (NumSuccessfulMatches > 1) { 4457 char MatchChars[4]; 4458 unsigned NumMatches = 0; 4459 for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) 4460 if (Match[I] == Match_Success) 4461 MatchChars[NumMatches++] = Suffixes[I]; 4462 4463 SmallString<126> Msg; 4464 raw_svector_ostream OS(Msg); 4465 OS << "ambiguous instructions require an explicit suffix (could be "; 4466 for (unsigned i = 0; i != NumMatches; ++i) { 4467 if (i != 0) 4468 OS << ", "; 4469 if (i + 1 == NumMatches) 4470 OS << "or "; 4471 OS << "'" << Base << MatchChars[i] << "'"; 4472 } 4473 OS << ")"; 4474 Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm); 4475 return true; 4476 } 4477 4478 // Okay, we know that none of the variants matched successfully. 4479 4480 // If all of the instructions reported an invalid mnemonic, then the original 4481 // mnemonic was invalid. 4482 if (llvm::count(Match, Match_MnemonicFail) == 4) { 4483 if (OriginalError == Match_MnemonicFail) 4484 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'", 4485 Op.getLocRange(), MatchingInlineAsm); 4486 4487 if (OriginalError == Match_Unsupported) 4488 return Error(IDLoc, "unsupported instruction", EmptyRange, 4489 MatchingInlineAsm); 4490 4491 assert(OriginalError == Match_InvalidOperand && "Unexpected error"); 4492 // Recover location info for the operand if we know which was the problem. 4493 if (ErrorInfo != ~0ULL) { 4494 if (ErrorInfo >= Operands.size()) 4495 return Error(IDLoc, "too few operands for instruction", EmptyRange, 4496 MatchingInlineAsm); 4497 4498 X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo]; 4499 if (Operand.getStartLoc().isValid()) { 4500 SMRange OperandRange = Operand.getLocRange(); 4501 return Error(Operand.getStartLoc(), "invalid operand for instruction", 4502 OperandRange, MatchingInlineAsm); 4503 } 4504 } 4505 4506 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 4507 MatchingInlineAsm); 4508 } 4509 4510 // If one instruction matched as unsupported, report this as unsupported. 4511 if (llvm::count(Match, Match_Unsupported) == 1) { 4512 return Error(IDLoc, "unsupported instruction", EmptyRange, 4513 MatchingInlineAsm); 4514 } 4515 4516 // If one instruction matched with a missing feature, report this as a 4517 // missing feature. 4518 if (llvm::count(Match, Match_MissingFeature) == 1) { 4519 ErrorInfo = Match_MissingFeature; 4520 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures, 4521 MatchingInlineAsm); 4522 } 4523 4524 // If one instruction matched with an invalid operand, report this as an 4525 // operand failure. 4526 if (llvm::count(Match, Match_InvalidOperand) == 1) { 4527 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 4528 MatchingInlineAsm); 4529 } 4530 4531 // If all of these were an outright failure, report it in a useless way. 4532 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix", 4533 EmptyRange, MatchingInlineAsm); 4534 return true; 4535 } 4536 4537 bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode, 4538 OperandVector &Operands, 4539 MCStreamer &Out, 4540 uint64_t &ErrorInfo, 4541 bool MatchingInlineAsm) { 4542 assert(!Operands.empty() && "Unexpect empty operand list!"); 4543 assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!"); 4544 StringRef Mnemonic = (static_cast<X86Operand &>(*Operands[0])).getToken(); 4545 SMRange EmptyRange = None; 4546 StringRef Base = (static_cast<X86Operand &>(*Operands[0])).getToken(); 4547 unsigned Prefixes = getPrefixes(Operands); 4548 4549 // First, handle aliases that expand to multiple instructions. 4550 MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands, Out, MatchingInlineAsm); 4551 X86Operand &Op = static_cast<X86Operand &>(*Operands[0]); 4552 4553 MCInst Inst; 4554 4555 // If VEX/EVEX encoding is forced, we need to pass the USE_* flag to the 4556 // encoder and printer. 4557 if (ForcedVEXEncoding == VEXEncoding_VEX) 4558 Prefixes |= X86::IP_USE_VEX; 4559 else if (ForcedVEXEncoding == VEXEncoding_VEX2) 4560 Prefixes |= X86::IP_USE_VEX2; 4561 else if (ForcedVEXEncoding == VEXEncoding_VEX3) 4562 Prefixes |= X86::IP_USE_VEX3; 4563 else if (ForcedVEXEncoding == VEXEncoding_EVEX) 4564 Prefixes |= X86::IP_USE_EVEX; 4565 4566 // Set encoded flags for {disp8} and {disp32}. 4567 if (ForcedDispEncoding == DispEncoding_Disp8) 4568 Prefixes |= X86::IP_USE_DISP8; 4569 else if (ForcedDispEncoding == DispEncoding_Disp32) 4570 Prefixes |= X86::IP_USE_DISP32; 4571 4572 if (Prefixes) 4573 Inst.setFlags(Prefixes); 4574 4575 // Find one unsized memory operand, if present. 4576 X86Operand *UnsizedMemOp = nullptr; 4577 for (const auto &Op : Operands) { 4578 X86Operand *X86Op = static_cast<X86Operand *>(Op.get()); 4579 if (X86Op->isMemUnsized()) { 4580 UnsizedMemOp = X86Op; 4581 // Have we found an unqualified memory operand, 4582 // break. IA allows only one memory operand. 4583 break; 4584 } 4585 } 4586 4587 // Allow some instructions to have implicitly pointer-sized operands. This is 4588 // compatible with gas. 4589 if (UnsizedMemOp) { 4590 static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"}; 4591 for (const char *Instr : PtrSizedInstrs) { 4592 if (Mnemonic == Instr) { 4593 UnsizedMemOp->Mem.Size = getPointerWidth(); 4594 break; 4595 } 4596 } 4597 } 4598 4599 SmallVector<unsigned, 8> Match; 4600 FeatureBitset ErrorInfoMissingFeatures; 4601 FeatureBitset MissingFeatures; 4602 4603 // If unsized push has immediate operand we should default the default pointer 4604 // size for the size. 4605 if (Mnemonic == "push" && Operands.size() == 2) { 4606 auto *X86Op = static_cast<X86Operand *>(Operands[1].get()); 4607 if (X86Op->isImm()) { 4608 // If it's not a constant fall through and let remainder take care of it. 4609 const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm()); 4610 unsigned Size = getPointerWidth(); 4611 if (CE && 4612 (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) { 4613 SmallString<16> Tmp; 4614 Tmp += Base; 4615 Tmp += (is64BitMode()) 4616 ? "q" 4617 : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " "; 4618 Op.setTokenValue(Tmp); 4619 // Do match in ATT mode to allow explicit suffix usage. 4620 Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo, 4621 MissingFeatures, MatchingInlineAsm, 4622 false /*isParsingIntelSyntax()*/)); 4623 Op.setTokenValue(Base); 4624 } 4625 } 4626 } 4627 4628 // If an unsized memory operand is present, try to match with each memory 4629 // operand size. In Intel assembly, the size is not part of the instruction 4630 // mnemonic. 4631 if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) { 4632 static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512}; 4633 for (unsigned Size : MopSizes) { 4634 UnsizedMemOp->Mem.Size = Size; 4635 uint64_t ErrorInfoIgnore; 4636 unsigned LastOpcode = Inst.getOpcode(); 4637 unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore, 4638 MissingFeatures, MatchingInlineAsm, 4639 isParsingIntelSyntax()); 4640 if (Match.empty() || LastOpcode != Inst.getOpcode()) 4641 Match.push_back(M); 4642 4643 // If this returned as a missing feature failure, remember that. 4644 if (Match.back() == Match_MissingFeature) 4645 ErrorInfoMissingFeatures = MissingFeatures; 4646 } 4647 4648 // Restore the size of the unsized memory operand if we modified it. 4649 UnsizedMemOp->Mem.Size = 0; 4650 } 4651 4652 // If we haven't matched anything yet, this is not a basic integer or FPU 4653 // operation. There shouldn't be any ambiguity in our mnemonic table, so try 4654 // matching with the unsized operand. 4655 if (Match.empty()) { 4656 Match.push_back(MatchInstruction( 4657 Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm, 4658 isParsingIntelSyntax())); 4659 // If this returned as a missing feature failure, remember that. 4660 if (Match.back() == Match_MissingFeature) 4661 ErrorInfoMissingFeatures = MissingFeatures; 4662 } 4663 4664 // Restore the size of the unsized memory operand if we modified it. 4665 if (UnsizedMemOp) 4666 UnsizedMemOp->Mem.Size = 0; 4667 4668 // If it's a bad mnemonic, all results will be the same. 4669 if (Match.back() == Match_MnemonicFail) { 4670 return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'", 4671 Op.getLocRange(), MatchingInlineAsm); 4672 } 4673 4674 unsigned NumSuccessfulMatches = llvm::count(Match, Match_Success); 4675 4676 // If matching was ambiguous and we had size information from the frontend, 4677 // try again with that. This handles cases like "movxz eax, m8/m16". 4678 if (UnsizedMemOp && NumSuccessfulMatches > 1 && 4679 UnsizedMemOp->getMemFrontendSize()) { 4680 UnsizedMemOp->Mem.Size = UnsizedMemOp->getMemFrontendSize(); 4681 unsigned M = MatchInstruction( 4682 Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm, 4683 isParsingIntelSyntax()); 4684 if (M == Match_Success) 4685 NumSuccessfulMatches = 1; 4686 4687 // Add a rewrite that encodes the size information we used from the 4688 // frontend. 4689 InstInfo->AsmRewrites->emplace_back( 4690 AOK_SizeDirective, UnsizedMemOp->getStartLoc(), 4691 /*Len=*/0, UnsizedMemOp->getMemFrontendSize()); 4692 } 4693 4694 // If exactly one matched, then we treat that as a successful match (and the 4695 // instruction will already have been filled in correctly, since the failing 4696 // matches won't have modified it). 4697 if (NumSuccessfulMatches == 1) { 4698 if (!MatchingInlineAsm && validateInstruction(Inst, Operands)) 4699 return true; 4700 // Some instructions need post-processing to, for example, tweak which 4701 // encoding is selected. Loop on it while changes happen so the individual 4702 // transformations can chain off each other. 4703 if (!MatchingInlineAsm) 4704 while (processInstruction(Inst, Operands)) 4705 ; 4706 Inst.setLoc(IDLoc); 4707 if (!MatchingInlineAsm) 4708 emitInstruction(Inst, Operands, Out); 4709 Opcode = Inst.getOpcode(); 4710 return false; 4711 } else if (NumSuccessfulMatches > 1) { 4712 assert(UnsizedMemOp && 4713 "multiple matches only possible with unsized memory operands"); 4714 return Error(UnsizedMemOp->getStartLoc(), 4715 "ambiguous operand size for instruction '" + Mnemonic + "\'", 4716 UnsizedMemOp->getLocRange()); 4717 } 4718 4719 // If one instruction matched as unsupported, report this as unsupported. 4720 if (llvm::count(Match, Match_Unsupported) == 1) { 4721 return Error(IDLoc, "unsupported instruction", EmptyRange, 4722 MatchingInlineAsm); 4723 } 4724 4725 // If one instruction matched with a missing feature, report this as a 4726 // missing feature. 4727 if (llvm::count(Match, Match_MissingFeature) == 1) { 4728 ErrorInfo = Match_MissingFeature; 4729 return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures, 4730 MatchingInlineAsm); 4731 } 4732 4733 // If one instruction matched with an invalid operand, report this as an 4734 // operand failure. 4735 if (llvm::count(Match, Match_InvalidOperand) == 1) { 4736 return Error(IDLoc, "invalid operand for instruction", EmptyRange, 4737 MatchingInlineAsm); 4738 } 4739 4740 if (llvm::count(Match, Match_InvalidImmUnsignedi4) == 1) { 4741 SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc(); 4742 if (ErrorLoc == SMLoc()) 4743 ErrorLoc = IDLoc; 4744 return Error(ErrorLoc, "immediate must be an integer in range [0, 15]", 4745 EmptyRange, MatchingInlineAsm); 4746 } 4747 4748 // If all of these were an outright failure, report it in a useless way. 4749 return Error(IDLoc, "unknown instruction mnemonic", EmptyRange, 4750 MatchingInlineAsm); 4751 } 4752 4753 bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) { 4754 return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo); 4755 } 4756 4757 bool X86AsmParser::ParseDirective(AsmToken DirectiveID) { 4758 MCAsmParser &Parser = getParser(); 4759 StringRef IDVal = DirectiveID.getIdentifier(); 4760 if (IDVal.startswith(".arch")) 4761 return parseDirectiveArch(); 4762 if (IDVal.startswith(".code")) 4763 return ParseDirectiveCode(IDVal, DirectiveID.getLoc()); 4764 else if (IDVal.startswith(".att_syntax")) { 4765 if (getLexer().isNot(AsmToken::EndOfStatement)) { 4766 if (Parser.getTok().getString() == "prefix") 4767 Parser.Lex(); 4768 else if (Parser.getTok().getString() == "noprefix") 4769 return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not " 4770 "supported: registers must have a " 4771 "'%' prefix in .att_syntax"); 4772 } 4773 getParser().setAssemblerDialect(0); 4774 return false; 4775 } else if (IDVal.startswith(".intel_syntax")) { 4776 getParser().setAssemblerDialect(1); 4777 if (getLexer().isNot(AsmToken::EndOfStatement)) { 4778 if (Parser.getTok().getString() == "noprefix") 4779 Parser.Lex(); 4780 else if (Parser.getTok().getString() == "prefix") 4781 return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not " 4782 "supported: registers must not have " 4783 "a '%' prefix in .intel_syntax"); 4784 } 4785 return false; 4786 } else if (IDVal == ".nops") 4787 return parseDirectiveNops(DirectiveID.getLoc()); 4788 else if (IDVal == ".even") 4789 return parseDirectiveEven(DirectiveID.getLoc()); 4790 else if (IDVal == ".cv_fpo_proc") 4791 return parseDirectiveFPOProc(DirectiveID.getLoc()); 4792 else if (IDVal == ".cv_fpo_setframe") 4793 return parseDirectiveFPOSetFrame(DirectiveID.getLoc()); 4794 else if (IDVal == ".cv_fpo_pushreg") 4795 return parseDirectiveFPOPushReg(DirectiveID.getLoc()); 4796 else if (IDVal == ".cv_fpo_stackalloc") 4797 return parseDirectiveFPOStackAlloc(DirectiveID.getLoc()); 4798 else if (IDVal == ".cv_fpo_stackalign") 4799 return parseDirectiveFPOStackAlign(DirectiveID.getLoc()); 4800 else if (IDVal == ".cv_fpo_endprologue") 4801 return parseDirectiveFPOEndPrologue(DirectiveID.getLoc()); 4802 else if (IDVal == ".cv_fpo_endproc") 4803 return parseDirectiveFPOEndProc(DirectiveID.getLoc()); 4804 else if (IDVal == ".seh_pushreg" || 4805 (Parser.isParsingMasm() && IDVal.equals_insensitive(".pushreg"))) 4806 return parseDirectiveSEHPushReg(DirectiveID.getLoc()); 4807 else if (IDVal == ".seh_setframe" || 4808 (Parser.isParsingMasm() && IDVal.equals_insensitive(".setframe"))) 4809 return parseDirectiveSEHSetFrame(DirectiveID.getLoc()); 4810 else if (IDVal == ".seh_savereg" || 4811 (Parser.isParsingMasm() && IDVal.equals_insensitive(".savereg"))) 4812 return parseDirectiveSEHSaveReg(DirectiveID.getLoc()); 4813 else if (IDVal == ".seh_savexmm" || 4814 (Parser.isParsingMasm() && IDVal.equals_insensitive(".savexmm128"))) 4815 return parseDirectiveSEHSaveXMM(DirectiveID.getLoc()); 4816 else if (IDVal == ".seh_pushframe" || 4817 (Parser.isParsingMasm() && IDVal.equals_insensitive(".pushframe"))) 4818 return parseDirectiveSEHPushFrame(DirectiveID.getLoc()); 4819 4820 return true; 4821 } 4822 4823 bool X86AsmParser::parseDirectiveArch() { 4824 // Ignore .arch for now. 4825 getParser().parseStringToEndOfStatement(); 4826 return false; 4827 } 4828 4829 /// parseDirectiveNops 4830 /// ::= .nops size[, control] 4831 bool X86AsmParser::parseDirectiveNops(SMLoc L) { 4832 int64_t NumBytes = 0, Control = 0; 4833 SMLoc NumBytesLoc, ControlLoc; 4834 const MCSubtargetInfo& STI = getSTI(); 4835 NumBytesLoc = getTok().getLoc(); 4836 if (getParser().checkForValidSection() || 4837 getParser().parseAbsoluteExpression(NumBytes)) 4838 return true; 4839 4840 if (parseOptionalToken(AsmToken::Comma)) { 4841 ControlLoc = getTok().getLoc(); 4842 if (getParser().parseAbsoluteExpression(Control)) 4843 return true; 4844 } 4845 if (getParser().parseToken(AsmToken::EndOfStatement, 4846 "unexpected token in '.nops' directive")) 4847 return true; 4848 4849 if (NumBytes <= 0) { 4850 Error(NumBytesLoc, "'.nops' directive with non-positive size"); 4851 return false; 4852 } 4853 4854 if (Control < 0) { 4855 Error(ControlLoc, "'.nops' directive with negative NOP size"); 4856 return false; 4857 } 4858 4859 /// Emit nops 4860 getParser().getStreamer().emitNops(NumBytes, Control, L, STI); 4861 4862 return false; 4863 } 4864 4865 /// parseDirectiveEven 4866 /// ::= .even 4867 bool X86AsmParser::parseDirectiveEven(SMLoc L) { 4868 if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive")) 4869 return false; 4870 4871 const MCSection *Section = getStreamer().getCurrentSectionOnly(); 4872 if (!Section) { 4873 getStreamer().initSections(false, getSTI()); 4874 Section = getStreamer().getCurrentSectionOnly(); 4875 } 4876 if (Section->useCodeAlign()) 4877 getStreamer().emitCodeAlignment(2, &getSTI(), 0); 4878 else 4879 getStreamer().emitValueToAlignment(2, 0, 1, 0); 4880 return false; 4881 } 4882 4883 /// ParseDirectiveCode 4884 /// ::= .code16 | .code32 | .code64 4885 bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) { 4886 MCAsmParser &Parser = getParser(); 4887 Code16GCC = false; 4888 if (IDVal == ".code16") { 4889 Parser.Lex(); 4890 if (!is16BitMode()) { 4891 SwitchMode(X86::Is16Bit); 4892 getParser().getStreamer().emitAssemblerFlag(MCAF_Code16); 4893 } 4894 } else if (IDVal == ".code16gcc") { 4895 // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode. 4896 Parser.Lex(); 4897 Code16GCC = true; 4898 if (!is16BitMode()) { 4899 SwitchMode(X86::Is16Bit); 4900 getParser().getStreamer().emitAssemblerFlag(MCAF_Code16); 4901 } 4902 } else if (IDVal == ".code32") { 4903 Parser.Lex(); 4904 if (!is32BitMode()) { 4905 SwitchMode(X86::Is32Bit); 4906 getParser().getStreamer().emitAssemblerFlag(MCAF_Code32); 4907 } 4908 } else if (IDVal == ".code64") { 4909 Parser.Lex(); 4910 if (!is64BitMode()) { 4911 SwitchMode(X86::Is64Bit); 4912 getParser().getStreamer().emitAssemblerFlag(MCAF_Code64); 4913 } 4914 } else { 4915 Error(L, "unknown directive " + IDVal); 4916 return false; 4917 } 4918 4919 return false; 4920 } 4921 4922 // .cv_fpo_proc foo 4923 bool X86AsmParser::parseDirectiveFPOProc(SMLoc L) { 4924 MCAsmParser &Parser = getParser(); 4925 StringRef ProcName; 4926 int64_t ParamsSize; 4927 if (Parser.parseIdentifier(ProcName)) 4928 return Parser.TokError("expected symbol name"); 4929 if (Parser.parseIntToken(ParamsSize, "expected parameter byte count")) 4930 return true; 4931 if (!isUIntN(32, ParamsSize)) 4932 return Parser.TokError("parameters size out of range"); 4933 if (parseEOL()) 4934 return true; 4935 MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); 4936 return getTargetStreamer().emitFPOProc(ProcSym, ParamsSize, L); 4937 } 4938 4939 // .cv_fpo_setframe ebp 4940 bool X86AsmParser::parseDirectiveFPOSetFrame(SMLoc L) { 4941 unsigned Reg; 4942 SMLoc DummyLoc; 4943 if (ParseRegister(Reg, DummyLoc, DummyLoc) || parseEOL()) 4944 return true; 4945 return getTargetStreamer().emitFPOSetFrame(Reg, L); 4946 } 4947 4948 // .cv_fpo_pushreg ebx 4949 bool X86AsmParser::parseDirectiveFPOPushReg(SMLoc L) { 4950 unsigned Reg; 4951 SMLoc DummyLoc; 4952 if (ParseRegister(Reg, DummyLoc, DummyLoc) || parseEOL()) 4953 return true; 4954 return getTargetStreamer().emitFPOPushReg(Reg, L); 4955 } 4956 4957 // .cv_fpo_stackalloc 20 4958 bool X86AsmParser::parseDirectiveFPOStackAlloc(SMLoc L) { 4959 MCAsmParser &Parser = getParser(); 4960 int64_t Offset; 4961 if (Parser.parseIntToken(Offset, "expected offset") || parseEOL()) 4962 return true; 4963 return getTargetStreamer().emitFPOStackAlloc(Offset, L); 4964 } 4965 4966 // .cv_fpo_stackalign 8 4967 bool X86AsmParser::parseDirectiveFPOStackAlign(SMLoc L) { 4968 MCAsmParser &Parser = getParser(); 4969 int64_t Offset; 4970 if (Parser.parseIntToken(Offset, "expected offset") || parseEOL()) 4971 return true; 4972 return getTargetStreamer().emitFPOStackAlign(Offset, L); 4973 } 4974 4975 // .cv_fpo_endprologue 4976 bool X86AsmParser::parseDirectiveFPOEndPrologue(SMLoc L) { 4977 MCAsmParser &Parser = getParser(); 4978 if (Parser.parseEOL()) 4979 return true; 4980 return getTargetStreamer().emitFPOEndPrologue(L); 4981 } 4982 4983 // .cv_fpo_endproc 4984 bool X86AsmParser::parseDirectiveFPOEndProc(SMLoc L) { 4985 MCAsmParser &Parser = getParser(); 4986 if (Parser.parseEOL()) 4987 return true; 4988 return getTargetStreamer().emitFPOEndProc(L); 4989 } 4990 4991 bool X86AsmParser::parseSEHRegisterNumber(unsigned RegClassID, 4992 unsigned &RegNo) { 4993 SMLoc startLoc = getLexer().getLoc(); 4994 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 4995 4996 // Try parsing the argument as a register first. 4997 if (getLexer().getTok().isNot(AsmToken::Integer)) { 4998 SMLoc endLoc; 4999 if (ParseRegister(RegNo, startLoc, endLoc)) 5000 return true; 5001 5002 if (!X86MCRegisterClasses[RegClassID].contains(RegNo)) { 5003 return Error(startLoc, 5004 "register is not supported for use with this directive"); 5005 } 5006 } else { 5007 // Otherwise, an integer number matching the encoding of the desired 5008 // register may appear. 5009 int64_t EncodedReg; 5010 if (getParser().parseAbsoluteExpression(EncodedReg)) 5011 return true; 5012 5013 // The SEH register number is the same as the encoding register number. Map 5014 // from the encoding back to the LLVM register number. 5015 RegNo = 0; 5016 for (MCPhysReg Reg : X86MCRegisterClasses[RegClassID]) { 5017 if (MRI->getEncodingValue(Reg) == EncodedReg) { 5018 RegNo = Reg; 5019 break; 5020 } 5021 } 5022 if (RegNo == 0) { 5023 return Error(startLoc, 5024 "incorrect register number for use with this directive"); 5025 } 5026 } 5027 5028 return false; 5029 } 5030 5031 bool X86AsmParser::parseDirectiveSEHPushReg(SMLoc Loc) { 5032 unsigned Reg = 0; 5033 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 5034 return true; 5035 5036 if (getLexer().isNot(AsmToken::EndOfStatement)) 5037 return TokError("unexpected token in directive"); 5038 5039 getParser().Lex(); 5040 getStreamer().EmitWinCFIPushReg(Reg, Loc); 5041 return false; 5042 } 5043 5044 bool X86AsmParser::parseDirectiveSEHSetFrame(SMLoc Loc) { 5045 unsigned Reg = 0; 5046 int64_t Off; 5047 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 5048 return true; 5049 if (getLexer().isNot(AsmToken::Comma)) 5050 return TokError("you must specify a stack pointer offset"); 5051 5052 getParser().Lex(); 5053 if (getParser().parseAbsoluteExpression(Off)) 5054 return true; 5055 5056 if (getLexer().isNot(AsmToken::EndOfStatement)) 5057 return TokError("unexpected token in directive"); 5058 5059 getParser().Lex(); 5060 getStreamer().EmitWinCFISetFrame(Reg, Off, Loc); 5061 return false; 5062 } 5063 5064 bool X86AsmParser::parseDirectiveSEHSaveReg(SMLoc Loc) { 5065 unsigned Reg = 0; 5066 int64_t Off; 5067 if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg)) 5068 return true; 5069 if (getLexer().isNot(AsmToken::Comma)) 5070 return TokError("you must specify an offset on the stack"); 5071 5072 getParser().Lex(); 5073 if (getParser().parseAbsoluteExpression(Off)) 5074 return true; 5075 5076 if (getLexer().isNot(AsmToken::EndOfStatement)) 5077 return TokError("unexpected token in directive"); 5078 5079 getParser().Lex(); 5080 getStreamer().EmitWinCFISaveReg(Reg, Off, Loc); 5081 return false; 5082 } 5083 5084 bool X86AsmParser::parseDirectiveSEHSaveXMM(SMLoc Loc) { 5085 unsigned Reg = 0; 5086 int64_t Off; 5087 if (parseSEHRegisterNumber(X86::VR128XRegClassID, Reg)) 5088 return true; 5089 if (getLexer().isNot(AsmToken::Comma)) 5090 return TokError("you must specify an offset on the stack"); 5091 5092 getParser().Lex(); 5093 if (getParser().parseAbsoluteExpression(Off)) 5094 return true; 5095 5096 if (getLexer().isNot(AsmToken::EndOfStatement)) 5097 return TokError("unexpected token in directive"); 5098 5099 getParser().Lex(); 5100 getStreamer().EmitWinCFISaveXMM(Reg, Off, Loc); 5101 return false; 5102 } 5103 5104 bool X86AsmParser::parseDirectiveSEHPushFrame(SMLoc Loc) { 5105 bool Code = false; 5106 StringRef CodeID; 5107 if (getLexer().is(AsmToken::At)) { 5108 SMLoc startLoc = getLexer().getLoc(); 5109 getParser().Lex(); 5110 if (!getParser().parseIdentifier(CodeID)) { 5111 if (CodeID != "code") 5112 return Error(startLoc, "expected @code"); 5113 Code = true; 5114 } 5115 } 5116 5117 if (getLexer().isNot(AsmToken::EndOfStatement)) 5118 return TokError("unexpected token in directive"); 5119 5120 getParser().Lex(); 5121 getStreamer().EmitWinCFIPushFrame(Code, Loc); 5122 return false; 5123 } 5124 5125 // Force static initialization. 5126 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86AsmParser() { 5127 RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target()); 5128 RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target()); 5129 } 5130 5131 #define GET_REGISTER_MATCHER 5132 #define GET_MATCHER_IMPLEMENTATION 5133 #define GET_SUBTARGET_FEATURE_NAME 5134 #include "X86GenAsmMatcher.inc" 5135