blob: 73a00950ac168be3abf15893994ccd7a8748214b [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 Rosier32461762012-08-09 22:04:55 +000070 bool MatchInstruction(SMLoc IDLoc,
71 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 Rosier36b8fed2012-06-27 22:34:28 +0000198
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000199 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000200
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000201 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000202
Daniel Dunbar20927f22009-08-07 08:26:05 +0000203 StringRef getToken() const {
204 assert(Kind == Token && "Invalid access!");
205 return StringRef(Tok.Data, Tok.Length);
206 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000207 void setTokenValue(StringRef Value) {
208 assert(Kind == Token && "Invalid access!");
209 Tok.Data = Value.data();
210 Tok.Length = Value.size();
211 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000212
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000213 unsigned getReg() const {
214 assert(Kind == Register && "Invalid access!");
215 return Reg.RegNo;
216 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000217
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000218 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000219 assert(Kind == Immediate && "Invalid access!");
220 return Imm.Val;
221 }
222
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000223 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000224 assert(Kind == Memory && "Invalid access!");
225 return Mem.Disp;
226 }
227 unsigned getMemSegReg() const {
228 assert(Kind == Memory && "Invalid access!");
229 return Mem.SegReg;
230 }
231 unsigned getMemBaseReg() const {
232 assert(Kind == Memory && "Invalid access!");
233 return Mem.BaseReg;
234 }
235 unsigned getMemIndexReg() const {
236 assert(Kind == Memory && "Invalid access!");
237 return Mem.IndexReg;
238 }
239 unsigned getMemScale() const {
240 assert(Kind == Memory && "Invalid access!");
241 return Mem.Scale;
242 }
243
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000244 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000245
246 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000247
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000248 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000249 if (!isImm())
250 return false;
251
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000252 // If this isn't a constant expr, just assume it fits and let relaxation
253 // handle it.
254 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
255 if (!CE)
256 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000257
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000258 // Otherwise, check the value is in a range that makes sense for this
259 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000260 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000261 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000262 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000263 if (!isImm())
264 return false;
265
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000266 // If this isn't a constant expr, just assume it fits and let relaxation
267 // handle it.
268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
269 if (!CE)
270 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000271
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000272 // Otherwise, check the value is in a range that makes sense for this
273 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000274 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000275 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000276 bool isImmZExtu32u8() const {
277 if (!isImm())
278 return false;
279
280 // If this isn't a constant expr, just assume it fits and let relaxation
281 // handle it.
282 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
283 if (!CE)
284 return true;
285
286 // Otherwise, check the value is in a range that makes sense for this
287 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000288 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000289 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000290 bool isImmSExti64i8() const {
291 if (!isImm())
292 return false;
293
294 // If this isn't a constant expr, just assume it fits and let relaxation
295 // handle it.
296 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
297 if (!CE)
298 return true;
299
300 // Otherwise, check the value is in a range that makes sense for this
301 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000302 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000303 }
304 bool isImmSExti64i32() const {
305 if (!isImm())
306 return false;
307
308 // If this isn't a constant expr, just assume it fits and let relaxation
309 // handle it.
310 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
311 if (!CE)
312 return true;
313
314 // Otherwise, check the value is in a range that makes sense for this
315 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000316 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000317 }
318
Daniel Dunbar20927f22009-08-07 08:26:05 +0000319 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000320 bool isMem8() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000321 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
322 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000323 bool isMem16() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000324 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
325 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000326 bool isMem32() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000327 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
328 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000329 bool isMem64() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000330 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
331 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000332 bool isMem80() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000333 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
334 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000335 bool isMem128() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000336 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
337 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000338 bool isMem256() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000339 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
340 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000341
Craig Topper75dc33a2012-07-18 04:11:12 +0000342 bool isMemVX32() const {
343 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
344 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
345 }
346 bool isMemVY32() const {
347 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
348 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
349 }
350 bool isMemVX64() const {
351 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
352 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
353 }
354 bool isMemVY64() const {
355 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
356 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
357 }
358
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000359 bool isAbsMem() const {
360 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000361 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000362 }
363
Daniel Dunbar20927f22009-08-07 08:26:05 +0000364 bool isReg() const { return Kind == Register; }
365
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000366 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
367 // Add as immediates when possible.
368 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
369 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
370 else
371 Inst.addOperand(MCOperand::CreateExpr(Expr));
372 }
373
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000374 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000375 assert(N == 1 && "Invalid number of operands!");
376 Inst.addOperand(MCOperand::CreateReg(getReg()));
377 }
378
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000379 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000381 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000382 }
383
Chad Rosier36b8fed2012-06-27 22:34:28 +0000384 void addMem8Operands(MCInst &Inst, unsigned N) const {
385 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000386 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000387 void addMem16Operands(MCInst &Inst, unsigned N) const {
388 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000389 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000390 void addMem32Operands(MCInst &Inst, unsigned N) const {
391 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000392 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000393 void addMem64Operands(MCInst &Inst, unsigned N) const {
394 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000395 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000396 void addMem80Operands(MCInst &Inst, unsigned N) const {
397 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000398 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000399 void addMem128Operands(MCInst &Inst, unsigned N) const {
400 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000401 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000402 void addMem256Operands(MCInst &Inst, unsigned N) const {
403 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000404 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000405 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
406 addMemOperands(Inst, N);
407 }
408 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
409 addMemOperands(Inst, N);
410 }
411 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
412 addMemOperands(Inst, N);
413 }
414 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
416 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000417
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000418 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000419 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000420 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
421 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
422 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000423 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000424 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
425 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000426
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000427 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
428 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000429 // Add as immediates when possible.
430 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
431 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
432 else
433 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000434 }
435
Chris Lattnerb4307b32010-01-15 19:28:38 +0000436 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000437 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
438 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000439 Res->Tok.Data = Str.data();
440 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000441 return Res;
442 }
443
Chris Lattner29ef9a22010-01-15 18:51:29 +0000444 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000445 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000447 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000448 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000449
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
451 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000452 Res->Imm.Val = Val;
453 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000454 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000455
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000456 /// Create an absolute memory operand.
457 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000458 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000459 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
460 Res->Mem.SegReg = 0;
461 Res->Mem.Disp = Disp;
462 Res->Mem.BaseReg = 0;
463 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000464 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000465 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000466 return Res;
467 }
468
469 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000470 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
471 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000472 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
473 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000474 // We should never just have a displacement, that should be parsed as an
475 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000476 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
477
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000478 // The scale should always be one of {1,2,4,8}.
479 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000480 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000481 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000482 Res->Mem.SegReg = SegReg;
483 Res->Mem.Disp = Disp;
484 Res->Mem.BaseReg = BaseReg;
485 Res->Mem.IndexReg = IndexReg;
486 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000487 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000488 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000489 }
490};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000491
Chris Lattner37dfdec2009-07-29 06:33:53 +0000492} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000493
Devang Pateldd929fc2012-01-12 18:03:40 +0000494bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000495 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000496
497 return (Op.isMem() &&
498 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
499 isa<MCConstantExpr>(Op.Mem.Disp) &&
500 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
501 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
502}
503
Devang Pateldd929fc2012-01-12 18:03:40 +0000504bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000505 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000506
Chad Rosier36b8fed2012-06-27 22:34:28 +0000507 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000508 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000509 isa<MCConstantExpr>(Op.Mem.Disp) &&
510 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
511 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
512}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000513
Devang Pateldd929fc2012-01-12 18:03:40 +0000514bool X86AsmParser::ParseRegister(unsigned &RegNo,
515 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000516 RegNo = 0;
Devang Patelbe3e3102012-01-30 20:02:42 +0000517 if (!isParsingIntelSyntax()) {
Devang Patel1aea4302012-01-20 22:32:05 +0000518 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000519 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
520 StartLoc = TokPercent.getLoc();
521 Parser.Lex(); // Eat percent token.
522 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000523
Sean Callanan18b83232010-01-19 21:44:56 +0000524 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000525 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000526 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000527 return Error(StartLoc, "invalid register name",
528 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000529 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000530
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000531 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000532
Chris Lattner33d60d52010-09-22 04:11:10 +0000533 // If the match failed, try the register name as lowercase.
534 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000535 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000536
Evan Cheng5de728c2011-07-27 23:22:03 +0000537 if (!is64BitMode()) {
538 // FIXME: This should be done using Requires<In32BitMode> and
539 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
540 // checked.
541 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
542 // REX prefix.
543 if (RegNo == X86::RIZ ||
544 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
545 X86II::isX86_64NonExtLowByteReg(RegNo) ||
546 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000547 return Error(StartLoc, "register %"
548 + Tok.getString() + " is only available in 64-bit mode",
549 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000550 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000551
Chris Lattner33d60d52010-09-22 04:11:10 +0000552 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
553 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000554 RegNo = X86::ST0;
555 EndLoc = Tok.getLoc();
556 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000557
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000558 // Check to see if we have '(4)' after %st.
559 if (getLexer().isNot(AsmToken::LParen))
560 return false;
561 // Lex the paren.
562 getParser().Lex();
563
564 const AsmToken &IntTok = Parser.getTok();
565 if (IntTok.isNot(AsmToken::Integer))
566 return Error(IntTok.getLoc(), "expected stack index");
567 switch (IntTok.getIntVal()) {
568 case 0: RegNo = X86::ST0; break;
569 case 1: RegNo = X86::ST1; break;
570 case 2: RegNo = X86::ST2; break;
571 case 3: RegNo = X86::ST3; break;
572 case 4: RegNo = X86::ST4; break;
573 case 5: RegNo = X86::ST5; break;
574 case 6: RegNo = X86::ST6; break;
575 case 7: RegNo = X86::ST7; break;
576 default: return Error(IntTok.getLoc(), "invalid stack index");
577 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000579 if (getParser().Lex().isNot(AsmToken::RParen))
580 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000581
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000582 EndLoc = Tok.getLoc();
583 Parser.Lex(); // Eat ')'
584 return false;
585 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000586
Chris Lattner645b2092010-06-24 07:29:18 +0000587 // If this is "db[0-7]", match it as an alias
588 // for dr[0-7].
589 if (RegNo == 0 && Tok.getString().size() == 3 &&
590 Tok.getString().startswith("db")) {
591 switch (Tok.getString()[2]) {
592 case '0': RegNo = X86::DR0; break;
593 case '1': RegNo = X86::DR1; break;
594 case '2': RegNo = X86::DR2; break;
595 case '3': RegNo = X86::DR3; break;
596 case '4': RegNo = X86::DR4; break;
597 case '5': RegNo = X86::DR5; break;
598 case '6': RegNo = X86::DR6; break;
599 case '7': RegNo = X86::DR7; break;
600 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000601
Chris Lattner645b2092010-06-24 07:29:18 +0000602 if (RegNo != 0) {
603 EndLoc = Tok.getLoc();
604 Parser.Lex(); // Eat it.
605 return false;
606 }
607 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Devang Patel1aea4302012-01-20 22:32:05 +0000609 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000610 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000611 return Error(StartLoc, "invalid register name",
612 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000613 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000614
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000615 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000616 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000617 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000618}
619
Devang Pateldd929fc2012-01-12 18:03:40 +0000620X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000621 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000622 return ParseIntelOperand();
623 return ParseATTOperand();
624}
625
Devang Pateld37ad242012-01-17 18:00:18 +0000626/// getIntelMemOperandSize - Return intel memory operand size.
627static unsigned getIntelMemOperandSize(StringRef OpStr) {
628 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000629 if (OpStr == "BYTE") Size = 8;
630 if (OpStr == "WORD") Size = 16;
631 if (OpStr == "DWORD") Size = 32;
632 if (OpStr == "QWORD") Size = 64;
633 if (OpStr == "XWORD") Size = 80;
634 if (OpStr == "XMMWORD") Size = 128;
635 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000636 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000637}
638
Devang Patel7c64fe62012-01-23 18:31:58 +0000639X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
640 unsigned Size) {
641 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000642 SMLoc Start = Parser.getTok().getLoc(), End;
643
Devang Pateld37ad242012-01-17 18:00:18 +0000644 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
645 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
646
647 // Eat '['
648 if (getLexer().isNot(AsmToken::LBrac))
649 return ErrorOperand(Start, "Expected '[' token!");
650 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000651
Devang Pateld37ad242012-01-17 18:00:18 +0000652 if (getLexer().is(AsmToken::Identifier)) {
653 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000654 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000655 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000656 if (getParser().ParseExpression(Disp, End)) return 0;
657 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000658 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000659 Parser.Lex();
660 return X86Operand::CreateMem(Disp, Start, End, Size);
661 }
662 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000663 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000664 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000665 SMLoc Loc = Parser.getTok().getLoc();
666 if (getLexer().is(AsmToken::RBrac)) {
667 // Handle '[' number ']'
668 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000669 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
670 if (SegReg)
671 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
672 Start, End, Size);
673 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000674 } else if (getLexer().is(AsmToken::Star)) {
675 // Handle '[' Scale*IndexReg ']'
676 Parser.Lex();
677 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000678 if (ParseRegister(IndexReg, IdxRegLoc, End))
679 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000680 Scale = Val;
681 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000682 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000683 }
684
685 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
686 bool isPlus = getLexer().is(AsmToken::Plus);
687 Parser.Lex();
688 SMLoc PlusLoc = Parser.getTok().getLoc();
689 if (getLexer().is(AsmToken::Integer)) {
690 int64_t Val = Parser.getTok().getIntVal();
691 Parser.Lex();
692 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000693 Parser.Lex();
694 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000695 if (ParseRegister(IndexReg, IdxRegLoc, End))
696 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000697 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000698 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000699 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000700 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000701 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000702 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000703 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000704 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000705 End = Parser.getTok().getLoc();
706 if (!IndexReg)
707 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000708 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000709 }
Devang Pateld37ad242012-01-17 18:00:18 +0000710 }
711
712 if (getLexer().isNot(AsmToken::RBrac))
713 if (getParser().ParseExpression(Disp, End)) return 0;
714
715 End = Parser.getTok().getLoc();
716 if (getLexer().isNot(AsmToken::RBrac))
717 return ErrorOperand(End, "expected ']' token!");
718 Parser.Lex();
719 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000720
721 // handle [-42]
722 if (!BaseReg && !IndexReg)
723 return X86Operand::CreateMem(Disp, Start, End, Size);
724
Devang Pateld37ad242012-01-17 18:00:18 +0000725 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000726 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000727}
728
729/// ParseIntelMemOperand - Parse intel style memory operand.
730X86Operand *X86AsmParser::ParseIntelMemOperand() {
731 const AsmToken &Tok = Parser.getTok();
732 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000733 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000734
735 unsigned Size = getIntelMemOperandSize(Tok.getString());
736 if (Size) {
737 Parser.Lex();
738 assert (Tok.getString() == "PTR" && "Unexpected token!");
739 Parser.Lex();
740 }
741
742 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000743 return ParseIntelBracExpression(SegReg, Size);
744
745 if (!ParseRegister(SegReg, Start, End)) {
746 // Handel SegReg : [ ... ]
747 if (getLexer().isNot(AsmToken::Colon))
748 return ErrorOperand(Start, "Expected ':' token!");
749 Parser.Lex(); // Eat :
750 if (getLexer().isNot(AsmToken::LBrac))
751 return ErrorOperand(Start, "Expected '[' token!");
752 return ParseIntelBracExpression(SegReg, Size);
753 }
Devang Pateld37ad242012-01-17 18:00:18 +0000754
755 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
756 if (getParser().ParseExpression(Disp, End)) return 0;
757 return X86Operand::CreateMem(Disp, Start, End, Size);
758}
759
760X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000761 SMLoc Start = Parser.getTok().getLoc(), End;
762
763 // immediate.
764 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
765 getLexer().is(AsmToken::Minus)) {
766 const MCExpr *Val;
767 if (!getParser().ParseExpression(Val, End)) {
768 End = Parser.getTok().getLoc();
769 return X86Operand::CreateImm(Val, Start, End);
770 }
771 }
772
Devang Patel0a338862012-01-12 01:36:43 +0000773 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000774 unsigned RegNo = 0;
775 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000776 End = Parser.getTok().getLoc();
777 return X86Operand::CreateReg(RegNo, Start, End);
778 }
779
780 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000781 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000782}
783
Devang Pateldd929fc2012-01-12 18:03:40 +0000784X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000785 switch (getLexer().getKind()) {
786 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000787 // Parse a memory operand with no segment register.
788 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000789 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000790 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000791 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000792 SMLoc Start, End;
793 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000794 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000795 Error(Start, "%eiz and %riz can only be used as index registers",
796 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000797 return 0;
798 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000799
Chris Lattnereef6d782010-04-17 18:56:34 +0000800 // If this is a segment register followed by a ':', then this is the start
801 // of a memory reference, otherwise this is a normal register reference.
802 if (getLexer().isNot(AsmToken::Colon))
803 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000804
805
Chris Lattnereef6d782010-04-17 18:56:34 +0000806 getParser().Lex(); // Eat the colon.
807 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000808 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000809 case AsmToken::Dollar: {
810 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000811 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000812 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000813 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000814 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000815 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000816 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000817 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000818 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000819}
820
Chris Lattnereef6d782010-04-17 18:56:34 +0000821/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
822/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000823X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000824
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000825 // We have to disambiguate a parenthesized expression "(4+5)" from the start
826 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000827 // only way to do this without lookahead is to eat the '(' and see what is
828 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000829 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000830 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000831 SMLoc ExprEnd;
832 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000833
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000834 // After parsing the base expression we could either have a parenthesized
835 // memory address or not. If not, return now. If so, eat the (.
836 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000837 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000838 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000839 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000840 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000841 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000842
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000843 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000844 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000845 } else {
846 // Okay, we have a '('. We don't know if this is an expression or not, but
847 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000848 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000849 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000850
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000851 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000852 // Nothing to do here, fall into the code below with the '(' part of the
853 // memory operand consumed.
854 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000855 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000856
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000857 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000858 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000859 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000860
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000861 // After parsing the base expression we could either have a parenthesized
862 // memory address or not. If not, return now. If so, eat the (.
863 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000864 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000865 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000866 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000867 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000868 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000869
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000870 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000871 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872 }
873 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000874
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000875 // If we reached here, then we just ate the ( of the memory operand. Process
876 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000877 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000878 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000879
Chris Lattner29ef9a22010-01-15 18:51:29 +0000880 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000881 SMLoc StartLoc, EndLoc;
882 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000883 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000884 Error(StartLoc, "eiz and riz can only be used as index registers",
885 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000886 return 0;
887 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000888 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000889
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000890 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000891 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000892 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000893
894 // Following the comma we should have either an index register, or a scale
895 // value. We don't support the later form, but we want to parse it
896 // correctly.
897 //
898 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000899 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000900 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000901 SMLoc L;
902 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000903
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000904 if (getLexer().isNot(AsmToken::RParen)) {
905 // Parse the scale amount:
906 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000907 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000908 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000909 "expected comma in scale expression");
910 return 0;
911 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000912 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000913
914 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000915 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000916
917 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000918 if (getParser().ParseAbsoluteExpression(ScaleVal)){
919 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000920 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000921 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000922
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000923 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000924 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
925 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
926 return 0;
927 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000928 Scale = (unsigned)ScaleVal;
929 }
930 }
931 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000932 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000933 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000934 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935
936 int64_t Value;
937 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000938 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000939
Daniel Dunbaree910252010-08-24 19:13:38 +0000940 if (Value != 1)
941 Warning(Loc, "scale factor without index register is ignored");
942 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000943 }
944 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000945
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000946 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000947 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000948 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000949 return 0;
950 }
Sean Callanan18b83232010-01-19 21:44:56 +0000951 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000952 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000953
Kevin Enderby84faf652012-03-12 21:32:09 +0000954 // If we have both a base register and an index register make sure they are
955 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000956 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000957 if (BaseReg != 0 && IndexReg != 0) {
958 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000959 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
960 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000961 IndexReg != X86::RIZ) {
962 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
963 return 0;
964 }
965 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000966 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
967 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000968 IndexReg != X86::EIZ){
969 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
970 return 0;
971 }
972 }
973
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000974 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
975 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000976}
977
Devang Pateldd929fc2012-01-12 18:03:40 +0000978bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000979ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000980 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000981 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000982
Chris Lattnerd8f71792010-11-28 20:23:50 +0000983 // FIXME: Hack to recognize setneb as setne.
984 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
985 PatchedName != "setb" && PatchedName != "setnb")
986 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000987
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000988 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
989 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000990 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000991 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
992 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000993 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000994 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000995 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000996 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000997 .Case("eq", 0x00)
998 .Case("lt", 0x01)
999 .Case("le", 0x02)
1000 .Case("unord", 0x03)
1001 .Case("neq", 0x04)
1002 .Case("nlt", 0x05)
1003 .Case("nle", 0x06)
1004 .Case("ord", 0x07)
1005 /* AVX only from here */
1006 .Case("eq_uq", 0x08)
1007 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001008 .Case("ngt", 0x0A)
1009 .Case("false", 0x0B)
1010 .Case("neq_oq", 0x0C)
1011 .Case("ge", 0x0D)
1012 .Case("gt", 0x0E)
1013 .Case("true", 0x0F)
1014 .Case("eq_os", 0x10)
1015 .Case("lt_oq", 0x11)
1016 .Case("le_oq", 0x12)
1017 .Case("unord_s", 0x13)
1018 .Case("neq_us", 0x14)
1019 .Case("nlt_uq", 0x15)
1020 .Case("nle_uq", 0x16)
1021 .Case("ord_s", 0x17)
1022 .Case("eq_us", 0x18)
1023 .Case("nge_uq", 0x19)
1024 .Case("ngt_uq", 0x1A)
1025 .Case("false_os", 0x1B)
1026 .Case("neq_os", 0x1C)
1027 .Case("ge_oq", 0x1D)
1028 .Case("gt_oq", 0x1E)
1029 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001030 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001031 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001032 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1033 getParser().getContext());
1034 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001035 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001036 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001037 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001038 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001039 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001040 } else {
1041 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001042 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001043 }
1044 }
1045 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001046
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001047 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001048
Devang Patel885f65b2012-01-30 22:47:12 +00001049 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001050 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001051
Chris Lattner2544f422010-09-08 05:17:37 +00001052 // Determine whether this is an instruction prefix.
1053 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001054 Name == "lock" || Name == "rep" ||
1055 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001056 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001057 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001058
1059
Chris Lattner2544f422010-09-08 05:17:37 +00001060 // This does the actual operand parsing. Don't parse any more if we have a
1061 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1062 // just want to parse the "lock" as the first instruction and the "incl" as
1063 // the next one.
1064 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001065
1066 // Parse '*' modifier.
1067 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001068 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001069 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001070 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001071 }
1072
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001073 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001074 if (X86Operand *Op = ParseOperand())
1075 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001076 else {
1077 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001078 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001079 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001080
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001081 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001082 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001083
1084 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001085 if (X86Operand *Op = ParseOperand())
1086 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001087 else {
1088 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001089 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001090 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001091 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001092
Chris Lattnercbf8a982010-09-11 16:18:25 +00001093 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001094 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001095 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001096 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001097 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001098 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Chris Lattner2544f422010-09-08 05:17:37 +00001100 if (getLexer().is(AsmToken::EndOfStatement))
1101 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001102 else if (isPrefix && getLexer().is(AsmToken::Slash))
1103 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001104
Devang Patel885f65b2012-01-30 22:47:12 +00001105 if (ExtraImmOp && isParsingIntelSyntax())
1106 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1107
Chris Lattner98c870f2010-11-06 19:25:43 +00001108 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1109 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1110 // documented form in various unofficial manuals, so a lot of code uses it.
1111 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1112 Operands.size() == 3) {
1113 X86Operand &Op = *(X86Operand*)Operands.back();
1114 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1115 isa<MCConstantExpr>(Op.Mem.Disp) &&
1116 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1117 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1118 SMLoc Loc = Op.getEndLoc();
1119 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1120 delete &Op;
1121 }
1122 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001123 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1124 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1125 Operands.size() == 3) {
1126 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1127 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1128 isa<MCConstantExpr>(Op.Mem.Disp) &&
1129 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1130 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1131 SMLoc Loc = Op.getEndLoc();
1132 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1133 delete &Op;
1134 }
1135 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001136 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1137 if (Name.startswith("ins") && Operands.size() == 3 &&
1138 (Name == "insb" || Name == "insw" || Name == "insl")) {
1139 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1140 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1141 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1142 Operands.pop_back();
1143 Operands.pop_back();
1144 delete &Op;
1145 delete &Op2;
1146 }
1147 }
1148
1149 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1150 if (Name.startswith("outs") && Operands.size() == 3 &&
1151 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1152 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1153 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1154 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1155 Operands.pop_back();
1156 Operands.pop_back();
1157 delete &Op;
1158 delete &Op2;
1159 }
1160 }
1161
1162 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1163 if (Name.startswith("movs") && Operands.size() == 3 &&
1164 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001165 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001166 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1167 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1168 if (isSrcOp(Op) && isDstOp(Op2)) {
1169 Operands.pop_back();
1170 Operands.pop_back();
1171 delete &Op;
1172 delete &Op2;
1173 }
1174 }
1175 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1176 if (Name.startswith("lods") && Operands.size() == 3 &&
1177 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001178 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001179 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1180 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1181 if (isSrcOp(*Op1) && Op2->isReg()) {
1182 const char *ins;
1183 unsigned reg = Op2->getReg();
1184 bool isLods = Name == "lods";
1185 if (reg == X86::AL && (isLods || Name == "lodsb"))
1186 ins = "lodsb";
1187 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1188 ins = "lodsw";
1189 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1190 ins = "lodsl";
1191 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1192 ins = "lodsq";
1193 else
1194 ins = NULL;
1195 if (ins != NULL) {
1196 Operands.pop_back();
1197 Operands.pop_back();
1198 delete Op1;
1199 delete Op2;
1200 if (Name != ins)
1201 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1202 }
1203 }
1204 }
1205 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1206 if (Name.startswith("stos") && Operands.size() == 3 &&
1207 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001208 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001209 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1210 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1211 if (isDstOp(*Op2) && Op1->isReg()) {
1212 const char *ins;
1213 unsigned reg = Op1->getReg();
1214 bool isStos = Name == "stos";
1215 if (reg == X86::AL && (isStos || Name == "stosb"))
1216 ins = "stosb";
1217 else if (reg == X86::AX && (isStos || Name == "stosw"))
1218 ins = "stosw";
1219 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1220 ins = "stosl";
1221 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1222 ins = "stosq";
1223 else
1224 ins = NULL;
1225 if (ins != NULL) {
1226 Operands.pop_back();
1227 Operands.pop_back();
1228 delete Op1;
1229 delete Op2;
1230 if (Name != ins)
1231 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1232 }
1233 }
1234 }
1235
Chris Lattnere9e16a32010-09-15 04:33:27 +00001236 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001237 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001238 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001239 Name.startswith("shl") || Name.startswith("sal") ||
1240 Name.startswith("rcl") || Name.startswith("rcr") ||
1241 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001242 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001243 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001244 // Intel syntax
1245 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1246 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001247 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1248 delete Operands[2];
1249 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001250 }
1251 } else {
1252 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1253 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001254 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1255 delete Operands[1];
1256 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001257 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001258 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001259 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001260
Chris Lattner15f89512011-04-09 19:41:05 +00001261 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1262 // instalias with an immediate operand yet.
1263 if (Name == "int" && Operands.size() == 2) {
1264 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1265 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1266 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1267 delete Operands[1];
1268 Operands.erase(Operands.begin() + 1);
1269 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1270 }
1271 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001272
Chris Lattner98986712010-01-14 22:21:20 +00001273 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001274}
1275
Devang Pateldd929fc2012-01-12 18:03:40 +00001276bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001277processInstruction(MCInst &Inst,
1278 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1279 switch (Inst.getOpcode()) {
1280 default: return false;
1281 case X86::AND16i16: {
1282 if (!Inst.getOperand(0).isImm() ||
1283 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1284 return false;
1285
1286 MCInst TmpInst;
1287 TmpInst.setOpcode(X86::AND16ri8);
1288 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1289 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1290 TmpInst.addOperand(Inst.getOperand(0));
1291 Inst = TmpInst;
1292 return true;
1293 }
1294 case X86::AND32i32: {
1295 if (!Inst.getOperand(0).isImm() ||
1296 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1297 return false;
1298
1299 MCInst TmpInst;
1300 TmpInst.setOpcode(X86::AND32ri8);
1301 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1302 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1303 TmpInst.addOperand(Inst.getOperand(0));
1304 Inst = TmpInst;
1305 return true;
1306 }
1307 case X86::AND64i32: {
1308 if (!Inst.getOperand(0).isImm() ||
1309 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1310 return false;
1311
1312 MCInst TmpInst;
1313 TmpInst.setOpcode(X86::AND64ri8);
1314 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1315 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1316 TmpInst.addOperand(Inst.getOperand(0));
1317 Inst = TmpInst;
1318 return true;
1319 }
Devang Patelac0f0482012-01-19 17:53:25 +00001320 case X86::XOR16i16: {
1321 if (!Inst.getOperand(0).isImm() ||
1322 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1323 return false;
1324
1325 MCInst TmpInst;
1326 TmpInst.setOpcode(X86::XOR16ri8);
1327 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1328 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1329 TmpInst.addOperand(Inst.getOperand(0));
1330 Inst = TmpInst;
1331 return true;
1332 }
1333 case X86::XOR32i32: {
1334 if (!Inst.getOperand(0).isImm() ||
1335 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1336 return false;
1337
1338 MCInst TmpInst;
1339 TmpInst.setOpcode(X86::XOR32ri8);
1340 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1341 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1342 TmpInst.addOperand(Inst.getOperand(0));
1343 Inst = TmpInst;
1344 return true;
1345 }
1346 case X86::XOR64i32: {
1347 if (!Inst.getOperand(0).isImm() ||
1348 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1349 return false;
1350
1351 MCInst TmpInst;
1352 TmpInst.setOpcode(X86::XOR64ri8);
1353 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1354 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1355 TmpInst.addOperand(Inst.getOperand(0));
1356 Inst = TmpInst;
1357 return true;
1358 }
1359 case X86::OR16i16: {
1360 if (!Inst.getOperand(0).isImm() ||
1361 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1362 return false;
1363
1364 MCInst TmpInst;
1365 TmpInst.setOpcode(X86::OR16ri8);
1366 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1367 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1368 TmpInst.addOperand(Inst.getOperand(0));
1369 Inst = TmpInst;
1370 return true;
1371 }
1372 case X86::OR32i32: {
1373 if (!Inst.getOperand(0).isImm() ||
1374 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1375 return false;
1376
1377 MCInst TmpInst;
1378 TmpInst.setOpcode(X86::OR32ri8);
1379 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1380 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1381 TmpInst.addOperand(Inst.getOperand(0));
1382 Inst = TmpInst;
1383 return true;
1384 }
1385 case X86::OR64i32: {
1386 if (!Inst.getOperand(0).isImm() ||
1387 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1388 return false;
1389
1390 MCInst TmpInst;
1391 TmpInst.setOpcode(X86::OR64ri8);
1392 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1393 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1394 TmpInst.addOperand(Inst.getOperand(0));
1395 Inst = TmpInst;
1396 return true;
1397 }
1398 case X86::CMP16i16: {
1399 if (!Inst.getOperand(0).isImm() ||
1400 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1401 return false;
1402
1403 MCInst TmpInst;
1404 TmpInst.setOpcode(X86::CMP16ri8);
1405 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1406 TmpInst.addOperand(Inst.getOperand(0));
1407 Inst = TmpInst;
1408 return true;
1409 }
1410 case X86::CMP32i32: {
1411 if (!Inst.getOperand(0).isImm() ||
1412 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1413 return false;
1414
1415 MCInst TmpInst;
1416 TmpInst.setOpcode(X86::CMP32ri8);
1417 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1418 TmpInst.addOperand(Inst.getOperand(0));
1419 Inst = TmpInst;
1420 return true;
1421 }
1422 case X86::CMP64i32: {
1423 if (!Inst.getOperand(0).isImm() ||
1424 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1425 return false;
1426
1427 MCInst TmpInst;
1428 TmpInst.setOpcode(X86::CMP64ri8);
1429 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1430 TmpInst.addOperand(Inst.getOperand(0));
1431 Inst = TmpInst;
1432 return true;
1433 }
Devang Patela951f772012-01-19 18:40:55 +00001434 case X86::ADD16i16: {
1435 if (!Inst.getOperand(0).isImm() ||
1436 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1437 return false;
1438
1439 MCInst TmpInst;
1440 TmpInst.setOpcode(X86::ADD16ri8);
1441 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1442 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1443 TmpInst.addOperand(Inst.getOperand(0));
1444 Inst = TmpInst;
1445 return true;
1446 }
1447 case X86::ADD32i32: {
1448 if (!Inst.getOperand(0).isImm() ||
1449 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1450 return false;
1451
1452 MCInst TmpInst;
1453 TmpInst.setOpcode(X86::ADD32ri8);
1454 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1455 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1456 TmpInst.addOperand(Inst.getOperand(0));
1457 Inst = TmpInst;
1458 return true;
1459 }
1460 case X86::ADD64i32: {
1461 if (!Inst.getOperand(0).isImm() ||
1462 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1463 return false;
1464
1465 MCInst TmpInst;
1466 TmpInst.setOpcode(X86::ADD64ri8);
1467 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1468 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1469 TmpInst.addOperand(Inst.getOperand(0));
1470 Inst = TmpInst;
1471 return true;
1472 }
1473 case X86::SUB16i16: {
1474 if (!Inst.getOperand(0).isImm() ||
1475 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1476 return false;
1477
1478 MCInst TmpInst;
1479 TmpInst.setOpcode(X86::SUB16ri8);
1480 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1481 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1482 TmpInst.addOperand(Inst.getOperand(0));
1483 Inst = TmpInst;
1484 return true;
1485 }
1486 case X86::SUB32i32: {
1487 if (!Inst.getOperand(0).isImm() ||
1488 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1489 return false;
1490
1491 MCInst TmpInst;
1492 TmpInst.setOpcode(X86::SUB32ri8);
1493 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1494 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1495 TmpInst.addOperand(Inst.getOperand(0));
1496 Inst = TmpInst;
1497 return true;
1498 }
1499 case X86::SUB64i32: {
1500 if (!Inst.getOperand(0).isImm() ||
1501 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1502 return false;
1503
1504 MCInst TmpInst;
1505 TmpInst.setOpcode(X86::SUB64ri8);
1506 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1507 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1508 TmpInst.addOperand(Inst.getOperand(0));
1509 Inst = TmpInst;
1510 return true;
1511 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001512 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001513}
1514
1515bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001516MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001517 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001518 MCStreamer &Out) {
Chad Rosier32461762012-08-09 22:04:55 +00001519 SmallVector<MCInst, 2> Insts;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001520 unsigned ErrorInfo;
1521 bool Error = MatchInstruction(IDLoc, Operands, Insts, ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001522 if (!Error)
1523 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
1524 Out.EmitInstruction(Insts[i]);
1525 return Error;
1526}
1527
1528bool X86AsmParser::
1529MatchInstruction(SMLoc IDLoc,
1530 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosierb4fdade2012-08-21 19:36:59 +00001531 SmallVectorImpl<MCInst> &MCInsts, unsigned &OrigErrorInfo,
1532 bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001533 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001534 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1535 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001536 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001537
Chris Lattner7c51a312010-09-29 01:50:45 +00001538 // First, handle aliases that expand to multiple instructions.
1539 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001540 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1541 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001542 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001543 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001544 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001545 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001546 MCInst Inst;
1547 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001548 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001549 MCInsts.push_back(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001550
Chris Lattner0bb83a82010-09-30 16:39:29 +00001551 const char *Repl =
1552 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001553 .Case("finit", "fninit")
1554 .Case("fsave", "fnsave")
1555 .Case("fstcw", "fnstcw")
1556 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001557 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001558 .Case("fstsw", "fnstsw")
1559 .Case("fstsww", "fnstsw")
1560 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001561 .Default(0);
1562 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001563 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001564 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001565 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001566
Chris Lattnera008e8a2010-09-06 21:54:15 +00001567 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001568 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001569
Daniel Dunbarc918d602010-05-04 16:12:42 +00001570 // First, try a direct match.
Devang Patelbe3e3102012-01-30 20:02:42 +00001571 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1572 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001573 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001574 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001575 // Some instructions need post-processing to, for example, tweak which
1576 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001577 // individual transformations can chain off each other.
Devang Patelb8ba13f2012-01-18 22:42:29 +00001578 while (processInstruction(Inst, Operands))
1579 ;
1580
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001581 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001582 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001583 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001584 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001585 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1586 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001587 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001588 case Match_ConversionFail:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001589 return Error(IDLoc, "unable to convert operands to instruction",
1590 EmptyRanges, matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001591 case Match_InvalidOperand:
1592 WasOriginallyInvalidOperand = true;
1593 break;
1594 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001595 break;
1596 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001597
Daniel Dunbarc918d602010-05-04 16:12:42 +00001598 // FIXME: Ideally, we would only attempt suffix matches for things which are
1599 // valid prefixes, and we could just infer the right unambiguous
1600 // type. However, that requires substantially more matcher support than the
1601 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001602
Daniel Dunbarc918d602010-05-04 16:12:42 +00001603 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001604 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001605 SmallString<16> Tmp;
1606 Tmp += Base;
1607 Tmp += ' ';
1608 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001609
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001610 // If this instruction starts with an 'f', then it is a floating point stack
1611 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1612 // 80-bit floating point, which use the suffixes s,l,t respectively.
1613 //
1614 // Otherwise, we assume that this may be an integer instruction, which comes
1615 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1616 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001617
Daniel Dunbarc918d602010-05-04 16:12:42 +00001618 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001619 Tmp[Base.size()] = Suffixes[0];
1620 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001621 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001622
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001623 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1624 Tmp[Base.size()] = Suffixes[1];
1625 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1626 Tmp[Base.size()] = Suffixes[2];
1627 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1628 Tmp[Base.size()] = Suffixes[3];
1629 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001630
1631 // Restore the old token.
1632 Op->setTokenValue(Base);
1633
1634 // If exactly one matched, then we treat that as a successful match (and the
1635 // instruction will already have been filled in correctly, since the failing
1636 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001637 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001638 (Match1 == Match_Success) + (Match2 == Match_Success) +
1639 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001640 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001641 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001642 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001643 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001644 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001645
Chris Lattnerec6789f2010-09-06 20:08:02 +00001646 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001647
Daniel Dunbar09062b12010-08-12 00:55:42 +00001648 // If we had multiple suffix matches, then identify this as an ambiguous
1649 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001650 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001651 char MatchChars[4];
1652 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001653 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1654 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1655 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1656 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001657
1658 SmallString<126> Msg;
1659 raw_svector_ostream OS(Msg);
1660 OS << "ambiguous instructions require an explicit suffix (could be ";
1661 for (unsigned i = 0; i != NumMatches; ++i) {
1662 if (i != 0)
1663 OS << ", ";
1664 if (i + 1 == NumMatches)
1665 OS << "or ";
1666 OS << "'" << Base << MatchChars[i] << "'";
1667 }
1668 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001669 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001670 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001671 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001672
Chris Lattnera008e8a2010-09-06 21:54:15 +00001673 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001674
Chris Lattnera008e8a2010-09-06 21:54:15 +00001675 // If all of the instructions reported an invalid mnemonic, then the original
1676 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001677 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1678 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001679 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001680 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001681 Op->getLocRange(), matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001682 }
1683
1684 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001685 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001686 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001687 return Error(IDLoc, "too few operands for instruction",
1688 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001689
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001690 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1691 if (Operand->getStartLoc().isValid()) {
1692 SMRange OperandRange = Operand->getLocRange();
1693 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001694 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001695 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001696 }
1697
Chad Rosierb4fdade2012-08-21 19:36:59 +00001698 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1699 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001700 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001701
Chris Lattnerec6789f2010-09-06 20:08:02 +00001702 // If one instruction matched with a missing feature, report this as a
1703 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001704 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1705 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001706 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1707 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001708 return true;
1709 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001710
Chris Lattnera008e8a2010-09-06 21:54:15 +00001711 // If one instruction matched with an invalid operand, report this as an
1712 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001713 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1714 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001715 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1716 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001717 return true;
1718 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001719
Chris Lattnerec6789f2010-09-06 20:08:02 +00001720 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001721 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1722 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001723 return true;
1724}
1725
1726
Devang Pateldd929fc2012-01-12 18:03:40 +00001727bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001728 StringRef IDVal = DirectiveID.getIdentifier();
1729 if (IDVal == ".word")
1730 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001731 else if (IDVal.startswith(".code"))
1732 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Devang Patelbe3e3102012-01-30 20:02:42 +00001733 else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001734 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001735 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1736 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001737 // FIXME : Handle noprefix
1738 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001739 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001740 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001741 }
1742 return false;
1743 }
Chris Lattner537ca842010-10-30 17:38:55 +00001744 return true;
1745}
1746
1747/// ParseDirectiveWord
1748/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001749bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001750 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1751 for (;;) {
1752 const MCExpr *Value;
1753 if (getParser().ParseExpression(Value))
1754 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001755
Chris Lattner537ca842010-10-30 17:38:55 +00001756 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001757
Chris Lattner537ca842010-10-30 17:38:55 +00001758 if (getLexer().is(AsmToken::EndOfStatement))
1759 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001760
Chris Lattner537ca842010-10-30 17:38:55 +00001761 // FIXME: Improve diagnostic.
1762 if (getLexer().isNot(AsmToken::Comma))
1763 return Error(L, "unexpected token in directive");
1764 Parser.Lex();
1765 }
1766 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001767
Chris Lattner537ca842010-10-30 17:38:55 +00001768 Parser.Lex();
1769 return false;
1770}
1771
Evan Chengbd27f5a2011-07-27 00:38:12 +00001772/// ParseDirectiveCode
1773/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001774bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001775 if (IDVal == ".code32") {
1776 Parser.Lex();
1777 if (is64BitMode()) {
1778 SwitchMode();
1779 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1780 }
1781 } else if (IDVal == ".code64") {
1782 Parser.Lex();
1783 if (!is64BitMode()) {
1784 SwitchMode();
1785 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1786 }
1787 } else {
1788 return Error(L, "unexpected directive " + IDVal);
1789 }
Chris Lattner537ca842010-10-30 17:38:55 +00001790
Evan Chengbd27f5a2011-07-27 00:38:12 +00001791 return false;
1792}
Chris Lattner537ca842010-10-30 17:38:55 +00001793
1794
Sean Callanane88f5522010-01-23 02:43:15 +00001795extern "C" void LLVMInitializeX86AsmLexer();
1796
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001797// Force static initialization.
1798extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001799 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1800 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001801 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001802}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001803
Chris Lattner0692ee62010-09-06 19:11:01 +00001804#define GET_REGISTER_MATCHER
1805#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001806#include "X86GenAsmMatcher.inc"