blob: 9263bdde20db73d714f2964b6cbdf0a086b0c3b5 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/StringSwitch.h"
23#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000026#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000027
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000028using namespace llvm;
29
30namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000031struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000032
Devang Pateldd929fc2012-01-12 18:03:40 +000033class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000034 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &getParser() const { return Parser; }
38
39 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
40
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000041 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000042 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
43 bool matchingInlineAsm = false) {
44 if (matchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000045 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000056 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000057 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Devang Patelb8ba13f2012-01-18 22:42:29 +000063 bool processInstruction(MCInst &Inst,
64 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
65
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000068 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000069
Chad Rosierc4d25602012-09-03 03:16:09 +000070 bool MatchInstruction(SMLoc IDLoc, unsigned &Kind,
Chad Rosier32461762012-08-09 22:04:55 +000071 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier64bfcbb2012-08-21 18:14:59 +000072 SmallVectorImpl<MCInst> &MCInsts,
Chad Rosierb4fdade2012-08-21 19:36:59 +000073 unsigned &OrigErrorInfo,
74 bool matchingInlineAsm = false);
Chad Rosier32461762012-08-09 22:04:55 +000075
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000076 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000077 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 bool isSrcOp(X86Operand &Op);
79
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000080 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
81 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000082 bool isDstOp(X86Operand &Op);
83
Evan Cheng59ee62d2011-07-11 03:57:24 +000084 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000085 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000086 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000087 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000088 void SwitchMode() {
89 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
90 setAvailableFeatures(FB);
91 }
Evan Chengebdeeab2011-07-08 01:53:10 +000092
Daniel Dunbar54074b52010-07-19 05:44:09 +000093 /// @name Auto-generated Matcher Functions
94 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000095
Chris Lattner0692ee62010-09-06 19:11:01 +000096#define GET_ASSEMBLER_HEADER
97#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000099 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000100
101public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000102 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000103 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000104
Daniel Dunbar54074b52010-07-19 05:44:09 +0000105 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000106 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000107 }
Roman Divackybf755322011-01-27 17:14:22 +0000108 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000109
Benjamin Kramer38e59892010-07-14 22:38:02 +0000110 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000111 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000112
113 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000114
115 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000116 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000117 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000118};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000119} // end anonymous namespace
120
Sean Callanane9b466d2010-01-23 00:40:33 +0000121/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000122/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000123
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000124static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000125
126/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000127
Craig Topper76bd9382012-07-18 04:59:16 +0000128static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000129 return (( Value <= 0x000000000000007FULL)||
130 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
131 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
132}
133
134static bool isImmSExti32i8Value(uint64_t Value) {
135 return (( Value <= 0x000000000000007FULL)||
136 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
137 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
138}
139
140static bool isImmZExtu32u8Value(uint64_t Value) {
141 return (Value <= 0x00000000000000FFULL);
142}
143
144static bool isImmSExti64i8Value(uint64_t Value) {
145 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000146 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000147}
148
149static bool isImmSExti64i32Value(uint64_t Value) {
150 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000151 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000152}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000153namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000154
155/// X86Operand - Instances of this class represent a parsed X86 machine
156/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000157struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000158 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000159 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 Register,
161 Immediate,
162 Memory
163 } Kind;
164
Chris Lattner29ef9a22010-01-15 18:51:29 +0000165 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000166
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 union {
168 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000169 const char *Data;
170 unsigned Length;
171 } Tok;
172
173 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000174 unsigned RegNo;
175 } Reg;
176
177 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000178 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000179 } Imm;
180
181 struct {
182 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000183 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000184 unsigned BaseReg;
185 unsigned IndexReg;
186 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000187 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000188 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000189 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000190
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000191 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000192 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000193
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000194 /// getStartLoc - Get the location of the first token of this operand.
195 SMLoc getStartLoc() const { return StartLoc; }
196 /// getEndLoc - Get the location of the last token of this operand.
197 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000198 /// getLocRange - Get the range between the first and last token of this
199 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000200 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000201
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000202 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000203
Daniel Dunbar20927f22009-08-07 08:26:05 +0000204 StringRef getToken() const {
205 assert(Kind == Token && "Invalid access!");
206 return StringRef(Tok.Data, Tok.Length);
207 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000208 void setTokenValue(StringRef Value) {
209 assert(Kind == Token && "Invalid access!");
210 Tok.Data = Value.data();
211 Tok.Length = Value.size();
212 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000213
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000214 unsigned getReg() const {
215 assert(Kind == Register && "Invalid access!");
216 return Reg.RegNo;
217 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000218
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000219 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000220 assert(Kind == Immediate && "Invalid access!");
221 return Imm.Val;
222 }
223
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000224 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000225 assert(Kind == Memory && "Invalid access!");
226 return Mem.Disp;
227 }
228 unsigned getMemSegReg() const {
229 assert(Kind == Memory && "Invalid access!");
230 return Mem.SegReg;
231 }
232 unsigned getMemBaseReg() const {
233 assert(Kind == Memory && "Invalid access!");
234 return Mem.BaseReg;
235 }
236 unsigned getMemIndexReg() const {
237 assert(Kind == Memory && "Invalid access!");
238 return Mem.IndexReg;
239 }
240 unsigned getMemScale() const {
241 assert(Kind == Memory && "Invalid access!");
242 return Mem.Scale;
243 }
244
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000245 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000246
247 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000248
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000249 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000250 if (!isImm())
251 return false;
252
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000253 // If this isn't a constant expr, just assume it fits and let relaxation
254 // handle it.
255 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
256 if (!CE)
257 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000258
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000259 // Otherwise, check the value is in a range that makes sense for this
260 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000261 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000262 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000263 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000264 if (!isImm())
265 return false;
266
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 // If this isn't a constant expr, just assume it fits and let relaxation
268 // handle it.
269 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
270 if (!CE)
271 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000272
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000273 // Otherwise, check the value is in a range that makes sense for this
274 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000275 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000276 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000277 bool isImmZExtu32u8() const {
278 if (!isImm())
279 return false;
280
281 // If this isn't a constant expr, just assume it fits and let relaxation
282 // handle it.
283 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
284 if (!CE)
285 return true;
286
287 // Otherwise, check the value is in a range that makes sense for this
288 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000289 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000290 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000291 bool isImmSExti64i8() const {
292 if (!isImm())
293 return false;
294
295 // If this isn't a constant expr, just assume it fits and let relaxation
296 // handle it.
297 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
298 if (!CE)
299 return true;
300
301 // Otherwise, check the value is in a range that makes sense for this
302 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000303 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000304 }
305 bool isImmSExti64i32() const {
306 if (!isImm())
307 return false;
308
309 // If this isn't a constant expr, just assume it fits and let relaxation
310 // handle it.
311 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
312 if (!CE)
313 return true;
314
315 // Otherwise, check the value is in a range that makes sense for this
316 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000317 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000318 }
319
Daniel Dunbar20927f22009-08-07 08:26:05 +0000320 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000321 bool isMem8() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000322 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
323 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000324 bool isMem16() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000325 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
326 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000327 bool isMem32() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000328 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
329 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000330 bool isMem64() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000331 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
332 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000333 bool isMem80() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000334 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
335 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000336 bool isMem128() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000337 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
338 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000339 bool isMem256() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000340 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
341 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000342
Craig Topper75dc33a2012-07-18 04:11:12 +0000343 bool isMemVX32() const {
344 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
345 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
346 }
347 bool isMemVY32() const {
348 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
349 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
350 }
351 bool isMemVX64() const {
352 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
353 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
354 }
355 bool isMemVY64() const {
356 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
357 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
358 }
359
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000360 bool isAbsMem() const {
361 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000362 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000363 }
364
Daniel Dunbar20927f22009-08-07 08:26:05 +0000365 bool isReg() const { return Kind == Register; }
366
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000367 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
368 // Add as immediates when possible.
369 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
370 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
371 else
372 Inst.addOperand(MCOperand::CreateExpr(Expr));
373 }
374
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000375 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000376 assert(N == 1 && "Invalid number of operands!");
377 Inst.addOperand(MCOperand::CreateReg(getReg()));
378 }
379
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000380 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000381 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000382 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 }
384
Chad Rosier36b8fed2012-06-27 22:34:28 +0000385 void addMem8Operands(MCInst &Inst, unsigned N) const {
386 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000387 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000388 void addMem16Operands(MCInst &Inst, unsigned N) const {
389 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000390 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000391 void addMem32Operands(MCInst &Inst, unsigned N) const {
392 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000393 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000394 void addMem64Operands(MCInst &Inst, unsigned N) const {
395 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000396 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000397 void addMem80Operands(MCInst &Inst, unsigned N) const {
398 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000399 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000400 void addMem128Operands(MCInst &Inst, unsigned N) const {
401 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000402 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000403 void addMem256Operands(MCInst &Inst, unsigned N) const {
404 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000405 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000406 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
407 addMemOperands(Inst, N);
408 }
409 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
410 addMemOperands(Inst, N);
411 }
412 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
413 addMemOperands(Inst, N);
414 }
415 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
416 addMemOperands(Inst, N);
417 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000418
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000419 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000420 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000421 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
422 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
423 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000424 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000425 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
426 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000427
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000428 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
429 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000430 // Add as immediates when possible.
431 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
432 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
433 else
434 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000435 }
436
Chris Lattnerb4307b32010-01-15 19:28:38 +0000437 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000438 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
439 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000440 Res->Tok.Data = Str.data();
441 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000442 return Res;
443 }
444
Chris Lattner29ef9a22010-01-15 18:51:29 +0000445 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000446 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000447 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000448 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000449 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000450
Chris Lattnerb4307b32010-01-15 19:28:38 +0000451 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
452 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000453 Res->Imm.Val = Val;
454 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000455 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000456
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000457 /// Create an absolute memory operand.
458 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000459 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000460 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
461 Res->Mem.SegReg = 0;
462 Res->Mem.Disp = Disp;
463 Res->Mem.BaseReg = 0;
464 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000465 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000466 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000467 return Res;
468 }
469
470 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000471 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
472 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000473 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
474 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000475 // We should never just have a displacement, that should be parsed as an
476 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000477 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
478
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000479 // The scale should always be one of {1,2,4,8}.
480 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000481 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000482 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000483 Res->Mem.SegReg = SegReg;
484 Res->Mem.Disp = Disp;
485 Res->Mem.BaseReg = BaseReg;
486 Res->Mem.IndexReg = IndexReg;
487 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000488 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000489 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000490 }
491};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000492
Chris Lattner37dfdec2009-07-29 06:33:53 +0000493} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000494
Devang Pateldd929fc2012-01-12 18:03:40 +0000495bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000496 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000497
498 return (Op.isMem() &&
499 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
500 isa<MCConstantExpr>(Op.Mem.Disp) &&
501 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
502 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
503}
504
Devang Pateldd929fc2012-01-12 18:03:40 +0000505bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000506 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000507
Chad Rosier36b8fed2012-06-27 22:34:28 +0000508 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000509 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000510 isa<MCConstantExpr>(Op.Mem.Disp) &&
511 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
512 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
513}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000514
Devang Pateldd929fc2012-01-12 18:03:40 +0000515bool X86AsmParser::ParseRegister(unsigned &RegNo,
516 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000517 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000518 const AsmToken &PercentTok = Parser.getTok();
519 StartLoc = PercentTok.getLoc();
520
521 // If we encounter a %, ignore it. This code handles registers with and
522 // without the prefix, unprefixed registers can occur in cfi directives.
523 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000524 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000525
Sean Callanan18b83232010-01-19 21:44:56 +0000526 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000527 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000528 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000529 return Error(StartLoc, "invalid register name",
530 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000531 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000532
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000533 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000534
Chris Lattner33d60d52010-09-22 04:11:10 +0000535 // If the match failed, try the register name as lowercase.
536 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000537 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000538
Evan Cheng5de728c2011-07-27 23:22:03 +0000539 if (!is64BitMode()) {
540 // FIXME: This should be done using Requires<In32BitMode> and
541 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
542 // checked.
543 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
544 // REX prefix.
545 if (RegNo == X86::RIZ ||
546 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
547 X86II::isX86_64NonExtLowByteReg(RegNo) ||
548 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000549 return Error(StartLoc, "register %"
550 + Tok.getString() + " is only available in 64-bit mode",
551 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000552 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000553
Chris Lattner33d60d52010-09-22 04:11:10 +0000554 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
555 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000556 RegNo = X86::ST0;
557 EndLoc = Tok.getLoc();
558 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000559
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000560 // Check to see if we have '(4)' after %st.
561 if (getLexer().isNot(AsmToken::LParen))
562 return false;
563 // Lex the paren.
564 getParser().Lex();
565
566 const AsmToken &IntTok = Parser.getTok();
567 if (IntTok.isNot(AsmToken::Integer))
568 return Error(IntTok.getLoc(), "expected stack index");
569 switch (IntTok.getIntVal()) {
570 case 0: RegNo = X86::ST0; break;
571 case 1: RegNo = X86::ST1; break;
572 case 2: RegNo = X86::ST2; break;
573 case 3: RegNo = X86::ST3; break;
574 case 4: RegNo = X86::ST4; break;
575 case 5: RegNo = X86::ST5; break;
576 case 6: RegNo = X86::ST6; break;
577 case 7: RegNo = X86::ST7; break;
578 default: return Error(IntTok.getLoc(), "invalid stack index");
579 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000580
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000581 if (getParser().Lex().isNot(AsmToken::RParen))
582 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000583
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000584 EndLoc = Tok.getLoc();
585 Parser.Lex(); // Eat ')'
586 return false;
587 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000588
Chris Lattner645b2092010-06-24 07:29:18 +0000589 // If this is "db[0-7]", match it as an alias
590 // for dr[0-7].
591 if (RegNo == 0 && Tok.getString().size() == 3 &&
592 Tok.getString().startswith("db")) {
593 switch (Tok.getString()[2]) {
594 case '0': RegNo = X86::DR0; break;
595 case '1': RegNo = X86::DR1; break;
596 case '2': RegNo = X86::DR2; break;
597 case '3': RegNo = X86::DR3; break;
598 case '4': RegNo = X86::DR4; break;
599 case '5': RegNo = X86::DR5; break;
600 case '6': RegNo = X86::DR6; break;
601 case '7': RegNo = X86::DR7; break;
602 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000603
Chris Lattner645b2092010-06-24 07:29:18 +0000604 if (RegNo != 0) {
605 EndLoc = Tok.getLoc();
606 Parser.Lex(); // Eat it.
607 return false;
608 }
609 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000610
Devang Patel1aea4302012-01-20 22:32:05 +0000611 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000612 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000613 return Error(StartLoc, "invalid register name",
614 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000615 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000616
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000617 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000618 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000619 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000620}
621
Devang Pateldd929fc2012-01-12 18:03:40 +0000622X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000623 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000624 return ParseIntelOperand();
625 return ParseATTOperand();
626}
627
Devang Pateld37ad242012-01-17 18:00:18 +0000628/// getIntelMemOperandSize - Return intel memory operand size.
629static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000630 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000631 .Cases("BYTE", "byte", 8)
632 .Cases("WORD", "word", 16)
633 .Cases("DWORD", "dword", 32)
634 .Cases("QWORD", "qword", 64)
635 .Cases("XWORD", "xword", 80)
636 .Cases("XMMWORD", "xmmword", 128)
637 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000638 .Default(0);
639 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000640}
641
Devang Patel7c64fe62012-01-23 18:31:58 +0000642X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
643 unsigned Size) {
644 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000645 SMLoc Start = Parser.getTok().getLoc(), End;
646
Devang Pateld37ad242012-01-17 18:00:18 +0000647 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
648 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
649
650 // Eat '['
651 if (getLexer().isNot(AsmToken::LBrac))
652 return ErrorOperand(Start, "Expected '[' token!");
653 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000654
Devang Pateld37ad242012-01-17 18:00:18 +0000655 if (getLexer().is(AsmToken::Identifier)) {
656 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000657 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000658 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000659 if (getParser().ParseExpression(Disp, End)) return 0;
660 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000661 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000662 Parser.Lex();
663 return X86Operand::CreateMem(Disp, Start, End, Size);
664 }
665 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000666 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000667 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000668 SMLoc Loc = Parser.getTok().getLoc();
669 if (getLexer().is(AsmToken::RBrac)) {
670 // Handle '[' number ']'
671 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000672 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
673 if (SegReg)
674 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
675 Start, End, Size);
676 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000677 } else if (getLexer().is(AsmToken::Star)) {
678 // Handle '[' Scale*IndexReg ']'
679 Parser.Lex();
680 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000681 if (ParseRegister(IndexReg, IdxRegLoc, End))
682 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000683 Scale = Val;
684 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000685 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000686 }
687
688 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
689 bool isPlus = getLexer().is(AsmToken::Plus);
690 Parser.Lex();
691 SMLoc PlusLoc = Parser.getTok().getLoc();
692 if (getLexer().is(AsmToken::Integer)) {
693 int64_t Val = Parser.getTok().getIntVal();
694 Parser.Lex();
695 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000696 Parser.Lex();
697 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000698 if (ParseRegister(IndexReg, IdxRegLoc, End))
699 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000700 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000701 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000702 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000703 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000704 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000705 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000706 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000707 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000708 End = Parser.getTok().getLoc();
709 if (!IndexReg)
710 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000711 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000712 }
Devang Pateld37ad242012-01-17 18:00:18 +0000713 }
714
715 if (getLexer().isNot(AsmToken::RBrac))
716 if (getParser().ParseExpression(Disp, End)) return 0;
717
718 End = Parser.getTok().getLoc();
719 if (getLexer().isNot(AsmToken::RBrac))
720 return ErrorOperand(End, "expected ']' token!");
721 Parser.Lex();
722 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000723
724 // handle [-42]
725 if (!BaseReg && !IndexReg)
726 return X86Operand::CreateMem(Disp, Start, End, Size);
727
Devang Pateld37ad242012-01-17 18:00:18 +0000728 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000729 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000730}
731
732/// ParseIntelMemOperand - Parse intel style memory operand.
733X86Operand *X86AsmParser::ParseIntelMemOperand() {
734 const AsmToken &Tok = Parser.getTok();
735 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000736 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000737
738 unsigned Size = getIntelMemOperandSize(Tok.getString());
739 if (Size) {
740 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000741 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
742 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000743 Parser.Lex();
744 }
745
746 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000747 return ParseIntelBracExpression(SegReg, Size);
748
749 if (!ParseRegister(SegReg, Start, End)) {
750 // Handel SegReg : [ ... ]
751 if (getLexer().isNot(AsmToken::Colon))
752 return ErrorOperand(Start, "Expected ':' token!");
753 Parser.Lex(); // Eat :
754 if (getLexer().isNot(AsmToken::LBrac))
755 return ErrorOperand(Start, "Expected '[' token!");
756 return ParseIntelBracExpression(SegReg, Size);
757 }
Devang Pateld37ad242012-01-17 18:00:18 +0000758
759 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
760 if (getParser().ParseExpression(Disp, End)) return 0;
761 return X86Operand::CreateMem(Disp, Start, End, Size);
762}
763
764X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000765 SMLoc Start = Parser.getTok().getLoc(), End;
766
767 // immediate.
768 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
769 getLexer().is(AsmToken::Minus)) {
770 const MCExpr *Val;
771 if (!getParser().ParseExpression(Val, End)) {
772 End = Parser.getTok().getLoc();
773 return X86Operand::CreateImm(Val, Start, End);
774 }
775 }
776
Devang Patel0a338862012-01-12 01:36:43 +0000777 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000778 unsigned RegNo = 0;
779 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000780 End = Parser.getTok().getLoc();
781 return X86Operand::CreateReg(RegNo, Start, End);
782 }
783
784 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000785 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000786}
787
Devang Pateldd929fc2012-01-12 18:03:40 +0000788X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000789 switch (getLexer().getKind()) {
790 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000791 // Parse a memory operand with no segment register.
792 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000793 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000794 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000795 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000796 SMLoc Start, End;
797 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000798 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000799 Error(Start, "%eiz and %riz can only be used as index registers",
800 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000801 return 0;
802 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000803
Chris Lattnereef6d782010-04-17 18:56:34 +0000804 // If this is a segment register followed by a ':', then this is the start
805 // of a memory reference, otherwise this is a normal register reference.
806 if (getLexer().isNot(AsmToken::Colon))
807 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000808
809
Chris Lattnereef6d782010-04-17 18:56:34 +0000810 getParser().Lex(); // Eat the colon.
811 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000812 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000813 case AsmToken::Dollar: {
814 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000815 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000816 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000817 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000818 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000819 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000820 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000821 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000822 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000823}
824
Chris Lattnereef6d782010-04-17 18:56:34 +0000825/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
826/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000827X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000828
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000829 // We have to disambiguate a parenthesized expression "(4+5)" from the start
830 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000831 // only way to do this without lookahead is to eat the '(' and see what is
832 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000833 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000834 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000835 SMLoc ExprEnd;
836 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000837
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000838 // After parsing the base expression we could either have a parenthesized
839 // memory address or not. If not, return now. If so, eat the (.
840 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000841 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000842 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000843 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000844 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000845 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000846
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000847 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000848 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 } else {
850 // Okay, we have a '('. We don't know if this is an expression or not, but
851 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000852 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000853 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000854
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000855 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000856 // Nothing to do here, fall into the code below with the '(' part of the
857 // memory operand consumed.
858 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000859 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000860
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000861 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000862 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000863 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000864
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000865 // After parsing the base expression we could either have a parenthesized
866 // memory address or not. If not, return now. If so, eat the (.
867 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000868 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000869 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000870 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000871 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000873
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000874 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000875 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000876 }
877 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000878
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000879 // If we reached here, then we just ate the ( of the memory operand. Process
880 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000881 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000882 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000883
Chris Lattner29ef9a22010-01-15 18:51:29 +0000884 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000885 SMLoc StartLoc, EndLoc;
886 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000887 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000888 Error(StartLoc, "eiz and riz can only be used as index registers",
889 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000890 return 0;
891 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000892 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000893
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000894 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000895 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000896 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000897
898 // Following the comma we should have either an index register, or a scale
899 // value. We don't support the later form, but we want to parse it
900 // correctly.
901 //
902 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000903 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000904 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000905 SMLoc L;
906 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000907
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000908 if (getLexer().isNot(AsmToken::RParen)) {
909 // Parse the scale amount:
910 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000911 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000912 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000913 "expected comma in scale expression");
914 return 0;
915 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000916 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000917
918 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000919 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920
921 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000922 if (getParser().ParseAbsoluteExpression(ScaleVal)){
923 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000924 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000925 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000926
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000927 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000928 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
929 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
930 return 0;
931 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000932 Scale = (unsigned)ScaleVal;
933 }
934 }
935 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000936 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000937 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000938 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000939
940 int64_t Value;
941 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000942 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000943
Daniel Dunbaree910252010-08-24 19:13:38 +0000944 if (Value != 1)
945 Warning(Loc, "scale factor without index register is ignored");
946 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000947 }
948 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000949
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000950 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000951 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000952 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000953 return 0;
954 }
Sean Callanan18b83232010-01-19 21:44:56 +0000955 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000956 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000957
Kevin Enderby84faf652012-03-12 21:32:09 +0000958 // If we have both a base register and an index register make sure they are
959 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000960 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000961 if (BaseReg != 0 && IndexReg != 0) {
962 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000963 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
964 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000965 IndexReg != X86::RIZ) {
966 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
967 return 0;
968 }
969 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000970 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
971 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000972 IndexReg != X86::EIZ){
973 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
974 return 0;
975 }
976 }
977
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000978 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
979 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000980}
981
Devang Pateldd929fc2012-01-12 18:03:40 +0000982bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000983ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000984 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000985 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000986
Chris Lattnerd8f71792010-11-28 20:23:50 +0000987 // FIXME: Hack to recognize setneb as setne.
988 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
989 PatchedName != "setb" && PatchedName != "setnb")
990 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000991
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000992 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
993 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000994 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000995 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
996 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000997 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000998 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000999 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001000 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001001 .Case("eq", 0x00)
1002 .Case("lt", 0x01)
1003 .Case("le", 0x02)
1004 .Case("unord", 0x03)
1005 .Case("neq", 0x04)
1006 .Case("nlt", 0x05)
1007 .Case("nle", 0x06)
1008 .Case("ord", 0x07)
1009 /* AVX only from here */
1010 .Case("eq_uq", 0x08)
1011 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001012 .Case("ngt", 0x0A)
1013 .Case("false", 0x0B)
1014 .Case("neq_oq", 0x0C)
1015 .Case("ge", 0x0D)
1016 .Case("gt", 0x0E)
1017 .Case("true", 0x0F)
1018 .Case("eq_os", 0x10)
1019 .Case("lt_oq", 0x11)
1020 .Case("le_oq", 0x12)
1021 .Case("unord_s", 0x13)
1022 .Case("neq_us", 0x14)
1023 .Case("nlt_uq", 0x15)
1024 .Case("nle_uq", 0x16)
1025 .Case("ord_s", 0x17)
1026 .Case("eq_us", 0x18)
1027 .Case("nge_uq", 0x19)
1028 .Case("ngt_uq", 0x1A)
1029 .Case("false_os", 0x1B)
1030 .Case("neq_os", 0x1C)
1031 .Case("ge_oq", 0x1D)
1032 .Case("gt_oq", 0x1E)
1033 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001034 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001035 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001036 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1037 getParser().getContext());
1038 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001039 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001040 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001041 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001042 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001043 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001044 } else {
1045 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001046 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001047 }
1048 }
1049 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001050
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001051 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001052
Devang Patel885f65b2012-01-30 22:47:12 +00001053 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001054 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001055
Chris Lattner2544f422010-09-08 05:17:37 +00001056 // Determine whether this is an instruction prefix.
1057 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001058 Name == "lock" || Name == "rep" ||
1059 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001060 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001061 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001062
1063
Chris Lattner2544f422010-09-08 05:17:37 +00001064 // This does the actual operand parsing. Don't parse any more if we have a
1065 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1066 // just want to parse the "lock" as the first instruction and the "incl" as
1067 // the next one.
1068 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001069
1070 // Parse '*' modifier.
1071 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001072 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001073 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001074 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001075 }
1076
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001077 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001078 if (X86Operand *Op = ParseOperand())
1079 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001080 else {
1081 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001082 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001083 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001084
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001085 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001086 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001087
1088 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001089 if (X86Operand *Op = ParseOperand())
1090 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001091 else {
1092 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001093 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001094 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001095 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001096
Chris Lattnercbf8a982010-09-11 16:18:25 +00001097 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001098 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001099 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001100 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001101 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001102 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001103
Chris Lattner2544f422010-09-08 05:17:37 +00001104 if (getLexer().is(AsmToken::EndOfStatement))
1105 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001106 else if (isPrefix && getLexer().is(AsmToken::Slash))
1107 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001108
Devang Patel885f65b2012-01-30 22:47:12 +00001109 if (ExtraImmOp && isParsingIntelSyntax())
1110 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1111
Chris Lattner98c870f2010-11-06 19:25:43 +00001112 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1113 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1114 // documented form in various unofficial manuals, so a lot of code uses it.
1115 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1116 Operands.size() == 3) {
1117 X86Operand &Op = *(X86Operand*)Operands.back();
1118 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1119 isa<MCConstantExpr>(Op.Mem.Disp) &&
1120 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1121 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1122 SMLoc Loc = Op.getEndLoc();
1123 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1124 delete &Op;
1125 }
1126 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001127 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1128 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1129 Operands.size() == 3) {
1130 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1131 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1132 isa<MCConstantExpr>(Op.Mem.Disp) &&
1133 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1134 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1135 SMLoc Loc = Op.getEndLoc();
1136 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1137 delete &Op;
1138 }
1139 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001140 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1141 if (Name.startswith("ins") && Operands.size() == 3 &&
1142 (Name == "insb" || Name == "insw" || Name == "insl")) {
1143 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1144 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1145 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1146 Operands.pop_back();
1147 Operands.pop_back();
1148 delete &Op;
1149 delete &Op2;
1150 }
1151 }
1152
1153 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1154 if (Name.startswith("outs") && Operands.size() == 3 &&
1155 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1156 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1157 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1158 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1159 Operands.pop_back();
1160 Operands.pop_back();
1161 delete &Op;
1162 delete &Op2;
1163 }
1164 }
1165
1166 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1167 if (Name.startswith("movs") && Operands.size() == 3 &&
1168 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001169 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001170 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1171 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1172 if (isSrcOp(Op) && isDstOp(Op2)) {
1173 Operands.pop_back();
1174 Operands.pop_back();
1175 delete &Op;
1176 delete &Op2;
1177 }
1178 }
1179 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1180 if (Name.startswith("lods") && Operands.size() == 3 &&
1181 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001182 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001183 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1184 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1185 if (isSrcOp(*Op1) && Op2->isReg()) {
1186 const char *ins;
1187 unsigned reg = Op2->getReg();
1188 bool isLods = Name == "lods";
1189 if (reg == X86::AL && (isLods || Name == "lodsb"))
1190 ins = "lodsb";
1191 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1192 ins = "lodsw";
1193 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1194 ins = "lodsl";
1195 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1196 ins = "lodsq";
1197 else
1198 ins = NULL;
1199 if (ins != NULL) {
1200 Operands.pop_back();
1201 Operands.pop_back();
1202 delete Op1;
1203 delete Op2;
1204 if (Name != ins)
1205 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1206 }
1207 }
1208 }
1209 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1210 if (Name.startswith("stos") && Operands.size() == 3 &&
1211 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001212 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001213 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1214 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1215 if (isDstOp(*Op2) && Op1->isReg()) {
1216 const char *ins;
1217 unsigned reg = Op1->getReg();
1218 bool isStos = Name == "stos";
1219 if (reg == X86::AL && (isStos || Name == "stosb"))
1220 ins = "stosb";
1221 else if (reg == X86::AX && (isStos || Name == "stosw"))
1222 ins = "stosw";
1223 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1224 ins = "stosl";
1225 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1226 ins = "stosq";
1227 else
1228 ins = NULL;
1229 if (ins != NULL) {
1230 Operands.pop_back();
1231 Operands.pop_back();
1232 delete Op1;
1233 delete Op2;
1234 if (Name != ins)
1235 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1236 }
1237 }
1238 }
1239
Chris Lattnere9e16a32010-09-15 04:33:27 +00001240 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001241 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001242 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001243 Name.startswith("shl") || Name.startswith("sal") ||
1244 Name.startswith("rcl") || Name.startswith("rcr") ||
1245 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001246 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001247 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001248 // Intel syntax
1249 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1250 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001251 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1252 delete Operands[2];
1253 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001254 }
1255 } else {
1256 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1257 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001258 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1259 delete Operands[1];
1260 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001261 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001262 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001263 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001264
Chris Lattner15f89512011-04-09 19:41:05 +00001265 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1266 // instalias with an immediate operand yet.
1267 if (Name == "int" && Operands.size() == 2) {
1268 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1269 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1270 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1271 delete Operands[1];
1272 Operands.erase(Operands.begin() + 1);
1273 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1274 }
1275 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001276
Chris Lattner98986712010-01-14 22:21:20 +00001277 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001278}
1279
Devang Pateldd929fc2012-01-12 18:03:40 +00001280bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001281processInstruction(MCInst &Inst,
1282 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1283 switch (Inst.getOpcode()) {
1284 default: return false;
1285 case X86::AND16i16: {
1286 if (!Inst.getOperand(0).isImm() ||
1287 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1288 return false;
1289
1290 MCInst TmpInst;
1291 TmpInst.setOpcode(X86::AND16ri8);
1292 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1293 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1294 TmpInst.addOperand(Inst.getOperand(0));
1295 Inst = TmpInst;
1296 return true;
1297 }
1298 case X86::AND32i32: {
1299 if (!Inst.getOperand(0).isImm() ||
1300 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1301 return false;
1302
1303 MCInst TmpInst;
1304 TmpInst.setOpcode(X86::AND32ri8);
1305 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1306 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1307 TmpInst.addOperand(Inst.getOperand(0));
1308 Inst = TmpInst;
1309 return true;
1310 }
1311 case X86::AND64i32: {
1312 if (!Inst.getOperand(0).isImm() ||
1313 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1314 return false;
1315
1316 MCInst TmpInst;
1317 TmpInst.setOpcode(X86::AND64ri8);
1318 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1319 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1320 TmpInst.addOperand(Inst.getOperand(0));
1321 Inst = TmpInst;
1322 return true;
1323 }
Devang Patelac0f0482012-01-19 17:53:25 +00001324 case X86::XOR16i16: {
1325 if (!Inst.getOperand(0).isImm() ||
1326 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1327 return false;
1328
1329 MCInst TmpInst;
1330 TmpInst.setOpcode(X86::XOR16ri8);
1331 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1332 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1333 TmpInst.addOperand(Inst.getOperand(0));
1334 Inst = TmpInst;
1335 return true;
1336 }
1337 case X86::XOR32i32: {
1338 if (!Inst.getOperand(0).isImm() ||
1339 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1340 return false;
1341
1342 MCInst TmpInst;
1343 TmpInst.setOpcode(X86::XOR32ri8);
1344 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1345 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1346 TmpInst.addOperand(Inst.getOperand(0));
1347 Inst = TmpInst;
1348 return true;
1349 }
1350 case X86::XOR64i32: {
1351 if (!Inst.getOperand(0).isImm() ||
1352 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1353 return false;
1354
1355 MCInst TmpInst;
1356 TmpInst.setOpcode(X86::XOR64ri8);
1357 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1358 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1359 TmpInst.addOperand(Inst.getOperand(0));
1360 Inst = TmpInst;
1361 return true;
1362 }
1363 case X86::OR16i16: {
1364 if (!Inst.getOperand(0).isImm() ||
1365 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1366 return false;
1367
1368 MCInst TmpInst;
1369 TmpInst.setOpcode(X86::OR16ri8);
1370 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1371 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1372 TmpInst.addOperand(Inst.getOperand(0));
1373 Inst = TmpInst;
1374 return true;
1375 }
1376 case X86::OR32i32: {
1377 if (!Inst.getOperand(0).isImm() ||
1378 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1379 return false;
1380
1381 MCInst TmpInst;
1382 TmpInst.setOpcode(X86::OR32ri8);
1383 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1384 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1385 TmpInst.addOperand(Inst.getOperand(0));
1386 Inst = TmpInst;
1387 return true;
1388 }
1389 case X86::OR64i32: {
1390 if (!Inst.getOperand(0).isImm() ||
1391 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1392 return false;
1393
1394 MCInst TmpInst;
1395 TmpInst.setOpcode(X86::OR64ri8);
1396 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1397 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1398 TmpInst.addOperand(Inst.getOperand(0));
1399 Inst = TmpInst;
1400 return true;
1401 }
1402 case X86::CMP16i16: {
1403 if (!Inst.getOperand(0).isImm() ||
1404 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1405 return false;
1406
1407 MCInst TmpInst;
1408 TmpInst.setOpcode(X86::CMP16ri8);
1409 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1410 TmpInst.addOperand(Inst.getOperand(0));
1411 Inst = TmpInst;
1412 return true;
1413 }
1414 case X86::CMP32i32: {
1415 if (!Inst.getOperand(0).isImm() ||
1416 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1417 return false;
1418
1419 MCInst TmpInst;
1420 TmpInst.setOpcode(X86::CMP32ri8);
1421 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1422 TmpInst.addOperand(Inst.getOperand(0));
1423 Inst = TmpInst;
1424 return true;
1425 }
1426 case X86::CMP64i32: {
1427 if (!Inst.getOperand(0).isImm() ||
1428 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1429 return false;
1430
1431 MCInst TmpInst;
1432 TmpInst.setOpcode(X86::CMP64ri8);
1433 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1434 TmpInst.addOperand(Inst.getOperand(0));
1435 Inst = TmpInst;
1436 return true;
1437 }
Devang Patela951f772012-01-19 18:40:55 +00001438 case X86::ADD16i16: {
1439 if (!Inst.getOperand(0).isImm() ||
1440 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1441 return false;
1442
1443 MCInst TmpInst;
1444 TmpInst.setOpcode(X86::ADD16ri8);
1445 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1446 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1447 TmpInst.addOperand(Inst.getOperand(0));
1448 Inst = TmpInst;
1449 return true;
1450 }
1451 case X86::ADD32i32: {
1452 if (!Inst.getOperand(0).isImm() ||
1453 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1454 return false;
1455
1456 MCInst TmpInst;
1457 TmpInst.setOpcode(X86::ADD32ri8);
1458 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1459 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1460 TmpInst.addOperand(Inst.getOperand(0));
1461 Inst = TmpInst;
1462 return true;
1463 }
1464 case X86::ADD64i32: {
1465 if (!Inst.getOperand(0).isImm() ||
1466 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1467 return false;
1468
1469 MCInst TmpInst;
1470 TmpInst.setOpcode(X86::ADD64ri8);
1471 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1472 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1473 TmpInst.addOperand(Inst.getOperand(0));
1474 Inst = TmpInst;
1475 return true;
1476 }
1477 case X86::SUB16i16: {
1478 if (!Inst.getOperand(0).isImm() ||
1479 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1480 return false;
1481
1482 MCInst TmpInst;
1483 TmpInst.setOpcode(X86::SUB16ri8);
1484 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1485 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1486 TmpInst.addOperand(Inst.getOperand(0));
1487 Inst = TmpInst;
1488 return true;
1489 }
1490 case X86::SUB32i32: {
1491 if (!Inst.getOperand(0).isImm() ||
1492 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1493 return false;
1494
1495 MCInst TmpInst;
1496 TmpInst.setOpcode(X86::SUB32ri8);
1497 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1498 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1499 TmpInst.addOperand(Inst.getOperand(0));
1500 Inst = TmpInst;
1501 return true;
1502 }
1503 case X86::SUB64i32: {
1504 if (!Inst.getOperand(0).isImm() ||
1505 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1506 return false;
1507
1508 MCInst TmpInst;
1509 TmpInst.setOpcode(X86::SUB64ri8);
1510 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1511 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1512 TmpInst.addOperand(Inst.getOperand(0));
1513 Inst = TmpInst;
1514 return true;
1515 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001516 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001517}
1518
1519bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001520MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001521 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001522 MCStreamer &Out) {
Chad Rosier3a86e132012-09-03 02:06:46 +00001523 unsigned Kind;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001524 unsigned ErrorInfo;
Chad Rosier3a86e132012-09-03 02:06:46 +00001525 SmallVector<MCInst, 2> Insts;
1526
Chad Rosierc4d25602012-09-03 03:16:09 +00001527 bool Error = MatchInstruction(IDLoc, Kind, Operands, Insts,
Chad Rosier3a86e132012-09-03 02:06:46 +00001528 ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001529 if (!Error)
1530 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
1531 Out.EmitInstruction(Insts[i]);
1532 return Error;
1533}
1534
1535bool X86AsmParser::
Chad Rosierc4d25602012-09-03 03:16:09 +00001536MatchInstruction(SMLoc IDLoc, unsigned &Kind,
Chad Rosier32461762012-08-09 22:04:55 +00001537 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosierb4fdade2012-08-21 19:36:59 +00001538 SmallVectorImpl<MCInst> &MCInsts, unsigned &OrigErrorInfo,
1539 bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001540 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001541 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1542 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001543 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001544
Chris Lattner7c51a312010-09-29 01:50:45 +00001545 // First, handle aliases that expand to multiple instructions.
1546 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001547 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001548 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001549 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001550 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001551 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001552 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001553 MCInst Inst;
1554 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001555 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001556 MCInsts.push_back(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001557
Chris Lattner0bb83a82010-09-30 16:39:29 +00001558 const char *Repl =
1559 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001560 .Case("finit", "fninit")
1561 .Case("fsave", "fnsave")
1562 .Case("fstcw", "fnstcw")
1563 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001564 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001565 .Case("fstsw", "fnstsw")
1566 .Case("fstsww", "fnstsw")
1567 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001568 .Default(0);
1569 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001570 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001571 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001572 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001573
Chris Lattnera008e8a2010-09-06 21:54:15 +00001574 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001575 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001576
Daniel Dunbarc918d602010-05-04 16:12:42 +00001577 // First, try a direct match.
Chad Rosierc4d25602012-09-03 03:16:09 +00001578 switch (MatchInstructionImpl(Operands, Kind, Inst, OrigErrorInfo,
Devang Patelbe3e3102012-01-30 20:02:42 +00001579 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001580 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001581 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001582 // Some instructions need post-processing to, for example, tweak which
1583 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001584 // individual transformations can chain off each other.
Devang Patelb8ba13f2012-01-18 22:42:29 +00001585 while (processInstruction(Inst, Operands))
1586 ;
1587
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001588 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001589 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001590 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001591 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001592 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1593 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001594 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001595 case Match_InvalidOperand:
1596 WasOriginallyInvalidOperand = true;
1597 break;
1598 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001599 break;
1600 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001601
Daniel Dunbarc918d602010-05-04 16:12:42 +00001602 // FIXME: Ideally, we would only attempt suffix matches for things which are
1603 // valid prefixes, and we could just infer the right unambiguous
1604 // type. However, that requires substantially more matcher support than the
1605 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001606
Daniel Dunbarc918d602010-05-04 16:12:42 +00001607 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001608 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001609 SmallString<16> Tmp;
1610 Tmp += Base;
1611 Tmp += ' ';
1612 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001613
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001614 // If this instruction starts with an 'f', then it is a floating point stack
1615 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1616 // 80-bit floating point, which use the suffixes s,l,t respectively.
1617 //
1618 // Otherwise, we assume that this may be an integer instruction, which comes
1619 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1620 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001621
Daniel Dunbarc918d602010-05-04 16:12:42 +00001622 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001623 Tmp[Base.size()] = Suffixes[0];
1624 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001625 unsigned Match1, Match2, Match3, Match4;
Chad Rosierc4d25602012-09-03 03:16:09 +00001626 unsigned tKind;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001627
Bob Wilsona3c3e1f2012-09-25 17:19:29 +00001628 Match1 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore,
1629 isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001630 if (Match1 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001631 Tmp[Base.size()] = Suffixes[1];
Bob Wilsona3c3e1f2012-09-25 17:19:29 +00001632 Match2 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore,
1633 isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001634 if (Match2 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001635 Tmp[Base.size()] = Suffixes[2];
Bob Wilsona3c3e1f2012-09-25 17:19:29 +00001636 Match3 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore,
1637 isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001638 if (Match3 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001639 Tmp[Base.size()] = Suffixes[3];
Bob Wilsona3c3e1f2012-09-25 17:19:29 +00001640 Match4 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore,
1641 isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001642 if (Match4 == Match_Success) Kind = tKind;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001643
1644 // Restore the old token.
1645 Op->setTokenValue(Base);
1646
1647 // If exactly one matched, then we treat that as a successful match (and the
1648 // instruction will already have been filled in correctly, since the failing
1649 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001650 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001651 (Match1 == Match_Success) + (Match2 == Match_Success) +
1652 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001653 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001654 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001655 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001656 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001657 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001658
Chris Lattnerec6789f2010-09-06 20:08:02 +00001659 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001660
Daniel Dunbar09062b12010-08-12 00:55:42 +00001661 // If we had multiple suffix matches, then identify this as an ambiguous
1662 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001663 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001664 char MatchChars[4];
1665 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001666 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1667 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1668 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1669 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001670
1671 SmallString<126> Msg;
1672 raw_svector_ostream OS(Msg);
1673 OS << "ambiguous instructions require an explicit suffix (could be ";
1674 for (unsigned i = 0; i != NumMatches; ++i) {
1675 if (i != 0)
1676 OS << ", ";
1677 if (i + 1 == NumMatches)
1678 OS << "or ";
1679 OS << "'" << Base << MatchChars[i] << "'";
1680 }
1681 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001682 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001683 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001684 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001685
Chris Lattnera008e8a2010-09-06 21:54:15 +00001686 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001687
Chris Lattnera008e8a2010-09-06 21:54:15 +00001688 // If all of the instructions reported an invalid mnemonic, then the original
1689 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001690 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1691 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001692 if (!WasOriginallyInvalidOperand) {
Chad Rosier674101e2012-08-22 19:14:29 +00001693 ArrayRef<SMRange> Ranges = matchingInlineAsm ? EmptyRanges :
1694 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001695 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier674101e2012-08-22 19:14:29 +00001696 Ranges, matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001697 }
1698
1699 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001700 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001701 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001702 return Error(IDLoc, "too few operands for instruction",
1703 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001704
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001705 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1706 if (Operand->getStartLoc().isValid()) {
1707 SMRange OperandRange = Operand->getLocRange();
1708 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001709 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001710 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001711 }
1712
Chad Rosierb4fdade2012-08-21 19:36:59 +00001713 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1714 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001715 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001716
Chris Lattnerec6789f2010-09-06 20:08:02 +00001717 // If one instruction matched with a missing feature, report this as a
1718 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001719 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1720 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001721 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1722 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001723 return true;
1724 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001725
Chris Lattnera008e8a2010-09-06 21:54:15 +00001726 // If one instruction matched with an invalid operand, report this as an
1727 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001728 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1729 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001730 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1731 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001732 return true;
1733 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001734
Chris Lattnerec6789f2010-09-06 20:08:02 +00001735 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001736 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1737 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001738 return true;
1739}
1740
1741
Devang Pateldd929fc2012-01-12 18:03:40 +00001742bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001743 StringRef IDVal = DirectiveID.getIdentifier();
1744 if (IDVal == ".word")
1745 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001746 else if (IDVal.startswith(".code"))
1747 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001748 else if (IDVal.startswith(".att_syntax")) {
1749 getParser().setAssemblerDialect(0);
1750 return false;
1751 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001752 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001753 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1754 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001755 // FIXME : Handle noprefix
1756 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001757 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001758 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001759 }
1760 return false;
1761 }
Chris Lattner537ca842010-10-30 17:38:55 +00001762 return true;
1763}
1764
1765/// ParseDirectiveWord
1766/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001767bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001768 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1769 for (;;) {
1770 const MCExpr *Value;
1771 if (getParser().ParseExpression(Value))
1772 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001773
Chris Lattner537ca842010-10-30 17:38:55 +00001774 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001775
Chris Lattner537ca842010-10-30 17:38:55 +00001776 if (getLexer().is(AsmToken::EndOfStatement))
1777 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001778
Chris Lattner537ca842010-10-30 17:38:55 +00001779 // FIXME: Improve diagnostic.
1780 if (getLexer().isNot(AsmToken::Comma))
1781 return Error(L, "unexpected token in directive");
1782 Parser.Lex();
1783 }
1784 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001785
Chris Lattner537ca842010-10-30 17:38:55 +00001786 Parser.Lex();
1787 return false;
1788}
1789
Evan Chengbd27f5a2011-07-27 00:38:12 +00001790/// ParseDirectiveCode
1791/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001792bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001793 if (IDVal == ".code32") {
1794 Parser.Lex();
1795 if (is64BitMode()) {
1796 SwitchMode();
1797 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1798 }
1799 } else if (IDVal == ".code64") {
1800 Parser.Lex();
1801 if (!is64BitMode()) {
1802 SwitchMode();
1803 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1804 }
1805 } else {
1806 return Error(L, "unexpected directive " + IDVal);
1807 }
Chris Lattner537ca842010-10-30 17:38:55 +00001808
Evan Chengbd27f5a2011-07-27 00:38:12 +00001809 return false;
1810}
Chris Lattner537ca842010-10-30 17:38:55 +00001811
1812
Sean Callanane88f5522010-01-23 02:43:15 +00001813extern "C" void LLVMInitializeX86AsmLexer();
1814
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001815// Force static initialization.
1816extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001817 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1818 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001819 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001820}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001821
Chris Lattner0692ee62010-09-06 19:11:01 +00001822#define GET_REGISTER_MATCHER
1823#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001824#include "X86GenAsmMatcher.inc"