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