blob: 75e286625b8005cf0c0e9938e4283dd0b7c923bd [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"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000037
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000043 bool Error(SMLoc L, const Twine &Msg,
44 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
45 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000056 X86Operand *ParseIntelMemOperand();
57 X86Operand *ParseIntelBracExpression(unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Chris Lattner7036f8b2010-09-29 01:42:58 +000063 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000064 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000065 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000066
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000067 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
68 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
69 bool isSrcOp(X86Operand &Op);
70
71 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
72 /// or %es:(%edi) in 32bit mode.
73 bool isDstOp(X86Operand &Op);
74
Evan Cheng59ee62d2011-07-11 03:57:24 +000075 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000076 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000077 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000078 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000079 void SwitchMode() {
80 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
81 setAvailableFeatures(FB);
82 }
Evan Chengebdeeab2011-07-08 01:53:10 +000083
Daniel Dunbar54074b52010-07-19 05:44:09 +000084 /// @name Auto-generated Matcher Functions
85 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000086
Chris Lattner0692ee62010-09-06 19:11:01 +000087#define GET_ASSEMBLER_HEADER
88#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000089
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000090 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000091
92public:
Devang Pateldd929fc2012-01-12 18:03:40 +000093 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000094 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000095
Daniel Dunbar54074b52010-07-19 05:44:09 +000096 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000097 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000098 }
Roman Divackybf755322011-01-27 17:14:22 +000099 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000100
Benjamin Kramer38e59892010-07-14 22:38:02 +0000101 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000102 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000103
104 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000105};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000106} // end anonymous namespace
107
Sean Callanane9b466d2010-01-23 00:40:33 +0000108/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000109/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000110
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000111static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000112
113/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000114
115namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000116
117/// X86Operand - Instances of this class represent a parsed X86 machine
118/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000119struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000120 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000121 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122 Register,
123 Immediate,
124 Memory
125 } Kind;
126
Chris Lattner29ef9a22010-01-15 18:51:29 +0000127 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000128
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000129 union {
130 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000131 const char *Data;
132 unsigned Length;
133 } Tok;
134
135 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000136 unsigned RegNo;
137 } Reg;
138
139 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000140 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000141 } Imm;
142
143 struct {
144 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000145 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000146 unsigned BaseReg;
147 unsigned IndexReg;
148 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000149 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000150 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000151 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000152
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000153 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000154 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000155
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000156 /// getStartLoc - Get the location of the first token of this operand.
157 SMLoc getStartLoc() const { return StartLoc; }
158 /// getEndLoc - Get the location of the last token of this operand.
159 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000160
161 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000162
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000163 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000164
Daniel Dunbar20927f22009-08-07 08:26:05 +0000165 StringRef getToken() const {
166 assert(Kind == Token && "Invalid access!");
167 return StringRef(Tok.Data, Tok.Length);
168 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000169 void setTokenValue(StringRef Value) {
170 assert(Kind == Token && "Invalid access!");
171 Tok.Data = Value.data();
172 Tok.Length = Value.size();
173 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000174
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000175 unsigned getReg() const {
176 assert(Kind == Register && "Invalid access!");
177 return Reg.RegNo;
178 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000179
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000180 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000181 assert(Kind == Immediate && "Invalid access!");
182 return Imm.Val;
183 }
184
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000185 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000186 assert(Kind == Memory && "Invalid access!");
187 return Mem.Disp;
188 }
189 unsigned getMemSegReg() const {
190 assert(Kind == Memory && "Invalid access!");
191 return Mem.SegReg;
192 }
193 unsigned getMemBaseReg() const {
194 assert(Kind == Memory && "Invalid access!");
195 return Mem.BaseReg;
196 }
197 unsigned getMemIndexReg() const {
198 assert(Kind == Memory && "Invalid access!");
199 return Mem.IndexReg;
200 }
201 unsigned getMemScale() const {
202 assert(Kind == Memory && "Invalid access!");
203 return Mem.Scale;
204 }
205
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000206 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000207
208 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000209
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000210 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000211 if (!isImm())
212 return false;
213
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000214 // If this isn't a constant expr, just assume it fits and let relaxation
215 // handle it.
216 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
217 if (!CE)
218 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000219
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000220 // Otherwise, check the value is in a range that makes sense for this
221 // extension.
222 uint64_t Value = CE->getValue();
223 return (( Value <= 0x000000000000007FULL)||
224 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
225 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000226 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000227 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000228 if (!isImm())
229 return false;
230
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000231 // If this isn't a constant expr, just assume it fits and let relaxation
232 // handle it.
233 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
234 if (!CE)
235 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000236
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000237 // Otherwise, check the value is in a range that makes sense for this
238 // extension.
239 uint64_t Value = CE->getValue();
240 return (( Value <= 0x000000000000007FULL)||
241 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
242 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
243 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000244 bool isImmZExtu32u8() const {
245 if (!isImm())
246 return false;
247
248 // If this isn't a constant expr, just assume it fits and let relaxation
249 // handle it.
250 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
251 if (!CE)
252 return true;
253
254 // Otherwise, check the value is in a range that makes sense for this
255 // extension.
256 uint64_t Value = CE->getValue();
257 return (Value <= 0x00000000000000FFULL);
258 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000259 bool isImmSExti64i8() const {
260 if (!isImm())
261 return false;
262
263 // If this isn't a constant expr, just assume it fits and let relaxation
264 // handle it.
265 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
266 if (!CE)
267 return true;
268
269 // Otherwise, check the value is in a range that makes sense for this
270 // extension.
271 uint64_t Value = CE->getValue();
272 return (( Value <= 0x000000000000007FULL)||
273 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
274 }
275 bool isImmSExti64i32() const {
276 if (!isImm())
277 return false;
278
279 // If this isn't a constant expr, just assume it fits and let relaxation
280 // handle it.
281 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
282 if (!CE)
283 return true;
284
285 // Otherwise, check the value is in a range that makes sense for this
286 // extension.
287 uint64_t Value = CE->getValue();
288 return (( Value <= 0x000000007FFFFFFFULL)||
289 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000290 }
291
Daniel Dunbar20927f22009-08-07 08:26:05 +0000292 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000293 bool isMem8() const {
294 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
295 }
296 bool isMem16() const {
297 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
298 }
299 bool isMem32() const {
300 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
301 }
302 bool isMem64() const {
303 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
304 }
305 bool isMem80() const {
306 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
307 }
308 bool isMem128() const {
309 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
310 }
311 bool isMem256() const {
312 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
313 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000314
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000315 bool isAbsMem() const {
316 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000317 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000318 }
319
Daniel Dunbar20927f22009-08-07 08:26:05 +0000320 bool isReg() const { return Kind == Register; }
321
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000322 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
323 // Add as immediates when possible.
324 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
325 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
326 else
327 Inst.addOperand(MCOperand::CreateExpr(Expr));
328 }
329
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000330 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000331 assert(N == 1 && "Invalid number of operands!");
332 Inst.addOperand(MCOperand::CreateReg(getReg()));
333 }
334
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000335 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000336 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000337 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000338 }
339
Devang Patelc59d9df2012-01-12 01:51:42 +0000340 void addMem8Operands(MCInst &Inst, unsigned N) const {
341 addMemOperands(Inst, N);
342 }
343 void addMem16Operands(MCInst &Inst, unsigned N) const {
344 addMemOperands(Inst, N);
345 }
346 void addMem32Operands(MCInst &Inst, unsigned N) const {
347 addMemOperands(Inst, N);
348 }
349 void addMem64Operands(MCInst &Inst, unsigned N) const {
350 addMemOperands(Inst, N);
351 }
352 void addMem80Operands(MCInst &Inst, unsigned N) const {
353 addMemOperands(Inst, N);
354 }
355 void addMem128Operands(MCInst &Inst, unsigned N) const {
356 addMemOperands(Inst, N);
357 }
358 void addMem256Operands(MCInst &Inst, unsigned N) const {
359 addMemOperands(Inst, N);
360 }
361
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000362 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000363 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000364 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
365 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
366 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000367 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000368 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
369 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000370
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000371 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
372 assert((N == 1) && "Invalid number of operands!");
373 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
374 }
375
Chris Lattnerb4307b32010-01-15 19:28:38 +0000376 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000377 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
378 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000379 Res->Tok.Data = Str.data();
380 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000381 return Res;
382 }
383
Chris Lattner29ef9a22010-01-15 18:51:29 +0000384 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000385 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000386 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000387 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000388 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000389
Chris Lattnerb4307b32010-01-15 19:28:38 +0000390 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
391 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000392 Res->Imm.Val = Val;
393 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000394 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000395
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000396 /// Create an absolute memory operand.
397 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000398 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000399 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
400 Res->Mem.SegReg = 0;
401 Res->Mem.Disp = Disp;
402 Res->Mem.BaseReg = 0;
403 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000404 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000405 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000406 return Res;
407 }
408
409 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000410 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
411 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000412 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
413 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000414 // We should never just have a displacement, that should be parsed as an
415 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000416 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
417
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000418 // The scale should always be one of {1,2,4,8}.
419 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000420 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000421 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000422 Res->Mem.SegReg = SegReg;
423 Res->Mem.Disp = Disp;
424 Res->Mem.BaseReg = BaseReg;
425 Res->Mem.IndexReg = IndexReg;
426 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000427 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000428 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000429 }
430};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000431
Chris Lattner37dfdec2009-07-29 06:33:53 +0000432} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000433
Devang Pateldd929fc2012-01-12 18:03:40 +0000434bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000435 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000436
437 return (Op.isMem() &&
438 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
439 isa<MCConstantExpr>(Op.Mem.Disp) &&
440 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
441 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
442}
443
Devang Pateldd929fc2012-01-12 18:03:40 +0000444bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000445 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000446
447 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
448 isa<MCConstantExpr>(Op.Mem.Disp) &&
449 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
450 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
451}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000452
Devang Pateldd929fc2012-01-12 18:03:40 +0000453bool X86AsmParser::ParseRegister(unsigned &RegNo,
454 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000455 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000456 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000457 if (!getParser().getAssemblerDialect()) {
458 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
459 StartLoc = TokPercent.getLoc();
460 Parser.Lex(); // Eat percent token.
461 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000462
Sean Callanan18b83232010-01-19 21:44:56 +0000463 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000464 if (Tok.isNot(AsmToken::Identifier))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000465 return Error(StartLoc, "invalid register name",
466 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000467
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000468 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000469
Chris Lattner33d60d52010-09-22 04:11:10 +0000470 // If the match failed, try the register name as lowercase.
471 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000472 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000473
Evan Cheng5de728c2011-07-27 23:22:03 +0000474 if (!is64BitMode()) {
475 // FIXME: This should be done using Requires<In32BitMode> and
476 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
477 // checked.
478 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
479 // REX prefix.
480 if (RegNo == X86::RIZ ||
481 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
482 X86II::isX86_64NonExtLowByteReg(RegNo) ||
483 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000484 return Error(StartLoc, "register %"
485 + Tok.getString() + " is only available in 64-bit mode",
486 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000487 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000488
Chris Lattner33d60d52010-09-22 04:11:10 +0000489 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
490 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000491 RegNo = X86::ST0;
492 EndLoc = Tok.getLoc();
493 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000494
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000495 // Check to see if we have '(4)' after %st.
496 if (getLexer().isNot(AsmToken::LParen))
497 return false;
498 // Lex the paren.
499 getParser().Lex();
500
501 const AsmToken &IntTok = Parser.getTok();
502 if (IntTok.isNot(AsmToken::Integer))
503 return Error(IntTok.getLoc(), "expected stack index");
504 switch (IntTok.getIntVal()) {
505 case 0: RegNo = X86::ST0; break;
506 case 1: RegNo = X86::ST1; break;
507 case 2: RegNo = X86::ST2; break;
508 case 3: RegNo = X86::ST3; break;
509 case 4: RegNo = X86::ST4; break;
510 case 5: RegNo = X86::ST5; break;
511 case 6: RegNo = X86::ST6; break;
512 case 7: RegNo = X86::ST7; break;
513 default: return Error(IntTok.getLoc(), "invalid stack index");
514 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000515
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000516 if (getParser().Lex().isNot(AsmToken::RParen))
517 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000518
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000519 EndLoc = Tok.getLoc();
520 Parser.Lex(); // Eat ')'
521 return false;
522 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000523
Chris Lattner645b2092010-06-24 07:29:18 +0000524 // If this is "db[0-7]", match it as an alias
525 // for dr[0-7].
526 if (RegNo == 0 && Tok.getString().size() == 3 &&
527 Tok.getString().startswith("db")) {
528 switch (Tok.getString()[2]) {
529 case '0': RegNo = X86::DR0; break;
530 case '1': RegNo = X86::DR1; break;
531 case '2': RegNo = X86::DR2; break;
532 case '3': RegNo = X86::DR3; break;
533 case '4': RegNo = X86::DR4; break;
534 case '5': RegNo = X86::DR5; break;
535 case '6': RegNo = X86::DR6; break;
536 case '7': RegNo = X86::DR7; break;
537 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000538
Chris Lattner645b2092010-06-24 07:29:18 +0000539 if (RegNo != 0) {
540 EndLoc = Tok.getLoc();
541 Parser.Lex(); // Eat it.
542 return false;
543 }
544 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000545
Daniel Dunbar245f0582009-08-08 21:22:41 +0000546 if (RegNo == 0)
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000547 return Error(StartLoc, "invalid register name",
548 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000549
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000550 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000551 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000553}
554
Devang Pateldd929fc2012-01-12 18:03:40 +0000555X86Operand *X86AsmParser::ParseOperand() {
Devang Patel0a338862012-01-12 01:36:43 +0000556 if (getParser().getAssemblerDialect())
557 return ParseIntelOperand();
558 return ParseATTOperand();
559}
560
561/// getIntelRegister - If this is an intel register operand
562/// then return register number, otherwise return 0;
563static unsigned getIntelRegisterOperand(StringRef Str) {
564 unsigned RegNo = MatchRegisterName(Str);
565 // If the match failed, try the register name as lowercase.
566 if (RegNo == 0)
567 RegNo = MatchRegisterName(Str.lower());
568 return RegNo;
569}
570
Devang Pateld37ad242012-01-17 18:00:18 +0000571/// getIntelMemOperandSize - Return intel memory operand size.
572static unsigned getIntelMemOperandSize(StringRef OpStr) {
573 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000574 if (OpStr == "BYTE") Size = 8;
575 if (OpStr == "WORD") Size = 16;
576 if (OpStr == "DWORD") Size = 32;
577 if (OpStr == "QWORD") Size = 64;
578 if (OpStr == "XWORD") Size = 80;
579 if (OpStr == "XMMWORD") Size = 128;
580 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000581 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000582}
583
Devang Pateld37ad242012-01-17 18:00:18 +0000584X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned Size) {
585 unsigned SegReg = 0, BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel85d5aae2012-01-13 19:28:58 +0000586 const AsmToken &Tok = Parser.getTok();
Devang Patel0a338862012-01-12 01:36:43 +0000587 SMLoc Start = Parser.getTok().getLoc(), End;
588
Devang Pateld37ad242012-01-17 18:00:18 +0000589 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
590 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
591
592 // Eat '['
593 if (getLexer().isNot(AsmToken::LBrac))
594 return ErrorOperand(Start, "Expected '[' token!");
595 Parser.Lex();
596
597 if (getLexer().is(AsmToken::Identifier)) {
598 // Parse BaseReg
599 BaseReg = getIntelRegisterOperand(Tok.getString());
600 if (BaseReg)
601 Parser.Lex();
602 else {
603 // Handle '[' 'symbol' ']'
604 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
605 if (getParser().ParseExpression(Disp, End)) return 0;
606 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000607 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000608 Parser.Lex();
609 return X86Operand::CreateMem(Disp, Start, End, Size);
610 }
611 } else if (getLexer().is(AsmToken::Integer)) {
612 // Handle '[' number ']'
613 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
614 if (getParser().ParseExpression(Disp, End)) return 0;
615 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000616 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000617 Parser.Lex();
618 return X86Operand::CreateMem(Disp, Start, End, Size);
619 }
620
621 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
622 bool isPlus = getLexer().is(AsmToken::Plus);
623 Parser.Lex();
624 SMLoc PlusLoc = Parser.getTok().getLoc();
625 if (getLexer().is(AsmToken::Integer)) {
626 int64_t Val = Parser.getTok().getIntVal();
627 Parser.Lex();
628 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000629 Parser.Lex();
630 SMLoc IdxRegLoc = Parser.getTok().getLoc();
631 IndexReg = getIntelRegisterOperand(Parser.getTok().getString());
632 if (!IndexReg) return ErrorOperand(IdxRegLoc, "Expected register");
633 Parser.Lex(); // Eat register
634 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000635 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000636 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
637 Disp = isPlus ? ValExpr : MCUnaryExpr::CreateMinus(ValExpr, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000638 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000639 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patel6220fea2012-01-17 21:25:10 +0000640 } else if (getLexer().is(AsmToken::Identifier)) {
641 IndexReg = getIntelRegisterOperand(Tok.getString());
642 if (IndexReg)
643 Parser.Lex();
Devang Pateld37ad242012-01-17 18:00:18 +0000644 }
645 }
646
647 if (getLexer().isNot(AsmToken::RBrac))
648 if (getParser().ParseExpression(Disp, End)) return 0;
649
650 End = Parser.getTok().getLoc();
651 if (getLexer().isNot(AsmToken::RBrac))
652 return ErrorOperand(End, "expected ']' token!");
653 Parser.Lex();
654 End = Parser.getTok().getLoc();
655 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000656 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000657}
658
659/// ParseIntelMemOperand - Parse intel style memory operand.
660X86Operand *X86AsmParser::ParseIntelMemOperand() {
661 const AsmToken &Tok = Parser.getTok();
662 SMLoc Start = Parser.getTok().getLoc(), End;
663
664 unsigned Size = getIntelMemOperandSize(Tok.getString());
665 if (Size) {
666 Parser.Lex();
667 assert (Tok.getString() == "PTR" && "Unexpected token!");
668 Parser.Lex();
669 }
670
671 if (getLexer().is(AsmToken::LBrac))
672 return ParseIntelBracExpression(Size);
673
674 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
675 if (getParser().ParseExpression(Disp, End)) return 0;
676 return X86Operand::CreateMem(Disp, Start, End, Size);
677}
678
679X86Operand *X86AsmParser::ParseIntelOperand() {
680 StringRef TokenString = Parser.getTok().getString();
681 SMLoc Start = Parser.getTok().getLoc(), End;
682
683 // immediate.
684 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
685 getLexer().is(AsmToken::Minus)) {
686 const MCExpr *Val;
687 if (!getParser().ParseExpression(Val, End)) {
688 End = Parser.getTok().getLoc();
689 return X86Operand::CreateImm(Val, Start, End);
690 }
691 }
692
Devang Patel0a338862012-01-12 01:36:43 +0000693 // register
Devang Pateld37ad242012-01-17 18:00:18 +0000694 if(unsigned RegNo = getIntelRegisterOperand(TokenString)) {
Devang Patel0a338862012-01-12 01:36:43 +0000695 Parser.Lex();
696 End = Parser.getTok().getLoc();
697 return X86Operand::CreateReg(RegNo, Start, End);
698 }
699
700 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000701 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000702}
703
Devang Pateldd929fc2012-01-12 18:03:40 +0000704X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000705 switch (getLexer().getKind()) {
706 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000707 // Parse a memory operand with no segment register.
708 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000709 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000710 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000711 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000712 SMLoc Start, End;
713 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000714 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000715 Error(Start, "%eiz and %riz can only be used as index registers",
716 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000717 return 0;
718 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000719
Chris Lattnereef6d782010-04-17 18:56:34 +0000720 // If this is a segment register followed by a ':', then this is the start
721 // of a memory reference, otherwise this is a normal register reference.
722 if (getLexer().isNot(AsmToken::Colon))
723 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000724
725
Chris Lattnereef6d782010-04-17 18:56:34 +0000726 getParser().Lex(); // Eat the colon.
727 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000728 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000729 case AsmToken::Dollar: {
730 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000731 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000732 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000733 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000734 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000735 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000736 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000737 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000738 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000739}
740
Chris Lattnereef6d782010-04-17 18:56:34 +0000741/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
742/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000743X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000744
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000745 // We have to disambiguate a parenthesized expression "(4+5)" from the start
746 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000747 // only way to do this without lookahead is to eat the '(' and see what is
748 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000749 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000751 SMLoc ExprEnd;
752 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000753
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000754 // After parsing the base expression we could either have a parenthesized
755 // memory address or not. If not, return now. If so, eat the (.
756 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000757 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000758 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000759 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000760 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000761 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000762
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000763 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000764 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000765 } else {
766 // Okay, we have a '('. We don't know if this is an expression or not, but
767 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000768 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000769 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000770
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000771 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000772 // Nothing to do here, fall into the code below with the '(' part of the
773 // memory operand consumed.
774 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000775 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000776
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000778 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000779 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000780
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000781 // After parsing the base expression we could either have a parenthesized
782 // memory address or not. If not, return now. If so, eat the (.
783 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000784 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000785 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000786 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000787 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000788 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000789
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000790 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000791 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000792 }
793 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000794
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000795 // If we reached here, then we just ate the ( of the memory operand. Process
796 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000797 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000798
Chris Lattner29ef9a22010-01-15 18:51:29 +0000799 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000800 SMLoc StartLoc, EndLoc;
801 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000802 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000803 Error(StartLoc, "eiz and riz can only be used as index registers",
804 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000805 return 0;
806 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000807 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000808
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000809 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000810 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000811
812 // Following the comma we should have either an index register, or a scale
813 // value. We don't support the later form, but we want to parse it
814 // correctly.
815 //
816 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000817 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000818 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000819 SMLoc L;
820 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000821
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000822 if (getLexer().isNot(AsmToken::RParen)) {
823 // Parse the scale amount:
824 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000825 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000826 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000827 "expected comma in scale expression");
828 return 0;
829 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000830 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000831
832 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000833 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000834
835 int64_t ScaleVal;
836 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000837 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000838
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000839 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000840 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
841 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
842 return 0;
843 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000844 Scale = (unsigned)ScaleVal;
845 }
846 }
847 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000848 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000850 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000851
852 int64_t Value;
853 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000854 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000855
Daniel Dunbaree910252010-08-24 19:13:38 +0000856 if (Value != 1)
857 Warning(Loc, "scale factor without index register is ignored");
858 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000859 }
860 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000861
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000863 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000864 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000865 return 0;
866 }
Sean Callanan18b83232010-01-19 21:44:56 +0000867 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000868 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000869
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000870 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
871 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872}
873
Devang Pateldd929fc2012-01-12 18:03:40 +0000874bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000875ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000876 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000877 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000878
Chris Lattnerd8f71792010-11-28 20:23:50 +0000879 // FIXME: Hack to recognize setneb as setne.
880 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
881 PatchedName != "setb" && PatchedName != "setnb")
882 PatchedName = PatchedName.substr(0, Name.size()-1);
883
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000884 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
885 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000886 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000887 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
888 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000889 bool IsVCMP = PatchedName.startswith("vcmp");
890 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000891 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000892 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000893 .Case("eq", 0)
894 .Case("lt", 1)
895 .Case("le", 2)
896 .Case("unord", 3)
897 .Case("neq", 4)
898 .Case("nlt", 5)
899 .Case("nle", 6)
900 .Case("ord", 7)
901 .Case("eq_uq", 8)
902 .Case("nge", 9)
903 .Case("ngt", 0x0A)
904 .Case("false", 0x0B)
905 .Case("neq_oq", 0x0C)
906 .Case("ge", 0x0D)
907 .Case("gt", 0x0E)
908 .Case("true", 0x0F)
909 .Case("eq_os", 0x10)
910 .Case("lt_oq", 0x11)
911 .Case("le_oq", 0x12)
912 .Case("unord_s", 0x13)
913 .Case("neq_us", 0x14)
914 .Case("nlt_uq", 0x15)
915 .Case("nle_uq", 0x16)
916 .Case("ord_s", 0x17)
917 .Case("eq_us", 0x18)
918 .Case("nge_uq", 0x19)
919 .Case("ngt_uq", 0x1A)
920 .Case("false_os", 0x1B)
921 .Case("neq_os", 0x1C)
922 .Case("ge_oq", 0x1D)
923 .Case("gt_oq", 0x1E)
924 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000925 .Default(~0U);
926 if (SSEComparisonCode != ~0U) {
927 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
928 getParser().getContext());
929 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000930 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000931 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000932 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000933 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000934 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000935 } else {
936 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000937 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000938 }
939 }
940 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000941
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000942 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000943
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000944 if (ExtraImmOp)
945 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000946
947
Chris Lattner2544f422010-09-08 05:17:37 +0000948 // Determine whether this is an instruction prefix.
949 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000950 Name == "lock" || Name == "rep" ||
951 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000952 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000953 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000954
955
Chris Lattner2544f422010-09-08 05:17:37 +0000956 // This does the actual operand parsing. Don't parse any more if we have a
957 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
958 // just want to parse the "lock" as the first instruction and the "incl" as
959 // the next one.
960 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000961
962 // Parse '*' modifier.
963 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000964 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000965 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000966 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000967 }
968
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000969 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000970 if (X86Operand *Op = ParseOperand())
971 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000972 else {
973 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000974 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000975 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000976
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000977 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000978 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000979
980 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000981 if (X86Operand *Op = ParseOperand())
982 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000983 else {
984 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000985 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000986 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000987 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000988
Chris Lattnercbf8a982010-09-11 16:18:25 +0000989 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000990 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000991 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000992 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000993 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000994 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000995
Chris Lattner2544f422010-09-08 05:17:37 +0000996 if (getLexer().is(AsmToken::EndOfStatement))
997 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000998 else if (isPrefix && getLexer().is(AsmToken::Slash))
999 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001000
Chris Lattner98c870f2010-11-06 19:25:43 +00001001 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1002 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1003 // documented form in various unofficial manuals, so a lot of code uses it.
1004 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1005 Operands.size() == 3) {
1006 X86Operand &Op = *(X86Operand*)Operands.back();
1007 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1008 isa<MCConstantExpr>(Op.Mem.Disp) &&
1009 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1010 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1011 SMLoc Loc = Op.getEndLoc();
1012 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1013 delete &Op;
1014 }
1015 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001016 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1017 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1018 Operands.size() == 3) {
1019 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1020 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1021 isa<MCConstantExpr>(Op.Mem.Disp) &&
1022 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1023 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1024 SMLoc Loc = Op.getEndLoc();
1025 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1026 delete &Op;
1027 }
1028 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001029 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1030 if (Name.startswith("ins") && Operands.size() == 3 &&
1031 (Name == "insb" || Name == "insw" || Name == "insl")) {
1032 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1033 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1034 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1035 Operands.pop_back();
1036 Operands.pop_back();
1037 delete &Op;
1038 delete &Op2;
1039 }
1040 }
1041
1042 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1043 if (Name.startswith("outs") && Operands.size() == 3 &&
1044 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1045 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1046 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1047 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1048 Operands.pop_back();
1049 Operands.pop_back();
1050 delete &Op;
1051 delete &Op2;
1052 }
1053 }
1054
1055 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1056 if (Name.startswith("movs") && Operands.size() == 3 &&
1057 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001058 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001059 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1060 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1061 if (isSrcOp(Op) && isDstOp(Op2)) {
1062 Operands.pop_back();
1063 Operands.pop_back();
1064 delete &Op;
1065 delete &Op2;
1066 }
1067 }
1068 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1069 if (Name.startswith("lods") && Operands.size() == 3 &&
1070 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001071 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001072 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1073 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1074 if (isSrcOp(*Op1) && Op2->isReg()) {
1075 const char *ins;
1076 unsigned reg = Op2->getReg();
1077 bool isLods = Name == "lods";
1078 if (reg == X86::AL && (isLods || Name == "lodsb"))
1079 ins = "lodsb";
1080 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1081 ins = "lodsw";
1082 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1083 ins = "lodsl";
1084 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1085 ins = "lodsq";
1086 else
1087 ins = NULL;
1088 if (ins != NULL) {
1089 Operands.pop_back();
1090 Operands.pop_back();
1091 delete Op1;
1092 delete Op2;
1093 if (Name != ins)
1094 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1095 }
1096 }
1097 }
1098 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1099 if (Name.startswith("stos") && Operands.size() == 3 &&
1100 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001101 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001102 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1103 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1104 if (isDstOp(*Op2) && Op1->isReg()) {
1105 const char *ins;
1106 unsigned reg = Op1->getReg();
1107 bool isStos = Name == "stos";
1108 if (reg == X86::AL && (isStos || Name == "stosb"))
1109 ins = "stosb";
1110 else if (reg == X86::AX && (isStos || Name == "stosw"))
1111 ins = "stosw";
1112 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1113 ins = "stosl";
1114 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1115 ins = "stosq";
1116 else
1117 ins = NULL;
1118 if (ins != NULL) {
1119 Operands.pop_back();
1120 Operands.pop_back();
1121 delete Op1;
1122 delete Op2;
1123 if (Name != ins)
1124 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1125 }
1126 }
1127 }
1128
Chris Lattnere9e16a32010-09-15 04:33:27 +00001129 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001130 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001131 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001132 Name.startswith("shl") || Name.startswith("sal") ||
1133 Name.startswith("rcl") || Name.startswith("rcr") ||
1134 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001135 Operands.size() == 3) {
1136 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1137 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1138 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1139 delete Operands[1];
1140 Operands.erase(Operands.begin() + 1);
1141 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001142 }
Chris Lattner15f89512011-04-09 19:41:05 +00001143
1144 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1145 // instalias with an immediate operand yet.
1146 if (Name == "int" && Operands.size() == 2) {
1147 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1148 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1149 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1150 delete Operands[1];
1151 Operands.erase(Operands.begin() + 1);
1152 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1153 }
1154 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001155
Chris Lattner98986712010-01-14 22:21:20 +00001156 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001157}
1158
Devang Pateldd929fc2012-01-12 18:03:40 +00001159bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001160MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001161 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001162 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001163 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001164 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1165 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001166
Chris Lattner7c51a312010-09-29 01:50:45 +00001167 // First, handle aliases that expand to multiple instructions.
1168 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001169 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1170 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001171 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001172 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001173 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001174 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001175 MCInst Inst;
1176 Inst.setOpcode(X86::WAIT);
1177 Out.EmitInstruction(Inst);
1178
Chris Lattner0bb83a82010-09-30 16:39:29 +00001179 const char *Repl =
1180 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001181 .Case("finit", "fninit")
1182 .Case("fsave", "fnsave")
1183 .Case("fstcw", "fnstcw")
1184 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001185 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001186 .Case("fstsw", "fnstsw")
1187 .Case("fstsww", "fnstsw")
1188 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001189 .Default(0);
1190 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001191 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001192 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001193 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001194
Chris Lattnera008e8a2010-09-06 21:54:15 +00001195 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001196 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001197 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001198
Daniel Dunbarc918d602010-05-04 16:12:42 +00001199 // First, try a direct match.
Devang Patel0a338862012-01-12 01:36:43 +00001200 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1201 getParser().getAssemblerDialect())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001202 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001203 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +00001204 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001205 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001206 case Match_MissingFeature:
1207 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1208 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001209 case Match_ConversionFail:
1210 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001211 case Match_InvalidOperand:
1212 WasOriginallyInvalidOperand = true;
1213 break;
1214 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001215 break;
1216 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001217
Daniel Dunbarc918d602010-05-04 16:12:42 +00001218 // FIXME: Ideally, we would only attempt suffix matches for things which are
1219 // valid prefixes, and we could just infer the right unambiguous
1220 // type. However, that requires substantially more matcher support than the
1221 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001222
Daniel Dunbarc918d602010-05-04 16:12:42 +00001223 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001224 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001225 SmallString<16> Tmp;
1226 Tmp += Base;
1227 Tmp += ' ';
1228 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001229
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001230 // If this instruction starts with an 'f', then it is a floating point stack
1231 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1232 // 80-bit floating point, which use the suffixes s,l,t respectively.
1233 //
1234 // Otherwise, we assume that this may be an integer instruction, which comes
1235 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1236 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1237
Daniel Dunbarc918d602010-05-04 16:12:42 +00001238 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001239 Tmp[Base.size()] = Suffixes[0];
1240 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001241 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001242
1243 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1244 Tmp[Base.size()] = Suffixes[1];
1245 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1246 Tmp[Base.size()] = Suffixes[2];
1247 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1248 Tmp[Base.size()] = Suffixes[3];
1249 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001250
1251 // Restore the old token.
1252 Op->setTokenValue(Base);
1253
1254 // If exactly one matched, then we treat that as a successful match (and the
1255 // instruction will already have been filled in correctly, since the failing
1256 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001257 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001258 (Match1 == Match_Success) + (Match2 == Match_Success) +
1259 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001260 if (NumSuccessfulMatches == 1) {
1261 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001262 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001263 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001264
Chris Lattnerec6789f2010-09-06 20:08:02 +00001265 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001266
Daniel Dunbar09062b12010-08-12 00:55:42 +00001267 // If we had multiple suffix matches, then identify this as an ambiguous
1268 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001269 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001270 char MatchChars[4];
1271 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001272 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1273 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1274 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1275 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001276
1277 SmallString<126> Msg;
1278 raw_svector_ostream OS(Msg);
1279 OS << "ambiguous instructions require an explicit suffix (could be ";
1280 for (unsigned i = 0; i != NumMatches; ++i) {
1281 if (i != 0)
1282 OS << ", ";
1283 if (i + 1 == NumMatches)
1284 OS << "or ";
1285 OS << "'" << Base << MatchChars[i] << "'";
1286 }
1287 OS << ")";
1288 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001289 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001290 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001291
Chris Lattnera008e8a2010-09-06 21:54:15 +00001292 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001293
Chris Lattnera008e8a2010-09-06 21:54:15 +00001294 // If all of the instructions reported an invalid mnemonic, then the original
1295 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001296 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1297 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001298 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001299 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1300 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001301 }
1302
1303 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001304 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001305 if (OrigErrorInfo >= Operands.size())
1306 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001307
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001308 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1309 if (Operand->getStartLoc().isValid()) {
1310 SMRange OperandRange = Operand->getLocRange();
1311 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1312 OperandRange);
1313 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001314 }
1315
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001316 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001317 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001318
Chris Lattnerec6789f2010-09-06 20:08:02 +00001319 // If one instruction matched with a missing feature, report this as a
1320 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001321 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1322 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001323 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1324 return true;
1325 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001326
Chris Lattnera008e8a2010-09-06 21:54:15 +00001327 // If one instruction matched with an invalid operand, report this as an
1328 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001329 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1330 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001331 Error(IDLoc, "invalid operand for instruction");
1332 return true;
1333 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001334
Chris Lattnerec6789f2010-09-06 20:08:02 +00001335 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001336 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001337 return true;
1338}
1339
1340
Devang Pateldd929fc2012-01-12 18:03:40 +00001341bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001342 StringRef IDVal = DirectiveID.getIdentifier();
1343 if (IDVal == ".word")
1344 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001345 else if (IDVal.startswith(".code"))
1346 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001347 return true;
1348}
1349
1350/// ParseDirectiveWord
1351/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001352bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1354 for (;;) {
1355 const MCExpr *Value;
1356 if (getParser().ParseExpression(Value))
1357 return true;
1358
1359 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1360
1361 if (getLexer().is(AsmToken::EndOfStatement))
1362 break;
1363
1364 // FIXME: Improve diagnostic.
1365 if (getLexer().isNot(AsmToken::Comma))
1366 return Error(L, "unexpected token in directive");
1367 Parser.Lex();
1368 }
1369 }
1370
1371 Parser.Lex();
1372 return false;
1373}
1374
Evan Chengbd27f5a2011-07-27 00:38:12 +00001375/// ParseDirectiveCode
1376/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001377bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001378 if (IDVal == ".code32") {
1379 Parser.Lex();
1380 if (is64BitMode()) {
1381 SwitchMode();
1382 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1383 }
1384 } else if (IDVal == ".code64") {
1385 Parser.Lex();
1386 if (!is64BitMode()) {
1387 SwitchMode();
1388 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1389 }
1390 } else {
1391 return Error(L, "unexpected directive " + IDVal);
1392 }
Chris Lattner537ca842010-10-30 17:38:55 +00001393
Evan Chengbd27f5a2011-07-27 00:38:12 +00001394 return false;
1395}
Chris Lattner537ca842010-10-30 17:38:55 +00001396
1397
Sean Callanane88f5522010-01-23 02:43:15 +00001398extern "C" void LLVMInitializeX86AsmLexer();
1399
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001400// Force static initialization.
1401extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001402 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1403 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001404 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001405}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001406
Chris Lattner0692ee62010-09-06 19:11:01 +00001407#define GET_REGISTER_MATCHER
1408#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001409#include "X86GenAsmMatcher.inc"