blob: b5d42ffc8c94d966898a7ccf177a59fefa7b8190 [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"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
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"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000028using namespace llvm;
29
30namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000031struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000032
33class X86ATTAsmParser : public TargetAsmParser {
34 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000035 TargetMachine &TM;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036
Daniel Dunbarf98bc632010-03-18 20:06:02 +000037protected:
38 unsigned Is64Bit : 1;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000039
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000041 MCAsmParser &getParser() const { return Parser; }
42
43 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
44
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000045 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
46
Chris Lattner309264d2010-01-15 18:44:13 +000047 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000048 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000049
50 bool ParseDirectiveWord(unsigned Size, SMLoc L);
51
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
Daniel Dunbar54074b52010-07-19 05:44:09 +000056 /// @name Auto-generated Matcher Functions
57 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000058
Chris Lattner0692ee62010-09-06 19:11:01 +000059#define GET_ASSEMBLER_HEADER
60#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000061
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000062 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000063
64public:
Chris Lattner9a82e702010-10-30 04:57:14 +000065 X86ATTAsmParser(const Target &T, MCAsmParser &parser, TargetMachine &TM)
66 : TargetAsmParser(T), Parser(parser), TM(TM) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000067
Daniel Dunbar54074b52010-07-19 05:44:09 +000068 // Initialize the set of available features.
69 setAvailableFeatures(ComputeAvailableFeatures(
70 &TM.getSubtarget<X86Subtarget>()));
71 }
Roman Divackybf755322011-01-27 17:14:22 +000072 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000073
Benjamin Kramer38e59892010-07-14 22:38:02 +000074 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000076
77 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000078};
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +000079
Daniel Dunbarf98bc632010-03-18 20:06:02 +000080class X86_32ATTAsmParser : public X86ATTAsmParser {
81public:
Chris Lattner9a82e702010-10-30 04:57:14 +000082 X86_32ATTAsmParser(const Target &T, MCAsmParser &Parser, TargetMachine &TM)
83 : X86ATTAsmParser(T, Parser, TM) {
Daniel Dunbarf98bc632010-03-18 20:06:02 +000084 Is64Bit = false;
85 }
86};
87
88class X86_64ATTAsmParser : public X86ATTAsmParser {
89public:
Chris Lattner9a82e702010-10-30 04:57:14 +000090 X86_64ATTAsmParser(const Target &T, MCAsmParser &Parser, TargetMachine &TM)
91 : X86ATTAsmParser(T, Parser, TM) {
Daniel Dunbarf98bc632010-03-18 20:06:02 +000092 Is64Bit = true;
93 }
94};
95
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
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000359
Chris Lattner29ef9a22010-01-15 18:51:29 +0000360bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
361 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000362 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000363 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000364 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000365 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000366 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000367
Sean Callanan18b83232010-01-19 21:44:56 +0000368 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000369 if (Tok.isNot(AsmToken::Identifier))
370 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000371
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000372 // FIXME: Validate register for the current architecture; we have to do
373 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000374 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000375
Chris Lattner33d60d52010-09-22 04:11:10 +0000376 // If the match failed, try the register name as lowercase.
377 if (RegNo == 0)
378 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000379
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000380 // FIXME: This should be done using Requires<In32BitMode> and
381 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
382 // can be also checked.
383 if (RegNo == X86::RIZ && !Is64Bit)
384 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
385
Chris Lattner33d60d52010-09-22 04:11:10 +0000386 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
387 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000388 RegNo = X86::ST0;
389 EndLoc = Tok.getLoc();
390 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000391
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000392 // Check to see if we have '(4)' after %st.
393 if (getLexer().isNot(AsmToken::LParen))
394 return false;
395 // Lex the paren.
396 getParser().Lex();
397
398 const AsmToken &IntTok = Parser.getTok();
399 if (IntTok.isNot(AsmToken::Integer))
400 return Error(IntTok.getLoc(), "expected stack index");
401 switch (IntTok.getIntVal()) {
402 case 0: RegNo = X86::ST0; break;
403 case 1: RegNo = X86::ST1; break;
404 case 2: RegNo = X86::ST2; break;
405 case 3: RegNo = X86::ST3; break;
406 case 4: RegNo = X86::ST4; break;
407 case 5: RegNo = X86::ST5; break;
408 case 6: RegNo = X86::ST6; break;
409 case 7: RegNo = X86::ST7; break;
410 default: return Error(IntTok.getLoc(), "invalid stack index");
411 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000412
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000413 if (getParser().Lex().isNot(AsmToken::RParen))
414 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000415
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000416 EndLoc = Tok.getLoc();
417 Parser.Lex(); // Eat ')'
418 return false;
419 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000420
Chris Lattner645b2092010-06-24 07:29:18 +0000421 // If this is "db[0-7]", match it as an alias
422 // for dr[0-7].
423 if (RegNo == 0 && Tok.getString().size() == 3 &&
424 Tok.getString().startswith("db")) {
425 switch (Tok.getString()[2]) {
426 case '0': RegNo = X86::DR0; break;
427 case '1': RegNo = X86::DR1; break;
428 case '2': RegNo = X86::DR2; break;
429 case '3': RegNo = X86::DR3; break;
430 case '4': RegNo = X86::DR4; break;
431 case '5': RegNo = X86::DR5; break;
432 case '6': RegNo = X86::DR6; break;
433 case '7': RegNo = X86::DR7; break;
434 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000435
Chris Lattner645b2092010-06-24 07:29:18 +0000436 if (RegNo != 0) {
437 EndLoc = Tok.getLoc();
438 Parser.Lex(); // Eat it.
439 return false;
440 }
441 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000442
Daniel Dunbar245f0582009-08-08 21:22:41 +0000443 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000444 return Error(Tok.getLoc(), "invalid register name");
445
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000447 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000448 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000449}
450
Chris Lattner309264d2010-01-15 18:44:13 +0000451X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000452 switch (getLexer().getKind()) {
453 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000454 // Parse a memory operand with no segment register.
455 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000456 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000457 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000458 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000459 SMLoc Start, End;
460 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000461 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
462 Error(Start, "eiz and riz can only be used as index registers");
463 return 0;
464 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000465
Chris Lattnereef6d782010-04-17 18:56:34 +0000466 // If this is a segment register followed by a ':', then this is the start
467 // of a memory reference, otherwise this is a normal register reference.
468 if (getLexer().isNot(AsmToken::Colon))
469 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000470
471
Chris Lattnereef6d782010-04-17 18:56:34 +0000472 getParser().Lex(); // Eat the colon.
473 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000474 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000475 case AsmToken::Dollar: {
476 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000477 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000478 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000479 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000480 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000481 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000482 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000483 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000484 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000485}
486
Chris Lattnereef6d782010-04-17 18:56:34 +0000487/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
488/// has already been parsed if present.
489X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000490
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000491 // We have to disambiguate a parenthesized expression "(4+5)" from the start
492 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000493 // only way to do this without lookahead is to eat the '(' and see what is
494 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000495 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000496 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000497 SMLoc ExprEnd;
498 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000499
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000500 // After parsing the base expression we could either have a parenthesized
501 // memory address or not. If not, return now. If so, eat the (.
502 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000503 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000504 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000505 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000506 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000507 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000508
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000509 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000510 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000511 } else {
512 // Okay, we have a '('. We don't know if this is an expression or not, but
513 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000514 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000515 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000516
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000517 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000518 // Nothing to do here, fall into the code below with the '(' part of the
519 // memory operand consumed.
520 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000521 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000522
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000523 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000524 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000525 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000526
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 // After parsing the base expression we could either have a parenthesized
528 // memory address or not. If not, return now. If so, eat the (.
529 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000530 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000531 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000532 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000533 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000534 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000535
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000536 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000537 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 }
539 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000540
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000541 // If we reached here, then we just ate the ( of the memory operand. Process
542 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000543 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000544
Chris Lattner29ef9a22010-01-15 18:51:29 +0000545 if (getLexer().is(AsmToken::Percent)) {
546 SMLoc L;
547 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000548 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
549 Error(L, "eiz and riz can only be used as index registers");
550 return 0;
551 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000552 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000553
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000555 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000556
557 // Following the comma we should have either an index register, or a scale
558 // value. We don't support the later form, but we want to parse it
559 // correctly.
560 //
561 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000562 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000563 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000564 SMLoc L;
565 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000566
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000567 if (getLexer().isNot(AsmToken::RParen)) {
568 // Parse the scale amount:
569 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000570 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000571 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000572 "expected comma in scale expression");
573 return 0;
574 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000575 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000576
577 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000578 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000579
580 int64_t ScaleVal;
581 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000582 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000583
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000584 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000585 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
586 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
587 return 0;
588 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000589 Scale = (unsigned)ScaleVal;
590 }
591 }
592 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000593 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000594 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000595 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000596
597 int64_t Value;
598 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000599 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000600
Daniel Dunbaree910252010-08-24 19:13:38 +0000601 if (Value != 1)
602 Warning(Loc, "scale factor without index register is ignored");
603 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000604 }
605 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000606
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000607 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000608 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000609 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000610 return 0;
611 }
Sean Callanan18b83232010-01-19 21:44:56 +0000612 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000614
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000615 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
616 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000617}
618
Chris Lattner98986712010-01-14 22:21:20 +0000619bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000620ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000621 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000622 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000623
Chris Lattnerd8f71792010-11-28 20:23:50 +0000624 // FIXME: Hack to recognize setneb as setne.
625 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
626 PatchedName != "setb" && PatchedName != "setnb")
627 PatchedName = PatchedName.substr(0, Name.size()-1);
628
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000629 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
630 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000631 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000632 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
633 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000634 bool IsVCMP = PatchedName.startswith("vcmp");
635 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000636 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000637 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000638 .Case("eq", 0)
639 .Case("lt", 1)
640 .Case("le", 2)
641 .Case("unord", 3)
642 .Case("neq", 4)
643 .Case("nlt", 5)
644 .Case("nle", 6)
645 .Case("ord", 7)
646 .Case("eq_uq", 8)
647 .Case("nge", 9)
648 .Case("ngt", 0x0A)
649 .Case("false", 0x0B)
650 .Case("neq_oq", 0x0C)
651 .Case("ge", 0x0D)
652 .Case("gt", 0x0E)
653 .Case("true", 0x0F)
654 .Case("eq_os", 0x10)
655 .Case("lt_oq", 0x11)
656 .Case("le_oq", 0x12)
657 .Case("unord_s", 0x13)
658 .Case("neq_us", 0x14)
659 .Case("nlt_uq", 0x15)
660 .Case("nle_uq", 0x16)
661 .Case("ord_s", 0x17)
662 .Case("eq_us", 0x18)
663 .Case("nge_uq", 0x19)
664 .Case("ngt_uq", 0x1A)
665 .Case("false_os", 0x1B)
666 .Case("neq_os", 0x1C)
667 .Case("ge_oq", 0x1D)
668 .Case("gt_oq", 0x1E)
669 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000670 .Default(~0U);
671 if (SSEComparisonCode != ~0U) {
672 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
673 getParser().getContext());
674 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000675 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000676 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000677 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000678 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000679 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000680 } else {
681 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000682 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000683 }
684 }
685 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000686
687 // FIXME: Hack to recognize vpclmul<src1_quadword, src2_quadword>dq
688 if (PatchedName.startswith("vpclmul")) {
689 unsigned CLMULQuadWordSelect = StringSwitch<unsigned>(
690 PatchedName.slice(7, PatchedName.size() - 2))
691 .Case("lqlq", 0x00) // src1[63:0], src2[63:0]
692 .Case("hqlq", 0x01) // src1[127:64], src2[63:0]
693 .Case("lqhq", 0x10) // src1[63:0], src2[127:64]
694 .Case("hqhq", 0x11) // src1[127:64], src2[127:64]
695 .Default(~0U);
696 if (CLMULQuadWordSelect != ~0U) {
697 ExtraImmOp = MCConstantExpr::Create(CLMULQuadWordSelect,
698 getParser().getContext());
699 assert(PatchedName.endswith("dq") && "Unexpected mnemonic!");
700 PatchedName = "vpclmulqdq";
701 }
702 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000703
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000704 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000705
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000706 if (ExtraImmOp)
707 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000708
709
Chris Lattner2544f422010-09-08 05:17:37 +0000710 // Determine whether this is an instruction prefix.
711 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000712 Name == "lock" || Name == "rep" ||
713 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000714 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000715 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000716
717
Chris Lattner2544f422010-09-08 05:17:37 +0000718 // This does the actual operand parsing. Don't parse any more if we have a
719 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
720 // just want to parse the "lock" as the first instruction and the "incl" as
721 // the next one.
722 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000723
724 // Parse '*' modifier.
725 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000726 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000727 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000728 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000729 }
730
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000731 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000732 if (X86Operand *Op = ParseOperand())
733 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000734 else {
735 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000736 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000737 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000738
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000739 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000740 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000741
742 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000743 if (X86Operand *Op = ParseOperand())
744 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000745 else {
746 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000747 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000748 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000749 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000750
Chris Lattnercbf8a982010-09-11 16:18:25 +0000751 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000752 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000753 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000754 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000755 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000756 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000757
Chris Lattner2544f422010-09-08 05:17:37 +0000758 if (getLexer().is(AsmToken::EndOfStatement))
759 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000760 else if (isPrefix && getLexer().is(AsmToken::Slash))
761 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000762
Chris Lattner98c870f2010-11-06 19:25:43 +0000763 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
764 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
765 // documented form in various unofficial manuals, so a lot of code uses it.
766 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
767 Operands.size() == 3) {
768 X86Operand &Op = *(X86Operand*)Operands.back();
769 if (Op.isMem() && Op.Mem.SegReg == 0 &&
770 isa<MCConstantExpr>(Op.Mem.Disp) &&
771 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
772 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
773 SMLoc Loc = Op.getEndLoc();
774 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
775 delete &Op;
776 }
777 }
778
Chris Lattnere9e16a32010-09-15 04:33:27 +0000779 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000780 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000781 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000782 Name.startswith("shl") || Name.startswith("sal") ||
783 Name.startswith("rcl") || Name.startswith("rcr") ||
784 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000785 Operands.size() == 3) {
786 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
787 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
788 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
789 delete Operands[1];
790 Operands.erase(Operands.begin() + 1);
791 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000792 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000793
Chris Lattner98986712010-01-14 22:21:20 +0000794 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000795}
796
Chris Lattner2d592d12010-09-15 04:04:33 +0000797bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000798MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000799 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000800 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000801 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000802 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
803 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000804
Chris Lattner7c51a312010-09-29 01:50:45 +0000805 // First, handle aliases that expand to multiple instructions.
806 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000807 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
808 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000809 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000810 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000811 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000812 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000813 MCInst Inst;
814 Inst.setOpcode(X86::WAIT);
815 Out.EmitInstruction(Inst);
816
Chris Lattner0bb83a82010-09-30 16:39:29 +0000817 const char *Repl =
818 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000819 .Case("finit", "fninit")
820 .Case("fsave", "fnsave")
821 .Case("fstcw", "fnstcw")
822 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000823 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000824 .Case("fstsw", "fnstsw")
825 .Case("fstsww", "fnstsw")
826 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000827 .Default(0);
828 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000829 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000830 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000831 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000832
Chris Lattnera008e8a2010-09-06 21:54:15 +0000833 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000834 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000835 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000836
Daniel Dunbarc918d602010-05-04 16:12:42 +0000837 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000838 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000839 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000840 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000841 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000842 case Match_MissingFeature:
843 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
844 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +0000845 case Match_InvalidOperand:
846 WasOriginallyInvalidOperand = true;
847 break;
848 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000849 break;
850 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000851
Daniel Dunbarc918d602010-05-04 16:12:42 +0000852 // FIXME: Ideally, we would only attempt suffix matches for things which are
853 // valid prefixes, and we could just infer the right unambiguous
854 // type. However, that requires substantially more matcher support than the
855 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856
Daniel Dunbarc918d602010-05-04 16:12:42 +0000857 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000858 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000859 SmallString<16> Tmp;
860 Tmp += Base;
861 Tmp += ' ';
862 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +0000863
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000864 // If this instruction starts with an 'f', then it is a floating point stack
865 // instruction. These come in up to three forms for 32-bit, 64-bit, and
866 // 80-bit floating point, which use the suffixes s,l,t respectively.
867 //
868 // Otherwise, we assume that this may be an integer instruction, which comes
869 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
870 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
871
Daniel Dunbarc918d602010-05-04 16:12:42 +0000872 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000873 Tmp[Base.size()] = Suffixes[0];
874 unsigned ErrorInfoIgnore;
875 MatchResultTy Match1, Match2, Match3, Match4;
876
877 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
878 Tmp[Base.size()] = Suffixes[1];
879 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
880 Tmp[Base.size()] = Suffixes[2];
881 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
882 Tmp[Base.size()] = Suffixes[3];
883 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000884
885 // Restore the old token.
886 Op->setTokenValue(Base);
887
888 // If exactly one matched, then we treat that as a successful match (and the
889 // instruction will already have been filled in correctly, since the failing
890 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +0000891 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000892 (Match1 == Match_Success) + (Match2 == Match_Success) +
893 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +0000894 if (NumSuccessfulMatches == 1) {
895 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000896 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000897 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000898
Chris Lattnerec6789f2010-09-06 20:08:02 +0000899 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000900
Daniel Dunbar09062b12010-08-12 00:55:42 +0000901 // If we had multiple suffix matches, then identify this as an ambiguous
902 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +0000903 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +0000904 char MatchChars[4];
905 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000906 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
907 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
908 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
909 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +0000910
911 SmallString<126> Msg;
912 raw_svector_ostream OS(Msg);
913 OS << "ambiguous instructions require an explicit suffix (could be ";
914 for (unsigned i = 0; i != NumMatches; ++i) {
915 if (i != 0)
916 OS << ", ";
917 if (i + 1 == NumMatches)
918 OS << "or ";
919 OS << "'" << Base << MatchChars[i] << "'";
920 }
921 OS << ")";
922 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +0000923 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +0000924 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000925
Chris Lattnera008e8a2010-09-06 21:54:15 +0000926 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000927
Chris Lattnera008e8a2010-09-06 21:54:15 +0000928 // If all of the instructions reported an invalid mnemonic, then the original
929 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000930 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
931 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +0000932 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000933 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +0000934 return true;
935 }
936
937 // Recover location info for the operand if we know which was the problem.
938 SMLoc ErrorLoc = IDLoc;
939 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +0000940 if (OrigErrorInfo >= Operands.size())
941 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000942
Chris Lattnerce4a3352010-09-06 22:11:18 +0000943 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
944 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
945 }
946
Chris Lattnerf8840122010-09-15 03:50:11 +0000947 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000948 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949
Chris Lattnerec6789f2010-09-06 20:08:02 +0000950 // If one instruction matched with a missing feature, report this as a
951 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000952 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
953 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +0000954 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
955 return true;
956 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957
Chris Lattnera008e8a2010-09-06 21:54:15 +0000958 // If one instruction matched with an invalid operand, report this as an
959 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000960 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
961 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +0000962 Error(IDLoc, "invalid operand for instruction");
963 return true;
964 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000965
Chris Lattnerec6789f2010-09-06 20:08:02 +0000966 // If all of these were an outright failure, report it in a useless way.
967 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +0000968 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +0000969 return true;
970}
971
972
Chris Lattner537ca842010-10-30 17:38:55 +0000973bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
974 StringRef IDVal = DirectiveID.getIdentifier();
975 if (IDVal == ".word")
976 return ParseDirectiveWord(2, DirectiveID.getLoc());
977 return true;
978}
979
980/// ParseDirectiveWord
981/// ::= .word [ expression (, expression)* ]
982bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
983 if (getLexer().isNot(AsmToken::EndOfStatement)) {
984 for (;;) {
985 const MCExpr *Value;
986 if (getParser().ParseExpression(Value))
987 return true;
988
989 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
990
991 if (getLexer().is(AsmToken::EndOfStatement))
992 break;
993
994 // FIXME: Improve diagnostic.
995 if (getLexer().isNot(AsmToken::Comma))
996 return Error(L, "unexpected token in directive");
997 Parser.Lex();
998 }
999 }
1000
1001 Parser.Lex();
1002 return false;
1003}
1004
1005
1006
1007
Sean Callanane88f5522010-01-23 02:43:15 +00001008extern "C" void LLVMInitializeX86AsmLexer();
1009
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001010// Force static initialization.
1011extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarf98bc632010-03-18 20:06:02 +00001012 RegisterAsmParser<X86_32ATTAsmParser> X(TheX86_32Target);
1013 RegisterAsmParser<X86_64ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001014 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001015}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001016
Chris Lattner0692ee62010-09-06 19:11:01 +00001017#define GET_REGISTER_MATCHER
1018#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001019#include "X86GenAsmMatcher.inc"