b84bcf9dd7e68d15d3b58b5b1ded3cb260c02a7b
[oota-llvm.git] / lib / Target / X86 / AsmParser / X86AsmParser.cpp
1 //===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MCTargetDesc/X86BaseInfo.h"
11 #include "X86AsmInstrumentation.h"
12 #include "X86AsmParserCommon.h"
13 #include "X86Operand.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCParser/MCAsmLexer.h"
25 #include "llvm/MC/MCParser/MCAsmParser.h"
26 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCTargetAsmParser.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <memory>
36
37 using namespace llvm;
38
39 namespace {
40
41 static const char OpPrecedence[] = {
42   0, // IC_OR
43   1, // IC_AND
44   2, // IC_LSHIFT
45   2, // IC_RSHIFT
46   3, // IC_PLUS
47   3, // IC_MINUS
48   4, // IC_MULTIPLY
49   4, // IC_DIVIDE
50   5, // IC_RPAREN
51   6, // IC_LPAREN
52   0, // IC_IMM
53   0  // IC_REGISTER
54 };
55
56 class X86AsmParser : public MCTargetAsmParser {
57   MCSubtargetInfo &STI;
58   MCAsmParser &Parser;
59   const MCInstrInfo &MII;
60   ParseInstructionInfo *InstInfo;
61   std::unique_ptr<X86AsmInstrumentation> Instrumentation;
62 private:
63   SMLoc consumeToken() {
64     SMLoc Result = Parser.getTok().getLoc();
65     Parser.Lex();
66     return Result;
67   }
68
69   enum InfixCalculatorTok {
70     IC_OR = 0,
71     IC_AND,
72     IC_LSHIFT,
73     IC_RSHIFT,
74     IC_PLUS,
75     IC_MINUS,
76     IC_MULTIPLY,
77     IC_DIVIDE,
78     IC_RPAREN,
79     IC_LPAREN,
80     IC_IMM,
81     IC_REGISTER
82   };
83
84   class InfixCalculator {
85     typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
86     SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
87     SmallVector<ICToken, 4> PostfixStack;
88     
89   public:
90     int64_t popOperand() {
91       assert (!PostfixStack.empty() && "Poped an empty stack!");
92       ICToken Op = PostfixStack.pop_back_val();
93       assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
94               && "Expected and immediate or register!");
95       return Op.second;
96     }
97     void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
98       assert ((Op == IC_IMM || Op == IC_REGISTER) &&
99               "Unexpected operand!");
100       PostfixStack.push_back(std::make_pair(Op, Val));
101     }
102     
103     void popOperator() { InfixOperatorStack.pop_back(); }
104     void pushOperator(InfixCalculatorTok Op) {
105       // Push the new operator if the stack is empty.
106       if (InfixOperatorStack.empty()) {
107         InfixOperatorStack.push_back(Op);
108         return;
109       }
110       
111       // Push the new operator if it has a higher precedence than the operator
112       // on the top of the stack or the operator on the top of the stack is a
113       // left parentheses.
114       unsigned Idx = InfixOperatorStack.size() - 1;
115       InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
116       if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
117         InfixOperatorStack.push_back(Op);
118         return;
119       }
120       
121       // The operator on the top of the stack has higher precedence than the
122       // new operator.
123       unsigned ParenCount = 0;
124       while (1) {
125         // Nothing to process.
126         if (InfixOperatorStack.empty())
127           break;
128         
129         Idx = InfixOperatorStack.size() - 1;
130         StackOp = InfixOperatorStack[Idx];
131         if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
132           break;
133         
134         // If we have an even parentheses count and we see a left parentheses,
135         // then stop processing.
136         if (!ParenCount && StackOp == IC_LPAREN)
137           break;
138         
139         if (StackOp == IC_RPAREN) {
140           ++ParenCount;
141           InfixOperatorStack.pop_back();
142         } else if (StackOp == IC_LPAREN) {
143           --ParenCount;
144           InfixOperatorStack.pop_back();
145         } else {
146           InfixOperatorStack.pop_back();
147           PostfixStack.push_back(std::make_pair(StackOp, 0));
148         }
149       }
150       // Push the new operator.
151       InfixOperatorStack.push_back(Op);
152     }
153     int64_t execute() {
154       // Push any remaining operators onto the postfix stack.
155       while (!InfixOperatorStack.empty()) {
156         InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
157         if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
158           PostfixStack.push_back(std::make_pair(StackOp, 0));
159       }
160       
161       if (PostfixStack.empty())
162         return 0;
163       
164       SmallVector<ICToken, 16> OperandStack;
165       for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
166         ICToken Op = PostfixStack[i];
167         if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
168           OperandStack.push_back(Op);
169         } else {
170           assert (OperandStack.size() > 1 && "Too few operands.");
171           int64_t Val;
172           ICToken Op2 = OperandStack.pop_back_val();
173           ICToken Op1 = OperandStack.pop_back_val();
174           switch (Op.first) {
175           default:
176             report_fatal_error("Unexpected operator!");
177             break;
178           case IC_PLUS:
179             Val = Op1.second + Op2.second;
180             OperandStack.push_back(std::make_pair(IC_IMM, Val));
181             break;
182           case IC_MINUS:
183             Val = Op1.second - Op2.second;
184             OperandStack.push_back(std::make_pair(IC_IMM, Val));
185             break;
186           case IC_MULTIPLY:
187             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
188                     "Multiply operation with an immediate and a register!");
189             Val = Op1.second * Op2.second;
190             OperandStack.push_back(std::make_pair(IC_IMM, Val));
191             break;
192           case IC_DIVIDE:
193             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
194                     "Divide operation with an immediate and a register!");
195             assert (Op2.second != 0 && "Division by zero!");
196             Val = Op1.second / Op2.second;
197             OperandStack.push_back(std::make_pair(IC_IMM, Val));
198             break;
199           case IC_OR:
200             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
201                     "Or operation with an immediate and a register!");
202             Val = Op1.second | Op2.second;
203             OperandStack.push_back(std::make_pair(IC_IMM, Val));
204             break;
205           case IC_AND:
206             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
207                     "And operation with an immediate and a register!");
208             Val = Op1.second & Op2.second;
209             OperandStack.push_back(std::make_pair(IC_IMM, Val));
210             break;
211           case IC_LSHIFT:
212             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
213                     "Left shift operation with an immediate and a register!");
214             Val = Op1.second << Op2.second;
215             OperandStack.push_back(std::make_pair(IC_IMM, Val));
216             break;
217           case IC_RSHIFT:
218             assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
219                     "Right shift operation with an immediate and a register!");
220             Val = Op1.second >> Op2.second;
221             OperandStack.push_back(std::make_pair(IC_IMM, Val));
222             break;
223           }
224         }
225       }
226       assert (OperandStack.size() == 1 && "Expected a single result.");
227       return OperandStack.pop_back_val().second;
228     }
229   };
230
231   enum IntelExprState {
232     IES_OR,
233     IES_AND,
234     IES_LSHIFT,
235     IES_RSHIFT,
236     IES_PLUS,
237     IES_MINUS,
238     IES_MULTIPLY,
239     IES_DIVIDE,
240     IES_LBRAC,
241     IES_RBRAC,
242     IES_LPAREN,
243     IES_RPAREN,
244     IES_REGISTER,
245     IES_INTEGER,
246     IES_IDENTIFIER,
247     IES_ERROR
248   };
249
250   class IntelExprStateMachine {
251     IntelExprState State, PrevState;
252     unsigned BaseReg, IndexReg, TmpReg, Scale;
253     int64_t Imm;
254     const MCExpr *Sym;
255     StringRef SymName;
256     bool StopOnLBrac, AddImmPrefix;
257     InfixCalculator IC;
258     InlineAsmIdentifierInfo Info;
259   public:
260     IntelExprStateMachine(int64_t imm, bool stoponlbrac, bool addimmprefix) :
261       State(IES_PLUS), PrevState(IES_ERROR), BaseReg(0), IndexReg(0), TmpReg(0),
262       Scale(1), Imm(imm), Sym(0), StopOnLBrac(stoponlbrac),
263       AddImmPrefix(addimmprefix) { Info.clear(); }
264     
265     unsigned getBaseReg() { return BaseReg; }
266     unsigned getIndexReg() { return IndexReg; }
267     unsigned getScale() { return Scale; }
268     const MCExpr *getSym() { return Sym; }
269     StringRef getSymName() { return SymName; }
270     int64_t getImm() { return Imm + IC.execute(); }
271     bool isValidEndState() {
272       return State == IES_RBRAC || State == IES_INTEGER;
273     }
274     bool getStopOnLBrac() { return StopOnLBrac; }
275     bool getAddImmPrefix() { return AddImmPrefix; }
276     bool hadError() { return State == IES_ERROR; }
277
278     InlineAsmIdentifierInfo &getIdentifierInfo() {
279       return Info;
280     }
281
282     void onOr() {
283       IntelExprState CurrState = State;
284       switch (State) {
285       default:
286         State = IES_ERROR;
287         break;
288       case IES_INTEGER:
289       case IES_RPAREN:
290       case IES_REGISTER:
291         State = IES_OR;
292         IC.pushOperator(IC_OR);
293         break;
294       }
295       PrevState = CurrState;
296     }
297     void onAnd() {
298       IntelExprState CurrState = State;
299       switch (State) {
300       default:
301         State = IES_ERROR;
302         break;
303       case IES_INTEGER:
304       case IES_RPAREN:
305       case IES_REGISTER:
306         State = IES_AND;
307         IC.pushOperator(IC_AND);
308         break;
309       }
310       PrevState = CurrState;
311     }
312     void onLShift() {
313       IntelExprState CurrState = State;
314       switch (State) {
315       default:
316         State = IES_ERROR;
317         break;
318       case IES_INTEGER:
319       case IES_RPAREN:
320       case IES_REGISTER:
321         State = IES_LSHIFT;
322         IC.pushOperator(IC_LSHIFT);
323         break;
324       }
325       PrevState = CurrState;
326     }
327     void onRShift() {
328       IntelExprState CurrState = State;
329       switch (State) {
330       default:
331         State = IES_ERROR;
332         break;
333       case IES_INTEGER:
334       case IES_RPAREN:
335       case IES_REGISTER:
336         State = IES_RSHIFT;
337         IC.pushOperator(IC_RSHIFT);
338         break;
339       }
340       PrevState = CurrState;
341     }
342     void onPlus() {
343       IntelExprState CurrState = State;
344       switch (State) {
345       default:
346         State = IES_ERROR;
347         break;
348       case IES_INTEGER:
349       case IES_RPAREN:
350       case IES_REGISTER:
351         State = IES_PLUS;
352         IC.pushOperator(IC_PLUS);
353         if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
354           // If we already have a BaseReg, then assume this is the IndexReg with
355           // a scale of 1.
356           if (!BaseReg) {
357             BaseReg = TmpReg;
358           } else {
359             assert (!IndexReg && "BaseReg/IndexReg already set!");
360             IndexReg = TmpReg;
361             Scale = 1;
362           }
363         }
364         break;
365       }
366       PrevState = CurrState;
367     }
368     void onMinus() {
369       IntelExprState CurrState = State;
370       switch (State) {
371       default:
372         State = IES_ERROR;
373         break;
374       case IES_PLUS:
375       case IES_MULTIPLY:
376       case IES_DIVIDE:
377       case IES_LPAREN:
378       case IES_RPAREN:
379       case IES_LBRAC:
380       case IES_RBRAC:
381       case IES_INTEGER:
382       case IES_REGISTER:
383         State = IES_MINUS;
384         // Only push the minus operator if it is not a unary operator.
385         if (!(CurrState == IES_PLUS || CurrState == IES_MINUS ||
386               CurrState == IES_MULTIPLY || CurrState == IES_DIVIDE ||
387               CurrState == IES_LPAREN || CurrState == IES_LBRAC))
388           IC.pushOperator(IC_MINUS);
389         if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
390           // If we already have a BaseReg, then assume this is the IndexReg with
391           // a scale of 1.
392           if (!BaseReg) {
393             BaseReg = TmpReg;
394           } else {
395             assert (!IndexReg && "BaseReg/IndexReg already set!");
396             IndexReg = TmpReg;
397             Scale = 1;
398           }
399         }
400         break;
401       }
402       PrevState = CurrState;
403     }
404     void onRegister(unsigned Reg) {
405       IntelExprState CurrState = State;
406       switch (State) {
407       default:
408         State = IES_ERROR;
409         break;
410       case IES_PLUS:
411       case IES_LPAREN:
412         State = IES_REGISTER;
413         TmpReg = Reg;
414         IC.pushOperand(IC_REGISTER);
415         break;
416       case IES_MULTIPLY:
417         // Index Register - Scale * Register
418         if (PrevState == IES_INTEGER) {
419           assert (!IndexReg && "IndexReg already set!");
420           State = IES_REGISTER;
421           IndexReg = Reg;
422           // Get the scale and replace the 'Scale * Register' with '0'.
423           Scale = IC.popOperand();
424           IC.pushOperand(IC_IMM);
425           IC.popOperator();
426         } else {
427           State = IES_ERROR;
428         }
429         break;
430       }
431       PrevState = CurrState;
432     }
433     void onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName) {
434       PrevState = State;
435       switch (State) {
436       default:
437         State = IES_ERROR;
438         break;
439       case IES_PLUS:
440       case IES_MINUS:
441         State = IES_INTEGER;
442         Sym = SymRef;
443         SymName = SymRefName;
444         IC.pushOperand(IC_IMM);
445         break;
446       }
447     }
448     bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
449       IntelExprState CurrState = State;
450       switch (State) {
451       default:
452         State = IES_ERROR;
453         break;
454       case IES_PLUS:
455       case IES_MINUS:
456       case IES_OR:
457       case IES_AND:
458       case IES_LSHIFT:
459       case IES_RSHIFT:
460       case IES_DIVIDE:
461       case IES_MULTIPLY:
462       case IES_LPAREN:
463         State = IES_INTEGER;
464         if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
465           // Index Register - Register * Scale
466           assert (!IndexReg && "IndexReg already set!");
467           IndexReg = TmpReg;
468           Scale = TmpInt;
469           if(Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
470             ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
471             return true;
472           }
473           // Get the scale and replace the 'Register * Scale' with '0'.
474           IC.popOperator();
475         } else if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
476                     PrevState == IES_OR || PrevState == IES_AND ||
477                     PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
478                     PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
479                     PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
480                    CurrState == IES_MINUS) {
481           // Unary minus.  No need to pop the minus operand because it was never
482           // pushed.
483           IC.pushOperand(IC_IMM, -TmpInt); // Push -Imm.
484         } else {
485           IC.pushOperand(IC_IMM, TmpInt);
486         }
487         break;
488       }
489       PrevState = CurrState;
490       return false;
491     }
492     void onStar() {
493       PrevState = State;
494       switch (State) {
495       default:
496         State = IES_ERROR;
497         break;
498       case IES_INTEGER:
499       case IES_REGISTER:
500       case IES_RPAREN:
501         State = IES_MULTIPLY;
502         IC.pushOperator(IC_MULTIPLY);
503         break;
504       }
505     }
506     void onDivide() {
507       PrevState = State;
508       switch (State) {
509       default:
510         State = IES_ERROR;
511         break;
512       case IES_INTEGER:
513       case IES_RPAREN:
514         State = IES_DIVIDE;
515         IC.pushOperator(IC_DIVIDE);
516         break;
517       }
518     }
519     void onLBrac() {
520       PrevState = State;
521       switch (State) {
522       default:
523         State = IES_ERROR;
524         break;
525       case IES_RBRAC:
526         State = IES_PLUS;
527         IC.pushOperator(IC_PLUS);
528         break;
529       }
530     }
531     void onRBrac() {
532       IntelExprState CurrState = State;
533       switch (State) {
534       default:
535         State = IES_ERROR;
536         break;
537       case IES_INTEGER:
538       case IES_REGISTER:
539       case IES_RPAREN:
540         State = IES_RBRAC;
541         if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
542           // If we already have a BaseReg, then assume this is the IndexReg with
543           // a scale of 1.
544           if (!BaseReg) {
545             BaseReg = TmpReg;
546           } else {
547             assert (!IndexReg && "BaseReg/IndexReg already set!");
548             IndexReg = TmpReg;
549             Scale = 1;
550           }
551         }
552         break;
553       }
554       PrevState = CurrState;
555     }
556     void onLParen() {
557       IntelExprState CurrState = State;
558       switch (State) {
559       default:
560         State = IES_ERROR;
561         break;
562       case IES_PLUS:
563       case IES_MINUS:
564       case IES_OR:
565       case IES_AND:
566       case IES_LSHIFT:
567       case IES_RSHIFT:
568       case IES_MULTIPLY:
569       case IES_DIVIDE:
570       case IES_LPAREN:
571         // FIXME: We don't handle this type of unary minus, yet.
572         if ((PrevState == IES_PLUS || PrevState == IES_MINUS ||
573             PrevState == IES_OR || PrevState == IES_AND ||
574             PrevState == IES_LSHIFT || PrevState == IES_RSHIFT ||
575             PrevState == IES_MULTIPLY || PrevState == IES_DIVIDE ||
576             PrevState == IES_LPAREN || PrevState == IES_LBRAC) &&
577             CurrState == IES_MINUS) {
578           State = IES_ERROR;
579           break;
580         }
581         State = IES_LPAREN;
582         IC.pushOperator(IC_LPAREN);
583         break;
584       }
585       PrevState = CurrState;
586     }
587     void onRParen() {
588       PrevState = State;
589       switch (State) {
590       default:
591         State = IES_ERROR;
592         break;
593       case IES_INTEGER:
594       case IES_REGISTER:
595       case IES_RPAREN:
596         State = IES_RPAREN;
597         IC.pushOperator(IC_RPAREN);
598         break;
599       }
600     }
601   };
602
603   MCAsmParser &getParser() const { return Parser; }
604
605   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
606
607   bool Error(SMLoc L, const Twine &Msg,
608              ArrayRef<SMRange> Ranges = None,
609              bool MatchingInlineAsm = false) {
610     if (MatchingInlineAsm) return true;
611     return Parser.Error(L, Msg, Ranges);
612   }
613
614   bool ErrorAndEatStatement(SMLoc L, const Twine &Msg,
615           ArrayRef<SMRange> Ranges = None,
616           bool MatchingInlineAsm = false) {
617       Parser.eatToEndOfStatement();
618       return Error(L, Msg, Ranges, MatchingInlineAsm);
619   }
620
621   X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
622     Error(Loc, Msg);
623     return 0;
624   }
625
626   X86Operand *DefaultMemSIOperand(SMLoc Loc);
627   X86Operand *DefaultMemDIOperand(SMLoc Loc);
628   X86Operand *ParseOperand();
629   X86Operand *ParseATTOperand();
630   X86Operand *ParseIntelOperand();
631   X86Operand *ParseIntelOffsetOfOperator();
632   bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr *&NewDisp);
633   X86Operand *ParseIntelOperator(unsigned OpKind);
634   X86Operand *ParseIntelSegmentOverride(unsigned SegReg, SMLoc Start, unsigned Size);
635   X86Operand *ParseIntelMemOperand(int64_t ImmDisp, SMLoc StartLoc,
636                                    unsigned Size);
637   bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
638   X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
639                                        int64_t ImmDisp, unsigned Size);
640   bool ParseIntelIdentifier(const MCExpr *&Val, StringRef &Identifier,
641                             InlineAsmIdentifierInfo &Info,
642                             bool IsUnevaluatedOperand, SMLoc &End);
643
644   X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
645
646   X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
647                                     unsigned BaseReg, unsigned IndexReg,
648                                     unsigned Scale, SMLoc Start, SMLoc End,
649                                     unsigned Size, StringRef Identifier,
650                                     InlineAsmIdentifierInfo &Info);
651
652   bool ParseDirectiveWord(unsigned Size, SMLoc L);
653   bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
654
655   bool processInstruction(MCInst &Inst,
656                           const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
657
658   /// Wrapper around MCStreamer::EmitInstruction(). Possibly adds
659   /// instrumentation around Inst.
660   void EmitInstruction(MCInst &Inst,
661                        SmallVectorImpl<MCParsedAsmOperand *> &Operands,
662                        MCStreamer &Out);
663
664   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
665                                SmallVectorImpl<MCParsedAsmOperand*> &Operands,
666                                MCStreamer &Out, unsigned &ErrorInfo,
667                                bool MatchingInlineAsm) override;
668
669   /// doSrcDstMatch - Returns true if operands are matching in their
670   /// word size (%si and %di, %esi and %edi, etc.). Order depends on
671   /// the parsing mode (Intel vs. AT&T).
672   bool doSrcDstMatch(X86Operand &Op1, X86Operand &Op2);
673
674   /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
675   /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
676   /// \return \c true if no parsing errors occurred, \c false otherwise.
677   bool HandleAVX512Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
678                             const MCParsedAsmOperand &Op);
679
680   bool is64BitMode() const {
681     // FIXME: Can tablegen auto-generate this?
682     return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
683   }
684   bool is32BitMode() const {
685     // FIXME: Can tablegen auto-generate this?
686     return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
687   }
688   bool is16BitMode() const {
689     // FIXME: Can tablegen auto-generate this?
690     return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
691   }
692   void SwitchMode(uint64_t mode) {
693     uint64_t oldMode = STI.getFeatureBits() &
694         (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit);
695     unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(oldMode | mode));
696     setAvailableFeatures(FB);
697     assert(mode == (STI.getFeatureBits() &
698                     (X86::Mode64Bit | X86::Mode32Bit | X86::Mode16Bit)));
699   }
700
701   bool isParsingIntelSyntax() {
702     return getParser().getAssemblerDialect();
703   }
704
705   /// @name Auto-generated Matcher Functions
706   /// {
707
708 #define GET_ASSEMBLER_HEADER
709 #include "X86GenAsmMatcher.inc"
710
711   /// }
712
713 public:
714   X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
715                const MCInstrInfo &mii,
716                const MCTargetOptions &Options)
717       : MCTargetAsmParser(), STI(sti), Parser(parser), MII(mii), InstInfo(0) {
718
719     // Initialize the set of available features.
720     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
721     Instrumentation.reset(
722         CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
723   }
724
725   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
726
727   bool
728     ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
729                      SmallVectorImpl<MCParsedAsmOperand*> &Operands) override;
730
731   bool ParseDirective(AsmToken DirectiveID) override;
732 };
733 } // end anonymous namespace
734
735 /// @name Auto-generated Match Functions
736 /// {
737
738 static unsigned MatchRegisterName(StringRef Name);
739
740 /// }
741
742 static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
743                                     StringRef &ErrMsg) {
744   // If we have both a base register and an index register make sure they are
745   // both 64-bit or 32-bit registers.
746   // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
747   if (BaseReg != 0 && IndexReg != 0) {
748     if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
749         (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
750          X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
751         IndexReg != X86::RIZ) {
752       ErrMsg = "base register is 64-bit, but index register is not";
753       return true;
754     }
755     if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
756         (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
757          X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
758         IndexReg != X86::EIZ){
759       ErrMsg = "base register is 32-bit, but index register is not";
760       return true;
761     }
762     if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
763       if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
764           X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
765         ErrMsg = "base register is 16-bit, but index register is not";
766         return true;
767       }
768       if (((BaseReg == X86::BX || BaseReg == X86::BP) &&
769            IndexReg != X86::SI && IndexReg != X86::DI) ||
770           ((BaseReg == X86::SI || BaseReg == X86::DI) &&
771            IndexReg != X86::BX && IndexReg != X86::BP)) {
772         ErrMsg = "invalid 16-bit base/index register combination";
773         return true;
774       }
775     }
776   }
777   return false;
778 }
779
780 bool X86AsmParser::doSrcDstMatch(X86Operand &Op1, X86Operand &Op2)
781 {
782   // Return true and let a normal complaint about bogus operands happen.
783   if (!Op1.isMem() || !Op2.isMem())
784     return true;
785
786   // Actually these might be the other way round if Intel syntax is
787   // being used. It doesn't matter.
788   unsigned diReg = Op1.Mem.BaseReg;
789   unsigned siReg = Op2.Mem.BaseReg;
790
791   if (X86MCRegisterClasses[X86::GR16RegClassID].contains(siReg))
792     return X86MCRegisterClasses[X86::GR16RegClassID].contains(diReg);
793   if (X86MCRegisterClasses[X86::GR32RegClassID].contains(siReg))
794     return X86MCRegisterClasses[X86::GR32RegClassID].contains(diReg);
795   if (X86MCRegisterClasses[X86::GR64RegClassID].contains(siReg))
796     return X86MCRegisterClasses[X86::GR64RegClassID].contains(diReg);
797   // Again, return true and let another error happen.
798   return true;
799 }
800
801 bool X86AsmParser::ParseRegister(unsigned &RegNo,
802                                  SMLoc &StartLoc, SMLoc &EndLoc) {
803   RegNo = 0;
804   const AsmToken &PercentTok = Parser.getTok();
805   StartLoc = PercentTok.getLoc();
806
807   // If we encounter a %, ignore it. This code handles registers with and
808   // without the prefix, unprefixed registers can occur in cfi directives.
809   if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
810     Parser.Lex(); // Eat percent token.
811
812   const AsmToken &Tok = Parser.getTok();
813   EndLoc = Tok.getEndLoc();
814
815   if (Tok.isNot(AsmToken::Identifier)) {
816     if (isParsingIntelSyntax()) return true;
817     return Error(StartLoc, "invalid register name",
818                  SMRange(StartLoc, EndLoc));
819   }
820
821   RegNo = MatchRegisterName(Tok.getString());
822
823   // If the match failed, try the register name as lowercase.
824   if (RegNo == 0)
825     RegNo = MatchRegisterName(Tok.getString().lower());
826
827   if (!is64BitMode()) {
828     // FIXME: This should be done using Requires<Not64BitMode> and
829     // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
830     // checked.
831     // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
832     // REX prefix.
833     if (RegNo == X86::RIZ ||
834         X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
835         X86II::isX86_64NonExtLowByteReg(RegNo) ||
836         X86II::isX86_64ExtendedReg(RegNo))
837       return Error(StartLoc, "register %"
838                    + Tok.getString() + " is only available in 64-bit mode",
839                    SMRange(StartLoc, EndLoc));
840   }
841
842   // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
843   if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
844     RegNo = X86::ST0;
845     Parser.Lex(); // Eat 'st'
846
847     // Check to see if we have '(4)' after %st.
848     if (getLexer().isNot(AsmToken::LParen))
849       return false;
850     // Lex the paren.
851     getParser().Lex();
852
853     const AsmToken &IntTok = Parser.getTok();
854     if (IntTok.isNot(AsmToken::Integer))
855       return Error(IntTok.getLoc(), "expected stack index");
856     switch (IntTok.getIntVal()) {
857     case 0: RegNo = X86::ST0; break;
858     case 1: RegNo = X86::ST1; break;
859     case 2: RegNo = X86::ST2; break;
860     case 3: RegNo = X86::ST3; break;
861     case 4: RegNo = X86::ST4; break;
862     case 5: RegNo = X86::ST5; break;
863     case 6: RegNo = X86::ST6; break;
864     case 7: RegNo = X86::ST7; break;
865     default: return Error(IntTok.getLoc(), "invalid stack index");
866     }
867
868     if (getParser().Lex().isNot(AsmToken::RParen))
869       return Error(Parser.getTok().getLoc(), "expected ')'");
870
871     EndLoc = Parser.getTok().getEndLoc();
872     Parser.Lex(); // Eat ')'
873     return false;
874   }
875
876   EndLoc = Parser.getTok().getEndLoc();
877
878   // If this is "db[0-7]", match it as an alias
879   // for dr[0-7].
880   if (RegNo == 0 && Tok.getString().size() == 3 &&
881       Tok.getString().startswith("db")) {
882     switch (Tok.getString()[2]) {
883     case '0': RegNo = X86::DR0; break;
884     case '1': RegNo = X86::DR1; break;
885     case '2': RegNo = X86::DR2; break;
886     case '3': RegNo = X86::DR3; break;
887     case '4': RegNo = X86::DR4; break;
888     case '5': RegNo = X86::DR5; break;
889     case '6': RegNo = X86::DR6; break;
890     case '7': RegNo = X86::DR7; break;
891     }
892
893     if (RegNo != 0) {
894       EndLoc = Parser.getTok().getEndLoc();
895       Parser.Lex(); // Eat it.
896       return false;
897     }
898   }
899
900   if (RegNo == 0) {
901     if (isParsingIntelSyntax()) return true;
902     return Error(StartLoc, "invalid register name",
903                  SMRange(StartLoc, EndLoc));
904   }
905
906   Parser.Lex(); // Eat identifier token.
907   return false;
908 }
909
910 X86Operand *X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
911   unsigned basereg =
912     is64BitMode() ? X86::RSI : (is32BitMode() ? X86::ESI : X86::SI);
913   const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
914   return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
915                                /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
916 }
917
918 X86Operand *X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
919   unsigned basereg =
920     is64BitMode() ? X86::RDI : (is32BitMode() ? X86::EDI : X86::DI);
921   const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
922   return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/basereg,
923                                /*IndexReg=*/0, /*Scale=*/1, Loc, Loc, 0);
924 }
925
926 X86Operand *X86AsmParser::ParseOperand() {
927   if (isParsingIntelSyntax())
928     return ParseIntelOperand();
929   return ParseATTOperand();
930 }
931
932 /// getIntelMemOperandSize - Return intel memory operand size.
933 static unsigned getIntelMemOperandSize(StringRef OpStr) {
934   unsigned Size = StringSwitch<unsigned>(OpStr)
935     .Cases("BYTE", "byte", 8)
936     .Cases("WORD", "word", 16)
937     .Cases("DWORD", "dword", 32)
938     .Cases("QWORD", "qword", 64)
939     .Cases("XWORD", "xword", 80)
940     .Cases("XMMWORD", "xmmword", 128)
941     .Cases("YMMWORD", "ymmword", 256)
942     .Cases("ZMMWORD", "zmmword", 512)
943     .Cases("OPAQUE", "opaque", -1U) // needs to be non-zero, but doesn't matter
944     .Default(0);
945   return Size;
946 }
947
948 X86Operand *
949 X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
950                                     unsigned BaseReg, unsigned IndexReg,
951                                     unsigned Scale, SMLoc Start, SMLoc End,
952                                     unsigned Size, StringRef Identifier,
953                                     InlineAsmIdentifierInfo &Info){
954   // If this is not a VarDecl then assume it is a FuncDecl or some other label
955   // reference.  We need an 'r' constraint here, so we need to create register
956   // operand to ensure proper matching.  Just pick a GPR based on the size of
957   // a pointer.
958   if (isa<MCSymbolRefExpr>(Disp) && !Info.IsVarDecl) {
959     unsigned RegNo =
960         is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
961     return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
962                                  SMLoc(), Identifier, Info.OpDecl);
963   }
964
965   // We either have a direct symbol reference, or an offset from a symbol.  The
966   // parser always puts the symbol on the LHS, so look there for size
967   // calculation purposes.
968   const MCBinaryExpr *BinOp = dyn_cast<MCBinaryExpr>(Disp);
969   bool IsSymRef =
970       isa<MCSymbolRefExpr>(BinOp ? BinOp->getLHS() : Disp);
971   if (IsSymRef) {
972     if (!Size) {
973       Size = Info.Type * 8; // Size is in terms of bits in this context.
974       if (Size)
975         InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
976                                                     /*Len=*/0, Size));
977     }
978   }
979
980   // When parsing inline assembly we set the base register to a non-zero value
981   // if we don't know the actual value at this time.  This is necessary to
982   // get the matching correct in some cases.
983   BaseReg = BaseReg ? BaseReg : 1;
984   return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
985                                End, Size, Identifier, Info.OpDecl);
986 }
987
988 static void
989 RewriteIntelBracExpression(SmallVectorImpl<AsmRewrite> *AsmRewrites,
990                            StringRef SymName, int64_t ImmDisp,
991                            int64_t FinalImmDisp, SMLoc &BracLoc,
992                            SMLoc &StartInBrac, SMLoc &End) {
993   // Remove the '[' and ']' from the IR string.
994   AsmRewrites->push_back(AsmRewrite(AOK_Skip, BracLoc, 1));
995   AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
996
997   // If ImmDisp is non-zero, then we parsed a displacement before the
998   // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
999   // If ImmDisp doesn't match the displacement computed by the state machine
1000   // then we have an additional displacement in the bracketed expression.
1001   if (ImmDisp != FinalImmDisp) {
1002     if (ImmDisp) {
1003       // We have an immediate displacement before the bracketed expression.
1004       // Adjust this to match the final immediate displacement.
1005       bool Found = false;
1006       for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1007              E = AsmRewrites->end(); I != E; ++I) {
1008         if ((*I).Loc.getPointer() > BracLoc.getPointer())
1009           continue;
1010         if ((*I).Kind == AOK_ImmPrefix || (*I).Kind == AOK_Imm) {
1011           assert (!Found && "ImmDisp already rewritten.");
1012           (*I).Kind = AOK_Imm;
1013           (*I).Len = BracLoc.getPointer() - (*I).Loc.getPointer();
1014           (*I).Val = FinalImmDisp;
1015           Found = true;
1016           break;
1017         }
1018       }
1019       assert (Found && "Unable to rewrite ImmDisp.");
1020       (void)Found;
1021     } else {
1022       // We have a symbolic and an immediate displacement, but no displacement
1023       // before the bracketed expression.  Put the immediate displacement
1024       // before the bracketed expression.
1025       AsmRewrites->push_back(AsmRewrite(AOK_Imm, BracLoc, 0, FinalImmDisp));
1026     }
1027   }
1028   // Remove all the ImmPrefix rewrites within the brackets.
1029   for (SmallVectorImpl<AsmRewrite>::iterator I = AsmRewrites->begin(),
1030          E = AsmRewrites->end(); I != E; ++I) {
1031     if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1032       continue;
1033     if ((*I).Kind == AOK_ImmPrefix)
1034       (*I).Kind = AOK_Delete;
1035   }
1036   const char *SymLocPtr = SymName.data();
1037   // Skip everything before the symbol.        
1038   if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1039     assert(Len > 0 && "Expected a non-negative length.");
1040     AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1041   }
1042   // Skip everything after the symbol.
1043   if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1044     SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1045     assert(Len > 0 && "Expected a non-negative length.");
1046     AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1047   }
1048 }
1049
1050 bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
1051   const AsmToken &Tok = Parser.getTok();
1052
1053   bool Done = false;
1054   while (!Done) {
1055     bool UpdateLocLex = true;
1056
1057     // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1058     // identifier.  Don't try an parse it as a register.
1059     if (Tok.getString().startswith("."))
1060       break;
1061     
1062     // If we're parsing an immediate expression, we don't expect a '['.
1063     if (SM.getStopOnLBrac() && getLexer().getKind() == AsmToken::LBrac)
1064       break;
1065
1066     switch (getLexer().getKind()) {
1067     default: {
1068       if (SM.isValidEndState()) {
1069         Done = true;
1070         break;
1071       }
1072       return Error(Tok.getLoc(), "unknown token in expression");
1073     }
1074     case AsmToken::EndOfStatement: {
1075       Done = true;
1076       break;
1077     }
1078     case AsmToken::Identifier: {
1079       // This could be a register or a symbolic displacement.
1080       unsigned TmpReg;
1081       const MCExpr *Val;
1082       SMLoc IdentLoc = Tok.getLoc();
1083       StringRef Identifier = Tok.getString();
1084       if(!ParseRegister(TmpReg, IdentLoc, End)) {
1085         SM.onRegister(TmpReg);
1086         UpdateLocLex = false;
1087         break;
1088       } else {
1089         if (!isParsingInlineAsm()) {
1090           if (getParser().parsePrimaryExpr(Val, End))
1091             return Error(Tok.getLoc(), "Unexpected identifier!");
1092         } else {
1093           // This is a dot operator, not an adjacent identifier.
1094           if (Identifier.find('.') != StringRef::npos) {
1095             return false;
1096           } else {
1097             InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1098             if (ParseIntelIdentifier(Val, Identifier, Info,
1099                                      /*Unevaluated=*/false, End))
1100               return true;
1101           }
1102         }
1103         SM.onIdentifierExpr(Val, Identifier);
1104         UpdateLocLex = false;
1105         break;
1106       }
1107       return Error(Tok.getLoc(), "Unexpected identifier!");
1108     }
1109     case AsmToken::Integer: {
1110       StringRef ErrMsg;
1111       if (isParsingInlineAsm() && SM.getAddImmPrefix())
1112         InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1113                                                     Tok.getLoc()));
1114       // Look for 'b' or 'f' following an Integer as a directional label
1115       SMLoc Loc = getTok().getLoc();
1116       int64_t IntVal = getTok().getIntVal();
1117       End = consumeToken();
1118       UpdateLocLex = false;
1119       if (getLexer().getKind() == AsmToken::Identifier) {
1120         StringRef IDVal = getTok().getString();
1121         if (IDVal == "f" || IDVal == "b") {
1122           MCSymbol *Sym =
1123               getContext().GetDirectionalLocalSymbol(IntVal, IDVal == "b");
1124           MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1125           const MCExpr *Val = 
1126             MCSymbolRefExpr::Create(Sym, Variant, getContext());
1127           if (IDVal == "b" && Sym->isUndefined())
1128             return Error(Loc, "invalid reference to undefined symbol");
1129           StringRef Identifier = Sym->getName();
1130           SM.onIdentifierExpr(Val, Identifier);
1131           End = consumeToken();
1132         } else {
1133           if (SM.onInteger(IntVal, ErrMsg))
1134             return Error(Loc, ErrMsg);
1135         }
1136       } else {
1137         if (SM.onInteger(IntVal, ErrMsg))
1138           return Error(Loc, ErrMsg);
1139       }
1140       break;
1141     }
1142     case AsmToken::Plus:    SM.onPlus(); break;
1143     case AsmToken::Minus:   SM.onMinus(); break;
1144     case AsmToken::Star:    SM.onStar(); break;
1145     case AsmToken::Slash:   SM.onDivide(); break;
1146     case AsmToken::Pipe:    SM.onOr(); break;
1147     case AsmToken::Amp:     SM.onAnd(); break;
1148     case AsmToken::LessLess:
1149                             SM.onLShift(); break;
1150     case AsmToken::GreaterGreater:
1151                             SM.onRShift(); break;
1152     case AsmToken::LBrac:   SM.onLBrac(); break;
1153     case AsmToken::RBrac:   SM.onRBrac(); break;
1154     case AsmToken::LParen:  SM.onLParen(); break;
1155     case AsmToken::RParen:  SM.onRParen(); break;
1156     }
1157     if (SM.hadError())
1158       return Error(Tok.getLoc(), "unknown token in expression");
1159
1160     if (!Done && UpdateLocLex)
1161       End = consumeToken();
1162   }
1163   return false;
1164 }
1165
1166 X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
1167                                                    int64_t ImmDisp,
1168                                                    unsigned Size) {
1169   const AsmToken &Tok = Parser.getTok();
1170   SMLoc BracLoc = Tok.getLoc(), End = Tok.getEndLoc();
1171   if (getLexer().isNot(AsmToken::LBrac))
1172     return ErrorOperand(BracLoc, "Expected '[' token!");
1173   Parser.Lex(); // Eat '['
1174
1175   SMLoc StartInBrac = Tok.getLoc();
1176   // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ].  We
1177   // may have already parsed an immediate displacement before the bracketed
1178   // expression.
1179   IntelExprStateMachine SM(ImmDisp, /*StopOnLBrac=*/false, /*AddImmPrefix=*/true);
1180   if (ParseIntelExpression(SM, End))
1181     return 0;
1182
1183   const MCExpr *Disp = 0;
1184   if (const MCExpr *Sym = SM.getSym()) {
1185     // A symbolic displacement.
1186     Disp = Sym;
1187     if (isParsingInlineAsm())
1188       RewriteIntelBracExpression(InstInfo->AsmRewrites, SM.getSymName(),
1189                                  ImmDisp, SM.getImm(), BracLoc, StartInBrac,
1190                                  End);
1191   }
1192
1193   if (SM.getImm() || !Disp) {
1194     const MCExpr *Imm = MCConstantExpr::Create(SM.getImm(), getContext());
1195     if (Disp)
1196       Disp = MCBinaryExpr::CreateAdd(Disp, Imm, getContext());
1197     else
1198       Disp = Imm;  // An immediate displacement only.
1199   }
1200
1201   // Parse struct field access.  Intel requires a dot, but MSVC doesn't.  MSVC
1202   // will in fact do global lookup the field name inside all global typedefs,
1203   // but we don't emulate that.
1204   if (Tok.getString().find('.') != StringRef::npos) {
1205     const MCExpr *NewDisp;
1206     if (ParseIntelDotOperator(Disp, NewDisp))
1207       return 0;
1208     
1209     End = Tok.getEndLoc();
1210     Parser.Lex();  // Eat the field.
1211     Disp = NewDisp;
1212   }
1213
1214   int BaseReg = SM.getBaseReg();
1215   int IndexReg = SM.getIndexReg();
1216   int Scale = SM.getScale();
1217   if (!isParsingInlineAsm()) {
1218     // handle [-42]
1219     if (!BaseReg && !IndexReg) {
1220       if (!SegReg)
1221         return X86Operand::CreateMem(Disp, Start, End, Size);
1222       else
1223         return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1224     }
1225     StringRef ErrMsg;
1226     if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1227       Error(StartInBrac, ErrMsg);
1228       return 0;
1229     }
1230     return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1231                                  End, Size);
1232   }
1233
1234   InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
1235   return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1236                                End, Size, SM.getSymName(), Info);
1237 }
1238
1239 // Inline assembly may use variable names with namespace alias qualifiers.
1240 bool X86AsmParser::ParseIntelIdentifier(const MCExpr *&Val,
1241                                         StringRef &Identifier,
1242                                         InlineAsmIdentifierInfo &Info,
1243                                         bool IsUnevaluatedOperand, SMLoc &End) {
1244   assert (isParsingInlineAsm() && "Expected to be parsing inline assembly.");
1245   Val = 0;
1246
1247   StringRef LineBuf(Identifier.data());
1248   SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
1249
1250   const AsmToken &Tok = Parser.getTok();
1251
1252   // Advance the token stream until the end of the current token is
1253   // after the end of what the frontend claimed.
1254   const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
1255   while (true) {
1256     End = Tok.getEndLoc();
1257     getLexer().Lex();
1258
1259     assert(End.getPointer() <= EndPtr && "frontend claimed part of a token?");
1260     if (End.getPointer() == EndPtr) break;
1261   }
1262
1263   // Create the symbol reference.
1264   Identifier = LineBuf;
1265   MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1266   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1267   Val = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1268   return false;
1269 }
1270
1271 /// \brief Parse intel style segment override.
1272 X86Operand *X86AsmParser::ParseIntelSegmentOverride(unsigned SegReg,
1273                                                     SMLoc Start,
1274                                                     unsigned Size) {
1275   assert(SegReg != 0 && "Tried to parse a segment override without a segment!");
1276   const AsmToken &Tok = Parser.getTok(); // Eat colon.
1277   if (Tok.isNot(AsmToken::Colon))
1278     return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1279   Parser.Lex(); // Eat ':'
1280
1281   int64_t ImmDisp = 0;
1282   if (getLexer().is(AsmToken::Integer)) {
1283     ImmDisp = Tok.getIntVal();
1284     AsmToken ImmDispToken = Parser.Lex(); // Eat the integer.
1285
1286     if (isParsingInlineAsm())
1287       InstInfo->AsmRewrites->push_back(
1288           AsmRewrite(AOK_ImmPrefix, ImmDispToken.getLoc()));
1289
1290     if (getLexer().isNot(AsmToken::LBrac)) {
1291       // An immediate following a 'segment register', 'colon' token sequence can
1292       // be followed by a bracketed expression.  If it isn't we know we have our
1293       // final segment override.
1294       const MCExpr *Disp = MCConstantExpr::Create(ImmDisp, getContext());
1295       return X86Operand::CreateMem(SegReg, Disp, /*BaseReg=*/0, /*IndexReg=*/0,
1296                                    /*Scale=*/1, Start, ImmDispToken.getEndLoc(),
1297                                    Size);
1298     }
1299   }
1300
1301   if (getLexer().is(AsmToken::LBrac))
1302     return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
1303
1304   const MCExpr *Val;
1305   SMLoc End;
1306   if (!isParsingInlineAsm()) {
1307     if (getParser().parsePrimaryExpr(Val, End))
1308       return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1309
1310     return X86Operand::CreateMem(Val, Start, End, Size);
1311   }
1312
1313   InlineAsmIdentifierInfo Info;
1314   StringRef Identifier = Tok.getString();
1315   if (ParseIntelIdentifier(Val, Identifier, Info,
1316                            /*Unevaluated=*/false, End))
1317     return 0;
1318   return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0,/*IndexReg=*/0,
1319                                /*Scale=*/1, Start, End, Size, Identifier, Info);
1320 }
1321
1322 /// ParseIntelMemOperand - Parse intel style memory operand.
1323 X86Operand *X86AsmParser::ParseIntelMemOperand(int64_t ImmDisp, SMLoc Start,
1324                                                unsigned Size) {
1325   const AsmToken &Tok = Parser.getTok();
1326   SMLoc End;
1327
1328   // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1329   if (getLexer().is(AsmToken::LBrac))
1330     return ParseIntelBracExpression(/*SegReg=*/0, Start, ImmDisp, Size);
1331   assert(ImmDisp == 0);
1332
1333   const MCExpr *Val;
1334   if (!isParsingInlineAsm()) {
1335     if (getParser().parsePrimaryExpr(Val, End))
1336       return ErrorOperand(Tok.getLoc(), "unknown token in expression");
1337
1338     return X86Operand::CreateMem(Val, Start, End, Size);
1339   }
1340
1341   InlineAsmIdentifierInfo Info;
1342   StringRef Identifier = Tok.getString();
1343   if (ParseIntelIdentifier(Val, Identifier, Info,
1344                            /*Unevaluated=*/false, End))
1345     return 0;
1346
1347   if (!getLexer().is(AsmToken::LBrac))
1348     return CreateMemForInlineAsm(/*SegReg=*/0, Val, /*BaseReg=*/0, /*IndexReg=*/0,
1349                                  /*Scale=*/1, Start, End, Size, Identifier, Info);
1350
1351   Parser.Lex(); // Eat '['
1352
1353   // Parse Identifier [ ImmDisp ]
1354   IntelExprStateMachine SM(/*ImmDisp=*/0, /*StopOnLBrac=*/true,
1355                            /*AddImmPrefix=*/false);
1356   if (ParseIntelExpression(SM, End))
1357     return 0;
1358
1359   if (SM.getSym()) {
1360     Error(Start, "cannot use more than one symbol in memory operand");
1361     return 0;
1362   }
1363   if (SM.getBaseReg()) {
1364     Error(Start, "cannot use base register with variable reference");
1365     return 0;
1366   }
1367   if (SM.getIndexReg()) {
1368     Error(Start, "cannot use index register with variable reference");
1369     return 0;
1370   }
1371
1372   const MCExpr *Disp = MCConstantExpr::Create(SM.getImm(), getContext());
1373   // BaseReg is non-zero to avoid assertions.  In the context of inline asm,
1374   // we're pointing to a local variable in memory, so the base register is
1375   // really the frame or stack pointer.
1376   return X86Operand::CreateMem(/*SegReg=*/0, Disp, /*BaseReg=*/1, /*IndexReg=*/0,
1377                                /*Scale=*/1, Start, End, Size, Identifier,
1378                                Info.OpDecl);
1379 }
1380
1381 /// Parse the '.' operator.
1382 bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1383                                                 const MCExpr *&NewDisp) {
1384   const AsmToken &Tok = Parser.getTok();
1385   int64_t OrigDispVal, DotDispVal;
1386
1387   // FIXME: Handle non-constant expressions.
1388   if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
1389     OrigDispVal = OrigDisp->getValue();
1390   else
1391     return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
1392
1393   // Drop the optional '.'.
1394   StringRef DotDispStr = Tok.getString();
1395   if (DotDispStr.startswith("."))
1396     DotDispStr = DotDispStr.drop_front(1);
1397
1398   // .Imm gets lexed as a real.
1399   if (Tok.is(AsmToken::Real)) {
1400     APInt DotDisp;
1401     DotDispStr.getAsInteger(10, DotDisp);
1402     DotDispVal = DotDisp.getZExtValue();
1403   } else if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1404     unsigned DotDisp;
1405     std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1406     if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1407                                            DotDisp))
1408       return Error(Tok.getLoc(), "Unable to lookup field reference!");
1409     DotDispVal = DotDisp;
1410   } else
1411     return Error(Tok.getLoc(), "Unexpected token type!");
1412
1413   if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1414     SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1415     unsigned Len = DotDispStr.size();
1416     unsigned Val = OrigDispVal + DotDispVal;
1417     InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1418                                                 Val));
1419   }
1420
1421   NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1422   return false;
1423 }
1424
1425 /// Parse the 'offset' operator.  This operator is used to specify the
1426 /// location rather then the content of a variable.
1427 X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
1428   const AsmToken &Tok = Parser.getTok();
1429   SMLoc OffsetOfLoc = Tok.getLoc();
1430   Parser.Lex(); // Eat offset.
1431
1432   const MCExpr *Val;
1433   InlineAsmIdentifierInfo Info;
1434   SMLoc Start = Tok.getLoc(), End;
1435   StringRef Identifier = Tok.getString();
1436   if (ParseIntelIdentifier(Val, Identifier, Info,
1437                            /*Unevaluated=*/false, End))
1438     return 0;
1439
1440   // Don't emit the offset operator.
1441   InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1442
1443   // The offset operator will have an 'r' constraint, thus we need to create
1444   // register operand to ensure proper matching.  Just pick a GPR based on
1445   // the size of a pointer.
1446   unsigned RegNo =
1447       is64BitMode() ? X86::RBX : (is32BitMode() ? X86::EBX : X86::BX);
1448   return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1449                                OffsetOfLoc, Identifier, Info.OpDecl);
1450 }
1451
1452 enum IntelOperatorKind {
1453   IOK_LENGTH,
1454   IOK_SIZE,
1455   IOK_TYPE
1456 };
1457
1458 /// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators.  The LENGTH operator
1459 /// returns the number of elements in an array.  It returns the value 1 for
1460 /// non-array variables.  The SIZE operator returns the size of a C or C++
1461 /// variable.  A variable's size is the product of its LENGTH and TYPE.  The
1462 /// TYPE operator returns the size of a C or C++ type or variable. If the
1463 /// variable is an array, TYPE returns the size of a single element.
1464 X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
1465   const AsmToken &Tok = Parser.getTok();
1466   SMLoc TypeLoc = Tok.getLoc();
1467   Parser.Lex(); // Eat operator.
1468
1469   const MCExpr *Val = 0;
1470   InlineAsmIdentifierInfo Info;
1471   SMLoc Start = Tok.getLoc(), End;
1472   StringRef Identifier = Tok.getString();
1473   if (ParseIntelIdentifier(Val, Identifier, Info,
1474                            /*Unevaluated=*/true, End))
1475     return 0;
1476
1477   if (!Info.OpDecl)
1478     return ErrorOperand(Start, "unable to lookup expression");
1479
1480   unsigned CVal = 0;
1481   switch(OpKind) {
1482   default: llvm_unreachable("Unexpected operand kind!");
1483   case IOK_LENGTH: CVal = Info.Length; break;
1484   case IOK_SIZE: CVal = Info.Size; break;
1485   case IOK_TYPE: CVal = Info.Type; break;
1486   }
1487
1488   // Rewrite the type operator and the C or C++ type or variable in terms of an
1489   // immediate.  E.g. TYPE foo -> $$4
1490   unsigned Len = End.getPointer() - TypeLoc.getPointer();
1491   InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
1492
1493   const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
1494   return X86Operand::CreateImm(Imm, Start, End);
1495 }
1496
1497 X86Operand *X86AsmParser::ParseIntelOperand() {
1498   const AsmToken &Tok = Parser.getTok();
1499   SMLoc Start, End;
1500
1501   // Offset, length, type and size operators.
1502   if (isParsingInlineAsm()) {
1503     StringRef AsmTokStr = Tok.getString();
1504     if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1505       return ParseIntelOffsetOfOperator();
1506     if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1507       return ParseIntelOperator(IOK_LENGTH);
1508     if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1509       return ParseIntelOperator(IOK_SIZE);
1510     if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1511       return ParseIntelOperator(IOK_TYPE);
1512   }
1513
1514   unsigned Size = getIntelMemOperandSize(Tok.getString());
1515   if (Size) {
1516     Parser.Lex(); // Eat operand size (e.g., byte, word).
1517     if (Tok.getString() != "PTR" && Tok.getString() != "ptr")
1518       return ErrorOperand(Start, "Expected 'PTR' or 'ptr' token!");
1519     Parser.Lex(); // Eat ptr.
1520   }
1521   Start = Tok.getLoc();
1522
1523   // Immediate.
1524   if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Minus) ||
1525       getLexer().is(AsmToken::LParen)) {    
1526     AsmToken StartTok = Tok;
1527     IntelExprStateMachine SM(/*Imm=*/0, /*StopOnLBrac=*/true,
1528                              /*AddImmPrefix=*/false);
1529     if (ParseIntelExpression(SM, End))
1530       return 0;
1531
1532     int64_t Imm = SM.getImm();
1533     if (isParsingInlineAsm()) {
1534       unsigned Len = Tok.getLoc().getPointer() - Start.getPointer();
1535       if (StartTok.getString().size() == Len)
1536         // Just add a prefix if this wasn't a complex immediate expression.
1537         InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
1538       else
1539         // Otherwise, rewrite the complex expression as a single immediate.
1540         InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, Len, Imm));
1541     }
1542
1543     if (getLexer().isNot(AsmToken::LBrac)) {
1544       // If a directional label (ie. 1f or 2b) was parsed above from
1545       // ParseIntelExpression() then SM.getSym() was set to a pointer to
1546       // to the MCExpr with the directional local symbol and this is a
1547       // memory operand not an immediate operand.
1548       if (SM.getSym())
1549         return X86Operand::CreateMem(SM.getSym(), Start, End, Size);
1550
1551       const MCExpr *ImmExpr = MCConstantExpr::Create(Imm, getContext());
1552       return X86Operand::CreateImm(ImmExpr, Start, End);
1553     }
1554
1555     // Only positive immediates are valid.
1556     if (Imm < 0)
1557       return ErrorOperand(Start, "expected a positive immediate displacement "
1558                           "before bracketed expr.");
1559
1560     // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1561     return ParseIntelMemOperand(Imm, Start, Size);
1562   }
1563
1564   // Register.
1565   unsigned RegNo = 0;
1566   if (!ParseRegister(RegNo, Start, End)) {
1567     // If this is a segment register followed by a ':', then this is the start
1568     // of a segment override, otherwise this is a normal register reference.
1569     if (getLexer().isNot(AsmToken::Colon))
1570       return X86Operand::CreateReg(RegNo, Start, End);
1571
1572     return ParseIntelSegmentOverride(/*SegReg=*/RegNo, Start, Size);
1573   }
1574
1575   // Memory operand.
1576   return ParseIntelMemOperand(/*Disp=*/0, Start, Size);
1577 }
1578
1579 X86Operand *X86AsmParser::ParseATTOperand() {
1580   switch (getLexer().getKind()) {
1581   default:
1582     // Parse a memory operand with no segment register.
1583     return ParseMemOperand(0, Parser.getTok().getLoc());
1584   case AsmToken::Percent: {
1585     // Read the register.
1586     unsigned RegNo;
1587     SMLoc Start, End;
1588     if (ParseRegister(RegNo, Start, End)) return 0;
1589     if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
1590       Error(Start, "%eiz and %riz can only be used as index registers",
1591             SMRange(Start, End));
1592       return 0;
1593     }
1594
1595     // If this is a segment register followed by a ':', then this is the start
1596     // of a memory reference, otherwise this is a normal register reference.
1597     if (getLexer().isNot(AsmToken::Colon))
1598       return X86Operand::CreateReg(RegNo, Start, End);
1599
1600     getParser().Lex(); // Eat the colon.
1601     return ParseMemOperand(RegNo, Start);
1602   }
1603   case AsmToken::Dollar: {
1604     // $42 -> immediate.
1605     SMLoc Start = Parser.getTok().getLoc(), End;
1606     Parser.Lex();
1607     const MCExpr *Val;
1608     if (getParser().parseExpression(Val, End))
1609       return 0;
1610     return X86Operand::CreateImm(Val, Start, End);
1611   }
1612   }
1613 }
1614
1615 bool
1616 X86AsmParser::HandleAVX512Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1617                                   const MCParsedAsmOperand &Op) {
1618   if(STI.getFeatureBits() & X86::FeatureAVX512) {
1619     if (getLexer().is(AsmToken::LCurly)) {
1620       // Eat "{" and mark the current place.
1621       const SMLoc consumedToken = consumeToken();
1622       // Distinguish {1to<NUM>} from {%k<NUM>}.
1623       if(getLexer().is(AsmToken::Integer)) {
1624         // Parse memory broadcasting ({1to<NUM>}).
1625         if (getLexer().getTok().getIntVal() != 1)
1626           return !ErrorAndEatStatement(getLexer().getLoc(),
1627                                        "Expected 1to<NUM> at this point");
1628         Parser.Lex();  // Eat "1" of 1to8
1629         if (!getLexer().is(AsmToken::Identifier) ||
1630             !getLexer().getTok().getIdentifier().startswith("to"))
1631           return !ErrorAndEatStatement(getLexer().getLoc(),
1632                                        "Expected 1to<NUM> at this point");
1633         // Recognize only reasonable suffixes.
1634         const char *BroadcastPrimitive =
1635           StringSwitch<const char*>(getLexer().getTok().getIdentifier())
1636             .Case("to8",  "{1to8}")
1637             .Case("to16", "{1to16}")
1638             .Default(0);
1639         if (!BroadcastPrimitive)
1640           return !ErrorAndEatStatement(getLexer().getLoc(),
1641                                        "Invalid memory broadcast primitive.");
1642         Parser.Lex();  // Eat "toN" of 1toN
1643         if (!getLexer().is(AsmToken::RCurly))
1644           return !ErrorAndEatStatement(getLexer().getLoc(),
1645                                        "Expected } at this point");
1646         Parser.Lex();  // Eat "}"
1647         Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
1648                                                    consumedToken));
1649         // No AVX512 specific primitives can pass
1650         // after memory broadcasting, so return.
1651         return true;
1652       } else {
1653         // Parse mask register {%k1}
1654         Operands.push_back(X86Operand::CreateToken("{", consumedToken));
1655         if (X86Operand *Op = ParseOperand()) {
1656           Operands.push_back(Op);
1657           if (!getLexer().is(AsmToken::RCurly))
1658             return !ErrorAndEatStatement(getLexer().getLoc(),
1659                                          "Expected } at this point");
1660           Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
1661
1662           // Parse "zeroing non-masked" semantic {z}
1663           if (getLexer().is(AsmToken::LCurly)) {
1664             Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
1665             if (!getLexer().is(AsmToken::Identifier) ||
1666                 getLexer().getTok().getIdentifier() != "z")
1667               return !ErrorAndEatStatement(getLexer().getLoc(),
1668                                            "Expected z at this point");
1669             Parser.Lex();  // Eat the z
1670             if (!getLexer().is(AsmToken::RCurly))
1671               return !ErrorAndEatStatement(getLexer().getLoc(),
1672                                            "Expected } at this point");
1673             Parser.Lex();  // Eat the }
1674           }
1675         }
1676       }
1677     }
1678   }
1679   return true;
1680 }
1681
1682 /// ParseMemOperand: segment: disp(basereg, indexreg, scale).  The '%ds:' prefix
1683 /// has already been parsed if present.
1684 X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
1685
1686   // We have to disambiguate a parenthesized expression "(4+5)" from the start
1687   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
1688   // only way to do this without lookahead is to eat the '(' and see what is
1689   // after it.
1690   const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
1691   if (getLexer().isNot(AsmToken::LParen)) {
1692     SMLoc ExprEnd;
1693     if (getParser().parseExpression(Disp, ExprEnd)) return 0;
1694
1695     // After parsing the base expression we could either have a parenthesized
1696     // memory address or not.  If not, return now.  If so, eat the (.
1697     if (getLexer().isNot(AsmToken::LParen)) {
1698       // Unless we have a segment register, treat this as an immediate.
1699       if (SegReg == 0)
1700         return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
1701       return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
1702     }
1703
1704     // Eat the '('.
1705     Parser.Lex();
1706   } else {
1707     // Okay, we have a '('.  We don't know if this is an expression or not, but
1708     // so we have to eat the ( to see beyond it.
1709     SMLoc LParenLoc = Parser.getTok().getLoc();
1710     Parser.Lex(); // Eat the '('.
1711
1712     if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
1713       // Nothing to do here, fall into the code below with the '(' part of the
1714       // memory operand consumed.
1715     } else {
1716       SMLoc ExprEnd;
1717
1718       // It must be an parenthesized expression, parse it now.
1719       if (getParser().parseParenExpression(Disp, ExprEnd))
1720         return 0;
1721
1722       // After parsing the base expression we could either have a parenthesized
1723       // memory address or not.  If not, return now.  If so, eat the (.
1724       if (getLexer().isNot(AsmToken::LParen)) {
1725         // Unless we have a segment register, treat this as an immediate.
1726         if (SegReg == 0)
1727           return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
1728         return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
1729       }
1730
1731       // Eat the '('.
1732       Parser.Lex();
1733     }
1734   }
1735
1736   // If we reached here, then we just ate the ( of the memory operand.  Process
1737   // the rest of the memory operand.
1738   unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
1739   SMLoc IndexLoc, BaseLoc;
1740
1741   if (getLexer().is(AsmToken::Percent)) {
1742     SMLoc StartLoc, EndLoc;
1743     BaseLoc = Parser.getTok().getLoc();
1744     if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
1745     if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
1746       Error(StartLoc, "eiz and riz can only be used as index registers",
1747             SMRange(StartLoc, EndLoc));
1748       return 0;
1749     }
1750   }
1751
1752   if (getLexer().is(AsmToken::Comma)) {
1753     Parser.Lex(); // Eat the comma.
1754     IndexLoc = Parser.getTok().getLoc();
1755
1756     // Following the comma we should have either an index register, or a scale
1757     // value. We don't support the later form, but we want to parse it
1758     // correctly.
1759     //
1760     // Not that even though it would be completely consistent to support syntax
1761     // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
1762     if (getLexer().is(AsmToken::Percent)) {
1763       SMLoc L;
1764       if (ParseRegister(IndexReg, L, L)) return 0;
1765
1766       if (getLexer().isNot(AsmToken::RParen)) {
1767         // Parse the scale amount:
1768         //  ::= ',' [scale-expression]
1769         if (getLexer().isNot(AsmToken::Comma)) {
1770           Error(Parser.getTok().getLoc(),
1771                 "expected comma in scale expression");
1772           return 0;
1773         }
1774         Parser.Lex(); // Eat the comma.
1775
1776         if (getLexer().isNot(AsmToken::RParen)) {
1777           SMLoc Loc = Parser.getTok().getLoc();
1778
1779           int64_t ScaleVal;
1780           if (getParser().parseAbsoluteExpression(ScaleVal)){
1781             Error(Loc, "expected scale expression");
1782             return 0;
1783           }
1784
1785           // Validate the scale amount.
1786           if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1787               ScaleVal != 1) {
1788             Error(Loc, "scale factor in 16-bit address must be 1");
1789             return 0;
1790           }
1791           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1792             Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1793             return 0;
1794           }
1795           Scale = (unsigned)ScaleVal;
1796         }
1797       }
1798     } else if (getLexer().isNot(AsmToken::RParen)) {
1799       // A scale amount without an index is ignored.
1800       // index.
1801       SMLoc Loc = Parser.getTok().getLoc();
1802
1803       int64_t Value;
1804       if (getParser().parseAbsoluteExpression(Value))
1805         return 0;
1806
1807       if (Value != 1)
1808         Warning(Loc, "scale factor without index register is ignored");
1809       Scale = 1;
1810     }
1811   }
1812
1813   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
1814   if (getLexer().isNot(AsmToken::RParen)) {
1815     Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
1816     return 0;
1817   }
1818   SMLoc MemEnd = Parser.getTok().getEndLoc();
1819   Parser.Lex(); // Eat the ')'.
1820
1821   // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
1822   // and then only in non-64-bit modes. Except for DX, which is a special case
1823   // because an unofficial form of in/out instructions uses it.
1824   if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
1825       (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
1826                          BaseReg != X86::SI && BaseReg != X86::DI)) &&
1827       BaseReg != X86::DX) {
1828     Error(BaseLoc, "invalid 16-bit base register");
1829     return 0;
1830   }
1831   if (BaseReg == 0 &&
1832       X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
1833     Error(IndexLoc, "16-bit memory operand may not include only index register");
1834     return 0;
1835   }
1836
1837   StringRef ErrMsg;
1838   if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
1839     Error(BaseLoc, ErrMsg);
1840     return 0;
1841   }
1842
1843   return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1844                                MemStart, MemEnd);
1845 }
1846
1847 bool X86AsmParser::
1848 ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
1849                  SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1850   InstInfo = &Info;
1851   StringRef PatchedName = Name;
1852
1853   // FIXME: Hack to recognize setneb as setne.
1854   if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1855       PatchedName != "setb" && PatchedName != "setnb")
1856     PatchedName = PatchedName.substr(0, Name.size()-1);
1857
1858   // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1859   const MCExpr *ExtraImmOp = 0;
1860   if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
1861       (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1862        PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
1863     bool IsVCMP = PatchedName[0] == 'v';
1864     unsigned SSECCIdx = IsVCMP ? 4 : 3;
1865     unsigned SSEComparisonCode = StringSwitch<unsigned>(
1866       PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
1867       .Case("eq",       0x00)
1868       .Case("lt",       0x01)
1869       .Case("le",       0x02)
1870       .Case("unord",    0x03)
1871       .Case("neq",      0x04)
1872       .Case("nlt",      0x05)
1873       .Case("nle",      0x06)
1874       .Case("ord",      0x07)
1875       /* AVX only from here */
1876       .Case("eq_uq",    0x08)
1877       .Case("nge",      0x09)
1878       .Case("ngt",      0x0A)
1879       .Case("false",    0x0B)
1880       .Case("neq_oq",   0x0C)
1881       .Case("ge",       0x0D)
1882       .Case("gt",       0x0E)
1883       .Case("true",     0x0F)
1884       .Case("eq_os",    0x10)
1885       .Case("lt_oq",    0x11)
1886       .Case("le_oq",    0x12)
1887       .Case("unord_s",  0x13)
1888       .Case("neq_us",   0x14)
1889       .Case("nlt_uq",   0x15)
1890       .Case("nle_uq",   0x16)
1891       .Case("ord_s",    0x17)
1892       .Case("eq_us",    0x18)
1893       .Case("nge_uq",   0x19)
1894       .Case("ngt_uq",   0x1A)
1895       .Case("false_os", 0x1B)
1896       .Case("neq_os",   0x1C)
1897       .Case("ge_oq",    0x1D)
1898       .Case("gt_oq",    0x1E)
1899       .Case("true_us",  0x1F)
1900       .Default(~0U);
1901     if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
1902       ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1903                                           getParser().getContext());
1904       if (PatchedName.endswith("ss")) {
1905         PatchedName = IsVCMP ? "vcmpss" : "cmpss";
1906       } else if (PatchedName.endswith("sd")) {
1907         PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
1908       } else if (PatchedName.endswith("ps")) {
1909         PatchedName = IsVCMP ? "vcmpps" : "cmpps";
1910       } else {
1911         assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
1912         PatchedName = IsVCMP ? "vcmppd" : "cmppd";
1913       }
1914     }
1915   }
1916
1917   Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
1918
1919   if (ExtraImmOp && !isParsingIntelSyntax())
1920     Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1921
1922   // Determine whether this is an instruction prefix.
1923   bool isPrefix =
1924     Name == "lock" || Name == "rep" ||
1925     Name == "repe" || Name == "repz" ||
1926     Name == "repne" || Name == "repnz" ||
1927     Name == "rex64" || Name == "data16";
1928
1929
1930   // This does the actual operand parsing.  Don't parse any more if we have a
1931   // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1932   // just want to parse the "lock" as the first instruction and the "incl" as
1933   // the next one.
1934   if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
1935
1936     // Parse '*' modifier.
1937     if (getLexer().is(AsmToken::Star))
1938       Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
1939
1940     // Read the operands.
1941     while(1) {
1942       if (X86Operand *Op = ParseOperand()) {
1943          Operands.push_back(Op);
1944         if (!HandleAVX512Operand(Operands, *Op))
1945           return true;
1946       } else {
1947          Parser.eatToEndOfStatement();
1948          return true;
1949       }
1950       // check for comma and eat it
1951       if (getLexer().is(AsmToken::Comma))
1952         Parser.Lex();
1953       else
1954         break;
1955      }
1956
1957     if (getLexer().isNot(AsmToken::EndOfStatement))
1958       return ErrorAndEatStatement(getLexer().getLoc(),
1959                                   "unexpected token in argument list");
1960    }
1961
1962   // Consume the EndOfStatement or the prefix separator Slash
1963   if (getLexer().is(AsmToken::EndOfStatement) ||
1964       (isPrefix && getLexer().is(AsmToken::Slash)))
1965     Parser.Lex();
1966
1967   if (ExtraImmOp && isParsingIntelSyntax())
1968     Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1969
1970   // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1971   // "outb %al, %dx".  Out doesn't take a memory form, but this is a widely
1972   // documented form in various unofficial manuals, so a lot of code uses it.
1973   if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1974       Operands.size() == 3) {
1975     X86Operand &Op = *(X86Operand*)Operands.back();
1976     if (Op.isMem() && Op.Mem.SegReg == 0 &&
1977         isa<MCConstantExpr>(Op.Mem.Disp) &&
1978         cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1979         Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1980       SMLoc Loc = Op.getEndLoc();
1981       Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1982       delete &Op;
1983     }
1984   }
1985   // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1986   if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1987       Operands.size() == 3) {
1988     X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1989     if (Op.isMem() && Op.Mem.SegReg == 0 &&
1990         isa<MCConstantExpr>(Op.Mem.Disp) &&
1991         cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1992         Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1993       SMLoc Loc = Op.getEndLoc();
1994       Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1995       delete &Op;
1996     }
1997   }
1998
1999   // Append default arguments to "ins[bwld]"
2000   if (Name.startswith("ins") && Operands.size() == 1 &&
2001       (Name == "insb" || Name == "insw" || Name == "insl" ||
2002        Name == "insd" )) {
2003     if (isParsingIntelSyntax()) {
2004       Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2005       Operands.push_back(DefaultMemDIOperand(NameLoc));
2006     } else {
2007       Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2008       Operands.push_back(DefaultMemDIOperand(NameLoc));
2009     }
2010   }
2011
2012   // Append default arguments to "outs[bwld]"
2013   if (Name.startswith("outs") && Operands.size() == 1 &&
2014       (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
2015        Name == "outsd" )) {
2016     if (isParsingIntelSyntax()) {
2017       Operands.push_back(DefaultMemSIOperand(NameLoc));
2018       Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2019     } else {
2020       Operands.push_back(DefaultMemSIOperand(NameLoc));
2021       Operands.push_back(X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
2022     }
2023   }
2024
2025   // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
2026   // values of $SIREG according to the mode. It would be nice if this
2027   // could be achieved with InstAlias in the tables.
2028   if (Name.startswith("lods") && Operands.size() == 1 &&
2029       (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
2030        Name == "lodsl" || Name == "lodsd" || Name == "lodsq"))
2031     Operands.push_back(DefaultMemSIOperand(NameLoc));
2032
2033   // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
2034   // values of $DIREG according to the mode. It would be nice if this
2035   // could be achieved with InstAlias in the tables.
2036   if (Name.startswith("stos") && Operands.size() == 1 &&
2037       (Name == "stos" || Name == "stosb" || Name == "stosw" ||
2038        Name == "stosl" || Name == "stosd" || Name == "stosq"))
2039     Operands.push_back(DefaultMemDIOperand(NameLoc));
2040
2041   // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
2042   // values of $DIREG according to the mode. It would be nice if this
2043   // could be achieved with InstAlias in the tables.
2044   if (Name.startswith("scas") && Operands.size() == 1 &&
2045       (Name == "scas" || Name == "scasb" || Name == "scasw" ||
2046        Name == "scasl" || Name == "scasd" || Name == "scasq"))
2047     Operands.push_back(DefaultMemDIOperand(NameLoc));
2048
2049   // Add default SI and DI operands to "cmps[bwlq]".
2050   if (Name.startswith("cmps") &&
2051       (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
2052        Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
2053     if (Operands.size() == 1) {
2054       if (isParsingIntelSyntax()) {
2055         Operands.push_back(DefaultMemSIOperand(NameLoc));
2056         Operands.push_back(DefaultMemDIOperand(NameLoc));
2057       } else {
2058         Operands.push_back(DefaultMemDIOperand(NameLoc));
2059         Operands.push_back(DefaultMemSIOperand(NameLoc));
2060       }
2061     } else if (Operands.size() == 3) {
2062       X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2063       X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2064       if (!doSrcDstMatch(Op, Op2))
2065         return Error(Op.getStartLoc(),
2066                      "mismatching source and destination index registers");
2067     }
2068   }
2069
2070   // Add default SI and DI operands to "movs[bwlq]".
2071   if ((Name.startswith("movs") &&
2072       (Name == "movs" || Name == "movsb" || Name == "movsw" ||
2073        Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
2074       (Name.startswith("smov") &&
2075       (Name == "smov" || Name == "smovb" || Name == "smovw" ||
2076        Name == "smovl" || Name == "smovd" || Name == "smovq"))) {
2077     if (Operands.size() == 1) {
2078       if (Name == "movsd") {
2079         delete Operands.back();
2080         Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
2081       }
2082       if (isParsingIntelSyntax()) {
2083         Operands.push_back(DefaultMemDIOperand(NameLoc));
2084         Operands.push_back(DefaultMemSIOperand(NameLoc));
2085       } else {
2086         Operands.push_back(DefaultMemSIOperand(NameLoc));
2087         Operands.push_back(DefaultMemDIOperand(NameLoc));
2088       }
2089     } else if (Operands.size() == 3) {
2090       X86Operand &Op = *(X86Operand*)Operands.begin()[1];
2091       X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
2092       if (!doSrcDstMatch(Op, Op2))
2093         return Error(Op.getStartLoc(),
2094                      "mismatching source and destination index registers");
2095     }
2096   }
2097
2098   // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>.  Canonicalize to
2099   // "shift <op>".
2100   if ((Name.startswith("shr") || Name.startswith("sar") ||
2101        Name.startswith("shl") || Name.startswith("sal") ||
2102        Name.startswith("rcl") || Name.startswith("rcr") ||
2103        Name.startswith("rol") || Name.startswith("ror")) &&
2104       Operands.size() == 3) {
2105     if (isParsingIntelSyntax()) {
2106       // Intel syntax
2107       X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2108       if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2109           cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2110         delete Operands[2];
2111         Operands.pop_back();
2112       }
2113     } else {
2114       X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2115       if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2116           cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2117         delete Operands[1];
2118         Operands.erase(Operands.begin() + 1);
2119       }
2120     }
2121   }
2122
2123   // Transforms "int $3" into "int3" as a size optimization.  We can't write an
2124   // instalias with an immediate operand yet.
2125   if (Name == "int" && Operands.size() == 2) {
2126     X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2127     if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2128         cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2129       delete Operands[1];
2130       Operands.erase(Operands.begin() + 1);
2131       static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2132     }
2133   }
2134
2135   return false;
2136 }
2137
2138 static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2139                             bool isCmp) {
2140   MCInst TmpInst;
2141   TmpInst.setOpcode(Opcode);
2142   if (!isCmp)
2143     TmpInst.addOperand(MCOperand::CreateReg(Reg));
2144   TmpInst.addOperand(MCOperand::CreateReg(Reg));
2145   TmpInst.addOperand(Inst.getOperand(0));
2146   Inst = TmpInst;
2147   return true;
2148 }
2149
2150 static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2151                                 bool isCmp = false) {
2152   if (!Inst.getOperand(0).isImm() ||
2153       !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2154     return false;
2155
2156   return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2157 }
2158
2159 static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2160                                 bool isCmp = false) {
2161   if (!Inst.getOperand(0).isImm() ||
2162       !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2163     return false;
2164
2165   return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2166 }
2167
2168 static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2169                                 bool isCmp = false) {
2170   if (!Inst.getOperand(0).isImm() ||
2171       !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2172     return false;
2173
2174   return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2175 }
2176
2177 bool X86AsmParser::
2178 processInstruction(MCInst &Inst,
2179                    const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2180   switch (Inst.getOpcode()) {
2181   default: return false;
2182   case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2183   case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2184   case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2185   case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2186   case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2187   case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2188   case X86::OR16i16:  return convert16i16to16ri8(Inst, X86::OR16ri8);
2189   case X86::OR32i32:  return convert32i32to32ri8(Inst, X86::OR32ri8);
2190   case X86::OR64i32:  return convert64i32to64ri8(Inst, X86::OR64ri8);
2191   case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2192   case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2193   case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2194   case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2195   case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2196   case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2197   case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2198   case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2199   case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
2200   case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2201   case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2202   case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2203   case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2204   case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2205   case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
2206   case X86::VMOVAPDrr:
2207   case X86::VMOVAPDYrr:
2208   case X86::VMOVAPSrr:
2209   case X86::VMOVAPSYrr:
2210   case X86::VMOVDQArr:
2211   case X86::VMOVDQAYrr:
2212   case X86::VMOVDQUrr:
2213   case X86::VMOVDQUYrr:
2214   case X86::VMOVUPDrr:
2215   case X86::VMOVUPDYrr:
2216   case X86::VMOVUPSrr:
2217   case X86::VMOVUPSYrr: {
2218     if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2219         !X86II::isX86_64ExtendedReg(Inst.getOperand(1).getReg()))
2220       return false;
2221
2222     unsigned NewOpc;
2223     switch (Inst.getOpcode()) {
2224     default: llvm_unreachable("Invalid opcode");
2225     case X86::VMOVAPDrr:  NewOpc = X86::VMOVAPDrr_REV;  break;
2226     case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
2227     case X86::VMOVAPSrr:  NewOpc = X86::VMOVAPSrr_REV;  break;
2228     case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
2229     case X86::VMOVDQArr:  NewOpc = X86::VMOVDQArr_REV;  break;
2230     case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
2231     case X86::VMOVDQUrr:  NewOpc = X86::VMOVDQUrr_REV;  break;
2232     case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
2233     case X86::VMOVUPDrr:  NewOpc = X86::VMOVUPDrr_REV;  break;
2234     case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
2235     case X86::VMOVUPSrr:  NewOpc = X86::VMOVUPSrr_REV;  break;
2236     case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
2237     }
2238     Inst.setOpcode(NewOpc);
2239     return true;
2240   }
2241   case X86::VMOVSDrr:
2242   case X86::VMOVSSrr: {
2243     if (X86II::isX86_64ExtendedReg(Inst.getOperand(0).getReg()) ||
2244         !X86II::isX86_64ExtendedReg(Inst.getOperand(2).getReg()))
2245       return false;
2246     unsigned NewOpc;
2247     switch (Inst.getOpcode()) {
2248     default: llvm_unreachable("Invalid opcode");
2249     case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV;   break;
2250     case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV;   break;
2251     }
2252     Inst.setOpcode(NewOpc);
2253     return true;
2254   }
2255   }
2256 }
2257
2258 static const char *getSubtargetFeatureName(unsigned Val);
2259
2260 void X86AsmParser::EmitInstruction(
2261     MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
2262     MCStreamer &Out) {
2263   Instrumentation->InstrumentInstruction(Inst, Operands, getContext(), MII,
2264                                          Out);
2265   Out.EmitInstruction(Inst, STI);
2266 }
2267
2268 bool X86AsmParser::
2269 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2270                         SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2271                         MCStreamer &Out, unsigned &ErrorInfo,
2272                         bool MatchingInlineAsm) {
2273   assert(!Operands.empty() && "Unexpect empty operand list!");
2274   X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2275   assert(Op->isToken() && "Leading operand should always be a mnemonic!");
2276   ArrayRef<SMRange> EmptyRanges = None;
2277
2278   // First, handle aliases that expand to multiple instructions.
2279   // FIXME: This should be replaced with a real .td file alias mechanism.
2280   // Also, MatchInstructionImpl should actually *do* the EmitInstruction
2281   // call.
2282   if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
2283       Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
2284       Op->getToken() == "finit" || Op->getToken() == "fsave" ||
2285       Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
2286     MCInst Inst;
2287     Inst.setOpcode(X86::WAIT);
2288     Inst.setLoc(IDLoc);
2289     if (!MatchingInlineAsm)
2290       EmitInstruction(Inst, Operands, Out);
2291
2292     const char *Repl =
2293       StringSwitch<const char*>(Op->getToken())
2294         .Case("finit",  "fninit")
2295         .Case("fsave",  "fnsave")
2296         .Case("fstcw",  "fnstcw")
2297         .Case("fstcww",  "fnstcw")
2298         .Case("fstenv", "fnstenv")
2299         .Case("fstsw",  "fnstsw")
2300         .Case("fstsww", "fnstsw")
2301         .Case("fclex",  "fnclex")
2302         .Default(0);
2303     assert(Repl && "Unknown wait-prefixed instruction");
2304     delete Operands[0];
2305     Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
2306   }
2307
2308   bool WasOriginallyInvalidOperand = false;
2309   MCInst Inst;
2310
2311   // First, try a direct match.
2312   switch (MatchInstructionImpl(Operands, Inst,
2313                                ErrorInfo, MatchingInlineAsm,
2314                                isParsingIntelSyntax())) {
2315   default: break;
2316   case Match_Success:
2317     // Some instructions need post-processing to, for example, tweak which
2318     // encoding is selected. Loop on it while changes happen so the
2319     // individual transformations can chain off each other.
2320     if (!MatchingInlineAsm)
2321       while (processInstruction(Inst, Operands))
2322         ;
2323
2324     Inst.setLoc(IDLoc);
2325     if (!MatchingInlineAsm)
2326       EmitInstruction(Inst, Operands, Out);
2327     Opcode = Inst.getOpcode();
2328     return false;
2329   case Match_MissingFeature: {
2330     assert(ErrorInfo && "Unknown missing feature!");
2331     // Special case the error message for the very common case where only
2332     // a single subtarget feature is missing.
2333     std::string Msg = "instruction requires:";
2334     unsigned Mask = 1;
2335     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2336       if (ErrorInfo & Mask) {
2337         Msg += " ";
2338         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2339       }
2340       Mask <<= 1;
2341     }
2342     return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2343   }
2344   case Match_InvalidOperand:
2345     WasOriginallyInvalidOperand = true;
2346     break;
2347   case Match_MnemonicFail:
2348     break;
2349   }
2350
2351   // FIXME: Ideally, we would only attempt suffix matches for things which are
2352   // valid prefixes, and we could just infer the right unambiguous
2353   // type. However, that requires substantially more matcher support than the
2354   // following hack.
2355
2356   // Change the operand to point to a temporary token.
2357   StringRef Base = Op->getToken();
2358   SmallString<16> Tmp;
2359   Tmp += Base;
2360   Tmp += ' ';
2361   Op->setTokenValue(Tmp.str());
2362
2363   // If this instruction starts with an 'f', then it is a floating point stack
2364   // instruction.  These come in up to three forms for 32-bit, 64-bit, and
2365   // 80-bit floating point, which use the suffixes s,l,t respectively.
2366   //
2367   // Otherwise, we assume that this may be an integer instruction, which comes
2368   // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2369   const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
2370
2371   // Check for the various suffix matches.
2372   Tmp[Base.size()] = Suffixes[0];
2373   unsigned ErrorInfoIgnore;
2374   unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
2375   unsigned Match1, Match2, Match3, Match4;
2376
2377   Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2378                                 MatchingInlineAsm, isParsingIntelSyntax());
2379   // If this returned as a missing feature failure, remember that.
2380   if (Match1 == Match_MissingFeature)
2381     ErrorInfoMissingFeature = ErrorInfoIgnore;
2382   Tmp[Base.size()] = Suffixes[1];
2383   Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2384                                 MatchingInlineAsm, isParsingIntelSyntax());
2385   // If this returned as a missing feature failure, remember that.
2386   if (Match2 == Match_MissingFeature)
2387     ErrorInfoMissingFeature = ErrorInfoIgnore;
2388   Tmp[Base.size()] = Suffixes[2];
2389   Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2390                                 MatchingInlineAsm, isParsingIntelSyntax());
2391   // If this returned as a missing feature failure, remember that.
2392   if (Match3 == Match_MissingFeature)
2393     ErrorInfoMissingFeature = ErrorInfoIgnore;
2394   Tmp[Base.size()] = Suffixes[3];
2395   Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2396                                 MatchingInlineAsm, isParsingIntelSyntax());
2397   // If this returned as a missing feature failure, remember that.
2398   if (Match4 == Match_MissingFeature)
2399     ErrorInfoMissingFeature = ErrorInfoIgnore;
2400
2401   // Restore the old token.
2402   Op->setTokenValue(Base);
2403
2404   // If exactly one matched, then we treat that as a successful match (and the
2405   // instruction will already have been filled in correctly, since the failing
2406   // matches won't have modified it).
2407   unsigned NumSuccessfulMatches =
2408     (Match1 == Match_Success) + (Match2 == Match_Success) +
2409     (Match3 == Match_Success) + (Match4 == Match_Success);
2410   if (NumSuccessfulMatches == 1) {
2411     Inst.setLoc(IDLoc);
2412     if (!MatchingInlineAsm)
2413       EmitInstruction(Inst, Operands, Out);
2414     Opcode = Inst.getOpcode();
2415     return false;
2416   }
2417
2418   // Otherwise, the match failed, try to produce a decent error message.
2419
2420   // If we had multiple suffix matches, then identify this as an ambiguous
2421   // match.
2422   if (NumSuccessfulMatches > 1) {
2423     char MatchChars[4];
2424     unsigned NumMatches = 0;
2425     if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2426     if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2427     if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2428     if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
2429
2430     SmallString<126> Msg;
2431     raw_svector_ostream OS(Msg);
2432     OS << "ambiguous instructions require an explicit suffix (could be ";
2433     for (unsigned i = 0; i != NumMatches; ++i) {
2434       if (i != 0)
2435         OS << ", ";
2436       if (i + 1 == NumMatches)
2437         OS << "or ";
2438       OS << "'" << Base << MatchChars[i] << "'";
2439     }
2440     OS << ")";
2441     Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
2442     return true;
2443   }
2444
2445   // Okay, we know that none of the variants matched successfully.
2446
2447   // If all of the instructions reported an invalid mnemonic, then the original
2448   // mnemonic was invalid.
2449   if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2450       (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
2451     if (!WasOriginallyInvalidOperand) {
2452       ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
2453         Op->getLocRange();
2454       return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
2455                    Ranges, MatchingInlineAsm);
2456     }
2457
2458     // Recover location info for the operand if we know which was the problem.
2459     if (ErrorInfo != ~0U) {
2460       if (ErrorInfo >= Operands.size())
2461         return Error(IDLoc, "too few operands for instruction",
2462                      EmptyRanges, MatchingInlineAsm);
2463
2464       X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
2465       if (Operand->getStartLoc().isValid()) {
2466         SMRange OperandRange = Operand->getLocRange();
2467         return Error(Operand->getStartLoc(), "invalid operand for instruction",
2468                      OperandRange, MatchingInlineAsm);
2469       }
2470     }
2471
2472     return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
2473                  MatchingInlineAsm);
2474   }
2475
2476   // If one instruction matched with a missing feature, report this as a
2477   // missing feature.
2478   if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2479       (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
2480     std::string Msg = "instruction requires:";
2481     unsigned Mask = 1;
2482     for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2483       if (ErrorInfoMissingFeature & Mask) {
2484         Msg += " ";
2485         Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2486       }
2487       Mask <<= 1;
2488     }
2489     return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2490   }
2491
2492   // If one instruction matched with an invalid operand, report this as an
2493   // operand failure.
2494   if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2495       (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
2496     Error(IDLoc, "invalid operand for instruction", EmptyRanges,
2497           MatchingInlineAsm);
2498     return true;
2499   }
2500
2501   // If all of these were an outright failure, report it in a useless way.
2502   Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
2503         EmptyRanges, MatchingInlineAsm);
2504   return true;
2505 }
2506
2507
2508 bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
2509   StringRef IDVal = DirectiveID.getIdentifier();
2510   if (IDVal == ".word")
2511     return ParseDirectiveWord(2, DirectiveID.getLoc());
2512   else if (IDVal.startswith(".code"))
2513     return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
2514   else if (IDVal.startswith(".att_syntax")) {
2515     getParser().setAssemblerDialect(0);
2516     return false;
2517   } else if (IDVal.startswith(".intel_syntax")) {
2518     getParser().setAssemblerDialect(1);
2519     if (getLexer().isNot(AsmToken::EndOfStatement)) {
2520       // FIXME: Handle noprefix
2521       if (Parser.getTok().getString() == "noprefix")
2522         Parser.Lex();
2523     }
2524     return false;
2525   }
2526   return true;
2527 }
2528
2529 /// ParseDirectiveWord
2530 ///  ::= .word [ expression (, expression)* ]
2531 bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2532   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2533     for (;;) {
2534       const MCExpr *Value;
2535       if (getParser().parseExpression(Value))
2536         return false;
2537
2538       getParser().getStreamer().EmitValue(Value, Size);
2539
2540       if (getLexer().is(AsmToken::EndOfStatement))
2541         break;
2542
2543       // FIXME: Improve diagnostic.
2544       if (getLexer().isNot(AsmToken::Comma)) {
2545         Error(L, "unexpected token in directive");
2546         return false;
2547       }
2548       Parser.Lex();
2549     }
2550   }
2551
2552   Parser.Lex();
2553   return false;
2554 }
2555
2556 /// ParseDirectiveCode
2557 ///  ::= .code16 | .code32 | .code64
2558 bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
2559   if (IDVal == ".code16") {
2560     Parser.Lex();
2561     if (!is16BitMode()) {
2562       SwitchMode(X86::Mode16Bit);
2563       getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2564     }
2565   } else if (IDVal == ".code32") {
2566     Parser.Lex();
2567     if (!is32BitMode()) {
2568       SwitchMode(X86::Mode32Bit);
2569       getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2570     }
2571   } else if (IDVal == ".code64") {
2572     Parser.Lex();
2573     if (!is64BitMode()) {
2574       SwitchMode(X86::Mode64Bit);
2575       getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2576     }
2577   } else {
2578     Error(L, "unknown directive " + IDVal);
2579     return false;
2580   }
2581
2582   return false;
2583 }
2584
2585 // Force static initialization.
2586 extern "C" void LLVMInitializeX86AsmParser() {
2587   RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2588   RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
2589 }
2590
2591 #define GET_REGISTER_MATCHER
2592 #define GET_MATCHER_IMPLEMENTATION
2593 #define GET_SUBTARGET_FEATURE_NAME
2594 #include "X86GenAsmMatcher.inc"