blob: 4f4fad9b9f9b4bad4fc40c2423ebee4d5d855ef8 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Chad Rosier4284e172012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chad Rosier30c729b2013-04-02 20:02:33 +000016#include "llvm/MC/MCContext.h"
Chandler Carruthd04a8d42012-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 Dunbar16cdcb32009-07-28 22:40:46 +000027#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000028#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000029#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000030
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000031using namespace llvm;
32
33namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000034struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035
Devang Pateldd929fc2012-01-12 18:03:40 +000036class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000037 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000039 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000041 MCAsmParser &getParser() const { return Parser; }
42
43 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
44
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000045 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000046 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000047 bool MatchingInlineAsm = false) {
48 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000049 return Parser.Error(L, Msg, Ranges);
50 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000051
Devang Pateld37ad242012-01-17 18:00:18 +000052 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
53 Error(Loc, Msg);
54 return 0;
55 }
56
Chris Lattner309264d2010-01-15 18:44:13 +000057 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000058 X86Operand *ParseATTOperand();
59 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000060 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier505bca32013-01-17 19:21:48 +000061 X86Operand *ParseIntelOperator(SMLoc StartLoc, unsigned OpKind);
Chad Rosierdd40e8c2013-03-27 21:49:56 +000062 X86Operand *ParseIntelMemOperand(unsigned SegReg, uint64_t ImmDisp,
63 SMLoc StartLoc);
Chad Rosier6b369ce2013-04-08 17:43:47 +000064 X86Operand *ParseIntelBracExpression(unsigned SegReg, SMLoc SizeDirLoc,
65 uint64_t ImmDisp, unsigned Size);
Chad Rosier30c729b2013-04-02 20:02:33 +000066 X86Operand *ParseIntelVarWithQualifier(const MCExpr *&Disp,
67 SMLoc &IdentStart);
Chris Lattnereef6d782010-04-17 18:56:34 +000068 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000069
Chad Rosierd3e74162013-03-19 21:11:56 +000070 X86Operand *CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start, SMLoc End,
71 SMLoc SizeDirLoc, unsigned Size);
72
Chad Rosier5e6b37f2012-10-25 17:37:43 +000073 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
74 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000075
Kevin Enderby9c656452009-09-10 20:51:44 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000077 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000078
Devang Patelb8ba13f2012-01-18 22:42:29 +000079 bool processInstruction(MCInst &Inst,
80 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
81
Chad Rosier84125ca2012-10-13 00:26:04 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000084 MCStreamer &Out, unsigned &ErrorInfo,
85 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000086
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000087 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000088 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000089 bool isSrcOp(X86Operand &Op);
90
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000091 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
92 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000093 bool isDstOp(X86Operand &Op);
94
Evan Cheng59ee62d2011-07-11 03:57:24 +000095 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000096 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000097 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000098 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000099 void SwitchMode() {
100 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
101 setAvailableFeatures(FB);
102 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000103
Daniel Dunbar54074b52010-07-19 05:44:09 +0000104 /// @name Auto-generated Matcher Functions
105 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000106
Chris Lattner0692ee62010-09-06 19:11:01 +0000107#define GET_ASSEMBLER_HEADER
108#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000109
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000110 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111
112public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000113 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000114 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000115
Daniel Dunbar54074b52010-07-19 05:44:09 +0000116 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000117 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000118 }
Roman Divackybf755322011-01-27 17:14:22 +0000119 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000120
Chad Rosier6a020a72012-10-25 20:41:34 +0000121 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
122 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000123 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000124
125 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000126
127 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000128 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000129 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000130};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000131} // end anonymous namespace
132
Sean Callanane9b466d2010-01-23 00:40:33 +0000133/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000134/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000135
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000136static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000137
138/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000139
Craig Topper76bd9382012-07-18 04:59:16 +0000140static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000141 return (( Value <= 0x000000000000007FULL)||
142 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
143 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
144}
145
146static bool isImmSExti32i8Value(uint64_t Value) {
147 return (( Value <= 0x000000000000007FULL)||
148 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
149 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
150}
151
152static bool isImmZExtu32u8Value(uint64_t Value) {
153 return (Value <= 0x00000000000000FFULL);
154}
155
156static bool isImmSExti64i8Value(uint64_t Value) {
157 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000158 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000159}
160
161static bool isImmSExti64i32Value(uint64_t Value) {
162 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000163 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000164}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000165namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166
167/// X86Operand - Instances of this class represent a parsed X86 machine
168/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000169struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000170 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000171 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 Register,
173 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000174 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000175 } Kind;
176
Chris Lattner29ef9a22010-01-15 18:51:29 +0000177 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000178 SMLoc OffsetOfLoc;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000179 bool AddressOf;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000180
Eric Christophera286fc02013-03-15 00:42:55 +0000181 struct TokOp {
182 const char *Data;
183 unsigned Length;
184 };
185
186 struct RegOp {
187 unsigned RegNo;
188 };
189
190 struct ImmOp {
191 const MCExpr *Val;
Eric Christophera286fc02013-03-15 00:42:55 +0000192 };
193
194 struct MemOp {
195 unsigned SegReg;
196 const MCExpr *Disp;
197 unsigned BaseReg;
198 unsigned IndexReg;
199 unsigned Scale;
200 unsigned Size;
Eric Christophera286fc02013-03-15 00:42:55 +0000201 };
202
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000203 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000204 struct TokOp Tok;
205 struct RegOp Reg;
206 struct ImmOp Imm;
207 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000208 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000209
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000210 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000211 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000212
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000213 /// getStartLoc - Get the location of the first token of this operand.
214 SMLoc getStartLoc() const { return StartLoc; }
215 /// getEndLoc - Get the location of the last token of this operand.
216 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000217 /// getLocRange - Get the range between the first and last token of this
218 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000219 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000220 /// getOffsetOfLoc - Get the location of the offset operator.
221 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000222
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000223 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000224
Daniel Dunbar20927f22009-08-07 08:26:05 +0000225 StringRef getToken() const {
226 assert(Kind == Token && "Invalid access!");
227 return StringRef(Tok.Data, Tok.Length);
228 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000229 void setTokenValue(StringRef Value) {
230 assert(Kind == Token && "Invalid access!");
231 Tok.Data = Value.data();
232 Tok.Length = Value.size();
233 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000234
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000235 unsigned getReg() const {
236 assert(Kind == Register && "Invalid access!");
237 return Reg.RegNo;
238 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000239
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000240 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000241 assert(Kind == Immediate && "Invalid access!");
242 return Imm.Val;
243 }
244
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000245 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000246 assert(Kind == Memory && "Invalid access!");
247 return Mem.Disp;
248 }
249 unsigned getMemSegReg() const {
250 assert(Kind == Memory && "Invalid access!");
251 return Mem.SegReg;
252 }
253 unsigned getMemBaseReg() const {
254 assert(Kind == Memory && "Invalid access!");
255 return Mem.BaseReg;
256 }
257 unsigned getMemIndexReg() const {
258 assert(Kind == Memory && "Invalid access!");
259 return Mem.IndexReg;
260 }
261 unsigned getMemScale() const {
262 assert(Kind == Memory && "Invalid access!");
263 return Mem.Scale;
264 }
265
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000266 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000267
268 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000269
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000270 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000271 if (!isImm())
272 return false;
273
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 // If this isn't a constant expr, just assume it fits and let relaxation
275 // handle it.
276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
277 if (!CE)
278 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000279
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 // Otherwise, check the value is in a range that makes sense for this
281 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000282 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000283 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000284 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000285 if (!isImm())
286 return false;
287
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000288 // If this isn't a constant expr, just assume it fits and let relaxation
289 // handle it.
290 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
291 if (!CE)
292 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000293
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000294 // Otherwise, check the value is in a range that makes sense for this
295 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000296 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000297 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000298 bool isImmZExtu32u8() const {
299 if (!isImm())
300 return false;
301
302 // If this isn't a constant expr, just assume it fits and let relaxation
303 // handle it.
304 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
305 if (!CE)
306 return true;
307
308 // Otherwise, check the value is in a range that makes sense for this
309 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000310 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000311 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000312 bool isImmSExti64i8() const {
313 if (!isImm())
314 return false;
315
316 // If this isn't a constant expr, just assume it fits and let relaxation
317 // handle it.
318 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
319 if (!CE)
320 return true;
321
322 // Otherwise, check the value is in a range that makes sense for this
323 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000324 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000325 }
326 bool isImmSExti64i32() const {
327 if (!isImm())
328 return false;
329
330 // If this isn't a constant expr, just assume it fits and let relaxation
331 // handle it.
332 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
333 if (!CE)
334 return true;
335
336 // Otherwise, check the value is in a range that makes sense for this
337 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000338 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000339 }
340
Chad Rosiera703fb92012-10-22 19:50:35 +0000341 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000342 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000343 }
344
Chad Rosierc1ec2072013-01-10 22:10:27 +0000345 bool needAddressOf() const {
346 return AddressOf;
347 }
348
Daniel Dunbar20927f22009-08-07 08:26:05 +0000349 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000350 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000351 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000352 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000353 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000354 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000355 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000356 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000357 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000358 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000359 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000360 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000361 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000362 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000363 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000364 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000365 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000366 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000367 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000368 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000369 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000370 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000371
Craig Topper75dc33a2012-07-18 04:11:12 +0000372 bool isMemVX32() const {
373 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
374 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
375 }
376 bool isMemVY32() const {
377 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
378 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
379 }
380 bool isMemVX64() const {
381 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
382 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
383 }
384 bool isMemVY64() const {
385 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
386 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
387 }
388
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000389 bool isAbsMem() const {
390 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000391 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000392 }
393
Daniel Dunbar20927f22009-08-07 08:26:05 +0000394 bool isReg() const { return Kind == Register; }
395
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000396 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
397 // Add as immediates when possible.
398 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
399 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
400 else
401 Inst.addOperand(MCOperand::CreateExpr(Expr));
402 }
403
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000404 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000405 assert(N == 1 && "Invalid number of operands!");
406 Inst.addOperand(MCOperand::CreateReg(getReg()));
407 }
408
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000409 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000410 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000411 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000412 }
413
Chad Rosier36b8fed2012-06-27 22:34:28 +0000414 void addMem8Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000416 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000417 void addMem16Operands(MCInst &Inst, unsigned N) const {
418 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000419 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000420 void addMem32Operands(MCInst &Inst, unsigned N) const {
421 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000422 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000423 void addMem64Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000425 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000426 void addMem80Operands(MCInst &Inst, unsigned N) const {
427 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000428 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000429 void addMem128Operands(MCInst &Inst, unsigned N) const {
430 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000431 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000432 void addMem256Operands(MCInst &Inst, unsigned N) const {
433 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000434 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000435 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
436 addMemOperands(Inst, N);
437 }
438 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
439 addMemOperands(Inst, N);
440 }
441 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
442 addMemOperands(Inst, N);
443 }
444 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
445 addMemOperands(Inst, N);
446 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000447
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000448 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000449 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000450 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
451 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
452 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000453 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000454 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
455 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000456
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000457 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
458 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000459 // Add as immediates when possible.
460 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
461 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
462 else
463 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000464 }
465
Chris Lattnerb4307b32010-01-15 19:28:38 +0000466 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000467 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000468 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000469 Res->Tok.Data = Str.data();
470 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000471 return Res;
472 }
473
Chad Rosierc0a14b82012-10-24 17:22:29 +0000474 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000475 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000476 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000477 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000478 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000479 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000480 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000481 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000482 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000483
Chad Rosier811ddf62013-03-19 21:58:18 +0000484 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000485 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000486 Res->Imm.Val = Val;
487 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000488 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000489
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000490 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000491 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000492 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000493 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
494 Res->Mem.SegReg = 0;
495 Res->Mem.Disp = Disp;
496 Res->Mem.BaseReg = 0;
497 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000498 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000499 Res->Mem.Size = Size;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000500 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000501 return Res;
502 }
503
504 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000505 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
506 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000507 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000508 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000509 // We should never just have a displacement, that should be parsed as an
510 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000511 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
512
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000513 // The scale should always be one of {1,2,4,8}.
514 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000515 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000516 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000517 Res->Mem.SegReg = SegReg;
518 Res->Mem.Disp = Disp;
519 Res->Mem.BaseReg = BaseReg;
520 Res->Mem.IndexReg = IndexReg;
521 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000522 Res->Mem.Size = Size;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000523 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000524 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000525 }
526};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000527
Chris Lattner37dfdec2009-07-29 06:33:53 +0000528} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000529
Devang Pateldd929fc2012-01-12 18:03:40 +0000530bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000531 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000532
533 return (Op.isMem() &&
534 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
535 isa<MCConstantExpr>(Op.Mem.Disp) &&
536 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
537 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
538}
539
Devang Pateldd929fc2012-01-12 18:03:40 +0000540bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000541 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000542
Chad Rosier36b8fed2012-06-27 22:34:28 +0000543 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000544 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000545 isa<MCConstantExpr>(Op.Mem.Disp) &&
546 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
547 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
548}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549
Devang Pateldd929fc2012-01-12 18:03:40 +0000550bool X86AsmParser::ParseRegister(unsigned &RegNo,
551 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000552 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000553 const AsmToken &PercentTok = Parser.getTok();
554 StartLoc = PercentTok.getLoc();
555
556 // If we encounter a %, ignore it. This code handles registers with and
557 // without the prefix, unprefixed registers can occur in cfi directives.
558 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000559 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000560
Sean Callanan18b83232010-01-19 21:44:56 +0000561 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000562 EndLoc = Tok.getEndLoc();
563
Devang Patel1aea4302012-01-20 22:32:05 +0000564 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000565 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000566 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000567 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000568 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000569
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000570 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000571
Chris Lattner33d60d52010-09-22 04:11:10 +0000572 // If the match failed, try the register name as lowercase.
573 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000574 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000575
Evan Cheng5de728c2011-07-27 23:22:03 +0000576 if (!is64BitMode()) {
577 // FIXME: This should be done using Requires<In32BitMode> and
578 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
579 // checked.
580 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
581 // REX prefix.
582 if (RegNo == X86::RIZ ||
583 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
584 X86II::isX86_64NonExtLowByteReg(RegNo) ||
585 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000586 return Error(StartLoc, "register %"
587 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000588 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000589 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000590
Chris Lattner33d60d52010-09-22 04:11:10 +0000591 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
592 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000593 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000594 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000595
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000596 // Check to see if we have '(4)' after %st.
597 if (getLexer().isNot(AsmToken::LParen))
598 return false;
599 // Lex the paren.
600 getParser().Lex();
601
602 const AsmToken &IntTok = Parser.getTok();
603 if (IntTok.isNot(AsmToken::Integer))
604 return Error(IntTok.getLoc(), "expected stack index");
605 switch (IntTok.getIntVal()) {
606 case 0: RegNo = X86::ST0; break;
607 case 1: RegNo = X86::ST1; break;
608 case 2: RegNo = X86::ST2; break;
609 case 3: RegNo = X86::ST3; break;
610 case 4: RegNo = X86::ST4; break;
611 case 5: RegNo = X86::ST5; break;
612 case 6: RegNo = X86::ST6; break;
613 case 7: RegNo = X86::ST7; break;
614 default: return Error(IntTok.getLoc(), "invalid stack index");
615 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000616
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000617 if (getParser().Lex().isNot(AsmToken::RParen))
618 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000619
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000620 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000621 Parser.Lex(); // Eat ')'
622 return false;
623 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000624
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000625 EndLoc = Parser.getTok().getEndLoc();
626
Chris Lattner645b2092010-06-24 07:29:18 +0000627 // If this is "db[0-7]", match it as an alias
628 // for dr[0-7].
629 if (RegNo == 0 && Tok.getString().size() == 3 &&
630 Tok.getString().startswith("db")) {
631 switch (Tok.getString()[2]) {
632 case '0': RegNo = X86::DR0; break;
633 case '1': RegNo = X86::DR1; break;
634 case '2': RegNo = X86::DR2; break;
635 case '3': RegNo = X86::DR3; break;
636 case '4': RegNo = X86::DR4; break;
637 case '5': RegNo = X86::DR5; break;
638 case '6': RegNo = X86::DR6; break;
639 case '7': RegNo = X86::DR7; break;
640 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000641
Chris Lattner645b2092010-06-24 07:29:18 +0000642 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000643 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000644 Parser.Lex(); // Eat it.
645 return false;
646 }
647 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000648
Devang Patel1aea4302012-01-20 22:32:05 +0000649 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000650 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000651 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000652 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000653 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000654
Sean Callananb9a25b72010-01-19 20:27:46 +0000655 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000656 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000657}
658
Devang Pateldd929fc2012-01-12 18:03:40 +0000659X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000660 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000661 return ParseIntelOperand();
662 return ParseATTOperand();
663}
664
Devang Pateld37ad242012-01-17 18:00:18 +0000665/// getIntelMemOperandSize - Return intel memory operand size.
666static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000667 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000668 .Cases("BYTE", "byte", 8)
669 .Cases("WORD", "word", 16)
670 .Cases("DWORD", "dword", 32)
671 .Cases("QWORD", "qword", 64)
672 .Cases("XWORD", "xword", 80)
673 .Cases("XMMWORD", "xmmword", 128)
674 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000675 .Default(0);
676 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000677}
678
Chad Rosiere1124532013-04-05 16:28:55 +0000679enum InfixCalculatorTok {
680 IC_PLUS = 0,
681 IC_MINUS,
682 IC_MULTIPLY,
683 IC_DIVIDE,
684 IC_RPAREN,
685 IC_LPAREN,
686 IC_IMM,
687 IC_REGISTER
688};
689static const char OpPrecedence[] = {
690 0, // IC_PLUS
691 0, // IC_MINUS
692 1, // IC_MULTIPLY
693 1, // IC_DIVIDE
694 2, // IC_RPAREN
695 3, // IC_LPAREN
696 0, // IC_IMM
697 0 // IC_REGISTER
698};
699
700class InfixCalculator {
701 typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
702 SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
703 SmallVector<ICToken, 4> PostfixStack;
704
705public:
706 int64_t popOperand() {
707 assert (!PostfixStack.empty() && "Poped an empty stack!");
708 ICToken Op = PostfixStack.pop_back_val();
709 assert ((Op.first == IC_IMM || Op.first == IC_REGISTER)
710 && "Expected and immediate or register!");
711 return Op.second;
712 }
713 void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
714 assert ((Op == IC_IMM || Op == IC_REGISTER) &&
715 "Unexpected operand!");
716 PostfixStack.push_back(std::make_pair(Op, Val));
717 }
718
719 void popOperator() { InfixOperatorStack.pop_back_val(); }
720 void pushOperator(InfixCalculatorTok Op) {
721 // Push the new operator if the stack is empty.
722 if (InfixOperatorStack.empty()) {
723 InfixOperatorStack.push_back(Op);
724 return;
725 }
726
727 // Push the new operator if it has a higher precedence than the operator on
728 // the top of the stack or the operator on the top of the stack is a left
729 // parentheses.
730 unsigned Idx = InfixOperatorStack.size() - 1;
731 InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
732 if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
733 InfixOperatorStack.push_back(Op);
734 return;
735 }
736
737 // The operator on the top of the stack has higher precedence than the
738 // new operator.
739 unsigned ParenCount = 0;
740 while (1) {
741 // Nothing to process.
742 if (InfixOperatorStack.empty())
743 break;
744
745 Idx = InfixOperatorStack.size() - 1;
746 StackOp = InfixOperatorStack[Idx];
747 if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
748 break;
749
750 // If we have an even parentheses count and we see a left parentheses,
751 // then stop processing.
752 if (!ParenCount && StackOp == IC_LPAREN)
753 break;
754
755 if (StackOp == IC_RPAREN) {
756 ++ParenCount;
757 InfixOperatorStack.pop_back_val();
758 } else if (StackOp == IC_LPAREN) {
759 --ParenCount;
760 InfixOperatorStack.pop_back_val();
761 } else {
762 InfixOperatorStack.pop_back_val();
763 PostfixStack.push_back(std::make_pair(StackOp, 0));
764 }
765 }
766 // Push the new operator.
767 InfixOperatorStack.push_back(Op);
768 }
769 int64_t execute() {
770 // Push any remaining operators onto the postfix stack.
771 while (!InfixOperatorStack.empty()) {
772 InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
773 if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
774 PostfixStack.push_back(std::make_pair(StackOp, 0));
775 }
776
777 if (PostfixStack.empty())
778 return 0;
779
780 SmallVector<ICToken, 16> OperandStack;
781 for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
782 ICToken Op = PostfixStack[i];
783 if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
784 OperandStack.push_back(Op);
785 } else {
786 assert (OperandStack.size() > 1 && "Too few operands.");
787 int64_t Val;
788 ICToken Op2 = OperandStack.pop_back_val();
789 ICToken Op1 = OperandStack.pop_back_val();
790 switch (Op.first) {
791 default:
792 report_fatal_error("Unexpected operator!");
793 break;
794 case IC_PLUS:
795 Val = Op1.second + Op2.second;
796 OperandStack.push_back(std::make_pair(IC_IMM, Val));
797 break;
798 case IC_MINUS:
799 Val = Op1.second - Op2.second;
800 OperandStack.push_back(std::make_pair(IC_IMM, Val));
801 break;
802 case IC_MULTIPLY:
803 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
804 "Multiply operation with an immediate and a register!");
805 Val = Op1.second * Op2.second;
806 OperandStack.push_back(std::make_pair(IC_IMM, Val));
807 break;
808 case IC_DIVIDE:
809 assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
810 "Divide operation with an immediate and a register!");
811 assert (Op2.second != 0 && "Division by zero!");
812 Val = Op1.second / Op2.second;
813 OperandStack.push_back(std::make_pair(IC_IMM, Val));
814 break;
815 }
816 }
817 }
818 assert (OperandStack.size() == 1 && "Expected a single result.");
819 return OperandStack.pop_back_val().second;
820 }
821};
822
Chad Rosierdd2e8952013-01-14 22:31:35 +0000823enum IntelBracExprState {
Chad Rosiere1124532013-04-05 16:28:55 +0000824 IBES_PLUS,
825 IBES_MINUS,
826 IBES_MULTIPLY,
827 IBES_DIVIDE,
Chad Rosierdd2e8952013-01-14 22:31:35 +0000828 IBES_LBRAC,
829 IBES_RBRAC,
Chad Rosiere1124532013-04-05 16:28:55 +0000830 IBES_LPAREN,
831 IBES_RPAREN,
Chad Rosierdd2e8952013-01-14 22:31:35 +0000832 IBES_REGISTER,
833 IBES_REGISTER_STAR,
Chad Rosierdd2e8952013-01-14 22:31:35 +0000834 IBES_INTEGER,
835 IBES_INTEGER_STAR,
Chad Rosierdd2e8952013-01-14 22:31:35 +0000836 IBES_IDENTIFIER,
Chad Rosierdd2e8952013-01-14 22:31:35 +0000837 IBES_ERROR
838};
839
840class IntelBracExprStateMachine {
841 IntelBracExprState State;
Chad Rosiere1124532013-04-05 16:28:55 +0000842 unsigned BaseReg, IndexReg, TmpReg, Scale;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000843 int64_t Disp;
Chad Rosiere1124532013-04-05 16:28:55 +0000844 InfixCalculator IC;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000845public:
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000846 IntelBracExprStateMachine(MCAsmParser &parser, int64_t disp) :
Chad Rosiere1124532013-04-05 16:28:55 +0000847 State(IBES_PLUS), BaseReg(0), IndexReg(0), TmpReg(0), Scale(1), Disp(disp){}
Chad Rosierdd2e8952013-01-14 22:31:35 +0000848
849 unsigned getBaseReg() { return BaseReg; }
850 unsigned getIndexReg() { return IndexReg; }
851 unsigned getScale() { return Scale; }
Chad Rosiere1124532013-04-05 16:28:55 +0000852 int64_t getDisp() { return Disp + IC.execute(); }
Chad Rosierdd2e8952013-01-14 22:31:35 +0000853 bool isValidEndState() { return State == IBES_RBRAC; }
854
855 void onPlus() {
856 switch (State) {
857 default:
858 State = IBES_ERROR;
859 break;
860 case IBES_INTEGER:
Chad Rosiere1124532013-04-05 16:28:55 +0000861 case IBES_RPAREN:
862 State = IBES_PLUS;
863 IC.pushOperator(IC_PLUS);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000864 break;
865 case IBES_REGISTER:
Chad Rosiere1124532013-04-05 16:28:55 +0000866 State = IBES_PLUS;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000867 // If we already have a BaseReg, then assume this is the IndexReg with a
868 // scale of 1.
869 if (!BaseReg) {
870 BaseReg = TmpReg;
871 } else {
872 assert (!IndexReg && "BaseReg/IndexReg already set!");
873 IndexReg = TmpReg;
874 Scale = 1;
875 }
Chad Rosiere1124532013-04-05 16:28:55 +0000876 IC.pushOperator(IC_PLUS);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000877 break;
878 }
Chad Rosierdd2e8952013-01-14 22:31:35 +0000879 }
880 void onMinus() {
881 switch (State) {
882 default:
883 State = IBES_ERROR;
884 break;
Chad Rosiere1124532013-04-05 16:28:55 +0000885 case IBES_PLUS:
886 case IBES_LPAREN:
887 IC.pushOperand(IC_IMM);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000888 case IBES_INTEGER:
Chad Rosiere1124532013-04-05 16:28:55 +0000889 case IBES_RPAREN:
890 State = IBES_MINUS;
891 IC.pushOperator(IC_MINUS);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000892 break;
893 case IBES_REGISTER:
Chad Rosiere1124532013-04-05 16:28:55 +0000894 State = IBES_MINUS;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000895 // If we already have a BaseReg, then assume this is the IndexReg with a
896 // scale of 1.
897 if (!BaseReg) {
898 BaseReg = TmpReg;
899 } else {
900 assert (!IndexReg && "BaseReg/IndexReg already set!");
901 IndexReg = TmpReg;
902 Scale = 1;
903 }
Chad Rosiere1124532013-04-05 16:28:55 +0000904 IC.pushOperator(IC_MINUS);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000905 break;
906 }
Chad Rosierdd2e8952013-01-14 22:31:35 +0000907 }
908 void onRegister(unsigned Reg) {
909 switch (State) {
910 default:
911 State = IBES_ERROR;
912 break;
Chad Rosiere1124532013-04-05 16:28:55 +0000913 case IBES_PLUS:
914 case IBES_LPAREN:
Chad Rosierdd2e8952013-01-14 22:31:35 +0000915 State = IBES_REGISTER;
916 TmpReg = Reg;
Chad Rosiere1124532013-04-05 16:28:55 +0000917 IC.pushOperand(IC_REGISTER);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000918 break;
919 case IBES_INTEGER_STAR:
920 assert (!IndexReg && "IndexReg already set!");
Chad Rosiere1124532013-04-05 16:28:55 +0000921 State = IBES_INTEGER;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000922 IndexReg = Reg;
Chad Rosiere1124532013-04-05 16:28:55 +0000923 Scale = IC.popOperand();
924 IC.pushOperand(IC_IMM);
925 IC.popOperator();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000926 break;
927 }
928 }
929 void onDispExpr() {
930 switch (State) {
931 default:
932 State = IBES_ERROR;
933 break;
Chad Rosiere1124532013-04-05 16:28:55 +0000934 case IBES_PLUS:
935 case IBES_MINUS:
936 State = IBES_INTEGER;
937 IC.pushOperand(IC_IMM);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000938 break;
939 }
940 }
941 void onInteger(int64_t TmpInt) {
942 switch (State) {
943 default:
944 State = IBES_ERROR;
945 break;
Chad Rosiere1124532013-04-05 16:28:55 +0000946 case IBES_PLUS:
Chad Rosierdd2e8952013-01-14 22:31:35 +0000947 case IBES_MINUS:
Chad Rosiere1124532013-04-05 16:28:55 +0000948 case IBES_MULTIPLY:
949 case IBES_DIVIDE:
950 case IBES_LPAREN:
951 case IBES_INTEGER_STAR:
Chad Rosierdd2e8952013-01-14 22:31:35 +0000952 State = IBES_INTEGER;
Chad Rosiere1124532013-04-05 16:28:55 +0000953 IC.pushOperand(IC_IMM, TmpInt);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000954 break;
955 case IBES_REGISTER_STAR:
956 assert (!IndexReg && "IndexReg already set!");
Chad Rosiere1124532013-04-05 16:28:55 +0000957 State = IBES_INTEGER;
Chad Rosierdd2e8952013-01-14 22:31:35 +0000958 IndexReg = TmpReg;
959 Scale = TmpInt;
Chad Rosiere1124532013-04-05 16:28:55 +0000960 IC.popOperator();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000961 break;
962 }
963 }
964 void onStar() {
965 switch (State) {
966 default:
967 State = IBES_ERROR;
968 break;
969 case IBES_INTEGER:
970 State = IBES_INTEGER_STAR;
Chad Rosiere1124532013-04-05 16:28:55 +0000971 IC.pushOperator(IC_MULTIPLY);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000972 break;
973 case IBES_REGISTER:
974 State = IBES_REGISTER_STAR;
Chad Rosiere1124532013-04-05 16:28:55 +0000975 IC.pushOperator(IC_MULTIPLY);
976 break;
977 case IBES_RPAREN:
978 State = IBES_MULTIPLY;
979 IC.pushOperator(IC_MULTIPLY);
980 break;
981 }
982 }
983 void onDivide() {
984 switch (State) {
985 default:
986 State = IBES_ERROR;
987 break;
988 case IBES_INTEGER:
989 State = IBES_DIVIDE;
990 IC.pushOperator(IC_DIVIDE);
Chad Rosierdd2e8952013-01-14 22:31:35 +0000991 break;
992 }
993 }
994 void onLBrac() {
995 switch (State) {
996 default:
997 State = IBES_ERROR;
998 break;
999 case IBES_RBRAC:
Chad Rosiere1124532013-04-05 16:28:55 +00001000 State = IBES_PLUS;
1001 IC.pushOperator(IC_PLUS);
Chad Rosierdd2e8952013-01-14 22:31:35 +00001002 break;
1003 }
1004 }
1005 void onRBrac() {
1006 switch (State) {
1007 default:
1008 State = IBES_ERROR;
1009 break;
Chad Rosiere1124532013-04-05 16:28:55 +00001010 case IBES_RPAREN:
Chad Rosierdd2e8952013-01-14 22:31:35 +00001011 case IBES_INTEGER:
1012 State = IBES_RBRAC;
Chad Rosierdd2e8952013-01-14 22:31:35 +00001013 break;
1014 case IBES_REGISTER:
1015 State = IBES_RBRAC;
1016 // If we already have a BaseReg, then assume this is the IndexReg with a
1017 // scale of 1.
1018 if (!BaseReg) {
1019 BaseReg = TmpReg;
1020 } else {
1021 assert (!IndexReg && "BaseReg/IndexReg already set!");
1022 IndexReg = TmpReg;
1023 Scale = 1;
1024 }
1025 break;
Chad Rosiere1124532013-04-05 16:28:55 +00001026 }
1027 }
1028 void onLParen() {
1029 switch (State) {
1030 default:
1031 State = IBES_ERROR;
1032 break;
1033 case IBES_PLUS:
1034 case IBES_MINUS:
1035 case IBES_MULTIPLY:
1036 case IBES_DIVIDE:
1037 case IBES_INTEGER_STAR:
1038 case IBES_LPAREN:
1039 State = IBES_LPAREN;
1040 IC.pushOperator(IC_LPAREN);
1041 break;
1042 }
1043 }
1044 void onRParen() {
1045 switch (State) {
1046 default:
1047 State = IBES_ERROR;
1048 break;
1049 case IBES_REGISTER:
1050 case IBES_INTEGER:
1051 case IBES_PLUS:
1052 case IBES_MINUS:
1053 case IBES_MULTIPLY:
1054 case IBES_DIVIDE:
1055 case IBES_RPAREN:
1056 State = IBES_RPAREN;
1057 IC.pushOperator(IC_RPAREN);
Chad Rosierdd2e8952013-01-14 22:31:35 +00001058 break;
1059 }
1060 }
1061};
1062
Chad Rosierd3e74162013-03-19 21:11:56 +00001063X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
1064 SMLoc End, SMLoc SizeDirLoc,
1065 unsigned Size) {
1066 bool NeedSizeDir = false;
1067 bool IsVarDecl = false;
1068 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1069 const MCSymbol &Sym = SymRef->getSymbol();
1070 // FIXME: The SemaLookup will fail if the name is anything other then an
1071 // identifier.
1072 // FIXME: Pass a valid SMLoc.
1073 unsigned tLength, tSize, tType;
1074 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
1075 tSize, tType, IsVarDecl);
1076 if (!Size) {
1077 Size = tType * 8; // Size is in terms of bits in this context.
1078 NeedSizeDir = Size > 0;
1079 }
1080 }
1081
1082 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1083 // reference. We need an 'r' constraint here, so we need to create register
1084 // operand to ensure proper matching. Just pick a GPR based on the size of
1085 // a pointer.
1086 if (!IsVarDecl) {
1087 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1088 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
1089 }
1090
1091 if (NeedSizeDir)
1092 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
1093 /*Len*/0, Size));
1094
1095 // When parsing inline assembly we set the base register to a non-zero value
1096 // as we don't know the actual value at this time. This is necessary to
1097 // get the matching correct in some cases.
1098 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
1099 /*Scale*/1, Start, End, Size);
1100}
1101
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001102X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Chad Rosier6b369ce2013-04-08 17:43:47 +00001103 SMLoc SizeDirLoc,
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001104 uint64_t ImmDisp,
Devang Patel7c64fe62012-01-23 18:31:58 +00001105 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +00001106 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001107 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +00001108
Devang Pateld37ad242012-01-17 18:00:18 +00001109 // Eat '['
1110 if (getLexer().isNot(AsmToken::LBrac))
1111 return ErrorOperand(Start, "Expected '[' token!");
1112 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +00001113
Chad Rosierdd2e8952013-01-14 22:31:35 +00001114 unsigned TmpReg = 0;
1115
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001116 // Try to handle '[' 'Symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +00001117 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +00001118 if (ParseRegister(TmpReg, Start, End)) {
1119 const MCExpr *Disp;
Chad Rosier30c729b2013-04-02 20:02:33 +00001120 SMLoc IdentStart = Tok.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001121 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +00001122 return 0;
1123
Chad Rosier30c729b2013-04-02 20:02:33 +00001124 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, IdentStart))
1125 return Err;
1126
Devang Pateld37ad242012-01-17 18:00:18 +00001127 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001128 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001129
Chad Rosier4fb25b72013-02-15 21:58:13 +00001130 // Adjust the EndLoc due to the ']'.
1131 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +00001132 Parser.Lex();
Chad Rosierd3e74162013-03-19 21:11:56 +00001133 if (!isParsingInlineAsm())
1134 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierd3e74162013-03-19 21:11:56 +00001135 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001136 }
Devang Pateld37ad242012-01-17 18:00:18 +00001137 }
1138
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001139 // Parse [ BaseReg + Scale*IndexReg + Disp ]. We may have already parsed an
1140 // immediate displacement before the bracketed expression.
Chad Rosierdd2e8952013-01-14 22:31:35 +00001141 bool Done = false;
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001142 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier2fbc2392012-10-29 18:01:54 +00001143
Chad Rosierdd2e8952013-01-14 22:31:35 +00001144 // If we parsed a register, then the end loc has already been set and
1145 // the identifier has already been lexed. We also need to update the
1146 // state.
1147 if (TmpReg)
1148 SM.onRegister(TmpReg);
1149
1150 const MCExpr *Disp = 0;
1151 while (!Done) {
1152 bool UpdateLocLex = true;
1153
1154 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
1155 // identifier. Don't try an parse it as a register.
1156 if (Tok.getString().startswith("."))
1157 break;
1158
1159 switch (getLexer().getKind()) {
1160 default: {
1161 if (SM.isValidEndState()) {
1162 Done = true;
1163 break;
1164 }
1165 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
1166 }
1167 case AsmToken::Identifier: {
1168 // This could be a register or a displacement expression.
1169 if(!ParseRegister(TmpReg, Start, End)) {
1170 SM.onRegister(TmpReg);
1171 UpdateLocLex = false;
1172 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001173 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +00001174 SM.onDispExpr();
1175 UpdateLocLex = false;
1176 break;
1177 }
1178 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1179 }
Chad Rosiere1124532013-04-05 16:28:55 +00001180 case AsmToken::Integer:
1181 if (isParsingInlineAsm())
1182 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1183 Tok.getLoc()));
1184 SM.onInteger(Tok.getIntVal());
Chad Rosierdd2e8952013-01-14 22:31:35 +00001185 break;
Chad Rosierdd2e8952013-01-14 22:31:35 +00001186 case AsmToken::Plus: SM.onPlus(); break;
1187 case AsmToken::Minus: SM.onMinus(); break;
1188 case AsmToken::Star: SM.onStar(); break;
Chad Rosiere1124532013-04-05 16:28:55 +00001189 case AsmToken::Slash: SM.onDivide(); break;
Chad Rosierdd2e8952013-01-14 22:31:35 +00001190 case AsmToken::LBrac: SM.onLBrac(); break;
1191 case AsmToken::RBrac: SM.onRBrac(); break;
Chad Rosiere1124532013-04-05 16:28:55 +00001192 case AsmToken::LParen: SM.onLParen(); break;
1193 case AsmToken::RParen: SM.onRParen(); break;
Chad Rosierdd2e8952013-01-14 22:31:35 +00001194 }
1195 if (!Done && UpdateLocLex) {
1196 End = Tok.getLoc();
1197 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +00001198 }
Devang Pateld37ad242012-01-17 18:00:18 +00001199 }
1200
Chad Rosierdd2e8952013-01-14 22:31:35 +00001201 if (!Disp)
1202 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +00001203
Chad Rosierddb53ef2012-10-26 22:01:25 +00001204 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001205 if (Tok.getString().startswith(".")) {
1206 SmallString<64> Err;
1207 const MCExpr *NewDisp;
1208 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1209 return ErrorOperand(Tok.getLoc(), Err);
1210
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001211 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001212 Parser.Lex(); // Eat the field.
1213 Disp = NewDisp;
1214 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001215
Chad Rosierdd2e8952013-01-14 22:31:35 +00001216 int BaseReg = SM.getBaseReg();
1217 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001218
Chad Rosierdd2e8952013-01-14 22:31:35 +00001219 // handle [-42]
1220 if (!BaseReg && !IndexReg) {
1221 if (!SegReg)
1222 return X86Operand::CreateMem(Disp, Start, End);
1223 else
1224 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1225 }
1226
1227 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001228 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001229 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001230}
1231
Chad Rosier30c729b2013-04-02 20:02:33 +00001232// Inline assembly may use variable names with namespace alias qualifiers.
1233X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
1234 SMLoc &IdentStart) {
1235 // We should only see Foo::Bar if we're parsing inline assembly.
1236 if (!isParsingInlineAsm())
1237 return 0;
1238
1239 // If we don't see a ':' then there can't be a qualifier.
1240 if (getLexer().isNot(AsmToken::Colon))
1241 return 0;
1242
1243
1244 bool Done = false;
1245 const AsmToken &Tok = Parser.getTok();
1246 SMLoc IdentEnd = Tok.getEndLoc();
1247 while (!Done) {
1248 switch (getLexer().getKind()) {
1249 default:
1250 Done = true;
1251 break;
1252 case AsmToken::Colon:
1253 getLexer().Lex(); // Consume ':'.
1254 if (getLexer().isNot(AsmToken::Colon))
1255 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1256 getLexer().Lex(); // Consume second ':'.
1257 if (getLexer().isNot(AsmToken::Identifier))
1258 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1259 break;
1260 case AsmToken::Identifier:
1261 IdentEnd = Tok.getEndLoc();
1262 getLexer().Lex(); // Consume the identifier.
1263 break;
1264 }
1265 }
1266 size_t Len = IdentEnd.getPointer() - IdentStart.getPointer();
1267 StringRef Identifier(IdentStart.getPointer(), Len);
1268 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1269 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1270 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1271 return 0;
1272}
1273
Devang Pateld37ad242012-01-17 18:00:18 +00001274/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001275X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1276 uint64_t ImmDisp,
1277 SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001278 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001279 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001280
1281 unsigned Size = getIntelMemOperandSize(Tok.getString());
1282 if (Size) {
1283 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001284 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1285 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001286 Parser.Lex();
1287 }
1288
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001289 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1290 if (getLexer().is(AsmToken::Integer)) {
1291 const AsmToken &IntTok = Parser.getTok();
1292 if (isParsingInlineAsm())
1293 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1294 IntTok.getLoc()));
1295 uint64_t ImmDisp = IntTok.getIntVal();
1296 Parser.Lex(); // Eat the integer.
1297 if (getLexer().isNot(AsmToken::LBrac))
1298 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosier6b369ce2013-04-08 17:43:47 +00001299 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001300 }
1301
Chad Rosierc0a14b82012-10-24 17:22:29 +00001302 if (getLexer().is(AsmToken::LBrac))
Chad Rosier6b369ce2013-04-08 17:43:47 +00001303 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001304
1305 if (!ParseRegister(SegReg, Start, End)) {
1306 // Handel SegReg : [ ... ]
1307 if (getLexer().isNot(AsmToken::Colon))
1308 return ErrorOperand(Start, "Expected ':' token!");
1309 Parser.Lex(); // Eat :
1310 if (getLexer().isNot(AsmToken::LBrac))
1311 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosier6b369ce2013-04-08 17:43:47 +00001312 return ParseIntelBracExpression(SegReg, Start, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001313 }
Devang Pateld37ad242012-01-17 18:00:18 +00001314
1315 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Chad Rosier30c729b2013-04-02 20:02:33 +00001316 SMLoc IdentStart = Tok.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001317 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001318 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001319
Chad Rosier2a784132012-10-23 23:31:33 +00001320 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001321 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier30c729b2013-04-02 20:02:33 +00001322
1323 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, IdentStart))
1324 return Err;
1325
Chad Rosierd3e74162013-03-19 21:11:56 +00001326 return CreateMemForInlineAsm(Disp, Start, End, Start, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001327}
1328
Chad Rosier22f441a2012-10-24 22:21:50 +00001329/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001330bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1331 const MCExpr **NewDisp,
1332 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001333 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001334 uint64_t OrigDispVal, DotDispVal;
1335
1336 // FIXME: Handle non-constant expressions.
1337 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1338 OrigDispVal = OrigDisp->getValue();
1339 } else {
1340 Err = "Non-constant offsets are not supported!";
1341 return true;
1342 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001343
1344 // Drop the '.'.
1345 StringRef DotDispStr = Tok.getString().drop_front(1);
1346
Chad Rosier22f441a2012-10-24 22:21:50 +00001347 // .Imm gets lexed as a real.
1348 if (Tok.is(AsmToken::Real)) {
1349 APInt DotDisp;
1350 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001351 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001352 } else if (Tok.is(AsmToken::Identifier)) {
1353 // We should only see an identifier when parsing the original inline asm.
1354 // The front-end should rewrite this in terms of immediates.
1355 assert (isParsingInlineAsm() && "Unexpected field name!");
1356
1357 unsigned DotDisp;
1358 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1359 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1360 DotDisp)) {
1361 Err = "Unable to lookup field reference!";
1362 return true;
1363 }
1364 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001365 } else {
1366 Err = "Unexpected token type!";
1367 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001368 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001369
Chad Rosierec130222012-10-25 21:51:10 +00001370 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1371 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1372 unsigned Len = DotDispStr.size();
1373 unsigned Val = OrigDispVal + DotDispVal;
1374 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1375 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001376 }
1377
1378 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1379 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001380}
1381
Chad Rosierc0a14b82012-10-24 17:22:29 +00001382/// Parse the 'offset' operator. This operator is used to specify the
1383/// location rather then the content of a variable.
1384X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1385 SMLoc OffsetOfLoc = Start;
1386 Parser.Lex(); // Eat offset.
1387 Start = Parser.getTok().getLoc();
1388 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1389
Chad Rosier6e431572012-10-26 16:09:20 +00001390 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001391 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001392 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001393 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001394
Chad Rosier6e431572012-10-26 16:09:20 +00001395 // Don't emit the offset operator.
1396 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1397
Chad Rosierc0a14b82012-10-24 17:22:29 +00001398 // The offset operator will have an 'r' constraint, thus we need to create
1399 // register operand to ensure proper matching. Just pick a GPR based on
1400 // the size of a pointer.
1401 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001402 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1403 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001404}
1405
Chad Rosier505bca32013-01-17 19:21:48 +00001406enum IntelOperatorKind {
1407 IOK_LENGTH,
1408 IOK_SIZE,
1409 IOK_TYPE
1410};
1411
1412/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1413/// returns the number of elements in an array. It returns the value 1 for
1414/// non-array variables. The SIZE operator returns the size of a C or C++
1415/// variable. A variable's size is the product of its LENGTH and TYPE. The
1416/// TYPE operator returns the size of a C or C++ type or variable. If the
1417/// variable is an array, TYPE returns the size of a single element.
1418X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001419 SMLoc TypeLoc = Start;
1420 Parser.Lex(); // Eat offset.
1421 Start = Parser.getTok().getLoc();
1422 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1423
1424 SMLoc End;
1425 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001426 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001427 return 0;
1428
Chad Rosier505bca32013-01-17 19:21:48 +00001429 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001430 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1431 const MCSymbol &Sym = SymRef->getSymbol();
1432 // FIXME: The SemaLookup will fail if the name is anything other then an
1433 // identifier.
1434 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001435 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001436 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1437 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001438 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001439 }
1440 unsigned CVal;
1441 switch(OpKind) {
1442 default: llvm_unreachable("Unexpected operand kind!");
1443 case IOK_LENGTH: CVal = Length; break;
1444 case IOK_SIZE: CVal = Size; break;
1445 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001446 }
1447
1448 // Rewrite the type operator and the C or C++ type or variable in terms of an
1449 // immediate. E.g. TYPE foo -> $$4
1450 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001451 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001452
Chad Rosier505bca32013-01-17 19:21:48 +00001453 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosier811ddf62013-03-19 21:58:18 +00001454 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosierefcb3d92012-10-26 18:04:20 +00001455}
1456
Devang Pateld37ad242012-01-17 18:00:18 +00001457X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001458 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001459 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001460
Chad Rosier505bca32013-01-17 19:21:48 +00001461 // Offset, length, type and size operators.
1462 if (isParsingInlineAsm()) {
1463 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1464 return ParseIntelOffsetOfOperator(Start);
1465 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1466 return ParseIntelOperator(Start, IOK_LENGTH);
1467 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1468 return ParseIntelOperator(Start, IOK_SIZE);
1469 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1470 return ParseIntelOperator(Start, IOK_TYPE);
1471 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001472
Chad Rosier505bca32013-01-17 19:21:48 +00001473 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001474 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1475 getLexer().is(AsmToken::Minus)) {
1476 const MCExpr *Val;
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001477 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001478 if (!getParser().parseExpression(Val, End)) {
Chad Rosier811ddf62013-03-19 21:58:18 +00001479 if (isParsingInlineAsm())
1480 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001481 // Immediate.
1482 if (getLexer().isNot(AsmToken::LBrac))
1483 return X86Operand::CreateImm(Val, Start, End);
1484
1485 // Only positive immediates are valid.
1486 if (!isInteger) {
1487 Error(Parser.getTok().getLoc(), "expected a positive immediate "
1488 "displacement before bracketed expr.");
1489 return 0;
1490 }
1491
1492 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1493 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1494 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Pateld37ad242012-01-17 18:00:18 +00001495 }
1496 }
1497
Chad Rosier505bca32013-01-17 19:21:48 +00001498 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001499 unsigned RegNo = 0;
1500 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001501 // If this is a segment register followed by a ':', then this is the start
1502 // of a memory reference, otherwise this is a normal register reference.
1503 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001504 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001505
1506 getParser().Lex(); // Eat the colon.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001507 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001508 }
1509
Chad Rosier505bca32013-01-17 19:21:48 +00001510 // Memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001511 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001512}
1513
Devang Pateldd929fc2012-01-12 18:03:40 +00001514X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001515 switch (getLexer().getKind()) {
1516 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001517 // Parse a memory operand with no segment register.
1518 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001519 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001520 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001521 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001522 SMLoc Start, End;
1523 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001524 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001525 Error(Start, "%eiz and %riz can only be used as index registers",
1526 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001527 return 0;
1528 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001529
Chris Lattnereef6d782010-04-17 18:56:34 +00001530 // If this is a segment register followed by a ':', then this is the start
1531 // of a memory reference, otherwise this is a normal register reference.
1532 if (getLexer().isNot(AsmToken::Colon))
1533 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001534
Chris Lattnereef6d782010-04-17 18:56:34 +00001535 getParser().Lex(); // Eat the colon.
1536 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001537 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001538 case AsmToken::Dollar: {
1539 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001540 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001541 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001542 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001543 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001544 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001545 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001546 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001547 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001548}
1549
Chris Lattnereef6d782010-04-17 18:56:34 +00001550/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1551/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001552X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001553
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001554 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1555 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001556 // only way to do this without lookahead is to eat the '(' and see what is
1557 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001558 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001559 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001560 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001561 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001562
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001563 // After parsing the base expression we could either have a parenthesized
1564 // memory address or not. If not, return now. If so, eat the (.
1565 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001566 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001567 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001568 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001569 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001570 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001571
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001572 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001573 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001574 } else {
1575 // Okay, we have a '('. We don't know if this is an expression or not, but
1576 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001577 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001578 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001579
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001580 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001581 // Nothing to do here, fall into the code below with the '(' part of the
1582 // memory operand consumed.
1583 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001584 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001585
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001586 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001587 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001588 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001589
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001590 // After parsing the base expression we could either have a parenthesized
1591 // memory address or not. If not, return now. If so, eat the (.
1592 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001593 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001594 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001595 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001596 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001597 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001598
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001599 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001600 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001601 }
1602 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001603
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001604 // If we reached here, then we just ate the ( of the memory operand. Process
1605 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001606 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001607 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001608
Chris Lattner29ef9a22010-01-15 18:51:29 +00001609 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001610 SMLoc StartLoc, EndLoc;
1611 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001612 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001613 Error(StartLoc, "eiz and riz can only be used as index registers",
1614 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001615 return 0;
1616 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001617 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001618
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001619 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001620 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001621 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001622
1623 // Following the comma we should have either an index register, or a scale
1624 // value. We don't support the later form, but we want to parse it
1625 // correctly.
1626 //
1627 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001628 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001629 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001630 SMLoc L;
1631 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001632
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001633 if (getLexer().isNot(AsmToken::RParen)) {
1634 // Parse the scale amount:
1635 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001636 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001637 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001638 "expected comma in scale expression");
1639 return 0;
1640 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001641 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001642
1643 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001644 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001645
1646 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001647 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001648 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001649 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001650 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001651
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001652 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001653 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1654 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1655 return 0;
1656 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001657 Scale = (unsigned)ScaleVal;
1658 }
1659 }
1660 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001661 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001662 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001663 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001664
1665 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001666 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001667 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001668
Daniel Dunbaree910252010-08-24 19:13:38 +00001669 if (Value != 1)
1670 Warning(Loc, "scale factor without index register is ignored");
1671 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001672 }
1673 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001674
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001675 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001676 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001677 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001678 return 0;
1679 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001680 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001681 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001682
Kevin Enderby84faf652012-03-12 21:32:09 +00001683 // If we have both a base register and an index register make sure they are
1684 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001685 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001686 if (BaseReg != 0 && IndexReg != 0) {
1687 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001688 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1689 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001690 IndexReg != X86::RIZ) {
1691 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1692 return 0;
1693 }
1694 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001695 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1696 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001697 IndexReg != X86::EIZ){
1698 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1699 return 0;
1700 }
1701 }
1702
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001703 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1704 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001705}
1706
Devang Pateldd929fc2012-01-12 18:03:40 +00001707bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001708ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001709 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001710 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001711 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001712
Chris Lattnerd8f71792010-11-28 20:23:50 +00001713 // FIXME: Hack to recognize setneb as setne.
1714 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1715 PatchedName != "setb" && PatchedName != "setnb")
1716 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001717
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001718 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1719 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001720 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001721 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1722 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001723 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001724 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001725 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001726 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001727 .Case("eq", 0x00)
1728 .Case("lt", 0x01)
1729 .Case("le", 0x02)
1730 .Case("unord", 0x03)
1731 .Case("neq", 0x04)
1732 .Case("nlt", 0x05)
1733 .Case("nle", 0x06)
1734 .Case("ord", 0x07)
1735 /* AVX only from here */
1736 .Case("eq_uq", 0x08)
1737 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001738 .Case("ngt", 0x0A)
1739 .Case("false", 0x0B)
1740 .Case("neq_oq", 0x0C)
1741 .Case("ge", 0x0D)
1742 .Case("gt", 0x0E)
1743 .Case("true", 0x0F)
1744 .Case("eq_os", 0x10)
1745 .Case("lt_oq", 0x11)
1746 .Case("le_oq", 0x12)
1747 .Case("unord_s", 0x13)
1748 .Case("neq_us", 0x14)
1749 .Case("nlt_uq", 0x15)
1750 .Case("nle_uq", 0x16)
1751 .Case("ord_s", 0x17)
1752 .Case("eq_us", 0x18)
1753 .Case("nge_uq", 0x19)
1754 .Case("ngt_uq", 0x1A)
1755 .Case("false_os", 0x1B)
1756 .Case("neq_os", 0x1C)
1757 .Case("ge_oq", 0x1D)
1758 .Case("gt_oq", 0x1E)
1759 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001760 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001761 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001762 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1763 getParser().getContext());
1764 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001765 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001766 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001767 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001768 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001769 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001770 } else {
1771 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001772 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001773 }
1774 }
1775 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001776
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001777 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001778
Devang Patel885f65b2012-01-30 22:47:12 +00001779 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001780 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001781
Chris Lattner2544f422010-09-08 05:17:37 +00001782 // Determine whether this is an instruction prefix.
1783 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001784 Name == "lock" || Name == "rep" ||
1785 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001786 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001787 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001788
1789
Chris Lattner2544f422010-09-08 05:17:37 +00001790 // This does the actual operand parsing. Don't parse any more if we have a
1791 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1792 // just want to parse the "lock" as the first instruction and the "incl" as
1793 // the next one.
1794 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001795
1796 // Parse '*' modifier.
1797 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001798 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001799 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001800 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001801 }
1802
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001803 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001804 if (X86Operand *Op = ParseOperand())
1805 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001806 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001807 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001808 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001809 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001810
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001811 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001812 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001813
1814 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001815 if (X86Operand *Op = ParseOperand())
1816 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001817 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001818 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001819 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001820 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001821 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001822
Chris Lattnercbf8a982010-09-11 16:18:25 +00001823 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001824 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001825 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001826 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001827 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001828 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001829
Chris Lattner2544f422010-09-08 05:17:37 +00001830 if (getLexer().is(AsmToken::EndOfStatement))
1831 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001832 else if (isPrefix && getLexer().is(AsmToken::Slash))
1833 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001834
Devang Patel885f65b2012-01-30 22:47:12 +00001835 if (ExtraImmOp && isParsingIntelSyntax())
1836 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1837
Chris Lattner98c870f2010-11-06 19:25:43 +00001838 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1839 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1840 // documented form in various unofficial manuals, so a lot of code uses it.
1841 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1842 Operands.size() == 3) {
1843 X86Operand &Op = *(X86Operand*)Operands.back();
1844 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1845 isa<MCConstantExpr>(Op.Mem.Disp) &&
1846 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1847 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1848 SMLoc Loc = Op.getEndLoc();
1849 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1850 delete &Op;
1851 }
1852 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001853 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1854 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1855 Operands.size() == 3) {
1856 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1857 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1858 isa<MCConstantExpr>(Op.Mem.Disp) &&
1859 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1860 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1861 SMLoc Loc = Op.getEndLoc();
1862 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1863 delete &Op;
1864 }
1865 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001866 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1867 if (Name.startswith("ins") && Operands.size() == 3 &&
1868 (Name == "insb" || Name == "insw" || Name == "insl")) {
1869 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1870 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1871 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1872 Operands.pop_back();
1873 Operands.pop_back();
1874 delete &Op;
1875 delete &Op2;
1876 }
1877 }
1878
1879 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1880 if (Name.startswith("outs") && Operands.size() == 3 &&
1881 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1882 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1883 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1884 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1885 Operands.pop_back();
1886 Operands.pop_back();
1887 delete &Op;
1888 delete &Op2;
1889 }
1890 }
1891
1892 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1893 if (Name.startswith("movs") && Operands.size() == 3 &&
1894 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001895 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001896 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1897 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1898 if (isSrcOp(Op) && isDstOp(Op2)) {
1899 Operands.pop_back();
1900 Operands.pop_back();
1901 delete &Op;
1902 delete &Op2;
1903 }
1904 }
1905 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1906 if (Name.startswith("lods") && Operands.size() == 3 &&
1907 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001908 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001909 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1910 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1911 if (isSrcOp(*Op1) && Op2->isReg()) {
1912 const char *ins;
1913 unsigned reg = Op2->getReg();
1914 bool isLods = Name == "lods";
1915 if (reg == X86::AL && (isLods || Name == "lodsb"))
1916 ins = "lodsb";
1917 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1918 ins = "lodsw";
1919 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1920 ins = "lodsl";
1921 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1922 ins = "lodsq";
1923 else
1924 ins = NULL;
1925 if (ins != NULL) {
1926 Operands.pop_back();
1927 Operands.pop_back();
1928 delete Op1;
1929 delete Op2;
1930 if (Name != ins)
1931 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1932 }
1933 }
1934 }
1935 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1936 if (Name.startswith("stos") && Operands.size() == 3 &&
1937 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001938 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001939 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1940 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1941 if (isDstOp(*Op2) && Op1->isReg()) {
1942 const char *ins;
1943 unsigned reg = Op1->getReg();
1944 bool isStos = Name == "stos";
1945 if (reg == X86::AL && (isStos || Name == "stosb"))
1946 ins = "stosb";
1947 else if (reg == X86::AX && (isStos || Name == "stosw"))
1948 ins = "stosw";
1949 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1950 ins = "stosl";
1951 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1952 ins = "stosq";
1953 else
1954 ins = NULL;
1955 if (ins != NULL) {
1956 Operands.pop_back();
1957 Operands.pop_back();
1958 delete Op1;
1959 delete Op2;
1960 if (Name != ins)
1961 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1962 }
1963 }
1964 }
1965
Chris Lattnere9e16a32010-09-15 04:33:27 +00001966 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001967 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001968 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001969 Name.startswith("shl") || Name.startswith("sal") ||
1970 Name.startswith("rcl") || Name.startswith("rcr") ||
1971 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001972 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001973 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001974 // Intel syntax
1975 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1976 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001977 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1978 delete Operands[2];
1979 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001980 }
1981 } else {
1982 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1983 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001984 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1985 delete Operands[1];
1986 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001987 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001988 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001989 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001990
Chris Lattner15f89512011-04-09 19:41:05 +00001991 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1992 // instalias with an immediate operand yet.
1993 if (Name == "int" && Operands.size() == 2) {
1994 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1995 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1996 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1997 delete Operands[1];
1998 Operands.erase(Operands.begin() + 1);
1999 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
2000 }
2001 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002002
Chris Lattner98986712010-01-14 22:21:20 +00002003 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00002004}
2005
Craig Topper4bef9612013-03-18 02:53:34 +00002006static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
2007 bool isCmp) {
2008 MCInst TmpInst;
2009 TmpInst.setOpcode(Opcode);
2010 if (!isCmp)
2011 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2012 TmpInst.addOperand(MCOperand::CreateReg(Reg));
2013 TmpInst.addOperand(Inst.getOperand(0));
2014 Inst = TmpInst;
2015 return true;
2016}
2017
2018static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
2019 bool isCmp = false) {
2020 if (!Inst.getOperand(0).isImm() ||
2021 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
2022 return false;
2023
2024 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
2025}
2026
2027static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
2028 bool isCmp = false) {
2029 if (!Inst.getOperand(0).isImm() ||
2030 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
2031 return false;
2032
2033 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
2034}
2035
2036static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
2037 bool isCmp = false) {
2038 if (!Inst.getOperand(0).isImm() ||
2039 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
2040 return false;
2041
2042 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
2043}
2044
Devang Pateldd929fc2012-01-12 18:03:40 +00002045bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00002046processInstruction(MCInst &Inst,
2047 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
2048 switch (Inst.getOpcode()) {
2049 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00002050 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
2051 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
2052 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
2053 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
2054 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
2055 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
2056 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
2057 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
2058 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
2059 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
2060 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
2061 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
2062 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
2063 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
2064 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
2065 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
2066 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
2067 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00002068 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
2069 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
2070 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
2071 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
2072 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
2073 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00002074 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00002075}
2076
Jim Grosbach3ca63822012-11-14 18:04:47 +00002077static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00002078bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00002079MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00002080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00002081 MCStreamer &Out, unsigned &ErrorInfo,
2082 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002083 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00002084 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
2085 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00002086 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002087
Chris Lattner7c51a312010-09-29 01:50:45 +00002088 // First, handle aliases that expand to multiple instructions.
2089 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00002090 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00002091 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00002092 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00002093 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00002094 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00002095 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00002096 MCInst Inst;
2097 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002098 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002099 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002100 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00002101
Chris Lattner0bb83a82010-09-30 16:39:29 +00002102 const char *Repl =
2103 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00002104 .Case("finit", "fninit")
2105 .Case("fsave", "fnsave")
2106 .Case("fstcw", "fnstcw")
2107 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00002108 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00002109 .Case("fstsw", "fnstsw")
2110 .Case("fstsww", "fnstsw")
2111 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00002112 .Default(0);
2113 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00002114 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00002115 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00002116 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002117
Chris Lattnera008e8a2010-09-06 21:54:15 +00002118 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00002119 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002120
Daniel Dunbarc918d602010-05-04 16:12:42 +00002121 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00002122 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00002123 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00002124 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002125 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00002126 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00002127 // Some instructions need post-processing to, for example, tweak which
2128 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00002129 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00002130 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002131 while (processInstruction(Inst, Operands))
2132 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00002133
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002134 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002135 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002136 Out.EmitInstruction(Inst);
2137 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00002138 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00002139 case Match_MissingFeature: {
2140 assert(ErrorInfo && "Unknown missing feature!");
2141 // Special case the error message for the very common case where only
2142 // a single subtarget feature is missing.
2143 std::string Msg = "instruction requires:";
2144 unsigned Mask = 1;
2145 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2146 if (ErrorInfo & Mask) {
2147 Msg += " ";
2148 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2149 }
2150 Mask <<= 1;
2151 }
2152 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2153 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00002154 case Match_InvalidOperand:
2155 WasOriginallyInvalidOperand = true;
2156 break;
2157 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00002158 break;
2159 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00002160
Daniel Dunbarc918d602010-05-04 16:12:42 +00002161 // FIXME: Ideally, we would only attempt suffix matches for things which are
2162 // valid prefixes, and we could just infer the right unambiguous
2163 // type. However, that requires substantially more matcher support than the
2164 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002165
Daniel Dunbarc918d602010-05-04 16:12:42 +00002166 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00002167 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002168 SmallString<16> Tmp;
2169 Tmp += Base;
2170 Tmp += ' ';
2171 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00002172
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002173 // If this instruction starts with an 'f', then it is a floating point stack
2174 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2175 // 80-bit floating point, which use the suffixes s,l,t respectively.
2176 //
2177 // Otherwise, we assume that this may be an integer instruction, which comes
2178 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2179 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00002180
Daniel Dunbarc918d602010-05-04 16:12:42 +00002181 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002182 Tmp[Base.size()] = Suffixes[0];
2183 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00002184 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002185 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002186
Chad Rosier6e006d32012-10-12 22:53:36 +00002187 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2188 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002189 // If this returned as a missing feature failure, remember that.
2190 if (Match1 == Match_MissingFeature)
2191 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002192 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00002193 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2194 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002195 // If this returned as a missing feature failure, remember that.
2196 if (Match2 == Match_MissingFeature)
2197 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002198 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00002199 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2200 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002201 // If this returned as a missing feature failure, remember that.
2202 if (Match3 == Match_MissingFeature)
2203 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002204 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00002205 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2206 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002207 // If this returned as a missing feature failure, remember that.
2208 if (Match4 == Match_MissingFeature)
2209 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00002210
2211 // Restore the old token.
2212 Op->setTokenValue(Base);
2213
2214 // If exactly one matched, then we treat that as a successful match (and the
2215 // instruction will already have been filled in correctly, since the failing
2216 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00002217 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002218 (Match1 == Match_Success) + (Match2 == Match_Success) +
2219 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00002220 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002221 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002222 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002223 Out.EmitInstruction(Inst);
2224 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00002225 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00002226 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00002227
Chris Lattnerec6789f2010-09-06 20:08:02 +00002228 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002229
Daniel Dunbar09062b12010-08-12 00:55:42 +00002230 // If we had multiple suffix matches, then identify this as an ambiguous
2231 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00002232 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00002233 char MatchChars[4];
2234 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002235 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2236 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2237 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2238 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00002239
2240 SmallString<126> Msg;
2241 raw_svector_ostream OS(Msg);
2242 OS << "ambiguous instructions require an explicit suffix (could be ";
2243 for (unsigned i = 0; i != NumMatches; ++i) {
2244 if (i != 0)
2245 OS << ", ";
2246 if (i + 1 == NumMatches)
2247 OS << "or ";
2248 OS << "'" << Base << MatchChars[i] << "'";
2249 }
2250 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00002251 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002252 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00002253 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002254
Chris Lattnera008e8a2010-09-06 21:54:15 +00002255 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002256
Chris Lattnera008e8a2010-09-06 21:54:15 +00002257 // If all of the instructions reported an invalid mnemonic, then the original
2258 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002259 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2260 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00002261 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00002262 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002263 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002264 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002265 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002266 }
2267
2268 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002269 if (ErrorInfo != ~0U) {
2270 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002271 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002272 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002273
Chad Rosier84125ca2012-10-13 00:26:04 +00002274 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002275 if (Operand->getStartLoc().isValid()) {
2276 SMRange OperandRange = Operand->getLocRange();
2277 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002278 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002279 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002280 }
2281
Chad Rosierb4fdade2012-08-21 19:36:59 +00002282 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002283 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002284 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002285
Chris Lattnerec6789f2010-09-06 20:08:02 +00002286 // If one instruction matched with a missing feature, report this as a
2287 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002288 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2289 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002290 std::string Msg = "instruction requires:";
2291 unsigned Mask = 1;
2292 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2293 if (ErrorInfoMissingFeature & Mask) {
2294 Msg += " ";
2295 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2296 }
2297 Mask <<= 1;
2298 }
2299 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002300 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002301
Chris Lattnera008e8a2010-09-06 21:54:15 +00002302 // If one instruction matched with an invalid operand, report this as an
2303 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002304 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2305 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002306 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002307 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002308 return true;
2309 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002310
Chris Lattnerec6789f2010-09-06 20:08:02 +00002311 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002312 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002313 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002314 return true;
2315}
2316
2317
Devang Pateldd929fc2012-01-12 18:03:40 +00002318bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002319 StringRef IDVal = DirectiveID.getIdentifier();
2320 if (IDVal == ".word")
2321 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002322 else if (IDVal.startswith(".code"))
2323 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002324 else if (IDVal.startswith(".att_syntax")) {
2325 getParser().setAssemblerDialect(0);
2326 return false;
2327 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002328 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002329 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2330 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002331 // FIXME : Handle noprefix
2332 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002333 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002334 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002335 }
2336 return false;
2337 }
Chris Lattner537ca842010-10-30 17:38:55 +00002338 return true;
2339}
2340
2341/// ParseDirectiveWord
2342/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002343bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002344 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2345 for (;;) {
2346 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002347 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002348 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002349
Eric Christopher1ced2082013-01-09 03:52:05 +00002350 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002351
Chris Lattner537ca842010-10-30 17:38:55 +00002352 if (getLexer().is(AsmToken::EndOfStatement))
2353 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002354
Chris Lattner537ca842010-10-30 17:38:55 +00002355 // FIXME: Improve diagnostic.
2356 if (getLexer().isNot(AsmToken::Comma))
2357 return Error(L, "unexpected token in directive");
2358 Parser.Lex();
2359 }
2360 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002361
Chris Lattner537ca842010-10-30 17:38:55 +00002362 Parser.Lex();
2363 return false;
2364}
2365
Evan Chengbd27f5a2011-07-27 00:38:12 +00002366/// ParseDirectiveCode
2367/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002368bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002369 if (IDVal == ".code32") {
2370 Parser.Lex();
2371 if (is64BitMode()) {
2372 SwitchMode();
2373 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2374 }
2375 } else if (IDVal == ".code64") {
2376 Parser.Lex();
2377 if (!is64BitMode()) {
2378 SwitchMode();
2379 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2380 }
2381 } else {
2382 return Error(L, "unexpected directive " + IDVal);
2383 }
Chris Lattner537ca842010-10-30 17:38:55 +00002384
Evan Chengbd27f5a2011-07-27 00:38:12 +00002385 return false;
2386}
Chris Lattner537ca842010-10-30 17:38:55 +00002387
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002388// Force static initialization.
2389extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002390 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2391 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002392}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002393
Chris Lattner0692ee62010-09-06 19:11:01 +00002394#define GET_REGISTER_MATCHER
2395#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002396#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002397#include "X86GenAsmMatcher.inc"