blob: 5b66a0597225417926658ddbe88598383cf9e672 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner98986712010-01-14 22:21:20 +000010#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000011#include "X86.h"
Daniel Dunbar54074b52010-07-19 05:44:09 +000012#include "X86Subtarget.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000013#include "llvm/Target/TargetRegistry.h"
14#include "llvm/Target/TargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000015#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000017#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000018#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-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"
Chris Lattner33d60d52010-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 Dunbar16cdcb32009-07-28 22:40:46 +000027#include "llvm/Support/SourceMgr.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
30#define GET_SUBTARGETINFO_ENUM
31#include "X86GenSubtargetInfo.inc"
32
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000033using namespace llvm;
34
35namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000036struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037
38class X86ATTAsmParser : public TargetAsmParser {
39 MCAsmParser &Parser;
Evan Cheng480cee52011-07-08 19:33:14 +000040 const MCSubtargetInfo *STI;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000041
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000042private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000043 MCAsmParser &getParser() const { return Parser; }
44
45 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
46
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
48
Chris Lattner309264d2010-01-15 18:44:13 +000049 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000050 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000051
52 bool ParseDirectiveWord(unsigned Size, SMLoc L);
53
Chris Lattner7036f8b2010-09-29 01:42:58 +000054 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000055 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000056 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000057
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000058 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
59 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
60 bool isSrcOp(X86Operand &Op);
61
62 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
63 /// or %es:(%edi) in 32bit mode.
64 bool isDstOp(X86Operand &Op);
65
Evan Chengebdeeab2011-07-08 01:53:10 +000066 bool is64Bit() {
67 // FIXME: Can tablegen auto-generate this?
68 return (STI->getFeatureBits() & X86::Mode64Bit) != 0;
69 }
70
Daniel Dunbar54074b52010-07-19 05:44:09 +000071 /// @name Auto-generated Matcher Functions
72 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000073
Chris Lattner0692ee62010-09-06 19:11:01 +000074#define GET_ASSEMBLER_HEADER
75#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000076
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000077 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000078
79public:
Evan Cheng480cee52011-07-08 19:33:14 +000080 X86ATTAsmParser(StringRef TT, StringRef CPU, StringRef FS,
Evan Chengebdeeab2011-07-08 01:53:10 +000081 MCAsmParser &parser)
Evan Cheng480cee52011-07-08 19:33:14 +000082 : TargetAsmParser(), Parser(parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +000083 STI = X86_MC::createX86MCSubtargetInfo(TT, CPU, FS);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000084
Daniel Dunbar54074b52010-07-19 05:44:09 +000085 // Initialize the set of available features.
Evan Chengebdeeab2011-07-08 01:53:10 +000086 setAvailableFeatures(ComputeAvailableFeatures(STI->getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000087 }
Roman Divackybf755322011-01-27 17:14:22 +000088 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000089
Benjamin Kramer38e59892010-07-14 22:38:02 +000090 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000091 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000092
93 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000094};
Chris Lattner37dfdec2009-07-29 06:33:53 +000095} // end anonymous namespace
96
Sean Callanane9b466d2010-01-23 00:40:33 +000097/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +000098/// {
Sean Callanane9b466d2010-01-23 00:40:33 +000099
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000100static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000101
102/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000103
104namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000105
106/// X86Operand - Instances of this class represent a parsed X86 machine
107/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000108struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000109 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000110 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111 Register,
112 Immediate,
113 Memory
114 } Kind;
115
Chris Lattner29ef9a22010-01-15 18:51:29 +0000116 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000117
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000118 union {
119 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000120 const char *Data;
121 unsigned Length;
122 } Tok;
123
124 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000125 unsigned RegNo;
126 } Reg;
127
128 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000129 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000130 } Imm;
131
132 struct {
133 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000134 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000135 unsigned BaseReg;
136 unsigned IndexReg;
137 unsigned Scale;
138 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000139 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000140
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000141 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000142 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000143
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000144 /// getStartLoc - Get the location of the first token of this operand.
145 SMLoc getStartLoc() const { return StartLoc; }
146 /// getEndLoc - Get the location of the last token of this operand.
147 SMLoc getEndLoc() const { return EndLoc; }
148
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000149 virtual void dump(raw_ostream &OS) const {}
150
Daniel Dunbar20927f22009-08-07 08:26:05 +0000151 StringRef getToken() const {
152 assert(Kind == Token && "Invalid access!");
153 return StringRef(Tok.Data, Tok.Length);
154 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000155 void setTokenValue(StringRef Value) {
156 assert(Kind == Token && "Invalid access!");
157 Tok.Data = Value.data();
158 Tok.Length = Value.size();
159 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000160
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161 unsigned getReg() const {
162 assert(Kind == Register && "Invalid access!");
163 return Reg.RegNo;
164 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000165
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000166 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000167 assert(Kind == Immediate && "Invalid access!");
168 return Imm.Val;
169 }
170
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000171 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000172 assert(Kind == Memory && "Invalid access!");
173 return Mem.Disp;
174 }
175 unsigned getMemSegReg() const {
176 assert(Kind == Memory && "Invalid access!");
177 return Mem.SegReg;
178 }
179 unsigned getMemBaseReg() const {
180 assert(Kind == Memory && "Invalid access!");
181 return Mem.BaseReg;
182 }
183 unsigned getMemIndexReg() const {
184 assert(Kind == Memory && "Invalid access!");
185 return Mem.IndexReg;
186 }
187 unsigned getMemScale() const {
188 assert(Kind == Memory && "Invalid access!");
189 return Mem.Scale;
190 }
191
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000192 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000193
194 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000195
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000196 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000197 if (!isImm())
198 return false;
199
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000200 // If this isn't a constant expr, just assume it fits and let relaxation
201 // handle it.
202 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
203 if (!CE)
204 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000205
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000206 // Otherwise, check the value is in a range that makes sense for this
207 // extension.
208 uint64_t Value = CE->getValue();
209 return (( Value <= 0x000000000000007FULL)||
210 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
211 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000212 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000213 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000214 if (!isImm())
215 return false;
216
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000217 // If this isn't a constant expr, just assume it fits and let relaxation
218 // handle it.
219 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
220 if (!CE)
221 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000222
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000223 // Otherwise, check the value is in a range that makes sense for this
224 // extension.
225 uint64_t Value = CE->getValue();
226 return (( Value <= 0x000000000000007FULL)||
227 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
228 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
229 }
230 bool isImmSExti64i8() const {
231 if (!isImm())
232 return false;
233
234 // If this isn't a constant expr, just assume it fits and let relaxation
235 // handle it.
236 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
237 if (!CE)
238 return true;
239
240 // Otherwise, check the value is in a range that makes sense for this
241 // extension.
242 uint64_t Value = CE->getValue();
243 return (( Value <= 0x000000000000007FULL)||
244 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
245 }
246 bool isImmSExti64i32() const {
247 if (!isImm())
248 return false;
249
250 // If this isn't a constant expr, just assume it fits and let relaxation
251 // handle it.
252 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
253 if (!CE)
254 return true;
255
256 // Otherwise, check the value is in a range that makes sense for this
257 // extension.
258 uint64_t Value = CE->getValue();
259 return (( Value <= 0x000000007FFFFFFFULL)||
260 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000261 }
262
Daniel Dunbar20927f22009-08-07 08:26:05 +0000263 bool isMem() const { return Kind == Memory; }
264
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000265 bool isAbsMem() const {
266 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000267 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000268 }
269
Daniel Dunbar20927f22009-08-07 08:26:05 +0000270 bool isReg() const { return Kind == Register; }
271
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000272 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
273 // Add as immediates when possible.
274 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
275 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
276 else
277 Inst.addOperand(MCOperand::CreateExpr(Expr));
278 }
279
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000280 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000281 assert(N == 1 && "Invalid number of operands!");
282 Inst.addOperand(MCOperand::CreateReg(getReg()));
283 }
284
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000285 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000286 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000287 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000288 }
289
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000290 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000291 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000292 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
293 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
294 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000295 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000296 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
297 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000298
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000299 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
300 assert((N == 1) && "Invalid number of operands!");
301 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
302 }
303
Chris Lattnerb4307b32010-01-15 19:28:38 +0000304 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
305 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000306 Res->Tok.Data = Str.data();
307 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000308 return Res;
309 }
310
Chris Lattner29ef9a22010-01-15 18:51:29 +0000311 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000312 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000313 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000314 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000315 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000316
Chris Lattnerb4307b32010-01-15 19:28:38 +0000317 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
318 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000319 Res->Imm.Val = Val;
320 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000321 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000322
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000323 /// Create an absolute memory operand.
324 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
325 SMLoc EndLoc) {
326 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
327 Res->Mem.SegReg = 0;
328 Res->Mem.Disp = Disp;
329 Res->Mem.BaseReg = 0;
330 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000331 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000332 return Res;
333 }
334
335 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000336 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
337 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000338 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000339 // We should never just have a displacement, that should be parsed as an
340 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000341 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
342
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000343 // The scale should always be one of {1,2,4,8}.
344 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000345 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000346 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000347 Res->Mem.SegReg = SegReg;
348 Res->Mem.Disp = Disp;
349 Res->Mem.BaseReg = BaseReg;
350 Res->Mem.IndexReg = IndexReg;
351 Res->Mem.Scale = Scale;
352 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000353 }
354};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000355
Chris Lattner37dfdec2009-07-29 06:33:53 +0000356} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000357
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000358bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000359 unsigned basereg = is64Bit() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000360
361 return (Op.isMem() &&
362 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
363 isa<MCConstantExpr>(Op.Mem.Disp) &&
364 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
365 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
366}
367
368bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000369 unsigned basereg = is64Bit() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000370
371 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
372 isa<MCConstantExpr>(Op.Mem.Disp) &&
373 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
374 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
375}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000376
Chris Lattner29ef9a22010-01-15 18:51:29 +0000377bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
378 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000379 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000380 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000381 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000382 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000383 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000384
Sean Callanan18b83232010-01-19 21:44:56 +0000385 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000386 if (Tok.isNot(AsmToken::Identifier))
387 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000388
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000389 // FIXME: Validate register for the current architecture; we have to do
390 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000391 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000392
Chris Lattner33d60d52010-09-22 04:11:10 +0000393 // If the match failed, try the register name as lowercase.
394 if (RegNo == 0)
395 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000396
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000397 // FIXME: This should be done using Requires<In32BitMode> and
398 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
399 // can be also checked.
Evan Chengebdeeab2011-07-08 01:53:10 +0000400 if (RegNo == X86::RIZ && !is64Bit())
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000401 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
402
Chris Lattner33d60d52010-09-22 04:11:10 +0000403 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
404 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000405 RegNo = X86::ST0;
406 EndLoc = Tok.getLoc();
407 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000408
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000409 // Check to see if we have '(4)' after %st.
410 if (getLexer().isNot(AsmToken::LParen))
411 return false;
412 // Lex the paren.
413 getParser().Lex();
414
415 const AsmToken &IntTok = Parser.getTok();
416 if (IntTok.isNot(AsmToken::Integer))
417 return Error(IntTok.getLoc(), "expected stack index");
418 switch (IntTok.getIntVal()) {
419 case 0: RegNo = X86::ST0; break;
420 case 1: RegNo = X86::ST1; break;
421 case 2: RegNo = X86::ST2; break;
422 case 3: RegNo = X86::ST3; break;
423 case 4: RegNo = X86::ST4; break;
424 case 5: RegNo = X86::ST5; break;
425 case 6: RegNo = X86::ST6; break;
426 case 7: RegNo = X86::ST7; break;
427 default: return Error(IntTok.getLoc(), "invalid stack index");
428 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000429
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000430 if (getParser().Lex().isNot(AsmToken::RParen))
431 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000432
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000433 EndLoc = Tok.getLoc();
434 Parser.Lex(); // Eat ')'
435 return false;
436 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000437
Chris Lattner645b2092010-06-24 07:29:18 +0000438 // If this is "db[0-7]", match it as an alias
439 // for dr[0-7].
440 if (RegNo == 0 && Tok.getString().size() == 3 &&
441 Tok.getString().startswith("db")) {
442 switch (Tok.getString()[2]) {
443 case '0': RegNo = X86::DR0; break;
444 case '1': RegNo = X86::DR1; break;
445 case '2': RegNo = X86::DR2; break;
446 case '3': RegNo = X86::DR3; break;
447 case '4': RegNo = X86::DR4; break;
448 case '5': RegNo = X86::DR5; break;
449 case '6': RegNo = X86::DR6; break;
450 case '7': RegNo = X86::DR7; break;
451 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000452
Chris Lattner645b2092010-06-24 07:29:18 +0000453 if (RegNo != 0) {
454 EndLoc = Tok.getLoc();
455 Parser.Lex(); // Eat it.
456 return false;
457 }
458 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000459
Daniel Dunbar245f0582009-08-08 21:22:41 +0000460 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000461 return Error(Tok.getLoc(), "invalid register name");
462
Chris Lattner29ef9a22010-01-15 18:51:29 +0000463 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000464 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000465 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000466}
467
Chris Lattner309264d2010-01-15 18:44:13 +0000468X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000469 switch (getLexer().getKind()) {
470 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000471 // Parse a memory operand with no segment register.
472 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000473 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000474 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000475 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 SMLoc Start, End;
477 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000478 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
479 Error(Start, "eiz and riz can only be used as index registers");
480 return 0;
481 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000482
Chris Lattnereef6d782010-04-17 18:56:34 +0000483 // If this is a segment register followed by a ':', then this is the start
484 // of a memory reference, otherwise this is a normal register reference.
485 if (getLexer().isNot(AsmToken::Colon))
486 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000487
488
Chris Lattnereef6d782010-04-17 18:56:34 +0000489 getParser().Lex(); // Eat the colon.
490 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000491 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000492 case AsmToken::Dollar: {
493 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000494 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000495 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000496 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000497 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000498 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000499 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000500 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000501 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000502}
503
Chris Lattnereef6d782010-04-17 18:56:34 +0000504/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
505/// has already been parsed if present.
506X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000507
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000508 // We have to disambiguate a parenthesized expression "(4+5)" from the start
509 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000510 // only way to do this without lookahead is to eat the '(' and see what is
511 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000512 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000513 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000514 SMLoc ExprEnd;
515 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000516
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000517 // After parsing the base expression we could either have a parenthesized
518 // memory address or not. If not, return now. If so, eat the (.
519 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000520 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000521 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000522 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000523 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000524 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000525
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000526 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000527 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000528 } else {
529 // Okay, we have a '('. We don't know if this is an expression or not, but
530 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000531 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000532 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000533
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000534 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000535 // Nothing to do here, fall into the code below with the '(' part of the
536 // memory operand consumed.
537 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000538 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000539
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000540 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000541 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000542 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000543
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000544 // After parsing the base expression we could either have a parenthesized
545 // memory address or not. If not, return now. If so, eat the (.
546 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000547 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000548 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000549 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000550 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000551 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000552
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000553 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000554 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000555 }
556 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000557
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000558 // If we reached here, then we just ate the ( of the memory operand. Process
559 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000560 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000561
Chris Lattner29ef9a22010-01-15 18:51:29 +0000562 if (getLexer().is(AsmToken::Percent)) {
563 SMLoc L;
564 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000565 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
566 Error(L, "eiz and riz can only be used as index registers");
567 return 0;
568 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000569 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000570
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000571 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000573
574 // Following the comma we should have either an index register, or a scale
575 // value. We don't support the later form, but we want to parse it
576 // correctly.
577 //
578 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000579 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000580 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000581 SMLoc L;
582 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000583
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000584 if (getLexer().isNot(AsmToken::RParen)) {
585 // Parse the scale amount:
586 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000587 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000588 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000589 "expected comma in scale expression");
590 return 0;
591 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000592 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000593
594 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000595 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000596
597 int64_t ScaleVal;
598 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000599 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000600
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000601 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000602 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
603 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
604 return 0;
605 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000606 Scale = (unsigned)ScaleVal;
607 }
608 }
609 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000610 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000611 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000612 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000613
614 int64_t Value;
615 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000616 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000617
Daniel Dunbaree910252010-08-24 19:13:38 +0000618 if (Value != 1)
619 Warning(Loc, "scale factor without index register is ignored");
620 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000621 }
622 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000623
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000624 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000625 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000626 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000627 return 0;
628 }
Sean Callanan18b83232010-01-19 21:44:56 +0000629 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000630 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000631
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000632 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
633 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000634}
635
Chris Lattner98986712010-01-14 22:21:20 +0000636bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000637ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000638 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000639 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000640
Chris Lattnerd8f71792010-11-28 20:23:50 +0000641 // FIXME: Hack to recognize setneb as setne.
642 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
643 PatchedName != "setb" && PatchedName != "setnb")
644 PatchedName = PatchedName.substr(0, Name.size()-1);
645
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000646 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
647 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000648 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000649 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
650 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000651 bool IsVCMP = PatchedName.startswith("vcmp");
652 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000653 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000654 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000655 .Case("eq", 0)
656 .Case("lt", 1)
657 .Case("le", 2)
658 .Case("unord", 3)
659 .Case("neq", 4)
660 .Case("nlt", 5)
661 .Case("nle", 6)
662 .Case("ord", 7)
663 .Case("eq_uq", 8)
664 .Case("nge", 9)
665 .Case("ngt", 0x0A)
666 .Case("false", 0x0B)
667 .Case("neq_oq", 0x0C)
668 .Case("ge", 0x0D)
669 .Case("gt", 0x0E)
670 .Case("true", 0x0F)
671 .Case("eq_os", 0x10)
672 .Case("lt_oq", 0x11)
673 .Case("le_oq", 0x12)
674 .Case("unord_s", 0x13)
675 .Case("neq_us", 0x14)
676 .Case("nlt_uq", 0x15)
677 .Case("nle_uq", 0x16)
678 .Case("ord_s", 0x17)
679 .Case("eq_us", 0x18)
680 .Case("nge_uq", 0x19)
681 .Case("ngt_uq", 0x1A)
682 .Case("false_os", 0x1B)
683 .Case("neq_os", 0x1C)
684 .Case("ge_oq", 0x1D)
685 .Case("gt_oq", 0x1E)
686 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000687 .Default(~0U);
688 if (SSEComparisonCode != ~0U) {
689 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
690 getParser().getContext());
691 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000692 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000693 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000694 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000695 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000696 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000697 } else {
698 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000699 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000700 }
701 }
702 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000703
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000704 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000705
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000706 if (ExtraImmOp)
707 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000708
709
Chris Lattner2544f422010-09-08 05:17:37 +0000710 // Determine whether this is an instruction prefix.
711 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000712 Name == "lock" || Name == "rep" ||
713 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000714 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000715 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000716
717
Chris Lattner2544f422010-09-08 05:17:37 +0000718 // This does the actual operand parsing. Don't parse any more if we have a
719 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
720 // just want to parse the "lock" as the first instruction and the "incl" as
721 // the next one.
722 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000723
724 // Parse '*' modifier.
725 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000726 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000727 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000728 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000729 }
730
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000731 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000732 if (X86Operand *Op = ParseOperand())
733 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000734 else {
735 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000736 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000737 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000738
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000739 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000740 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000741
742 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000743 if (X86Operand *Op = ParseOperand())
744 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000745 else {
746 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000747 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000748 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000749 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000750
Chris Lattnercbf8a982010-09-11 16:18:25 +0000751 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000752 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000753 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000754 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000755 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000756 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000757
Chris Lattner2544f422010-09-08 05:17:37 +0000758 if (getLexer().is(AsmToken::EndOfStatement))
759 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000760 else if (isPrefix && getLexer().is(AsmToken::Slash))
761 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000762
Chris Lattner98c870f2010-11-06 19:25:43 +0000763 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
764 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
765 // documented form in various unofficial manuals, so a lot of code uses it.
766 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
767 Operands.size() == 3) {
768 X86Operand &Op = *(X86Operand*)Operands.back();
769 if (Op.isMem() && Op.Mem.SegReg == 0 &&
770 isa<MCConstantExpr>(Op.Mem.Disp) &&
771 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
772 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
773 SMLoc Loc = Op.getEndLoc();
774 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
775 delete &Op;
776 }
777 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000778 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
779 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
780 Operands.size() == 3) {
781 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
782 if (Op.isMem() && Op.Mem.SegReg == 0 &&
783 isa<MCConstantExpr>(Op.Mem.Disp) &&
784 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
785 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
786 SMLoc Loc = Op.getEndLoc();
787 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
788 delete &Op;
789 }
790 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000791 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
792 if (Name.startswith("ins") && Operands.size() == 3 &&
793 (Name == "insb" || Name == "insw" || Name == "insl")) {
794 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
795 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
796 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
797 Operands.pop_back();
798 Operands.pop_back();
799 delete &Op;
800 delete &Op2;
801 }
802 }
803
804 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
805 if (Name.startswith("outs") && Operands.size() == 3 &&
806 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
807 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
808 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
809 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
810 Operands.pop_back();
811 Operands.pop_back();
812 delete &Op;
813 delete &Op2;
814 }
815 }
816
817 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
818 if (Name.startswith("movs") && Operands.size() == 3 &&
819 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000820 (is64Bit() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000821 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
822 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
823 if (isSrcOp(Op) && isDstOp(Op2)) {
824 Operands.pop_back();
825 Operands.pop_back();
826 delete &Op;
827 delete &Op2;
828 }
829 }
830 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
831 if (Name.startswith("lods") && Operands.size() == 3 &&
832 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000833 Name == "lodsl" || (is64Bit() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000834 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
835 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
836 if (isSrcOp(*Op1) && Op2->isReg()) {
837 const char *ins;
838 unsigned reg = Op2->getReg();
839 bool isLods = Name == "lods";
840 if (reg == X86::AL && (isLods || Name == "lodsb"))
841 ins = "lodsb";
842 else if (reg == X86::AX && (isLods || Name == "lodsw"))
843 ins = "lodsw";
844 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
845 ins = "lodsl";
846 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
847 ins = "lodsq";
848 else
849 ins = NULL;
850 if (ins != NULL) {
851 Operands.pop_back();
852 Operands.pop_back();
853 delete Op1;
854 delete Op2;
855 if (Name != ins)
856 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
857 }
858 }
859 }
860 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
861 if (Name.startswith("stos") && Operands.size() == 3 &&
862 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000863 Name == "stosl" || (is64Bit() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000864 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
865 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
866 if (isDstOp(*Op2) && Op1->isReg()) {
867 const char *ins;
868 unsigned reg = Op1->getReg();
869 bool isStos = Name == "stos";
870 if (reg == X86::AL && (isStos || Name == "stosb"))
871 ins = "stosb";
872 else if (reg == X86::AX && (isStos || Name == "stosw"))
873 ins = "stosw";
874 else if (reg == X86::EAX && (isStos || Name == "stosl"))
875 ins = "stosl";
876 else if (reg == X86::RAX && (isStos || Name == "stosq"))
877 ins = "stosq";
878 else
879 ins = NULL;
880 if (ins != NULL) {
881 Operands.pop_back();
882 Operands.pop_back();
883 delete Op1;
884 delete Op2;
885 if (Name != ins)
886 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
887 }
888 }
889 }
890
Chris Lattnere9e16a32010-09-15 04:33:27 +0000891 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000892 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000893 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000894 Name.startswith("shl") || Name.startswith("sal") ||
895 Name.startswith("rcl") || Name.startswith("rcr") ||
896 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000897 Operands.size() == 3) {
898 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
899 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
900 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
901 delete Operands[1];
902 Operands.erase(Operands.begin() + 1);
903 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000904 }
Chris Lattner15f89512011-04-09 19:41:05 +0000905
906 // Transforms "int $3" into "int3" as a size optimization. We can't write an
907 // instalias with an immediate operand yet.
908 if (Name == "int" && Operands.size() == 2) {
909 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
910 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
911 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
912 delete Operands[1];
913 Operands.erase(Operands.begin() + 1);
914 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
915 }
916 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000917
Chris Lattner98986712010-01-14 22:21:20 +0000918 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000919}
920
Chris Lattner2d592d12010-09-15 04:04:33 +0000921bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000922MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000923 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000924 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000925 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000926 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
927 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000928
Chris Lattner7c51a312010-09-29 01:50:45 +0000929 // First, handle aliases that expand to multiple instructions.
930 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000931 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
932 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000933 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000934 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000935 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000936 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000937 MCInst Inst;
938 Inst.setOpcode(X86::WAIT);
939 Out.EmitInstruction(Inst);
940
Chris Lattner0bb83a82010-09-30 16:39:29 +0000941 const char *Repl =
942 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000943 .Case("finit", "fninit")
944 .Case("fsave", "fnsave")
945 .Case("fstcw", "fnstcw")
946 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000947 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000948 .Case("fstsw", "fnstsw")
949 .Case("fstsww", "fnstsw")
950 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000951 .Default(0);
952 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000953 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000954 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000955 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000956
Chris Lattnera008e8a2010-09-06 21:54:15 +0000957 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000958 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000959 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000960
Daniel Dunbarc918d602010-05-04 16:12:42 +0000961 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000962 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000963 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000964 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000965 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000966 case Match_MissingFeature:
967 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
968 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000969 case Match_ConversionFail:
970 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000971 case Match_InvalidOperand:
972 WasOriginallyInvalidOperand = true;
973 break;
974 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000975 break;
976 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000977
Daniel Dunbarc918d602010-05-04 16:12:42 +0000978 // FIXME: Ideally, we would only attempt suffix matches for things which are
979 // valid prefixes, and we could just infer the right unambiguous
980 // type. However, that requires substantially more matcher support than the
981 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000982
Daniel Dunbarc918d602010-05-04 16:12:42 +0000983 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000984 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000985 SmallString<16> Tmp;
986 Tmp += Base;
987 Tmp += ' ';
988 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +0000989
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000990 // If this instruction starts with an 'f', then it is a floating point stack
991 // instruction. These come in up to three forms for 32-bit, 64-bit, and
992 // 80-bit floating point, which use the suffixes s,l,t respectively.
993 //
994 // Otherwise, we assume that this may be an integer instruction, which comes
995 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
996 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
997
Daniel Dunbarc918d602010-05-04 16:12:42 +0000998 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000999 Tmp[Base.size()] = Suffixes[0];
1000 unsigned ErrorInfoIgnore;
1001 MatchResultTy Match1, Match2, Match3, Match4;
1002
1003 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1004 Tmp[Base.size()] = Suffixes[1];
1005 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1006 Tmp[Base.size()] = Suffixes[2];
1007 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1008 Tmp[Base.size()] = Suffixes[3];
1009 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001010
1011 // Restore the old token.
1012 Op->setTokenValue(Base);
1013
1014 // If exactly one matched, then we treat that as a successful match (and the
1015 // instruction will already have been filled in correctly, since the failing
1016 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001017 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001018 (Match1 == Match_Success) + (Match2 == Match_Success) +
1019 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001020 if (NumSuccessfulMatches == 1) {
1021 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001022 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001023 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001024
Chris Lattnerec6789f2010-09-06 20:08:02 +00001025 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001026
Daniel Dunbar09062b12010-08-12 00:55:42 +00001027 // If we had multiple suffix matches, then identify this as an ambiguous
1028 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001029 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001030 char MatchChars[4];
1031 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001032 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1033 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1034 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1035 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001036
1037 SmallString<126> Msg;
1038 raw_svector_ostream OS(Msg);
1039 OS << "ambiguous instructions require an explicit suffix (could be ";
1040 for (unsigned i = 0; i != NumMatches; ++i) {
1041 if (i != 0)
1042 OS << ", ";
1043 if (i + 1 == NumMatches)
1044 OS << "or ";
1045 OS << "'" << Base << MatchChars[i] << "'";
1046 }
1047 OS << ")";
1048 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001049 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001050 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001051
Chris Lattnera008e8a2010-09-06 21:54:15 +00001052 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001053
Chris Lattnera008e8a2010-09-06 21:54:15 +00001054 // If all of the instructions reported an invalid mnemonic, then the original
1055 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001056 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1057 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001058 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001059 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001060 return true;
1061 }
1062
1063 // Recover location info for the operand if we know which was the problem.
1064 SMLoc ErrorLoc = IDLoc;
1065 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001066 if (OrigErrorInfo >= Operands.size())
1067 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001068
Chris Lattnerce4a3352010-09-06 22:11:18 +00001069 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1070 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1071 }
1072
Chris Lattnerf8840122010-09-15 03:50:11 +00001073 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001074 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001075
Chris Lattnerec6789f2010-09-06 20:08:02 +00001076 // If one instruction matched with a missing feature, report this as a
1077 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001078 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1079 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001080 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1081 return true;
1082 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001083
Chris Lattnera008e8a2010-09-06 21:54:15 +00001084 // If one instruction matched with an invalid operand, report this as an
1085 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001086 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1087 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001088 Error(IDLoc, "invalid operand for instruction");
1089 return true;
1090 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001091
Chris Lattnerec6789f2010-09-06 20:08:02 +00001092 // If all of these were an outright failure, report it in a useless way.
1093 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001094 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001095 return true;
1096}
1097
1098
Chris Lattner537ca842010-10-30 17:38:55 +00001099bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1100 StringRef IDVal = DirectiveID.getIdentifier();
1101 if (IDVal == ".word")
1102 return ParseDirectiveWord(2, DirectiveID.getLoc());
1103 return true;
1104}
1105
1106/// ParseDirectiveWord
1107/// ::= .word [ expression (, expression)* ]
1108bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1109 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1110 for (;;) {
1111 const MCExpr *Value;
1112 if (getParser().ParseExpression(Value))
1113 return true;
1114
1115 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1116
1117 if (getLexer().is(AsmToken::EndOfStatement))
1118 break;
1119
1120 // FIXME: Improve diagnostic.
1121 if (getLexer().isNot(AsmToken::Comma))
1122 return Error(L, "unexpected token in directive");
1123 Parser.Lex();
1124 }
1125 }
1126
1127 Parser.Lex();
1128 return false;
1129}
1130
1131
1132
1133
Sean Callanane88f5522010-01-23 02:43:15 +00001134extern "C" void LLVMInitializeX86AsmLexer();
1135
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001136// Force static initialization.
1137extern "C" void LLVMInitializeX86AsmParser() {
Evan Chengebdeeab2011-07-08 01:53:10 +00001138 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1139 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001140 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001141}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001142
Chris Lattner0692ee62010-09-06 19:11:01 +00001143#define GET_REGISTER_MATCHER
1144#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001145#include "X86GenAsmMatcher.inc"