blob: 7df8be7607a1fac3654e0fd944be86b21a318f3d [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"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chad Rosier4284e172012-10-24 22:13:37 +000021#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000024#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.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;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000043 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000044 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000045 bool MatchingInlineAsm = false) {
46 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000047 return Parser.Error(L, Msg, Ranges);
48 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000049
Devang Pateld37ad242012-01-17 18:00:18 +000050 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
51 Error(Loc, Msg);
52 return 0;
53 }
54
Chris Lattner309264d2010-01-15 18:44:13 +000055 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000056 X86Operand *ParseATTOperand();
57 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000058 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000059 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000060 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000061 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Chad Rosier5e6b37f2012-10-25 17:37:43 +000063 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
64 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000065
Kevin Enderby9c656452009-09-10 20:51:44 +000066 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000067 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000068
Devang Patelb8ba13f2012-01-18 22:42:29 +000069 bool processInstruction(MCInst &Inst,
70 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
71
Chad Rosier84125ca2012-10-13 00:26:04 +000072 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000073 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000074 MCStreamer &Out, unsigned &ErrorInfo,
75 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000076
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000077 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000078 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000079 bool isSrcOp(X86Operand &Op);
80
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000081 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
82 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000083 bool isDstOp(X86Operand &Op);
84
Evan Cheng59ee62d2011-07-11 03:57:24 +000085 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000086 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000087 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000088 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000089 void SwitchMode() {
90 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
91 setAvailableFeatures(FB);
92 }
Evan Chengebdeeab2011-07-08 01:53:10 +000093
Daniel Dunbar54074b52010-07-19 05:44:09 +000094 /// @name Auto-generated Matcher Functions
95 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000096
Chris Lattner0692ee62010-09-06 19:11:01 +000097#define GET_ASSEMBLER_HEADER
98#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000099
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000100 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000101
102public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000103 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000104 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000105
Daniel Dunbar54074b52010-07-19 05:44:09 +0000106 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000107 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000108 }
Roman Divackybf755322011-01-27 17:14:22 +0000109 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000110
Benjamin Kramer38e59892010-07-14 22:38:02 +0000111 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000112 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000113
114 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000115
116 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000117 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000118 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000119};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000120} // end anonymous namespace
121
Sean Callanane9b466d2010-01-23 00:40:33 +0000122/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000123/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000124
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000125static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000126
127/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000128
Craig Topper76bd9382012-07-18 04:59:16 +0000129static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000130 return (( Value <= 0x000000000000007FULL)||
131 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
132 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
133}
134
135static bool isImmSExti32i8Value(uint64_t Value) {
136 return (( Value <= 0x000000000000007FULL)||
137 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
138 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
139}
140
141static bool isImmZExtu32u8Value(uint64_t Value) {
142 return (Value <= 0x00000000000000FFULL);
143}
144
145static bool isImmSExti64i8Value(uint64_t Value) {
146 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000147 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000148}
149
150static bool isImmSExti64i32Value(uint64_t Value) {
151 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000152 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000153}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000154namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000155
156/// X86Operand - Instances of this class represent a parsed X86 machine
157/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000158struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000159 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000160 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161 Register,
162 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000163 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000164 } Kind;
165
Chris Lattner29ef9a22010-01-15 18:51:29 +0000166 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000167 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000168
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000169 union {
170 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000171 const char *Data;
172 unsigned Length;
173 } Tok;
174
175 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000176 unsigned RegNo;
177 } Reg;
178
179 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000180 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000181 } Imm;
182
183 struct {
184 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000185 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000186 unsigned BaseReg;
187 unsigned IndexReg;
188 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000189 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000190 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000191 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000192 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000193
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000194 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000195 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000196
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000197 /// getStartLoc - Get the location of the first token of this operand.
198 SMLoc getStartLoc() const { return StartLoc; }
199 /// getEndLoc - Get the location of the last token of this operand.
200 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000201 /// getLocRange - Get the range between the first and last token of this
202 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000203 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000204 /// getOffsetOfLoc - Get the location of the offset operator.
205 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000206
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000207 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000208
Daniel Dunbar20927f22009-08-07 08:26:05 +0000209 StringRef getToken() const {
210 assert(Kind == Token && "Invalid access!");
211 return StringRef(Tok.Data, Tok.Length);
212 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000213 void setTokenValue(StringRef Value) {
214 assert(Kind == Token && "Invalid access!");
215 Tok.Data = Value.data();
216 Tok.Length = Value.size();
217 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000218
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000219 unsigned getReg() const {
220 assert(Kind == Register && "Invalid access!");
221 return Reg.RegNo;
222 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000223
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000224 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000225 assert(Kind == Immediate && "Invalid access!");
226 return Imm.Val;
227 }
228
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000229 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000230 assert(Kind == Memory && "Invalid access!");
231 return Mem.Disp;
232 }
233 unsigned getMemSegReg() const {
234 assert(Kind == Memory && "Invalid access!");
235 return Mem.SegReg;
236 }
237 unsigned getMemBaseReg() const {
238 assert(Kind == Memory && "Invalid access!");
239 return Mem.BaseReg;
240 }
241 unsigned getMemIndexReg() const {
242 assert(Kind == Memory && "Invalid access!");
243 return Mem.IndexReg;
244 }
245 unsigned getMemScale() const {
246 assert(Kind == Memory && "Invalid access!");
247 return Mem.Scale;
248 }
249
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000250 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000251
252 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000253
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000254 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000255 if (!isImm())
256 return false;
257
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000258 // If this isn't a constant expr, just assume it fits and let relaxation
259 // handle it.
260 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
261 if (!CE)
262 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000263
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000264 // Otherwise, check the value is in a range that makes sense for this
265 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000266 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000267 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000269 if (!isImm())
270 return false;
271
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000272 // If this isn't a constant expr, just assume it fits and let relaxation
273 // handle it.
274 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
275 if (!CE)
276 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000277
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000278 // Otherwise, check the value is in a range that makes sense for this
279 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000280 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000281 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000282 bool isImmZExtu32u8() const {
283 if (!isImm())
284 return false;
285
286 // If this isn't a constant expr, just assume it fits and let relaxation
287 // handle it.
288 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
289 if (!CE)
290 return true;
291
292 // Otherwise, check the value is in a range that makes sense for this
293 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000294 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000295 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000296 bool isImmSExti64i8() const {
297 if (!isImm())
298 return false;
299
300 // If this isn't a constant expr, just assume it fits and let relaxation
301 // handle it.
302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
303 if (!CE)
304 return true;
305
306 // Otherwise, check the value is in a range that makes sense for this
307 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000308 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000309 }
310 bool isImmSExti64i32() const {
311 if (!isImm())
312 return false;
313
314 // If this isn't a constant expr, just assume it fits and let relaxation
315 // handle it.
316 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
317 if (!CE)
318 return true;
319
320 // Otherwise, check the value is in a range that makes sense for this
321 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000322 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000323 }
324
Chad Rosier96d58e62012-10-19 20:57:14 +0000325 unsigned getMemSize() const {
326 assert(Kind == Memory && "Invalid access!");
327 return Mem.Size;
328 }
329
Chad Rosiera703fb92012-10-22 19:50:35 +0000330 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000331 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000332 }
333
Chad Rosier96d58e62012-10-19 20:57:14 +0000334 bool needSizeDirective() const {
335 assert(Kind == Memory && "Invalid access!");
336 return Mem.NeedSizeDir;
337 }
338
Daniel Dunbar20927f22009-08-07 08:26:05 +0000339 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000340 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000341 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000342 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000343 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000344 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000345 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000346 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000347 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000348 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000349 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000350 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000351 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000352 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000353 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000354 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000355 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000356 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000357 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000358 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000359 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000360 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000361
Craig Topper75dc33a2012-07-18 04:11:12 +0000362 bool isMemVX32() const {
363 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
364 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
365 }
366 bool isMemVY32() const {
367 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
368 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
369 }
370 bool isMemVX64() const {
371 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
372 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
373 }
374 bool isMemVY64() const {
375 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
376 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
377 }
378
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000379 bool isAbsMem() const {
380 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000381 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000382 }
383
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384 bool isReg() const { return Kind == Register; }
385
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000386 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
387 // Add as immediates when possible.
388 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
389 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
390 else
391 Inst.addOperand(MCOperand::CreateExpr(Expr));
392 }
393
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000394 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000395 assert(N == 1 && "Invalid number of operands!");
396 Inst.addOperand(MCOperand::CreateReg(getReg()));
397 }
398
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000399 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000400 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000401 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402 }
403
Chad Rosier36b8fed2012-06-27 22:34:28 +0000404 void addMem8Operands(MCInst &Inst, unsigned N) const {
405 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000406 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000407 void addMem16Operands(MCInst &Inst, unsigned N) const {
408 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000409 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000410 void addMem32Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000412 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000413 void addMem64Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000415 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000416 void addMem80Operands(MCInst &Inst, unsigned N) const {
417 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000418 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000419 void addMem128Operands(MCInst &Inst, unsigned N) const {
420 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000421 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000422 void addMem256Operands(MCInst &Inst, unsigned N) const {
423 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000424 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000425 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
426 addMemOperands(Inst, N);
427 }
428 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
429 addMemOperands(Inst, N);
430 }
431 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
432 addMemOperands(Inst, N);
433 }
434 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
435 addMemOperands(Inst, N);
436 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000437
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000438 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000439 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000440 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
441 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
442 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000443 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000444 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
445 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000446
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000447 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
448 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000449 // Add as immediates when possible.
450 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
451 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
452 else
453 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000454 }
455
Chris Lattnerb4307b32010-01-15 19:28:38 +0000456 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000457 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
458 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000459 Res->Tok.Data = Str.data();
460 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000461 return Res;
462 }
463
Chad Rosierc0a14b82012-10-24 17:22:29 +0000464 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
465 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000466 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000467 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000468 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000469 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000470 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000471
Chris Lattnerb4307b32010-01-15 19:28:38 +0000472 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
473 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000474 Res->Imm.Val = Val;
475 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000476 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000477
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000478 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000479 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
480 unsigned Size = 0, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000481 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
482 Res->Mem.SegReg = 0;
483 Res->Mem.Disp = Disp;
484 Res->Mem.BaseReg = 0;
485 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000486 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000487 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000488 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000489 return Res;
490 }
491
492 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000493 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
494 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000495 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000496 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000497 // We should never just have a displacement, that should be parsed as an
498 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000499 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
500
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000501 // The scale should always be one of {1,2,4,8}.
502 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000503 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000504 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000505 Res->Mem.SegReg = SegReg;
506 Res->Mem.Disp = Disp;
507 Res->Mem.BaseReg = BaseReg;
508 Res->Mem.IndexReg = IndexReg;
509 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000510 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000511 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000512 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000513 }
514};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000515
Chris Lattner37dfdec2009-07-29 06:33:53 +0000516} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000517
Devang Pateldd929fc2012-01-12 18:03:40 +0000518bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000519 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000520
521 return (Op.isMem() &&
522 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
523 isa<MCConstantExpr>(Op.Mem.Disp) &&
524 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
525 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
526}
527
Devang Pateldd929fc2012-01-12 18:03:40 +0000528bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000529 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000530
Chad Rosier36b8fed2012-06-27 22:34:28 +0000531 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000532 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000533 isa<MCConstantExpr>(Op.Mem.Disp) &&
534 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
535 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
536}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000537
Devang Pateldd929fc2012-01-12 18:03:40 +0000538bool X86AsmParser::ParseRegister(unsigned &RegNo,
539 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000540 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000541 const AsmToken &PercentTok = Parser.getTok();
542 StartLoc = PercentTok.getLoc();
543
544 // If we encounter a %, ignore it. This code handles registers with and
545 // without the prefix, unprefixed registers can occur in cfi directives.
546 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000547 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000548
Sean Callanan18b83232010-01-19 21:44:56 +0000549 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000550 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000551 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000552 return Error(StartLoc, "invalid register name",
553 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000554 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000555
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000556 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000557
Chris Lattner33d60d52010-09-22 04:11:10 +0000558 // If the match failed, try the register name as lowercase.
559 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000560 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000561
Evan Cheng5de728c2011-07-27 23:22:03 +0000562 if (!is64BitMode()) {
563 // FIXME: This should be done using Requires<In32BitMode> and
564 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
565 // checked.
566 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
567 // REX prefix.
568 if (RegNo == X86::RIZ ||
569 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
570 X86II::isX86_64NonExtLowByteReg(RegNo) ||
571 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000572 return Error(StartLoc, "register %"
573 + Tok.getString() + " is only available in 64-bit mode",
574 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000575 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000576
Chris Lattner33d60d52010-09-22 04:11:10 +0000577 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
578 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000579 RegNo = X86::ST0;
580 EndLoc = Tok.getLoc();
581 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000582
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000583 // Check to see if we have '(4)' after %st.
584 if (getLexer().isNot(AsmToken::LParen))
585 return false;
586 // Lex the paren.
587 getParser().Lex();
588
589 const AsmToken &IntTok = Parser.getTok();
590 if (IntTok.isNot(AsmToken::Integer))
591 return Error(IntTok.getLoc(), "expected stack index");
592 switch (IntTok.getIntVal()) {
593 case 0: RegNo = X86::ST0; break;
594 case 1: RegNo = X86::ST1; break;
595 case 2: RegNo = X86::ST2; break;
596 case 3: RegNo = X86::ST3; break;
597 case 4: RegNo = X86::ST4; break;
598 case 5: RegNo = X86::ST5; break;
599 case 6: RegNo = X86::ST6; break;
600 case 7: RegNo = X86::ST7; break;
601 default: return Error(IntTok.getLoc(), "invalid stack index");
602 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000603
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000604 if (getParser().Lex().isNot(AsmToken::RParen))
605 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000606
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000607 EndLoc = Tok.getLoc();
608 Parser.Lex(); // Eat ')'
609 return false;
610 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000611
Chris Lattner645b2092010-06-24 07:29:18 +0000612 // If this is "db[0-7]", match it as an alias
613 // for dr[0-7].
614 if (RegNo == 0 && Tok.getString().size() == 3 &&
615 Tok.getString().startswith("db")) {
616 switch (Tok.getString()[2]) {
617 case '0': RegNo = X86::DR0; break;
618 case '1': RegNo = X86::DR1; break;
619 case '2': RegNo = X86::DR2; break;
620 case '3': RegNo = X86::DR3; break;
621 case '4': RegNo = X86::DR4; break;
622 case '5': RegNo = X86::DR5; break;
623 case '6': RegNo = X86::DR6; break;
624 case '7': RegNo = X86::DR7; break;
625 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000626
Chris Lattner645b2092010-06-24 07:29:18 +0000627 if (RegNo != 0) {
628 EndLoc = Tok.getLoc();
629 Parser.Lex(); // Eat it.
630 return false;
631 }
632 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000633
Devang Patel1aea4302012-01-20 22:32:05 +0000634 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000635 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000636 return Error(StartLoc, "invalid register name",
637 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000638 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000639
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000640 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000641 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000642 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000643}
644
Devang Pateldd929fc2012-01-12 18:03:40 +0000645X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000646 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000647 return ParseIntelOperand();
648 return ParseATTOperand();
649}
650
Devang Pateld37ad242012-01-17 18:00:18 +0000651/// getIntelMemOperandSize - Return intel memory operand size.
652static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000653 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000654 .Cases("BYTE", "byte", 8)
655 .Cases("WORD", "word", 16)
656 .Cases("DWORD", "dword", 32)
657 .Cases("QWORD", "qword", 64)
658 .Cases("XWORD", "xword", 80)
659 .Cases("XMMWORD", "xmmword", 128)
660 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000661 .Default(0);
662 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000663}
664
Chad Rosier65c88922012-10-22 19:42:52 +0000665X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000666 unsigned Size) {
667 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000668 const AsmToken &Tok = Parser.getTok();
669 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000670
Chad Rosier4284e172012-10-24 22:13:37 +0000671 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000672 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
673
674 // Eat '['
675 if (getLexer().isNot(AsmToken::LBrac))
676 return ErrorOperand(Start, "Expected '[' token!");
677 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000678
Devang Pateld37ad242012-01-17 18:00:18 +0000679 if (getLexer().is(AsmToken::Identifier)) {
680 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000681 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000682 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000683 if (getParser().ParseExpression(Disp, End)) return 0;
684 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000685 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000686 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000687 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000688 }
689 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000690 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000691 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000692 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000693 if (getLexer().is(AsmToken::RBrac)) {
694 // Handle '[' number ']'
695 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000696 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
697 if (SegReg)
698 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000699 Start, End, Size);
700 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000701 } else if (getLexer().is(AsmToken::Star)) {
702 // Handle '[' Scale*IndexReg ']'
703 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000704 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000705 if (ParseRegister(IndexReg, IdxRegLoc, End))
706 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000707 Scale = Val;
708 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000709 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000710 }
711
712 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
713 bool isPlus = getLexer().is(AsmToken::Plus);
714 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000715 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000716 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000717 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000718 Parser.Lex();
719 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000720 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000721 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000722 if (ParseRegister(IndexReg, IdxRegLoc, End))
723 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000724 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000725 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000726 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000727 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000728 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000729 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000730 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000731 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000732 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000733 if (!IndexReg)
734 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000735 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000736 }
Devang Pateld37ad242012-01-17 18:00:18 +0000737 }
738
739 if (getLexer().isNot(AsmToken::RBrac))
740 if (getParser().ParseExpression(Disp, End)) return 0;
741
Chad Rosier4284e172012-10-24 22:13:37 +0000742 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000743 if (getLexer().isNot(AsmToken::RBrac))
744 return ErrorOperand(End, "expected ']' token!");
745 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000746 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000747
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000748 if (Tok.getString().startswith(".")) {
749 SmallString<64> Err;
750 const MCExpr *NewDisp;
751 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
752 return ErrorOperand(Tok.getLoc(), Err);
753
754 Parser.Lex(); // Eat the field.
755 Disp = NewDisp;
756 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000757
758 End = Tok.getLoc();
759
Devang Patelfdd3b302012-01-20 21:21:01 +0000760 // handle [-42]
761 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000762 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000763
Devang Pateld37ad242012-01-17 18:00:18 +0000764 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000765 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000766}
767
768/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000769X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000770 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000771 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000772
773 unsigned Size = getIntelMemOperandSize(Tok.getString());
774 if (Size) {
775 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000776 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
777 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000778 Parser.Lex();
779 }
780
Chad Rosierc0a14b82012-10-24 17:22:29 +0000781 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000782 return ParseIntelBracExpression(SegReg, Size);
783
784 if (!ParseRegister(SegReg, Start, End)) {
785 // Handel SegReg : [ ... ]
786 if (getLexer().isNot(AsmToken::Colon))
787 return ErrorOperand(Start, "Expected ':' token!");
788 Parser.Lex(); // Eat :
789 if (getLexer().isNot(AsmToken::LBrac))
790 return ErrorOperand(Start, "Expected '[' token!");
791 return ParseIntelBracExpression(SegReg, Size);
792 }
Devang Pateld37ad242012-01-17 18:00:18 +0000793
794 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
795 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000796 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000797
798 bool NeedSizeDir = false;
799 if (!Size && isParsingInlineAsm()) {
800 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
801 const MCSymbol &Sym = SymRef->getSymbol();
802 // FIXME: The SemaLookup will fail if the name is anything other then an
803 // identifier.
804 // FIXME: Pass a valid SMLoc.
805 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
806 NeedSizeDir = Size > 0;
807 }
808 }
Chad Rosier2a784132012-10-23 23:31:33 +0000809 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000810 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000811 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000812 // When parsing inline assembly we set the base register to a non-zero value
813 // as we don't know the actual value at this time. This is necessary to
814 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000815 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000816 /*Scale*/1, Start, End, Size, NeedSizeDir);
817}
818
Chad Rosier22f441a2012-10-24 22:21:50 +0000819/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000820bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
821 const MCExpr **NewDisp,
822 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +0000823 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000824 uint64_t OrigDispVal, DotDispVal;
825
826 // FIXME: Handle non-constant expressions.
827 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
828 OrigDispVal = OrigDisp->getValue();
829 } else {
830 Err = "Non-constant offsets are not supported!";
831 return true;
832 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000833
834 // Drop the '.'.
835 StringRef DotDispStr = Tok.getString().drop_front(1);
836
Chad Rosier22f441a2012-10-24 22:21:50 +0000837 // .Imm gets lexed as a real.
838 if (Tok.is(AsmToken::Real)) {
839 APInt DotDisp;
840 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000841 DotDispVal = DotDisp.getZExtValue();
842 } else {
843 Err = "Unexpected token type!";
844 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000845 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000846
847 // Special case zero dot displacement.
848 if (!DotDispVal) {
849 *NewDisp = Disp;
850 return false;
851 }
852
853 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
854 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000855}
856
Chad Rosierc0a14b82012-10-24 17:22:29 +0000857/// Parse the 'offset' operator. This operator is used to specify the
858/// location rather then the content of a variable.
859X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
860 SMLoc OffsetOfLoc = Start;
861 Parser.Lex(); // Eat offset.
862 Start = Parser.getTok().getLoc();
863 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
864
865 SMLoc End;
866 const MCExpr *Val;
867 if (getParser().ParseExpression(Val, End))
868 return 0;
869
870 End = Parser.getTok().getLoc();
871
872 // The offset operator will have an 'r' constraint, thus we need to create
873 // register operand to ensure proper matching. Just pick a GPR based on
874 // the size of a pointer.
875 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
876 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000877}
878
879X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000880 SMLoc Start = Parser.getTok().getLoc(), End;
881
Chad Rosierc0a14b82012-10-24 17:22:29 +0000882 // offset operator.
883 const AsmToken &Tok = Parser.getTok();
884 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
885 isParsingInlineAsm())
886 return ParseIntelOffsetOfOperator(Start);
887
Devang Pateld37ad242012-01-17 18:00:18 +0000888 // immediate.
889 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
890 getLexer().is(AsmToken::Minus)) {
891 const MCExpr *Val;
892 if (!getParser().ParseExpression(Val, End)) {
893 End = Parser.getTok().getLoc();
894 return X86Operand::CreateImm(Val, Start, End);
895 }
896 }
897
Devang Patel0a338862012-01-12 01:36:43 +0000898 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000899 unsigned RegNo = 0;
900 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000901 // If this is a segment register followed by a ':', then this is the start
902 // of a memory reference, otherwise this is a normal register reference.
903 if (getLexer().isNot(AsmToken::Colon))
904 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
905
906 getParser().Lex(); // Eat the colon.
907 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000908 }
909
910 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000911 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000912}
913
Devang Pateldd929fc2012-01-12 18:03:40 +0000914X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000915 switch (getLexer().getKind()) {
916 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000917 // Parse a memory operand with no segment register.
918 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000919 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000920 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000921 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000922 SMLoc Start, End;
923 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000924 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000925 Error(Start, "%eiz and %riz can only be used as index registers",
926 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000927 return 0;
928 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000929
Chris Lattnereef6d782010-04-17 18:56:34 +0000930 // If this is a segment register followed by a ':', then this is the start
931 // of a memory reference, otherwise this is a normal register reference.
932 if (getLexer().isNot(AsmToken::Colon))
933 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000934
935
Chris Lattnereef6d782010-04-17 18:56:34 +0000936 getParser().Lex(); // Eat the colon.
937 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000938 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000939 case AsmToken::Dollar: {
940 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000941 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000942 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000943 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000944 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000945 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000946 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000947 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000948 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000949}
950
Chris Lattnereef6d782010-04-17 18:56:34 +0000951/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
952/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000953X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000954
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000955 // We have to disambiguate a parenthesized expression "(4+5)" from the start
956 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000957 // only way to do this without lookahead is to eat the '(' and see what is
958 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000959 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000960 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000961 SMLoc ExprEnd;
962 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000963
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000964 // After parsing the base expression we could either have a parenthesized
965 // memory address or not. If not, return now. If so, eat the (.
966 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000967 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000968 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000969 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000970 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000971 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000972
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000973 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000974 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000975 } else {
976 // Okay, we have a '('. We don't know if this is an expression or not, but
977 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000978 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000979 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000980
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000981 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000982 // Nothing to do here, fall into the code below with the '(' part of the
983 // memory operand consumed.
984 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000985 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000986
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000987 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000988 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000989 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000990
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000991 // After parsing the base expression we could either have a parenthesized
992 // memory address or not. If not, return now. If so, eat the (.
993 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000994 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000995 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000996 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000997 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000998 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000999
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001000 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001001 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001002 }
1003 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001004
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001005 // If we reached here, then we just ate the ( of the memory operand. Process
1006 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001007 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001008 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001009
Chris Lattner29ef9a22010-01-15 18:51:29 +00001010 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001011 SMLoc StartLoc, EndLoc;
1012 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001013 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001014 Error(StartLoc, "eiz and riz can only be used as index registers",
1015 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001016 return 0;
1017 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001018 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001019
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001020 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001021 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001022 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001023
1024 // Following the comma we should have either an index register, or a scale
1025 // value. We don't support the later form, but we want to parse it
1026 // correctly.
1027 //
1028 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001029 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001030 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001031 SMLoc L;
1032 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001033
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001034 if (getLexer().isNot(AsmToken::RParen)) {
1035 // Parse the scale amount:
1036 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001037 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001038 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001039 "expected comma in scale expression");
1040 return 0;
1041 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001042 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001043
1044 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001045 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001046
1047 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001048 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1049 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001050 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001051 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001052
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001053 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001054 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1055 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1056 return 0;
1057 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001058 Scale = (unsigned)ScaleVal;
1059 }
1060 }
1061 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001062 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001063 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001064 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001065
1066 int64_t Value;
1067 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001068 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001069
Daniel Dunbaree910252010-08-24 19:13:38 +00001070 if (Value != 1)
1071 Warning(Loc, "scale factor without index register is ignored");
1072 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001073 }
1074 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001075
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001076 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001077 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001078 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001079 return 0;
1080 }
Sean Callanan18b83232010-01-19 21:44:56 +00001081 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001082 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001083
Kevin Enderby84faf652012-03-12 21:32:09 +00001084 // If we have both a base register and an index register make sure they are
1085 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001086 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001087 if (BaseReg != 0 && IndexReg != 0) {
1088 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001089 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1090 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001091 IndexReg != X86::RIZ) {
1092 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1093 return 0;
1094 }
1095 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001096 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1097 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001098 IndexReg != X86::EIZ){
1099 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1100 return 0;
1101 }
1102 }
1103
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001104 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1105 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001106}
1107
Devang Pateldd929fc2012-01-12 18:03:40 +00001108bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001109ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001110 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001111 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001112
Chris Lattnerd8f71792010-11-28 20:23:50 +00001113 // FIXME: Hack to recognize setneb as setne.
1114 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1115 PatchedName != "setb" && PatchedName != "setnb")
1116 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001117
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001118 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1119 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001120 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001121 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1122 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001123 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001124 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001125 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001126 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001127 .Case("eq", 0x00)
1128 .Case("lt", 0x01)
1129 .Case("le", 0x02)
1130 .Case("unord", 0x03)
1131 .Case("neq", 0x04)
1132 .Case("nlt", 0x05)
1133 .Case("nle", 0x06)
1134 .Case("ord", 0x07)
1135 /* AVX only from here */
1136 .Case("eq_uq", 0x08)
1137 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001138 .Case("ngt", 0x0A)
1139 .Case("false", 0x0B)
1140 .Case("neq_oq", 0x0C)
1141 .Case("ge", 0x0D)
1142 .Case("gt", 0x0E)
1143 .Case("true", 0x0F)
1144 .Case("eq_os", 0x10)
1145 .Case("lt_oq", 0x11)
1146 .Case("le_oq", 0x12)
1147 .Case("unord_s", 0x13)
1148 .Case("neq_us", 0x14)
1149 .Case("nlt_uq", 0x15)
1150 .Case("nle_uq", 0x16)
1151 .Case("ord_s", 0x17)
1152 .Case("eq_us", 0x18)
1153 .Case("nge_uq", 0x19)
1154 .Case("ngt_uq", 0x1A)
1155 .Case("false_os", 0x1B)
1156 .Case("neq_os", 0x1C)
1157 .Case("ge_oq", 0x1D)
1158 .Case("gt_oq", 0x1E)
1159 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001160 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001161 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001162 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1163 getParser().getContext());
1164 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001165 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001166 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001167 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001168 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001169 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001170 } else {
1171 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001172 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001173 }
1174 }
1175 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001176
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001177 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001178
Devang Patel885f65b2012-01-30 22:47:12 +00001179 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001180 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001181
Chris Lattner2544f422010-09-08 05:17:37 +00001182 // Determine whether this is an instruction prefix.
1183 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001184 Name == "lock" || Name == "rep" ||
1185 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001186 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001187 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001188
1189
Chris Lattner2544f422010-09-08 05:17:37 +00001190 // This does the actual operand parsing. Don't parse any more if we have a
1191 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1192 // just want to parse the "lock" as the first instruction and the "incl" as
1193 // the next one.
1194 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001195
1196 // Parse '*' modifier.
1197 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001198 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001199 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001200 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001201 }
1202
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001203 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001204 if (X86Operand *Op = ParseOperand())
1205 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001206 else {
1207 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001208 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001209 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001210
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001211 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001212 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001213
1214 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001215 if (X86Operand *Op = ParseOperand())
1216 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001217 else {
1218 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001219 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001220 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001221 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001222
Chris Lattnercbf8a982010-09-11 16:18:25 +00001223 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001224 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001225 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001226 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001227 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001228 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001229
Chris Lattner2544f422010-09-08 05:17:37 +00001230 if (getLexer().is(AsmToken::EndOfStatement))
1231 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001232 else if (isPrefix && getLexer().is(AsmToken::Slash))
1233 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001234
Devang Patel885f65b2012-01-30 22:47:12 +00001235 if (ExtraImmOp && isParsingIntelSyntax())
1236 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1237
Chris Lattner98c870f2010-11-06 19:25:43 +00001238 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1239 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1240 // documented form in various unofficial manuals, so a lot of code uses it.
1241 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1242 Operands.size() == 3) {
1243 X86Operand &Op = *(X86Operand*)Operands.back();
1244 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1245 isa<MCConstantExpr>(Op.Mem.Disp) &&
1246 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1247 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1248 SMLoc Loc = Op.getEndLoc();
1249 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1250 delete &Op;
1251 }
1252 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001253 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1254 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1255 Operands.size() == 3) {
1256 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1257 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1258 isa<MCConstantExpr>(Op.Mem.Disp) &&
1259 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1260 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1261 SMLoc Loc = Op.getEndLoc();
1262 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1263 delete &Op;
1264 }
1265 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001266 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1267 if (Name.startswith("ins") && Operands.size() == 3 &&
1268 (Name == "insb" || Name == "insw" || Name == "insl")) {
1269 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1270 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1271 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1272 Operands.pop_back();
1273 Operands.pop_back();
1274 delete &Op;
1275 delete &Op2;
1276 }
1277 }
1278
1279 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1280 if (Name.startswith("outs") && Operands.size() == 3 &&
1281 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1282 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1283 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1284 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1285 Operands.pop_back();
1286 Operands.pop_back();
1287 delete &Op;
1288 delete &Op2;
1289 }
1290 }
1291
1292 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1293 if (Name.startswith("movs") && Operands.size() == 3 &&
1294 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001295 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001296 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1297 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1298 if (isSrcOp(Op) && isDstOp(Op2)) {
1299 Operands.pop_back();
1300 Operands.pop_back();
1301 delete &Op;
1302 delete &Op2;
1303 }
1304 }
1305 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1306 if (Name.startswith("lods") && Operands.size() == 3 &&
1307 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001308 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001309 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1310 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1311 if (isSrcOp(*Op1) && Op2->isReg()) {
1312 const char *ins;
1313 unsigned reg = Op2->getReg();
1314 bool isLods = Name == "lods";
1315 if (reg == X86::AL && (isLods || Name == "lodsb"))
1316 ins = "lodsb";
1317 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1318 ins = "lodsw";
1319 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1320 ins = "lodsl";
1321 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1322 ins = "lodsq";
1323 else
1324 ins = NULL;
1325 if (ins != NULL) {
1326 Operands.pop_back();
1327 Operands.pop_back();
1328 delete Op1;
1329 delete Op2;
1330 if (Name != ins)
1331 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1332 }
1333 }
1334 }
1335 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1336 if (Name.startswith("stos") && Operands.size() == 3 &&
1337 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001338 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001339 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1340 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1341 if (isDstOp(*Op2) && Op1->isReg()) {
1342 const char *ins;
1343 unsigned reg = Op1->getReg();
1344 bool isStos = Name == "stos";
1345 if (reg == X86::AL && (isStos || Name == "stosb"))
1346 ins = "stosb";
1347 else if (reg == X86::AX && (isStos || Name == "stosw"))
1348 ins = "stosw";
1349 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1350 ins = "stosl";
1351 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1352 ins = "stosq";
1353 else
1354 ins = NULL;
1355 if (ins != NULL) {
1356 Operands.pop_back();
1357 Operands.pop_back();
1358 delete Op1;
1359 delete Op2;
1360 if (Name != ins)
1361 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1362 }
1363 }
1364 }
1365
Chris Lattnere9e16a32010-09-15 04:33:27 +00001366 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001367 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001368 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001369 Name.startswith("shl") || Name.startswith("sal") ||
1370 Name.startswith("rcl") || Name.startswith("rcr") ||
1371 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001372 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001373 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001374 // Intel syntax
1375 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1376 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001377 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1378 delete Operands[2];
1379 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001380 }
1381 } else {
1382 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1383 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001384 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1385 delete Operands[1];
1386 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001387 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001388 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001389 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001390
Chris Lattner15f89512011-04-09 19:41:05 +00001391 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1392 // instalias with an immediate operand yet.
1393 if (Name == "int" && Operands.size() == 2) {
1394 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1395 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1396 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1397 delete Operands[1];
1398 Operands.erase(Operands.begin() + 1);
1399 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1400 }
1401 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001402
Chris Lattner98986712010-01-14 22:21:20 +00001403 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001404}
1405
Devang Pateldd929fc2012-01-12 18:03:40 +00001406bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001407processInstruction(MCInst &Inst,
1408 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1409 switch (Inst.getOpcode()) {
1410 default: return false;
1411 case X86::AND16i16: {
1412 if (!Inst.getOperand(0).isImm() ||
1413 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1414 return false;
1415
1416 MCInst TmpInst;
1417 TmpInst.setOpcode(X86::AND16ri8);
1418 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1419 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1420 TmpInst.addOperand(Inst.getOperand(0));
1421 Inst = TmpInst;
1422 return true;
1423 }
1424 case X86::AND32i32: {
1425 if (!Inst.getOperand(0).isImm() ||
1426 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1427 return false;
1428
1429 MCInst TmpInst;
1430 TmpInst.setOpcode(X86::AND32ri8);
1431 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1432 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1433 TmpInst.addOperand(Inst.getOperand(0));
1434 Inst = TmpInst;
1435 return true;
1436 }
1437 case X86::AND64i32: {
1438 if (!Inst.getOperand(0).isImm() ||
1439 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1440 return false;
1441
1442 MCInst TmpInst;
1443 TmpInst.setOpcode(X86::AND64ri8);
1444 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1445 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1446 TmpInst.addOperand(Inst.getOperand(0));
1447 Inst = TmpInst;
1448 return true;
1449 }
Devang Patelac0f0482012-01-19 17:53:25 +00001450 case X86::XOR16i16: {
1451 if (!Inst.getOperand(0).isImm() ||
1452 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1453 return false;
1454
1455 MCInst TmpInst;
1456 TmpInst.setOpcode(X86::XOR16ri8);
1457 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1458 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1459 TmpInst.addOperand(Inst.getOperand(0));
1460 Inst = TmpInst;
1461 return true;
1462 }
1463 case X86::XOR32i32: {
1464 if (!Inst.getOperand(0).isImm() ||
1465 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1466 return false;
1467
1468 MCInst TmpInst;
1469 TmpInst.setOpcode(X86::XOR32ri8);
1470 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1471 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1472 TmpInst.addOperand(Inst.getOperand(0));
1473 Inst = TmpInst;
1474 return true;
1475 }
1476 case X86::XOR64i32: {
1477 if (!Inst.getOperand(0).isImm() ||
1478 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1479 return false;
1480
1481 MCInst TmpInst;
1482 TmpInst.setOpcode(X86::XOR64ri8);
1483 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1484 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1485 TmpInst.addOperand(Inst.getOperand(0));
1486 Inst = TmpInst;
1487 return true;
1488 }
1489 case X86::OR16i16: {
1490 if (!Inst.getOperand(0).isImm() ||
1491 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1492 return false;
1493
1494 MCInst TmpInst;
1495 TmpInst.setOpcode(X86::OR16ri8);
1496 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1497 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1498 TmpInst.addOperand(Inst.getOperand(0));
1499 Inst = TmpInst;
1500 return true;
1501 }
1502 case X86::OR32i32: {
1503 if (!Inst.getOperand(0).isImm() ||
1504 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1505 return false;
1506
1507 MCInst TmpInst;
1508 TmpInst.setOpcode(X86::OR32ri8);
1509 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1510 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1511 TmpInst.addOperand(Inst.getOperand(0));
1512 Inst = TmpInst;
1513 return true;
1514 }
1515 case X86::OR64i32: {
1516 if (!Inst.getOperand(0).isImm() ||
1517 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1518 return false;
1519
1520 MCInst TmpInst;
1521 TmpInst.setOpcode(X86::OR64ri8);
1522 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1523 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1524 TmpInst.addOperand(Inst.getOperand(0));
1525 Inst = TmpInst;
1526 return true;
1527 }
1528 case X86::CMP16i16: {
1529 if (!Inst.getOperand(0).isImm() ||
1530 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1531 return false;
1532
1533 MCInst TmpInst;
1534 TmpInst.setOpcode(X86::CMP16ri8);
1535 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1536 TmpInst.addOperand(Inst.getOperand(0));
1537 Inst = TmpInst;
1538 return true;
1539 }
1540 case X86::CMP32i32: {
1541 if (!Inst.getOperand(0).isImm() ||
1542 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1543 return false;
1544
1545 MCInst TmpInst;
1546 TmpInst.setOpcode(X86::CMP32ri8);
1547 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1548 TmpInst.addOperand(Inst.getOperand(0));
1549 Inst = TmpInst;
1550 return true;
1551 }
1552 case X86::CMP64i32: {
1553 if (!Inst.getOperand(0).isImm() ||
1554 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1555 return false;
1556
1557 MCInst TmpInst;
1558 TmpInst.setOpcode(X86::CMP64ri8);
1559 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1560 TmpInst.addOperand(Inst.getOperand(0));
1561 Inst = TmpInst;
1562 return true;
1563 }
Devang Patela951f772012-01-19 18:40:55 +00001564 case X86::ADD16i16: {
1565 if (!Inst.getOperand(0).isImm() ||
1566 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1567 return false;
1568
1569 MCInst TmpInst;
1570 TmpInst.setOpcode(X86::ADD16ri8);
1571 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1572 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1573 TmpInst.addOperand(Inst.getOperand(0));
1574 Inst = TmpInst;
1575 return true;
1576 }
1577 case X86::ADD32i32: {
1578 if (!Inst.getOperand(0).isImm() ||
1579 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1580 return false;
1581
1582 MCInst TmpInst;
1583 TmpInst.setOpcode(X86::ADD32ri8);
1584 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1585 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1586 TmpInst.addOperand(Inst.getOperand(0));
1587 Inst = TmpInst;
1588 return true;
1589 }
1590 case X86::ADD64i32: {
1591 if (!Inst.getOperand(0).isImm() ||
1592 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1593 return false;
1594
1595 MCInst TmpInst;
1596 TmpInst.setOpcode(X86::ADD64ri8);
1597 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1598 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1599 TmpInst.addOperand(Inst.getOperand(0));
1600 Inst = TmpInst;
1601 return true;
1602 }
1603 case X86::SUB16i16: {
1604 if (!Inst.getOperand(0).isImm() ||
1605 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1606 return false;
1607
1608 MCInst TmpInst;
1609 TmpInst.setOpcode(X86::SUB16ri8);
1610 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1611 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1612 TmpInst.addOperand(Inst.getOperand(0));
1613 Inst = TmpInst;
1614 return true;
1615 }
1616 case X86::SUB32i32: {
1617 if (!Inst.getOperand(0).isImm() ||
1618 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1619 return false;
1620
1621 MCInst TmpInst;
1622 TmpInst.setOpcode(X86::SUB32ri8);
1623 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1624 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1625 TmpInst.addOperand(Inst.getOperand(0));
1626 Inst = TmpInst;
1627 return true;
1628 }
1629 case X86::SUB64i32: {
1630 if (!Inst.getOperand(0).isImm() ||
1631 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1632 return false;
1633
1634 MCInst TmpInst;
1635 TmpInst.setOpcode(X86::SUB64ri8);
1636 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1637 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1638 TmpInst.addOperand(Inst.getOperand(0));
1639 Inst = TmpInst;
1640 return true;
1641 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001642 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001643}
1644
1645bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001646MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001647 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001648 MCStreamer &Out, unsigned &ErrorInfo,
1649 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001650 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001651 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1652 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001653 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001654
Chris Lattner7c51a312010-09-29 01:50:45 +00001655 // First, handle aliases that expand to multiple instructions.
1656 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001657 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001658 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001659 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001660 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001661 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001662 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001663 MCInst Inst;
1664 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001665 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001666 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001667 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001668
Chris Lattner0bb83a82010-09-30 16:39:29 +00001669 const char *Repl =
1670 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001671 .Case("finit", "fninit")
1672 .Case("fsave", "fnsave")
1673 .Case("fstcw", "fnstcw")
1674 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001675 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001676 .Case("fstsw", "fnstsw")
1677 .Case("fstsww", "fnstsw")
1678 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001679 .Default(0);
1680 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001681 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001682 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001683 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001684
Chris Lattnera008e8a2010-09-06 21:54:15 +00001685 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001686 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001687
Daniel Dunbarc918d602010-05-04 16:12:42 +00001688 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001689 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001690 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001691 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001692 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001693 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001694 // Some instructions need post-processing to, for example, tweak which
1695 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001696 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001697 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001698 while (processInstruction(Inst, Operands))
1699 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001700
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001701 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001702 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001703 Out.EmitInstruction(Inst);
1704 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001705 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001706 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001707 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001708 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001709 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001710 case Match_InvalidOperand:
1711 WasOriginallyInvalidOperand = true;
1712 break;
1713 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001714 break;
1715 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001716
Daniel Dunbarc918d602010-05-04 16:12:42 +00001717 // FIXME: Ideally, we would only attempt suffix matches for things which are
1718 // valid prefixes, and we could just infer the right unambiguous
1719 // type. However, that requires substantially more matcher support than the
1720 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001721
Daniel Dunbarc918d602010-05-04 16:12:42 +00001722 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001723 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001724 SmallString<16> Tmp;
1725 Tmp += Base;
1726 Tmp += ' ';
1727 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001728
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001729 // If this instruction starts with an 'f', then it is a floating point stack
1730 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1731 // 80-bit floating point, which use the suffixes s,l,t respectively.
1732 //
1733 // Otherwise, we assume that this may be an integer instruction, which comes
1734 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1735 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001736
Daniel Dunbarc918d602010-05-04 16:12:42 +00001737 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001738 Tmp[Base.size()] = Suffixes[0];
1739 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001740 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001741
Chad Rosier6e006d32012-10-12 22:53:36 +00001742 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1743 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001744 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001745 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1746 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001747 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001748 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1749 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001750 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001751 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1752 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001753
1754 // Restore the old token.
1755 Op->setTokenValue(Base);
1756
1757 // If exactly one matched, then we treat that as a successful match (and the
1758 // instruction will already have been filled in correctly, since the failing
1759 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001760 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001761 (Match1 == Match_Success) + (Match2 == Match_Success) +
1762 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001763 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001764 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001765 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001766 Out.EmitInstruction(Inst);
1767 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001768 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001769 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001770
Chris Lattnerec6789f2010-09-06 20:08:02 +00001771 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001772
Daniel Dunbar09062b12010-08-12 00:55:42 +00001773 // If we had multiple suffix matches, then identify this as an ambiguous
1774 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001775 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001776 char MatchChars[4];
1777 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001778 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1779 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1780 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1781 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001782
1783 SmallString<126> Msg;
1784 raw_svector_ostream OS(Msg);
1785 OS << "ambiguous instructions require an explicit suffix (could be ";
1786 for (unsigned i = 0; i != NumMatches; ++i) {
1787 if (i != 0)
1788 OS << ", ";
1789 if (i + 1 == NumMatches)
1790 OS << "or ";
1791 OS << "'" << Base << MatchChars[i] << "'";
1792 }
1793 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001794 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001795 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001796 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797
Chris Lattnera008e8a2010-09-06 21:54:15 +00001798 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001799
Chris Lattnera008e8a2010-09-06 21:54:15 +00001800 // If all of the instructions reported an invalid mnemonic, then the original
1801 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001802 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1803 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001804 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001805 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001806 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001807 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001808 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001809 }
1810
1811 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001812 if (ErrorInfo != ~0U) {
1813 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001814 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001815 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001816
Chad Rosier84125ca2012-10-13 00:26:04 +00001817 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001818 if (Operand->getStartLoc().isValid()) {
1819 SMRange OperandRange = Operand->getLocRange();
1820 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001821 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001822 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001823 }
1824
Chad Rosierb4fdade2012-08-21 19:36:59 +00001825 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001826 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001827 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001828
Chris Lattnerec6789f2010-09-06 20:08:02 +00001829 // If one instruction matched with a missing feature, report this as a
1830 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001831 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1832 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001833 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001834 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001835 return true;
1836 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001837
Chris Lattnera008e8a2010-09-06 21:54:15 +00001838 // If one instruction matched with an invalid operand, report this as an
1839 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001840 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1841 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001842 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001843 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001844 return true;
1845 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001846
Chris Lattnerec6789f2010-09-06 20:08:02 +00001847 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001848 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001849 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001850 return true;
1851}
1852
1853
Devang Pateldd929fc2012-01-12 18:03:40 +00001854bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001855 StringRef IDVal = DirectiveID.getIdentifier();
1856 if (IDVal == ".word")
1857 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001858 else if (IDVal.startswith(".code"))
1859 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001860 else if (IDVal.startswith(".att_syntax")) {
1861 getParser().setAssemblerDialect(0);
1862 return false;
1863 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001864 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001865 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1866 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001867 // FIXME : Handle noprefix
1868 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001869 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001870 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001871 }
1872 return false;
1873 }
Chris Lattner537ca842010-10-30 17:38:55 +00001874 return true;
1875}
1876
1877/// ParseDirectiveWord
1878/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001879bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001880 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1881 for (;;) {
1882 const MCExpr *Value;
1883 if (getParser().ParseExpression(Value))
1884 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001885
Chris Lattner537ca842010-10-30 17:38:55 +00001886 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001887
Chris Lattner537ca842010-10-30 17:38:55 +00001888 if (getLexer().is(AsmToken::EndOfStatement))
1889 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001890
Chris Lattner537ca842010-10-30 17:38:55 +00001891 // FIXME: Improve diagnostic.
1892 if (getLexer().isNot(AsmToken::Comma))
1893 return Error(L, "unexpected token in directive");
1894 Parser.Lex();
1895 }
1896 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001897
Chris Lattner537ca842010-10-30 17:38:55 +00001898 Parser.Lex();
1899 return false;
1900}
1901
Evan Chengbd27f5a2011-07-27 00:38:12 +00001902/// ParseDirectiveCode
1903/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001904bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001905 if (IDVal == ".code32") {
1906 Parser.Lex();
1907 if (is64BitMode()) {
1908 SwitchMode();
1909 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1910 }
1911 } else if (IDVal == ".code64") {
1912 Parser.Lex();
1913 if (!is64BitMode()) {
1914 SwitchMode();
1915 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1916 }
1917 } else {
1918 return Error(L, "unexpected directive " + IDVal);
1919 }
Chris Lattner537ca842010-10-30 17:38:55 +00001920
Evan Chengbd27f5a2011-07-27 00:38:12 +00001921 return false;
1922}
Chris Lattner537ca842010-10-30 17:38:55 +00001923
1924
Sean Callanane88f5522010-01-23 02:43:15 +00001925extern "C" void LLVMInitializeX86AsmLexer();
1926
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001927// Force static initialization.
1928extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001929 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1930 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001931 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001932}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001933
Chris Lattner0692ee62010-09-06 19:11:01 +00001934#define GET_REGISTER_MATCHER
1935#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001936#include "X86GenAsmMatcher.inc"