]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Target/X86/AsmParser/X86AsmParser.cpp
Update LLVM to r103052.
[FreeBSD/FreeBSD.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 "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCParser/MCAsmLexer.h"
19 #include "llvm/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Target/TargetAsmParser.h"
24 using namespace llvm;
25
26 namespace {
27 struct X86Operand;
28
29 class X86ATTAsmParser : public TargetAsmParser {
30   MCAsmParser &Parser;
31
32 protected:
33   unsigned Is64Bit : 1;
34
35 private:
36   MCAsmParser &getParser() const { return Parser; }
37
38   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
39
40   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
41
42   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
43
44   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
45
46   X86Operand *ParseOperand();
47   X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
48
49   bool ParseDirectiveWord(unsigned Size, SMLoc L);
50
51   void InstructionCleanup(MCInst &Inst);
52
53   /// @name Auto-generated Match Functions
54   /// {
55
56   bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
57                         MCInst &Inst);
58
59   bool MatchInstructionImpl(
60     const SmallVectorImpl<MCParsedAsmOperand*> &Operands, MCInst &Inst);
61
62   /// }
63
64 public:
65   X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
66     : TargetAsmParser(T), Parser(_Parser) {}
67
68   virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
69                                 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
70
71   virtual bool ParseDirective(AsmToken DirectiveID);
72 };
73  
74 class X86_32ATTAsmParser : public X86ATTAsmParser {
75 public:
76   X86_32ATTAsmParser(const Target &T, MCAsmParser &_Parser)
77     : X86ATTAsmParser(T, _Parser) {
78     Is64Bit = false;
79   }
80 };
81
82 class X86_64ATTAsmParser : public X86ATTAsmParser {
83 public:
84   X86_64ATTAsmParser(const Target &T, MCAsmParser &_Parser)
85     : X86ATTAsmParser(T, _Parser) {
86     Is64Bit = true;
87   }
88 };
89
90 } // end anonymous namespace
91
92 /// @name Auto-generated Match Functions
93 /// {  
94
95 static unsigned MatchRegisterName(StringRef Name);
96
97 /// }
98
99 namespace {
100
101 /// X86Operand - Instances of this class represent a parsed X86 machine
102 /// instruction.
103 struct X86Operand : public MCParsedAsmOperand {
104   enum KindTy {
105     Token,
106     Register,
107     Immediate,
108     Memory
109   } Kind;
110
111   SMLoc StartLoc, EndLoc;
112   
113   union {
114     struct {
115       const char *Data;
116       unsigned Length;
117     } Tok;
118
119     struct {
120       unsigned RegNo;
121     } Reg;
122
123     struct {
124       const MCExpr *Val;
125     } Imm;
126
127     struct {
128       unsigned SegReg;
129       const MCExpr *Disp;
130       unsigned BaseReg;
131       unsigned IndexReg;
132       unsigned Scale;
133     } Mem;
134   };
135
136   X86Operand(KindTy K, SMLoc Start, SMLoc End)
137     : Kind(K), StartLoc(Start), EndLoc(End) {}
138
139   /// getStartLoc - Get the location of the first token of this operand.
140   SMLoc getStartLoc() const { return StartLoc; }
141   /// getEndLoc - Get the location of the last token of this operand.
142   SMLoc getEndLoc() const { return EndLoc; }
143
144   StringRef getToken() const {
145     assert(Kind == Token && "Invalid access!");
146     return StringRef(Tok.Data, Tok.Length);
147   }
148   void setTokenValue(StringRef Value) {
149     assert(Kind == Token && "Invalid access!");
150     Tok.Data = Value.data();
151     Tok.Length = Value.size();
152   }
153
154   unsigned getReg() const {
155     assert(Kind == Register && "Invalid access!");
156     return Reg.RegNo;
157   }
158
159   const MCExpr *getImm() const {
160     assert(Kind == Immediate && "Invalid access!");
161     return Imm.Val;
162   }
163
164   const MCExpr *getMemDisp() const {
165     assert(Kind == Memory && "Invalid access!");
166     return Mem.Disp;
167   }
168   unsigned getMemSegReg() const {
169     assert(Kind == Memory && "Invalid access!");
170     return Mem.SegReg;
171   }
172   unsigned getMemBaseReg() const {
173     assert(Kind == Memory && "Invalid access!");
174     return Mem.BaseReg;
175   }
176   unsigned getMemIndexReg() const {
177     assert(Kind == Memory && "Invalid access!");
178     return Mem.IndexReg;
179   }
180   unsigned getMemScale() const {
181     assert(Kind == Memory && "Invalid access!");
182     return Mem.Scale;
183   }
184
185   bool isToken() const {return Kind == Token; }
186
187   bool isImm() const { return Kind == Immediate; }
188   
189   bool isImmSExt8() const { 
190     // Accept immediates which fit in 8 bits when sign extended, and
191     // non-absolute immediates.
192     if (!isImm())
193       return false;
194
195     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
196       int64_t Value = CE->getValue();
197       return Value == (int64_t) (int8_t) Value;
198     }
199
200     return true;
201   }
202   
203   bool isMem() const { return Kind == Memory; }
204
205   bool isAbsMem() const {
206     return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
207       !getMemIndexReg() && getMemScale() == 1;
208   }
209
210   bool isNoSegMem() const {
211     return Kind == Memory && !getMemSegReg();
212   }
213
214   bool isReg() const { return Kind == Register; }
215
216   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
217     // Add as immediates when possible.
218     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
219       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
220     else
221       Inst.addOperand(MCOperand::CreateExpr(Expr));
222   }
223
224   void addRegOperands(MCInst &Inst, unsigned N) const {
225     assert(N == 1 && "Invalid number of operands!");
226     Inst.addOperand(MCOperand::CreateReg(getReg()));
227   }
228
229   void addImmOperands(MCInst &Inst, unsigned N) const {
230     assert(N == 1 && "Invalid number of operands!");
231     addExpr(Inst, getImm());
232   }
233
234   void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
235     // FIXME: Support user customization of the render method.
236     assert(N == 1 && "Invalid number of operands!");
237     addExpr(Inst, getImm());
238   }
239
240   void addMemOperands(MCInst &Inst, unsigned N) const {
241     assert((N == 5) && "Invalid number of operands!");
242     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
243     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
244     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
245     addExpr(Inst, getMemDisp());
246     Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
247   }
248
249   void addAbsMemOperands(MCInst &Inst, unsigned N) const {
250     assert((N == 1) && "Invalid number of operands!");
251     Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
252   }
253
254   void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
255     assert((N == 4) && "Invalid number of operands!");
256     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
257     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
258     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
259     addExpr(Inst, getMemDisp());
260   }
261
262   static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
263     X86Operand *Res = new X86Operand(Token, Loc, Loc);
264     Res->Tok.Data = Str.data();
265     Res->Tok.Length = Str.size();
266     return Res;
267   }
268
269   static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
270     X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
271     Res->Reg.RegNo = RegNo;
272     return Res;
273   }
274
275   static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
276     X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
277     Res->Imm.Val = Val;
278     return Res;
279   }
280
281   /// Create an absolute memory operand.
282   static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
283                                SMLoc EndLoc) {
284     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
285     Res->Mem.SegReg   = 0;
286     Res->Mem.Disp     = Disp;
287     Res->Mem.BaseReg  = 0;
288     Res->Mem.IndexReg = 0;
289     Res->Mem.Scale    = 1;
290     return Res;
291   }
292
293   /// Create a generalized memory operand.
294   static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
295                                unsigned BaseReg, unsigned IndexReg,
296                                unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
297     // We should never just have a displacement, that should be parsed as an
298     // absolute memory operand.
299     assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
300
301     // The scale should always be one of {1,2,4,8}.
302     assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
303            "Invalid scale!");
304     X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
305     Res->Mem.SegReg   = SegReg;
306     Res->Mem.Disp     = Disp;
307     Res->Mem.BaseReg  = BaseReg;
308     Res->Mem.IndexReg = IndexReg;
309     Res->Mem.Scale    = Scale;
310     return Res;
311   }
312 };
313
314 } // end anonymous namespace.
315
316
317 bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
318                                     SMLoc &StartLoc, SMLoc &EndLoc) {
319   RegNo = 0;
320   const AsmToken &TokPercent = Parser.getTok();
321   assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
322   StartLoc = TokPercent.getLoc();
323   Parser.Lex(); // Eat percent token.
324
325   const AsmToken &Tok = Parser.getTok();
326   if (Tok.isNot(AsmToken::Identifier))
327     return Error(Tok.getLoc(), "invalid register name");
328
329   // FIXME: Validate register for the current architecture; we have to do
330   // validation later, so maybe there is no need for this here.
331   RegNo = MatchRegisterName(Tok.getString());
332   
333   // Parse %st(1) and "%st" as "%st(0)"
334   if (RegNo == 0 && Tok.getString() == "st") {
335     RegNo = X86::ST0;
336     EndLoc = Tok.getLoc();
337     Parser.Lex(); // Eat 'st'
338     
339     // Check to see if we have '(4)' after %st.
340     if (getLexer().isNot(AsmToken::LParen))
341       return false;
342     // Lex the paren.
343     getParser().Lex();
344
345     const AsmToken &IntTok = Parser.getTok();
346     if (IntTok.isNot(AsmToken::Integer))
347       return Error(IntTok.getLoc(), "expected stack index");
348     switch (IntTok.getIntVal()) {
349     case 0: RegNo = X86::ST0; break;
350     case 1: RegNo = X86::ST1; break;
351     case 2: RegNo = X86::ST2; break;
352     case 3: RegNo = X86::ST3; break;
353     case 4: RegNo = X86::ST4; break;
354     case 5: RegNo = X86::ST5; break;
355     case 6: RegNo = X86::ST6; break;
356     case 7: RegNo = X86::ST7; break;
357     default: return Error(IntTok.getLoc(), "invalid stack index");
358     }
359     
360     if (getParser().Lex().isNot(AsmToken::RParen))
361       return Error(Parser.getTok().getLoc(), "expected ')'");
362     
363     EndLoc = Tok.getLoc();
364     Parser.Lex(); // Eat ')'
365     return false;
366   }
367   
368   if (RegNo == 0)
369     return Error(Tok.getLoc(), "invalid register name");
370
371   EndLoc = Tok.getLoc();
372   Parser.Lex(); // Eat identifier token.
373   return false;
374 }
375
376 X86Operand *X86ATTAsmParser::ParseOperand() {
377   switch (getLexer().getKind()) {
378   default:
379     // Parse a memory operand with no segment register.
380     return ParseMemOperand(0, Parser.getTok().getLoc());
381   case AsmToken::Percent: {
382     // Read the register.
383     unsigned RegNo;
384     SMLoc Start, End;
385     if (ParseRegister(RegNo, Start, End)) return 0;
386     
387     // If this is a segment register followed by a ':', then this is the start
388     // of a memory reference, otherwise this is a normal register reference.
389     if (getLexer().isNot(AsmToken::Colon))
390       return X86Operand::CreateReg(RegNo, Start, End);
391     
392     
393     getParser().Lex(); // Eat the colon.
394     return ParseMemOperand(RegNo, Start);
395   }
396   case AsmToken::Dollar: {
397     // $42 -> immediate.
398     SMLoc Start = Parser.getTok().getLoc(), End;
399     Parser.Lex();
400     const MCExpr *Val;
401     if (getParser().ParseExpression(Val, End))
402       return 0;
403     return X86Operand::CreateImm(Val, Start, End);
404   }
405   }
406 }
407
408 /// ParseMemOperand: segment: disp(basereg, indexreg, scale).  The '%ds:' prefix
409 /// has already been parsed if present.
410 X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
411  
412   // We have to disambiguate a parenthesized expression "(4+5)" from the start
413   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
414   // only way to do this without lookahead is to eat the '(' and see what is
415   // after it.
416   const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
417   if (getLexer().isNot(AsmToken::LParen)) {
418     SMLoc ExprEnd;
419     if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
420     
421     // After parsing the base expression we could either have a parenthesized
422     // memory address or not.  If not, return now.  If so, eat the (.
423     if (getLexer().isNot(AsmToken::LParen)) {
424       // Unless we have a segment register, treat this as an immediate.
425       if (SegReg == 0)
426         return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
427       return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
428     }
429     
430     // Eat the '('.
431     Parser.Lex();
432   } else {
433     // Okay, we have a '('.  We don't know if this is an expression or not, but
434     // so we have to eat the ( to see beyond it.
435     SMLoc LParenLoc = Parser.getTok().getLoc();
436     Parser.Lex(); // Eat the '('.
437     
438     if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
439       // Nothing to do here, fall into the code below with the '(' part of the
440       // memory operand consumed.
441     } else {
442       SMLoc ExprEnd;
443       
444       // It must be an parenthesized expression, parse it now.
445       if (getParser().ParseParenExpression(Disp, ExprEnd))
446         return 0;
447       
448       // After parsing the base expression we could either have a parenthesized
449       // memory address or not.  If not, return now.  If so, eat the (.
450       if (getLexer().isNot(AsmToken::LParen)) {
451         // Unless we have a segment register, treat this as an immediate.
452         if (SegReg == 0)
453           return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
454         return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
455       }
456       
457       // Eat the '('.
458       Parser.Lex();
459     }
460   }
461   
462   // If we reached here, then we just ate the ( of the memory operand.  Process
463   // the rest of the memory operand.
464   unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
465   
466   if (getLexer().is(AsmToken::Percent)) {
467     SMLoc L;
468     if (ParseRegister(BaseReg, L, L)) return 0;
469   }
470   
471   if (getLexer().is(AsmToken::Comma)) {
472     Parser.Lex(); // Eat the comma.
473
474     // Following the comma we should have either an index register, or a scale
475     // value. We don't support the later form, but we want to parse it
476     // correctly.
477     //
478     // Not that even though it would be completely consistent to support syntax
479     // like "1(%eax,,1)", the assembler doesn't.
480     if (getLexer().is(AsmToken::Percent)) {
481       SMLoc L;
482       if (ParseRegister(IndexReg, L, L)) return 0;
483     
484       if (getLexer().isNot(AsmToken::RParen)) {
485         // Parse the scale amount:
486         //  ::= ',' [scale-expression]
487         if (getLexer().isNot(AsmToken::Comma)) {
488           Error(Parser.getTok().getLoc(),
489                 "expected comma in scale expression");
490           return 0;
491         }
492         Parser.Lex(); // Eat the comma.
493
494         if (getLexer().isNot(AsmToken::RParen)) {
495           SMLoc Loc = Parser.getTok().getLoc();
496
497           int64_t ScaleVal;
498           if (getParser().ParseAbsoluteExpression(ScaleVal))
499             return 0;
500           
501           // Validate the scale amount.
502           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
503             Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
504             return 0;
505           }
506           Scale = (unsigned)ScaleVal;
507         }
508       }
509     } else if (getLexer().isNot(AsmToken::RParen)) {
510       // Otherwise we have the unsupported form of a scale amount without an
511       // index.
512       SMLoc Loc = Parser.getTok().getLoc();
513
514       int64_t Value;
515       if (getParser().ParseAbsoluteExpression(Value))
516         return 0;
517       
518       Error(Loc, "cannot have scale factor without index register");
519       return 0;
520     }
521   }
522   
523   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
524   if (getLexer().isNot(AsmToken::RParen)) {
525     Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
526     return 0;
527   }
528   SMLoc MemEnd = Parser.getTok().getLoc();
529   Parser.Lex(); // Eat the ')'.
530   
531   return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
532                                MemStart, MemEnd);
533 }
534
535 bool X86ATTAsmParser::
536 ParseInstruction(const StringRef &Name, SMLoc NameLoc,
537                  SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
538   // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to
539   // represent alternative syntaxes in the .td file, without requiring
540   // instruction duplication.
541   StringRef PatchedName = StringSwitch<StringRef>(Name)
542     .Case("sal", "shl")
543     .Case("salb", "shlb")
544     .Case("sall", "shll")
545     .Case("salq", "shlq")
546     .Case("salw", "shlw")
547     .Case("repe", "rep")
548     .Case("repz", "rep")
549     .Case("repnz", "repne")
550     .Default(Name);
551   Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
552
553   if (getLexer().isNot(AsmToken::EndOfStatement)) {
554
555     // Parse '*' modifier.
556     if (getLexer().is(AsmToken::Star)) {
557       SMLoc Loc = Parser.getTok().getLoc();
558       Operands.push_back(X86Operand::CreateToken("*", Loc));
559       Parser.Lex(); // Eat the star.
560     }
561
562     // Read the first operand.
563     if (X86Operand *Op = ParseOperand())
564       Operands.push_back(Op);
565     else
566       return true;
567     
568     while (getLexer().is(AsmToken::Comma)) {
569       Parser.Lex();  // Eat the comma.
570
571       // Parse and remember the operand.
572       if (X86Operand *Op = ParseOperand())
573         Operands.push_back(Op);
574       else
575         return true;
576     }
577   }
578
579   // FIXME: Hack to handle recognizing s{hr,ar,hl}? $1.
580   if ((Name.startswith("shr") || Name.startswith("sar") ||
581        Name.startswith("shl")) &&
582       Operands.size() == 3 &&
583       static_cast<X86Operand*>(Operands[1])->isImm() &&
584       isa<MCConstantExpr>(static_cast<X86Operand*>(Operands[1])->getImm()) &&
585       cast<MCConstantExpr>(static_cast<X86Operand*>(Operands[1])->getImm())->getValue() == 1) {
586     delete Operands[1];
587     Operands.erase(Operands.begin() + 1);
588   }
589
590   return false;
591 }
592
593 bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
594   StringRef IDVal = DirectiveID.getIdentifier();
595   if (IDVal == ".word")
596     return ParseDirectiveWord(2, DirectiveID.getLoc());
597   return true;
598 }
599
600 /// ParseDirectiveWord
601 ///  ::= .word [ expression (, expression)* ]
602 bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
603   if (getLexer().isNot(AsmToken::EndOfStatement)) {
604     for (;;) {
605       const MCExpr *Value;
606       if (getParser().ParseExpression(Value))
607         return true;
608
609       getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
610
611       if (getLexer().is(AsmToken::EndOfStatement))
612         break;
613       
614       // FIXME: Improve diagnostic.
615       if (getLexer().isNot(AsmToken::Comma))
616         return Error(L, "unexpected token in directive");
617       Parser.Lex();
618     }
619   }
620
621   Parser.Lex();
622   return false;
623 }
624
625 // FIXME: Custom X86 cleanup function to implement a temporary hack to handle
626 // matching INCL/DECL correctly for x86_64. This needs to be replaced by a
627 // proper mechanism for supporting (ambiguous) feature dependent instructions.
628 void X86ATTAsmParser::InstructionCleanup(MCInst &Inst) {
629   if (!Is64Bit) return;
630
631   switch (Inst.getOpcode()) {
632   case X86::DEC16r: Inst.setOpcode(X86::DEC64_16r); break;
633   case X86::DEC16m: Inst.setOpcode(X86::DEC64_16m); break;
634   case X86::DEC32r: Inst.setOpcode(X86::DEC64_32r); break;
635   case X86::DEC32m: Inst.setOpcode(X86::DEC64_32m); break;
636   case X86::INC16r: Inst.setOpcode(X86::INC64_16r); break;
637   case X86::INC16m: Inst.setOpcode(X86::INC64_16m); break;
638   case X86::INC32r: Inst.setOpcode(X86::INC64_32r); break;
639   case X86::INC32m: Inst.setOpcode(X86::INC64_32m); break;
640   }
641 }
642
643 bool
644 X86ATTAsmParser::MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*>
645                                     &Operands,
646                                   MCInst &Inst) {
647   // First, try a direct match.
648   if (!MatchInstructionImpl(Operands, Inst))
649     return false;
650
651   // Ignore anything which is obviously not a suffix match.
652   if (Operands.size() == 0)
653     return true;
654   X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
655   if (!Op->isToken() || Op->getToken().size() > 15)
656     return true;
657
658   // FIXME: Ideally, we would only attempt suffix matches for things which are
659   // valid prefixes, and we could just infer the right unambiguous
660   // type. However, that requires substantially more matcher support than the
661   // following hack.
662
663   // Change the operand to point to a temporary token.
664   char Tmp[16];
665   StringRef Base = Op->getToken();
666   memcpy(Tmp, Base.data(), Base.size());
667   Op->setTokenValue(StringRef(Tmp, Base.size() + 1));
668
669   // Check for the various suffix matches.
670   Tmp[Base.size()] = 'b';
671   bool MatchB = MatchInstructionImpl(Operands, Inst);
672   Tmp[Base.size()] = 'w';
673   bool MatchW = MatchInstructionImpl(Operands, Inst);
674   Tmp[Base.size()] = 'l';
675   bool MatchL = MatchInstructionImpl(Operands, Inst);
676
677   // Restore the old token.
678   Op->setTokenValue(Base);
679
680   // If exactly one matched, then we treat that as a successful match (and the
681   // instruction will already have been filled in correctly, since the failing
682   // matches won't have modified it).
683   if (MatchB + MatchW + MatchL == 2)
684     return false;
685
686   // Otherwise, the match failed.
687   return true;
688 }
689
690
691 extern "C" void LLVMInitializeX86AsmLexer();
692
693 // Force static initialization.
694 extern "C" void LLVMInitializeX86AsmParser() {
695   RegisterAsmParser<X86_32ATTAsmParser> X(TheX86_32Target);
696   RegisterAsmParser<X86_64ATTAsmParser> Y(TheX86_64Target);
697   LLVMInitializeX86AsmLexer();
698 }
699
700 #include "X86GenAsmMatcher.inc"