blob: e4623228b397d9e4d2d21dba28e4070623053237 [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);
64 X86Operand *ParseIntelBracExpression(unsigned SegReg, uint64_t ImmDisp,
65 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 Rosierdd2e8952013-01-14 22:31:35 +0000679enum IntelBracExprState {
680 IBES_START,
681 IBES_LBRAC,
682 IBES_RBRAC,
683 IBES_REGISTER,
684 IBES_REGISTER_STAR,
685 IBES_REGISTER_STAR_INTEGER,
686 IBES_INTEGER,
687 IBES_INTEGER_STAR,
688 IBES_INDEX_REGISTER,
689 IBES_IDENTIFIER,
690 IBES_DISP_EXPR,
691 IBES_MINUS,
692 IBES_ERROR
693};
694
695class IntelBracExprStateMachine {
696 IntelBracExprState State;
697 unsigned BaseReg, IndexReg, Scale;
698 int64_t Disp;
699
700 unsigned TmpReg;
701 int64_t TmpInteger;
702
703 bool isPlus;
704
705public:
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000706 IntelBracExprStateMachine(MCAsmParser &parser, int64_t disp) :
707 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(disp),
Chad Rosierdd2e8952013-01-14 22:31:35 +0000708 TmpReg(0), TmpInteger(0), isPlus(true) {}
709
710 unsigned getBaseReg() { return BaseReg; }
711 unsigned getIndexReg() { return IndexReg; }
712 unsigned getScale() { return Scale; }
713 int64_t getDisp() { return Disp; }
714 bool isValidEndState() { return State == IBES_RBRAC; }
715
716 void onPlus() {
717 switch (State) {
718 default:
719 State = IBES_ERROR;
720 break;
721 case IBES_INTEGER:
722 State = IBES_START;
723 if (isPlus)
724 Disp += TmpInteger;
725 else
726 Disp -= TmpInteger;
727 break;
728 case IBES_REGISTER:
729 State = IBES_START;
730 // If we already have a BaseReg, then assume this is the IndexReg with a
731 // scale of 1.
732 if (!BaseReg) {
733 BaseReg = TmpReg;
734 } else {
735 assert (!IndexReg && "BaseReg/IndexReg already set!");
736 IndexReg = TmpReg;
737 Scale = 1;
738 }
739 break;
740 case IBES_INDEX_REGISTER:
741 State = IBES_START;
742 break;
743 }
744 isPlus = true;
745 }
746 void onMinus() {
747 switch (State) {
748 default:
749 State = IBES_ERROR;
750 break;
751 case IBES_START:
752 State = IBES_MINUS;
753 break;
754 case IBES_INTEGER:
755 State = IBES_START;
756 if (isPlus)
757 Disp += TmpInteger;
758 else
759 Disp -= TmpInteger;
760 break;
761 case IBES_REGISTER:
762 State = IBES_START;
763 // If we already have a BaseReg, then assume this is the IndexReg with a
764 // scale of 1.
765 if (!BaseReg) {
766 BaseReg = TmpReg;
767 } else {
768 assert (!IndexReg && "BaseReg/IndexReg already set!");
769 IndexReg = TmpReg;
770 Scale = 1;
771 }
772 break;
773 case IBES_INDEX_REGISTER:
774 State = IBES_START;
775 break;
776 }
777 isPlus = false;
778 }
779 void onRegister(unsigned Reg) {
780 switch (State) {
781 default:
782 State = IBES_ERROR;
783 break;
784 case IBES_START:
785 State = IBES_REGISTER;
786 TmpReg = Reg;
787 break;
788 case IBES_INTEGER_STAR:
789 assert (!IndexReg && "IndexReg already set!");
790 State = IBES_INDEX_REGISTER;
791 IndexReg = Reg;
792 Scale = TmpInteger;
793 break;
794 }
795 }
796 void onDispExpr() {
797 switch (State) {
798 default:
799 State = IBES_ERROR;
800 break;
801 case IBES_START:
802 State = IBES_DISP_EXPR;
803 break;
804 }
805 }
806 void onInteger(int64_t TmpInt) {
807 switch (State) {
808 default:
809 State = IBES_ERROR;
810 break;
811 case IBES_START:
812 State = IBES_INTEGER;
813 TmpInteger = TmpInt;
814 break;
815 case IBES_MINUS:
816 State = IBES_INTEGER;
817 TmpInteger = TmpInt;
818 break;
819 case IBES_REGISTER_STAR:
820 assert (!IndexReg && "IndexReg already set!");
821 State = IBES_INDEX_REGISTER;
822 IndexReg = TmpReg;
823 Scale = TmpInt;
824 break;
825 }
826 }
827 void onStar() {
828 switch (State) {
829 default:
830 State = IBES_ERROR;
831 break;
832 case IBES_INTEGER:
833 State = IBES_INTEGER_STAR;
834 break;
835 case IBES_REGISTER:
836 State = IBES_REGISTER_STAR;
837 break;
838 }
839 }
840 void onLBrac() {
841 switch (State) {
842 default:
843 State = IBES_ERROR;
844 break;
845 case IBES_RBRAC:
846 State = IBES_START;
847 isPlus = true;
848 break;
849 }
850 }
851 void onRBrac() {
852 switch (State) {
853 default:
854 State = IBES_ERROR;
855 break;
856 case IBES_DISP_EXPR:
857 State = IBES_RBRAC;
858 break;
859 case IBES_INTEGER:
860 State = IBES_RBRAC;
861 if (isPlus)
862 Disp += TmpInteger;
863 else
864 Disp -= TmpInteger;
865 break;
866 case IBES_REGISTER:
867 State = IBES_RBRAC;
868 // If we already have a BaseReg, then assume this is the IndexReg with a
869 // scale of 1.
870 if (!BaseReg) {
871 BaseReg = TmpReg;
872 } else {
873 assert (!IndexReg && "BaseReg/IndexReg already set!");
874 IndexReg = TmpReg;
875 Scale = 1;
876 }
877 break;
878 case IBES_INDEX_REGISTER:
879 State = IBES_RBRAC;
880 break;
881 }
882 }
883};
884
Chad Rosierd3e74162013-03-19 21:11:56 +0000885X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
886 SMLoc End, SMLoc SizeDirLoc,
887 unsigned Size) {
888 bool NeedSizeDir = false;
889 bool IsVarDecl = false;
890 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
891 const MCSymbol &Sym = SymRef->getSymbol();
892 // FIXME: The SemaLookup will fail if the name is anything other then an
893 // identifier.
894 // FIXME: Pass a valid SMLoc.
895 unsigned tLength, tSize, tType;
896 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
897 tSize, tType, IsVarDecl);
898 if (!Size) {
899 Size = tType * 8; // Size is in terms of bits in this context.
900 NeedSizeDir = Size > 0;
901 }
902 }
903
904 // If this is not a VarDecl then assume it is a FuncDecl or some other label
905 // reference. We need an 'r' constraint here, so we need to create register
906 // operand to ensure proper matching. Just pick a GPR based on the size of
907 // a pointer.
908 if (!IsVarDecl) {
909 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
910 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
911 }
912
913 if (NeedSizeDir)
914 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
915 /*Len*/0, Size));
916
917 // When parsing inline assembly we set the base register to a non-zero value
918 // as we don't know the actual value at this time. This is necessary to
919 // get the matching correct in some cases.
920 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
921 /*Scale*/1, Start, End, Size);
922}
923
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000924X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
925 uint64_t ImmDisp,
Devang Patel7c64fe62012-01-23 18:31:58 +0000926 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000927 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000928 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000929
Devang Pateld37ad242012-01-17 18:00:18 +0000930 // Eat '['
931 if (getLexer().isNot(AsmToken::LBrac))
932 return ErrorOperand(Start, "Expected '[' token!");
933 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000934
Chad Rosierdd2e8952013-01-14 22:31:35 +0000935 unsigned TmpReg = 0;
936
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000937 // Try to handle '[' 'Symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000938 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000939 if (ParseRegister(TmpReg, Start, End)) {
940 const MCExpr *Disp;
Chad Rosier30c729b2013-04-02 20:02:33 +0000941 SMLoc IdentStart = Tok.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000942 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000943 return 0;
944
Chad Rosier30c729b2013-04-02 20:02:33 +0000945 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, IdentStart))
946 return Err;
947
Devang Pateld37ad242012-01-17 18:00:18 +0000948 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000949 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000950
951 // FIXME: We don't handle 'ImmDisp' '[' 'Symbol' ']'.
952 if (ImmDisp)
953 return ErrorOperand(Start, "Unsupported immediate displacement!");
954
Chad Rosier4fb25b72013-02-15 21:58:13 +0000955 // Adjust the EndLoc due to the ']'.
956 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000957 Parser.Lex();
Chad Rosierd3e74162013-03-19 21:11:56 +0000958 if (!isParsingInlineAsm())
959 return X86Operand::CreateMem(Disp, Start, End, Size);
960
961 // We want the size directive before the '['.
962 SMLoc SizeDirLoc = SMLoc::getFromPointer(Start.getPointer()-1);
963 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000964 }
Devang Pateld37ad242012-01-17 18:00:18 +0000965 }
966
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000967 // Parse [ BaseReg + Scale*IndexReg + Disp ]. We may have already parsed an
968 // immediate displacement before the bracketed expression.
Chad Rosierdd2e8952013-01-14 22:31:35 +0000969 bool Done = false;
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000970 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000971
Chad Rosierdd2e8952013-01-14 22:31:35 +0000972 // If we parsed a register, then the end loc has already been set and
973 // the identifier has already been lexed. We also need to update the
974 // state.
975 if (TmpReg)
976 SM.onRegister(TmpReg);
977
978 const MCExpr *Disp = 0;
979 while (!Done) {
980 bool UpdateLocLex = true;
981
982 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
983 // identifier. Don't try an parse it as a register.
984 if (Tok.getString().startswith("."))
985 break;
986
987 switch (getLexer().getKind()) {
988 default: {
989 if (SM.isValidEndState()) {
990 Done = true;
991 break;
992 }
993 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
994 }
995 case AsmToken::Identifier: {
996 // This could be a register or a displacement expression.
997 if(!ParseRegister(TmpReg, Start, End)) {
998 SM.onRegister(TmpReg);
999 UpdateLocLex = false;
1000 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001001 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +00001002 SM.onDispExpr();
1003 UpdateLocLex = false;
1004 break;
1005 }
1006 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1007 }
1008 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +00001009 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +00001010 SM.onInteger(Val);
1011 break;
1012 }
1013 case AsmToken::Plus: SM.onPlus(); break;
1014 case AsmToken::Minus: SM.onMinus(); break;
1015 case AsmToken::Star: SM.onStar(); break;
1016 case AsmToken::LBrac: SM.onLBrac(); break;
1017 case AsmToken::RBrac: SM.onRBrac(); break;
1018 }
1019 if (!Done && UpdateLocLex) {
1020 End = Tok.getLoc();
1021 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +00001022 }
Devang Pateld37ad242012-01-17 18:00:18 +00001023 }
1024
Chad Rosierdd2e8952013-01-14 22:31:35 +00001025 if (!Disp)
1026 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +00001027
Chad Rosierddb53ef2012-10-26 22:01:25 +00001028 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001029 if (Tok.getString().startswith(".")) {
1030 SmallString<64> Err;
1031 const MCExpr *NewDisp;
1032 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1033 return ErrorOperand(Tok.getLoc(), Err);
1034
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001035 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001036 Parser.Lex(); // Eat the field.
1037 Disp = NewDisp;
1038 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001039
Chad Rosierdd2e8952013-01-14 22:31:35 +00001040 int BaseReg = SM.getBaseReg();
1041 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001042
Chad Rosierdd2e8952013-01-14 22:31:35 +00001043 // handle [-42]
1044 if (!BaseReg && !IndexReg) {
1045 if (!SegReg)
1046 return X86Operand::CreateMem(Disp, Start, End);
1047 else
1048 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1049 }
1050
1051 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001052 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001053 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001054}
1055
Chad Rosier30c729b2013-04-02 20:02:33 +00001056// Inline assembly may use variable names with namespace alias qualifiers.
1057X86Operand *X86AsmParser::ParseIntelVarWithQualifier(const MCExpr *&Disp,
1058 SMLoc &IdentStart) {
1059 // We should only see Foo::Bar if we're parsing inline assembly.
1060 if (!isParsingInlineAsm())
1061 return 0;
1062
1063 // If we don't see a ':' then there can't be a qualifier.
1064 if (getLexer().isNot(AsmToken::Colon))
1065 return 0;
1066
1067
1068 bool Done = false;
1069 const AsmToken &Tok = Parser.getTok();
1070 SMLoc IdentEnd = Tok.getEndLoc();
1071 while (!Done) {
1072 switch (getLexer().getKind()) {
1073 default:
1074 Done = true;
1075 break;
1076 case AsmToken::Colon:
1077 getLexer().Lex(); // Consume ':'.
1078 if (getLexer().isNot(AsmToken::Colon))
1079 return ErrorOperand(Tok.getLoc(), "Expected ':' token!");
1080 getLexer().Lex(); // Consume second ':'.
1081 if (getLexer().isNot(AsmToken::Identifier))
1082 return ErrorOperand(Tok.getLoc(), "Expected an identifier token!");
1083 break;
1084 case AsmToken::Identifier:
1085 IdentEnd = Tok.getEndLoc();
1086 getLexer().Lex(); // Consume the identifier.
1087 break;
1088 }
1089 }
1090 size_t Len = IdentEnd.getPointer() - IdentStart.getPointer();
1091 StringRef Identifier(IdentStart.getPointer(), Len);
1092 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
1093 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1094 Disp = MCSymbolRefExpr::Create(Sym, Variant, getParser().getContext());
1095 return 0;
1096}
1097
Devang Pateld37ad242012-01-17 18:00:18 +00001098/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001099X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1100 uint64_t ImmDisp,
1101 SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001102 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001103 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001104
1105 unsigned Size = getIntelMemOperandSize(Tok.getString());
1106 if (Size) {
1107 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001108 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1109 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001110 Parser.Lex();
1111 }
1112
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001113 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1114 if (getLexer().is(AsmToken::Integer)) {
1115 const AsmToken &IntTok = Parser.getTok();
1116 if (isParsingInlineAsm())
1117 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1118 IntTok.getLoc()));
1119 uint64_t ImmDisp = IntTok.getIntVal();
1120 Parser.Lex(); // Eat the integer.
1121 if (getLexer().isNot(AsmToken::LBrac))
1122 return ErrorOperand(Start, "Expected '[' token!");
1123 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
1124 }
1125
Chad Rosierc0a14b82012-10-24 17:22:29 +00001126 if (getLexer().is(AsmToken::LBrac))
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001127 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001128
1129 if (!ParseRegister(SegReg, Start, End)) {
1130 // Handel SegReg : [ ... ]
1131 if (getLexer().isNot(AsmToken::Colon))
1132 return ErrorOperand(Start, "Expected ':' token!");
1133 Parser.Lex(); // Eat :
1134 if (getLexer().isNot(AsmToken::LBrac))
1135 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001136 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001137 }
Devang Pateld37ad242012-01-17 18:00:18 +00001138
1139 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Chad Rosier30c729b2013-04-02 20:02:33 +00001140 SMLoc IdentStart = Tok.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001141 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001142 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001143
Chad Rosier2a784132012-10-23 23:31:33 +00001144 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001145 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier30c729b2013-04-02 20:02:33 +00001146
1147 if (X86Operand *Err = ParseIntelVarWithQualifier(Disp, IdentStart))
1148 return Err;
1149
Chad Rosierd3e74162013-03-19 21:11:56 +00001150 return CreateMemForInlineAsm(Disp, Start, End, Start, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001151}
1152
Chad Rosier22f441a2012-10-24 22:21:50 +00001153/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001154bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1155 const MCExpr **NewDisp,
1156 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001157 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001158 uint64_t OrigDispVal, DotDispVal;
1159
1160 // FIXME: Handle non-constant expressions.
1161 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1162 OrigDispVal = OrigDisp->getValue();
1163 } else {
1164 Err = "Non-constant offsets are not supported!";
1165 return true;
1166 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001167
1168 // Drop the '.'.
1169 StringRef DotDispStr = Tok.getString().drop_front(1);
1170
Chad Rosier22f441a2012-10-24 22:21:50 +00001171 // .Imm gets lexed as a real.
1172 if (Tok.is(AsmToken::Real)) {
1173 APInt DotDisp;
1174 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001175 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001176 } else if (Tok.is(AsmToken::Identifier)) {
1177 // We should only see an identifier when parsing the original inline asm.
1178 // The front-end should rewrite this in terms of immediates.
1179 assert (isParsingInlineAsm() && "Unexpected field name!");
1180
1181 unsigned DotDisp;
1182 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1183 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1184 DotDisp)) {
1185 Err = "Unable to lookup field reference!";
1186 return true;
1187 }
1188 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001189 } else {
1190 Err = "Unexpected token type!";
1191 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001192 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001193
Chad Rosierec130222012-10-25 21:51:10 +00001194 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1195 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1196 unsigned Len = DotDispStr.size();
1197 unsigned Val = OrigDispVal + DotDispVal;
1198 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1199 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001200 }
1201
1202 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1203 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001204}
1205
Chad Rosierc0a14b82012-10-24 17:22:29 +00001206/// Parse the 'offset' operator. This operator is used to specify the
1207/// location rather then the content of a variable.
1208X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1209 SMLoc OffsetOfLoc = Start;
1210 Parser.Lex(); // Eat offset.
1211 Start = Parser.getTok().getLoc();
1212 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1213
Chad Rosier6e431572012-10-26 16:09:20 +00001214 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001215 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001216 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001217 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001218
Chad Rosier6e431572012-10-26 16:09:20 +00001219 // Don't emit the offset operator.
1220 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1221
Chad Rosierc0a14b82012-10-24 17:22:29 +00001222 // The offset operator will have an 'r' constraint, thus we need to create
1223 // register operand to ensure proper matching. Just pick a GPR based on
1224 // the size of a pointer.
1225 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001226 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1227 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001228}
1229
Chad Rosier505bca32013-01-17 19:21:48 +00001230enum IntelOperatorKind {
1231 IOK_LENGTH,
1232 IOK_SIZE,
1233 IOK_TYPE
1234};
1235
1236/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1237/// returns the number of elements in an array. It returns the value 1 for
1238/// non-array variables. The SIZE operator returns the size of a C or C++
1239/// variable. A variable's size is the product of its LENGTH and TYPE. The
1240/// TYPE operator returns the size of a C or C++ type or variable. If the
1241/// variable is an array, TYPE returns the size of a single element.
1242X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001243 SMLoc TypeLoc = Start;
1244 Parser.Lex(); // Eat offset.
1245 Start = Parser.getTok().getLoc();
1246 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1247
1248 SMLoc End;
1249 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001250 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001251 return 0;
1252
Chad Rosier505bca32013-01-17 19:21:48 +00001253 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001254 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1255 const MCSymbol &Sym = SymRef->getSymbol();
1256 // FIXME: The SemaLookup will fail if the name is anything other then an
1257 // identifier.
1258 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001259 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001260 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1261 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001262 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001263 }
1264 unsigned CVal;
1265 switch(OpKind) {
1266 default: llvm_unreachable("Unexpected operand kind!");
1267 case IOK_LENGTH: CVal = Length; break;
1268 case IOK_SIZE: CVal = Size; break;
1269 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001270 }
1271
1272 // Rewrite the type operator and the C or C++ type or variable in terms of an
1273 // immediate. E.g. TYPE foo -> $$4
1274 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001275 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001276
Chad Rosier505bca32013-01-17 19:21:48 +00001277 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosier811ddf62013-03-19 21:58:18 +00001278 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosierefcb3d92012-10-26 18:04:20 +00001279}
1280
Devang Pateld37ad242012-01-17 18:00:18 +00001281X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001282 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001283 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001284
Chad Rosier505bca32013-01-17 19:21:48 +00001285 // Offset, length, type and size operators.
1286 if (isParsingInlineAsm()) {
1287 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1288 return ParseIntelOffsetOfOperator(Start);
1289 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1290 return ParseIntelOperator(Start, IOK_LENGTH);
1291 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1292 return ParseIntelOperator(Start, IOK_SIZE);
1293 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1294 return ParseIntelOperator(Start, IOK_TYPE);
1295 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001296
Chad Rosier505bca32013-01-17 19:21:48 +00001297 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001298 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1299 getLexer().is(AsmToken::Minus)) {
1300 const MCExpr *Val;
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001301 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001302 if (!getParser().parseExpression(Val, End)) {
Chad Rosier811ddf62013-03-19 21:58:18 +00001303 if (isParsingInlineAsm())
1304 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001305 // Immediate.
1306 if (getLexer().isNot(AsmToken::LBrac))
1307 return X86Operand::CreateImm(Val, Start, End);
1308
1309 // Only positive immediates are valid.
1310 if (!isInteger) {
1311 Error(Parser.getTok().getLoc(), "expected a positive immediate "
1312 "displacement before bracketed expr.");
1313 return 0;
1314 }
1315
1316 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1317 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1318 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Pateld37ad242012-01-17 18:00:18 +00001319 }
1320 }
1321
Chad Rosier505bca32013-01-17 19:21:48 +00001322 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001323 unsigned RegNo = 0;
1324 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001325 // If this is a segment register followed by a ':', then this is the start
1326 // of a memory reference, otherwise this is a normal register reference.
1327 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001328 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001329
1330 getParser().Lex(); // Eat the colon.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001331 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001332 }
1333
Chad Rosier505bca32013-01-17 19:21:48 +00001334 // Memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001335 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001336}
1337
Devang Pateldd929fc2012-01-12 18:03:40 +00001338X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001339 switch (getLexer().getKind()) {
1340 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001341 // Parse a memory operand with no segment register.
1342 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001343 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001344 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001345 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001346 SMLoc Start, End;
1347 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001348 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001349 Error(Start, "%eiz and %riz can only be used as index registers",
1350 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001351 return 0;
1352 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001353
Chris Lattnereef6d782010-04-17 18:56:34 +00001354 // If this is a segment register followed by a ':', then this is the start
1355 // of a memory reference, otherwise this is a normal register reference.
1356 if (getLexer().isNot(AsmToken::Colon))
1357 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001358
Chris Lattnereef6d782010-04-17 18:56:34 +00001359 getParser().Lex(); // Eat the colon.
1360 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001361 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001362 case AsmToken::Dollar: {
1363 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001364 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001365 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001366 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001367 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001368 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001369 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001370 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001371 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001372}
1373
Chris Lattnereef6d782010-04-17 18:56:34 +00001374/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1375/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001376X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001377
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001378 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1379 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001380 // only way to do this without lookahead is to eat the '(' and see what is
1381 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001382 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001383 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001384 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001385 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001386
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001387 // After parsing the base expression we could either have a parenthesized
1388 // memory address or not. If not, return now. If so, eat the (.
1389 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001390 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001391 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001392 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001393 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001394 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001395
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001396 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001397 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001398 } else {
1399 // Okay, we have a '('. We don't know if this is an expression or not, but
1400 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001401 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001402 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001403
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001404 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001405 // Nothing to do here, fall into the code below with the '(' part of the
1406 // memory operand consumed.
1407 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001408 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001409
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001410 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001411 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001412 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001413
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001414 // After parsing the base expression we could either have a parenthesized
1415 // memory address or not. If not, return now. If so, eat the (.
1416 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001417 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001418 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001419 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001420 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001421 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001422
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001423 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001424 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001425 }
1426 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001427
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001428 // If we reached here, then we just ate the ( of the memory operand. Process
1429 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001430 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001431 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001432
Chris Lattner29ef9a22010-01-15 18:51:29 +00001433 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001434 SMLoc StartLoc, EndLoc;
1435 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001436 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001437 Error(StartLoc, "eiz and riz can only be used as index registers",
1438 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001439 return 0;
1440 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001441 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001442
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001443 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001444 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001445 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001446
1447 // Following the comma we should have either an index register, or a scale
1448 // value. We don't support the later form, but we want to parse it
1449 // correctly.
1450 //
1451 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001452 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001453 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001454 SMLoc L;
1455 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001456
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001457 if (getLexer().isNot(AsmToken::RParen)) {
1458 // Parse the scale amount:
1459 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001460 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001461 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001462 "expected comma in scale expression");
1463 return 0;
1464 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001465 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001466
1467 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001468 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001469
1470 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001471 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001472 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001473 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001474 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001475
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001476 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001477 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1478 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1479 return 0;
1480 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001481 Scale = (unsigned)ScaleVal;
1482 }
1483 }
1484 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001485 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001486 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001487 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001488
1489 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001490 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001491 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001492
Daniel Dunbaree910252010-08-24 19:13:38 +00001493 if (Value != 1)
1494 Warning(Loc, "scale factor without index register is ignored");
1495 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001496 }
1497 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001498
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001499 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001500 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001501 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001502 return 0;
1503 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001504 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001505 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001506
Kevin Enderby84faf652012-03-12 21:32:09 +00001507 // If we have both a base register and an index register make sure they are
1508 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001509 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001510 if (BaseReg != 0 && IndexReg != 0) {
1511 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001512 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1513 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001514 IndexReg != X86::RIZ) {
1515 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1516 return 0;
1517 }
1518 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001519 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1520 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001521 IndexReg != X86::EIZ){
1522 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1523 return 0;
1524 }
1525 }
1526
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001527 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1528 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001529}
1530
Devang Pateldd929fc2012-01-12 18:03:40 +00001531bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001532ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001533 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001534 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001535 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001536
Chris Lattnerd8f71792010-11-28 20:23:50 +00001537 // FIXME: Hack to recognize setneb as setne.
1538 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1539 PatchedName != "setb" && PatchedName != "setnb")
1540 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001541
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001542 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1543 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001544 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001545 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1546 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001547 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001548 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001549 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001550 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001551 .Case("eq", 0x00)
1552 .Case("lt", 0x01)
1553 .Case("le", 0x02)
1554 .Case("unord", 0x03)
1555 .Case("neq", 0x04)
1556 .Case("nlt", 0x05)
1557 .Case("nle", 0x06)
1558 .Case("ord", 0x07)
1559 /* AVX only from here */
1560 .Case("eq_uq", 0x08)
1561 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001562 .Case("ngt", 0x0A)
1563 .Case("false", 0x0B)
1564 .Case("neq_oq", 0x0C)
1565 .Case("ge", 0x0D)
1566 .Case("gt", 0x0E)
1567 .Case("true", 0x0F)
1568 .Case("eq_os", 0x10)
1569 .Case("lt_oq", 0x11)
1570 .Case("le_oq", 0x12)
1571 .Case("unord_s", 0x13)
1572 .Case("neq_us", 0x14)
1573 .Case("nlt_uq", 0x15)
1574 .Case("nle_uq", 0x16)
1575 .Case("ord_s", 0x17)
1576 .Case("eq_us", 0x18)
1577 .Case("nge_uq", 0x19)
1578 .Case("ngt_uq", 0x1A)
1579 .Case("false_os", 0x1B)
1580 .Case("neq_os", 0x1C)
1581 .Case("ge_oq", 0x1D)
1582 .Case("gt_oq", 0x1E)
1583 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001584 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001585 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001586 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1587 getParser().getContext());
1588 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001589 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001590 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001591 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001592 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001593 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001594 } else {
1595 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001596 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001597 }
1598 }
1599 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001600
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001601 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001602
Devang Patel885f65b2012-01-30 22:47:12 +00001603 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001604 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001605
Chris Lattner2544f422010-09-08 05:17:37 +00001606 // Determine whether this is an instruction prefix.
1607 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001608 Name == "lock" || Name == "rep" ||
1609 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001610 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001611 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001612
1613
Chris Lattner2544f422010-09-08 05:17:37 +00001614 // This does the actual operand parsing. Don't parse any more if we have a
1615 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1616 // just want to parse the "lock" as the first instruction and the "incl" as
1617 // the next one.
1618 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001619
1620 // Parse '*' modifier.
1621 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001622 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001623 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001624 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001625 }
1626
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001627 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001628 if (X86Operand *Op = ParseOperand())
1629 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001630 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001631 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001632 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001633 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001634
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001635 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001636 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001637
1638 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001639 if (X86Operand *Op = ParseOperand())
1640 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001641 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001642 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001643 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001644 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001645 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001646
Chris Lattnercbf8a982010-09-11 16:18:25 +00001647 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001648 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001649 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001650 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001651 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001652 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001653
Chris Lattner2544f422010-09-08 05:17:37 +00001654 if (getLexer().is(AsmToken::EndOfStatement))
1655 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001656 else if (isPrefix && getLexer().is(AsmToken::Slash))
1657 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001658
Devang Patel885f65b2012-01-30 22:47:12 +00001659 if (ExtraImmOp && isParsingIntelSyntax())
1660 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1661
Chris Lattner98c870f2010-11-06 19:25:43 +00001662 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1663 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1664 // documented form in various unofficial manuals, so a lot of code uses it.
1665 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1666 Operands.size() == 3) {
1667 X86Operand &Op = *(X86Operand*)Operands.back();
1668 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1669 isa<MCConstantExpr>(Op.Mem.Disp) &&
1670 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1671 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1672 SMLoc Loc = Op.getEndLoc();
1673 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1674 delete &Op;
1675 }
1676 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001677 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1678 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1679 Operands.size() == 3) {
1680 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1681 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1682 isa<MCConstantExpr>(Op.Mem.Disp) &&
1683 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1684 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1685 SMLoc Loc = Op.getEndLoc();
1686 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1687 delete &Op;
1688 }
1689 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001690 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1691 if (Name.startswith("ins") && Operands.size() == 3 &&
1692 (Name == "insb" || Name == "insw" || Name == "insl")) {
1693 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1694 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1695 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1696 Operands.pop_back();
1697 Operands.pop_back();
1698 delete &Op;
1699 delete &Op2;
1700 }
1701 }
1702
1703 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1704 if (Name.startswith("outs") && Operands.size() == 3 &&
1705 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1706 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1707 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1708 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1709 Operands.pop_back();
1710 Operands.pop_back();
1711 delete &Op;
1712 delete &Op2;
1713 }
1714 }
1715
1716 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1717 if (Name.startswith("movs") && Operands.size() == 3 &&
1718 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001719 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001720 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1721 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1722 if (isSrcOp(Op) && isDstOp(Op2)) {
1723 Operands.pop_back();
1724 Operands.pop_back();
1725 delete &Op;
1726 delete &Op2;
1727 }
1728 }
1729 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1730 if (Name.startswith("lods") && Operands.size() == 3 &&
1731 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001732 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001733 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1734 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1735 if (isSrcOp(*Op1) && Op2->isReg()) {
1736 const char *ins;
1737 unsigned reg = Op2->getReg();
1738 bool isLods = Name == "lods";
1739 if (reg == X86::AL && (isLods || Name == "lodsb"))
1740 ins = "lodsb";
1741 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1742 ins = "lodsw";
1743 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1744 ins = "lodsl";
1745 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1746 ins = "lodsq";
1747 else
1748 ins = NULL;
1749 if (ins != NULL) {
1750 Operands.pop_back();
1751 Operands.pop_back();
1752 delete Op1;
1753 delete Op2;
1754 if (Name != ins)
1755 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1756 }
1757 }
1758 }
1759 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1760 if (Name.startswith("stos") && Operands.size() == 3 &&
1761 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001762 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001763 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1764 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1765 if (isDstOp(*Op2) && Op1->isReg()) {
1766 const char *ins;
1767 unsigned reg = Op1->getReg();
1768 bool isStos = Name == "stos";
1769 if (reg == X86::AL && (isStos || Name == "stosb"))
1770 ins = "stosb";
1771 else if (reg == X86::AX && (isStos || Name == "stosw"))
1772 ins = "stosw";
1773 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1774 ins = "stosl";
1775 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1776 ins = "stosq";
1777 else
1778 ins = NULL;
1779 if (ins != NULL) {
1780 Operands.pop_back();
1781 Operands.pop_back();
1782 delete Op1;
1783 delete Op2;
1784 if (Name != ins)
1785 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1786 }
1787 }
1788 }
1789
Chris Lattnere9e16a32010-09-15 04:33:27 +00001790 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001791 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001792 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001793 Name.startswith("shl") || Name.startswith("sal") ||
1794 Name.startswith("rcl") || Name.startswith("rcr") ||
1795 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001796 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001797 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001798 // Intel syntax
1799 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1800 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001801 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1802 delete Operands[2];
1803 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001804 }
1805 } else {
1806 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1807 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001808 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1809 delete Operands[1];
1810 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001811 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001812 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001813 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001814
Chris Lattner15f89512011-04-09 19:41:05 +00001815 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1816 // instalias with an immediate operand yet.
1817 if (Name == "int" && Operands.size() == 2) {
1818 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1819 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1820 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1821 delete Operands[1];
1822 Operands.erase(Operands.begin() + 1);
1823 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1824 }
1825 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001826
Chris Lattner98986712010-01-14 22:21:20 +00001827 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001828}
1829
Craig Topper4bef9612013-03-18 02:53:34 +00001830static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1831 bool isCmp) {
1832 MCInst TmpInst;
1833 TmpInst.setOpcode(Opcode);
1834 if (!isCmp)
1835 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1836 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1837 TmpInst.addOperand(Inst.getOperand(0));
1838 Inst = TmpInst;
1839 return true;
1840}
1841
1842static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1843 bool isCmp = false) {
1844 if (!Inst.getOperand(0).isImm() ||
1845 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1846 return false;
1847
1848 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1849}
1850
1851static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1852 bool isCmp = false) {
1853 if (!Inst.getOperand(0).isImm() ||
1854 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1855 return false;
1856
1857 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1858}
1859
1860static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1861 bool isCmp = false) {
1862 if (!Inst.getOperand(0).isImm() ||
1863 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1864 return false;
1865
1866 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1867}
1868
Devang Pateldd929fc2012-01-12 18:03:40 +00001869bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001870processInstruction(MCInst &Inst,
1871 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1872 switch (Inst.getOpcode()) {
1873 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001874 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1875 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1876 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1877 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1878 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1879 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1880 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1881 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1882 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1883 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1884 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1885 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1886 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1887 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1888 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1889 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1890 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1891 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001892 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1893 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1894 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1895 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1896 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1897 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001898 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001899}
1900
Jim Grosbach3ca63822012-11-14 18:04:47 +00001901static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001902bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001903MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001904 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001905 MCStreamer &Out, unsigned &ErrorInfo,
1906 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001907 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001908 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1909 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001910 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001911
Chris Lattner7c51a312010-09-29 01:50:45 +00001912 // First, handle aliases that expand to multiple instructions.
1913 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001914 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001915 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001916 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001917 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001918 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001919 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001920 MCInst Inst;
1921 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001922 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001923 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001924 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001925
Chris Lattner0bb83a82010-09-30 16:39:29 +00001926 const char *Repl =
1927 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001928 .Case("finit", "fninit")
1929 .Case("fsave", "fnsave")
1930 .Case("fstcw", "fnstcw")
1931 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001932 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001933 .Case("fstsw", "fnstsw")
1934 .Case("fstsww", "fnstsw")
1935 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001936 .Default(0);
1937 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001938 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001939 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001940 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001941
Chris Lattnera008e8a2010-09-06 21:54:15 +00001942 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001943 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001944
Daniel Dunbarc918d602010-05-04 16:12:42 +00001945 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001946 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001947 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001948 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001949 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001950 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001951 // Some instructions need post-processing to, for example, tweak which
1952 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001953 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001954 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001955 while (processInstruction(Inst, Operands))
1956 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001957
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001958 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001959 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001960 Out.EmitInstruction(Inst);
1961 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001962 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001963 case Match_MissingFeature: {
1964 assert(ErrorInfo && "Unknown missing feature!");
1965 // Special case the error message for the very common case where only
1966 // a single subtarget feature is missing.
1967 std::string Msg = "instruction requires:";
1968 unsigned Mask = 1;
1969 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1970 if (ErrorInfo & Mask) {
1971 Msg += " ";
1972 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1973 }
1974 Mask <<= 1;
1975 }
1976 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1977 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001978 case Match_InvalidOperand:
1979 WasOriginallyInvalidOperand = true;
1980 break;
1981 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001982 break;
1983 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001984
Daniel Dunbarc918d602010-05-04 16:12:42 +00001985 // FIXME: Ideally, we would only attempt suffix matches for things which are
1986 // valid prefixes, and we could just infer the right unambiguous
1987 // type. However, that requires substantially more matcher support than the
1988 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001989
Daniel Dunbarc918d602010-05-04 16:12:42 +00001990 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001991 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001992 SmallString<16> Tmp;
1993 Tmp += Base;
1994 Tmp += ' ';
1995 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001996
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001997 // If this instruction starts with an 'f', then it is a floating point stack
1998 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1999 // 80-bit floating point, which use the suffixes s,l,t respectively.
2000 //
2001 // Otherwise, we assume that this may be an integer instruction, which comes
2002 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2003 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00002004
Daniel Dunbarc918d602010-05-04 16:12:42 +00002005 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002006 Tmp[Base.size()] = Suffixes[0];
2007 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00002008 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002009 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002010
Chad Rosier6e006d32012-10-12 22:53:36 +00002011 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2012 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002013 // If this returned as a missing feature failure, remember that.
2014 if (Match1 == Match_MissingFeature)
2015 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002016 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00002017 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2018 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002019 // If this returned as a missing feature failure, remember that.
2020 if (Match2 == Match_MissingFeature)
2021 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002022 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00002023 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2024 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002025 // If this returned as a missing feature failure, remember that.
2026 if (Match3 == Match_MissingFeature)
2027 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002028 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00002029 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2030 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002031 // If this returned as a missing feature failure, remember that.
2032 if (Match4 == Match_MissingFeature)
2033 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00002034
2035 // Restore the old token.
2036 Op->setTokenValue(Base);
2037
2038 // If exactly one matched, then we treat that as a successful match (and the
2039 // instruction will already have been filled in correctly, since the failing
2040 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00002041 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002042 (Match1 == Match_Success) + (Match2 == Match_Success) +
2043 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00002044 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002045 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002046 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002047 Out.EmitInstruction(Inst);
2048 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00002049 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00002050 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00002051
Chris Lattnerec6789f2010-09-06 20:08:02 +00002052 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002053
Daniel Dunbar09062b12010-08-12 00:55:42 +00002054 // If we had multiple suffix matches, then identify this as an ambiguous
2055 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00002056 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00002057 char MatchChars[4];
2058 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002059 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2060 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2061 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2062 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00002063
2064 SmallString<126> Msg;
2065 raw_svector_ostream OS(Msg);
2066 OS << "ambiguous instructions require an explicit suffix (could be ";
2067 for (unsigned i = 0; i != NumMatches; ++i) {
2068 if (i != 0)
2069 OS << ", ";
2070 if (i + 1 == NumMatches)
2071 OS << "or ";
2072 OS << "'" << Base << MatchChars[i] << "'";
2073 }
2074 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00002075 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002076 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00002077 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002078
Chris Lattnera008e8a2010-09-06 21:54:15 +00002079 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002080
Chris Lattnera008e8a2010-09-06 21:54:15 +00002081 // If all of the instructions reported an invalid mnemonic, then the original
2082 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002083 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2084 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00002085 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00002086 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002087 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002088 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002089 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002090 }
2091
2092 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002093 if (ErrorInfo != ~0U) {
2094 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002095 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002096 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002097
Chad Rosier84125ca2012-10-13 00:26:04 +00002098 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002099 if (Operand->getStartLoc().isValid()) {
2100 SMRange OperandRange = Operand->getLocRange();
2101 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002102 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002103 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002104 }
2105
Chad Rosierb4fdade2012-08-21 19:36:59 +00002106 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002107 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002108 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002109
Chris Lattnerec6789f2010-09-06 20:08:02 +00002110 // If one instruction matched with a missing feature, report this as a
2111 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002112 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2113 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002114 std::string Msg = "instruction requires:";
2115 unsigned Mask = 1;
2116 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2117 if (ErrorInfoMissingFeature & Mask) {
2118 Msg += " ";
2119 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2120 }
2121 Mask <<= 1;
2122 }
2123 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002124 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002125
Chris Lattnera008e8a2010-09-06 21:54:15 +00002126 // If one instruction matched with an invalid operand, report this as an
2127 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002128 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2129 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002130 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002131 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002132 return true;
2133 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002134
Chris Lattnerec6789f2010-09-06 20:08:02 +00002135 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002136 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002137 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002138 return true;
2139}
2140
2141
Devang Pateldd929fc2012-01-12 18:03:40 +00002142bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002143 StringRef IDVal = DirectiveID.getIdentifier();
2144 if (IDVal == ".word")
2145 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002146 else if (IDVal.startswith(".code"))
2147 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002148 else if (IDVal.startswith(".att_syntax")) {
2149 getParser().setAssemblerDialect(0);
2150 return false;
2151 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002152 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2154 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002155 // FIXME : Handle noprefix
2156 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002157 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002158 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002159 }
2160 return false;
2161 }
Chris Lattner537ca842010-10-30 17:38:55 +00002162 return true;
2163}
2164
2165/// ParseDirectiveWord
2166/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002167bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002168 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2169 for (;;) {
2170 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002171 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002172 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002173
Eric Christopher1ced2082013-01-09 03:52:05 +00002174 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002175
Chris Lattner537ca842010-10-30 17:38:55 +00002176 if (getLexer().is(AsmToken::EndOfStatement))
2177 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002178
Chris Lattner537ca842010-10-30 17:38:55 +00002179 // FIXME: Improve diagnostic.
2180 if (getLexer().isNot(AsmToken::Comma))
2181 return Error(L, "unexpected token in directive");
2182 Parser.Lex();
2183 }
2184 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002185
Chris Lattner537ca842010-10-30 17:38:55 +00002186 Parser.Lex();
2187 return false;
2188}
2189
Evan Chengbd27f5a2011-07-27 00:38:12 +00002190/// ParseDirectiveCode
2191/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002192bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002193 if (IDVal == ".code32") {
2194 Parser.Lex();
2195 if (is64BitMode()) {
2196 SwitchMode();
2197 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2198 }
2199 } else if (IDVal == ".code64") {
2200 Parser.Lex();
2201 if (!is64BitMode()) {
2202 SwitchMode();
2203 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2204 }
2205 } else {
2206 return Error(L, "unexpected directive " + IDVal);
2207 }
Chris Lattner537ca842010-10-30 17:38:55 +00002208
Evan Chengbd27f5a2011-07-27 00:38:12 +00002209 return false;
2210}
Chris Lattner537ca842010-10-30 17:38:55 +00002211
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002212// Force static initialization.
2213extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002214 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2215 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002216}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002217
Chris Lattner0692ee62010-09-06 19:11:01 +00002218#define GET_REGISTER_MATCHER
2219#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002220#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002221#include "X86GenAsmMatcher.inc"