blob: 5ef59a7113af8fdd8cfd32b78614744e271daf47 [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 Rosier5c118fd2013-01-14 22:31:35 +00001138
Chad Rosier1530ba52013-03-27 21:49:56 +00001139 // Try to handle '[' 'Symbol' ']'
Devang Patel41b9dde2012-01-17 18:00:18 +00001140 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosier70f47592013-04-10 20:07:47 +00001141 SMLoc Loc = Tok.getLoc();
1142 if (ParseRegister(TmpReg, Loc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001143 const MCExpr *Disp;
Chad Rosierce031892013-04-11 23:24:15 +00001144 StringRef Identifier = Tok.getString();
Chad Rosiere8d82882013-04-09 19:59:12 +00001145 if (getParser().parseExpression(Disp, End))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001146 return 0;
1147
Chad Rosierce031892013-04-11 23:24:15 +00001148 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001149 return Err;
1150
Devang Patel41b9dde2012-01-17 18:00:18 +00001151 if (getLexer().isNot(AsmToken::RBrac))
Chad Rosier70f47592013-04-10 20:07:47 +00001152 return ErrorOperand(Tok.getLoc(), "Expected ']' token!");
Chad Rosier1530ba52013-03-27 21:49:56 +00001153
Chad Rosier8fb83302013-04-11 21:49:30 +00001154 if (isParsingInlineAsm()) {
1155 // Remove the '[' and ']' from the IR string.
1156 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Start, 1));
1157 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Tok.getLoc(), 1));
1158 }
Chad Rosiere81309b2013-04-09 17:53:49 +00001159 Parser.Lex(); // Eat ']'
Chad Rosier7ca135b2013-03-19 21:11:56 +00001160 if (!isParsingInlineAsm())
Chad Rosierce031892013-04-11 23:24:15 +00001161 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier175d0ae2013-04-12 18:21:18 +00001162 return CreateMemForInlineAsm(/*SegReg=*/0, Disp, /*BaseReg=*/0,
1163 /*IndexReg=*/0, /*Scale*/1, Start, End,
1164 SizeDirLoc, Size, Identifier);
Devang Patel41b9dde2012-01-17 18:00:18 +00001165 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001166 }
1167
Chad Rosier1530ba52013-03-27 21:49:56 +00001168 // Parse [ BaseReg + Scale*IndexReg + Disp ]. We may have already parsed an
1169 // immediate displacement before the bracketed expression.
Chad Rosier5c118fd2013-01-14 22:31:35 +00001170 bool Done = false;
Chad Rosier1530ba52013-03-27 21:49:56 +00001171 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier1bbaa442012-10-29 18:01:54 +00001172
Chad Rosier5c118fd2013-01-14 22:31:35 +00001173 // If we parsed a register, then the end loc has already been set and
1174 // the identifier has already been lexed. We also need to update the
1175 // state.
1176 if (TmpReg)
1177 SM.onRegister(TmpReg);
1178
Chad Rosier5c118fd2013-01-14 22:31:35 +00001179 while (!Done) {
1180 bool UpdateLocLex = true;
1181
1182 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1183 // identifier. Don't try an parse it as a register.
1184 if (Tok.getString().startswith("."))
1185 break;
1186
1187 switch (getLexer().getKind()) {
1188 default: {
1189 if (SM.isValidEndState()) {
1190 Done = true;
1191 break;
1192 }
1193 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1194 }
1195 case AsmToken::Identifier: {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001196 // This could be a register or a symbolic displacement.
1197 unsigned TmpReg;
1198 const MCExpr *Disp = 0;
1199 AsmToken IdentTok = Parser.getTok();
1200 SMLoc IdentLoc = IdentTok.getLoc();
1201 if(!ParseRegister(TmpReg, IdentLoc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001202 SM.onRegister(TmpReg);
1203 UpdateLocLex = false;
1204 break;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001205 } else if (!getParser().parsePrimaryExpr(Disp, End)) {
Chad Rosier175d0ae2013-04-12 18:21:18 +00001206 SM.onDispExpr(Disp, IdentTok.getString());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001207 UpdateLocLex = false;
1208 break;
1209 }
1210 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1211 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001212 case AsmToken::Integer:
1213 if (isParsingInlineAsm())
1214 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1215 Tok.getLoc()));
1216 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001217 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001218 case AsmToken::Plus: SM.onPlus(); break;
1219 case AsmToken::Minus: SM.onMinus(); break;
1220 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001221 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001222 case AsmToken::LBrac: SM.onLBrac(); break;
1223 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001224 case AsmToken::LParen: SM.onLParen(); break;
1225 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001226 }
1227 if (!Done && UpdateLocLex) {
1228 End = Tok.getLoc();
1229 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001230 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001231 }
1232
Chad Rosier175d0ae2013-04-12 18:21:18 +00001233 const MCExpr *Disp;
1234 if (const MCExpr *Sym = SM.getSym()) {
1235 Disp = Sym;
1236
1237 if (isParsingInlineAsm()) {
1238 // Remove the '[' and ']' from the IR string.
1239 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Start, 1));
1240 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1241
1242 // If ImmDisp is non-zero, then we parsed a displacement before the
1243 // bracketed expression (i.e., ImmDisp [ BaseReg + Scale*IndexReg + Disp ])
1244 uint64_t FinalImmDisp = SM.getImmDisp();
1245 if (ImmDisp && ImmDisp != FinalImmDisp) {
1246 // If ImmDisp doesn't match the displacement computed by the state machine
1247 // then we have an additional displacement in the bracketed expression.
1248
1249 } else if (FinalImmDisp) {
1250 // We have a symbolic and an immediate displacement, but no displacement
1251 // before the bracketed expression.
1252
1253 // Put the immediate displacement before the bracketed expression.
1254 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, Start, 0,
1255 FinalImmDisp));
1256 }
1257 // Remove all the ImmPrefix rewrites within the brackets.
1258 for (SmallVectorImpl<AsmRewrite>::iterator
1259 I = InstInfo->AsmRewrites->begin(),
1260 E = InstInfo->AsmRewrites->end(); I != E; ++I) {
1261 if ((*I).Loc.getPointer() < StartInBrac.getPointer())
1262 continue;
1263 if ((*I).Kind == AOK_ImmPrefix)
1264 (*I).Kind = AOK_Delete;
1265 }
1266 StringRef SymName = SM.getSymName();
1267 const char *SymLocPtr = SymName.data();
1268 // Skip everything before the symbol.
1269 if (unsigned Len = SymLocPtr - StartInBrac.getPointer()) {
1270 assert(Len > 0 && "Expected a non-negative length.");
1271 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, StartInBrac, Len));
1272 }
1273 // Skip everything after the symbol.
1274 if (unsigned Len = End.getPointer() - (SymLocPtr + SymName.size())) {
1275 SMLoc Loc = SMLoc::getFromPointer(SymLocPtr + SymName.size());
1276 assert(Len > 0 && "Expected a non-negative length.");
1277 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Loc, Len));
1278 }
1279 }
1280 } else {
1281 // An immediate displacement only.
1282 Disp = MCConstantExpr::Create(SM.getImmDisp(), getContext());
1283 }
Devang Pateld0930ff2012-01-20 21:21:01 +00001284
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001285 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001286 if (Tok.getString().startswith(".")) {
1287 SmallString<64> Err;
1288 const MCExpr *NewDisp;
1289 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1290 return ErrorOperand(Tok.getLoc(), Err);
1291
Chad Rosier70f47592013-04-10 20:07:47 +00001292 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001293 Parser.Lex(); // Eat the field.
1294 Disp = NewDisp;
1295 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001296
Chad Rosier5c118fd2013-01-14 22:31:35 +00001297 int BaseReg = SM.getBaseReg();
1298 int IndexReg = SM.getIndexReg();
Chad Rosier175d0ae2013-04-12 18:21:18 +00001299 int Scale = SM.getScale();
1300
1301 if (isParsingInlineAsm())
1302 return CreateMemForInlineAsm(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1303 End, SizeDirLoc, Size, SM.getSymName());
Devang Pateld0930ff2012-01-20 21:21:01 +00001304
Chad Rosier5c118fd2013-01-14 22:31:35 +00001305 // handle [-42]
1306 if (!BaseReg && !IndexReg) {
1307 if (!SegReg)
Chad Rosiere81309b2013-04-09 17:53:49 +00001308 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001309 else
1310 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1311 }
Chad Rosiere81309b2013-04-09 17:53:49 +00001312 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1313 End, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001314}
1315
Chad Rosier8a244662013-04-02 20:02:33 +00001316// Inline assembly may use variable names with namespace alias qualifiers.
1317X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +00001318 StringRef &Identifier) {
Chad Rosier8a244662013-04-02 20:02:33 +00001319 // We should only see Foo::Bar if we're parsing inline assembly.
1320 if (!isParsingInlineAsm())
1321 return 0;
1322
1323 // If we don't see a ':' then there can't be a qualifier.
1324 if (getLexer().isNot(AsmToken::Colon))
1325 return 0;
1326
Chad Rosier8a244662013-04-02 20:02:33 +00001327 bool Done = false;
1328 const AsmToken &Tok = Parser.getTok();
Chad Rosierce031892013-04-11 23:24:15 +00001329 AsmToken IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001330 while (!Done) {
1331 switch (getLexer().getKind()) {
1332 default:
1333 Done = true;
1334 break;
1335 case AsmToken::Colon:
1336 getLexer().Lex(); // Consume ':'.
1337 if (getLexer().isNot(AsmToken::Colon))
1338 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1339 getLexer().Lex(); // Consume second ':'.
1340 if (getLexer().isNot(AsmToken::Identifier))
1341 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1342 break;
1343 case AsmToken::Identifier:
Chad Rosierce031892013-04-11 23:24:15 +00001344 IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001345 getLexer().Lex(); // Consume the identifier.
1346 break;
1347 }
1348 }
Chad Rosierce031892013-04-11 23:24:15 +00001349
1350 unsigned Len = IdentEnd.getLoc().getPointer() - Identifier.data();
1351 Identifier = StringRef(Identifier.data(), Len + IdentEnd.getString().size());
Chad Rosier8a244662013-04-02 20:02:33 +00001352 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1353 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1354 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1355 return 0;
1356}
1357
Devang Patel41b9dde2012-01-17 18:00:18 +00001358/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001359X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1360 uint64_t ImmDisp,
1361 SMLoc Start) {
Devang Patel41b9dde2012-01-17 18:00:18 +00001362 const AsmToken &Tok = Parser.getTok();
Chad Rosier91c82662012-10-24 17:22:29 +00001363 SMLoc End;
Devang Patel41b9dde2012-01-17 18:00:18 +00001364
1365 unsigned Size = getIntelMemOperandSize(Tok.getString());
1366 if (Size) {
1367 Parser.Lex();
Chad Rosierab53b4f2012-09-12 18:24:26 +00001368 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1369 "Unexpected token!");
Devang Patel41b9dde2012-01-17 18:00:18 +00001370 Parser.Lex();
1371 }
1372
Chad Rosier1530ba52013-03-27 21:49:56 +00001373 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1374 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier1530ba52013-03-27 21:49:56 +00001375 if (isParsingInlineAsm())
1376 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
Chad Rosier70f47592013-04-10 20:07:47 +00001377 Tok.getLoc()));
1378 uint64_t ImmDisp = Tok.getIntVal();
Chad Rosier1530ba52013-03-27 21:49:56 +00001379 Parser.Lex(); // Eat the integer.
1380 if (getLexer().isNot(AsmToken::LBrac))
1381 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001382 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosier1530ba52013-03-27 21:49:56 +00001383 }
1384
Chad Rosier91c82662012-10-24 17:22:29 +00001385 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001386 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001387
1388 if (!ParseRegister(SegReg, Start, End)) {
1389 // Handel SegReg : [ ... ]
1390 if (getLexer().isNot(AsmToken::Colon))
1391 return ErrorOperand(Start, "Expected ':' token!");
1392 Parser.Lex(); // Eat :
1393 if (getLexer().isNot(AsmToken::LBrac))
1394 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001395 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001396 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001397
Chad Rosiere81309b2013-04-09 17:53:49 +00001398 const MCExpr *Disp = 0;
Chad Rosierce031892013-04-11 23:24:15 +00001399 StringRef Identifier = Tok.getString();
Chad Rosiere8d82882013-04-09 19:59:12 +00001400 if (getParser().parseExpression(Disp, End))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001401 return 0;
Chad Rosier0f48c552012-10-19 20:57:14 +00001402
Chad Rosier146310a2012-10-23 23:31:33 +00001403 if (!isParsingInlineAsm())
Chad Rosier91c82662012-10-24 17:22:29 +00001404 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier8a244662013-04-02 20:02:33 +00001405
Chad Rosierce031892013-04-11 23:24:15 +00001406 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001407 return Err;
1408
Chad Rosier175d0ae2013-04-12 18:21:18 +00001409 return CreateMemForInlineAsm(/*SegReg=*/0, Disp, /*BaseReg=*/0,/*IndexReg=*/0,
1410 /*Scale=*/1, Start, End, Start, Size,Identifier);
Chad Rosier91c82662012-10-24 17:22:29 +00001411}
1412
Chad Rosier5dcb4662012-10-24 22:21:50 +00001413/// Parse the '.' operator.
Chad Rosier911c1f32012-10-25 17:37:43 +00001414bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1415 const MCExpr **NewDisp,
1416 SmallString<64> &Err) {
Chad Rosier70f47592013-04-10 20:07:47 +00001417 const AsmToken &Tok = Parser.getTok();
Chad Rosier911c1f32012-10-25 17:37:43 +00001418 uint64_t OrigDispVal, DotDispVal;
1419
1420 // FIXME: Handle non-constant expressions.
1421 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1422 OrigDispVal = OrigDisp->getValue();
1423 } else {
1424 Err = "Non-constant offsets are not supported!";
1425 return true;
1426 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001427
1428 // Drop the '.'.
1429 StringRef DotDispStr = Tok.getString().drop_front(1);
1430
Chad Rosier5dcb4662012-10-24 22:21:50 +00001431 // .Imm gets lexed as a real.
1432 if (Tok.is(AsmToken::Real)) {
1433 APInt DotDisp;
1434 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001435 DotDispVal = DotDisp.getZExtValue();
Chad Rosier240b7b92012-10-25 21:51:10 +00001436 } else if (Tok.is(AsmToken::Identifier)) {
1437 // We should only see an identifier when parsing the original inline asm.
1438 // The front-end should rewrite this in terms of immediates.
1439 assert (isParsingInlineAsm() && "Unexpected field name!");
1440
1441 unsigned DotDisp;
1442 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1443 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1444 DotDisp)) {
1445 Err = "Unable to lookup field reference!";
1446 return true;
1447 }
1448 DotDispVal = DotDisp;
Chad Rosier911c1f32012-10-25 17:37:43 +00001449 } else {
1450 Err = "Unexpected token type!";
1451 return true;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001452 }
Chad Rosier911c1f32012-10-25 17:37:43 +00001453
Chad Rosier240b7b92012-10-25 21:51:10 +00001454 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1455 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1456 unsigned Len = DotDispStr.size();
1457 unsigned Val = OrigDispVal + DotDispVal;
1458 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1459 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001460 }
1461
1462 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1463 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001464}
1465
Chad Rosier91c82662012-10-24 17:22:29 +00001466/// Parse the 'offset' operator. This operator is used to specify the
1467/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001468X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001469 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001470 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001471 Parser.Lex(); // Eat offset.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001472 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier91c82662012-10-24 17:22:29 +00001473
Chad Rosier91c82662012-10-24 17:22:29 +00001474 const MCExpr *Val;
Chad Rosier18785852013-04-09 20:58:48 +00001475 SMLoc Start = Tok.getLoc(), End;
Chad Rosierae7ecd62013-04-11 23:37:34 +00001476 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001477 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosier58593562012-10-26 18:32:44 +00001478 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosier91c82662012-10-24 17:22:29 +00001479
Chad Rosierae7ecd62013-04-11 23:37:34 +00001480 const MCExpr *Disp = 0;
1481 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1482 return Err;
1483
Chad Rosiere2f03772012-10-26 16:09:20 +00001484 // Don't emit the offset operator.
1485 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1486
Chad Rosier91c82662012-10-24 17:22:29 +00001487 // The offset operator will have an 'r' constraint, thus we need to create
1488 // register operand to ensure proper matching. Just pick a GPR based on
1489 // the size of a pointer.
1490 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiera4bc9432013-01-10 22:10:27 +00001491 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosierae7ecd62013-04-11 23:37:34 +00001492 OffsetOfLoc, Identifier);
Devang Patel41b9dde2012-01-17 18:00:18 +00001493}
1494
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001495enum IntelOperatorKind {
1496 IOK_LENGTH,
1497 IOK_SIZE,
1498 IOK_TYPE
1499};
1500
1501/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1502/// returns the number of elements in an array. It returns the value 1 for
1503/// non-array variables. The SIZE operator returns the size of a C or C++
1504/// variable. A variable's size is the product of its LENGTH and TYPE. The
1505/// TYPE operator returns the size of a C or C++ type or variable. If the
1506/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001507X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001508 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001509 SMLoc TypeLoc = Tok.getLoc();
1510 Parser.Lex(); // Eat operator.
Chad Rosier18785852013-04-09 20:58:48 +00001511 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier11c42f22012-10-26 18:04:20 +00001512
Chad Rosier11c42f22012-10-26 18:04:20 +00001513 const MCExpr *Val;
Chad Rosierb67f8052013-04-11 23:57:04 +00001514 AsmToken StartTok = Tok;
Chad Rosier18785852013-04-09 20:58:48 +00001515 SMLoc Start = Tok.getLoc(), End;
Chad Rosierb67f8052013-04-11 23:57:04 +00001516 StringRef Identifier = Tok.getString();
Chad Rosier1863f4f2013-04-10 17:35:30 +00001517 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosierb67f8052013-04-11 23:57:04 +00001518 return ErrorOperand(Start, "Unable to parse expression!");
1519
1520 const MCExpr *Disp = 0;
1521 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
1522 return Err;
Chad Rosier11c42f22012-10-26 18:04:20 +00001523
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001524 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosier11c42f22012-10-26 18:04:20 +00001525 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1526 const MCSymbol &Sym = SymRef->getSymbol();
1527 // FIXME: The SemaLookup will fail if the name is anything other then an
1528 // identifier.
1529 // FIXME: Pass a valid SMLoc.
Chad Rosiera4bc9432013-01-10 22:10:27 +00001530 bool IsVarDecl;
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001531 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1532 Size, Type, IsVarDecl))
Chad Rosierb67f8052013-04-11 23:57:04 +00001533 // FIXME: We don't warn on variables with namespace alias qualifiers
1534 // because support still needs to be added in the frontend.
1535 if (Identifier.equals(StartTok.getString()))
1536 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001537 }
1538 unsigned CVal;
1539 switch(OpKind) {
1540 default: llvm_unreachable("Unexpected operand kind!");
1541 case IOK_LENGTH: CVal = Length; break;
1542 case IOK_SIZE: CVal = Size; break;
1543 case IOK_TYPE: CVal = Type; break;
Chad Rosier11c42f22012-10-26 18:04:20 +00001544 }
1545
1546 // Rewrite the type operator and the C or C++ type or variable in terms of an
1547 // immediate. E.g. TYPE foo -> $$4
1548 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001549 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001550
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001551 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001552 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001553}
1554
Devang Patel41b9dde2012-01-17 18:00:18 +00001555X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001556 const AsmToken &Tok = Parser.getTok();
1557 SMLoc Start = Tok.getLoc(), End;
1558 StringRef AsmTokStr = Tok.getString();
Chad Rosier91c82662012-10-24 17:22:29 +00001559
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001560 // Offset, length, type and size operators.
1561 if (isParsingInlineAsm()) {
1562 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001563 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001564 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001565 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001566 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001567 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001568 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001569 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001570 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001571
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001572 // Immediate.
Devang Patel41b9dde2012-01-17 18:00:18 +00001573 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1574 getLexer().is(AsmToken::Minus)) {
1575 const MCExpr *Val;
Chad Rosier1530ba52013-03-27 21:49:56 +00001576 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001577 if (!getParser().parseExpression(Val, End)) {
Chad Rosierf3c04f62013-03-19 21:58:18 +00001578 if (isParsingInlineAsm())
1579 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosier1530ba52013-03-27 21:49:56 +00001580 // Immediate.
1581 if (getLexer().isNot(AsmToken::LBrac))
1582 return X86Operand::CreateImm(Val, Start, End);
1583
1584 // Only positive immediates are valid.
1585 if (!isInteger) {
Chad Rosier70f47592013-04-10 20:07:47 +00001586 Error(Tok.getLoc(), "expected a positive immediate "
Chad Rosier1530ba52013-03-27 21:49:56 +00001587 "displacement before bracketed expr.");
1588 return 0;
1589 }
1590
1591 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1592 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1593 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Patel41b9dde2012-01-17 18:00:18 +00001594 }
1595 }
1596
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001597 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001598 unsigned RegNo = 0;
1599 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001600 // If this is a segment register followed by a ':', then this is the start
1601 // of a memory reference, otherwise this is a normal register reference.
1602 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001603 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001604
1605 getParser().Lex(); // Eat the colon.
Chad Rosier1530ba52013-03-27 21:49:56 +00001606 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001607 }
1608
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001609 // Memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001610 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001611}
1612
Devang Patel4a6e7782012-01-12 18:03:40 +00001613X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001614 switch (getLexer().getKind()) {
1615 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001616 // Parse a memory operand with no segment register.
1617 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001618 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001619 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001620 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001621 SMLoc Start, End;
1622 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001623 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001624 Error(Start, "%eiz and %riz can only be used as index registers",
1625 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001626 return 0;
1627 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001628
Chris Lattnerb9270732010-04-17 18:56:34 +00001629 // If this is a segment register followed by a ':', then this is the start
1630 // of a memory reference, otherwise this is a normal register reference.
1631 if (getLexer().isNot(AsmToken::Colon))
1632 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001633
Chris Lattnerb9270732010-04-17 18:56:34 +00001634 getParser().Lex(); // Eat the colon.
1635 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001636 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001637 case AsmToken::Dollar: {
1638 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001639 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001640 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001641 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001642 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001643 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001644 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001645 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001646 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001647}
1648
Chris Lattnerb9270732010-04-17 18:56:34 +00001649/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1650/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001651X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001652
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001653 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1654 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001655 // only way to do this without lookahead is to eat the '(' and see what is
1656 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001657 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001658 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001659 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001660 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001661
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001662 // After parsing the base expression we could either have a parenthesized
1663 // memory address or not. If not, return now. If so, eat the (.
1664 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001665 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001666 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001667 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001668 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001669 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001670
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001671 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001672 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001673 } else {
1674 // Okay, we have a '('. We don't know if this is an expression or not, but
1675 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001676 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001677 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001678
Kevin Enderby7d912182009-09-03 17:15:07 +00001679 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001680 // Nothing to do here, fall into the code below with the '(' part of the
1681 // memory operand consumed.
1682 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001683 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001684
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001685 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001686 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001687 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001688
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001689 // After parsing the base expression we could either have a parenthesized
1690 // memory address or not. If not, return now. If so, eat the (.
1691 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001692 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001693 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001694 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001695 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001696 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001697
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001698 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001699 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001700 }
1701 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001702
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001703 // If we reached here, then we just ate the ( of the memory operand. Process
1704 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001705 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001706 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001707
Chris Lattner0c2538f2010-01-15 18:51:29 +00001708 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001709 SMLoc StartLoc, EndLoc;
1710 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001711 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001712 Error(StartLoc, "eiz and riz can only be used as index registers",
1713 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001714 return 0;
1715 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001716 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001717
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001718 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001719 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001720 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001721
1722 // Following the comma we should have either an index register, or a scale
1723 // value. We don't support the later form, but we want to parse it
1724 // correctly.
1725 //
1726 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001727 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001728 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001729 SMLoc L;
1730 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001731
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001732 if (getLexer().isNot(AsmToken::RParen)) {
1733 // Parse the scale amount:
1734 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001735 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001736 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001737 "expected comma in scale expression");
1738 return 0;
1739 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001740 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001741
1742 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001743 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001744
1745 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001746 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001747 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001748 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001749 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001750
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001751 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001752 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1753 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1754 return 0;
1755 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001756 Scale = (unsigned)ScaleVal;
1757 }
1758 }
1759 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001760 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001761 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001762 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001763
1764 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001765 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001766 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001767
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001768 if (Value != 1)
1769 Warning(Loc, "scale factor without index register is ignored");
1770 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001771 }
1772 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001773
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001774 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001775 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001776 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001777 return 0;
1778 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001779 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001780 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001781
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001782 // If we have both a base register and an index register make sure they are
1783 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001784 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001785 if (BaseReg != 0 && IndexReg != 0) {
1786 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001787 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1788 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001789 IndexReg != X86::RIZ) {
1790 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1791 return 0;
1792 }
1793 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001794 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1795 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001796 IndexReg != X86::EIZ){
1797 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1798 return 0;
1799 }
1800 }
1801
Chris Lattner015cfb12010-01-15 19:33:43 +00001802 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1803 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001804}
1805
Devang Patel4a6e7782012-01-12 18:03:40 +00001806bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001807ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001808 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001809 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001810 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001811
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001812 // FIXME: Hack to recognize setneb as setne.
1813 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1814 PatchedName != "setb" && PatchedName != "setnb")
1815 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001816
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001817 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1818 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001819 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001820 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1821 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001822 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001823 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001824 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001825 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001826 .Case("eq", 0x00)
1827 .Case("lt", 0x01)
1828 .Case("le", 0x02)
1829 .Case("unord", 0x03)
1830 .Case("neq", 0x04)
1831 .Case("nlt", 0x05)
1832 .Case("nle", 0x06)
1833 .Case("ord", 0x07)
1834 /* AVX only from here */
1835 .Case("eq_uq", 0x08)
1836 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001837 .Case("ngt", 0x0A)
1838 .Case("false", 0x0B)
1839 .Case("neq_oq", 0x0C)
1840 .Case("ge", 0x0D)
1841 .Case("gt", 0x0E)
1842 .Case("true", 0x0F)
1843 .Case("eq_os", 0x10)
1844 .Case("lt_oq", 0x11)
1845 .Case("le_oq", 0x12)
1846 .Case("unord_s", 0x13)
1847 .Case("neq_us", 0x14)
1848 .Case("nlt_uq", 0x15)
1849 .Case("nle_uq", 0x16)
1850 .Case("ord_s", 0x17)
1851 .Case("eq_us", 0x18)
1852 .Case("nge_uq", 0x19)
1853 .Case("ngt_uq", 0x1A)
1854 .Case("false_os", 0x1B)
1855 .Case("neq_os", 0x1C)
1856 .Case("ge_oq", 0x1D)
1857 .Case("gt_oq", 0x1E)
1858 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001859 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001860 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001861 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1862 getParser().getContext());
1863 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001864 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001865 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001866 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001867 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001868 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001869 } else {
1870 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001871 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001872 }
1873 }
1874 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001875
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001876 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001877
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001878 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001879 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001880
Chris Lattner086a83a2010-09-08 05:17:37 +00001881 // Determine whether this is an instruction prefix.
1882 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001883 Name == "lock" || Name == "rep" ||
1884 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001885 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001886 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001887
1888
Chris Lattner086a83a2010-09-08 05:17:37 +00001889 // This does the actual operand parsing. Don't parse any more if we have a
1890 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1891 // just want to parse the "lock" as the first instruction and the "incl" as
1892 // the next one.
1893 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001894
1895 // Parse '*' modifier.
1896 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001897 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001898 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001899 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001900 }
1901
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001902 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001903 if (X86Operand *Op = ParseOperand())
1904 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001905 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001906 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001907 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001908 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001909
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001910 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001911 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001912
1913 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001914 if (X86Operand *Op = ParseOperand())
1915 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001916 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001917 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001918 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001919 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001920 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001921
Chris Lattnera2a9d162010-09-11 16:18:25 +00001922 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001923 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001924 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001925 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001926 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001927 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001928
Chris Lattner086a83a2010-09-08 05:17:37 +00001929 if (getLexer().is(AsmToken::EndOfStatement))
1930 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001931 else if (isPrefix && getLexer().is(AsmToken::Slash))
1932 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001933
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001934 if (ExtraImmOp && isParsingIntelSyntax())
1935 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1936
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001937 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1938 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1939 // documented form in various unofficial manuals, so a lot of code uses it.
1940 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1941 Operands.size() == 3) {
1942 X86Operand &Op = *(X86Operand*)Operands.back();
1943 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1944 isa<MCConstantExpr>(Op.Mem.Disp) &&
1945 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1946 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1947 SMLoc Loc = Op.getEndLoc();
1948 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1949 delete &Op;
1950 }
1951 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001952 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1953 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1954 Operands.size() == 3) {
1955 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1956 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1957 isa<MCConstantExpr>(Op.Mem.Disp) &&
1958 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1959 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1960 SMLoc Loc = Op.getEndLoc();
1961 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1962 delete &Op;
1963 }
1964 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001965 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1966 if (Name.startswith("ins") && Operands.size() == 3 &&
1967 (Name == "insb" || Name == "insw" || Name == "insl")) {
1968 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1969 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1970 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1971 Operands.pop_back();
1972 Operands.pop_back();
1973 delete &Op;
1974 delete &Op2;
1975 }
1976 }
1977
1978 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1979 if (Name.startswith("outs") && Operands.size() == 3 &&
1980 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1981 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1982 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1983 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1984 Operands.pop_back();
1985 Operands.pop_back();
1986 delete &Op;
1987 delete &Op2;
1988 }
1989 }
1990
1991 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1992 if (Name.startswith("movs") && Operands.size() == 3 &&
1993 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001994 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001995 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1996 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1997 if (isSrcOp(Op) && isDstOp(Op2)) {
1998 Operands.pop_back();
1999 Operands.pop_back();
2000 delete &Op;
2001 delete &Op2;
2002 }
2003 }
2004 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
2005 if (Name.startswith("lods") && Operands.size() == 3 &&
2006 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002007 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002008 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2009 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2010 if (isSrcOp(*Op1) && Op2->isReg()) {
2011 const char *ins;
2012 unsigned reg = Op2->getReg();
2013 bool isLods = Name == "lods";
2014 if (reg == X86::AL && (isLods || Name == "lodsb"))
2015 ins = "lodsb";
2016 else if (reg == X86::AX && (isLods || Name == "lodsw"))
2017 ins = "lodsw";
2018 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
2019 ins = "lodsl";
2020 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
2021 ins = "lodsq";
2022 else
2023 ins = NULL;
2024 if (ins != NULL) {
2025 Operands.pop_back();
2026 Operands.pop_back();
2027 delete Op1;
2028 delete Op2;
2029 if (Name != ins)
2030 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2031 }
2032 }
2033 }
2034 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
2035 if (Name.startswith("stos") && Operands.size() == 3 &&
2036 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00002037 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00002038 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2039 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
2040 if (isDstOp(*Op2) && Op1->isReg()) {
2041 const char *ins;
2042 unsigned reg = Op1->getReg();
2043 bool isStos = Name == "stos";
2044 if (reg == X86::AL && (isStos || Name == "stosb"))
2045 ins = "stosb";
2046 else if (reg == X86::AX && (isStos || Name == "stosw"))
2047 ins = "stosw";
2048 else if (reg == X86::EAX && (isStos || Name == "stosl"))
2049 ins = "stosl";
2050 else if (reg == X86::RAX && (isStos || Name == "stosq"))
2051 ins = "stosq";
2052 else
2053 ins = NULL;
2054 if (ins != NULL) {
2055 Operands.pop_back();
2056 Operands.pop_back();
2057 delete Op1;
2058 delete Op2;
2059 if (Name != ins)
2060 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
2061 }
2062 }
2063 }
2064
Chris Lattner4bd21712010-09-15 04:33:27 +00002065 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00002066 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00002067 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00002068 Name.startswith("shl") || Name.startswith("sal") ||
2069 Name.startswith("rcl") || Name.startswith("rcr") ||
2070 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002071 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002072 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00002073 // Intel syntax
2074 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2075 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002076 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2077 delete Operands[2];
2078 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002079 }
2080 } else {
2081 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2082 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002083 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2084 delete Operands[1];
2085 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002086 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002087 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002088 }
Chad Rosier51afe632012-06-27 22:34:28 +00002089
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002090 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2091 // instalias with an immediate operand yet.
2092 if (Name == "int" && Operands.size() == 2) {
2093 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2094 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2095 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2096 delete Operands[1];
2097 Operands.erase(Operands.begin() + 1);
2098 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2099 }
2100 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002101
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002102 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002103}
2104
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002105static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2106 bool isCmp) {
2107 MCInst TmpInst;
2108 TmpInst.setOpcode(Opcode);
2109 if (!isCmp)
2110 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2111 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2112 TmpInst.addOperand(Inst.getOperand(0));
2113 Inst = TmpInst;
2114 return true;
2115}
2116
2117static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2118 bool isCmp = false) {
2119 if (!Inst.getOperand(0).isImm() ||
2120 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2121 return false;
2122
2123 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2124}
2125
2126static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2127 bool isCmp = false) {
2128 if (!Inst.getOperand(0).isImm() ||
2129 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2130 return false;
2131
2132 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2133}
2134
2135static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2136 bool isCmp = false) {
2137 if (!Inst.getOperand(0).isImm() ||
2138 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2139 return false;
2140
2141 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2142}
2143
Devang Patel4a6e7782012-01-12 18:03:40 +00002144bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002145processInstruction(MCInst &Inst,
2146 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2147 switch (Inst.getOpcode()) {
2148 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002149 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2150 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2151 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2152 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2153 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2154 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2155 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2156 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2157 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2158 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2159 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2160 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2161 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2162 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2163 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2164 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2165 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2166 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002167 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2168 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2169 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2170 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2171 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2172 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002173 }
Devang Patelde47cce2012-01-18 22:42:29 +00002174}
2175
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002176static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002177bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002178MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002179 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002180 MCStreamer &Out, unsigned &ErrorInfo,
2181 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002182 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002183 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2184 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosier3d4bc622012-08-21 19:36:59 +00002185 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002186
Chris Lattnera63292a2010-09-29 01:50:45 +00002187 // First, handle aliases that expand to multiple instructions.
2188 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002189 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002190 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002191 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002192 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002193 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002194 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002195 MCInst Inst;
2196 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002197 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002198 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002199 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002200
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002201 const char *Repl =
2202 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002203 .Case("finit", "fninit")
2204 .Case("fsave", "fnsave")
2205 .Case("fstcw", "fnstcw")
2206 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002207 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002208 .Case("fstsw", "fnstsw")
2209 .Case("fstsww", "fnstsw")
2210 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002211 .Default(0);
2212 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002213 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002214 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002215 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002216
Chris Lattner628fbec2010-09-06 21:54:15 +00002217 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002218 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002219
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002220 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002221 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002222 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002223 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002224 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002225 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002226 // Some instructions need post-processing to, for example, tweak which
2227 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002228 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002229 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002230 while (processInstruction(Inst, Operands))
2231 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002232
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002233 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002234 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002235 Out.EmitInstruction(Inst);
2236 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002237 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002238 case Match_MissingFeature: {
2239 assert(ErrorInfo && "Unknown missing feature!");
2240 // Special case the error message for the very common case where only
2241 // a single subtarget feature is missing.
2242 std::string Msg = "instruction requires:";
2243 unsigned Mask = 1;
2244 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2245 if (ErrorInfo & Mask) {
2246 Msg += " ";
2247 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2248 }
2249 Mask <<= 1;
2250 }
2251 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2252 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002253 case Match_InvalidOperand:
2254 WasOriginallyInvalidOperand = true;
2255 break;
2256 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002257 break;
2258 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002259
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002260 // FIXME: Ideally, we would only attempt suffix matches for things which are
2261 // valid prefixes, and we could just infer the right unambiguous
2262 // type. However, that requires substantially more matcher support than the
2263 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002264
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002265 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002266 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002267 SmallString<16> Tmp;
2268 Tmp += Base;
2269 Tmp += ' ';
2270 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002271
Chris Lattnerfab94132010-11-06 18:28:02 +00002272 // If this instruction starts with an 'f', then it is a floating point stack
2273 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2274 // 80-bit floating point, which use the suffixes s,l,t respectively.
2275 //
2276 // Otherwise, we assume that this may be an integer instruction, which comes
2277 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2278 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002279
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002280 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002281 Tmp[Base.size()] = Suffixes[0];
2282 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002283 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002284 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002285
Chad Rosier2f480a82012-10-12 22:53:36 +00002286 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2287 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002288 // If this returned as a missing feature failure, remember that.
2289 if (Match1 == Match_MissingFeature)
2290 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002291 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002292 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2293 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002294 // If this returned as a missing feature failure, remember that.
2295 if (Match2 == Match_MissingFeature)
2296 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002297 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002298 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2299 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002300 // If this returned as a missing feature failure, remember that.
2301 if (Match3 == Match_MissingFeature)
2302 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002303 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002304 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2305 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002306 // If this returned as a missing feature failure, remember that.
2307 if (Match4 == Match_MissingFeature)
2308 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002309
2310 // Restore the old token.
2311 Op->setTokenValue(Base);
2312
2313 // If exactly one matched, then we treat that as a successful match (and the
2314 // instruction will already have been filled in correctly, since the failing
2315 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002316 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002317 (Match1 == Match_Success) + (Match2 == Match_Success) +
2318 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002319 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002320 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002321 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002322 Out.EmitInstruction(Inst);
2323 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002324 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002325 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002326
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002327 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002328
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002329 // If we had multiple suffix matches, then identify this as an ambiguous
2330 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002331 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002332 char MatchChars[4];
2333 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002334 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2335 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2336 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2337 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002338
2339 SmallString<126> Msg;
2340 raw_svector_ostream OS(Msg);
2341 OS << "ambiguous instructions require an explicit suffix (could be ";
2342 for (unsigned i = 0; i != NumMatches; ++i) {
2343 if (i != 0)
2344 OS << ", ";
2345 if (i + 1 == NumMatches)
2346 OS << "or ";
2347 OS << "'" << Base << MatchChars[i] << "'";
2348 }
2349 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002350 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002351 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002352 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002353
Chris Lattner628fbec2010-09-06 21:54:15 +00002354 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002355
Chris Lattner628fbec2010-09-06 21:54:15 +00002356 // If all of the instructions reported an invalid mnemonic, then the original
2357 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002358 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2359 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002360 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002361 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002362 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002363 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002364 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002365 }
2366
2367 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002368 if (ErrorInfo != ~0U) {
2369 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002370 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002371 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002372
Chad Rosier49963552012-10-13 00:26:04 +00002373 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002374 if (Operand->getStartLoc().isValid()) {
2375 SMRange OperandRange = Operand->getLocRange();
2376 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002377 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002378 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002379 }
2380
Chad Rosier3d4bc622012-08-21 19:36:59 +00002381 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002382 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002383 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002384
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002385 // If one instruction matched with a missing feature, report this as a
2386 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002387 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2388 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002389 std::string Msg = "instruction requires:";
2390 unsigned Mask = 1;
2391 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2392 if (ErrorInfoMissingFeature & Mask) {
2393 Msg += " ";
2394 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2395 }
2396 Mask <<= 1;
2397 }
2398 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002399 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002400
Chris Lattner628fbec2010-09-06 21:54:15 +00002401 // If one instruction matched with an invalid operand, report this as an
2402 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002403 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2404 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002405 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002406 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002407 return true;
2408 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002409
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002410 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002411 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002412 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002413 return true;
2414}
2415
2416
Devang Patel4a6e7782012-01-12 18:03:40 +00002417bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002418 StringRef IDVal = DirectiveID.getIdentifier();
2419 if (IDVal == ".word")
2420 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002421 else if (IDVal.startswith(".code"))
2422 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002423 else if (IDVal.startswith(".att_syntax")) {
2424 getParser().setAssemblerDialect(0);
2425 return false;
2426 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002427 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002428 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2429 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002430 // FIXME : Handle noprefix
2431 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002432 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002433 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002434 }
2435 return false;
2436 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002437 return true;
2438}
2439
2440/// ParseDirectiveWord
2441/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002442bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2444 for (;;) {
2445 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002446 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002447 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002448
Eric Christopherbf7bc492013-01-09 03:52:05 +00002449 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002450
Chris Lattner72c0b592010-10-30 17:38:55 +00002451 if (getLexer().is(AsmToken::EndOfStatement))
2452 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002453
Chris Lattner72c0b592010-10-30 17:38:55 +00002454 // FIXME: Improve diagnostic.
2455 if (getLexer().isNot(AsmToken::Comma))
2456 return Error(L, "unexpected token in directive");
2457 Parser.Lex();
2458 }
2459 }
Chad Rosier51afe632012-06-27 22:34:28 +00002460
Chris Lattner72c0b592010-10-30 17:38:55 +00002461 Parser.Lex();
2462 return false;
2463}
2464
Evan Cheng481ebb02011-07-27 00:38:12 +00002465/// ParseDirectiveCode
2466/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002467bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002468 if (IDVal == ".code32") {
2469 Parser.Lex();
2470 if (is64BitMode()) {
2471 SwitchMode();
2472 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2473 }
2474 } else if (IDVal == ".code64") {
2475 Parser.Lex();
2476 if (!is64BitMode()) {
2477 SwitchMode();
2478 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2479 }
2480 } else {
2481 return Error(L, "unexpected directive " + IDVal);
2482 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002483
Evan Cheng481ebb02011-07-27 00:38:12 +00002484 return false;
2485}
Chris Lattner72c0b592010-10-30 17:38:55 +00002486
Daniel Dunbar71475772009-07-17 20:42:00 +00002487// Force static initialization.
2488extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002489 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2490 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002491}
Daniel Dunbar00331992009-07-29 00:02:19 +00002492
Chris Lattner3e4582a2010-09-06 19:11:01 +00002493#define GET_REGISTER_MATCHER
2494#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002495#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002496#include "X86GenAsmMatcher.inc"