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