blob: 33a5557c5f7d8ba0bb6a7bed075e6c18c49a0030 [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;
142 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000143 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000144
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000145 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000146 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000147
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000148 /// getStartLoc - Get the location of the first token of this operand.
149 SMLoc getStartLoc() const { return StartLoc; }
150 /// getEndLoc - Get the location of the last token of this operand.
151 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000152
153 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000154
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000155 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000156
Daniel Dunbar20927f22009-08-07 08:26:05 +0000157 StringRef getToken() const {
158 assert(Kind == Token && "Invalid access!");
159 return StringRef(Tok.Data, Tok.Length);
160 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000161 void setTokenValue(StringRef Value) {
162 assert(Kind == Token && "Invalid access!");
163 Tok.Data = Value.data();
164 Tok.Length = Value.size();
165 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000166
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 unsigned getReg() const {
168 assert(Kind == Register && "Invalid access!");
169 return Reg.RegNo;
170 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000171
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000172 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000173 assert(Kind == Immediate && "Invalid access!");
174 return Imm.Val;
175 }
176
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000177 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000178 assert(Kind == Memory && "Invalid access!");
179 return Mem.Disp;
180 }
181 unsigned getMemSegReg() const {
182 assert(Kind == Memory && "Invalid access!");
183 return Mem.SegReg;
184 }
185 unsigned getMemBaseReg() const {
186 assert(Kind == Memory && "Invalid access!");
187 return Mem.BaseReg;
188 }
189 unsigned getMemIndexReg() const {
190 assert(Kind == Memory && "Invalid access!");
191 return Mem.IndexReg;
192 }
193 unsigned getMemScale() const {
194 assert(Kind == Memory && "Invalid access!");
195 return Mem.Scale;
196 }
197
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000198 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000199
200 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000201
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000202 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000203 if (!isImm())
204 return false;
205
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000206 // If this isn't a constant expr, just assume it fits and let relaxation
207 // handle it.
208 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
209 if (!CE)
210 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000211
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000212 // Otherwise, check the value is in a range that makes sense for this
213 // extension.
214 uint64_t Value = CE->getValue();
215 return (( Value <= 0x000000000000007FULL)||
216 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
217 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000218 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000219 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000220 if (!isImm())
221 return false;
222
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000223 // If this isn't a constant expr, just assume it fits and let relaxation
224 // handle it.
225 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
226 if (!CE)
227 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000228
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000229 // Otherwise, check the value is in a range that makes sense for this
230 // extension.
231 uint64_t Value = CE->getValue();
232 return (( Value <= 0x000000000000007FULL)||
233 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
234 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
235 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000236 bool isImmZExtu32u8() const {
237 if (!isImm())
238 return false;
239
240 // If this isn't a constant expr, just assume it fits and let relaxation
241 // handle it.
242 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
243 if (!CE)
244 return true;
245
246 // Otherwise, check the value is in a range that makes sense for this
247 // extension.
248 uint64_t Value = CE->getValue();
249 return (Value <= 0x00000000000000FFULL);
250 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 bool isImmSExti64i8() const {
252 if (!isImm())
253 return false;
254
255 // If this isn't a constant expr, just assume it fits and let relaxation
256 // handle it.
257 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
258 if (!CE)
259 return true;
260
261 // Otherwise, check the value is in a range that makes sense for this
262 // extension.
263 uint64_t Value = CE->getValue();
264 return (( Value <= 0x000000000000007FULL)||
265 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
266 }
267 bool isImmSExti64i32() const {
268 if (!isImm())
269 return false;
270
271 // If this isn't a constant expr, just assume it fits and let relaxation
272 // handle it.
273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
274 if (!CE)
275 return true;
276
277 // Otherwise, check the value is in a range that makes sense for this
278 // extension.
279 uint64_t Value = CE->getValue();
280 return (( Value <= 0x000000007FFFFFFFULL)||
281 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000282 }
283
Daniel Dunbar20927f22009-08-07 08:26:05 +0000284 bool isMem() const { return Kind == Memory; }
285
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000286 bool isAbsMem() const {
287 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000288 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000289 }
290
Daniel Dunbar20927f22009-08-07 08:26:05 +0000291 bool isReg() const { return Kind == Register; }
292
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000293 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
294 // Add as immediates when possible.
295 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
296 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
297 else
298 Inst.addOperand(MCOperand::CreateExpr(Expr));
299 }
300
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000301 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000302 assert(N == 1 && "Invalid number of operands!");
303 Inst.addOperand(MCOperand::CreateReg(getReg()));
304 }
305
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000306 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000307 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000308 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000309 }
310
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000311 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000312 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000313 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
314 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
315 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000316 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000317 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
318 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000319
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000320 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
321 assert((N == 1) && "Invalid number of operands!");
322 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
323 }
324
Chris Lattnerb4307b32010-01-15 19:28:38 +0000325 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000326 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
327 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000328 Res->Tok.Data = Str.data();
329 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000330 return Res;
331 }
332
Chris Lattner29ef9a22010-01-15 18:51:29 +0000333 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000334 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000335 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000336 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000337 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000338
Chris Lattnerb4307b32010-01-15 19:28:38 +0000339 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
340 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000341 Res->Imm.Val = Val;
342 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000343 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000344
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000345 /// Create an absolute memory operand.
346 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
347 SMLoc EndLoc) {
348 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
349 Res->Mem.SegReg = 0;
350 Res->Mem.Disp = Disp;
351 Res->Mem.BaseReg = 0;
352 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000353 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000354 return Res;
355 }
356
357 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000358 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
359 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000360 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000361 // We should never just have a displacement, that should be parsed as an
362 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000363 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
364
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000365 // The scale should always be one of {1,2,4,8}.
366 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000367 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000368 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000369 Res->Mem.SegReg = SegReg;
370 Res->Mem.Disp = Disp;
371 Res->Mem.BaseReg = BaseReg;
372 Res->Mem.IndexReg = IndexReg;
373 Res->Mem.Scale = Scale;
374 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000375 }
376};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000377
Chris Lattner37dfdec2009-07-29 06:33:53 +0000378} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000379
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000380bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000381 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000382
383 return (Op.isMem() &&
384 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
385 isa<MCConstantExpr>(Op.Mem.Disp) &&
386 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
387 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
388}
389
390bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000391 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000392
393 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
394 isa<MCConstantExpr>(Op.Mem.Disp) &&
395 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
396 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
397}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000398
Chris Lattner29ef9a22010-01-15 18:51:29 +0000399bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
400 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000401 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000402 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000403 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000404 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000405 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000406
Sean Callanan18b83232010-01-19 21:44:56 +0000407 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000408 if (Tok.isNot(AsmToken::Identifier))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000409 return Error(StartLoc, "invalid register name",
410 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000411
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000412 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000413
Chris Lattner33d60d52010-09-22 04:11:10 +0000414 // If the match failed, try the register name as lowercase.
415 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000416 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000417
Evan Cheng5de728c2011-07-27 23:22:03 +0000418 if (!is64BitMode()) {
419 // FIXME: This should be done using Requires<In32BitMode> and
420 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
421 // checked.
422 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
423 // REX prefix.
424 if (RegNo == X86::RIZ ||
425 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
426 X86II::isX86_64NonExtLowByteReg(RegNo) ||
427 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000428 return Error(StartLoc, "register %"
429 + Tok.getString() + " is only available in 64-bit mode",
430 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000431 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000432
Chris Lattner33d60d52010-09-22 04:11:10 +0000433 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
434 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000435 RegNo = X86::ST0;
436 EndLoc = Tok.getLoc();
437 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000438
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000439 // Check to see if we have '(4)' after %st.
440 if (getLexer().isNot(AsmToken::LParen))
441 return false;
442 // Lex the paren.
443 getParser().Lex();
444
445 const AsmToken &IntTok = Parser.getTok();
446 if (IntTok.isNot(AsmToken::Integer))
447 return Error(IntTok.getLoc(), "expected stack index");
448 switch (IntTok.getIntVal()) {
449 case 0: RegNo = X86::ST0; break;
450 case 1: RegNo = X86::ST1; break;
451 case 2: RegNo = X86::ST2; break;
452 case 3: RegNo = X86::ST3; break;
453 case 4: RegNo = X86::ST4; break;
454 case 5: RegNo = X86::ST5; break;
455 case 6: RegNo = X86::ST6; break;
456 case 7: RegNo = X86::ST7; break;
457 default: return Error(IntTok.getLoc(), "invalid stack index");
458 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000459
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000460 if (getParser().Lex().isNot(AsmToken::RParen))
461 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000462
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000463 EndLoc = Tok.getLoc();
464 Parser.Lex(); // Eat ')'
465 return false;
466 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000467
Chris Lattner645b2092010-06-24 07:29:18 +0000468 // If this is "db[0-7]", match it as an alias
469 // for dr[0-7].
470 if (RegNo == 0 && Tok.getString().size() == 3 &&
471 Tok.getString().startswith("db")) {
472 switch (Tok.getString()[2]) {
473 case '0': RegNo = X86::DR0; break;
474 case '1': RegNo = X86::DR1; break;
475 case '2': RegNo = X86::DR2; break;
476 case '3': RegNo = X86::DR3; break;
477 case '4': RegNo = X86::DR4; break;
478 case '5': RegNo = X86::DR5; break;
479 case '6': RegNo = X86::DR6; break;
480 case '7': RegNo = X86::DR7; break;
481 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000482
Chris Lattner645b2092010-06-24 07:29:18 +0000483 if (RegNo != 0) {
484 EndLoc = Tok.getLoc();
485 Parser.Lex(); // Eat it.
486 return false;
487 }
488 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000489
Daniel Dunbar245f0582009-08-08 21:22:41 +0000490 if (RegNo == 0)
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000491 return Error(StartLoc, "invalid register name",
492 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000493
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000494 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000495 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000496 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000497}
498
Chris Lattner309264d2010-01-15 18:44:13 +0000499X86Operand *X86ATTAsmParser::ParseOperand() {
Devang Patel0a338862012-01-12 01:36:43 +0000500 if (getParser().getAssemblerDialect())
501 return ParseIntelOperand();
502 return ParseATTOperand();
503}
504
505/// getIntelRegister - If this is an intel register operand
506/// then return register number, otherwise return 0;
507static unsigned getIntelRegisterOperand(StringRef Str) {
508 unsigned RegNo = MatchRegisterName(Str);
509 // If the match failed, try the register name as lowercase.
510 if (RegNo == 0)
511 RegNo = MatchRegisterName(Str.lower());
512 return RegNo;
513}
514
515/// isIntelMemOperand - If this is an intel memory operand
516/// then return true.
517static bool isIntelMemOperand(StringRef OpStr, unsigned &Size) {
518 Size = 0;
519 if (OpStr == "BYTE") Size = 8;
520 if (OpStr == "WORD") Size = 16;
521 if (OpStr == "DWORD") Size = 32;
522 if (OpStr == "QWORD") Size = 64;
523 if (OpStr == "XWORD") Size = 80;
524 if (OpStr == "XMMWORD") Size = 128;
525 if (OpStr == "YMMWORD") Size = 256;
526 return Size != 0;
527}
528
529X86Operand *X86ATTAsmParser::ParseIntelOperand() {
530
531 const AsmToken &Tok = Parser.getTok();
532 SMLoc Start = Parser.getTok().getLoc(), End;
533
534 // register
535 if(unsigned RegNo = getIntelRegisterOperand(Tok.getString())) {
536 Parser.Lex();
537 End = Parser.getTok().getLoc();
538 return X86Operand::CreateReg(RegNo, Start, End);
539 }
540
541 // mem operand
542 unsigned SegReg = 0, BaseReg = 0, IndexReg = 0, Scale = 1;
543 StringRef OpStr = Tok.getString();
544 unsigned Size = 0;
545 if (isIntelMemOperand(OpStr, Size)) {
546 Parser.Lex();
547 if (Tok.getString() == "PTR")
548 Parser.Lex();
549 else {
550 Error(Start, "unexpected token!");
551 return 0;
552 }
553
554 if (Tok.getString() == "[")
555 Parser.Lex();
556 else {
557 Error(Start, "unexpected token!");
558 return 0;
559 }
560
561 SMLoc LParenLoc = Parser.getTok().getLoc();
562 BaseReg = getIntelRegisterOperand(Tok.getString());
563 if (BaseReg == 0) {
564 Error(LParenLoc, "unexpected token!");
565 return 0;
566 }
567 Parser.Lex();
568 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
569 SMLoc ExprEnd;
570 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
571 End = Parser.getTok().getLoc();
572 if (Tok.getString() == "]")
573 Parser.Lex();
574 if (BaseReg == 0) {
575 Error(End, "unexpected token!");
576 return 0;
577 }
578 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
579 Start, End);
580 }
581
582 // immediate.
583 const MCExpr *Val;
584 if (!getParser().ParseExpression(Val, End)) {
585 End = Parser.getTok().getLoc();
586 return X86Operand::CreateImm(Val, Start, End);
587 }
588
589 return 0;
590}
591
592X86Operand *X86ATTAsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000593 switch (getLexer().getKind()) {
594 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000595 // Parse a memory operand with no segment register.
596 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000597 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000598 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000599 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000600 SMLoc Start, End;
601 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000602 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000603 Error(Start, "%eiz and %riz can only be used as index registers",
604 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000605 return 0;
606 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000607
Chris Lattnereef6d782010-04-17 18:56:34 +0000608 // If this is a segment register followed by a ':', then this is the start
609 // of a memory reference, otherwise this is a normal register reference.
610 if (getLexer().isNot(AsmToken::Colon))
611 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000612
613
Chris Lattnereef6d782010-04-17 18:56:34 +0000614 getParser().Lex(); // Eat the colon.
615 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000616 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000617 case AsmToken::Dollar: {
618 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000619 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000620 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000621 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000622 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000623 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000624 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000625 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000626 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000627}
628
Chris Lattnereef6d782010-04-17 18:56:34 +0000629/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
630/// has already been parsed if present.
631X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000632
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000633 // We have to disambiguate a parenthesized expression "(4+5)" from the start
634 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000635 // only way to do this without lookahead is to eat the '(' and see what is
636 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000637 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000638 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000639 SMLoc ExprEnd;
640 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000641
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000642 // After parsing the base expression we could either have a parenthesized
643 // memory address or not. If not, return now. If so, eat the (.
644 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000645 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000646 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000647 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000648 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000649 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000650
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000651 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000652 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000653 } else {
654 // Okay, we have a '('. We don't know if this is an expression or not, but
655 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000656 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000657 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000658
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000659 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000660 // Nothing to do here, fall into the code below with the '(' part of the
661 // memory operand consumed.
662 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000663 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000664
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000665 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000666 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000667 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000668
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000669 // After parsing the base expression we could either have a parenthesized
670 // memory address or not. If not, return now. If so, eat the (.
671 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000672 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000673 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000674 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000675 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000676 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000677
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000678 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000679 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000680 }
681 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000682
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000683 // If we reached here, then we just ate the ( of the memory operand. Process
684 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000685 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000686
Chris Lattner29ef9a22010-01-15 18:51:29 +0000687 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000688 SMLoc StartLoc, EndLoc;
689 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000690 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000691 Error(StartLoc, "eiz and riz can only be used as index registers",
692 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000693 return 0;
694 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000695 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000696
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000697 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000698 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000699
700 // Following the comma we should have either an index register, or a scale
701 // value. We don't support the later form, but we want to parse it
702 // correctly.
703 //
704 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000705 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000706 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000707 SMLoc L;
708 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000709
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000710 if (getLexer().isNot(AsmToken::RParen)) {
711 // Parse the scale amount:
712 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000713 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000714 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000715 "expected comma in scale expression");
716 return 0;
717 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000718 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000719
720 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000721 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000722
723 int64_t ScaleVal;
724 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000725 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000726
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000727 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000728 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
729 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
730 return 0;
731 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000732 Scale = (unsigned)ScaleVal;
733 }
734 }
735 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000736 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000737 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000738 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000739
740 int64_t Value;
741 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000742 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000743
Daniel Dunbaree910252010-08-24 19:13:38 +0000744 if (Value != 1)
745 Warning(Loc, "scale factor without index register is ignored");
746 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000747 }
748 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000749
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000751 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000752 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000753 return 0;
754 }
Sean Callanan18b83232010-01-19 21:44:56 +0000755 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000756 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000757
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000758 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
759 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000760}
761
Chris Lattner98986712010-01-14 22:21:20 +0000762bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000763ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000764 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000765 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000766
Chris Lattnerd8f71792010-11-28 20:23:50 +0000767 // FIXME: Hack to recognize setneb as setne.
768 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
769 PatchedName != "setb" && PatchedName != "setnb")
770 PatchedName = PatchedName.substr(0, Name.size()-1);
771
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000772 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
773 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000774 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000775 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
776 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000777 bool IsVCMP = PatchedName.startswith("vcmp");
778 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000779 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000780 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000781 .Case("eq", 0)
782 .Case("lt", 1)
783 .Case("le", 2)
784 .Case("unord", 3)
785 .Case("neq", 4)
786 .Case("nlt", 5)
787 .Case("nle", 6)
788 .Case("ord", 7)
789 .Case("eq_uq", 8)
790 .Case("nge", 9)
791 .Case("ngt", 0x0A)
792 .Case("false", 0x0B)
793 .Case("neq_oq", 0x0C)
794 .Case("ge", 0x0D)
795 .Case("gt", 0x0E)
796 .Case("true", 0x0F)
797 .Case("eq_os", 0x10)
798 .Case("lt_oq", 0x11)
799 .Case("le_oq", 0x12)
800 .Case("unord_s", 0x13)
801 .Case("neq_us", 0x14)
802 .Case("nlt_uq", 0x15)
803 .Case("nle_uq", 0x16)
804 .Case("ord_s", 0x17)
805 .Case("eq_us", 0x18)
806 .Case("nge_uq", 0x19)
807 .Case("ngt_uq", 0x1A)
808 .Case("false_os", 0x1B)
809 .Case("neq_os", 0x1C)
810 .Case("ge_oq", 0x1D)
811 .Case("gt_oq", 0x1E)
812 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000813 .Default(~0U);
814 if (SSEComparisonCode != ~0U) {
815 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
816 getParser().getContext());
817 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000818 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000819 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000820 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000821 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000822 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000823 } else {
824 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000825 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000826 }
827 }
828 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000829
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000830 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000831
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000832 if (ExtraImmOp)
833 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000834
835
Chris Lattner2544f422010-09-08 05:17:37 +0000836 // Determine whether this is an instruction prefix.
837 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000838 Name == "lock" || Name == "rep" ||
839 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000840 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000841 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000842
843
Chris Lattner2544f422010-09-08 05:17:37 +0000844 // This does the actual operand parsing. Don't parse any more if we have a
845 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
846 // just want to parse the "lock" as the first instruction and the "incl" as
847 // the next one.
848 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000849
850 // Parse '*' modifier.
851 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000852 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000853 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000854 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000855 }
856
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000857 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000858 if (X86Operand *Op = ParseOperand())
859 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000860 else {
861 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000863 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000864
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000865 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000866 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000867
868 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000869 if (X86Operand *Op = ParseOperand())
870 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000871 else {
872 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000873 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000874 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000875 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000876
Chris Lattnercbf8a982010-09-11 16:18:25 +0000877 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000878 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000879 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000880 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000881 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000882 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000883
Chris Lattner2544f422010-09-08 05:17:37 +0000884 if (getLexer().is(AsmToken::EndOfStatement))
885 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000886 else if (isPrefix && getLexer().is(AsmToken::Slash))
887 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000888
Chris Lattner98c870f2010-11-06 19:25:43 +0000889 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
890 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
891 // documented form in various unofficial manuals, so a lot of code uses it.
892 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
893 Operands.size() == 3) {
894 X86Operand &Op = *(X86Operand*)Operands.back();
895 if (Op.isMem() && Op.Mem.SegReg == 0 &&
896 isa<MCConstantExpr>(Op.Mem.Disp) &&
897 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
898 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
899 SMLoc Loc = Op.getEndLoc();
900 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
901 delete &Op;
902 }
903 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000904 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
905 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
906 Operands.size() == 3) {
907 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
908 if (Op.isMem() && Op.Mem.SegReg == 0 &&
909 isa<MCConstantExpr>(Op.Mem.Disp) &&
910 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
911 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
912 SMLoc Loc = Op.getEndLoc();
913 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
914 delete &Op;
915 }
916 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000917 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
918 if (Name.startswith("ins") && Operands.size() == 3 &&
919 (Name == "insb" || Name == "insw" || Name == "insl")) {
920 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
921 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
922 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
923 Operands.pop_back();
924 Operands.pop_back();
925 delete &Op;
926 delete &Op2;
927 }
928 }
929
930 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
931 if (Name.startswith("outs") && Operands.size() == 3 &&
932 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
933 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
934 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
935 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
936 Operands.pop_back();
937 Operands.pop_back();
938 delete &Op;
939 delete &Op2;
940 }
941 }
942
943 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
944 if (Name.startswith("movs") && Operands.size() == 3 &&
945 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000946 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000947 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
948 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
949 if (isSrcOp(Op) && isDstOp(Op2)) {
950 Operands.pop_back();
951 Operands.pop_back();
952 delete &Op;
953 delete &Op2;
954 }
955 }
956 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
957 if (Name.startswith("lods") && Operands.size() == 3 &&
958 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000959 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000960 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
961 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
962 if (isSrcOp(*Op1) && Op2->isReg()) {
963 const char *ins;
964 unsigned reg = Op2->getReg();
965 bool isLods = Name == "lods";
966 if (reg == X86::AL && (isLods || Name == "lodsb"))
967 ins = "lodsb";
968 else if (reg == X86::AX && (isLods || Name == "lodsw"))
969 ins = "lodsw";
970 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
971 ins = "lodsl";
972 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
973 ins = "lodsq";
974 else
975 ins = NULL;
976 if (ins != NULL) {
977 Operands.pop_back();
978 Operands.pop_back();
979 delete Op1;
980 delete Op2;
981 if (Name != ins)
982 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
983 }
984 }
985 }
986 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
987 if (Name.startswith("stos") && Operands.size() == 3 &&
988 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000989 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000990 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
991 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
992 if (isDstOp(*Op2) && Op1->isReg()) {
993 const char *ins;
994 unsigned reg = Op1->getReg();
995 bool isStos = Name == "stos";
996 if (reg == X86::AL && (isStos || Name == "stosb"))
997 ins = "stosb";
998 else if (reg == X86::AX && (isStos || Name == "stosw"))
999 ins = "stosw";
1000 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1001 ins = "stosl";
1002 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1003 ins = "stosq";
1004 else
1005 ins = NULL;
1006 if (ins != NULL) {
1007 Operands.pop_back();
1008 Operands.pop_back();
1009 delete Op1;
1010 delete Op2;
1011 if (Name != ins)
1012 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1013 }
1014 }
1015 }
1016
Chris Lattnere9e16a32010-09-15 04:33:27 +00001017 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001018 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001019 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001020 Name.startswith("shl") || Name.startswith("sal") ||
1021 Name.startswith("rcl") || Name.startswith("rcr") ||
1022 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001023 Operands.size() == 3) {
1024 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1025 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1026 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1027 delete Operands[1];
1028 Operands.erase(Operands.begin() + 1);
1029 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001030 }
Chris Lattner15f89512011-04-09 19:41:05 +00001031
1032 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1033 // instalias with an immediate operand yet.
1034 if (Name == "int" && Operands.size() == 2) {
1035 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1036 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1037 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1038 delete Operands[1];
1039 Operands.erase(Operands.begin() + 1);
1040 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1041 }
1042 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001043
Chris Lattner98986712010-01-14 22:21:20 +00001044 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001045}
1046
Chris Lattner2d592d12010-09-15 04:04:33 +00001047bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001048MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001049 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001050 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001051 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001052 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1053 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001054
Chris Lattner7c51a312010-09-29 01:50:45 +00001055 // First, handle aliases that expand to multiple instructions.
1056 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001057 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1058 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001059 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001060 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001061 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001062 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001063 MCInst Inst;
1064 Inst.setOpcode(X86::WAIT);
1065 Out.EmitInstruction(Inst);
1066
Chris Lattner0bb83a82010-09-30 16:39:29 +00001067 const char *Repl =
1068 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001069 .Case("finit", "fninit")
1070 .Case("fsave", "fnsave")
1071 .Case("fstcw", "fnstcw")
1072 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001073 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001074 .Case("fstsw", "fnstsw")
1075 .Case("fstsww", "fnstsw")
1076 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001077 .Default(0);
1078 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001079 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001080 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001081 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001082
Chris Lattnera008e8a2010-09-06 21:54:15 +00001083 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001084 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001085 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001086
Daniel Dunbarc918d602010-05-04 16:12:42 +00001087 // First, try a direct match.
Devang Patel0a338862012-01-12 01:36:43 +00001088 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1089 getParser().getAssemblerDialect())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001090 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001091 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +00001092 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001093 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001094 case Match_MissingFeature:
1095 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1096 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001097 case Match_ConversionFail:
1098 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001099 case Match_InvalidOperand:
1100 WasOriginallyInvalidOperand = true;
1101 break;
1102 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001103 break;
1104 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001105
Daniel Dunbarc918d602010-05-04 16:12:42 +00001106 // FIXME: Ideally, we would only attempt suffix matches for things which are
1107 // valid prefixes, and we could just infer the right unambiguous
1108 // type. However, that requires substantially more matcher support than the
1109 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Daniel Dunbarc918d602010-05-04 16:12:42 +00001111 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001112 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001113 SmallString<16> Tmp;
1114 Tmp += Base;
1115 Tmp += ' ';
1116 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001117
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001118 // If this instruction starts with an 'f', then it is a floating point stack
1119 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1120 // 80-bit floating point, which use the suffixes s,l,t respectively.
1121 //
1122 // Otherwise, we assume that this may be an integer instruction, which comes
1123 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1124 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1125
Daniel Dunbarc918d602010-05-04 16:12:42 +00001126 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001127 Tmp[Base.size()] = Suffixes[0];
1128 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001129 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001130
1131 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1132 Tmp[Base.size()] = Suffixes[1];
1133 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1134 Tmp[Base.size()] = Suffixes[2];
1135 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1136 Tmp[Base.size()] = Suffixes[3];
1137 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001138
1139 // Restore the old token.
1140 Op->setTokenValue(Base);
1141
1142 // If exactly one matched, then we treat that as a successful match (and the
1143 // instruction will already have been filled in correctly, since the failing
1144 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001145 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001146 (Match1 == Match_Success) + (Match2 == Match_Success) +
1147 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001148 if (NumSuccessfulMatches == 1) {
1149 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001150 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001151 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001152
Chris Lattnerec6789f2010-09-06 20:08:02 +00001153 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001154
Daniel Dunbar09062b12010-08-12 00:55:42 +00001155 // If we had multiple suffix matches, then identify this as an ambiguous
1156 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001157 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001158 char MatchChars[4];
1159 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001160 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1161 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1162 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1163 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001164
1165 SmallString<126> Msg;
1166 raw_svector_ostream OS(Msg);
1167 OS << "ambiguous instructions require an explicit suffix (could be ";
1168 for (unsigned i = 0; i != NumMatches; ++i) {
1169 if (i != 0)
1170 OS << ", ";
1171 if (i + 1 == NumMatches)
1172 OS << "or ";
1173 OS << "'" << Base << MatchChars[i] << "'";
1174 }
1175 OS << ")";
1176 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001177 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001178 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001179
Chris Lattnera008e8a2010-09-06 21:54:15 +00001180 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001181
Chris Lattnera008e8a2010-09-06 21:54:15 +00001182 // If all of the instructions reported an invalid mnemonic, then the original
1183 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001184 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1185 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001186 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001187 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1188 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001189 }
1190
1191 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001192 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001193 if (OrigErrorInfo >= Operands.size())
1194 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001195
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001196 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1197 if (Operand->getStartLoc().isValid()) {
1198 SMRange OperandRange = Operand->getLocRange();
1199 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1200 OperandRange);
1201 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001202 }
1203
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001204 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001205 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001206
Chris Lattnerec6789f2010-09-06 20:08:02 +00001207 // If one instruction matched with a missing feature, report this as a
1208 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001209 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1210 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001211 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1212 return true;
1213 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001214
Chris Lattnera008e8a2010-09-06 21:54:15 +00001215 // If one instruction matched with an invalid operand, report this as an
1216 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001217 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1218 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001219 Error(IDLoc, "invalid operand for instruction");
1220 return true;
1221 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001222
Chris Lattnerec6789f2010-09-06 20:08:02 +00001223 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001224 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001225 return true;
1226}
1227
1228
Chris Lattner537ca842010-10-30 17:38:55 +00001229bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1230 StringRef IDVal = DirectiveID.getIdentifier();
1231 if (IDVal == ".word")
1232 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001233 else if (IDVal.startswith(".code"))
1234 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001235 return true;
1236}
1237
1238/// ParseDirectiveWord
1239/// ::= .word [ expression (, expression)* ]
1240bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1241 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1242 for (;;) {
1243 const MCExpr *Value;
1244 if (getParser().ParseExpression(Value))
1245 return true;
1246
1247 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1248
1249 if (getLexer().is(AsmToken::EndOfStatement))
1250 break;
1251
1252 // FIXME: Improve diagnostic.
1253 if (getLexer().isNot(AsmToken::Comma))
1254 return Error(L, "unexpected token in directive");
1255 Parser.Lex();
1256 }
1257 }
1258
1259 Parser.Lex();
1260 return false;
1261}
1262
Evan Chengbd27f5a2011-07-27 00:38:12 +00001263/// ParseDirectiveCode
1264/// ::= .code32 | .code64
1265bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1266 if (IDVal == ".code32") {
1267 Parser.Lex();
1268 if (is64BitMode()) {
1269 SwitchMode();
1270 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1271 }
1272 } else if (IDVal == ".code64") {
1273 Parser.Lex();
1274 if (!is64BitMode()) {
1275 SwitchMode();
1276 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1277 }
1278 } else {
1279 return Error(L, "unexpected directive " + IDVal);
1280 }
Chris Lattner537ca842010-10-30 17:38:55 +00001281
Evan Chengbd27f5a2011-07-27 00:38:12 +00001282 return false;
1283}
Chris Lattner537ca842010-10-30 17:38:55 +00001284
1285
Sean Callanane88f5522010-01-23 02:43:15 +00001286extern "C" void LLVMInitializeX86AsmLexer();
1287
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001288// Force static initialization.
1289extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001290 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1291 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001292 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001293}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001294
Chris Lattner0692ee62010-09-06 19:11:01 +00001295#define GET_REGISTER_MATCHER
1296#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001297#include "X86GenAsmMatcher.inc"