blob: d09661f7fdd652f45215d4f5502b5a4f667f173e [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
Evan Chengf2596bc2011-07-23 00:45:41 +000010#include "llvm/MC/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"
Kevin Enderbyce4bec82009-09-10 20:51:44 +000014#include "llvm/MC/MCStreamer.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbarb6d6aa22009-07-31 02:32:59 +000016#include "llvm/MC/MCInst.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerdebe69f2011-07-08 21:06:23 +000021#include "llvm/ADT/OwningPtr.h"
Chris Lattner1261b812010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringSwitch.h"
26#include "llvm/ADT/Twine.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000027#include "llvm/Support/SourceMgr.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000029
Daniel Dunbar71475772009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000034
35class X86ATTAsmParser : public TargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Michael J. Spencer530ce852010-10-09 11:00:50 +000038
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000039private:
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000044 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
Chris Lattnera2bbb7c2010-01-15 18:44:13 +000046 X86Operand *ParseOperand();
Chris Lattnerb9270732010-04-17 18:56:34 +000047 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000048
49 bool ParseDirectiveWord(unsigned Size, SMLoc L);
50
Chris Lattnerb44fd242010-09-29 01:42:58 +000051 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattnera63292a2010-09-29 01:50:45 +000052 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerb44fd242010-09-29 01:42:58 +000053 MCStreamer &Out);
Daniel Dunbare10787e2009-08-07 08:26:05 +000054
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000055 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
56 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
57 bool isSrcOp(X86Operand &Op);
58
59 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
60 /// or %es:(%edi) in 32bit mode.
61 bool isDstOp(X86Operand &Op);
62
Evan Chengc5e6d2f2011-07-11 03:57:24 +000063 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +000064 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +000065 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +000066 }
67
Daniel Dunbareefe8612010-07-19 05:44:09 +000068 /// @name Auto-generated Matcher Functions
69 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +000070
Chris Lattner3e4582a2010-09-06 19:11:01 +000071#define GET_ASSEMBLER_HEADER
72#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +000073
Daniel Dunbar00331992009-07-29 00:02:19 +000074 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000075
76public:
Evan Cheng91111d22011-07-09 05:47:46 +000077 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
78 : TargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencer530ce852010-10-09 11:00:50 +000079
Daniel Dunbareefe8612010-07-19 05:44:09 +000080 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +000081 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +000082 }
Roman Divacky36b1b472011-01-27 17:14:22 +000083 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000084
Benjamin Kramer92d89982010-07-14 22:38:02 +000085 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +000086 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000087
88 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000089};
Chris Lattner4eb9df02009-07-29 06:33:53 +000090} // end anonymous namespace
91
Sean Callanan86c11812010-01-23 00:40:33 +000092/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +000093/// {
Sean Callanan86c11812010-01-23 00:40:33 +000094
Chris Lattner60db0a62010-02-09 00:34:28 +000095static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +000096
97/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +000098
99namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000100
101/// X86Operand - Instances of this class represent a parsed X86 machine
102/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000103struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000104 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000105 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000106 Register,
107 Immediate,
108 Memory
109 } Kind;
110
Chris Lattner0c2538f2010-01-15 18:51:29 +0000111 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000112
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000113 union {
114 struct {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000115 const char *Data;
116 unsigned Length;
117 } Tok;
118
119 struct {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000120 unsigned RegNo;
121 } Reg;
122
123 struct {
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000124 const MCExpr *Val;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000125 } Imm;
126
127 struct {
128 unsigned SegReg;
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000129 const MCExpr *Disp;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000130 unsigned BaseReg;
131 unsigned IndexReg;
132 unsigned Scale;
133 } Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000134 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000135
Chris Lattner015cfb12010-01-15 19:33:43 +0000136 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000137 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000138
Chris Lattner86e61532010-01-15 19:06:59 +0000139 /// getStartLoc - Get the location of the first token of this operand.
140 SMLoc getStartLoc() const { return StartLoc; }
141 /// getEndLoc - Get the location of the last token of this operand.
142 SMLoc getEndLoc() const { return EndLoc; }
143
Jim Grosbach602aa902011-07-13 15:34:57 +0000144 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000145
Daniel Dunbare10787e2009-08-07 08:26:05 +0000146 StringRef getToken() const {
147 assert(Kind == Token && "Invalid access!");
148 return StringRef(Tok.Data, Tok.Length);
149 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000150 void setTokenValue(StringRef Value) {
151 assert(Kind == Token && "Invalid access!");
152 Tok.Data = Value.data();
153 Tok.Length = Value.size();
154 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000155
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000156 unsigned getReg() const {
157 assert(Kind == Register && "Invalid access!");
158 return Reg.RegNo;
159 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000160
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000161 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000162 assert(Kind == Immediate && "Invalid access!");
163 return Imm.Val;
164 }
165
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000166 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000167 assert(Kind == Memory && "Invalid access!");
168 return Mem.Disp;
169 }
170 unsigned getMemSegReg() const {
171 assert(Kind == Memory && "Invalid access!");
172 return Mem.SegReg;
173 }
174 unsigned getMemBaseReg() const {
175 assert(Kind == Memory && "Invalid access!");
176 return Mem.BaseReg;
177 }
178 unsigned getMemIndexReg() const {
179 assert(Kind == Memory && "Invalid access!");
180 return Mem.IndexReg;
181 }
182 unsigned getMemScale() const {
183 assert(Kind == Memory && "Invalid access!");
184 return Mem.Scale;
185 }
186
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000187 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000188
189 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000190
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000191 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000192 if (!isImm())
193 return false;
194
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000195 // If this isn't a constant expr, just assume it fits and let relaxation
196 // handle it.
197 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
198 if (!CE)
199 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000200
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000201 // Otherwise, check the value is in a range that makes sense for this
202 // extension.
203 uint64_t Value = CE->getValue();
204 return (( Value <= 0x000000000000007FULL)||
205 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
206 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000207 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000208 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000209 if (!isImm())
210 return false;
211
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000212 // If this isn't a constant expr, just assume it fits and let relaxation
213 // handle it.
214 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
215 if (!CE)
216 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000217
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000218 // Otherwise, check the value is in a range that makes sense for this
219 // extension.
220 uint64_t Value = CE->getValue();
221 return (( Value <= 0x000000000000007FULL)||
222 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
223 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
224 }
225 bool isImmSExti64i8() const {
226 if (!isImm())
227 return false;
228
229 // If this isn't a constant expr, just assume it fits and let relaxation
230 // handle it.
231 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
232 if (!CE)
233 return true;
234
235 // Otherwise, check the value is in a range that makes sense for this
236 // extension.
237 uint64_t Value = CE->getValue();
238 return (( Value <= 0x000000000000007FULL)||
239 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
240 }
241 bool isImmSExti64i32() const {
242 if (!isImm())
243 return false;
244
245 // If this isn't a constant expr, just assume it fits and let relaxation
246 // handle it.
247 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
248 if (!CE)
249 return true;
250
251 // Otherwise, check the value is in a range that makes sense for this
252 // extension.
253 uint64_t Value = CE->getValue();
254 return (( Value <= 0x000000007FFFFFFFULL)||
255 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000256 }
257
Daniel Dunbare10787e2009-08-07 08:26:05 +0000258 bool isMem() const { return Kind == Memory; }
259
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000260 bool isAbsMem() const {
261 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000262 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000263 }
264
Daniel Dunbare10787e2009-08-07 08:26:05 +0000265 bool isReg() const { return Kind == Register; }
266
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000267 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
268 // Add as immediates when possible.
269 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
270 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
271 else
272 Inst.addOperand(MCOperand::CreateExpr(Expr));
273 }
274
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000275 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000276 assert(N == 1 && "Invalid number of operands!");
277 Inst.addOperand(MCOperand::CreateReg(getReg()));
278 }
279
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000280 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000281 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000282 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000283 }
284
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000285 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000286 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000287 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
288 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
289 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000290 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000291 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
292 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000293
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000294 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
295 assert((N == 1) && "Invalid number of operands!");
296 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
297 }
298
Chris Lattner528d00b2010-01-15 19:28:38 +0000299 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
300 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000301 Res->Tok.Data = Str.data();
302 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000303 return Res;
304 }
305
Chris Lattner0c2538f2010-01-15 18:51:29 +0000306 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner86e61532010-01-15 19:06:59 +0000307 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000308 Res->Reg.RegNo = RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000309 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000310 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000311
Chris Lattner528d00b2010-01-15 19:28:38 +0000312 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
313 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000314 Res->Imm.Val = Val;
315 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000316 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000317
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000318 /// Create an absolute memory operand.
319 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
320 SMLoc EndLoc) {
321 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
322 Res->Mem.SegReg = 0;
323 Res->Mem.Disp = Disp;
324 Res->Mem.BaseReg = 0;
325 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000326 Res->Mem.Scale = 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000327 return Res;
328 }
329
330 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000331 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
332 unsigned BaseReg, unsigned IndexReg,
Chris Lattner015cfb12010-01-15 19:33:43 +0000333 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000334 // We should never just have a displacement, that should be parsed as an
335 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000336 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
337
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000338 // The scale should always be one of {1,2,4,8}.
339 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000340 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000341 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000342 Res->Mem.SegReg = SegReg;
343 Res->Mem.Disp = Disp;
344 Res->Mem.BaseReg = BaseReg;
345 Res->Mem.IndexReg = IndexReg;
346 Res->Mem.Scale = Scale;
347 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000348 }
349};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000350
Chris Lattner4eb9df02009-07-29 06:33:53 +0000351} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000352
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000353bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000354 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000355
356 return (Op.isMem() &&
357 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
358 isa<MCConstantExpr>(Op.Mem.Disp) &&
359 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
360 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
361}
362
363bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000364 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000365
366 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
367 isa<MCConstantExpr>(Op.Mem.Disp) &&
368 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
369 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
370}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000371
Chris Lattner0c2538f2010-01-15 18:51:29 +0000372bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
373 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000374 RegNo = 0;
Sean Callanan936b0d32010-01-19 21:44:56 +0000375 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7d912182009-09-03 17:15:07 +0000376 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner0c2538f2010-01-15 18:51:29 +0000377 StartLoc = TokPercent.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000378 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000379
Sean Callanan936b0d32010-01-19 21:44:56 +0000380 const AsmToken &Tok = Parser.getTok();
Kevin Enderbyc0edda32009-09-16 17:18:29 +0000381 if (Tok.isNot(AsmToken::Identifier))
382 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000383
Daniel Dunbar00331992009-07-29 00:02:19 +0000384 // FIXME: Validate register for the current architecture; we have to do
385 // validation later, so maybe there is no need for this here.
Kevin Enderby7d912182009-09-03 17:15:07 +0000386 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000387
Chris Lattner1261b812010-09-22 04:11:10 +0000388 // If the match failed, try the register name as lowercase.
389 if (RegNo == 0)
390 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000391
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000392 // FIXME: This should be done using Requires<In32BitMode> and
393 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
394 // can be also checked.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000395 if (RegNo == X86::RIZ && !is64BitMode())
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000396 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
397
Chris Lattner1261b812010-09-22 04:11:10 +0000398 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
399 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000400 RegNo = X86::ST0;
401 EndLoc = Tok.getLoc();
402 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000403
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000404 // Check to see if we have '(4)' after %st.
405 if (getLexer().isNot(AsmToken::LParen))
406 return false;
407 // Lex the paren.
408 getParser().Lex();
409
410 const AsmToken &IntTok = Parser.getTok();
411 if (IntTok.isNot(AsmToken::Integer))
412 return Error(IntTok.getLoc(), "expected stack index");
413 switch (IntTok.getIntVal()) {
414 case 0: RegNo = X86::ST0; break;
415 case 1: RegNo = X86::ST1; break;
416 case 2: RegNo = X86::ST2; break;
417 case 3: RegNo = X86::ST3; break;
418 case 4: RegNo = X86::ST4; break;
419 case 5: RegNo = X86::ST5; break;
420 case 6: RegNo = X86::ST6; break;
421 case 7: RegNo = X86::ST7; break;
422 default: return Error(IntTok.getLoc(), "invalid stack index");
423 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000424
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000425 if (getParser().Lex().isNot(AsmToken::RParen))
426 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000427
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000428 EndLoc = Tok.getLoc();
429 Parser.Lex(); // Eat ')'
430 return false;
431 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000432
Chris Lattner80486622010-06-24 07:29:18 +0000433 // If this is "db[0-7]", match it as an alias
434 // for dr[0-7].
435 if (RegNo == 0 && Tok.getString().size() == 3 &&
436 Tok.getString().startswith("db")) {
437 switch (Tok.getString()[2]) {
438 case '0': RegNo = X86::DR0; break;
439 case '1': RegNo = X86::DR1; break;
440 case '2': RegNo = X86::DR2; break;
441 case '3': RegNo = X86::DR3; break;
442 case '4': RegNo = X86::DR4; break;
443 case '5': RegNo = X86::DR5; break;
444 case '6': RegNo = X86::DR6; break;
445 case '7': RegNo = X86::DR7; break;
446 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000447
Chris Lattner80486622010-06-24 07:29:18 +0000448 if (RegNo != 0) {
449 EndLoc = Tok.getLoc();
450 Parser.Lex(); // Eat it.
451 return false;
452 }
453 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000454
Daniel Dunbar66f4f542009-08-08 21:22:41 +0000455 if (RegNo == 0)
Daniel Dunbar00331992009-07-29 00:02:19 +0000456 return Error(Tok.getLoc(), "invalid register name");
457
Chris Lattner0c2538f2010-01-15 18:51:29 +0000458 EndLoc = Tok.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000459 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000460 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000461}
462
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000463X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000464 switch (getLexer().getKind()) {
465 default:
Chris Lattnerb9270732010-04-17 18:56:34 +0000466 // Parse a memory operand with no segment register.
467 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +0000468 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +0000469 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +0000470 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000471 SMLoc Start, End;
472 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000473 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
474 Error(Start, "eiz and riz can only be used as index registers");
475 return 0;
476 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000477
Chris Lattnerb9270732010-04-17 18:56:34 +0000478 // If this is a segment register followed by a ':', then this is the start
479 // of a memory reference, otherwise this is a normal register reference.
480 if (getLexer().isNot(AsmToken::Colon))
481 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000482
483
Chris Lattnerb9270732010-04-17 18:56:34 +0000484 getParser().Lex(); // Eat the colon.
485 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +0000486 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000487 case AsmToken::Dollar: {
488 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +0000489 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000490 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000491 const MCExpr *Val;
Chris Lattnere17df0b2010-01-15 19:39:23 +0000492 if (getParser().ParseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000493 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +0000494 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000495 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000496 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000497}
498
Chris Lattnerb9270732010-04-17 18:56:34 +0000499/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
500/// has already been parsed if present.
501X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000502
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000503 // We have to disambiguate a parenthesized expression "(4+5)" from the start
504 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +0000505 // only way to do this without lookahead is to eat the '(' and see what is
506 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000507 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000508 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000509 SMLoc ExprEnd;
510 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000511
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000512 // After parsing the base expression we could either have a parenthesized
513 // memory address or not. If not, return now. If so, eat the (.
514 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000515 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000516 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000517 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +0000518 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000519 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000520
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000521 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +0000522 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000523 } else {
524 // Okay, we have a '('. We don't know if this is an expression or not, but
525 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +0000526 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000527 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000528
Kevin Enderby7d912182009-09-03 17:15:07 +0000529 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000530 // Nothing to do here, fall into the code below with the '(' part of the
531 // memory operand consumed.
532 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +0000533 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000534
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000535 // It must be an parenthesized expression, parse it now.
Chris Lattner528d00b2010-01-15 19:28:38 +0000536 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000537 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000538
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000539 // After parsing the base expression we could either have a parenthesized
540 // memory address or not. If not, return now. If so, eat the (.
541 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000542 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000543 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000544 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +0000545 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000546 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000547
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000548 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +0000549 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000550 }
551 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000552
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000553 // If we reached here, then we just ate the ( of the memory operand. Process
554 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000555 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000556
Chris Lattner0c2538f2010-01-15 18:51:29 +0000557 if (getLexer().is(AsmToken::Percent)) {
558 SMLoc L;
559 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000560 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
561 Error(L, "eiz and riz can only be used as index registers");
562 return 0;
563 }
Chris Lattner0c2538f2010-01-15 18:51:29 +0000564 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000565
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000566 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000567 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000568
569 // Following the comma we should have either an index register, or a scale
570 // value. We don't support the later form, but we want to parse it
571 // correctly.
572 //
573 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000574 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +0000575 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +0000576 SMLoc L;
577 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000578
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000579 if (getLexer().isNot(AsmToken::RParen)) {
580 // Parse the scale amount:
581 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000582 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000583 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000584 "expected comma in scale expression");
585 return 0;
586 }
Sean Callanana83fd7d2010-01-19 20:27:46 +0000587 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000588
589 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000590 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000591
592 int64_t ScaleVal;
593 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000594 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000595
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000596 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000597 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
598 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
599 return 0;
600 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000601 Scale = (unsigned)ScaleVal;
602 }
603 }
604 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +0000605 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000606 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +0000607 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000608
609 int64_t Value;
610 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000611 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000612
Daniel Dunbar94b84a12010-08-24 19:13:38 +0000613 if (Value != 1)
614 Warning(Loc, "scale factor without index register is ignored");
615 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000616 }
617 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000618
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000619 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000620 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000621 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000622 return 0;
623 }
Sean Callanan936b0d32010-01-19 21:44:56 +0000624 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000626
Chris Lattner015cfb12010-01-15 19:33:43 +0000627 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
628 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000629}
630
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000631bool X86ATTAsmParser::
Benjamin Kramer92d89982010-07-14 22:38:02 +0000632ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000633 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner2cb092d2010-10-30 19:23:13 +0000634 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000635
Chris Lattner7e8a99b2010-11-28 20:23:50 +0000636 // FIXME: Hack to recognize setneb as setne.
637 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
638 PatchedName != "setb" && PatchedName != "setnb")
639 PatchedName = PatchedName.substr(0, Name.size()-1);
640
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000641 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
642 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000643 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000644 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
645 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000646 bool IsVCMP = PatchedName.startswith("vcmp");
647 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000648 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000649 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +0000650 .Case("eq", 0)
651 .Case("lt", 1)
652 .Case("le", 2)
653 .Case("unord", 3)
654 .Case("neq", 4)
655 .Case("nlt", 5)
656 .Case("nle", 6)
657 .Case("ord", 7)
658 .Case("eq_uq", 8)
659 .Case("nge", 9)
660 .Case("ngt", 0x0A)
661 .Case("false", 0x0B)
662 .Case("neq_oq", 0x0C)
663 .Case("ge", 0x0D)
664 .Case("gt", 0x0E)
665 .Case("true", 0x0F)
666 .Case("eq_os", 0x10)
667 .Case("lt_oq", 0x11)
668 .Case("le_oq", 0x12)
669 .Case("unord_s", 0x13)
670 .Case("neq_us", 0x14)
671 .Case("nlt_uq", 0x15)
672 .Case("nle_uq", 0x16)
673 .Case("ord_s", 0x17)
674 .Case("eq_us", 0x18)
675 .Case("nge_uq", 0x19)
676 .Case("ngt_uq", 0x1A)
677 .Case("false_os", 0x1B)
678 .Case("neq_os", 0x1C)
679 .Case("ge_oq", 0x1D)
680 .Case("gt_oq", 0x1E)
681 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000682 .Default(~0U);
683 if (SSEComparisonCode != ~0U) {
684 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
685 getParser().getContext());
686 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000687 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000688 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000689 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000690 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000691 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000692 } else {
693 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +0000694 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000695 }
696 }
697 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +0000698
Daniel Dunbar3e0c9792010-02-10 21:19:28 +0000699 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000700
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000701 if (ExtraImmOp)
702 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000703
704
Chris Lattner086a83a2010-09-08 05:17:37 +0000705 // Determine whether this is an instruction prefix.
706 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +0000707 Name == "lock" || Name == "rep" ||
708 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +0000709 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +0000710 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +0000711
712
Chris Lattner086a83a2010-09-08 05:17:37 +0000713 // This does the actual operand parsing. Don't parse any more if we have a
714 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
715 // just want to parse the "lock" as the first instruction and the "incl" as
716 // the next one.
717 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +0000718
719 // Parse '*' modifier.
720 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000721 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +0000722 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +0000723 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +0000724 }
725
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000726 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000727 if (X86Operand *Op = ParseOperand())
728 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +0000729 else {
730 Parser.EatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000731 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +0000732 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +0000733
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000734 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000735 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000736
737 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000738 if (X86Operand *Op = ParseOperand())
739 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +0000740 else {
741 Parser.EatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000742 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +0000743 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000744 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000745
Chris Lattnera2a9d162010-09-11 16:18:25 +0000746 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +0000747 SMLoc Loc = getLexer().getLoc();
Chris Lattnera2a9d162010-09-11 16:18:25 +0000748 Parser.EatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +0000749 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +0000750 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000751 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000752
Chris Lattner086a83a2010-09-08 05:17:37 +0000753 if (getLexer().is(AsmToken::EndOfStatement))
754 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +0000755 else if (isPrefix && getLexer().is(AsmToken::Slash))
756 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000757
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000758 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
759 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
760 // documented form in various unofficial manuals, so a lot of code uses it.
761 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
762 Operands.size() == 3) {
763 X86Operand &Op = *(X86Operand*)Operands.back();
764 if (Op.isMem() && Op.Mem.SegReg == 0 &&
765 isa<MCConstantExpr>(Op.Mem.Disp) &&
766 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
767 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
768 SMLoc Loc = Op.getEndLoc();
769 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
770 delete &Op;
771 }
772 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +0000773 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
774 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
775 Operands.size() == 3) {
776 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
777 if (Op.isMem() && Op.Mem.SegReg == 0 &&
778 isa<MCConstantExpr>(Op.Mem.Disp) &&
779 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
780 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
781 SMLoc Loc = Op.getEndLoc();
782 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
783 delete &Op;
784 }
785 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000786 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
787 if (Name.startswith("ins") && Operands.size() == 3 &&
788 (Name == "insb" || Name == "insw" || Name == "insl")) {
789 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
790 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
791 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
792 Operands.pop_back();
793 Operands.pop_back();
794 delete &Op;
795 delete &Op2;
796 }
797 }
798
799 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
800 if (Name.startswith("outs") && Operands.size() == 3 &&
801 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
802 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
803 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
804 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
805 Operands.pop_back();
806 Operands.pop_back();
807 delete &Op;
808 delete &Op2;
809 }
810 }
811
812 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
813 if (Name.startswith("movs") && Operands.size() == 3 &&
814 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000815 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000816 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
817 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
818 if (isSrcOp(Op) && isDstOp(Op2)) {
819 Operands.pop_back();
820 Operands.pop_back();
821 delete &Op;
822 delete &Op2;
823 }
824 }
825 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
826 if (Name.startswith("lods") && Operands.size() == 3 &&
827 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000828 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000829 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
830 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
831 if (isSrcOp(*Op1) && Op2->isReg()) {
832 const char *ins;
833 unsigned reg = Op2->getReg();
834 bool isLods = Name == "lods";
835 if (reg == X86::AL && (isLods || Name == "lodsb"))
836 ins = "lodsb";
837 else if (reg == X86::AX && (isLods || Name == "lodsw"))
838 ins = "lodsw";
839 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
840 ins = "lodsl";
841 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
842 ins = "lodsq";
843 else
844 ins = NULL;
845 if (ins != NULL) {
846 Operands.pop_back();
847 Operands.pop_back();
848 delete Op1;
849 delete Op2;
850 if (Name != ins)
851 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
852 }
853 }
854 }
855 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
856 if (Name.startswith("stos") && Operands.size() == 3 &&
857 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000858 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000859 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
860 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
861 if (isDstOp(*Op2) && Op1->isReg()) {
862 const char *ins;
863 unsigned reg = Op1->getReg();
864 bool isStos = Name == "stos";
865 if (reg == X86::AL && (isStos || Name == "stosb"))
866 ins = "stosb";
867 else if (reg == X86::AX && (isStos || Name == "stosw"))
868 ins = "stosw";
869 else if (reg == X86::EAX && (isStos || Name == "stosl"))
870 ins = "stosl";
871 else if (reg == X86::RAX && (isStos || Name == "stosq"))
872 ins = "stosq";
873 else
874 ins = NULL;
875 if (ins != NULL) {
876 Operands.pop_back();
877 Operands.pop_back();
878 delete Op1;
879 delete Op2;
880 if (Name != ins)
881 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
882 }
883 }
884 }
885
Chris Lattner4bd21712010-09-15 04:33:27 +0000886 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +0000887 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +0000888 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +0000889 Name.startswith("shl") || Name.startswith("sal") ||
890 Name.startswith("rcl") || Name.startswith("rcr") ||
891 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +0000892 Operands.size() == 3) {
893 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
894 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
895 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
896 delete Operands[1];
897 Operands.erase(Operands.begin() + 1);
898 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +0000899 }
Chris Lattnerfc4fe002011-04-09 19:41:05 +0000900
901 // Transforms "int $3" into "int3" as a size optimization. We can't write an
902 // instalias with an immediate operand yet.
903 if (Name == "int" && Operands.size() == 2) {
904 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
905 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
906 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
907 delete Operands[1];
908 Operands.erase(Operands.begin() + 1);
909 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
910 }
911 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000912
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000913 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000914}
915
Chris Lattner4dbcba02010-09-15 04:04:33 +0000916bool X86ATTAsmParser::
Chris Lattnerb44fd242010-09-29 01:42:58 +0000917MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattnera63292a2010-09-29 01:50:45 +0000918 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerb44fd242010-09-29 01:42:58 +0000919 MCStreamer &Out) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000920 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +0000921 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
922 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000923
Chris Lattnera63292a2010-09-29 01:50:45 +0000924 // First, handle aliases that expand to multiple instructions.
925 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner4869d342010-11-06 19:57:21 +0000926 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
927 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +0000928 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +0000929 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +0000930 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +0000931 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +0000932 MCInst Inst;
933 Inst.setOpcode(X86::WAIT);
934 Out.EmitInstruction(Inst);
935
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000936 const char *Repl =
937 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +0000938 .Case("finit", "fninit")
939 .Case("fsave", "fnsave")
940 .Case("fstcw", "fnstcw")
941 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +0000942 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +0000943 .Case("fstsw", "fnstsw")
944 .Case("fstsww", "fnstsw")
945 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000946 .Default(0);
947 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +0000948 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +0000949 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +0000950 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000951
Chris Lattner628fbec2010-09-06 21:54:15 +0000952 bool WasOriginallyInvalidOperand = false;
Chris Lattner339cc7b2010-09-06 22:11:18 +0000953 unsigned OrigErrorInfo;
Chris Lattnerb44fd242010-09-29 01:42:58 +0000954 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000955
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000956 // First, try a direct match.
Chris Lattner339cc7b2010-09-06 22:11:18 +0000957 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000958 case Match_Success:
Chris Lattnerb44fd242010-09-29 01:42:58 +0000959 Out.EmitInstruction(Inst);
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000960 return false;
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000961 case Match_MissingFeature:
962 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
963 return true;
Daniel Dunbar66193402011-02-04 17:12:23 +0000964 case Match_ConversionFail:
965 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattner628fbec2010-09-06 21:54:15 +0000966 case Match_InvalidOperand:
967 WasOriginallyInvalidOperand = true;
968 break;
969 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +0000970 break;
971 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000972
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000973 // FIXME: Ideally, we would only attempt suffix matches for things which are
974 // valid prefixes, and we could just infer the right unambiguous
975 // type. However, that requires substantially more matcher support than the
976 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000977
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000978 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000979 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +0000980 SmallString<16> Tmp;
981 Tmp += Base;
982 Tmp += ' ';
983 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000984
Chris Lattnerfab94132010-11-06 18:28:02 +0000985 // If this instruction starts with an 'f', then it is a floating point stack
986 // instruction. These come in up to three forms for 32-bit, 64-bit, and
987 // 80-bit floating point, which use the suffixes s,l,t respectively.
988 //
989 // Otherwise, we assume that this may be an integer instruction, which comes
990 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
991 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
992
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000993 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +0000994 Tmp[Base.size()] = Suffixes[0];
995 unsigned ErrorInfoIgnore;
996 MatchResultTy Match1, Match2, Match3, Match4;
997
998 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
999 Tmp[Base.size()] = Suffixes[1];
1000 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1001 Tmp[Base.size()] = Suffixes[2];
1002 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1003 Tmp[Base.size()] = Suffixes[3];
1004 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001005
1006 // Restore the old token.
1007 Op->setTokenValue(Base);
1008
1009 // If exactly one matched, then we treat that as a successful match (and the
1010 // instruction will already have been filled in correctly, since the failing
1011 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001012 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00001013 (Match1 == Match_Success) + (Match2 == Match_Success) +
1014 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00001015 if (NumSuccessfulMatches == 1) {
1016 Out.EmitInstruction(Inst);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001017 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00001018 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001019
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001020 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00001021
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001022 // If we had multiple suffix matches, then identify this as an ambiguous
1023 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001024 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001025 char MatchChars[4];
1026 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00001027 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1028 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1029 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1030 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001031
1032 SmallString<126> Msg;
1033 raw_svector_ostream OS(Msg);
1034 OS << "ambiguous instructions require an explicit suffix (could be ";
1035 for (unsigned i = 0; i != NumMatches; ++i) {
1036 if (i != 0)
1037 OS << ", ";
1038 if (i + 1 == NumMatches)
1039 OS << "or ";
1040 OS << "'" << Base << MatchChars[i] << "'";
1041 }
1042 OS << ")";
1043 Error(IDLoc, OS.str());
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001044 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00001045 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001046
Chris Lattner628fbec2010-09-06 21:54:15 +00001047 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001048
Chris Lattner628fbec2010-09-06 21:54:15 +00001049 // If all of the instructions reported an invalid mnemonic, then the original
1050 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00001051 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1052 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00001053 if (!WasOriginallyInvalidOperand) {
Michael J. Spencer530ce852010-10-09 11:00:50 +00001054 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattner339cc7b2010-09-06 22:11:18 +00001055 return true;
1056 }
1057
1058 // Recover location info for the operand if we know which was the problem.
1059 SMLoc ErrorLoc = IDLoc;
1060 if (OrigErrorInfo != ~0U) {
Chris Lattnerd28452d2010-09-15 03:50:11 +00001061 if (OrigErrorInfo >= Operands.size())
1062 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001063
Chris Lattner339cc7b2010-09-06 22:11:18 +00001064 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1065 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1066 }
1067
Chris Lattnerd28452d2010-09-15 03:50:11 +00001068 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattner628fbec2010-09-06 21:54:15 +00001069 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001071 // If one instruction matched with a missing feature, report this as a
1072 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00001073 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1074 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001075 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1076 return true;
1077 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001078
Chris Lattner628fbec2010-09-06 21:54:15 +00001079 // If one instruction matched with an invalid operand, report this as an
1080 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00001081 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1082 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattner628fbec2010-09-06 21:54:15 +00001083 Error(IDLoc, "invalid operand for instruction");
1084 return true;
1085 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001086
Chris Lattnerb4be28f2010-09-06 20:08:02 +00001087 // If all of these were an outright failure, report it in a useless way.
1088 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattner628fbec2010-09-06 21:54:15 +00001089 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbar9b816a12010-05-04 16:12:42 +00001090 return true;
1091}
1092
1093
Chris Lattner72c0b592010-10-30 17:38:55 +00001094bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1095 StringRef IDVal = DirectiveID.getIdentifier();
1096 if (IDVal == ".word")
1097 return ParseDirectiveWord(2, DirectiveID.getLoc());
1098 return true;
1099}
1100
1101/// ParseDirectiveWord
1102/// ::= .word [ expression (, expression)* ]
1103bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1104 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1105 for (;;) {
1106 const MCExpr *Value;
1107 if (getParser().ParseExpression(Value))
1108 return true;
1109
1110 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1111
1112 if (getLexer().is(AsmToken::EndOfStatement))
1113 break;
1114
1115 // FIXME: Improve diagnostic.
1116 if (getLexer().isNot(AsmToken::Comma))
1117 return Error(L, "unexpected token in directive");
1118 Parser.Lex();
1119 }
1120 }
1121
1122 Parser.Lex();
1123 return false;
1124}
1125
1126
1127
1128
Sean Callanan5051cb82010-01-23 02:43:15 +00001129extern "C" void LLVMInitializeX86AsmLexer();
1130
Daniel Dunbar71475772009-07-17 20:42:00 +00001131// Force static initialization.
1132extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng4d1ca962011-07-08 01:53:10 +00001133 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1134 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5051cb82010-01-23 02:43:15 +00001135 LLVMInitializeX86AsmLexer();
Daniel Dunbar71475772009-07-17 20:42:00 +00001136}
Daniel Dunbar00331992009-07-29 00:02:19 +00001137
Chris Lattner3e4582a2010-09-06 19:11:01 +00001138#define GET_REGISTER_MATCHER
1139#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar00331992009-07-29 00:02:19 +00001140#include "X86GenAsmMatcher.inc"