blob: 298f80aa54dfc4edd4f2fcc092376cfac0fdd57b [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"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/Target/TargetRegistry.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000013#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.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"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Evan Cheng94b95502011-07-26 00:24:13 +000034class X86ATTAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000037
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000043 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
44
Chris Lattner309264d2010-01-15 18:44:13 +000045 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000046 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000047
48 bool ParseDirectiveWord(unsigned Size, SMLoc L);
49
Chris Lattner7036f8b2010-09-29 01:42:58 +000050 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000051 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000052 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000053
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000054 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
55 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
56 bool isSrcOp(X86Operand &Op);
57
58 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
59 /// or %es:(%edi) in 32bit mode.
60 bool isDstOp(X86Operand &Op);
61
Evan Cheng59ee62d2011-07-11 03:57:24 +000062 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000063 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000064 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000065 }
66
Daniel Dunbar54074b52010-07-19 05:44:09 +000067 /// @name Auto-generated Matcher Functions
68 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000069
Chris Lattner0692ee62010-09-06 19:11:01 +000070#define GET_ASSEMBLER_HEADER
71#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000072
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000073 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000074
75public:
Evan Chengffc0e732011-07-09 05:47:46 +000076 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000077 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000078
Daniel Dunbar54074b52010-07-19 05:44:09 +000079 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000080 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000081 }
Roman Divackybf755322011-01-27 17:14:22 +000082 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000083
Benjamin Kramer38e59892010-07-14 22:38:02 +000084 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000085 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000086
87 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000088};
Chris Lattner37dfdec2009-07-29 06:33:53 +000089} // end anonymous namespace
90
Sean Callanane9b466d2010-01-23 00:40:33 +000091/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +000092/// {
Sean Callanane9b466d2010-01-23 00:40:33 +000093
Chris Lattnerb8d6e982010-02-09 00:34:28 +000094static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +000095
96/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +000097
98namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000099
100/// X86Operand - Instances of this class represent a parsed X86 machine
101/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000102struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000103 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000104 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000105 Register,
106 Immediate,
107 Memory
108 } Kind;
109
Chris Lattner29ef9a22010-01-15 18:51:29 +0000110 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000111
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112 union {
113 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000114 const char *Data;
115 unsigned Length;
116 } Tok;
117
118 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000119 unsigned RegNo;
120 } Reg;
121
122 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000123 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000124 } Imm;
125
126 struct {
127 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000128 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000129 unsigned BaseReg;
130 unsigned IndexReg;
131 unsigned Scale;
132 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000133 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000134
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000135 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000136 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000137
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000138 /// getStartLoc - Get the location of the first token of this operand.
139 SMLoc getStartLoc() const { return StartLoc; }
140 /// getEndLoc - Get the location of the last token of this operand.
141 SMLoc getEndLoc() const { return EndLoc; }
142
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000143 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000144
Daniel Dunbar20927f22009-08-07 08:26:05 +0000145 StringRef getToken() const {
146 assert(Kind == Token && "Invalid access!");
147 return StringRef(Tok.Data, Tok.Length);
148 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000149 void setTokenValue(StringRef Value) {
150 assert(Kind == Token && "Invalid access!");
151 Tok.Data = Value.data();
152 Tok.Length = Value.size();
153 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000154
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000155 unsigned getReg() const {
156 assert(Kind == Register && "Invalid access!");
157 return Reg.RegNo;
158 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000159
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000160 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000161 assert(Kind == Immediate && "Invalid access!");
162 return Imm.Val;
163 }
164
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000165 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000166 assert(Kind == Memory && "Invalid access!");
167 return Mem.Disp;
168 }
169 unsigned getMemSegReg() const {
170 assert(Kind == Memory && "Invalid access!");
171 return Mem.SegReg;
172 }
173 unsigned getMemBaseReg() const {
174 assert(Kind == Memory && "Invalid access!");
175 return Mem.BaseReg;
176 }
177 unsigned getMemIndexReg() const {
178 assert(Kind == Memory && "Invalid access!");
179 return Mem.IndexReg;
180 }
181 unsigned getMemScale() const {
182 assert(Kind == Memory && "Invalid access!");
183 return Mem.Scale;
184 }
185
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000186 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000187
188 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000189
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000190 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000191 if (!isImm())
192 return false;
193
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000194 // If this isn't a constant expr, just assume it fits and let relaxation
195 // handle it.
196 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
197 if (!CE)
198 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000199
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000200 // Otherwise, check the value is in a range that makes sense for this
201 // extension.
202 uint64_t Value = CE->getValue();
203 return (( Value <= 0x000000000000007FULL)||
204 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
205 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000206 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000207 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000208 if (!isImm())
209 return false;
210
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000211 // If this isn't a constant expr, just assume it fits and let relaxation
212 // handle it.
213 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
214 if (!CE)
215 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000216
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000217 // Otherwise, check the value is in a range that makes sense for this
218 // extension.
219 uint64_t Value = CE->getValue();
220 return (( Value <= 0x000000000000007FULL)||
221 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
222 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
223 }
224 bool isImmSExti64i8() const {
225 if (!isImm())
226 return false;
227
228 // If this isn't a constant expr, just assume it fits and let relaxation
229 // handle it.
230 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
231 if (!CE)
232 return true;
233
234 // Otherwise, check the value is in a range that makes sense for this
235 // extension.
236 uint64_t Value = CE->getValue();
237 return (( Value <= 0x000000000000007FULL)||
238 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
239 }
240 bool isImmSExti64i32() const {
241 if (!isImm())
242 return false;
243
244 // If this isn't a constant expr, just assume it fits and let relaxation
245 // handle it.
246 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
247 if (!CE)
248 return true;
249
250 // Otherwise, check the value is in a range that makes sense for this
251 // extension.
252 uint64_t Value = CE->getValue();
253 return (( Value <= 0x000000007FFFFFFFULL)||
254 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000255 }
256
Daniel Dunbar20927f22009-08-07 08:26:05 +0000257 bool isMem() const { return Kind == Memory; }
258
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000259 bool isAbsMem() const {
260 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000261 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000262 }
263
Daniel Dunbar20927f22009-08-07 08:26:05 +0000264 bool isReg() const { return Kind == Register; }
265
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000266 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
267 // Add as immediates when possible.
268 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
269 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
270 else
271 Inst.addOperand(MCOperand::CreateExpr(Expr));
272 }
273
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000274 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000275 assert(N == 1 && "Invalid number of operands!");
276 Inst.addOperand(MCOperand::CreateReg(getReg()));
277 }
278
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000279 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000280 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000281 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000282 }
283
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000284 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000285 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000286 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
287 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
288 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000289 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000290 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
291 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000292
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000293 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
294 assert((N == 1) && "Invalid number of operands!");
295 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
296 }
297
Chris Lattnerb4307b32010-01-15 19:28:38 +0000298 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
299 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000300 Res->Tok.Data = Str.data();
301 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000302 return Res;
303 }
304
Chris Lattner29ef9a22010-01-15 18:51:29 +0000305 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000306 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000307 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000308 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000309 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000310
Chris Lattnerb4307b32010-01-15 19:28:38 +0000311 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
312 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000313 Res->Imm.Val = Val;
314 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000315 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000316
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000317 /// Create an absolute memory operand.
318 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
319 SMLoc EndLoc) {
320 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
321 Res->Mem.SegReg = 0;
322 Res->Mem.Disp = Disp;
323 Res->Mem.BaseReg = 0;
324 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000325 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000326 return Res;
327 }
328
329 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000330 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
331 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000332 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000333 // We should never just have a displacement, that should be parsed as an
334 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000335 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
336
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000337 // The scale should always be one of {1,2,4,8}.
338 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000339 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000340 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000341 Res->Mem.SegReg = SegReg;
342 Res->Mem.Disp = Disp;
343 Res->Mem.BaseReg = BaseReg;
344 Res->Mem.IndexReg = IndexReg;
345 Res->Mem.Scale = Scale;
346 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000347 }
348};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000349
Chris Lattner37dfdec2009-07-29 06:33:53 +0000350} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000351
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000352bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000353 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000354
355 return (Op.isMem() &&
356 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
357 isa<MCConstantExpr>(Op.Mem.Disp) &&
358 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
359 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
360}
361
362bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000363 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000364
365 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
366 isa<MCConstantExpr>(Op.Mem.Disp) &&
367 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
368 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
369}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000370
Chris Lattner29ef9a22010-01-15 18:51:29 +0000371bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
372 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000373 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000374 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000375 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000376 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000377 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000378
Sean Callanan18b83232010-01-19 21:44:56 +0000379 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000380 if (Tok.isNot(AsmToken::Identifier))
381 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000382
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000383 // FIXME: Validate register for the current architecture; we have to do
384 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000385 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000386
Chris Lattner33d60d52010-09-22 04:11:10 +0000387 // If the match failed, try the register name as lowercase.
388 if (RegNo == 0)
389 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000390
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000391 // FIXME: This should be done using Requires<In32BitMode> and
392 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
393 // can be also checked.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000394 if (RegNo == X86::RIZ && !is64BitMode())
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000395 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
396
Chris Lattner33d60d52010-09-22 04:11:10 +0000397 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
398 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000399 RegNo = X86::ST0;
400 EndLoc = Tok.getLoc();
401 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000402
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000403 // Check to see if we have '(4)' after %st.
404 if (getLexer().isNot(AsmToken::LParen))
405 return false;
406 // Lex the paren.
407 getParser().Lex();
408
409 const AsmToken &IntTok = Parser.getTok();
410 if (IntTok.isNot(AsmToken::Integer))
411 return Error(IntTok.getLoc(), "expected stack index");
412 switch (IntTok.getIntVal()) {
413 case 0: RegNo = X86::ST0; break;
414 case 1: RegNo = X86::ST1; break;
415 case 2: RegNo = X86::ST2; break;
416 case 3: RegNo = X86::ST3; break;
417 case 4: RegNo = X86::ST4; break;
418 case 5: RegNo = X86::ST5; break;
419 case 6: RegNo = X86::ST6; break;
420 case 7: RegNo = X86::ST7; break;
421 default: return Error(IntTok.getLoc(), "invalid stack index");
422 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000423
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000424 if (getParser().Lex().isNot(AsmToken::RParen))
425 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000426
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000427 EndLoc = Tok.getLoc();
428 Parser.Lex(); // Eat ')'
429 return false;
430 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000431
Chris Lattner645b2092010-06-24 07:29:18 +0000432 // If this is "db[0-7]", match it as an alias
433 // for dr[0-7].
434 if (RegNo == 0 && Tok.getString().size() == 3 &&
435 Tok.getString().startswith("db")) {
436 switch (Tok.getString()[2]) {
437 case '0': RegNo = X86::DR0; break;
438 case '1': RegNo = X86::DR1; break;
439 case '2': RegNo = X86::DR2; break;
440 case '3': RegNo = X86::DR3; break;
441 case '4': RegNo = X86::DR4; break;
442 case '5': RegNo = X86::DR5; break;
443 case '6': RegNo = X86::DR6; break;
444 case '7': RegNo = X86::DR7; break;
445 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000446
Chris Lattner645b2092010-06-24 07:29:18 +0000447 if (RegNo != 0) {
448 EndLoc = Tok.getLoc();
449 Parser.Lex(); // Eat it.
450 return false;
451 }
452 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000453
Daniel Dunbar245f0582009-08-08 21:22:41 +0000454 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000455 return Error(Tok.getLoc(), "invalid register name");
456
Chris Lattner29ef9a22010-01-15 18:51:29 +0000457 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000458 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000459 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000460}
461
Chris Lattner309264d2010-01-15 18:44:13 +0000462X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000463 switch (getLexer().getKind()) {
464 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000465 // Parse a memory operand with no segment register.
466 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000467 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000468 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000469 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000470 SMLoc Start, End;
471 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000472 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
473 Error(Start, "eiz and riz can only be used as index registers");
474 return 0;
475 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000476
Chris Lattnereef6d782010-04-17 18:56:34 +0000477 // If this is a segment register followed by a ':', then this is the start
478 // of a memory reference, otherwise this is a normal register reference.
479 if (getLexer().isNot(AsmToken::Colon))
480 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000481
482
Chris Lattnereef6d782010-04-17 18:56:34 +0000483 getParser().Lex(); // Eat the colon.
484 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000485 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000486 case AsmToken::Dollar: {
487 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000488 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000489 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000490 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000491 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000492 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000493 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000494 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000495 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000496}
497
Chris Lattnereef6d782010-04-17 18:56:34 +0000498/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
499/// has already been parsed if present.
500X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000501
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000502 // We have to disambiguate a parenthesized expression "(4+5)" from the start
503 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000504 // only way to do this without lookahead is to eat the '(' and see what is
505 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000506 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000507 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000508 SMLoc ExprEnd;
509 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000510
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000511 // After parsing the base expression we could either have a parenthesized
512 // memory address or not. If not, return now. If so, eat the (.
513 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000514 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000515 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000516 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000517 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000518 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000519
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000520 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000521 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 } else {
523 // Okay, we have a '('. We don't know if this is an expression or not, but
524 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000525 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000526 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000527
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000528 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000529 // Nothing to do here, fall into the code below with the '(' part of the
530 // memory operand consumed.
531 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000532 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000533
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000534 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000535 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000536 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000537
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 // After parsing the base expression we could either have a parenthesized
539 // memory address or not. If not, return now. If so, eat the (.
540 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000541 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000542 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000543 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000544 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000545 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000546
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000547 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000548 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549 }
550 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000551
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552 // If we reached here, then we just ate the ( of the memory operand. Process
553 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000554 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000555
Chris Lattner29ef9a22010-01-15 18:51:29 +0000556 if (getLexer().is(AsmToken::Percent)) {
557 SMLoc L;
558 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000559 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
560 Error(L, "eiz and riz can only be used as index registers");
561 return 0;
562 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000563 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000564
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000565 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000566 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000567
568 // Following the comma we should have either an index register, or a scale
569 // value. We don't support the later form, but we want to parse it
570 // correctly.
571 //
572 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000573 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000574 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000575 SMLoc L;
576 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000577
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000578 if (getLexer().isNot(AsmToken::RParen)) {
579 // Parse the scale amount:
580 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000581 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000582 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000583 "expected comma in scale expression");
584 return 0;
585 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000586 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000587
588 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000589 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000590
591 int64_t ScaleVal;
592 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000593 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000594
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000595 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000596 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
597 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
598 return 0;
599 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000600 Scale = (unsigned)ScaleVal;
601 }
602 }
603 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000604 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000605 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000606 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000607
608 int64_t Value;
609 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000610 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000611
Daniel Dunbaree910252010-08-24 19:13:38 +0000612 if (Value != 1)
613 Warning(Loc, "scale factor without index register is ignored");
614 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000615 }
616 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000617
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000618 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000619 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000620 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000621 return 0;
622 }
Sean Callanan18b83232010-01-19 21:44:56 +0000623 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000624 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000625
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000626 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
627 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000628}
629
Chris Lattner98986712010-01-14 22:21:20 +0000630bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000631ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000632 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000633 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000634
Chris Lattnerd8f71792010-11-28 20:23:50 +0000635 // FIXME: Hack to recognize setneb as setne.
636 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
637 PatchedName != "setb" && PatchedName != "setnb")
638 PatchedName = PatchedName.substr(0, Name.size()-1);
639
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000640 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
641 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000642 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000643 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
644 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000645 bool IsVCMP = PatchedName.startswith("vcmp");
646 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000647 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000648 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000649 .Case("eq", 0)
650 .Case("lt", 1)
651 .Case("le", 2)
652 .Case("unord", 3)
653 .Case("neq", 4)
654 .Case("nlt", 5)
655 .Case("nle", 6)
656 .Case("ord", 7)
657 .Case("eq_uq", 8)
658 .Case("nge", 9)
659 .Case("ngt", 0x0A)
660 .Case("false", 0x0B)
661 .Case("neq_oq", 0x0C)
662 .Case("ge", 0x0D)
663 .Case("gt", 0x0E)
664 .Case("true", 0x0F)
665 .Case("eq_os", 0x10)
666 .Case("lt_oq", 0x11)
667 .Case("le_oq", 0x12)
668 .Case("unord_s", 0x13)
669 .Case("neq_us", 0x14)
670 .Case("nlt_uq", 0x15)
671 .Case("nle_uq", 0x16)
672 .Case("ord_s", 0x17)
673 .Case("eq_us", 0x18)
674 .Case("nge_uq", 0x19)
675 .Case("ngt_uq", 0x1A)
676 .Case("false_os", 0x1B)
677 .Case("neq_os", 0x1C)
678 .Case("ge_oq", 0x1D)
679 .Case("gt_oq", 0x1E)
680 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000681 .Default(~0U);
682 if (SSEComparisonCode != ~0U) {
683 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
684 getParser().getContext());
685 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000686 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000687 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000688 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000689 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000690 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000691 } else {
692 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000693 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000694 }
695 }
696 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000697
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000698 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000699
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000700 if (ExtraImmOp)
701 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000702
703
Chris Lattner2544f422010-09-08 05:17:37 +0000704 // Determine whether this is an instruction prefix.
705 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000706 Name == "lock" || Name == "rep" ||
707 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000708 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000709 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000710
711
Chris Lattner2544f422010-09-08 05:17:37 +0000712 // This does the actual operand parsing. Don't parse any more if we have a
713 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
714 // just want to parse the "lock" as the first instruction and the "incl" as
715 // the next one.
716 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000717
718 // Parse '*' modifier.
719 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000720 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000721 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000722 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000723 }
724
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000725 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000726 if (X86Operand *Op = ParseOperand())
727 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000728 else {
729 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000730 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000731 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000732
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000733 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000734 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000735
736 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000737 if (X86Operand *Op = ParseOperand())
738 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000739 else {
740 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000741 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000742 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000743 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000744
Chris Lattnercbf8a982010-09-11 16:18:25 +0000745 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000746 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000747 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000748 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000749 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000751
Chris Lattner2544f422010-09-08 05:17:37 +0000752 if (getLexer().is(AsmToken::EndOfStatement))
753 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000754 else if (isPrefix && getLexer().is(AsmToken::Slash))
755 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000756
Chris Lattner98c870f2010-11-06 19:25:43 +0000757 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
758 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
759 // documented form in various unofficial manuals, so a lot of code uses it.
760 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
761 Operands.size() == 3) {
762 X86Operand &Op = *(X86Operand*)Operands.back();
763 if (Op.isMem() && Op.Mem.SegReg == 0 &&
764 isa<MCConstantExpr>(Op.Mem.Disp) &&
765 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
766 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
767 SMLoc Loc = Op.getEndLoc();
768 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
769 delete &Op;
770 }
771 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000772 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
773 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
774 Operands.size() == 3) {
775 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
776 if (Op.isMem() && Op.Mem.SegReg == 0 &&
777 isa<MCConstantExpr>(Op.Mem.Disp) &&
778 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
779 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
780 SMLoc Loc = Op.getEndLoc();
781 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
782 delete &Op;
783 }
784 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000785 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
786 if (Name.startswith("ins") && Operands.size() == 3 &&
787 (Name == "insb" || Name == "insw" || Name == "insl")) {
788 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
789 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
790 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
791 Operands.pop_back();
792 Operands.pop_back();
793 delete &Op;
794 delete &Op2;
795 }
796 }
797
798 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
799 if (Name.startswith("outs") && Operands.size() == 3 &&
800 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
801 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
802 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
803 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
804 Operands.pop_back();
805 Operands.pop_back();
806 delete &Op;
807 delete &Op2;
808 }
809 }
810
811 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
812 if (Name.startswith("movs") && Operands.size() == 3 &&
813 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000814 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000815 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
816 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
817 if (isSrcOp(Op) && isDstOp(Op2)) {
818 Operands.pop_back();
819 Operands.pop_back();
820 delete &Op;
821 delete &Op2;
822 }
823 }
824 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
825 if (Name.startswith("lods") && Operands.size() == 3 &&
826 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000827 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000828 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
829 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
830 if (isSrcOp(*Op1) && Op2->isReg()) {
831 const char *ins;
832 unsigned reg = Op2->getReg();
833 bool isLods = Name == "lods";
834 if (reg == X86::AL && (isLods || Name == "lodsb"))
835 ins = "lodsb";
836 else if (reg == X86::AX && (isLods || Name == "lodsw"))
837 ins = "lodsw";
838 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
839 ins = "lodsl";
840 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
841 ins = "lodsq";
842 else
843 ins = NULL;
844 if (ins != NULL) {
845 Operands.pop_back();
846 Operands.pop_back();
847 delete Op1;
848 delete Op2;
849 if (Name != ins)
850 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
851 }
852 }
853 }
854 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
855 if (Name.startswith("stos") && Operands.size() == 3 &&
856 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000857 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000858 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
859 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
860 if (isDstOp(*Op2) && Op1->isReg()) {
861 const char *ins;
862 unsigned reg = Op1->getReg();
863 bool isStos = Name == "stos";
864 if (reg == X86::AL && (isStos || Name == "stosb"))
865 ins = "stosb";
866 else if (reg == X86::AX && (isStos || Name == "stosw"))
867 ins = "stosw";
868 else if (reg == X86::EAX && (isStos || Name == "stosl"))
869 ins = "stosl";
870 else if (reg == X86::RAX && (isStos || Name == "stosq"))
871 ins = "stosq";
872 else
873 ins = NULL;
874 if (ins != NULL) {
875 Operands.pop_back();
876 Operands.pop_back();
877 delete Op1;
878 delete Op2;
879 if (Name != ins)
880 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
881 }
882 }
883 }
884
Chris Lattnere9e16a32010-09-15 04:33:27 +0000885 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000886 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000887 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000888 Name.startswith("shl") || Name.startswith("sal") ||
889 Name.startswith("rcl") || Name.startswith("rcr") ||
890 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000891 Operands.size() == 3) {
892 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
893 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
894 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
895 delete Operands[1];
896 Operands.erase(Operands.begin() + 1);
897 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000898 }
Chris Lattner15f89512011-04-09 19:41:05 +0000899
900 // Transforms "int $3" into "int3" as a size optimization. We can't write an
901 // instalias with an immediate operand yet.
902 if (Name == "int" && Operands.size() == 2) {
903 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
904 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
905 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
906 delete Operands[1];
907 Operands.erase(Operands.begin() + 1);
908 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
909 }
910 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000911
Chris Lattner98986712010-01-14 22:21:20 +0000912 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000913}
914
Chris Lattner2d592d12010-09-15 04:04:33 +0000915bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000916MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000917 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000918 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000919 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000920 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
921 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000922
Chris Lattner7c51a312010-09-29 01:50:45 +0000923 // First, handle aliases that expand to multiple instructions.
924 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000925 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
926 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000927 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000928 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000929 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000930 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000931 MCInst Inst;
932 Inst.setOpcode(X86::WAIT);
933 Out.EmitInstruction(Inst);
934
Chris Lattner0bb83a82010-09-30 16:39:29 +0000935 const char *Repl =
936 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000937 .Case("finit", "fninit")
938 .Case("fsave", "fnsave")
939 .Case("fstcw", "fnstcw")
940 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000941 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000942 .Case("fstsw", "fnstsw")
943 .Case("fstsww", "fnstsw")
944 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000945 .Default(0);
946 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000947 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000948 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000949 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000950
Chris Lattnera008e8a2010-09-06 21:54:15 +0000951 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000952 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000953 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000954
Daniel Dunbarc918d602010-05-04 16:12:42 +0000955 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000956 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000957 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000958 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000959 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000960 case Match_MissingFeature:
961 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
962 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000963 case Match_ConversionFail:
964 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000965 case Match_InvalidOperand:
966 WasOriginallyInvalidOperand = true;
967 break;
968 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000969 break;
970 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000971
Daniel Dunbarc918d602010-05-04 16:12:42 +0000972 // FIXME: Ideally, we would only attempt suffix matches for things which are
973 // valid prefixes, and we could just infer the right unambiguous
974 // type. However, that requires substantially more matcher support than the
975 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000976
Daniel Dunbarc918d602010-05-04 16:12:42 +0000977 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000978 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000979 SmallString<16> Tmp;
980 Tmp += Base;
981 Tmp += ' ';
982 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +0000983
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000984 // If this instruction starts with an 'f', then it is a floating point stack
985 // instruction. These come in up to three forms for 32-bit, 64-bit, and
986 // 80-bit floating point, which use the suffixes s,l,t respectively.
987 //
988 // Otherwise, we assume that this may be an integer instruction, which comes
989 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
990 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
991
Daniel Dunbarc918d602010-05-04 16:12:42 +0000992 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000993 Tmp[Base.size()] = Suffixes[0];
994 unsigned ErrorInfoIgnore;
995 MatchResultTy Match1, Match2, Match3, Match4;
996
997 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
998 Tmp[Base.size()] = Suffixes[1];
999 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1000 Tmp[Base.size()] = Suffixes[2];
1001 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1002 Tmp[Base.size()] = Suffixes[3];
1003 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001004
1005 // Restore the old token.
1006 Op->setTokenValue(Base);
1007
1008 // If exactly one matched, then we treat that as a successful match (and the
1009 // instruction will already have been filled in correctly, since the failing
1010 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001011 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001012 (Match1 == Match_Success) + (Match2 == Match_Success) +
1013 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001014 if (NumSuccessfulMatches == 1) {
1015 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001016 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001017 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001018
Chris Lattnerec6789f2010-09-06 20:08:02 +00001019 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001020
Daniel Dunbar09062b12010-08-12 00:55:42 +00001021 // If we had multiple suffix matches, then identify this as an ambiguous
1022 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001023 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001024 char MatchChars[4];
1025 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001026 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1027 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1028 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1029 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001030
1031 SmallString<126> Msg;
1032 raw_svector_ostream OS(Msg);
1033 OS << "ambiguous instructions require an explicit suffix (could be ";
1034 for (unsigned i = 0; i != NumMatches; ++i) {
1035 if (i != 0)
1036 OS << ", ";
1037 if (i + 1 == NumMatches)
1038 OS << "or ";
1039 OS << "'" << Base << MatchChars[i] << "'";
1040 }
1041 OS << ")";
1042 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001043 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001044 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001045
Chris Lattnera008e8a2010-09-06 21:54:15 +00001046 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001047
Chris Lattnera008e8a2010-09-06 21:54:15 +00001048 // If all of the instructions reported an invalid mnemonic, then the original
1049 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001050 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1051 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001052 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001053 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001054 return true;
1055 }
1056
1057 // Recover location info for the operand if we know which was the problem.
1058 SMLoc ErrorLoc = IDLoc;
1059 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001060 if (OrigErrorInfo >= Operands.size())
1061 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001062
Chris Lattnerce4a3352010-09-06 22:11:18 +00001063 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1064 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1065 }
1066
Chris Lattnerf8840122010-09-15 03:50:11 +00001067 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001068 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001069
Chris Lattnerec6789f2010-09-06 20:08:02 +00001070 // If one instruction matched with a missing feature, report this as a
1071 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001072 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1073 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001074 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1075 return true;
1076 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001077
Chris Lattnera008e8a2010-09-06 21:54:15 +00001078 // If one instruction matched with an invalid operand, report this as an
1079 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001080 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1081 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001082 Error(IDLoc, "invalid operand for instruction");
1083 return true;
1084 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001085
Chris Lattnerec6789f2010-09-06 20:08:02 +00001086 // If all of these were an outright failure, report it in a useless way.
1087 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001088 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001089 return true;
1090}
1091
1092
Chris Lattner537ca842010-10-30 17:38:55 +00001093bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1094 StringRef IDVal = DirectiveID.getIdentifier();
1095 if (IDVal == ".word")
1096 return ParseDirectiveWord(2, DirectiveID.getLoc());
1097 return true;
1098}
1099
1100/// ParseDirectiveWord
1101/// ::= .word [ expression (, expression)* ]
1102bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1103 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1104 for (;;) {
1105 const MCExpr *Value;
1106 if (getParser().ParseExpression(Value))
1107 return true;
1108
1109 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1110
1111 if (getLexer().is(AsmToken::EndOfStatement))
1112 break;
1113
1114 // FIXME: Improve diagnostic.
1115 if (getLexer().isNot(AsmToken::Comma))
1116 return Error(L, "unexpected token in directive");
1117 Parser.Lex();
1118 }
1119 }
1120
1121 Parser.Lex();
1122 return false;
1123}
1124
1125
1126
1127
Sean Callanane88f5522010-01-23 02:43:15 +00001128extern "C" void LLVMInitializeX86AsmLexer();
1129
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001130// Force static initialization.
1131extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001132 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1133 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001134 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001135}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001136
Chris Lattner0692ee62010-09-06 19:11:01 +00001137#define GET_REGISTER_MATCHER
1138#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001139#include "X86GenAsmMatcher.inc"