blob: cdbbcd365fe009ad3153754c23e4994ca005765a [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
Chris Lattner98986712010-01-14 22:21:20 +000010#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000011#include "X86.h"
Daniel Dunbar54074b52010-07-19 05:44:09 +000012#include "X86Subtarget.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000013#include "llvm/Target/TargetRegistry.h"
14#include "llvm/Target/TargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000015#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000017#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000018#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000019#include "llvm/MC/MCParser/MCAsmLexer.h"
20#include "llvm/MC/MCParser/MCAsmParser.h"
21#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000028#include "llvm/Support/SourceMgr.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000030
31#define GET_SUBTARGETINFO_ENUM
32#include "X86GenSubtargetInfo.inc"
33
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000034using namespace llvm;
35
36namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000037struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038
39class X86ATTAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000040 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000041 MCAsmParser &Parser;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000042
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000043private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000044 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Chris Lattner309264d2010-01-15 18:44:13 +000050 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000051 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000052
53 bool ParseDirectiveWord(unsigned Size, SMLoc L);
54
Chris Lattner7036f8b2010-09-29 01:42:58 +000055 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000056 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000057 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000058
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000059 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
60 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
61 bool isSrcOp(X86Operand &Op);
62
63 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
64 /// or %es:(%edi) in 32bit mode.
65 bool isDstOp(X86Operand &Op);
66
Evan Chengebdeeab2011-07-08 01:53:10 +000067 bool is64Bit() {
68 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000069 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000070 }
71
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)
82 : TargetAsmParser(), 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
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000148 virtual void dump(raw_ostream &OS) const {}
149
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 }
229 bool isImmSExti64i8() 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 <= 0x000000000000007FULL)||
243 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
244 }
245 bool isImmSExti64i32() const {
246 if (!isImm())
247 return false;
248
249 // If this isn't a constant expr, just assume it fits and let relaxation
250 // handle it.
251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
252 if (!CE)
253 return true;
254
255 // Otherwise, check the value is in a range that makes sense for this
256 // extension.
257 uint64_t Value = CE->getValue();
258 return (( Value <= 0x000000007FFFFFFFULL)||
259 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000260 }
261
Daniel Dunbar20927f22009-08-07 08:26:05 +0000262 bool isMem() const { return Kind == Memory; }
263
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000264 bool isAbsMem() const {
265 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000266 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000267 }
268
Daniel Dunbar20927f22009-08-07 08:26:05 +0000269 bool isReg() const { return Kind == Register; }
270
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000271 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
272 // Add as immediates when possible.
273 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
274 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
275 else
276 Inst.addOperand(MCOperand::CreateExpr(Expr));
277 }
278
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000279 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000280 assert(N == 1 && "Invalid number of operands!");
281 Inst.addOperand(MCOperand::CreateReg(getReg()));
282 }
283
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000284 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000285 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000286 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000287 }
288
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000289 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000290 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000291 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
292 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
293 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000294 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000295 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
296 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000297
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000298 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
299 assert((N == 1) && "Invalid number of operands!");
300 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
301 }
302
Chris Lattnerb4307b32010-01-15 19:28:38 +0000303 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
304 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000305 Res->Tok.Data = Str.data();
306 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000307 return Res;
308 }
309
Chris Lattner29ef9a22010-01-15 18:51:29 +0000310 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000311 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000312 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000313 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000314 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000315
Chris Lattnerb4307b32010-01-15 19:28:38 +0000316 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
317 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000318 Res->Imm.Val = Val;
319 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000320 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000321
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000322 /// Create an absolute memory operand.
323 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
324 SMLoc EndLoc) {
325 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
326 Res->Mem.SegReg = 0;
327 Res->Mem.Disp = Disp;
328 Res->Mem.BaseReg = 0;
329 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000330 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000331 return Res;
332 }
333
334 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000335 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
336 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000337 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000338 // We should never just have a displacement, that should be parsed as an
339 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000340 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
341
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000342 // The scale should always be one of {1,2,4,8}.
343 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000344 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000345 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000346 Res->Mem.SegReg = SegReg;
347 Res->Mem.Disp = Disp;
348 Res->Mem.BaseReg = BaseReg;
349 Res->Mem.IndexReg = IndexReg;
350 Res->Mem.Scale = Scale;
351 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000352 }
353};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000354
Chris Lattner37dfdec2009-07-29 06:33:53 +0000355} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000356
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000357bool X86ATTAsmParser::isSrcOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000358 unsigned basereg = is64Bit() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000359
360 return (Op.isMem() &&
361 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
362 isa<MCConstantExpr>(Op.Mem.Disp) &&
363 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
364 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
365}
366
367bool X86ATTAsmParser::isDstOp(X86Operand &Op) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000368 unsigned basereg = is64Bit() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000369
370 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
371 isa<MCConstantExpr>(Op.Mem.Disp) &&
372 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
373 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
374}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000375
Chris Lattner29ef9a22010-01-15 18:51:29 +0000376bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
377 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000378 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000379 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000380 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000381 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000382 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000383
Sean Callanan18b83232010-01-19 21:44:56 +0000384 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000385 if (Tok.isNot(AsmToken::Identifier))
386 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000387
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000388 // FIXME: Validate register for the current architecture; we have to do
389 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000390 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000391
Chris Lattner33d60d52010-09-22 04:11:10 +0000392 // If the match failed, try the register name as lowercase.
393 if (RegNo == 0)
394 RegNo = MatchRegisterName(LowercaseString(Tok.getString()));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000395
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000396 // FIXME: This should be done using Requires<In32BitMode> and
397 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions
398 // can be also checked.
Evan Chengebdeeab2011-07-08 01:53:10 +0000399 if (RegNo == X86::RIZ && !is64Bit())
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000400 return Error(Tok.getLoc(), "riz register in 64-bit mode only");
401
Chris Lattner33d60d52010-09-22 04:11:10 +0000402 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
403 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000404 RegNo = X86::ST0;
405 EndLoc = Tok.getLoc();
406 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000407
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000408 // Check to see if we have '(4)' after %st.
409 if (getLexer().isNot(AsmToken::LParen))
410 return false;
411 // Lex the paren.
412 getParser().Lex();
413
414 const AsmToken &IntTok = Parser.getTok();
415 if (IntTok.isNot(AsmToken::Integer))
416 return Error(IntTok.getLoc(), "expected stack index");
417 switch (IntTok.getIntVal()) {
418 case 0: RegNo = X86::ST0; break;
419 case 1: RegNo = X86::ST1; break;
420 case 2: RegNo = X86::ST2; break;
421 case 3: RegNo = X86::ST3; break;
422 case 4: RegNo = X86::ST4; break;
423 case 5: RegNo = X86::ST5; break;
424 case 6: RegNo = X86::ST6; break;
425 case 7: RegNo = X86::ST7; break;
426 default: return Error(IntTok.getLoc(), "invalid stack index");
427 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000428
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000429 if (getParser().Lex().isNot(AsmToken::RParen))
430 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000431
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000432 EndLoc = Tok.getLoc();
433 Parser.Lex(); // Eat ')'
434 return false;
435 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000436
Chris Lattner645b2092010-06-24 07:29:18 +0000437 // If this is "db[0-7]", match it as an alias
438 // for dr[0-7].
439 if (RegNo == 0 && Tok.getString().size() == 3 &&
440 Tok.getString().startswith("db")) {
441 switch (Tok.getString()[2]) {
442 case '0': RegNo = X86::DR0; break;
443 case '1': RegNo = X86::DR1; break;
444 case '2': RegNo = X86::DR2; break;
445 case '3': RegNo = X86::DR3; break;
446 case '4': RegNo = X86::DR4; break;
447 case '5': RegNo = X86::DR5; break;
448 case '6': RegNo = X86::DR6; break;
449 case '7': RegNo = X86::DR7; break;
450 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000451
Chris Lattner645b2092010-06-24 07:29:18 +0000452 if (RegNo != 0) {
453 EndLoc = Tok.getLoc();
454 Parser.Lex(); // Eat it.
455 return false;
456 }
457 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000458
Daniel Dunbar245f0582009-08-08 21:22:41 +0000459 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000460 return Error(Tok.getLoc(), "invalid register name");
461
Chris Lattner29ef9a22010-01-15 18:51:29 +0000462 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000463 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000464 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000465}
466
Chris Lattner309264d2010-01-15 18:44:13 +0000467X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000468 switch (getLexer().getKind()) {
469 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000470 // Parse a memory operand with no segment register.
471 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000472 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000473 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000474 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000475 SMLoc Start, End;
476 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000477 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
478 Error(Start, "eiz and riz can only be used as index registers");
479 return 0;
480 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000481
Chris Lattnereef6d782010-04-17 18:56:34 +0000482 // If this is a segment register followed by a ':', then this is the start
483 // of a memory reference, otherwise this is a normal register reference.
484 if (getLexer().isNot(AsmToken::Colon))
485 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000486
487
Chris Lattnereef6d782010-04-17 18:56:34 +0000488 getParser().Lex(); // Eat the colon.
489 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000490 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000491 case AsmToken::Dollar: {
492 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000493 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000494 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000495 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000496 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000497 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000498 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000499 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000500 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000501}
502
Chris Lattnereef6d782010-04-17 18:56:34 +0000503/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
504/// has already been parsed if present.
505X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000506
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000507 // We have to disambiguate a parenthesized expression "(4+5)" from the start
508 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000509 // only way to do this without lookahead is to eat the '(' and see what is
510 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000511 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000512 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000513 SMLoc ExprEnd;
514 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000515
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000516 // After parsing the base expression we could either have a parenthesized
517 // memory address or not. If not, return now. If so, eat the (.
518 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000519 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000520 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000521 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000522 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000523 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000524
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000525 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000526 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 } else {
528 // Okay, we have a '('. We don't know if this is an expression or not, but
529 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000530 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000531 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000532
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000533 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000534 // Nothing to do here, fall into the code below with the '(' part of the
535 // memory operand consumed.
536 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000537 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000538
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000539 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000540 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000541 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000542
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000543 // After parsing the base expression we could either have a parenthesized
544 // memory address or not. If not, return now. If so, eat the (.
545 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000546 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000547 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000548 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000549 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000550 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000551
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000553 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 }
555 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000556
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000557 // If we reached here, then we just ate the ( of the memory operand. Process
558 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000559 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000560
Chris Lattner29ef9a22010-01-15 18:51:29 +0000561 if (getLexer().is(AsmToken::Percent)) {
562 SMLoc L;
563 if (ParseRegister(BaseReg, L, L)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000564 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
565 Error(L, "eiz and riz can only be used as index registers");
566 return 0;
567 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000568 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000569
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000570 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000571 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000572
573 // Following the comma we should have either an index register, or a scale
574 // value. We don't support the later form, but we want to parse it
575 // correctly.
576 //
577 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000578 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000579 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000580 SMLoc L;
581 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000582
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000583 if (getLexer().isNot(AsmToken::RParen)) {
584 // Parse the scale amount:
585 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000586 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000587 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000588 "expected comma in scale expression");
589 return 0;
590 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000591 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000592
593 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000594 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000595
596 int64_t ScaleVal;
597 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000598 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000599
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000600 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000601 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
602 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
603 return 0;
604 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000605 Scale = (unsigned)ScaleVal;
606 }
607 }
608 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000609 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000610 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000611 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000612
613 int64_t Value;
614 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000615 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000616
Daniel Dunbaree910252010-08-24 19:13:38 +0000617 if (Value != 1)
618 Warning(Loc, "scale factor without index register is ignored");
619 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000620 }
621 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000622
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000623 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000624 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000625 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000626 return 0;
627 }
Sean Callanan18b83232010-01-19 21:44:56 +0000628 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000629 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000630
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000631 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
632 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000633}
634
Chris Lattner98986712010-01-14 22:21:20 +0000635bool X86ATTAsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000636ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000637 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000638 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000639
Chris Lattnerd8f71792010-11-28 20:23:50 +0000640 // FIXME: Hack to recognize setneb as setne.
641 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
642 PatchedName != "setb" && PatchedName != "setnb")
643 PatchedName = PatchedName.substr(0, Name.size()-1);
644
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000645 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
646 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000647 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000648 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
649 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000650 bool IsVCMP = PatchedName.startswith("vcmp");
651 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000652 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000653 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000654 .Case("eq", 0)
655 .Case("lt", 1)
656 .Case("le", 2)
657 .Case("unord", 3)
658 .Case("neq", 4)
659 .Case("nlt", 5)
660 .Case("nle", 6)
661 .Case("ord", 7)
662 .Case("eq_uq", 8)
663 .Case("nge", 9)
664 .Case("ngt", 0x0A)
665 .Case("false", 0x0B)
666 .Case("neq_oq", 0x0C)
667 .Case("ge", 0x0D)
668 .Case("gt", 0x0E)
669 .Case("true", 0x0F)
670 .Case("eq_os", 0x10)
671 .Case("lt_oq", 0x11)
672 .Case("le_oq", 0x12)
673 .Case("unord_s", 0x13)
674 .Case("neq_us", 0x14)
675 .Case("nlt_uq", 0x15)
676 .Case("nle_uq", 0x16)
677 .Case("ord_s", 0x17)
678 .Case("eq_us", 0x18)
679 .Case("nge_uq", 0x19)
680 .Case("ngt_uq", 0x1A)
681 .Case("false_os", 0x1B)
682 .Case("neq_os", 0x1C)
683 .Case("ge_oq", 0x1D)
684 .Case("gt_oq", 0x1E)
685 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000686 .Default(~0U);
687 if (SSEComparisonCode != ~0U) {
688 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
689 getParser().getContext());
690 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000691 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000692 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000693 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000694 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000695 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000696 } else {
697 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000698 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000699 }
700 }
701 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000702
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000703 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000704
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000705 if (ExtraImmOp)
706 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000707
708
Chris Lattner2544f422010-09-08 05:17:37 +0000709 // Determine whether this is an instruction prefix.
710 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000711 Name == "lock" || Name == "rep" ||
712 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000713 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000714 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000715
716
Chris Lattner2544f422010-09-08 05:17:37 +0000717 // This does the actual operand parsing. Don't parse any more if we have a
718 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
719 // just want to parse the "lock" as the first instruction and the "incl" as
720 // the next one.
721 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000722
723 // Parse '*' modifier.
724 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000725 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000726 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000727 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000728 }
729
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000730 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000731 if (X86Operand *Op = ParseOperand())
732 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000733 else {
734 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000735 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000736 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000737
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000738 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000739 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000740
741 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000742 if (X86Operand *Op = ParseOperand())
743 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000744 else {
745 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000746 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000747 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000748 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000749
Chris Lattnercbf8a982010-09-11 16:18:25 +0000750 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000751 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +0000752 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +0000753 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000754 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000755 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000756
Chris Lattner2544f422010-09-08 05:17:37 +0000757 if (getLexer().is(AsmToken::EndOfStatement))
758 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +0000759 else if (isPrefix && getLexer().is(AsmToken::Slash))
760 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000761
Chris Lattner98c870f2010-11-06 19:25:43 +0000762 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
763 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
764 // documented form in various unofficial manuals, so a lot of code uses it.
765 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
766 Operands.size() == 3) {
767 X86Operand &Op = *(X86Operand*)Operands.back();
768 if (Op.isMem() && Op.Mem.SegReg == 0 &&
769 isa<MCConstantExpr>(Op.Mem.Disp) &&
770 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
771 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
772 SMLoc Loc = Op.getEndLoc();
773 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
774 delete &Op;
775 }
776 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +0000777 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
778 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
779 Operands.size() == 3) {
780 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
781 if (Op.isMem() && Op.Mem.SegReg == 0 &&
782 isa<MCConstantExpr>(Op.Mem.Disp) &&
783 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
784 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
785 SMLoc Loc = Op.getEndLoc();
786 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
787 delete &Op;
788 }
789 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000790 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
791 if (Name.startswith("ins") && Operands.size() == 3 &&
792 (Name == "insb" || Name == "insw" || Name == "insl")) {
793 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
794 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
795 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
796 Operands.pop_back();
797 Operands.pop_back();
798 delete &Op;
799 delete &Op2;
800 }
801 }
802
803 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
804 if (Name.startswith("outs") && Operands.size() == 3 &&
805 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
806 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
807 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
808 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
809 Operands.pop_back();
810 Operands.pop_back();
811 delete &Op;
812 delete &Op2;
813 }
814 }
815
816 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
817 if (Name.startswith("movs") && Operands.size() == 3 &&
818 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000819 (is64Bit() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000820 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
821 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
822 if (isSrcOp(Op) && isDstOp(Op2)) {
823 Operands.pop_back();
824 Operands.pop_back();
825 delete &Op;
826 delete &Op2;
827 }
828 }
829 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
830 if (Name.startswith("lods") && Operands.size() == 3 &&
831 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000832 Name == "lodsl" || (is64Bit() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000833 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
834 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
835 if (isSrcOp(*Op1) && Op2->isReg()) {
836 const char *ins;
837 unsigned reg = Op2->getReg();
838 bool isLods = Name == "lods";
839 if (reg == X86::AL && (isLods || Name == "lodsb"))
840 ins = "lodsb";
841 else if (reg == X86::AX && (isLods || Name == "lodsw"))
842 ins = "lodsw";
843 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
844 ins = "lodsl";
845 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
846 ins = "lodsq";
847 else
848 ins = NULL;
849 if (ins != NULL) {
850 Operands.pop_back();
851 Operands.pop_back();
852 delete Op1;
853 delete Op2;
854 if (Name != ins)
855 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
856 }
857 }
858 }
859 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
860 if (Name.startswith("stos") && Operands.size() == 3 &&
861 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengebdeeab2011-07-08 01:53:10 +0000862 Name == "stosl" || (is64Bit() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000863 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
864 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
865 if (isDstOp(*Op2) && Op1->isReg()) {
866 const char *ins;
867 unsigned reg = Op1->getReg();
868 bool isStos = Name == "stos";
869 if (reg == X86::AL && (isStos || Name == "stosb"))
870 ins = "stosb";
871 else if (reg == X86::AX && (isStos || Name == "stosw"))
872 ins = "stosw";
873 else if (reg == X86::EAX && (isStos || Name == "stosl"))
874 ins = "stosl";
875 else if (reg == X86::RAX && (isStos || Name == "stosq"))
876 ins = "stosq";
877 else
878 ins = NULL;
879 if (ins != NULL) {
880 Operands.pop_back();
881 Operands.pop_back();
882 delete Op1;
883 delete Op2;
884 if (Name != ins)
885 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
886 }
887 }
888 }
889
Chris Lattnere9e16a32010-09-15 04:33:27 +0000890 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +0000891 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +0000892 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +0000893 Name.startswith("shl") || Name.startswith("sal") ||
894 Name.startswith("rcl") || Name.startswith("rcr") ||
895 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +0000896 Operands.size() == 3) {
897 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
898 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
899 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
900 delete Operands[1];
901 Operands.erase(Operands.begin() + 1);
902 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000903 }
Chris Lattner15f89512011-04-09 19:41:05 +0000904
905 // Transforms "int $3" into "int3" as a size optimization. We can't write an
906 // instalias with an immediate operand yet.
907 if (Name == "int" && Operands.size() == 2) {
908 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
909 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
910 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
911 delete Operands[1];
912 Operands.erase(Operands.begin() + 1);
913 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
914 }
915 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000916
Chris Lattner98986712010-01-14 22:21:20 +0000917 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000918}
919
Chris Lattner2d592d12010-09-15 04:04:33 +0000920bool X86ATTAsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +0000921MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +0000922 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +0000923 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000924 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +0000925 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
926 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000927
Chris Lattner7c51a312010-09-29 01:50:45 +0000928 // First, handle aliases that expand to multiple instructions.
929 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +0000930 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
931 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +0000932 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +0000933 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +0000934 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +0000935 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +0000936 MCInst Inst;
937 Inst.setOpcode(X86::WAIT);
938 Out.EmitInstruction(Inst);
939
Chris Lattner0bb83a82010-09-30 16:39:29 +0000940 const char *Repl =
941 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +0000942 .Case("finit", "fninit")
943 .Case("fsave", "fnsave")
944 .Case("fstcw", "fnstcw")
945 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +0000946 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +0000947 .Case("fstsw", "fnstsw")
948 .Case("fstsww", "fnstsw")
949 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +0000950 .Default(0);
951 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +0000952 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +0000953 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +0000954 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000955
Chris Lattnera008e8a2010-09-06 21:54:15 +0000956 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +0000957 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +0000958 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000959
Daniel Dunbarc918d602010-05-04 16:12:42 +0000960 // First, try a direct match.
Chris Lattnerce4a3352010-09-06 22:11:18 +0000961 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
Chris Lattnerec6789f2010-09-06 20:08:02 +0000962 case Match_Success:
Chris Lattner7036f8b2010-09-29 01:42:58 +0000963 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +0000964 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +0000965 case Match_MissingFeature:
966 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
967 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +0000968 case Match_ConversionFail:
969 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +0000970 case Match_InvalidOperand:
971 WasOriginallyInvalidOperand = true;
972 break;
973 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +0000974 break;
975 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000976
Daniel Dunbarc918d602010-05-04 16:12:42 +0000977 // FIXME: Ideally, we would only attempt suffix matches for things which are
978 // valid prefixes, and we could just infer the right unambiguous
979 // type. However, that requires substantially more matcher support than the
980 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000981
Daniel Dunbarc918d602010-05-04 16:12:42 +0000982 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +0000983 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000984 SmallString<16> Tmp;
985 Tmp += Base;
986 Tmp += ' ';
987 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +0000988
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000989 // If this instruction starts with an 'f', then it is a floating point stack
990 // instruction. These come in up to three forms for 32-bit, 64-bit, and
991 // 80-bit floating point, which use the suffixes s,l,t respectively.
992 //
993 // Otherwise, we assume that this may be an integer instruction, which comes
994 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
995 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
996
Daniel Dunbarc918d602010-05-04 16:12:42 +0000997 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +0000998 Tmp[Base.size()] = Suffixes[0];
999 unsigned ErrorInfoIgnore;
1000 MatchResultTy Match1, Match2, Match3, Match4;
1001
1002 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1003 Tmp[Base.size()] = Suffixes[1];
1004 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1005 Tmp[Base.size()] = Suffixes[2];
1006 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1007 Tmp[Base.size()] = Suffixes[3];
1008 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001009
1010 // Restore the old token.
1011 Op->setTokenValue(Base);
1012
1013 // If exactly one matched, then we treat that as a successful match (and the
1014 // instruction will already have been filled in correctly, since the failing
1015 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001016 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001017 (Match1 == Match_Success) + (Match2 == Match_Success) +
1018 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001019 if (NumSuccessfulMatches == 1) {
1020 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001021 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001022 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001023
Chris Lattnerec6789f2010-09-06 20:08:02 +00001024 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001025
Daniel Dunbar09062b12010-08-12 00:55:42 +00001026 // If we had multiple suffix matches, then identify this as an ambiguous
1027 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001028 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001029 char MatchChars[4];
1030 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001031 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1032 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1033 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1034 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001035
1036 SmallString<126> Msg;
1037 raw_svector_ostream OS(Msg);
1038 OS << "ambiguous instructions require an explicit suffix (could be ";
1039 for (unsigned i = 0; i != NumMatches; ++i) {
1040 if (i != 0)
1041 OS << ", ";
1042 if (i + 1 == NumMatches)
1043 OS << "or ";
1044 OS << "'" << Base << MatchChars[i] << "'";
1045 }
1046 OS << ")";
1047 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001048 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001049 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001050
Chris Lattnera008e8a2010-09-06 21:54:15 +00001051 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052
Chris Lattnera008e8a2010-09-06 21:54:15 +00001053 // If all of the instructions reported an invalid mnemonic, then the original
1054 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001055 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1056 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001057 if (!WasOriginallyInvalidOperand) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001058 Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
Chris Lattnerce4a3352010-09-06 22:11:18 +00001059 return true;
1060 }
1061
1062 // Recover location info for the operand if we know which was the problem.
1063 SMLoc ErrorLoc = IDLoc;
1064 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001065 if (OrigErrorInfo >= Operands.size())
1066 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001067
Chris Lattnerce4a3352010-09-06 22:11:18 +00001068 ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
1069 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1070 }
1071
Chris Lattnerf8840122010-09-15 03:50:11 +00001072 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001073 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001074
Chris Lattnerec6789f2010-09-06 20:08:02 +00001075 // If one instruction matched with a missing feature, report this as a
1076 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001077 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1078 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001079 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1080 return true;
1081 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001082
Chris Lattnera008e8a2010-09-06 21:54:15 +00001083 // If one instruction matched with an invalid operand, report this as an
1084 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001085 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1086 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001087 Error(IDLoc, "invalid operand for instruction");
1088 return true;
1089 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001090
Chris Lattnerec6789f2010-09-06 20:08:02 +00001091 // If all of these were an outright failure, report it in a useless way.
1092 // FIXME: We should give nicer diagnostics about the exact failure.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001093 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001094 return true;
1095}
1096
1097
Chris Lattner537ca842010-10-30 17:38:55 +00001098bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
1099 StringRef IDVal = DirectiveID.getIdentifier();
1100 if (IDVal == ".word")
1101 return ParseDirectiveWord(2, DirectiveID.getLoc());
1102 return true;
1103}
1104
1105/// ParseDirectiveWord
1106/// ::= .word [ expression (, expression)* ]
1107bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1108 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1109 for (;;) {
1110 const MCExpr *Value;
1111 if (getParser().ParseExpression(Value))
1112 return true;
1113
1114 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1115
1116 if (getLexer().is(AsmToken::EndOfStatement))
1117 break;
1118
1119 // FIXME: Improve diagnostic.
1120 if (getLexer().isNot(AsmToken::Comma))
1121 return Error(L, "unexpected token in directive");
1122 Parser.Lex();
1123 }
1124 }
1125
1126 Parser.Lex();
1127 return false;
1128}
1129
1130
1131
1132
Sean Callanane88f5522010-01-23 02:43:15 +00001133extern "C" void LLVMInitializeX86AsmLexer();
1134
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001135// Force static initialization.
1136extern "C" void LLVMInitializeX86AsmParser() {
Evan Chengebdeeab2011-07-08 01:53:10 +00001137 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
1138 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001139 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001140}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001141
Chris Lattner0692ee62010-09-06 19:11:01 +00001142#define GET_REGISTER_MATCHER
1143#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001144#include "X86GenAsmMatcher.inc"