4c51ed674e3bc8536e50e0da6a3ad60b6d4eff5c
[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 "llvm/Target/TargetAsmParser.h"
11 #include "X86.h"
12 #include "X86Subtarget.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCAsmParser.h"
22 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetRegistry.h"
26 #include "llvm/Target/TargetAsmParser.h"
27 using namespace llvm;
28
29 namespace {
30 struct X86Operand;
31
32 class X86ATTAsmParser : public TargetAsmParser {
33   MCAsmParser &Parser;
34   TargetMachine &TM;
35
36 protected:
37   unsigned Is64Bit : 1;
38
39 private:
40   MCAsmParser &getParser() const { return Parser; }
41
42   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
44   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
46   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
47
48   X86Operand *ParseOperand();
49   X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
50
51   bool ParseDirectiveWord(unsigned Size, SMLoc L);
52
53   bool MatchInstruction(SMLoc IDLoc,
54                         const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
55                         MCInst &Inst);
56
57   /// @name Auto-generated Matcher Functions
58   /// {
59   
60 #define GET_ASSEMBLER_HEADER
61 #include "X86GenAsmMatcher.inc"
62   
63   /// }
64
65 public:
66   X86ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM)
67     : TargetAsmParser(T), Parser(_Parser), TM(TM) {
68
69     // Initialize the set of available features.
70     setAvailableFeatures(ComputeAvailableFeatures(
71                            &TM.getSubtarget<X86Subtarget>()));
72   }
73
74   virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
75                                 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
76
77   virtual bool ParseDirective(AsmToken DirectiveID);
78 };
79
80 class X86_32ATTAsmParser : public X86ATTAsmParser {
81 public:
82   X86_32ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM)
83     : X86ATTAsmParser(T, _Parser, TM) {
84     Is64Bit = false;
85   }
86 };
87
88 class X86_64ATTAsmParser : public X86ATTAsmParser {
89 public:
90   X86_64ATTAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &TM)
91     : X86ATTAsmParser(T, _Parser, TM) {
92     Is64Bit = true;
93   }
94 };
95
96 } // end anonymous namespace
97
98 /// @name Auto-generated Match Functions
99 /// {
100
101 static unsigned MatchRegisterName(StringRef Name);
102
103 /// }
104
105 namespace {
106
107 /// X86Operand - Instances of this class represent a parsed X86 machine
108 /// instruction.
109 struct X86Operand : public MCParsedAsmOperand {
110   enum KindTy {
111     Token,
112     Register,
113     Immediate,
114     Memory
115   } Kind;
116
117   SMLoc StartLoc, EndLoc;
118
119   union {
120     struct {
121       const char *Data;
122       unsigned Length;
123     } Tok;
124
125     struct {
126       unsigned RegNo;
127     } Reg;
128
129     struct {
130       const MCExpr *Val;
131     } Imm;
132
133     struct {
134       unsigned SegReg;
135       const MCExpr *Disp;
136       unsigned BaseReg;
137       unsigned IndexReg;
138       unsigned Scale;
139     } Mem;
140   };
141
142   X86Operand(KindTy K, SMLoc Start, SMLoc End)
143     : Kind(K), StartLoc(Start), EndLoc(End) {}
144
145   /// getStartLoc - Get the location of the first token of this operand.
146   SMLoc getStartLoc() const { return StartLoc; }
147   /// getEndLoc - Get the location of the last token of this operand.
148   SMLoc getEndLoc() const { return EndLoc; }
149
150   virtual void dump(raw_ostream &OS) const {}
151
152   StringRef getToken() const {
153     assert(Kind == Token && "Invalid access!");
154     return StringRef(Tok.Data, Tok.Length);
155   }
156   void setTokenValue(StringRef Value) {
157     assert(Kind == Token && "Invalid access!");
158     Tok.Data = Value.data();
159     Tok.Length = Value.size();
160   }
161
162   unsigned getReg() const {
163     assert(Kind == Register && "Invalid access!");
164     return Reg.RegNo;
165   }
166
167   const MCExpr *getImm() const {
168     assert(Kind == Immediate && "Invalid access!");
169     return Imm.Val;
170   }
171
172   const MCExpr *getMemDisp() const {
173     assert(Kind == Memory && "Invalid access!");
174     return Mem.Disp;
175   }
176   unsigned getMemSegReg() const {
177     assert(Kind == Memory && "Invalid access!");
178     return Mem.SegReg;
179   }
180   unsigned getMemBaseReg() const {
181     assert(Kind == Memory && "Invalid access!");
182     return Mem.BaseReg;
183   }
184   unsigned getMemIndexReg() const {
185     assert(Kind == Memory && "Invalid access!");
186     return Mem.IndexReg;
187   }
188   unsigned getMemScale() const {
189     assert(Kind == Memory && "Invalid access!");
190     return Mem.Scale;
191   }
192
193   bool isToken() const {return Kind == Token; }
194
195   bool isImm() const { return Kind == Immediate; }
196
197   bool isImmSExti16i8() const {
198     if (!isImm())
199       return false;
200
201     // If this isn't a constant expr, just assume it fits and let relaxation
202     // handle it.
203     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
204     if (!CE)
205       return true;
206
207     // Otherwise, check the value is in a range that makes sense for this
208     // extension.
209     uint64_t Value = CE->getValue();
210     return ((                                  Value <= 0x000000000000007FULL)||
211             (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
212             (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
213   }
214   bool isImmSExti32i8() const {
215     if (!isImm())
216       return false;
217
218     // If this isn't a constant expr, just assume it fits and let relaxation
219     // handle it.
220     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
221     if (!CE)
222       return true;
223
224     // Otherwise, check the value is in a range that makes sense for this
225     // extension.
226     uint64_t Value = CE->getValue();
227     return ((                                  Value <= 0x000000000000007FULL)||
228             (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
229             (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
230   }
231   bool isImmSExti64i8() const {
232     if (!isImm())
233       return false;
234
235     // If this isn't a constant expr, just assume it fits and let relaxation
236     // handle it.
237     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
238     if (!CE)
239       return true;
240
241     // Otherwise, check the value is in a range that makes sense for this
242     // extension.
243     uint64_t Value = CE->getValue();
244     return ((                                  Value <= 0x000000000000007FULL)||
245             (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
246   }
247   bool isImmSExti64i32() const {
248     if (!isImm())
249       return false;
250
251     // If this isn't a constant expr, just assume it fits and let relaxation
252     // handle it.
253     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
254     if (!CE)
255       return true;
256
257     // Otherwise, check the value is in a range that makes sense for this
258     // extension.
259     uint64_t Value = CE->getValue();
260     return ((                                  Value <= 0x000000007FFFFFFFULL)||
261             (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
262   }
263
264   bool isMem() const { return Kind == Memory; }
265
266   bool isAbsMem() const {
267     return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
268       !getMemIndexReg() && getMemScale() == 1;
269   }
270
271   bool isReg() const { return Kind == Register; }
272
273   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
274     // Add as immediates when possible.
275     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
276       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
277     else
278       Inst.addOperand(MCOperand::CreateExpr(Expr));
279   }
280
281   void addRegOperands(MCInst &Inst, unsigned N) const {
282     assert(N == 1 && "Invalid number of operands!");
283     Inst.addOperand(MCOperand::CreateReg(getReg()));
284   }
285
286   void addImmOperands(MCInst &Inst, unsigned N) const {
287     assert(N == 1 && "Invalid number of operands!");
288     addExpr(Inst, getImm());
289   }
290
291   void addMemOperands(MCInst &Inst, unsigned N) const {
292     assert((N == 5) && "Invalid number of operands!");
293     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
294     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
295     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
296     addExpr(Inst, getMemDisp());
297     Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
298   }
299
300   void addAbsMemOperands(MCInst &Inst, unsigned N) const {
301     assert((N == 1) && "Invalid number of operands!");
302     Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
303   }
304
305   static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
306     X86Operand *Res = new X86Operand(Token, Loc, Loc);
307     Res->Tok.Data = Str.data();
308     Res->Tok.Length = Str.size();
309     return Res;
310   }
311
312   static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
313     X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
314     Res->Reg.RegNo = RegNo;
315     return Res;
316   }
317
318   static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
319     X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
320     Res->Imm.Val = Val;
321     return Res;
322   }
323
324   /// Create an absolute memory operand.
325   static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
326                                SMLoc EndLoc) {
327     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
328     Res->Mem.SegReg   = 0;
329     Res->Mem.Disp     = Disp;
330     Res->Mem.BaseReg  = 0;
331     Res->Mem.IndexReg = 0;
332     Res->Mem.Scale    = 1;
333     return Res;
334   }
335
336   /// Create a generalized memory operand.
337   static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
338                                unsigned BaseReg, unsigned IndexReg,
339                                unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
340     // We should never just have a displacement, that should be parsed as an
341     // absolute memory operand.
342     assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
343
344     // The scale should always be one of {1,2,4,8}.
345     assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
346            "Invalid scale!");
347     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
348     Res->Mem.SegReg   = SegReg;
349     Res->Mem.Disp     = Disp;
350     Res->Mem.BaseReg  = BaseReg;
351     Res->Mem.IndexReg = IndexReg;
352     Res->Mem.Scale    = Scale;
353     return Res;
354   }
355 };
356
357 } // end anonymous namespace.
358
359
360 bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
361                                     SMLoc &StartLoc, SMLoc &EndLoc) {
362   RegNo = 0;
363   const AsmToken &TokPercent = Parser.getTok();
364   assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
365   StartLoc = TokPercent.getLoc();
366   Parser.Lex(); // Eat percent token.
367
368   const AsmToken &Tok = Parser.getTok();
369   if (Tok.isNot(AsmToken::Identifier))
370     return Error(Tok.getLoc(), "invalid register name");
371
372   // FIXME: Validate register for the current architecture; we have to do
373   // validation later, so maybe there is no need for this here.
374   RegNo = MatchRegisterName(Tok.getString());
375
376   // FIXME: This should be done using Requires<In32BitMode> and
377   // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
378   // can be also checked.
379   if (RegNo == X86::RIZ && !Is64Bit)
380     return Error(Tok.getLoc(), "riz register in 64-bit mode only");
381
382   // Parse %st(1) and "%st" as "%st(0)"
383   if (RegNo == 0 && Tok.getString() == "st") {
384     RegNo = X86::ST0;
385     EndLoc = Tok.getLoc();
386     Parser.Lex(); // Eat 'st'
387
388     // Check to see if we have '(4)' after %st.
389     if (getLexer().isNot(AsmToken::LParen))
390       return false;
391     // Lex the paren.
392     getParser().Lex();
393
394     const AsmToken &IntTok = Parser.getTok();
395     if (IntTok.isNot(AsmToken::Integer))
396       return Error(IntTok.getLoc(), "expected stack index");
397     switch (IntTok.getIntVal()) {
398     case 0: RegNo = X86::ST0; break;
399     case 1: RegNo = X86::ST1; break;
400     case 2: RegNo = X86::ST2; break;
401     case 3: RegNo = X86::ST3; break;
402     case 4: RegNo = X86::ST4; break;
403     case 5: RegNo = X86::ST5; break;
404     case 6: RegNo = X86::ST6; break;
405     case 7: RegNo = X86::ST7; break;
406     default: return Error(IntTok.getLoc(), "invalid stack index");
407     }
408
409     if (getParser().Lex().isNot(AsmToken::RParen))
410       return Error(Parser.getTok().getLoc(), "expected ')'");
411
412     EndLoc = Tok.getLoc();
413     Parser.Lex(); // Eat ')'
414     return false;
415   }
416
417   // If this is "db[0-7]", match it as an alias
418   // for dr[0-7].
419   if (RegNo == 0 && Tok.getString().size() == 3 &&
420       Tok.getString().startswith("db")) {
421     switch (Tok.getString()[2]) {
422     case '0': RegNo = X86::DR0; break;
423     case '1': RegNo = X86::DR1; break;
424     case '2': RegNo = X86::DR2; break;
425     case '3': RegNo = X86::DR3; break;
426     case '4': RegNo = X86::DR4; break;
427     case '5': RegNo = X86::DR5; break;
428     case '6': RegNo = X86::DR6; break;
429     case '7': RegNo = X86::DR7; break;
430     }
431
432     if (RegNo != 0) {
433       EndLoc = Tok.getLoc();
434       Parser.Lex(); // Eat it.
435       return false;
436     }
437   }
438
439   if (RegNo == 0)
440     return Error(Tok.getLoc(), "invalid register name");
441
442   EndLoc = Tok.getLoc();
443   Parser.Lex(); // Eat identifier token.
444   return false;
445 }
446
447 X86Operand *X86ATTAsmParser::ParseOperand() {
448   switch (getLexer().getKind()) {
449   default:
450     // Parse a memory operand with no segment register.
451     return ParseMemOperand(0, Parser.getTok().getLoc());
452   case AsmToken::Percent: {
453     // Read the register.
454     unsigned RegNo;
455     SMLoc Start, End;
456     if (ParseRegister(RegNo, Start, End)) return 0;
457     if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
458       Error(Start, "eiz and riz can only be used as index registers");
459       return 0;
460     }
461
462     // If this is a segment register followed by a ':', then this is the start
463     // of a memory reference, otherwise this is a normal register reference.
464     if (getLexer().isNot(AsmToken::Colon))
465       return X86Operand::CreateReg(RegNo, Start, End);
466
467
468     getParser().Lex(); // Eat the colon.
469     return ParseMemOperand(RegNo, Start);
470   }
471   case AsmToken::Dollar: {
472     // $42 -> immediate.
473     SMLoc Start = Parser.getTok().getLoc(), End;
474     Parser.Lex();
475     const MCExpr *Val;
476     if (getParser().ParseExpression(Val, End))
477       return 0;
478     return X86Operand::CreateImm(Val, Start, End);
479   }
480   }
481 }
482
483 /// ParseMemOperand: segment: disp(basereg, indexreg, scale).  The '%ds:' prefix
484 /// has already been parsed if present.
485 X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
486
487   // We have to disambiguate a parenthesized expression "(4+5)" from the start
488   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
489   // only way to do this without lookahead is to eat the '(' and see what is
490   // after it.
491   const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
492   if (getLexer().isNot(AsmToken::LParen)) {
493     SMLoc ExprEnd;
494     if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
495
496     // After parsing the base expression we could either have a parenthesized
497     // memory address or not.  If not, return now.  If so, eat the (.
498     if (getLexer().isNot(AsmToken::LParen)) {
499       // Unless we have a segment register, treat this as an immediate.
500       if (SegReg == 0)
501         return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
502       return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
503     }
504
505     // Eat the '('.
506     Parser.Lex();
507   } else {
508     // Okay, we have a '('.  We don't know if this is an expression or not, but
509     // so we have to eat the ( to see beyond it.
510     SMLoc LParenLoc = Parser.getTok().getLoc();
511     Parser.Lex(); // Eat the '('.
512
513     if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
514       // Nothing to do here, fall into the code below with the '(' part of the
515       // memory operand consumed.
516     } else {
517       SMLoc ExprEnd;
518
519       // It must be an parenthesized expression, parse it now.
520       if (getParser().ParseParenExpression(Disp, ExprEnd))
521         return 0;
522
523       // After parsing the base expression we could either have a parenthesized
524       // memory address or not.  If not, return now.  If so, eat the (.
525       if (getLexer().isNot(AsmToken::LParen)) {
526         // Unless we have a segment register, treat this as an immediate.
527         if (SegReg == 0)
528           return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
529         return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
530       }
531
532       // Eat the '('.
533       Parser.Lex();
534     }
535   }
536
537   // If we reached here, then we just ate the ( of the memory operand.  Process
538   // the rest of the memory operand.
539   unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
540
541   if (getLexer().is(AsmToken::Percent)) {
542     SMLoc L;
543     if (ParseRegister(BaseReg, L, L)) return 0;
544     if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
545       Error(L, "eiz and riz can only be used as index registers");
546       return 0;
547     }
548   }
549
550   if (getLexer().is(AsmToken::Comma)) {
551     Parser.Lex(); // Eat the comma.
552
553     // Following the comma we should have either an index register, or a scale
554     // value. We don't support the later form, but we want to parse it
555     // correctly.
556     //
557     // Not that even though it would be completely consistent to support syntax
558     // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
559     if (getLexer().is(AsmToken::Percent)) {
560       SMLoc L;
561       if (ParseRegister(IndexReg, L, L)) return 0;
562
563       if (getLexer().isNot(AsmToken::RParen)) {
564         // Parse the scale amount:
565         //  ::= ',' [scale-expression]
566         if (getLexer().isNot(AsmToken::Comma)) {
567           Error(Parser.getTok().getLoc(),
568                 "expected comma in scale expression");
569           return 0;
570         }
571         Parser.Lex(); // Eat the comma.
572
573         if (getLexer().isNot(AsmToken::RParen)) {
574           SMLoc Loc = Parser.getTok().getLoc();
575
576           int64_t ScaleVal;
577           if (getParser().ParseAbsoluteExpression(ScaleVal))
578             return 0;
579
580           // Validate the scale amount.
581           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
582             Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
583             return 0;
584           }
585           Scale = (unsigned)ScaleVal;
586         }
587       }
588     } else if (getLexer().isNot(AsmToken::RParen)) {
589       // A scale amount without an index is ignored.
590       // index.
591       SMLoc Loc = Parser.getTok().getLoc();
592
593       int64_t Value;
594       if (getParser().ParseAbsoluteExpression(Value))
595         return 0;
596
597       if (Value != 1)
598         Warning(Loc, "scale factor without index register is ignored");
599       Scale = 1;
600     }
601   }
602
603   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
604   if (getLexer().isNot(AsmToken::RParen)) {
605     Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
606     return 0;
607   }
608   SMLoc MemEnd = Parser.getTok().getLoc();
609   Parser.Lex(); // Eat the ')'.
610
611   return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
612                                MemStart, MemEnd);
613 }
614
615 bool X86ATTAsmParser::
616 ParseInstruction(StringRef Name, SMLoc NameLoc,
617                  SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
618   // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to
619   // represent alternative syntaxes in the .td file, without requiring
620   // instruction duplication.
621   StringRef PatchedName = StringSwitch<StringRef>(Name)
622     .Case("sal", "shl")
623     .Case("salb", "shlb")
624     .Case("sall", "shll")
625     .Case("salq", "shlq")
626     .Case("salw", "shlw")
627     .Case("repe", "rep")
628     .Case("repz", "rep")
629     .Case("repnz", "repne")
630     .Case("iret", "iretl")
631     .Case("sysret", "sysretl")
632     .Case("push", Is64Bit ? "pushq" : "pushl")
633     .Case("pop", Is64Bit ? "popq" : "popl")
634     .Case("pushf", Is64Bit ? "pushfq" : "pushfl")
635     .Case("popf",  Is64Bit ? "popfq"  : "popfl")
636     .Case("pushfd", "pushfl")
637     .Case("popfd",  "popfl")
638     .Case("retl", Is64Bit ? "retl" : "ret")
639     .Case("retq", Is64Bit ? "ret" : "retq")
640     .Case("setz", "sete")  .Case("setnz", "setne")
641     .Case("setc", "setb")  .Case("setna", "setbe")
642     .Case("setnae", "setb").Case("setnb", "setae")
643     .Case("setnbe", "seta").Case("setnc", "setae")
644     .Case("setng", "setle").Case("setnge", "setl")
645     .Case("setnl", "setge").Case("setnle", "setg")
646     .Case("setpe", "setp") .Case("setpo", "setnp")
647     .Case("jz", "je")  .Case("jnz", "jne")
648     .Case("jc", "jb")  .Case("jna", "jbe")
649     .Case("jnae", "jb").Case("jnb", "jae")
650     .Case("jnbe", "ja").Case("jnc", "jae")
651     .Case("jng", "jle").Case("jnge", "jl")
652     .Case("jnl", "jge").Case("jnle", "jg")
653     .Case("jpe", "jp") .Case("jpo", "jnp")
654     // Condition code aliases for 16-bit, 32-bit, 64-bit and unspec operands.
655     .Case("cmovcw",  "cmovbw") .Case("cmovcl",  "cmovbl")
656     .Case("cmovcq",  "cmovbq") .Case("cmovc",   "cmovb")
657     .Case("cmovnaew","cmovbw") .Case("cmovnael","cmovbl")
658     .Case("cmovnaeq","cmovbq") .Case("cmovnae", "cmovb")
659     .Case("cmovnaw", "cmovbew").Case("cmovnal", "cmovbel")
660     .Case("cmovnaq", "cmovbeq").Case("cmovna",  "cmovbe")
661     .Case("cmovnbw", "cmovaew").Case("cmovnbl", "cmovael")
662     .Case("cmovnbq", "cmovaeq").Case("cmovnb",  "cmovae")
663     .Case("cmovnbew","cmovaw") .Case("cmovnbel","cmoval")
664     .Case("cmovnbeq","cmovaq") .Case("cmovnbe", "cmova")
665     .Case("cmovncw", "cmovaew").Case("cmovncl", "cmovael")
666     .Case("cmovncq", "cmovaeq").Case("cmovnc",  "cmovae")
667     .Case("cmovngw", "cmovlew").Case("cmovngl", "cmovlel")
668     .Case("cmovngq", "cmovleq").Case("cmovng",  "cmovle")
669     .Case("cmovnw",  "cmovgew").Case("cmovnl",  "cmovgel")
670     .Case("cmovnq",  "cmovgeq").Case("cmovn",   "cmovge")
671     .Case("cmovngw", "cmovlew").Case("cmovngl", "cmovlel")
672     .Case("cmovngq", "cmovleq").Case("cmovng",  "cmovle")
673     .Case("cmovngew","cmovlw") .Case("cmovngel","cmovll")
674     .Case("cmovngeq","cmovlq") .Case("cmovnge", "cmovl")
675     .Case("cmovnlw", "cmovgew").Case("cmovnll", "cmovgel")
676     .Case("cmovnlq", "cmovgeq").Case("cmovnl",  "cmovge")
677     .Case("cmovnlew","cmovgw") .Case("cmovnlel","cmovgl")
678     .Case("cmovnleq","cmovgq") .Case("cmovnle", "cmovg")
679     .Case("cmovnzw", "cmovnew").Case("cmovnzl", "cmovnel")
680     .Case("cmovnzq", "cmovneq").Case("cmovnz",  "cmovne")
681     .Case("cmovzw",  "cmovew") .Case("cmovzl",  "cmovel")
682     .Case("cmovzq",  "cmoveq") .Case("cmovz",   "cmove")
683     .Case("fwait", "wait")
684     .Case("movzx", "movzb")  // FIXME: Not correct.
685     .Case("fildq", "fildll")
686     .Default(Name);
687
688   // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
689   const MCExpr *ExtraImmOp = 0;
690   if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
691       (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
692        PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
693     bool IsVCMP = PatchedName.startswith("vcmp");
694     unsigned SSECCIdx = IsVCMP ? 4 : 3;
695     unsigned SSEComparisonCode = StringSwitch<unsigned>(
696       PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
697       .Case("eq",          0)
698       .Case("lt",          1)
699       .Case("le",          2)
700       .Case("unord",       3)
701       .Case("neq",         4)
702       .Case("nlt",         5)
703       .Case("nle",         6)
704       .Case("ord",         7)
705       .Case("eq_uq",       8)
706       .Case("nge",         9)
707       .Case("ngt",      0x0A)
708       .Case("false",    0x0B)
709       .Case("neq_oq",   0x0C)
710       .Case("ge",       0x0D)
711       .Case("gt",       0x0E)
712       .Case("true",     0x0F)
713       .Case("eq_os",    0x10)
714       .Case("lt_oq",    0x11)
715       .Case("le_oq",    0x12)
716       .Case("unord_s",  0x13)
717       .Case("neq_us",   0x14)
718       .Case("nlt_uq",   0x15)
719       .Case("nle_uq",   0x16)
720       .Case("ord_s",    0x17)
721       .Case("eq_us",    0x18)
722       .Case("nge_uq",   0x19)
723       .Case("ngt_uq",   0x1A)
724       .Case("false_os", 0x1B)
725       .Case("neq_os",   0x1C)
726       .Case("ge_oq",    0x1D)
727       .Case("gt_oq",    0x1E)
728       .Case("true_us",  0x1F)
729       .Default(~0U);
730     if (SSEComparisonCode != ~0U) {
731       ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
732                                           getParser().getContext());
733       if (PatchedName.endswith("ss")) {
734         PatchedName = IsVCMP ? "vcmpss" : "cmpss";
735       } else if (PatchedName.endswith("sd")) {
736         PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
737       } else if (PatchedName.endswith("ps")) {
738         PatchedName = IsVCMP ? "vcmpps" : "cmpps";
739       } else {
740         assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
741         PatchedName = IsVCMP ? "vcmppd" : "cmppd";
742       }
743     }
744   }
745
746   // FIXME: Hack to recognize vpclmul<src1_quadword, src2_quadword>dq
747   if (PatchedName.startswith("vpclmul")) {
748     unsigned CLMULQuadWordSelect = StringSwitch<unsigned>(
749       PatchedName.slice(7, PatchedName.size() - 2))
750       .Case("lqlq", 0x00) // src1[63:0],   src2[63:0]
751       .Case("hqlq", 0x01) // src1[127:64], src2[63:0]
752       .Case("lqhq", 0x10) // src1[63:0],   src2[127:64]
753       .Case("hqhq", 0x11) // src1[127:64], src2[127:64]
754       .Default(~0U);
755     if (CLMULQuadWordSelect != ~0U) {
756       ExtraImmOp = MCConstantExpr::Create(CLMULQuadWordSelect,
757                                           getParser().getContext());
758       assert(PatchedName.endswith("dq") && "Unexpected mnemonic!");
759       PatchedName = "vpclmulqdq";
760     }
761   }
762   
763   Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
764
765   if (ExtraImmOp)
766     Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
767   
768   
769   // Determine whether this is an instruction prefix.
770   bool isPrefix =
771     PatchedName == "lock" || PatchedName == "rep" || 
772     PatchedName == "repne";
773   
774   
775   // This does the actual operand parsing.  Don't parse any more if we have a
776   // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
777   // just want to parse the "lock" as the first instruction and the "incl" as
778   // the next one.
779   if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
780
781     // Parse '*' modifier.
782     if (getLexer().is(AsmToken::Star)) {
783       SMLoc Loc = Parser.getTok().getLoc();
784       Operands.push_back(X86Operand::CreateToken("*", Loc));
785       Parser.Lex(); // Eat the star.
786     }
787
788     // Read the first operand.
789     if (X86Operand *Op = ParseOperand())
790       Operands.push_back(Op);
791     else {
792       Parser.EatToEndOfStatement();
793       return true;
794     }
795
796     while (getLexer().is(AsmToken::Comma)) {
797       Parser.Lex();  // Eat the comma.
798
799       // Parse and remember the operand.
800       if (X86Operand *Op = ParseOperand())
801         Operands.push_back(Op);
802       else {
803         Parser.EatToEndOfStatement();
804         return true;
805       }
806     }
807     
808     if (getLexer().isNot(AsmToken::EndOfStatement)) {
809       Parser.EatToEndOfStatement();
810       return TokError("unexpected token in argument list");
811     }
812   }
813   
814   if (getLexer().is(AsmToken::EndOfStatement))
815     Parser.Lex(); // Consume the EndOfStatement
816
817   // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>.  Canonicalize to
818   // "shift <op>".
819   if ((Name.startswith("shr") || Name.startswith("sar") ||
820        Name.startswith("shl")) &&
821       Operands.size() == 3) {
822     X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
823     if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
824         cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
825       delete Operands[1];
826       Operands.erase(Operands.begin() + 1);
827     }
828   }
829   
830   // FIXME: Hack to handle recognize "rc[lr] <op>" -> "rcl $1, <op>".
831   if ((Name.startswith("rcl") || Name.startswith("rcr")) &&
832       Operands.size() == 2) {
833     const MCExpr *One = MCConstantExpr::Create(1, getParser().getContext());
834     Operands.push_back(X86Operand::CreateImm(One, NameLoc, NameLoc));
835     std::swap(Operands[1], Operands[2]);
836   }
837   
838   // FIXME: Hack to handle recognize "sh[lr]d op,op" -> "shld $1, op,op".
839   if ((Name.startswith("shld") || Name.startswith("shrd")) &&
840       Operands.size() == 3) {
841     const MCExpr *One = MCConstantExpr::Create(1, getParser().getContext());
842     Operands.insert(Operands.begin()+1,
843                     X86Operand::CreateImm(One, NameLoc, NameLoc));
844   }
845   
846
847   // FIXME: Hack to handle recognize "in[bwl] <op>".  Canonicalize it to
848   // "inb <op>, %al".
849   if ((Name == "inb" || Name == "inw" || Name == "inl") &&
850       Operands.size() == 2) {
851     unsigned Reg;
852     if (Name[2] == 'b')
853       Reg = MatchRegisterName("al");
854     else if (Name[2] == 'w')
855       Reg = MatchRegisterName("ax");
856     else
857       Reg = MatchRegisterName("eax");
858     SMLoc Loc = Operands.back()->getEndLoc();
859     Operands.push_back(X86Operand::CreateReg(Reg, Loc, Loc));
860   }
861   
862   // FIXME: Hack to handle recognize "out[bwl] <op>".  Canonicalize it to
863   // "outb %al, <op>".
864   if ((Name == "outb" || Name == "outw" || Name == "outl") &&
865       Operands.size() == 2) {
866     unsigned Reg;
867     if (Name[3] == 'b')
868       Reg = MatchRegisterName("al");
869     else if (Name[3] == 'w')
870       Reg = MatchRegisterName("ax");
871     else
872       Reg = MatchRegisterName("eax");
873     SMLoc Loc = Operands.back()->getEndLoc();
874     Operands.push_back(X86Operand::CreateReg(Reg, Loc, Loc));
875     std::swap(Operands[1], Operands[2]);
876   }
877   
878   // FIXME: Hack to handle "out[bwl]? %al, (%dx)" -> "outb %al, %dx".
879   if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
880       Operands.size() == 3) {
881     X86Operand &Op = *(X86Operand*)Operands.back();
882     if (Op.isMem() && Op.Mem.SegReg == 0 &&
883         isa<MCConstantExpr>(Op.Mem.Disp) &&
884         cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
885         Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
886       SMLoc Loc = Op.getEndLoc();
887       Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
888       delete &Op;
889     }
890   }
891   
892   // FIXME: Hack to handle "f{mul*,add*,sub*,div*} $op, st(0)" the same as
893   // "f{mul*,add*,sub*,div*} $op"
894   if ((Name.startswith("fmul") || Name.startswith("fadd") ||
895        Name.startswith("fsub") || Name.startswith("fdiv")) &&
896       Operands.size() == 3 &&
897       static_cast<X86Operand*>(Operands[2])->isReg() &&
898       static_cast<X86Operand*>(Operands[2])->getReg() == X86::ST0) {
899     delete Operands[2];
900     Operands.erase(Operands.begin() + 2);
901   }
902
903   // FIXME: Hack to handle "imul <imm>, B" which is an alias for "imul <imm>, B,
904   // B".
905   if (Name.startswith("imul") && Operands.size() == 3 &&
906       static_cast<X86Operand*>(Operands[1])->isImm() &&
907       static_cast<X86Operand*>(Operands.back())->isReg()) {
908     X86Operand *Op = static_cast<X86Operand*>(Operands.back());
909     Operands.push_back(X86Operand::CreateReg(Op->getReg(), Op->getStartLoc(),
910                                              Op->getEndLoc()));
911   }
912   
913   // 'sldt <mem>' can be encoded with either sldtw or sldtq with the same
914   // effect (both store to a 16-bit mem).  Force to sldtw to avoid ambiguity
915   // errors, since its encoding is the most compact.
916   if (Name == "sldt" && Operands.size() == 2 &&
917       static_cast<X86Operand*>(Operands[1])->isMem()) {
918     delete Operands[0];
919     Operands[0] = X86Operand::CreateToken("sldtw", NameLoc);
920   }
921   
922   // The assembler accepts "xchgX <reg>, <mem>" and "xchgX <mem>, <reg>" as
923   // synonyms.  Our tables only have the "<reg>, <mem>" form, so if we see the
924   // other operand order, swap them.
925   if (Name == "xchgb" || Name == "xchgw" || Name == "xchgl" || Name == "xchgq"||
926       Name == "xchg")
927     if (Operands.size() == 3 &&
928         static_cast<X86Operand*>(Operands[1])->isMem() &&
929         static_cast<X86Operand*>(Operands[2])->isReg()) {
930       std::swap(Operands[1], Operands[2]);
931     }
932
933   // The assembler accepts "testX <reg>, <mem>" and "testX <mem>, <reg>" as
934   // synonyms.  Our tables only have the "<mem>, <reg>" form, so if we see the
935   // other operand order, swap them.
936   if (Name == "testb" || Name == "testw" || Name == "testl" || Name == "testq"||
937       Name == "test")
938     if (Operands.size() == 3 &&
939         static_cast<X86Operand*>(Operands[1])->isReg() &&
940         static_cast<X86Operand*>(Operands[2])->isMem()) {
941       std::swap(Operands[1], Operands[2]);
942     }
943   
944   // The assembler accepts these instructions with no operand as a synonym for
945   // an instruction acting on st(1).  e.g. "fxch" -> "fxch %st(1)".
946   if ((Name == "fxch" || Name == "fucom" || Name == "fucomp" ||
947        Name == "faddp" || Name == "fsubp" || Name == "fsubrp" || 
948        Name == "fmulp" || Name == "fdivp" || Name == "fdivrp") &&
949       Operands.size() == 1) {
950     Operands.push_back(X86Operand::CreateReg(MatchRegisterName("st(1)"),
951                                              NameLoc, NameLoc));
952   }
953   
954   // The assembler accepts these instructions with two few operands as a synonym
955   // for taking %st(1),%st(0) or X, %st(0).
956   if ((Name == "fcomi" || Name == "fucomi") && Operands.size() < 3) {
957     if (Operands.size() == 1)
958       Operands.push_back(X86Operand::CreateReg(MatchRegisterName("st(1)"),
959                                                NameLoc, NameLoc));
960     Operands.push_back(X86Operand::CreateReg(MatchRegisterName("st(0)"),
961                                              NameLoc, NameLoc));
962   }
963   
964   // The assembler accepts various amounts of brokenness for fnstsw.
965   if (Name == "fnstsw") {
966     if (Operands.size() == 2 &&
967         static_cast<X86Operand*>(Operands[1])->isReg()) {
968       // "fnstsw al" and "fnstsw eax" -> "fnstw"
969       unsigned Reg = static_cast<X86Operand*>(Operands[1])->Reg.RegNo;
970       if (Reg == MatchRegisterName("eax") ||
971           Reg == MatchRegisterName("al")) {
972         delete Operands[1];
973         Operands.pop_back();
974       }
975     }
976
977     // "fnstw" -> "fnstw %ax"
978     if (Operands.size() == 1)
979       Operands.push_back(X86Operand::CreateReg(MatchRegisterName("ax"),
980                                                NameLoc, NameLoc));
981   }
982   
983   // jmp $42,$5 -> ljmp, similarly for call.
984   if ((Name.startswith("call") || Name.startswith("jmp")) &&
985       Operands.size() == 3 &&
986       static_cast<X86Operand*>(Operands[1])->isImm() &&
987       static_cast<X86Operand*>(Operands[2])->isImm()) {
988     const char *NewOpName = StringSwitch<const char *>(Name)
989       .Case("jmp", "ljmp")
990       .Case("jmpw", "ljmpw")
991       .Case("jmpl", "ljmpl")
992       .Case("jmpq", "ljmpq")
993       .Case("call", "lcall")
994       .Case("callw", "lcallw")
995       .Case("calll", "lcalll")
996       .Case("callq", "lcallq")
997     .Default(0);
998     if (NewOpName) {
999       delete Operands[0];
1000       Operands[0] = X86Operand::CreateToken(NewOpName, NameLoc);
1001       Name = NewOpName;
1002     }
1003   }
1004   
1005   // lcall  and ljmp  -> lcalll and ljmpl
1006   if ((Name == "lcall" || Name == "ljmp") && Operands.size() == 3) {
1007     delete Operands[0];
1008     Operands[0] = X86Operand::CreateToken(Name == "lcall" ? "lcalll" : "ljmpl",
1009                                           NameLoc);
1010   }
1011   
1012   // movsd -> movsl (when no operands are specified).
1013   if (Name == "movsd" && Operands.size() == 1) {
1014     delete Operands[0];
1015     Operands[0] = X86Operand::CreateToken("movsl", NameLoc);
1016   }
1017   
1018   return false;
1019 }
1020
1021 bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1022   StringRef IDVal = DirectiveID.getIdentifier();
1023   if (IDVal == ".word")
1024     return ParseDirectiveWord(2, DirectiveID.getLoc());
1025   return true;
1026 }
1027
1028 /// ParseDirectiveWord
1029 ///  ::= .word [ expression (, expression)* ]
1030 bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1031   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1032     for (;;) {
1033       const MCExpr *Value;
1034       if (getParser().ParseExpression(Value))
1035         return true;
1036
1037       getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1038
1039       if (getLexer().is(AsmToken::EndOfStatement))
1040         break;
1041
1042       // FIXME: Improve diagnostic.
1043       if (getLexer().isNot(AsmToken::Comma))
1044         return Error(L, "unexpected token in directive");
1045       Parser.Lex();
1046     }
1047   }
1048
1049   Parser.Lex();
1050   return false;
1051 }
1052
1053
1054 bool X86ATTAsmParser::
1055 MatchInstruction(SMLoc IDLoc,
1056                  const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1057                  MCInst &Inst) {
1058   assert(!Operands.empty() && "Unexpect empty operand list!");
1059
1060   bool WasOriginallyInvalidOperand = false;
1061   unsigned OrigErrorInfo;
1062   
1063   // First, try a direct match.
1064   switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
1065   case Match_Success:
1066     return false;
1067   case Match_MissingFeature:
1068     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1069     return true;
1070   case Match_InvalidOperand:
1071     WasOriginallyInvalidOperand = true;
1072     break;
1073   case Match_MnemonicFail:
1074     break;
1075   }
1076
1077   // FIXME: Ideally, we would only attempt suffix matches for things which are
1078   // valid prefixes, and we could just infer the right unambiguous
1079   // type. However, that requires substantially more matcher support than the
1080   // following hack.
1081
1082   X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1083   assert(Op->isToken() && "Leading operand should always be a mnemonic!");
1084   
1085   // Change the operand to point to a temporary token.
1086   StringRef Base = Op->getToken();
1087   SmallString<16> Tmp;
1088   Tmp += Base;
1089   Tmp += ' ';
1090   Op->setTokenValue(Tmp.str());
1091
1092   // Check for the various suffix matches.
1093   Tmp[Base.size()] = 'b';
1094   unsigned BErrorInfo, WErrorInfo, LErrorInfo, QErrorInfo;
1095   MatchResultTy MatchB = MatchInstructionImpl(Operands, Inst, BErrorInfo);
1096   Tmp[Base.size()] = 'w';
1097   MatchResultTy MatchW = MatchInstructionImpl(Operands, Inst, WErrorInfo);
1098   Tmp[Base.size()] = 'l';
1099   MatchResultTy MatchL = MatchInstructionImpl(Operands, Inst, LErrorInfo);
1100   Tmp[Base.size()] = 'q';
1101   MatchResultTy MatchQ = MatchInstructionImpl(Operands, Inst, QErrorInfo);
1102
1103   // Restore the old token.
1104   Op->setTokenValue(Base);
1105
1106   // If exactly one matched, then we treat that as a successful match (and the
1107   // instruction will already have been filled in correctly, since the failing
1108   // matches won't have modified it).
1109   unsigned NumSuccessfulMatches =
1110     (MatchB == Match_Success) + (MatchW == Match_Success) +
1111     (MatchL == Match_Success) + (MatchQ == Match_Success);
1112   if (NumSuccessfulMatches == 1)
1113     return false;
1114
1115   // Otherwise, the match failed, try to produce a decent error message.
1116
1117   // If we had multiple suffix matches, then identify this as an ambiguous
1118   // match.
1119   if (NumSuccessfulMatches > 1) {
1120     char MatchChars[4];
1121     unsigned NumMatches = 0;
1122     if (MatchB == Match_Success)
1123       MatchChars[NumMatches++] = 'b';
1124     if (MatchW == Match_Success)
1125       MatchChars[NumMatches++] = 'w';
1126     if (MatchL == Match_Success)
1127       MatchChars[NumMatches++] = 'l';
1128     if (MatchQ == Match_Success)
1129       MatchChars[NumMatches++] = 'q';
1130
1131     SmallString<126> Msg;
1132     raw_svector_ostream OS(Msg);
1133     OS << "ambiguous instructions require an explicit suffix (could be ";
1134     for (unsigned i = 0; i != NumMatches; ++i) {
1135       if (i != 0)
1136         OS << ", ";
1137       if (i + 1 == NumMatches)
1138         OS << "or ";
1139       OS << "'" << Base << MatchChars[i] << "'";
1140     }
1141     OS << ")";
1142     Error(IDLoc, OS.str());
1143     return true;
1144   }
1145   
1146   // Okay, we know that none of the variants matched successfully.
1147   
1148   // If all of the instructions reported an invalid mnemonic, then the original
1149   // mnemonic was invalid.
1150   if ((MatchB == Match_MnemonicFail) && (MatchW == Match_MnemonicFail) &&
1151       (MatchL == Match_MnemonicFail) && (MatchQ == Match_MnemonicFail)) {
1152     if (!WasOriginallyInvalidOperand) {
1153       Error(IDLoc, "invalid instruction mnemonic '" + Base + "'"); 
1154       return true;
1155     }
1156
1157     // Recover location info for the operand if we know which was the problem.
1158     SMLoc ErrorLoc = IDLoc;
1159     if (OrigErrorInfo != ~0U) {
1160       if (OrigErrorInfo >= Operands.size())
1161         return Error(IDLoc, "too few operands for instruction");
1162       
1163       ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1164       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1165     }
1166
1167     return Error(ErrorLoc, "invalid operand for instruction");
1168   }
1169   
1170   // If one instruction matched with a missing feature, report this as a
1171   // missing feature.
1172   if ((MatchB == Match_MissingFeature) + (MatchW == Match_MissingFeature) +
1173       (MatchL == Match_MissingFeature) + (MatchQ == Match_MissingFeature) == 1){
1174     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1175     return true;
1176   }
1177   
1178   // If one instruction matched with an invalid operand, report this as an
1179   // operand failure.
1180   if ((MatchB == Match_InvalidOperand) + (MatchW == Match_InvalidOperand) +
1181       (MatchL == Match_InvalidOperand) + (MatchQ == Match_InvalidOperand) == 1){
1182     Error(IDLoc, "invalid operand for instruction");
1183     return true;
1184   }
1185   
1186   // If all of these were an outright failure, report it in a useless way.
1187   // FIXME: We should give nicer diagnostics about the exact failure.
1188   Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
1189   return true;
1190 }
1191
1192
1193 extern "C" void LLVMInitializeX86AsmLexer();
1194
1195 // Force static initialization.
1196 extern "C" void LLVMInitializeX86AsmParser() {
1197   RegisterAsmParser<X86_32ATTAsmParser> X(TheX86_32Target);
1198   RegisterAsmParser<X86_64ATTAsmParser> Y(TheX86_64Target);
1199   LLVMInitializeX86AsmLexer();
1200 }
1201
1202 #define GET_REGISTER_MATCHER
1203 #define GET_MATCHER_IMPLEMENTATION
1204 #include "X86GenAsmMatcher.inc"