blob: 1c45973f3718c49aa9c260e7948cd4bc6e24825b [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;
194 bool NeedSizeDir;
195 };
196
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000197 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000198 struct TokOp Tok;
199 struct RegOp Reg;
200 struct ImmOp Imm;
201 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000202 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000203
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000204 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000205 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000206
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000207 /// getStartLoc - Get the location of the first token of this operand.
208 SMLoc getStartLoc() const { return StartLoc; }
209 /// getEndLoc - Get the location of the last token of this operand.
210 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000211 /// getLocRange - Get the range between the first and last token of this
212 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000213 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000214 /// getOffsetOfLoc - Get the location of the offset operator.
215 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000216
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000217 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000218
Daniel Dunbar20927f22009-08-07 08:26:05 +0000219 StringRef getToken() const {
220 assert(Kind == Token && "Invalid access!");
221 return StringRef(Tok.Data, Tok.Length);
222 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000223 void setTokenValue(StringRef Value) {
224 assert(Kind == Token && "Invalid access!");
225 Tok.Data = Value.data();
226 Tok.Length = Value.size();
227 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000228
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000229 unsigned getReg() const {
230 assert(Kind == Register && "Invalid access!");
231 return Reg.RegNo;
232 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000233
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000234 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000235 assert(Kind == Immediate && "Invalid access!");
236 return Imm.Val;
237 }
238
Chad Rosierefcb3d92012-10-26 18:04:20 +0000239 bool needAsmRewrite() const {
240 assert(Kind == Immediate && "Invalid access!");
241 return Imm.NeedAsmRewrite;
242 }
243
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000244 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000245 assert(Kind == Memory && "Invalid access!");
246 return Mem.Disp;
247 }
248 unsigned getMemSegReg() const {
249 assert(Kind == Memory && "Invalid access!");
250 return Mem.SegReg;
251 }
252 unsigned getMemBaseReg() const {
253 assert(Kind == Memory && "Invalid access!");
254 return Mem.BaseReg;
255 }
256 unsigned getMemIndexReg() const {
257 assert(Kind == Memory && "Invalid access!");
258 return Mem.IndexReg;
259 }
260 unsigned getMemScale() const {
261 assert(Kind == Memory && "Invalid access!");
262 return Mem.Scale;
263 }
264
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000265 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000266
267 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000268
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000269 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000270 if (!isImm())
271 return false;
272
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000273 // If this isn't a constant expr, just assume it fits and let relaxation
274 // handle it.
275 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
276 if (!CE)
277 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000278
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000279 // Otherwise, check the value is in a range that makes sense for this
280 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000281 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000282 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000284 if (!isImm())
285 return false;
286
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000287 // If this isn't a constant expr, just assume it fits and let relaxation
288 // handle it.
289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
290 if (!CE)
291 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000292
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000293 // Otherwise, check the value is in a range that makes sense for this
294 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000295 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000296 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000297 bool isImmZExtu32u8() const {
298 if (!isImm())
299 return false;
300
301 // If this isn't a constant expr, just assume it fits and let relaxation
302 // handle it.
303 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
304 if (!CE)
305 return true;
306
307 // Otherwise, check the value is in a range that makes sense for this
308 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000309 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000310 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000311 bool isImmSExti64i8() const {
312 if (!isImm())
313 return false;
314
315 // If this isn't a constant expr, just assume it fits and let relaxation
316 // handle it.
317 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
318 if (!CE)
319 return true;
320
321 // Otherwise, check the value is in a range that makes sense for this
322 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000323 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000324 }
325 bool isImmSExti64i32() const {
326 if (!isImm())
327 return false;
328
329 // If this isn't a constant expr, just assume it fits and let relaxation
330 // handle it.
331 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
332 if (!CE)
333 return true;
334
335 // Otherwise, check the value is in a range that makes sense for this
336 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000337 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000338 }
339
Chad Rosier96d58e62012-10-19 20:57:14 +0000340 unsigned getMemSize() const {
341 assert(Kind == Memory && "Invalid access!");
342 return Mem.Size;
343 }
344
Chad Rosiera703fb92012-10-22 19:50:35 +0000345 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000346 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000347 }
348
Chad Rosierc1ec2072013-01-10 22:10:27 +0000349 bool needAddressOf() const {
350 return AddressOf;
351 }
352
Chad Rosier96d58e62012-10-19 20:57:14 +0000353 bool needSizeDirective() const {
354 assert(Kind == Memory && "Invalid access!");
355 return Mem.NeedSizeDir;
356 }
357
Daniel Dunbar20927f22009-08-07 08:26:05 +0000358 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000359 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000360 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000361 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000362 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000363 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000364 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000365 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000366 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000367 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000368 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000369 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000370 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000371 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000372 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000373 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000374 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000375 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000376 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000377 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000378 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000379 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380
Craig Topper75dc33a2012-07-18 04:11:12 +0000381 bool isMemVX32() const {
382 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
383 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
384 }
385 bool isMemVY32() const {
386 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
387 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
388 }
389 bool isMemVX64() const {
390 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
391 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
392 }
393 bool isMemVY64() const {
394 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
395 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
396 }
397
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000398 bool isAbsMem() const {
399 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000400 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000401 }
402
Daniel Dunbar20927f22009-08-07 08:26:05 +0000403 bool isReg() const { return Kind == Register; }
404
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000405 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
406 // Add as immediates when possible.
407 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
408 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
409 else
410 Inst.addOperand(MCOperand::CreateExpr(Expr));
411 }
412
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000413 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000414 assert(N == 1 && "Invalid number of operands!");
415 Inst.addOperand(MCOperand::CreateReg(getReg()));
416 }
417
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000418 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000419 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000420 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000421 }
422
Chad Rosier36b8fed2012-06-27 22:34:28 +0000423 void addMem8Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000425 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000426 void addMem16Operands(MCInst &Inst, unsigned N) const {
427 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000428 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000429 void addMem32Operands(MCInst &Inst, unsigned N) const {
430 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000431 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000432 void addMem64Operands(MCInst &Inst, unsigned N) const {
433 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000434 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000435 void addMem80Operands(MCInst &Inst, unsigned N) const {
436 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000437 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000438 void addMem128Operands(MCInst &Inst, unsigned N) const {
439 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000440 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000441 void addMem256Operands(MCInst &Inst, unsigned N) const {
442 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000443 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000444 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
445 addMemOperands(Inst, N);
446 }
447 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
448 addMemOperands(Inst, N);
449 }
450 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
451 addMemOperands(Inst, N);
452 }
453 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
454 addMemOperands(Inst, N);
455 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000456
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000457 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000458 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000459 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
460 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
461 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000462 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000463 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
464 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000465
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000466 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
467 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000468 // Add as immediates when possible.
469 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
470 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
471 else
472 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000473 }
474
Chris Lattnerb4307b32010-01-15 19:28:38 +0000475 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000476 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000477 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000478 Res->Tok.Data = Str.data();
479 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000480 return Res;
481 }
482
Chad Rosierc0a14b82012-10-24 17:22:29 +0000483 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000484 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000485 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000486 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000487 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000488 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000489 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000490 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000491 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000492
Chad Rosierefcb3d92012-10-26 18:04:20 +0000493 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc,
494 bool NeedRewrite = true){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000495 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000496 Res->Imm.Val = Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000497 Res->Imm.NeedAsmRewrite = NeedRewrite;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000498 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000499 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000500
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000501 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000502 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier7109fbe2013-01-10 23:39:07 +0000503 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000504 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
505 Res->Mem.SegReg = 0;
506 Res->Mem.Disp = Disp;
507 Res->Mem.BaseReg = 0;
508 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000509 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000510 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000511 Res->Mem.NeedSizeDir = NeedSizeDir;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000512 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000513 return Res;
514 }
515
516 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000517 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
518 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000519 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000520 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000521 // We should never just have a displacement, that should be parsed as an
522 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000523 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
524
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000525 // The scale should always be one of {1,2,4,8}.
526 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000528 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000529 Res->Mem.SegReg = SegReg;
530 Res->Mem.Disp = Disp;
531 Res->Mem.BaseReg = BaseReg;
532 Res->Mem.IndexReg = IndexReg;
533 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000534 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000535 Res->Mem.NeedSizeDir = NeedSizeDir;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000536 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000537 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000538 }
539};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000540
Chris Lattner37dfdec2009-07-29 06:33:53 +0000541} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000542
Devang Pateldd929fc2012-01-12 18:03:40 +0000543bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000544 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000545
546 return (Op.isMem() &&
547 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
548 isa<MCConstantExpr>(Op.Mem.Disp) &&
549 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
550 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
551}
552
Devang Pateldd929fc2012-01-12 18:03:40 +0000553bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000554 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000555
Chad Rosier36b8fed2012-06-27 22:34:28 +0000556 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000557 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000558 isa<MCConstantExpr>(Op.Mem.Disp) &&
559 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
560 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
561}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000562
Devang Pateldd929fc2012-01-12 18:03:40 +0000563bool X86AsmParser::ParseRegister(unsigned &RegNo,
564 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000565 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000566 const AsmToken &PercentTok = Parser.getTok();
567 StartLoc = PercentTok.getLoc();
568
569 // If we encounter a %, ignore it. This code handles registers with and
570 // without the prefix, unprefixed registers can occur in cfi directives.
571 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000572 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000573
Sean Callanan18b83232010-01-19 21:44:56 +0000574 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000575 EndLoc = Tok.getEndLoc();
576
Devang Patel1aea4302012-01-20 22:32:05 +0000577 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000578 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000579 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000580 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000581 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000582
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000583 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Chris Lattner33d60d52010-09-22 04:11:10 +0000585 // If the match failed, try the register name as lowercase.
586 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000587 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000588
Evan Cheng5de728c2011-07-27 23:22:03 +0000589 if (!is64BitMode()) {
590 // FIXME: This should be done using Requires<In32BitMode> and
591 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
592 // checked.
593 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
594 // REX prefix.
595 if (RegNo == X86::RIZ ||
596 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
597 X86II::isX86_64NonExtLowByteReg(RegNo) ||
598 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000599 return Error(StartLoc, "register %"
600 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000601 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000602 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000603
Chris Lattner33d60d52010-09-22 04:11:10 +0000604 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
605 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000606 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000607 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000609 // Check to see if we have '(4)' after %st.
610 if (getLexer().isNot(AsmToken::LParen))
611 return false;
612 // Lex the paren.
613 getParser().Lex();
614
615 const AsmToken &IntTok = Parser.getTok();
616 if (IntTok.isNot(AsmToken::Integer))
617 return Error(IntTok.getLoc(), "expected stack index");
618 switch (IntTok.getIntVal()) {
619 case 0: RegNo = X86::ST0; break;
620 case 1: RegNo = X86::ST1; break;
621 case 2: RegNo = X86::ST2; break;
622 case 3: RegNo = X86::ST3; break;
623 case 4: RegNo = X86::ST4; break;
624 case 5: RegNo = X86::ST5; break;
625 case 6: RegNo = X86::ST6; break;
626 case 7: RegNo = X86::ST7; break;
627 default: return Error(IntTok.getLoc(), "invalid stack index");
628 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000629
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000630 if (getParser().Lex().isNot(AsmToken::RParen))
631 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000632
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000633 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000634 Parser.Lex(); // Eat ')'
635 return false;
636 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000637
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000638 EndLoc = Parser.getTok().getEndLoc();
639
Chris Lattner645b2092010-06-24 07:29:18 +0000640 // If this is "db[0-7]", match it as an alias
641 // for dr[0-7].
642 if (RegNo == 0 && Tok.getString().size() == 3 &&
643 Tok.getString().startswith("db")) {
644 switch (Tok.getString()[2]) {
645 case '0': RegNo = X86::DR0; break;
646 case '1': RegNo = X86::DR1; break;
647 case '2': RegNo = X86::DR2; break;
648 case '3': RegNo = X86::DR3; break;
649 case '4': RegNo = X86::DR4; break;
650 case '5': RegNo = X86::DR5; break;
651 case '6': RegNo = X86::DR6; break;
652 case '7': RegNo = X86::DR7; break;
653 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000654
Chris Lattner645b2092010-06-24 07:29:18 +0000655 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000656 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000657 Parser.Lex(); // Eat it.
658 return false;
659 }
660 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000661
Devang Patel1aea4302012-01-20 22:32:05 +0000662 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000663 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000664 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000665 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000666 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000667
Sean Callananb9a25b72010-01-19 20:27:46 +0000668 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000669 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000670}
671
Devang Pateldd929fc2012-01-12 18:03:40 +0000672X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000673 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000674 return ParseIntelOperand();
675 return ParseATTOperand();
676}
677
Devang Pateld37ad242012-01-17 18:00:18 +0000678/// getIntelMemOperandSize - Return intel memory operand size.
679static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000680 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000681 .Cases("BYTE", "byte", 8)
682 .Cases("WORD", "word", 16)
683 .Cases("DWORD", "dword", 32)
684 .Cases("QWORD", "qword", 64)
685 .Cases("XWORD", "xword", 80)
686 .Cases("XMMWORD", "xmmword", 128)
687 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000688 .Default(0);
689 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000690}
691
Chad Rosierdd2e8952013-01-14 22:31:35 +0000692enum IntelBracExprState {
693 IBES_START,
694 IBES_LBRAC,
695 IBES_RBRAC,
696 IBES_REGISTER,
697 IBES_REGISTER_STAR,
698 IBES_REGISTER_STAR_INTEGER,
699 IBES_INTEGER,
700 IBES_INTEGER_STAR,
701 IBES_INDEX_REGISTER,
702 IBES_IDENTIFIER,
703 IBES_DISP_EXPR,
704 IBES_MINUS,
705 IBES_ERROR
706};
707
708class IntelBracExprStateMachine {
709 IntelBracExprState State;
710 unsigned BaseReg, IndexReg, Scale;
711 int64_t Disp;
712
713 unsigned TmpReg;
714 int64_t TmpInteger;
715
716 bool isPlus;
717
718public:
719 IntelBracExprStateMachine(MCAsmParser &parser) :
720 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(0),
721 TmpReg(0), TmpInteger(0), isPlus(true) {}
722
723 unsigned getBaseReg() { return BaseReg; }
724 unsigned getIndexReg() { return IndexReg; }
725 unsigned getScale() { return Scale; }
726 int64_t getDisp() { return Disp; }
727 bool isValidEndState() { return State == IBES_RBRAC; }
728
729 void onPlus() {
730 switch (State) {
731 default:
732 State = IBES_ERROR;
733 break;
734 case IBES_INTEGER:
735 State = IBES_START;
736 if (isPlus)
737 Disp += TmpInteger;
738 else
739 Disp -= TmpInteger;
740 break;
741 case IBES_REGISTER:
742 State = IBES_START;
743 // If we already have a BaseReg, then assume this is the IndexReg with a
744 // scale of 1.
745 if (!BaseReg) {
746 BaseReg = TmpReg;
747 } else {
748 assert (!IndexReg && "BaseReg/IndexReg already set!");
749 IndexReg = TmpReg;
750 Scale = 1;
751 }
752 break;
753 case IBES_INDEX_REGISTER:
754 State = IBES_START;
755 break;
756 }
757 isPlus = true;
758 }
759 void onMinus() {
760 switch (State) {
761 default:
762 State = IBES_ERROR;
763 break;
764 case IBES_START:
765 State = IBES_MINUS;
766 break;
767 case IBES_INTEGER:
768 State = IBES_START;
769 if (isPlus)
770 Disp += TmpInteger;
771 else
772 Disp -= TmpInteger;
773 break;
774 case IBES_REGISTER:
775 State = IBES_START;
776 // If we already have a BaseReg, then assume this is the IndexReg with a
777 // scale of 1.
778 if (!BaseReg) {
779 BaseReg = TmpReg;
780 } else {
781 assert (!IndexReg && "BaseReg/IndexReg already set!");
782 IndexReg = TmpReg;
783 Scale = 1;
784 }
785 break;
786 case IBES_INDEX_REGISTER:
787 State = IBES_START;
788 break;
789 }
790 isPlus = false;
791 }
792 void onRegister(unsigned Reg) {
793 switch (State) {
794 default:
795 State = IBES_ERROR;
796 break;
797 case IBES_START:
798 State = IBES_REGISTER;
799 TmpReg = Reg;
800 break;
801 case IBES_INTEGER_STAR:
802 assert (!IndexReg && "IndexReg already set!");
803 State = IBES_INDEX_REGISTER;
804 IndexReg = Reg;
805 Scale = TmpInteger;
806 break;
807 }
808 }
809 void onDispExpr() {
810 switch (State) {
811 default:
812 State = IBES_ERROR;
813 break;
814 case IBES_START:
815 State = IBES_DISP_EXPR;
816 break;
817 }
818 }
819 void onInteger(int64_t TmpInt) {
820 switch (State) {
821 default:
822 State = IBES_ERROR;
823 break;
824 case IBES_START:
825 State = IBES_INTEGER;
826 TmpInteger = TmpInt;
827 break;
828 case IBES_MINUS:
829 State = IBES_INTEGER;
830 TmpInteger = TmpInt;
831 break;
832 case IBES_REGISTER_STAR:
833 assert (!IndexReg && "IndexReg already set!");
834 State = IBES_INDEX_REGISTER;
835 IndexReg = TmpReg;
836 Scale = TmpInt;
837 break;
838 }
839 }
840 void onStar() {
841 switch (State) {
842 default:
843 State = IBES_ERROR;
844 break;
845 case IBES_INTEGER:
846 State = IBES_INTEGER_STAR;
847 break;
848 case IBES_REGISTER:
849 State = IBES_REGISTER_STAR;
850 break;
851 }
852 }
853 void onLBrac() {
854 switch (State) {
855 default:
856 State = IBES_ERROR;
857 break;
858 case IBES_RBRAC:
859 State = IBES_START;
860 isPlus = true;
861 break;
862 }
863 }
864 void onRBrac() {
865 switch (State) {
866 default:
867 State = IBES_ERROR;
868 break;
869 case IBES_DISP_EXPR:
870 State = IBES_RBRAC;
871 break;
872 case IBES_INTEGER:
873 State = IBES_RBRAC;
874 if (isPlus)
875 Disp += TmpInteger;
876 else
877 Disp -= TmpInteger;
878 break;
879 case IBES_REGISTER:
880 State = IBES_RBRAC;
881 // If we already have a BaseReg, then assume this is the IndexReg with a
882 // scale of 1.
883 if (!BaseReg) {
884 BaseReg = TmpReg;
885 } else {
886 assert (!IndexReg && "BaseReg/IndexReg already set!");
887 IndexReg = TmpReg;
888 Scale = 1;
889 }
890 break;
891 case IBES_INDEX_REGISTER:
892 State = IBES_RBRAC;
893 break;
894 }
895 }
896};
897
Chad Rosier65c88922012-10-22 19:42:52 +0000898X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000899 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000900 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000901 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000902
Devang Pateld37ad242012-01-17 18:00:18 +0000903 // Eat '['
904 if (getLexer().isNot(AsmToken::LBrac))
905 return ErrorOperand(Start, "Expected '[' token!");
906 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000907
Chad Rosierdd2e8952013-01-14 22:31:35 +0000908 unsigned TmpReg = 0;
909
910 // Try to handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000911 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000912 if (ParseRegister(TmpReg, Start, End)) {
913 const MCExpr *Disp;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000914 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000915 return 0;
916
Devang Pateld37ad242012-01-17 18:00:18 +0000917 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000918 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosier4fb25b72013-02-15 21:58:13 +0000919 // Adjust the EndLoc due to the ']'.
920 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000921 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000922 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000923 }
Devang Pateld37ad242012-01-17 18:00:18 +0000924 }
925
Chad Rosierdd2e8952013-01-14 22:31:35 +0000926 // Parse [ BaseReg + Scale*IndexReg + Disp ].
927 bool Done = false;
928 IntelBracExprStateMachine SM(Parser);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000929
Chad Rosierdd2e8952013-01-14 22:31:35 +0000930 // If we parsed a register, then the end loc has already been set and
931 // the identifier has already been lexed. We also need to update the
932 // state.
933 if (TmpReg)
934 SM.onRegister(TmpReg);
935
936 const MCExpr *Disp = 0;
937 while (!Done) {
938 bool UpdateLocLex = true;
939
940 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
941 // identifier. Don't try an parse it as a register.
942 if (Tok.getString().startswith("."))
943 break;
944
945 switch (getLexer().getKind()) {
946 default: {
947 if (SM.isValidEndState()) {
948 Done = true;
949 break;
950 }
951 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
952 }
953 case AsmToken::Identifier: {
954 // This could be a register or a displacement expression.
955 if(!ParseRegister(TmpReg, Start, End)) {
956 SM.onRegister(TmpReg);
957 UpdateLocLex = false;
958 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000959 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000960 SM.onDispExpr();
961 UpdateLocLex = false;
962 break;
963 }
964 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
965 }
966 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +0000967 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000968 SM.onInteger(Val);
969 break;
970 }
971 case AsmToken::Plus: SM.onPlus(); break;
972 case AsmToken::Minus: SM.onMinus(); break;
973 case AsmToken::Star: SM.onStar(); break;
974 case AsmToken::LBrac: SM.onLBrac(); break;
975 case AsmToken::RBrac: SM.onRBrac(); break;
976 }
977 if (!Done && UpdateLocLex) {
978 End = Tok.getLoc();
979 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +0000980 }
Devang Pateld37ad242012-01-17 18:00:18 +0000981 }
982
Chad Rosierdd2e8952013-01-14 22:31:35 +0000983 if (!Disp)
984 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +0000985
Chad Rosierddb53ef2012-10-26 22:01:25 +0000986 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000987 if (Tok.getString().startswith(".")) {
988 SmallString<64> Err;
989 const MCExpr *NewDisp;
990 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
991 return ErrorOperand(Tok.getLoc(), Err);
992
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000993 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000994 Parser.Lex(); // Eat the field.
995 Disp = NewDisp;
996 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000997
Chad Rosierdd2e8952013-01-14 22:31:35 +0000998 int BaseReg = SM.getBaseReg();
999 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001000
Chad Rosierdd2e8952013-01-14 22:31:35 +00001001 // handle [-42]
1002 if (!BaseReg && !IndexReg) {
1003 if (!SegReg)
1004 return X86Operand::CreateMem(Disp, Start, End);
1005 else
1006 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1007 }
1008
1009 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001010 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001011 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001012}
1013
1014/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001015X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001016 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001017 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001018
1019 unsigned Size = getIntelMemOperandSize(Tok.getString());
1020 if (Size) {
1021 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001022 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1023 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001024 Parser.Lex();
1025 }
1026
Chad Rosierc0a14b82012-10-24 17:22:29 +00001027 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +00001028 return ParseIntelBracExpression(SegReg, Size);
1029
1030 if (!ParseRegister(SegReg, Start, End)) {
1031 // Handel SegReg : [ ... ]
1032 if (getLexer().isNot(AsmToken::Colon))
1033 return ErrorOperand(Start, "Expected ':' token!");
1034 Parser.Lex(); // Eat :
1035 if (getLexer().isNot(AsmToken::LBrac))
1036 return ErrorOperand(Start, "Expected '[' token!");
1037 return ParseIntelBracExpression(SegReg, Size);
1038 }
Devang Pateld37ad242012-01-17 18:00:18 +00001039
1040 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001041 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001042 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001043
1044 bool NeedSizeDir = false;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001045 bool IsVarDecl = false;
1046 if (isParsingInlineAsm()) {
Chad Rosier96d58e62012-10-19 20:57:14 +00001047 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1048 const MCSymbol &Sym = SymRef->getSymbol();
1049 // FIXME: The SemaLookup will fail if the name is anything other then an
1050 // identifier.
1051 // FIXME: Pass a valid SMLoc.
Chad Rosier505bca32013-01-17 19:21:48 +00001052 unsigned tLength, tSize, tType;
1053 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
1054 tSize, tType, IsVarDecl);
Chad Rosieree29c162013-03-18 23:31:24 +00001055 if (!Size) {
Chad Rosier505bca32013-01-17 19:21:48 +00001056 Size = tType * 8; // Size is in terms of bits in this context.
Chad Rosieree29c162013-03-18 23:31:24 +00001057 NeedSizeDir = Size > 0;
1058 }
Chad Rosier96d58e62012-10-19 20:57:14 +00001059 }
1060 }
Chad Rosier2a784132012-10-23 23:31:33 +00001061 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001062 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001063 else {
1064 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1065 // reference. We need an 'r' constraint here, so we need to create register
1066 // operand to ensure proper matching. Just pick a GPR based on the size of
1067 // a pointer.
1068 if (!IsVarDecl) {
1069 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1070 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
1071 }
1072
Chad Rosierd4d96ac2012-10-23 23:34:28 +00001073 // When parsing inline assembly we set the base register to a non-zero value
1074 // as we don't know the actual value at this time. This is necessary to
1075 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +00001076 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001077 /*Scale*/1, Start, End, Size, NeedSizeDir);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001078 }
Chad Rosierc0a14b82012-10-24 17:22:29 +00001079}
1080
Chad Rosier22f441a2012-10-24 22:21:50 +00001081/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001082bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1083 const MCExpr **NewDisp,
1084 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001085 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001086 uint64_t OrigDispVal, DotDispVal;
1087
1088 // FIXME: Handle non-constant expressions.
1089 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1090 OrigDispVal = OrigDisp->getValue();
1091 } else {
1092 Err = "Non-constant offsets are not supported!";
1093 return true;
1094 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001095
1096 // Drop the '.'.
1097 StringRef DotDispStr = Tok.getString().drop_front(1);
1098
Chad Rosier22f441a2012-10-24 22:21:50 +00001099 // .Imm gets lexed as a real.
1100 if (Tok.is(AsmToken::Real)) {
1101 APInt DotDisp;
1102 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001103 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001104 } else if (Tok.is(AsmToken::Identifier)) {
1105 // We should only see an identifier when parsing the original inline asm.
1106 // The front-end should rewrite this in terms of immediates.
1107 assert (isParsingInlineAsm() && "Unexpected field name!");
1108
1109 unsigned DotDisp;
1110 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1111 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1112 DotDisp)) {
1113 Err = "Unable to lookup field reference!";
1114 return true;
1115 }
1116 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001117 } else {
1118 Err = "Unexpected token type!";
1119 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001120 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001121
Chad Rosierec130222012-10-25 21:51:10 +00001122 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1123 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1124 unsigned Len = DotDispStr.size();
1125 unsigned Val = OrigDispVal + DotDispVal;
1126 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1127 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001128 }
1129
1130 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1131 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001132}
1133
Chad Rosierc0a14b82012-10-24 17:22:29 +00001134/// Parse the 'offset' operator. This operator is used to specify the
1135/// location rather then the content of a variable.
1136X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1137 SMLoc OffsetOfLoc = Start;
1138 Parser.Lex(); // Eat offset.
1139 Start = Parser.getTok().getLoc();
1140 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1141
Chad Rosier6e431572012-10-26 16:09:20 +00001142 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001143 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001144 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001145 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001146
Chad Rosier6e431572012-10-26 16:09:20 +00001147 // Don't emit the offset operator.
1148 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1149
Chad Rosierc0a14b82012-10-24 17:22:29 +00001150 // The offset operator will have an 'r' constraint, thus we need to create
1151 // register operand to ensure proper matching. Just pick a GPR based on
1152 // the size of a pointer.
1153 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001154 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1155 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001156}
1157
Chad Rosier505bca32013-01-17 19:21:48 +00001158enum IntelOperatorKind {
1159 IOK_LENGTH,
1160 IOK_SIZE,
1161 IOK_TYPE
1162};
1163
1164/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1165/// returns the number of elements in an array. It returns the value 1 for
1166/// non-array variables. The SIZE operator returns the size of a C or C++
1167/// variable. A variable's size is the product of its LENGTH and TYPE. The
1168/// TYPE operator returns the size of a C or C++ type or variable. If the
1169/// variable is an array, TYPE returns the size of a single element.
1170X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001171 SMLoc TypeLoc = Start;
1172 Parser.Lex(); // Eat offset.
1173 Start = Parser.getTok().getLoc();
1174 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1175
1176 SMLoc End;
1177 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001178 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001179 return 0;
1180
Chad Rosier505bca32013-01-17 19:21:48 +00001181 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001182 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1183 const MCSymbol &Sym = SymRef->getSymbol();
1184 // FIXME: The SemaLookup will fail if the name is anything other then an
1185 // identifier.
1186 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001187 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001188 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1189 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001190 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001191 }
1192 unsigned CVal;
1193 switch(OpKind) {
1194 default: llvm_unreachable("Unexpected operand kind!");
1195 case IOK_LENGTH: CVal = Length; break;
1196 case IOK_SIZE: CVal = Size; break;
1197 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001198 }
1199
1200 // Rewrite the type operator and the C or C++ type or variable in terms of an
1201 // immediate. E.g. TYPE foo -> $$4
1202 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001203 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001204
Chad Rosier505bca32013-01-17 19:21:48 +00001205 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierefcb3d92012-10-26 18:04:20 +00001206 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
1207}
1208
Devang Pateld37ad242012-01-17 18:00:18 +00001209X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001210 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001211 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001212
Chad Rosier505bca32013-01-17 19:21:48 +00001213 // Offset, length, type and size operators.
1214 if (isParsingInlineAsm()) {
1215 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1216 return ParseIntelOffsetOfOperator(Start);
1217 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1218 return ParseIntelOperator(Start, IOK_LENGTH);
1219 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1220 return ParseIntelOperator(Start, IOK_SIZE);
1221 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1222 return ParseIntelOperator(Start, IOK_TYPE);
1223 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001224
Chad Rosier505bca32013-01-17 19:21:48 +00001225 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001226 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1227 getLexer().is(AsmToken::Minus)) {
1228 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001229 if (!getParser().parseExpression(Val, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +00001230 return X86Operand::CreateImm(Val, Start, End);
1231 }
1232 }
1233
Chad Rosier505bca32013-01-17 19:21:48 +00001234 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001235 unsigned RegNo = 0;
1236 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001237 // If this is a segment register followed by a ':', then this is the start
1238 // of a memory reference, otherwise this is a normal register reference.
1239 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001240 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001241
1242 getParser().Lex(); // Eat the colon.
1243 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001244 }
1245
Chad Rosier505bca32013-01-17 19:21:48 +00001246 // Memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001247 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001248}
1249
Devang Pateldd929fc2012-01-12 18:03:40 +00001250X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001251 switch (getLexer().getKind()) {
1252 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001253 // Parse a memory operand with no segment register.
1254 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001255 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001256 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001257 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001258 SMLoc Start, End;
1259 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001260 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001261 Error(Start, "%eiz and %riz can only be used as index registers",
1262 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001263 return 0;
1264 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001265
Chris Lattnereef6d782010-04-17 18:56:34 +00001266 // If this is a segment register followed by a ':', then this is the start
1267 // of a memory reference, otherwise this is a normal register reference.
1268 if (getLexer().isNot(AsmToken::Colon))
1269 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001270
1271
Chris Lattnereef6d782010-04-17 18:56:34 +00001272 getParser().Lex(); // Eat the colon.
1273 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001274 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001275 case AsmToken::Dollar: {
1276 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001277 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001278 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001279 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001280 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001281 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001282 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001283 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001284 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001285}
1286
Chris Lattnereef6d782010-04-17 18:56:34 +00001287/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1288/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001289X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001290
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001291 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1292 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001293 // only way to do this without lookahead is to eat the '(' and see what is
1294 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001295 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001296 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001297 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001298 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001299
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001300 // After parsing the base expression we could either have a parenthesized
1301 // memory address or not. If not, return now. If so, eat the (.
1302 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001303 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001304 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001305 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001306 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001307 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001308
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001309 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001310 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001311 } else {
1312 // Okay, we have a '('. We don't know if this is an expression or not, but
1313 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001314 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001315 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001316
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001317 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001318 // Nothing to do here, fall into the code below with the '(' part of the
1319 // memory operand consumed.
1320 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001321 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001322
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001323 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001324 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001325 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001326
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001327 // After parsing the base expression we could either have a parenthesized
1328 // memory address or not. If not, return now. If so, eat the (.
1329 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001330 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001331 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001332 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001333 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001334 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001335
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001336 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001337 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001338 }
1339 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001340
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001341 // If we reached here, then we just ate the ( of the memory operand. Process
1342 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001343 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001344 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001345
Chris Lattner29ef9a22010-01-15 18:51:29 +00001346 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001347 SMLoc StartLoc, EndLoc;
1348 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001349 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001350 Error(StartLoc, "eiz and riz can only be used as index registers",
1351 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001352 return 0;
1353 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001354 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001355
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001356 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001357 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001358 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001359
1360 // Following the comma we should have either an index register, or a scale
1361 // value. We don't support the later form, but we want to parse it
1362 // correctly.
1363 //
1364 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001365 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001366 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001367 SMLoc L;
1368 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001369
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001370 if (getLexer().isNot(AsmToken::RParen)) {
1371 // Parse the scale amount:
1372 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001373 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001374 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001375 "expected comma in scale expression");
1376 return 0;
1377 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001378 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001379
1380 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001381 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001382
1383 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001384 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001385 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001386 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001387 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001388
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001389 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001390 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1391 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1392 return 0;
1393 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001394 Scale = (unsigned)ScaleVal;
1395 }
1396 }
1397 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001398 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001399 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001400 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001401
1402 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001403 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001404 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001405
Daniel Dunbaree910252010-08-24 19:13:38 +00001406 if (Value != 1)
1407 Warning(Loc, "scale factor without index register is ignored");
1408 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001409 }
1410 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001411
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001412 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001413 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001414 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001415 return 0;
1416 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001417 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001418 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001419
Kevin Enderby84faf652012-03-12 21:32:09 +00001420 // If we have both a base register and an index register make sure they are
1421 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001422 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001423 if (BaseReg != 0 && IndexReg != 0) {
1424 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001425 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1426 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001427 IndexReg != X86::RIZ) {
1428 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1429 return 0;
1430 }
1431 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001432 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1433 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001434 IndexReg != X86::EIZ){
1435 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1436 return 0;
1437 }
1438 }
1439
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001440 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1441 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001442}
1443
Devang Pateldd929fc2012-01-12 18:03:40 +00001444bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001445ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001446 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001447 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001448 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001449
Chris Lattnerd8f71792010-11-28 20:23:50 +00001450 // FIXME: Hack to recognize setneb as setne.
1451 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1452 PatchedName != "setb" && PatchedName != "setnb")
1453 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001454
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001455 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1456 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001457 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001458 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1459 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001460 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001461 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001462 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001463 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001464 .Case("eq", 0x00)
1465 .Case("lt", 0x01)
1466 .Case("le", 0x02)
1467 .Case("unord", 0x03)
1468 .Case("neq", 0x04)
1469 .Case("nlt", 0x05)
1470 .Case("nle", 0x06)
1471 .Case("ord", 0x07)
1472 /* AVX only from here */
1473 .Case("eq_uq", 0x08)
1474 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001475 .Case("ngt", 0x0A)
1476 .Case("false", 0x0B)
1477 .Case("neq_oq", 0x0C)
1478 .Case("ge", 0x0D)
1479 .Case("gt", 0x0E)
1480 .Case("true", 0x0F)
1481 .Case("eq_os", 0x10)
1482 .Case("lt_oq", 0x11)
1483 .Case("le_oq", 0x12)
1484 .Case("unord_s", 0x13)
1485 .Case("neq_us", 0x14)
1486 .Case("nlt_uq", 0x15)
1487 .Case("nle_uq", 0x16)
1488 .Case("ord_s", 0x17)
1489 .Case("eq_us", 0x18)
1490 .Case("nge_uq", 0x19)
1491 .Case("ngt_uq", 0x1A)
1492 .Case("false_os", 0x1B)
1493 .Case("neq_os", 0x1C)
1494 .Case("ge_oq", 0x1D)
1495 .Case("gt_oq", 0x1E)
1496 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001497 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001498 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001499 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1500 getParser().getContext());
1501 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001502 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001503 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001504 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001505 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001506 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001507 } else {
1508 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001509 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001510 }
1511 }
1512 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001513
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001514 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001515
Devang Patel885f65b2012-01-30 22:47:12 +00001516 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001517 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001518
Chris Lattner2544f422010-09-08 05:17:37 +00001519 // Determine whether this is an instruction prefix.
1520 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001521 Name == "lock" || Name == "rep" ||
1522 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001523 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001524 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001525
1526
Chris Lattner2544f422010-09-08 05:17:37 +00001527 // This does the actual operand parsing. Don't parse any more if we have a
1528 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1529 // just want to parse the "lock" as the first instruction and the "incl" as
1530 // the next one.
1531 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001532
1533 // Parse '*' modifier.
1534 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001535 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001536 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001537 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001538 }
1539
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001540 // Read the first 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 Dunbar39e2dd72010-05-25 19:49:32 +00001547
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001548 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001549 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001550
1551 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001552 if (X86Operand *Op = ParseOperand())
1553 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001554 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001555 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001556 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001557 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001558 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001559
Chris Lattnercbf8a982010-09-11 16:18:25 +00001560 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001561 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001562 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001563 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001564 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001565 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001566
Chris Lattner2544f422010-09-08 05:17:37 +00001567 if (getLexer().is(AsmToken::EndOfStatement))
1568 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001569 else if (isPrefix && getLexer().is(AsmToken::Slash))
1570 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001571
Devang Patel885f65b2012-01-30 22:47:12 +00001572 if (ExtraImmOp && isParsingIntelSyntax())
1573 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1574
Chris Lattner98c870f2010-11-06 19:25:43 +00001575 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1576 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1577 // documented form in various unofficial manuals, so a lot of code uses it.
1578 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1579 Operands.size() == 3) {
1580 X86Operand &Op = *(X86Operand*)Operands.back();
1581 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1582 isa<MCConstantExpr>(Op.Mem.Disp) &&
1583 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1584 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1585 SMLoc Loc = Op.getEndLoc();
1586 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1587 delete &Op;
1588 }
1589 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001590 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1591 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1592 Operands.size() == 3) {
1593 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1594 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1595 isa<MCConstantExpr>(Op.Mem.Disp) &&
1596 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1597 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1598 SMLoc Loc = Op.getEndLoc();
1599 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1600 delete &Op;
1601 }
1602 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001603 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1604 if (Name.startswith("ins") && Operands.size() == 3 &&
1605 (Name == "insb" || Name == "insw" || Name == "insl")) {
1606 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1607 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1608 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1609 Operands.pop_back();
1610 Operands.pop_back();
1611 delete &Op;
1612 delete &Op2;
1613 }
1614 }
1615
1616 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1617 if (Name.startswith("outs") && Operands.size() == 3 &&
1618 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1619 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1620 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1621 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1622 Operands.pop_back();
1623 Operands.pop_back();
1624 delete &Op;
1625 delete &Op2;
1626 }
1627 }
1628
1629 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1630 if (Name.startswith("movs") && Operands.size() == 3 &&
1631 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001632 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001633 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1634 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1635 if (isSrcOp(Op) && isDstOp(Op2)) {
1636 Operands.pop_back();
1637 Operands.pop_back();
1638 delete &Op;
1639 delete &Op2;
1640 }
1641 }
1642 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1643 if (Name.startswith("lods") && Operands.size() == 3 &&
1644 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001645 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001646 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1647 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1648 if (isSrcOp(*Op1) && Op2->isReg()) {
1649 const char *ins;
1650 unsigned reg = Op2->getReg();
1651 bool isLods = Name == "lods";
1652 if (reg == X86::AL && (isLods || Name == "lodsb"))
1653 ins = "lodsb";
1654 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1655 ins = "lodsw";
1656 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1657 ins = "lodsl";
1658 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1659 ins = "lodsq";
1660 else
1661 ins = NULL;
1662 if (ins != NULL) {
1663 Operands.pop_back();
1664 Operands.pop_back();
1665 delete Op1;
1666 delete Op2;
1667 if (Name != ins)
1668 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1669 }
1670 }
1671 }
1672 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1673 if (Name.startswith("stos") && Operands.size() == 3 &&
1674 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001675 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001676 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1677 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1678 if (isDstOp(*Op2) && Op1->isReg()) {
1679 const char *ins;
1680 unsigned reg = Op1->getReg();
1681 bool isStos = Name == "stos";
1682 if (reg == X86::AL && (isStos || Name == "stosb"))
1683 ins = "stosb";
1684 else if (reg == X86::AX && (isStos || Name == "stosw"))
1685 ins = "stosw";
1686 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1687 ins = "stosl";
1688 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1689 ins = "stosq";
1690 else
1691 ins = NULL;
1692 if (ins != NULL) {
1693 Operands.pop_back();
1694 Operands.pop_back();
1695 delete Op1;
1696 delete Op2;
1697 if (Name != ins)
1698 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1699 }
1700 }
1701 }
1702
Chris Lattnere9e16a32010-09-15 04:33:27 +00001703 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001704 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001705 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001706 Name.startswith("shl") || Name.startswith("sal") ||
1707 Name.startswith("rcl") || Name.startswith("rcr") ||
1708 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001709 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001710 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001711 // Intel syntax
1712 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1713 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001714 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1715 delete Operands[2];
1716 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001717 }
1718 } else {
1719 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1720 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001721 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1722 delete Operands[1];
1723 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001724 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001725 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001726 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001727
Chris Lattner15f89512011-04-09 19:41:05 +00001728 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1729 // instalias with an immediate operand yet.
1730 if (Name == "int" && Operands.size() == 2) {
1731 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1732 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1733 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1734 delete Operands[1];
1735 Operands.erase(Operands.begin() + 1);
1736 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1737 }
1738 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001739
Chris Lattner98986712010-01-14 22:21:20 +00001740 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001741}
1742
Craig Topper4bef9612013-03-18 02:53:34 +00001743static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1744 bool isCmp) {
1745 MCInst TmpInst;
1746 TmpInst.setOpcode(Opcode);
1747 if (!isCmp)
1748 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1749 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1750 TmpInst.addOperand(Inst.getOperand(0));
1751 Inst = TmpInst;
1752 return true;
1753}
1754
1755static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1756 bool isCmp = false) {
1757 if (!Inst.getOperand(0).isImm() ||
1758 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1759 return false;
1760
1761 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1762}
1763
1764static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1765 bool isCmp = false) {
1766 if (!Inst.getOperand(0).isImm() ||
1767 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1768 return false;
1769
1770 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1771}
1772
1773static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1774 bool isCmp = false) {
1775 if (!Inst.getOperand(0).isImm() ||
1776 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1777 return false;
1778
1779 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1780}
1781
Devang Pateldd929fc2012-01-12 18:03:40 +00001782bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001783processInstruction(MCInst &Inst,
1784 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1785 switch (Inst.getOpcode()) {
1786 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001787 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1788 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1789 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1790 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1791 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1792 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1793 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1794 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1795 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1796 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1797 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1798 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1799 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1800 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1801 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1802 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1803 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1804 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001805 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1806 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1807 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1808 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1809 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1810 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001811 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001812}
1813
Jim Grosbach3ca63822012-11-14 18:04:47 +00001814static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001815bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001816MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001817 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001818 MCStreamer &Out, unsigned &ErrorInfo,
1819 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001820 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001821 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1822 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001823 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001824
Chris Lattner7c51a312010-09-29 01:50:45 +00001825 // First, handle aliases that expand to multiple instructions.
1826 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001827 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001828 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001829 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001830 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001831 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001832 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001833 MCInst Inst;
1834 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001835 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001836 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001837 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001838
Chris Lattner0bb83a82010-09-30 16:39:29 +00001839 const char *Repl =
1840 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001841 .Case("finit", "fninit")
1842 .Case("fsave", "fnsave")
1843 .Case("fstcw", "fnstcw")
1844 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001845 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001846 .Case("fstsw", "fnstsw")
1847 .Case("fstsww", "fnstsw")
1848 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001849 .Default(0);
1850 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001851 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001852 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001853 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001854
Chris Lattnera008e8a2010-09-06 21:54:15 +00001855 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001856 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001857
Daniel Dunbarc918d602010-05-04 16:12:42 +00001858 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001859 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001860 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001861 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001862 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001863 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001864 // Some instructions need post-processing to, for example, tweak which
1865 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001866 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001867 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001868 while (processInstruction(Inst, Operands))
1869 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001870
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001871 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001872 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001873 Out.EmitInstruction(Inst);
1874 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001875 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001876 case Match_MissingFeature: {
1877 assert(ErrorInfo && "Unknown missing feature!");
1878 // Special case the error message for the very common case where only
1879 // a single subtarget feature is missing.
1880 std::string Msg = "instruction requires:";
1881 unsigned Mask = 1;
1882 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1883 if (ErrorInfo & Mask) {
1884 Msg += " ";
1885 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1886 }
1887 Mask <<= 1;
1888 }
1889 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1890 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001891 case Match_InvalidOperand:
1892 WasOriginallyInvalidOperand = true;
1893 break;
1894 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001895 break;
1896 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001897
Daniel Dunbarc918d602010-05-04 16:12:42 +00001898 // FIXME: Ideally, we would only attempt suffix matches for things which are
1899 // valid prefixes, and we could just infer the right unambiguous
1900 // type. However, that requires substantially more matcher support than the
1901 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001902
Daniel Dunbarc918d602010-05-04 16:12:42 +00001903 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001904 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001905 SmallString<16> Tmp;
1906 Tmp += Base;
1907 Tmp += ' ';
1908 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001909
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001910 // If this instruction starts with an 'f', then it is a floating point stack
1911 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1912 // 80-bit floating point, which use the suffixes s,l,t respectively.
1913 //
1914 // Otherwise, we assume that this may be an integer instruction, which comes
1915 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1916 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001917
Daniel Dunbarc918d602010-05-04 16:12:42 +00001918 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001919 Tmp[Base.size()] = Suffixes[0];
1920 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00001921 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001922 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001923
Chad Rosier6e006d32012-10-12 22:53:36 +00001924 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1925 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001926 // If this returned as a missing feature failure, remember that.
1927 if (Match1 == Match_MissingFeature)
1928 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001929 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001930 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1931 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001932 // If this returned as a missing feature failure, remember that.
1933 if (Match2 == Match_MissingFeature)
1934 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001935 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001936 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1937 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001938 // If this returned as a missing feature failure, remember that.
1939 if (Match3 == Match_MissingFeature)
1940 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001941 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001942 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1943 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001944 // If this returned as a missing feature failure, remember that.
1945 if (Match4 == Match_MissingFeature)
1946 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001947
1948 // Restore the old token.
1949 Op->setTokenValue(Base);
1950
1951 // If exactly one matched, then we treat that as a successful match (and the
1952 // instruction will already have been filled in correctly, since the failing
1953 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001954 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001955 (Match1 == Match_Success) + (Match2 == Match_Success) +
1956 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001957 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001958 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001959 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001960 Out.EmitInstruction(Inst);
1961 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001962 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001963 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001964
Chris Lattnerec6789f2010-09-06 20:08:02 +00001965 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001966
Daniel Dunbar09062b12010-08-12 00:55:42 +00001967 // If we had multiple suffix matches, then identify this as an ambiguous
1968 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001969 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001970 char MatchChars[4];
1971 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001972 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1973 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1974 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1975 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001976
1977 SmallString<126> Msg;
1978 raw_svector_ostream OS(Msg);
1979 OS << "ambiguous instructions require an explicit suffix (could be ";
1980 for (unsigned i = 0; i != NumMatches; ++i) {
1981 if (i != 0)
1982 OS << ", ";
1983 if (i + 1 == NumMatches)
1984 OS << "or ";
1985 OS << "'" << Base << MatchChars[i] << "'";
1986 }
1987 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001988 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001989 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001990 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001991
Chris Lattnera008e8a2010-09-06 21:54:15 +00001992 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001993
Chris Lattnera008e8a2010-09-06 21:54:15 +00001994 // If all of the instructions reported an invalid mnemonic, then the original
1995 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001996 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1997 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001998 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001999 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002000 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002001 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002002 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002003 }
2004
2005 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002006 if (ErrorInfo != ~0U) {
2007 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002008 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002009 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002010
Chad Rosier84125ca2012-10-13 00:26:04 +00002011 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002012 if (Operand->getStartLoc().isValid()) {
2013 SMRange OperandRange = Operand->getLocRange();
2014 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002015 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002016 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002017 }
2018
Chad Rosierb4fdade2012-08-21 19:36:59 +00002019 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002020 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002021 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002022
Chris Lattnerec6789f2010-09-06 20:08:02 +00002023 // If one instruction matched with a missing feature, report this as a
2024 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002025 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2026 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002027 std::string Msg = "instruction requires:";
2028 unsigned Mask = 1;
2029 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2030 if (ErrorInfoMissingFeature & Mask) {
2031 Msg += " ";
2032 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2033 }
2034 Mask <<= 1;
2035 }
2036 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002037 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002038
Chris Lattnera008e8a2010-09-06 21:54:15 +00002039 // If one instruction matched with an invalid operand, report this as an
2040 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002041 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2042 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002043 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002044 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002045 return true;
2046 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002047
Chris Lattnerec6789f2010-09-06 20:08:02 +00002048 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002049 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002050 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002051 return true;
2052}
2053
2054
Devang Pateldd929fc2012-01-12 18:03:40 +00002055bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002056 StringRef IDVal = DirectiveID.getIdentifier();
2057 if (IDVal == ".word")
2058 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002059 else if (IDVal.startswith(".code"))
2060 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002061 else if (IDVal.startswith(".att_syntax")) {
2062 getParser().setAssemblerDialect(0);
2063 return false;
2064 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002065 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002066 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2067 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002068 // FIXME : Handle noprefix
2069 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002070 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002071 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002072 }
2073 return false;
2074 }
Chris Lattner537ca842010-10-30 17:38:55 +00002075 return true;
2076}
2077
2078/// ParseDirectiveWord
2079/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002080bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002081 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2082 for (;;) {
2083 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002084 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002085 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002086
Eric Christopher1ced2082013-01-09 03:52:05 +00002087 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002088
Chris Lattner537ca842010-10-30 17:38:55 +00002089 if (getLexer().is(AsmToken::EndOfStatement))
2090 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002091
Chris Lattner537ca842010-10-30 17:38:55 +00002092 // FIXME: Improve diagnostic.
2093 if (getLexer().isNot(AsmToken::Comma))
2094 return Error(L, "unexpected token in directive");
2095 Parser.Lex();
2096 }
2097 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002098
Chris Lattner537ca842010-10-30 17:38:55 +00002099 Parser.Lex();
2100 return false;
2101}
2102
Evan Chengbd27f5a2011-07-27 00:38:12 +00002103/// ParseDirectiveCode
2104/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002105bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002106 if (IDVal == ".code32") {
2107 Parser.Lex();
2108 if (is64BitMode()) {
2109 SwitchMode();
2110 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2111 }
2112 } else if (IDVal == ".code64") {
2113 Parser.Lex();
2114 if (!is64BitMode()) {
2115 SwitchMode();
2116 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2117 }
2118 } else {
2119 return Error(L, "unexpected directive " + IDVal);
2120 }
Chris Lattner537ca842010-10-30 17:38:55 +00002121
Evan Chengbd27f5a2011-07-27 00:38:12 +00002122 return false;
2123}
Chris Lattner537ca842010-10-30 17:38:55 +00002124
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002125// Force static initialization.
2126extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002127 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2128 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002129}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002130
Chris Lattner0692ee62010-09-06 19:11:01 +00002131#define GET_REGISTER_MATCHER
2132#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002133#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002134#include "X86GenAsmMatcher.inc"