blob: 8317a87baa0ae2b4025453d983aa16111154ff48 [file] [log] [blame]
Daniel Dunbar71475772009-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 Cheng11424442011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Chad Rosier6844ea02012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Chris Lattner1261b812010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner1261b812010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chad Rosier8a244662013-04-02 20:02:33 +000016#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCParser/MCAsmLexer.h"
20#include "llvm/MC/MCParser/MCAsmParser.h"
21#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCStreamer.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000027#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000028#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Cheng4d1ca962011-07-08 01:53:10 +000030
Daniel Dunbar71475772009-07-17 20:42:00 +000031using namespace llvm;
32
33namespace {
Benjamin Kramerb60210e2009-07-31 11:35:26 +000034struct X86Operand;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000035
Devang Patel4a6e7782012-01-12 18:03:40 +000036class X86AsmParser : public MCTargetAsmParser {
Evan Cheng91111d22011-07-09 05:47:46 +000037 MCSubtargetInfo &STI;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000038 MCAsmParser &Parser;
Chad Rosierf0e87202012-10-25 20:41:34 +000039 ParseInstructionInfo *InstInfo;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000040private:
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000041 MCAsmParser &getParser() const { return Parser; }
42
43 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
44
Chris Lattnera3a06812011-10-16 04:47:35 +000045 bool Error(SMLoc L, const Twine &Msg,
Chad Rosier3d4bc622012-08-21 19:36:59 +000046 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier4453e842012-10-12 23:09:25 +000047 bool MatchingInlineAsm = false) {
48 if (MatchingInlineAsm) return true;
Chris Lattnera3a06812011-10-16 04:47:35 +000049 return Parser.Error(L, Msg, Ranges);
50 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +000051
Devang Patel41b9dde2012-01-17 18:00:18 +000052 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
53 Error(Loc, Msg);
54 return 0;
55 }
56
Chris Lattnera2bbb7c2010-01-15 18:44:13 +000057 X86Operand *ParseOperand();
Devang Patel46831de2012-01-12 01:36:43 +000058 X86Operand *ParseATTOperand();
59 X86Operand *ParseIntelOperand();
Chad Rosier10d1d1c2013-04-09 20:44:09 +000060 X86Operand *ParseIntelOffsetOfOperator();
61 X86Operand *ParseIntelOperator(unsigned OpKind);
Chad Rosier1530ba52013-03-27 21:49:56 +000062 X86Operand *ParseIntelMemOperand(unsigned SegReg, uint64_t ImmDisp,
63 SMLoc StartLoc);
Chad Rosierfce4fab2013-04-08 17:43:47 +000064 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc SizeDirLoc,
65 uint64_t ImmDisp, unsigned Size);
Chad Rosier8a244662013-04-02 20:02:33 +000066 X86Operand *ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +000067 StringRef &Identifier);
Chris Lattnerb9270732010-04-17 18:56:34 +000068 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000069
Chad Rosier175d0ae2013-04-12 18:21:18 +000070 X86Operand *CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
71 unsigned BaseReg, unsigned IndexReg,
72 unsigned Scale, SMLoc Start, SMLoc End,
Chad Rosiere81309b2013-04-09 17:53:49 +000073 SMLoc SizeDirLoc, unsigned Size,
74 StringRef SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +000075
Chad Rosier911c1f32012-10-25 17:37:43 +000076 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
77 SmallString<64> &Err);
Chad Rosier5dcb4662012-10-24 22:21:50 +000078
Kevin Enderbyce4bec82009-09-10 20:51:44 +000079 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +000080 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000081
Devang Patelde47cce2012-01-18 22:42:29 +000082 bool processInstruction(MCInst &Inst,
83 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
84
Chad Rosier49963552012-10-13 00:26:04 +000085 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +000086 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +000087 MCStreamer &Out, unsigned &ErrorInfo,
88 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +000089
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000090 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +000091 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000092 bool isSrcOp(X86Operand &Op);
93
Kevin Enderby1ef22f32012-03-13 19:47:55 +000094 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
95 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000096 bool isDstOp(X86Operand &Op);
97
Evan Chengc5e6d2f2011-07-11 03:57:24 +000098 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +000099 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +0000100 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +0000101 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000102 void SwitchMode() {
103 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
104 setAvailableFeatures(FB);
105 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000106
Daniel Dunbareefe8612010-07-19 05:44:09 +0000107 /// @name Auto-generated Matcher Functions
108 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000109
Chris Lattner3e4582a2010-09-06 19:11:01 +0000110#define GET_ASSEMBLER_HEADER
111#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000112
Daniel Dunbar00331992009-07-29 00:02:19 +0000113 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000114
115public:
Devang Patel4a6e7782012-01-12 18:03:40 +0000116 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosierf0e87202012-10-25 20:41:34 +0000117 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000118
Daniel Dunbareefe8612010-07-19 05:44:09 +0000119 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000120 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000121 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000122 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000123
Chad Rosierf0e87202012-10-25 20:41:34 +0000124 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
125 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000126 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000127
128 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000129
130 bool isParsingIntelSyntax() {
Devang Patela173ee52012-01-31 18:14:05 +0000131 return getParser().getAssemblerDialect();
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000132 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000133};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000134} // end anonymous namespace
135
Sean Callanan86c11812010-01-23 00:40:33 +0000136/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000137/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000138
Chris Lattner60db0a62010-02-09 00:34:28 +0000139static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000140
141/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000142
Craig Topper6bf3ed42012-07-18 04:59:16 +0000143static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000144 return (( Value <= 0x000000000000007FULL)||
145 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
146 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
147}
148
149static bool isImmSExti32i8Value(uint64_t Value) {
150 return (( Value <= 0x000000000000007FULL)||
151 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
152 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
153}
154
155static bool isImmZExtu32u8Value(uint64_t Value) {
156 return (Value <= 0x00000000000000FFULL);
157}
158
159static bool isImmSExti64i8Value(uint64_t Value) {
160 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000161 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000162}
163
164static bool isImmSExti64i32Value(uint64_t Value) {
165 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000166 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000167}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000168namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000169
170/// X86Operand - Instances of this class represent a parsed X86 machine
171/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000172struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000173 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000174 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000175 Register,
176 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000177 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000178 } Kind;
179
Chris Lattner0c2538f2010-01-15 18:51:29 +0000180 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000181 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000182 StringRef SymName;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000183 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000184
Eric Christopher8996c5d2013-03-15 00:42:55 +0000185 struct TokOp {
186 const char *Data;
187 unsigned Length;
188 };
189
190 struct RegOp {
191 unsigned RegNo;
192 };
193
194 struct ImmOp {
195 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000196 };
197
198 struct MemOp {
199 unsigned SegReg;
200 const MCExpr *Disp;
201 unsigned BaseReg;
202 unsigned IndexReg;
203 unsigned Scale;
204 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000205 };
206
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000207 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000208 struct TokOp Tok;
209 struct RegOp Reg;
210 struct ImmOp Imm;
211 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000212 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000213
Chris Lattner015cfb12010-01-15 19:33:43 +0000214 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000215 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000216
Chad Rosiere81309b2013-04-09 17:53:49 +0000217 StringRef getSymName() { return SymName; }
218
Chris Lattner86e61532010-01-15 19:06:59 +0000219 /// getStartLoc - Get the location of the first token of this operand.
220 SMLoc getStartLoc() const { return StartLoc; }
221 /// getEndLoc - Get the location of the last token of this operand.
222 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000223 /// getLocRange - Get the range between the first and last token of this
224 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000225 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000226 /// getOffsetOfLoc - Get the location of the offset operator.
227 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000228
Jim Grosbach602aa902011-07-13 15:34:57 +0000229 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000230
Daniel Dunbare10787e2009-08-07 08:26:05 +0000231 StringRef getToken() const {
232 assert(Kind == Token && "Invalid access!");
233 return StringRef(Tok.Data, Tok.Length);
234 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000235 void setTokenValue(StringRef Value) {
236 assert(Kind == Token && "Invalid access!");
237 Tok.Data = Value.data();
238 Tok.Length = Value.size();
239 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000240
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000241 unsigned getReg() const {
242 assert(Kind == Register && "Invalid access!");
243 return Reg.RegNo;
244 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000245
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000246 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000247 assert(Kind == Immediate && "Invalid access!");
248 return Imm.Val;
249 }
250
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000251 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000252 assert(Kind == Memory && "Invalid access!");
253 return Mem.Disp;
254 }
255 unsigned getMemSegReg() const {
256 assert(Kind == Memory && "Invalid access!");
257 return Mem.SegReg;
258 }
259 unsigned getMemBaseReg() const {
260 assert(Kind == Memory && "Invalid access!");
261 return Mem.BaseReg;
262 }
263 unsigned getMemIndexReg() const {
264 assert(Kind == Memory && "Invalid access!");
265 return Mem.IndexReg;
266 }
267 unsigned getMemScale() const {
268 assert(Kind == Memory && "Invalid access!");
269 return Mem.Scale;
270 }
271
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000272 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000273
274 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000275
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000276 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000277 if (!isImm())
278 return false;
279
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000280 // If this isn't a constant expr, just assume it fits and let relaxation
281 // handle it.
282 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
283 if (!CE)
284 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000285
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000286 // Otherwise, check the value is in a range that makes sense for this
287 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000288 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000289 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000290 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000291 if (!isImm())
292 return false;
293
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000294 // If this isn't a constant expr, just assume it fits and let relaxation
295 // handle it.
296 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
297 if (!CE)
298 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000299
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000300 // Otherwise, check the value is in a range that makes sense for this
301 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000302 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000303 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000304 bool isImmZExtu32u8() const {
305 if (!isImm())
306 return false;
307
308 // If this isn't a constant expr, just assume it fits and let relaxation
309 // handle it.
310 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
311 if (!CE)
312 return true;
313
314 // Otherwise, check the value is in a range that makes sense for this
315 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000316 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000317 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000318 bool isImmSExti64i8() const {
319 if (!isImm())
320 return false;
321
322 // If this isn't a constant expr, just assume it fits and let relaxation
323 // handle it.
324 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
325 if (!CE)
326 return true;
327
328 // Otherwise, check the value is in a range that makes sense for this
329 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000330 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000331 }
332 bool isImmSExti64i32() const {
333 if (!isImm())
334 return false;
335
336 // If this isn't a constant expr, just assume it fits and let relaxation
337 // handle it.
338 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
339 if (!CE)
340 return true;
341
342 // Otherwise, check the value is in a range that makes sense for this
343 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000344 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000345 }
346
Chad Rosier5bca3f92012-10-22 19:50:35 +0000347 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000348 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000349 }
350
Chad Rosiera4bc9432013-01-10 22:10:27 +0000351 bool needAddressOf() const {
352 return AddressOf;
353 }
354
Daniel Dunbare10787e2009-08-07 08:26:05 +0000355 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000356 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000357 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000358 }
Chad Rosier51afe632012-06-27 22:34:28 +0000359 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000360 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000361 }
Chad Rosier51afe632012-06-27 22:34:28 +0000362 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000363 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000364 }
Chad Rosier51afe632012-06-27 22:34:28 +0000365 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000366 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000367 }
Chad Rosier51afe632012-06-27 22:34:28 +0000368 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000369 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000370 }
Chad Rosier51afe632012-06-27 22:34:28 +0000371 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000372 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000373 }
Chad Rosier51afe632012-06-27 22:34:28 +0000374 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000375 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000376 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000377
Craig Topper01deb5f2012-07-18 04:11:12 +0000378 bool isMemVX32() const {
379 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
380 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
381 }
382 bool isMemVY32() const {
383 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
384 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
385 }
386 bool isMemVX64() const {
387 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
388 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
389 }
390 bool isMemVY64() const {
391 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
392 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
393 }
394
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000395 bool isAbsMem() const {
396 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000397 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000398 }
399
Daniel Dunbare10787e2009-08-07 08:26:05 +0000400 bool isReg() const { return Kind == Register; }
401
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000402 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
403 // Add as immediates when possible.
404 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
405 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
406 else
407 Inst.addOperand(MCOperand::CreateExpr(Expr));
408 }
409
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000410 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000411 assert(N == 1 && "Invalid number of operands!");
412 Inst.addOperand(MCOperand::CreateReg(getReg()));
413 }
414
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000415 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000416 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000417 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000418 }
419
Chad Rosier51afe632012-06-27 22:34:28 +0000420 void addMem8Operands(MCInst &Inst, unsigned N) const {
421 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000422 }
Chad Rosier51afe632012-06-27 22:34:28 +0000423 void addMem16Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000425 }
Chad Rosier51afe632012-06-27 22:34:28 +0000426 void addMem32Operands(MCInst &Inst, unsigned N) const {
427 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000428 }
Chad Rosier51afe632012-06-27 22:34:28 +0000429 void addMem64Operands(MCInst &Inst, unsigned N) const {
430 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000431 }
Chad Rosier51afe632012-06-27 22:34:28 +0000432 void addMem80Operands(MCInst &Inst, unsigned N) const {
433 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000434 }
Chad Rosier51afe632012-06-27 22:34:28 +0000435 void addMem128Operands(MCInst &Inst, unsigned N) const {
436 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000437 }
Chad Rosier51afe632012-06-27 22:34:28 +0000438 void addMem256Operands(MCInst &Inst, unsigned N) const {
439 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000440 }
Craig Topper01deb5f2012-07-18 04:11:12 +0000441 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
442 addMemOperands(Inst, N);
443 }
444 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
445 addMemOperands(Inst, N);
446 }
447 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
448 addMemOperands(Inst, N);
449 }
450 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
451 addMemOperands(Inst, N);
452 }
Devang Patelfc6be102012-01-12 01:51:42 +0000453
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000454 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000455 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000456 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
457 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
458 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000459 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000460 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
461 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000462
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000463 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
464 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000465 // Add as immediates when possible.
466 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
467 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
468 else
469 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000470 }
471
Chris Lattner528d00b2010-01-15 19:28:38 +0000472 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000473 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000474 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000475 Res->Tok.Data = Str.data();
476 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000477 return Res;
478 }
479
Chad Rosier91c82662012-10-24 17:22:29 +0000480 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000481 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000482 SMLoc OffsetOfLoc = SMLoc(),
483 StringRef SymName = StringRef()) {
Chris Lattner86e61532010-01-15 19:06:59 +0000484 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000485 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000486 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000487 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000488 Res->SymName = SymName;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000489 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000490 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000491
Chad Rosierf3c04f62013-03-19 21:58:18 +0000492 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000493 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000494 Res->Imm.Val = Val;
495 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000496 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000497
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000498 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000499 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000500 unsigned Size = 0,
501 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000502 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
503 Res->Mem.SegReg = 0;
504 Res->Mem.Disp = Disp;
505 Res->Mem.BaseReg = 0;
506 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000507 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000508 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000509 Res->SymName = SymName;
Chad Rosier8c2a9c72013-01-10 23:39:07 +0000510 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000511 return Res;
512 }
513
514 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000515 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
516 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +0000517 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000518 unsigned Size = 0,
519 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000520 // We should never just have a displacement, that should be parsed as an
521 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000522 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
523
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000524 // The scale should always be one of {1,2,4,8}.
525 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000526 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000527 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000528 Res->Mem.SegReg = SegReg;
529 Res->Mem.Disp = Disp;
530 Res->Mem.BaseReg = BaseReg;
531 Res->Mem.IndexReg = IndexReg;
532 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +0000533 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000534 Res->SymName = SymName;
NAKAMURA Takumi7f254272013-01-11 01:13:54 +0000535 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000536 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000537 }
538};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000539
Chris Lattner4eb9df02009-07-29 06:33:53 +0000540} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000541
Devang Patel4a6e7782012-01-12 18:03:40 +0000542bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000543 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000544
545 return (Op.isMem() &&
546 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
547 isa<MCConstantExpr>(Op.Mem.Disp) &&
548 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
549 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
550}
551
Devang Patel4a6e7782012-01-12 18:03:40 +0000552bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000553 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000554
Chad Rosier51afe632012-06-27 22:34:28 +0000555 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000556 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000557 isa<MCConstantExpr>(Op.Mem.Disp) &&
558 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
559 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
560}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000561
Devang Patel4a6e7782012-01-12 18:03:40 +0000562bool X86AsmParser::ParseRegister(unsigned &RegNo,
563 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000564 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000565 const AsmToken &PercentTok = Parser.getTok();
566 StartLoc = PercentTok.getLoc();
567
568 // If we encounter a %, ignore it. This code handles registers with and
569 // without the prefix, unprefixed registers can occur in cfi directives.
570 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000571 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000572
Sean Callanan936b0d32010-01-19 21:44:56 +0000573 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000574 EndLoc = Tok.getEndLoc();
575
Devang Patelce6a2ca2012-01-20 22:32:05 +0000576 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000577 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000578 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000579 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000580 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000581
Kevin Enderby7d912182009-09-03 17:15:07 +0000582 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000583
Chris Lattner1261b812010-09-22 04:11:10 +0000584 // If the match failed, try the register name as lowercase.
585 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000586 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000587
Evan Chengeda1d4f2011-07-27 23:22:03 +0000588 if (!is64BitMode()) {
589 // FIXME: This should be done using Requires<In32BitMode> and
590 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
591 // checked.
592 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
593 // REX prefix.
594 if (RegNo == X86::RIZ ||
595 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
596 X86II::isX86_64NonExtLowByteReg(RegNo) ||
597 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000598 return Error(StartLoc, "register %"
599 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000600 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000601 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000602
Chris Lattner1261b812010-09-22 04:11:10 +0000603 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
604 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000605 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000606 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000607
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000608 // Check to see if we have '(4)' after %st.
609 if (getLexer().isNot(AsmToken::LParen))
610 return false;
611 // Lex the paren.
612 getParser().Lex();
613
614 const AsmToken &IntTok = Parser.getTok();
615 if (IntTok.isNot(AsmToken::Integer))
616 return Error(IntTok.getLoc(), "expected stack index");
617 switch (IntTok.getIntVal()) {
618 case 0: RegNo = X86::ST0; break;
619 case 1: RegNo = X86::ST1; break;
620 case 2: RegNo = X86::ST2; break;
621 case 3: RegNo = X86::ST3; break;
622 case 4: RegNo = X86::ST4; break;
623 case 5: RegNo = X86::ST5; break;
624 case 6: RegNo = X86::ST6; break;
625 case 7: RegNo = X86::ST7; break;
626 default: return Error(IntTok.getLoc(), "invalid stack index");
627 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000628
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000629 if (getParser().Lex().isNot(AsmToken::RParen))
630 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000631
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000632 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000633 Parser.Lex(); // Eat ')'
634 return false;
635 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000636
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000637 EndLoc = Parser.getTok().getEndLoc();
638
Chris Lattner80486622010-06-24 07:29:18 +0000639 // If this is "db[0-7]", match it as an alias
640 // for dr[0-7].
641 if (RegNo == 0 && Tok.getString().size() == 3 &&
642 Tok.getString().startswith("db")) {
643 switch (Tok.getString()[2]) {
644 case '0': RegNo = X86::DR0; break;
645 case '1': RegNo = X86::DR1; break;
646 case '2': RegNo = X86::DR2; break;
647 case '3': RegNo = X86::DR3; break;
648 case '4': RegNo = X86::DR4; break;
649 case '5': RegNo = X86::DR5; break;
650 case '6': RegNo = X86::DR6; break;
651 case '7': RegNo = X86::DR7; break;
652 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000653
Chris Lattner80486622010-06-24 07:29:18 +0000654 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000655 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000656 Parser.Lex(); // Eat it.
657 return false;
658 }
659 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000660
Devang Patelce6a2ca2012-01-20 22:32:05 +0000661 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000662 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000663 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000664 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000665 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000666
Sean Callanana83fd7d2010-01-19 20:27:46 +0000667 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000668 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000669}
670
Devang Patel4a6e7782012-01-12 18:03:40 +0000671X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000672 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000673 return ParseIntelOperand();
674 return ParseATTOperand();
675}
676
Devang Patel41b9dde2012-01-17 18:00:18 +0000677/// getIntelMemOperandSize - Return intel memory operand size.
678static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000679 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000680 .Cases("BYTE", "byte", 8)
681 .Cases("WORD", "word", 16)
682 .Cases("DWORD", "dword", 32)
683 .Cases("QWORD", "qword", 64)
684 .Cases("XWORD", "xword", 80)
685 .Cases("XMMWORD", "xmmword", 128)
686 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +0000687 .Default(0);
688 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000689}
690
Chad Rosier4a7005e2013-04-05 16:28:55 +0000691enum InfixCalculatorTok {
692 IC_PLUS = 0,
693 IC_MINUS,
694 IC_MULTIPLY,
695 IC_DIVIDE,
696 IC_RPAREN,
697 IC_LPAREN,
698 IC_IMM,
699 IC_REGISTER
700};
701static const char OpPrecedence[] = {
702 0, // IC_PLUS
703 0, // IC_MINUS
704 1, // IC_MULTIPLY
705 1, // IC_DIVIDE
706 2, // IC_RPAREN
707 3, // IC_LPAREN
708 0, // IC_IMM
709 0 // IC_REGISTER
710};
711
712class InfixCalculator {
713 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
714 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
715 SmallVector<ICToken, 4> PostfixStack;
716
717public:
718 int64_t popOperand() {
719 assert (!PostfixStack.empty() && "Poped an empty stack!");
720 ICToken Op = PostfixStack.pop_back_val();
721 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
722 && "Expected and immediate or register!");
723 return Op.second;
724 }
725 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
726 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
727 "Unexpected operand!");
728 PostfixStack.push_back(std::make_pair(Op, Val));
729 }
730
731 void popOperator() { InfixOperatorStack.pop_back_val(); }
732 void pushOperator(InfixCalculatorTok Op) {
733 // Push the new operator if the stack is empty.
734 if (InfixOperatorStack.empty()) {
735 InfixOperatorStack.push_back(Op);
736 return;
737 }
738
739 // Push the new operator if it has a higher precedence than the operator on
740 // the top of the stack or the operator on the top of the stack is a left
741 // parentheses.
742 unsigned Idx = InfixOperatorStack.size() - 1;
743 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
744 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
745 InfixOperatorStack.push_back(Op);
746 return;
747 }
748
749 // The operator on the top of the stack has higher precedence than the
750 // new operator.
751 unsigned ParenCount = 0;
752 while (1) {
753 // Nothing to process.
754 if (InfixOperatorStack.empty())
755 break;
756
757 Idx = InfixOperatorStack.size() - 1;
758 StackOp = InfixOperatorStack[Idx];
759 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
760 break;
761
762 // If we have an even parentheses count and we see a left parentheses,
763 // then stop processing.
764 if (!ParenCount && StackOp == IC_LPAREN)
765 break;
766
767 if (StackOp == IC_RPAREN) {
768 ++ParenCount;
769 InfixOperatorStack.pop_back_val();
770 } else if (StackOp == IC_LPAREN) {
771 --ParenCount;
772 InfixOperatorStack.pop_back_val();
773 } else {
774 InfixOperatorStack.pop_back_val();
775 PostfixStack.push_back(std::make_pair(StackOp, 0));
776 }
777 }
778 // Push the new operator.
779 InfixOperatorStack.push_back(Op);
780 }
781 int64_t execute() {
782 // Push any remaining operators onto the postfix stack.
783 while (!InfixOperatorStack.empty()) {
784 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
785 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
786 PostfixStack.push_back(std::make_pair(StackOp, 0));
787 }
788
789 if (PostfixStack.empty())
790 return 0;
791
792 SmallVector<ICToken, 16> OperandStack;
793 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
794 ICToken Op = PostfixStack[i];
795 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
796 OperandStack.push_back(Op);
797 } else {
798 assert (OperandStack.size() > 1 && "Too few operands.");
799 int64_t Val;
800 ICToken Op2 = OperandStack.pop_back_val();
801 ICToken Op1 = OperandStack.pop_back_val();
802 switch (Op.first) {
803 default:
804 report_fatal_error("Unexpected operator!");
805 break;
806 case IC_PLUS:
807 Val = Op1.second + Op2.second;
808 OperandStack.push_back(std::make_pair(IC_IMM, Val));
809 break;
810 case IC_MINUS:
811 Val = Op1.second - Op2.second;
812 OperandStack.push_back(std::make_pair(IC_IMM, Val));
813 break;
814 case IC_MULTIPLY:
815 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
816 "Multiply operation with an immediate and a register!");
817 Val = Op1.second * Op2.second;
818 OperandStack.push_back(std::make_pair(IC_IMM, Val));
819 break;
820 case IC_DIVIDE:
821 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
822 "Divide operation with an immediate and a register!");
823 assert (Op2.second != 0 && "Division by zero!");
824 Val = Op1.second / Op2.second;
825 OperandStack.push_back(std::make_pair(IC_IMM, Val));
826 break;
827 }
828 }
829 }
830 assert (OperandStack.size() == 1 && "Expected a single result.");
831 return OperandStack.pop_back_val().second;
832 }
833};
834
Chad Rosier5c118fd2013-01-14 22:31:35 +0000835enum IntelBracExprState {
Chad Rosier4a7005e2013-04-05 16:28:55 +0000836 IBES_PLUS,
837 IBES_MINUS,
838 IBES_MULTIPLY,
839 IBES_DIVIDE,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000840 IBES_LBRAC,
841 IBES_RBRAC,
Chad Rosier4a7005e2013-04-05 16:28:55 +0000842 IBES_LPAREN,
843 IBES_RPAREN,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000844 IBES_REGISTER,
845 IBES_REGISTER_STAR,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000846 IBES_INTEGER,
847 IBES_INTEGER_STAR,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000848 IBES_IDENTIFIER,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000849 IBES_ERROR
850};
851
852class IntelBracExprStateMachine {
853 IntelBracExprState State;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000854 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000855 int64_t Disp;
Chad Rosier175d0ae2013-04-12 18:21:18 +0000856 const MCExpr *Sym;
857 StringRef SymName;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000858 InfixCalculator IC;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000859public:
Chad Rosier1530ba52013-03-27 21:49:56 +0000860 IntelBracExprStateMachine(MCAsmParser &parser, int64_t disp) :
Chad Rosier175d0ae2013-04-12 18:21:18 +0000861 State(IBES_PLUS), BaseReg(0), IndexReg(0), TmpReg(0), Scale(1), Disp(disp),
862 Sym(0) {}
Chad Rosier5c118fd2013-01-14 22:31:35 +0000863
864 unsigned getBaseReg() { return BaseReg; }
865 unsigned getIndexReg() { return IndexReg; }
866 unsigned getScale() { return Scale; }
Chad Rosier175d0ae2013-04-12 18:21:18 +0000867 const MCExpr *getSym() { return Sym; }
868 StringRef getSymName() { return SymName; }
869 int64_t getImmDisp() { return Disp + IC.execute(); }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000870 bool isValidEndState() { return State == IBES_RBRAC; }
871
872 void onPlus() {
873 switch (State) {
874 default:
875 State = IBES_ERROR;
876 break;
877 case IBES_INTEGER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000878 case IBES_RPAREN:
879 State = IBES_PLUS;
880 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000881 break;
882 case IBES_REGISTER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000883 State = IBES_PLUS;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000884 // If we already have a BaseReg, then assume this is the IndexReg with a
885 // scale of 1.
886 if (!BaseReg) {
887 BaseReg = TmpReg;
888 } else {
889 assert (!IndexReg && "BaseReg/IndexReg already set!");
890 IndexReg = TmpReg;
891 Scale = 1;
892 }
Chad Rosier4a7005e2013-04-05 16:28:55 +0000893 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000894 break;
895 }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000896 }
897 void onMinus() {
898 switch (State) {
899 default:
900 State = IBES_ERROR;
901 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000902 case IBES_PLUS:
903 case IBES_LPAREN:
904 IC.pushOperand(IC_IMM);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000905 case IBES_INTEGER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000906 case IBES_RPAREN:
907 State = IBES_MINUS;
908 IC.pushOperator(IC_MINUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000909 break;
910 case IBES_REGISTER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000911 State = IBES_MINUS;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000912 // If we already have a BaseReg, then assume this is the IndexReg with a
913 // scale of 1.
914 if (!BaseReg) {
915 BaseReg = TmpReg;
916 } else {
917 assert (!IndexReg && "BaseReg/IndexReg already set!");
918 IndexReg = TmpReg;
919 Scale = 1;
920 }
Chad Rosier4a7005e2013-04-05 16:28:55 +0000921 IC.pushOperator(IC_MINUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000922 break;
923 }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000924 }
925 void onRegister(unsigned Reg) {
926 switch (State) {
927 default:
928 State = IBES_ERROR;
929 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000930 case IBES_PLUS:
931 case IBES_LPAREN:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000932 State = IBES_REGISTER;
933 TmpReg = Reg;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000934 IC.pushOperand(IC_REGISTER);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000935 break;
936 case IBES_INTEGER_STAR:
937 assert (!IndexReg && "IndexReg already set!");
Chad Rosier4a7005e2013-04-05 16:28:55 +0000938 State = IBES_INTEGER;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000939 IndexReg = Reg;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000940 Scale = IC.popOperand();
941 IC.pushOperand(IC_IMM);
942 IC.popOperator();
Chad Rosier5c118fd2013-01-14 22:31:35 +0000943 break;
944 }
945 }
Chad Rosier175d0ae2013-04-12 18:21:18 +0000946 void onDispExpr(const MCExpr *SymRef, StringRef SymRefName) {
Chad Rosier5c118fd2013-01-14 22:31:35 +0000947 switch (State) {
948 default:
949 State = IBES_ERROR;
950 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000951 case IBES_PLUS:
952 case IBES_MINUS:
953 State = IBES_INTEGER;
Chad Rosier175d0ae2013-04-12 18:21:18 +0000954 Sym = SymRef;
955 SymName = SymRefName;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000956 IC.pushOperand(IC_IMM);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000957 break;
958 }
959 }
960 void onInteger(int64_t TmpInt) {
961 switch (State) {
962 default:
963 State = IBES_ERROR;
964 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000965 case IBES_PLUS:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000966 case IBES_MINUS:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000967 case IBES_MULTIPLY:
968 case IBES_DIVIDE:
969 case IBES_LPAREN:
970 case IBES_INTEGER_STAR:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000971 State = IBES_INTEGER;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000972 IC.pushOperand(IC_IMM, TmpInt);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000973 break;
974 case IBES_REGISTER_STAR:
975 assert (!IndexReg && "IndexReg already set!");
Chad Rosier4a7005e2013-04-05 16:28:55 +0000976 State = IBES_INTEGER;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000977 IndexReg = TmpReg;
978 Scale = TmpInt;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000979 IC.popOperator();
Chad Rosier5c118fd2013-01-14 22:31:35 +0000980 break;
981 }
982 }
983 void onStar() {
984 switch (State) {
985 default:
986 State = IBES_ERROR;
987 break;
988 case IBES_INTEGER:
989 State = IBES_INTEGER_STAR;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000990 IC.pushOperator(IC_MULTIPLY);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000991 break;
992 case IBES_REGISTER:
993 State = IBES_REGISTER_STAR;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000994 IC.pushOperator(IC_MULTIPLY);
995 break;
996 case IBES_RPAREN:
997 State = IBES_MULTIPLY;
998 IC.pushOperator(IC_MULTIPLY);
999 break;
1000 }
1001 }
1002 void onDivide() {
1003 switch (State) {
1004 default:
1005 State = IBES_ERROR;
1006 break;
1007 case IBES_INTEGER:
1008 State = IBES_DIVIDE;
1009 IC.pushOperator(IC_DIVIDE);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001010 break;
1011 }
1012 }
1013 void onLBrac() {
1014 switch (State) {
1015 default:
1016 State = IBES_ERROR;
1017 break;
1018 case IBES_RBRAC:
Chad Rosier4a7005e2013-04-05 16:28:55 +00001019 State = IBES_PLUS;
1020 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001021 break;
1022 }
1023 }
1024 void onRBrac() {
1025 switch (State) {
1026 default:
1027 State = IBES_ERROR;
1028 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001029 case IBES_RPAREN:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001030 case IBES_INTEGER:
1031 State = IBES_RBRAC;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001032 break;
1033 case IBES_REGISTER:
1034 State = IBES_RBRAC;
1035 // If we already have a BaseReg, then assume this is the IndexReg with a
1036 // scale of 1.
1037 if (!BaseReg) {
1038 BaseReg = TmpReg;
1039 } else {
1040 assert (!IndexReg && "BaseReg/IndexReg already set!");
1041 IndexReg = TmpReg;
1042 Scale = 1;
1043 }
1044 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001045 }
1046 }
1047 void onLParen() {
1048 switch (State) {
1049 default:
1050 State = IBES_ERROR;
1051 break;
1052 case IBES_PLUS:
1053 case IBES_MINUS:
1054 case IBES_MULTIPLY:
1055 case IBES_DIVIDE:
1056 case IBES_INTEGER_STAR:
1057 case IBES_LPAREN:
1058 State = IBES_LPAREN;
1059 IC.pushOperator(IC_LPAREN);
1060 break;
1061 }
1062 }
1063 void onRParen() {
1064 switch (State) {
1065 default:
1066 State = IBES_ERROR;
1067 break;
1068 case IBES_REGISTER:
1069 case IBES_INTEGER:
1070 case IBES_PLUS:
1071 case IBES_MINUS:
1072 case IBES_MULTIPLY:
1073 case IBES_DIVIDE:
1074 case IBES_RPAREN:
1075 State = IBES_RPAREN;
1076 IC.pushOperator(IC_RPAREN);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001077 break;
1078 }
1079 }
1080};
1081
Chad Rosier175d0ae2013-04-12 18:21:18 +00001082X86Operand *
1083X86AsmParser::CreateMemForInlineAsm(unsigned SegReg, const MCExpr *Disp,
1084 unsigned BaseReg, unsigned IndexReg,
1085 unsigned Scale, SMLoc Start, SMLoc End,
1086 SMLoc SizeDirLoc, unsigned Size,
1087 StringRef SymName) {
Chad Rosier7ca135b2013-03-19 21:11:56 +00001088 bool NeedSizeDir = false;
Chad Rosier7ca135b2013-03-19 21:11:56 +00001089 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1090 const MCSymbol &Sym = SymRef->getSymbol();
1091 // FIXME: The SemaLookup will fail if the name is anything other then an
1092 // identifier.
1093 // FIXME: Pass a valid SMLoc.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001094 bool IsVarDecl = false;
Chad Rosier7ca135b2013-03-19 21:11:56 +00001095 unsigned tLength, tSize, tType;
Chad Rosiere81309b2013-04-09 17:53:49 +00001096 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength, tSize,
1097 tType, IsVarDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001098 if (!Size) {
1099 Size = tType * 8; // Size is in terms of bits in this context.
1100 NeedSizeDir = Size > 0;
1101 }
Chad Rosier175d0ae2013-04-12 18:21:18 +00001102 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1103 // reference. We need an 'r' constraint here, so we need to create register
1104 // operand to ensure proper matching. Just pick a GPR based on the size of
1105 // a pointer.
1106 if (!IsVarDecl) {
1107 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1108 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true,
1109 SMLoc(), SymName);
1110 }
Chad Rosier7ca135b2013-03-19 21:11:56 +00001111 }
1112
1113 if (NeedSizeDir)
1114 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
1115 /*Len*/0, Size));
1116
1117 // When parsing inline assembly we set the base register to a non-zero value
Chad Rosier175d0ae2013-04-12 18:21:18 +00001118 // if we don't know the actual value at this time. This is necessary to
Chad Rosier7ca135b2013-03-19 21:11:56 +00001119 // get the matching correct in some cases.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001120 BaseReg = BaseReg ? BaseReg : 1;
1121 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1122 End, Size, SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001123}
1124
Chad Rosier1530ba52013-03-27 21:49:56 +00001125X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Chad Rosierfce4fab2013-04-08 17:43:47 +00001126 SMLoc SizeDirLoc,
Chad Rosier1530ba52013-03-27 21:49:56 +00001127 uint64_t ImmDisp,
Devang Patel880bc162012-01-23 18:31:58 +00001128 unsigned Size) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001129 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001130 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel46831de2012-01-12 01:36:43 +00001131
Devang Patel41b9dde2012-01-17 18:00:18 +00001132 if (getLexer().isNot(AsmToken::LBrac))
1133 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosier175d0ae2013-04-12 18:21:18 +00001134 Parser.Lex(); // Eat '['
Chad Rosier51afe632012-06-27 22:34:28 +00001135
Chad Rosier5c118fd2013-01-14 22:31:35 +00001136 unsigned TmpReg = 0;
Chad Rosier175d0ae2013-04-12 18:21:18 +00001137 SMLoc StartInBrac = Tok.getLoc();
Chad Rosier152749c2013-04-12 18:54:20 +00001138 // Parse [ Symbol + ImmDisp ] and [ BaseReg + Scale*IndexReg + ImmDisp ]. We
1139 // may have already parsed an immediate displacement before the bracketed
1140 // expression.
Chad Rosier5c118fd2013-01-14 22:31:35 +00001141 bool Done = false;
Chad Rosier1530ba52013-03-27 21:49:56 +00001142 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier1bbaa442012-10-29 18:01:54 +00001143
Chad Rosier5c118fd2013-01-14 22:31:35 +00001144 // If we parsed a register, then the end loc has already been set and
1145 // the identifier has already been lexed. We also need to update the
1146 // state.
1147 if (TmpReg)
1148 SM.onRegister(TmpReg);
1149
Chad Rosier5c118fd2013-01-14 22:31:35 +00001150 while (!Done) {
1151 bool UpdateLocLex = true;
1152
1153 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1154 // identifier. Don't try an parse it as a register.
1155 if (Tok.getString().startswith("."))
1156 break;
1157
1158 switch (getLexer().getKind()) {
1159 default: {
1160 if (SM.isValidEndState()) {
1161 Done = true;
1162 break;
1163 }
1164 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1165 }
1166 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001167 // This could be a register or a symbolic displacement.
1168 unsigned TmpReg;
1169 const MCExpr *Disp = 0;
Chad Rosier152749c2013-04-12 18:54:20 +00001170 SMLoc IdentLoc = Tok.getLoc();
1171 StringRef Identifier = Tok.getString();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001172 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001173 SM.onRegister(TmpReg);
1174 UpdateLocLex = false;
1175 break;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001176 } else if (!getParser().parsePrimaryExpr(Disp, End)) {
Chad Rosier152749c2013-04-12 18:54:20 +00001177 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1178 return Err;
1179
1180 SM.onDispExpr(Disp, Identifier);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001181 UpdateLocLex = false;
1182 break;
1183 }
1184 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1185 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001186 case AsmToken::Integer:
1187 if (isParsingInlineAsm())
1188 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1189 Tok.getLoc()));
1190 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001191 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001192 case AsmToken::Plus: SM.onPlus(); break;
1193 case AsmToken::Minus: SM.onMinus(); break;
1194 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001195 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001196 case AsmToken::LBrac: SM.onLBrac(); break;
1197 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001198 case AsmToken::LParen: SM.onLParen(); break;
1199 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001200 }
1201 if (!Done && UpdateLocLex) {
1202 End = Tok.getLoc();
1203 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001204 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001205 }
1206
Chad Rosier175d0ae2013-04-12 18:21:18 +00001207 const MCExpr *Disp;
1208 if (const MCExpr *Sym = SM.getSym()) {
1209 Disp = Sym;
1210
1211 if (isParsingInlineAsm()) {
1212 // Remove the '[' and ']' from the IR string.
1213 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Start, 1));
1214 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1215
1216 // If ImmDisp is non-zero, then we parsed a displacement before the
Chad Rosier152749c2013-04-12 18:54:20 +00001217 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp])
Chad Rosier175d0ae2013-04-12 18:21:18 +00001218 uint64_t FinalImmDisp = SM.getImmDisp();
Chad Rosier152749c2013-04-12 18:54:20 +00001219
1220 // If ImmDisp doesn't match the displacement computed by the state machine
1221 // then we have an additional displacement in the bracketed expression.
1222 if (ImmDisp != FinalImmDisp) {
1223 if (ImmDisp) {
1224 // FIXME: We have an immediate displacement before the bracketed
1225 // expression. Adjust this to match the final immediate displacement.
1226 } else {
1227 // We have a symbolic and an immediate displacement, but no displacement
1228 // before the bracketed expression.
Chad Rosier175d0ae2013-04-12 18:21:18 +00001229
Chad Rosier152749c2013-04-12 18:54:20 +00001230 // Put the immediate displacement before the bracketed expression.
1231 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, 0,
1232 FinalImmDisp));
1233 }
Chad Rosier175d0ae2013-04-12 18:21:18 +00001234 }
1235 // Remove all the ImmPrefix rewrites within the brackets.
1236 for (SmallVectorImpl<AsmRewrite>::iterator
1237 I = InstInfo->AsmRewrites->begin(),
1238 E = InstInfo->AsmRewrites->end(); I != E; ++I) {
1239 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1240 continue;
1241 if ((*I).Kind == AOK_ImmPrefix)
1242 (*I).Kind = AOK_Delete;
1243 }
1244 StringRef SymName = SM.getSymName();
1245 const char *SymLocPtr = SymName.data();
1246 // Skip everything before the symbol.
1247 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1248 assert(Len > 0 && "Expected a non-negative length.");
1249 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1250 }
1251 // Skip everything after the symbol.
1252 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1253 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1254 assert(Len > 0 && "Expected a non-negative length.");
1255 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1256 }
1257 }
1258 } else {
1259 // An immediate displacement only.
1260 Disp = MCConstantExpr::Create(SM.getImmDisp(), getContext());
1261 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001262
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001263 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001264 if (Tok.getString().startswith(".")) {
1265 SmallString<64> Err;
1266 const MCExpr *NewDisp;
1267 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1268 return ErrorOperand(Tok.getLoc(), Err);
1269
Chad Rosier70f47592013-04-10 20:07:47 +00001270 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001271 Parser.Lex(); // Eat the field.
1272 Disp = NewDisp;
1273 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001274
Chad Rosier5c118fd2013-01-14 22:31:35 +00001275 int BaseReg = SM.getBaseReg();
1276 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001277 int Scale = SM.getScale();
1278
1279 if (isParsingInlineAsm())
1280 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1281 End, SizeDirLoc, Size, SM.getSymName());
Devang Pateld0930ff2012-01-20 21:21:01 +00001282
Chad Rosier5c118fd2013-01-14 22:31:35 +00001283 // handle [-42]
1284 if (!BaseReg && !IndexReg) {
1285 if (!SegReg)
Chad Rosiere81309b2013-04-09 17:53:49 +00001286 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001287 else
1288 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1289 }
Chad Rosiere81309b2013-04-09 17:53:49 +00001290 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1291 End, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001292}
1293
Chad Rosier8a244662013-04-02 20:02:33 +00001294// Inline assembly may use variable names with namespace alias qualifiers.
1295X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +00001296 StringRef &Identifier) {
Chad Rosier8a244662013-04-02 20:02:33 +00001297 // We should only see Foo::Bar if we're parsing inline assembly.
1298 if (!isParsingInlineAsm())
1299 return 0;
1300
1301 // If we don't see a ':' then there can't be a qualifier.
1302 if (getLexer().isNot(AsmToken::Colon))
1303 return 0;
1304
Chad Rosier8a244662013-04-02 20:02:33 +00001305 bool Done = false;
1306 const AsmToken &Tok = Parser.getTok();
Chad Rosierce031892013-04-11 23:24:15 +00001307 AsmToken IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001308 while (!Done) {
1309 switch (getLexer().getKind()) {
1310 default:
1311 Done = true;
1312 break;
1313 case AsmToken::Colon:
1314 getLexer().Lex(); // Consume ':'.
1315 if (getLexer().isNot(AsmToken::Colon))
1316 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1317 getLexer().Lex(); // Consume second ':'.
1318 if (getLexer().isNot(AsmToken::Identifier))
1319 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1320 break;
1321 case AsmToken::Identifier:
Chad Rosierce031892013-04-11 23:24:15 +00001322 IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001323 getLexer().Lex(); // Consume the identifier.
1324 break;
1325 }
1326 }
Chad Rosierce031892013-04-11 23:24:15 +00001327
1328 unsigned Len = IdentEnd.getLoc().getPointer() - Identifier.data();
1329 Identifier = StringRef(Identifier.data(), Len + IdentEnd.getString().size());
Chad Rosier8a244662013-04-02 20:02:33 +00001330 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1331 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1332 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1333 return 0;
1334}
1335
Devang Patel41b9dde2012-01-17 18:00:18 +00001336/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001337X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1338 uint64_t ImmDisp,
1339 SMLoc Start) {
Devang Patel41b9dde2012-01-17 18:00:18 +00001340 const AsmToken &Tok = Parser.getTok();
Chad Rosier91c82662012-10-24 17:22:29 +00001341 SMLoc End;
Devang Patel41b9dde2012-01-17 18:00:18 +00001342
1343 unsigned Size = getIntelMemOperandSize(Tok.getString());
1344 if (Size) {
1345 Parser.Lex();
Chad Rosierab53b4f2012-09-12 18:24:26 +00001346 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1347 "Unexpected token!");
Devang Patel41b9dde2012-01-17 18:00:18 +00001348 Parser.Lex();
1349 }
1350
Chad Rosier1530ba52013-03-27 21:49:56 +00001351 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1352 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier1530ba52013-03-27 21:49:56 +00001353 if (isParsingInlineAsm())
1354 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
Chad Rosier70f47592013-04-10 20:07:47 +00001355 Tok.getLoc()));
1356 uint64_t ImmDisp = Tok.getIntVal();
Chad Rosier1530ba52013-03-27 21:49:56 +00001357 Parser.Lex(); // Eat the integer.
1358 if (getLexer().isNot(AsmToken::LBrac))
1359 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001360 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosier1530ba52013-03-27 21:49:56 +00001361 }
1362
Chad Rosier91c82662012-10-24 17:22:29 +00001363 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001364 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001365
1366 if (!ParseRegister(SegReg, Start, End)) {
1367 // Handel SegReg : [ ... ]
1368 if (getLexer().isNot(AsmToken::Colon))
1369 return ErrorOperand(Start, "Expected ':' token!");
1370 Parser.Lex(); // Eat :
1371 if (getLexer().isNot(AsmToken::LBrac))
1372 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001373 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001374 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001375
Chad Rosiere81309b2013-04-09 17:53:49 +00001376 const MCExpr *Disp = 0;
Chad Rosierce031892013-04-11 23:24:15 +00001377 StringRef Identifier = Tok.getString();
Chad Rosiere8d82882013-04-09 19:59:12 +00001378 if (getParser().parseExpression(Disp, End))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001379 return 0;
Chad Rosier0f48c552012-10-19 20:57:14 +00001380
Chad Rosier146310a2012-10-23 23:31:33 +00001381 if (!isParsingInlineAsm())
Chad Rosier91c82662012-10-24 17:22:29 +00001382 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier8a244662013-04-02 20:02:33 +00001383
Chad Rosierce031892013-04-11 23:24:15 +00001384 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001385 return Err;
1386
Chad Rosier175d0ae2013-04-12 18:21:18 +00001387 return CreateMemForInlineAsm(/*SegReg=*/0, Disp, /*BaseReg=*/0,/*IndexReg=*/0,
1388 /*Scale=*/1, Start, End, Start, Size,Identifier);
Chad Rosier91c82662012-10-24 17:22:29 +00001389}
1390
Chad Rosier5dcb4662012-10-24 22:21:50 +00001391/// Parse the '.' operator.
Chad Rosier911c1f32012-10-25 17:37:43 +00001392bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1393 const MCExpr **NewDisp,
1394 SmallString<64> &Err) {
Chad Rosier70f47592013-04-10 20:07:47 +00001395 const AsmToken &Tok = Parser.getTok();
Chad Rosier911c1f32012-10-25 17:37:43 +00001396 uint64_t OrigDispVal, DotDispVal;
1397
1398 // FIXME: Handle non-constant expressions.
1399 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1400 OrigDispVal = OrigDisp->getValue();
1401 } else {
1402 Err = "Non-constant offsets are not supported!";
1403 return true;
1404 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001405
1406 // Drop the '.'.
1407 StringRef DotDispStr = Tok.getString().drop_front(1);
1408
Chad Rosier5dcb4662012-10-24 22:21:50 +00001409 // .Imm gets lexed as a real.
1410 if (Tok.is(AsmToken::Real)) {
1411 APInt DotDisp;
1412 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001413 DotDispVal = DotDisp.getZExtValue();
Chad Rosier240b7b92012-10-25 21:51:10 +00001414 } else if (Tok.is(AsmToken::Identifier)) {
1415 // We should only see an identifier when parsing the original inline asm.
1416 // The front-end should rewrite this in terms of immediates.
1417 assert (isParsingInlineAsm() && "Unexpected field name!");
1418
1419 unsigned DotDisp;
1420 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1421 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1422 DotDisp)) {
1423 Err = "Unable to lookup field reference!";
1424 return true;
1425 }
1426 DotDispVal = DotDisp;
Chad Rosier911c1f32012-10-25 17:37:43 +00001427 } else {
1428 Err = "Unexpected token type!";
1429 return true;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001430 }
Chad Rosier911c1f32012-10-25 17:37:43 +00001431
Chad Rosier240b7b92012-10-25 21:51:10 +00001432 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1433 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1434 unsigned Len = DotDispStr.size();
1435 unsigned Val = OrigDispVal + DotDispVal;
1436 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1437 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001438 }
1439
1440 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1441 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001442}
1443
Chad Rosier91c82662012-10-24 17:22:29 +00001444/// Parse the 'offset' operator. This operator is used to specify the
1445/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001446X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001447 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001448 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001449 Parser.Lex(); // Eat offset.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001450 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier91c82662012-10-24 17:22:29 +00001451
Chad Rosier91c82662012-10-24 17:22:29 +00001452 const MCExpr *Val;
Chad Rosier18785852013-04-09 20:58:48 +00001453 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001454 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001455 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosier58593562012-10-26 18:32:44 +00001456 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosier91c82662012-10-24 17:22:29 +00001457
Chad Rosierae7ecd62013-04-11 23:37:34 +00001458 const MCExpr *Disp = 0;
1459 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1460 return Err;
1461
Chad Rosiere2f03772012-10-26 16:09:20 +00001462 // Don't emit the offset operator.
1463 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1464
Chad Rosier91c82662012-10-24 17:22:29 +00001465 // The offset operator will have an 'r' constraint, thus we need to create
1466 // register operand to ensure proper matching. Just pick a GPR based on
1467 // the size of a pointer.
1468 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001469 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosierae7ecd62013-04-11 23:37:34 +00001470 OffsetOfLoc, Identifier);
Devang Patel41b9dde2012-01-17 18:00:18 +00001471}
1472
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001473enum IntelOperatorKind {
1474 IOK_LENGTH,
1475 IOK_SIZE,
1476 IOK_TYPE
1477};
1478
1479/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1480/// returns the number of elements in an array. It returns the value 1 for
1481/// non-array variables. The SIZE operator returns the size of a C or C++
1482/// variable. A variable's size is the product of its LENGTH and TYPE. The
1483/// TYPE operator returns the size of a C or C++ type or variable. If the
1484/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001485X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001486 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001487 SMLoc TypeLoc = Tok.getLoc();
1488 Parser.Lex(); // Eat operator.
Chad Rosier18785852013-04-09 20:58:48 +00001489 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier11c42f22012-10-26 18:04:20 +00001490
Chad Rosier11c42f22012-10-26 18:04:20 +00001491 const MCExpr *Val;
Chad Rosierb67f8052013-04-11 23:57:04 +00001492 AsmToken StartTok = Tok;
Chad Rosier18785852013-04-09 20:58:48 +00001493 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001494 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001495 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosierb67f8052013-04-11 23:57:04 +00001496 return ErrorOperand(Start, "Unable to parse expression!");
1497
1498 const MCExpr *Disp = 0;
1499 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1500 return Err;
Chad Rosier11c42f22012-10-26 18:04:20 +00001501
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001502 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosier11c42f22012-10-26 18:04:20 +00001503 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1504 const MCSymbol &Sym = SymRef->getSymbol();
1505 // FIXME: The SemaLookup will fail if the name is anything other then an
1506 // identifier.
1507 // FIXME: Pass a valid SMLoc.
Chad Rosiera4bc9432013-01-10 22:10:27 +00001508 bool IsVarDecl;
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001509 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1510 Size, Type, IsVarDecl))
Chad Rosierb67f8052013-04-11 23:57:04 +00001511 // FIXME: We don't warn on variables with namespace alias qualifiers
1512 // because support still needs to be added in the frontend.
1513 if (Identifier.equals(StartTok.getString()))
1514 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001515 }
1516 unsigned CVal;
1517 switch(OpKind) {
1518 default: llvm_unreachable("Unexpected operand kind!");
1519 case IOK_LENGTH: CVal = Length; break;
1520 case IOK_SIZE: CVal = Size; break;
1521 case IOK_TYPE: CVal = Type; break;
Chad Rosier11c42f22012-10-26 18:04:20 +00001522 }
1523
1524 // Rewrite the type operator and the C or C++ type or variable in terms of an
1525 // immediate. E.g. TYPE foo -> $$4
1526 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001527 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001528
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001529 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001530 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001531}
1532
Devang Patel41b9dde2012-01-17 18:00:18 +00001533X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001534 const AsmToken &Tok = Parser.getTok();
1535 SMLoc Start = Tok.getLoc(), End;
1536 StringRef AsmTokStr = Tok.getString();
Chad Rosier91c82662012-10-24 17:22:29 +00001537
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001538 // Offset, length, type and size operators.
1539 if (isParsingInlineAsm()) {
1540 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001541 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001542 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001543 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001544 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001545 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001546 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001547 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001548 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001549
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001550 // Immediate.
Devang Patel41b9dde2012-01-17 18:00:18 +00001551 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1552 getLexer().is(AsmToken::Minus)) {
1553 const MCExpr *Val;
Chad Rosier1530ba52013-03-27 21:49:56 +00001554 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001555 if (!getParser().parseExpression(Val, End)) {
Chad Rosierf3c04f62013-03-19 21:58:18 +00001556 if (isParsingInlineAsm())
1557 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosier1530ba52013-03-27 21:49:56 +00001558 // Immediate.
1559 if (getLexer().isNot(AsmToken::LBrac))
1560 return X86Operand::CreateImm(Val, Start, End);
1561
1562 // Only positive immediates are valid.
1563 if (!isInteger) {
Chad Rosier70f47592013-04-10 20:07:47 +00001564 Error(Tok.getLoc(), "expected a positive immediate "
Chad Rosier1530ba52013-03-27 21:49:56 +00001565 "displacement before bracketed expr.");
1566 return 0;
1567 }
1568
1569 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1570 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1571 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Patel41b9dde2012-01-17 18:00:18 +00001572 }
1573 }
1574
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001575 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001576 unsigned RegNo = 0;
1577 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001578 // If this is a segment register followed by a ':', then this is the start
1579 // of a memory reference, otherwise this is a normal register reference.
1580 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001581 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001582
1583 getParser().Lex(); // Eat the colon.
Chad Rosier1530ba52013-03-27 21:49:56 +00001584 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001585 }
1586
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001587 // Memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001588 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001589}
1590
Devang Patel4a6e7782012-01-12 18:03:40 +00001591X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001592 switch (getLexer().getKind()) {
1593 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001594 // Parse a memory operand with no segment register.
1595 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001596 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001597 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001598 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001599 SMLoc Start, End;
1600 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001601 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001602 Error(Start, "%eiz and %riz can only be used as index registers",
1603 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001604 return 0;
1605 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001606
Chris Lattnerb9270732010-04-17 18:56:34 +00001607 // If this is a segment register followed by a ':', then this is the start
1608 // of a memory reference, otherwise this is a normal register reference.
1609 if (getLexer().isNot(AsmToken::Colon))
1610 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001611
Chris Lattnerb9270732010-04-17 18:56:34 +00001612 getParser().Lex(); // Eat the colon.
1613 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001614 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001615 case AsmToken::Dollar: {
1616 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001617 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001618 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001619 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001620 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001621 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001622 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001623 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001624 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001625}
1626
Chris Lattnerb9270732010-04-17 18:56:34 +00001627/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1628/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001629X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001630
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001631 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1632 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001633 // only way to do this without lookahead is to eat the '(' and see what is
1634 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001635 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001636 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001637 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001638 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001639
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001640 // After parsing the base expression we could either have a parenthesized
1641 // memory address or not. If not, return now. If so, eat the (.
1642 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001643 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001644 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001645 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001646 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001647 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001648
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001649 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001650 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001651 } else {
1652 // Okay, we have a '('. We don't know if this is an expression or not, but
1653 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001654 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001655 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001656
Kevin Enderby7d912182009-09-03 17:15:07 +00001657 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001658 // Nothing to do here, fall into the code below with the '(' part of the
1659 // memory operand consumed.
1660 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001661 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001662
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001663 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001664 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001665 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001666
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001667 // After parsing the base expression we could either have a parenthesized
1668 // memory address or not. If not, return now. If so, eat the (.
1669 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001670 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001671 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001672 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001673 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001674 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001675
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001676 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001677 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001678 }
1679 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001680
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001681 // If we reached here, then we just ate the ( of the memory operand. Process
1682 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001683 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001684 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001685
Chris Lattner0c2538f2010-01-15 18:51:29 +00001686 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001687 SMLoc StartLoc, EndLoc;
1688 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001689 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001690 Error(StartLoc, "eiz and riz can only be used as index registers",
1691 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001692 return 0;
1693 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001694 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001695
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001696 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001697 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001698 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001699
1700 // Following the comma we should have either an index register, or a scale
1701 // value. We don't support the later form, but we want to parse it
1702 // correctly.
1703 //
1704 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001705 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001706 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001707 SMLoc L;
1708 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001709
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001710 if (getLexer().isNot(AsmToken::RParen)) {
1711 // Parse the scale amount:
1712 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001713 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001714 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001715 "expected comma in scale expression");
1716 return 0;
1717 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001718 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001719
1720 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001721 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001722
1723 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001724 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001725 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001726 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001727 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001728
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001729 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001730 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1731 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1732 return 0;
1733 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001734 Scale = (unsigned)ScaleVal;
1735 }
1736 }
1737 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001738 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001739 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001740 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001741
1742 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001743 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001744 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001745
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001746 if (Value != 1)
1747 Warning(Loc, "scale factor without index register is ignored");
1748 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001749 }
1750 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001751
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001752 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001753 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001754 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001755 return 0;
1756 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001757 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001758 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001759
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001760 // If we have both a base register and an index register make sure they are
1761 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001762 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001763 if (BaseReg != 0 && IndexReg != 0) {
1764 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001765 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1766 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001767 IndexReg != X86::RIZ) {
1768 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1769 return 0;
1770 }
1771 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001772 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1773 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001774 IndexReg != X86::EIZ){
1775 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1776 return 0;
1777 }
1778 }
1779
Chris Lattner015cfb12010-01-15 19:33:43 +00001780 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1781 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001782}
1783
Devang Patel4a6e7782012-01-12 18:03:40 +00001784bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001785ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001786 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001787 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001788 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001789
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001790 // FIXME: Hack to recognize setneb as setne.
1791 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1792 PatchedName != "setb" && PatchedName != "setnb")
1793 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001794
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001795 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1796 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001797 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001798 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1799 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001800 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001801 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001802 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001803 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001804 .Case("eq", 0x00)
1805 .Case("lt", 0x01)
1806 .Case("le", 0x02)
1807 .Case("unord", 0x03)
1808 .Case("neq", 0x04)
1809 .Case("nlt", 0x05)
1810 .Case("nle", 0x06)
1811 .Case("ord", 0x07)
1812 /* AVX only from here */
1813 .Case("eq_uq", 0x08)
1814 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001815 .Case("ngt", 0x0A)
1816 .Case("false", 0x0B)
1817 .Case("neq_oq", 0x0C)
1818 .Case("ge", 0x0D)
1819 .Case("gt", 0x0E)
1820 .Case("true", 0x0F)
1821 .Case("eq_os", 0x10)
1822 .Case("lt_oq", 0x11)
1823 .Case("le_oq", 0x12)
1824 .Case("unord_s", 0x13)
1825 .Case("neq_us", 0x14)
1826 .Case("nlt_uq", 0x15)
1827 .Case("nle_uq", 0x16)
1828 .Case("ord_s", 0x17)
1829 .Case("eq_us", 0x18)
1830 .Case("nge_uq", 0x19)
1831 .Case("ngt_uq", 0x1A)
1832 .Case("false_os", 0x1B)
1833 .Case("neq_os", 0x1C)
1834 .Case("ge_oq", 0x1D)
1835 .Case("gt_oq", 0x1E)
1836 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001837 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001838 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001839 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1840 getParser().getContext());
1841 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001842 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001843 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001844 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001845 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001846 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001847 } else {
1848 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001849 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001850 }
1851 }
1852 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001853
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001854 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001855
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001856 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001857 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001858
Chris Lattner086a83a2010-09-08 05:17:37 +00001859 // Determine whether this is an instruction prefix.
1860 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001861 Name == "lock" || Name == "rep" ||
1862 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001863 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001864 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001865
1866
Chris Lattner086a83a2010-09-08 05:17:37 +00001867 // This does the actual operand parsing. Don't parse any more if we have a
1868 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1869 // just want to parse the "lock" as the first instruction and the "incl" as
1870 // the next one.
1871 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001872
1873 // Parse '*' modifier.
1874 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001875 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001876 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001877 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001878 }
1879
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001880 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001881 if (X86Operand *Op = ParseOperand())
1882 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001883 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001884 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001885 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001886 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001887
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001888 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001889 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001890
1891 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001892 if (X86Operand *Op = ParseOperand())
1893 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001894 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001895 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001896 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001897 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001898 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001899
Chris Lattnera2a9d162010-09-11 16:18:25 +00001900 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001901 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001902 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001903 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001904 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001905 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001906
Chris Lattner086a83a2010-09-08 05:17:37 +00001907 if (getLexer().is(AsmToken::EndOfStatement))
1908 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001909 else if (isPrefix && getLexer().is(AsmToken::Slash))
1910 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001911
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001912 if (ExtraImmOp && isParsingIntelSyntax())
1913 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1914
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001915 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1916 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1917 // documented form in various unofficial manuals, so a lot of code uses it.
1918 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1919 Operands.size() == 3) {
1920 X86Operand &Op = *(X86Operand*)Operands.back();
1921 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1922 isa<MCConstantExpr>(Op.Mem.Disp) &&
1923 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1924 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1925 SMLoc Loc = Op.getEndLoc();
1926 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1927 delete &Op;
1928 }
1929 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001930 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1931 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1932 Operands.size() == 3) {
1933 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1934 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1935 isa<MCConstantExpr>(Op.Mem.Disp) &&
1936 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1937 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1938 SMLoc Loc = Op.getEndLoc();
1939 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1940 delete &Op;
1941 }
1942 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001943 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1944 if (Name.startswith("ins") && Operands.size() == 3 &&
1945 (Name == "insb" || Name == "insw" || Name == "insl")) {
1946 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1947 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1948 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1949 Operands.pop_back();
1950 Operands.pop_back();
1951 delete &Op;
1952 delete &Op2;
1953 }
1954 }
1955
1956 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1957 if (Name.startswith("outs") && Operands.size() == 3 &&
1958 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1959 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1960 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1961 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1962 Operands.pop_back();
1963 Operands.pop_back();
1964 delete &Op;
1965 delete &Op2;
1966 }
1967 }
1968
1969 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1970 if (Name.startswith("movs") && Operands.size() == 3 &&
1971 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001972 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001973 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1974 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1975 if (isSrcOp(Op) && isDstOp(Op2)) {
1976 Operands.pop_back();
1977 Operands.pop_back();
1978 delete &Op;
1979 delete &Op2;
1980 }
1981 }
1982 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1983 if (Name.startswith("lods") && Operands.size() == 3 &&
1984 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001985 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001986 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1987 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1988 if (isSrcOp(*Op1) && Op2->isReg()) {
1989 const char *ins;
1990 unsigned reg = Op2->getReg();
1991 bool isLods = Name == "lods";
1992 if (reg == X86::AL && (isLods || Name == "lodsb"))
1993 ins = "lodsb";
1994 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1995 ins = "lodsw";
1996 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1997 ins = "lodsl";
1998 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1999 ins = "lodsq";
2000 else
2001 ins = NULL;
2002 if (ins != NULL) {
2003 Operands.pop_back();
2004 Operands.pop_back();
2005 delete Op1;
2006 delete Op2;
2007 if (Name != ins)
2008 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2009 }
2010 }
2011 }
2012 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2013 if (Name.startswith("stos") && Operands.size() == 3 &&
2014 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002015 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002016 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2017 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2018 if (isDstOp(*Op2) && Op1->isReg()) {
2019 const char *ins;
2020 unsigned reg = Op1->getReg();
2021 bool isStos = Name == "stos";
2022 if (reg == X86::AL && (isStos || Name == "stosb"))
2023 ins = "stosb";
2024 else if (reg == X86::AX && (isStos || Name == "stosw"))
2025 ins = "stosw";
2026 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2027 ins = "stosl";
2028 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2029 ins = "stosq";
2030 else
2031 ins = NULL;
2032 if (ins != NULL) {
2033 Operands.pop_back();
2034 Operands.pop_back();
2035 delete Op1;
2036 delete Op2;
2037 if (Name != ins)
2038 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2039 }
2040 }
2041 }
2042
Chris Lattner4bd21712010-09-15 04:33:27 +00002043 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002044 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002045 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002046 Name.startswith("shl") || Name.startswith("sal") ||
2047 Name.startswith("rcl") || Name.startswith("rcr") ||
2048 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002049 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002050 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002051 // Intel syntax
2052 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2053 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002054 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2055 delete Operands[2];
2056 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002057 }
2058 } else {
2059 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2060 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002061 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2062 delete Operands[1];
2063 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002064 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002065 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002066 }
Chad Rosier51afe632012-06-27 22:34:28 +00002067
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002068 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2069 // instalias with an immediate operand yet.
2070 if (Name == "int" && Operands.size() == 2) {
2071 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2072 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2073 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2074 delete Operands[1];
2075 Operands.erase(Operands.begin() + 1);
2076 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2077 }
2078 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002079
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002080 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002081}
2082
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002083static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2084 bool isCmp) {
2085 MCInst TmpInst;
2086 TmpInst.setOpcode(Opcode);
2087 if (!isCmp)
2088 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2089 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2090 TmpInst.addOperand(Inst.getOperand(0));
2091 Inst = TmpInst;
2092 return true;
2093}
2094
2095static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2096 bool isCmp = false) {
2097 if (!Inst.getOperand(0).isImm() ||
2098 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2099 return false;
2100
2101 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2102}
2103
2104static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2105 bool isCmp = false) {
2106 if (!Inst.getOperand(0).isImm() ||
2107 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2108 return false;
2109
2110 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2111}
2112
2113static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2114 bool isCmp = false) {
2115 if (!Inst.getOperand(0).isImm() ||
2116 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2117 return false;
2118
2119 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2120}
2121
Devang Patel4a6e7782012-01-12 18:03:40 +00002122bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002123processInstruction(MCInst &Inst,
2124 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2125 switch (Inst.getOpcode()) {
2126 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002127 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2128 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2129 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2130 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2131 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2132 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2133 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2134 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2135 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2136 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2137 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2138 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2139 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2140 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2141 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2142 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2143 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2144 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002145 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2146 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2147 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2148 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2149 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2150 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002151 }
Devang Patelde47cce2012-01-18 22:42:29 +00002152}
2153
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002154static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002155bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002156MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002157 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002158 MCStreamer &Out, unsigned &ErrorInfo,
2159 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002160 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002161 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2162 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosier3d4bc622012-08-21 19:36:59 +00002163 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002164
Chris Lattnera63292a2010-09-29 01:50:45 +00002165 // First, handle aliases that expand to multiple instructions.
2166 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002167 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002168 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002169 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002170 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002171 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002172 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002173 MCInst Inst;
2174 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002175 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002176 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002177 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002178
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002179 const char *Repl =
2180 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002181 .Case("finit", "fninit")
2182 .Case("fsave", "fnsave")
2183 .Case("fstcw", "fnstcw")
2184 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002185 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002186 .Case("fstsw", "fnstsw")
2187 .Case("fstsww", "fnstsw")
2188 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002189 .Default(0);
2190 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002191 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002192 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002193 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002194
Chris Lattner628fbec2010-09-06 21:54:15 +00002195 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002196 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002197
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002198 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002199 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002200 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002201 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002202 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002203 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002204 // Some instructions need post-processing to, for example, tweak which
2205 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002206 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002207 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002208 while (processInstruction(Inst, Operands))
2209 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002210
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002211 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002212 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002213 Out.EmitInstruction(Inst);
2214 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002215 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002216 case Match_MissingFeature: {
2217 assert(ErrorInfo && "Unknown missing feature!");
2218 // Special case the error message for the very common case where only
2219 // a single subtarget feature is missing.
2220 std::string Msg = "instruction requires:";
2221 unsigned Mask = 1;
2222 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2223 if (ErrorInfo & Mask) {
2224 Msg += " ";
2225 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2226 }
2227 Mask <<= 1;
2228 }
2229 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2230 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002231 case Match_InvalidOperand:
2232 WasOriginallyInvalidOperand = true;
2233 break;
2234 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002235 break;
2236 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002237
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002238 // FIXME: Ideally, we would only attempt suffix matches for things which are
2239 // valid prefixes, and we could just infer the right unambiguous
2240 // type. However, that requires substantially more matcher support than the
2241 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002242
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002243 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002244 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002245 SmallString<16> Tmp;
2246 Tmp += Base;
2247 Tmp += ' ';
2248 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002249
Chris Lattnerfab94132010-11-06 18:28:02 +00002250 // If this instruction starts with an 'f', then it is a floating point stack
2251 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2252 // 80-bit floating point, which use the suffixes s,l,t respectively.
2253 //
2254 // Otherwise, we assume that this may be an integer instruction, which comes
2255 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2256 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002257
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002258 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002259 Tmp[Base.size()] = Suffixes[0];
2260 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002261 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002262 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002263
Chad Rosier2f480a82012-10-12 22:53:36 +00002264 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2265 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002266 // If this returned as a missing feature failure, remember that.
2267 if (Match1 == Match_MissingFeature)
2268 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002269 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002270 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2271 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002272 // If this returned as a missing feature failure, remember that.
2273 if (Match2 == Match_MissingFeature)
2274 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002275 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002276 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2277 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002278 // If this returned as a missing feature failure, remember that.
2279 if (Match3 == Match_MissingFeature)
2280 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002281 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002282 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2283 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002284 // If this returned as a missing feature failure, remember that.
2285 if (Match4 == Match_MissingFeature)
2286 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002287
2288 // Restore the old token.
2289 Op->setTokenValue(Base);
2290
2291 // If exactly one matched, then we treat that as a successful match (and the
2292 // instruction will already have been filled in correctly, since the failing
2293 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002294 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002295 (Match1 == Match_Success) + (Match2 == Match_Success) +
2296 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002297 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002298 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002299 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002300 Out.EmitInstruction(Inst);
2301 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002302 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002303 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002304
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002305 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002306
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002307 // If we had multiple suffix matches, then identify this as an ambiguous
2308 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002309 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002310 char MatchChars[4];
2311 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002312 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2313 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2314 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2315 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002316
2317 SmallString<126> Msg;
2318 raw_svector_ostream OS(Msg);
2319 OS << "ambiguous instructions require an explicit suffix (could be ";
2320 for (unsigned i = 0; i != NumMatches; ++i) {
2321 if (i != 0)
2322 OS << ", ";
2323 if (i + 1 == NumMatches)
2324 OS << "or ";
2325 OS << "'" << Base << MatchChars[i] << "'";
2326 }
2327 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002328 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002329 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002330 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002331
Chris Lattner628fbec2010-09-06 21:54:15 +00002332 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002333
Chris Lattner628fbec2010-09-06 21:54:15 +00002334 // If all of the instructions reported an invalid mnemonic, then the original
2335 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002336 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2337 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002338 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002339 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002340 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002341 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002342 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002343 }
2344
2345 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002346 if (ErrorInfo != ~0U) {
2347 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002348 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002349 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002350
Chad Rosier49963552012-10-13 00:26:04 +00002351 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002352 if (Operand->getStartLoc().isValid()) {
2353 SMRange OperandRange = Operand->getLocRange();
2354 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002355 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002356 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002357 }
2358
Chad Rosier3d4bc622012-08-21 19:36:59 +00002359 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002360 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002361 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002362
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002363 // If one instruction matched with a missing feature, report this as a
2364 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002365 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2366 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002367 std::string Msg = "instruction requires:";
2368 unsigned Mask = 1;
2369 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2370 if (ErrorInfoMissingFeature & Mask) {
2371 Msg += " ";
2372 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2373 }
2374 Mask <<= 1;
2375 }
2376 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002377 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002378
Chris Lattner628fbec2010-09-06 21:54:15 +00002379 // If one instruction matched with an invalid operand, report this as an
2380 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002381 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2382 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002383 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002384 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002385 return true;
2386 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002387
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002388 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002389 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002390 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002391 return true;
2392}
2393
2394
Devang Patel4a6e7782012-01-12 18:03:40 +00002395bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002396 StringRef IDVal = DirectiveID.getIdentifier();
2397 if (IDVal == ".word")
2398 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002399 else if (IDVal.startswith(".code"))
2400 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002401 else if (IDVal.startswith(".att_syntax")) {
2402 getParser().setAssemblerDialect(0);
2403 return false;
2404 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002405 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2407 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002408 // FIXME : Handle noprefix
2409 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002410 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002411 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002412 }
2413 return false;
2414 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002415 return true;
2416}
2417
2418/// ParseDirectiveWord
2419/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002420bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002421 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2422 for (;;) {
2423 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002424 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002425 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002426
Eric Christopherbf7bc492013-01-09 03:52:05 +00002427 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002428
Chris Lattner72c0b592010-10-30 17:38:55 +00002429 if (getLexer().is(AsmToken::EndOfStatement))
2430 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002431
Chris Lattner72c0b592010-10-30 17:38:55 +00002432 // FIXME: Improve diagnostic.
2433 if (getLexer().isNot(AsmToken::Comma))
2434 return Error(L, "unexpected token in directive");
2435 Parser.Lex();
2436 }
2437 }
Chad Rosier51afe632012-06-27 22:34:28 +00002438
Chris Lattner72c0b592010-10-30 17:38:55 +00002439 Parser.Lex();
2440 return false;
2441}
2442
Evan Cheng481ebb02011-07-27 00:38:12 +00002443/// ParseDirectiveCode
2444/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002445bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002446 if (IDVal == ".code32") {
2447 Parser.Lex();
2448 if (is64BitMode()) {
2449 SwitchMode();
2450 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2451 }
2452 } else if (IDVal == ".code64") {
2453 Parser.Lex();
2454 if (!is64BitMode()) {
2455 SwitchMode();
2456 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2457 }
2458 } else {
2459 return Error(L, "unexpected directive " + IDVal);
2460 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002461
Evan Cheng481ebb02011-07-27 00:38:12 +00002462 return false;
2463}
Chris Lattner72c0b592010-10-30 17:38:55 +00002464
Daniel Dunbar71475772009-07-17 20:42:00 +00002465// Force static initialization.
2466extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002467 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2468 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002469}
Daniel Dunbar00331992009-07-29 00:02:19 +00002470
Chris Lattner3e4582a2010-09-06 19:11:01 +00002471#define GET_REGISTER_MATCHER
2472#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002473#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002474#include "X86GenAsmMatcher.inc"