blob: efa8a6e1547e7b63dafc7b99d60af5208cf5c47d [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"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/Target/TargetRegistry.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000013#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.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"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.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
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000043 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
44
Chris Lattner309264d2010-01-15 18:44:13 +000045 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000046 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000047
48 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000049 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000050
Chris Lattner7036f8b2010-09-29 01:42:58 +000051 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000052 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000053 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000054
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000055 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
56 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
57 bool isSrcOp(X86Operand &Op);
58
59 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
60 /// or %es:(%edi) in 32bit mode.
61 bool isDstOp(X86Operand &Op);
62
Evan Cheng59ee62d2011-07-11 03:57:24 +000063 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000064 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000065 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000066 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000067 void SwitchMode() {
68 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
69 setAvailableFeatures(FB);
70 }
Evan Chengebdeeab2011-07-08 01:53:10 +000071
Daniel Dunbar54074b52010-07-19 05:44:09 +000072 /// @name Auto-generated Matcher Functions
73 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000074
Chris Lattner0692ee62010-09-06 19:11:01 +000075#define GET_ASSEMBLER_HEADER
76#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000077
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000078 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000079
80public:
Evan Chengffc0e732011-07-09 05:47:46 +000081 X86ATTAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Evan Cheng94b95502011-07-26 00:24:13 +000082 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000083
Daniel Dunbar54074b52010-07-19 05:44:09 +000084 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000085 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000086 }
Roman Divackybf755322011-01-27 17:14:22 +000087 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000088
Benjamin Kramer38e59892010-07-14 22:38:02 +000089 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000090 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000091
92 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000093};
Chris Lattner37dfdec2009-07-29 06:33:53 +000094} // end anonymous namespace
95
Sean Callanane9b466d2010-01-23 00:40:33 +000096/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +000097/// {
Sean Callanane9b466d2010-01-23 00:40:33 +000098
Chris Lattnerb8d6e982010-02-09 00:34:28 +000099static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000100
101/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000102
103namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000104
105/// X86Operand - Instances of this class represent a parsed X86 machine
106/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000107struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000108 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000109 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000110 Register,
111 Immediate,
112 Memory
113 } Kind;
114
Chris Lattner29ef9a22010-01-15 18:51:29 +0000115 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000116
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000117 union {
118 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000119 const char *Data;
120 unsigned Length;
121 } Tok;
122
123 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000124 unsigned RegNo;
125 } Reg;
126
127 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000128 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000129 } Imm;
130
131 struct {
132 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000133 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000134 unsigned BaseReg;
135 unsigned IndexReg;
136 unsigned Scale;
137 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000138 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000139
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000140 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000141 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000142
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000143 /// getStartLoc - Get the location of the first token of this operand.
144 SMLoc getStartLoc() const { return StartLoc; }
145 /// getEndLoc - Get the location of the last token of this operand.
146 SMLoc getEndLoc() const { return EndLoc; }
147
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000148 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000149
Daniel Dunbar20927f22009-08-07 08:26:05 +0000150 StringRef getToken() const {
151 assert(Kind == Token && "Invalid access!");
152 return StringRef(Tok.Data, Tok.Length);
153 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000154 void setTokenValue(StringRef Value) {
155 assert(Kind == Token && "Invalid access!");
156 Tok.Data = Value.data();
157 Tok.Length = Value.size();
158 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000159
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 unsigned getReg() const {
161 assert(Kind == Register && "Invalid access!");
162 return Reg.RegNo;
163 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000164
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000165 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000166 assert(Kind == Immediate && "Invalid access!");
167 return Imm.Val;
168 }
169
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000170 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000171 assert(Kind == Memory && "Invalid access!");
172 return Mem.Disp;
173 }
174 unsigned getMemSegReg() const {
175 assert(Kind == Memory && "Invalid access!");
176 return Mem.SegReg;
177 }
178 unsigned getMemBaseReg() const {
179 assert(Kind == Memory && "Invalid access!");
180 return Mem.BaseReg;
181 }
182 unsigned getMemIndexReg() const {
183 assert(Kind == Memory && "Invalid access!");
184 return Mem.IndexReg;
185 }
186 unsigned getMemScale() const {
187 assert(Kind == Memory && "Invalid access!");
188 return Mem.Scale;
189 }
190
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000191 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000192
193 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000194
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000195 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000196 if (!isImm())
197 return false;
198
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000199 // If this isn't a constant expr, just assume it fits and let relaxation
200 // handle it.
201 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
202 if (!CE)
203 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000204
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000205 // Otherwise, check the value is in a range that makes sense for this
206 // extension.
207 uint64_t Value = CE->getValue();
208 return (( Value <= 0x000000000000007FULL)||
209 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
210 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000211 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000212 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000213 if (!isImm())
214 return false;
215
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000216 // If this isn't a constant expr, just assume it fits and let relaxation
217 // handle it.
218 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
219 if (!CE)
220 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000221
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000222 // Otherwise, check the value is in a range that makes sense for this
223 // extension.
224 uint64_t Value = CE->getValue();
225 return (( Value <= 0x000000000000007FULL)||
226 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
227 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
228 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000229 bool isImmZExtu32u8() const {
230 if (!isImm())
231 return false;
232
233 // If this isn't a constant expr, just assume it fits and let relaxation
234 // handle it.
235 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
236 if (!CE)
237 return true;
238
239 // Otherwise, check the value is in a range that makes sense for this
240 // extension.
241 uint64_t Value = CE->getValue();
242 return (Value <= 0x00000000000000FFULL);
243 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000244 bool isImmSExti64i8() const {
245 if (!isImm())
246 return false;
247
248 // If this isn't a constant expr, just assume it fits and let relaxation
249 // handle it.
250 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
251 if (!CE)
252 return true;
253
254 // Otherwise, check the value is in a range that makes sense for this
255 // extension.
256 uint64_t Value = CE->getValue();
257 return (( Value <= 0x000000000000007FULL)||
258 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
259 }
260 bool isImmSExti64i32() const {
261 if (!isImm())
262 return false;
263
264 // If this isn't a constant expr, just assume it fits and let relaxation
265 // handle it.
266 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
267 if (!CE)
268 return true;
269
270 // Otherwise, check the value is in a range that makes sense for this
271 // extension.
272 uint64_t Value = CE->getValue();
273 return (( Value <= 0x000000007FFFFFFFULL)||
274 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000275 }
276
Daniel Dunbar20927f22009-08-07 08:26:05 +0000277 bool isMem() const { return Kind == Memory; }
278
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000279 bool isAbsMem() const {
280 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000281 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000282 }
283
Daniel Dunbar20927f22009-08-07 08:26:05 +0000284 bool isReg() const { return Kind == Register; }
285
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000286 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
287 // Add as immediates when possible.
288 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
289 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
290 else
291 Inst.addOperand(MCOperand::CreateExpr(Expr));
292 }
293
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000294 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000295 assert(N == 1 && "Invalid number of operands!");
296 Inst.addOperand(MCOperand::CreateReg(getReg()));
297 }
298
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000299 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000300 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000301 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000302 }
303
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000304 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000305 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000306 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
307 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
308 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000309 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000310 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
311 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000312
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000313 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
314 assert((N == 1) && "Invalid number of operands!");
315 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
316 }
317
Chris Lattnerb4307b32010-01-15 19:28:38 +0000318 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
319 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000320 Res->Tok.Data = Str.data();
321 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000322 return Res;
323 }
324
Chris Lattner29ef9a22010-01-15 18:51:29 +0000325 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000326 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000327 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000328 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000329 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000330
Chris Lattnerb4307b32010-01-15 19:28:38 +0000331 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
332 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000333 Res->Imm.Val = Val;
334 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000335 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000336
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000337 /// Create an absolute memory operand.
338 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
339 SMLoc EndLoc) {
340 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
341 Res->Mem.SegReg = 0;
342 Res->Mem.Disp = Disp;
343 Res->Mem.BaseReg = 0;
344 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000345 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000346 return Res;
347 }
348
349 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000350 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
351 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000352 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000353 // We should never just have a displacement, that should be parsed as an
354 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000355 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
356
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000357 // The scale should always be one of {1,2,4,8}.
358 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000359 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000360 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000361 Res->Mem.SegReg = SegReg;
362 Res->Mem.Disp = Disp;
363 Res->Mem.BaseReg = BaseReg;
364 Res->Mem.IndexReg = IndexReg;
365 Res->Mem.Scale = Scale;
366 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000367 }
368};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000369
Chris Lattner37dfdec2009-07-29 06:33:53 +0000370} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000371
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000372bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000373 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000374
375 return (Op.isMem() &&
376 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
377 isa<MCConstantExpr>(Op.Mem.Disp) &&
378 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
379 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
380}
381
382bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000383 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000384
385 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
386 isa<MCConstantExpr>(Op.Mem.Disp) &&
387 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
388 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
389}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000390
Chris Lattner29ef9a22010-01-15 18:51:29 +0000391bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
392 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000393 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000394 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000395 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000396 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000397 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000398
Sean Callanan18b83232010-01-19 21:44:56 +0000399 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000400 if (Tok.isNot(AsmToken::Identifier))
401 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000402
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000403 // FIXME: Validate register for the current architecture; we have to do
404 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000405 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000406
Chris Lattner33d60d52010-09-22 04:11:10 +0000407 // If the match failed, try the register name as lowercase.
408 if (RegNo == 0)
409 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000410
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000411 // FIXME: This should be done using Requires<In32BitMode> and
412 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
413 // can be also checked.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000414 if (RegNo == X86::RIZ && !is64BitMode())
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000415 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
416
Chris Lattner33d60d52010-09-22 04:11:10 +0000417 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
418 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000419 RegNo = X86::ST0;
420 EndLoc = Tok.getLoc();
421 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000422
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000423 // Check to see if we have '(4)' after %st.
424 if (getLexer().isNot(AsmToken::LParen))
425 return false;
426 // Lex the paren.
427 getParser().Lex();
428
429 const AsmToken &IntTok = Parser.getTok();
430 if (IntTok.isNot(AsmToken::Integer))
431 return Error(IntTok.getLoc(), "expected stack index");
432 switch (IntTok.getIntVal()) {
433 case 0: RegNo = X86::ST0; break;
434 case 1: RegNo = X86::ST1; break;
435 case 2: RegNo = X86::ST2; break;
436 case 3: RegNo = X86::ST3; break;
437 case 4: RegNo = X86::ST4; break;
438 case 5: RegNo = X86::ST5; break;
439 case 6: RegNo = X86::ST6; break;
440 case 7: RegNo = X86::ST7; break;
441 default: return Error(IntTok.getLoc(), "invalid stack index");
442 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000443
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000444 if (getParser().Lex().isNot(AsmToken::RParen))
445 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000446
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000447 EndLoc = Tok.getLoc();
448 Parser.Lex(); // Eat ')'
449 return false;
450 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000451
Chris Lattner645b2092010-06-24 07:29:18 +0000452 // If this is "db[0-7]", match it as an alias
453 // for dr[0-7].
454 if (RegNo == 0 && Tok.getString().size() == 3 &&
455 Tok.getString().startswith("db")) {
456 switch (Tok.getString()[2]) {
457 case '0': RegNo = X86::DR0; break;
458 case '1': RegNo = X86::DR1; break;
459 case '2': RegNo = X86::DR2; break;
460 case '3': RegNo = X86::DR3; break;
461 case '4': RegNo = X86::DR4; break;
462 case '5': RegNo = X86::DR5; break;
463 case '6': RegNo = X86::DR6; break;
464 case '7': RegNo = X86::DR7; break;
465 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000466
Chris Lattner645b2092010-06-24 07:29:18 +0000467 if (RegNo != 0) {
468 EndLoc = Tok.getLoc();
469 Parser.Lex(); // Eat it.
470 return false;
471 }
472 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000473
Daniel Dunbar245f0582009-08-08 21:22:41 +0000474 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000475 return Error(Tok.getLoc(), "invalid register name");
476
Chris Lattner29ef9a22010-01-15 18:51:29 +0000477 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000478 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000479 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000480}
481
Chris Lattner309264d2010-01-15 18:44:13 +0000482X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000483 switch (getLexer().getKind()) {
484 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000485 // Parse a memory operand with no segment register.
486 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000487 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000488 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000489 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000490 SMLoc Start, End;
491 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000492 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
493 Error(Start, "eiz and riz can only be used as index registers");
494 return 0;
495 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000496
Chris Lattnereef6d782010-04-17 18:56:34 +0000497 // If this is a segment register followed by a ':', then this is the start
498 // of a memory reference, otherwise this is a normal register reference.
499 if (getLexer().isNot(AsmToken::Colon))
500 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000501
502
Chris Lattnereef6d782010-04-17 18:56:34 +0000503 getParser().Lex(); // Eat the colon.
504 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000505 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000506 case AsmToken::Dollar: {
507 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000508 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000509 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000510 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000511 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000512 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000513 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000514 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000515 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000516}
517
Chris Lattnereef6d782010-04-17 18:56:34 +0000518/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
519/// has already been parsed if present.
520X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000521
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 // We have to disambiguate a parenthesized expression "(4+5)" from the start
523 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000524 // only way to do this without lookahead is to eat the '(' and see what is
525 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000526 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000528 SMLoc ExprEnd;
529 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000530
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000531 // After parsing the base expression we could either have a parenthesized
532 // memory address or not. If not, return now. If so, eat the (.
533 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000534 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000535 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000536 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000537 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000539
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000540 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000541 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000542 } else {
543 // Okay, we have a '('. We don't know if this is an expression or not, but
544 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000545 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000546 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000547
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000548 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549 // Nothing to do here, fall into the code below with the '(' part of the
550 // memory operand consumed.
551 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000552 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000553
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000555 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000556 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000557
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000558 // After parsing the base expression we could either have a parenthesized
559 // memory address or not. If not, return now. If so, eat the (.
560 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000561 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000562 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000563 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000564 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000565 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000566
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000567 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000568 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000569 }
570 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000571
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000572 // If we reached here, then we just ate the ( of the memory operand. Process
573 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000574 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000575
Chris Lattner29ef9a22010-01-15 18:51:29 +0000576 if (getLexer().is(AsmToken::Percent)) {
577 SMLoc L;
578 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000579 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
580 Error(L, "eiz and riz can only be used as index registers");
581 return 0;
582 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000583 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000585 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000586 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000587
588 // Following the comma we should have either an index register, or a scale
589 // value. We don't support the later form, but we want to parse it
590 // correctly.
591 //
592 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000593 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000594 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000595 SMLoc L;
596 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000597
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000598 if (getLexer().isNot(AsmToken::RParen)) {
599 // Parse the scale amount:
600 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000601 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000602 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000603 "expected comma in scale expression");
604 return 0;
605 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000606 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000607
608 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000609 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000610
611 int64_t ScaleVal;
612 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000613 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000614
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000615 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000616 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
617 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
618 return 0;
619 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000620 Scale = (unsigned)ScaleVal;
621 }
622 }
623 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000624 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000625 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000626 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000627
628 int64_t Value;
629 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000630 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000631
Daniel Dunbaree910252010-08-24 19:13:38 +0000632 if (Value != 1)
633 Warning(Loc, "scale factor without index register is ignored");
634 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000635 }
636 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000637
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000638 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000639 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000640 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000641 return 0;
642 }
Sean Callanan18b83232010-01-19 21:44:56 +0000643 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000644 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000645
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000646 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
647 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000648}
649
Chris Lattner98986712010-01-14 22:21:20 +0000650bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000651ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000652 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000653 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000654
Chris Lattnerd8f71792010-11-28 20:23:50 +0000655 // FIXME: Hack to recognize setneb as setne.
656 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
657 PatchedName != "setb" && PatchedName != "setnb")
658 PatchedName = PatchedName.substr(0, Name.size()-1);
659
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000660 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
661 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000662 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000663 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
664 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000665 bool IsVCMP = PatchedName.startswith("vcmp");
666 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000667 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000668 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000669 .Case("eq", 0)
670 .Case("lt", 1)
671 .Case("le", 2)
672 .Case("unord", 3)
673 .Case("neq", 4)
674 .Case("nlt", 5)
675 .Case("nle", 6)
676 .Case("ord", 7)
677 .Case("eq_uq", 8)
678 .Case("nge", 9)
679 .Case("ngt", 0x0A)
680 .Case("false", 0x0B)
681 .Case("neq_oq", 0x0C)
682 .Case("ge", 0x0D)
683 .Case("gt", 0x0E)
684 .Case("true", 0x0F)
685 .Case("eq_os", 0x10)
686 .Case("lt_oq", 0x11)
687 .Case("le_oq", 0x12)
688 .Case("unord_s", 0x13)
689 .Case("neq_us", 0x14)
690 .Case("nlt_uq", 0x15)
691 .Case("nle_uq", 0x16)
692 .Case("ord_s", 0x17)
693 .Case("eq_us", 0x18)
694 .Case("nge_uq", 0x19)
695 .Case("ngt_uq", 0x1A)
696 .Case("false_os", 0x1B)
697 .Case("neq_os", 0x1C)
698 .Case("ge_oq", 0x1D)
699 .Case("gt_oq", 0x1E)
700 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000701 .Default(~0U);
702 if (SSEComparisonCode != ~0U) {
703 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
704 getParser().getContext());
705 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000706 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000707 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000708 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000709 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000710 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000711 } else {
712 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000713 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000714 }
715 }
716 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000717
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000718 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000719
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000720 if (ExtraImmOp)
721 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000722
723
Chris Lattner2544f422010-09-08 05:17:37 +0000724 // Determine whether this is an instruction prefix.
725 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000726 Name == "lock" || Name == "rep" ||
727 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000728 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000729 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000730
731
Chris Lattner2544f422010-09-08 05:17:37 +0000732 // This does the actual operand parsing. Don't parse any more if we have a
733 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
734 // just want to parse the "lock" as the first instruction and the "incl" as
735 // the next one.
736 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000737
738 // Parse '*' modifier.
739 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000740 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000741 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000742 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000743 }
744
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000745 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000746 if (X86Operand *Op = ParseOperand())
747 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000748 else {
749 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000750 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000751 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000752
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000753 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000754 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000755
756 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000757 if (X86Operand *Op = ParseOperand())
758 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000759 else {
760 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000761 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000762 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000763 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000764
Chris Lattnercbf8a982010-09-11 16:18:25 +0000765 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000766 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000767 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000768 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000769 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000770 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000771
Chris Lattner2544f422010-09-08 05:17:37 +0000772 if (getLexer().is(AsmToken::EndOfStatement))
773 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000774 else if (isPrefix && getLexer().is(AsmToken::Slash))
775 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000776
Chris Lattner98c870f2010-11-06 19:25:43 +0000777 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
778 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
779 // documented form in various unofficial manuals, so a lot of code uses it.
780 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
781 Operands.size() == 3) {
782 X86Operand &Op = *(X86Operand*)Operands.back();
783 if (Op.isMem() && Op.Mem.SegReg == 0 &&
784 isa<MCConstantExpr>(Op.Mem.Disp) &&
785 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
786 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
787 SMLoc Loc = Op.getEndLoc();
788 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
789 delete &Op;
790 }
791 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000792 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
793 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
794 Operands.size() == 3) {
795 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
796 if (Op.isMem() && Op.Mem.SegReg == 0 &&
797 isa<MCConstantExpr>(Op.Mem.Disp) &&
798 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
799 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
800 SMLoc Loc = Op.getEndLoc();
801 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
802 delete &Op;
803 }
804 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000805 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
806 if (Name.startswith("ins") && Operands.size() == 3 &&
807 (Name == "insb" || Name == "insw" || Name == "insl")) {
808 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
809 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
810 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
811 Operands.pop_back();
812 Operands.pop_back();
813 delete &Op;
814 delete &Op2;
815 }
816 }
817
818 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
819 if (Name.startswith("outs") && Operands.size() == 3 &&
820 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
821 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
822 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
823 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
824 Operands.pop_back();
825 Operands.pop_back();
826 delete &Op;
827 delete &Op2;
828 }
829 }
830
831 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
832 if (Name.startswith("movs") && Operands.size() == 3 &&
833 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000834 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000835 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
836 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
837 if (isSrcOp(Op) && isDstOp(Op2)) {
838 Operands.pop_back();
839 Operands.pop_back();
840 delete &Op;
841 delete &Op2;
842 }
843 }
844 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
845 if (Name.startswith("lods") && Operands.size() == 3 &&
846 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000847 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000848 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
849 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
850 if (isSrcOp(*Op1) && Op2->isReg()) {
851 const char *ins;
852 unsigned reg = Op2->getReg();
853 bool isLods = Name == "lods";
854 if (reg == X86::AL && (isLods || Name == "lodsb"))
855 ins = "lodsb";
856 else if (reg == X86::AX && (isLods || Name == "lodsw"))
857 ins = "lodsw";
858 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
859 ins = "lodsl";
860 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
861 ins = "lodsq";
862 else
863 ins = NULL;
864 if (ins != NULL) {
865 Operands.pop_back();
866 Operands.pop_back();
867 delete Op1;
868 delete Op2;
869 if (Name != ins)
870 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
871 }
872 }
873 }
874 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
875 if (Name.startswith("stos") && Operands.size() == 3 &&
876 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +0000877 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000878 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
879 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
880 if (isDstOp(*Op2) && Op1->isReg()) {
881 const char *ins;
882 unsigned reg = Op1->getReg();
883 bool isStos = Name == "stos";
884 if (reg == X86::AL && (isStos || Name == "stosb"))
885 ins = "stosb";
886 else if (reg == X86::AX && (isStos || Name == "stosw"))
887 ins = "stosw";
888 else if (reg == X86::EAX && (isStos || Name == "stosl"))
889 ins = "stosl";
890 else if (reg == X86::RAX && (isStos || Name == "stosq"))
891 ins = "stosq";
892 else
893 ins = NULL;
894 if (ins != NULL) {
895 Operands.pop_back();
896 Operands.pop_back();
897 delete Op1;
898 delete Op2;
899 if (Name != ins)
900 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
901 }
902 }
903 }
904
Chris Lattnere9e16a32010-09-15 04:33:27 +0000905 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000906 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000907 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000908 Name.startswith("shl") || Name.startswith("sal") ||
909 Name.startswith("rcl") || Name.startswith("rcr") ||
910 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000911 Operands.size() == 3) {
912 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
913 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
914 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
915 delete Operands[1];
916 Operands.erase(Operands.begin() + 1);
917 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000918 }
Chris Lattner15f89512011-04-09 19:41:05 +0000919
920 // Transforms "int $3" into "int3" as a size optimization. We can't write an
921 // instalias with an immediate operand yet.
922 if (Name == "int" && Operands.size() == 2) {
923 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
924 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
925 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
926 delete Operands[1];
927 Operands.erase(Operands.begin() + 1);
928 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
929 }
930 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000931
Chris Lattner98986712010-01-14 22:21:20 +0000932 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000933}
934
Chris Lattner2d592d12010-09-15 04:04:33 +0000935bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000936MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000937 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000938 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000939 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000940 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
941 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000942
Chris Lattner7c51a312010-09-29 01:50:45 +0000943 // First, handle aliases that expand to multiple instructions.
944 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000945 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
946 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000947 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000948 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000949 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000950 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000951 MCInst Inst;
952 Inst.setOpcode(X86::WAIT);
953 Out.EmitInstruction(Inst);
954
Chris Lattner0bb83a82010-09-30 16:39:29 +0000955 const char *Repl =
956 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000957 .Case("finit", "fninit")
958 .Case("fsave", "fnsave")
959 .Case("fstcw", "fnstcw")
960 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000961 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000962 .Case("fstsw", "fnstsw")
963 .Case("fstsww", "fnstsw")
964 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000965 .Default(0);
966 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000967 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000968 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000969 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000970
Chris Lattnera008e8a2010-09-06 21:54:15 +0000971 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000972 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000973 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000974
Daniel Dunbarc918d602010-05-04 16:12:42 +0000975 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000976 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000977 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000978 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000979 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000980 case Match_MissingFeature:
981 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
982 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000983 case Match_ConversionFail:
984 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000985 case Match_InvalidOperand:
986 WasOriginallyInvalidOperand = true;
987 break;
988 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000989 break;
990 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000991
Daniel Dunbarc918d602010-05-04 16:12:42 +0000992 // FIXME: Ideally, we would only attempt suffix matches for things which are
993 // valid prefixes, and we could just infer the right unambiguous
994 // type. However, that requires substantially more matcher support than the
995 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000996
Daniel Dunbarc918d602010-05-04 16:12:42 +0000997 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000998 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000999 SmallString<16> Tmp;
1000 Tmp += Base;
1001 Tmp += ' ';
1002 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001003
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001004 // If this instruction starts with an 'f', then it is a floating point stack
1005 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1006 // 80-bit floating point, which use the suffixes s,l,t respectively.
1007 //
1008 // Otherwise, we assume that this may be an integer instruction, which comes
1009 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1010 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1011
Daniel Dunbarc918d602010-05-04 16:12:42 +00001012 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001013 Tmp[Base.size()] = Suffixes[0];
1014 unsigned ErrorInfoIgnore;
1015 MatchResultTy Match1, Match2, Match3, Match4;
1016
1017 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1018 Tmp[Base.size()] = Suffixes[1];
1019 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1020 Tmp[Base.size()] = Suffixes[2];
1021 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1022 Tmp[Base.size()] = Suffixes[3];
1023 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001024
1025 // Restore the old token.
1026 Op->setTokenValue(Base);
1027
1028 // If exactly one matched, then we treat that as a successful match (and the
1029 // instruction will already have been filled in correctly, since the failing
1030 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001031 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001032 (Match1 == Match_Success) + (Match2 == Match_Success) +
1033 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001034 if (NumSuccessfulMatches == 1) {
1035 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001036 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001037 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001038
Chris Lattnerec6789f2010-09-06 20:08:02 +00001039 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001040
Daniel Dunbar09062b12010-08-12 00:55:42 +00001041 // If we had multiple suffix matches, then identify this as an ambiguous
1042 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001043 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001044 char MatchChars[4];
1045 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001046 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1047 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1048 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1049 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001050
1051 SmallString<126> Msg;
1052 raw_svector_ostream OS(Msg);
1053 OS << "ambiguous instructions require an explicit suffix (could be ";
1054 for (unsigned i = 0; i != NumMatches; ++i) {
1055 if (i != 0)
1056 OS << ", ";
1057 if (i + 1 == NumMatches)
1058 OS << "or ";
1059 OS << "'" << Base << MatchChars[i] << "'";
1060 }
1061 OS << ")";
1062 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001063 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001064 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001065
Chris Lattnera008e8a2010-09-06 21:54:15 +00001066 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001067
Chris Lattnera008e8a2010-09-06 21:54:15 +00001068 // If all of the instructions reported an invalid mnemonic, then the original
1069 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001070 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1071 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001072 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001073 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001074 return true;
1075 }
1076
1077 // Recover location info for the operand if we know which was the problem.
1078 SMLoc ErrorLoc = IDLoc;
1079 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001080 if (OrigErrorInfo >= Operands.size())
1081 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001082
Chris Lattnerce4a3352010-09-06 22:11:18 +00001083 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1084 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1085 }
1086
Chris Lattnerf8840122010-09-15 03:50:11 +00001087 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001088 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001089
Chris Lattnerec6789f2010-09-06 20:08:02 +00001090 // If one instruction matched with a missing feature, report this as a
1091 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001092 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1093 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001094 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1095 return true;
1096 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001097
Chris Lattnera008e8a2010-09-06 21:54:15 +00001098 // If one instruction matched with an invalid operand, report this as an
1099 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001100 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1101 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001102 Error(IDLoc, "invalid operand for instruction");
1103 return true;
1104 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Chris Lattnerec6789f2010-09-06 20:08:02 +00001106 // If all of these were an outright failure, report it in a useless way.
1107 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001108 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001109 return true;
1110}
1111
1112
Chris Lattner537ca842010-10-30 17:38:55 +00001113bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1114 StringRef IDVal = DirectiveID.getIdentifier();
1115 if (IDVal == ".word")
1116 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001117 else if (IDVal.startswith(".code"))
1118 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001119 return true;
1120}
1121
1122/// ParseDirectiveWord
1123/// ::= .word [ expression (, expression)* ]
1124bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1125 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1126 for (;;) {
1127 const MCExpr *Value;
1128 if (getParser().ParseExpression(Value))
1129 return true;
1130
1131 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1132
1133 if (getLexer().is(AsmToken::EndOfStatement))
1134 break;
1135
1136 // FIXME: Improve diagnostic.
1137 if (getLexer().isNot(AsmToken::Comma))
1138 return Error(L, "unexpected token in directive");
1139 Parser.Lex();
1140 }
1141 }
1142
1143 Parser.Lex();
1144 return false;
1145}
1146
Evan Chengbd27f5a2011-07-27 00:38:12 +00001147/// ParseDirectiveCode
1148/// ::= .code32 | .code64
1149bool X86ATTAsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
1150 if (IDVal == ".code32") {
1151 Parser.Lex();
1152 if (is64BitMode()) {
1153 SwitchMode();
1154 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1155 }
1156 } else if (IDVal == ".code64") {
1157 Parser.Lex();
1158 if (!is64BitMode()) {
1159 SwitchMode();
1160 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1161 }
1162 } else {
1163 return Error(L, "unexpected directive " + IDVal);
1164 }
Chris Lattner537ca842010-10-30 17:38:55 +00001165
Evan Chengbd27f5a2011-07-27 00:38:12 +00001166 return false;
1167}
Chris Lattner537ca842010-10-30 17:38:55 +00001168
1169
Sean Callanane88f5522010-01-23 02:43:15 +00001170extern "C" void LLVMInitializeX86AsmLexer();
1171
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001172// Force static initialization.
1173extern "C" void LLVMInitializeX86AsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00001174 RegisterMCAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1175 RegisterMCAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001176 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001177}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001178
Chris Lattner0692ee62010-09-06 19:11:01 +00001179#define GET_REGISTER_MATCHER
1180#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001181#include "X86GenAsmMatcher.inc"