blob: d5568e08d30b089f83ce8c254995e597434e27db [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier505bca32013-01-17 19:21:48 +000060 X86Operand *ParseIntelOperator(SMLoc StartLoc, unsigned OpKind);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000061 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000062 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000063 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000064
Chad Rosierd3e74162013-03-19 21:11:56 +000065 X86Operand *CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start, SMLoc End,
66 SMLoc SizeDirLoc, unsigned Size);
67
Chad Rosier5e6b37f2012-10-25 17:37:43 +000068 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
69 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000070
Kevin Enderby9c656452009-09-10 20:51:44 +000071 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000072 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000073
Devang Patelb8ba13f2012-01-18 22:42:29 +000074 bool processInstruction(MCInst &Inst,
75 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
76
Chad Rosier84125ca2012-10-13 00:26:04 +000077 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000078 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000079 MCStreamer &Out, unsigned &ErrorInfo,
80 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000081
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000082 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000083 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000084 bool isSrcOp(X86Operand &Op);
85
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000086 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
87 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000088 bool isDstOp(X86Operand &Op);
89
Evan Cheng59ee62d2011-07-11 03:57:24 +000090 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000091 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000092 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000093 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000094 void SwitchMode() {
95 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
96 setAvailableFeatures(FB);
97 }
Evan Chengebdeeab2011-07-08 01:53:10 +000098
Daniel Dunbar54074b52010-07-19 05:44:09 +000099 /// @name Auto-generated Matcher Functions
100 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000101
Chris Lattner0692ee62010-09-06 19:11:01 +0000102#define GET_ASSEMBLER_HEADER
103#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000104
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000105 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000106
107public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000108 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000109 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000110
Daniel Dunbar54074b52010-07-19 05:44:09 +0000111 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000112 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000113 }
Roman Divackybf755322011-01-27 17:14:22 +0000114 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000115
Chad Rosier6a020a72012-10-25 20:41:34 +0000116 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
117 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000118 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000119
120 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000121
122 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000123 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000124 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000125};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000126} // end anonymous namespace
127
Sean Callanane9b466d2010-01-23 00:40:33 +0000128/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000129/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000130
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000131static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000132
133/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000134
Craig Topper76bd9382012-07-18 04:59:16 +0000135static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000136 return (( Value <= 0x000000000000007FULL)||
137 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
138 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
139}
140
141static bool isImmSExti32i8Value(uint64_t Value) {
142 return (( Value <= 0x000000000000007FULL)||
143 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
144 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
145}
146
147static bool isImmZExtu32u8Value(uint64_t Value) {
148 return (Value <= 0x00000000000000FFULL);
149}
150
151static bool isImmSExti64i8Value(uint64_t Value) {
152 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000153 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000154}
155
156static bool isImmSExti64i32Value(uint64_t Value) {
157 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000158 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000159}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000160namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161
162/// X86Operand - Instances of this class represent a parsed X86 machine
163/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000164struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000165 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000166 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 Register,
168 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000169 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000170 } Kind;
171
Chris Lattner29ef9a22010-01-15 18:51:29 +0000172 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000173 SMLoc OffsetOfLoc;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000174 bool AddressOf;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000175
Eric Christophera286fc02013-03-15 00:42:55 +0000176 struct TokOp {
177 const char *Data;
178 unsigned Length;
179 };
180
181 struct RegOp {
182 unsigned RegNo;
183 };
184
185 struct ImmOp {
186 const MCExpr *Val;
Eric Christophera286fc02013-03-15 00:42:55 +0000187 };
188
189 struct MemOp {
190 unsigned SegReg;
191 const MCExpr *Disp;
192 unsigned BaseReg;
193 unsigned IndexReg;
194 unsigned Scale;
195 unsigned Size;
Eric Christophera286fc02013-03-15 00:42:55 +0000196 };
197
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000198 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000199 struct TokOp Tok;
200 struct RegOp Reg;
201 struct ImmOp Imm;
202 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000203 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000204
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000205 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000206 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000207
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000208 /// getStartLoc - Get the location of the first token of this operand.
209 SMLoc getStartLoc() const { return StartLoc; }
210 /// getEndLoc - Get the location of the last token of this operand.
211 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000212 /// getLocRange - Get the range between the first and last token of this
213 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000214 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000215 /// getOffsetOfLoc - Get the location of the offset operator.
216 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000217
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000218 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000219
Daniel Dunbar20927f22009-08-07 08:26:05 +0000220 StringRef getToken() const {
221 assert(Kind == Token && "Invalid access!");
222 return StringRef(Tok.Data, Tok.Length);
223 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000224 void setTokenValue(StringRef Value) {
225 assert(Kind == Token && "Invalid access!");
226 Tok.Data = Value.data();
227 Tok.Length = Value.size();
228 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000229
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000230 unsigned getReg() const {
231 assert(Kind == Register && "Invalid access!");
232 return Reg.RegNo;
233 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000234
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000235 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000236 assert(Kind == Immediate && "Invalid access!");
237 return Imm.Val;
238 }
239
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000240 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000241 assert(Kind == Memory && "Invalid access!");
242 return Mem.Disp;
243 }
244 unsigned getMemSegReg() const {
245 assert(Kind == Memory && "Invalid access!");
246 return Mem.SegReg;
247 }
248 unsigned getMemBaseReg() const {
249 assert(Kind == Memory && "Invalid access!");
250 return Mem.BaseReg;
251 }
252 unsigned getMemIndexReg() const {
253 assert(Kind == Memory && "Invalid access!");
254 return Mem.IndexReg;
255 }
256 unsigned getMemScale() const {
257 assert(Kind == Memory && "Invalid access!");
258 return Mem.Scale;
259 }
260
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000261 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000262
263 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000264
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000266 if (!isImm())
267 return false;
268
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000269 // If this isn't a constant expr, just assume it fits and let relaxation
270 // handle it.
271 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
272 if (!CE)
273 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000274
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000275 // Otherwise, check the value is in a range that makes sense for this
276 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000277 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000278 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000279 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000280 if (!isImm())
281 return false;
282
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 // If this isn't a constant expr, just assume it fits and let relaxation
284 // handle it.
285 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
286 if (!CE)
287 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000288
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000289 // Otherwise, check the value is in a range that makes sense for this
290 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000291 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000292 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000293 bool isImmZExtu32u8() const {
294 if (!isImm())
295 return false;
296
297 // If this isn't a constant expr, just assume it fits and let relaxation
298 // handle it.
299 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
300 if (!CE)
301 return true;
302
303 // Otherwise, check the value is in a range that makes sense for this
304 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000305 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000306 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000307 bool isImmSExti64i8() const {
308 if (!isImm())
309 return false;
310
311 // If this isn't a constant expr, just assume it fits and let relaxation
312 // handle it.
313 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
314 if (!CE)
315 return true;
316
317 // Otherwise, check the value is in a range that makes sense for this
318 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000319 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000320 }
321 bool isImmSExti64i32() const {
322 if (!isImm())
323 return false;
324
325 // If this isn't a constant expr, just assume it fits and let relaxation
326 // handle it.
327 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
328 if (!CE)
329 return true;
330
331 // Otherwise, check the value is in a range that makes sense for this
332 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000333 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000334 }
335
Chad Rosiera703fb92012-10-22 19:50:35 +0000336 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000337 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000338 }
339
Chad Rosierc1ec2072013-01-10 22:10:27 +0000340 bool needAddressOf() const {
341 return AddressOf;
342 }
343
Daniel Dunbar20927f22009-08-07 08:26:05 +0000344 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000346 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000363 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000364 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000365 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000366
Craig Topper75dc33a2012-07-18 04:11:12 +0000367 bool isMemVX32() const {
368 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
369 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
370 }
371 bool isMemVY32() const {
372 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
373 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
374 }
375 bool isMemVX64() const {
376 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
377 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
378 }
379 bool isMemVY64() const {
380 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
381 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
382 }
383
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000384 bool isAbsMem() const {
385 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000386 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000387 }
388
Daniel Dunbar20927f22009-08-07 08:26:05 +0000389 bool isReg() const { return Kind == Register; }
390
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000391 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
392 // Add as immediates when possible.
393 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
394 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
395 else
396 Inst.addOperand(MCOperand::CreateExpr(Expr));
397 }
398
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000399 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000400 assert(N == 1 && "Invalid number of operands!");
401 Inst.addOperand(MCOperand::CreateReg(getReg()));
402 }
403
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000404 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000405 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000406 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000407 }
408
Chad Rosier36b8fed2012-06-27 22:34:28 +0000409 void addMem8Operands(MCInst &Inst, unsigned N) const {
410 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000411 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000412 void addMem16Operands(MCInst &Inst, unsigned N) const {
413 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000414 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000415 void addMem32Operands(MCInst &Inst, unsigned N) const {
416 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000417 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000418 void addMem64Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000421 void addMem80Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000424 void addMem128Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000427 void addMem256Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000430 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
432 }
433 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
435 }
436 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
438 }
439 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
440 addMemOperands(Inst, N);
441 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000442
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000443 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000444 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000445 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
446 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
447 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000448 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000449 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
450 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000451
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000452 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
453 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000454 // Add as immediates when possible.
455 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
456 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
457 else
458 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000459 }
460
Chris Lattnerb4307b32010-01-15 19:28:38 +0000461 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000462 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000463 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000464 Res->Tok.Data = Str.data();
465 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000466 return Res;
467 }
468
Chad Rosierc0a14b82012-10-24 17:22:29 +0000469 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000470 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000471 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000472 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000473 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000474 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000475 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000477 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000478
Chad Rosier811ddf62013-03-19 21:58:18 +0000479 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000480 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000481 Res->Imm.Val = Val;
482 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000483 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000484
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000485 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000486 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000487 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000488 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
489 Res->Mem.SegReg = 0;
490 Res->Mem.Disp = Disp;
491 Res->Mem.BaseReg = 0;
492 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000493 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000494 Res->Mem.Size = Size;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000495 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000496 return Res;
497 }
498
499 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000500 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
501 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000502 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000503 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000504 // We should never just have a displacement, that should be parsed as an
505 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000506 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
507
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000508 // The scale should always be one of {1,2,4,8}.
509 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000510 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000511 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000512 Res->Mem.SegReg = SegReg;
513 Res->Mem.Disp = Disp;
514 Res->Mem.BaseReg = BaseReg;
515 Res->Mem.IndexReg = IndexReg;
516 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000517 Res->Mem.Size = Size;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000518 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000519 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000520 }
521};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000522
Chris Lattner37dfdec2009-07-29 06:33:53 +0000523} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000524
Devang Pateldd929fc2012-01-12 18:03:40 +0000525bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000526 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000527
528 return (Op.isMem() &&
529 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
530 isa<MCConstantExpr>(Op.Mem.Disp) &&
531 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
532 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
533}
534
Devang Pateldd929fc2012-01-12 18:03:40 +0000535bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000536 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000537
Chad Rosier36b8fed2012-06-27 22:34:28 +0000538 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000539 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000540 isa<MCConstantExpr>(Op.Mem.Disp) &&
541 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
542 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
543}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000544
Devang Pateldd929fc2012-01-12 18:03:40 +0000545bool X86AsmParser::ParseRegister(unsigned &RegNo,
546 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000547 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000548 const AsmToken &PercentTok = Parser.getTok();
549 StartLoc = PercentTok.getLoc();
550
551 // If we encounter a %, ignore it. This code handles registers with and
552 // without the prefix, unprefixed registers can occur in cfi directives.
553 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000554 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000555
Sean Callanan18b83232010-01-19 21:44:56 +0000556 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000557 EndLoc = Tok.getEndLoc();
558
Devang Patel1aea4302012-01-20 22:32:05 +0000559 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000560 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000561 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000562 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000563 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000564
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000565 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000566
Chris Lattner33d60d52010-09-22 04:11:10 +0000567 // If the match failed, try the register name as lowercase.
568 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000569 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000570
Evan Cheng5de728c2011-07-27 23:22:03 +0000571 if (!is64BitMode()) {
572 // FIXME: This should be done using Requires<In32BitMode> and
573 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
574 // checked.
575 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
576 // REX prefix.
577 if (RegNo == X86::RIZ ||
578 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
579 X86II::isX86_64NonExtLowByteReg(RegNo) ||
580 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000581 return Error(StartLoc, "register %"
582 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000583 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000584 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000585
Chris Lattner33d60d52010-09-22 04:11:10 +0000586 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
587 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000588 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000589 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000590
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000591 // Check to see if we have '(4)' after %st.
592 if (getLexer().isNot(AsmToken::LParen))
593 return false;
594 // Lex the paren.
595 getParser().Lex();
596
597 const AsmToken &IntTok = Parser.getTok();
598 if (IntTok.isNot(AsmToken::Integer))
599 return Error(IntTok.getLoc(), "expected stack index");
600 switch (IntTok.getIntVal()) {
601 case 0: RegNo = X86::ST0; break;
602 case 1: RegNo = X86::ST1; break;
603 case 2: RegNo = X86::ST2; break;
604 case 3: RegNo = X86::ST3; break;
605 case 4: RegNo = X86::ST4; break;
606 case 5: RegNo = X86::ST5; break;
607 case 6: RegNo = X86::ST6; break;
608 case 7: RegNo = X86::ST7; break;
609 default: return Error(IntTok.getLoc(), "invalid stack index");
610 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000611
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000612 if (getParser().Lex().isNot(AsmToken::RParen))
613 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000614
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000615 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000616 Parser.Lex(); // Eat ')'
617 return false;
618 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000619
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000620 EndLoc = Parser.getTok().getEndLoc();
621
Chris Lattner645b2092010-06-24 07:29:18 +0000622 // If this is "db[0-7]", match it as an alias
623 // for dr[0-7].
624 if (RegNo == 0 && Tok.getString().size() == 3 &&
625 Tok.getString().startswith("db")) {
626 switch (Tok.getString()[2]) {
627 case '0': RegNo = X86::DR0; break;
628 case '1': RegNo = X86::DR1; break;
629 case '2': RegNo = X86::DR2; break;
630 case '3': RegNo = X86::DR3; break;
631 case '4': RegNo = X86::DR4; break;
632 case '5': RegNo = X86::DR5; break;
633 case '6': RegNo = X86::DR6; break;
634 case '7': RegNo = X86::DR7; break;
635 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000636
Chris Lattner645b2092010-06-24 07:29:18 +0000637 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000638 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000639 Parser.Lex(); // Eat it.
640 return false;
641 }
642 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000643
Devang Patel1aea4302012-01-20 22:32:05 +0000644 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000645 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000646 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000647 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000648 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000649
Sean Callananb9a25b72010-01-19 20:27:46 +0000650 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000651 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000652}
653
Devang Pateldd929fc2012-01-12 18:03:40 +0000654X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000655 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000656 return ParseIntelOperand();
657 return ParseATTOperand();
658}
659
Devang Pateld37ad242012-01-17 18:00:18 +0000660/// getIntelMemOperandSize - Return intel memory operand size.
661static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000662 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000663 .Cases("BYTE", "byte", 8)
664 .Cases("WORD", "word", 16)
665 .Cases("DWORD", "dword", 32)
666 .Cases("QWORD", "qword", 64)
667 .Cases("XWORD", "xword", 80)
668 .Cases("XMMWORD", "xmmword", 128)
669 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000670 .Default(0);
671 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000672}
673
Chad Rosierdd2e8952013-01-14 22:31:35 +0000674enum IntelBracExprState {
675 IBES_START,
676 IBES_LBRAC,
677 IBES_RBRAC,
678 IBES_REGISTER,
679 IBES_REGISTER_STAR,
680 IBES_REGISTER_STAR_INTEGER,
681 IBES_INTEGER,
682 IBES_INTEGER_STAR,
683 IBES_INDEX_REGISTER,
684 IBES_IDENTIFIER,
685 IBES_DISP_EXPR,
686 IBES_MINUS,
687 IBES_ERROR
688};
689
690class IntelBracExprStateMachine {
691 IntelBracExprState State;
692 unsigned BaseReg, IndexReg, Scale;
693 int64_t Disp;
694
695 unsigned TmpReg;
696 int64_t TmpInteger;
697
698 bool isPlus;
699
700public:
701 IntelBracExprStateMachine(MCAsmParser &parser) :
702 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(0),
703 TmpReg(0), TmpInteger(0), isPlus(true) {}
704
705 unsigned getBaseReg() { return BaseReg; }
706 unsigned getIndexReg() { return IndexReg; }
707 unsigned getScale() { return Scale; }
708 int64_t getDisp() { return Disp; }
709 bool isValidEndState() { return State == IBES_RBRAC; }
710
711 void onPlus() {
712 switch (State) {
713 default:
714 State = IBES_ERROR;
715 break;
716 case IBES_INTEGER:
717 State = IBES_START;
718 if (isPlus)
719 Disp += TmpInteger;
720 else
721 Disp -= TmpInteger;
722 break;
723 case IBES_REGISTER:
724 State = IBES_START;
725 // If we already have a BaseReg, then assume this is the IndexReg with a
726 // scale of 1.
727 if (!BaseReg) {
728 BaseReg = TmpReg;
729 } else {
730 assert (!IndexReg && "BaseReg/IndexReg already set!");
731 IndexReg = TmpReg;
732 Scale = 1;
733 }
734 break;
735 case IBES_INDEX_REGISTER:
736 State = IBES_START;
737 break;
738 }
739 isPlus = true;
740 }
741 void onMinus() {
742 switch (State) {
743 default:
744 State = IBES_ERROR;
745 break;
746 case IBES_START:
747 State = IBES_MINUS;
748 break;
749 case IBES_INTEGER:
750 State = IBES_START;
751 if (isPlus)
752 Disp += TmpInteger;
753 else
754 Disp -= TmpInteger;
755 break;
756 case IBES_REGISTER:
757 State = IBES_START;
758 // If we already have a BaseReg, then assume this is the IndexReg with a
759 // scale of 1.
760 if (!BaseReg) {
761 BaseReg = TmpReg;
762 } else {
763 assert (!IndexReg && "BaseReg/IndexReg already set!");
764 IndexReg = TmpReg;
765 Scale = 1;
766 }
767 break;
768 case IBES_INDEX_REGISTER:
769 State = IBES_START;
770 break;
771 }
772 isPlus = false;
773 }
774 void onRegister(unsigned Reg) {
775 switch (State) {
776 default:
777 State = IBES_ERROR;
778 break;
779 case IBES_START:
780 State = IBES_REGISTER;
781 TmpReg = Reg;
782 break;
783 case IBES_INTEGER_STAR:
784 assert (!IndexReg && "IndexReg already set!");
785 State = IBES_INDEX_REGISTER;
786 IndexReg = Reg;
787 Scale = TmpInteger;
788 break;
789 }
790 }
791 void onDispExpr() {
792 switch (State) {
793 default:
794 State = IBES_ERROR;
795 break;
796 case IBES_START:
797 State = IBES_DISP_EXPR;
798 break;
799 }
800 }
801 void onInteger(int64_t TmpInt) {
802 switch (State) {
803 default:
804 State = IBES_ERROR;
805 break;
806 case IBES_START:
807 State = IBES_INTEGER;
808 TmpInteger = TmpInt;
809 break;
810 case IBES_MINUS:
811 State = IBES_INTEGER;
812 TmpInteger = TmpInt;
813 break;
814 case IBES_REGISTER_STAR:
815 assert (!IndexReg && "IndexReg already set!");
816 State = IBES_INDEX_REGISTER;
817 IndexReg = TmpReg;
818 Scale = TmpInt;
819 break;
820 }
821 }
822 void onStar() {
823 switch (State) {
824 default:
825 State = IBES_ERROR;
826 break;
827 case IBES_INTEGER:
828 State = IBES_INTEGER_STAR;
829 break;
830 case IBES_REGISTER:
831 State = IBES_REGISTER_STAR;
832 break;
833 }
834 }
835 void onLBrac() {
836 switch (State) {
837 default:
838 State = IBES_ERROR;
839 break;
840 case IBES_RBRAC:
841 State = IBES_START;
842 isPlus = true;
843 break;
844 }
845 }
846 void onRBrac() {
847 switch (State) {
848 default:
849 State = IBES_ERROR;
850 break;
851 case IBES_DISP_EXPR:
852 State = IBES_RBRAC;
853 break;
854 case IBES_INTEGER:
855 State = IBES_RBRAC;
856 if (isPlus)
857 Disp += TmpInteger;
858 else
859 Disp -= TmpInteger;
860 break;
861 case IBES_REGISTER:
862 State = IBES_RBRAC;
863 // If we already have a BaseReg, then assume this is the IndexReg with a
864 // scale of 1.
865 if (!BaseReg) {
866 BaseReg = TmpReg;
867 } else {
868 assert (!IndexReg && "BaseReg/IndexReg already set!");
869 IndexReg = TmpReg;
870 Scale = 1;
871 }
872 break;
873 case IBES_INDEX_REGISTER:
874 State = IBES_RBRAC;
875 break;
876 }
877 }
878};
879
Chad Rosierd3e74162013-03-19 21:11:56 +0000880X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
881 SMLoc End, SMLoc SizeDirLoc,
882 unsigned Size) {
883 bool NeedSizeDir = false;
884 bool IsVarDecl = false;
885 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
886 const MCSymbol &Sym = SymRef->getSymbol();
887 // FIXME: The SemaLookup will fail if the name is anything other then an
888 // identifier.
889 // FIXME: Pass a valid SMLoc.
890 unsigned tLength, tSize, tType;
891 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
892 tSize, tType, IsVarDecl);
893 if (!Size) {
894 Size = tType * 8; // Size is in terms of bits in this context.
895 NeedSizeDir = Size > 0;
896 }
897 }
898
899 // If this is not a VarDecl then assume it is a FuncDecl or some other label
900 // reference. We need an 'r' constraint here, so we need to create register
901 // operand to ensure proper matching. Just pick a GPR based on the size of
902 // a pointer.
903 if (!IsVarDecl) {
904 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
905 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
906 }
907
908 if (NeedSizeDir)
909 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
910 /*Len*/0, Size));
911
912 // When parsing inline assembly we set the base register to a non-zero value
913 // as we don't know the actual value at this time. This is necessary to
914 // get the matching correct in some cases.
915 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
916 /*Scale*/1, Start, End, Size);
917}
918
Chad Rosier65c88922012-10-22 19:42:52 +0000919X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000920 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000921 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000922 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000923
Devang Pateld37ad242012-01-17 18:00:18 +0000924 // Eat '['
925 if (getLexer().isNot(AsmToken::LBrac))
926 return ErrorOperand(Start, "Expected '[' token!");
927 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000928
Chad Rosierdd2e8952013-01-14 22:31:35 +0000929 unsigned TmpReg = 0;
930
931 // Try to handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000932 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000933 if (ParseRegister(TmpReg, Start, End)) {
934 const MCExpr *Disp;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000935 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000936 return 0;
937
Devang Pateld37ad242012-01-17 18:00:18 +0000938 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000939 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosier4fb25b72013-02-15 21:58:13 +0000940 // Adjust the EndLoc due to the ']'.
941 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000942 Parser.Lex();
Chad Rosierd3e74162013-03-19 21:11:56 +0000943 if (!isParsingInlineAsm())
944 return X86Operand::CreateMem(Disp, Start, End, Size);
945
946 // We want the size directive before the '['.
947 SMLoc SizeDirLoc = SMLoc::getFromPointer(Start.getPointer()-1);
948 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000949 }
Devang Pateld37ad242012-01-17 18:00:18 +0000950 }
951
Chad Rosierdd2e8952013-01-14 22:31:35 +0000952 // Parse [ BaseReg + Scale*IndexReg + Disp ].
953 bool Done = false;
954 IntelBracExprStateMachine SM(Parser);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000955
Chad Rosierdd2e8952013-01-14 22:31:35 +0000956 // If we parsed a register, then the end loc has already been set and
957 // the identifier has already been lexed. We also need to update the
958 // state.
959 if (TmpReg)
960 SM.onRegister(TmpReg);
961
962 const MCExpr *Disp = 0;
963 while (!Done) {
964 bool UpdateLocLex = true;
965
966 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
967 // identifier. Don't try an parse it as a register.
968 if (Tok.getString().startswith("."))
969 break;
970
971 switch (getLexer().getKind()) {
972 default: {
973 if (SM.isValidEndState()) {
974 Done = true;
975 break;
976 }
977 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
978 }
979 case AsmToken::Identifier: {
980 // This could be a register or a displacement expression.
981 if(!ParseRegister(TmpReg, Start, End)) {
982 SM.onRegister(TmpReg);
983 UpdateLocLex = false;
984 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000985 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000986 SM.onDispExpr();
987 UpdateLocLex = false;
988 break;
989 }
990 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
991 }
992 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +0000993 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000994 SM.onInteger(Val);
995 break;
996 }
997 case AsmToken::Plus: SM.onPlus(); break;
998 case AsmToken::Minus: SM.onMinus(); break;
999 case AsmToken::Star: SM.onStar(); break;
1000 case AsmToken::LBrac: SM.onLBrac(); break;
1001 case AsmToken::RBrac: SM.onRBrac(); break;
1002 }
1003 if (!Done && UpdateLocLex) {
1004 End = Tok.getLoc();
1005 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +00001006 }
Devang Pateld37ad242012-01-17 18:00:18 +00001007 }
1008
Chad Rosierdd2e8952013-01-14 22:31:35 +00001009 if (!Disp)
1010 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +00001011
Chad Rosierddb53ef2012-10-26 22:01:25 +00001012 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001013 if (Tok.getString().startswith(".")) {
1014 SmallString<64> Err;
1015 const MCExpr *NewDisp;
1016 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1017 return ErrorOperand(Tok.getLoc(), Err);
1018
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001019 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001020 Parser.Lex(); // Eat the field.
1021 Disp = NewDisp;
1022 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001023
Chad Rosierdd2e8952013-01-14 22:31:35 +00001024 int BaseReg = SM.getBaseReg();
1025 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001026
Chad Rosierdd2e8952013-01-14 22:31:35 +00001027 // handle [-42]
1028 if (!BaseReg && !IndexReg) {
1029 if (!SegReg)
1030 return X86Operand::CreateMem(Disp, Start, End);
1031 else
1032 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1033 }
1034
1035 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001036 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001037 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001038}
1039
1040/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001041X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001042 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001043 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001044
1045 unsigned Size = getIntelMemOperandSize(Tok.getString());
1046 if (Size) {
1047 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001048 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1049 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001050 Parser.Lex();
1051 }
1052
Chad Rosierc0a14b82012-10-24 17:22:29 +00001053 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +00001054 return ParseIntelBracExpression(SegReg, Size);
1055
1056 if (!ParseRegister(SegReg, Start, End)) {
1057 // Handel SegReg : [ ... ]
1058 if (getLexer().isNot(AsmToken::Colon))
1059 return ErrorOperand(Start, "Expected ':' token!");
1060 Parser.Lex(); // Eat :
1061 if (getLexer().isNot(AsmToken::LBrac))
1062 return ErrorOperand(Start, "Expected '[' token!");
1063 return ParseIntelBracExpression(SegReg, Size);
1064 }
Devang Pateld37ad242012-01-17 18:00:18 +00001065
1066 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001067 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001068 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001069
Chad Rosier2a784132012-10-23 23:31:33 +00001070 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001071 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierd3e74162013-03-19 21:11:56 +00001072 return CreateMemForInlineAsm(Disp, Start, End, Start, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001073}
1074
Chad Rosier22f441a2012-10-24 22:21:50 +00001075/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001076bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1077 const MCExpr **NewDisp,
1078 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001079 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001080 uint64_t OrigDispVal, DotDispVal;
1081
1082 // FIXME: Handle non-constant expressions.
1083 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1084 OrigDispVal = OrigDisp->getValue();
1085 } else {
1086 Err = "Non-constant offsets are not supported!";
1087 return true;
1088 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001089
1090 // Drop the '.'.
1091 StringRef DotDispStr = Tok.getString().drop_front(1);
1092
Chad Rosier22f441a2012-10-24 22:21:50 +00001093 // .Imm gets lexed as a real.
1094 if (Tok.is(AsmToken::Real)) {
1095 APInt DotDisp;
1096 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001097 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001098 } else if (Tok.is(AsmToken::Identifier)) {
1099 // We should only see an identifier when parsing the original inline asm.
1100 // The front-end should rewrite this in terms of immediates.
1101 assert (isParsingInlineAsm() && "Unexpected field name!");
1102
1103 unsigned DotDisp;
1104 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1105 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1106 DotDisp)) {
1107 Err = "Unable to lookup field reference!";
1108 return true;
1109 }
1110 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001111 } else {
1112 Err = "Unexpected token type!";
1113 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001114 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001115
Chad Rosierec130222012-10-25 21:51:10 +00001116 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1117 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1118 unsigned Len = DotDispStr.size();
1119 unsigned Val = OrigDispVal + DotDispVal;
1120 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1121 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001122 }
1123
1124 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1125 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001126}
1127
Chad Rosierc0a14b82012-10-24 17:22:29 +00001128/// Parse the 'offset' operator. This operator is used to specify the
1129/// location rather then the content of a variable.
1130X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1131 SMLoc OffsetOfLoc = Start;
1132 Parser.Lex(); // Eat offset.
1133 Start = Parser.getTok().getLoc();
1134 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1135
Chad Rosier6e431572012-10-26 16:09:20 +00001136 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001137 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001138 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001139 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001140
Chad Rosier6e431572012-10-26 16:09:20 +00001141 // Don't emit the offset operator.
1142 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1143
Chad Rosierc0a14b82012-10-24 17:22:29 +00001144 // The offset operator will have an 'r' constraint, thus we need to create
1145 // register operand to ensure proper matching. Just pick a GPR based on
1146 // the size of a pointer.
1147 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001148 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1149 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001150}
1151
Chad Rosier505bca32013-01-17 19:21:48 +00001152enum IntelOperatorKind {
1153 IOK_LENGTH,
1154 IOK_SIZE,
1155 IOK_TYPE
1156};
1157
1158/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1159/// returns the number of elements in an array. It returns the value 1 for
1160/// non-array variables. The SIZE operator returns the size of a C or C++
1161/// variable. A variable's size is the product of its LENGTH and TYPE. The
1162/// TYPE operator returns the size of a C or C++ type or variable. If the
1163/// variable is an array, TYPE returns the size of a single element.
1164X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001165 SMLoc TypeLoc = Start;
1166 Parser.Lex(); // Eat offset.
1167 Start = Parser.getTok().getLoc();
1168 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1169
1170 SMLoc End;
1171 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001172 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001173 return 0;
1174
Chad Rosier505bca32013-01-17 19:21:48 +00001175 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001176 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1177 const MCSymbol &Sym = SymRef->getSymbol();
1178 // FIXME: The SemaLookup will fail if the name is anything other then an
1179 // identifier.
1180 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001181 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001182 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1183 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001184 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001185 }
1186 unsigned CVal;
1187 switch(OpKind) {
1188 default: llvm_unreachable("Unexpected operand kind!");
1189 case IOK_LENGTH: CVal = Length; break;
1190 case IOK_SIZE: CVal = Size; break;
1191 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001192 }
1193
1194 // Rewrite the type operator and the C or C++ type or variable in terms of an
1195 // immediate. E.g. TYPE foo -> $$4
1196 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001197 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001198
Chad Rosier505bca32013-01-17 19:21:48 +00001199 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosier811ddf62013-03-19 21:58:18 +00001200 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosierefcb3d92012-10-26 18:04:20 +00001201}
1202
Devang Pateld37ad242012-01-17 18:00:18 +00001203X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001204 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001205 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001206
Chad Rosier505bca32013-01-17 19:21:48 +00001207 // Offset, length, type and size operators.
1208 if (isParsingInlineAsm()) {
1209 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1210 return ParseIntelOffsetOfOperator(Start);
1211 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1212 return ParseIntelOperator(Start, IOK_LENGTH);
1213 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1214 return ParseIntelOperator(Start, IOK_SIZE);
1215 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1216 return ParseIntelOperator(Start, IOK_TYPE);
1217 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001218
Chad Rosier505bca32013-01-17 19:21:48 +00001219 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001220 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1221 getLexer().is(AsmToken::Minus)) {
1222 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001223 if (!getParser().parseExpression(Val, End)) {
Chad Rosier811ddf62013-03-19 21:58:18 +00001224 if (isParsingInlineAsm())
1225 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Devang Pateld37ad242012-01-17 18:00:18 +00001226 return X86Operand::CreateImm(Val, Start, End);
1227 }
1228 }
1229
Chad Rosier505bca32013-01-17 19:21:48 +00001230 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001231 unsigned RegNo = 0;
1232 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001233 // If this is a segment register followed by a ':', then this is the start
1234 // of a memory reference, otherwise this is a normal register reference.
1235 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001236 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001237
1238 getParser().Lex(); // Eat the colon.
1239 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001240 }
1241
Chad Rosier505bca32013-01-17 19:21:48 +00001242 // Memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001243 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001244}
1245
Devang Pateldd929fc2012-01-12 18:03:40 +00001246X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001247 switch (getLexer().getKind()) {
1248 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001249 // Parse a memory operand with no segment register.
1250 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001251 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001252 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001253 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001254 SMLoc Start, End;
1255 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001256 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001257 Error(Start, "%eiz and %riz can only be used as index registers",
1258 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001259 return 0;
1260 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001261
Chris Lattnereef6d782010-04-17 18:56:34 +00001262 // If this is a segment register followed by a ':', then this is the start
1263 // of a memory reference, otherwise this is a normal register reference.
1264 if (getLexer().isNot(AsmToken::Colon))
1265 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001266
1267
Chris Lattnereef6d782010-04-17 18:56:34 +00001268 getParser().Lex(); // Eat the colon.
1269 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001270 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001271 case AsmToken::Dollar: {
1272 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001273 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001274 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001275 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001276 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001277 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001278 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001279 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001280 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001281}
1282
Chris Lattnereef6d782010-04-17 18:56:34 +00001283/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1284/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001285X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001286
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001287 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1288 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001289 // only way to do this without lookahead is to eat the '(' and see what is
1290 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001291 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001292 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001293 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001294 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001295
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001296 // After parsing the base expression we could either have a parenthesized
1297 // memory address or not. If not, return now. If so, eat the (.
1298 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001299 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001300 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001301 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001302 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001303 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001304
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001305 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001306 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001307 } else {
1308 // Okay, we have a '('. We don't know if this is an expression or not, but
1309 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001310 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001311 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001312
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001313 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001314 // Nothing to do here, fall into the code below with the '(' part of the
1315 // memory operand consumed.
1316 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001317 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001318
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001319 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001320 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001321 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001322
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001323 // After parsing the base expression we could either have a parenthesized
1324 // memory address or not. If not, return now. If so, eat the (.
1325 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001326 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001327 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001328 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001329 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001330 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001331
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001332 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001333 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001334 }
1335 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001336
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001337 // If we reached here, then we just ate the ( of the memory operand. Process
1338 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001339 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001340 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001341
Chris Lattner29ef9a22010-01-15 18:51:29 +00001342 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001343 SMLoc StartLoc, EndLoc;
1344 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001345 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001346 Error(StartLoc, "eiz and riz can only be used as index registers",
1347 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001348 return 0;
1349 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001350 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001351
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001352 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001353 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001354 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001355
1356 // Following the comma we should have either an index register, or a scale
1357 // value. We don't support the later form, but we want to parse it
1358 // correctly.
1359 //
1360 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001361 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001362 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001363 SMLoc L;
1364 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001365
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001366 if (getLexer().isNot(AsmToken::RParen)) {
1367 // Parse the scale amount:
1368 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001369 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001370 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001371 "expected comma in scale expression");
1372 return 0;
1373 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001374 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001375
1376 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001377 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001378
1379 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001380 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001381 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001382 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001383 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001384
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001385 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001386 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1387 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1388 return 0;
1389 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001390 Scale = (unsigned)ScaleVal;
1391 }
1392 }
1393 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001394 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001395 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001396 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001397
1398 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001399 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001400 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001401
Daniel Dunbaree910252010-08-24 19:13:38 +00001402 if (Value != 1)
1403 Warning(Loc, "scale factor without index register is ignored");
1404 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001405 }
1406 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001407
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001408 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001409 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001410 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001411 return 0;
1412 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001413 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001414 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001415
Kevin Enderby84faf652012-03-12 21:32:09 +00001416 // If we have both a base register and an index register make sure they are
1417 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001418 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001419 if (BaseReg != 0 && IndexReg != 0) {
1420 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001421 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1422 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001423 IndexReg != X86::RIZ) {
1424 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1425 return 0;
1426 }
1427 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001428 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1429 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001430 IndexReg != X86::EIZ){
1431 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1432 return 0;
1433 }
1434 }
1435
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001436 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1437 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001438}
1439
Devang Pateldd929fc2012-01-12 18:03:40 +00001440bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001441ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001442 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001443 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001444 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001445
Chris Lattnerd8f71792010-11-28 20:23:50 +00001446 // FIXME: Hack to recognize setneb as setne.
1447 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1448 PatchedName != "setb" && PatchedName != "setnb")
1449 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001450
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001451 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1452 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001453 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001454 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1455 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001456 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001457 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001458 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001459 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001460 .Case("eq", 0x00)
1461 .Case("lt", 0x01)
1462 .Case("le", 0x02)
1463 .Case("unord", 0x03)
1464 .Case("neq", 0x04)
1465 .Case("nlt", 0x05)
1466 .Case("nle", 0x06)
1467 .Case("ord", 0x07)
1468 /* AVX only from here */
1469 .Case("eq_uq", 0x08)
1470 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001471 .Case("ngt", 0x0A)
1472 .Case("false", 0x0B)
1473 .Case("neq_oq", 0x0C)
1474 .Case("ge", 0x0D)
1475 .Case("gt", 0x0E)
1476 .Case("true", 0x0F)
1477 .Case("eq_os", 0x10)
1478 .Case("lt_oq", 0x11)
1479 .Case("le_oq", 0x12)
1480 .Case("unord_s", 0x13)
1481 .Case("neq_us", 0x14)
1482 .Case("nlt_uq", 0x15)
1483 .Case("nle_uq", 0x16)
1484 .Case("ord_s", 0x17)
1485 .Case("eq_us", 0x18)
1486 .Case("nge_uq", 0x19)
1487 .Case("ngt_uq", 0x1A)
1488 .Case("false_os", 0x1B)
1489 .Case("neq_os", 0x1C)
1490 .Case("ge_oq", 0x1D)
1491 .Case("gt_oq", 0x1E)
1492 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001493 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001494 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001495 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1496 getParser().getContext());
1497 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001498 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001499 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001500 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001501 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001502 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001503 } else {
1504 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001505 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001506 }
1507 }
1508 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001509
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001510 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001511
Devang Patel885f65b2012-01-30 22:47:12 +00001512 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001513 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001514
Chris Lattner2544f422010-09-08 05:17:37 +00001515 // Determine whether this is an instruction prefix.
1516 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001517 Name == "lock" || Name == "rep" ||
1518 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001519 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001520 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001521
1522
Chris Lattner2544f422010-09-08 05:17:37 +00001523 // This does the actual operand parsing. Don't parse any more if we have a
1524 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1525 // just want to parse the "lock" as the first instruction and the "incl" as
1526 // the next one.
1527 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001528
1529 // Parse '*' modifier.
1530 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001531 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001532 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001533 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001534 }
1535
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001536 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001537 if (X86Operand *Op = ParseOperand())
1538 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001539 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001540 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001541 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001542 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001543
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001544 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001545 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001546
1547 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001548 if (X86Operand *Op = ParseOperand())
1549 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001550 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001551 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001552 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001553 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001554 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001555
Chris Lattnercbf8a982010-09-11 16:18:25 +00001556 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001557 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001558 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001559 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001560 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001561 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001562
Chris Lattner2544f422010-09-08 05:17:37 +00001563 if (getLexer().is(AsmToken::EndOfStatement))
1564 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001565 else if (isPrefix && getLexer().is(AsmToken::Slash))
1566 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001567
Devang Patel885f65b2012-01-30 22:47:12 +00001568 if (ExtraImmOp && isParsingIntelSyntax())
1569 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1570
Chris Lattner98c870f2010-11-06 19:25:43 +00001571 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1572 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1573 // documented form in various unofficial manuals, so a lot of code uses it.
1574 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1575 Operands.size() == 3) {
1576 X86Operand &Op = *(X86Operand*)Operands.back();
1577 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1578 isa<MCConstantExpr>(Op.Mem.Disp) &&
1579 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1580 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1581 SMLoc Loc = Op.getEndLoc();
1582 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1583 delete &Op;
1584 }
1585 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001586 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1587 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1588 Operands.size() == 3) {
1589 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1590 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1591 isa<MCConstantExpr>(Op.Mem.Disp) &&
1592 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1593 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1594 SMLoc Loc = Op.getEndLoc();
1595 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1596 delete &Op;
1597 }
1598 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001599 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1600 if (Name.startswith("ins") && Operands.size() == 3 &&
1601 (Name == "insb" || Name == "insw" || Name == "insl")) {
1602 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1603 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1604 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1605 Operands.pop_back();
1606 Operands.pop_back();
1607 delete &Op;
1608 delete &Op2;
1609 }
1610 }
1611
1612 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1613 if (Name.startswith("outs") && Operands.size() == 3 &&
1614 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1615 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1616 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1617 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1618 Operands.pop_back();
1619 Operands.pop_back();
1620 delete &Op;
1621 delete &Op2;
1622 }
1623 }
1624
1625 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1626 if (Name.startswith("movs") && Operands.size() == 3 &&
1627 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001628 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001629 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1630 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1631 if (isSrcOp(Op) && isDstOp(Op2)) {
1632 Operands.pop_back();
1633 Operands.pop_back();
1634 delete &Op;
1635 delete &Op2;
1636 }
1637 }
1638 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1639 if (Name.startswith("lods") && Operands.size() == 3 &&
1640 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001641 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001642 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1643 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1644 if (isSrcOp(*Op1) && Op2->isReg()) {
1645 const char *ins;
1646 unsigned reg = Op2->getReg();
1647 bool isLods = Name == "lods";
1648 if (reg == X86::AL && (isLods || Name == "lodsb"))
1649 ins = "lodsb";
1650 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1651 ins = "lodsw";
1652 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1653 ins = "lodsl";
1654 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1655 ins = "lodsq";
1656 else
1657 ins = NULL;
1658 if (ins != NULL) {
1659 Operands.pop_back();
1660 Operands.pop_back();
1661 delete Op1;
1662 delete Op2;
1663 if (Name != ins)
1664 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1665 }
1666 }
1667 }
1668 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1669 if (Name.startswith("stos") && Operands.size() == 3 &&
1670 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001671 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001672 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1673 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1674 if (isDstOp(*Op2) && Op1->isReg()) {
1675 const char *ins;
1676 unsigned reg = Op1->getReg();
1677 bool isStos = Name == "stos";
1678 if (reg == X86::AL && (isStos || Name == "stosb"))
1679 ins = "stosb";
1680 else if (reg == X86::AX && (isStos || Name == "stosw"))
1681 ins = "stosw";
1682 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1683 ins = "stosl";
1684 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1685 ins = "stosq";
1686 else
1687 ins = NULL;
1688 if (ins != NULL) {
1689 Operands.pop_back();
1690 Operands.pop_back();
1691 delete Op1;
1692 delete Op2;
1693 if (Name != ins)
1694 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1695 }
1696 }
1697 }
1698
Chris Lattnere9e16a32010-09-15 04:33:27 +00001699 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001700 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001701 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001702 Name.startswith("shl") || Name.startswith("sal") ||
1703 Name.startswith("rcl") || Name.startswith("rcr") ||
1704 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001705 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001706 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001707 // Intel syntax
1708 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1709 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001710 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1711 delete Operands[2];
1712 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001713 }
1714 } else {
1715 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1716 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001717 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1718 delete Operands[1];
1719 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001720 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001721 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001722 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001723
Chris Lattner15f89512011-04-09 19:41:05 +00001724 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1725 // instalias with an immediate operand yet.
1726 if (Name == "int" && Operands.size() == 2) {
1727 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1728 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1729 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1730 delete Operands[1];
1731 Operands.erase(Operands.begin() + 1);
1732 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1733 }
1734 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001735
Chris Lattner98986712010-01-14 22:21:20 +00001736 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001737}
1738
Craig Topper4bef9612013-03-18 02:53:34 +00001739static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1740 bool isCmp) {
1741 MCInst TmpInst;
1742 TmpInst.setOpcode(Opcode);
1743 if (!isCmp)
1744 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1745 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1746 TmpInst.addOperand(Inst.getOperand(0));
1747 Inst = TmpInst;
1748 return true;
1749}
1750
1751static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1752 bool isCmp = false) {
1753 if (!Inst.getOperand(0).isImm() ||
1754 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1755 return false;
1756
1757 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1758}
1759
1760static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1761 bool isCmp = false) {
1762 if (!Inst.getOperand(0).isImm() ||
1763 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1764 return false;
1765
1766 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1767}
1768
1769static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1770 bool isCmp = false) {
1771 if (!Inst.getOperand(0).isImm() ||
1772 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1773 return false;
1774
1775 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1776}
1777
Devang Pateldd929fc2012-01-12 18:03:40 +00001778bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001779processInstruction(MCInst &Inst,
1780 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1781 switch (Inst.getOpcode()) {
1782 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001783 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1784 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1785 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1786 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1787 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1788 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1789 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1790 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1791 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1792 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1793 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1794 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1795 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1796 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1797 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1798 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1799 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1800 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001801 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1802 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1803 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1804 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1805 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1806 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001807 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001808}
1809
Jim Grosbach3ca63822012-11-14 18:04:47 +00001810static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001811bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001812MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001813 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001814 MCStreamer &Out, unsigned &ErrorInfo,
1815 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001816 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001817 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1818 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001819 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001820
Chris Lattner7c51a312010-09-29 01:50:45 +00001821 // First, handle aliases that expand to multiple instructions.
1822 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001823 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001824 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001825 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001826 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001827 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001828 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001829 MCInst Inst;
1830 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001831 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001832 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001833 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001834
Chris Lattner0bb83a82010-09-30 16:39:29 +00001835 const char *Repl =
1836 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001837 .Case("finit", "fninit")
1838 .Case("fsave", "fnsave")
1839 .Case("fstcw", "fnstcw")
1840 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001841 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001842 .Case("fstsw", "fnstsw")
1843 .Case("fstsww", "fnstsw")
1844 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001845 .Default(0);
1846 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001847 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001848 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001849 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001850
Chris Lattnera008e8a2010-09-06 21:54:15 +00001851 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001852 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001853
Daniel Dunbarc918d602010-05-04 16:12:42 +00001854 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001855 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001856 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001857 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001858 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001859 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001860 // Some instructions need post-processing to, for example, tweak which
1861 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001862 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001863 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001864 while (processInstruction(Inst, Operands))
1865 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001866
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001867 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001868 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001869 Out.EmitInstruction(Inst);
1870 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001871 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001872 case Match_MissingFeature: {
1873 assert(ErrorInfo && "Unknown missing feature!");
1874 // Special case the error message for the very common case where only
1875 // a single subtarget feature is missing.
1876 std::string Msg = "instruction requires:";
1877 unsigned Mask = 1;
1878 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1879 if (ErrorInfo & Mask) {
1880 Msg += " ";
1881 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1882 }
1883 Mask <<= 1;
1884 }
1885 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1886 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001887 case Match_InvalidOperand:
1888 WasOriginallyInvalidOperand = true;
1889 break;
1890 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001891 break;
1892 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001893
Daniel Dunbarc918d602010-05-04 16:12:42 +00001894 // FIXME: Ideally, we would only attempt suffix matches for things which are
1895 // valid prefixes, and we could just infer the right unambiguous
1896 // type. However, that requires substantially more matcher support than the
1897 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001898
Daniel Dunbarc918d602010-05-04 16:12:42 +00001899 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001900 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001901 SmallString<16> Tmp;
1902 Tmp += Base;
1903 Tmp += ' ';
1904 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001905
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001906 // If this instruction starts with an 'f', then it is a floating point stack
1907 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1908 // 80-bit floating point, which use the suffixes s,l,t respectively.
1909 //
1910 // Otherwise, we assume that this may be an integer instruction, which comes
1911 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1912 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001913
Daniel Dunbarc918d602010-05-04 16:12:42 +00001914 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001915 Tmp[Base.size()] = Suffixes[0];
1916 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00001917 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001918 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001919
Chad Rosier6e006d32012-10-12 22:53:36 +00001920 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1921 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001922 // If this returned as a missing feature failure, remember that.
1923 if (Match1 == Match_MissingFeature)
1924 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001925 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001926 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1927 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001928 // If this returned as a missing feature failure, remember that.
1929 if (Match2 == Match_MissingFeature)
1930 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001931 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001932 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1933 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001934 // If this returned as a missing feature failure, remember that.
1935 if (Match3 == Match_MissingFeature)
1936 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001937 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001938 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1939 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001940 // If this returned as a missing feature failure, remember that.
1941 if (Match4 == Match_MissingFeature)
1942 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001943
1944 // Restore the old token.
1945 Op->setTokenValue(Base);
1946
1947 // If exactly one matched, then we treat that as a successful match (and the
1948 // instruction will already have been filled in correctly, since the failing
1949 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001950 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001951 (Match1 == Match_Success) + (Match2 == Match_Success) +
1952 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001953 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001954 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001955 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001956 Out.EmitInstruction(Inst);
1957 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001958 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001959 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001960
Chris Lattnerec6789f2010-09-06 20:08:02 +00001961 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001962
Daniel Dunbar09062b12010-08-12 00:55:42 +00001963 // If we had multiple suffix matches, then identify this as an ambiguous
1964 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001965 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001966 char MatchChars[4];
1967 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001968 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1969 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1970 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1971 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001972
1973 SmallString<126> Msg;
1974 raw_svector_ostream OS(Msg);
1975 OS << "ambiguous instructions require an explicit suffix (could be ";
1976 for (unsigned i = 0; i != NumMatches; ++i) {
1977 if (i != 0)
1978 OS << ", ";
1979 if (i + 1 == NumMatches)
1980 OS << "or ";
1981 OS << "'" << Base << MatchChars[i] << "'";
1982 }
1983 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001984 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001985 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001986 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001987
Chris Lattnera008e8a2010-09-06 21:54:15 +00001988 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001989
Chris Lattnera008e8a2010-09-06 21:54:15 +00001990 // If all of the instructions reported an invalid mnemonic, then the original
1991 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001992 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1993 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001994 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001995 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001996 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001997 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001998 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001999 }
2000
2001 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002002 if (ErrorInfo != ~0U) {
2003 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002004 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002005 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002006
Chad Rosier84125ca2012-10-13 00:26:04 +00002007 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002008 if (Operand->getStartLoc().isValid()) {
2009 SMRange OperandRange = Operand->getLocRange();
2010 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002011 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002012 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002013 }
2014
Chad Rosierb4fdade2012-08-21 19:36:59 +00002015 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002016 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002017 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002018
Chris Lattnerec6789f2010-09-06 20:08:02 +00002019 // If one instruction matched with a missing feature, report this as a
2020 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002021 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2022 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002023 std::string Msg = "instruction requires:";
2024 unsigned Mask = 1;
2025 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2026 if (ErrorInfoMissingFeature & Mask) {
2027 Msg += " ";
2028 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2029 }
2030 Mask <<= 1;
2031 }
2032 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002033 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002034
Chris Lattnera008e8a2010-09-06 21:54:15 +00002035 // If one instruction matched with an invalid operand, report this as an
2036 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002037 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2038 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002039 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002040 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002041 return true;
2042 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002043
Chris Lattnerec6789f2010-09-06 20:08:02 +00002044 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002045 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002046 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002047 return true;
2048}
2049
2050
Devang Pateldd929fc2012-01-12 18:03:40 +00002051bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002052 StringRef IDVal = DirectiveID.getIdentifier();
2053 if (IDVal == ".word")
2054 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002055 else if (IDVal.startswith(".code"))
2056 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002057 else if (IDVal.startswith(".att_syntax")) {
2058 getParser().setAssemblerDialect(0);
2059 return false;
2060 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002061 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002062 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2063 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002064 // FIXME : Handle noprefix
2065 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002066 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002067 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002068 }
2069 return false;
2070 }
Chris Lattner537ca842010-10-30 17:38:55 +00002071 return true;
2072}
2073
2074/// ParseDirectiveWord
2075/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002076bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002077 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2078 for (;;) {
2079 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002080 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002081 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002082
Eric Christopher1ced2082013-01-09 03:52:05 +00002083 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002084
Chris Lattner537ca842010-10-30 17:38:55 +00002085 if (getLexer().is(AsmToken::EndOfStatement))
2086 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002087
Chris Lattner537ca842010-10-30 17:38:55 +00002088 // FIXME: Improve diagnostic.
2089 if (getLexer().isNot(AsmToken::Comma))
2090 return Error(L, "unexpected token in directive");
2091 Parser.Lex();
2092 }
2093 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002094
Chris Lattner537ca842010-10-30 17:38:55 +00002095 Parser.Lex();
2096 return false;
2097}
2098
Evan Chengbd27f5a2011-07-27 00:38:12 +00002099/// ParseDirectiveCode
2100/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002101bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002102 if (IDVal == ".code32") {
2103 Parser.Lex();
2104 if (is64BitMode()) {
2105 SwitchMode();
2106 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2107 }
2108 } else if (IDVal == ".code64") {
2109 Parser.Lex();
2110 if (!is64BitMode()) {
2111 SwitchMode();
2112 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2113 }
2114 } else {
2115 return Error(L, "unexpected directive " + IDVal);
2116 }
Chris Lattner537ca842010-10-30 17:38:55 +00002117
Evan Chengbd27f5a2011-07-27 00:38:12 +00002118 return false;
2119}
Chris Lattner537ca842010-10-30 17:38:55 +00002120
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002121// Force static initialization.
2122extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002123 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2124 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002125}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002126
Chris Lattner0692ee62010-09-06 19:11:01 +00002127#define GET_REGISTER_MATCHER
2128#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002129#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002130#include "X86GenAsmMatcher.inc"