blob: 70f6ef766f88d944c8613ad8fa86af1e460b1f6c [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"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &getParser() const { return Parser; }
39
40 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
41
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000042 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000043 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000044 bool MatchingInlineAsm = false) {
45 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000046 return Parser.Error(L, Msg, Ranges);
47 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048
Devang Pateld37ad242012-01-17 18:00:18 +000049 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
50 Error(Loc, Msg);
51 return 0;
52 }
53
Chris Lattner309264d2010-01-15 18:44:13 +000054 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000055 X86Operand *ParseATTOperand();
56 X86Operand *ParseIntelOperand();
Chad Rosier5b0f1b32012-10-04 23:59:38 +000057 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000058 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000059 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000060
61 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000062 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000063
Devang Patelb8ba13f2012-01-18 22:42:29 +000064 bool processInstruction(MCInst &Inst,
65 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
66
Chad Rosier84125ca2012-10-13 00:26:04 +000067 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000068 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000069 MCStreamer &Out, unsigned &ErrorInfo,
70 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000071
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000072 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000073 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000074 bool isSrcOp(X86Operand &Op);
75
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000076 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
77 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 bool isDstOp(X86Operand &Op);
79
Evan Cheng59ee62d2011-07-11 03:57:24 +000080 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000081 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000082 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000083 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000084 void SwitchMode() {
85 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
86 setAvailableFeatures(FB);
87 }
Evan Chengebdeeab2011-07-08 01:53:10 +000088
Daniel Dunbar54074b52010-07-19 05:44:09 +000089 /// @name Auto-generated Matcher Functions
90 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000091
Chris Lattner0692ee62010-09-06 19:11:01 +000092#define GET_ASSEMBLER_HEADER
93#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000094
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000095 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000096
97public:
Devang Pateldd929fc2012-01-12 18:03:40 +000098 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000099 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000100
Daniel Dunbar54074b52010-07-19 05:44:09 +0000101 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000102 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000103 }
Roman Divackybf755322011-01-27 17:14:22 +0000104 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000105
Benjamin Kramer38e59892010-07-14 22:38:02 +0000106 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000107 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000108
109 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000110
111 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000112 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000113 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000114};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000115} // end anonymous namespace
116
Sean Callanane9b466d2010-01-23 00:40:33 +0000117/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000118/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000119
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000120static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000121
122/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000123
Craig Topper76bd9382012-07-18 04:59:16 +0000124static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000125 return (( Value <= 0x000000000000007FULL)||
126 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
127 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
128}
129
130static bool isImmSExti32i8Value(uint64_t Value) {
131 return (( Value <= 0x000000000000007FULL)||
132 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
133 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
134}
135
136static bool isImmZExtu32u8Value(uint64_t Value) {
137 return (Value <= 0x00000000000000FFULL);
138}
139
140static bool isImmSExti64i8Value(uint64_t Value) {
141 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000142 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000143}
144
145static bool isImmSExti64i32Value(uint64_t Value) {
146 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000147 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000148}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000149namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000150
151/// X86Operand - Instances of this class represent a parsed X86 machine
152/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000153struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000154 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000155 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000156 Register,
157 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000158 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000159 } Kind;
160
Chris Lattner29ef9a22010-01-15 18:51:29 +0000161 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000162
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 union {
164 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000165 const char *Data;
166 unsigned Length;
167 } Tok;
168
169 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000170 unsigned RegNo;
171 } Reg;
172
173 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000174 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000175 } Imm;
176
177 struct {
178 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000179 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000180 unsigned BaseReg;
181 unsigned IndexReg;
182 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000183 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000184 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000185 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000186 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000187
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000188 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000189 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000190
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000191 /// getStartLoc - Get the location of the first token of this operand.
192 SMLoc getStartLoc() const { return StartLoc; }
193 /// getEndLoc - Get the location of the last token of this operand.
194 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000195 /// getLocRange - Get the range between the first and last token of this
196 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000197 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000198
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000199 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000200
Daniel Dunbar20927f22009-08-07 08:26:05 +0000201 StringRef getToken() const {
202 assert(Kind == Token && "Invalid access!");
203 return StringRef(Tok.Data, Tok.Length);
204 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000205 void setTokenValue(StringRef Value) {
206 assert(Kind == Token && "Invalid access!");
207 Tok.Data = Value.data();
208 Tok.Length = Value.size();
209 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000210
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000211 unsigned getReg() const {
212 assert(Kind == Register && "Invalid access!");
213 return Reg.RegNo;
214 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000215
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000216 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000217 assert(Kind == Immediate && "Invalid access!");
218 return Imm.Val;
219 }
220
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000221 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000222 assert(Kind == Memory && "Invalid access!");
223 return Mem.Disp;
224 }
225 unsigned getMemSegReg() const {
226 assert(Kind == Memory && "Invalid access!");
227 return Mem.SegReg;
228 }
229 unsigned getMemBaseReg() const {
230 assert(Kind == Memory && "Invalid access!");
231 return Mem.BaseReg;
232 }
233 unsigned getMemIndexReg() const {
234 assert(Kind == Memory && "Invalid access!");
235 return Mem.IndexReg;
236 }
237 unsigned getMemScale() const {
238 assert(Kind == Memory && "Invalid access!");
239 return Mem.Scale;
240 }
241
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000242 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000243
244 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000245
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000246 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000247 if (!isImm())
248 return false;
249
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000250 // If this isn't a constant expr, just assume it fits and let relaxation
251 // handle it.
252 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
253 if (!CE)
254 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000255
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000256 // Otherwise, check the value is in a range that makes sense for this
257 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000258 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000259 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000260 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000261 if (!isImm())
262 return false;
263
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000264 // If this isn't a constant expr, just assume it fits and let relaxation
265 // handle it.
266 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
267 if (!CE)
268 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000269
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000270 // Otherwise, check the value is in a range that makes sense for this
271 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000272 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000273 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000274 bool isImmZExtu32u8() const {
275 if (!isImm())
276 return false;
277
278 // If this isn't a constant expr, just assume it fits and let relaxation
279 // handle it.
280 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
281 if (!CE)
282 return true;
283
284 // Otherwise, check the value is in a range that makes sense for this
285 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000286 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000287 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000288 bool isImmSExti64i8() const {
289 if (!isImm())
290 return false;
291
292 // If this isn't a constant expr, just assume it fits and let relaxation
293 // handle it.
294 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
295 if (!CE)
296 return true;
297
298 // Otherwise, check the value is in a range that makes sense for this
299 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000300 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000301 }
302 bool isImmSExti64i32() const {
303 if (!isImm())
304 return false;
305
306 // If this isn't a constant expr, just assume it fits and let relaxation
307 // handle it.
308 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
309 if (!CE)
310 return true;
311
312 // Otherwise, check the value is in a range that makes sense for this
313 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000314 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000315 }
316
Chad Rosier96d58e62012-10-19 20:57:14 +0000317 unsigned getMemSize() const {
318 assert(Kind == Memory && "Invalid access!");
319 return Mem.Size;
320 }
321
322 bool needSizeDirective() const {
323 assert(Kind == Memory && "Invalid access!");
324 return Mem.NeedSizeDir;
325 }
326
Daniel Dunbar20927f22009-08-07 08:26:05 +0000327 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000328 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000329 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000330 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000331 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000332 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000333 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000334 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000335 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000336 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000337 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000338 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000339 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000340 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000341 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000342 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000343 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000344 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000345 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000346 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000347 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000348 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000349
Craig Topper75dc33a2012-07-18 04:11:12 +0000350 bool isMemVX32() const {
351 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
352 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
353 }
354 bool isMemVY32() const {
355 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
356 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
357 }
358 bool isMemVX64() const {
359 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
360 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
361 }
362 bool isMemVY64() const {
363 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
364 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
365 }
366
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000367 bool isAbsMem() const {
368 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000369 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000370 }
371
Daniel Dunbar20927f22009-08-07 08:26:05 +0000372 bool isReg() const { return Kind == Register; }
373
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000374 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
375 // Add as immediates when possible.
376 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
377 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
378 else
379 Inst.addOperand(MCOperand::CreateExpr(Expr));
380 }
381
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000382 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 assert(N == 1 && "Invalid number of operands!");
384 Inst.addOperand(MCOperand::CreateReg(getReg()));
385 }
386
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000387 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000388 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000389 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000390 }
391
Chad Rosier36b8fed2012-06-27 22:34:28 +0000392 void addMem8Operands(MCInst &Inst, unsigned N) const {
393 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000394 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000395 void addMem16Operands(MCInst &Inst, unsigned N) const {
396 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000397 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000398 void addMem32Operands(MCInst &Inst, unsigned N) const {
399 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000400 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000401 void addMem64Operands(MCInst &Inst, unsigned N) const {
402 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000403 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000404 void addMem80Operands(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 addMem128Operands(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 addMem256Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000412 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000413 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
415 }
416 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
417 addMemOperands(Inst, N);
418 }
419 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
420 addMemOperands(Inst, N);
421 }
422 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
423 addMemOperands(Inst, N);
424 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000425
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000426 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000427 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000428 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
429 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
430 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000431 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000432 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
433 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000434
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000435 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
436 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000437 // Add as immediates when possible.
438 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
439 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
440 else
441 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000442 }
443
Chris Lattnerb4307b32010-01-15 19:28:38 +0000444 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000445 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
446 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000447 Res->Tok.Data = Str.data();
448 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000449 return Res;
450 }
451
Chris Lattner29ef9a22010-01-15 18:51:29 +0000452 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000453 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000454 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000455 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000456 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000457
Chris Lattnerb4307b32010-01-15 19:28:38 +0000458 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
459 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000460 Res->Imm.Val = Val;
461 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000462 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000463
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000464 /// Create an absolute memory operand.
465 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Chad Rosier96d58e62012-10-19 20:57:14 +0000466 SMLoc EndLoc, unsigned Size = 0,
467 bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000468 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
469 Res->Mem.SegReg = 0;
470 Res->Mem.Disp = Disp;
471 Res->Mem.BaseReg = 0;
472 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000473 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000474 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000475 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000476 return Res;
477 }
478
479 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000480 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
481 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000482 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier96d58e62012-10-19 20:57:14 +0000483 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000484 // We should never just have a displacement, that should be parsed as an
485 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000486 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
487
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000488 // The scale should always be one of {1,2,4,8}.
489 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000490 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000491 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000492 Res->Mem.SegReg = SegReg;
493 Res->Mem.Disp = Disp;
494 Res->Mem.BaseReg = BaseReg;
495 Res->Mem.IndexReg = IndexReg;
496 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000497 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000498 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000499 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000500 }
501};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000502
Chris Lattner37dfdec2009-07-29 06:33:53 +0000503} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000504
Devang Pateldd929fc2012-01-12 18:03:40 +0000505bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000506 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000507
508 return (Op.isMem() &&
509 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
510 isa<MCConstantExpr>(Op.Mem.Disp) &&
511 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
512 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
513}
514
Devang Pateldd929fc2012-01-12 18:03:40 +0000515bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000516 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000517
Chad Rosier36b8fed2012-06-27 22:34:28 +0000518 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000519 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000520 isa<MCConstantExpr>(Op.Mem.Disp) &&
521 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
522 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
523}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000524
Devang Pateldd929fc2012-01-12 18:03:40 +0000525bool X86AsmParser::ParseRegister(unsigned &RegNo,
526 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000527 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000528 const AsmToken &PercentTok = Parser.getTok();
529 StartLoc = PercentTok.getLoc();
530
531 // If we encounter a %, ignore it. This code handles registers with and
532 // without the prefix, unprefixed registers can occur in cfi directives.
533 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000534 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000535
Sean Callanan18b83232010-01-19 21:44:56 +0000536 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000537 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000538 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000539 return Error(StartLoc, "invalid register name",
540 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000541 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000542
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000543 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000544
Chris Lattner33d60d52010-09-22 04:11:10 +0000545 // If the match failed, try the register name as lowercase.
546 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000547 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000548
Evan Cheng5de728c2011-07-27 23:22:03 +0000549 if (!is64BitMode()) {
550 // FIXME: This should be done using Requires<In32BitMode> and
551 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
552 // checked.
553 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
554 // REX prefix.
555 if (RegNo == X86::RIZ ||
556 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
557 X86II::isX86_64NonExtLowByteReg(RegNo) ||
558 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000559 return Error(StartLoc, "register %"
560 + Tok.getString() + " is only available in 64-bit mode",
561 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000562 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000563
Chris Lattner33d60d52010-09-22 04:11:10 +0000564 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
565 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000566 RegNo = X86::ST0;
567 EndLoc = Tok.getLoc();
568 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000569
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000570 // Check to see if we have '(4)' after %st.
571 if (getLexer().isNot(AsmToken::LParen))
572 return false;
573 // Lex the paren.
574 getParser().Lex();
575
576 const AsmToken &IntTok = Parser.getTok();
577 if (IntTok.isNot(AsmToken::Integer))
578 return Error(IntTok.getLoc(), "expected stack index");
579 switch (IntTok.getIntVal()) {
580 case 0: RegNo = X86::ST0; break;
581 case 1: RegNo = X86::ST1; break;
582 case 2: RegNo = X86::ST2; break;
583 case 3: RegNo = X86::ST3; break;
584 case 4: RegNo = X86::ST4; break;
585 case 5: RegNo = X86::ST5; break;
586 case 6: RegNo = X86::ST6; break;
587 case 7: RegNo = X86::ST7; break;
588 default: return Error(IntTok.getLoc(), "invalid stack index");
589 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000590
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000591 if (getParser().Lex().isNot(AsmToken::RParen))
592 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000593
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000594 EndLoc = Tok.getLoc();
595 Parser.Lex(); // Eat ')'
596 return false;
597 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000598
Chris Lattner645b2092010-06-24 07:29:18 +0000599 // If this is "db[0-7]", match it as an alias
600 // for dr[0-7].
601 if (RegNo == 0 && Tok.getString().size() == 3 &&
602 Tok.getString().startswith("db")) {
603 switch (Tok.getString()[2]) {
604 case '0': RegNo = X86::DR0; break;
605 case '1': RegNo = X86::DR1; break;
606 case '2': RegNo = X86::DR2; break;
607 case '3': RegNo = X86::DR3; break;
608 case '4': RegNo = X86::DR4; break;
609 case '5': RegNo = X86::DR5; break;
610 case '6': RegNo = X86::DR6; break;
611 case '7': RegNo = X86::DR7; break;
612 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000613
Chris Lattner645b2092010-06-24 07:29:18 +0000614 if (RegNo != 0) {
615 EndLoc = Tok.getLoc();
616 Parser.Lex(); // Eat it.
617 return false;
618 }
619 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000620
Devang Patel1aea4302012-01-20 22:32:05 +0000621 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000622 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000623 return Error(StartLoc, "invalid register name",
624 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000625 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000626
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000627 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000628 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000629 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000630}
631
Devang Pateldd929fc2012-01-12 18:03:40 +0000632X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000633 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000634 return ParseIntelOperand();
635 return ParseATTOperand();
636}
637
Devang Pateld37ad242012-01-17 18:00:18 +0000638/// getIntelMemOperandSize - Return intel memory operand size.
639static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000640 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000641 .Cases("BYTE", "byte", 8)
642 .Cases("WORD", "word", 16)
643 .Cases("DWORD", "dword", 32)
644 .Cases("QWORD", "qword", 64)
645 .Cases("XWORD", "xword", 80)
646 .Cases("XMMWORD", "xmmword", 128)
647 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000648 .Default(0);
649 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000650}
651
Devang Patel7c64fe62012-01-23 18:31:58 +0000652X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
653 unsigned Size) {
654 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000655 SMLoc Start = Parser.getTok().getLoc(), End;
656
Devang Pateld37ad242012-01-17 18:00:18 +0000657 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
658 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
659
660 // Eat '['
661 if (getLexer().isNot(AsmToken::LBrac))
662 return ErrorOperand(Start, "Expected '[' token!");
663 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000664
Devang Pateld37ad242012-01-17 18:00:18 +0000665 if (getLexer().is(AsmToken::Identifier)) {
666 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000667 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000668 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000669 if (getParser().ParseExpression(Disp, End)) return 0;
670 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000671 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000672 Parser.Lex();
673 return X86Operand::CreateMem(Disp, Start, End, Size);
674 }
675 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000676 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000677 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000678 SMLoc Loc = Parser.getTok().getLoc();
679 if (getLexer().is(AsmToken::RBrac)) {
680 // Handle '[' number ']'
681 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000682 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
683 if (SegReg)
684 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
685 Start, End, Size);
686 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000687 } else if (getLexer().is(AsmToken::Star)) {
688 // Handle '[' Scale*IndexReg ']'
689 Parser.Lex();
690 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000691 if (ParseRegister(IndexReg, IdxRegLoc, End))
692 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000693 Scale = Val;
694 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000695 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000696 }
697
698 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
699 bool isPlus = getLexer().is(AsmToken::Plus);
700 Parser.Lex();
701 SMLoc PlusLoc = Parser.getTok().getLoc();
702 if (getLexer().is(AsmToken::Integer)) {
703 int64_t Val = Parser.getTok().getIntVal();
704 Parser.Lex();
705 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000706 Parser.Lex();
707 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000708 if (ParseRegister(IndexReg, IdxRegLoc, End))
709 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000710 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000711 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000712 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000713 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000714 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000715 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000716 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000717 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000718 End = Parser.getTok().getLoc();
719 if (!IndexReg)
720 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000721 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000722 }
Devang Pateld37ad242012-01-17 18:00:18 +0000723 }
724
725 if (getLexer().isNot(AsmToken::RBrac))
726 if (getParser().ParseExpression(Disp, End)) return 0;
727
728 End = Parser.getTok().getLoc();
729 if (getLexer().isNot(AsmToken::RBrac))
730 return ErrorOperand(End, "expected ']' token!");
731 Parser.Lex();
732 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000733
734 // handle [-42]
735 if (!BaseReg && !IndexReg)
736 return X86Operand::CreateMem(Disp, Start, End, Size);
737
Devang Pateld37ad242012-01-17 18:00:18 +0000738 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000739 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000740}
741
742/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000743X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000744 const AsmToken &Tok = Parser.getTok();
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000745 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000746
747 unsigned Size = getIntelMemOperandSize(Tok.getString());
748 if (Size) {
749 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000750 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
751 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000752 Parser.Lex();
753 }
754
755 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000756 return ParseIntelBracExpression(SegReg, Size);
757
758 if (!ParseRegister(SegReg, Start, End)) {
759 // Handel SegReg : [ ... ]
760 if (getLexer().isNot(AsmToken::Colon))
761 return ErrorOperand(Start, "Expected ':' token!");
762 Parser.Lex(); // Eat :
763 if (getLexer().isNot(AsmToken::LBrac))
764 return ErrorOperand(Start, "Expected '[' token!");
765 return ParseIntelBracExpression(SegReg, Size);
766 }
Devang Pateld37ad242012-01-17 18:00:18 +0000767
768 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
769 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000770 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000771
772 bool NeedSizeDir = false;
773 if (!Size && isParsingInlineAsm()) {
774 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
775 const MCSymbol &Sym = SymRef->getSymbol();
776 // FIXME: The SemaLookup will fail if the name is anything other then an
777 // identifier.
778 // FIXME: Pass a valid SMLoc.
779 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
780 NeedSizeDir = Size > 0;
781 }
782 }
783 return X86Operand::CreateMem(Disp, Start, End, Size, NeedSizeDir);
Devang Pateld37ad242012-01-17 18:00:18 +0000784}
785
786X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000787 SMLoc Start = Parser.getTok().getLoc(), End;
788
789 // immediate.
790 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
791 getLexer().is(AsmToken::Minus)) {
792 const MCExpr *Val;
793 if (!getParser().ParseExpression(Val, End)) {
794 End = Parser.getTok().getLoc();
795 return X86Operand::CreateImm(Val, Start, End);
796 }
797 }
798
Devang Patel0a338862012-01-12 01:36:43 +0000799 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000800 unsigned RegNo = 0;
801 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000802 // If this is a segment register followed by a ':', then this is the start
803 // of a memory reference, otherwise this is a normal register reference.
804 if (getLexer().isNot(AsmToken::Colon))
805 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
806
807 getParser().Lex(); // Eat the colon.
808 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000809 }
810
811 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000812 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000813}
814
Devang Pateldd929fc2012-01-12 18:03:40 +0000815X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000816 switch (getLexer().getKind()) {
817 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000818 // Parse a memory operand with no segment register.
819 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000820 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000821 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000822 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000823 SMLoc Start, End;
824 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000825 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000826 Error(Start, "%eiz and %riz can only be used as index registers",
827 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000828 return 0;
829 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000830
Chris Lattnereef6d782010-04-17 18:56:34 +0000831 // If this is a segment register followed by a ':', then this is the start
832 // of a memory reference, otherwise this is a normal register reference.
833 if (getLexer().isNot(AsmToken::Colon))
834 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000835
836
Chris Lattnereef6d782010-04-17 18:56:34 +0000837 getParser().Lex(); // Eat the colon.
838 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000839 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000840 case AsmToken::Dollar: {
841 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000842 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000843 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000844 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000845 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000846 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000847 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000848 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000850}
851
Chris Lattnereef6d782010-04-17 18:56:34 +0000852/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
853/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000854X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000855
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000856 // We have to disambiguate a parenthesized expression "(4+5)" from the start
857 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000858 // only way to do this without lookahead is to eat the '(' and see what is
859 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000860 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000861 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000862 SMLoc ExprEnd;
863 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000864
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000865 // After parsing the base expression we could either have a parenthesized
866 // memory address or not. If not, return now. If so, eat the (.
867 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000868 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000869 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000870 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000871 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000873
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000874 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000875 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000876 } else {
877 // Okay, we have a '('. We don't know if this is an expression or not, but
878 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000879 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000880 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000882 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000883 // Nothing to do here, fall into the code below with the '(' part of the
884 // memory operand consumed.
885 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000886 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000887
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000888 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000889 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000890 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000891
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000892 // After parsing the base expression we could either have a parenthesized
893 // memory address or not. If not, return now. If so, eat the (.
894 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000895 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000896 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000897 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000898 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000899 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000900
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000901 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000902 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000903 }
904 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000905
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000906 // If we reached here, then we just ate the ( of the memory operand. Process
907 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000908 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000909 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000910
Chris Lattner29ef9a22010-01-15 18:51:29 +0000911 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000912 SMLoc StartLoc, EndLoc;
913 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000914 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000915 Error(StartLoc, "eiz and riz can only be used as index registers",
916 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000917 return 0;
918 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000919 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000920
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000921 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000922 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000923 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000924
925 // Following the comma we should have either an index register, or a scale
926 // value. We don't support the later form, but we want to parse it
927 // correctly.
928 //
929 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000930 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000931 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000932 SMLoc L;
933 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000934
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935 if (getLexer().isNot(AsmToken::RParen)) {
936 // Parse the scale amount:
937 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000938 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000939 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000940 "expected comma in scale expression");
941 return 0;
942 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000943 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000944
945 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000946 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000947
948 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000949 if (getParser().ParseAbsoluteExpression(ScaleVal)){
950 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000951 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000952 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000953
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000954 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000955 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
956 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
957 return 0;
958 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000959 Scale = (unsigned)ScaleVal;
960 }
961 }
962 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000963 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000964 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000965 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000966
967 int64_t Value;
968 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000969 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000970
Daniel Dunbaree910252010-08-24 19:13:38 +0000971 if (Value != 1)
972 Warning(Loc, "scale factor without index register is ignored");
973 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000974 }
975 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000976
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000977 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000978 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000979 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000980 return 0;
981 }
Sean Callanan18b83232010-01-19 21:44:56 +0000982 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000983 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000984
Kevin Enderby84faf652012-03-12 21:32:09 +0000985 // If we have both a base register and an index register make sure they are
986 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000987 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000988 if (BaseReg != 0 && IndexReg != 0) {
989 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000990 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
991 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000992 IndexReg != X86::RIZ) {
993 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
994 return 0;
995 }
996 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000997 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
998 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000999 IndexReg != X86::EIZ){
1000 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1001 return 0;
1002 }
1003 }
1004
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001005 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1006 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001007}
1008
Devang Pateldd929fc2012-01-12 18:03:40 +00001009bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001010ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001011 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001012 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001013
Chris Lattnerd8f71792010-11-28 20:23:50 +00001014 // FIXME: Hack to recognize setneb as setne.
1015 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1016 PatchedName != "setb" && PatchedName != "setnb")
1017 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001018
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001019 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1020 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001021 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001022 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1023 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001024 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001025 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001026 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001027 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001028 .Case("eq", 0x00)
1029 .Case("lt", 0x01)
1030 .Case("le", 0x02)
1031 .Case("unord", 0x03)
1032 .Case("neq", 0x04)
1033 .Case("nlt", 0x05)
1034 .Case("nle", 0x06)
1035 .Case("ord", 0x07)
1036 /* AVX only from here */
1037 .Case("eq_uq", 0x08)
1038 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001039 .Case("ngt", 0x0A)
1040 .Case("false", 0x0B)
1041 .Case("neq_oq", 0x0C)
1042 .Case("ge", 0x0D)
1043 .Case("gt", 0x0E)
1044 .Case("true", 0x0F)
1045 .Case("eq_os", 0x10)
1046 .Case("lt_oq", 0x11)
1047 .Case("le_oq", 0x12)
1048 .Case("unord_s", 0x13)
1049 .Case("neq_us", 0x14)
1050 .Case("nlt_uq", 0x15)
1051 .Case("nle_uq", 0x16)
1052 .Case("ord_s", 0x17)
1053 .Case("eq_us", 0x18)
1054 .Case("nge_uq", 0x19)
1055 .Case("ngt_uq", 0x1A)
1056 .Case("false_os", 0x1B)
1057 .Case("neq_os", 0x1C)
1058 .Case("ge_oq", 0x1D)
1059 .Case("gt_oq", 0x1E)
1060 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001061 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001062 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001063 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1064 getParser().getContext());
1065 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001066 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001067 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001068 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001069 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001070 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001071 } else {
1072 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001073 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001074 }
1075 }
1076 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001077
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001078 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001079
Devang Patel885f65b2012-01-30 22:47:12 +00001080 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001081 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001082
Chris Lattner2544f422010-09-08 05:17:37 +00001083 // Determine whether this is an instruction prefix.
1084 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001085 Name == "lock" || Name == "rep" ||
1086 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001087 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001088 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001089
1090
Chris Lattner2544f422010-09-08 05:17:37 +00001091 // This does the actual operand parsing. Don't parse any more if we have a
1092 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1093 // just want to parse the "lock" as the first instruction and the "incl" as
1094 // the next one.
1095 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001096
1097 // Parse '*' modifier.
1098 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001099 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001100 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001101 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001102 }
1103
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001104 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001105 if (X86Operand *Op = ParseOperand())
1106 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001107 else {
1108 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001109 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001110 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001111
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001112 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001113 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001114
1115 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001116 if (X86Operand *Op = ParseOperand())
1117 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001118 else {
1119 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001120 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001121 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001122 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001123
Chris Lattnercbf8a982010-09-11 16:18:25 +00001124 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001125 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001126 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001127 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001128 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001129 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001130
Chris Lattner2544f422010-09-08 05:17:37 +00001131 if (getLexer().is(AsmToken::EndOfStatement))
1132 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001133 else if (isPrefix && getLexer().is(AsmToken::Slash))
1134 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001135
Devang Patel885f65b2012-01-30 22:47:12 +00001136 if (ExtraImmOp && isParsingIntelSyntax())
1137 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1138
Chris Lattner98c870f2010-11-06 19:25:43 +00001139 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1140 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1141 // documented form in various unofficial manuals, so a lot of code uses it.
1142 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1143 Operands.size() == 3) {
1144 X86Operand &Op = *(X86Operand*)Operands.back();
1145 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1146 isa<MCConstantExpr>(Op.Mem.Disp) &&
1147 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1148 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1149 SMLoc Loc = Op.getEndLoc();
1150 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1151 delete &Op;
1152 }
1153 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001154 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1155 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1156 Operands.size() == 3) {
1157 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1158 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1159 isa<MCConstantExpr>(Op.Mem.Disp) &&
1160 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1161 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1162 SMLoc Loc = Op.getEndLoc();
1163 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1164 delete &Op;
1165 }
1166 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001167 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1168 if (Name.startswith("ins") && Operands.size() == 3 &&
1169 (Name == "insb" || Name == "insw" || Name == "insl")) {
1170 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1171 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1172 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1173 Operands.pop_back();
1174 Operands.pop_back();
1175 delete &Op;
1176 delete &Op2;
1177 }
1178 }
1179
1180 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1181 if (Name.startswith("outs") && Operands.size() == 3 &&
1182 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1183 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1184 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1185 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1186 Operands.pop_back();
1187 Operands.pop_back();
1188 delete &Op;
1189 delete &Op2;
1190 }
1191 }
1192
1193 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1194 if (Name.startswith("movs") && Operands.size() == 3 &&
1195 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001196 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001197 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1198 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1199 if (isSrcOp(Op) && isDstOp(Op2)) {
1200 Operands.pop_back();
1201 Operands.pop_back();
1202 delete &Op;
1203 delete &Op2;
1204 }
1205 }
1206 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1207 if (Name.startswith("lods") && Operands.size() == 3 &&
1208 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001209 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001210 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1211 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1212 if (isSrcOp(*Op1) && Op2->isReg()) {
1213 const char *ins;
1214 unsigned reg = Op2->getReg();
1215 bool isLods = Name == "lods";
1216 if (reg == X86::AL && (isLods || Name == "lodsb"))
1217 ins = "lodsb";
1218 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1219 ins = "lodsw";
1220 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1221 ins = "lodsl";
1222 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1223 ins = "lodsq";
1224 else
1225 ins = NULL;
1226 if (ins != NULL) {
1227 Operands.pop_back();
1228 Operands.pop_back();
1229 delete Op1;
1230 delete Op2;
1231 if (Name != ins)
1232 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1233 }
1234 }
1235 }
1236 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1237 if (Name.startswith("stos") && Operands.size() == 3 &&
1238 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001239 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001240 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1241 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1242 if (isDstOp(*Op2) && Op1->isReg()) {
1243 const char *ins;
1244 unsigned reg = Op1->getReg();
1245 bool isStos = Name == "stos";
1246 if (reg == X86::AL && (isStos || Name == "stosb"))
1247 ins = "stosb";
1248 else if (reg == X86::AX && (isStos || Name == "stosw"))
1249 ins = "stosw";
1250 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1251 ins = "stosl";
1252 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1253 ins = "stosq";
1254 else
1255 ins = NULL;
1256 if (ins != NULL) {
1257 Operands.pop_back();
1258 Operands.pop_back();
1259 delete Op1;
1260 delete Op2;
1261 if (Name != ins)
1262 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1263 }
1264 }
1265 }
1266
Chris Lattnere9e16a32010-09-15 04:33:27 +00001267 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001268 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001269 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001270 Name.startswith("shl") || Name.startswith("sal") ||
1271 Name.startswith("rcl") || Name.startswith("rcr") ||
1272 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001273 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001274 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001275 // Intel syntax
1276 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1277 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001278 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1279 delete Operands[2];
1280 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001281 }
1282 } else {
1283 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1284 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001285 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1286 delete Operands[1];
1287 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001288 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001289 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001290 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001291
Chris Lattner15f89512011-04-09 19:41:05 +00001292 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1293 // instalias with an immediate operand yet.
1294 if (Name == "int" && Operands.size() == 2) {
1295 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1296 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1297 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1298 delete Operands[1];
1299 Operands.erase(Operands.begin() + 1);
1300 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1301 }
1302 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001303
Chris Lattner98986712010-01-14 22:21:20 +00001304 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001305}
1306
Devang Pateldd929fc2012-01-12 18:03:40 +00001307bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001308processInstruction(MCInst &Inst,
1309 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1310 switch (Inst.getOpcode()) {
1311 default: return false;
1312 case X86::AND16i16: {
1313 if (!Inst.getOperand(0).isImm() ||
1314 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1315 return false;
1316
1317 MCInst TmpInst;
1318 TmpInst.setOpcode(X86::AND16ri8);
1319 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1320 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1321 TmpInst.addOperand(Inst.getOperand(0));
1322 Inst = TmpInst;
1323 return true;
1324 }
1325 case X86::AND32i32: {
1326 if (!Inst.getOperand(0).isImm() ||
1327 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1328 return false;
1329
1330 MCInst TmpInst;
1331 TmpInst.setOpcode(X86::AND32ri8);
1332 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1333 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1334 TmpInst.addOperand(Inst.getOperand(0));
1335 Inst = TmpInst;
1336 return true;
1337 }
1338 case X86::AND64i32: {
1339 if (!Inst.getOperand(0).isImm() ||
1340 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1341 return false;
1342
1343 MCInst TmpInst;
1344 TmpInst.setOpcode(X86::AND64ri8);
1345 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1346 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1347 TmpInst.addOperand(Inst.getOperand(0));
1348 Inst = TmpInst;
1349 return true;
1350 }
Devang Patelac0f0482012-01-19 17:53:25 +00001351 case X86::XOR16i16: {
1352 if (!Inst.getOperand(0).isImm() ||
1353 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1354 return false;
1355
1356 MCInst TmpInst;
1357 TmpInst.setOpcode(X86::XOR16ri8);
1358 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1359 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1360 TmpInst.addOperand(Inst.getOperand(0));
1361 Inst = TmpInst;
1362 return true;
1363 }
1364 case X86::XOR32i32: {
1365 if (!Inst.getOperand(0).isImm() ||
1366 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1367 return false;
1368
1369 MCInst TmpInst;
1370 TmpInst.setOpcode(X86::XOR32ri8);
1371 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1372 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1373 TmpInst.addOperand(Inst.getOperand(0));
1374 Inst = TmpInst;
1375 return true;
1376 }
1377 case X86::XOR64i32: {
1378 if (!Inst.getOperand(0).isImm() ||
1379 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1380 return false;
1381
1382 MCInst TmpInst;
1383 TmpInst.setOpcode(X86::XOR64ri8);
1384 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1385 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1386 TmpInst.addOperand(Inst.getOperand(0));
1387 Inst = TmpInst;
1388 return true;
1389 }
1390 case X86::OR16i16: {
1391 if (!Inst.getOperand(0).isImm() ||
1392 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1393 return false;
1394
1395 MCInst TmpInst;
1396 TmpInst.setOpcode(X86::OR16ri8);
1397 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1398 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1399 TmpInst.addOperand(Inst.getOperand(0));
1400 Inst = TmpInst;
1401 return true;
1402 }
1403 case X86::OR32i32: {
1404 if (!Inst.getOperand(0).isImm() ||
1405 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1406 return false;
1407
1408 MCInst TmpInst;
1409 TmpInst.setOpcode(X86::OR32ri8);
1410 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1411 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1412 TmpInst.addOperand(Inst.getOperand(0));
1413 Inst = TmpInst;
1414 return true;
1415 }
1416 case X86::OR64i32: {
1417 if (!Inst.getOperand(0).isImm() ||
1418 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1419 return false;
1420
1421 MCInst TmpInst;
1422 TmpInst.setOpcode(X86::OR64ri8);
1423 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1424 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1425 TmpInst.addOperand(Inst.getOperand(0));
1426 Inst = TmpInst;
1427 return true;
1428 }
1429 case X86::CMP16i16: {
1430 if (!Inst.getOperand(0).isImm() ||
1431 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1432 return false;
1433
1434 MCInst TmpInst;
1435 TmpInst.setOpcode(X86::CMP16ri8);
1436 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1437 TmpInst.addOperand(Inst.getOperand(0));
1438 Inst = TmpInst;
1439 return true;
1440 }
1441 case X86::CMP32i32: {
1442 if (!Inst.getOperand(0).isImm() ||
1443 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1444 return false;
1445
1446 MCInst TmpInst;
1447 TmpInst.setOpcode(X86::CMP32ri8);
1448 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1449 TmpInst.addOperand(Inst.getOperand(0));
1450 Inst = TmpInst;
1451 return true;
1452 }
1453 case X86::CMP64i32: {
1454 if (!Inst.getOperand(0).isImm() ||
1455 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1456 return false;
1457
1458 MCInst TmpInst;
1459 TmpInst.setOpcode(X86::CMP64ri8);
1460 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1461 TmpInst.addOperand(Inst.getOperand(0));
1462 Inst = TmpInst;
1463 return true;
1464 }
Devang Patela951f772012-01-19 18:40:55 +00001465 case X86::ADD16i16: {
1466 if (!Inst.getOperand(0).isImm() ||
1467 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1468 return false;
1469
1470 MCInst TmpInst;
1471 TmpInst.setOpcode(X86::ADD16ri8);
1472 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1473 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1474 TmpInst.addOperand(Inst.getOperand(0));
1475 Inst = TmpInst;
1476 return true;
1477 }
1478 case X86::ADD32i32: {
1479 if (!Inst.getOperand(0).isImm() ||
1480 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1481 return false;
1482
1483 MCInst TmpInst;
1484 TmpInst.setOpcode(X86::ADD32ri8);
1485 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1486 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1487 TmpInst.addOperand(Inst.getOperand(0));
1488 Inst = TmpInst;
1489 return true;
1490 }
1491 case X86::ADD64i32: {
1492 if (!Inst.getOperand(0).isImm() ||
1493 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1494 return false;
1495
1496 MCInst TmpInst;
1497 TmpInst.setOpcode(X86::ADD64ri8);
1498 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1499 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1500 TmpInst.addOperand(Inst.getOperand(0));
1501 Inst = TmpInst;
1502 return true;
1503 }
1504 case X86::SUB16i16: {
1505 if (!Inst.getOperand(0).isImm() ||
1506 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1507 return false;
1508
1509 MCInst TmpInst;
1510 TmpInst.setOpcode(X86::SUB16ri8);
1511 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1512 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1513 TmpInst.addOperand(Inst.getOperand(0));
1514 Inst = TmpInst;
1515 return true;
1516 }
1517 case X86::SUB32i32: {
1518 if (!Inst.getOperand(0).isImm() ||
1519 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1520 return false;
1521
1522 MCInst TmpInst;
1523 TmpInst.setOpcode(X86::SUB32ri8);
1524 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1525 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1526 TmpInst.addOperand(Inst.getOperand(0));
1527 Inst = TmpInst;
1528 return true;
1529 }
1530 case X86::SUB64i32: {
1531 if (!Inst.getOperand(0).isImm() ||
1532 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1533 return false;
1534
1535 MCInst TmpInst;
1536 TmpInst.setOpcode(X86::SUB64ri8);
1537 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1538 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1539 TmpInst.addOperand(Inst.getOperand(0));
1540 Inst = TmpInst;
1541 return true;
1542 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001543 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001544}
1545
1546bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001547MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001548 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001549 MCStreamer &Out, unsigned &ErrorInfo,
1550 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001551 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001552 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1553 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001554 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001555
Chad Rosier127f5ed2012-10-15 19:08:18 +00001556 // Clear the opcode.
1557 Opcode = ~0x0;
1558
Chris Lattner7c51a312010-09-29 01:50:45 +00001559 // First, handle aliases that expand to multiple instructions.
1560 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001561 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001562 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001563 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001564 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001565 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001566 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001567 MCInst Inst;
1568 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001569 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001570 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001571 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001572
Chris Lattner0bb83a82010-09-30 16:39:29 +00001573 const char *Repl =
1574 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001575 .Case("finit", "fninit")
1576 .Case("fsave", "fnsave")
1577 .Case("fstcw", "fnstcw")
1578 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001579 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001580 .Case("fstsw", "fnstsw")
1581 .Case("fstsww", "fnstsw")
1582 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001583 .Default(0);
1584 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001585 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001586 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001587 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001588
Chris Lattnera008e8a2010-09-06 21:54:15 +00001589 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001590 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001591
Daniel Dunbarc918d602010-05-04 16:12:42 +00001592 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001593 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001594 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001595 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001596 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001597 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001598 // Some instructions need post-processing to, for example, tweak which
1599 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001600 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001601 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001602 while (processInstruction(Inst, Operands))
1603 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001604
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001605 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001606 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001607 Out.EmitInstruction(Inst);
1608 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001609 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001610 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001611 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001612 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001613 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001614 case Match_InvalidOperand:
1615 WasOriginallyInvalidOperand = true;
1616 break;
1617 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001618 break;
1619 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001620
Daniel Dunbarc918d602010-05-04 16:12:42 +00001621 // FIXME: Ideally, we would only attempt suffix matches for things which are
1622 // valid prefixes, and we could just infer the right unambiguous
1623 // type. However, that requires substantially more matcher support than the
1624 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001625
Daniel Dunbarc918d602010-05-04 16:12:42 +00001626 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001627 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001628 SmallString<16> Tmp;
1629 Tmp += Base;
1630 Tmp += ' ';
1631 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001632
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001633 // If this instruction starts with an 'f', then it is a floating point stack
1634 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1635 // 80-bit floating point, which use the suffixes s,l,t respectively.
1636 //
1637 // Otherwise, we assume that this may be an integer instruction, which comes
1638 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1639 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001640
Daniel Dunbarc918d602010-05-04 16:12:42 +00001641 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001642 Tmp[Base.size()] = Suffixes[0];
1643 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001644 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001645
Chad Rosier6e006d32012-10-12 22:53:36 +00001646 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1647 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001648 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001649 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1650 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001651 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001652 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1653 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001654 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001655 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1656 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001657
1658 // Restore the old token.
1659 Op->setTokenValue(Base);
1660
1661 // If exactly one matched, then we treat that as a successful match (and the
1662 // instruction will already have been filled in correctly, since the failing
1663 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001664 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001665 (Match1 == Match_Success) + (Match2 == Match_Success) +
1666 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001667 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001668 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001669 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001670 Out.EmitInstruction(Inst);
1671 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001672 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001673 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001674
Chris Lattnerec6789f2010-09-06 20:08:02 +00001675 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001676
Daniel Dunbar09062b12010-08-12 00:55:42 +00001677 // If we had multiple suffix matches, then identify this as an ambiguous
1678 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001679 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001680 char MatchChars[4];
1681 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001682 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1683 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1684 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1685 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001686
1687 SmallString<126> Msg;
1688 raw_svector_ostream OS(Msg);
1689 OS << "ambiguous instructions require an explicit suffix (could be ";
1690 for (unsigned i = 0; i != NumMatches; ++i) {
1691 if (i != 0)
1692 OS << ", ";
1693 if (i + 1 == NumMatches)
1694 OS << "or ";
1695 OS << "'" << Base << MatchChars[i] << "'";
1696 }
1697 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001698 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001699 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001700 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001701
Chris Lattnera008e8a2010-09-06 21:54:15 +00001702 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001703
Chris Lattnera008e8a2010-09-06 21:54:15 +00001704 // If all of the instructions reported an invalid mnemonic, then the original
1705 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001706 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1707 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001708 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001709 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001710 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001711 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001712 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001713 }
1714
1715 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001716 if (ErrorInfo != ~0U) {
1717 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001718 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001719 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001720
Chad Rosier84125ca2012-10-13 00:26:04 +00001721 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001722 if (Operand->getStartLoc().isValid()) {
1723 SMRange OperandRange = Operand->getLocRange();
1724 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001725 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001726 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001727 }
1728
Chad Rosierb4fdade2012-08-21 19:36:59 +00001729 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001730 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001731 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001732
Chris Lattnerec6789f2010-09-06 20:08:02 +00001733 // If one instruction matched with a missing feature, report this as a
1734 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001735 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1736 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001737 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001738 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001739 return true;
1740 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001741
Chris Lattnera008e8a2010-09-06 21:54:15 +00001742 // If one instruction matched with an invalid operand, report this as an
1743 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001744 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1745 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001746 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001747 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001748 return true;
1749 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001750
Chris Lattnerec6789f2010-09-06 20:08:02 +00001751 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001752 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001753 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001754 return true;
1755}
1756
1757
Devang Pateldd929fc2012-01-12 18:03:40 +00001758bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001759 StringRef IDVal = DirectiveID.getIdentifier();
1760 if (IDVal == ".word")
1761 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001762 else if (IDVal.startswith(".code"))
1763 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001764 else if (IDVal.startswith(".att_syntax")) {
1765 getParser().setAssemblerDialect(0);
1766 return false;
1767 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001768 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001769 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1770 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001771 // FIXME : Handle noprefix
1772 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001773 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001774 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001775 }
1776 return false;
1777 }
Chris Lattner537ca842010-10-30 17:38:55 +00001778 return true;
1779}
1780
1781/// ParseDirectiveWord
1782/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001783bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001784 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1785 for (;;) {
1786 const MCExpr *Value;
1787 if (getParser().ParseExpression(Value))
1788 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001789
Chris Lattner537ca842010-10-30 17:38:55 +00001790 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001791
Chris Lattner537ca842010-10-30 17:38:55 +00001792 if (getLexer().is(AsmToken::EndOfStatement))
1793 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001794
Chris Lattner537ca842010-10-30 17:38:55 +00001795 // FIXME: Improve diagnostic.
1796 if (getLexer().isNot(AsmToken::Comma))
1797 return Error(L, "unexpected token in directive");
1798 Parser.Lex();
1799 }
1800 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001801
Chris Lattner537ca842010-10-30 17:38:55 +00001802 Parser.Lex();
1803 return false;
1804}
1805
Evan Chengbd27f5a2011-07-27 00:38:12 +00001806/// ParseDirectiveCode
1807/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001808bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001809 if (IDVal == ".code32") {
1810 Parser.Lex();
1811 if (is64BitMode()) {
1812 SwitchMode();
1813 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1814 }
1815 } else if (IDVal == ".code64") {
1816 Parser.Lex();
1817 if (!is64BitMode()) {
1818 SwitchMode();
1819 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1820 }
1821 } else {
1822 return Error(L, "unexpected directive " + IDVal);
1823 }
Chris Lattner537ca842010-10-30 17:38:55 +00001824
Evan Chengbd27f5a2011-07-27 00:38:12 +00001825 return false;
1826}
Chris Lattner537ca842010-10-30 17:38:55 +00001827
1828
Sean Callanane88f5522010-01-23 02:43:15 +00001829extern "C" void LLVMInitializeX86AsmLexer();
1830
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001831// Force static initialization.
1832extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001833 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1834 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001835 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001836}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001837
Chris Lattner0692ee62010-09-06 19:11:01 +00001838#define GET_REGISTER_MATCHER
1839#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001840#include "X86GenAsmMatcher.inc"