blob: f4639a399eef56f0f77a7640d8f573ff85689b13 [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();
Chris Lattnereef6d782010-04-17 18:56:34 +000049 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000050
51 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000052 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000053
Chris Lattner7036f8b2010-09-29 01:42:58 +000054 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000055 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000056 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000057
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000058 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
59 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
60 bool isSrcOp(X86Operand &Op);
61
62 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
63 /// or %es:(%edi) in 32bit mode.
64 bool isDstOp(X86Operand &Op);
65
Evan Cheng59ee62d2011-07-11 03:57:24 +000066 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000067 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000068 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000069 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000070 void SwitchMode() {
71 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
72 setAvailableFeatures(FB);
73 }
Evan Chengebdeeab2011-07-08 01:53:10 +000074
Daniel Dunbar54074b52010-07-19 05:44:09 +000075 /// @name Auto-generated Matcher Functions
76 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000077
Chris Lattner0692ee62010-09-06 19:11:01 +000078#define GET_ASSEMBLER_HEADER
79#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000080
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000081 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000082
83public:
Evan Chengffc0e732011-07-09 05:47:46 +000084 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000085 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000086
Daniel Dunbar54074b52010-07-19 05:44:09 +000087 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000088 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000089 }
Roman Divackybf755322011-01-27 17:14:22 +000090 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000091
Benjamin Kramer38e59892010-07-14 22:38:02 +000092 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000093 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000094
95 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000096};
Chris Lattner37dfdec2009-07-29 06:33:53 +000097} // end anonymous namespace
98
Sean Callanane9b466d2010-01-23 00:40:33 +000099/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000100/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000101
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000102static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000103
104/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000105
106namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000107
108/// X86Operand - Instances of this class represent a parsed X86 machine
109/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000110struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000111 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000112 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000113 Register,
114 Immediate,
115 Memory
116 } Kind;
117
Chris Lattner29ef9a22010-01-15 18:51:29 +0000118 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000119
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000120 union {
121 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000122 const char *Data;
123 unsigned Length;
124 } Tok;
125
126 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000127 unsigned RegNo;
128 } Reg;
129
130 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000131 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000132 } Imm;
133
134 struct {
135 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000136 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000137 unsigned BaseReg;
138 unsigned IndexReg;
139 unsigned Scale;
140 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000141 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000142
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000143 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000144 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000145
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000146 /// getStartLoc - Get the location of the first token of this operand.
147 SMLoc getStartLoc() const { return StartLoc; }
148 /// getEndLoc - Get the location of the last token of this operand.
149 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000150
151 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000152
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000153 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000154
Daniel Dunbar20927f22009-08-07 08:26:05 +0000155 StringRef getToken() const {
156 assert(Kind == Token && "Invalid access!");
157 return StringRef(Tok.Data, Tok.Length);
158 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000159 void setTokenValue(StringRef Value) {
160 assert(Kind == Token && "Invalid access!");
161 Tok.Data = Value.data();
162 Tok.Length = Value.size();
163 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000164
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000165 unsigned getReg() const {
166 assert(Kind == Register && "Invalid access!");
167 return Reg.RegNo;
168 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000169
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000170 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000171 assert(Kind == Immediate && "Invalid access!");
172 return Imm.Val;
173 }
174
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000175 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000176 assert(Kind == Memory && "Invalid access!");
177 return Mem.Disp;
178 }
179 unsigned getMemSegReg() const {
180 assert(Kind == Memory && "Invalid access!");
181 return Mem.SegReg;
182 }
183 unsigned getMemBaseReg() const {
184 assert(Kind == Memory && "Invalid access!");
185 return Mem.BaseReg;
186 }
187 unsigned getMemIndexReg() const {
188 assert(Kind == Memory && "Invalid access!");
189 return Mem.IndexReg;
190 }
191 unsigned getMemScale() const {
192 assert(Kind == Memory && "Invalid access!");
193 return Mem.Scale;
194 }
195
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000196 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000197
198 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000199
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000200 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000201 if (!isImm())
202 return false;
203
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000204 // If this isn't a constant expr, just assume it fits and let relaxation
205 // handle it.
206 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
207 if (!CE)
208 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000209
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000210 // Otherwise, check the value is in a range that makes sense for this
211 // extension.
212 uint64_t Value = CE->getValue();
213 return (( Value <= 0x000000000000007FULL)||
214 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
215 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000216 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000217 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000218 if (!isImm())
219 return false;
220
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000221 // If this isn't a constant expr, just assume it fits and let relaxation
222 // handle it.
223 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
224 if (!CE)
225 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000226
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000227 // Otherwise, check the value is in a range that makes sense for this
228 // extension.
229 uint64_t Value = CE->getValue();
230 return (( Value <= 0x000000000000007FULL)||
231 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
232 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
233 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000234 bool isImmZExtu32u8() const {
235 if (!isImm())
236 return false;
237
238 // If this isn't a constant expr, just assume it fits and let relaxation
239 // handle it.
240 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
241 if (!CE)
242 return true;
243
244 // Otherwise, check the value is in a range that makes sense for this
245 // extension.
246 uint64_t Value = CE->getValue();
247 return (Value <= 0x00000000000000FFULL);
248 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000249 bool isImmSExti64i8() const {
250 if (!isImm())
251 return false;
252
253 // If this isn't a constant expr, just assume it fits and let relaxation
254 // handle it.
255 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
256 if (!CE)
257 return true;
258
259 // Otherwise, check the value is in a range that makes sense for this
260 // extension.
261 uint64_t Value = CE->getValue();
262 return (( Value <= 0x000000000000007FULL)||
263 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
264 }
265 bool isImmSExti64i32() const {
266 if (!isImm())
267 return false;
268
269 // If this isn't a constant expr, just assume it fits and let relaxation
270 // handle it.
271 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
272 if (!CE)
273 return true;
274
275 // Otherwise, check the value is in a range that makes sense for this
276 // extension.
277 uint64_t Value = CE->getValue();
278 return (( Value <= 0x000000007FFFFFFFULL)||
279 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000280 }
281
Daniel Dunbar20927f22009-08-07 08:26:05 +0000282 bool isMem() const { return Kind == Memory; }
283
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000284 bool isAbsMem() const {
285 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000286 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000287 }
288
Daniel Dunbar20927f22009-08-07 08:26:05 +0000289 bool isReg() const { return Kind == Register; }
290
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000291 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
292 // Add as immediates when possible.
293 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
294 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
295 else
296 Inst.addOperand(MCOperand::CreateExpr(Expr));
297 }
298
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000299 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000300 assert(N == 1 && "Invalid number of operands!");
301 Inst.addOperand(MCOperand::CreateReg(getReg()));
302 }
303
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000304 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000305 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000306 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000307 }
308
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000309 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000310 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000311 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
312 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
313 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000314 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000315 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
316 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000317
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000318 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
319 assert((N == 1) && "Invalid number of operands!");
320 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
321 }
322
Chris Lattnerb4307b32010-01-15 19:28:38 +0000323 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000324 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
325 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000326 Res->Tok.Data = Str.data();
327 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000328 return Res;
329 }
330
Chris Lattner29ef9a22010-01-15 18:51:29 +0000331 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000332 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000333 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000334 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000335 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000336
Chris Lattnerb4307b32010-01-15 19:28:38 +0000337 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
338 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000339 Res->Imm.Val = Val;
340 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000341 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000342
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000343 /// Create an absolute memory operand.
344 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
345 SMLoc EndLoc) {
346 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
347 Res->Mem.SegReg = 0;
348 Res->Mem.Disp = Disp;
349 Res->Mem.BaseReg = 0;
350 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000351 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000352 return Res;
353 }
354
355 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000356 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
357 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000358 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000359 // We should never just have a displacement, that should be parsed as an
360 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000361 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
362
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000363 // The scale should always be one of {1,2,4,8}.
364 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000365 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000366 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000367 Res->Mem.SegReg = SegReg;
368 Res->Mem.Disp = Disp;
369 Res->Mem.BaseReg = BaseReg;
370 Res->Mem.IndexReg = IndexReg;
371 Res->Mem.Scale = Scale;
372 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000373 }
374};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000375
Chris Lattner37dfdec2009-07-29 06:33:53 +0000376} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000377
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000378bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000379 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000380
381 return (Op.isMem() &&
382 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
383 isa<MCConstantExpr>(Op.Mem.Disp) &&
384 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
385 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
386}
387
388bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000389 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000390
391 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
392 isa<MCConstantExpr>(Op.Mem.Disp) &&
393 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
394 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
395}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000396
Chris Lattner29ef9a22010-01-15 18:51:29 +0000397bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
398 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000399 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000400 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000401 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000402 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000403 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000404
Sean Callanan18b83232010-01-19 21:44:56 +0000405 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000406 if (Tok.isNot(AsmToken::Identifier))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000407 return Error(StartLoc, "invalid register name",
408 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000409
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000410 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000411
Chris Lattner33d60d52010-09-22 04:11:10 +0000412 // If the match failed, try the register name as lowercase.
413 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000414 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000415
Evan Cheng5de728c2011-07-27 23:22:03 +0000416 if (!is64BitMode()) {
417 // FIXME: This should be done using Requires<In32BitMode> and
418 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
419 // checked.
420 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
421 // REX prefix.
422 if (RegNo == X86::RIZ ||
423 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
424 X86II::isX86_64NonExtLowByteReg(RegNo) ||
425 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000426 return Error(StartLoc, "register %"
427 + Tok.getString() + " is only available in 64-bit mode",
428 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000429 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000430
Chris Lattner33d60d52010-09-22 04:11:10 +0000431 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
432 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000433 RegNo = X86::ST0;
434 EndLoc = Tok.getLoc();
435 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000436
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000437 // Check to see if we have '(4)' after %st.
438 if (getLexer().isNot(AsmToken::LParen))
439 return false;
440 // Lex the paren.
441 getParser().Lex();
442
443 const AsmToken &IntTok = Parser.getTok();
444 if (IntTok.isNot(AsmToken::Integer))
445 return Error(IntTok.getLoc(), "expected stack index");
446 switch (IntTok.getIntVal()) {
447 case 0: RegNo = X86::ST0; break;
448 case 1: RegNo = X86::ST1; break;
449 case 2: RegNo = X86::ST2; break;
450 case 3: RegNo = X86::ST3; break;
451 case 4: RegNo = X86::ST4; break;
452 case 5: RegNo = X86::ST5; break;
453 case 6: RegNo = X86::ST6; break;
454 case 7: RegNo = X86::ST7; break;
455 default: return Error(IntTok.getLoc(), "invalid stack index");
456 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000457
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000458 if (getParser().Lex().isNot(AsmToken::RParen))
459 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000460
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000461 EndLoc = Tok.getLoc();
462 Parser.Lex(); // Eat ')'
463 return false;
464 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000465
Chris Lattner645b2092010-06-24 07:29:18 +0000466 // If this is "db[0-7]", match it as an alias
467 // for dr[0-7].
468 if (RegNo == 0 && Tok.getString().size() == 3 &&
469 Tok.getString().startswith("db")) {
470 switch (Tok.getString()[2]) {
471 case '0': RegNo = X86::DR0; break;
472 case '1': RegNo = X86::DR1; break;
473 case '2': RegNo = X86::DR2; break;
474 case '3': RegNo = X86::DR3; break;
475 case '4': RegNo = X86::DR4; break;
476 case '5': RegNo = X86::DR5; break;
477 case '6': RegNo = X86::DR6; break;
478 case '7': RegNo = X86::DR7; break;
479 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000480
Chris Lattner645b2092010-06-24 07:29:18 +0000481 if (RegNo != 0) {
482 EndLoc = Tok.getLoc();
483 Parser.Lex(); // Eat it.
484 return false;
485 }
486 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000487
Daniel Dunbar245f0582009-08-08 21:22:41 +0000488 if (RegNo == 0)
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000489 return Error(StartLoc, "invalid register name",
490 SMRange(StartLoc, Tok.getEndLoc()));
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000491
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000492 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000493 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000494 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000495}
496
Chris Lattner309264d2010-01-15 18:44:13 +0000497X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000498 switch (getLexer().getKind()) {
499 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000500 // Parse a memory operand with no segment register.
501 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000502 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000503 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000504 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000505 SMLoc Start, End;
506 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000507 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000508 Error(Start, "%eiz and %riz can only be used as index registers",
509 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000510 return 0;
511 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000512
Chris Lattnereef6d782010-04-17 18:56:34 +0000513 // If this is a segment register followed by a ':', then this is the start
514 // of a memory reference, otherwise this is a normal register reference.
515 if (getLexer().isNot(AsmToken::Colon))
516 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000517
518
Chris Lattnereef6d782010-04-17 18:56:34 +0000519 getParser().Lex(); // Eat the colon.
520 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000521 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 case AsmToken::Dollar: {
523 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000524 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000525 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000526 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000527 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000528 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000529 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000530 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000531 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000532}
533
Chris Lattnereef6d782010-04-17 18:56:34 +0000534/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
535/// has already been parsed if present.
536X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000537
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 // We have to disambiguate a parenthesized expression "(4+5)" from the start
539 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000540 // only way to do this without lookahead is to eat the '(' and see what is
541 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000542 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000543 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000544 SMLoc ExprEnd;
545 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000546
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000547 // After parsing the base expression we could either have a parenthesized
548 // memory address or not. If not, return now. If so, eat the (.
549 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000550 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000551 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000552 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000553 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000555
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000556 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000557 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000558 } else {
559 // Okay, we have a '('. We don't know if this is an expression or not, but
560 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000561 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000562 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000563
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000564 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000565 // Nothing to do here, fall into the code below with the '(' part of the
566 // memory operand consumed.
567 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000569
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000570 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000571 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000572 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000573
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000574 // After parsing the base expression we could either have a parenthesized
575 // memory address or not. If not, return now. If so, eat the (.
576 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000577 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000578 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000579 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000580 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000581 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000582
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000583 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000584 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000585 }
586 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000587
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000588 // If we reached here, then we just ate the ( of the memory operand. Process
589 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000590 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000591
Chris Lattner29ef9a22010-01-15 18:51:29 +0000592 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000593 SMLoc StartLoc, EndLoc;
594 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000595 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000596 Error(StartLoc, "eiz and riz can only be used as index registers",
597 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000598 return 0;
599 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000600 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000601
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000602 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000603 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000604
605 // Following the comma we should have either an index register, or a scale
606 // value. We don't support the later form, but we want to parse it
607 // correctly.
608 //
609 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000610 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000611 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000612 SMLoc L;
613 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000614
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000615 if (getLexer().isNot(AsmToken::RParen)) {
616 // Parse the scale amount:
617 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000618 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000619 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000620 "expected comma in scale expression");
621 return 0;
622 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000623 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000624
625 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000626 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000627
628 int64_t ScaleVal;
629 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000630 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000631
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000632 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000633 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
634 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
635 return 0;
636 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000637 Scale = (unsigned)ScaleVal;
638 }
639 }
640 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000641 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000642 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000643 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000644
645 int64_t Value;
646 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000647 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000648
Daniel Dunbaree910252010-08-24 19:13:38 +0000649 if (Value != 1)
650 Warning(Loc, "scale factor without index register is ignored");
651 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000652 }
653 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000654
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000655 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000656 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000657 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000658 return 0;
659 }
Sean Callanan18b83232010-01-19 21:44:56 +0000660 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000661 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000662
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000663 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
664 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000665}
666
Chris Lattner98986712010-01-14 22:21:20 +0000667bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000668ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000669 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000670 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000671
Chris Lattnerd8f71792010-11-28 20:23:50 +0000672 // FIXME: Hack to recognize setneb as setne.
673 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
674 PatchedName != "setb" && PatchedName != "setnb")
675 PatchedName = PatchedName.substr(0, Name.size()-1);
676
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000677 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
678 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000679 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000680 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
681 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000682 bool IsVCMP = PatchedName.startswith("vcmp");
683 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000684 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000685 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000686 .Case("eq", 0)
687 .Case("lt", 1)
688 .Case("le", 2)
689 .Case("unord", 3)
690 .Case("neq", 4)
691 .Case("nlt", 5)
692 .Case("nle", 6)
693 .Case("ord", 7)
694 .Case("eq_uq", 8)
695 .Case("nge", 9)
696 .Case("ngt", 0x0A)
697 .Case("false", 0x0B)
698 .Case("neq_oq", 0x0C)
699 .Case("ge", 0x0D)
700 .Case("gt", 0x0E)
701 .Case("true", 0x0F)
702 .Case("eq_os", 0x10)
703 .Case("lt_oq", 0x11)
704 .Case("le_oq", 0x12)
705 .Case("unord_s", 0x13)
706 .Case("neq_us", 0x14)
707 .Case("nlt_uq", 0x15)
708 .Case("nle_uq", 0x16)
709 .Case("ord_s", 0x17)
710 .Case("eq_us", 0x18)
711 .Case("nge_uq", 0x19)
712 .Case("ngt_uq", 0x1A)
713 .Case("false_os", 0x1B)
714 .Case("neq_os", 0x1C)
715 .Case("ge_oq", 0x1D)
716 .Case("gt_oq", 0x1E)
717 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000718 .Default(~0U);
719 if (SSEComparisonCode != ~0U) {
720 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
721 getParser().getContext());
722 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000723 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000724 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000725 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000726 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000727 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000728 } else {
729 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000730 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000731 }
732 }
733 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000734
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000735 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000736
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000737 if (ExtraImmOp)
738 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000739
740
Chris Lattner2544f422010-09-08 05:17:37 +0000741 // Determine whether this is an instruction prefix.
742 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000743 Name == "lock" || Name == "rep" ||
744 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000745 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000746 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000747
748
Chris Lattner2544f422010-09-08 05:17:37 +0000749 // This does the actual operand parsing. Don't parse any more if we have a
750 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
751 // just want to parse the "lock" as the first instruction and the "incl" as
752 // the next one.
753 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000754
755 // Parse '*' modifier.
756 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000757 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000758 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000759 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000760 }
761
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000762 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000763 if (X86Operand *Op = ParseOperand())
764 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000765 else {
766 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000767 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000768 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000769
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000770 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000771 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000772
773 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000774 if (X86Operand *Op = ParseOperand())
775 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000776 else {
777 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000778 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000779 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000780 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000781
Chris Lattnercbf8a982010-09-11 16:18:25 +0000782 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000783 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000784 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000785 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000786 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000787 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000788
Chris Lattner2544f422010-09-08 05:17:37 +0000789 if (getLexer().is(AsmToken::EndOfStatement))
790 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000791 else if (isPrefix && getLexer().is(AsmToken::Slash))
792 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000793
Chris Lattner98c870f2010-11-06 19:25:43 +0000794 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
795 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
796 // documented form in various unofficial manuals, so a lot of code uses it.
797 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
798 Operands.size() == 3) {
799 X86Operand &Op = *(X86Operand*)Operands.back();
800 if (Op.isMem() && Op.Mem.SegReg == 0 &&
801 isa<MCConstantExpr>(Op.Mem.Disp) &&
802 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
803 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
804 SMLoc Loc = Op.getEndLoc();
805 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
806 delete &Op;
807 }
808 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000809 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
810 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
811 Operands.size() == 3) {
812 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
813 if (Op.isMem() && Op.Mem.SegReg == 0 &&
814 isa<MCConstantExpr>(Op.Mem.Disp) &&
815 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
816 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
817 SMLoc Loc = Op.getEndLoc();
818 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
819 delete &Op;
820 }
821 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000822 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
823 if (Name.startswith("ins") && Operands.size() == 3 &&
824 (Name == "insb" || Name == "insw" || Name == "insl")) {
825 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
826 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
827 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
828 Operands.pop_back();
829 Operands.pop_back();
830 delete &Op;
831 delete &Op2;
832 }
833 }
834
835 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
836 if (Name.startswith("outs") && Operands.size() == 3 &&
837 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
838 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
839 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
840 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
841 Operands.pop_back();
842 Operands.pop_back();
843 delete &Op;
844 delete &Op2;
845 }
846 }
847
848 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
849 if (Name.startswith("movs") && Operands.size() == 3 &&
850 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000851 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000852 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
853 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
854 if (isSrcOp(Op) && isDstOp(Op2)) {
855 Operands.pop_back();
856 Operands.pop_back();
857 delete &Op;
858 delete &Op2;
859 }
860 }
861 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
862 if (Name.startswith("lods") && Operands.size() == 3 &&
863 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000864 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000865 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
866 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
867 if (isSrcOp(*Op1) && Op2->isReg()) {
868 const char *ins;
869 unsigned reg = Op2->getReg();
870 bool isLods = Name == "lods";
871 if (reg == X86::AL && (isLods || Name == "lodsb"))
872 ins = "lodsb";
873 else if (reg == X86::AX && (isLods || Name == "lodsw"))
874 ins = "lodsw";
875 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
876 ins = "lodsl";
877 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
878 ins = "lodsq";
879 else
880 ins = NULL;
881 if (ins != NULL) {
882 Operands.pop_back();
883 Operands.pop_back();
884 delete Op1;
885 delete Op2;
886 if (Name != ins)
887 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
888 }
889 }
890 }
891 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
892 if (Name.startswith("stos") && Operands.size() == 3 &&
893 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000894 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000895 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
896 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
897 if (isDstOp(*Op2) && Op1->isReg()) {
898 const char *ins;
899 unsigned reg = Op1->getReg();
900 bool isStos = Name == "stos";
901 if (reg == X86::AL && (isStos || Name == "stosb"))
902 ins = "stosb";
903 else if (reg == X86::AX && (isStos || Name == "stosw"))
904 ins = "stosw";
905 else if (reg == X86::EAX && (isStos || Name == "stosl"))
906 ins = "stosl";
907 else if (reg == X86::RAX && (isStos || Name == "stosq"))
908 ins = "stosq";
909 else
910 ins = NULL;
911 if (ins != NULL) {
912 Operands.pop_back();
913 Operands.pop_back();
914 delete Op1;
915 delete Op2;
916 if (Name != ins)
917 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
918 }
919 }
920 }
921
Chris Lattnere9e16a32010-09-15 04:33:27 +0000922 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000923 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000924 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000925 Name.startswith("shl") || Name.startswith("sal") ||
926 Name.startswith("rcl") || Name.startswith("rcr") ||
927 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000928 Operands.size() == 3) {
929 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
930 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
931 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
932 delete Operands[1];
933 Operands.erase(Operands.begin() + 1);
934 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000935 }
Chris Lattner15f89512011-04-09 19:41:05 +0000936
937 // Transforms "int $3" into "int3" as a size optimization. We can't write an
938 // instalias with an immediate operand yet.
939 if (Name == "int" && Operands.size() == 2) {
940 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
941 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
942 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
943 delete Operands[1];
944 Operands.erase(Operands.begin() + 1);
945 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
946 }
947 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000948
Chris Lattner98986712010-01-14 22:21:20 +0000949 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000950}
951
Chris Lattner2d592d12010-09-15 04:04:33 +0000952bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000953MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000954 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000955 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000956 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000957 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
958 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000959
Chris Lattner7c51a312010-09-29 01:50:45 +0000960 // First, handle aliases that expand to multiple instructions.
961 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000962 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
963 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000964 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000965 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000966 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000967 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000968 MCInst Inst;
969 Inst.setOpcode(X86::WAIT);
970 Out.EmitInstruction(Inst);
971
Chris Lattner0bb83a82010-09-30 16:39:29 +0000972 const char *Repl =
973 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000974 .Case("finit", "fninit")
975 .Case("fsave", "fnsave")
976 .Case("fstcw", "fnstcw")
977 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000978 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000979 .Case("fstsw", "fnstsw")
980 .Case("fstsww", "fnstsw")
981 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000982 .Default(0);
983 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000984 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000985 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000986 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000987
Chris Lattnera008e8a2010-09-06 21:54:15 +0000988 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000989 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000990 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991
Daniel Dunbarc918d602010-05-04 16:12:42 +0000992 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000993 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +0000994 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000995 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000996 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000997 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000998 case Match_MissingFeature:
999 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1000 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001001 case Match_ConversionFail:
1002 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001003 case Match_InvalidOperand:
1004 WasOriginallyInvalidOperand = true;
1005 break;
1006 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001007 break;
1008 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001009
Daniel Dunbarc918d602010-05-04 16:12:42 +00001010 // FIXME: Ideally, we would only attempt suffix matches for things which are
1011 // valid prefixes, and we could just infer the right unambiguous
1012 // type. However, that requires substantially more matcher support than the
1013 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001014
Daniel Dunbarc918d602010-05-04 16:12:42 +00001015 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001016 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001017 SmallString<16> Tmp;
1018 Tmp += Base;
1019 Tmp += ' ';
1020 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001021
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001022 // If this instruction starts with an 'f', then it is a floating point stack
1023 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1024 // 80-bit floating point, which use the suffixes s,l,t respectively.
1025 //
1026 // Otherwise, we assume that this may be an integer instruction, which comes
1027 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1028 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1029
Daniel Dunbarc918d602010-05-04 16:12:42 +00001030 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001031 Tmp[Base.size()] = Suffixes[0];
1032 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001033 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001034
1035 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1036 Tmp[Base.size()] = Suffixes[1];
1037 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1038 Tmp[Base.size()] = Suffixes[2];
1039 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1040 Tmp[Base.size()] = Suffixes[3];
1041 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001042
1043 // Restore the old token.
1044 Op->setTokenValue(Base);
1045
1046 // If exactly one matched, then we treat that as a successful match (and the
1047 // instruction will already have been filled in correctly, since the failing
1048 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001049 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001050 (Match1 == Match_Success) + (Match2 == Match_Success) +
1051 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001052 if (NumSuccessfulMatches == 1) {
1053 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001054 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001055 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001056
Chris Lattnerec6789f2010-09-06 20:08:02 +00001057 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001058
Daniel Dunbar09062b12010-08-12 00:55:42 +00001059 // If we had multiple suffix matches, then identify this as an ambiguous
1060 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001061 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001062 char MatchChars[4];
1063 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001064 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1065 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1066 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1067 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001068
1069 SmallString<126> Msg;
1070 raw_svector_ostream OS(Msg);
1071 OS << "ambiguous instructions require an explicit suffix (could be ";
1072 for (unsigned i = 0; i != NumMatches; ++i) {
1073 if (i != 0)
1074 OS << ", ";
1075 if (i + 1 == NumMatches)
1076 OS << "or ";
1077 OS << "'" << Base << MatchChars[i] << "'";
1078 }
1079 OS << ")";
1080 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001081 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001082 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001083
Chris Lattnera008e8a2010-09-06 21:54:15 +00001084 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001085
Chris Lattnera008e8a2010-09-06 21:54:15 +00001086 // If all of the instructions reported an invalid mnemonic, then the original
1087 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001088 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1089 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001090 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001091 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1092 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001093 }
1094
1095 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001096 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001097 if (OrigErrorInfo >= Operands.size())
1098 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001100 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1101 if (Operand->getStartLoc().isValid()) {
1102 SMRange OperandRange = Operand->getLocRange();
1103 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1104 OperandRange);
1105 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001106 }
1107
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001108 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001109 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattnerec6789f2010-09-06 20:08:02 +00001111 // If one instruction matched with a missing feature, report this as a
1112 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001113 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1114 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001115 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1116 return true;
1117 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001118
Chris Lattnera008e8a2010-09-06 21:54:15 +00001119 // If one instruction matched with an invalid operand, report this as an
1120 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001121 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1122 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001123 Error(IDLoc, "invalid operand for instruction");
1124 return true;
1125 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001126
Chris Lattnerec6789f2010-09-06 20:08:02 +00001127 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001128 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001129 return true;
1130}
1131
1132
Chris Lattner537ca842010-10-30 17:38:55 +00001133bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1134 StringRef IDVal = DirectiveID.getIdentifier();
1135 if (IDVal == ".word")
1136 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001137 else if (IDVal.startswith(".code"))
1138 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001139 return true;
1140}
1141
1142/// ParseDirectiveWord
1143/// ::= .word [ expression (, expression)* ]
1144bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1145 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1146 for (;;) {
1147 const MCExpr *Value;
1148 if (getParser().ParseExpression(Value))
1149 return true;
1150
1151 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1152
1153 if (getLexer().is(AsmToken::EndOfStatement))
1154 break;
1155
1156 // FIXME: Improve diagnostic.
1157 if (getLexer().isNot(AsmToken::Comma))
1158 return Error(L, "unexpected token in directive");
1159 Parser.Lex();
1160 }
1161 }
1162
1163 Parser.Lex();
1164 return false;
1165}
1166
Evan Chengbd27f5a2011-07-27 00:38:12 +00001167/// ParseDirectiveCode
1168/// ::= .code32 | .code64
1169bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1170 if (IDVal == ".code32") {
1171 Parser.Lex();
1172 if (is64BitMode()) {
1173 SwitchMode();
1174 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1175 }
1176 } else if (IDVal == ".code64") {
1177 Parser.Lex();
1178 if (!is64BitMode()) {
1179 SwitchMode();
1180 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1181 }
1182 } else {
1183 return Error(L, "unexpected directive " + IDVal);
1184 }
Chris Lattner537ca842010-10-30 17:38:55 +00001185
Evan Chengbd27f5a2011-07-27 00:38:12 +00001186 return false;
1187}
Chris Lattner537ca842010-10-30 17:38:55 +00001188
1189
Sean Callanane88f5522010-01-23 02:43:15 +00001190extern "C" void LLVMInitializeX86AsmLexer();
1191
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001192// Force static initialization.
1193extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001194 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1195 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001196 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001197}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001198
Chris Lattner0692ee62010-09-06 19:11:01 +00001199#define GET_REGISTER_MATCHER
1200#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001201#include "X86GenAsmMatcher.inc"