blob: d45dd352fbc49627d4d49240e930d103a230e10d [file] [log] [blame]
Daniel Dunbar71475772009-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
Chris Lattnerf29c0b62010-01-14 22:21:20 +000010#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbar67038c12009-07-18 23:03:22 +000011#include "X86.h"
Daniel Dunbareefe8612010-07-19 05:44:09 +000012#include "X86Subtarget.h"
Chris Lattner1261b812010-09-22 04:11:10 +000013#include "llvm/Target/TargetRegistry.h"
14#include "llvm/Target/TargetAsmParser.h"
Kevin Enderbyce4bec82009-09-10 20:51:44 +000015#include "llvm/MC/MCStreamer.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbarb6d6aa22009-07-31 02:32:59 +000017#include "llvm/MC/MCInst.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000018#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000019#include "llvm/MC/MCParser/MCAsmLexer.h"
20#include "llvm/MC/MCParser/MCAsmParser.h"
21#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerdebe69f2011-07-08 21:06:23 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattner1261b812010-09-22 04:11:10 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/ADT/Twine.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000028#include "llvm/Support/SourceMgr.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000030
Daniel Dunbar71475772009-07-17 20:42:00 +000031using namespace llvm;
32
33namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000034struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000035
36class X86ATTAsmParser : public TargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000037 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000038 MCAsmParser &Parser;
Michael J. Spencer530ce852010-10-09 11:00:50 +000039
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000040private:
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000041 MCAsmParser &getParser() const { return Parser; }
42
43 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
44
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000045 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
46
Chris Lattnera2bbb7c2010-01-15 18:44:13 +000047 X86Operand *ParseOperand();
Chris Lattnerb9270732010-04-17 18:56:34 +000048 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000049
50 bool ParseDirectiveWord(unsigned Size, SMLoc L);
51
Chris Lattnerb44fd242010-09-29 01:42:58 +000052 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattnera63292a2010-09-29 01:50:45 +000053 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerb44fd242010-09-29 01:42:58 +000054 MCStreamer &Out);
Daniel Dunbare10787e2009-08-07 08:26:05 +000055
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000056 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
57 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
58 bool isSrcOp(X86Operand &Op);
59
60 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
61 /// or %es:(%edi) in 32bit mode.
62 bool isDstOp(X86Operand &Op);
63
Evan Chengc5e6d2f2011-07-11 03:57:24 +000064 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +000065 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +000066 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +000067 }
68
Daniel Dunbareefe8612010-07-19 05:44:09 +000069 /// @name Auto-generated Matcher Functions
70 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +000071
Chris Lattner3e4582a2010-09-06 19:11:01 +000072#define GET_ASSEMBLER_HEADER
73#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +000074
Daniel Dunbar00331992009-07-29 00:02:19 +000075 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000076
77public:
Evan Cheng91111d22011-07-09 05:47:46 +000078 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
79 : TargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencer530ce852010-10-09 11:00:50 +000080
Daniel Dunbareefe8612010-07-19 05:44:09 +000081 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +000082 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +000083 }
Roman Divacky36b1b472011-01-27 17:14:22 +000084 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000085
Benjamin Kramer92d89982010-07-14 22:38:02 +000086 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +000087 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000088
89 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000090};
Chris Lattner4eb9df02009-07-29 06:33:53 +000091} // end anonymous namespace
92
Sean Callanan86c11812010-01-23 00:40:33 +000093/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +000094/// {
Sean Callanan86c11812010-01-23 00:40:33 +000095
Chris Lattner60db0a62010-02-09 00:34:28 +000096static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +000097
98/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +000099
100namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000101
102/// X86Operand - Instances of this class represent a parsed X86 machine
103/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000104struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000105 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000106 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000107 Register,
108 Immediate,
109 Memory
110 } Kind;
111
Chris Lattner0c2538f2010-01-15 18:51:29 +0000112 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000113
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000114 union {
115 struct {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000116 const char *Data;
117 unsigned Length;
118 } Tok;
119
120 struct {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000121 unsigned RegNo;
122 } Reg;
123
124 struct {
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000125 const MCExpr *Val;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000126 } Imm;
127
128 struct {
129 unsigned SegReg;
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000130 const MCExpr *Disp;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000131 unsigned BaseReg;
132 unsigned IndexReg;
133 unsigned Scale;
134 } Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000135 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000136
Chris Lattner015cfb12010-01-15 19:33:43 +0000137 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000138 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000139
Chris Lattner86e61532010-01-15 19:06:59 +0000140 /// getStartLoc - Get the location of the first token of this operand.
141 SMLoc getStartLoc() const { return StartLoc; }
142 /// getEndLoc - Get the location of the last token of this operand.
143 SMLoc getEndLoc() const { return EndLoc; }
144
Jim Grosbach602aa902011-07-13 15:34:57 +0000145 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000146
Daniel Dunbare10787e2009-08-07 08:26:05 +0000147 StringRef getToken() const {
148 assert(Kind == Token && "Invalid access!");
149 return StringRef(Tok.Data, Tok.Length);
150 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000151 void setTokenValue(StringRef Value) {
152 assert(Kind == Token && "Invalid access!");
153 Tok.Data = Value.data();
154 Tok.Length = Value.size();
155 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000156
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000157 unsigned getReg() const {
158 assert(Kind == Register && "Invalid access!");
159 return Reg.RegNo;
160 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000161
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000162 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000163 assert(Kind == Immediate && "Invalid access!");
164 return Imm.Val;
165 }
166
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000167 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000168 assert(Kind == Memory && "Invalid access!");
169 return Mem.Disp;
170 }
171 unsigned getMemSegReg() const {
172 assert(Kind == Memory && "Invalid access!");
173 return Mem.SegReg;
174 }
175 unsigned getMemBaseReg() const {
176 assert(Kind == Memory && "Invalid access!");
177 return Mem.BaseReg;
178 }
179 unsigned getMemIndexReg() const {
180 assert(Kind == Memory && "Invalid access!");
181 return Mem.IndexReg;
182 }
183 unsigned getMemScale() const {
184 assert(Kind == Memory && "Invalid access!");
185 return Mem.Scale;
186 }
187
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000188 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000189
190 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000191
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000192 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000193 if (!isImm())
194 return false;
195
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000196 // If this isn't a constant expr, just assume it fits and let relaxation
197 // handle it.
198 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
199 if (!CE)
200 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000201
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000202 // Otherwise, check the value is in a range that makes sense for this
203 // extension.
204 uint64_t Value = CE->getValue();
205 return (( Value <= 0x000000000000007FULL)||
206 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
207 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000208 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000209 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000210 if (!isImm())
211 return false;
212
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000213 // If this isn't a constant expr, just assume it fits and let relaxation
214 // handle it.
215 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
216 if (!CE)
217 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000218
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000219 // Otherwise, check the value is in a range that makes sense for this
220 // extension.
221 uint64_t Value = CE->getValue();
222 return (( Value <= 0x000000000000007FULL)||
223 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
224 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
225 }
226 bool isImmSExti64i8() const {
227 if (!isImm())
228 return false;
229
230 // If this isn't a constant expr, just assume it fits and let relaxation
231 // handle it.
232 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
233 if (!CE)
234 return true;
235
236 // Otherwise, check the value is in a range that makes sense for this
237 // extension.
238 uint64_t Value = CE->getValue();
239 return (( Value <= 0x000000000000007FULL)||
240 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
241 }
242 bool isImmSExti64i32() const {
243 if (!isImm())
244 return false;
245
246 // If this isn't a constant expr, just assume it fits and let relaxation
247 // handle it.
248 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
249 if (!CE)
250 return true;
251
252 // Otherwise, check the value is in a range that makes sense for this
253 // extension.
254 uint64_t Value = CE->getValue();
255 return (( Value <= 0x000000007FFFFFFFULL)||
256 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000257 }
258
Daniel Dunbare10787e2009-08-07 08:26:05 +0000259 bool isMem() const { return Kind == Memory; }
260
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000261 bool isAbsMem() const {
262 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000263 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000264 }
265
Daniel Dunbare10787e2009-08-07 08:26:05 +0000266 bool isReg() const { return Kind == Register; }
267
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000268 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
269 // Add as immediates when possible.
270 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
271 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
272 else
273 Inst.addOperand(MCOperand::CreateExpr(Expr));
274 }
275
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000276 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000277 assert(N == 1 && "Invalid number of operands!");
278 Inst.addOperand(MCOperand::CreateReg(getReg()));
279 }
280
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000281 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000282 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000283 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000284 }
285
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000286 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000287 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000288 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
289 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
290 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000291 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000292 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
293 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000294
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000295 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
296 assert((N == 1) && "Invalid number of operands!");
297 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
298 }
299
Chris Lattner528d00b2010-01-15 19:28:38 +0000300 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
301 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000302 Res->Tok.Data = Str.data();
303 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000304 return Res;
305 }
306
Chris Lattner0c2538f2010-01-15 18:51:29 +0000307 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner86e61532010-01-15 19:06:59 +0000308 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000309 Res->Reg.RegNo = RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000310 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000311 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000312
Chris Lattner528d00b2010-01-15 19:28:38 +0000313 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
314 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000315 Res->Imm.Val = Val;
316 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000317 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000318
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000319 /// Create an absolute memory operand.
320 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
321 SMLoc EndLoc) {
322 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
323 Res->Mem.SegReg = 0;
324 Res->Mem.Disp = Disp;
325 Res->Mem.BaseReg = 0;
326 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000327 Res->Mem.Scale = 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000328 return Res;
329 }
330
331 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000332 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
333 unsigned BaseReg, unsigned IndexReg,
Chris Lattner015cfb12010-01-15 19:33:43 +0000334 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000335 // We should never just have a displacement, that should be parsed as an
336 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000337 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
338
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000339 // The scale should always be one of {1,2,4,8}.
340 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000341 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000342 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000343 Res->Mem.SegReg = SegReg;
344 Res->Mem.Disp = Disp;
345 Res->Mem.BaseReg = BaseReg;
346 Res->Mem.IndexReg = IndexReg;
347 Res->Mem.Scale = Scale;
348 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000349 }
350};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000351
Chris Lattner4eb9df02009-07-29 06:33:53 +0000352} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000353
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000354bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000355 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000356
357 return (Op.isMem() &&
358 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
359 isa<MCConstantExpr>(Op.Mem.Disp) &&
360 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
361 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
362}
363
364bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000365 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000366
367 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
368 isa<MCConstantExpr>(Op.Mem.Disp) &&
369 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
370 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
371}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000372
Chris Lattner0c2538f2010-01-15 18:51:29 +0000373bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
374 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000375 RegNo = 0;
Sean Callanan936b0d32010-01-19 21:44:56 +0000376 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7d912182009-09-03 17:15:07 +0000377 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner0c2538f2010-01-15 18:51:29 +0000378 StartLoc = TokPercent.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000379 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000380
Sean Callanan936b0d32010-01-19 21:44:56 +0000381 const AsmToken &Tok = Parser.getTok();
Kevin Enderbyc0edda32009-09-16 17:18:29 +0000382 if (Tok.isNot(AsmToken::Identifier))
383 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000384
Daniel Dunbar00331992009-07-29 00:02:19 +0000385 // FIXME: Validate register for the current architecture; we have to do
386 // validation later, so maybe there is no need for this here.
Kevin Enderby7d912182009-09-03 17:15:07 +0000387 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000388
Chris Lattner1261b812010-09-22 04:11:10 +0000389 // If the match failed, try the register name as lowercase.
390 if (RegNo == 0)
391 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000392
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000393 // FIXME: This should be done using Requires<In32BitMode> and
394 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
395 // can be also checked.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000396 if (RegNo == X86::RIZ && !is64BitMode())
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000397 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
398
Chris Lattner1261b812010-09-22 04:11:10 +0000399 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
400 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000401 RegNo = X86::ST0;
402 EndLoc = Tok.getLoc();
403 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000404
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000405 // Check to see if we have '(4)' after %st.
406 if (getLexer().isNot(AsmToken::LParen))
407 return false;
408 // Lex the paren.
409 getParser().Lex();
410
411 const AsmToken &IntTok = Parser.getTok();
412 if (IntTok.isNot(AsmToken::Integer))
413 return Error(IntTok.getLoc(), "expected stack index");
414 switch (IntTok.getIntVal()) {
415 case 0: RegNo = X86::ST0; break;
416 case 1: RegNo = X86::ST1; break;
417 case 2: RegNo = X86::ST2; break;
418 case 3: RegNo = X86::ST3; break;
419 case 4: RegNo = X86::ST4; break;
420 case 5: RegNo = X86::ST5; break;
421 case 6: RegNo = X86::ST6; break;
422 case 7: RegNo = X86::ST7; break;
423 default: return Error(IntTok.getLoc(), "invalid stack index");
424 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000425
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000426 if (getParser().Lex().isNot(AsmToken::RParen))
427 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000428
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000429 EndLoc = Tok.getLoc();
430 Parser.Lex(); // Eat ')'
431 return false;
432 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000433
Chris Lattner80486622010-06-24 07:29:18 +0000434 // If this is "db[0-7]", match it as an alias
435 // for dr[0-7].
436 if (RegNo == 0 && Tok.getString().size() == 3 &&
437 Tok.getString().startswith("db")) {
438 switch (Tok.getString()[2]) {
439 case '0': RegNo = X86::DR0; break;
440 case '1': RegNo = X86::DR1; break;
441 case '2': RegNo = X86::DR2; break;
442 case '3': RegNo = X86::DR3; break;
443 case '4': RegNo = X86::DR4; break;
444 case '5': RegNo = X86::DR5; break;
445 case '6': RegNo = X86::DR6; break;
446 case '7': RegNo = X86::DR7; break;
447 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000448
Chris Lattner80486622010-06-24 07:29:18 +0000449 if (RegNo != 0) {
450 EndLoc = Tok.getLoc();
451 Parser.Lex(); // Eat it.
452 return false;
453 }
454 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000455
Daniel Dunbar66f4f542009-08-08 21:22:41 +0000456 if (RegNo == 0)
Daniel Dunbar00331992009-07-29 00:02:19 +0000457 return Error(Tok.getLoc(), "invalid register name");
458
Chris Lattner0c2538f2010-01-15 18:51:29 +0000459 EndLoc = Tok.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000460 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000461 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000462}
463
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000464X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000465 switch (getLexer().getKind()) {
466 default:
Chris Lattnerb9270732010-04-17 18:56:34 +0000467 // Parse a memory operand with no segment register.
468 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +0000469 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +0000470 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +0000471 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000472 SMLoc Start, End;
473 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000474 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
475 Error(Start, "eiz and riz can only be used as index registers");
476 return 0;
477 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000478
Chris Lattnerb9270732010-04-17 18:56:34 +0000479 // If this is a segment register followed by a ':', then this is the start
480 // of a memory reference, otherwise this is a normal register reference.
481 if (getLexer().isNot(AsmToken::Colon))
482 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000483
484
Chris Lattnerb9270732010-04-17 18:56:34 +0000485 getParser().Lex(); // Eat the colon.
486 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +0000487 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000488 case AsmToken::Dollar: {
489 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +0000490 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000491 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000492 const MCExpr *Val;
Chris Lattnere17df0b2010-01-15 19:39:23 +0000493 if (getParser().ParseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000494 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +0000495 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000496 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000497 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000498}
499
Chris Lattnerb9270732010-04-17 18:56:34 +0000500/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
501/// has already been parsed if present.
502X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000503
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000504 // We have to disambiguate a parenthesized expression "(4+5)" from the start
505 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +0000506 // only way to do this without lookahead is to eat the '(' and see what is
507 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000508 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000509 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000510 SMLoc ExprEnd;
511 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000512
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000513 // After parsing the base expression we could either have a parenthesized
514 // memory address or not. If not, return now. If so, eat the (.
515 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000516 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000517 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000518 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +0000519 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000520 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000521
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000522 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +0000523 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000524 } else {
525 // Okay, we have a '('. We don't know if this is an expression or not, but
526 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +0000527 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000528 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000529
Kevin Enderby7d912182009-09-03 17:15:07 +0000530 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000531 // Nothing to do here, fall into the code below with the '(' part of the
532 // memory operand consumed.
533 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +0000534 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000535
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000536 // It must be an parenthesized expression, parse it now.
Chris Lattner528d00b2010-01-15 19:28:38 +0000537 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000538 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000539
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000540 // After parsing the base expression we could either have a parenthesized
541 // memory address or not. If not, return now. If so, eat the (.
542 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000543 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000544 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000545 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +0000546 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000547 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000548
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000549 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +0000550 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000551 }
552 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000553
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000554 // If we reached here, then we just ate the ( of the memory operand. Process
555 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000556 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000557
Chris Lattner0c2538f2010-01-15 18:51:29 +0000558 if (getLexer().is(AsmToken::Percent)) {
559 SMLoc L;
560 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000561 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
562 Error(L, "eiz and riz can only be used as index registers");
563 return 0;
564 }
Chris Lattner0c2538f2010-01-15 18:51:29 +0000565 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000566
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000567 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000568 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000569
570 // Following the comma we should have either an index register, or a scale
571 // value. We don't support the later form, but we want to parse it
572 // correctly.
573 //
574 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000575 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +0000576 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +0000577 SMLoc L;
578 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000579
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000580 if (getLexer().isNot(AsmToken::RParen)) {
581 // Parse the scale amount:
582 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000583 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000584 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000585 "expected comma in scale expression");
586 return 0;
587 }
Sean Callanana83fd7d2010-01-19 20:27:46 +0000588 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000589
590 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000591 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000592
593 int64_t ScaleVal;
594 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000595 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000596
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000597 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000598 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
599 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
600 return 0;
601 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000602 Scale = (unsigned)ScaleVal;
603 }
604 }
605 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +0000606 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000607 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +0000608 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000609
610 int64_t Value;
611 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000612 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000613
Daniel Dunbar94b84a12010-08-24 19:13:38 +0000614 if (Value != 1)
615 Warning(Loc, "scale factor without index register is ignored");
616 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000617 }
618 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000619
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000620 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000621 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000622 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000623 return 0;
624 }
Sean Callanan936b0d32010-01-19 21:44:56 +0000625 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000626 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000627
Chris Lattner015cfb12010-01-15 19:33:43 +0000628 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
629 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000630}
631
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000632bool X86ATTAsmParser::
Benjamin Kramer92d89982010-07-14 22:38:02 +0000633ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000634 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner2cb092d2010-10-30 19:23:13 +0000635 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000636
Chris Lattner7e8a99b2010-11-28 20:23:50 +0000637 // FIXME: Hack to recognize setneb as setne.
638 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
639 PatchedName != "setb" && PatchedName != "setnb")
640 PatchedName = PatchedName.substr(0, Name.size()-1);
641
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000642 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
643 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000644 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000645 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
646 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000647 bool IsVCMP = PatchedName.startswith("vcmp");
648 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000649 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000650 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +0000651 .Case("eq", 0)
652 .Case("lt", 1)
653 .Case("le", 2)
654 .Case("unord", 3)
655 .Case("neq", 4)
656 .Case("nlt", 5)
657 .Case("nle", 6)
658 .Case("ord", 7)
659 .Case("eq_uq", 8)
660 .Case("nge", 9)
661 .Case("ngt", 0x0A)
662 .Case("false", 0x0B)
663 .Case("neq_oq", 0x0C)
664 .Case("ge", 0x0D)
665 .Case("gt", 0x0E)
666 .Case("true", 0x0F)
667 .Case("eq_os", 0x10)
668 .Case("lt_oq", 0x11)
669 .Case("le_oq", 0x12)
670 .Case("unord_s", 0x13)
671 .Case("neq_us", 0x14)
672 .Case("nlt_uq", 0x15)
673 .Case("nle_uq", 0x16)
674 .Case("ord_s", 0x17)
675 .Case("eq_us", 0x18)
676 .Case("nge_uq", 0x19)
677 .Case("ngt_uq", 0x1A)
678 .Case("false_os", 0x1B)
679 .Case("neq_os", 0x1C)
680 .Case("ge_oq", 0x1D)
681 .Case("gt_oq", 0x1E)
682 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000683 .Default(~0U);
684 if (SSEComparisonCode != ~0U) {
685 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
686 getParser().getContext());
687 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000688 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000689 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000690 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000691 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000692 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000693 } else {
694 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000695 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000696 }
697 }
698 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +0000699
Daniel Dunbar3e0c9792010-02-10 21:19:28 +0000700 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000701
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000702 if (ExtraImmOp)
703 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000704
705
Chris Lattner086a83a2010-09-08 05:17:37 +0000706 // Determine whether this is an instruction prefix.
707 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +0000708 Name == "lock" || Name == "rep" ||
709 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +0000710 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +0000711 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +0000712
713
Chris Lattner086a83a2010-09-08 05:17:37 +0000714 // This does the actual operand parsing. Don't parse any more if we have a
715 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
716 // just want to parse the "lock" as the first instruction and the "incl" as
717 // the next one.
718 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +0000719
720 // Parse '*' modifier.
721 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000722 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +0000723 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +0000724 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +0000725 }
726
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000727 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000728 if (X86Operand *Op = ParseOperand())
729 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +0000730 else {
731 Parser.EatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000732 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +0000733 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000734
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000735 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000736 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000737
738 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000739 if (X86Operand *Op = ParseOperand())
740 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +0000741 else {
742 Parser.EatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000743 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +0000744 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000745 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000746
Chris Lattnera2a9d162010-09-11 16:18:25 +0000747 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +0000748 SMLoc Loc = getLexer().getLoc();
Chris Lattnera2a9d162010-09-11 16:18:25 +0000749 Parser.EatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +0000750 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +0000751 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000752 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000753
Chris Lattner086a83a2010-09-08 05:17:37 +0000754 if (getLexer().is(AsmToken::EndOfStatement))
755 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +0000756 else if (isPrefix && getLexer().is(AsmToken::Slash))
757 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000758
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000759 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
760 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
761 // documented form in various unofficial manuals, so a lot of code uses it.
762 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
763 Operands.size() == 3) {
764 X86Operand &Op = *(X86Operand*)Operands.back();
765 if (Op.isMem() && Op.Mem.SegReg == 0 &&
766 isa<MCConstantExpr>(Op.Mem.Disp) &&
767 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
768 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
769 SMLoc Loc = Op.getEndLoc();
770 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
771 delete &Op;
772 }
773 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +0000774 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
775 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
776 Operands.size() == 3) {
777 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
778 if (Op.isMem() && Op.Mem.SegReg == 0 &&
779 isa<MCConstantExpr>(Op.Mem.Disp) &&
780 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
781 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
782 SMLoc Loc = Op.getEndLoc();
783 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
784 delete &Op;
785 }
786 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000787 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
788 if (Name.startswith("ins") && Operands.size() == 3 &&
789 (Name == "insb" || Name == "insw" || Name == "insl")) {
790 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
791 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
792 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
793 Operands.pop_back();
794 Operands.pop_back();
795 delete &Op;
796 delete &Op2;
797 }
798 }
799
800 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
801 if (Name.startswith("outs") && Operands.size() == 3 &&
802 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
803 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
804 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
805 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
806 Operands.pop_back();
807 Operands.pop_back();
808 delete &Op;
809 delete &Op2;
810 }
811 }
812
813 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
814 if (Name.startswith("movs") && Operands.size() == 3 &&
815 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000816 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000817 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
818 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
819 if (isSrcOp(Op) && isDstOp(Op2)) {
820 Operands.pop_back();
821 Operands.pop_back();
822 delete &Op;
823 delete &Op2;
824 }
825 }
826 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
827 if (Name.startswith("lods") && Operands.size() == 3 &&
828 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000829 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000830 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
831 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
832 if (isSrcOp(*Op1) && Op2->isReg()) {
833 const char *ins;
834 unsigned reg = Op2->getReg();
835 bool isLods = Name == "lods";
836 if (reg == X86::AL && (isLods || Name == "lodsb"))
837 ins = "lodsb";
838 else if (reg == X86::AX && (isLods || Name == "lodsw"))
839 ins = "lodsw";
840 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
841 ins = "lodsl";
842 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
843 ins = "lodsq";
844 else
845 ins = NULL;
846 if (ins != NULL) {
847 Operands.pop_back();
848 Operands.pop_back();
849 delete Op1;
850 delete Op2;
851 if (Name != ins)
852 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
853 }
854 }
855 }
856 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
857 if (Name.startswith("stos") && Operands.size() == 3 &&
858 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000859 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000860 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
861 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
862 if (isDstOp(*Op2) && Op1->isReg()) {
863 const char *ins;
864 unsigned reg = Op1->getReg();
865 bool isStos = Name == "stos";
866 if (reg == X86::AL && (isStos || Name == "stosb"))
867 ins = "stosb";
868 else if (reg == X86::AX && (isStos || Name == "stosw"))
869 ins = "stosw";
870 else if (reg == X86::EAX && (isStos || Name == "stosl"))
871 ins = "stosl";
872 else if (reg == X86::RAX && (isStos || Name == "stosq"))
873 ins = "stosq";
874 else
875 ins = NULL;
876 if (ins != NULL) {
877 Operands.pop_back();
878 Operands.pop_back();
879 delete Op1;
880 delete Op2;
881 if (Name != ins)
882 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
883 }
884 }
885 }
886
Chris Lattner4bd21712010-09-15 04:33:27 +0000887 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +0000888 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +0000889 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +0000890 Name.startswith("shl") || Name.startswith("sal") ||
891 Name.startswith("rcl") || Name.startswith("rcr") ||
892 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +0000893 Operands.size() == 3) {
894 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
895 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
896 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
897 delete Operands[1];
898 Operands.erase(Operands.begin() + 1);
899 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +0000900 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +0000901
902 // Transforms "int $3" into "int3" as a size optimization. We can't write an
903 // instalias with an immediate operand yet.
904 if (Name == "int" && Operands.size() == 2) {
905 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
906 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
907 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
908 delete Operands[1];
909 Operands.erase(Operands.begin() + 1);
910 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
911 }
912 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000913
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000914 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000915}
916
Chris Lattner4dbcba02010-09-15 04:04:33 +0000917bool X86ATTAsmParser::
Chris Lattnerb44fd242010-09-29 01:42:58 +0000918MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattnera63292a2010-09-29 01:50:45 +0000919 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerb44fd242010-09-29 01:42:58 +0000920 MCStreamer &Out) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000921 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +0000922 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
923 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000924
Chris Lattnera63292a2010-09-29 01:50:45 +0000925 // First, handle aliases that expand to multiple instructions.
926 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner4869d342010-11-06 19:57:21 +0000927 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
928 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +0000929 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +0000930 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +0000931 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +0000932 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +0000933 MCInst Inst;
934 Inst.setOpcode(X86::WAIT);
935 Out.EmitInstruction(Inst);
936
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000937 const char *Repl =
938 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +0000939 .Case("finit", "fninit")
940 .Case("fsave", "fnsave")
941 .Case("fstcw", "fnstcw")
942 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +0000943 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +0000944 .Case("fstsw", "fnstsw")
945 .Case("fstsww", "fnstsw")
946 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000947 .Default(0);
948 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +0000949 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000950 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +0000951 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000952
Chris Lattner628fbec2010-09-06 21:54:15 +0000953 bool WasOriginallyInvalidOperand = false;
Chris Lattner339cc7b2010-09-06 22:11:18 +0000954 unsigned OrigErrorInfo;
Chris Lattnerb44fd242010-09-29 01:42:58 +0000955 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000956
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000957 // First, try a direct match.
Chris Lattner339cc7b2010-09-06 22:11:18 +0000958 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000959 case Match_Success:
Chris Lattnerb44fd242010-09-29 01:42:58 +0000960 Out.EmitInstruction(Inst);
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000961 return false;
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000962 case Match_MissingFeature:
963 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
964 return true;
Daniel Dunbar66193402011-02-04 17:12:23 +0000965 case Match_ConversionFail:
966 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattner628fbec2010-09-06 21:54:15 +0000967 case Match_InvalidOperand:
968 WasOriginallyInvalidOperand = true;
969 break;
970 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000971 break;
972 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000973
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000974 // FIXME: Ideally, we would only attempt suffix matches for things which are
975 // valid prefixes, and we could just infer the right unambiguous
976 // type. However, that requires substantially more matcher support than the
977 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000978
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000979 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000980 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000981 SmallString<16> Tmp;
982 Tmp += Base;
983 Tmp += ' ';
984 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000985
Chris Lattnerfab94132010-11-06 18:28:02 +0000986 // If this instruction starts with an 'f', then it is a floating point stack
987 // instruction. These come in up to three forms for 32-bit, 64-bit, and
988 // 80-bit floating point, which use the suffixes s,l,t respectively.
989 //
990 // Otherwise, we assume that this may be an integer instruction, which comes
991 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
992 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
993
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000994 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +0000995 Tmp[Base.size()] = Suffixes[0];
996 unsigned ErrorInfoIgnore;
997 MatchResultTy Match1, Match2, Match3, Match4;
998
999 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1000 Tmp[Base.size()] = Suffixes[1];
1001 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1002 Tmp[Base.size()] = Suffixes[2];
1003 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1004 Tmp[Base.size()] = Suffixes[3];
1005 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001006
1007 // Restore the old token.
1008 Op->setTokenValue(Base);
1009
1010 // If exactly one matched, then we treat that as a successful match (and the
1011 // instruction will already have been filled in correctly, since the failing
1012 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001013 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00001014 (Match1 == Match_Success) + (Match2 == Match_Success) +
1015 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00001016 if (NumSuccessfulMatches == 1) {
1017 Out.EmitInstruction(Inst);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001018 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00001019 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001020
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001021 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00001022
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001023 // If we had multiple suffix matches, then identify this as an ambiguous
1024 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001025 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001026 char MatchChars[4];
1027 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00001028 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1029 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1030 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1031 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001032
1033 SmallString<126> Msg;
1034 raw_svector_ostream OS(Msg);
1035 OS << "ambiguous instructions require an explicit suffix (could be ";
1036 for (unsigned i = 0; i != NumMatches; ++i) {
1037 if (i != 0)
1038 OS << ", ";
1039 if (i + 1 == NumMatches)
1040 OS << "or ";
1041 OS << "'" << Base << MatchChars[i] << "'";
1042 }
1043 OS << ")";
1044 Error(IDLoc, OS.str());
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001045 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001046 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001047
Chris Lattner628fbec2010-09-06 21:54:15 +00001048 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001049
Chris Lattner628fbec2010-09-06 21:54:15 +00001050 // If all of the instructions reported an invalid mnemonic, then the original
1051 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00001052 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1053 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00001054 if (!WasOriginallyInvalidOperand) {
Michael J. Spencer530ce852010-10-09 11:00:50 +00001055 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattner339cc7b2010-09-06 22:11:18 +00001056 return true;
1057 }
1058
1059 // Recover location info for the operand if we know which was the problem.
1060 SMLoc ErrorLoc = IDLoc;
1061 if (OrigErrorInfo != ~0U) {
Chris Lattnerd28452d2010-09-15 03:50:11 +00001062 if (OrigErrorInfo >= Operands.size())
1063 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001064
Chris Lattner339cc7b2010-09-06 22:11:18 +00001065 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1066 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1067 }
1068
Chris Lattnerd28452d2010-09-15 03:50:11 +00001069 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattner628fbec2010-09-06 21:54:15 +00001070 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001071
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001072 // If one instruction matched with a missing feature, report this as a
1073 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00001074 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1075 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001076 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1077 return true;
1078 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001079
Chris Lattner628fbec2010-09-06 21:54:15 +00001080 // If one instruction matched with an invalid operand, report this as an
1081 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00001082 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1083 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattner628fbec2010-09-06 21:54:15 +00001084 Error(IDLoc, "invalid operand for instruction");
1085 return true;
1086 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001087
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001088 // If all of these were an outright failure, report it in a useless way.
1089 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattner628fbec2010-09-06 21:54:15 +00001090 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001091 return true;
1092}
1093
1094
Chris Lattner72c0b592010-10-30 17:38:55 +00001095bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1096 StringRef IDVal = DirectiveID.getIdentifier();
1097 if (IDVal == ".word")
1098 return ParseDirectiveWord(2, DirectiveID.getLoc());
1099 return true;
1100}
1101
1102/// ParseDirectiveWord
1103/// ::= .word [ expression (, expression)* ]
1104bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1105 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1106 for (;;) {
1107 const MCExpr *Value;
1108 if (getParser().ParseExpression(Value))
1109 return true;
1110
1111 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1112
1113 if (getLexer().is(AsmToken::EndOfStatement))
1114 break;
1115
1116 // FIXME: Improve diagnostic.
1117 if (getLexer().isNot(AsmToken::Comma))
1118 return Error(L, "unexpected token in directive");
1119 Parser.Lex();
1120 }
1121 }
1122
1123 Parser.Lex();
1124 return false;
1125}
1126
1127
1128
1129
Sean Callanan5051cb82010-01-23 02:43:15 +00001130extern "C" void LLVMInitializeX86AsmLexer();
1131
Daniel Dunbar71475772009-07-17 20:42:00 +00001132// Force static initialization.
1133extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng4d1ca962011-07-08 01:53:10 +00001134 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1135 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5051cb82010-01-23 02:43:15 +00001136 LLVMInitializeX86AsmLexer();
Daniel Dunbar71475772009-07-17 20:42:00 +00001137}
Daniel Dunbar00331992009-07-29 00:02:19 +00001138
Chris Lattner3e4582a2010-09-06 19:11:01 +00001139#define GET_REGISTER_MATCHER
1140#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar00331992009-07-29 00:02:19 +00001141#include "X86GenAsmMatcher.inc"