blob: cb4f15ffed3e92f45a34edb8f9289875ccfb6545 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Evan Cheng94b95502011-07-26 00:24:13 +000035class X86ATTAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000038
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000044 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
Chris Lattner309264d2010-01-15 18:44:13 +000046 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000047 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000048
49 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000050 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000051
Chris Lattner7036f8b2010-09-29 01:42:58 +000052 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000053 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000054 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000055
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000056 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
57 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
58 bool isSrcOp(X86Operand &Op);
59
60 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
61 /// or %es:(%edi) in 32bit mode.
62 bool isDstOp(X86Operand &Op);
63
Evan Cheng59ee62d2011-07-11 03:57:24 +000064 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000065 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000066 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000067 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000068 void SwitchMode() {
69 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
70 setAvailableFeatures(FB);
71 }
Evan Chengebdeeab2011-07-08 01:53:10 +000072
Daniel Dunbar54074b52010-07-19 05:44:09 +000073 /// @name Auto-generated Matcher Functions
74 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000075
Chris Lattner0692ee62010-09-06 19:11:01 +000076#define GET_ASSEMBLER_HEADER
77#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000078
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000079 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000080
81public:
Evan Chengffc0e732011-07-09 05:47:46 +000082 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000083 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000084
Daniel Dunbar54074b52010-07-19 05:44:09 +000085 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +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
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000149 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000150
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 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000230 bool isImmZExtu32u8() 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 <= 0x00000000000000FFULL);
244 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000245 bool isImmSExti64i8() const {
246 if (!isImm())
247 return false;
248
249 // If this isn't a constant expr, just assume it fits and let relaxation
250 // handle it.
251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
252 if (!CE)
253 return true;
254
255 // Otherwise, check the value is in a range that makes sense for this
256 // extension.
257 uint64_t Value = CE->getValue();
258 return (( Value <= 0x000000000000007FULL)||
259 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
260 }
261 bool isImmSExti64i32() const {
262 if (!isImm())
263 return false;
264
265 // If this isn't a constant expr, just assume it fits and let relaxation
266 // handle it.
267 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
268 if (!CE)
269 return true;
270
271 // Otherwise, check the value is in a range that makes sense for this
272 // extension.
273 uint64_t Value = CE->getValue();
274 return (( Value <= 0x000000007FFFFFFFULL)||
275 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000276 }
277
Daniel Dunbar20927f22009-08-07 08:26:05 +0000278 bool isMem() const { return Kind == Memory; }
279
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000280 bool isAbsMem() const {
281 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000282 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000283 }
284
Daniel Dunbar20927f22009-08-07 08:26:05 +0000285 bool isReg() const { return Kind == Register; }
286
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000287 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
288 // Add as immediates when possible.
289 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
290 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
291 else
292 Inst.addOperand(MCOperand::CreateExpr(Expr));
293 }
294
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000295 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000296 assert(N == 1 && "Invalid number of operands!");
297 Inst.addOperand(MCOperand::CreateReg(getReg()));
298 }
299
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000300 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000301 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000302 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000303 }
304
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000305 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000306 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000307 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
308 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
309 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000310 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000311 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
312 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000313
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000314 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
315 assert((N == 1) && "Invalid number of operands!");
316 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
317 }
318
Chris Lattnerb4307b32010-01-15 19:28:38 +0000319 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
320 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000321 Res->Tok.Data = Str.data();
322 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000323 return Res;
324 }
325
Chris Lattner29ef9a22010-01-15 18:51:29 +0000326 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000327 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000328 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000329 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000330 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000331
Chris Lattnerb4307b32010-01-15 19:28:38 +0000332 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
333 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000334 Res->Imm.Val = Val;
335 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000336 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000337
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000338 /// Create an absolute memory operand.
339 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
340 SMLoc EndLoc) {
341 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
342 Res->Mem.SegReg = 0;
343 Res->Mem.Disp = Disp;
344 Res->Mem.BaseReg = 0;
345 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000346 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000347 return Res;
348 }
349
350 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000351 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
352 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000353 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000354 // We should never just have a displacement, that should be parsed as an
355 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000356 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
357
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000358 // The scale should always be one of {1,2,4,8}.
359 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000360 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000361 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000362 Res->Mem.SegReg = SegReg;
363 Res->Mem.Disp = Disp;
364 Res->Mem.BaseReg = BaseReg;
365 Res->Mem.IndexReg = IndexReg;
366 Res->Mem.Scale = Scale;
367 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000368 }
369};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000370
Chris Lattner37dfdec2009-07-29 06:33:53 +0000371} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000372
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000373bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000374 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000375
376 return (Op.isMem() &&
377 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
378 isa<MCConstantExpr>(Op.Mem.Disp) &&
379 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
380 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
381}
382
383bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000384 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000385
386 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
387 isa<MCConstantExpr>(Op.Mem.Disp) &&
388 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
389 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
390}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000391
Chris Lattner29ef9a22010-01-15 18:51:29 +0000392bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
393 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000394 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000395 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000396 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000397 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000398 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000399
Sean Callanan18b83232010-01-19 21:44:56 +0000400 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000401 if (Tok.isNot(AsmToken::Identifier))
402 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000403
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000404 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000405
Chris Lattner33d60d52010-09-22 04:11:10 +0000406 // If the match failed, try the register name as lowercase.
407 if (RegNo == 0)
408 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000409
Evan Cheng5de728c2011-07-27 23:22:03 +0000410 if (!is64BitMode()) {
411 // FIXME: This should be done using Requires<In32BitMode> and
412 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
413 // checked.
414 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
415 // REX prefix.
416 if (RegNo == X86::RIZ ||
417 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
418 X86II::isX86_64NonExtLowByteReg(RegNo) ||
419 X86II::isX86_64ExtendedReg(RegNo))
420 return Error(Tok.getLoc(), "register %"
421 + Tok.getString() + " is only available in 64-bit mode");
422 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000423
Chris Lattner33d60d52010-09-22 04:11:10 +0000424 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
425 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000426 RegNo = X86::ST0;
427 EndLoc = Tok.getLoc();
428 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000429
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000430 // Check to see if we have '(4)' after %st.
431 if (getLexer().isNot(AsmToken::LParen))
432 return false;
433 // Lex the paren.
434 getParser().Lex();
435
436 const AsmToken &IntTok = Parser.getTok();
437 if (IntTok.isNot(AsmToken::Integer))
438 return Error(IntTok.getLoc(), "expected stack index");
439 switch (IntTok.getIntVal()) {
440 case 0: RegNo = X86::ST0; break;
441 case 1: RegNo = X86::ST1; break;
442 case 2: RegNo = X86::ST2; break;
443 case 3: RegNo = X86::ST3; break;
444 case 4: RegNo = X86::ST4; break;
445 case 5: RegNo = X86::ST5; break;
446 case 6: RegNo = X86::ST6; break;
447 case 7: RegNo = X86::ST7; break;
448 default: return Error(IntTok.getLoc(), "invalid stack index");
449 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000450
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000451 if (getParser().Lex().isNot(AsmToken::RParen))
452 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000453
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000454 EndLoc = Tok.getLoc();
455 Parser.Lex(); // Eat ')'
456 return false;
457 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000458
Chris Lattner645b2092010-06-24 07:29:18 +0000459 // If this is "db[0-7]", match it as an alias
460 // for dr[0-7].
461 if (RegNo == 0 && Tok.getString().size() == 3 &&
462 Tok.getString().startswith("db")) {
463 switch (Tok.getString()[2]) {
464 case '0': RegNo = X86::DR0; break;
465 case '1': RegNo = X86::DR1; break;
466 case '2': RegNo = X86::DR2; break;
467 case '3': RegNo = X86::DR3; break;
468 case '4': RegNo = X86::DR4; break;
469 case '5': RegNo = X86::DR5; break;
470 case '6': RegNo = X86::DR6; break;
471 case '7': RegNo = X86::DR7; break;
472 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000473
Chris Lattner645b2092010-06-24 07:29:18 +0000474 if (RegNo != 0) {
475 EndLoc = Tok.getLoc();
476 Parser.Lex(); // Eat it.
477 return false;
478 }
479 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000480
Daniel Dunbar245f0582009-08-08 21:22:41 +0000481 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000482 return Error(Tok.getLoc(), "invalid register name");
483
Chris Lattner29ef9a22010-01-15 18:51:29 +0000484 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000485 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000486 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000487}
488
Chris Lattner309264d2010-01-15 18:44:13 +0000489X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000490 switch (getLexer().getKind()) {
491 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000492 // Parse a memory operand with no segment register.
493 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000494 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000495 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000496 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000497 SMLoc Start, End;
498 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000499 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Evan Cheng5de728c2011-07-27 23:22:03 +0000500 Error(Start, "%eiz and %riz can only be used as index registers");
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000501 return 0;
502 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000503
Chris Lattnereef6d782010-04-17 18:56:34 +0000504 // If this is a segment register followed by a ':', then this is the start
505 // of a memory reference, otherwise this is a normal register reference.
506 if (getLexer().isNot(AsmToken::Colon))
507 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000508
509
Chris Lattnereef6d782010-04-17 18:56:34 +0000510 getParser().Lex(); // Eat the colon.
511 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000512 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000513 case AsmToken::Dollar: {
514 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000515 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000516 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000517 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000518 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000519 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000520 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000521 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000523}
524
Chris Lattnereef6d782010-04-17 18:56:34 +0000525/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
526/// has already been parsed if present.
527X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000528
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000529 // We have to disambiguate a parenthesized expression "(4+5)" from the start
530 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000531 // only way to do this without lookahead is to eat the '(' and see what is
532 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000533 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000534 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000535 SMLoc ExprEnd;
536 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000537
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 // After parsing the base expression we could either have a parenthesized
539 // memory address or not. If not, return now. If so, eat the (.
540 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000541 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000542 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000543 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000544 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000545 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000546
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000547 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000548 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549 } else {
550 // Okay, we have a '('. We don't know if this is an expression or not, but
551 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000552 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000553 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000554
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000555 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000556 // Nothing to do here, fall into the code below with the '(' part of the
557 // memory operand consumed.
558 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000559 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000560
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000561 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000563 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000564
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000565 // After parsing the base expression we could either have a parenthesized
566 // memory address or not. If not, return now. If so, eat the (.
567 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000568 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000569 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000570 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000571 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000572 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000573
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000574 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000575 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000576 }
577 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000579 // If we reached here, then we just ate the ( of the memory operand. Process
580 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000581 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000582
Chris Lattner29ef9a22010-01-15 18:51:29 +0000583 if (getLexer().is(AsmToken::Percent)) {
584 SMLoc L;
585 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000586 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
587 Error(L, "eiz and riz can only be used as index registers");
588 return 0;
589 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000590 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000591
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000592 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000594
595 // Following the comma we should have either an index register, or a scale
596 // value. We don't support the later form, but we want to parse it
597 // correctly.
598 //
599 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000600 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000601 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000602 SMLoc L;
603 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000604
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000605 if (getLexer().isNot(AsmToken::RParen)) {
606 // Parse the scale amount:
607 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000608 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000609 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000610 "expected comma in scale expression");
611 return 0;
612 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000614
615 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000616 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000617
618 int64_t ScaleVal;
619 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000620 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000621
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000622 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000623 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
624 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
625 return 0;
626 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000627 Scale = (unsigned)ScaleVal;
628 }
629 }
630 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000631 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000632 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000633 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000634
635 int64_t Value;
636 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000637 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000638
Daniel Dunbaree910252010-08-24 19:13:38 +0000639 if (Value != 1)
640 Warning(Loc, "scale factor without index register is ignored");
641 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000642 }
643 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000644
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000645 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000646 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000647 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000648 return 0;
649 }
Sean Callanan18b83232010-01-19 21:44:56 +0000650 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000651 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000652
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000653 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
654 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000655}
656
Chris Lattner98986712010-01-14 22:21:20 +0000657bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000658ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000659 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000660 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000661
Chris Lattnerd8f71792010-11-28 20:23:50 +0000662 // FIXME: Hack to recognize setneb as setne.
663 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
664 PatchedName != "setb" && PatchedName != "setnb")
665 PatchedName = PatchedName.substr(0, Name.size()-1);
666
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000667 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
668 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000669 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000670 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
671 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000672 bool IsVCMP = PatchedName.startswith("vcmp");
673 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000674 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000675 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000676 .Case("eq", 0)
677 .Case("lt", 1)
678 .Case("le", 2)
679 .Case("unord", 3)
680 .Case("neq", 4)
681 .Case("nlt", 5)
682 .Case("nle", 6)
683 .Case("ord", 7)
684 .Case("eq_uq", 8)
685 .Case("nge", 9)
686 .Case("ngt", 0x0A)
687 .Case("false", 0x0B)
688 .Case("neq_oq", 0x0C)
689 .Case("ge", 0x0D)
690 .Case("gt", 0x0E)
691 .Case("true", 0x0F)
692 .Case("eq_os", 0x10)
693 .Case("lt_oq", 0x11)
694 .Case("le_oq", 0x12)
695 .Case("unord_s", 0x13)
696 .Case("neq_us", 0x14)
697 .Case("nlt_uq", 0x15)
698 .Case("nle_uq", 0x16)
699 .Case("ord_s", 0x17)
700 .Case("eq_us", 0x18)
701 .Case("nge_uq", 0x19)
702 .Case("ngt_uq", 0x1A)
703 .Case("false_os", 0x1B)
704 .Case("neq_os", 0x1C)
705 .Case("ge_oq", 0x1D)
706 .Case("gt_oq", 0x1E)
707 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000708 .Default(~0U);
709 if (SSEComparisonCode != ~0U) {
710 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
711 getParser().getContext());
712 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000713 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000714 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000715 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000716 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000717 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000718 } else {
719 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000720 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000721 }
722 }
723 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000724
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000725 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000726
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000727 if (ExtraImmOp)
728 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000729
730
Chris Lattner2544f422010-09-08 05:17:37 +0000731 // Determine whether this is an instruction prefix.
732 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000733 Name == "lock" || Name == "rep" ||
734 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000735 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000736 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000737
738
Chris Lattner2544f422010-09-08 05:17:37 +0000739 // This does the actual operand parsing. Don't parse any more if we have a
740 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
741 // just want to parse the "lock" as the first instruction and the "incl" as
742 // the next one.
743 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000744
745 // Parse '*' modifier.
746 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000747 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000748 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000749 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000750 }
751
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000752 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000753 if (X86Operand *Op = ParseOperand())
754 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000755 else {
756 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000757 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000758 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000759
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000760 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000761 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000762
763 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000764 if (X86Operand *Op = ParseOperand())
765 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000766 else {
767 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000768 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000769 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000770 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000771
Chris Lattnercbf8a982010-09-11 16:18:25 +0000772 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000773 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000774 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000775 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000776 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000778
Chris Lattner2544f422010-09-08 05:17:37 +0000779 if (getLexer().is(AsmToken::EndOfStatement))
780 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000781 else if (isPrefix && getLexer().is(AsmToken::Slash))
782 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000783
Chris Lattner98c870f2010-11-06 19:25:43 +0000784 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
785 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
786 // documented form in various unofficial manuals, so a lot of code uses it.
787 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
788 Operands.size() == 3) {
789 X86Operand &Op = *(X86Operand*)Operands.back();
790 if (Op.isMem() && Op.Mem.SegReg == 0 &&
791 isa<MCConstantExpr>(Op.Mem.Disp) &&
792 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
793 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
794 SMLoc Loc = Op.getEndLoc();
795 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
796 delete &Op;
797 }
798 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000799 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
800 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
801 Operands.size() == 3) {
802 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
803 if (Op.isMem() && Op.Mem.SegReg == 0 &&
804 isa<MCConstantExpr>(Op.Mem.Disp) &&
805 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
806 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
807 SMLoc Loc = Op.getEndLoc();
808 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
809 delete &Op;
810 }
811 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000812 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
813 if (Name.startswith("ins") && Operands.size() == 3 &&
814 (Name == "insb" || Name == "insw" || Name == "insl")) {
815 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
816 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
817 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
818 Operands.pop_back();
819 Operands.pop_back();
820 delete &Op;
821 delete &Op2;
822 }
823 }
824
825 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
826 if (Name.startswith("outs") && Operands.size() == 3 &&
827 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
828 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
829 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
830 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
831 Operands.pop_back();
832 Operands.pop_back();
833 delete &Op;
834 delete &Op2;
835 }
836 }
837
838 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
839 if (Name.startswith("movs") && Operands.size() == 3 &&
840 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000841 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000842 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
843 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
844 if (isSrcOp(Op) && isDstOp(Op2)) {
845 Operands.pop_back();
846 Operands.pop_back();
847 delete &Op;
848 delete &Op2;
849 }
850 }
851 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
852 if (Name.startswith("lods") && Operands.size() == 3 &&
853 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000854 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000855 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
856 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
857 if (isSrcOp(*Op1) && Op2->isReg()) {
858 const char *ins;
859 unsigned reg = Op2->getReg();
860 bool isLods = Name == "lods";
861 if (reg == X86::AL && (isLods || Name == "lodsb"))
862 ins = "lodsb";
863 else if (reg == X86::AX && (isLods || Name == "lodsw"))
864 ins = "lodsw";
865 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
866 ins = "lodsl";
867 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
868 ins = "lodsq";
869 else
870 ins = NULL;
871 if (ins != NULL) {
872 Operands.pop_back();
873 Operands.pop_back();
874 delete Op1;
875 delete Op2;
876 if (Name != ins)
877 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
878 }
879 }
880 }
881 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
882 if (Name.startswith("stos") && Operands.size() == 3 &&
883 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000884 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000885 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
886 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
887 if (isDstOp(*Op2) && Op1->isReg()) {
888 const char *ins;
889 unsigned reg = Op1->getReg();
890 bool isStos = Name == "stos";
891 if (reg == X86::AL && (isStos || Name == "stosb"))
892 ins = "stosb";
893 else if (reg == X86::AX && (isStos || Name == "stosw"))
894 ins = "stosw";
895 else if (reg == X86::EAX && (isStos || Name == "stosl"))
896 ins = "stosl";
897 else if (reg == X86::RAX && (isStos || Name == "stosq"))
898 ins = "stosq";
899 else
900 ins = NULL;
901 if (ins != NULL) {
902 Operands.pop_back();
903 Operands.pop_back();
904 delete Op1;
905 delete Op2;
906 if (Name != ins)
907 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
908 }
909 }
910 }
911
Chris Lattnere9e16a32010-09-15 04:33:27 +0000912 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000913 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000914 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000915 Name.startswith("shl") || Name.startswith("sal") ||
916 Name.startswith("rcl") || Name.startswith("rcr") ||
917 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000918 Operands.size() == 3) {
919 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
920 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
921 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
922 delete Operands[1];
923 Operands.erase(Operands.begin() + 1);
924 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000925 }
Chris Lattner15f89512011-04-09 19:41:05 +0000926
927 // Transforms "int $3" into "int3" as a size optimization. We can't write an
928 // instalias with an immediate operand yet.
929 if (Name == "int" && Operands.size() == 2) {
930 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
931 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
932 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
933 delete Operands[1];
934 Operands.erase(Operands.begin() + 1);
935 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
936 }
937 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000938
Chris Lattner98986712010-01-14 22:21:20 +0000939 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000940}
941
Chris Lattner2d592d12010-09-15 04:04:33 +0000942bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000943MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000944 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000945 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000946 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000947 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
948 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000949
Chris Lattner7c51a312010-09-29 01:50:45 +0000950 // First, handle aliases that expand to multiple instructions.
951 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000952 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
953 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000954 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000955 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000956 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000957 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000958 MCInst Inst;
959 Inst.setOpcode(X86::WAIT);
960 Out.EmitInstruction(Inst);
961
Chris Lattner0bb83a82010-09-30 16:39:29 +0000962 const char *Repl =
963 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000964 .Case("finit", "fninit")
965 .Case("fsave", "fnsave")
966 .Case("fstcw", "fnstcw")
967 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000968 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000969 .Case("fstsw", "fnstsw")
970 .Case("fstsww", "fnstsw")
971 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000972 .Default(0);
973 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000974 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000975 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000976 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000977
Chris Lattnera008e8a2010-09-06 21:54:15 +0000978 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000979 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000980 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000981
Daniel Dunbarc918d602010-05-04 16:12:42 +0000982 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000983 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +0000984 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000985 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000986 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000987 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000988 case Match_MissingFeature:
989 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
990 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000991 case Match_ConversionFail:
992 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000993 case Match_InvalidOperand:
994 WasOriginallyInvalidOperand = true;
995 break;
996 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000997 break;
998 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000999
Daniel Dunbarc918d602010-05-04 16:12:42 +00001000 // FIXME: Ideally, we would only attempt suffix matches for things which are
1001 // valid prefixes, and we could just infer the right unambiguous
1002 // type. However, that requires substantially more matcher support than the
1003 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001004
Daniel Dunbarc918d602010-05-04 16:12:42 +00001005 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001006 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001007 SmallString<16> Tmp;
1008 Tmp += Base;
1009 Tmp += ' ';
1010 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001011
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001012 // If this instruction starts with an 'f', then it is a floating point stack
1013 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1014 // 80-bit floating point, which use the suffixes s,l,t respectively.
1015 //
1016 // Otherwise, we assume that this may be an integer instruction, which comes
1017 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1018 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1019
Daniel Dunbarc918d602010-05-04 16:12:42 +00001020 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001021 Tmp[Base.size()] = Suffixes[0];
1022 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001023 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001024
1025 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1026 Tmp[Base.size()] = Suffixes[1];
1027 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1028 Tmp[Base.size()] = Suffixes[2];
1029 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1030 Tmp[Base.size()] = Suffixes[3];
1031 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001032
1033 // Restore the old token.
1034 Op->setTokenValue(Base);
1035
1036 // If exactly one matched, then we treat that as a successful match (and the
1037 // instruction will already have been filled in correctly, since the failing
1038 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001039 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001040 (Match1 == Match_Success) + (Match2 == Match_Success) +
1041 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001042 if (NumSuccessfulMatches == 1) {
1043 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001044 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001045 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001046
Chris Lattnerec6789f2010-09-06 20:08:02 +00001047 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001048
Daniel Dunbar09062b12010-08-12 00:55:42 +00001049 // If we had multiple suffix matches, then identify this as an ambiguous
1050 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001051 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001052 char MatchChars[4];
1053 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001054 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1055 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1056 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1057 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001058
1059 SmallString<126> Msg;
1060 raw_svector_ostream OS(Msg);
1061 OS << "ambiguous instructions require an explicit suffix (could be ";
1062 for (unsigned i = 0; i != NumMatches; ++i) {
1063 if (i != 0)
1064 OS << ", ";
1065 if (i + 1 == NumMatches)
1066 OS << "or ";
1067 OS << "'" << Base << MatchChars[i] << "'";
1068 }
1069 OS << ")";
1070 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001071 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001072 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001073
Chris Lattnera008e8a2010-09-06 21:54:15 +00001074 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001075
Chris Lattnera008e8a2010-09-06 21:54:15 +00001076 // If all of the instructions reported an invalid mnemonic, then the original
1077 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001078 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1079 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001080 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001081 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001082 return true;
1083 }
1084
1085 // Recover location info for the operand if we know which was the problem.
1086 SMLoc ErrorLoc = IDLoc;
1087 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001088 if (OrigErrorInfo >= Operands.size())
1089 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001090
Chris Lattnerce4a3352010-09-06 22:11:18 +00001091 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1092 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1093 }
1094
Chris Lattnerf8840122010-09-15 03:50:11 +00001095 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001096 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001097
Chris Lattnerec6789f2010-09-06 20:08:02 +00001098 // If one instruction matched with a missing feature, report this as a
1099 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001100 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1101 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001102 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1103 return true;
1104 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Chris Lattnera008e8a2010-09-06 21:54:15 +00001106 // If one instruction matched with an invalid operand, report this as an
1107 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001108 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1109 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001110 Error(IDLoc, "invalid operand for instruction");
1111 return true;
1112 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001113
Chris Lattnerec6789f2010-09-06 20:08:02 +00001114 // If all of these were an outright failure, report it in a useless way.
1115 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001116 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001117 return true;
1118}
1119
1120
Chris Lattner537ca842010-10-30 17:38:55 +00001121bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1122 StringRef IDVal = DirectiveID.getIdentifier();
1123 if (IDVal == ".word")
1124 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001125 else if (IDVal.startswith(".code"))
1126 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001127 return true;
1128}
1129
1130/// ParseDirectiveWord
1131/// ::= .word [ expression (, expression)* ]
1132bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1133 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1134 for (;;) {
1135 const MCExpr *Value;
1136 if (getParser().ParseExpression(Value))
1137 return true;
1138
1139 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1140
1141 if (getLexer().is(AsmToken::EndOfStatement))
1142 break;
1143
1144 // FIXME: Improve diagnostic.
1145 if (getLexer().isNot(AsmToken::Comma))
1146 return Error(L, "unexpected token in directive");
1147 Parser.Lex();
1148 }
1149 }
1150
1151 Parser.Lex();
1152 return false;
1153}
1154
Evan Chengbd27f5a2011-07-27 00:38:12 +00001155/// ParseDirectiveCode
1156/// ::= .code32 | .code64
1157bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1158 if (IDVal == ".code32") {
1159 Parser.Lex();
1160 if (is64BitMode()) {
1161 SwitchMode();
1162 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1163 }
1164 } else if (IDVal == ".code64") {
1165 Parser.Lex();
1166 if (!is64BitMode()) {
1167 SwitchMode();
1168 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1169 }
1170 } else {
1171 return Error(L, "unexpected directive " + IDVal);
1172 }
Chris Lattner537ca842010-10-30 17:38:55 +00001173
Evan Chengbd27f5a2011-07-27 00:38:12 +00001174 return false;
1175}
Chris Lattner537ca842010-10-30 17:38:55 +00001176
1177
Sean Callanane88f5522010-01-23 02:43:15 +00001178extern "C" void LLVMInitializeX86AsmLexer();
1179
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001180// Force static initialization.
1181extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001182 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1183 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001184 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001185}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001186
Chris Lattner0692ee62010-09-06 19:11:01 +00001187#define GET_REGISTER_MATCHER
1188#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001189#include "X86GenAsmMatcher.inc"