blob: 42512a7c50dc1ed4b14dba2cdf62184ff92de9bd [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- 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
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Evan Cheng94b95502011-07-26 00:24:13 +000035class X86ATTAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000038
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
45 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
46 return Parser.Error(L, Msg, Ranges);
47 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048
Chris Lattner309264d2010-01-15 18:44:13 +000049 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000050 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000051
52 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000053 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000054
Chris Lattner7036f8b2010-09-29 01:42:58 +000055 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000056 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000057 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000058
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000059 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
60 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
61 bool isSrcOp(X86Operand &Op);
62
63 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
64 /// or %es:(%edi) in 32bit mode.
65 bool isDstOp(X86Operand &Op);
66
Evan Cheng59ee62d2011-07-11 03:57:24 +000067 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000068 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000069 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000070 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000071 void SwitchMode() {
72 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
73 setAvailableFeatures(FB);
74 }
Evan Chengebdeeab2011-07-08 01:53:10 +000075
Daniel Dunbar54074b52010-07-19 05:44:09 +000076 /// @name Auto-generated Matcher Functions
77 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000078
Chris Lattner0692ee62010-09-06 19:11:01 +000079#define GET_ASSEMBLER_HEADER
80#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000081
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000082 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000083
84public:
Evan Chengffc0e732011-07-09 05:47:46 +000085 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000086 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000087
Daniel Dunbar54074b52010-07-19 05:44:09 +000088 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000089 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000090 }
Roman Divackybf755322011-01-27 17:14:22 +000091 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000092
Benjamin Kramer38e59892010-07-14 22:38:02 +000093 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000094 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000095
96 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000097};
Chris Lattner37dfdec2009-07-29 06:33:53 +000098} // end anonymous namespace
99
Sean Callanane9b466d2010-01-23 00:40:33 +0000100/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000101/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000102
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000103static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000104
105/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000106
107namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000108
109/// X86Operand - Instances of this class represent a parsed X86 machine
110/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000111struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000112 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000113 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000114 Register,
115 Immediate,
116 Memory
117 } Kind;
118
Chris Lattner29ef9a22010-01-15 18:51:29 +0000119 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000120
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000121 union {
122 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000123 const char *Data;
124 unsigned Length;
125 } Tok;
126
127 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000128 unsigned RegNo;
129 } Reg;
130
131 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000132 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000133 } Imm;
134
135 struct {
136 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000137 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000138 unsigned BaseReg;
139 unsigned IndexReg;
140 unsigned Scale;
141 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000142 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000143
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000144 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000145 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000146
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000147 /// getStartLoc - Get the location of the first token of this operand.
148 SMLoc getStartLoc() const { return StartLoc; }
149 /// getEndLoc - Get the location of the last token of this operand.
150 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000151
152 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000153
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000154 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000155
Daniel Dunbar20927f22009-08-07 08:26:05 +0000156 StringRef getToken() const {
157 assert(Kind == Token && "Invalid access!");
158 return StringRef(Tok.Data, Tok.Length);
159 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000160 void setTokenValue(StringRef Value) {
161 assert(Kind == Token && "Invalid access!");
162 Tok.Data = Value.data();
163 Tok.Length = Value.size();
164 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000165
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 unsigned getReg() const {
167 assert(Kind == Register && "Invalid access!");
168 return Reg.RegNo;
169 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000170
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000171 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000172 assert(Kind == Immediate && "Invalid access!");
173 return Imm.Val;
174 }
175
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000176 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000177 assert(Kind == Memory && "Invalid access!");
178 return Mem.Disp;
179 }
180 unsigned getMemSegReg() const {
181 assert(Kind == Memory && "Invalid access!");
182 return Mem.SegReg;
183 }
184 unsigned getMemBaseReg() const {
185 assert(Kind == Memory && "Invalid access!");
186 return Mem.BaseReg;
187 }
188 unsigned getMemIndexReg() const {
189 assert(Kind == Memory && "Invalid access!");
190 return Mem.IndexReg;
191 }
192 unsigned getMemScale() const {
193 assert(Kind == Memory && "Invalid access!");
194 return Mem.Scale;
195 }
196
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000197 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000198
199 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000200
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000201 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000202 if (!isImm())
203 return false;
204
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000205 // If this isn't a constant expr, just assume it fits and let relaxation
206 // handle it.
207 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
208 if (!CE)
209 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000210
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000211 // Otherwise, check the value is in a range that makes sense for this
212 // extension.
213 uint64_t Value = CE->getValue();
214 return (( Value <= 0x000000000000007FULL)||
215 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
216 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000217 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000218 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000219 if (!isImm())
220 return false;
221
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000222 // If this isn't a constant expr, just assume it fits and let relaxation
223 // handle it.
224 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
225 if (!CE)
226 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000227
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000228 // Otherwise, check the value is in a range that makes sense for this
229 // extension.
230 uint64_t Value = CE->getValue();
231 return (( Value <= 0x000000000000007FULL)||
232 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
233 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
234 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000235 bool isImmZExtu32u8() const {
236 if (!isImm())
237 return false;
238
239 // If this isn't a constant expr, just assume it fits and let relaxation
240 // handle it.
241 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
242 if (!CE)
243 return true;
244
245 // Otherwise, check the value is in a range that makes sense for this
246 // extension.
247 uint64_t Value = CE->getValue();
248 return (Value <= 0x00000000000000FFULL);
249 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000250 bool isImmSExti64i8() const {
251 if (!isImm())
252 return false;
253
254 // If this isn't a constant expr, just assume it fits and let relaxation
255 // handle it.
256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
257 if (!CE)
258 return true;
259
260 // Otherwise, check the value is in a range that makes sense for this
261 // extension.
262 uint64_t Value = CE->getValue();
263 return (( Value <= 0x000000000000007FULL)||
264 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
265 }
266 bool isImmSExti64i32() const {
267 if (!isImm())
268 return false;
269
270 // If this isn't a constant expr, just assume it fits and let relaxation
271 // handle it.
272 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
273 if (!CE)
274 return true;
275
276 // Otherwise, check the value is in a range that makes sense for this
277 // extension.
278 uint64_t Value = CE->getValue();
279 return (( Value <= 0x000000007FFFFFFFULL)||
280 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000281 }
282
Daniel Dunbar20927f22009-08-07 08:26:05 +0000283 bool isMem() const { return Kind == Memory; }
284
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000285 bool isAbsMem() const {
286 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000287 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000288 }
289
Daniel Dunbar20927f22009-08-07 08:26:05 +0000290 bool isReg() const { return Kind == Register; }
291
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000292 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
293 // Add as immediates when possible.
294 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
295 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
296 else
297 Inst.addOperand(MCOperand::CreateExpr(Expr));
298 }
299
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000300 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000301 assert(N == 1 && "Invalid number of operands!");
302 Inst.addOperand(MCOperand::CreateReg(getReg()));
303 }
304
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000305 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000306 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000307 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000308 }
309
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000310 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000311 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000312 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
313 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
314 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000315 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000316 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
317 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000318
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000319 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
320 assert((N == 1) && "Invalid number of operands!");
321 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
322 }
323
Chris Lattnerb4307b32010-01-15 19:28:38 +0000324 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000325 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
326 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000327 Res->Tok.Data = Str.data();
328 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000329 return Res;
330 }
331
Chris Lattner29ef9a22010-01-15 18:51:29 +0000332 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000333 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000334 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000335 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000336 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000337
Chris Lattnerb4307b32010-01-15 19:28:38 +0000338 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
339 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000340 Res->Imm.Val = Val;
341 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000342 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000343
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000344 /// Create an absolute memory operand.
345 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
346 SMLoc EndLoc) {
347 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
348 Res->Mem.SegReg = 0;
349 Res->Mem.Disp = Disp;
350 Res->Mem.BaseReg = 0;
351 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000352 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000353 return Res;
354 }
355
356 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000357 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
358 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000359 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000360 // We should never just have a displacement, that should be parsed as an
361 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000362 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
363
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000364 // The scale should always be one of {1,2,4,8}.
365 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000366 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000367 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000368 Res->Mem.SegReg = SegReg;
369 Res->Mem.Disp = Disp;
370 Res->Mem.BaseReg = BaseReg;
371 Res->Mem.IndexReg = IndexReg;
372 Res->Mem.Scale = Scale;
373 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000374 }
375};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000376
Chris Lattner37dfdec2009-07-29 06:33:53 +0000377} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000378
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000379bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000380 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000381
382 return (Op.isMem() &&
383 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
384 isa<MCConstantExpr>(Op.Mem.Disp) &&
385 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
386 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
387}
388
389bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000390 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000391
392 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
393 isa<MCConstantExpr>(Op.Mem.Disp) &&
394 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
395 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
396}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000397
Chris Lattner29ef9a22010-01-15 18:51:29 +0000398bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
399 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000400 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000401 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000402 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000403 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000404 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000405
Sean Callanan18b83232010-01-19 21:44:56 +0000406 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000407 if (Tok.isNot(AsmToken::Identifier))
408 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000409
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000410 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000411
Chris Lattner33d60d52010-09-22 04:11:10 +0000412 // If the match failed, try the register name as lowercase.
413 if (RegNo == 0)
414 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000415
Evan Cheng5de728c2011-07-27 23:22:03 +0000416 if (!is64BitMode()) {
417 // FIXME: This should be done using Requires<In32BitMode> and
418 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
419 // checked.
420 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
421 // REX prefix.
422 if (RegNo == X86::RIZ ||
423 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
424 X86II::isX86_64NonExtLowByteReg(RegNo) ||
425 X86II::isX86_64ExtendedReg(RegNo))
426 return Error(Tok.getLoc(), "register %"
427 + Tok.getString() + " is only available in 64-bit mode");
428 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000429
Chris Lattner33d60d52010-09-22 04:11:10 +0000430 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
431 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000432 RegNo = X86::ST0;
433 EndLoc = Tok.getLoc();
434 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000435
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000436 // Check to see if we have '(4)' after %st.
437 if (getLexer().isNot(AsmToken::LParen))
438 return false;
439 // Lex the paren.
440 getParser().Lex();
441
442 const AsmToken &IntTok = Parser.getTok();
443 if (IntTok.isNot(AsmToken::Integer))
444 return Error(IntTok.getLoc(), "expected stack index");
445 switch (IntTok.getIntVal()) {
446 case 0: RegNo = X86::ST0; break;
447 case 1: RegNo = X86::ST1; break;
448 case 2: RegNo = X86::ST2; break;
449 case 3: RegNo = X86::ST3; break;
450 case 4: RegNo = X86::ST4; break;
451 case 5: RegNo = X86::ST5; break;
452 case 6: RegNo = X86::ST6; break;
453 case 7: RegNo = X86::ST7; break;
454 default: return Error(IntTok.getLoc(), "invalid stack index");
455 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000456
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000457 if (getParser().Lex().isNot(AsmToken::RParen))
458 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000459
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000460 EndLoc = Tok.getLoc();
461 Parser.Lex(); // Eat ')'
462 return false;
463 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000464
Chris Lattner645b2092010-06-24 07:29:18 +0000465 // If this is "db[0-7]", match it as an alias
466 // for dr[0-7].
467 if (RegNo == 0 && Tok.getString().size() == 3 &&
468 Tok.getString().startswith("db")) {
469 switch (Tok.getString()[2]) {
470 case '0': RegNo = X86::DR0; break;
471 case '1': RegNo = X86::DR1; break;
472 case '2': RegNo = X86::DR2; break;
473 case '3': RegNo = X86::DR3; break;
474 case '4': RegNo = X86::DR4; break;
475 case '5': RegNo = X86::DR5; break;
476 case '6': RegNo = X86::DR6; break;
477 case '7': RegNo = X86::DR7; break;
478 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000479
Chris Lattner645b2092010-06-24 07:29:18 +0000480 if (RegNo != 0) {
481 EndLoc = Tok.getLoc();
482 Parser.Lex(); // Eat it.
483 return false;
484 }
485 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000486
Daniel Dunbar245f0582009-08-08 21:22:41 +0000487 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000488 return Error(Tok.getLoc(), "invalid register name");
489
Chris Lattner29ef9a22010-01-15 18:51:29 +0000490 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000491 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000492 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000493}
494
Chris Lattner309264d2010-01-15 18:44:13 +0000495X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000496 switch (getLexer().getKind()) {
497 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000498 // Parse a memory operand with no segment register.
499 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000500 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000501 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000502 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000503 SMLoc Start, End;
504 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000505 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Evan Cheng5de728c2011-07-27 23:22:03 +0000506 Error(Start, "%eiz and %riz can only be used as index registers");
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000507 return 0;
508 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000509
Chris Lattnereef6d782010-04-17 18:56:34 +0000510 // If this is a segment register followed by a ':', then this is the start
511 // of a memory reference, otherwise this is a normal register reference.
512 if (getLexer().isNot(AsmToken::Colon))
513 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000514
515
Chris Lattnereef6d782010-04-17 18:56:34 +0000516 getParser().Lex(); // Eat the colon.
517 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000518 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000519 case AsmToken::Dollar: {
520 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000521 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000522 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000523 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000524 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000525 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000526 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000528 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000529}
530
Chris Lattnereef6d782010-04-17 18:56:34 +0000531/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
532/// has already been parsed if present.
533X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000534
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000535 // We have to disambiguate a parenthesized expression "(4+5)" from the start
536 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000537 // only way to do this without lookahead is to eat the '(' and see what is
538 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000539 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000540 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000541 SMLoc ExprEnd;
542 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000543
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000544 // After parsing the base expression we could either have a parenthesized
545 // memory address or not. If not, return now. If so, eat the (.
546 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000547 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000548 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000549 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000550 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000551 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000552
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000553 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000554 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000555 } else {
556 // Okay, we have a '('. We don't know if this is an expression or not, but
557 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000558 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000559 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000560
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000561 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000562 // Nothing to do here, fall into the code below with the '(' part of the
563 // memory operand consumed.
564 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000565 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000566
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000567 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000569 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000570
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000571 // After parsing the base expression we could either have a parenthesized
572 // memory address or not. If not, return now. If so, eat the (.
573 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000574 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000575 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000576 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000577 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000578 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000579
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000580 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000581 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000582 }
583 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000585 // If we reached here, then we just ate the ( of the memory operand. Process
586 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000587 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000588
Chris Lattner29ef9a22010-01-15 18:51:29 +0000589 if (getLexer().is(AsmToken::Percent)) {
590 SMLoc L;
591 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000592 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
593 Error(L, "eiz and riz can only be used as index registers");
594 return 0;
595 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000596 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000597
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000598 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000599 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000600
601 // Following the comma we should have either an index register, or a scale
602 // value. We don't support the later form, but we want to parse it
603 // correctly.
604 //
605 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000606 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000607 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000608 SMLoc L;
609 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000610
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000611 if (getLexer().isNot(AsmToken::RParen)) {
612 // Parse the scale amount:
613 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000614 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000615 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000616 "expected comma in scale expression");
617 return 0;
618 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000619 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000620
621 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000622 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000623
624 int64_t ScaleVal;
625 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000626 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000627
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000628 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000629 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
630 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
631 return 0;
632 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000633 Scale = (unsigned)ScaleVal;
634 }
635 }
636 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000637 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000638 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000639 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000640
641 int64_t Value;
642 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000643 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000644
Daniel Dunbaree910252010-08-24 19:13:38 +0000645 if (Value != 1)
646 Warning(Loc, "scale factor without index register is ignored");
647 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000648 }
649 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000650
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000651 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000652 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000653 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000654 return 0;
655 }
Sean Callanan18b83232010-01-19 21:44:56 +0000656 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000657 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000658
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000659 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
660 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000661}
662
Chris Lattner98986712010-01-14 22:21:20 +0000663bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000664ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000665 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000666 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000667
Chris Lattnerd8f71792010-11-28 20:23:50 +0000668 // FIXME: Hack to recognize setneb as setne.
669 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
670 PatchedName != "setb" && PatchedName != "setnb")
671 PatchedName = PatchedName.substr(0, Name.size()-1);
672
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000673 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
674 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000675 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000676 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
677 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000678 bool IsVCMP = PatchedName.startswith("vcmp");
679 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000680 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000681 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000682 .Case("eq", 0)
683 .Case("lt", 1)
684 .Case("le", 2)
685 .Case("unord", 3)
686 .Case("neq", 4)
687 .Case("nlt", 5)
688 .Case("nle", 6)
689 .Case("ord", 7)
690 .Case("eq_uq", 8)
691 .Case("nge", 9)
692 .Case("ngt", 0x0A)
693 .Case("false", 0x0B)
694 .Case("neq_oq", 0x0C)
695 .Case("ge", 0x0D)
696 .Case("gt", 0x0E)
697 .Case("true", 0x0F)
698 .Case("eq_os", 0x10)
699 .Case("lt_oq", 0x11)
700 .Case("le_oq", 0x12)
701 .Case("unord_s", 0x13)
702 .Case("neq_us", 0x14)
703 .Case("nlt_uq", 0x15)
704 .Case("nle_uq", 0x16)
705 .Case("ord_s", 0x17)
706 .Case("eq_us", 0x18)
707 .Case("nge_uq", 0x19)
708 .Case("ngt_uq", 0x1A)
709 .Case("false_os", 0x1B)
710 .Case("neq_os", 0x1C)
711 .Case("ge_oq", 0x1D)
712 .Case("gt_oq", 0x1E)
713 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000714 .Default(~0U);
715 if (SSEComparisonCode != ~0U) {
716 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
717 getParser().getContext());
718 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000719 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000720 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000721 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000722 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000723 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000724 } else {
725 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000726 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000727 }
728 }
729 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000730
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000731 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000732
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000733 if (ExtraImmOp)
734 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000735
736
Chris Lattner2544f422010-09-08 05:17:37 +0000737 // Determine whether this is an instruction prefix.
738 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000739 Name == "lock" || Name == "rep" ||
740 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000741 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000742 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000743
744
Chris Lattner2544f422010-09-08 05:17:37 +0000745 // This does the actual operand parsing. Don't parse any more if we have a
746 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
747 // just want to parse the "lock" as the first instruction and the "incl" as
748 // the next one.
749 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000750
751 // Parse '*' modifier.
752 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000753 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000754 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000755 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000756 }
757
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000758 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000759 if (X86Operand *Op = ParseOperand())
760 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000761 else {
762 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000763 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000764 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000765
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000766 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000767 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000768
769 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000770 if (X86Operand *Op = ParseOperand())
771 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000772 else {
773 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000774 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000775 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000776 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000777
Chris Lattnercbf8a982010-09-11 16:18:25 +0000778 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000779 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000780 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000781 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000782 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000783 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000784
Chris Lattner2544f422010-09-08 05:17:37 +0000785 if (getLexer().is(AsmToken::EndOfStatement))
786 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000787 else if (isPrefix && getLexer().is(AsmToken::Slash))
788 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000789
Chris Lattner98c870f2010-11-06 19:25:43 +0000790 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
791 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
792 // documented form in various unofficial manuals, so a lot of code uses it.
793 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
794 Operands.size() == 3) {
795 X86Operand &Op = *(X86Operand*)Operands.back();
796 if (Op.isMem() && Op.Mem.SegReg == 0 &&
797 isa<MCConstantExpr>(Op.Mem.Disp) &&
798 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
799 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
800 SMLoc Loc = Op.getEndLoc();
801 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
802 delete &Op;
803 }
804 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000805 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
806 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
807 Operands.size() == 3) {
808 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
809 if (Op.isMem() && Op.Mem.SegReg == 0 &&
810 isa<MCConstantExpr>(Op.Mem.Disp) &&
811 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
812 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
813 SMLoc Loc = Op.getEndLoc();
814 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
815 delete &Op;
816 }
817 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000818 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
819 if (Name.startswith("ins") && Operands.size() == 3 &&
820 (Name == "insb" || Name == "insw" || Name == "insl")) {
821 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
822 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
823 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
824 Operands.pop_back();
825 Operands.pop_back();
826 delete &Op;
827 delete &Op2;
828 }
829 }
830
831 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
832 if (Name.startswith("outs") && Operands.size() == 3 &&
833 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
834 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
835 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
836 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
837 Operands.pop_back();
838 Operands.pop_back();
839 delete &Op;
840 delete &Op2;
841 }
842 }
843
844 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
845 if (Name.startswith("movs") && Operands.size() == 3 &&
846 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000847 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000848 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
849 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
850 if (isSrcOp(Op) && isDstOp(Op2)) {
851 Operands.pop_back();
852 Operands.pop_back();
853 delete &Op;
854 delete &Op2;
855 }
856 }
857 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
858 if (Name.startswith("lods") && Operands.size() == 3 &&
859 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000860 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000861 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
862 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
863 if (isSrcOp(*Op1) && Op2->isReg()) {
864 const char *ins;
865 unsigned reg = Op2->getReg();
866 bool isLods = Name == "lods";
867 if (reg == X86::AL && (isLods || Name == "lodsb"))
868 ins = "lodsb";
869 else if (reg == X86::AX && (isLods || Name == "lodsw"))
870 ins = "lodsw";
871 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
872 ins = "lodsl";
873 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
874 ins = "lodsq";
875 else
876 ins = NULL;
877 if (ins != NULL) {
878 Operands.pop_back();
879 Operands.pop_back();
880 delete Op1;
881 delete Op2;
882 if (Name != ins)
883 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
884 }
885 }
886 }
887 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
888 if (Name.startswith("stos") && Operands.size() == 3 &&
889 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000890 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000891 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
892 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
893 if (isDstOp(*Op2) && Op1->isReg()) {
894 const char *ins;
895 unsigned reg = Op1->getReg();
896 bool isStos = Name == "stos";
897 if (reg == X86::AL && (isStos || Name == "stosb"))
898 ins = "stosb";
899 else if (reg == X86::AX && (isStos || Name == "stosw"))
900 ins = "stosw";
901 else if (reg == X86::EAX && (isStos || Name == "stosl"))
902 ins = "stosl";
903 else if (reg == X86::RAX && (isStos || Name == "stosq"))
904 ins = "stosq";
905 else
906 ins = NULL;
907 if (ins != NULL) {
908 Operands.pop_back();
909 Operands.pop_back();
910 delete Op1;
911 delete Op2;
912 if (Name != ins)
913 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
914 }
915 }
916 }
917
Chris Lattnere9e16a32010-09-15 04:33:27 +0000918 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000919 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000920 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000921 Name.startswith("shl") || Name.startswith("sal") ||
922 Name.startswith("rcl") || Name.startswith("rcr") ||
923 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000924 Operands.size() == 3) {
925 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
926 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
927 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
928 delete Operands[1];
929 Operands.erase(Operands.begin() + 1);
930 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000931 }
Chris Lattner15f89512011-04-09 19:41:05 +0000932
933 // Transforms "int $3" into "int3" as a size optimization. We can't write an
934 // instalias with an immediate operand yet.
935 if (Name == "int" && Operands.size() == 2) {
936 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
937 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
938 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
939 delete Operands[1];
940 Operands.erase(Operands.begin() + 1);
941 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
942 }
943 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000944
Chris Lattner98986712010-01-14 22:21:20 +0000945 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000946}
947
Chris Lattner2d592d12010-09-15 04:04:33 +0000948bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000949MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000950 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000951 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000952 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000953 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
954 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000955
Chris Lattner7c51a312010-09-29 01:50:45 +0000956 // First, handle aliases that expand to multiple instructions.
957 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000958 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
959 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000960 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000961 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000962 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000963 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000964 MCInst Inst;
965 Inst.setOpcode(X86::WAIT);
966 Out.EmitInstruction(Inst);
967
Chris Lattner0bb83a82010-09-30 16:39:29 +0000968 const char *Repl =
969 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000970 .Case("finit", "fninit")
971 .Case("fsave", "fnsave")
972 .Case("fstcw", "fnstcw")
973 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000974 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000975 .Case("fstsw", "fnstsw")
976 .Case("fstsww", "fnstsw")
977 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000978 .Default(0);
979 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000980 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000981 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000982 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Chris Lattnera008e8a2010-09-06 21:54:15 +0000984 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000985 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000986 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000987
Daniel Dunbarc918d602010-05-04 16:12:42 +0000988 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000989 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +0000990 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000991 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000992 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000993 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000994 case Match_MissingFeature:
995 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
996 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000997 case Match_ConversionFail:
998 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000999 case Match_InvalidOperand:
1000 WasOriginallyInvalidOperand = true;
1001 break;
1002 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001003 break;
1004 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001005
Daniel Dunbarc918d602010-05-04 16:12:42 +00001006 // FIXME: Ideally, we would only attempt suffix matches for things which are
1007 // valid prefixes, and we could just infer the right unambiguous
1008 // type. However, that requires substantially more matcher support than the
1009 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001010
Daniel Dunbarc918d602010-05-04 16:12:42 +00001011 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001012 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001013 SmallString<16> Tmp;
1014 Tmp += Base;
1015 Tmp += ' ';
1016 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001017
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001018 // If this instruction starts with an 'f', then it is a floating point stack
1019 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1020 // 80-bit floating point, which use the suffixes s,l,t respectively.
1021 //
1022 // Otherwise, we assume that this may be an integer instruction, which comes
1023 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1024 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1025
Daniel Dunbarc918d602010-05-04 16:12:42 +00001026 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001027 Tmp[Base.size()] = Suffixes[0];
1028 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001029 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001030
1031 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1032 Tmp[Base.size()] = Suffixes[1];
1033 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1034 Tmp[Base.size()] = Suffixes[2];
1035 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1036 Tmp[Base.size()] = Suffixes[3];
1037 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001038
1039 // Restore the old token.
1040 Op->setTokenValue(Base);
1041
1042 // If exactly one matched, then we treat that as a successful match (and the
1043 // instruction will already have been filled in correctly, since the failing
1044 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001045 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001046 (Match1 == Match_Success) + (Match2 == Match_Success) +
1047 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001048 if (NumSuccessfulMatches == 1) {
1049 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001050 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001051 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001052
Chris Lattnerec6789f2010-09-06 20:08:02 +00001053 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001054
Daniel Dunbar09062b12010-08-12 00:55:42 +00001055 // If we had multiple suffix matches, then identify this as an ambiguous
1056 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001057 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001058 char MatchChars[4];
1059 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001060 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1061 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1062 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1063 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001064
1065 SmallString<126> Msg;
1066 raw_svector_ostream OS(Msg);
1067 OS << "ambiguous instructions require an explicit suffix (could be ";
1068 for (unsigned i = 0; i != NumMatches; ++i) {
1069 if (i != 0)
1070 OS << ", ";
1071 if (i + 1 == NumMatches)
1072 OS << "or ";
1073 OS << "'" << Base << MatchChars[i] << "'";
1074 }
1075 OS << ")";
1076 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001077 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001078 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001079
Chris Lattnera008e8a2010-09-06 21:54:15 +00001080 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001081
Chris Lattnera008e8a2010-09-06 21:54:15 +00001082 // If all of the instructions reported an invalid mnemonic, then the original
1083 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001084 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1085 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001086 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001087 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1088 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001089 }
1090
1091 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001092 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001093 if (OrigErrorInfo >= Operands.size())
1094 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001095
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001096 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1097 if (Operand->getStartLoc().isValid()) {
1098 SMRange OperandRange = Operand->getLocRange();
1099 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1100 OperandRange);
1101 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001102 }
1103
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001104 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001105 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001106
Chris Lattnerec6789f2010-09-06 20:08:02 +00001107 // If one instruction matched with a missing feature, report this as a
1108 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001109 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1110 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001111 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1112 return true;
1113 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001114
Chris Lattnera008e8a2010-09-06 21:54:15 +00001115 // If one instruction matched with an invalid operand, report this as an
1116 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001117 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1118 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001119 Error(IDLoc, "invalid operand for instruction");
1120 return true;
1121 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001122
Chris Lattnerec6789f2010-09-06 20:08:02 +00001123 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001124 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001125 return true;
1126}
1127
1128
Chris Lattner537ca842010-10-30 17:38:55 +00001129bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1130 StringRef IDVal = DirectiveID.getIdentifier();
1131 if (IDVal == ".word")
1132 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001133 else if (IDVal.startswith(".code"))
1134 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001135 return true;
1136}
1137
1138/// ParseDirectiveWord
1139/// ::= .word [ expression (, expression)* ]
1140bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1141 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1142 for (;;) {
1143 const MCExpr *Value;
1144 if (getParser().ParseExpression(Value))
1145 return true;
1146
1147 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1148
1149 if (getLexer().is(AsmToken::EndOfStatement))
1150 break;
1151
1152 // FIXME: Improve diagnostic.
1153 if (getLexer().isNot(AsmToken::Comma))
1154 return Error(L, "unexpected token in directive");
1155 Parser.Lex();
1156 }
1157 }
1158
1159 Parser.Lex();
1160 return false;
1161}
1162
Evan Chengbd27f5a2011-07-27 00:38:12 +00001163/// ParseDirectiveCode
1164/// ::= .code32 | .code64
1165bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1166 if (IDVal == ".code32") {
1167 Parser.Lex();
1168 if (is64BitMode()) {
1169 SwitchMode();
1170 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1171 }
1172 } else if (IDVal == ".code64") {
1173 Parser.Lex();
1174 if (!is64BitMode()) {
1175 SwitchMode();
1176 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1177 }
1178 } else {
1179 return Error(L, "unexpected directive " + IDVal);
1180 }
Chris Lattner537ca842010-10-30 17:38:55 +00001181
Evan Chengbd27f5a2011-07-27 00:38:12 +00001182 return false;
1183}
Chris Lattner537ca842010-10-30 17:38:55 +00001184
1185
Sean Callanane88f5522010-01-23 02:43:15 +00001186extern "C" void LLVMInitializeX86AsmLexer();
1187
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001188// Force static initialization.
1189extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001190 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1191 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001192 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001193}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001194
Chris Lattner0692ee62010-09-06 19:11:01 +00001195#define GET_REGISTER_MATCHER
1196#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001197#include "X86GenAsmMatcher.inc"