blob: db6ab33ed68ef125208c05a2f3bd4cdebf13ea00 [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"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000028#include "llvm/Support/SourceMgr.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000030
31#define GET_SUBTARGETINFO_ENUM
32#include "X86GenSubtargetInfo.inc"
33
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000034using namespace llvm;
35
36namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000037struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038
39class X86ATTAsmParser : public TargetAsmParser {
40 MCAsmParser &Parser;
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000041 OwningPtr<const MCSubtargetInfo> STI;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000042
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000043private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000044 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Chris Lattner309264d2010-01-15 18:44:13 +000050 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000051 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000052
53 bool ParseDirectiveWord(unsigned Size, SMLoc L);
54
Chris Lattner7036f8b2010-09-29 01:42:58 +000055 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000056 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000057 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000058
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000059 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
60 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
61 bool isSrcOp(X86Operand &Op);
62
63 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
64 /// or %es:(%edi) in 32bit mode.
65 bool isDstOp(X86Operand &Op);
66
Evan Chengebdeeab2011-07-08 01:53:10 +000067 bool is64Bit() {
68 // FIXME: Can tablegen auto-generate this?
69 return (STI->getFeatureBits() & X86::Mode64Bit) != 0;
70 }
71
Daniel Dunbar54074b52010-07-19 05:44:09 +000072 /// @name Auto-generated Matcher Functions
73 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000074
Chris Lattner0692ee62010-09-06 19:11:01 +000075#define GET_ASSEMBLER_HEADER
76#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000077
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000078 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000079
80public:
Evan Cheng480cee52011-07-08 19:33:14 +000081 X86ATTAsmParser(StringRef TT, StringRef CPU, StringRef FS,
Evan Chengebdeeab2011-07-08 01:53:10 +000082 MCAsmParser &parser)
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000083 : TargetAsmParser(), Parser(parser),
84 STI(X86_MC::createX86MCSubtargetInfo(TT, CPU, FS)) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000085
Daniel Dunbar54074b52010-07-19 05:44:09 +000086 // Initialize the set of available features.
Evan Chengebdeeab2011-07-08 01:53:10 +000087 setAvailableFeatures(ComputeAvailableFeatures(STI->getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000088 }
Roman Divackybf755322011-01-27 17:14:22 +000089 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000090
Benjamin Kramer38e59892010-07-14 22:38:02 +000091 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000092 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000093
94 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000095};
Chris Lattner37dfdec2009-07-29 06:33:53 +000096} // end anonymous namespace
97
Sean Callanane9b466d2010-01-23 00:40:33 +000098/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +000099/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000100
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000101static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000102
103/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000104
105namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000106
107/// X86Operand - Instances of this class represent a parsed X86 machine
108/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000109struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000110 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000111 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112 Register,
113 Immediate,
114 Memory
115 } Kind;
116
Chris Lattner29ef9a22010-01-15 18:51:29 +0000117 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000118
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000119 union {
120 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000121 const char *Data;
122 unsigned Length;
123 } Tok;
124
125 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000126 unsigned RegNo;
127 } Reg;
128
129 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000130 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000131 } Imm;
132
133 struct {
134 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000135 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000136 unsigned BaseReg;
137 unsigned IndexReg;
138 unsigned Scale;
139 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000140 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000141
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000142 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000143 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000144
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000145 /// getStartLoc - Get the location of the first token of this operand.
146 SMLoc getStartLoc() const { return StartLoc; }
147 /// getEndLoc - Get the location of the last token of this operand.
148 SMLoc getEndLoc() const { return EndLoc; }
149
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000150 virtual void dump(raw_ostream &OS) const {}
151
Daniel Dunbar20927f22009-08-07 08:26:05 +0000152 StringRef getToken() const {
153 assert(Kind == Token && "Invalid access!");
154 return StringRef(Tok.Data, Tok.Length);
155 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000156 void setTokenValue(StringRef Value) {
157 assert(Kind == Token && "Invalid access!");
158 Tok.Data = Value.data();
159 Tok.Length = Value.size();
160 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000161
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000162 unsigned getReg() const {
163 assert(Kind == Register && "Invalid access!");
164 return Reg.RegNo;
165 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000166
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000167 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000168 assert(Kind == Immediate && "Invalid access!");
169 return Imm.Val;
170 }
171
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000172 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000173 assert(Kind == Memory && "Invalid access!");
174 return Mem.Disp;
175 }
176 unsigned getMemSegReg() const {
177 assert(Kind == Memory && "Invalid access!");
178 return Mem.SegReg;
179 }
180 unsigned getMemBaseReg() const {
181 assert(Kind == Memory && "Invalid access!");
182 return Mem.BaseReg;
183 }
184 unsigned getMemIndexReg() const {
185 assert(Kind == Memory && "Invalid access!");
186 return Mem.IndexReg;
187 }
188 unsigned getMemScale() const {
189 assert(Kind == Memory && "Invalid access!");
190 return Mem.Scale;
191 }
192
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000193 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000194
195 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000196
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000197 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000198 if (!isImm())
199 return false;
200
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000201 // If this isn't a constant expr, just assume it fits and let relaxation
202 // handle it.
203 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
204 if (!CE)
205 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000206
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000207 // Otherwise, check the value is in a range that makes sense for this
208 // extension.
209 uint64_t Value = CE->getValue();
210 return (( Value <= 0x000000000000007FULL)||
211 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
212 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000213 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000214 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000215 if (!isImm())
216 return false;
217
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000218 // If this isn't a constant expr, just assume it fits and let relaxation
219 // handle it.
220 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
221 if (!CE)
222 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000223
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000224 // Otherwise, check the value is in a range that makes sense for this
225 // extension.
226 uint64_t Value = CE->getValue();
227 return (( Value <= 0x000000000000007FULL)||
228 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
229 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
230 }
231 bool isImmSExti64i8() const {
232 if (!isImm())
233 return false;
234
235 // If this isn't a constant expr, just assume it fits and let relaxation
236 // handle it.
237 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
238 if (!CE)
239 return true;
240
241 // Otherwise, check the value is in a range that makes sense for this
242 // extension.
243 uint64_t Value = CE->getValue();
244 return (( Value <= 0x000000000000007FULL)||
245 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
246 }
247 bool isImmSExti64i32() const {
248 if (!isImm())
249 return false;
250
251 // If this isn't a constant expr, just assume it fits and let relaxation
252 // handle it.
253 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
254 if (!CE)
255 return true;
256
257 // Otherwise, check the value is in a range that makes sense for this
258 // extension.
259 uint64_t Value = CE->getValue();
260 return (( Value <= 0x000000007FFFFFFFULL)||
261 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000262 }
263
Daniel Dunbar20927f22009-08-07 08:26:05 +0000264 bool isMem() const { return Kind == Memory; }
265
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000266 bool isAbsMem() const {
267 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000268 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000269 }
270
Daniel Dunbar20927f22009-08-07 08:26:05 +0000271 bool isReg() const { return Kind == Register; }
272
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000273 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
274 // Add as immediates when possible.
275 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
276 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
277 else
278 Inst.addOperand(MCOperand::CreateExpr(Expr));
279 }
280
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000281 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000282 assert(N == 1 && "Invalid number of operands!");
283 Inst.addOperand(MCOperand::CreateReg(getReg()));
284 }
285
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000286 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000287 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000288 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000289 }
290
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000291 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000292 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000293 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
294 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
295 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000296 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000297 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
298 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000299
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000300 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
301 assert((N == 1) && "Invalid number of operands!");
302 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
303 }
304
Chris Lattnerb4307b32010-01-15 19:28:38 +0000305 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
306 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000307 Res->Tok.Data = Str.data();
308 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000309 return Res;
310 }
311
Chris Lattner29ef9a22010-01-15 18:51:29 +0000312 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000313 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000314 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000315 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000316 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000317
Chris Lattnerb4307b32010-01-15 19:28:38 +0000318 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
319 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000320 Res->Imm.Val = Val;
321 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000322 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000323
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000324 /// Create an absolute memory operand.
325 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
326 SMLoc EndLoc) {
327 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
328 Res->Mem.SegReg = 0;
329 Res->Mem.Disp = Disp;
330 Res->Mem.BaseReg = 0;
331 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000332 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000333 return Res;
334 }
335
336 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000337 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
338 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000339 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000340 // We should never just have a displacement, that should be parsed as an
341 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000342 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
343
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000344 // The scale should always be one of {1,2,4,8}.
345 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000346 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000347 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000348 Res->Mem.SegReg = SegReg;
349 Res->Mem.Disp = Disp;
350 Res->Mem.BaseReg = BaseReg;
351 Res->Mem.IndexReg = IndexReg;
352 Res->Mem.Scale = Scale;
353 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000354 }
355};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000356
Chris Lattner37dfdec2009-07-29 06:33:53 +0000357} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000358
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000359bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000360 unsigned basereg = is64Bit() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000361
362 return (Op.isMem() &&
363 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
364 isa<MCConstantExpr>(Op.Mem.Disp) &&
365 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
366 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
367}
368
369bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000370 unsigned basereg = is64Bit() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000371
372 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
373 isa<MCConstantExpr>(Op.Mem.Disp) &&
374 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
375 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
376}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000377
Chris Lattner29ef9a22010-01-15 18:51:29 +0000378bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
379 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000380 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000381 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000382 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000383 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000384 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000385
Sean Callanan18b83232010-01-19 21:44:56 +0000386 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000387 if (Tok.isNot(AsmToken::Identifier))
388 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000389
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000390 // FIXME: Validate register for the current architecture; we have to do
391 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000392 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000393
Chris Lattner33d60d52010-09-22 04:11:10 +0000394 // If the match failed, try the register name as lowercase.
395 if (RegNo == 0)
396 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000397
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000398 // FIXME: This should be done using Requires<In32BitMode> and
399 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
400 // can be also checked.
Evan Chengebdeeab2011-07-08 01:53:10 +0000401 if (RegNo == X86::RIZ && !is64Bit())
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000402 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
403
Chris Lattner33d60d52010-09-22 04:11:10 +0000404 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
405 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000406 RegNo = X86::ST0;
407 EndLoc = Tok.getLoc();
408 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000409
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000410 // Check to see if we have '(4)' after %st.
411 if (getLexer().isNot(AsmToken::LParen))
412 return false;
413 // Lex the paren.
414 getParser().Lex();
415
416 const AsmToken &IntTok = Parser.getTok();
417 if (IntTok.isNot(AsmToken::Integer))
418 return Error(IntTok.getLoc(), "expected stack index");
419 switch (IntTok.getIntVal()) {
420 case 0: RegNo = X86::ST0; break;
421 case 1: RegNo = X86::ST1; break;
422 case 2: RegNo = X86::ST2; break;
423 case 3: RegNo = X86::ST3; break;
424 case 4: RegNo = X86::ST4; break;
425 case 5: RegNo = X86::ST5; break;
426 case 6: RegNo = X86::ST6; break;
427 case 7: RegNo = X86::ST7; break;
428 default: return Error(IntTok.getLoc(), "invalid stack index");
429 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000430
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000431 if (getParser().Lex().isNot(AsmToken::RParen))
432 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000433
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000434 EndLoc = Tok.getLoc();
435 Parser.Lex(); // Eat ')'
436 return false;
437 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000438
Chris Lattner645b2092010-06-24 07:29:18 +0000439 // If this is "db[0-7]", match it as an alias
440 // for dr[0-7].
441 if (RegNo == 0 && Tok.getString().size() == 3 &&
442 Tok.getString().startswith("db")) {
443 switch (Tok.getString()[2]) {
444 case '0': RegNo = X86::DR0; break;
445 case '1': RegNo = X86::DR1; break;
446 case '2': RegNo = X86::DR2; break;
447 case '3': RegNo = X86::DR3; break;
448 case '4': RegNo = X86::DR4; break;
449 case '5': RegNo = X86::DR5; break;
450 case '6': RegNo = X86::DR6; break;
451 case '7': RegNo = X86::DR7; break;
452 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000453
Chris Lattner645b2092010-06-24 07:29:18 +0000454 if (RegNo != 0) {
455 EndLoc = Tok.getLoc();
456 Parser.Lex(); // Eat it.
457 return false;
458 }
459 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000460
Daniel Dunbar245f0582009-08-08 21:22:41 +0000461 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000462 return Error(Tok.getLoc(), "invalid register name");
463
Chris Lattner29ef9a22010-01-15 18:51:29 +0000464 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000465 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000466 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000467}
468
Chris Lattner309264d2010-01-15 18:44:13 +0000469X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000470 switch (getLexer().getKind()) {
471 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000472 // Parse a memory operand with no segment register.
473 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000474 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000475 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000476 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000477 SMLoc Start, End;
478 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000479 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
480 Error(Start, "eiz and riz can only be used as index registers");
481 return 0;
482 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000483
Chris Lattnereef6d782010-04-17 18:56:34 +0000484 // If this is a segment register followed by a ':', then this is the start
485 // of a memory reference, otherwise this is a normal register reference.
486 if (getLexer().isNot(AsmToken::Colon))
487 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000488
489
Chris Lattnereef6d782010-04-17 18:56:34 +0000490 getParser().Lex(); // Eat the colon.
491 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000492 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000493 case AsmToken::Dollar: {
494 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000495 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000496 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000497 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000498 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000499 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000500 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000501 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000502 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000503}
504
Chris Lattnereef6d782010-04-17 18:56:34 +0000505/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
506/// has already been parsed if present.
507X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000508
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000509 // We have to disambiguate a parenthesized expression "(4+5)" from the start
510 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000511 // only way to do this without lookahead is to eat the '(' and see what is
512 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000513 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000514 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000515 SMLoc ExprEnd;
516 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000517
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000518 // After parsing the base expression we could either have a parenthesized
519 // memory address or not. If not, return now. If so, eat the (.
520 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000521 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000522 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000523 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000524 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000525 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000526
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000528 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000529 } else {
530 // Okay, we have a '('. We don't know if this is an expression or not, but
531 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000532 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000533 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000534
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000535 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000536 // Nothing to do here, fall into the code below with the '(' part of the
537 // memory operand consumed.
538 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000540
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000541 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000542 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000543 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000544
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000545 // After parsing the base expression we could either have a parenthesized
546 // memory address or not. If not, return now. If so, eat the (.
547 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000548 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000549 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000550 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000551 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000553
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000555 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000556 }
557 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000558
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000559 // If we reached here, then we just ate the ( of the memory operand. Process
560 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000561 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000562
Chris Lattner29ef9a22010-01-15 18:51:29 +0000563 if (getLexer().is(AsmToken::Percent)) {
564 SMLoc L;
565 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000566 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
567 Error(L, "eiz and riz can only be used as index registers");
568 return 0;
569 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000570 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000571
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000572 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000573 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000574
575 // Following the comma we should have either an index register, or a scale
576 // value. We don't support the later form, but we want to parse it
577 // correctly.
578 //
579 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000580 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000581 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000582 SMLoc L;
583 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000585 if (getLexer().isNot(AsmToken::RParen)) {
586 // Parse the scale amount:
587 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000588 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000589 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000590 "expected comma in scale expression");
591 return 0;
592 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000594
595 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000596 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000597
598 int64_t ScaleVal;
599 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000600 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000601
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000602 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000603 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
604 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
605 return 0;
606 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000607 Scale = (unsigned)ScaleVal;
608 }
609 }
610 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000611 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000612 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000613 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000614
615 int64_t Value;
616 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000617 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000618
Daniel Dunbaree910252010-08-24 19:13:38 +0000619 if (Value != 1)
620 Warning(Loc, "scale factor without index register is ignored");
621 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000622 }
623 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000624
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000625 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000626 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000627 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000628 return 0;
629 }
Sean Callanan18b83232010-01-19 21:44:56 +0000630 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000631 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000632
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000633 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
634 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000635}
636
Chris Lattner98986712010-01-14 22:21:20 +0000637bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000638ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000639 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000640 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000641
Chris Lattnerd8f71792010-11-28 20:23:50 +0000642 // FIXME: Hack to recognize setneb as setne.
643 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
644 PatchedName != "setb" && PatchedName != "setnb")
645 PatchedName = PatchedName.substr(0, Name.size()-1);
646
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000647 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
648 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000649 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000650 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
651 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000652 bool IsVCMP = PatchedName.startswith("vcmp");
653 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000654 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000655 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000656 .Case("eq", 0)
657 .Case("lt", 1)
658 .Case("le", 2)
659 .Case("unord", 3)
660 .Case("neq", 4)
661 .Case("nlt", 5)
662 .Case("nle", 6)
663 .Case("ord", 7)
664 .Case("eq_uq", 8)
665 .Case("nge", 9)
666 .Case("ngt", 0x0A)
667 .Case("false", 0x0B)
668 .Case("neq_oq", 0x0C)
669 .Case("ge", 0x0D)
670 .Case("gt", 0x0E)
671 .Case("true", 0x0F)
672 .Case("eq_os", 0x10)
673 .Case("lt_oq", 0x11)
674 .Case("le_oq", 0x12)
675 .Case("unord_s", 0x13)
676 .Case("neq_us", 0x14)
677 .Case("nlt_uq", 0x15)
678 .Case("nle_uq", 0x16)
679 .Case("ord_s", 0x17)
680 .Case("eq_us", 0x18)
681 .Case("nge_uq", 0x19)
682 .Case("ngt_uq", 0x1A)
683 .Case("false_os", 0x1B)
684 .Case("neq_os", 0x1C)
685 .Case("ge_oq", 0x1D)
686 .Case("gt_oq", 0x1E)
687 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000688 .Default(~0U);
689 if (SSEComparisonCode != ~0U) {
690 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
691 getParser().getContext());
692 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000693 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000694 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000695 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000696 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000697 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000698 } else {
699 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000700 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000701 }
702 }
703 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000704
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000705 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000706
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000707 if (ExtraImmOp)
708 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000709
710
Chris Lattner2544f422010-09-08 05:17:37 +0000711 // Determine whether this is an instruction prefix.
712 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000713 Name == "lock" || Name == "rep" ||
714 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000715 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000716 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000717
718
Chris Lattner2544f422010-09-08 05:17:37 +0000719 // This does the actual operand parsing. Don't parse any more if we have a
720 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
721 // just want to parse the "lock" as the first instruction and the "incl" as
722 // the next one.
723 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000724
725 // Parse '*' modifier.
726 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000727 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000728 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000729 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000730 }
731
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000732 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000733 if (X86Operand *Op = ParseOperand())
734 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000735 else {
736 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000737 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000738 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000739
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000740 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000741 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000742
743 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000744 if (X86Operand *Op = ParseOperand())
745 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000746 else {
747 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000748 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000749 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000751
Chris Lattnercbf8a982010-09-11 16:18:25 +0000752 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000753 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000754 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000755 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000756 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000757 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000758
Chris Lattner2544f422010-09-08 05:17:37 +0000759 if (getLexer().is(AsmToken::EndOfStatement))
760 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000761 else if (isPrefix && getLexer().is(AsmToken::Slash))
762 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000763
Chris Lattner98c870f2010-11-06 19:25:43 +0000764 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
765 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
766 // documented form in various unofficial manuals, so a lot of code uses it.
767 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
768 Operands.size() == 3) {
769 X86Operand &Op = *(X86Operand*)Operands.back();
770 if (Op.isMem() && Op.Mem.SegReg == 0 &&
771 isa<MCConstantExpr>(Op.Mem.Disp) &&
772 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
773 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
774 SMLoc Loc = Op.getEndLoc();
775 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
776 delete &Op;
777 }
778 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000779 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
780 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
781 Operands.size() == 3) {
782 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
783 if (Op.isMem() && Op.Mem.SegReg == 0 &&
784 isa<MCConstantExpr>(Op.Mem.Disp) &&
785 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
786 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
787 SMLoc Loc = Op.getEndLoc();
788 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
789 delete &Op;
790 }
791 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000792 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
793 if (Name.startswith("ins") && Operands.size() == 3 &&
794 (Name == "insb" || Name == "insw" || Name == "insl")) {
795 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
796 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
797 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
798 Operands.pop_back();
799 Operands.pop_back();
800 delete &Op;
801 delete &Op2;
802 }
803 }
804
805 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
806 if (Name.startswith("outs") && Operands.size() == 3 &&
807 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
808 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
809 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
810 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
811 Operands.pop_back();
812 Operands.pop_back();
813 delete &Op;
814 delete &Op2;
815 }
816 }
817
818 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
819 if (Name.startswith("movs") && Operands.size() == 3 &&
820 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000821 (is64Bit() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000822 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
823 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
824 if (isSrcOp(Op) && isDstOp(Op2)) {
825 Operands.pop_back();
826 Operands.pop_back();
827 delete &Op;
828 delete &Op2;
829 }
830 }
831 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
832 if (Name.startswith("lods") && Operands.size() == 3 &&
833 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000834 Name == "lodsl" || (is64Bit() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000835 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
836 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
837 if (isSrcOp(*Op1) && Op2->isReg()) {
838 const char *ins;
839 unsigned reg = Op2->getReg();
840 bool isLods = Name == "lods";
841 if (reg == X86::AL && (isLods || Name == "lodsb"))
842 ins = "lodsb";
843 else if (reg == X86::AX && (isLods || Name == "lodsw"))
844 ins = "lodsw";
845 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
846 ins = "lodsl";
847 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
848 ins = "lodsq";
849 else
850 ins = NULL;
851 if (ins != NULL) {
852 Operands.pop_back();
853 Operands.pop_back();
854 delete Op1;
855 delete Op2;
856 if (Name != ins)
857 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
858 }
859 }
860 }
861 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
862 if (Name.startswith("stos") && Operands.size() == 3 &&
863 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000864 Name == "stosl" || (is64Bit() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000865 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
866 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
867 if (isDstOp(*Op2) && Op1->isReg()) {
868 const char *ins;
869 unsigned reg = Op1->getReg();
870 bool isStos = Name == "stos";
871 if (reg == X86::AL && (isStos || Name == "stosb"))
872 ins = "stosb";
873 else if (reg == X86::AX && (isStos || Name == "stosw"))
874 ins = "stosw";
875 else if (reg == X86::EAX && (isStos || Name == "stosl"))
876 ins = "stosl";
877 else if (reg == X86::RAX && (isStos || Name == "stosq"))
878 ins = "stosq";
879 else
880 ins = NULL;
881 if (ins != NULL) {
882 Operands.pop_back();
883 Operands.pop_back();
884 delete Op1;
885 delete Op2;
886 if (Name != ins)
887 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
888 }
889 }
890 }
891
Chris Lattnere9e16a32010-09-15 04:33:27 +0000892 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000893 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000894 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000895 Name.startswith("shl") || Name.startswith("sal") ||
896 Name.startswith("rcl") || Name.startswith("rcr") ||
897 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000898 Operands.size() == 3) {
899 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
900 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
901 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
902 delete Operands[1];
903 Operands.erase(Operands.begin() + 1);
904 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000905 }
Chris Lattner15f89512011-04-09 19:41:05 +0000906
907 // Transforms "int $3" into "int3" as a size optimization. We can't write an
908 // instalias with an immediate operand yet.
909 if (Name == "int" && Operands.size() == 2) {
910 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
911 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
912 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
913 delete Operands[1];
914 Operands.erase(Operands.begin() + 1);
915 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
916 }
917 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000918
Chris Lattner98986712010-01-14 22:21:20 +0000919 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000920}
921
Chris Lattner2d592d12010-09-15 04:04:33 +0000922bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000923MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000924 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000925 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000926 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000927 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
928 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000929
Chris Lattner7c51a312010-09-29 01:50:45 +0000930 // First, handle aliases that expand to multiple instructions.
931 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000932 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
933 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000934 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000935 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000936 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000937 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000938 MCInst Inst;
939 Inst.setOpcode(X86::WAIT);
940 Out.EmitInstruction(Inst);
941
Chris Lattner0bb83a82010-09-30 16:39:29 +0000942 const char *Repl =
943 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000944 .Case("finit", "fninit")
945 .Case("fsave", "fnsave")
946 .Case("fstcw", "fnstcw")
947 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000948 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000949 .Case("fstsw", "fnstsw")
950 .Case("fstsww", "fnstsw")
951 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000952 .Default(0);
953 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000954 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000955 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000956 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957
Chris Lattnera008e8a2010-09-06 21:54:15 +0000958 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000959 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000960 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000961
Daniel Dunbarc918d602010-05-04 16:12:42 +0000962 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000963 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000964 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000965 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000966 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000967 case Match_MissingFeature:
968 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
969 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000970 case Match_ConversionFail:
971 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000972 case Match_InvalidOperand:
973 WasOriginallyInvalidOperand = true;
974 break;
975 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000976 break;
977 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000978
Daniel Dunbarc918d602010-05-04 16:12:42 +0000979 // FIXME: Ideally, we would only attempt suffix matches for things which are
980 // valid prefixes, and we could just infer the right unambiguous
981 // type. However, that requires substantially more matcher support than the
982 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Daniel Dunbarc918d602010-05-04 16:12:42 +0000984 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000985 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000986 SmallString<16> Tmp;
987 Tmp += Base;
988 Tmp += ' ';
989 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +0000990
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000991 // If this instruction starts with an 'f', then it is a floating point stack
992 // instruction. These come in up to three forms for 32-bit, 64-bit, and
993 // 80-bit floating point, which use the suffixes s,l,t respectively.
994 //
995 // Otherwise, we assume that this may be an integer instruction, which comes
996 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
997 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
998
Daniel Dunbarc918d602010-05-04 16:12:42 +0000999 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001000 Tmp[Base.size()] = Suffixes[0];
1001 unsigned ErrorInfoIgnore;
1002 MatchResultTy Match1, Match2, Match3, Match4;
1003
1004 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1005 Tmp[Base.size()] = Suffixes[1];
1006 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1007 Tmp[Base.size()] = Suffixes[2];
1008 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1009 Tmp[Base.size()] = Suffixes[3];
1010 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001011
1012 // Restore the old token.
1013 Op->setTokenValue(Base);
1014
1015 // If exactly one matched, then we treat that as a successful match (and the
1016 // instruction will already have been filled in correctly, since the failing
1017 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001018 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001019 (Match1 == Match_Success) + (Match2 == Match_Success) +
1020 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001021 if (NumSuccessfulMatches == 1) {
1022 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001023 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001024 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001025
Chris Lattnerec6789f2010-09-06 20:08:02 +00001026 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001027
Daniel Dunbar09062b12010-08-12 00:55:42 +00001028 // If we had multiple suffix matches, then identify this as an ambiguous
1029 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001030 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001031 char MatchChars[4];
1032 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001033 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1034 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1035 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1036 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001037
1038 SmallString<126> Msg;
1039 raw_svector_ostream OS(Msg);
1040 OS << "ambiguous instructions require an explicit suffix (could be ";
1041 for (unsigned i = 0; i != NumMatches; ++i) {
1042 if (i != 0)
1043 OS << ", ";
1044 if (i + 1 == NumMatches)
1045 OS << "or ";
1046 OS << "'" << Base << MatchChars[i] << "'";
1047 }
1048 OS << ")";
1049 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001050 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001051 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052
Chris Lattnera008e8a2010-09-06 21:54:15 +00001053 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001054
Chris Lattnera008e8a2010-09-06 21:54:15 +00001055 // If all of the instructions reported an invalid mnemonic, then the original
1056 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001057 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1058 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001059 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001060 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001061 return true;
1062 }
1063
1064 // Recover location info for the operand if we know which was the problem.
1065 SMLoc ErrorLoc = IDLoc;
1066 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001067 if (OrigErrorInfo >= Operands.size())
1068 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001069
Chris Lattnerce4a3352010-09-06 22:11:18 +00001070 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1071 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1072 }
1073
Chris Lattnerf8840122010-09-15 03:50:11 +00001074 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001075 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001076
Chris Lattnerec6789f2010-09-06 20:08:02 +00001077 // If one instruction matched with a missing feature, report this as a
1078 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001079 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1080 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001081 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1082 return true;
1083 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001084
Chris Lattnera008e8a2010-09-06 21:54:15 +00001085 // If one instruction matched with an invalid operand, report this as an
1086 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001087 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1088 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001089 Error(IDLoc, "invalid operand for instruction");
1090 return true;
1091 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001092
Chris Lattnerec6789f2010-09-06 20:08:02 +00001093 // If all of these were an outright failure, report it in a useless way.
1094 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001095 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001096 return true;
1097}
1098
1099
Chris Lattner537ca842010-10-30 17:38:55 +00001100bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1101 StringRef IDVal = DirectiveID.getIdentifier();
1102 if (IDVal == ".word")
1103 return ParseDirectiveWord(2, DirectiveID.getLoc());
1104 return true;
1105}
1106
1107/// ParseDirectiveWord
1108/// ::= .word [ expression (, expression)* ]
1109bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1110 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1111 for (;;) {
1112 const MCExpr *Value;
1113 if (getParser().ParseExpression(Value))
1114 return true;
1115
1116 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1117
1118 if (getLexer().is(AsmToken::EndOfStatement))
1119 break;
1120
1121 // FIXME: Improve diagnostic.
1122 if (getLexer().isNot(AsmToken::Comma))
1123 return Error(L, "unexpected token in directive");
1124 Parser.Lex();
1125 }
1126 }
1127
1128 Parser.Lex();
1129 return false;
1130}
1131
1132
1133
1134
Sean Callanane88f5522010-01-23 02:43:15 +00001135extern "C" void LLVMInitializeX86AsmLexer();
1136
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001137// Force static initialization.
1138extern "C" void LLVMInitializeX86AsmParser() {
Evan Chengebdeeab2011-07-08 01:53:10 +00001139 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1140 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001141 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001142}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001143
Chris Lattner0692ee62010-09-06 19:11:01 +00001144#define GET_REGISTER_MATCHER
1145#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001146#include "X86GenAsmMatcher.inc"