blob: 6133cb379190eaafd4ab4ac5a1df82de85fef7fb [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
Evan Cheng94b95502011-07-26 00:24:13 +000034class X86ATTAsmParser : 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
Chris Lattner309264d2010-01-15 18:44:13 +000048 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000049 X86Operand *ParseATTOperand();
50 X86Operand *ParseIntelOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000051 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000052
53 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000054 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000055
Chris Lattner7036f8b2010-09-29 01:42:58 +000056 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000057 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000058 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000059
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000060 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
61 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
62 bool isSrcOp(X86Operand &Op);
63
64 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
65 /// or %es:(%edi) in 32bit mode.
66 bool isDstOp(X86Operand &Op);
67
Evan Cheng59ee62d2011-07-11 03:57:24 +000068 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000069 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000070 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000071 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000072 void SwitchMode() {
73 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
74 setAvailableFeatures(FB);
75 }
Evan Chengebdeeab2011-07-08 01:53:10 +000076
Daniel Dunbar54074b52010-07-19 05:44:09 +000077 /// @name Auto-generated Matcher Functions
78 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000079
Chris Lattner0692ee62010-09-06 19:11:01 +000080#define GET_ASSEMBLER_HEADER
81#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000082
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000083 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000084
85public:
Evan Chengffc0e732011-07-09 05:47:46 +000086 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000087 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000088
Daniel Dunbar54074b52010-07-19 05:44:09 +000089 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000090 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000091 }
Roman Divackybf755322011-01-27 17:14:22 +000092 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000093
Benjamin Kramer38e59892010-07-14 22:38:02 +000094 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000095 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000096
97 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000098};
Chris Lattner37dfdec2009-07-29 06:33:53 +000099} // end anonymous namespace
100
Sean Callanane9b466d2010-01-23 00:40:33 +0000101/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000102/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000103
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000104static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000105
106/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000107
108namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000109
110/// X86Operand - Instances of this class represent a parsed X86 machine
111/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000112struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000113 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000114 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000115 Register,
116 Immediate,
117 Memory
118 } Kind;
119
Chris Lattner29ef9a22010-01-15 18:51:29 +0000120 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000121
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122 union {
123 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000124 const char *Data;
125 unsigned Length;
126 } Tok;
127
128 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000129 unsigned RegNo;
130 } Reg;
131
132 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000133 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000134 } Imm;
135
136 struct {
137 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000138 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000139 unsigned BaseReg;
140 unsigned IndexReg;
141 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000142 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000143 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000144 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000145
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000146 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000147 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000148
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000149 /// getStartLoc - Get the location of the first token of this operand.
150 SMLoc getStartLoc() const { return StartLoc; }
151 /// getEndLoc - Get the location of the last token of this operand.
152 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000153
154 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000155
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000156 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000157
Daniel Dunbar20927f22009-08-07 08:26:05 +0000158 StringRef getToken() const {
159 assert(Kind == Token && "Invalid access!");
160 return StringRef(Tok.Data, Tok.Length);
161 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000162 void setTokenValue(StringRef Value) {
163 assert(Kind == Token && "Invalid access!");
164 Tok.Data = Value.data();
165 Tok.Length = Value.size();
166 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000167
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000168 unsigned getReg() const {
169 assert(Kind == Register && "Invalid access!");
170 return Reg.RegNo;
171 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000172
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000173 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000174 assert(Kind == Immediate && "Invalid access!");
175 return Imm.Val;
176 }
177
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000178 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000179 assert(Kind == Memory && "Invalid access!");
180 return Mem.Disp;
181 }
182 unsigned getMemSegReg() const {
183 assert(Kind == Memory && "Invalid access!");
184 return Mem.SegReg;
185 }
186 unsigned getMemBaseReg() const {
187 assert(Kind == Memory && "Invalid access!");
188 return Mem.BaseReg;
189 }
190 unsigned getMemIndexReg() const {
191 assert(Kind == Memory && "Invalid access!");
192 return Mem.IndexReg;
193 }
194 unsigned getMemScale() const {
195 assert(Kind == Memory && "Invalid access!");
196 return Mem.Scale;
197 }
198
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000199 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000200
201 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000202
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000203 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000204 if (!isImm())
205 return false;
206
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000207 // If this isn't a constant expr, just assume it fits and let relaxation
208 // handle it.
209 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
210 if (!CE)
211 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000212
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000213 // Otherwise, check the value is in a range that makes sense for this
214 // extension.
215 uint64_t Value = CE->getValue();
216 return (( Value <= 0x000000000000007FULL)||
217 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
218 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000219 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000220 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000221 if (!isImm())
222 return false;
223
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000224 // If this isn't a constant expr, just assume it fits and let relaxation
225 // handle it.
226 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
227 if (!CE)
228 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000229
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000230 // Otherwise, check the value is in a range that makes sense for this
231 // extension.
232 uint64_t Value = CE->getValue();
233 return (( Value <= 0x000000000000007FULL)||
234 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
235 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
236 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000237 bool isImmZExtu32u8() const {
238 if (!isImm())
239 return false;
240
241 // If this isn't a constant expr, just assume it fits and let relaxation
242 // handle it.
243 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
244 if (!CE)
245 return true;
246
247 // Otherwise, check the value is in a range that makes sense for this
248 // extension.
249 uint64_t Value = CE->getValue();
250 return (Value <= 0x00000000000000FFULL);
251 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000252 bool isImmSExti64i8() const {
253 if (!isImm())
254 return false;
255
256 // If this isn't a constant expr, just assume it fits and let relaxation
257 // handle it.
258 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
259 if (!CE)
260 return true;
261
262 // Otherwise, check the value is in a range that makes sense for this
263 // extension.
264 uint64_t Value = CE->getValue();
265 return (( Value <= 0x000000000000007FULL)||
266 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
267 }
268 bool isImmSExti64i32() const {
269 if (!isImm())
270 return false;
271
272 // If this isn't a constant expr, just assume it fits and let relaxation
273 // handle it.
274 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
275 if (!CE)
276 return true;
277
278 // Otherwise, check the value is in a range that makes sense for this
279 // extension.
280 uint64_t Value = CE->getValue();
281 return (( Value <= 0x000000007FFFFFFFULL)||
282 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000283 }
284
Daniel Dunbar20927f22009-08-07 08:26:05 +0000285 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000286 bool isMem8() const {
287 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
288 }
289 bool isMem16() const {
290 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
291 }
292 bool isMem32() const {
293 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
294 }
295 bool isMem64() const {
296 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
297 }
298 bool isMem80() const {
299 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
300 }
301 bool isMem128() const {
302 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
303 }
304 bool isMem256() const {
305 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
306 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000307
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000308 bool isAbsMem() const {
309 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000310 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000311 }
312
Daniel Dunbar20927f22009-08-07 08:26:05 +0000313 bool isReg() const { return Kind == Register; }
314
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000315 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
316 // Add as immediates when possible.
317 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
318 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
319 else
320 Inst.addOperand(MCOperand::CreateExpr(Expr));
321 }
322
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000323 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000324 assert(N == 1 && "Invalid number of operands!");
325 Inst.addOperand(MCOperand::CreateReg(getReg()));
326 }
327
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000328 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000329 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000330 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000331 }
332
Devang Patelc59d9df2012-01-12 01:51:42 +0000333 void addMem8Operands(MCInst &Inst, unsigned N) const {
334 addMemOperands(Inst, N);
335 }
336 void addMem16Operands(MCInst &Inst, unsigned N) const {
337 addMemOperands(Inst, N);
338 }
339 void addMem32Operands(MCInst &Inst, unsigned N) const {
340 addMemOperands(Inst, N);
341 }
342 void addMem64Operands(MCInst &Inst, unsigned N) const {
343 addMemOperands(Inst, N);
344 }
345 void addMem80Operands(MCInst &Inst, unsigned N) const {
346 addMemOperands(Inst, N);
347 }
348 void addMem128Operands(MCInst &Inst, unsigned N) const {
349 addMemOperands(Inst, N);
350 }
351 void addMem256Operands(MCInst &Inst, unsigned N) const {
352 addMemOperands(Inst, N);
353 }
354
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000355 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000356 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000357 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
358 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
359 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000360 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000361 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
362 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000364 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
365 assert((N == 1) && "Invalid number of operands!");
366 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
367 }
368
Chris Lattnerb4307b32010-01-15 19:28:38 +0000369 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000370 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
371 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000372 Res->Tok.Data = Str.data();
373 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000374 return Res;
375 }
376
Chris Lattner29ef9a22010-01-15 18:51:29 +0000377 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000378 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000379 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000380 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000381 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000382
Chris Lattnerb4307b32010-01-15 19:28:38 +0000383 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
384 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000385 Res->Imm.Val = Val;
386 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000387 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000388
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000389 /// Create an absolute memory operand.
390 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000391 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000392 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
393 Res->Mem.SegReg = 0;
394 Res->Mem.Disp = Disp;
395 Res->Mem.BaseReg = 0;
396 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000397 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000398 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000399 return Res;
400 }
401
402 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000403 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
404 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000405 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
406 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000407 // We should never just have a displacement, that should be parsed as an
408 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000409 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
410
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000411 // The scale should always be one of {1,2,4,8}.
412 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000413 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000414 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000415 Res->Mem.SegReg = SegReg;
416 Res->Mem.Disp = Disp;
417 Res->Mem.BaseReg = BaseReg;
418 Res->Mem.IndexReg = IndexReg;
419 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000421 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000422 }
423};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000424
Chris Lattner37dfdec2009-07-29 06:33:53 +0000425} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000426
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000427bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000428 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000429
430 return (Op.isMem() &&
431 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
432 isa<MCConstantExpr>(Op.Mem.Disp) &&
433 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
434 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
435}
436
437bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000438 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000439
440 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
441 isa<MCConstantExpr>(Op.Mem.Disp) &&
442 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
443 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
444}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000445
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
447 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000448 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000449 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000450 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000451 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000452 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000453
Sean Callanan18b83232010-01-19 21:44:56 +0000454 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000455 if (Tok.isNot(AsmToken::Identifier))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000456 return Error(StartLoc, "invalid register name",
457 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000458
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000459 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000460
Chris Lattner33d60d52010-09-22 04:11:10 +0000461 // If the match failed, try the register name as lowercase.
462 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000463 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000464
Evan Cheng5de728c2011-07-27 23:22:03 +0000465 if (!is64BitMode()) {
466 // FIXME: This should be done using Requires<In32BitMode> and
467 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
468 // checked.
469 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
470 // REX prefix.
471 if (RegNo == X86::RIZ ||
472 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
473 X86II::isX86_64NonExtLowByteReg(RegNo) ||
474 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000475 return Error(StartLoc, "register %"
476 + Tok.getString() + " is only available in 64-bit mode",
477 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000478 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000479
Chris Lattner33d60d52010-09-22 04:11:10 +0000480 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
481 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000482 RegNo = X86::ST0;
483 EndLoc = Tok.getLoc();
484 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000485
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000486 // Check to see if we have '(4)' after %st.
487 if (getLexer().isNot(AsmToken::LParen))
488 return false;
489 // Lex the paren.
490 getParser().Lex();
491
492 const AsmToken &IntTok = Parser.getTok();
493 if (IntTok.isNot(AsmToken::Integer))
494 return Error(IntTok.getLoc(), "expected stack index");
495 switch (IntTok.getIntVal()) {
496 case 0: RegNo = X86::ST0; break;
497 case 1: RegNo = X86::ST1; break;
498 case 2: RegNo = X86::ST2; break;
499 case 3: RegNo = X86::ST3; break;
500 case 4: RegNo = X86::ST4; break;
501 case 5: RegNo = X86::ST5; break;
502 case 6: RegNo = X86::ST6; break;
503 case 7: RegNo = X86::ST7; break;
504 default: return Error(IntTok.getLoc(), "invalid stack index");
505 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000506
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000507 if (getParser().Lex().isNot(AsmToken::RParen))
508 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000509
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000510 EndLoc = Tok.getLoc();
511 Parser.Lex(); // Eat ')'
512 return false;
513 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000514
Chris Lattner645b2092010-06-24 07:29:18 +0000515 // If this is "db[0-7]", match it as an alias
516 // for dr[0-7].
517 if (RegNo == 0 && Tok.getString().size() == 3 &&
518 Tok.getString().startswith("db")) {
519 switch (Tok.getString()[2]) {
520 case '0': RegNo = X86::DR0; break;
521 case '1': RegNo = X86::DR1; break;
522 case '2': RegNo = X86::DR2; break;
523 case '3': RegNo = X86::DR3; break;
524 case '4': RegNo = X86::DR4; break;
525 case '5': RegNo = X86::DR5; break;
526 case '6': RegNo = X86::DR6; break;
527 case '7': RegNo = X86::DR7; break;
528 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000529
Chris Lattner645b2092010-06-24 07:29:18 +0000530 if (RegNo != 0) {
531 EndLoc = Tok.getLoc();
532 Parser.Lex(); // Eat it.
533 return false;
534 }
535 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000536
Daniel Dunbar245f0582009-08-08 21:22:41 +0000537 if (RegNo == 0)
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000538 return Error(StartLoc, "invalid register name",
539 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000540
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000541 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000542 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000543 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000544}
545
Chris Lattner309264d2010-01-15 18:44:13 +0000546X86Operand *X86ATTAsmParser::ParseOperand() {
Devang Patel0a338862012-01-12 01:36:43 +0000547 if (getParser().getAssemblerDialect())
548 return ParseIntelOperand();
549 return ParseATTOperand();
550}
551
552/// getIntelRegister - If this is an intel register operand
553/// then return register number, otherwise return 0;
554static unsigned getIntelRegisterOperand(StringRef Str) {
555 unsigned RegNo = MatchRegisterName(Str);
556 // If the match failed, try the register name as lowercase.
557 if (RegNo == 0)
558 RegNo = MatchRegisterName(Str.lower());
559 return RegNo;
560}
561
562/// isIntelMemOperand - If this is an intel memory operand
563/// then return true.
564static bool isIntelMemOperand(StringRef OpStr, unsigned &Size) {
565 Size = 0;
566 if (OpStr == "BYTE") Size = 8;
567 if (OpStr == "WORD") Size = 16;
568 if (OpStr == "DWORD") Size = 32;
569 if (OpStr == "QWORD") Size = 64;
570 if (OpStr == "XWORD") Size = 80;
571 if (OpStr == "XMMWORD") Size = 128;
572 if (OpStr == "YMMWORD") Size = 256;
573 return Size != 0;
574}
575
576X86Operand *X86ATTAsmParser::ParseIntelOperand() {
577
578 const AsmToken &Tok = Parser.getTok();
579 SMLoc Start = Parser.getTok().getLoc(), End;
580
581 // register
582 if(unsigned RegNo = getIntelRegisterOperand(Tok.getString())) {
583 Parser.Lex();
584 End = Parser.getTok().getLoc();
585 return X86Operand::CreateReg(RegNo, Start, End);
586 }
587
588 // mem operand
589 unsigned SegReg = 0, BaseReg = 0, IndexReg = 0, Scale = 1;
590 StringRef OpStr = Tok.getString();
591 unsigned Size = 0;
592 if (isIntelMemOperand(OpStr, Size)) {
593 Parser.Lex();
594 if (Tok.getString() == "PTR")
595 Parser.Lex();
596 else {
597 Error(Start, "unexpected token!");
598 return 0;
599 }
600
601 if (Tok.getString() == "[")
602 Parser.Lex();
603 else {
604 Error(Start, "unexpected token!");
605 return 0;
606 }
607
608 SMLoc LParenLoc = Parser.getTok().getLoc();
609 BaseReg = getIntelRegisterOperand(Tok.getString());
610 if (BaseReg == 0) {
611 Error(LParenLoc, "unexpected token!");
612 return 0;
613 }
614 Parser.Lex();
615 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
616 SMLoc ExprEnd;
617 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
618 End = Parser.getTok().getLoc();
619 if (Tok.getString() == "]")
620 Parser.Lex();
621 if (BaseReg == 0) {
622 Error(End, "unexpected token!");
623 return 0;
624 }
625 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelc59d9df2012-01-12 01:51:42 +0000626 Start, End, Size);
Devang Patel0a338862012-01-12 01:36:43 +0000627 }
628
629 // immediate.
630 const MCExpr *Val;
631 if (!getParser().ParseExpression(Val, End)) {
632 End = Parser.getTok().getLoc();
633 return X86Operand::CreateImm(Val, Start, End);
634 }
635
636 return 0;
637}
638
639X86Operand *X86ATTAsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000640 switch (getLexer().getKind()) {
641 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000642 // Parse a memory operand with no segment register.
643 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000644 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000645 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000646 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000647 SMLoc Start, End;
648 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000649 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000650 Error(Start, "%eiz and %riz can only be used as index registers",
651 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000652 return 0;
653 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000654
Chris Lattnereef6d782010-04-17 18:56:34 +0000655 // If this is a segment register followed by a ':', then this is the start
656 // of a memory reference, otherwise this is a normal register reference.
657 if (getLexer().isNot(AsmToken::Colon))
658 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000659
660
Chris Lattnereef6d782010-04-17 18:56:34 +0000661 getParser().Lex(); // Eat the colon.
662 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000663 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000664 case AsmToken::Dollar: {
665 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000666 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000667 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000668 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000669 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000670 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000671 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000672 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000673 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000674}
675
Chris Lattnereef6d782010-04-17 18:56:34 +0000676/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
677/// has already been parsed if present.
678X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000679
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000680 // We have to disambiguate a parenthesized expression "(4+5)" from the start
681 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000682 // only way to do this without lookahead is to eat the '(' and see what is
683 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000684 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000685 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000686 SMLoc ExprEnd;
687 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000688
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000689 // After parsing the base expression we could either have a parenthesized
690 // memory address or not. If not, return now. If so, eat the (.
691 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000692 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000693 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000694 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000695 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000696 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000697
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000698 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000699 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000700 } else {
701 // Okay, we have a '('. We don't know if this is an expression or not, but
702 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000703 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000704 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000705
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000706 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000707 // Nothing to do here, fall into the code below with the '(' part of the
708 // memory operand consumed.
709 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000710 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000711
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000712 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000713 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000714 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000715
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000716 // After parsing the base expression we could either have a parenthesized
717 // memory address or not. If not, return now. If so, eat the (.
718 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000719 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000720 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000721 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000722 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000723 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000724
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000725 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000726 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000727 }
728 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000729
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000730 // If we reached here, then we just ate the ( of the memory operand. Process
731 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000732 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000733
Chris Lattner29ef9a22010-01-15 18:51:29 +0000734 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000735 SMLoc StartLoc, EndLoc;
736 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000737 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000738 Error(StartLoc, "eiz and riz can only be used as index registers",
739 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000740 return 0;
741 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000742 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000743
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000744 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000745 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000746
747 // Following the comma we should have either an index register, or a scale
748 // value. We don't support the later form, but we want to parse it
749 // correctly.
750 //
751 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000752 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000753 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000754 SMLoc L;
755 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000756
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000757 if (getLexer().isNot(AsmToken::RParen)) {
758 // Parse the scale amount:
759 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000760 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000761 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000762 "expected comma in scale expression");
763 return 0;
764 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000765 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000766
767 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000768 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000769
770 int64_t ScaleVal;
771 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000772 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000773
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000774 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000775 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
776 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
777 return 0;
778 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000779 Scale = (unsigned)ScaleVal;
780 }
781 }
782 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000783 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000784 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000785 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000786
787 int64_t Value;
788 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000789 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000790
Daniel Dunbaree910252010-08-24 19:13:38 +0000791 if (Value != 1)
792 Warning(Loc, "scale factor without index register is ignored");
793 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000794 }
795 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000796
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000797 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000798 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000799 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000800 return 0;
801 }
Sean Callanan18b83232010-01-19 21:44:56 +0000802 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000803 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000804
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000805 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
806 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000807}
808
Chris Lattner98986712010-01-14 22:21:20 +0000809bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000810ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000811 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000812 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000813
Chris Lattnerd8f71792010-11-28 20:23:50 +0000814 // FIXME: Hack to recognize setneb as setne.
815 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
816 PatchedName != "setb" && PatchedName != "setnb")
817 PatchedName = PatchedName.substr(0, Name.size()-1);
818
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000819 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
820 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000821 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000822 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
823 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000824 bool IsVCMP = PatchedName.startswith("vcmp");
825 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000826 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000827 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000828 .Case("eq", 0)
829 .Case("lt", 1)
830 .Case("le", 2)
831 .Case("unord", 3)
832 .Case("neq", 4)
833 .Case("nlt", 5)
834 .Case("nle", 6)
835 .Case("ord", 7)
836 .Case("eq_uq", 8)
837 .Case("nge", 9)
838 .Case("ngt", 0x0A)
839 .Case("false", 0x0B)
840 .Case("neq_oq", 0x0C)
841 .Case("ge", 0x0D)
842 .Case("gt", 0x0E)
843 .Case("true", 0x0F)
844 .Case("eq_os", 0x10)
845 .Case("lt_oq", 0x11)
846 .Case("le_oq", 0x12)
847 .Case("unord_s", 0x13)
848 .Case("neq_us", 0x14)
849 .Case("nlt_uq", 0x15)
850 .Case("nle_uq", 0x16)
851 .Case("ord_s", 0x17)
852 .Case("eq_us", 0x18)
853 .Case("nge_uq", 0x19)
854 .Case("ngt_uq", 0x1A)
855 .Case("false_os", 0x1B)
856 .Case("neq_os", 0x1C)
857 .Case("ge_oq", 0x1D)
858 .Case("gt_oq", 0x1E)
859 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000860 .Default(~0U);
861 if (SSEComparisonCode != ~0U) {
862 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
863 getParser().getContext());
864 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000865 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000866 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000867 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000868 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000869 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000870 } else {
871 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000872 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000873 }
874 }
875 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000876
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000877 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000878
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000879 if (ExtraImmOp)
880 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000881
882
Chris Lattner2544f422010-09-08 05:17:37 +0000883 // Determine whether this is an instruction prefix.
884 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000885 Name == "lock" || Name == "rep" ||
886 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000887 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000888 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000889
890
Chris Lattner2544f422010-09-08 05:17:37 +0000891 // This does the actual operand parsing. Don't parse any more if we have a
892 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
893 // just want to parse the "lock" as the first instruction and the "incl" as
894 // the next one.
895 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000896
897 // Parse '*' modifier.
898 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000899 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000900 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000901 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000902 }
903
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000904 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000905 if (X86Operand *Op = ParseOperand())
906 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000907 else {
908 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000909 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000910 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000911
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000912 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000913 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000914
915 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000916 if (X86Operand *Op = ParseOperand())
917 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000918 else {
919 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000921 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000922 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000923
Chris Lattnercbf8a982010-09-11 16:18:25 +0000924 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000925 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000926 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000927 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000928 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000929 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000930
Chris Lattner2544f422010-09-08 05:17:37 +0000931 if (getLexer().is(AsmToken::EndOfStatement))
932 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000933 else if (isPrefix && getLexer().is(AsmToken::Slash))
934 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935
Chris Lattner98c870f2010-11-06 19:25:43 +0000936 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
937 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
938 // documented form in various unofficial manuals, so a lot of code uses it.
939 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
940 Operands.size() == 3) {
941 X86Operand &Op = *(X86Operand*)Operands.back();
942 if (Op.isMem() && Op.Mem.SegReg == 0 &&
943 isa<MCConstantExpr>(Op.Mem.Disp) &&
944 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
945 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
946 SMLoc Loc = Op.getEndLoc();
947 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
948 delete &Op;
949 }
950 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000951 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
952 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
953 Operands.size() == 3) {
954 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
955 if (Op.isMem() && Op.Mem.SegReg == 0 &&
956 isa<MCConstantExpr>(Op.Mem.Disp) &&
957 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
958 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
959 SMLoc Loc = Op.getEndLoc();
960 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
961 delete &Op;
962 }
963 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000964 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
965 if (Name.startswith("ins") && Operands.size() == 3 &&
966 (Name == "insb" || Name == "insw" || Name == "insl")) {
967 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
968 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
969 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
970 Operands.pop_back();
971 Operands.pop_back();
972 delete &Op;
973 delete &Op2;
974 }
975 }
976
977 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
978 if (Name.startswith("outs") && Operands.size() == 3 &&
979 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
980 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
981 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
982 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
983 Operands.pop_back();
984 Operands.pop_back();
985 delete &Op;
986 delete &Op2;
987 }
988 }
989
990 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
991 if (Name.startswith("movs") && Operands.size() == 3 &&
992 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000993 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000994 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
995 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
996 if (isSrcOp(Op) && isDstOp(Op2)) {
997 Operands.pop_back();
998 Operands.pop_back();
999 delete &Op;
1000 delete &Op2;
1001 }
1002 }
1003 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1004 if (Name.startswith("lods") && Operands.size() == 3 &&
1005 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001006 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001007 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1008 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1009 if (isSrcOp(*Op1) && Op2->isReg()) {
1010 const char *ins;
1011 unsigned reg = Op2->getReg();
1012 bool isLods = Name == "lods";
1013 if (reg == X86::AL && (isLods || Name == "lodsb"))
1014 ins = "lodsb";
1015 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1016 ins = "lodsw";
1017 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1018 ins = "lodsl";
1019 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1020 ins = "lodsq";
1021 else
1022 ins = NULL;
1023 if (ins != NULL) {
1024 Operands.pop_back();
1025 Operands.pop_back();
1026 delete Op1;
1027 delete Op2;
1028 if (Name != ins)
1029 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1030 }
1031 }
1032 }
1033 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1034 if (Name.startswith("stos") && Operands.size() == 3 &&
1035 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001036 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001037 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1038 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1039 if (isDstOp(*Op2) && Op1->isReg()) {
1040 const char *ins;
1041 unsigned reg = Op1->getReg();
1042 bool isStos = Name == "stos";
1043 if (reg == X86::AL && (isStos || Name == "stosb"))
1044 ins = "stosb";
1045 else if (reg == X86::AX && (isStos || Name == "stosw"))
1046 ins = "stosw";
1047 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1048 ins = "stosl";
1049 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1050 ins = "stosq";
1051 else
1052 ins = NULL;
1053 if (ins != NULL) {
1054 Operands.pop_back();
1055 Operands.pop_back();
1056 delete Op1;
1057 delete Op2;
1058 if (Name != ins)
1059 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1060 }
1061 }
1062 }
1063
Chris Lattnere9e16a32010-09-15 04:33:27 +00001064 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001065 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001066 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001067 Name.startswith("shl") || Name.startswith("sal") ||
1068 Name.startswith("rcl") || Name.startswith("rcr") ||
1069 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001070 Operands.size() == 3) {
1071 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1072 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1073 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1074 delete Operands[1];
1075 Operands.erase(Operands.begin() + 1);
1076 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001077 }
Chris Lattner15f89512011-04-09 19:41:05 +00001078
1079 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1080 // instalias with an immediate operand yet.
1081 if (Name == "int" && Operands.size() == 2) {
1082 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1083 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1084 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1085 delete Operands[1];
1086 Operands.erase(Operands.begin() + 1);
1087 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1088 }
1089 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001090
Chris Lattner98986712010-01-14 22:21:20 +00001091 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001092}
1093
Chris Lattner2d592d12010-09-15 04:04:33 +00001094bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001095MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001096 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001097 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001098 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001099 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1100 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001101
Chris Lattner7c51a312010-09-29 01:50:45 +00001102 // First, handle aliases that expand to multiple instructions.
1103 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001104 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1105 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001106 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001107 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001108 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001109 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001110 MCInst Inst;
1111 Inst.setOpcode(X86::WAIT);
1112 Out.EmitInstruction(Inst);
1113
Chris Lattner0bb83a82010-09-30 16:39:29 +00001114 const char *Repl =
1115 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001116 .Case("finit", "fninit")
1117 .Case("fsave", "fnsave")
1118 .Case("fstcw", "fnstcw")
1119 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001120 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001121 .Case("fstsw", "fnstsw")
1122 .Case("fstsww", "fnstsw")
1123 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001124 .Default(0);
1125 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001126 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001127 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001128 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001129
Chris Lattnera008e8a2010-09-06 21:54:15 +00001130 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001131 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001132 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001133
Daniel Dunbarc918d602010-05-04 16:12:42 +00001134 // First, try a direct match.
Devang Patel0a338862012-01-12 01:36:43 +00001135 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1136 getParser().getAssemblerDialect())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001137 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001138 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +00001139 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001140 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001141 case Match_MissingFeature:
1142 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1143 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001144 case Match_ConversionFail:
1145 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001146 case Match_InvalidOperand:
1147 WasOriginallyInvalidOperand = true;
1148 break;
1149 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001150 break;
1151 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001152
Daniel Dunbarc918d602010-05-04 16:12:42 +00001153 // FIXME: Ideally, we would only attempt suffix matches for things which are
1154 // valid prefixes, and we could just infer the right unambiguous
1155 // type. However, that requires substantially more matcher support than the
1156 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001157
Daniel Dunbarc918d602010-05-04 16:12:42 +00001158 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001159 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001160 SmallString<16> Tmp;
1161 Tmp += Base;
1162 Tmp += ' ';
1163 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001164
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001165 // If this instruction starts with an 'f', then it is a floating point stack
1166 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1167 // 80-bit floating point, which use the suffixes s,l,t respectively.
1168 //
1169 // Otherwise, we assume that this may be an integer instruction, which comes
1170 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1171 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1172
Daniel Dunbarc918d602010-05-04 16:12:42 +00001173 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001174 Tmp[Base.size()] = Suffixes[0];
1175 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001176 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001177
1178 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1179 Tmp[Base.size()] = Suffixes[1];
1180 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1181 Tmp[Base.size()] = Suffixes[2];
1182 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1183 Tmp[Base.size()] = Suffixes[3];
1184 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001185
1186 // Restore the old token.
1187 Op->setTokenValue(Base);
1188
1189 // If exactly one matched, then we treat that as a successful match (and the
1190 // instruction will already have been filled in correctly, since the failing
1191 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001192 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001193 (Match1 == Match_Success) + (Match2 == Match_Success) +
1194 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001195 if (NumSuccessfulMatches == 1) {
1196 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001197 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001198 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001199
Chris Lattnerec6789f2010-09-06 20:08:02 +00001200 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001201
Daniel Dunbar09062b12010-08-12 00:55:42 +00001202 // If we had multiple suffix matches, then identify this as an ambiguous
1203 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001204 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001205 char MatchChars[4];
1206 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001207 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1208 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1209 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1210 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001211
1212 SmallString<126> Msg;
1213 raw_svector_ostream OS(Msg);
1214 OS << "ambiguous instructions require an explicit suffix (could be ";
1215 for (unsigned i = 0; i != NumMatches; ++i) {
1216 if (i != 0)
1217 OS << ", ";
1218 if (i + 1 == NumMatches)
1219 OS << "or ";
1220 OS << "'" << Base << MatchChars[i] << "'";
1221 }
1222 OS << ")";
1223 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001224 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001225 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001226
Chris Lattnera008e8a2010-09-06 21:54:15 +00001227 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001228
Chris Lattnera008e8a2010-09-06 21:54:15 +00001229 // If all of the instructions reported an invalid mnemonic, then the original
1230 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001231 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1232 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001233 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001234 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1235 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001236 }
1237
1238 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001239 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001240 if (OrigErrorInfo >= Operands.size())
1241 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001242
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001243 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1244 if (Operand->getStartLoc().isValid()) {
1245 SMRange OperandRange = Operand->getLocRange();
1246 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1247 OperandRange);
1248 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001249 }
1250
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001251 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001252 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001253
Chris Lattnerec6789f2010-09-06 20:08:02 +00001254 // If one instruction matched with a missing feature, report this as a
1255 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001256 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1257 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001258 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1259 return true;
1260 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001261
Chris Lattnera008e8a2010-09-06 21:54:15 +00001262 // If one instruction matched with an invalid operand, report this as an
1263 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001264 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1265 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001266 Error(IDLoc, "invalid operand for instruction");
1267 return true;
1268 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001269
Chris Lattnerec6789f2010-09-06 20:08:02 +00001270 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001271 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001272 return true;
1273}
1274
1275
Chris Lattner537ca842010-10-30 17:38:55 +00001276bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1277 StringRef IDVal = DirectiveID.getIdentifier();
1278 if (IDVal == ".word")
1279 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001280 else if (IDVal.startswith(".code"))
1281 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001282 return true;
1283}
1284
1285/// ParseDirectiveWord
1286/// ::= .word [ expression (, expression)* ]
1287bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1288 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1289 for (;;) {
1290 const MCExpr *Value;
1291 if (getParser().ParseExpression(Value))
1292 return true;
1293
1294 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1295
1296 if (getLexer().is(AsmToken::EndOfStatement))
1297 break;
1298
1299 // FIXME: Improve diagnostic.
1300 if (getLexer().isNot(AsmToken::Comma))
1301 return Error(L, "unexpected token in directive");
1302 Parser.Lex();
1303 }
1304 }
1305
1306 Parser.Lex();
1307 return false;
1308}
1309
Evan Chengbd27f5a2011-07-27 00:38:12 +00001310/// ParseDirectiveCode
1311/// ::= .code32 | .code64
1312bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1313 if (IDVal == ".code32") {
1314 Parser.Lex();
1315 if (is64BitMode()) {
1316 SwitchMode();
1317 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1318 }
1319 } else if (IDVal == ".code64") {
1320 Parser.Lex();
1321 if (!is64BitMode()) {
1322 SwitchMode();
1323 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1324 }
1325 } else {
1326 return Error(L, "unexpected directive " + IDVal);
1327 }
Chris Lattner537ca842010-10-30 17:38:55 +00001328
Evan Chengbd27f5a2011-07-27 00:38:12 +00001329 return false;
1330}
Chris Lattner537ca842010-10-30 17:38:55 +00001331
1332
Sean Callanane88f5522010-01-23 02:43:15 +00001333extern "C" void LLVMInitializeX86AsmLexer();
1334
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001335// Force static initialization.
1336extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001337 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1338 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001339 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001340}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001341
Chris Lattner0692ee62010-09-06 19:11:01 +00001342#define GET_REGISTER_MATCHER
1343#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001344#include "X86GenAsmMatcher.inc"