blob: 58d0d3d228e61a5819c07d5f0adc7eb05cfc9258 [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"
Chad Rosier4284e172012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier505bca32013-01-17 19:21:48 +000060 X86Operand *ParseIntelOperator(SMLoc StartLoc, unsigned OpKind);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000061 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000062 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000063 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000064
Chad Rosier5e6b37f2012-10-25 17:37:43 +000065 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
66 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000067
Kevin Enderby9c656452009-09-10 20:51:44 +000068 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000069 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000070
Devang Patelb8ba13f2012-01-18 22:42:29 +000071 bool processInstruction(MCInst &Inst,
72 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
73
Chad Rosier84125ca2012-10-13 00:26:04 +000074 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000076 MCStreamer &Out, unsigned &ErrorInfo,
77 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000078
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000079 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000080 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000081 bool isSrcOp(X86Operand &Op);
82
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000083 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
84 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000085 bool isDstOp(X86Operand &Op);
86
Evan Cheng59ee62d2011-07-11 03:57:24 +000087 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000088 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000089 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000090 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000091 void SwitchMode() {
92 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
93 setAvailableFeatures(FB);
94 }
Evan Chengebdeeab2011-07-08 01:53:10 +000095
Daniel Dunbar54074b52010-07-19 05:44:09 +000096 /// @name Auto-generated Matcher Functions
97 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Chris Lattner0692ee62010-09-06 19:11:01 +000099#define GET_ASSEMBLER_HEADER
100#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000101
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000102 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000103
104public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000105 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000106 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000107
Daniel Dunbar54074b52010-07-19 05:44:09 +0000108 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000109 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000110 }
Roman Divackybf755322011-01-27 17:14:22 +0000111 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112
Chad Rosier6a020a72012-10-25 20:41:34 +0000113 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
114 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000115 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000116
117 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000118
119 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000120 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000121 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000123} // end anonymous namespace
124
Sean Callanane9b466d2010-01-23 00:40:33 +0000125/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000126/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000127
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000128static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000129
130/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000131
Craig Topper76bd9382012-07-18 04:59:16 +0000132static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000133 return (( Value <= 0x000000000000007FULL)||
134 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
135 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
136}
137
138static bool isImmSExti32i8Value(uint64_t Value) {
139 return (( Value <= 0x000000000000007FULL)||
140 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
141 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
142}
143
144static bool isImmZExtu32u8Value(uint64_t Value) {
145 return (Value <= 0x00000000000000FFULL);
146}
147
148static bool isImmSExti64i8Value(uint64_t Value) {
149 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000150 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000151}
152
153static bool isImmSExti64i32Value(uint64_t Value) {
154 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000155 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000156}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000157namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158
159/// X86Operand - Instances of this class represent a parsed X86 machine
160/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000161struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000162 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000163 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000164 Register,
165 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000166 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 } Kind;
168
Chris Lattner29ef9a22010-01-15 18:51:29 +0000169 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000170 SMLoc OffsetOfLoc;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000171 bool AddressOf;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000172
Eric Christophera286fc02013-03-15 00:42:55 +0000173 struct TokOp {
174 const char *Data;
175 unsigned Length;
176 };
177
178 struct RegOp {
179 unsigned RegNo;
180 };
181
182 struct ImmOp {
183 const MCExpr *Val;
184 bool NeedAsmRewrite;
185 };
186
187 struct MemOp {
188 unsigned SegReg;
189 const MCExpr *Disp;
190 unsigned BaseReg;
191 unsigned IndexReg;
192 unsigned Scale;
193 unsigned Size;
Eric Christophera286fc02013-03-15 00:42:55 +0000194 };
195
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000196 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000197 struct TokOp Tok;
198 struct RegOp Reg;
199 struct ImmOp Imm;
200 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000201 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000202
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000203 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000204 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000205
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000206 /// getStartLoc - Get the location of the first token of this operand.
207 SMLoc getStartLoc() const { return StartLoc; }
208 /// getEndLoc - Get the location of the last token of this operand.
209 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000210 /// getLocRange - Get the range between the first and last token of this
211 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000212 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000213 /// getOffsetOfLoc - Get the location of the offset operator.
214 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000215
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000216 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000217
Daniel Dunbar20927f22009-08-07 08:26:05 +0000218 StringRef getToken() const {
219 assert(Kind == Token && "Invalid access!");
220 return StringRef(Tok.Data, Tok.Length);
221 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000222 void setTokenValue(StringRef Value) {
223 assert(Kind == Token && "Invalid access!");
224 Tok.Data = Value.data();
225 Tok.Length = Value.size();
226 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000227
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000228 unsigned getReg() const {
229 assert(Kind == Register && "Invalid access!");
230 return Reg.RegNo;
231 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000232
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000233 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000234 assert(Kind == Immediate && "Invalid access!");
235 return Imm.Val;
236 }
237
Chad Rosierefcb3d92012-10-26 18:04:20 +0000238 bool needAsmRewrite() const {
239 assert(Kind == Immediate && "Invalid access!");
240 return Imm.NeedAsmRewrite;
241 }
242
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000243 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000244 assert(Kind == Memory && "Invalid access!");
245 return Mem.Disp;
246 }
247 unsigned getMemSegReg() const {
248 assert(Kind == Memory && "Invalid access!");
249 return Mem.SegReg;
250 }
251 unsigned getMemBaseReg() const {
252 assert(Kind == Memory && "Invalid access!");
253 return Mem.BaseReg;
254 }
255 unsigned getMemIndexReg() const {
256 assert(Kind == Memory && "Invalid access!");
257 return Mem.IndexReg;
258 }
259 unsigned getMemScale() const {
260 assert(Kind == Memory && "Invalid access!");
261 return Mem.Scale;
262 }
263
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000264 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000265
266 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000267
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000269 if (!isImm())
270 return false;
271
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000272 // If this isn't a constant expr, just assume it fits and let relaxation
273 // handle it.
274 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
275 if (!CE)
276 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000277
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000278 // Otherwise, check the value is in a range that makes sense for this
279 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000280 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000281 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000282 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000283 if (!isImm())
284 return false;
285
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000286 // If this isn't a constant expr, just assume it fits and let relaxation
287 // handle it.
288 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
289 if (!CE)
290 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000291
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000292 // Otherwise, check the value is in a range that makes sense for this
293 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000294 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000295 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000296 bool isImmZExtu32u8() const {
297 if (!isImm())
298 return false;
299
300 // If this isn't a constant expr, just assume it fits and let relaxation
301 // handle it.
302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
303 if (!CE)
304 return true;
305
306 // Otherwise, check the value is in a range that makes sense for this
307 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000308 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000309 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000310 bool isImmSExti64i8() const {
311 if (!isImm())
312 return false;
313
314 // If this isn't a constant expr, just assume it fits and let relaxation
315 // handle it.
316 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
317 if (!CE)
318 return true;
319
320 // Otherwise, check the value is in a range that makes sense for this
321 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000322 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000323 }
324 bool isImmSExti64i32() const {
325 if (!isImm())
326 return false;
327
328 // If this isn't a constant expr, just assume it fits and let relaxation
329 // handle it.
330 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
331 if (!CE)
332 return true;
333
334 // Otherwise, check the value is in a range that makes sense for this
335 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000336 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000337 }
338
Chad Rosiera703fb92012-10-22 19:50:35 +0000339 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000340 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000341 }
342
Chad Rosierc1ec2072013-01-10 22:10:27 +0000343 bool needAddressOf() const {
344 return AddressOf;
345 }
346
Daniel Dunbar20927f22009-08-07 08:26:05 +0000347 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000363 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000364 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000365 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000366 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000367 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000368 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000369
Craig Topper75dc33a2012-07-18 04:11:12 +0000370 bool isMemVX32() const {
371 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
372 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
373 }
374 bool isMemVY32() const {
375 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
376 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
377 }
378 bool isMemVX64() const {
379 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
380 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
381 }
382 bool isMemVY64() const {
383 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
384 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
385 }
386
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000387 bool isAbsMem() const {
388 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000389 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000390 }
391
Daniel Dunbar20927f22009-08-07 08:26:05 +0000392 bool isReg() const { return Kind == Register; }
393
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000394 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
395 // Add as immediates when possible.
396 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
397 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
398 else
399 Inst.addOperand(MCOperand::CreateExpr(Expr));
400 }
401
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000402 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000403 assert(N == 1 && "Invalid number of operands!");
404 Inst.addOperand(MCOperand::CreateReg(getReg()));
405 }
406
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000407 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000408 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000409 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000410 }
411
Chad Rosier36b8fed2012-06-27 22:34:28 +0000412 void addMem8Operands(MCInst &Inst, unsigned N) const {
413 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000414 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000415 void addMem16Operands(MCInst &Inst, unsigned N) const {
416 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000417 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000418 void addMem32Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000421 void addMem64Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000424 void addMem80Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000427 void addMem128Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000430 void addMem256Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000432 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000433 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
435 }
436 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
438 }
439 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
440 addMemOperands(Inst, N);
441 }
442 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
443 addMemOperands(Inst, N);
444 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000445
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000446 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000447 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000448 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
449 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
450 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000451 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000452 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
453 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000454
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000455 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
456 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000457 // Add as immediates when possible.
458 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
459 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
460 else
461 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000462 }
463
Chris Lattnerb4307b32010-01-15 19:28:38 +0000464 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000465 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000466 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000467 Res->Tok.Data = Str.data();
468 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000469 return Res;
470 }
471
Chad Rosierc0a14b82012-10-24 17:22:29 +0000472 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000473 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000474 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000475 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000477 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000478 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000479 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000480 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000481
Chad Rosierefcb3d92012-10-26 18:04:20 +0000482 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc,
483 bool NeedRewrite = true){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000484 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000485 Res->Imm.Val = Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000486 Res->Imm.NeedAsmRewrite = NeedRewrite;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000487 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000488 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000489
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000490 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000491 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000492 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000493 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
494 Res->Mem.SegReg = 0;
495 Res->Mem.Disp = Disp;
496 Res->Mem.BaseReg = 0;
497 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000498 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000499 Res->Mem.Size = Size;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000500 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000501 return Res;
502 }
503
504 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000505 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
506 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000507 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000508 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000509 // We should never just have a displacement, that should be parsed as an
510 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000511 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
512
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000513 // The scale should always be one of {1,2,4,8}.
514 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000515 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000516 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000517 Res->Mem.SegReg = SegReg;
518 Res->Mem.Disp = Disp;
519 Res->Mem.BaseReg = BaseReg;
520 Res->Mem.IndexReg = IndexReg;
521 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000522 Res->Mem.Size = Size;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000523 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000524 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000525 }
526};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000527
Chris Lattner37dfdec2009-07-29 06:33:53 +0000528} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000529
Devang Pateldd929fc2012-01-12 18:03:40 +0000530bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000531 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000532
533 return (Op.isMem() &&
534 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
535 isa<MCConstantExpr>(Op.Mem.Disp) &&
536 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
537 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
538}
539
Devang Pateldd929fc2012-01-12 18:03:40 +0000540bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000541 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000542
Chad Rosier36b8fed2012-06-27 22:34:28 +0000543 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000544 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000545 isa<MCConstantExpr>(Op.Mem.Disp) &&
546 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
547 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
548}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549
Devang Pateldd929fc2012-01-12 18:03:40 +0000550bool X86AsmParser::ParseRegister(unsigned &RegNo,
551 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000552 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000553 const AsmToken &PercentTok = Parser.getTok();
554 StartLoc = PercentTok.getLoc();
555
556 // If we encounter a %, ignore it. This code handles registers with and
557 // without the prefix, unprefixed registers can occur in cfi directives.
558 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000559 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000560
Sean Callanan18b83232010-01-19 21:44:56 +0000561 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000562 EndLoc = Tok.getEndLoc();
563
Devang Patel1aea4302012-01-20 22:32:05 +0000564 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000565 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000566 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000567 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000568 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000569
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000570 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000571
Chris Lattner33d60d52010-09-22 04:11:10 +0000572 // If the match failed, try the register name as lowercase.
573 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000574 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000575
Evan Cheng5de728c2011-07-27 23:22:03 +0000576 if (!is64BitMode()) {
577 // FIXME: This should be done using Requires<In32BitMode> and
578 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
579 // checked.
580 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
581 // REX prefix.
582 if (RegNo == X86::RIZ ||
583 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
584 X86II::isX86_64NonExtLowByteReg(RegNo) ||
585 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000586 return Error(StartLoc, "register %"
587 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000588 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000589 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000590
Chris Lattner33d60d52010-09-22 04:11:10 +0000591 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
592 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000593 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000594 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000595
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000596 // Check to see if we have '(4)' after %st.
597 if (getLexer().isNot(AsmToken::LParen))
598 return false;
599 // Lex the paren.
600 getParser().Lex();
601
602 const AsmToken &IntTok = Parser.getTok();
603 if (IntTok.isNot(AsmToken::Integer))
604 return Error(IntTok.getLoc(), "expected stack index");
605 switch (IntTok.getIntVal()) {
606 case 0: RegNo = X86::ST0; break;
607 case 1: RegNo = X86::ST1; break;
608 case 2: RegNo = X86::ST2; break;
609 case 3: RegNo = X86::ST3; break;
610 case 4: RegNo = X86::ST4; break;
611 case 5: RegNo = X86::ST5; break;
612 case 6: RegNo = X86::ST6; break;
613 case 7: RegNo = X86::ST7; break;
614 default: return Error(IntTok.getLoc(), "invalid stack index");
615 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000616
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000617 if (getParser().Lex().isNot(AsmToken::RParen))
618 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000619
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000620 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000621 Parser.Lex(); // Eat ')'
622 return false;
623 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000624
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000625 EndLoc = Parser.getTok().getEndLoc();
626
Chris Lattner645b2092010-06-24 07:29:18 +0000627 // If this is "db[0-7]", match it as an alias
628 // for dr[0-7].
629 if (RegNo == 0 && Tok.getString().size() == 3 &&
630 Tok.getString().startswith("db")) {
631 switch (Tok.getString()[2]) {
632 case '0': RegNo = X86::DR0; break;
633 case '1': RegNo = X86::DR1; break;
634 case '2': RegNo = X86::DR2; break;
635 case '3': RegNo = X86::DR3; break;
636 case '4': RegNo = X86::DR4; break;
637 case '5': RegNo = X86::DR5; break;
638 case '6': RegNo = X86::DR6; break;
639 case '7': RegNo = X86::DR7; break;
640 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000641
Chris Lattner645b2092010-06-24 07:29:18 +0000642 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000643 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000644 Parser.Lex(); // Eat it.
645 return false;
646 }
647 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000648
Devang Patel1aea4302012-01-20 22:32:05 +0000649 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000650 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000651 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000652 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000653 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000654
Sean Callananb9a25b72010-01-19 20:27:46 +0000655 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000656 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000657}
658
Devang Pateldd929fc2012-01-12 18:03:40 +0000659X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000660 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000661 return ParseIntelOperand();
662 return ParseATTOperand();
663}
664
Devang Pateld37ad242012-01-17 18:00:18 +0000665/// getIntelMemOperandSize - Return intel memory operand size.
666static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000667 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000668 .Cases("BYTE", "byte", 8)
669 .Cases("WORD", "word", 16)
670 .Cases("DWORD", "dword", 32)
671 .Cases("QWORD", "qword", 64)
672 .Cases("XWORD", "xword", 80)
673 .Cases("XMMWORD", "xmmword", 128)
674 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000675 .Default(0);
676 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000677}
678
Chad Rosierdd2e8952013-01-14 22:31:35 +0000679enum IntelBracExprState {
680 IBES_START,
681 IBES_LBRAC,
682 IBES_RBRAC,
683 IBES_REGISTER,
684 IBES_REGISTER_STAR,
685 IBES_REGISTER_STAR_INTEGER,
686 IBES_INTEGER,
687 IBES_INTEGER_STAR,
688 IBES_INDEX_REGISTER,
689 IBES_IDENTIFIER,
690 IBES_DISP_EXPR,
691 IBES_MINUS,
692 IBES_ERROR
693};
694
695class IntelBracExprStateMachine {
696 IntelBracExprState State;
697 unsigned BaseReg, IndexReg, Scale;
698 int64_t Disp;
699
700 unsigned TmpReg;
701 int64_t TmpInteger;
702
703 bool isPlus;
704
705public:
706 IntelBracExprStateMachine(MCAsmParser &parser) :
707 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(0),
708 TmpReg(0), TmpInteger(0), isPlus(true) {}
709
710 unsigned getBaseReg() { return BaseReg; }
711 unsigned getIndexReg() { return IndexReg; }
712 unsigned getScale() { return Scale; }
713 int64_t getDisp() { return Disp; }
714 bool isValidEndState() { return State == IBES_RBRAC; }
715
716 void onPlus() {
717 switch (State) {
718 default:
719 State = IBES_ERROR;
720 break;
721 case IBES_INTEGER:
722 State = IBES_START;
723 if (isPlus)
724 Disp += TmpInteger;
725 else
726 Disp -= TmpInteger;
727 break;
728 case IBES_REGISTER:
729 State = IBES_START;
730 // If we already have a BaseReg, then assume this is the IndexReg with a
731 // scale of 1.
732 if (!BaseReg) {
733 BaseReg = TmpReg;
734 } else {
735 assert (!IndexReg && "BaseReg/IndexReg already set!");
736 IndexReg = TmpReg;
737 Scale = 1;
738 }
739 break;
740 case IBES_INDEX_REGISTER:
741 State = IBES_START;
742 break;
743 }
744 isPlus = true;
745 }
746 void onMinus() {
747 switch (State) {
748 default:
749 State = IBES_ERROR;
750 break;
751 case IBES_START:
752 State = IBES_MINUS;
753 break;
754 case IBES_INTEGER:
755 State = IBES_START;
756 if (isPlus)
757 Disp += TmpInteger;
758 else
759 Disp -= TmpInteger;
760 break;
761 case IBES_REGISTER:
762 State = IBES_START;
763 // If we already have a BaseReg, then assume this is the IndexReg with a
764 // scale of 1.
765 if (!BaseReg) {
766 BaseReg = TmpReg;
767 } else {
768 assert (!IndexReg && "BaseReg/IndexReg already set!");
769 IndexReg = TmpReg;
770 Scale = 1;
771 }
772 break;
773 case IBES_INDEX_REGISTER:
774 State = IBES_START;
775 break;
776 }
777 isPlus = false;
778 }
779 void onRegister(unsigned Reg) {
780 switch (State) {
781 default:
782 State = IBES_ERROR;
783 break;
784 case IBES_START:
785 State = IBES_REGISTER;
786 TmpReg = Reg;
787 break;
788 case IBES_INTEGER_STAR:
789 assert (!IndexReg && "IndexReg already set!");
790 State = IBES_INDEX_REGISTER;
791 IndexReg = Reg;
792 Scale = TmpInteger;
793 break;
794 }
795 }
796 void onDispExpr() {
797 switch (State) {
798 default:
799 State = IBES_ERROR;
800 break;
801 case IBES_START:
802 State = IBES_DISP_EXPR;
803 break;
804 }
805 }
806 void onInteger(int64_t TmpInt) {
807 switch (State) {
808 default:
809 State = IBES_ERROR;
810 break;
811 case IBES_START:
812 State = IBES_INTEGER;
813 TmpInteger = TmpInt;
814 break;
815 case IBES_MINUS:
816 State = IBES_INTEGER;
817 TmpInteger = TmpInt;
818 break;
819 case IBES_REGISTER_STAR:
820 assert (!IndexReg && "IndexReg already set!");
821 State = IBES_INDEX_REGISTER;
822 IndexReg = TmpReg;
823 Scale = TmpInt;
824 break;
825 }
826 }
827 void onStar() {
828 switch (State) {
829 default:
830 State = IBES_ERROR;
831 break;
832 case IBES_INTEGER:
833 State = IBES_INTEGER_STAR;
834 break;
835 case IBES_REGISTER:
836 State = IBES_REGISTER_STAR;
837 break;
838 }
839 }
840 void onLBrac() {
841 switch (State) {
842 default:
843 State = IBES_ERROR;
844 break;
845 case IBES_RBRAC:
846 State = IBES_START;
847 isPlus = true;
848 break;
849 }
850 }
851 void onRBrac() {
852 switch (State) {
853 default:
854 State = IBES_ERROR;
855 break;
856 case IBES_DISP_EXPR:
857 State = IBES_RBRAC;
858 break;
859 case IBES_INTEGER:
860 State = IBES_RBRAC;
861 if (isPlus)
862 Disp += TmpInteger;
863 else
864 Disp -= TmpInteger;
865 break;
866 case IBES_REGISTER:
867 State = IBES_RBRAC;
868 // If we already have a BaseReg, then assume this is the IndexReg with a
869 // scale of 1.
870 if (!BaseReg) {
871 BaseReg = TmpReg;
872 } else {
873 assert (!IndexReg && "BaseReg/IndexReg already set!");
874 IndexReg = TmpReg;
875 Scale = 1;
876 }
877 break;
878 case IBES_INDEX_REGISTER:
879 State = IBES_RBRAC;
880 break;
881 }
882 }
883};
884
Chad Rosier65c88922012-10-22 19:42:52 +0000885X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000886 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000887 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000888 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000889
Devang Pateld37ad242012-01-17 18:00:18 +0000890 // Eat '['
891 if (getLexer().isNot(AsmToken::LBrac))
892 return ErrorOperand(Start, "Expected '[' token!");
893 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000894
Chad Rosierdd2e8952013-01-14 22:31:35 +0000895 unsigned TmpReg = 0;
896
897 // Try to handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000898 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000899 if (ParseRegister(TmpReg, Start, End)) {
900 const MCExpr *Disp;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000901 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000902 return 0;
903
Devang Pateld37ad242012-01-17 18:00:18 +0000904 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000905 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosier4fb25b72013-02-15 21:58:13 +0000906 // Adjust the EndLoc due to the ']'.
907 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000908 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000909 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000910 }
Devang Pateld37ad242012-01-17 18:00:18 +0000911 }
912
Chad Rosierdd2e8952013-01-14 22:31:35 +0000913 // Parse [ BaseReg + Scale*IndexReg + Disp ].
914 bool Done = false;
915 IntelBracExprStateMachine SM(Parser);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000916
Chad Rosierdd2e8952013-01-14 22:31:35 +0000917 // If we parsed a register, then the end loc has already been set and
918 // the identifier has already been lexed. We also need to update the
919 // state.
920 if (TmpReg)
921 SM.onRegister(TmpReg);
922
923 const MCExpr *Disp = 0;
924 while (!Done) {
925 bool UpdateLocLex = true;
926
927 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
928 // identifier. Don't try an parse it as a register.
929 if (Tok.getString().startswith("."))
930 break;
931
932 switch (getLexer().getKind()) {
933 default: {
934 if (SM.isValidEndState()) {
935 Done = true;
936 break;
937 }
938 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
939 }
940 case AsmToken::Identifier: {
941 // This could be a register or a displacement expression.
942 if(!ParseRegister(TmpReg, Start, End)) {
943 SM.onRegister(TmpReg);
944 UpdateLocLex = false;
945 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000946 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000947 SM.onDispExpr();
948 UpdateLocLex = false;
949 break;
950 }
951 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
952 }
953 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +0000954 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000955 SM.onInteger(Val);
956 break;
957 }
958 case AsmToken::Plus: SM.onPlus(); break;
959 case AsmToken::Minus: SM.onMinus(); break;
960 case AsmToken::Star: SM.onStar(); break;
961 case AsmToken::LBrac: SM.onLBrac(); break;
962 case AsmToken::RBrac: SM.onRBrac(); break;
963 }
964 if (!Done && UpdateLocLex) {
965 End = Tok.getLoc();
966 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +0000967 }
Devang Pateld37ad242012-01-17 18:00:18 +0000968 }
969
Chad Rosierdd2e8952013-01-14 22:31:35 +0000970 if (!Disp)
971 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +0000972
Chad Rosierddb53ef2012-10-26 22:01:25 +0000973 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000974 if (Tok.getString().startswith(".")) {
975 SmallString<64> Err;
976 const MCExpr *NewDisp;
977 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
978 return ErrorOperand(Tok.getLoc(), Err);
979
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000980 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000981 Parser.Lex(); // Eat the field.
982 Disp = NewDisp;
983 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000984
Chad Rosierdd2e8952013-01-14 22:31:35 +0000985 int BaseReg = SM.getBaseReg();
986 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +0000987
Chad Rosierdd2e8952013-01-14 22:31:35 +0000988 // handle [-42]
989 if (!BaseReg && !IndexReg) {
990 if (!SegReg)
991 return X86Operand::CreateMem(Disp, Start, End);
992 else
993 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
994 }
995
996 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +0000997 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000998 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000999}
1000
1001/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001002X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001003 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001004 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001005
1006 unsigned Size = getIntelMemOperandSize(Tok.getString());
1007 if (Size) {
1008 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001009 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1010 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001011 Parser.Lex();
1012 }
1013
Chad Rosierc0a14b82012-10-24 17:22:29 +00001014 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +00001015 return ParseIntelBracExpression(SegReg, Size);
1016
1017 if (!ParseRegister(SegReg, Start, End)) {
1018 // Handel SegReg : [ ... ]
1019 if (getLexer().isNot(AsmToken::Colon))
1020 return ErrorOperand(Start, "Expected ':' token!");
1021 Parser.Lex(); // Eat :
1022 if (getLexer().isNot(AsmToken::LBrac))
1023 return ErrorOperand(Start, "Expected '[' token!");
1024 return ParseIntelBracExpression(SegReg, Size);
1025 }
Devang Pateld37ad242012-01-17 18:00:18 +00001026
1027 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001028 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001029 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001030
Chad Rosier2a784132012-10-23 23:31:33 +00001031 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001032 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001033
Chad Rosier023c8802013-03-19 17:32:17 +00001034 bool NeedSizeDir = false;
1035 bool IsVarDecl = false;
1036 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1037 const MCSymbol &Sym = SymRef->getSymbol();
1038 // FIXME: The SemaLookup will fail if the name is anything other then an
1039 // identifier.
1040 // FIXME: Pass a valid SMLoc.
1041 unsigned tLength, tSize, tType;
1042 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
1043 tSize, tType, IsVarDecl);
1044 if (!Size) {
1045 Size = tType * 8; // Size is in terms of bits in this context.
1046 NeedSizeDir = Size > 0;
1047 }
Chad Rosierc1ec2072013-01-10 22:10:27 +00001048 }
Chad Rosier023c8802013-03-19 17:32:17 +00001049
1050 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1051 // reference. We need an 'r' constraint here, so we need to create register
1052 // operand to ensure proper matching. Just pick a GPR based on the size of
1053 // a pointer.
1054 if (!IsVarDecl) {
1055 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1056 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
1057 }
1058
1059 if (NeedSizeDir)
1060 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, Start,
1061 /*Len*/0, Size));
1062
1063 // When parsing inline assembly we set the base register to a non-zero value
1064 // as we don't know the actual value at this time. This is necessary to
1065 // get the matching correct in some cases.
1066 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
1067 /*Scale*/1, Start, End, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001068}
1069
Chad Rosier22f441a2012-10-24 22:21:50 +00001070/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001071bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1072 const MCExpr **NewDisp,
1073 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001074 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001075 uint64_t OrigDispVal, DotDispVal;
1076
1077 // FIXME: Handle non-constant expressions.
1078 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1079 OrigDispVal = OrigDisp->getValue();
1080 } else {
1081 Err = "Non-constant offsets are not supported!";
1082 return true;
1083 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001084
1085 // Drop the '.'.
1086 StringRef DotDispStr = Tok.getString().drop_front(1);
1087
Chad Rosier22f441a2012-10-24 22:21:50 +00001088 // .Imm gets lexed as a real.
1089 if (Tok.is(AsmToken::Real)) {
1090 APInt DotDisp;
1091 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001092 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001093 } else if (Tok.is(AsmToken::Identifier)) {
1094 // We should only see an identifier when parsing the original inline asm.
1095 // The front-end should rewrite this in terms of immediates.
1096 assert (isParsingInlineAsm() && "Unexpected field name!");
1097
1098 unsigned DotDisp;
1099 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1100 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1101 DotDisp)) {
1102 Err = "Unable to lookup field reference!";
1103 return true;
1104 }
1105 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001106 } else {
1107 Err = "Unexpected token type!";
1108 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001109 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001110
Chad Rosierec130222012-10-25 21:51:10 +00001111 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1112 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1113 unsigned Len = DotDispStr.size();
1114 unsigned Val = OrigDispVal + DotDispVal;
1115 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1116 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001117 }
1118
1119 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1120 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001121}
1122
Chad Rosierc0a14b82012-10-24 17:22:29 +00001123/// Parse the 'offset' operator. This operator is used to specify the
1124/// location rather then the content of a variable.
1125X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1126 SMLoc OffsetOfLoc = Start;
1127 Parser.Lex(); // Eat offset.
1128 Start = Parser.getTok().getLoc();
1129 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1130
Chad Rosier6e431572012-10-26 16:09:20 +00001131 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001132 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001133 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001134 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001135
Chad Rosier6e431572012-10-26 16:09:20 +00001136 // Don't emit the offset operator.
1137 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1138
Chad Rosierc0a14b82012-10-24 17:22:29 +00001139 // The offset operator will have an 'r' constraint, thus we need to create
1140 // register operand to ensure proper matching. Just pick a GPR based on
1141 // the size of a pointer.
1142 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001143 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1144 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001145}
1146
Chad Rosier505bca32013-01-17 19:21:48 +00001147enum IntelOperatorKind {
1148 IOK_LENGTH,
1149 IOK_SIZE,
1150 IOK_TYPE
1151};
1152
1153/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1154/// returns the number of elements in an array. It returns the value 1 for
1155/// non-array variables. The SIZE operator returns the size of a C or C++
1156/// variable. A variable's size is the product of its LENGTH and TYPE. The
1157/// TYPE operator returns the size of a C or C++ type or variable. If the
1158/// variable is an array, TYPE returns the size of a single element.
1159X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001160 SMLoc TypeLoc = Start;
1161 Parser.Lex(); // Eat offset.
1162 Start = Parser.getTok().getLoc();
1163 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1164
1165 SMLoc End;
1166 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001167 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001168 return 0;
1169
Chad Rosier505bca32013-01-17 19:21:48 +00001170 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001171 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1172 const MCSymbol &Sym = SymRef->getSymbol();
1173 // FIXME: The SemaLookup will fail if the name is anything other then an
1174 // identifier.
1175 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001176 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001177 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1178 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001179 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001180 }
1181 unsigned CVal;
1182 switch(OpKind) {
1183 default: llvm_unreachable("Unexpected operand kind!");
1184 case IOK_LENGTH: CVal = Length; break;
1185 case IOK_SIZE: CVal = Size; break;
1186 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001187 }
1188
1189 // Rewrite the type operator and the C or C++ type or variable in terms of an
1190 // immediate. E.g. TYPE foo -> $$4
1191 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001192 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001193
Chad Rosier505bca32013-01-17 19:21:48 +00001194 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierefcb3d92012-10-26 18:04:20 +00001195 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
1196}
1197
Devang Pateld37ad242012-01-17 18:00:18 +00001198X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001199 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001200 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001201
Chad Rosier505bca32013-01-17 19:21:48 +00001202 // Offset, length, type and size operators.
1203 if (isParsingInlineAsm()) {
1204 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1205 return ParseIntelOffsetOfOperator(Start);
1206 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1207 return ParseIntelOperator(Start, IOK_LENGTH);
1208 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1209 return ParseIntelOperator(Start, IOK_SIZE);
1210 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1211 return ParseIntelOperator(Start, IOK_TYPE);
1212 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001213
Chad Rosier505bca32013-01-17 19:21:48 +00001214 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001215 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1216 getLexer().is(AsmToken::Minus)) {
1217 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001218 if (!getParser().parseExpression(Val, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +00001219 return X86Operand::CreateImm(Val, Start, End);
1220 }
1221 }
1222
Chad Rosier505bca32013-01-17 19:21:48 +00001223 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001224 unsigned RegNo = 0;
1225 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001226 // If this is a segment register followed by a ':', then this is the start
1227 // of a memory reference, otherwise this is a normal register reference.
1228 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001229 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001230
1231 getParser().Lex(); // Eat the colon.
1232 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001233 }
1234
Chad Rosier505bca32013-01-17 19:21:48 +00001235 // Memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001236 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001237}
1238
Devang Pateldd929fc2012-01-12 18:03:40 +00001239X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001240 switch (getLexer().getKind()) {
1241 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001242 // Parse a memory operand with no segment register.
1243 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001244 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001245 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001246 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001247 SMLoc Start, End;
1248 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001249 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001250 Error(Start, "%eiz and %riz can only be used as index registers",
1251 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001252 return 0;
1253 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001254
Chris Lattnereef6d782010-04-17 18:56:34 +00001255 // If this is a segment register followed by a ':', then this is the start
1256 // of a memory reference, otherwise this is a normal register reference.
1257 if (getLexer().isNot(AsmToken::Colon))
1258 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001259
1260
Chris Lattnereef6d782010-04-17 18:56:34 +00001261 getParser().Lex(); // Eat the colon.
1262 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001263 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001264 case AsmToken::Dollar: {
1265 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001266 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001267 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001268 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001269 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001270 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001271 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001272 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001273 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001274}
1275
Chris Lattnereef6d782010-04-17 18:56:34 +00001276/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1277/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001278X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001279
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001280 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1281 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001282 // only way to do this without lookahead is to eat the '(' and see what is
1283 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001284 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001285 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001286 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001287 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001288
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001289 // After parsing the base expression we could either have a parenthesized
1290 // memory address or not. If not, return now. If so, eat the (.
1291 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001292 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001293 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001294 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001295 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001296 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001297
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001298 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001299 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001300 } else {
1301 // Okay, we have a '('. We don't know if this is an expression or not, but
1302 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001303 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001304 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001305
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001306 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001307 // Nothing to do here, fall into the code below with the '(' part of the
1308 // memory operand consumed.
1309 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001310 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001311
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001312 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001313 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001314 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001315
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001316 // After parsing the base expression we could either have a parenthesized
1317 // memory address or not. If not, return now. If so, eat the (.
1318 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001319 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001320 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001321 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001322 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001323 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001324
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001325 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001326 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001327 }
1328 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001329
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001330 // If we reached here, then we just ate the ( of the memory operand. Process
1331 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001332 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001333 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001334
Chris Lattner29ef9a22010-01-15 18:51:29 +00001335 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001336 SMLoc StartLoc, EndLoc;
1337 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001338 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001339 Error(StartLoc, "eiz and riz can only be used as index registers",
1340 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001341 return 0;
1342 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001343 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001344
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001345 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001346 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001347 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001348
1349 // Following the comma we should have either an index register, or a scale
1350 // value. We don't support the later form, but we want to parse it
1351 // correctly.
1352 //
1353 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001354 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001355 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001356 SMLoc L;
1357 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001358
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001359 if (getLexer().isNot(AsmToken::RParen)) {
1360 // Parse the scale amount:
1361 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001362 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001363 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001364 "expected comma in scale expression");
1365 return 0;
1366 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001367 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001368
1369 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001370 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001371
1372 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001373 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001374 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001375 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001376 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001377
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001378 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001379 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1380 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1381 return 0;
1382 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001383 Scale = (unsigned)ScaleVal;
1384 }
1385 }
1386 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001387 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001388 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001389 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001390
1391 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001392 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001393 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001394
Daniel Dunbaree910252010-08-24 19:13:38 +00001395 if (Value != 1)
1396 Warning(Loc, "scale factor without index register is ignored");
1397 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001398 }
1399 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001400
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001401 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001402 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001403 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001404 return 0;
1405 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001406 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001407 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001408
Kevin Enderby84faf652012-03-12 21:32:09 +00001409 // If we have both a base register and an index register make sure they are
1410 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001411 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001412 if (BaseReg != 0 && IndexReg != 0) {
1413 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001414 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1415 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001416 IndexReg != X86::RIZ) {
1417 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1418 return 0;
1419 }
1420 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001421 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1422 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001423 IndexReg != X86::EIZ){
1424 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1425 return 0;
1426 }
1427 }
1428
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001429 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1430 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001431}
1432
Devang Pateldd929fc2012-01-12 18:03:40 +00001433bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001434ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001435 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001436 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001437 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001438
Chris Lattnerd8f71792010-11-28 20:23:50 +00001439 // FIXME: Hack to recognize setneb as setne.
1440 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1441 PatchedName != "setb" && PatchedName != "setnb")
1442 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001443
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001444 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1445 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001446 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001447 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1448 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001449 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001450 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001451 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001452 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001453 .Case("eq", 0x00)
1454 .Case("lt", 0x01)
1455 .Case("le", 0x02)
1456 .Case("unord", 0x03)
1457 .Case("neq", 0x04)
1458 .Case("nlt", 0x05)
1459 .Case("nle", 0x06)
1460 .Case("ord", 0x07)
1461 /* AVX only from here */
1462 .Case("eq_uq", 0x08)
1463 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001464 .Case("ngt", 0x0A)
1465 .Case("false", 0x0B)
1466 .Case("neq_oq", 0x0C)
1467 .Case("ge", 0x0D)
1468 .Case("gt", 0x0E)
1469 .Case("true", 0x0F)
1470 .Case("eq_os", 0x10)
1471 .Case("lt_oq", 0x11)
1472 .Case("le_oq", 0x12)
1473 .Case("unord_s", 0x13)
1474 .Case("neq_us", 0x14)
1475 .Case("nlt_uq", 0x15)
1476 .Case("nle_uq", 0x16)
1477 .Case("ord_s", 0x17)
1478 .Case("eq_us", 0x18)
1479 .Case("nge_uq", 0x19)
1480 .Case("ngt_uq", 0x1A)
1481 .Case("false_os", 0x1B)
1482 .Case("neq_os", 0x1C)
1483 .Case("ge_oq", 0x1D)
1484 .Case("gt_oq", 0x1E)
1485 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001486 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001487 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001488 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1489 getParser().getContext());
1490 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001491 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001492 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001493 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001494 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001495 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001496 } else {
1497 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001498 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001499 }
1500 }
1501 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001502
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001503 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001504
Devang Patel885f65b2012-01-30 22:47:12 +00001505 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001506 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001507
Chris Lattner2544f422010-09-08 05:17:37 +00001508 // Determine whether this is an instruction prefix.
1509 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001510 Name == "lock" || Name == "rep" ||
1511 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001512 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001513 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001514
1515
Chris Lattner2544f422010-09-08 05:17:37 +00001516 // This does the actual operand parsing. Don't parse any more if we have a
1517 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1518 // just want to parse the "lock" as the first instruction and the "incl" as
1519 // the next one.
1520 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001521
1522 // Parse '*' modifier.
1523 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001524 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001525 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001526 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001527 }
1528
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001529 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001530 if (X86Operand *Op = ParseOperand())
1531 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001532 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001533 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001534 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001535 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001536
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001537 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001538 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001539
1540 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001541 if (X86Operand *Op = ParseOperand())
1542 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001543 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001544 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001545 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001546 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001547 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001548
Chris Lattnercbf8a982010-09-11 16:18:25 +00001549 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001550 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001551 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001552 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001553 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001554 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001555
Chris Lattner2544f422010-09-08 05:17:37 +00001556 if (getLexer().is(AsmToken::EndOfStatement))
1557 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001558 else if (isPrefix && getLexer().is(AsmToken::Slash))
1559 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001560
Devang Patel885f65b2012-01-30 22:47:12 +00001561 if (ExtraImmOp && isParsingIntelSyntax())
1562 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1563
Chris Lattner98c870f2010-11-06 19:25:43 +00001564 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1565 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1566 // documented form in various unofficial manuals, so a lot of code uses it.
1567 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1568 Operands.size() == 3) {
1569 X86Operand &Op = *(X86Operand*)Operands.back();
1570 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1571 isa<MCConstantExpr>(Op.Mem.Disp) &&
1572 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1573 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1574 SMLoc Loc = Op.getEndLoc();
1575 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1576 delete &Op;
1577 }
1578 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001579 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1580 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1581 Operands.size() == 3) {
1582 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1583 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1584 isa<MCConstantExpr>(Op.Mem.Disp) &&
1585 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1586 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1587 SMLoc Loc = Op.getEndLoc();
1588 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1589 delete &Op;
1590 }
1591 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001592 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1593 if (Name.startswith("ins") && Operands.size() == 3 &&
1594 (Name == "insb" || Name == "insw" || Name == "insl")) {
1595 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1596 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1597 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1598 Operands.pop_back();
1599 Operands.pop_back();
1600 delete &Op;
1601 delete &Op2;
1602 }
1603 }
1604
1605 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1606 if (Name.startswith("outs") && Operands.size() == 3 &&
1607 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1608 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1609 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1610 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1611 Operands.pop_back();
1612 Operands.pop_back();
1613 delete &Op;
1614 delete &Op2;
1615 }
1616 }
1617
1618 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1619 if (Name.startswith("movs") && Operands.size() == 3 &&
1620 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001621 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001622 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1623 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1624 if (isSrcOp(Op) && isDstOp(Op2)) {
1625 Operands.pop_back();
1626 Operands.pop_back();
1627 delete &Op;
1628 delete &Op2;
1629 }
1630 }
1631 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1632 if (Name.startswith("lods") && Operands.size() == 3 &&
1633 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001634 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001635 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1636 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1637 if (isSrcOp(*Op1) && Op2->isReg()) {
1638 const char *ins;
1639 unsigned reg = Op2->getReg();
1640 bool isLods = Name == "lods";
1641 if (reg == X86::AL && (isLods || Name == "lodsb"))
1642 ins = "lodsb";
1643 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1644 ins = "lodsw";
1645 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1646 ins = "lodsl";
1647 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1648 ins = "lodsq";
1649 else
1650 ins = NULL;
1651 if (ins != NULL) {
1652 Operands.pop_back();
1653 Operands.pop_back();
1654 delete Op1;
1655 delete Op2;
1656 if (Name != ins)
1657 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1658 }
1659 }
1660 }
1661 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1662 if (Name.startswith("stos") && Operands.size() == 3 &&
1663 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001664 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001665 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1666 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1667 if (isDstOp(*Op2) && Op1->isReg()) {
1668 const char *ins;
1669 unsigned reg = Op1->getReg();
1670 bool isStos = Name == "stos";
1671 if (reg == X86::AL && (isStos || Name == "stosb"))
1672 ins = "stosb";
1673 else if (reg == X86::AX && (isStos || Name == "stosw"))
1674 ins = "stosw";
1675 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1676 ins = "stosl";
1677 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1678 ins = "stosq";
1679 else
1680 ins = NULL;
1681 if (ins != NULL) {
1682 Operands.pop_back();
1683 Operands.pop_back();
1684 delete Op1;
1685 delete Op2;
1686 if (Name != ins)
1687 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1688 }
1689 }
1690 }
1691
Chris Lattnere9e16a32010-09-15 04:33:27 +00001692 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001693 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001694 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001695 Name.startswith("shl") || Name.startswith("sal") ||
1696 Name.startswith("rcl") || Name.startswith("rcr") ||
1697 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001698 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001699 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001700 // Intel syntax
1701 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1702 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001703 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1704 delete Operands[2];
1705 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001706 }
1707 } else {
1708 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1709 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001710 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1711 delete Operands[1];
1712 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001713 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001714 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001715 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001716
Chris Lattner15f89512011-04-09 19:41:05 +00001717 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1718 // instalias with an immediate operand yet.
1719 if (Name == "int" && Operands.size() == 2) {
1720 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1721 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1722 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1723 delete Operands[1];
1724 Operands.erase(Operands.begin() + 1);
1725 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1726 }
1727 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001728
Chris Lattner98986712010-01-14 22:21:20 +00001729 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001730}
1731
Craig Topper4bef9612013-03-18 02:53:34 +00001732static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1733 bool isCmp) {
1734 MCInst TmpInst;
1735 TmpInst.setOpcode(Opcode);
1736 if (!isCmp)
1737 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1738 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1739 TmpInst.addOperand(Inst.getOperand(0));
1740 Inst = TmpInst;
1741 return true;
1742}
1743
1744static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1745 bool isCmp = false) {
1746 if (!Inst.getOperand(0).isImm() ||
1747 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1748 return false;
1749
1750 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1751}
1752
1753static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1754 bool isCmp = false) {
1755 if (!Inst.getOperand(0).isImm() ||
1756 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1757 return false;
1758
1759 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1760}
1761
1762static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1763 bool isCmp = false) {
1764 if (!Inst.getOperand(0).isImm() ||
1765 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1766 return false;
1767
1768 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1769}
1770
Devang Pateldd929fc2012-01-12 18:03:40 +00001771bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001772processInstruction(MCInst &Inst,
1773 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1774 switch (Inst.getOpcode()) {
1775 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001776 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1777 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1778 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1779 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1780 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1781 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1782 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1783 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1784 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1785 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1786 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1787 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1788 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1789 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1790 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1791 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1792 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1793 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001794 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1795 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1796 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1797 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1798 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1799 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001800 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001801}
1802
Jim Grosbach3ca63822012-11-14 18:04:47 +00001803static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001804bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001805MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001806 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001807 MCStreamer &Out, unsigned &ErrorInfo,
1808 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001809 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001810 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1811 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001812 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001813
Chris Lattner7c51a312010-09-29 01:50:45 +00001814 // First, handle aliases that expand to multiple instructions.
1815 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001816 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001817 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001818 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001819 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001820 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001821 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001822 MCInst Inst;
1823 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001824 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001825 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001826 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001827
Chris Lattner0bb83a82010-09-30 16:39:29 +00001828 const char *Repl =
1829 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001830 .Case("finit", "fninit")
1831 .Case("fsave", "fnsave")
1832 .Case("fstcw", "fnstcw")
1833 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001834 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001835 .Case("fstsw", "fnstsw")
1836 .Case("fstsww", "fnstsw")
1837 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001838 .Default(0);
1839 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001840 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001841 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001842 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001843
Chris Lattnera008e8a2010-09-06 21:54:15 +00001844 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001845 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001846
Daniel Dunbarc918d602010-05-04 16:12:42 +00001847 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001848 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001849 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001850 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001851 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001852 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001853 // Some instructions need post-processing to, for example, tweak which
1854 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001855 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001856 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001857 while (processInstruction(Inst, Operands))
1858 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001859
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001860 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001861 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001862 Out.EmitInstruction(Inst);
1863 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001864 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001865 case Match_MissingFeature: {
1866 assert(ErrorInfo && "Unknown missing feature!");
1867 // Special case the error message for the very common case where only
1868 // a single subtarget feature is missing.
1869 std::string Msg = "instruction requires:";
1870 unsigned Mask = 1;
1871 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1872 if (ErrorInfo & Mask) {
1873 Msg += " ";
1874 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1875 }
1876 Mask <<= 1;
1877 }
1878 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1879 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001880 case Match_InvalidOperand:
1881 WasOriginallyInvalidOperand = true;
1882 break;
1883 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001884 break;
1885 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001886
Daniel Dunbarc918d602010-05-04 16:12:42 +00001887 // FIXME: Ideally, we would only attempt suffix matches for things which are
1888 // valid prefixes, and we could just infer the right unambiguous
1889 // type. However, that requires substantially more matcher support than the
1890 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001891
Daniel Dunbarc918d602010-05-04 16:12:42 +00001892 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001893 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001894 SmallString<16> Tmp;
1895 Tmp += Base;
1896 Tmp += ' ';
1897 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001898
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001899 // If this instruction starts with an 'f', then it is a floating point stack
1900 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1901 // 80-bit floating point, which use the suffixes s,l,t respectively.
1902 //
1903 // Otherwise, we assume that this may be an integer instruction, which comes
1904 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1905 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001906
Daniel Dunbarc918d602010-05-04 16:12:42 +00001907 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001908 Tmp[Base.size()] = Suffixes[0];
1909 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00001910 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001911 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001912
Chad Rosier6e006d32012-10-12 22:53:36 +00001913 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1914 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001915 // If this returned as a missing feature failure, remember that.
1916 if (Match1 == Match_MissingFeature)
1917 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001918 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001919 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1920 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001921 // If this returned as a missing feature failure, remember that.
1922 if (Match2 == Match_MissingFeature)
1923 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001924 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001925 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1926 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001927 // If this returned as a missing feature failure, remember that.
1928 if (Match3 == Match_MissingFeature)
1929 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001930 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001931 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1932 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001933 // If this returned as a missing feature failure, remember that.
1934 if (Match4 == Match_MissingFeature)
1935 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001936
1937 // Restore the old token.
1938 Op->setTokenValue(Base);
1939
1940 // If exactly one matched, then we treat that as a successful match (and the
1941 // instruction will already have been filled in correctly, since the failing
1942 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001943 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001944 (Match1 == Match_Success) + (Match2 == Match_Success) +
1945 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001946 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001947 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001948 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001949 Out.EmitInstruction(Inst);
1950 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001951 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001952 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001953
Chris Lattnerec6789f2010-09-06 20:08:02 +00001954 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001955
Daniel Dunbar09062b12010-08-12 00:55:42 +00001956 // If we had multiple suffix matches, then identify this as an ambiguous
1957 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001958 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001959 char MatchChars[4];
1960 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001961 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1962 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1963 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1964 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001965
1966 SmallString<126> Msg;
1967 raw_svector_ostream OS(Msg);
1968 OS << "ambiguous instructions require an explicit suffix (could be ";
1969 for (unsigned i = 0; i != NumMatches; ++i) {
1970 if (i != 0)
1971 OS << ", ";
1972 if (i + 1 == NumMatches)
1973 OS << "or ";
1974 OS << "'" << Base << MatchChars[i] << "'";
1975 }
1976 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001977 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001978 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001979 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001980
Chris Lattnera008e8a2010-09-06 21:54:15 +00001981 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001982
Chris Lattnera008e8a2010-09-06 21:54:15 +00001983 // If all of the instructions reported an invalid mnemonic, then the original
1984 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001985 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1986 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001987 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001988 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001989 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001990 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001991 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001992 }
1993
1994 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001995 if (ErrorInfo != ~0U) {
1996 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001997 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001998 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001999
Chad Rosier84125ca2012-10-13 00:26:04 +00002000 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002001 if (Operand->getStartLoc().isValid()) {
2002 SMRange OperandRange = Operand->getLocRange();
2003 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002004 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002005 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002006 }
2007
Chad Rosierb4fdade2012-08-21 19:36:59 +00002008 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002009 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002010 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002011
Chris Lattnerec6789f2010-09-06 20:08:02 +00002012 // If one instruction matched with a missing feature, report this as a
2013 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002014 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2015 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002016 std::string Msg = "instruction requires:";
2017 unsigned Mask = 1;
2018 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2019 if (ErrorInfoMissingFeature & Mask) {
2020 Msg += " ";
2021 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2022 }
2023 Mask <<= 1;
2024 }
2025 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002026 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002027
Chris Lattnera008e8a2010-09-06 21:54:15 +00002028 // If one instruction matched with an invalid operand, report this as an
2029 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002030 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2031 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002032 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002033 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002034 return true;
2035 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002036
Chris Lattnerec6789f2010-09-06 20:08:02 +00002037 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002038 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002039 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002040 return true;
2041}
2042
2043
Devang Pateldd929fc2012-01-12 18:03:40 +00002044bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002045 StringRef IDVal = DirectiveID.getIdentifier();
2046 if (IDVal == ".word")
2047 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002048 else if (IDVal.startswith(".code"))
2049 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002050 else if (IDVal.startswith(".att_syntax")) {
2051 getParser().setAssemblerDialect(0);
2052 return false;
2053 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002054 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002055 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2056 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002057 // FIXME : Handle noprefix
2058 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002059 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002060 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002061 }
2062 return false;
2063 }
Chris Lattner537ca842010-10-30 17:38:55 +00002064 return true;
2065}
2066
2067/// ParseDirectiveWord
2068/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002069bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002070 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2071 for (;;) {
2072 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002073 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002074 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002075
Eric Christopher1ced2082013-01-09 03:52:05 +00002076 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002077
Chris Lattner537ca842010-10-30 17:38:55 +00002078 if (getLexer().is(AsmToken::EndOfStatement))
2079 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002080
Chris Lattner537ca842010-10-30 17:38:55 +00002081 // FIXME: Improve diagnostic.
2082 if (getLexer().isNot(AsmToken::Comma))
2083 return Error(L, "unexpected token in directive");
2084 Parser.Lex();
2085 }
2086 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002087
Chris Lattner537ca842010-10-30 17:38:55 +00002088 Parser.Lex();
2089 return false;
2090}
2091
Evan Chengbd27f5a2011-07-27 00:38:12 +00002092/// ParseDirectiveCode
2093/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002094bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002095 if (IDVal == ".code32") {
2096 Parser.Lex();
2097 if (is64BitMode()) {
2098 SwitchMode();
2099 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2100 }
2101 } else if (IDVal == ".code64") {
2102 Parser.Lex();
2103 if (!is64BitMode()) {
2104 SwitchMode();
2105 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2106 }
2107 } else {
2108 return Error(L, "unexpected directive " + IDVal);
2109 }
Chris Lattner537ca842010-10-30 17:38:55 +00002110
Evan Chengbd27f5a2011-07-27 00:38:12 +00002111 return false;
2112}
Chris Lattner537ca842010-10-30 17:38:55 +00002113
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002114// Force static initialization.
2115extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002116 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2117 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002118}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002119
Chris Lattner0692ee62010-09-06 19:11:01 +00002120#define GET_REGISTER_MATCHER
2121#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002122#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002123#include "X86GenAsmMatcher.inc"