blob: 100feea3b915d2ec7c8f73495a577de08a1bdda0 [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;
187 bool NeedAsmRewrite;
188 };
189
190 struct MemOp {
191 unsigned SegReg;
192 const MCExpr *Disp;
193 unsigned BaseReg;
194 unsigned IndexReg;
195 unsigned Scale;
196 unsigned Size;
Eric Christophera286fc02013-03-15 00:42:55 +0000197 };
198
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000199 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000200 struct TokOp Tok;
201 struct RegOp Reg;
202 struct ImmOp Imm;
203 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000204 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000205
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000206 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000207 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000208
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000209 /// getStartLoc - Get the location of the first token of this operand.
210 SMLoc getStartLoc() const { return StartLoc; }
211 /// getEndLoc - Get the location of the last token of this operand.
212 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000213 /// getLocRange - Get the range between the first and last token of this
214 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000215 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000216 /// getOffsetOfLoc - Get the location of the offset operator.
217 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000218
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000219 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000220
Daniel Dunbar20927f22009-08-07 08:26:05 +0000221 StringRef getToken() const {
222 assert(Kind == Token && "Invalid access!");
223 return StringRef(Tok.Data, Tok.Length);
224 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000225 void setTokenValue(StringRef Value) {
226 assert(Kind == Token && "Invalid access!");
227 Tok.Data = Value.data();
228 Tok.Length = Value.size();
229 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000230
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000231 unsigned getReg() const {
232 assert(Kind == Register && "Invalid access!");
233 return Reg.RegNo;
234 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000235
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000236 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000237 assert(Kind == Immediate && "Invalid access!");
238 return Imm.Val;
239 }
240
Chad Rosierefcb3d92012-10-26 18:04:20 +0000241 bool needAsmRewrite() const {
242 assert(Kind == Immediate && "Invalid access!");
243 return Imm.NeedAsmRewrite;
244 }
245
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000246 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000247 assert(Kind == Memory && "Invalid access!");
248 return Mem.Disp;
249 }
250 unsigned getMemSegReg() const {
251 assert(Kind == Memory && "Invalid access!");
252 return Mem.SegReg;
253 }
254 unsigned getMemBaseReg() const {
255 assert(Kind == Memory && "Invalid access!");
256 return Mem.BaseReg;
257 }
258 unsigned getMemIndexReg() const {
259 assert(Kind == Memory && "Invalid access!");
260 return Mem.IndexReg;
261 }
262 unsigned getMemScale() const {
263 assert(Kind == Memory && "Invalid access!");
264 return Mem.Scale;
265 }
266
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000267 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000268
269 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000272 if (!isImm())
273 return false;
274
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000275 // If this isn't a constant expr, just assume it fits and let relaxation
276 // handle it.
277 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
278 if (!CE)
279 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000280
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000281 // Otherwise, check the value is in a range that makes sense for this
282 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000283 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000284 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000285 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000286 if (!isImm())
287 return false;
288
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000289 // If this isn't a constant expr, just assume it fits and let relaxation
290 // handle it.
291 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
292 if (!CE)
293 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000294
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000295 // Otherwise, check the value is in a range that makes sense for this
296 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000297 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000298 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000299 bool isImmZExtu32u8() const {
300 if (!isImm())
301 return false;
302
303 // If this isn't a constant expr, just assume it fits and let relaxation
304 // handle it.
305 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
306 if (!CE)
307 return true;
308
309 // Otherwise, check the value is in a range that makes sense for this
310 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000311 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000312 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000313 bool isImmSExti64i8() const {
314 if (!isImm())
315 return false;
316
317 // If this isn't a constant expr, just assume it fits and let relaxation
318 // handle it.
319 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
320 if (!CE)
321 return true;
322
323 // Otherwise, check the value is in a range that makes sense for this
324 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000325 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000326 }
327 bool isImmSExti64i32() const {
328 if (!isImm())
329 return false;
330
331 // If this isn't a constant expr, just assume it fits and let relaxation
332 // handle it.
333 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
334 if (!CE)
335 return true;
336
337 // Otherwise, check the value is in a range that makes sense for this
338 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000339 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000340 }
341
Chad Rosiera703fb92012-10-22 19:50:35 +0000342 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000343 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000344 }
345
Chad Rosierc1ec2072013-01-10 22:10:27 +0000346 bool needAddressOf() const {
347 return AddressOf;
348 }
349
Daniel Dunbar20927f22009-08-07 08:26:05 +0000350 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000363 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000364 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000365 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000366 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000367 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000368 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000369 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000370 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000371 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000372
Craig Topper75dc33a2012-07-18 04:11:12 +0000373 bool isMemVX32() const {
374 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
375 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
376 }
377 bool isMemVY32() const {
378 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
379 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
380 }
381 bool isMemVX64() const {
382 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
383 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
384 }
385 bool isMemVY64() const {
386 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
387 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
388 }
389
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000390 bool isAbsMem() const {
391 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000392 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000393 }
394
Daniel Dunbar20927f22009-08-07 08:26:05 +0000395 bool isReg() const { return Kind == Register; }
396
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000397 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
398 // Add as immediates when possible.
399 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
400 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
401 else
402 Inst.addOperand(MCOperand::CreateExpr(Expr));
403 }
404
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000405 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000406 assert(N == 1 && "Invalid number of operands!");
407 Inst.addOperand(MCOperand::CreateReg(getReg()));
408 }
409
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000410 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000411 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000412 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000413 }
414
Chad Rosier36b8fed2012-06-27 22:34:28 +0000415 void addMem8Operands(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 addMem16Operands(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 addMem32Operands(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 addMem64Operands(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 addMem80Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000430 void addMem128Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000432 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000433 void addMem256Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000435 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000436 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
438 }
439 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
440 addMemOperands(Inst, N);
441 }
442 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
443 addMemOperands(Inst, N);
444 }
445 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
446 addMemOperands(Inst, N);
447 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000448
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000449 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000450 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000451 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
452 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
453 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000454 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000455 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
456 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000457
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000458 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
459 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000460 // Add as immediates when possible.
461 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
462 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
463 else
464 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000465 }
466
Chris Lattnerb4307b32010-01-15 19:28:38 +0000467 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000468 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000469 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000470 Res->Tok.Data = Str.data();
471 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000472 return Res;
473 }
474
Chad Rosierc0a14b82012-10-24 17:22:29 +0000475 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000476 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000477 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000478 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000479 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000480 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000481 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000482 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000483 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000484
Chad Rosierefcb3d92012-10-26 18:04:20 +0000485 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc,
486 bool NeedRewrite = true){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000487 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000488 Res->Imm.Val = Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000489 Res->Imm.NeedAsmRewrite = NeedRewrite;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000490 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000491 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000492
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000493 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000494 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000495 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000496 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
497 Res->Mem.SegReg = 0;
498 Res->Mem.Disp = Disp;
499 Res->Mem.BaseReg = 0;
500 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000501 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000502 Res->Mem.Size = Size;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000503 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000504 return Res;
505 }
506
507 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000508 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
509 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000510 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000511 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000512 // We should never just have a displacement, that should be parsed as an
513 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000514 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
515
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000516 // The scale should always be one of {1,2,4,8}.
517 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000518 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000519 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000520 Res->Mem.SegReg = SegReg;
521 Res->Mem.Disp = Disp;
522 Res->Mem.BaseReg = BaseReg;
523 Res->Mem.IndexReg = IndexReg;
524 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000525 Res->Mem.Size = Size;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000526 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000527 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000528 }
529};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000530
Chris Lattner37dfdec2009-07-29 06:33:53 +0000531} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000532
Devang Pateldd929fc2012-01-12 18:03:40 +0000533bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000534 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000535
536 return (Op.isMem() &&
537 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
538 isa<MCConstantExpr>(Op.Mem.Disp) &&
539 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
540 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
541}
542
Devang Pateldd929fc2012-01-12 18:03:40 +0000543bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000544 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000545
Chad Rosier36b8fed2012-06-27 22:34:28 +0000546 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000547 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000548 isa<MCConstantExpr>(Op.Mem.Disp) &&
549 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
550 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
551}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552
Devang Pateldd929fc2012-01-12 18:03:40 +0000553bool X86AsmParser::ParseRegister(unsigned &RegNo,
554 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000555 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000556 const AsmToken &PercentTok = Parser.getTok();
557 StartLoc = PercentTok.getLoc();
558
559 // If we encounter a %, ignore it. This code handles registers with and
560 // without the prefix, unprefixed registers can occur in cfi directives.
561 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000562 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000563
Sean Callanan18b83232010-01-19 21:44:56 +0000564 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000565 EndLoc = Tok.getEndLoc();
566
Devang Patel1aea4302012-01-20 22:32:05 +0000567 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000568 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000569 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000570 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000571 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000572
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000573 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000574
Chris Lattner33d60d52010-09-22 04:11:10 +0000575 // If the match failed, try the register name as lowercase.
576 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000577 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000578
Evan Cheng5de728c2011-07-27 23:22:03 +0000579 if (!is64BitMode()) {
580 // FIXME: This should be done using Requires<In32BitMode> and
581 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
582 // checked.
583 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
584 // REX prefix.
585 if (RegNo == X86::RIZ ||
586 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
587 X86II::isX86_64NonExtLowByteReg(RegNo) ||
588 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000589 return Error(StartLoc, "register %"
590 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000591 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000592 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000593
Chris Lattner33d60d52010-09-22 04:11:10 +0000594 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
595 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000596 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000597 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000598
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000599 // Check to see if we have '(4)' after %st.
600 if (getLexer().isNot(AsmToken::LParen))
601 return false;
602 // Lex the paren.
603 getParser().Lex();
604
605 const AsmToken &IntTok = Parser.getTok();
606 if (IntTok.isNot(AsmToken::Integer))
607 return Error(IntTok.getLoc(), "expected stack index");
608 switch (IntTok.getIntVal()) {
609 case 0: RegNo = X86::ST0; break;
610 case 1: RegNo = X86::ST1; break;
611 case 2: RegNo = X86::ST2; break;
612 case 3: RegNo = X86::ST3; break;
613 case 4: RegNo = X86::ST4; break;
614 case 5: RegNo = X86::ST5; break;
615 case 6: RegNo = X86::ST6; break;
616 case 7: RegNo = X86::ST7; break;
617 default: return Error(IntTok.getLoc(), "invalid stack index");
618 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000619
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000620 if (getParser().Lex().isNot(AsmToken::RParen))
621 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000622
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000623 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000624 Parser.Lex(); // Eat ')'
625 return false;
626 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000627
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000628 EndLoc = Parser.getTok().getEndLoc();
629
Chris Lattner645b2092010-06-24 07:29:18 +0000630 // If this is "db[0-7]", match it as an alias
631 // for dr[0-7].
632 if (RegNo == 0 && Tok.getString().size() == 3 &&
633 Tok.getString().startswith("db")) {
634 switch (Tok.getString()[2]) {
635 case '0': RegNo = X86::DR0; break;
636 case '1': RegNo = X86::DR1; break;
637 case '2': RegNo = X86::DR2; break;
638 case '3': RegNo = X86::DR3; break;
639 case '4': RegNo = X86::DR4; break;
640 case '5': RegNo = X86::DR5; break;
641 case '6': RegNo = X86::DR6; break;
642 case '7': RegNo = X86::DR7; break;
643 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000644
Chris Lattner645b2092010-06-24 07:29:18 +0000645 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000646 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000647 Parser.Lex(); // Eat it.
648 return false;
649 }
650 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000651
Devang Patel1aea4302012-01-20 22:32:05 +0000652 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000653 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000654 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000655 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000656 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000657
Sean Callananb9a25b72010-01-19 20:27:46 +0000658 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000659 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000660}
661
Devang Pateldd929fc2012-01-12 18:03:40 +0000662X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000663 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000664 return ParseIntelOperand();
665 return ParseATTOperand();
666}
667
Devang Pateld37ad242012-01-17 18:00:18 +0000668/// getIntelMemOperandSize - Return intel memory operand size.
669static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000670 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000671 .Cases("BYTE", "byte", 8)
672 .Cases("WORD", "word", 16)
673 .Cases("DWORD", "dword", 32)
674 .Cases("QWORD", "qword", 64)
675 .Cases("XWORD", "xword", 80)
676 .Cases("XMMWORD", "xmmword", 128)
677 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000678 .Default(0);
679 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000680}
681
Chad Rosierdd2e8952013-01-14 22:31:35 +0000682enum IntelBracExprState {
683 IBES_START,
684 IBES_LBRAC,
685 IBES_RBRAC,
686 IBES_REGISTER,
687 IBES_REGISTER_STAR,
688 IBES_REGISTER_STAR_INTEGER,
689 IBES_INTEGER,
690 IBES_INTEGER_STAR,
691 IBES_INDEX_REGISTER,
692 IBES_IDENTIFIER,
693 IBES_DISP_EXPR,
694 IBES_MINUS,
695 IBES_ERROR
696};
697
698class IntelBracExprStateMachine {
699 IntelBracExprState State;
700 unsigned BaseReg, IndexReg, Scale;
701 int64_t Disp;
702
703 unsigned TmpReg;
704 int64_t TmpInteger;
705
706 bool isPlus;
707
708public:
709 IntelBracExprStateMachine(MCAsmParser &parser) :
710 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(0),
711 TmpReg(0), TmpInteger(0), isPlus(true) {}
712
713 unsigned getBaseReg() { return BaseReg; }
714 unsigned getIndexReg() { return IndexReg; }
715 unsigned getScale() { return Scale; }
716 int64_t getDisp() { return Disp; }
717 bool isValidEndState() { return State == IBES_RBRAC; }
718
719 void onPlus() {
720 switch (State) {
721 default:
722 State = IBES_ERROR;
723 break;
724 case IBES_INTEGER:
725 State = IBES_START;
726 if (isPlus)
727 Disp += TmpInteger;
728 else
729 Disp -= TmpInteger;
730 break;
731 case IBES_REGISTER:
732 State = IBES_START;
733 // If we already have a BaseReg, then assume this is the IndexReg with a
734 // scale of 1.
735 if (!BaseReg) {
736 BaseReg = TmpReg;
737 } else {
738 assert (!IndexReg && "BaseReg/IndexReg already set!");
739 IndexReg = TmpReg;
740 Scale = 1;
741 }
742 break;
743 case IBES_INDEX_REGISTER:
744 State = IBES_START;
745 break;
746 }
747 isPlus = true;
748 }
749 void onMinus() {
750 switch (State) {
751 default:
752 State = IBES_ERROR;
753 break;
754 case IBES_START:
755 State = IBES_MINUS;
756 break;
757 case IBES_INTEGER:
758 State = IBES_START;
759 if (isPlus)
760 Disp += TmpInteger;
761 else
762 Disp -= TmpInteger;
763 break;
764 case IBES_REGISTER:
765 State = IBES_START;
766 // If we already have a BaseReg, then assume this is the IndexReg with a
767 // scale of 1.
768 if (!BaseReg) {
769 BaseReg = TmpReg;
770 } else {
771 assert (!IndexReg && "BaseReg/IndexReg already set!");
772 IndexReg = TmpReg;
773 Scale = 1;
774 }
775 break;
776 case IBES_INDEX_REGISTER:
777 State = IBES_START;
778 break;
779 }
780 isPlus = false;
781 }
782 void onRegister(unsigned Reg) {
783 switch (State) {
784 default:
785 State = IBES_ERROR;
786 break;
787 case IBES_START:
788 State = IBES_REGISTER;
789 TmpReg = Reg;
790 break;
791 case IBES_INTEGER_STAR:
792 assert (!IndexReg && "IndexReg already set!");
793 State = IBES_INDEX_REGISTER;
794 IndexReg = Reg;
795 Scale = TmpInteger;
796 break;
797 }
798 }
799 void onDispExpr() {
800 switch (State) {
801 default:
802 State = IBES_ERROR;
803 break;
804 case IBES_START:
805 State = IBES_DISP_EXPR;
806 break;
807 }
808 }
809 void onInteger(int64_t TmpInt) {
810 switch (State) {
811 default:
812 State = IBES_ERROR;
813 break;
814 case IBES_START:
815 State = IBES_INTEGER;
816 TmpInteger = TmpInt;
817 break;
818 case IBES_MINUS:
819 State = IBES_INTEGER;
820 TmpInteger = TmpInt;
821 break;
822 case IBES_REGISTER_STAR:
823 assert (!IndexReg && "IndexReg already set!");
824 State = IBES_INDEX_REGISTER;
825 IndexReg = TmpReg;
826 Scale = TmpInt;
827 break;
828 }
829 }
830 void onStar() {
831 switch (State) {
832 default:
833 State = IBES_ERROR;
834 break;
835 case IBES_INTEGER:
836 State = IBES_INTEGER_STAR;
837 break;
838 case IBES_REGISTER:
839 State = IBES_REGISTER_STAR;
840 break;
841 }
842 }
843 void onLBrac() {
844 switch (State) {
845 default:
846 State = IBES_ERROR;
847 break;
848 case IBES_RBRAC:
849 State = IBES_START;
850 isPlus = true;
851 break;
852 }
853 }
854 void onRBrac() {
855 switch (State) {
856 default:
857 State = IBES_ERROR;
858 break;
859 case IBES_DISP_EXPR:
860 State = IBES_RBRAC;
861 break;
862 case IBES_INTEGER:
863 State = IBES_RBRAC;
864 if (isPlus)
865 Disp += TmpInteger;
866 else
867 Disp -= TmpInteger;
868 break;
869 case IBES_REGISTER:
870 State = IBES_RBRAC;
871 // If we already have a BaseReg, then assume this is the IndexReg with a
872 // scale of 1.
873 if (!BaseReg) {
874 BaseReg = TmpReg;
875 } else {
876 assert (!IndexReg && "BaseReg/IndexReg already set!");
877 IndexReg = TmpReg;
878 Scale = 1;
879 }
880 break;
881 case IBES_INDEX_REGISTER:
882 State = IBES_RBRAC;
883 break;
884 }
885 }
886};
887
Chad Rosierd3e74162013-03-19 21:11:56 +0000888X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
889 SMLoc End, SMLoc SizeDirLoc,
890 unsigned Size) {
891 bool NeedSizeDir = false;
892 bool IsVarDecl = false;
893 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
894 const MCSymbol &Sym = SymRef->getSymbol();
895 // FIXME: The SemaLookup will fail if the name is anything other then an
896 // identifier.
897 // FIXME: Pass a valid SMLoc.
898 unsigned tLength, tSize, tType;
899 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
900 tSize, tType, IsVarDecl);
901 if (!Size) {
902 Size = tType * 8; // Size is in terms of bits in this context.
903 NeedSizeDir = Size > 0;
904 }
905 }
906
907 // If this is not a VarDecl then assume it is a FuncDecl or some other label
908 // reference. We need an 'r' constraint here, so we need to create register
909 // operand to ensure proper matching. Just pick a GPR based on the size of
910 // a pointer.
911 if (!IsVarDecl) {
912 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
913 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
914 }
915
916 if (NeedSizeDir)
917 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
918 /*Len*/0, Size));
919
920 // When parsing inline assembly we set the base register to a non-zero value
921 // as we don't know the actual value at this time. This is necessary to
922 // get the matching correct in some cases.
923 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
924 /*Scale*/1, Start, End, Size);
925}
926
Chad Rosier65c88922012-10-22 19:42:52 +0000927X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000928 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000929 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000930 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000931
Devang Pateld37ad242012-01-17 18:00:18 +0000932 // Eat '['
933 if (getLexer().isNot(AsmToken::LBrac))
934 return ErrorOperand(Start, "Expected '[' token!");
935 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000936
Chad Rosierdd2e8952013-01-14 22:31:35 +0000937 unsigned TmpReg = 0;
938
939 // Try to handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000940 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000941 if (ParseRegister(TmpReg, Start, End)) {
942 const MCExpr *Disp;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000943 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000944 return 0;
945
Devang Pateld37ad242012-01-17 18:00:18 +0000946 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000947 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosier4fb25b72013-02-15 21:58:13 +0000948 // Adjust the EndLoc due to the ']'.
949 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000950 Parser.Lex();
Chad Rosierd3e74162013-03-19 21:11:56 +0000951 if (!isParsingInlineAsm())
952 return X86Operand::CreateMem(Disp, Start, End, Size);
953
954 // We want the size directive before the '['.
955 SMLoc SizeDirLoc = SMLoc::getFromPointer(Start.getPointer()-1);
956 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000957 }
Devang Pateld37ad242012-01-17 18:00:18 +0000958 }
959
Chad Rosierdd2e8952013-01-14 22:31:35 +0000960 // Parse [ BaseReg + Scale*IndexReg + Disp ].
961 bool Done = false;
962 IntelBracExprStateMachine SM(Parser);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000963
Chad Rosierdd2e8952013-01-14 22:31:35 +0000964 // If we parsed a register, then the end loc has already been set and
965 // the identifier has already been lexed. We also need to update the
966 // state.
967 if (TmpReg)
968 SM.onRegister(TmpReg);
969
970 const MCExpr *Disp = 0;
971 while (!Done) {
972 bool UpdateLocLex = true;
973
974 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
975 // identifier. Don't try an parse it as a register.
976 if (Tok.getString().startswith("."))
977 break;
978
979 switch (getLexer().getKind()) {
980 default: {
981 if (SM.isValidEndState()) {
982 Done = true;
983 break;
984 }
985 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
986 }
987 case AsmToken::Identifier: {
988 // This could be a register or a displacement expression.
989 if(!ParseRegister(TmpReg, Start, End)) {
990 SM.onRegister(TmpReg);
991 UpdateLocLex = false;
992 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000993 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000994 SM.onDispExpr();
995 UpdateLocLex = false;
996 break;
997 }
998 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
999 }
1000 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +00001001 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +00001002 SM.onInteger(Val);
1003 break;
1004 }
1005 case AsmToken::Plus: SM.onPlus(); break;
1006 case AsmToken::Minus: SM.onMinus(); break;
1007 case AsmToken::Star: SM.onStar(); break;
1008 case AsmToken::LBrac: SM.onLBrac(); break;
1009 case AsmToken::RBrac: SM.onRBrac(); break;
1010 }
1011 if (!Done && UpdateLocLex) {
1012 End = Tok.getLoc();
1013 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +00001014 }
Devang Pateld37ad242012-01-17 18:00:18 +00001015 }
1016
Chad Rosierdd2e8952013-01-14 22:31:35 +00001017 if (!Disp)
1018 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +00001019
Chad Rosierddb53ef2012-10-26 22:01:25 +00001020 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001021 if (Tok.getString().startswith(".")) {
1022 SmallString<64> Err;
1023 const MCExpr *NewDisp;
1024 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1025 return ErrorOperand(Tok.getLoc(), Err);
1026
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001027 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001028 Parser.Lex(); // Eat the field.
1029 Disp = NewDisp;
1030 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001031
Chad Rosierdd2e8952013-01-14 22:31:35 +00001032 int BaseReg = SM.getBaseReg();
1033 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001034
Chad Rosierdd2e8952013-01-14 22:31:35 +00001035 // handle [-42]
1036 if (!BaseReg && !IndexReg) {
1037 if (!SegReg)
1038 return X86Operand::CreateMem(Disp, Start, End);
1039 else
1040 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1041 }
1042
1043 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001044 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001045 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001046}
1047
1048/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001049X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001050 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001051 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001052
1053 unsigned Size = getIntelMemOperandSize(Tok.getString());
1054 if (Size) {
1055 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001056 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1057 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001058 Parser.Lex();
1059 }
1060
Chad Rosierc0a14b82012-10-24 17:22:29 +00001061 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +00001062 return ParseIntelBracExpression(SegReg, Size);
1063
1064 if (!ParseRegister(SegReg, Start, End)) {
1065 // Handel SegReg : [ ... ]
1066 if (getLexer().isNot(AsmToken::Colon))
1067 return ErrorOperand(Start, "Expected ':' token!");
1068 Parser.Lex(); // Eat :
1069 if (getLexer().isNot(AsmToken::LBrac))
1070 return ErrorOperand(Start, "Expected '[' token!");
1071 return ParseIntelBracExpression(SegReg, Size);
1072 }
Devang Pateld37ad242012-01-17 18:00:18 +00001073
1074 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001075 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001076 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001077
Chad Rosier2a784132012-10-23 23:31:33 +00001078 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001079 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierd3e74162013-03-19 21:11:56 +00001080 return CreateMemForInlineAsm(Disp, Start, End, Start, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001081}
1082
Chad Rosier22f441a2012-10-24 22:21:50 +00001083/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001084bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1085 const MCExpr **NewDisp,
1086 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001087 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001088 uint64_t OrigDispVal, DotDispVal;
1089
1090 // FIXME: Handle non-constant expressions.
1091 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1092 OrigDispVal = OrigDisp->getValue();
1093 } else {
1094 Err = "Non-constant offsets are not supported!";
1095 return true;
1096 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001097
1098 // Drop the '.'.
1099 StringRef DotDispStr = Tok.getString().drop_front(1);
1100
Chad Rosier22f441a2012-10-24 22:21:50 +00001101 // .Imm gets lexed as a real.
1102 if (Tok.is(AsmToken::Real)) {
1103 APInt DotDisp;
1104 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001105 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001106 } else if (Tok.is(AsmToken::Identifier)) {
1107 // We should only see an identifier when parsing the original inline asm.
1108 // The front-end should rewrite this in terms of immediates.
1109 assert (isParsingInlineAsm() && "Unexpected field name!");
1110
1111 unsigned DotDisp;
1112 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1113 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1114 DotDisp)) {
1115 Err = "Unable to lookup field reference!";
1116 return true;
1117 }
1118 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001119 } else {
1120 Err = "Unexpected token type!";
1121 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001122 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001123
Chad Rosierec130222012-10-25 21:51:10 +00001124 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1125 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1126 unsigned Len = DotDispStr.size();
1127 unsigned Val = OrigDispVal + DotDispVal;
1128 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1129 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001130 }
1131
1132 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1133 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001134}
1135
Chad Rosierc0a14b82012-10-24 17:22:29 +00001136/// Parse the 'offset' operator. This operator is used to specify the
1137/// location rather then the content of a variable.
1138X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1139 SMLoc OffsetOfLoc = Start;
1140 Parser.Lex(); // Eat offset.
1141 Start = Parser.getTok().getLoc();
1142 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1143
Chad Rosier6e431572012-10-26 16:09:20 +00001144 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001145 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001146 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001147 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001148
Chad Rosier6e431572012-10-26 16:09:20 +00001149 // Don't emit the offset operator.
1150 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1151
Chad Rosierc0a14b82012-10-24 17:22:29 +00001152 // The offset operator will have an 'r' constraint, thus we need to create
1153 // register operand to ensure proper matching. Just pick a GPR based on
1154 // the size of a pointer.
1155 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001156 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1157 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001158}
1159
Chad Rosier505bca32013-01-17 19:21:48 +00001160enum IntelOperatorKind {
1161 IOK_LENGTH,
1162 IOK_SIZE,
1163 IOK_TYPE
1164};
1165
1166/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1167/// returns the number of elements in an array. It returns the value 1 for
1168/// non-array variables. The SIZE operator returns the size of a C or C++
1169/// variable. A variable's size is the product of its LENGTH and TYPE. The
1170/// TYPE operator returns the size of a C or C++ type or variable. If the
1171/// variable is an array, TYPE returns the size of a single element.
1172X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001173 SMLoc TypeLoc = Start;
1174 Parser.Lex(); // Eat offset.
1175 Start = Parser.getTok().getLoc();
1176 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1177
1178 SMLoc End;
1179 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001180 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001181 return 0;
1182
Chad Rosier505bca32013-01-17 19:21:48 +00001183 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001184 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1185 const MCSymbol &Sym = SymRef->getSymbol();
1186 // FIXME: The SemaLookup will fail if the name is anything other then an
1187 // identifier.
1188 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001189 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001190 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1191 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001192 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001193 }
1194 unsigned CVal;
1195 switch(OpKind) {
1196 default: llvm_unreachable("Unexpected operand kind!");
1197 case IOK_LENGTH: CVal = Length; break;
1198 case IOK_SIZE: CVal = Size; break;
1199 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001200 }
1201
1202 // Rewrite the type operator and the C or C++ type or variable in terms of an
1203 // immediate. E.g. TYPE foo -> $$4
1204 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001205 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001206
Chad Rosier505bca32013-01-17 19:21:48 +00001207 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierefcb3d92012-10-26 18:04:20 +00001208 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
1209}
1210
Devang Pateld37ad242012-01-17 18:00:18 +00001211X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001212 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001213 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001214
Chad Rosier505bca32013-01-17 19:21:48 +00001215 // Offset, length, type and size operators.
1216 if (isParsingInlineAsm()) {
1217 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1218 return ParseIntelOffsetOfOperator(Start);
1219 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1220 return ParseIntelOperator(Start, IOK_LENGTH);
1221 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1222 return ParseIntelOperator(Start, IOK_SIZE);
1223 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1224 return ParseIntelOperator(Start, IOK_TYPE);
1225 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001226
Chad Rosier505bca32013-01-17 19:21:48 +00001227 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001228 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1229 getLexer().is(AsmToken::Minus)) {
1230 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001231 if (!getParser().parseExpression(Val, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +00001232 return X86Operand::CreateImm(Val, Start, End);
1233 }
1234 }
1235
Chad Rosier505bca32013-01-17 19:21:48 +00001236 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001237 unsigned RegNo = 0;
1238 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001239 // If this is a segment register followed by a ':', then this is the start
1240 // of a memory reference, otherwise this is a normal register reference.
1241 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001242 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001243
1244 getParser().Lex(); // Eat the colon.
1245 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001246 }
1247
Chad Rosier505bca32013-01-17 19:21:48 +00001248 // Memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001249 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001250}
1251
Devang Pateldd929fc2012-01-12 18:03:40 +00001252X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001253 switch (getLexer().getKind()) {
1254 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001255 // Parse a memory operand with no segment register.
1256 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001257 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001258 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001259 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001260 SMLoc Start, End;
1261 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001262 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001263 Error(Start, "%eiz and %riz can only be used as index registers",
1264 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001265 return 0;
1266 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001267
Chris Lattnereef6d782010-04-17 18:56:34 +00001268 // If this is a segment register followed by a ':', then this is the start
1269 // of a memory reference, otherwise this is a normal register reference.
1270 if (getLexer().isNot(AsmToken::Colon))
1271 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001272
1273
Chris Lattnereef6d782010-04-17 18:56:34 +00001274 getParser().Lex(); // Eat the colon.
1275 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001276 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001277 case AsmToken::Dollar: {
1278 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001279 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001280 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001281 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001282 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001283 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001284 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001285 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001286 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001287}
1288
Chris Lattnereef6d782010-04-17 18:56:34 +00001289/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1290/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001291X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001292
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001293 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1294 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001295 // only way to do this without lookahead is to eat the '(' and see what is
1296 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001297 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001298 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001299 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001300 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001301
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001302 // After parsing the base expression we could either have a parenthesized
1303 // memory address or not. If not, return now. If so, eat the (.
1304 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001305 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001306 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001307 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001308 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001309 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001310
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001311 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001312 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001313 } else {
1314 // Okay, we have a '('. We don't know if this is an expression or not, but
1315 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001316 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001317 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001318
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001319 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001320 // Nothing to do here, fall into the code below with the '(' part of the
1321 // memory operand consumed.
1322 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001323 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001324
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001325 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001326 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001327 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001328
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001329 // After parsing the base expression we could either have a parenthesized
1330 // memory address or not. If not, return now. If so, eat the (.
1331 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001332 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001333 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001334 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001335 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001336 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001337
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001338 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001339 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001340 }
1341 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001342
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001343 // If we reached here, then we just ate the ( of the memory operand. Process
1344 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001345 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001346 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001347
Chris Lattner29ef9a22010-01-15 18:51:29 +00001348 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001349 SMLoc StartLoc, EndLoc;
1350 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001351 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001352 Error(StartLoc, "eiz and riz can only be used as index registers",
1353 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001354 return 0;
1355 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001356 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001357
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001358 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001359 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001360 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001361
1362 // Following the comma we should have either an index register, or a scale
1363 // value. We don't support the later form, but we want to parse it
1364 // correctly.
1365 //
1366 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001367 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001368 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001369 SMLoc L;
1370 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001371
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001372 if (getLexer().isNot(AsmToken::RParen)) {
1373 // Parse the scale amount:
1374 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001375 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001376 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001377 "expected comma in scale expression");
1378 return 0;
1379 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001380 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001381
1382 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001383 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001384
1385 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001386 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001387 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001388 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001389 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001390
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001391 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001392 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1393 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1394 return 0;
1395 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001396 Scale = (unsigned)ScaleVal;
1397 }
1398 }
1399 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001400 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001401 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001402 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001403
1404 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001405 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001406 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001407
Daniel Dunbaree910252010-08-24 19:13:38 +00001408 if (Value != 1)
1409 Warning(Loc, "scale factor without index register is ignored");
1410 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001411 }
1412 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001413
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001414 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001415 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001416 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001417 return 0;
1418 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001419 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001420 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001421
Kevin Enderby84faf652012-03-12 21:32:09 +00001422 // If we have both a base register and an index register make sure they are
1423 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001424 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001425 if (BaseReg != 0 && IndexReg != 0) {
1426 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001427 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1428 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001429 IndexReg != X86::RIZ) {
1430 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1431 return 0;
1432 }
1433 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001434 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1435 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001436 IndexReg != X86::EIZ){
1437 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1438 return 0;
1439 }
1440 }
1441
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001442 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1443 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001444}
1445
Devang Pateldd929fc2012-01-12 18:03:40 +00001446bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001447ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001448 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001449 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001450 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001451
Chris Lattnerd8f71792010-11-28 20:23:50 +00001452 // FIXME: Hack to recognize setneb as setne.
1453 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1454 PatchedName != "setb" && PatchedName != "setnb")
1455 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001456
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001457 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1458 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001459 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001460 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1461 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001462 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001463 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001464 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001465 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001466 .Case("eq", 0x00)
1467 .Case("lt", 0x01)
1468 .Case("le", 0x02)
1469 .Case("unord", 0x03)
1470 .Case("neq", 0x04)
1471 .Case("nlt", 0x05)
1472 .Case("nle", 0x06)
1473 .Case("ord", 0x07)
1474 /* AVX only from here */
1475 .Case("eq_uq", 0x08)
1476 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001477 .Case("ngt", 0x0A)
1478 .Case("false", 0x0B)
1479 .Case("neq_oq", 0x0C)
1480 .Case("ge", 0x0D)
1481 .Case("gt", 0x0E)
1482 .Case("true", 0x0F)
1483 .Case("eq_os", 0x10)
1484 .Case("lt_oq", 0x11)
1485 .Case("le_oq", 0x12)
1486 .Case("unord_s", 0x13)
1487 .Case("neq_us", 0x14)
1488 .Case("nlt_uq", 0x15)
1489 .Case("nle_uq", 0x16)
1490 .Case("ord_s", 0x17)
1491 .Case("eq_us", 0x18)
1492 .Case("nge_uq", 0x19)
1493 .Case("ngt_uq", 0x1A)
1494 .Case("false_os", 0x1B)
1495 .Case("neq_os", 0x1C)
1496 .Case("ge_oq", 0x1D)
1497 .Case("gt_oq", 0x1E)
1498 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001499 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001500 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001501 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1502 getParser().getContext());
1503 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001504 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001505 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001506 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001507 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001508 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001509 } else {
1510 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001511 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001512 }
1513 }
1514 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001515
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001516 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001517
Devang Patel885f65b2012-01-30 22:47:12 +00001518 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001519 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001520
Chris Lattner2544f422010-09-08 05:17:37 +00001521 // Determine whether this is an instruction prefix.
1522 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001523 Name == "lock" || Name == "rep" ||
1524 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001525 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001526 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001527
1528
Chris Lattner2544f422010-09-08 05:17:37 +00001529 // This does the actual operand parsing. Don't parse any more if we have a
1530 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1531 // just want to parse the "lock" as the first instruction and the "incl" as
1532 // the next one.
1533 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001534
1535 // Parse '*' modifier.
1536 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001537 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001538 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001539 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001540 }
1541
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001542 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001543 if (X86Operand *Op = ParseOperand())
1544 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001545 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001546 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001547 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001548 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001549
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001550 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001551 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001552
1553 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001554 if (X86Operand *Op = ParseOperand())
1555 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001556 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001557 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001558 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001559 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001560 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001561
Chris Lattnercbf8a982010-09-11 16:18:25 +00001562 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001563 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001564 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001565 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001566 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001567 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001568
Chris Lattner2544f422010-09-08 05:17:37 +00001569 if (getLexer().is(AsmToken::EndOfStatement))
1570 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001571 else if (isPrefix && getLexer().is(AsmToken::Slash))
1572 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001573
Devang Patel885f65b2012-01-30 22:47:12 +00001574 if (ExtraImmOp && isParsingIntelSyntax())
1575 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1576
Chris Lattner98c870f2010-11-06 19:25:43 +00001577 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1578 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1579 // documented form in various unofficial manuals, so a lot of code uses it.
1580 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1581 Operands.size() == 3) {
1582 X86Operand &Op = *(X86Operand*)Operands.back();
1583 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1584 isa<MCConstantExpr>(Op.Mem.Disp) &&
1585 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1586 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1587 SMLoc Loc = Op.getEndLoc();
1588 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1589 delete &Op;
1590 }
1591 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001592 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1593 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1594 Operands.size() == 3) {
1595 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1596 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1597 isa<MCConstantExpr>(Op.Mem.Disp) &&
1598 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1599 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1600 SMLoc Loc = Op.getEndLoc();
1601 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1602 delete &Op;
1603 }
1604 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001605 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1606 if (Name.startswith("ins") && Operands.size() == 3 &&
1607 (Name == "insb" || Name == "insw" || Name == "insl")) {
1608 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1609 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1610 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1611 Operands.pop_back();
1612 Operands.pop_back();
1613 delete &Op;
1614 delete &Op2;
1615 }
1616 }
1617
1618 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1619 if (Name.startswith("outs") && Operands.size() == 3 &&
1620 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1621 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1622 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1623 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1624 Operands.pop_back();
1625 Operands.pop_back();
1626 delete &Op;
1627 delete &Op2;
1628 }
1629 }
1630
1631 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1632 if (Name.startswith("movs") && Operands.size() == 3 &&
1633 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001634 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001635 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1636 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1637 if (isSrcOp(Op) && isDstOp(Op2)) {
1638 Operands.pop_back();
1639 Operands.pop_back();
1640 delete &Op;
1641 delete &Op2;
1642 }
1643 }
1644 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1645 if (Name.startswith("lods") && Operands.size() == 3 &&
1646 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001647 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001648 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1649 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1650 if (isSrcOp(*Op1) && Op2->isReg()) {
1651 const char *ins;
1652 unsigned reg = Op2->getReg();
1653 bool isLods = Name == "lods";
1654 if (reg == X86::AL && (isLods || Name == "lodsb"))
1655 ins = "lodsb";
1656 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1657 ins = "lodsw";
1658 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1659 ins = "lodsl";
1660 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1661 ins = "lodsq";
1662 else
1663 ins = NULL;
1664 if (ins != NULL) {
1665 Operands.pop_back();
1666 Operands.pop_back();
1667 delete Op1;
1668 delete Op2;
1669 if (Name != ins)
1670 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1671 }
1672 }
1673 }
1674 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1675 if (Name.startswith("stos") && Operands.size() == 3 &&
1676 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001677 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001678 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1679 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1680 if (isDstOp(*Op2) && Op1->isReg()) {
1681 const char *ins;
1682 unsigned reg = Op1->getReg();
1683 bool isStos = Name == "stos";
1684 if (reg == X86::AL && (isStos || Name == "stosb"))
1685 ins = "stosb";
1686 else if (reg == X86::AX && (isStos || Name == "stosw"))
1687 ins = "stosw";
1688 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1689 ins = "stosl";
1690 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1691 ins = "stosq";
1692 else
1693 ins = NULL;
1694 if (ins != NULL) {
1695 Operands.pop_back();
1696 Operands.pop_back();
1697 delete Op1;
1698 delete Op2;
1699 if (Name != ins)
1700 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1701 }
1702 }
1703 }
1704
Chris Lattnere9e16a32010-09-15 04:33:27 +00001705 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001706 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001707 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001708 Name.startswith("shl") || Name.startswith("sal") ||
1709 Name.startswith("rcl") || Name.startswith("rcr") ||
1710 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001711 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001712 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001713 // Intel syntax
1714 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1715 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001716 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1717 delete Operands[2];
1718 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001719 }
1720 } else {
1721 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1722 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001723 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1724 delete Operands[1];
1725 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001726 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001727 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001728 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001729
Chris Lattner15f89512011-04-09 19:41:05 +00001730 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1731 // instalias with an immediate operand yet.
1732 if (Name == "int" && Operands.size() == 2) {
1733 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1734 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1735 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1736 delete Operands[1];
1737 Operands.erase(Operands.begin() + 1);
1738 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1739 }
1740 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001741
Chris Lattner98986712010-01-14 22:21:20 +00001742 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001743}
1744
Craig Topper4bef9612013-03-18 02:53:34 +00001745static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1746 bool isCmp) {
1747 MCInst TmpInst;
1748 TmpInst.setOpcode(Opcode);
1749 if (!isCmp)
1750 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1751 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1752 TmpInst.addOperand(Inst.getOperand(0));
1753 Inst = TmpInst;
1754 return true;
1755}
1756
1757static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1758 bool isCmp = false) {
1759 if (!Inst.getOperand(0).isImm() ||
1760 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1761 return false;
1762
1763 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1764}
1765
1766static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1767 bool isCmp = false) {
1768 if (!Inst.getOperand(0).isImm() ||
1769 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1770 return false;
1771
1772 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1773}
1774
1775static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1776 bool isCmp = false) {
1777 if (!Inst.getOperand(0).isImm() ||
1778 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1779 return false;
1780
1781 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1782}
1783
Devang Pateldd929fc2012-01-12 18:03:40 +00001784bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001785processInstruction(MCInst &Inst,
1786 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1787 switch (Inst.getOpcode()) {
1788 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001789 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1790 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1791 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1792 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1793 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1794 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1795 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1796 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1797 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1798 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1799 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1800 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1801 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1802 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1803 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1804 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1805 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1806 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001807 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1808 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1809 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1810 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1811 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1812 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001813 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001814}
1815
Jim Grosbach3ca63822012-11-14 18:04:47 +00001816static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001817bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001818MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001819 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001820 MCStreamer &Out, unsigned &ErrorInfo,
1821 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001822 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001823 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1824 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001825 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001826
Chris Lattner7c51a312010-09-29 01:50:45 +00001827 // First, handle aliases that expand to multiple instructions.
1828 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001829 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001830 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001831 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001832 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001833 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001834 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001835 MCInst Inst;
1836 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001837 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001838 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001839 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001840
Chris Lattner0bb83a82010-09-30 16:39:29 +00001841 const char *Repl =
1842 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001843 .Case("finit", "fninit")
1844 .Case("fsave", "fnsave")
1845 .Case("fstcw", "fnstcw")
1846 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001847 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001848 .Case("fstsw", "fnstsw")
1849 .Case("fstsww", "fnstsw")
1850 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001851 .Default(0);
1852 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001853 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001854 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001855 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001856
Chris Lattnera008e8a2010-09-06 21:54:15 +00001857 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001858 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001859
Daniel Dunbarc918d602010-05-04 16:12:42 +00001860 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001861 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001862 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001863 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001864 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001865 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001866 // Some instructions need post-processing to, for example, tweak which
1867 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001868 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001869 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001870 while (processInstruction(Inst, Operands))
1871 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001872
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001873 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001874 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001875 Out.EmitInstruction(Inst);
1876 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001877 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001878 case Match_MissingFeature: {
1879 assert(ErrorInfo && "Unknown missing feature!");
1880 // Special case the error message for the very common case where only
1881 // a single subtarget feature is missing.
1882 std::string Msg = "instruction requires:";
1883 unsigned Mask = 1;
1884 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1885 if (ErrorInfo & Mask) {
1886 Msg += " ";
1887 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1888 }
1889 Mask <<= 1;
1890 }
1891 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1892 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001893 case Match_InvalidOperand:
1894 WasOriginallyInvalidOperand = true;
1895 break;
1896 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001897 break;
1898 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001899
Daniel Dunbarc918d602010-05-04 16:12:42 +00001900 // FIXME: Ideally, we would only attempt suffix matches for things which are
1901 // valid prefixes, and we could just infer the right unambiguous
1902 // type. However, that requires substantially more matcher support than the
1903 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001904
Daniel Dunbarc918d602010-05-04 16:12:42 +00001905 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001906 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001907 SmallString<16> Tmp;
1908 Tmp += Base;
1909 Tmp += ' ';
1910 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001911
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001912 // If this instruction starts with an 'f', then it is a floating point stack
1913 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1914 // 80-bit floating point, which use the suffixes s,l,t respectively.
1915 //
1916 // Otherwise, we assume that this may be an integer instruction, which comes
1917 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1918 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001919
Daniel Dunbarc918d602010-05-04 16:12:42 +00001920 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001921 Tmp[Base.size()] = Suffixes[0];
1922 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00001923 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001924 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001925
Chad Rosier6e006d32012-10-12 22:53:36 +00001926 Match1 = 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 (Match1 == Match_MissingFeature)
1930 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001931 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001932 Match2 = 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 (Match2 == Match_MissingFeature)
1936 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001937 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001938 Match3 = 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 (Match3 == Match_MissingFeature)
1942 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001943 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001944 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1945 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001946 // If this returned as a missing feature failure, remember that.
1947 if (Match4 == Match_MissingFeature)
1948 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001949
1950 // Restore the old token.
1951 Op->setTokenValue(Base);
1952
1953 // If exactly one matched, then we treat that as a successful match (and the
1954 // instruction will already have been filled in correctly, since the failing
1955 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001956 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001957 (Match1 == Match_Success) + (Match2 == Match_Success) +
1958 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001959 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001960 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001961 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001962 Out.EmitInstruction(Inst);
1963 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001964 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001965 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001966
Chris Lattnerec6789f2010-09-06 20:08:02 +00001967 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001968
Daniel Dunbar09062b12010-08-12 00:55:42 +00001969 // If we had multiple suffix matches, then identify this as an ambiguous
1970 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001971 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001972 char MatchChars[4];
1973 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001974 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1975 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1976 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1977 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001978
1979 SmallString<126> Msg;
1980 raw_svector_ostream OS(Msg);
1981 OS << "ambiguous instructions require an explicit suffix (could be ";
1982 for (unsigned i = 0; i != NumMatches; ++i) {
1983 if (i != 0)
1984 OS << ", ";
1985 if (i + 1 == NumMatches)
1986 OS << "or ";
1987 OS << "'" << Base << MatchChars[i] << "'";
1988 }
1989 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001990 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001991 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001992 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001993
Chris Lattnera008e8a2010-09-06 21:54:15 +00001994 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001995
Chris Lattnera008e8a2010-09-06 21:54:15 +00001996 // If all of the instructions reported an invalid mnemonic, then the original
1997 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001998 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1999 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00002000 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00002001 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002002 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002003 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002004 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002005 }
2006
2007 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002008 if (ErrorInfo != ~0U) {
2009 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002010 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002011 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002012
Chad Rosier84125ca2012-10-13 00:26:04 +00002013 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002014 if (Operand->getStartLoc().isValid()) {
2015 SMRange OperandRange = Operand->getLocRange();
2016 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002017 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002018 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002019 }
2020
Chad Rosierb4fdade2012-08-21 19:36:59 +00002021 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002022 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002023 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002024
Chris Lattnerec6789f2010-09-06 20:08:02 +00002025 // If one instruction matched with a missing feature, report this as a
2026 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002027 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2028 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002029 std::string Msg = "instruction requires:";
2030 unsigned Mask = 1;
2031 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2032 if (ErrorInfoMissingFeature & Mask) {
2033 Msg += " ";
2034 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2035 }
2036 Mask <<= 1;
2037 }
2038 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002039 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002040
Chris Lattnera008e8a2010-09-06 21:54:15 +00002041 // If one instruction matched with an invalid operand, report this as an
2042 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002043 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2044 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002045 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002046 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002047 return true;
2048 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002049
Chris Lattnerec6789f2010-09-06 20:08:02 +00002050 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002051 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002052 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002053 return true;
2054}
2055
2056
Devang Pateldd929fc2012-01-12 18:03:40 +00002057bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002058 StringRef IDVal = DirectiveID.getIdentifier();
2059 if (IDVal == ".word")
2060 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002061 else if (IDVal.startswith(".code"))
2062 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002063 else if (IDVal.startswith(".att_syntax")) {
2064 getParser().setAssemblerDialect(0);
2065 return false;
2066 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002067 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002068 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2069 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002070 // FIXME : Handle noprefix
2071 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002072 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002073 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002074 }
2075 return false;
2076 }
Chris Lattner537ca842010-10-30 17:38:55 +00002077 return true;
2078}
2079
2080/// ParseDirectiveWord
2081/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002082bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002083 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2084 for (;;) {
2085 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002086 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002087 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002088
Eric Christopher1ced2082013-01-09 03:52:05 +00002089 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002090
Chris Lattner537ca842010-10-30 17:38:55 +00002091 if (getLexer().is(AsmToken::EndOfStatement))
2092 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002093
Chris Lattner537ca842010-10-30 17:38:55 +00002094 // FIXME: Improve diagnostic.
2095 if (getLexer().isNot(AsmToken::Comma))
2096 return Error(L, "unexpected token in directive");
2097 Parser.Lex();
2098 }
2099 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002100
Chris Lattner537ca842010-10-30 17:38:55 +00002101 Parser.Lex();
2102 return false;
2103}
2104
Evan Chengbd27f5a2011-07-27 00:38:12 +00002105/// ParseDirectiveCode
2106/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002107bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002108 if (IDVal == ".code32") {
2109 Parser.Lex();
2110 if (is64BitMode()) {
2111 SwitchMode();
2112 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2113 }
2114 } else if (IDVal == ".code64") {
2115 Parser.Lex();
2116 if (!is64BitMode()) {
2117 SwitchMode();
2118 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2119 }
2120 } else {
2121 return Error(L, "unexpected directive " + IDVal);
2122 }
Chris Lattner537ca842010-10-30 17:38:55 +00002123
Evan Chengbd27f5a2011-07-27 00:38:12 +00002124 return false;
2125}
Chris Lattner537ca842010-10-30 17:38:55 +00002126
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002127// Force static initialization.
2128extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002129 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2130 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002131}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002132
Chris Lattner0692ee62010-09-06 19:11:01 +00002133#define GET_REGISTER_MATCHER
2134#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002135#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002136#include "X86GenAsmMatcher.inc"