blob: 6f026cd3798e0245b3dee60d7f5ab56d90dbf081 [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 Rosier7ca135b2013-03-19 21:11:56 +000070 X86Operand *CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start, SMLoc End,
Chad Rosiere81309b2013-04-09 17:53:49 +000071 SMLoc SizeDirLoc, unsigned Size,
72 StringRef SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +000073
Chad Rosier911c1f32012-10-25 17:37:43 +000074 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
75 SmallString<64> &Err);
Chad Rosier5dcb4662012-10-24 22:21:50 +000076
Kevin Enderbyce4bec82009-09-10 20:51:44 +000077 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Cheng481ebb02011-07-27 00:38:12 +000078 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderbyce4bec82009-09-10 20:51:44 +000079
Devang Patelde47cce2012-01-18 22:42:29 +000080 bool processInstruction(MCInst &Inst,
81 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
82
Chad Rosier49963552012-10-13 00:26:04 +000083 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +000084 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +000085 MCStreamer &Out, unsigned &ErrorInfo,
86 bool MatchingInlineAsm);
Chad Rosier9cb988f2012-08-09 22:04:55 +000087
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000088 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby1ef22f32012-03-13 19:47:55 +000089 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000090 bool isSrcOp(X86Operand &Op);
91
Kevin Enderby1ef22f32012-03-13 19:47:55 +000092 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
93 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +000094 bool isDstOp(X86Operand &Op);
95
Evan Chengc5e6d2f2011-07-11 03:57:24 +000096 bool is64BitMode() const {
Evan Cheng4d1ca962011-07-08 01:53:10 +000097 // FIXME: Can tablegen auto-generate this?
Evan Cheng91111d22011-07-09 05:47:46 +000098 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Cheng4d1ca962011-07-08 01:53:10 +000099 }
Evan Cheng481ebb02011-07-27 00:38:12 +0000100 void SwitchMode() {
101 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
102 setAvailableFeatures(FB);
103 }
Evan Cheng4d1ca962011-07-08 01:53:10 +0000104
Daniel Dunbareefe8612010-07-19 05:44:09 +0000105 /// @name Auto-generated Matcher Functions
106 /// {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000107
Chris Lattner3e4582a2010-09-06 19:11:01 +0000108#define GET_ASSEMBLER_HEADER
109#include "X86GenAsmMatcher.inc"
Michael J. Spencer530ce852010-10-09 11:00:50 +0000110
Daniel Dunbar00331992009-07-29 00:02:19 +0000111 /// }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000112
113public:
Devang Patel4a6e7782012-01-12 18:03:40 +0000114 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosierf0e87202012-10-25 20:41:34 +0000115 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencer530ce852010-10-09 11:00:50 +0000116
Daniel Dunbareefe8612010-07-19 05:44:09 +0000117 // Initialize the set of available features.
Evan Cheng91111d22011-07-09 05:47:46 +0000118 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbareefe8612010-07-19 05:44:09 +0000119 }
Roman Divacky36b1b472011-01-27 17:14:22 +0000120 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000121
Chad Rosierf0e87202012-10-25 20:41:34 +0000122 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
123 SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000124 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyce4bec82009-09-10 20:51:44 +0000125
126 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000127
128 bool isParsingIntelSyntax() {
Devang Patela173ee52012-01-31 18:14:05 +0000129 return getParser().getAssemblerDialect();
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000130 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000131};
Chris Lattner4eb9df02009-07-29 06:33:53 +0000132} // end anonymous namespace
133
Sean Callanan86c11812010-01-23 00:40:33 +0000134/// @name Auto-generated Match Functions
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000135/// {
Sean Callanan86c11812010-01-23 00:40:33 +0000136
Chris Lattner60db0a62010-02-09 00:34:28 +0000137static unsigned MatchRegisterName(StringRef Name);
Sean Callanan86c11812010-01-23 00:40:33 +0000138
139/// }
Chris Lattner4eb9df02009-07-29 06:33:53 +0000140
Craig Topper6bf3ed42012-07-18 04:59:16 +0000141static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelde47cce2012-01-18 22:42:29 +0000142 return (( Value <= 0x000000000000007FULL)||
143 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
144 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
145}
146
147static bool isImmSExti32i8Value(uint64_t Value) {
148 return (( Value <= 0x000000000000007FULL)||
149 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
150 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
151}
152
153static bool isImmZExtu32u8Value(uint64_t Value) {
154 return (Value <= 0x00000000000000FFULL);
155}
156
157static bool isImmSExti64i8Value(uint64_t Value) {
158 return (( Value <= 0x000000000000007FULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000159 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000160}
161
162static bool isImmSExti64i32Value(uint64_t Value) {
163 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper6bf3ed42012-07-18 04:59:16 +0000164 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelde47cce2012-01-18 22:42:29 +0000165}
Chris Lattner4eb9df02009-07-29 06:33:53 +0000166namespace {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000167
168/// X86Operand - Instances of this class represent a parsed X86 machine
169/// instruction.
Chris Lattner872501b2010-01-14 21:20:55 +0000170struct X86Operand : public MCParsedAsmOperand {
Chris Lattner86e61532010-01-15 19:06:59 +0000171 enum KindTy {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000172 Token,
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000173 Register,
174 Immediate,
Chad Rosier985b1dc2012-10-02 23:38:50 +0000175 Memory
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000176 } Kind;
177
Chris Lattner0c2538f2010-01-15 18:51:29 +0000178 SMLoc StartLoc, EndLoc;
Chad Rosier37e755c2012-10-23 17:43:43 +0000179 SMLoc OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000180 StringRef SymName;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000181 bool AddressOf;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000182
Eric Christopher8996c5d2013-03-15 00:42:55 +0000183 struct TokOp {
184 const char *Data;
185 unsigned Length;
186 };
187
188 struct RegOp {
189 unsigned RegNo;
190 };
191
192 struct ImmOp {
193 const MCExpr *Val;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000194 };
195
196 struct MemOp {
197 unsigned SegReg;
198 const MCExpr *Disp;
199 unsigned BaseReg;
200 unsigned IndexReg;
201 unsigned Scale;
202 unsigned Size;
Eric Christopher8996c5d2013-03-15 00:42:55 +0000203 };
204
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000205 union {
Eric Christopher8996c5d2013-03-15 00:42:55 +0000206 struct TokOp Tok;
207 struct RegOp Reg;
208 struct ImmOp Imm;
209 struct MemOp Mem;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +0000210 };
Daniel Dunbar71475772009-07-17 20:42:00 +0000211
Chris Lattner015cfb12010-01-15 19:33:43 +0000212 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner86e61532010-01-15 19:06:59 +0000213 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000214
Chad Rosiere81309b2013-04-09 17:53:49 +0000215 StringRef getSymName() { return SymName; }
216
Chris Lattner86e61532010-01-15 19:06:59 +0000217 /// getStartLoc - Get the location of the first token of this operand.
218 SMLoc getStartLoc() const { return StartLoc; }
219 /// getEndLoc - Get the location of the last token of this operand.
220 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier3d325cf2012-09-21 21:08:46 +0000221 /// getLocRange - Get the range between the first and last token of this
222 /// operand.
Chris Lattnera3a06812011-10-16 04:47:35 +0000223 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier37e755c2012-10-23 17:43:43 +0000224 /// getOffsetOfLoc - Get the location of the offset operator.
225 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner86e61532010-01-15 19:06:59 +0000226
Jim Grosbach602aa902011-07-13 15:34:57 +0000227 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarebace222010-08-11 06:37:04 +0000228
Daniel Dunbare10787e2009-08-07 08:26:05 +0000229 StringRef getToken() const {
230 assert(Kind == Token && "Invalid access!");
231 return StringRef(Tok.Data, Tok.Length);
232 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +0000233 void setTokenValue(StringRef Value) {
234 assert(Kind == Token && "Invalid access!");
235 Tok.Data = Value.data();
236 Tok.Length = Value.size();
237 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000238
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000239 unsigned getReg() const {
240 assert(Kind == Register && "Invalid access!");
241 return Reg.RegNo;
242 }
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000243
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000244 const MCExpr *getImm() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000245 assert(Kind == Immediate && "Invalid access!");
246 return Imm.Val;
247 }
248
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000249 const MCExpr *getMemDisp() const {
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000250 assert(Kind == Memory && "Invalid access!");
251 return Mem.Disp;
252 }
253 unsigned getMemSegReg() const {
254 assert(Kind == Memory && "Invalid access!");
255 return Mem.SegReg;
256 }
257 unsigned getMemBaseReg() const {
258 assert(Kind == Memory && "Invalid access!");
259 return Mem.BaseReg;
260 }
261 unsigned getMemIndexReg() const {
262 assert(Kind == Memory && "Invalid access!");
263 return Mem.IndexReg;
264 }
265 unsigned getMemScale() const {
266 assert(Kind == Memory && "Invalid access!");
267 return Mem.Scale;
268 }
269
Daniel Dunbar541efcc2009-08-08 07:50:56 +0000270 bool isToken() const {return Kind == Token; }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000271
272 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000273
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000274 bool isImmSExti16i8() const {
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000275 if (!isImm())
276 return false;
277
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000278 // If this isn't a constant expr, just assume it fits and let relaxation
279 // handle it.
280 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
281 if (!CE)
282 return true;
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000283
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000284 // Otherwise, check the value is in a range that makes sense for this
285 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000286 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar8e33cb22009-08-09 07:20:21 +0000287 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000288 bool isImmSExti32i8() const {
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000289 if (!isImm())
290 return false;
291
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000292 // If this isn't a constant expr, just assume it fits and let relaxation
293 // handle it.
294 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
295 if (!CE)
296 return true;
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000297
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000298 // Otherwise, check the value is in a range that makes sense for this
299 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000300 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000301 }
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000302 bool isImmZExtu32u8() const {
303 if (!isImm())
304 return false;
305
306 // If this isn't a constant expr, just assume it fits and let relaxation
307 // handle it.
308 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
309 if (!CE)
310 return true;
311
312 // Otherwise, check the value is in a range that makes sense for this
313 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000314 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderby5ef6c452011-07-27 23:01:50 +0000315 }
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000316 bool isImmSExti64i8() const {
317 if (!isImm())
318 return false;
319
320 // If this isn't a constant expr, just assume it fits and let relaxation
321 // handle it.
322 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
323 if (!CE)
324 return true;
325
326 // Otherwise, check the value is in a range that makes sense for this
327 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000328 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbarb52fcd62010-05-22 21:02:33 +0000329 }
330 bool isImmSExti64i32() const {
331 if (!isImm())
332 return false;
333
334 // If this isn't a constant expr, just assume it fits and let relaxation
335 // handle it.
336 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
337 if (!CE)
338 return true;
339
340 // Otherwise, check the value is in a range that makes sense for this
341 // extension.
Devang Patelde47cce2012-01-18 22:42:29 +0000342 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar61655aa2010-05-20 20:20:39 +0000343 }
344
Chad Rosier5bca3f92012-10-22 19:50:35 +0000345 bool isOffsetOf() const {
Chad Rosier91c82662012-10-24 17:22:29 +0000346 return OffsetOfLoc.getPointer();
Chad Rosier5bca3f92012-10-22 19:50:35 +0000347 }
348
Chad Rosiera4bc9432013-01-10 22:10:27 +0000349 bool needAddressOf() const {
350 return AddressOf;
351 }
352
Daniel Dunbare10787e2009-08-07 08:26:05 +0000353 bool isMem() const { return Kind == Memory; }
Chad Rosier51afe632012-06-27 22:34:28 +0000354 bool isMem8() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelfc6be102012-01-12 01:51:42 +0000356 }
Chad Rosier51afe632012-06-27 22:34:28 +0000357 bool isMem16() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelfc6be102012-01-12 01:51:42 +0000359 }
Chad Rosier51afe632012-06-27 22:34:28 +0000360 bool isMem32() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelfc6be102012-01-12 01:51:42 +0000362 }
Chad Rosier51afe632012-06-27 22:34:28 +0000363 bool isMem64() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000364 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelfc6be102012-01-12 01:51:42 +0000365 }
Chad Rosier51afe632012-06-27 22:34:28 +0000366 bool isMem80() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000367 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelfc6be102012-01-12 01:51:42 +0000368 }
Chad Rosier51afe632012-06-27 22:34:28 +0000369 bool isMem128() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000370 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelfc6be102012-01-12 01:51:42 +0000371 }
Chad Rosier51afe632012-06-27 22:34:28 +0000372 bool isMem256() const {
Chad Rosier985b1dc2012-10-02 23:38:50 +0000373 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelfc6be102012-01-12 01:51:42 +0000374 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000375
Craig Topper01deb5f2012-07-18 04:11:12 +0000376 bool isMemVX32() const {
377 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
378 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
379 }
380 bool isMemVY32() const {
381 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
382 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
383 }
384 bool isMemVX64() const {
385 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
386 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
387 }
388 bool isMemVY64() const {
389 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
390 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
391 }
392
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000393 bool isAbsMem() const {
394 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar3184f222010-02-02 21:44:16 +0000395 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000396 }
397
Daniel Dunbare10787e2009-08-07 08:26:05 +0000398 bool isReg() const { return Kind == Register; }
399
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000400 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
401 // Add as immediates when possible.
402 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
403 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
404 else
405 Inst.addOperand(MCOperand::CreateExpr(Expr));
406 }
407
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000408 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000409 assert(N == 1 && "Invalid number of operands!");
410 Inst.addOperand(MCOperand::CreateReg(getReg()));
411 }
412
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000413 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbare10787e2009-08-07 08:26:05 +0000414 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000415 addExpr(Inst, getImm());
Daniel Dunbare10787e2009-08-07 08:26:05 +0000416 }
417
Chad Rosier51afe632012-06-27 22:34:28 +0000418 void addMem8Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000420 }
Chad Rosier51afe632012-06-27 22:34:28 +0000421 void addMem16Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000423 }
Chad Rosier51afe632012-06-27 22:34:28 +0000424 void addMem32Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000426 }
Chad Rosier51afe632012-06-27 22:34:28 +0000427 void addMem64Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000429 }
Chad Rosier51afe632012-06-27 22:34:28 +0000430 void addMem80Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000432 }
Chad Rosier51afe632012-06-27 22:34:28 +0000433 void addMem128Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000435 }
Chad Rosier51afe632012-06-27 22:34:28 +0000436 void addMem256Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
Devang Patelfc6be102012-01-12 01:51:42 +0000438 }
Craig Topper01deb5f2012-07-18 04:11:12 +0000439 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
440 addMemOperands(Inst, N);
441 }
442 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
443 addMemOperands(Inst, N);
444 }
445 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
446 addMemOperands(Inst, N);
447 }
448 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
449 addMemOperands(Inst, N);
450 }
Devang Patelfc6be102012-01-12 01:51:42 +0000451
Daniel Dunbaraeb1feb2009-08-10 21:00:45 +0000452 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbara97adee2010-01-30 00:24:00 +0000453 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbare10787e2009-08-07 08:26:05 +0000454 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
455 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
456 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar224340ca2010-02-13 00:17:21 +0000457 addExpr(Inst, getMemDisp());
Daniel Dunbara97adee2010-01-30 00:24:00 +0000458 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
459 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000460
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000461 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
462 assert((N == 1) && "Invalid number of operands!");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000463 // Add as immediates when possible.
464 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
465 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
466 else
467 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000468 }
469
Chris Lattner528d00b2010-01-15 19:28:38 +0000470 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000471 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerd416bae2011-10-16 11:28:29 +0000472 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000473 Res->Tok.Data = Str.data();
474 Res->Tok.Length = Str.size();
Daniel Dunbare10787e2009-08-07 08:26:05 +0000475 return Res;
476 }
477
Chad Rosier91c82662012-10-24 17:22:29 +0000478 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiera4bc9432013-01-10 22:10:27 +0000479 bool AddressOf = false,
Chad Rosiere81309b2013-04-09 17:53:49 +0000480 SMLoc OffsetOfLoc = SMLoc(),
481 StringRef SymName = StringRef()) {
Chris Lattner86e61532010-01-15 19:06:59 +0000482 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000483 Res->Reg.RegNo = RegNo;
Chad Rosiera4bc9432013-01-10 22:10:27 +0000484 Res->AddressOf = AddressOf;
Chad Rosier91c82662012-10-24 17:22:29 +0000485 Res->OffsetOfLoc = OffsetOfLoc;
Chad Rosiere81309b2013-04-09 17:53:49 +0000486 Res->SymName = SymName;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000487 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000488 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000489
Chad Rosierf3c04f62013-03-19 21:58:18 +0000490 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattner528d00b2010-01-15 19:28:38 +0000491 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000492 Res->Imm.Val = Val;
493 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000494 }
Daniel Dunbare10787e2009-08-07 08:26:05 +0000495
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000496 /// Create an absolute memory operand.
Chad Rosier6844ea02012-10-24 22:13:37 +0000497 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000498 unsigned Size = 0,
499 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000500 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
501 Res->Mem.SegReg = 0;
502 Res->Mem.Disp = Disp;
503 Res->Mem.BaseReg = 0;
504 Res->Mem.IndexReg = 0;
Daniel Dunbar3184f222010-02-02 21:44:16 +0000505 Res->Mem.Scale = 1;
Devang Patelfc6be102012-01-12 01:51:42 +0000506 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000507 Res->SymName = SymName;
Chad Rosier8c2a9c72013-01-10 23:39:07 +0000508 Res->AddressOf = false;
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000509 return Res;
510 }
511
512 /// Create a generalized memory operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +0000513 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
514 unsigned BaseReg, unsigned IndexReg,
Devang Patelfc6be102012-01-12 01:51:42 +0000515 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +0000516 unsigned Size = 0,
517 StringRef SymName = StringRef()) {
Daniel Dunbar76e5d702010-01-30 01:02:48 +0000518 // We should never just have a displacement, that should be parsed as an
519 // absolute memory operand.
Daniel Dunbara4fc8d92009-07-31 22:22:54 +0000520 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
521
Daniel Dunbar3ebf8482009-07-31 20:53:16 +0000522 // The scale should always be one of {1,2,4,8}.
523 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000524 "Invalid scale!");
Chris Lattner015cfb12010-01-15 19:33:43 +0000525 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner0c2538f2010-01-15 18:51:29 +0000526 Res->Mem.SegReg = SegReg;
527 Res->Mem.Disp = Disp;
528 Res->Mem.BaseReg = BaseReg;
529 Res->Mem.IndexReg = IndexReg;
530 Res->Mem.Scale = Scale;
Devang Patelfc6be102012-01-12 01:51:42 +0000531 Res->Mem.Size = Size;
Chad Rosiere81309b2013-04-09 17:53:49 +0000532 Res->SymName = SymName;
NAKAMURA Takumi7f254272013-01-11 01:13:54 +0000533 Res->AddressOf = false;
Chris Lattner0c2538f2010-01-15 18:51:29 +0000534 return Res;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000535 }
536};
Daniel Dunbar3c2a8932009-07-20 18:55:04 +0000537
Chris Lattner4eb9df02009-07-29 06:33:53 +0000538} // end anonymous namespace.
Daniel Dunbarf59ee962009-07-28 20:47:52 +0000539
Devang Patel4a6e7782012-01-12 18:03:40 +0000540bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000541 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000542
543 return (Op.isMem() &&
544 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
545 isa<MCConstantExpr>(Op.Mem.Disp) &&
546 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
547 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
548}
549
Devang Patel4a6e7782012-01-12 18:03:40 +0000550bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000551 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000552
Chad Rosier51afe632012-06-27 22:34:28 +0000553 return Op.isMem() &&
Kevin Enderby1ef22f32012-03-13 19:47:55 +0000554 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +0000555 isa<MCConstantExpr>(Op.Mem.Disp) &&
556 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
557 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
558}
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000559
Devang Patel4a6e7782012-01-12 18:03:40 +0000560bool X86AsmParser::ParseRegister(unsigned &RegNo,
561 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattnercc2ad082010-01-15 18:27:19 +0000562 RegNo = 0;
Benjamin Kramere3d658b2012-09-07 14:51:35 +0000563 const AsmToken &PercentTok = Parser.getTok();
564 StartLoc = PercentTok.getLoc();
565
566 // If we encounter a %, ignore it. This code handles registers with and
567 // without the prefix, unprefixed registers can occur in cfi directives.
568 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Patel41b9dde2012-01-17 18:00:18 +0000569 Parser.Lex(); // Eat percent token.
Kevin Enderby7d912182009-09-03 17:15:07 +0000570
Sean Callanan936b0d32010-01-19 21:44:56 +0000571 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000572 EndLoc = Tok.getEndLoc();
573
Devang Patelce6a2ca2012-01-20 22:32:05 +0000574 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000575 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000576 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000577 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000578 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000579
Kevin Enderby7d912182009-09-03 17:15:07 +0000580 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000581
Chris Lattner1261b812010-09-22 04:11:10 +0000582 // If the match failed, try the register name as lowercase.
583 if (RegNo == 0)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000584 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000585
Evan Chengeda1d4f2011-07-27 23:22:03 +0000586 if (!is64BitMode()) {
587 // FIXME: This should be done using Requires<In32BitMode> and
588 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
589 // checked.
590 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
591 // REX prefix.
592 if (RegNo == X86::RIZ ||
593 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
594 X86II::isX86_64NonExtLowByteReg(RegNo) ||
595 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer1930b002011-10-16 12:10:27 +0000596 return Error(StartLoc, "register %"
597 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000598 SMRange(StartLoc, EndLoc));
Evan Chengeda1d4f2011-07-27 23:22:03 +0000599 }
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +0000600
Chris Lattner1261b812010-09-22 04:11:10 +0000601 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
602 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000603 RegNo = X86::ST0;
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000604 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000605
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000606 // Check to see if we have '(4)' after %st.
607 if (getLexer().isNot(AsmToken::LParen))
608 return false;
609 // Lex the paren.
610 getParser().Lex();
611
612 const AsmToken &IntTok = Parser.getTok();
613 if (IntTok.isNot(AsmToken::Integer))
614 return Error(IntTok.getLoc(), "expected stack index");
615 switch (IntTok.getIntVal()) {
616 case 0: RegNo = X86::ST0; break;
617 case 1: RegNo = X86::ST1; break;
618 case 2: RegNo = X86::ST2; break;
619 case 3: RegNo = X86::ST3; break;
620 case 4: RegNo = X86::ST4; break;
621 case 5: RegNo = X86::ST5; break;
622 case 6: RegNo = X86::ST6; break;
623 case 7: RegNo = X86::ST7; break;
624 default: return Error(IntTok.getLoc(), "invalid stack index");
625 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000626
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000627 if (getParser().Lex().isNot(AsmToken::RParen))
628 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000629
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000630 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnerd00faaa2010-02-09 00:49:22 +0000631 Parser.Lex(); // Eat ')'
632 return false;
633 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000634
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000635 EndLoc = Parser.getTok().getEndLoc();
636
Chris Lattner80486622010-06-24 07:29:18 +0000637 // If this is "db[0-7]", match it as an alias
638 // for dr[0-7].
639 if (RegNo == 0 && Tok.getString().size() == 3 &&
640 Tok.getString().startswith("db")) {
641 switch (Tok.getString()[2]) {
642 case '0': RegNo = X86::DR0; break;
643 case '1': RegNo = X86::DR1; break;
644 case '2': RegNo = X86::DR2; break;
645 case '3': RegNo = X86::DR3; break;
646 case '4': RegNo = X86::DR4; break;
647 case '5': RegNo = X86::DR5; break;
648 case '6': RegNo = X86::DR6; break;
649 case '7': RegNo = X86::DR7; break;
650 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000651
Chris Lattner80486622010-06-24 07:29:18 +0000652 if (RegNo != 0) {
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000653 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner80486622010-06-24 07:29:18 +0000654 Parser.Lex(); // Eat it.
655 return false;
656 }
657 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +0000658
Devang Patelce6a2ca2012-01-20 22:32:05 +0000659 if (RegNo == 0) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000660 if (isParsingIntelSyntax()) return true;
Benjamin Kramer1930b002011-10-16 12:10:27 +0000661 return Error(StartLoc, "invalid register name",
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000662 SMRange(StartLoc, EndLoc));
Devang Patelce6a2ca2012-01-20 22:32:05 +0000663 }
Daniel Dunbar00331992009-07-29 00:02:19 +0000664
Sean Callanana83fd7d2010-01-19 20:27:46 +0000665 Parser.Lex(); // Eat identifier token.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +0000666 return false;
Daniel Dunbar71475772009-07-17 20:42:00 +0000667}
668
Devang Patel4a6e7782012-01-12 18:03:40 +0000669X86Operand *X86AsmParser::ParseOperand() {
Devang Patel9a9bb5c2012-01-30 20:02:42 +0000670 if (isParsingIntelSyntax())
Devang Patel46831de2012-01-12 01:36:43 +0000671 return ParseIntelOperand();
672 return ParseATTOperand();
673}
674
Devang Patel41b9dde2012-01-17 18:00:18 +0000675/// getIntelMemOperandSize - Return intel memory operand size.
676static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosierb6b8e962012-09-11 21:10:25 +0000677 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierab53b4f2012-09-12 18:24:26 +0000678 .Cases("BYTE", "byte", 8)
679 .Cases("WORD", "word", 16)
680 .Cases("DWORD", "dword", 32)
681 .Cases("QWORD", "qword", 64)
682 .Cases("XWORD", "xword", 80)
683 .Cases("XMMWORD", "xmmword", 128)
684 .Cases("YMMWORD", "ymmword", 256)
Chad Rosierb6b8e962012-09-11 21:10:25 +0000685 .Default(0);
686 return Size;
Devang Patel46831de2012-01-12 01:36:43 +0000687}
688
Chad Rosier4a7005e2013-04-05 16:28:55 +0000689enum InfixCalculatorTok {
690 IC_PLUS = 0,
691 IC_MINUS,
692 IC_MULTIPLY,
693 IC_DIVIDE,
694 IC_RPAREN,
695 IC_LPAREN,
696 IC_IMM,
697 IC_REGISTER
698};
699static const char OpPrecedence[] = {
700 0, // IC_PLUS
701 0, // IC_MINUS
702 1, // IC_MULTIPLY
703 1, // IC_DIVIDE
704 2, // IC_RPAREN
705 3, // IC_LPAREN
706 0, // IC_IMM
707 0 // IC_REGISTER
708};
709
710class InfixCalculator {
711 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
712 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
713 SmallVector<ICToken, 4> PostfixStack;
714
715public:
716 int64_t popOperand() {
717 assert (!PostfixStack.empty() && "Poped an empty stack!");
718 ICToken Op = PostfixStack.pop_back_val();
719 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
720 && "Expected and immediate or register!");
721 return Op.second;
722 }
723 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
724 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
725 "Unexpected operand!");
726 PostfixStack.push_back(std::make_pair(Op, Val));
727 }
728
729 void popOperator() { InfixOperatorStack.pop_back_val(); }
730 void pushOperator(InfixCalculatorTok Op) {
731 // Push the new operator if the stack is empty.
732 if (InfixOperatorStack.empty()) {
733 InfixOperatorStack.push_back(Op);
734 return;
735 }
736
737 // Push the new operator if it has a higher precedence than the operator on
738 // the top of the stack or the operator on the top of the stack is a left
739 // parentheses.
740 unsigned Idx = InfixOperatorStack.size() - 1;
741 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
742 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
743 InfixOperatorStack.push_back(Op);
744 return;
745 }
746
747 // The operator on the top of the stack has higher precedence than the
748 // new operator.
749 unsigned ParenCount = 0;
750 while (1) {
751 // Nothing to process.
752 if (InfixOperatorStack.empty())
753 break;
754
755 Idx = InfixOperatorStack.size() - 1;
756 StackOp = InfixOperatorStack[Idx];
757 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
758 break;
759
760 // If we have an even parentheses count and we see a left parentheses,
761 // then stop processing.
762 if (!ParenCount && StackOp == IC_LPAREN)
763 break;
764
765 if (StackOp == IC_RPAREN) {
766 ++ParenCount;
767 InfixOperatorStack.pop_back_val();
768 } else if (StackOp == IC_LPAREN) {
769 --ParenCount;
770 InfixOperatorStack.pop_back_val();
771 } else {
772 InfixOperatorStack.pop_back_val();
773 PostfixStack.push_back(std::make_pair(StackOp, 0));
774 }
775 }
776 // Push the new operator.
777 InfixOperatorStack.push_back(Op);
778 }
779 int64_t execute() {
780 // Push any remaining operators onto the postfix stack.
781 while (!InfixOperatorStack.empty()) {
782 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
783 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
784 PostfixStack.push_back(std::make_pair(StackOp, 0));
785 }
786
787 if (PostfixStack.empty())
788 return 0;
789
790 SmallVector<ICToken, 16> OperandStack;
791 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
792 ICToken Op = PostfixStack[i];
793 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
794 OperandStack.push_back(Op);
795 } else {
796 assert (OperandStack.size() > 1 && "Too few operands.");
797 int64_t Val;
798 ICToken Op2 = OperandStack.pop_back_val();
799 ICToken Op1 = OperandStack.pop_back_val();
800 switch (Op.first) {
801 default:
802 report_fatal_error("Unexpected operator!");
803 break;
804 case IC_PLUS:
805 Val = Op1.second + Op2.second;
806 OperandStack.push_back(std::make_pair(IC_IMM, Val));
807 break;
808 case IC_MINUS:
809 Val = Op1.second - Op2.second;
810 OperandStack.push_back(std::make_pair(IC_IMM, Val));
811 break;
812 case IC_MULTIPLY:
813 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
814 "Multiply operation with an immediate and a register!");
815 Val = Op1.second * Op2.second;
816 OperandStack.push_back(std::make_pair(IC_IMM, Val));
817 break;
818 case IC_DIVIDE:
819 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
820 "Divide operation with an immediate and a register!");
821 assert (Op2.second != 0 && "Division by zero!");
822 Val = Op1.second / Op2.second;
823 OperandStack.push_back(std::make_pair(IC_IMM, Val));
824 break;
825 }
826 }
827 }
828 assert (OperandStack.size() == 1 && "Expected a single result.");
829 return OperandStack.pop_back_val().second;
830 }
831};
832
Chad Rosier5c118fd2013-01-14 22:31:35 +0000833enum IntelBracExprState {
Chad Rosier4a7005e2013-04-05 16:28:55 +0000834 IBES_PLUS,
835 IBES_MINUS,
836 IBES_MULTIPLY,
837 IBES_DIVIDE,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000838 IBES_LBRAC,
839 IBES_RBRAC,
Chad Rosier4a7005e2013-04-05 16:28:55 +0000840 IBES_LPAREN,
841 IBES_RPAREN,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000842 IBES_REGISTER,
843 IBES_REGISTER_STAR,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000844 IBES_INTEGER,
845 IBES_INTEGER_STAR,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000846 IBES_IDENTIFIER,
Chad Rosier5c118fd2013-01-14 22:31:35 +0000847 IBES_ERROR
848};
849
850class IntelBracExprStateMachine {
851 IntelBracExprState State;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000852 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000853 int64_t Disp;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000854 InfixCalculator IC;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000855public:
Chad Rosier1530ba52013-03-27 21:49:56 +0000856 IntelBracExprStateMachine(MCAsmParser &parser, int64_t disp) :
Chad Rosier4a7005e2013-04-05 16:28:55 +0000857 State(IBES_PLUS), BaseReg(0), IndexReg(0), TmpReg(0), Scale(1), Disp(disp){}
Chad Rosier5c118fd2013-01-14 22:31:35 +0000858
859 unsigned getBaseReg() { return BaseReg; }
860 unsigned getIndexReg() { return IndexReg; }
861 unsigned getScale() { return Scale; }
Chad Rosier4a7005e2013-04-05 16:28:55 +0000862 int64_t getDisp() { return Disp + IC.execute(); }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000863 bool isValidEndState() { return State == IBES_RBRAC; }
864
865 void onPlus() {
866 switch (State) {
867 default:
868 State = IBES_ERROR;
869 break;
870 case IBES_INTEGER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000871 case IBES_RPAREN:
872 State = IBES_PLUS;
873 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000874 break;
875 case IBES_REGISTER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000876 State = IBES_PLUS;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000877 // If we already have a BaseReg, then assume this is the IndexReg with a
878 // scale of 1.
879 if (!BaseReg) {
880 BaseReg = TmpReg;
881 } else {
882 assert (!IndexReg && "BaseReg/IndexReg already set!");
883 IndexReg = TmpReg;
884 Scale = 1;
885 }
Chad Rosier4a7005e2013-04-05 16:28:55 +0000886 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000887 break;
888 }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000889 }
890 void onMinus() {
891 switch (State) {
892 default:
893 State = IBES_ERROR;
894 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000895 case IBES_PLUS:
896 case IBES_LPAREN:
897 IC.pushOperand(IC_IMM);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000898 case IBES_INTEGER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000899 case IBES_RPAREN:
900 State = IBES_MINUS;
901 IC.pushOperator(IC_MINUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000902 break;
903 case IBES_REGISTER:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000904 State = IBES_MINUS;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000905 // If we already have a BaseReg, then assume this is the IndexReg with a
906 // scale of 1.
907 if (!BaseReg) {
908 BaseReg = TmpReg;
909 } else {
910 assert (!IndexReg && "BaseReg/IndexReg already set!");
911 IndexReg = TmpReg;
912 Scale = 1;
913 }
Chad Rosier4a7005e2013-04-05 16:28:55 +0000914 IC.pushOperator(IC_MINUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000915 break;
916 }
Chad Rosier5c118fd2013-01-14 22:31:35 +0000917 }
918 void onRegister(unsigned Reg) {
919 switch (State) {
920 default:
921 State = IBES_ERROR;
922 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000923 case IBES_PLUS:
924 case IBES_LPAREN:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000925 State = IBES_REGISTER;
926 TmpReg = Reg;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000927 IC.pushOperand(IC_REGISTER);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000928 break;
929 case IBES_INTEGER_STAR:
930 assert (!IndexReg && "IndexReg already set!");
Chad Rosier4a7005e2013-04-05 16:28:55 +0000931 State = IBES_INTEGER;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000932 IndexReg = Reg;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000933 Scale = IC.popOperand();
934 IC.pushOperand(IC_IMM);
935 IC.popOperator();
Chad Rosier5c118fd2013-01-14 22:31:35 +0000936 break;
937 }
938 }
939 void onDispExpr() {
940 switch (State) {
941 default:
942 State = IBES_ERROR;
943 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000944 case IBES_PLUS:
945 case IBES_MINUS:
946 State = IBES_INTEGER;
947 IC.pushOperand(IC_IMM);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000948 break;
949 }
950 }
951 void onInteger(int64_t TmpInt) {
952 switch (State) {
953 default:
954 State = IBES_ERROR;
955 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000956 case IBES_PLUS:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000957 case IBES_MINUS:
Chad Rosier4a7005e2013-04-05 16:28:55 +0000958 case IBES_MULTIPLY:
959 case IBES_DIVIDE:
960 case IBES_LPAREN:
961 case IBES_INTEGER_STAR:
Chad Rosier5c118fd2013-01-14 22:31:35 +0000962 State = IBES_INTEGER;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000963 IC.pushOperand(IC_IMM, TmpInt);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000964 break;
965 case IBES_REGISTER_STAR:
966 assert (!IndexReg && "IndexReg already set!");
Chad Rosier4a7005e2013-04-05 16:28:55 +0000967 State = IBES_INTEGER;
Chad Rosier5c118fd2013-01-14 22:31:35 +0000968 IndexReg = TmpReg;
969 Scale = TmpInt;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000970 IC.popOperator();
Chad Rosier5c118fd2013-01-14 22:31:35 +0000971 break;
972 }
973 }
974 void onStar() {
975 switch (State) {
976 default:
977 State = IBES_ERROR;
978 break;
979 case IBES_INTEGER:
980 State = IBES_INTEGER_STAR;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000981 IC.pushOperator(IC_MULTIPLY);
Chad Rosier5c118fd2013-01-14 22:31:35 +0000982 break;
983 case IBES_REGISTER:
984 State = IBES_REGISTER_STAR;
Chad Rosier4a7005e2013-04-05 16:28:55 +0000985 IC.pushOperator(IC_MULTIPLY);
986 break;
987 case IBES_RPAREN:
988 State = IBES_MULTIPLY;
989 IC.pushOperator(IC_MULTIPLY);
990 break;
991 }
992 }
993 void onDivide() {
994 switch (State) {
995 default:
996 State = IBES_ERROR;
997 break;
998 case IBES_INTEGER:
999 State = IBES_DIVIDE;
1000 IC.pushOperator(IC_DIVIDE);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001001 break;
1002 }
1003 }
1004 void onLBrac() {
1005 switch (State) {
1006 default:
1007 State = IBES_ERROR;
1008 break;
1009 case IBES_RBRAC:
Chad Rosier4a7005e2013-04-05 16:28:55 +00001010 State = IBES_PLUS;
1011 IC.pushOperator(IC_PLUS);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001012 break;
1013 }
1014 }
1015 void onRBrac() {
1016 switch (State) {
1017 default:
1018 State = IBES_ERROR;
1019 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001020 case IBES_RPAREN:
Chad Rosier5c118fd2013-01-14 22:31:35 +00001021 case IBES_INTEGER:
1022 State = IBES_RBRAC;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001023 break;
1024 case IBES_REGISTER:
1025 State = IBES_RBRAC;
1026 // If we already have a BaseReg, then assume this is the IndexReg with a
1027 // scale of 1.
1028 if (!BaseReg) {
1029 BaseReg = TmpReg;
1030 } else {
1031 assert (!IndexReg && "BaseReg/IndexReg already set!");
1032 IndexReg = TmpReg;
1033 Scale = 1;
1034 }
1035 break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001036 }
1037 }
1038 void onLParen() {
1039 switch (State) {
1040 default:
1041 State = IBES_ERROR;
1042 break;
1043 case IBES_PLUS:
1044 case IBES_MINUS:
1045 case IBES_MULTIPLY:
1046 case IBES_DIVIDE:
1047 case IBES_INTEGER_STAR:
1048 case IBES_LPAREN:
1049 State = IBES_LPAREN;
1050 IC.pushOperator(IC_LPAREN);
1051 break;
1052 }
1053 }
1054 void onRParen() {
1055 switch (State) {
1056 default:
1057 State = IBES_ERROR;
1058 break;
1059 case IBES_REGISTER:
1060 case IBES_INTEGER:
1061 case IBES_PLUS:
1062 case IBES_MINUS:
1063 case IBES_MULTIPLY:
1064 case IBES_DIVIDE:
1065 case IBES_RPAREN:
1066 State = IBES_RPAREN;
1067 IC.pushOperator(IC_RPAREN);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001068 break;
1069 }
1070 }
1071};
1072
Chad Rosier7ca135b2013-03-19 21:11:56 +00001073X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
1074 SMLoc End, SMLoc SizeDirLoc,
Chad Rosiere81309b2013-04-09 17:53:49 +00001075 unsigned Size, StringRef SymName) {
Chad Rosier7ca135b2013-03-19 21:11:56 +00001076 bool NeedSizeDir = false;
1077 bool IsVarDecl = false;
Chad Rosiere81309b2013-04-09 17:53:49 +00001078
Chad Rosier7ca135b2013-03-19 21:11:56 +00001079 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1080 const MCSymbol &Sym = SymRef->getSymbol();
1081 // FIXME: The SemaLookup will fail if the name is anything other then an
1082 // identifier.
1083 // FIXME: Pass a valid SMLoc.
1084 unsigned tLength, tSize, tType;
Chad Rosiere81309b2013-04-09 17:53:49 +00001085 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength, tSize,
1086 tType, IsVarDecl);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001087 if (!Size) {
1088 Size = tType * 8; // Size is in terms of bits in this context.
1089 NeedSizeDir = Size > 0;
1090 }
1091 }
1092
1093 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1094 // reference. We need an 'r' constraint here, so we need to create register
1095 // operand to ensure proper matching. Just pick a GPR based on the size of
1096 // a pointer.
1097 if (!IsVarDecl) {
1098 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiere81309b2013-04-09 17:53:49 +00001099 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true, SMLoc(),
1100 SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001101 }
1102
1103 if (NeedSizeDir)
1104 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
1105 /*Len*/0, Size));
1106
1107 // When parsing inline assembly we set the base register to a non-zero value
1108 // as we don't know the actual value at this time. This is necessary to
1109 // get the matching correct in some cases.
1110 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosiere81309b2013-04-09 17:53:49 +00001111 /*Scale*/1, Start, End, Size, SymName);
Chad Rosier7ca135b2013-03-19 21:11:56 +00001112}
1113
Chad Rosier1530ba52013-03-27 21:49:56 +00001114X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Chad Rosierfce4fab2013-04-08 17:43:47 +00001115 SMLoc SizeDirLoc,
Chad Rosier1530ba52013-03-27 21:49:56 +00001116 uint64_t ImmDisp,
Devang Patel880bc162012-01-23 18:31:58 +00001117 unsigned Size) {
Chad Rosier6844ea02012-10-24 22:13:37 +00001118 const AsmToken &Tok = Parser.getTok();
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001119 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel46831de2012-01-12 01:36:43 +00001120
Devang Patel41b9dde2012-01-17 18:00:18 +00001121 // Eat '['
1122 if (getLexer().isNot(AsmToken::LBrac))
1123 return ErrorOperand(Start, "Expected '[' token!");
1124 Parser.Lex();
Chad Rosier51afe632012-06-27 22:34:28 +00001125
Chad Rosier5c118fd2013-01-14 22:31:35 +00001126 unsigned TmpReg = 0;
1127
Chad Rosier1530ba52013-03-27 21:49:56 +00001128 // Try to handle '[' 'Symbol' ']'
Devang Patel41b9dde2012-01-17 18:00:18 +00001129 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosier70f47592013-04-10 20:07:47 +00001130 SMLoc Loc = Tok.getLoc();
1131 if (ParseRegister(TmpReg, Loc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001132 const MCExpr *Disp;
Chad Rosierce031892013-04-11 23:24:15 +00001133 StringRef Identifier = Tok.getString();
Chad Rosiere8d82882013-04-09 19:59:12 +00001134 if (getParser().parseExpression(Disp, End))
Chad Rosier5c118fd2013-01-14 22:31:35 +00001135 return 0;
1136
Chad Rosierce031892013-04-11 23:24:15 +00001137 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001138 return Err;
1139
Devang Patel41b9dde2012-01-17 18:00:18 +00001140 if (getLexer().isNot(AsmToken::RBrac))
Chad Rosier70f47592013-04-10 20:07:47 +00001141 return ErrorOperand(Tok.getLoc(), "Expected ']' token!");
Chad Rosier1530ba52013-03-27 21:49:56 +00001142
Chad Rosier8fb83302013-04-11 21:49:30 +00001143 if (isParsingInlineAsm()) {
1144 // Remove the '[' and ']' from the IR string.
1145 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Start, 1));
1146 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Tok.getLoc(), 1));
1147 }
Chad Rosiere81309b2013-04-09 17:53:49 +00001148 Parser.Lex(); // Eat ']'
Chad Rosier7ca135b2013-03-19 21:11:56 +00001149 if (!isParsingInlineAsm())
Chad Rosierce031892013-04-11 23:24:15 +00001150 return X86Operand::CreateMem(Disp, Start, End, Size);
1151 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size,
1152 Identifier);
Devang Patel41b9dde2012-01-17 18:00:18 +00001153 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001154 }
1155
Chad Rosier1530ba52013-03-27 21:49:56 +00001156 // Parse [ BaseReg + Scale*IndexReg + Disp ]. We may have already parsed an
1157 // immediate displacement before the bracketed expression.
Chad Rosier5c118fd2013-01-14 22:31:35 +00001158 bool Done = false;
Chad Rosier1530ba52013-03-27 21:49:56 +00001159 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier1bbaa442012-10-29 18:01:54 +00001160
Chad Rosier5c118fd2013-01-14 22:31:35 +00001161 // If we parsed a register, then the end loc has already been set and
1162 // the identifier has already been lexed. We also need to update the
1163 // state.
1164 if (TmpReg)
1165 SM.onRegister(TmpReg);
1166
1167 const MCExpr *Disp = 0;
1168 while (!Done) {
1169 bool UpdateLocLex = true;
1170
1171 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1172 // identifier. Don't try an parse it as a register.
1173 if (Tok.getString().startswith("."))
1174 break;
1175
1176 switch (getLexer().getKind()) {
1177 default: {
1178 if (SM.isValidEndState()) {
1179 Done = true;
1180 break;
1181 }
1182 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1183 }
1184 case AsmToken::Identifier: {
1185 // This could be a register or a displacement expression.
Chad Rosier70f47592013-04-10 20:07:47 +00001186 SMLoc Loc = Tok.getLoc();
1187 if(!ParseRegister(TmpReg, Loc, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001188 SM.onRegister(TmpReg);
1189 UpdateLocLex = false;
1190 break;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001191 } else if (!getParser().parsePrimaryExpr(Disp, End)) {
Chad Rosier5c118fd2013-01-14 22:31:35 +00001192 SM.onDispExpr();
1193 UpdateLocLex = false;
1194 break;
1195 }
1196 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1197 }
Chad Rosier4a7005e2013-04-05 16:28:55 +00001198 case AsmToken::Integer:
1199 if (isParsingInlineAsm())
1200 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1201 Tok.getLoc()));
1202 SM.onInteger(Tok.getIntVal());
Chad Rosier5c118fd2013-01-14 22:31:35 +00001203 break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001204 case AsmToken::Plus: SM.onPlus(); break;
1205 case AsmToken::Minus: SM.onMinus(); break;
1206 case AsmToken::Star: SM.onStar(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001207 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001208 case AsmToken::LBrac: SM.onLBrac(); break;
1209 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosier4a7005e2013-04-05 16:28:55 +00001210 case AsmToken::LParen: SM.onLParen(); break;
1211 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosier5c118fd2013-01-14 22:31:35 +00001212 }
1213 if (!Done && UpdateLocLex) {
1214 End = Tok.getLoc();
1215 Parser.Lex(); // Consume the token.
Devang Patelcf893a42012-01-23 22:35:25 +00001216 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001217 }
Chad Rosier8fb83302013-04-11 21:49:30 +00001218 if (isParsingInlineAsm() && Disp && isa<MCSymbolRefExpr>(Disp)) {
1219 // Remove the '[' and ']' from the IR string.
1220 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, Start, 1));
1221 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, End, 1));
1222 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001223
Chad Rosier5c118fd2013-01-14 22:31:35 +00001224 if (!Disp)
1225 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Pateld0930ff2012-01-20 21:21:01 +00001226
Chad Rosier8e71f7c2012-10-26 22:01:25 +00001227 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier911c1f32012-10-25 17:37:43 +00001228 if (Tok.getString().startswith(".")) {
1229 SmallString<64> Err;
1230 const MCExpr *NewDisp;
1231 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1232 return ErrorOperand(Tok.getLoc(), Err);
1233
Chad Rosier70f47592013-04-10 20:07:47 +00001234 End = Tok.getEndLoc();
Chad Rosier911c1f32012-10-25 17:37:43 +00001235 Parser.Lex(); // Eat the field.
1236 Disp = NewDisp;
1237 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001238
Chad Rosier5c118fd2013-01-14 22:31:35 +00001239 int BaseReg = SM.getBaseReg();
1240 int IndexReg = SM.getIndexReg();
Devang Pateld0930ff2012-01-20 21:21:01 +00001241
Chad Rosier5c118fd2013-01-14 22:31:35 +00001242 // handle [-42]
1243 if (!BaseReg && !IndexReg) {
1244 if (!SegReg)
Chad Rosiere81309b2013-04-09 17:53:49 +00001245 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier5c118fd2013-01-14 22:31:35 +00001246 else
1247 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1248 }
1249
1250 int Scale = SM.getScale();
Chad Rosiere81309b2013-04-09 17:53:49 +00001251 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale, Start,
1252 End, Size);
Devang Patel41b9dde2012-01-17 18:00:18 +00001253}
1254
Chad Rosier8a244662013-04-02 20:02:33 +00001255// Inline assembly may use variable names with namespace alias qualifiers.
1256X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
Chad Rosierce031892013-04-11 23:24:15 +00001257 StringRef &Identifier) {
Chad Rosier8a244662013-04-02 20:02:33 +00001258 // We should only see Foo::Bar if we're parsing inline assembly.
1259 if (!isParsingInlineAsm())
1260 return 0;
1261
1262 // If we don't see a ':' then there can't be a qualifier.
1263 if (getLexer().isNot(AsmToken::Colon))
1264 return 0;
1265
Chad Rosier8a244662013-04-02 20:02:33 +00001266 bool Done = false;
1267 const AsmToken &Tok = Parser.getTok();
Chad Rosierce031892013-04-11 23:24:15 +00001268 AsmToken IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001269 while (!Done) {
1270 switch (getLexer().getKind()) {
1271 default:
1272 Done = true;
1273 break;
1274 case AsmToken::Colon:
1275 getLexer().Lex(); // Consume ':'.
1276 if (getLexer().isNot(AsmToken::Colon))
1277 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1278 getLexer().Lex(); // Consume second ':'.
1279 if (getLexer().isNot(AsmToken::Identifier))
1280 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1281 break;
1282 case AsmToken::Identifier:
Chad Rosierce031892013-04-11 23:24:15 +00001283 IdentEnd = Tok;
Chad Rosier8a244662013-04-02 20:02:33 +00001284 getLexer().Lex(); // Consume the identifier.
1285 break;
1286 }
1287 }
Chad Rosierce031892013-04-11 23:24:15 +00001288
1289 unsigned Len = IdentEnd.getLoc().getPointer() - Identifier.data();
1290 Identifier = StringRef(Identifier.data(), Len + IdentEnd.getString().size());
Chad Rosier8a244662013-04-02 20:02:33 +00001291 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1292 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1293 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1294 return 0;
1295}
1296
Devang Patel41b9dde2012-01-17 18:00:18 +00001297/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001298X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1299 uint64_t ImmDisp,
1300 SMLoc Start) {
Devang Patel41b9dde2012-01-17 18:00:18 +00001301 const AsmToken &Tok = Parser.getTok();
Chad Rosier91c82662012-10-24 17:22:29 +00001302 SMLoc End;
Devang Patel41b9dde2012-01-17 18:00:18 +00001303
1304 unsigned Size = getIntelMemOperandSize(Tok.getString());
1305 if (Size) {
1306 Parser.Lex();
Chad Rosierab53b4f2012-09-12 18:24:26 +00001307 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1308 "Unexpected token!");
Devang Patel41b9dde2012-01-17 18:00:18 +00001309 Parser.Lex();
1310 }
1311
Chad Rosier1530ba52013-03-27 21:49:56 +00001312 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1313 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier1530ba52013-03-27 21:49:56 +00001314 if (isParsingInlineAsm())
1315 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
Chad Rosier70f47592013-04-10 20:07:47 +00001316 Tok.getLoc()));
1317 uint64_t ImmDisp = Tok.getIntVal();
Chad Rosier1530ba52013-03-27 21:49:56 +00001318 Parser.Lex(); // Eat the integer.
1319 if (getLexer().isNot(AsmToken::LBrac))
1320 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001321 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosier1530ba52013-03-27 21:49:56 +00001322 }
1323
Chad Rosier91c82662012-10-24 17:22:29 +00001324 if (getLexer().is(AsmToken::LBrac))
Chad Rosierfce4fab2013-04-08 17:43:47 +00001325 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001326
1327 if (!ParseRegister(SegReg, Start, End)) {
1328 // Handel SegReg : [ ... ]
1329 if (getLexer().isNot(AsmToken::Colon))
1330 return ErrorOperand(Start, "Expected ':' token!");
1331 Parser.Lex(); // Eat :
1332 if (getLexer().isNot(AsmToken::LBrac))
1333 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierfce4fab2013-04-08 17:43:47 +00001334 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel880bc162012-01-23 18:31:58 +00001335 }
Devang Patel41b9dde2012-01-17 18:00:18 +00001336
Chad Rosiere81309b2013-04-09 17:53:49 +00001337 const MCExpr *Disp = 0;
Chad Rosierce031892013-04-11 23:24:15 +00001338 StringRef Identifier = Tok.getString();
Chad Rosiere8d82882013-04-09 19:59:12 +00001339 if (getParser().parseExpression(Disp, End))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001340 return 0;
Chad Rosier0f48c552012-10-19 20:57:14 +00001341
Chad Rosier146310a2012-10-23 23:31:33 +00001342 if (!isParsingInlineAsm())
Chad Rosier91c82662012-10-24 17:22:29 +00001343 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier8a244662013-04-02 20:02:33 +00001344
Chad Rosierce031892013-04-11 23:24:15 +00001345 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, Identifier))
Chad Rosier8a244662013-04-02 20:02:33 +00001346 return Err;
1347
Chad Rosierce031892013-04-11 23:24:15 +00001348 return CreateMemForInlineAsm(Disp, Start, End, Start, Size, Identifier);
Chad Rosier91c82662012-10-24 17:22:29 +00001349}
1350
Chad Rosier5dcb4662012-10-24 22:21:50 +00001351/// Parse the '.' operator.
Chad Rosier911c1f32012-10-25 17:37:43 +00001352bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1353 const MCExpr **NewDisp,
1354 SmallString<64> &Err) {
Chad Rosier70f47592013-04-10 20:07:47 +00001355 const AsmToken &Tok = Parser.getTok();
Chad Rosier911c1f32012-10-25 17:37:43 +00001356 uint64_t OrigDispVal, DotDispVal;
1357
1358 // FIXME: Handle non-constant expressions.
1359 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1360 OrigDispVal = OrigDisp->getValue();
1361 } else {
1362 Err = "Non-constant offsets are not supported!";
1363 return true;
1364 }
Chad Rosier5dcb4662012-10-24 22:21:50 +00001365
1366 // Drop the '.'.
1367 StringRef DotDispStr = Tok.getString().drop_front(1);
1368
Chad Rosier5dcb4662012-10-24 22:21:50 +00001369 // .Imm gets lexed as a real.
1370 if (Tok.is(AsmToken::Real)) {
1371 APInt DotDisp;
1372 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier911c1f32012-10-25 17:37:43 +00001373 DotDispVal = DotDisp.getZExtValue();
Chad Rosier240b7b92012-10-25 21:51:10 +00001374 } else if (Tok.is(AsmToken::Identifier)) {
1375 // We should only see an identifier when parsing the original inline asm.
1376 // The front-end should rewrite this in terms of immediates.
1377 assert (isParsingInlineAsm() && "Unexpected field name!");
1378
1379 unsigned DotDisp;
1380 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1381 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1382 DotDisp)) {
1383 Err = "Unable to lookup field reference!";
1384 return true;
1385 }
1386 DotDispVal = DotDisp;
Chad Rosier911c1f32012-10-25 17:37:43 +00001387 } else {
1388 Err = "Unexpected token type!";
1389 return true;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001390 }
Chad Rosier911c1f32012-10-25 17:37:43 +00001391
Chad Rosier240b7b92012-10-25 21:51:10 +00001392 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1393 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1394 unsigned Len = DotDispStr.size();
1395 unsigned Val = OrigDispVal + DotDispVal;
1396 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1397 Val));
Chad Rosier911c1f32012-10-25 17:37:43 +00001398 }
1399
1400 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1401 return false;
Chad Rosier5dcb4662012-10-24 22:21:50 +00001402}
1403
Chad Rosier91c82662012-10-24 17:22:29 +00001404/// Parse the 'offset' operator. This operator is used to specify the
1405/// location rather then the content of a variable.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001406X86Operand *X86AsmParser::ParseIntelOffsetOfOperator() {
Chad Rosier18785852013-04-09 20:58:48 +00001407 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001408 SMLoc OffsetOfLoc = Tok.getLoc();
Chad Rosier91c82662012-10-24 17:22:29 +00001409 Parser.Lex(); // Eat offset.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001410 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier91c82662012-10-24 17:22:29 +00001411
Chad Rosier91c82662012-10-24 17:22:29 +00001412 const MCExpr *Val;
Chad Rosier18785852013-04-09 20:58:48 +00001413 SMLoc Start = Tok.getLoc(), End;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001414 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosier58593562012-10-26 18:32:44 +00001415 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosier91c82662012-10-24 17:22:29 +00001416
Chad Rosiere2f03772012-10-26 16:09:20 +00001417 // Don't emit the offset operator.
1418 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1419
Chad Rosier91c82662012-10-24 17:22:29 +00001420 // The offset operator will have an 'r' constraint, thus we need to create
1421 // register operand to ensure proper matching. Just pick a GPR based on
1422 // the size of a pointer.
1423 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosiere81309b2013-04-09 17:53:49 +00001424 unsigned Len = End.getPointer() - Start.getPointer();
1425 StringRef SymName(Start.getPointer(), Len);
Chad Rosiera4bc9432013-01-10 22:10:27 +00001426 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
Chad Rosiere81309b2013-04-09 17:53:49 +00001427 OffsetOfLoc, SymName);
Devang Patel41b9dde2012-01-17 18:00:18 +00001428}
1429
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001430enum IntelOperatorKind {
1431 IOK_LENGTH,
1432 IOK_SIZE,
1433 IOK_TYPE
1434};
1435
1436/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1437/// returns the number of elements in an array. It returns the value 1 for
1438/// non-array variables. The SIZE operator returns the size of a C or C++
1439/// variable. A variable's size is the product of its LENGTH and TYPE. The
1440/// TYPE operator returns the size of a C or C++ type or variable. If the
1441/// variable is an array, TYPE returns the size of a single element.
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001442X86Operand *X86AsmParser::ParseIntelOperator(unsigned OpKind) {
Chad Rosier18785852013-04-09 20:58:48 +00001443 const AsmToken &Tok = Parser.getTok();
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001444 SMLoc TypeLoc = Tok.getLoc();
1445 Parser.Lex(); // Eat operator.
Chad Rosier18785852013-04-09 20:58:48 +00001446 assert (Tok.is(AsmToken::Identifier) && "Expected an identifier");
Chad Rosier11c42f22012-10-26 18:04:20 +00001447
Chad Rosier11c42f22012-10-26 18:04:20 +00001448 const MCExpr *Val;
Chad Rosier18785852013-04-09 20:58:48 +00001449 SMLoc Start = Tok.getLoc(), End;
Chad Rosier1863f4f2013-04-10 17:35:30 +00001450 if (getParser().parsePrimaryExpr(Val, End))
Chad Rosier11c42f22012-10-26 18:04:20 +00001451 return 0;
1452
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001453 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosier11c42f22012-10-26 18:04:20 +00001454 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1455 const MCSymbol &Sym = SymRef->getSymbol();
1456 // FIXME: The SemaLookup will fail if the name is anything other then an
1457 // identifier.
1458 // FIXME: Pass a valid SMLoc.
Chad Rosiera4bc9432013-01-10 22:10:27 +00001459 bool IsVarDecl;
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001460 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1461 Size, Type, IsVarDecl))
Chad Rosier1e8f0532013-01-18 00:50:59 +00001462 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001463 }
1464 unsigned CVal;
1465 switch(OpKind) {
1466 default: llvm_unreachable("Unexpected operand kind!");
1467 case IOK_LENGTH: CVal = Length; break;
1468 case IOK_SIZE: CVal = Size; break;
1469 case IOK_TYPE: CVal = Type; break;
Chad Rosier11c42f22012-10-26 18:04:20 +00001470 }
1471
1472 // Rewrite the type operator and the C or C++ type or variable in terms of an
1473 // immediate. E.g. TYPE foo -> $$4
1474 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001475 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosier11c42f22012-10-26 18:04:20 +00001476
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001477 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierf3c04f62013-03-19 21:58:18 +00001478 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosier11c42f22012-10-26 18:04:20 +00001479}
1480
Devang Patel41b9dde2012-01-17 18:00:18 +00001481X86Operand *X86AsmParser::ParseIntelOperand() {
Chad Rosier70f47592013-04-10 20:07:47 +00001482 const AsmToken &Tok = Parser.getTok();
1483 SMLoc Start = Tok.getLoc(), End;
1484 StringRef AsmTokStr = Tok.getString();
Chad Rosier91c82662012-10-24 17:22:29 +00001485
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001486 // Offset, length, type and size operators.
1487 if (isParsingInlineAsm()) {
1488 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001489 return ParseIntelOffsetOfOperator();
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001490 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001491 return ParseIntelOperator(IOK_LENGTH);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001492 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001493 return ParseIntelOperator(IOK_SIZE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001494 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
Chad Rosier10d1d1c2013-04-09 20:44:09 +00001495 return ParseIntelOperator(IOK_TYPE);
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001496 }
Chad Rosier11c42f22012-10-26 18:04:20 +00001497
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001498 // Immediate.
Devang Patel41b9dde2012-01-17 18:00:18 +00001499 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1500 getLexer().is(AsmToken::Minus)) {
1501 const MCExpr *Val;
Chad Rosier1530ba52013-03-27 21:49:56 +00001502 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001503 if (!getParser().parseExpression(Val, End)) {
Chad Rosierf3c04f62013-03-19 21:58:18 +00001504 if (isParsingInlineAsm())
1505 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosier1530ba52013-03-27 21:49:56 +00001506 // Immediate.
1507 if (getLexer().isNot(AsmToken::LBrac))
1508 return X86Operand::CreateImm(Val, Start, End);
1509
1510 // Only positive immediates are valid.
1511 if (!isInteger) {
Chad Rosier70f47592013-04-10 20:07:47 +00001512 Error(Tok.getLoc(), "expected a positive immediate "
Chad Rosier1530ba52013-03-27 21:49:56 +00001513 "displacement before bracketed expr.");
1514 return 0;
1515 }
1516
1517 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1518 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1519 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Patel41b9dde2012-01-17 18:00:18 +00001520 }
1521 }
1522
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001523 // Register.
Devang Patelce6a2ca2012-01-20 22:32:05 +00001524 unsigned RegNo = 0;
1525 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier0397edd2012-10-04 23:59:38 +00001526 // If this is a segment register followed by a ':', then this is the start
1527 // of a memory reference, otherwise this is a normal register reference.
1528 if (getLexer().isNot(AsmToken::Colon))
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001529 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier0397edd2012-10-04 23:59:38 +00001530
1531 getParser().Lex(); // Eat the colon.
Chad Rosier1530ba52013-03-27 21:49:56 +00001532 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001533 }
1534
Chad Rosierd0ed73a2013-01-17 19:21:48 +00001535 // Memory operand.
Chad Rosier1530ba52013-03-27 21:49:56 +00001536 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel46831de2012-01-12 01:36:43 +00001537}
1538
Devang Patel4a6e7782012-01-12 18:03:40 +00001539X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001540 switch (getLexer().getKind()) {
1541 default:
Chris Lattnerb9270732010-04-17 18:56:34 +00001542 // Parse a memory operand with no segment register.
1543 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattnercc2ad082010-01-15 18:27:19 +00001544 case AsmToken::Percent: {
Chris Lattnerb9270732010-04-17 18:56:34 +00001545 // Read the register.
Chris Lattnercc2ad082010-01-15 18:27:19 +00001546 unsigned RegNo;
Chris Lattner0c2538f2010-01-15 18:51:29 +00001547 SMLoc Start, End;
1548 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001549 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001550 Error(Start, "%eiz and %riz can only be used as index registers",
1551 SMRange(Start, End));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001552 return 0;
1553 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001554
Chris Lattnerb9270732010-04-17 18:56:34 +00001555 // If this is a segment register followed by a ':', then this is the start
1556 // of a memory reference, otherwise this is a normal register reference.
1557 if (getLexer().isNot(AsmToken::Colon))
1558 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001559
Chris Lattnerb9270732010-04-17 18:56:34 +00001560 getParser().Lex(); // Eat the colon.
1561 return ParseMemOperand(RegNo, Start);
Chris Lattnercc2ad082010-01-15 18:27:19 +00001562 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001563 case AsmToken::Dollar: {
1564 // $42 -> immediate.
Sean Callanan936b0d32010-01-19 21:44:56 +00001565 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanana83fd7d2010-01-19 20:27:46 +00001566 Parser.Lex();
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001567 const MCExpr *Val;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001568 if (getParser().parseExpression(Val, End))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001569 return 0;
Chris Lattner528d00b2010-01-15 19:28:38 +00001570 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001571 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001572 }
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +00001573}
1574
Chris Lattnerb9270732010-04-17 18:56:34 +00001575/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1576/// has already been parsed if present.
Devang Patel4a6e7782012-01-12 18:03:40 +00001577X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001578
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001579 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1580 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner807a3bc2010-01-24 01:07:33 +00001581 // only way to do this without lookahead is to eat the '(' and see what is
1582 // after it.
Daniel Dunbar73da11e2009-08-31 08:08:38 +00001583 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001584 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001585 SMLoc ExprEnd;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001586 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001587
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001588 // After parsing the base expression we could either have a parenthesized
1589 // memory address or not. If not, return now. If so, eat the (.
1590 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001591 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001592 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001593 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001594 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001595 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001596
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001597 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001598 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001599 } else {
1600 // Okay, we have a '('. We don't know if this is an expression or not, but
1601 // so we have to eat the ( to see beyond it.
Sean Callanan936b0d32010-01-19 21:44:56 +00001602 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001603 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001604
Kevin Enderby7d912182009-09-03 17:15:07 +00001605 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001606 // Nothing to do here, fall into the code below with the '(' part of the
1607 // memory operand consumed.
1608 } else {
Chris Lattner528d00b2010-01-15 19:28:38 +00001609 SMLoc ExprEnd;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001610
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001611 // It must be an parenthesized expression, parse it now.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001612 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001613 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001614
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001615 // After parsing the base expression we could either have a parenthesized
1616 // memory address or not. If not, return now. If so, eat the (.
1617 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbara4fc8d92009-07-31 22:22:54 +00001618 // Unless we have a segment register, treat this as an immediate.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001619 if (SegReg == 0)
Daniel Dunbar76e5d702010-01-30 01:02:48 +00001620 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner015cfb12010-01-15 19:33:43 +00001621 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001622 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001623
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001624 // Eat the '('.
Sean Callanana83fd7d2010-01-19 20:27:46 +00001625 Parser.Lex();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001626 }
1627 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001628
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001629 // If we reached here, then we just ate the ( of the memory operand. Process
1630 // the rest of the memory operand.
Daniel Dunbar3ebf8482009-07-31 20:53:16 +00001631 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001632 SMLoc IndexLoc;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001633
Chris Lattner0c2538f2010-01-15 18:51:29 +00001634 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001635 SMLoc StartLoc, EndLoc;
1636 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001637 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer1930b002011-10-16 12:10:27 +00001638 Error(StartLoc, "eiz and riz can only be used as index registers",
1639 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001640 return 0;
1641 }
Chris Lattner0c2538f2010-01-15 18:51:29 +00001642 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001643
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001644 if (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001645 Parser.Lex(); // Eat the comma.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001646 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001647
1648 // Following the comma we should have either an index register, or a scale
1649 // value. We don't support the later form, but we want to parse it
1650 // correctly.
1651 //
1652 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes306a1f92010-07-24 00:06:39 +00001653 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7d912182009-09-03 17:15:07 +00001654 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner0c2538f2010-01-15 18:51:29 +00001655 SMLoc L;
1656 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001657
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001658 if (getLexer().isNot(AsmToken::RParen)) {
1659 // Parse the scale amount:
1660 // ::= ',' [scale-expression]
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001661 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001662 Error(Parser.getTok().getLoc(),
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001663 "expected comma in scale expression");
1664 return 0;
1665 }
Sean Callanana83fd7d2010-01-19 20:27:46 +00001666 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001667
1668 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001669 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001670
1671 int64_t ScaleVal;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001672 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderbydeed5aa2012-03-09 22:24:10 +00001673 Error(Loc, "expected scale expression");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001674 return 0;
Craig Topper6bf3ed42012-07-18 04:59:16 +00001675 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001676
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001677 // Validate the scale amount.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001678 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1679 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1680 return 0;
1681 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001682 Scale = (unsigned)ScaleVal;
1683 }
1684 }
1685 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001686 // A scale amount without an index is ignored.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001687 // index.
Sean Callanan936b0d32010-01-19 21:44:56 +00001688 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001689
1690 int64_t Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001691 if (getParser().parseAbsoluteExpression(Value))
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001692 return 0;
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001693
Daniel Dunbar94b84a12010-08-24 19:13:38 +00001694 if (Value != 1)
1695 Warning(Loc, "scale factor without index register is ignored");
1696 Scale = 1;
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001697 }
1698 }
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001699
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001700 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001701 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001702 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001703 return 0;
1704 }
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001705 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +00001706 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesd65cd1d2010-07-23 22:15:26 +00001707
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001708 // If we have both a base register and an index register make sure they are
1709 // both 64-bit or 32-bit registers.
Manman Rena0982042012-06-26 19:47:59 +00001710 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001711 if (BaseReg != 0 && IndexReg != 0) {
1712 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001713 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1714 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001715 IndexReg != X86::RIZ) {
1716 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1717 return 0;
1718 }
1719 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Rena0982042012-06-26 19:47:59 +00001720 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1721 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderbyfb3110b2012-03-12 21:32:09 +00001722 IndexReg != X86::EIZ){
1723 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1724 return 0;
1725 }
1726 }
1727
Chris Lattner015cfb12010-01-15 19:33:43 +00001728 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1729 MemStart, MemEnd);
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001730}
1731
Devang Patel4a6e7782012-01-12 18:03:40 +00001732bool X86AsmParser::
Chad Rosierf0e87202012-10-25 20:41:34 +00001733ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001734 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosierf0e87202012-10-25 20:41:34 +00001735 InstInfo = &Info;
Chris Lattner2cb092d2010-10-30 19:23:13 +00001736 StringRef PatchedName = Name;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001737
Chris Lattner7e8a99b2010-11-28 20:23:50 +00001738 // FIXME: Hack to recognize setneb as setne.
1739 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1740 PatchedName != "setb" && PatchedName != "setnb")
1741 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier51afe632012-06-27 22:34:28 +00001742
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001743 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1744 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001745 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001746 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1747 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Toppera0a603e2012-03-29 07:11:23 +00001748 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001749 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001750 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001751 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Toppera0a603e2012-03-29 07:11:23 +00001752 .Case("eq", 0x00)
1753 .Case("lt", 0x01)
1754 .Case("le", 0x02)
1755 .Case("unord", 0x03)
1756 .Case("neq", 0x04)
1757 .Case("nlt", 0x05)
1758 .Case("nle", 0x06)
1759 .Case("ord", 0x07)
1760 /* AVX only from here */
1761 .Case("eq_uq", 0x08)
1762 .Case("nge", 0x09)
Bruno Cardoso Lopes6c614512010-07-07 22:24:03 +00001763 .Case("ngt", 0x0A)
1764 .Case("false", 0x0B)
1765 .Case("neq_oq", 0x0C)
1766 .Case("ge", 0x0D)
1767 .Case("gt", 0x0E)
1768 .Case("true", 0x0F)
1769 .Case("eq_os", 0x10)
1770 .Case("lt_oq", 0x11)
1771 .Case("le_oq", 0x12)
1772 .Case("unord_s", 0x13)
1773 .Case("neq_us", 0x14)
1774 .Case("nlt_uq", 0x15)
1775 .Case("nle_uq", 0x16)
1776 .Case("ord_s", 0x17)
1777 .Case("eq_us", 0x18)
1778 .Case("nge_uq", 0x19)
1779 .Case("ngt_uq", 0x1A)
1780 .Case("false_os", 0x1B)
1781 .Case("neq_os", 0x1C)
1782 .Case("ge_oq", 0x1D)
1783 .Case("gt_oq", 0x1E)
1784 .Case("true_us", 0x1F)
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001785 .Default(~0U);
Craig Toppera0a603e2012-03-29 07:11:23 +00001786 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001787 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1788 getParser().getContext());
1789 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001790 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001791 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001792 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001793 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001794 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001795 } else {
1796 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes3183dd52010-06-23 21:10:57 +00001797 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001798 }
1799 }
1800 }
Bruno Cardoso Lopesea0e05a2010-07-23 18:41:12 +00001801
Daniel Dunbar3e0c9792010-02-10 21:19:28 +00001802 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001803
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001804 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001805 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencer530ce852010-10-09 11:00:50 +00001806
Chris Lattner086a83a2010-09-08 05:17:37 +00001807 // Determine whether this is an instruction prefix.
1808 bool isPrefix =
Chris Lattner2cb092d2010-10-30 19:23:13 +00001809 Name == "lock" || Name == "rep" ||
1810 Name == "repe" || Name == "repz" ||
Rafael Espindolaf6c05b12010-11-23 11:23:24 +00001811 Name == "repne" || Name == "repnz" ||
Rafael Espindolaeab08002010-11-27 20:29:45 +00001812 Name == "rex64" || Name == "data16";
Michael J. Spencer530ce852010-10-09 11:00:50 +00001813
1814
Chris Lattner086a83a2010-09-08 05:17:37 +00001815 // This does the actual operand parsing. Don't parse any more if we have a
1816 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1817 // just want to parse the "lock" as the first instruction and the "incl" as
1818 // the next one.
1819 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar71527c12009-08-11 05:00:25 +00001820
1821 // Parse '*' modifier.
1822 if (getLexer().is(AsmToken::Star)) {
Sean Callanan936b0d32010-01-19 21:44:56 +00001823 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner528d00b2010-01-15 19:28:38 +00001824 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanana83fd7d2010-01-19 20:27:46 +00001825 Parser.Lex(); // Eat the star.
Daniel Dunbar71527c12009-08-11 05:00:25 +00001826 }
1827
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001828 // Read the first operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001829 if (X86Operand *Op = ParseOperand())
1830 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001831 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001832 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001833 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001834 }
Daniel Dunbar0e767d72010-05-25 19:49:32 +00001835
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001836 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +00001837 Parser.Lex(); // Eat the comma.
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001838
1839 // Parse and remember the operand.
Chris Lattnera2bbb7c2010-01-15 18:44:13 +00001840 if (X86Operand *Op = ParseOperand())
1841 Operands.push_back(Op);
Chris Lattnera2a9d162010-09-11 16:18:25 +00001842 else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001843 Parser.eatToEndOfStatement();
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001844 return true;
Chris Lattnera2a9d162010-09-11 16:18:25 +00001845 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001846 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001847
Chris Lattnera2a9d162010-09-11 16:18:25 +00001848 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerdca25f62010-11-18 02:53:02 +00001849 SMLoc Loc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001850 Parser.eatToEndOfStatement();
Chris Lattnerdca25f62010-11-18 02:53:02 +00001851 return Error(Loc, "unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +00001852 }
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001853 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001854
Chris Lattner086a83a2010-09-08 05:17:37 +00001855 if (getLexer().is(AsmToken::EndOfStatement))
1856 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby87bc5912010-12-08 23:57:59 +00001857 else if (isPrefix && getLexer().is(AsmToken::Slash))
1858 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbare1fdb0e2009-07-28 22:40:46 +00001859
Devang Patel7cdb2ff2012-01-30 22:47:12 +00001860 if (ExtraImmOp && isParsingIntelSyntax())
1861 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1862
Chris Lattnerb6f8e822010-11-06 19:25:43 +00001863 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1864 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1865 // documented form in various unofficial manuals, so a lot of code uses it.
1866 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1867 Operands.size() == 3) {
1868 X86Operand &Op = *(X86Operand*)Operands.back();
1869 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1870 isa<MCConstantExpr>(Op.Mem.Disp) &&
1871 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1872 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1873 SMLoc Loc = Op.getEndLoc();
1874 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1875 delete &Op;
1876 }
1877 }
Joerg Sonnenbergerb7e635d2011-02-22 20:40:09 +00001878 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1879 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1880 Operands.size() == 3) {
1881 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1882 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1883 isa<MCConstantExpr>(Op.Mem.Disp) &&
1884 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1885 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1886 SMLoc Loc = Op.getEndLoc();
1887 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1888 delete &Op;
1889 }
1890 }
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001891 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1892 if (Name.startswith("ins") && Operands.size() == 3 &&
1893 (Name == "insb" || Name == "insw" || Name == "insl")) {
1894 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1895 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1896 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1897 Operands.pop_back();
1898 Operands.pop_back();
1899 delete &Op;
1900 delete &Op2;
1901 }
1902 }
1903
1904 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1905 if (Name.startswith("outs") && Operands.size() == 3 &&
1906 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1907 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1908 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1909 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1910 Operands.pop_back();
1911 Operands.pop_back();
1912 delete &Op;
1913 delete &Op2;
1914 }
1915 }
1916
1917 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1918 if (Name.startswith("movs") && Operands.size() == 3 &&
1919 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001920 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001921 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1922 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1923 if (isSrcOp(Op) && isDstOp(Op2)) {
1924 Operands.pop_back();
1925 Operands.pop_back();
1926 delete &Op;
1927 delete &Op2;
1928 }
1929 }
1930 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1931 if (Name.startswith("lods") && Operands.size() == 3 &&
1932 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001933 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001934 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1935 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1936 if (isSrcOp(*Op1) && Op2->isReg()) {
1937 const char *ins;
1938 unsigned reg = Op2->getReg();
1939 bool isLods = Name == "lods";
1940 if (reg == X86::AL && (isLods || Name == "lodsb"))
1941 ins = "lodsb";
1942 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1943 ins = "lodsw";
1944 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1945 ins = "lodsl";
1946 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1947 ins = "lodsq";
1948 else
1949 ins = NULL;
1950 if (ins != NULL) {
1951 Operands.pop_back();
1952 Operands.pop_back();
1953 delete Op1;
1954 delete Op2;
1955 if (Name != ins)
1956 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1957 }
1958 }
1959 }
1960 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1961 if (Name.startswith("stos") && Operands.size() == 3 &&
1962 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001963 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger3fbfcc02011-03-18 11:59:40 +00001964 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1965 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1966 if (isDstOp(*Op2) && Op1->isReg()) {
1967 const char *ins;
1968 unsigned reg = Op1->getReg();
1969 bool isStos = Name == "stos";
1970 if (reg == X86::AL && (isStos || Name == "stosb"))
1971 ins = "stosb";
1972 else if (reg == X86::AX && (isStos || Name == "stosw"))
1973 ins = "stosw";
1974 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1975 ins = "stosl";
1976 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1977 ins = "stosq";
1978 else
1979 ins = NULL;
1980 if (ins != NULL) {
1981 Operands.pop_back();
1982 Operands.pop_back();
1983 delete Op1;
1984 delete Op2;
1985 if (Name != ins)
1986 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1987 }
1988 }
1989 }
1990
Chris Lattner4bd21712010-09-15 04:33:27 +00001991 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattner30561ab2010-09-11 16:32:12 +00001992 // "shift <op>".
Daniel Dunbar18fc3442010-03-13 00:47:29 +00001993 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner64f91b92010-11-06 21:23:40 +00001994 Name.startswith("shl") || Name.startswith("sal") ||
1995 Name.startswith("rcl") || Name.startswith("rcr") ||
1996 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00001997 Operands.size() == 3) {
Devang Patel9a9bb5c2012-01-30 20:02:42 +00001998 if (isParsingIntelSyntax()) {
Devang Patela410ed32012-01-24 21:43:36 +00001999 // Intel syntax
2000 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
2001 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002002 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2003 delete Operands[2];
2004 Operands.pop_back();
Devang Patela410ed32012-01-24 21:43:36 +00002005 }
2006 } else {
2007 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2008 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper6bf3ed42012-07-18 04:59:16 +00002009 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
2010 delete Operands[1];
2011 Operands.erase(Operands.begin() + 1);
Devang Patela410ed32012-01-24 21:43:36 +00002012 }
Chris Lattner4cfbcdc2010-09-06 18:32:06 +00002013 }
Daniel Dunbarfbd12cc2010-03-20 22:36:38 +00002014 }
Chad Rosier51afe632012-06-27 22:34:28 +00002015
Chris Lattnerfc4fe002011-04-09 19:41:05 +00002016 // Transforms "int $3" into "int3" as a size optimization. We can't write an
2017 // instalias with an immediate operand yet.
2018 if (Name == "int" && Operands.size() == 2) {
2019 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
2020 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
2021 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
2022 delete Operands[1];
2023 Operands.erase(Operands.begin() + 1);
2024 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2025 }
2026 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002027
Chris Lattnerf29c0b62010-01-14 22:21:20 +00002028 return false;
Daniel Dunbar3c2a8932009-07-20 18:55:04 +00002029}
2030
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002031static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2032 bool isCmp) {
2033 MCInst TmpInst;
2034 TmpInst.setOpcode(Opcode);
2035 if (!isCmp)
2036 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2037 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2038 TmpInst.addOperand(Inst.getOperand(0));
2039 Inst = TmpInst;
2040 return true;
2041}
2042
2043static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2044 bool isCmp = false) {
2045 if (!Inst.getOperand(0).isImm() ||
2046 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2047 return false;
2048
2049 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2050}
2051
2052static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2053 bool isCmp = false) {
2054 if (!Inst.getOperand(0).isImm() ||
2055 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2056 return false;
2057
2058 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2059}
2060
2061static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2062 bool isCmp = false) {
2063 if (!Inst.getOperand(0).isImm() ||
2064 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2065 return false;
2066
2067 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2068}
2069
Devang Patel4a6e7782012-01-12 18:03:40 +00002070bool X86AsmParser::
Devang Patelde47cce2012-01-18 22:42:29 +00002071processInstruction(MCInst &Inst,
2072 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2073 switch (Inst.getOpcode()) {
2074 default: return false;
Craig Topper7e9a1cb2013-03-18 02:53:34 +00002075 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2076 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2077 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2078 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2079 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2080 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2081 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2082 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2083 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2084 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2085 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2086 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2087 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2088 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2089 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2090 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2091 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2092 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper0498b882013-03-18 03:34:55 +00002093 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2094 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2095 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2096 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2097 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2098 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelde47cce2012-01-18 22:42:29 +00002099 }
Devang Patelde47cce2012-01-18 22:42:29 +00002100}
2101
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002102static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelde47cce2012-01-18 22:42:29 +00002103bool X86AsmParser::
Chad Rosier49963552012-10-13 00:26:04 +00002104MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattnera63292a2010-09-29 01:50:45 +00002105 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier49963552012-10-13 00:26:04 +00002106 MCStreamer &Out, unsigned &ErrorInfo,
2107 bool MatchingInlineAsm) {
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002108 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattnera63292a2010-09-29 01:50:45 +00002109 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2110 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosier3d4bc622012-08-21 19:36:59 +00002111 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002112
Chris Lattnera63292a2010-09-29 01:50:45 +00002113 // First, handle aliases that expand to multiple instructions.
2114 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier3b1336c2012-08-28 23:57:47 +00002115 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner4869d342010-11-06 19:57:21 +00002116 // call.
Andrew Trickedd006c2010-10-22 03:58:29 +00002117 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner06913232010-10-30 18:07:17 +00002118 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner73a7cae2010-09-30 17:11:29 +00002119 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby20b021c2010-10-27 02:53:04 +00002120 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattnera63292a2010-09-29 01:50:45 +00002121 MCInst Inst;
2122 Inst.setOpcode(X86::WAIT);
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002123 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002124 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002125 Out.EmitInstruction(Inst);
Chris Lattnera63292a2010-09-29 01:50:45 +00002126
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002127 const char *Repl =
2128 StringSwitch<const char*>(Op->getToken())
Chris Lattner06913232010-10-30 18:07:17 +00002129 .Case("finit", "fninit")
2130 .Case("fsave", "fnsave")
2131 .Case("fstcw", "fnstcw")
2132 .Case("fstcww", "fnstcw")
Chris Lattner73a7cae2010-09-30 17:11:29 +00002133 .Case("fstenv", "fnstenv")
Chris Lattner06913232010-10-30 18:07:17 +00002134 .Case("fstsw", "fnstsw")
2135 .Case("fstsww", "fnstsw")
2136 .Case("fclex", "fnclex")
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002137 .Default(0);
2138 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramer14e909a2010-10-01 12:25:27 +00002139 delete Operands[0];
Chris Lattneradc0dbe2010-09-30 16:39:29 +00002140 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattnera63292a2010-09-29 01:50:45 +00002141 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002142
Chris Lattner628fbec2010-09-06 21:54:15 +00002143 bool WasOriginallyInvalidOperand = false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002144 MCInst Inst;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002145
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002146 // First, try a direct match.
Chad Rosier2f480a82012-10-12 22:53:36 +00002147 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier49963552012-10-13 00:26:04 +00002148 ErrorInfo, MatchingInlineAsm,
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002149 isParsingIntelSyntax())) {
Jim Grosbach120a96a2011-08-15 23:03:29 +00002150 default: break;
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002151 case Match_Success:
Devang Patelde47cce2012-01-18 22:42:29 +00002152 // Some instructions need post-processing to, for example, tweak which
2153 // encoding is selected. Loop on it while changes happen so the
Chad Rosier51afe632012-06-27 22:34:28 +00002154 // individual transformations can chain off each other.
Chad Rosier4453e842012-10-12 23:09:25 +00002155 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002156 while (processInstruction(Inst, Operands))
2157 ;
Devang Patelde47cce2012-01-18 22:42:29 +00002158
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002159 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002160 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002161 Out.EmitInstruction(Inst);
2162 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002163 return false;
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002164 case Match_MissingFeature: {
2165 assert(ErrorInfo && "Unknown missing feature!");
2166 // Special case the error message for the very common case where only
2167 // a single subtarget feature is missing.
2168 std::string Msg = "instruction requires:";
2169 unsigned Mask = 1;
2170 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2171 if (ErrorInfo & Mask) {
2172 Msg += " ";
2173 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2174 }
2175 Mask <<= 1;
2176 }
2177 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2178 }
Chris Lattner628fbec2010-09-06 21:54:15 +00002179 case Match_InvalidOperand:
2180 WasOriginallyInvalidOperand = true;
2181 break;
2182 case Match_MnemonicFail:
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002183 break;
2184 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002185
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002186 // FIXME: Ideally, we would only attempt suffix matches for things which are
2187 // valid prefixes, and we could just infer the right unambiguous
2188 // type. However, that requires substantially more matcher support than the
2189 // following hack.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002190
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002191 // Change the operand to point to a temporary token.
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002192 StringRef Base = Op->getToken();
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002193 SmallString<16> Tmp;
2194 Tmp += Base;
2195 Tmp += ' ';
2196 Op->setTokenValue(Tmp.str());
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002197
Chris Lattnerfab94132010-11-06 18:28:02 +00002198 // If this instruction starts with an 'f', then it is a floating point stack
2199 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2200 // 80-bit floating point, which use the suffixes s,l,t respectively.
2201 //
2202 // Otherwise, we assume that this may be an integer instruction, which comes
2203 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2204 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier51afe632012-06-27 22:34:28 +00002205
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002206 // Check for the various suffix matches.
Chris Lattnerfab94132010-11-06 18:28:02 +00002207 Tmp[Base.size()] = Suffixes[0];
2208 unsigned ErrorInfoIgnore;
Duncan Sands2cb41d32013-03-01 09:46:03 +00002209 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach120a96a2011-08-15 23:03:29 +00002210 unsigned Match1, Match2, Match3, Match4;
Chad Rosier51afe632012-06-27 22:34:28 +00002211
Chad Rosier2f480a82012-10-12 22:53:36 +00002212 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2213 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002214 // If this returned as a missing feature failure, remember that.
2215 if (Match1 == Match_MissingFeature)
2216 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002217 Tmp[Base.size()] = Suffixes[1];
Chad Rosier2f480a82012-10-12 22:53:36 +00002218 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2219 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002220 // If this returned as a missing feature failure, remember that.
2221 if (Match2 == Match_MissingFeature)
2222 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002223 Tmp[Base.size()] = Suffixes[2];
Chad Rosier2f480a82012-10-12 22:53:36 +00002224 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2225 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002226 // If this returned as a missing feature failure, remember that.
2227 if (Match3 == Match_MissingFeature)
2228 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfab94132010-11-06 18:28:02 +00002229 Tmp[Base.size()] = Suffixes[3];
Chad Rosier2f480a82012-10-12 22:53:36 +00002230 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2231 isParsingIntelSyntax());
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002232 // If this returned as a missing feature failure, remember that.
2233 if (Match4 == Match_MissingFeature)
2234 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002235
2236 // Restore the old token.
2237 Op->setTokenValue(Base);
2238
2239 // If exactly one matched, then we treat that as a successful match (and the
2240 // instruction will already have been filled in correctly, since the failing
2241 // matches won't have modified it).
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002242 unsigned NumSuccessfulMatches =
Chris Lattnerfab94132010-11-06 18:28:02 +00002243 (Match1 == Match_Success) + (Match2 == Match_Success) +
2244 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattnerb44fd242010-09-29 01:42:58 +00002245 if (NumSuccessfulMatches == 1) {
Jim Grosbach8f28dbd2012-01-27 00:51:27 +00002246 Inst.setLoc(IDLoc);
Chad Rosier4453e842012-10-12 23:09:25 +00002247 if (!MatchingInlineAsm)
Chad Rosierf4e35dc2012-10-01 23:45:51 +00002248 Out.EmitInstruction(Inst);
2249 Opcode = Inst.getOpcode();
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002250 return false;
Chris Lattnerb44fd242010-09-29 01:42:58 +00002251 }
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002252
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002253 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbar2ecc3bb2010-08-12 00:55:38 +00002254
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002255 // If we had multiple suffix matches, then identify this as an ambiguous
2256 // match.
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002257 if (NumSuccessfulMatches > 1) {
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002258 char MatchChars[4];
2259 unsigned NumMatches = 0;
Chris Lattnerfab94132010-11-06 18:28:02 +00002260 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2261 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2262 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2263 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002264
2265 SmallString<126> Msg;
2266 raw_svector_ostream OS(Msg);
2267 OS << "ambiguous instructions require an explicit suffix (could be ";
2268 for (unsigned i = 0; i != NumMatches; ++i) {
2269 if (i != 0)
2270 OS << ", ";
2271 if (i + 1 == NumMatches)
2272 OS << "or ";
2273 OS << "'" << Base << MatchChars[i] << "'";
2274 }
2275 OS << ")";
Chad Rosier4453e842012-10-12 23:09:25 +00002276 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002277 return true;
Daniel Dunbar7d7b4d12010-08-12 00:55:42 +00002278 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002279
Chris Lattner628fbec2010-09-06 21:54:15 +00002280 // Okay, we know that none of the variants matched successfully.
Michael J. Spencer530ce852010-10-09 11:00:50 +00002281
Chris Lattner628fbec2010-09-06 21:54:15 +00002282 // If all of the instructions reported an invalid mnemonic, then the original
2283 // mnemonic was invalid.
Chris Lattnerfab94132010-11-06 18:28:02 +00002284 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2285 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattner339cc7b2010-09-06 22:11:18 +00002286 if (!WasOriginallyInvalidOperand) {
Chad Rosier4453e842012-10-12 23:09:25 +00002287 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosiercf172e52012-08-22 19:14:29 +00002288 Op->getLocRange();
Benjamin Kramerd416bae2011-10-16 11:28:29 +00002289 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier4453e842012-10-12 23:09:25 +00002290 Ranges, MatchingInlineAsm);
Chris Lattner339cc7b2010-09-06 22:11:18 +00002291 }
2292
2293 // Recover location info for the operand if we know which was the problem.
Chad Rosier49963552012-10-13 00:26:04 +00002294 if (ErrorInfo != ~0U) {
2295 if (ErrorInfo >= Operands.size())
Chad Rosier3d4bc622012-08-21 19:36:59 +00002296 return Error(IDLoc, "too few operands for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002297 EmptyRanges, MatchingInlineAsm);
Michael J. Spencer530ce852010-10-09 11:00:50 +00002298
Chad Rosier49963552012-10-13 00:26:04 +00002299 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnera3a06812011-10-16 04:47:35 +00002300 if (Operand->getStartLoc().isValid()) {
2301 SMRange OperandRange = Operand->getLocRange();
2302 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier4453e842012-10-12 23:09:25 +00002303 OperandRange, MatchingInlineAsm);
Chris Lattnera3a06812011-10-16 04:47:35 +00002304 }
Chris Lattner339cc7b2010-09-06 22:11:18 +00002305 }
2306
Chad Rosier3d4bc622012-08-21 19:36:59 +00002307 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002308 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002309 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002310
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002311 // If one instruction matched with a missing feature, report this as a
2312 // missing feature.
Chris Lattnerfab94132010-11-06 18:28:02 +00002313 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2314 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002315 std::string Msg = "instruction requires:";
2316 unsigned Mask = 1;
2317 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2318 if (ErrorInfoMissingFeature & Mask) {
2319 Msg += " ";
2320 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2321 }
2322 Mask <<= 1;
2323 }
2324 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002325 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002326
Chris Lattner628fbec2010-09-06 21:54:15 +00002327 // If one instruction matched with an invalid operand, report this as an
2328 // operand failure.
Chris Lattnerfab94132010-11-06 18:28:02 +00002329 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2330 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosier3d4bc622012-08-21 19:36:59 +00002331 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier4453e842012-10-12 23:09:25 +00002332 MatchingInlineAsm);
Chris Lattner628fbec2010-09-06 21:54:15 +00002333 return true;
2334 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00002335
Chris Lattnerb4be28f2010-09-06 20:08:02 +00002336 // If all of these were an outright failure, report it in a useless way.
Chad Rosier3d4bc622012-08-21 19:36:59 +00002337 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier4453e842012-10-12 23:09:25 +00002338 EmptyRanges, MatchingInlineAsm);
Daniel Dunbar9b816a12010-05-04 16:12:42 +00002339 return true;
2340}
2341
2342
Devang Patel4a6e7782012-01-12 18:03:40 +00002343bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002344 StringRef IDVal = DirectiveID.getIdentifier();
2345 if (IDVal == ".word")
2346 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Cheng481ebb02011-07-27 00:38:12 +00002347 else if (IDVal.startswith(".code"))
2348 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier6f8d8b22012-09-10 20:54:39 +00002349 else if (IDVal.startswith(".att_syntax")) {
2350 getParser().setAssemblerDialect(0);
2351 return false;
2352 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patela173ee52012-01-31 18:14:05 +00002353 getParser().setAssemblerDialect(1);
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002354 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2355 if(Parser.getTok().getString() == "noprefix") {
Craig Topper6bf3ed42012-07-18 04:59:16 +00002356 // FIXME : Handle noprefix
2357 Parser.Lex();
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002358 } else
Craig Topper6bf3ed42012-07-18 04:59:16 +00002359 return true;
Devang Patel9a9bb5c2012-01-30 20:02:42 +00002360 }
2361 return false;
2362 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002363 return true;
2364}
2365
2366/// ParseDirectiveWord
2367/// ::= .word [ expression (, expression)* ]
Devang Patel4a6e7782012-01-12 18:03:40 +00002368bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner72c0b592010-10-30 17:38:55 +00002369 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2370 for (;;) {
2371 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002372 if (getParser().parseExpression(Value))
Chris Lattner72c0b592010-10-30 17:38:55 +00002373 return true;
Chad Rosier51afe632012-06-27 22:34:28 +00002374
Eric Christopherbf7bc492013-01-09 03:52:05 +00002375 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier51afe632012-06-27 22:34:28 +00002376
Chris Lattner72c0b592010-10-30 17:38:55 +00002377 if (getLexer().is(AsmToken::EndOfStatement))
2378 break;
Chad Rosier51afe632012-06-27 22:34:28 +00002379
Chris Lattner72c0b592010-10-30 17:38:55 +00002380 // FIXME: Improve diagnostic.
2381 if (getLexer().isNot(AsmToken::Comma))
2382 return Error(L, "unexpected token in directive");
2383 Parser.Lex();
2384 }
2385 }
Chad Rosier51afe632012-06-27 22:34:28 +00002386
Chris Lattner72c0b592010-10-30 17:38:55 +00002387 Parser.Lex();
2388 return false;
2389}
2390
Evan Cheng481ebb02011-07-27 00:38:12 +00002391/// ParseDirectiveCode
2392/// ::= .code32 | .code64
Devang Patel4a6e7782012-01-12 18:03:40 +00002393bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Cheng481ebb02011-07-27 00:38:12 +00002394 if (IDVal == ".code32") {
2395 Parser.Lex();
2396 if (is64BitMode()) {
2397 SwitchMode();
2398 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2399 }
2400 } else if (IDVal == ".code64") {
2401 Parser.Lex();
2402 if (!is64BitMode()) {
2403 SwitchMode();
2404 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2405 }
2406 } else {
2407 return Error(L, "unexpected directive " + IDVal);
2408 }
Chris Lattner72c0b592010-10-30 17:38:55 +00002409
Evan Cheng481ebb02011-07-27 00:38:12 +00002410 return false;
2411}
Chris Lattner72c0b592010-10-30 17:38:55 +00002412
Daniel Dunbar71475772009-07-17 20:42:00 +00002413// Force static initialization.
2414extern "C" void LLVMInitializeX86AsmParser() {
Devang Patel4a6e7782012-01-12 18:03:40 +00002415 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2416 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar71475772009-07-17 20:42:00 +00002417}
Daniel Dunbar00331992009-07-29 00:02:19 +00002418
Chris Lattner3e4582a2010-09-06 19:11:01 +00002419#define GET_REGISTER_MATCHER
2420#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach6f1f41b2012-11-14 18:04:47 +00002421#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar00331992009-07-29 00:02:19 +00002422#include "X86GenAsmMatcher.inc"