blob: c3f6563d199a3e42d320f7aad47ad655edac3368 [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))
607 return ErrorOperand(Start, "Expected ']' token!");
608 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))
616 return ErrorOperand(Start, "Expected ']' token!");
617 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)) {
629 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;
635 } else if (getLexer().is(AsmToken::RBrac)) {
636 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patel9a3d2932012-01-17 19:08:07 +0000637 Disp = isPlus ? ValExpr : MCUnaryExpr::CreateMinus(ValExpr, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000638 } else
639 return ErrorOperand(PlusLoc, "unexpected token after +");
640 }
641 }
642
643 if (getLexer().isNot(AsmToken::RBrac))
644 if (getParser().ParseExpression(Disp, End)) return 0;
645
646 End = Parser.getTok().getLoc();
647 if (getLexer().isNot(AsmToken::RBrac))
648 return ErrorOperand(End, "expected ']' token!");
649 Parser.Lex();
650 End = Parser.getTok().getLoc();
651 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
652 Start, End, Size);
653}
654
655/// ParseIntelMemOperand - Parse intel style memory operand.
656X86Operand *X86AsmParser::ParseIntelMemOperand() {
657 const AsmToken &Tok = Parser.getTok();
658 SMLoc Start = Parser.getTok().getLoc(), End;
659
660 unsigned Size = getIntelMemOperandSize(Tok.getString());
661 if (Size) {
662 Parser.Lex();
663 assert (Tok.getString() == "PTR" && "Unexpected token!");
664 Parser.Lex();
665 }
666
667 if (getLexer().is(AsmToken::LBrac))
668 return ParseIntelBracExpression(Size);
669
670 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
671 if (getParser().ParseExpression(Disp, End)) return 0;
672 return X86Operand::CreateMem(Disp, Start, End, Size);
673}
674
675X86Operand *X86AsmParser::ParseIntelOperand() {
676 StringRef TokenString = Parser.getTok().getString();
677 SMLoc Start = Parser.getTok().getLoc(), End;
678
679 // immediate.
680 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
681 getLexer().is(AsmToken::Minus)) {
682 const MCExpr *Val;
683 if (!getParser().ParseExpression(Val, End)) {
684 End = Parser.getTok().getLoc();
685 return X86Operand::CreateImm(Val, Start, End);
686 }
687 }
688
Devang Patel0a338862012-01-12 01:36:43 +0000689 // register
Devang Pateld37ad242012-01-17 18:00:18 +0000690 if(unsigned RegNo = getIntelRegisterOperand(TokenString)) {
Devang Patel0a338862012-01-12 01:36:43 +0000691 Parser.Lex();
692 End = Parser.getTok().getLoc();
693 return X86Operand::CreateReg(RegNo, Start, End);
694 }
695
696 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000697 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000698}
699
Devang Pateldd929fc2012-01-12 18:03:40 +0000700X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000701 switch (getLexer().getKind()) {
702 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000703 // Parse a memory operand with no segment register.
704 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000705 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000706 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000707 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000708 SMLoc Start, End;
709 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000710 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000711 Error(Start, "%eiz and %riz can only be used as index registers",
712 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000713 return 0;
714 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000715
Chris Lattnereef6d782010-04-17 18:56:34 +0000716 // If this is a segment register followed by a ':', then this is the start
717 // of a memory reference, otherwise this is a normal register reference.
718 if (getLexer().isNot(AsmToken::Colon))
719 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000720
721
Chris Lattnereef6d782010-04-17 18:56:34 +0000722 getParser().Lex(); // Eat the colon.
723 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000724 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000725 case AsmToken::Dollar: {
726 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000727 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000728 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000729 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000730 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000731 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000732 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000733 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000734 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000735}
736
Chris Lattnereef6d782010-04-17 18:56:34 +0000737/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
738/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000739X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000740
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000741 // We have to disambiguate a parenthesized expression "(4+5)" from the start
742 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000743 // only way to do this without lookahead is to eat the '(' and see what is
744 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000745 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000746 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000747 SMLoc ExprEnd;
748 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000749
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 // After parsing the base expression we could either have a parenthesized
751 // memory address or not. If not, return now. If so, eat the (.
752 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000753 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000754 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000755 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000756 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000757 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000758
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000759 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000760 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000761 } else {
762 // Okay, we have a '('. We don't know if this is an expression or not, but
763 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000764 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000765 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000766
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000767 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000768 // Nothing to do here, fall into the code below with the '(' part of the
769 // memory operand consumed.
770 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000771 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000772
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000773 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000774 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000775 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000776
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 // After parsing the base expression we could either have a parenthesized
778 // memory address or not. If not, return now. If so, eat the (.
779 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000780 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000781 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000782 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000783 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000784 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000785
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000786 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000787 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000788 }
789 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000790
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000791 // If we reached here, then we just ate the ( of the memory operand. Process
792 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000793 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000794
Chris Lattner29ef9a22010-01-15 18:51:29 +0000795 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000796 SMLoc StartLoc, EndLoc;
797 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000798 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000799 Error(StartLoc, "eiz and riz can only be used as index registers",
800 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000801 return 0;
802 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000803 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000804
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000805 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000806 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000807
808 // Following the comma we should have either an index register, or a scale
809 // value. We don't support the later form, but we want to parse it
810 // correctly.
811 //
812 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000813 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000814 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000815 SMLoc L;
816 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000817
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000818 if (getLexer().isNot(AsmToken::RParen)) {
819 // Parse the scale amount:
820 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000821 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000822 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000823 "expected comma in scale expression");
824 return 0;
825 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000826 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000827
828 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000829 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000830
831 int64_t ScaleVal;
832 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000833 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000834
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000835 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000836 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
837 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
838 return 0;
839 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000840 Scale = (unsigned)ScaleVal;
841 }
842 }
843 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000844 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000845 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000846 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000847
848 int64_t Value;
849 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000850 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000851
Daniel Dunbaree910252010-08-24 19:13:38 +0000852 if (Value != 1)
853 Warning(Loc, "scale factor without index register is ignored");
854 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000855 }
856 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000857
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000858 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000859 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000860 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000861 return 0;
862 }
Sean Callanan18b83232010-01-19 21:44:56 +0000863 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000864 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000865
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000866 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
867 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000868}
869
Devang Pateldd929fc2012-01-12 18:03:40 +0000870bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000871ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000872 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000873 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000874
Chris Lattnerd8f71792010-11-28 20:23:50 +0000875 // FIXME: Hack to recognize setneb as setne.
876 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
877 PatchedName != "setb" && PatchedName != "setnb")
878 PatchedName = PatchedName.substr(0, Name.size()-1);
879
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000880 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
881 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000882 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000883 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
884 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000885 bool IsVCMP = PatchedName.startswith("vcmp");
886 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000887 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000888 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000889 .Case("eq", 0)
890 .Case("lt", 1)
891 .Case("le", 2)
892 .Case("unord", 3)
893 .Case("neq", 4)
894 .Case("nlt", 5)
895 .Case("nle", 6)
896 .Case("ord", 7)
897 .Case("eq_uq", 8)
898 .Case("nge", 9)
899 .Case("ngt", 0x0A)
900 .Case("false", 0x0B)
901 .Case("neq_oq", 0x0C)
902 .Case("ge", 0x0D)
903 .Case("gt", 0x0E)
904 .Case("true", 0x0F)
905 .Case("eq_os", 0x10)
906 .Case("lt_oq", 0x11)
907 .Case("le_oq", 0x12)
908 .Case("unord_s", 0x13)
909 .Case("neq_us", 0x14)
910 .Case("nlt_uq", 0x15)
911 .Case("nle_uq", 0x16)
912 .Case("ord_s", 0x17)
913 .Case("eq_us", 0x18)
914 .Case("nge_uq", 0x19)
915 .Case("ngt_uq", 0x1A)
916 .Case("false_os", 0x1B)
917 .Case("neq_os", 0x1C)
918 .Case("ge_oq", 0x1D)
919 .Case("gt_oq", 0x1E)
920 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000921 .Default(~0U);
922 if (SSEComparisonCode != ~0U) {
923 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
924 getParser().getContext());
925 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000926 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000927 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000928 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000929 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000930 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000931 } else {
932 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000933 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000934 }
935 }
936 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000937
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000938 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000939
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000940 if (ExtraImmOp)
941 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000942
943
Chris Lattner2544f422010-09-08 05:17:37 +0000944 // Determine whether this is an instruction prefix.
945 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000946 Name == "lock" || Name == "rep" ||
947 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000948 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000949 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000950
951
Chris Lattner2544f422010-09-08 05:17:37 +0000952 // This does the actual operand parsing. Don't parse any more if we have a
953 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
954 // just want to parse the "lock" as the first instruction and the "incl" as
955 // the next one.
956 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000957
958 // Parse '*' modifier.
959 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000960 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000961 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000962 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000963 }
964
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000965 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000966 if (X86Operand *Op = ParseOperand())
967 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000968 else {
969 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000970 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000971 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000972
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000973 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000974 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000975
976 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000977 if (X86Operand *Op = ParseOperand())
978 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000979 else {
980 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000981 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000982 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000983 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000984
Chris Lattnercbf8a982010-09-11 16:18:25 +0000985 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000986 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000987 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000988 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000989 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000990 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991
Chris Lattner2544f422010-09-08 05:17:37 +0000992 if (getLexer().is(AsmToken::EndOfStatement))
993 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000994 else if (isPrefix && getLexer().is(AsmToken::Slash))
995 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000996
Chris Lattner98c870f2010-11-06 19:25:43 +0000997 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
998 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
999 // documented form in various unofficial manuals, so a lot of code uses it.
1000 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1001 Operands.size() == 3) {
1002 X86Operand &Op = *(X86Operand*)Operands.back();
1003 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1004 isa<MCConstantExpr>(Op.Mem.Disp) &&
1005 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1006 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1007 SMLoc Loc = Op.getEndLoc();
1008 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1009 delete &Op;
1010 }
1011 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001012 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1013 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1014 Operands.size() == 3) {
1015 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1016 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1017 isa<MCConstantExpr>(Op.Mem.Disp) &&
1018 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1019 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1020 SMLoc Loc = Op.getEndLoc();
1021 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1022 delete &Op;
1023 }
1024 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001025 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1026 if (Name.startswith("ins") && Operands.size() == 3 &&
1027 (Name == "insb" || Name == "insw" || Name == "insl")) {
1028 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1029 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1030 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1031 Operands.pop_back();
1032 Operands.pop_back();
1033 delete &Op;
1034 delete &Op2;
1035 }
1036 }
1037
1038 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1039 if (Name.startswith("outs") && Operands.size() == 3 &&
1040 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1041 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1042 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1043 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1044 Operands.pop_back();
1045 Operands.pop_back();
1046 delete &Op;
1047 delete &Op2;
1048 }
1049 }
1050
1051 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1052 if (Name.startswith("movs") && Operands.size() == 3 &&
1053 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001054 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001055 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1056 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1057 if (isSrcOp(Op) && isDstOp(Op2)) {
1058 Operands.pop_back();
1059 Operands.pop_back();
1060 delete &Op;
1061 delete &Op2;
1062 }
1063 }
1064 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1065 if (Name.startswith("lods") && Operands.size() == 3 &&
1066 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001067 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001068 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1069 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1070 if (isSrcOp(*Op1) && Op2->isReg()) {
1071 const char *ins;
1072 unsigned reg = Op2->getReg();
1073 bool isLods = Name == "lods";
1074 if (reg == X86::AL && (isLods || Name == "lodsb"))
1075 ins = "lodsb";
1076 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1077 ins = "lodsw";
1078 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1079 ins = "lodsl";
1080 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1081 ins = "lodsq";
1082 else
1083 ins = NULL;
1084 if (ins != NULL) {
1085 Operands.pop_back();
1086 Operands.pop_back();
1087 delete Op1;
1088 delete Op2;
1089 if (Name != ins)
1090 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1091 }
1092 }
1093 }
1094 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1095 if (Name.startswith("stos") && Operands.size() == 3 &&
1096 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001097 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001098 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1099 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1100 if (isDstOp(*Op2) && Op1->isReg()) {
1101 const char *ins;
1102 unsigned reg = Op1->getReg();
1103 bool isStos = Name == "stos";
1104 if (reg == X86::AL && (isStos || Name == "stosb"))
1105 ins = "stosb";
1106 else if (reg == X86::AX && (isStos || Name == "stosw"))
1107 ins = "stosw";
1108 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1109 ins = "stosl";
1110 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1111 ins = "stosq";
1112 else
1113 ins = NULL;
1114 if (ins != NULL) {
1115 Operands.pop_back();
1116 Operands.pop_back();
1117 delete Op1;
1118 delete Op2;
1119 if (Name != ins)
1120 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1121 }
1122 }
1123 }
1124
Chris Lattnere9e16a32010-09-15 04:33:27 +00001125 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001126 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001127 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001128 Name.startswith("shl") || Name.startswith("sal") ||
1129 Name.startswith("rcl") || Name.startswith("rcr") ||
1130 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001131 Operands.size() == 3) {
1132 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1133 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1134 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1135 delete Operands[1];
1136 Operands.erase(Operands.begin() + 1);
1137 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001138 }
Chris Lattner15f89512011-04-09 19:41:05 +00001139
1140 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1141 // instalias with an immediate operand yet.
1142 if (Name == "int" && Operands.size() == 2) {
1143 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1144 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1145 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1146 delete Operands[1];
1147 Operands.erase(Operands.begin() + 1);
1148 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1149 }
1150 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001151
Chris Lattner98986712010-01-14 22:21:20 +00001152 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001153}
1154
Devang Pateldd929fc2012-01-12 18:03:40 +00001155bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001156MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001157 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001158 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001159 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001160 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1161 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001162
Chris Lattner7c51a312010-09-29 01:50:45 +00001163 // First, handle aliases that expand to multiple instructions.
1164 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001165 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1166 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001167 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001168 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001169 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001170 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001171 MCInst Inst;
1172 Inst.setOpcode(X86::WAIT);
1173 Out.EmitInstruction(Inst);
1174
Chris Lattner0bb83a82010-09-30 16:39:29 +00001175 const char *Repl =
1176 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001177 .Case("finit", "fninit")
1178 .Case("fsave", "fnsave")
1179 .Case("fstcw", "fnstcw")
1180 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001181 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001182 .Case("fstsw", "fnstsw")
1183 .Case("fstsww", "fnstsw")
1184 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001185 .Default(0);
1186 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001187 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001188 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001189 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001190
Chris Lattnera008e8a2010-09-06 21:54:15 +00001191 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001192 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001193 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001194
Daniel Dunbarc918d602010-05-04 16:12:42 +00001195 // First, try a direct match.
Devang Patel0a338862012-01-12 01:36:43 +00001196 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1197 getParser().getAssemblerDialect())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001198 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001199 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +00001200 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001201 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001202 case Match_MissingFeature:
1203 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1204 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001205 case Match_ConversionFail:
1206 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001207 case Match_InvalidOperand:
1208 WasOriginallyInvalidOperand = true;
1209 break;
1210 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001211 break;
1212 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001213
Daniel Dunbarc918d602010-05-04 16:12:42 +00001214 // FIXME: Ideally, we would only attempt suffix matches for things which are
1215 // valid prefixes, and we could just infer the right unambiguous
1216 // type. However, that requires substantially more matcher support than the
1217 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001218
Daniel Dunbarc918d602010-05-04 16:12:42 +00001219 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001220 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001221 SmallString<16> Tmp;
1222 Tmp += Base;
1223 Tmp += ' ';
1224 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001225
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001226 // If this instruction starts with an 'f', then it is a floating point stack
1227 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1228 // 80-bit floating point, which use the suffixes s,l,t respectively.
1229 //
1230 // Otherwise, we assume that this may be an integer instruction, which comes
1231 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1232 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1233
Daniel Dunbarc918d602010-05-04 16:12:42 +00001234 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001235 Tmp[Base.size()] = Suffixes[0];
1236 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001237 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001238
1239 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1240 Tmp[Base.size()] = Suffixes[1];
1241 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1242 Tmp[Base.size()] = Suffixes[2];
1243 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1244 Tmp[Base.size()] = Suffixes[3];
1245 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001246
1247 // Restore the old token.
1248 Op->setTokenValue(Base);
1249
1250 // If exactly one matched, then we treat that as a successful match (and the
1251 // instruction will already have been filled in correctly, since the failing
1252 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001253 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001254 (Match1 == Match_Success) + (Match2 == Match_Success) +
1255 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001256 if (NumSuccessfulMatches == 1) {
1257 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001258 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001259 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001260
Chris Lattnerec6789f2010-09-06 20:08:02 +00001261 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001262
Daniel Dunbar09062b12010-08-12 00:55:42 +00001263 // If we had multiple suffix matches, then identify this as an ambiguous
1264 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001265 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001266 char MatchChars[4];
1267 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001268 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1269 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1270 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1271 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001272
1273 SmallString<126> Msg;
1274 raw_svector_ostream OS(Msg);
1275 OS << "ambiguous instructions require an explicit suffix (could be ";
1276 for (unsigned i = 0; i != NumMatches; ++i) {
1277 if (i != 0)
1278 OS << ", ";
1279 if (i + 1 == NumMatches)
1280 OS << "or ";
1281 OS << "'" << Base << MatchChars[i] << "'";
1282 }
1283 OS << ")";
1284 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001285 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001286 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001287
Chris Lattnera008e8a2010-09-06 21:54:15 +00001288 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001289
Chris Lattnera008e8a2010-09-06 21:54:15 +00001290 // If all of the instructions reported an invalid mnemonic, then the original
1291 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001292 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1293 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001294 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001295 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1296 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001297 }
1298
1299 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001300 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001301 if (OrigErrorInfo >= Operands.size())
1302 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001303
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001304 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1305 if (Operand->getStartLoc().isValid()) {
1306 SMRange OperandRange = Operand->getLocRange();
1307 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1308 OperandRange);
1309 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001310 }
1311
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001312 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001313 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001314
Chris Lattnerec6789f2010-09-06 20:08:02 +00001315 // If one instruction matched with a missing feature, report this as a
1316 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001317 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1318 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001319 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1320 return true;
1321 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001322
Chris Lattnera008e8a2010-09-06 21:54:15 +00001323 // If one instruction matched with an invalid operand, report this as an
1324 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001325 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1326 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001327 Error(IDLoc, "invalid operand for instruction");
1328 return true;
1329 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001330
Chris Lattnerec6789f2010-09-06 20:08:02 +00001331 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001332 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001333 return true;
1334}
1335
1336
Devang Pateldd929fc2012-01-12 18:03:40 +00001337bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001338 StringRef IDVal = DirectiveID.getIdentifier();
1339 if (IDVal == ".word")
1340 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001341 else if (IDVal.startswith(".code"))
1342 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001343 return true;
1344}
1345
1346/// ParseDirectiveWord
1347/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001348bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001349 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1350 for (;;) {
1351 const MCExpr *Value;
1352 if (getParser().ParseExpression(Value))
1353 return true;
1354
1355 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1356
1357 if (getLexer().is(AsmToken::EndOfStatement))
1358 break;
1359
1360 // FIXME: Improve diagnostic.
1361 if (getLexer().isNot(AsmToken::Comma))
1362 return Error(L, "unexpected token in directive");
1363 Parser.Lex();
1364 }
1365 }
1366
1367 Parser.Lex();
1368 return false;
1369}
1370
Evan Chengbd27f5a2011-07-27 00:38:12 +00001371/// ParseDirectiveCode
1372/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001373bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001374 if (IDVal == ".code32") {
1375 Parser.Lex();
1376 if (is64BitMode()) {
1377 SwitchMode();
1378 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1379 }
1380 } else if (IDVal == ".code64") {
1381 Parser.Lex();
1382 if (!is64BitMode()) {
1383 SwitchMode();
1384 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1385 }
1386 } else {
1387 return Error(L, "unexpected directive " + IDVal);
1388 }
Chris Lattner537ca842010-10-30 17:38:55 +00001389
Evan Chengbd27f5a2011-07-27 00:38:12 +00001390 return false;
1391}
Chris Lattner537ca842010-10-30 17:38:55 +00001392
1393
Sean Callanane88f5522010-01-23 02:43:15 +00001394extern "C" void LLVMInitializeX86AsmLexer();
1395
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001396// Force static initialization.
1397extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001398 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1399 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001400 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001401}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001402
Chris Lattner0692ee62010-09-06 19:11:01 +00001403#define GET_REGISTER_MATCHER
1404#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001405#include "X86GenAsmMatcher.inc"